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

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

work from yesterday

File size: 14.9 KB
Line 
1\documentclass{llncs}
2
3\usepackage{amsmath}
4\usepackage[english]{babel}
5\usepackage[colorlinks]{hyperref}
6
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}
10
11\bibliographystyle{splncs03}
12
13\begin{document}
14
15\maketitle
16
17\begin{abstract}
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...
21\end{abstract}
22
23% ---------------------------------------------------------------------------- %
24% SECTION                                                                      %
25% ---------------------------------------------------------------------------- %
26\section{Introduction}
27\label{sect.introduction}
28
29We consider the formalisation of an assembler for the Intel MCS-51 8-bit microprocessor in the Matita proof assistant.
30This 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.
31
32The MCS-51 dates from the early 1980s and is commonly called the 8051/8052.
33Despite the microprocessor's age, derivatives are still widely manufactured by a number of semiconductor foundries and the processor is widely used, especially in embedded systems development, where well-tested, cheap, predictable microprocessors find a niche.
34
35The MCS-51 has a relative paucity of features compared to its more modern brethren.
36In 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.
37Instead, 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.
38It is important to stress that this timing information, unlike in more sophisticated processors, is not an estimate, it is a definition.
39With 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.
40
41This predicability of timing information is especially attractive to the CerCo consortium.
42We are in the process of constructing a cost-preserving certified 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.
43However, the MCS-51's paucity of features is a double edged sword.
44In particular, the MCS-51 features relatively miniscule memory spaces (including read-only code memory, stack and internal/external RAM) by modern standards.
45As a result, our compiler, to have any sort of hope of successfully compiling realistic C programs, must produce `tight' machine code.
46This is not simple.
47
48To begin to understand the problems we faced, we here focus on a single issue in the MCS-51's instruction set: unconditional jumps.
49The MCS-51 features three conditional jump instructions: \texttt{LJMP} and \texttt{SJMP}---`long jump' and `short jump' respectively---and \texttt{AJMP}, an 11-bit oddity of the MCS-51 that we choose to ignore for simplicity's sake.\footnote{Ignoring \texttt{AJMP} and its analogue \texttt{ACALL} is not idiosyncratic.  The Small Device C Compiler (SDCC), 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.}
50Each of these three instructions expects arguments in different sizes and behaves in different ways.
51For instance, \texttt{SJMP} expects an 8-bit offset which is added to the current program counter to produce a relative, local jump.
52In contrast, \texttt{LJMP} expects a 16-bit addressing mode and can jump to any address in the MCS-51's memory space.
53As a result, 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 instruction that produces the required effect should be picked.
54
55Having the compiler attempt to select the smallest possible jump instruction was deemed too high a burden, unneccessarily complicating the compilation chain.
56Instead, we decided to have the compiler target an assembly language, complete with pseudoinstructions.
57These pseudoinstructions included generic \texttt{Jmp} and \texttt{Call} instructions.
58We also implemented labels, 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.
59This latter feature will ease any later consideration of separate compilation in the CerCo compiler.
60Further, our conditional jumps to labels behave differently from their machine code counterparts.
61At the machine code level, conditional jumps may only jump to a relative offset of the current program counter, limiting their scope.
62However, at the assembly level, conditional jumps may jump to a label that appears anywhere in the program, significantly liberalising the use of conditional jumps.
63
64However, in line with CerCo's goal to produce a verified compilation chain, this assembly language to machine language translation must also be proved correct.
65Assemblers are not as simple as they first appear, and are in fact quite hard to formalise.
66In particular, the CerCo assembler needs to expand labels and pseudoinstructions into a correct representation at the machine level.
67
68Trying to na\"ively relate assembly programs with their machine code counterparts simply does not work.
69Machine 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.
70More generally, memory addresses can only be compared with other memory addresses.
71However, checking that memory addresses are only compared against each other at the assembly level is in fact undecidable.
72In short, the full preservation of the semantics of the two languages is impossible.
73
74A further set of complications is added by the peculiarities of the CerCo project itself.
75As mentioned, the CerCo consortium is in the business of constructing a verified compiler for the C programming language.
76However, unlike CompCert---currently representing the state of the art for `industrial grade' verified compilers---and similar projects, CerCo considers not just the \emph{intensional correctness} of the compiler, but also its \emph{extensional correctness}.
77That 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.
78However 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.
79CerCo aims to expand the current state of the art by producing a compiler where this temporal degradation is guaranteed not to happen.
80
81In order to achieve this CerCo imposes a cost model on programs, or more specifically, on simple blocks of instructions.
82This 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.
83However, 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.
84This also applies for the translation from assembly language to machine code.
85How do we assign a cost to a pseudoinstruction?
86
87There is one snag: how to expand jumps.
88As mentioned, conditional jumps at the assembly level can jump to a label appearing anywhere in the program.
89However, at the machine code level, conditional jumps are limited to jumping `locally', using an 8-bit relative offset of the program counter.
90To 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'):
91\begin{displaymath}
92\begin{array}{r@{\quad}l@{\;\;}l@{\qquad}c@{\qquad}l@{\;\;}l}
93       & \mathtt{JZ}  & label                      &                 & \mathtt{JZ}   & \text{size of \texttt{SJMP} instruction} \\
94       & \ldots       &                            & \text{translates to}   & \mathtt{SJMP} & \text{size of \texttt{LJMP} instruction} \\
95label: & \mathtt{MOV} & \mathtt{A}\;\;\mathtt{B}   & \Longrightarrow & \mathtt{LJMP} & \text{address of \textit{label}} \\
96       &              &                            &                 & \ldots        & \\
97       &              &                            &                 & \mathtt{MOV}  & \mathtt{A}\;\;\mathtt{B}
98\end{array}
99\end{displaymath}
100In the translation, if \texttt{JZ} fails, we fall through to the \texttt{SJMP} which jumps over the \texttt{LJMP}.
101Naturally, 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.
102Similarly, we must also work out whether to expand an unconditional jump pse
103This leaves the problem, addressed below, of calculating whether a label is indeed `close enough' for the simpler translation to be used.
104
105Crucially, the above translation demonstrates the difficulty in predicting how many clock cycles a pseudoinstruction will take to execute.
106A conditional jump may be mapped to a single machine instruction or a block of three.
107Perhaps more insidious, the number of cycles needed to execute the instructions in the two branches of a translated conditional jump may be different.
108Depending on the semiconductor manufacturer, an \texttt{SJMP} could in theory take a different number of clock cycles to execute than an \texttt{LJMP}.
109These issues must also be dealt with in order to prove that the translation pass preserves the concrete complexity of the code.
110
111The question remains: how do we decide whether to expand a jump into an \texttt{SJMP} or an \texttt{LJMP}?
112This problem is far from trivial.
113To understand why, consider the following snippet of assembly code:
114\begin{displaymath}
115\text{dpm: finish me}
116\end{displaymath}
117As 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.
118However, shrinking these \texttt{LJMP}s may in turn depend on shrinking $l$ to an \texttt{SJMP}, as it is perfectly possible to jump backwards.
119In short, unless we can somehow break this loop of circularity, we are stuck with a suboptimal solution to the expanding jumps problem.
120
121How we go about resolving this problem affected the shape of our proof of correctness for the whole assembler in a rather profound way.
122We first attempted to synthesize a solution bottom up.
123That is, starting with no solution, we gradually refine a solution using the same functions that implement the jump expansion.
124Using this technique, solutions can fail to exist, and the proof quickly descends into a diabolical quagmire.
125
126Abandoning this attempt, we instead split the `policy', i.e. the decision over how any particular jump should be expanded, from the implementation.
127Assuming the existence of a correct policy, we proved the implementation of the assembler correct.
128Further, we proved that the assembler fails to assemble a file if and only if a correct policy does not exist.
129Policies 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.
130The 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% ---------------------------------------------------------------------------- %
133% SECTION                                                                      %
134% ---------------------------------------------------------------------------- %
135\subsection{Overview of the paper}
136\label{subsect.overview.of.the.paper}
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.
140
141% ---------------------------------------------------------------------------- %
142% SECTION                                                                      %
143% ---------------------------------------------------------------------------- %
144\section{Matita}
145\label{sect.matita}
146
147
148% ---------------------------------------------------------------------------- %
149% SECTION                                                                      %
150% ---------------------------------------------------------------------------- %
151\section{The proof}
152\label{sect.the.proof}
153
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
166% ---------------------------------------------------------------------------- %
167% SECTION                                                                      %
168% ---------------------------------------------------------------------------- %
169\section{Conclusions}
170\label{sect.conclusions}
171
172\subsection{Use of dependent types}
173\label{subsect.use.of.dependent.types}
174
175As it stands our use of complex dependent types is limited in the formalisation.
176Where it made sense, for example in data structures like tries and vectors, we have made limited use of dependent types.
177
178\subsection{Related work}
179\label{subsect.related.work}
180
181\bibliography{cpp-2011.bib}
182
183\end{document}
Note: See TracBrowser for help on using the repository browser.