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

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

more work on conclusions

File size: 15.1 KB
RevLine
[917]1\documentclass{llncs}
2
[952]3\usepackage{amsmath}
[917]4\usepackage[english]{babel}
6
[952]7\title{On the correctness of an assembler for the Intel MCS-51 microprocessor}
[927]8\author{Jaap Boender \and Dominic P. Mulligan \and Claudio Sacerdoti Coen}
9\institute{Dipartimento di Scienze dell'Informazione, Universit\'a di Bologna}
[918]10
[927]11\bibliographystyle{splncs03}
12
[917]13\begin{document}
14
15\maketitle
16
17\begin{abstract}
[952]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.
20...
[917]21\end{abstract}
22
[918]23% ---------------------------------------------------------------------------- %
24% SECTION                                                                      %
25% ---------------------------------------------------------------------------- %
26\section{Introduction}
27\label{sect.introduction}
28
[953]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.
[947]31
[953]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.
[927]35
[952]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.
[953]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.
[927]41
[952]42This predicability of timing information is especially attractive to the CerCo consortium.
[953]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.
44
[952]45However, the MCS-51's paucity of features is a double edged sword.
[953]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.
[952]48This is not simple.
[927]49
[953]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.}
[952]52Each of these three instructions expects arguments in different sizes and behaves in different ways.
[953]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.
[927]55
[953]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.
[952]59This latter feature will ease any later consideration of separate compilation in the CerCo compiler.
[953]60An assembler is used to expand pseudoinstructions into MCS-51 machine code.
[927]61
[953]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.
[952]66
[953]67Further, trying to na\"ively relate assembly programs with their machine code counterparts simply does not work.
[952]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.
72
[953]73Yet more complications are added by the peculiarities of the CerCo project itself.
[952]74As mentioned, the CerCo consortium is in the business of constructing a verified compiler for the C programming language.
[953]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}.
[952]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.
79
80In order to achieve this CerCo imposes a cost model on programs, or more specifically, on simple blocks of instructions.
[954]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.
82In short, we aim to obtain a very precise costing for a program by embracing the compilation process, not ignoring it.
83This, 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 complexity characteristics.
[952]84This also applies for the translation from assembly language to machine code.
[954]85
[952]86How do we assign a cost to a pseudoinstruction?
87As mentioned, conditional jumps at the assembly level can jump to a label appearing anywhere in the program.
[954]88However, at the machine code level, conditional jumps are limited to jumping locally', using a measly byte offset.
89To 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]90\begin{displaymath}
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}
97\end{array}
98\end{displaymath}
99In the translation, if \texttt{JZ} fails, we fall through to the \texttt{SJMP} which jumps over the \texttt{LJMP}.
[954]100Naturally, if \textit{label} is close enough, a conditional jump pseudoinstruction is mapped directly to a conditional jump machine instruction; the above translation only applies if \textit{label} is not sufficiently local.
[952]101This leaves the problem, addressed below, of calculating whether a label is indeed close enough' for the simpler translation to be used.
102
103Crucially, the above translation demonstrates the difficulty in predicting how many clock cycles a pseudoinstruction will take to execute.
104A conditional jump may be mapped to a single machine instruction or a block of three.
105Perhaps more insidious, the number of cycles needed to execute the instructions in the two branches of a translated conditional jump may be different.
[954]106Depending 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}.
[952]107These issues must also be dealt with in order to prove that the translation pass preserves the concrete complexity of the code.
108
109The question remains: how do we decide whether to expand a jump into an \texttt{SJMP} or an \texttt{LJMP}?
[954]110To understand why this problem is not trivial, consider the following snippet of assembly code:
[952]111\begin{displaymath}
112\text{dpm: finish me}
113\end{displaymath}
[954]114
[952]115As 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.
116However, shrinking these \texttt{LJMP}s may in turn depend on shrinking $l$ to an \texttt{SJMP}, as it is perfectly possible to jump backwards.
[954]117In short, unless we can somehow break this loop of circularity, and similar knotty configurations of jumps, we are stuck with a suboptimal solution to the expanding jumps problem.
[952]118
[954]119How we went about resolving this problem affected the shape of our proof of correctness for the whole assembler in a rather profound way.
120We 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.
[952]121Using this technique, solutions can fail to exist, and the proof quickly descends into a diabolical quagmire.
122
[954]123Abandoning 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]124Assuming the existence of a correct policy, we proved the implementation of the assembler correct.
[954]125Further, we proved that the assembler fails to assemble an assembly program if and only if a correct policy does not exist.
[952]126Policies 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.
[954]127The first case would constitute a serious compiler error, and hopefully certifying the rest of the compiler would rule this possibility out.
128The second case is unavoidable---certified compiler or not, trying to load a huge program into a small code memory will break \emph{something}.
[952]129
[954]130The rest of this paper is a detailed description of this proof.
131
[927]132% ---------------------------------------------------------------------------- %
133% SECTION                                                                      %
134% ---------------------------------------------------------------------------- %
135\subsection{Overview of the paper}
136\label{subsect.overview.of.the.paper}
[952]137In Section~\ref{sect.matita} we provide a brief overview of the Matita proof assistant for the unfamiliar reader.
138In Section~\ref{sect.the.proof} we discuss the design and implementation of the proof proper.
139In Section~\ref{sect.conclusions} we conclude.
[927]140
141% ---------------------------------------------------------------------------- %
142% SECTION                                                                      %
143% ---------------------------------------------------------------------------- %
[952]144\section{Matita}
145\label{sect.matita}
[927]146
[952]147
[927]148% ---------------------------------------------------------------------------- %
149% SECTION                                                                      %
150% ---------------------------------------------------------------------------- %
[952]151\section{The proof}
152\label{sect.the.proof}
[927]153
[952]154  \begin{itemize}
155   \item use of dependent types to throw away wrong programs that would made
156    the statement for completeness complex. E.g. misuse of addressing modes,
157    jumps to non existent labels, etc.
158   \item try to go for small reflection; avoid inductive predicates that require
159    tactics (inversion, etc.) that do not work well with dependent types; small
160    reflection usually does
161   \item use coercions to simulate Russell; mix together the proof styles
162    a la Russell (for situations with heavy dependent types) and the standard
163    one
164  \end{itemize}
165
[927]166% ---------------------------------------------------------------------------- %
167% SECTION                                                                      %
168% ---------------------------------------------------------------------------- %
169\section{Conclusions}
170\label{sect.conclusions}
171
[955]172\subsection{Use of dependent types and Russell}
173\label{subsect.use.of.dependent.types.and.Russell}
[952]174
[955]175Our formalisation makes sparing use of dependent types.
176In certain datastructures, such as tries and vectors, they are used to guarantee invariants.
177However, we have currently shyed away from making extensive use of dependent types and inductive predicates in the proof of correctness for the assembler itself.
178This is because complex dependent types and inductive predicates tend not to co\"operate particularly well with tactics such as inversion.
[952]179
[955]180In their stead, where it makes sense, we used Matita's implementation of Russell~\cite{}.
181
[952]182\subsection{Related work}
183\label{subsect.related.work}
184
[927]185\bibliography{cpp-2011.bib}
186
[917]187\end{document}
Note: See TracBrowser for help on using the repository browser.