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

Last change on this file since 953 was 953, checked in by mulligan, 9 years ago

more changes, including additions to the bibliography, and tightening up the introduction

File size: 14.7 KB
7\title{On the correctness of an assembler for the Intel MCS-51 microprocessor}
8\author{Jaap Boender \and Dominic P. Mulligan \and Claudio Sacerdoti Coen}
9\institute{Dipartimento di Scienze dell'Informazione, Universit\'a di Bologna}
18We consider the formalisation of an assembler for the Intel MCS-51 8-bit microprocessor in the Matita proof assistant.
19This 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.
23% ---------------------------------------------------------------------------- %
24% SECTION                                                                      %
25% ---------------------------------------------------------------------------- %
29We consider the formalisation of an assembler for the Intel MCS-51 8-bit microprocessor in the Matita proof assistant~\cite{asperti:user:2007}.
30This 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.
32The MCS-51 dates from the early 1980s and commonly called the 8051/8052.
33Despite the microprocessor's age, derivatives are still widely manufactured by a number of semiconductor foundries.
34As a result the processor is widely used, especially in embedded systems development, where well-tested, cheap, predictable microprocessors find their niche.
36The MCS-51 has a relative paucity of features compared to its more modern brethren.
37In 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.
38Instead, 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.
39It is important to stress that this timing information, unlike in more sophisticated processors, is not an estimate, it is a definition.
40For 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.
42This predicability of timing information is especially attractive to the CerCo consortium.
43We 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 (WCET) tools that would be necessary to achieve the same result with, for example, a modern ARM or PowerPC microprocessor.
45However, the MCS-51's paucity of features is a double edged sword.
46In particular, the MCS-51 features relatively miniscule memory spaces (including read-only code memory, stack and internal/external random access memory) by modern standards.
47As a result our compiler, to have any sort of hope of successfully compiling realistic C programs, ought to produce `tight' machine code.
48This is not simple.
50We here focus on a single issue in the MCS-51's instruction set: unconditional jumps.
51The MCS-51 features three conditional jump instructions: \texttt{LJMP} and \texttt{SJMP}---`long jump' and `short jump' respectively---and an 11-bit oddity of the MCS-51, \texttt{AJMP}, that the prototype CerCo compiler~\cite{cerco-report-code:2011} ignores for simplicity's sake.\footnote{Ignoring \texttt{AJMP} and its analogue \texttt{ACALL} is not idiosyncratic.  The Small Device C Compiler (SDCC)~\cite{sdcc:2011}, the leading open source C compiler for the MCS-51, also seemingly does not produce \texttt{AJMP} and \texttt{ACALL} instructions.  Their utility in a modern context remains unclear.}
52Each of these three instructions expects arguments in different sizes and behaves in different ways.
53\texttt{SJMP} may only perform a `local jump' whereas \texttt{LJMP} may jump to any address in the MCS-51's memory space.
54Consequently, 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 should be selected.
56The 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.
57Instead, the compiler targets an assembly language, complete with pseudoinstructions including bespoke \texttt{Jmp} and \texttt{Call} instructions.
58Labels, 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.
59This latter feature will ease any later consideration of separate compilation in the CerCo compiler.
60An assembler is used to expand pseudoinstructions into MCS-51 machine code.
62However, this assembly process is not trivial, for numerous reasons.
63For example, our conditional jumps to labels behave differently from their machine code counterparts.
64At the machine code level, conditional jumps may only jump to a relative offset, expressed in a byte, of the current program counter, limiting their range.
65However, 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.
67Further, trying to na\"ively relate assembly programs with their machine code counterparts simply does not work.
68Machine code programs that fetch from code memory and programs that combine the program counter with constant shifts do not make sense at the assembly level.
69More generally, memory addresses can only be compared with other memory addresses.
70However, checking that memory addresses are only compared against each other at the assembly level is in fact undecidable.
71In short, the full preservation of the semantics of the two languages is impossible.
73Yet more complications are added by the peculiarities of the CerCo project itself.
74As mentioned, the CerCo consortium is in the business of constructing a verified compiler for the C programming language.
75However, 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{intensional correctness} of the compiler, but also its \emph{extensional correctness}.
76That 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.
77However 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.
78CerCo aims to expand the current state of the art by producing a compiler where this temporal degradation is guaranteed not to happen.
80In order to achieve this CerCo imposes a cost model on programs, or more specifically, on simple blocks of instructions.
81This 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, obtaining a very precise costing for a program by embracing the compilation process, not ignoring it.
82However, this 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 complexity characteristics.
83This also applies for the translation from assembly language to machine code.
84How do we assign a cost to a pseudoinstruction?
86There is one snag: how to expand jumps.
87As mentioned, conditional jumps at the assembly level can jump to a label appearing anywhere in the program.
88However, at the machine code level, conditional jumps are limited to jumping `locally', using an 8-bit relative offset of the program counter.
89To translate a jump to a label, a single conditional jump pseudoinstruction is potentially translated into a block of three real instructions, as follows (here, \texttt{JZ} is `jump if accumulator is zero'):
92       & \mathtt{JZ}  & label                      &                 & \mathtt{JZ}   & \text{size of \texttt{SJMP} instruction} \\
93       & \ldots       &                            & \text{translates to}   & \mathtt{SJMP} & \text{size of \texttt{LJMP} instruction} \\
94label: & \mathtt{MOV} & \mathtt{A}\;\;\mathtt{B}   & \Longrightarrow & \mathtt{LJMP} & \text{address of \textit{label}} \\
95       &              &                            &                 & \ldots        & \\
96       &              &                            &                 & \mathtt{MOV}  & \mathtt{A}\;\;\mathtt{B}
99In the translation, if \texttt{JZ} fails, we fall through to the \texttt{SJMP} which jumps over the \texttt{LJMP}.
100Naturally, if \textit{label} is close enough, a conditional jump pseudoinstruction is mapped directly to a conditional jump instruction; the above translation only applies if \textit{label} is not sufficiently local.
101Similarly, we must also work out whether to expand an unconditional jump pse
102This leaves the problem, addressed below, of calculating whether a label is indeed `close enough' for the simpler translation to be used.
104Crucially, the above translation demonstrates the difficulty in predicting how many clock cycles a pseudoinstruction will take to execute.
105A conditional jump may be mapped to a single machine instruction or a block of three.
106Perhaps more insidious, the number of cycles needed to execute the instructions in the two branches of a translated conditional jump may be different.
107Depending on the semiconductor manufacturer, an \texttt{SJMP} could in theory take a different number of clock cycles to execute than an \texttt{LJMP}.
108These issues must also be dealt with in order to prove that the translation pass preserves the concrete complexity of the code.
110The question remains: how do we decide whether to expand a jump into an \texttt{SJMP} or an \texttt{LJMP}?
111This problem is far from trivial.
112To understand why, consider the following snippet of assembly code:
114\text{dpm: finish me}
116As 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} providing we can shrink any \texttt{LJMP}s that exist between $l$ and its target location.
117However, shrinking these \texttt{LJMP}s may in turn depend on shrinking $l$ to an \texttt{SJMP}, as it is perfectly possible to jump backwards.
118In short, unless we can somehow break this loop of circularity, we are stuck with a suboptimal solution to the expanding jumps problem.
120How we go about resolving this problem affected the shape of our proof of correctness for the whole assembler in a rather profound way.
121We first attempted to synthesize a solution bottom up.
122That is, starting with no solution, we gradually refine a solution using the same functions that implement the jump expansion.
123Using this technique, solutions can fail to exist, and the proof quickly descends into a diabolical quagmire.
125Abandoning this attempt, we instead split the `policy', i.e. the decision over how any particular jump should be expanded, from the implementation.
126Assuming the existence of a correct policy, we proved the implementation of the assembler correct.
127Further, we proved that the assembler fails to assemble a file if and only if a correct policy does not exist.
128Policies 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.
129The first case would constitute a serious compiler error, and hopefully certifying the rest of the compiler would rule this possibility out; the second case is unavoidable---certified compiler or not, trying to load a huge program into a small code memory will break \emph{something}.
131% ---------------------------------------------------------------------------- %
132% SECTION                                                                      %
133% ---------------------------------------------------------------------------- %
134\subsection{Overview of the paper}
136In Section~\ref{sect.matita} we provide a brief overview of the Matita proof assistant for the unfamiliar reader.
137In Section~\ref{sect.the.proof} we discuss the design and implementation of the proof proper.
138In Section~\ref{sect.conclusions} we conclude.
140% ---------------------------------------------------------------------------- %
141% SECTION                                                                      %
142% ---------------------------------------------------------------------------- %
147% ---------------------------------------------------------------------------- %
148% SECTION                                                                      %
149% ---------------------------------------------------------------------------- %
150\section{The proof}
153  \begin{itemize}
154   \item use of dependent types to throw away wrong programs that would made
155    the statement for completeness complex. E.g. misuse of addressing modes,
156    jumps to non existent labels, etc.
157   \item try to go for small reflection; avoid inductive predicates that require
158    tactics (inversion, etc.) that do not work well with dependent types; small
159    reflection usually does
160   \item use coercions to simulate Russell; mix together the proof styles
161    a la Russell (for situations with heavy dependent types) and the standard
162    one
163  \end{itemize}
165% ---------------------------------------------------------------------------- %
166% SECTION                                                                      %
167% ---------------------------------------------------------------------------- %
171\subsection{Use of dependent types}
174As it stands our use of complex dependent types is limited in the formalisation.
175Where it made sense, for example in data structures like tries and vectors, we have made limited use of dependent types.
177\subsection{Related work}
Note: See TracBrowser for help on using the repository browser.