Changeset 952

Jun 15, 2011, 10:15:00 AM (10 years ago)

work from yesterday

1 edited


  • src/ASM/CPP2011/cpp-2011.tex

    r947 r952  
    6 \title{Proving the correctness of an assembler for the MCS-51 microprocessor}
     7\title{On the correctness of an assembler for the Intel MCS-51 microprocessor}
    78\author{Jaap Boender \and Dominic P. Mulligan \and Claudio Sacerdoti Coen}
    89\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.
    25 \begin{enumerate}
    26  \item Assembler are considered simple pieces of code, but this is not the
    27        case and they can be quite hard to formalize.
    28  \item We are interested in an assembler for the legacy MCS family.
    29  \item What does it do:
    30   \begin{enumerate}
    31    \item translates from human readabale to machine readable
    32    \item expand labels
    33    \item expand pseudo instructions by optimizing the expansion
    34   \end{enumerate}
    35  \item Problems due to the expansion/optimization:
    36   \begin{itemize}
    37    \item operations that fetch from the code memory do not make sense at
    38          pseudo level
    39    \item operations that combine the PC with constant shifts do not make sense
    40          at pseudo level
    41    \item more generally, memory addresses, wherever they are (memory, registers)
    42          can only be copied and compared with other memory addresses
    43          $\Rightarrow$ need to trace memory addresses UNDECIDABLE PROBLEM
    44    \item consequence: full preservation of the semantics becomes IMPOSSIBLE
    45   \end{itemize}
    46  \item We are also interested in intensional properties
    47   \begin{itemize}
    48    \item the semantics is sensible to the timing (e.g. I/O, interrupts)
    49    \item to show that the semantics is preserved, one needs to assign a
    50          precise cost model to the pseudo instructions
    51    \item the cost model is induced by the compilation itself
    52          $\Rightarrow$ ``recursive'' statement
    53   \end{itemize}
    54  \item Finally, the optimizing expansion itself: certified compilers are
    55    usually proved to be correct, but we want more: completeness and optimality
    56    of the expansion.
    57   \begin{itemize}
    58    \item the optimization starts with a non solution
    59     and incrementally refines it to a solution. At each step it uses functions
    60     that are used to implement the expansion and that needs to be correct.
    61     The solution can fail to exist. The proof becomes a mess
    62    \item idea: split the policy from the implementation; prove the
    63     implementation to be correct w.r.t. any correct policy; provide a correct
    64     policy (when it exists) and show that it is also complete and optimal.
    65     Show that the assembler fails iff a correct policy does not exist
    66     (completeness).
    67   \end{itemize}
    68  \item Additional issues:
     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.
     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.
     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.
     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.
     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.
     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.
     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.
     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.
     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.
     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?
     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'):
     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}
     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.
     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.
     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:
     115\text{dpm: finish me}
     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.
     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.
     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}.
     132% ---------------------------------------------------------------------------- %
     133% SECTION                                                                      %
     134% ---------------------------------------------------------------------------- %
     135\subsection{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.
     141% ---------------------------------------------------------------------------- %
     142% SECTION                                                                      %
     143% ---------------------------------------------------------------------------- %
     148% ---------------------------------------------------------------------------- %
     149% SECTION                                                                      %
     150% ---------------------------------------------------------------------------- %
     151\section{The proof}
    69154  \begin{itemize}
    70155   \item use of dependent types to throw away wrong programs that would made
    78163    one
    79164  \end{itemize}
    80 \end{itemize}
    82 \end{enumerate}
    84 This paper discusses the proof of correctness of an assembler for the Intel MCS-51 8-bit family of microprocessors.
    86 The work presented herein is a component of the EU-funded CerCo (`Certified Complexity') project.
    87 CerCo aims to produce a verified compiler from a large subset of C to the machine language of a microprocessor commonly used in embedded systems.
    88 In this respect, CerCo aims to go beyond the state of the art in verified compiler technology.
    90 Arguably, the CompCert C compiler currently represents the state of the art in the field of verified compilers.
    91 However, the verified backend of the CompCert C compiler stops at Power PC and ARM assembly \emph{languages}.
    92 Assembly of these languages into machine code is left untrusted.
    93 We aim to go further.
    95 The MCS-51 is CerCo's target processor.
    97 % ---------------------------------------------------------------------------- %
    98 % SECTION                                                                      %
    99 % ---------------------------------------------------------------------------- %
    100 \subsection{CerCo}
    101 \label{subsect.cerco}
    103 % ---------------------------------------------------------------------------- %
    104 % SECTION                                                                      %
    105 % ---------------------------------------------------------------------------- %
    106 \subsection{Overview of the paper}
    107 \label{subsect.overview.of.the.paper}
    109 % ---------------------------------------------------------------------------- %
    110 % SECTION                                                                      %
    111 % ---------------------------------------------------------------------------- %
    112 \section{The proof}
    113 \label{sect.the.proof}
    115 % ---------------------------------------------------------------------------- %
    116 % SECTION                                                                      %
    117 % ---------------------------------------------------------------------------- %
    118 \subsection{Matita}
    119 \label{subsect.matita}
    121166% ---------------------------------------------------------------------------- %
     172\subsection{Use of dependent types}
     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.
     178\subsection{Related work}
Note: See TracChangeset for help on using the changeset viewer.