source: src/ASM/CPP2011/cpp-2011.tex @ 1022

Last change on this file since 1022 was 1022, checked in by sacerdot, 9 years ago

...

File size: 53.5 KB
RevLine 
[917]1\documentclass{llncs}
2
[960]3\usepackage{amsfonts}
[952]4\usepackage{amsmath}
[960]5\usepackage{amssymb} 
[917]6\usepackage[english]{babel}
[960]7\usepackage{color}
8\usepackage{fancybox}
9\usepackage{graphicx}
[917]10\usepackage[colorlinks]{hyperref}
[969]11\usepackage{hyphenat}
[957]12\usepackage[utf8x]{inputenc}
13\usepackage{listings}
[960]14\usepackage{mdwlist}
15\usepackage{microtype}
16\usepackage{stmaryrd}
17\usepackage{url}
[917]18
[960]19\renewcommand{\verb}{\lstinline}
20\def\lstlanguagefiles{lst-grafite.tex}
21\lstset{language=Grafite}
22
[957]23\newlength{\mylength}
24\newenvironment{frametxt}%
25        {\setlength{\fboxsep}{5pt}
26                \setlength{\mylength}{\linewidth}%
27                \addtolength{\mylength}{-2\fboxsep}%
28                \addtolength{\mylength}{-2\fboxrule}%
29                \Sbox
30                \minipage{\mylength}%
31                        \setlength{\abovedisplayskip}{0pt}%
32                        \setlength{\belowdisplayskip}{0pt}%
33                }%
34                {\endminipage\endSbox
35                        \[\fbox{\TheSbox}\]}
36
[997]37\title{On the correctness of an assembler for the Intel MCS-51 microprocessor\thanks{The project CerCo acknowledges the financial support of the Future and Emerging Technologies (FET) programme within the Seventh Framework Programme for Research of the European Commission, under FET-Open grant number: 243881}}
38\author{Dominic P. Mulligan \and Claudio Sacerdoti Coen}
[927]39\institute{Dipartimento di Scienze dell'Informazione, Universit\'a di Bologna}
[918]40
[927]41\bibliographystyle{splncs03}
42
[917]43\begin{document}
44
45\maketitle
46
47\begin{abstract}
[983]48We consider the formalisation of an assembler for Intel MCS-51 assembly language in the Matita proof assistant.
[952]49This formalisation forms a major component of the EU-funded CerCo project, concering the construction and formalisation of a concrete complexity preserving compiler for a large subset of the C programming language.
[983]50
51The efficient expansion of pseudoinstructions---particularly jumps---into MCS-51 machine instructions is complex.
52We employ a strategy, involving the use of `policies', that separates the decision making over how jumps should be expanded from the expansion process itself.
53This makes the proof of correctness for the assembler significantly more straightforward.
54
[996]55We prove, under the assumption of the existence of a correct policy, that the assembly process never fails and preserves the semantics of a subset of assembly programs.
[983]56Correct policies fail to exist only in a limited number of pathological circumstances.
[999]57Our assembler is complete with respect to the choice of policy.
58
59Surprisingly, we observe that it is impossible for an optimising assembler to preserve the semantics of every assembly program.
[917]60\end{abstract}
61
[918]62% ---------------------------------------------------------------------------- %
63% SECTION                                                                      %
64% ---------------------------------------------------------------------------- %
65\section{Introduction}
66\label{sect.introduction}
67
[953]68We consider the formalisation of an assembler for the Intel MCS-51 8-bit microprocessor in the Matita proof assistant~\cite{asperti:user:2007}.
69This formalisation forms a major component of the EU-funded CerCo project~\cite{cerco:2011}, concering the construction and formalisation of a concrete complexity preserving compiler for a large subset of the C programming language.
[947]70
[1017]71The MCS-51 dates from the early 1980s and is commonly called the 8051/8052.\footnote{Being strict, the 8051 and 8052 are two different microprocessors, though the features that the 8052 added over the 8051 are minor, and largely irrelevant for our formalisation project.}
[953]72Despite the microprocessor's age, derivatives are still widely manufactured by a number of semiconductor foundries.
73As a result the processor is widely used, especially in embedded systems development, where well-tested, cheap, predictable microprocessors find their niche.
[927]74
[952]75The MCS-51 has a relative paucity of features compared to its more modern brethren.
76In particular, the MCS-51 does not possess a cache or any instruction pipelining that would make predicting the concrete cost of executing a single instruction an involved process.
77Instead, each semiconductor foundry that produces an MCS-51 derivative is able to provide accurate timing information in clock cycles for each instruction in their derivative's instruction set.
[1017]78It is important to stress that this timing information, unlike in more sophisticated processors, is not an estimate, it is a `definition'.
[953]79For the MCS-51, if a manufacturer states that a particular opcode takes three clock cycles to execute, then that opcode \emph{always} takes three clock cycles to execute.
[927]80
[952]81This predicability of timing information is especially attractive to the CerCo consortium.
[1017]82We are in the process of constructing a certified, concrete complexity compiler for a realistic processor, and not for building and formalising the worst case execution time tools (WCET---see~\cite{yan:wcet:2008} and~\cite{bate:wcet:2011}, amongst many others, for an application of WCET technology to microprocessors with more complex designs) that would be necessary to achieve the same result with, for example, a modern ARM or PowerPC microprocessor.
[953]83
[1017]84As in most things, what one hand giveth, the other taketh away: the MCS-51's paucity of features, though an advantage in many respects, also quickly become a hindrance, and successfully compiling high-level code for this architecture is a cumbrous and involved process.
85In particular, the MCS-51 features a relatively miniscule series of memory spaces (including read-only code memory, stack and internal/external random access memory) by modern standards.
86As a result our C compiler, to have any sort of hope of successfully compiling realistic programs for embedded devices, ought to produce `tight' machine code.
[995]87This is not simple and requires the use of optimisations.
[927]88
[996]89For example, the MCS-51 features three unconditional jump instructions: \texttt{LJMP} and \texttt{SJMP}---`long jump' and `short jump' respectively---and an 11-bit oddity of the MCS-51, \texttt{AJMP}.
[1017]90Each of these three instructions expects arguments in different sizes and behaves in markedly different ways: \texttt{SJMP} may only perform a `local jump'; \texttt{LJMP} may jump to any address in the MCS-51's memory space and \texttt{AJMP} may jump to any address in the current memory page.
91Consequently, the size of each opcode is different, and to squeeze as much code as possible into the MCS-51's limited code memory, the smallest possible opcode that will suffice should be selected.
[927]92
[1017]93The prototype CerCo C compiler does not attempt to select the smallest jump opcode in this manner, as this was thought to unneccessarily complicate the compilation chain, making the eventual translation and formalisation of the compiler into Matita much harder.
94Instead, the compiler targets a bespoke assembly language, similar to `real world' assembly languages, complete with pseudoinstructions including \texttt{Jmp} and \texttt{Call} instructions.
[953]95Labels, conditional jumps to labels, a program preamble containing global data and a \texttt{MOV} instruction for moving this global data into the MCS-51's one 16-bit register also feature.
[952]96This latter feature will ease any later consideration of separate compilation in the CerCo compiler.
[953]97An assembler is used to expand pseudoinstructions into MCS-51 machine code.
[927]98
[953]99However, this assembly process is not trivial, for numerous reasons.
100For example, our conditional jumps to labels behave differently from their machine code counterparts.
[995]101At the machine code level, all conditional jumps are `short', limiting their range.
[953]102However, at the assembly level, conditional jumps may jump to a label that appears anywhere in the program, significantly liberalising the use of conditional jumps and further simplifying the design of the CerCo compiler.
[952]103
[953]104Further, trying to na\"ively relate assembly programs with their machine code counterparts simply does not work.
[995]105Machine code programs that fetch from constant addresses in code memory or programs that combine the program counter with constant shifts do not make sense at the assembly level, since the position of instructions in code memory will be known only after assembly and optimisation.
[952]106More generally, memory addresses can only be compared with other memory addresses.
107However, checking that memory addresses are only compared against each other at the assembly level is in fact undecidable.
[1017]108In short, we come to the shocking\footnote{For us, anyway.} realisation that, with optimisations, the full preservation of the semantics of all assembly programs is impossible.
[995]109We believe that this revelation is significant for large formalisation projects that assume the existence of a correct assembler.
[1017]110Projects in this class include both the recent CompCert~\cite{compcert:2011,leroy:formal:2009} and seL4 formalisations~\cite{klein:sel4:2009,klein:sel4:2010}.
[952]111
[995]112Yet, the situation is even more complex than having to expand pseudoinstructions correctly.
113In particular, when formalising the assembler, we must make sure that the assembly process does not change the timing characteristics of an assembly program for two reasons.
[1017]114
[995]115First, the semantics of some functions of the MCS-51, notably I/O, depend on the microprocessor's clock.
116Changing how long a particular program takes to execute can affect the semantics of a program.
117This is undesirable.
118
119Second, as mentioned, the CerCo consortium is in the business of constructing a verified compiler for the C programming language.
120However, unlike CompCert~\cite{compcert:2011,leroy:formal:2009,leroy:formally:2009}---which currently represents the state of the art for `industrial grade' verified compilers---CerCo considers not just the \emph{extensional correctness} of the compiler, but also its \emph{intensional correctness}.
[952]121That is, CompCert focusses solely on the preservation of the \emph{meaning} of a program during the compilation process, guaranteeing that the program's meaning does not change as it is gradually transformed into assembly code.
122However in any realistic compiler (even the CompCert compiler!) there is no guarantee that the program's time properties are preserved during the compilation process; a compiler's `optimisations' could, in theory, even conspire to degrade the concrete complexity of certain classes of programs.
123CerCo aims to expand the current state of the art by producing a compiler where this temporal degradation is guaranteed not to happen.
[1017]124Moreover, CerCo's approach lifts a program's timing information to the source (C language) level.
125This has the advantage of allowing a programmer to reason about a program's intensional properties directly on the source code that they write, not on the code that the compiler produces.
[952]126
[1017]127In order to achieve this, CerCo imposes a cost model on programs or, more specifically, on simple blocks of instructions.
[954]128This cost model is induced by the compilation process itself, and its non-compositional nature allows us to assign different costs to identical blocks of instructions depending on how they are compiled.
129In short, we aim to obtain a very precise costing for a program by embracing the compilation process, not ignoring it.
[1017]130This, however, complicates the proof of correctness for the compiler proper: for every translation pass from intermediate language to intermediate language, we must prove that not only has the meaning of a program been preserved, but also its concrete complexity characteristics.
[952]131This also applies for the translation from assembly language to machine code.
[954]132
[1017]133Naturally, this raises a question: how do we assign an \emph{accurate} cost to a pseudoinstruction?
[952]134As mentioned, conditional jumps at the assembly level can jump to a label appearing anywhere in the program.
[954]135However, at the machine code level, conditional jumps are limited to jumping `locally', using a measly byte offset.
[1017]136To translate a jump to a label, a single conditional jump pseudoinstruction may be translated into a block of three real instructions as follows (here, \texttt{JZ} is `jump if accumulator is zero'):
[952]137\begin{displaymath}
138\begin{array}{r@{\quad}l@{\;\;}l@{\qquad}c@{\qquad}l@{\;\;}l}
[1017]139       & \mathtt{JZ}  & \mathtt{label}                      &                 & \mathtt{JZ}   & \text{size of \texttt{SJMP} instruction} \\
[952]140       & \ldots       &                            & \text{translates to}   & \mathtt{SJMP} & \text{size of \texttt{LJMP} instruction} \\
[1017]141\mathtt{label:} & \mathtt{MOV} & \mathtt{A}\;\;\mathtt{B}   & \Longrightarrow & \mathtt{LJMP} & \text{address of \textit{label}} \\
[952]142       &              &                            &                 & \ldots        & \\
143       &              &                            &                 & \mathtt{MOV}  & \mathtt{A}\;\;\mathtt{B}
144\end{array}
145\end{displaymath}
146In the translation, if \texttt{JZ} fails, we fall through to the \texttt{SJMP} which jumps over the \texttt{LJMP}.
[1017]147Naturally, if \texttt{label} is close enough, a conditional jump pseudoinstruction is mapped directly to a conditional jump machine instruction; the above translation only applies if \texttt{label} is not sufficiently local.
[952]148This leaves the problem, addressed below, of calculating whether a label is indeed `close enough' for the simpler translation to be used.
149
150Crucially, the above translation demonstrates the difficulty in predicting how many clock cycles a pseudoinstruction will take to execute.
151A conditional jump may be mapped to a single machine instruction or a block of three.
[1017]152Perhaps more insidious is the realisation that the number of cycles needed to execute the instructions in the two branches of a translated conditional jump may be different.
[954]153Depending on the particular MCS-51 derivative at hand, an \texttt{SJMP} could in theory take a different number of clock cycles to execute than an \texttt{LJMP}.
[1017]154These issues must also be dealt with in order to prove that the translation pass preserves the concrete complexity of assembly code, and that the semantics of a program using the MCS-51's I/O facilities does not change.
[997]155We address this problem by parameterizing the semantics over a cost model.
156We prove the preservation of concrete complexity only for the program-dependent cost model induced by the optimisation.
[952]157
[1017]158Yet one more question remains: how do we decide whether to expand a jump into an \texttt{SJMP} or an \texttt{LJMP}?
159To understand, again, why this problem is not trivial, consider the following snippet of assembly code:
[952]160\begin{displaymath}
[999]161\begin{array}{r@{\qquad}r@{\quad}l@{\;\;}l@{\qquad}l}
[1001]162\text{1:} & \mathtt{(0x000)}  & \texttt{LJMP} & \texttt{0x100}  & \text{\texttt{;; Jump forward 256.}} \\
[999]163\text{2:} & \mathtt{...}    & \mathtt{...}  &                 &                                               \\
[1001]164\text{3:} & \mathtt{(0x0FA)}  & \texttt{LJMP} & \texttt{0x100}  & \text{\texttt{;; Jump forward 256.}} \\
[999]165\text{4:} & \mathtt{...}    & \mathtt{...}  &                 &                                               \\
[1004]166\text{5:} & \mathtt{(0x100)}  & \texttt{LJMP} & \texttt{-0x100}  & \text{\texttt{;; Jump backward 256.}} \\
[999]167\end{array}
[952]168\end{displaymath}
[999]169We observe that $100_{16} = 256_{10}$, and lies \emph{just} outside the range expressible in an 8-bit byte (0--255).
[954]170
[999]171As our example shows, given an occurence $l$ of an \texttt{LJMP} instruction, it may be possible to shrink $l$ to an occurence of an \texttt{SJMP}---consuming fewer bytes of code memory---provided we can shrink any \texttt{LJMP}s that exist between $l$ and its target location.
[1001]172In particular, if we wish to shrink the \texttt{LJMP} occurring at line 1, then we must shrink the \texttt{LJMP} occurring at line 3.
173However, to shrink the \texttt{LJMP} occurring at line 3 we must also shrink the \texttt{LJMP} occurring at line 5, and \emph{vice versa}.
174
175Further, consider what happens if, instead of appearing at memory address \texttt{0x100}, the instruction at line 5 instead appeared \emph{just} beyond the size of code memory, and all other memory addresses were shifted accordingly.
[1017]176Now, in order to be able to successfully fit our program into the MCS-51's limited code memory, we are \emph{obliged} to shrink the \texttt{LJMP} occurring at line 5.
177That is, the shrinking process is not just related to the optimisation of generated machine code but also the completeness of the assembler itself.
[1001]178
[954]179How we went about resolving this problem affected the shape of our proof of correctness for the whole assembler in a rather profound way.
[1017]180We first attempted to synthesize a solution bottom up: starting with no solution, we gradually refine a solution using the same functions that implement the jump expansion process.
181Using this technique, solutions can fail to exist, and the proof of correctness for the assembler quickly descends into a diabolical quagmire.
[952]182
[954]183Abandoning this attempt, we instead split the `policy'---the decision over how any particular jump should be expanded---from the implementation that actually expands assembly programs into machine code.
[952]184Assuming the existence of a correct policy, we proved the implementation of the assembler correct.
[1017]185Further, we proved that the assembler fails to assemble an assembly program if and only if a correct policy does not exist.
186This is achieved by means of dependent types: the assembly function is total over a program, a policy and the proof that the policy is correct for that program.
[952]187
[1017]188Policies do not exist in only a limited number of circumstances: namely, if a pseudoinstruction attempts to jump to a label that does not exist, or the program is too large to fit in code memory, even after shrinking jumps according to the best policy.
189The first circumstance is an example of a serious compiler error, as an ill-formed assembly program was generated, and does not (and should not) count as a mark against the completeness of the assembler.
[1007]190We plan to employ dependent types in CerCo in order to restrict the domain of the compiler to those programs that are `semantically correct' and should be compiled.
191In particular, in CerCo we are also interested in the completeness of the compilation process, whereas previous formalisations only focused on correctness.
[1005]192
[1020]193The rest of this paper is a detailed description of our proof that is partly
194still in progress.
[954]195
[927]196% ---------------------------------------------------------------------------- %
197% SECTION                                                                      %
198% ---------------------------------------------------------------------------- %
199\subsection{Overview of the paper}
200\label{subsect.overview.of.the.paper}
[952]201In Section~\ref{sect.matita} we provide a brief overview of the Matita proof assistant for the unfamiliar reader.
202In Section~\ref{sect.the.proof} we discuss the design and implementation of the proof proper.
203In Section~\ref{sect.conclusions} we conclude.
[927]204
205% ---------------------------------------------------------------------------- %
206% SECTION                                                                      %
207% ---------------------------------------------------------------------------- %
[952]208\section{Matita}
209\label{sect.matita}
[927]210
[1020]211Matita is a proof assistant based on a variant of the Calculus of (Co)inductive Constructions~\cite{asperti:user:2007}. In particular, it features dependent
212types that we heavily exploit in the formalization. The syntax of the
213statements and definitions in the paper should be self-explanatory, at least
214to those exposed to dependent type theory. We only remark the use of
215of `$\mathtt{?}$' or `$\mathtt{\ldots}$' for omitting single terms or
216sequences of terms to be inferred automatically by the system, respectively.
217Those that are not inferred are left to the user as proof obligations.
218Pairs are denoted with angular brackets.
[952]219
[1020]220Matita features a liberal system of coercions. In particular, it is possible
221to define a uniform coercion $\lambda x.\langle x,?\rangle$ from every type $T$ to the dependent product $\Sigma x:T.P~x$. The coercion opens a proof
222obligation that asks the user to prove that $P$ holds for $x$. When a coercion
223must be applied to a complex term (a lambda-abstraction, a local definition,
224a case analysis), the system automatically propagates the coercion to the
225sub-terms. For instance, to apply a coercion to force $\lambda x.M : A \to B$
226to have type $\forall x:A.\Sigma y:B.P~x~y$, the system looks for a coercion from
227$M: B$ to $\Sigma y:B.P~x~y$ in a context augmented with $x:A$. This is
228significant when the coercion opens a proof obligation, since the user
229will be presented with multiple, but simpler proof obligations in the right
230context. Matita supports in this way the proof methodology developed by
231Sozeau in~\cite{russell}, but with an implementation that is lighter than the
232one of Coq and more integrated in the system.
233
[927]234% ---------------------------------------------------------------------------- %
235% SECTION                                                                      %
236% ---------------------------------------------------------------------------- %
[952]237\section{The proof}
238\label{sect.the.proof}
[927]239
[960]240\subsection{The assembler and semantics of machine code}
241\label{subsect.the.assembler.and.semantics.of.machine.code}
[952]242
[960]243The formalisation in Matita of the semantics of MCS-51 machine code is described in~\cite{mulligan:executable:2011}.
244We merely describe enough here to understand the rest of the proof.
245
[1021]246The MCS-51 emulator centres around a \texttt{Status} record, describing the current state of the microprocessor.
[960]247This record contains fields corresponding to the microprocessor's program counter, special function registers, and so on.
[1021]248At the machine code level, code memory is implemented as a compact trie of bytes, addressed by the program counter.
[960]249Machine code programs are loaded into \texttt{Status} using the \texttt{load\_code\_memory} function.
250
[1021]251We may execute a single step of a machine code program using the \texttt{execute\_1} function, which returns an updated \texttt{Status}:
[957]252\begin{lstlisting}
[960]253definition execute_1: Status $\rightarrow$ Status := $\ldots$
[957]254\end{lstlisting}
[1021]255The function \texttt{execute} allows one to execute an arbitrary, but fixed (due to Matita's normalisation requirement) number of steps of a program.
[957]256
[960]257Naturally, assembly programs have analogues.
258The counterpart of the \texttt{Status} record is \texttt{PseudoStatus}.
259Instead of code memory being implemented as tries of bytes, code memory is here implemented as lists of pseudoinstructions, and program counters are merely indices into this list.
[1021]260Both \texttt{Status} and \texttt{PseudoStatus} are specialisations of the same \texttt{PreStatus} record, parametric in the representation of code memory.
[999]261This allows us to share some code that is common to both records (for instance, `setter' and `getter' functions).
262
[960]263Our analogue of \texttt{execute\_1} is \texttt{execute\_1\_pseudo\_instruction}:
[957]264\begin{lstlisting}
[960]265definition execute_1_pseudo_instruction: (Word $\rightarrow$ nat $\times$ nat) $\rightarrow$
266                                         PseudoStatus $\rightarrow$ PseudoStatus := $\ldots$
[957]267\end{lstlisting}
[984]268Notice, here, that the emulation function for assembly programs takes an additional argument.
269This is a function that maps program counters (at the assembly level) to pairs of natural numbers representing the number of clock ticks that the pseudoinstruction needs to execute, post expansion.
[968]270We call this function a \emph{costing}, and note that the costing is induced by the particular strategy we use to expand pseudoinstructions.
271If we change how we expand conditional jumps to labels, for instance, then the costing needs to change, hence \texttt{execute\_1\_pseudo\_instruction}'s parametricity in the costing.
272
273The costing returns \emph{pairs} of natural numbers because, in the case of expanding conditional jumps to labels, the expansion of the `true branch' and `false branch' may differ in the number of clock ticks needed for execution.
[960]274This timing information is used inside \texttt{execute\_1\_pseudo\_instruction} to update the clock of the \texttt{PseudoStatus}.
[999]275During the proof of correctness of the assembler we relate the clocks of \texttt{Status}es and \texttt{PseudoStatus}es for the policy induced by the cost model and optimisations.
[957]276
[960]277The assembler, mapping programs consisting of lists of pseudoinstructions to lists of bytes, operates in a mostly straightforward manner.
[999]278To a degree of approximation, the assembler on an assembly program, consisting of $n$ pseudoinstructions $\mathtt{P_i}$ for $1 \leq i \leq n$, works as in the following diagram (we use $-^{*}$ to denote a combined map and flatten operation):
[960]279\begin{displaymath}
[999]280[\mathtt{P_1}, \ldots \mathtt{P_n}] \xrightarrow{\left(\mathtt{P_i} \xrightarrow{\mbox{\fontsize{7}{9}\selectfont$\mathtt{expand\_pseudo\_instruction}$}} \mathtt{[I_1^i, \ldots I^q_i]} \xrightarrow{\mbox{\fontsize{7}{9}\selectfont$\mathtt{~~~~~~~~assembly1^{*}~~~~~~~~}$}} \mathtt{[0110]}\right)^{*}} \mathtt{[010101]}
[960]281\end{displaymath}
282Here $\mathtt{I^i_j}$ for $1 \leq j \leq q$ are the $q$ machine code instructions obtained by expanding, with \texttt{expand\_pseudo\_instruction}, a single pseudoinstruction.
283Each machine code instruction $\mathtt{I^i_j}$ is then assembled, using the \texttt{assembly1} function, into a list of bytes.
284This process is iterated for each pseudoinstruction, before the lists are flattened into a single bit list representation of the original assembly program.
285
[1021]286%By inspecting the above diagram, it would appear that the best way to proceed with a proof that the assembly process does not change the semantics of an assembly program is via a decomposition of the problem into two subproblems.
287%Namely, we first expand any and all pseudoinstructions into lists of machine instructions, and provide a proof that this process does not change our program's semantics.
288%Finally, we assemble all machine code instructions into machine code---lists of bytes---and prove once more that this process does not have an adverse effect on a program's semantics.
289%By composition, we then have that the whole assembly process is semantics preserving.
[974]290
[1022]291This is a tempting approach to the proof, but ultimately the wrong approach.
292In particular, to expand a pseudo instruction we need to know the address
293at which the expanded instructions will be located, for instance to determine
294if a short jump is possible. That address is a function of the
295\emph{object code} generated for the pseudo-instructions already visited.
296Thus we need to assemble each pseduto instruction down to object code before
297moving to the next one and this must be eventually reflected on the proof
298of correctness. Therefore we will have lemmas for the \texttt{assembly1}
299function and for the composition of \texttt{expand\_pseudo\_instruction} and
300\texttt{assembly1}, but not for \texttt{expand\_pseudo\_instruction} alone.
[974]301
[960]302% ---------------------------------------------------------------------------- %
303% SECTION                                                                      %
304% ---------------------------------------------------------------------------- %
305\subsection{Policies}
306\label{subsect.policies}
307
[968]308Policies exist to dictate how conditional and unconditional jumps at the assembly level should be expanded into machine code instructions.
309Using policies, we are able to completely decouple the decision over how jumps are expanded with the act of expansion, simplifying our proofs.
310As mentioned, the MCS-51 instruction set includes three different jump instructions: \texttt{SJMP}, \texttt{AJMP} and \texttt{LJMP}; call these `short', `medium' and `long' jumps, respectively:
[957]311\begin{lstlisting}
[968]312inductive jump_length: Type[0] :=
[957]313  | short_jump: jump_length
314  | medium_jump: jump_length
315  | long_jump: jump_length.
[960]316\end{lstlisting}
[1021]317
318A \texttt{jump\_expansion\_policy} is a map from pseudo program counters (implemented as \texttt{Word}s) to \texttt{jump\_length}s.
319Efficient implementations of policies are based on tries.
320Intuitively, a policy maps positions in a program (indexed using program counters implemented as \texttt{Word}s) to a particular variety of jump:
[960]321\begin{lstlisting}
[1021]322definition policy_type := Word $\rightarrow$ jump_length.
[957]323\end{lstlisting}
[1021]324
[968]325Next, we require a series of `sigma' functions.
326These functions map assembly program counters to their machine code counterparts, establishing the correspondence between `positions' in an assembly program and `positions' in a machine code program.
[1001]327At the heart of this process is \texttt{sigma0} which traverses an assembly program building maps from pseudo program counters to program counters.
[1010]328This function fails if and only if an internal call to \texttt{assembly\_1\_pseudoinstruction\_safe} fails, which happens if a jump pseudoinstruction is expanded incorrectly:
[957]329\begin{lstlisting}
[1016]330definition sigma0: pseudo_assembly_program $\rightarrow$ policy_type
[974]331  $\rightarrow$ option (nat $\times$ (nat $\times$ (BitVectorTrie Word 16))) := $\ldots$
[957]332\end{lstlisting}
[1010]333Here, the returned \texttt{BitVectorTrie} is a map between pseudo program counters and program counters, and is constructed by successively expanding pseudoinstructions and incrementing the two program counters the requisite amount to keep them in correct correspondence.
334The two natural numbers returned are the maximum values that the two program counters attained.
335
336We eventually lift this to functions from pseudo program counters to program counters, implemented as \texttt{Word}s:
[957]337\begin{lstlisting}
[969]338definition sigma_safe:
[1016]339  pseudo_assembly_program $\rightarrow$ policy_type $\rightarrow$ option (Word $\rightarrow$ Word) := $\ldots$
[968]340\end{lstlisting}
[1021]341
[1016]342Now, it's possible to define what a `good policy' is for a program \texttt{p}.
[1018]343A policy \texttt{pol} is deemed good when it prevents \texttt{sigma\_safe} from failing on \texttt{p}.
344Failure is only possible when the policy dictates that short or medium jumps be expanded to jumps to locations too far away, or when the produced object code does not fit into code memory.
345A \texttt{policy} for a program \texttt{p} is a policy that is good for \texttt{p}:
[968]346\begin{lstlisting}
[1018]347definition policy_ok := $\lambda$pol.$\lambda$p. sigma_safe p $\neq$ None $\ldots$
[1021]348definition policy :=
349  $\lambda$p. $\Sigma$jump_expansion: policy_type. policy_ok jump_expansion p
[968]350\end{lstlisting}
[1021]351
[1018]352Finally, we obtain \texttt{sigma}, a mapping from pseudo program counters to program counters that takes in input a good policy and thus never fails.
353Note how we avoid failure here, and in most of the remaining functions, by restricting the domain using the dependent type \texttt{policy}:
[968]354\begin{lstlisting}
[1010]355definition sigma: $\forall$p. policy p $\rightarrow$ Word $\rightarrow$ Word := $\ldots$
[957]356\end{lstlisting}
[974]357
358% ---------------------------------------------------------------------------- %
359% SECTION                                                                      %
360% ---------------------------------------------------------------------------- %
[1019]361\subsection{Correctness of the assembler with respect to fetching}
[974]362\label{subsect.total.correctness.of.the.assembler}
363
[976]364Using our policies, we now work toward proving the total correctness of the assembler.
[1021]365By `total correctness', we mean that the assembly process never fails when provided with a good policy and that the process does not change the semantics of a certain class of well behaved assembly programs.
[1010]366Naturally, this necessitates keeping some sort of correspondence between addresses at the assembly level and addresses at the machine code level.
[976]367For this, we use the \texttt{sigma} machinery defined at the end of Subsection~\ref{subsect.policies}.
368
[1021]369We expand pseudoinstructions using the function \texttt{expand\_pseudo\_instruction}.
370This takes an assembly program (consisting of a list of pseudoinstructions), a good policy for the program and a pointer to the pseudo code memory.
371It returns a list of instructions, corresponding to the expanded pseudoinstruction referenced by the pointer.
372The policy is used to decide how to expand \texttt{Call}s, \texttt{Jmp}s and conditional jumps.
373The function is given a dependent type that incorporates its specification.
374Its pre- and post-conditions are omitted in the paper due to lack of space.
375We show them as an example in the next function, \texttt{build\_maps}.
[974]376\begin{lstlisting}
377definition expand_pseudo_instruction:
[1021]378  $\forall$program. $\forall$pol: policy program.
379  $\forall$ppc:Word. $\ldots$ $\Sigma$res. list instruction. $\ldots$ := $\ldots$
[974]380\end{lstlisting}
381
[1013]382The following function, \texttt{build\_maps}, is used to construct a pair of mappings from program counters to labels and cost labels, respectively.
383Cost labels are a technical device used in the CerCo prototype C compiler for proving that the compiler is cost preserving.
384For our purposes in this paper, they can be safely ignored, though the interested reader may consult~\cite{amadio:certifying:2010} for an overview.
[1012]385
[1013]386The label map, on the other hand, records the position of labels that appear in an assembly program, so that the pseudoinstruction expansion process can replace them with real memory addresses:
[974]387\begin{lstlisting}
[1013]388definition build_maps:
389 $\forall$p. $\forall$pol: policy p.
390 $\Sigma$res : ((BitVectorTrie Word 16) $\times$ (BitVectorTrie Word 16)).
391   let $\langle$labels, costs$\rangle$ := res in
392     $\forall$id. occurs_exactly_once id ($\pi_2$ p) $\rightarrow$
393    let addr := address_of_word_labels_code_mem ($\pi_2$ p) id in
394      lookup $\ldots$ id labels (zero $\ldots$) = sigma pseudo_program pol addr := $\ldots$
395\end{lstlisting}
396The rather complex type of \texttt{build\_maps} owes to our use of Matita's Russell facility to provide a strong specification for the function in the type (c.f. the use of sigma types, through which Russell is implemented in Matita).
[1019]397In particular, we express that for all labels that appear exactly once in any assembly program, the newly created map used in the implementation and the
398stronger \texttt{sigma} function used in the specification agree.
[1013]399
400Using \texttt{build\_maps}, we can express the following lemma, expressing the correctness of the assembly function:
401\begin{lstlisting}
402lemma assembly_ok: $\forall$p,pol,assembled.
[1017]403  let $\langle$labels, costs$\rangle$ := build_maps p pol in
[1013]404  $\langle$assembled,costs$\rangle$ = assembly p pol $\rightarrow$
405  let cmem := load_code_memory assembled in
[1017]406  let preamble := $\pi_1$ p in
[1013]407  let dlbls := construct_datalabels preamble in
[1017]408  let addr := address_of_word_labels_code_mem ($\pi_2$ p) in
409  let lk_lbls := λx. sigma p pol (addr x) in
[1013]410  let lk_dlbls := λx. lookup $\ldots$ x datalabels (zero ?) in
411  $\forall$ppc, pi, newppc.
[1017]412  $\forall$prf: $\langle$pi, newppc$\rangle$ = fetch_pseudo_instruction ($\pi_2$ p) ppc.
[1013]413  $\forall$len, assm.
414  let spol := sigma program pol ppc in
415  let spol_len := spol + len in
416  let echeck := encoding_check cmem spol spol_len assm in
417  let a1pi := assembly_1_pseudoinstruction in
418  $\langle$len, assm$\rangle$ = a1pi p pol ppc lk_lbls lk_dlbls pi (refl $\ldots$) (refl $\ldots$) ? $\rightarrow$
419    echeck $\wedge$ sigma p pol newppc = spol_len.
[974]420\end{lstlisting}
[1017]421Suppose also we assemble our program \texttt{p} in accordance with a policy \texttt{pol} to obtain \texttt{assembled}.
422Here, we perform a `sanity check' to ensure that the two cost label maps generated are identical, before loading the assembled program into code memory \texttt{cmem}.
423Then, for every pseudoinstruction \texttt{pi}, pseudo program counter \texttt{ppc} and new pseudo program counter \texttt{newppc}, such that we obtain \texttt{pi} and \texttt{newppc} from fetching a pseudoinstruction at \texttt{ppc}, we check that assembling this pseudoinstruction produces the correct number of machine code instructions, and that the new pseudo program counter \texttt{ppc} has the value expected of it.
[974]424
[1012]425Theorem \texttt{fetch\_assembly} establishes that the \texttt{fetch} and \texttt{assembly1} functions interact correctly.
426The \texttt{fetch} function, as its name implies, fetches the instruction indexed by the program counter in the code memory, while \texttt{assembly1} maps a single instruction to its byte encoding:
[974]427\begin{lstlisting}
[983]428theorem fetch_assembly: $\forall$pc, i, cmem, assembled.
429  assembled = assembly1 i $\rightarrow$
430  let len := length $\ldots$ assembled in
431    encoding_check cmem pc (pc + len) assembled $\rightarrow$
432    let fetched := fetch code_memory (bitvector_of_nat $\ldots$ pc) in
433    let $\langle$instr_pc, ticks$\rangle$ := fetched in
434    let $\langle$instr, pc'$\rangle$ := instr_pc in
435      (eq_instruction instr i $\wedge$
436       eqb ticks (ticks_of_instruction instr) $\wedge$
437       eq_bv $\ldots$ pc' (pc + len)) = true.
438\end{lstlisting}
[1012]439In particular, we read \texttt{fetch\_assembly} as follows.
440Given an instruction, \texttt{i}, we first assemble the instruction to obtain \texttt{assembled}, checking that the assembled instruction was stored in code memory correctly.
441Fetching from code memory, we obtain \texttt{fetched}, a tuple consisting of the instruction, new program counter, and the number of ticks this instruction will take to execute.
442Deconstructing these tuples, we finally check that the fetched instruction is the same instruction that we began with, and the number of ticks this instruction will take to execute is the same as the result returned by a lookup function, \texttt{ticks\_of\_instruction}, devoted to tracking this information.
443Or, in plainer words, assembling and then immediately fetching again gets you back to where you started.
[983]444
[1021]445Lemma \texttt{fetch\_assembly\_pseudo} (whose type is shown here slightly simplified) is obtained by composition of \texttt{expand\_pseudo\_instruction} and \texttt{assembly\_1\_pseudoinstruction}:
[983]446\begin{lstlisting}
[1019]447lemma fetch_assembly_pseudo:
448 ∀program.∀pol:policy program.∀ppc.∀code_memory.
449  let pi := fst (fetch_pseudo_instruction (snd program) ppc) in
450  let instructions := expand_pseudo_instruction program pol ppc ... in
451  let $\langle$len,assembled$\rangle$ := assembly_1_pseudoinstruction program pol ppc ... in
452  encoding_check code_memory pc (pc + len) assembled →
453  fetch_many code_memory (pc + len) pc instructions.
[974]454\end{lstlisting}
[1011]455Here, \texttt{len} is the number of machine code instructions the pseudoinstruction at hand has been expanded into, and \texttt{encoding\_check} is a recursive function that checks that assembled machine code is correctly stored in code memory.
[981]456We assemble a single pseudoinstruction with \texttt{assembly\_1\_pseudoinstruction}, which internally calls \texttt{jump\_expansion} and \texttt{expand\_pseudo\_instruction}.
[976]457The function \texttt{fetch\_many} fetches multiple machine code instructions from code memory and performs some routine checks.
[974]458
[983]459Intuitively, Lemma \texttt{fetch\_assembly\_pseudo} can be read as follows.
[1019]460Suppose our policy \texttt{jump\_expansion} dictates that the pseudoinstruction indexed by the pseudo program counter \texttt{ppc} in assembly program \texttt{program} gives us the policy decision \texttt{pol}.
461Further, suppose we expand the pseudoinstruction at \texttt{ppc} with the policy decision \texttt{pol}, obtaining the list of machine code instructions \texttt{instructions}.
462Suppose we also assemble the pseudoinstruction at \texttt{ppc} to obtain \texttt{assembled}, a list of bytes.
[1012]463Then, we check with \texttt{fetch\_many} that the number of machine instructions that were fetched matches the number of instruction that \texttt{expand\_pseudo\_instruction} expanded.
[976]464
[1021]465The final lemma in this series is \texttt{fetch\_assembly\_pseudo2} that combines the Lemma \texttt{fetch\_aasembly\_pseudo} with the correctness of the functions that load object code into the processor's memory.
466At first, the lemmas appears to nearly establish the correctness of the assembler:
[974]467\begin{lstlisting}
[1019]468lemma fetch_assembly_pseudo2:
469 ∀program,pol,ppc.
470  let $\langle$labels,costs$\rangle$ := build_maps program pol in
471  let assembled := \fst (assembly program pol) in
[983]472  let code_memory := load_code_memory assembled in
[1019]473  let data_labels := construct_datalabels (\fst program) in
474  let lookup_labels :=
475    λx. sigma $\ldots$ pol (address_of_word_labels_code_mem (\snd program) x) in
476  let lookup_datalabels := λx. lookup ? ? x data_labels (zero ?) in
477  let $\langle$pi,newppc$\rangle$ := fetch_pseudo_instruction (\snd program) ppc in
478  let instructions ≝ expand_pseudo_instruction program pol ppc ... in
479   fetch_many code_memory (sigma program pol newppc)
480     (sigma program pol ppc) instructions.
[974]481\end{lstlisting}
[1012]482
[983]483Intuitively, we may read \texttt{fetch\_assembly\_pseudo2} as follows.
[1021]484Suppose we are able to successfully assemble an assembly program using \texttt{assembly} and produce a code memory, \texttt{code\_memory}.
485Then, fetching a pseudoinstruction from the pseudo code memory at address \texttt{ppc} corresponds to fetching a sequence of instructions from the real code memory at address \texttt{sigma program pol ppc}.
486The fetched sequence is established as the expansion of the pseudoinstruction, according to the good policy \texttt{pol}.
[974]487
[1021]488However, this property is \emph{not} strong enough to establish that the semantics of an assembly program has been preserved by the assembly process since it does not establish the correspondence between the semantics of a pseudo-instruction and that of its expansion.
489In particular, the two semantics differ on instructions that \emph{could} directly manipulate program addresses.
[983]490
[1021]491% ---------------------------------------------------------------------------- %
492% SECTION                                                                      %
493% ---------------------------------------------------------------------------- %
494\subsection{Total correctness for `well behaved' assembly programs}
495\label{subsect.total.correctness.for.well.behaved.assembly.programs}
[1019]496
[1021]497In any `reasonable' assembly language addresses in code memory are just data that can be manipulated in multiple ways by the program.
498An assembly program can forge, compare and move addresses around, shift existing addresses or apply logical and arithmetical operations to them.
499Further, every optimising assembler is allowed to modify code memory.
500Hence only the semantics of a few of the aforementioned operations are preserved by an optimising assembler/compiler.
501Moreover, this characterisation of well behaved programs is clearly undecidable.
[1019]502
[1021]503To obtain a reasonable statement of correctness for our assembler, we need to trace memory locations (and, potentially, registers) that contain memory addresses.
504This is necessary for two purposes.
[1019]505
[1021]506First we must detect (at run time) programs that manipulate addresses in well behaved ways, according to some approximation of well-behavedness.
507Second, we must compute statuses that correspond to pseudo-statuses.
508The contents of the program counter must be translated, as well as the contents of all traced locations, by applying the \texttt{sigma} map.
509Remaining memory cells are copied \emph{verbatim}.
510
511For instance, after a function call, the two bytes that form the return pseudo address are pushed on top of the stack, i.e. in internal RAM.
512This pseudo internal RAM corresponds to an internal RAM where the stack holds the real addresses after optimisation, and all the other values remain untouched.
513
514We use an \texttt{internal\_pseudo\_address\_map} to trace addresses of code memory addresses in internal RAM.
[1019]515The current code is parametric on the implementation of the map itself.
[957]516\begin{lstlisting}
[1019]517axiom internal_pseudo_address_map: Type[0].
[969]518\end{lstlisting}
[1012]519
[1021]520The \texttt{low\_internal\_ram\_of\_pseudo\_low\_internal\_ram} function converts the lower internal RAM of a \texttt{PseudoStatus} into the lower internal RAM of a \texttt{Status}.
521A similar function exists for higher internal RAM.
522Note that both RAM segments are indexed using addresses 7-bits long.
523The function is currently axiomatised, and an associated set of axioms prescribe the behaviour of the function:
[969]524\begin{lstlisting}
[957]525axiom low_internal_ram_of_pseudo_low_internal_ram:
[969]526  internal_pseudo_address_map $\rightarrow$ BitVectorTrie Byte 7 $\rightarrow$ BitVectorTrie Byte 7.
[957]527\end{lstlisting}
528
[969]529Next, we are able to translate \texttt{PseudoStatus} records into \texttt{Status} records using \texttt{status\_of\_pseudo\_status}.
530Translating a \texttt{PseudoStatus}'s code memory requires we expand pseudoinstructions and then assemble to obtain a trie of bytes.
[1019]531This never fails, providing that our policy is correct:
[957]532\begin{lstlisting}
[1012]533definition status_of_pseudo_status: internal_pseudo_address_map $\rightarrow$
534  $\forall$ps:PseudoStatus. policy (code_memory $\ldots$ ps) $\rightarrow$ Status
[957]535\end{lstlisting}
[1012]536
[1021]537The \texttt{next\_internal\_pseudo\_address\_map} function is responsible for run time monitoring of the behaviour of assembly programs, in order to detect well behaved ones.
538It returns the new map that traces memory addresses in internal RAM after execution of the next pseudoinstruction.
539It fails when the instruction tampers with memory addresses in unanticipated (but potentially correct) ways.
540It thus decides the membership of a correct but not complete subset of well behaved programs.
[957]541\begin{lstlisting}
[974]542definition next_internal_pseudo_address_map: internal_pseudo_address_map
543  $\rightarrow$ PseudoStatus $\rightarrow$ option internal_pseudo_address_map
[957]544\end{lstlisting}
[1012]545
546The function \texttt{ticks\_of} computes how long---in clock cycles---a pseudoinstruction will take to execute when expanded in accordance with a given policy.
547The function returns a pair of natural numbers, needed for recording the execution times of each branch of a conditional jump.
548\begin{lstlisting}
549definition ticks_of:
550  $\forall$p:pseudo_assembly_program. policy p $\rightarrow$ Word $\rightarrow$ nat $\times$ nat := $\ldots$
551\end{lstlisting}
552
[974]553Finally, we are able to state and prove our main theorem.
[1012]554This relates the execution of a single assembly instruction and the execution of (possibly) many machine code instructions, as long .
555That is, the assembly process preserves the semantics of an assembly program, as it is translated into machine code, as long as we are able to track memory addresses properly:
[957]556\begin{lstlisting}
[974]557theorem main_thm:
[1019]558 ∀M,M':internal_pseudo_address_map.∀ps.∀pol: policy ps.
559  next_internal_pseudo_address_map M ps = Some $\ldots$ M' →
560   ∃n.
561      execute n (status_of_pseudo_status M ps pol)
562    = status_of_pseudo_status M'
563       (execute_1_pseudo_instruction (ticks_of (code_memory $\ldots$ ps) pol) ps)
564       [pol].
[957]565\end{lstlisting}
[1021]566The statement is standard for forward simulation, but restricted to \texttt{PseudoStatuses} \texttt{ps} whose next instruction to be executed is well-behaved with respect to the \texttt{internal\_pseudo\_address\_map} \texttt{M}.
567Theorem \texttt{main\_thm} establishes the total correctness of the assembly process and can simply be lifted to the forward simulation of an arbitrary number of well behaved steps on the assembly program.
[1012]568
[969]569% ---------------------------------------------------------------------------- %
570% SECTION                                                                      %
571% ---------------------------------------------------------------------------- %
[927]572\section{Conclusions}
573\label{sect.conclusions}
574
[976]575We have proved the total correctness of an assembler for MCS-51 assembly language.
576In particular, our assembly language featured labels, arbitrary conditional and unconditional jumps to labels, global data and instructions for moving this data into the MCS-51's single 16-bit register.
[999]577Expanding these pseudoinstructions into machine code instructions is not trivial, and the proof that the assembly process is `correct', in that the semantics of a subset of assembly programs are not changed is complex.
578Further, we have observed the `shocking' fact that any optimising assembler cannot preserve the semantics of all assembly programs.
[960]579
[983]580The formalisation is a key component of the CerCo project, which aims to produce a verified concrete complexity preserving compiler for a large subset of the C programming language.
581The verified assembler, complete with the underlying formalisation of the semantics of MCS-51 machine code (described fully in~\cite{mulligan:executable:2011}), will form the bedrock layer upon which the rest of the CerCo project will build its verified compiler platform.
[999]582However, further work is needed.
583In particular, as it stands, the code produced by the prototype CerCo C compiler does not fall into the `semantics preserving' subset of assembly programs for our assembler.
584This is because the MCS-51 features a small stack space, and a larger stack is customarily manually emulated in external RAM.
585As a result, the majority of programs feature slices of memory addresses and program counters being moved in-and-out of external RAM via the registers, simulating the stack mechanism.
586At the moment, this movement is not tracked by \texttt{internal\_pseudo\_address\_map}, which only tracks the movement of memory addresses in low internal RAM.
587We leave extending this tracking of memory addresses throughout the whole of the MCS-51's address spaces as future work.
[983]588
[1007]589It is interesting to compare our work to an `industrial grade' assembler for the MCS-51: SDCC~\cite{sdcc:2011}.
[1017]590SDCC is the only open source C compiler that targets the MCS-51 instruction set.
[1007]591It appears that all pseudojumps in SDCC assembly are expanded to \texttt{LJMP} instructions, the worst possible jump expansion policy from an efficiency point of view.
[1008]592Note that this policy is the only possible policy \emph{in theory} that can preserve the semantics of an assembly program during the assembly process.
[1007]593However, this comes at the expense of assembler completeness: the generated program may be too large to fit into code memory.
[1017]594In this respect, there is a trade-off between the completeness of the assembler and the efficiency of the assembled program.
[1007]595The definition and proof of an complete, optimal (in the sense that jump pseudoinstructions are expanded to the smallest possible opcode) and correct jump expansion policy is ongoing work.
596
[999]597Aside from their application in verified compiler projects such as CerCo and CompCert, verified assemblers such as ours could also be applied to the verification of operating system kernels.
[981]598Of particular note is the verified seL4 kernel~\cite{klein:sel4:2009,klein:sel4:2010}.
[983]599This verification explicitly assumes the existence of, amongst other things, a trustworthy assembler and compiler.
[952]600
[999]601We note here that both CompCert and the seL4 formalisation assume the existence of `trustworthy' assemblers.
602Our observation that an optimising assembler cannot preserve the semantics of every assembly program may have important consequences for these projects.
603In particular, if CompCert chooses to assume the existence of an optimising assembler, then care should be made to ensure that any assembly program produced by the CompCert C compiler falls into the class of assembly programs that have a hope of having their semantics preserved by an optimising assembler.
[952]604
[999]605In certain places in our formalisation (e.g. in proving \texttt{build\_maps} is correct) we made use of Matita's implementation of Russell~\cite{sozeau:subset:2006}.
606In Matita, Russell may be implemented using two coercions and some notational sugaring.
607% more
[955]608
[952]609\subsection{Related work}
610\label{subsect.related.work}
611
[981]612% piton
613We are not the first to consider the total correctness of an assembler for a non-trivial assembly language.
614Perhaps the most impressive piece of work in this domain is the Piton stack~\cite{moore:piton:1996,moore:grand:2005}.
615This was a stack of verified components, written and verified in ACL2, ranging from a proprietary FM9001 microprocessor verified at the gate level, to assemblers and compilers for two high-level languages---a dialect of Lisp and $\mu$Gypsy~\cite{moore:grand:2005}.
616
617% jinja
618Klein and Nipkow consider a Java-like programming language, Jinja~\cite{klein:machine:2006,klein:machine:2010}.
619They provide a compiler, virtual machine and operational semantics for the programming language and virtual machine, and prove that their compiler is semantics and type preserving.
620
[1000]621We believe some other verified assemblers exist in the literature.
[999]622However, what sets our work apart from that above is our attempt to optimise the machine code generated by our assembler.
623This complicates any formalisation effort, as the best possible selection of machine instructions must be made, especially important on a device such as the MCS-51 with a miniscule code memory.
[1004]624Further, care must be taken to ensure that the time properties of an assembly program are not modified by the assembly process lest we affect the semantics of any program employing the MCS-51's I/O facilities.
625This is only possible by inducing a cost model on the source code from the optimisation strategy and input program.
626This will be a \emph{leit motif} of CerCo.
[999]627
628Finally, mention of CerCo will invariably invite comparisons with CompCert~\cite{compcert:2011,leroy:formal:2009}, another verified compiler project closely related to CerCo.
[981]629As previously mentioned, CompCert considers only extensional correctness of the compiler, and not intensional correctness, which CerCo focusses on.
630However, CerCo also extends CompCert in other ways.
631Namely, the CompCert verified compilation chain terminates at the PowerPC or ARM assembly level, and takes for granted the existence of a trustworthy assembler.
632CerCo chooses to go further, by considering a verified compilation chain all the way down to the machine code level.
633In essence, the work presented in this publication is one part of CerCo's extension over CompCert.
634
[1007]635\subsection{Resources}
636\label{subsect.resources}
[999]637
638All files relating to our formalisation effort can be found online at~\url{http://cerco.cs.unibo.it}.
[1009]639Our development, including the definition of the executable semantics of the MCS-51, is spread across 17 files, totalling around 13,000 lines of Matita source.
640The bulk of the proof described herein is contained in a single file, \texttt{AssemblyProof.ma}, consisting of approximately 3000 lines of Matita source.
[999]641
[1012]642We admit to using a number of axioms in our development.
643We do not believe the use of these axioms has been particularly onerous---very few concern anything more interesting than, say, stating that converting from a natural number to a bitvector and back again is the identity---and what axioms remain are rapidly being closed as work continues.
644
[927]645\bibliography{cpp-2011.bib}
646
[960]647\end{document}\renewcommand{\verb}{\lstinline}
648\def\lstlanguagefiles{lst-grafite.tex}
649\lstset{language=Grafite}
Note: See TracBrowser for help on using the repository browser.