source: Deliverables/D4.1/ITP-Paper/itp-2011.tex @ 1202

Last change on this file since 1202 was 1202, checked in by mulligan, 8 years ago

more changes, changed mention of atkey's jvm

File size: 54.1 KB
Line 
1\documentclass[10pt, a4paper]{llncs}
2
3\usepackage{amsfonts}
4\usepackage{amsmath}
5\usepackage{amssymb} 
6\usepackage[english]{babel}
7\usepackage{color}
8\usepackage{fancybox}
9\usepackage{graphicx}
10\usepackage[colorlinks]{hyperref}
11\usepackage[utf8x]{inputenc}
12\usepackage{listings}
13\usepackage{mdwlist}
14\usepackage{microtype}
15\usepackage{stmaryrd}
16\usepackage{url}
17
18\newlength{\mylength} 
19\newenvironment{frametxt}%
20        {\setlength{\fboxsep}{5pt}
21                \setlength{\mylength}{\linewidth}%
22                \addtolength{\mylength}{-2\fboxsep}%
23                \addtolength{\mylength}{-2\fboxrule}%
24                \Sbox
25                \minipage{\mylength}%
26                        \setlength{\abovedisplayskip}{0pt}%
27                        \setlength{\belowdisplayskip}{0pt}%
28                }%
29                {\endminipage\endSbox
30                        \[\fbox{\TheSbox}\]}
31
32\lstdefinelanguage{matita-ocaml}
33  {keywords={definition,coercion,lemma,theorem,remark,inductive,record,qed,let,in,rec,match,return,with,Type,try,on,to},
34   morekeywords={[2]whd,normalize,elim,cases,destruct},
35   morekeywords={[3]type,of,val,assert,let,function},
36   mathescape=true,
37  }
38\lstset{language=matita-ocaml,basicstyle=\scriptsize\tt,columns=flexible,breaklines=false,
39        keywordstyle=\bfseries, %\color{red}\bfseries,
40        keywordstyle=[2]\bfseries, %\color{blue},
41        keywordstyle=[3]\bfseries, %\color{blue}\bfseries,
42%        commentstyle=\color{green},
43%        stringstyle=\color{blue},
44        showspaces=false,showstringspaces=false}
45\DeclareUnicodeCharacter{8797}{:=}
46\DeclareUnicodeCharacter{10746}{++}
47\DeclareUnicodeCharacter{9001}{\ensuremath{\langle}}
48\DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}}
49
50\lstset{
51  extendedchars=false,
52  inputencoding=utf8x,
53  tabsize=1
54}
55
56\setlength{\belowcaptionskip}{0pt}
57\setlength{\textfloatsep}{1em}
58
59\begin{document}
60
61\author{Dominic P. Mulligan \and Claudio Sacerdoti Coen}
62\authorrunning{D. P. Mulligan \and C. Sacerdoti Coen}
63\institute{\vspace{-2em}}
64% \institute{Dipartimento di Scienze dell'Informazione,\\ Universit\`a di Bologna}
65
66\bibliographystyle{plain}
67
68\title{An executable formalisation of the MCS-51 microprocessor in Matita
69 \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}}
70
71\maketitle
72
73\begin{abstract}
74We summarise the formalisation of emulators for the MCS-51 microprocessor in O'Caml and the Matita proof assistant.
75The MCS-51 is a widely used microprocessor, especially popular in embedded devices.
76
77The O'Caml emulator is intended to be `feature complete' with respect to the MCS-51 device.
78However, the Matita emulator is intended to be used as a target for a certified, complexity preserving C compiler, as part of the EU-funded CerCo project.
79As a result, not all features of the MCS-51 are formalised in the Matita emulator.
80Both the O'Caml and Matita emulators are `executable'.
81Assembly programs may be animated within Matita, producing a trace of instructions executed.
82\end{abstract}
83
84%\begin{keywords}
85%Hardware formalisation, Matita, dependent types, CerCo
86%\end{keywords}
87
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89% SECTION                                                                      %
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91\section{Introduction}
92\label{sect.introduction}
93
94Formal methods aim to increase our confidence in the design and implementation of software.
95Ideally, all software should come equipped with a formal specification and a proof of correctness for the corresponding implementation.
96The majority of programs are written in high level languages and then compiled into low level ones.
97Specifications are therefore also given at a high level and correctness can be proved by reasoning on the program's source code.
98The code that is actually run is not the high level source code that we reason on, but low level code generated by the compiler.
99
100Questions now arise:  What properties are preserved during compilation?
101What properties are affected by the compilation strategy?
102To what extent can you trust your compiler in preserving those properties?
103These and other questions motivate a current `hot topic' in computer science research: \emph{compiler verification} (e.g.~\cite{chlipala:verified:2010,leroy:formal:2009}, and so on).
104So far, the field has only been focused on the first and last questions.
105Much attention has been placed on verifying compiler correctness with respect to extensional properties of programs.
106These are `easily' preserved during compilation.
107
108If we consider intentional properties of programs---space, time, and so forth---the situation is more complex.
109To express these properties, and reason about them, we must adopt a cost model that assigns a cost to single, or blocks, of instructions.
110A compositional cost model, assigning the same cost to all occurrences of one instruction, would be ideal.
111However, compiler optimisations are inherently non-compositional: each occurrence of a high level instruction may be compiled in a different way depending on its context.
112Therefore both the cost model and intentional specifications are affected by the compilation process.
113
114In the CerCo project (`Certified Complexity')~\cite{cerco:2011} we approach the problem of reasoning about intentional properties of programs as follows.
115We are currently developing a compiler that induces a cost model on high level source code.
116Costs are assigned to each block of high level instructions by considering the costs of the corresponding blocks of compiled code.
117The cost model is therefore inherently non-compositional, but has the potential to be extremely \emph{precise}, capturing a program's \emph{realistic} cost.
118That is, the compilation process is taken into account, not ignored.
119A prototype compiler, where no approximation of the cost is provided, has been developed.
120(The technical details of the cost model is explained in~\cite{amadio:certifying:2010}.)
121
122We believe that our approach is applicable to certifying real time programs.
123A user can certify that `deadlines' are met whilst wringing as many clock cycles from the processor---using a cost model that does not over-estimate---as possible.
124
125Our approach is also relevant to compiler verification and construction.
126\emph{An optimisation specified only extensionally is only half specified}; the optimisation may preserve the denotational semantics of a program, but there is no guarantee that intentional properties of the program improve.
127
128Another potential application is the completeness and correctness of the compilation process in the presence of space constraints.
129A compiler could reject a source program targetting an embedded system when the size of the compiled code exceeds available ROM size.
130Preservation of semantics may only be required for those programs that do not exhaust the stack or heap.
131The statement of completeness of the compiler must take into account a realistic cost model.
132
133With the CerCo methodology, we assume we can assign to object code exact and realistic costs for sequential blocks of instructions.
134The WCET community has developed complex tools for bounding the worst-case execution times of sequential blocks on modern processors.
135WCET analysis takes place at the object code level.
136However, it is more convenient to reason about programs at a much higher-level of abstraction.
137Therefore, the analysis must be reflected back onto the original source code.
138This reflection process is completely `untrusted' and makes strong assumptions about the internal design and correctness of the compiler.
139For example, some WCET analysis tools, to maximise precision, require a programmer-provided strict upper bound on the number of loop iterations.
140Compiler optimizations could rearrange code in such a manner that the upper bound is no longer strict.
141The certified CerCo C compiler validates such strong assumptions, and a certified analysis tool could be obtained by combining the CerCo compiler with any certified WCET tool.
142
143We are interested in building a fully certified tool.
144However we are not able to build a certified WCET tool \emph{and} certified C compiler within the confines of the CerCo project.
145We therefore focus on certifying the compiler by targetting a microprocessor where complex WCET analyses are not required.
146
147Caching, memory effects, and advanced features such as branch prediction all have an effect on the complexity of WCET analyses (see~\cite{bate:wcet:2011,yan:wcet:2008}, and so on).
148CerCo therefore decided to focus on 8-bit microprocessors, which are still used in embedded systems.
149These have a predictable, precise cost model due to their relative paucity of features.
150Manufacturer timesheets provide \emph{exact guarantees} for the number of processor cycles each instruction will take to execute.
151
152We have fully formalised an executable formal semantics of a family of 8-bit Freescale microprocessors~\cite{oliboni:matita:2008}, and provided a similar executable formal semantics for the MCS-51 microprocessor.
153The latter is what we describe in this paper.
154The focus of the formalisation has been on capturing the intentional behaviour of the processor; this is not novel.
155However, the design of the MCS-51 itself has caused problems in the formalisation.
156For example, the MCS-51 has a highly unorthogonal instruction set.
157To cope with this unorthogonality, and to produce an executable specification, we rely on the dependent types of Matita, an interactive proof assistant~\cite{asperti:user:2007}.
158The manner in which we combined dependent types and coercions to handle this problem is novel.
159
160\paragraph*{The MCS-51}\quad
161The MCS-51 is an 8-bit microprocessor.
162Commonly called the 8051, since its introduction by Intel the processor has become a popular component of embedded systems.
163Despite being manufactured \emph{en masse}, there is not yet a formal model of the MCS-51.
164
165The 8051 is a well documented processor, with very few underspecified behaviours (almost all of these correspond to erroneous usage of the processor).
166The processor also has the support of numerous open source and commercial tools, such as compilers and emulators.
167For instance, the open source Small Device C Compiler (SDCC) recognises a dialect of C~\cite{sdcc:2010}, and other compilers for BASIC, Forth and Modula-2 are also extant.
168An open source emulator for the processor, MCU 8051 IDE, is also available~\cite{mcu8051ide:2010}.
169Both MCU 8051 IDE and SDCC were used in for validating the formalisation.
170
171\begin{figure*}
172\centering
173\setlength{\unitlength}{0.87pt}
174\small{
175\begin{picture}(410,250)(-50,200)
176%\put(-50,200){\framebox(410,250){}}
177\put(12,410){\makebox(80,0)[b]{Internal (256B)}}
178\put(13,242){\line(0,1){165}}
179\put(93,242){\line(0,1){165}}
180\put(13,407){\line(1,0){80}}
181\put(12,400){\makebox(0,0)[r]{0h}}  \put(14,400){\makebox(0,0)[l]{Register bank 0}}
182\put(13,393){\line(1,0){80}}
183\put(12,386){\makebox(0,0)[r]{8h}}  \put(14,386){\makebox(0,0)[l]{Register bank 1}}
184\put(13,379){\line(1,0){80}}
185\put(12,372){\makebox(0,0)[r]{10h}}  \put(14,372){\makebox(0,0)[l]{Register bank 2}}
186\put(13,365){\line(1,0){80}}
187\put(12,358){\makebox(0,0)[r]{18h}} \put(14,358){\makebox(0,0)[l]{Register bank 3}}
188\put(13,351){\line(1,0){80}}
189\put(12,344){\makebox(0,0)[r]{20h}} \put(14,344){\makebox(0,0)[l]{Bit addressable}}
190\put(13,323){\line(1,0){80}}
191\put(12,316){\makebox(0,0)[r]{30h}}
192  \put(14,309){\makebox(0,0)[l]{\quad \vdots}}
193\put(13,291){\line(1,0){80}}
194\put(12,284){\makebox(0,0)[r]{80h}}
195  \put(14,263){\makebox(0,0)[l]{\quad \vdots}}
196\put(12,249){\makebox(0,0)[r]{ffh}}
197\put(13,242){\line(1,0){80}}
198
199\qbezier(-2,407)(-6,407)(-6,393)
200\qbezier(-6,393)(-6,324)(-10,324)
201\put(-12,324){\makebox(0,0)[r]{Indirect/stack}}
202\qbezier(-6,256)(-6,324)(-10,324)
203\qbezier(-2,242)(-6,242)(-6,256)
204
205\qbezier(94,407)(98,407)(98,393)
206\qbezier(98,393)(98,349)(102,349)
207\put(104,349){\makebox(0,0)[l]{Direct}}
208\qbezier(98,305)(98,349)(102,349)
209\qbezier(94,291)(98,291)(98,305)
210
211\put(102,242){\framebox(20,49){SFR}}
212% bit access to sfrs?
213
214\qbezier(124,291)(128,291)(128,277)
215\qbezier(128,277)(128,266)(132,266)
216\put(134,266){\makebox(0,0)[l]{Direct}}
217\qbezier(128,257)(128,266)(132,266)
218\qbezier(124,242)(128,242)(128,256)
219
220\put(164,410){\makebox(80,0)[b]{External (64kB)}}
221\put(164,220){\line(0,1){187}}
222\put(164,407){\line(1,0){80}}
223\put(244,220){\line(0,1){187}}
224\put(164,242){\line(1,0){80}}
225\put(163,400){\makebox(0,0)[r]{0h}}
226\put(164,324){\makebox(80,0){Paged access}}
227  \put(164,310){\makebox(80,0){Direct/indirect}}
228\put(163,235){\makebox(0,0)[r]{80h}}
229  \put(164,228){\makebox(80,0){\vdots}}
230  \put(164,210){\makebox(80,0){Direct/indirect}}
231
232\put(264,410){\makebox(80,0)[b]{Code (64kB)}}
233\put(264,220){\line(0,1){187}}
234\put(264,407){\line(1,0){80}}
235\put(344,220){\line(0,1){187}}
236\put(263,400){\makebox(0,0)[r]{0h}}
237  \put(264,228){\makebox(80,0){\vdots}}
238  \put(264,324){\makebox(80,0){Direct}}
239  \put(264,310){\makebox(80,0){PC relative}}
240\end{picture}}
241\caption{The 8051 memory model}
242\label{fig.memory.layout}
243\end{figure*}
244
245The 8051 has a relatively straightforward architecture.
246A high-level overview of the processor's memory layout, along with the ways in which different memory spaces may be addressed, is provided in Figure~\ref{fig.memory.layout}.
247
248Processor RAM is divided into numerous segments, with the most prominent division being between internal and (optional) external memory.
249Internal memory, commonly provided on the die itself with fast access, is composed of 256 bytes, but, in direct addressing mode, half of them are overloaded with 128 bytes of memory-mapped Special Function Registers (SFRs).
250SFRs control the operation of the processor.
251Internal RAM (IRAM) is divided again into 8 general purpose bit-addressable registers (R0--R7).
252These sit in the first 8 bytes of IRAM, though can be programmatically `shifted up' as needed.
253Bit memory, followed by a small amount of stack space, resides in the memory space immediately following the register banks.
254What remains of IRAM may be treated as general purpose memory.
255A schematic view of IRAM is also provided in Figure~\ref{fig.memory.layout}.
256
257External RAM (XRAM), limited to a maximum size of 64 kilobytes, is optional, and may be provided on or off chip, depending on the vendor.
258XRAM is accessed using a dedicated instruction, and requires 16 bits to address fully.
259External code memory (XCODE) is often stored as an EPROM, and limited to 64 kilobytes in size.
260However, depending on the particular processor model, a dedicated on-die read-only memory area for program code (ICODE) may be supplied.
261
262Memory may be addressed in numerous ways: immediate, direct, indirect, external direct and code indirect.
263As the latter two addressing modes hint, there are some restrictions enforced by the 8051, and its derivatives, on which addressing modes may be used with specific types of memory.
264For instance, the extra 128 bytes of IRAM of the 8052 cannot be addressed using indirect addressing; rather, external (in)direct addressing must be used.
265Some memory segments are also addressed using 8-bit pointers while others require 16-bits.
266
267The 8051 possesses an 8-bit Arithmetic and Logic Unit (ALU), with a variety of instructions for performing arithmetic and logical operations on bits and integers.
268Two 8-bit general purpose accumulators, A and B, are provided.
269
270Communication with the device is handled by a UART serial port and controller.
271This can operate in numerous modes.
272Serial baud rate is determined by one of two 16-bit timers included with the 8051, which can be set to multiple modes of operation.
273(The 8052 provides an additional 16-bit timer.)
274The 8051 also provides a 4 byte bit-addressable I/O port.
275
276The programmer may take advantage of an interrupt mechanism.
277This is especially useful when dealing with I/O involving the serial device, as an interrupt can be set when a whole character is sent or received via the UART.
278
279Interrupts immediately halt the flow of execution of the processor, and cause the program counter to jump to a fixed address, where the requisite interrupt handler is stored.
280However, interrupts may be set to one of two priorities: low and high.
281The interrupt handler of an interrupt with high priority is executed ahead of the interrupt handler of an interrupt of lower priority, interrupting a currently executing handler of lower priority, if necessary.
282
283The 8051 has interrupts disabled by default.
284The programmer is free to handle serial input and output manually, by poking serial flags in the SFRs.
285`Exceptional circumstances' that would otherwise trigger an interrupt on more modern processors, (e.g. division by zero) are also signalled by setting flags.
286
287%\begin{figure}[t]
288%\begin{center}
289%\includegraphics[scale=0.5]{iramlayout.png}
290%\end{center}
291%\caption{Schematic view of 8051 IRAM layout}
292%\label{fig.iram.layout}
293%\end{figure}
294
295\paragraph*{Overview of paper}\quad
296In Section~\ref{sect.design.issues.formalisation} we discuss design issues in the development of the formalisation.
297In Section~\ref{sect.validation} we discuss validation of the emulator to ensure that what we formalised was an accurate model of an MCS-51 series microprocessor.
298In Section~\ref{sect.related.work} we describe previous work, with an eye toward describing its relation with the work described herein.
299In Section~\ref{sect.conclusions} we conclude.
300
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302% SECTION                                                                      %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304\section{Design issues in the formalisation}
305\label{sect.design.issues.formalisation}
306
307We implemented two emulators, one in O'Caml and one in Matita.
308The O'Caml emulator is intended to be `feature complete' with respect to the MCS-51 device.
309However, the Matita emulator is intended to be used as a target for a certified, complexity preserving C compiler.
310As a result, not all features of the MCS-51 are formalised in the Matita emulator.
311
312We designed the O'Caml emulator to be as efficient as possible, under the constraint that it would be eventually translated into Matita.
313One performance drain in the O'Caml emulator is the use of purely functional map datastructures to represent memory spaces, used to maintain the close correspondence between the Matita and O'Caml emulators.
314
315Matita~\cite{asperti:user:2007} is a proof assistant based on the Calculus of Coinductive constructions, similar to Coq.
316As a programming language, Matita corresponds to the functional fragment of O'Caml extended with dependent types.
317Matita also features a rich higher-order logic for reasoning about programs, including a universe hierarchy with a single impredicative universe, \texttt{Prop}, and potentially infinitely many predicative universes \texttt{Type[i]} for $0 \geq i$.
318Unlike O'Caml, all recursive functions admitted by the Matita typechecker must be structurally recursive and total.
319
320We box Matita code to distinguish it from O'Caml code.
321In Matita `\texttt{$?$}' and  `\texttt{$\ldots$}' are arguments to be inferred automatically.
322
323A full account of the formalisation can be found in~\cite{cerco-report-code:2011}.
324All source code is available from the CerCo project website~\cite{cerco-report-code:2011}.
325
326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327% SECTION                                                                      %
328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329\subsection{Representation of bytes, words, etc.}
330\label{subsect.representation.integers}
331
332\begin{figure}[t]
333\begin{minipage}[t]{0.45\textwidth}
334\vspace{0pt}
335\begin{lstlisting}
336type 'a vect = bit list
337type nibble = [`Sixteen] vect
338type byte = [`Eight] vect
339let split_word w = split_nth 4 w
340let split_byte b = split_nth 2 b
341\end{lstlisting}
342\end{minipage}
343%
344\begin{minipage}[t]{0.55\textwidth}
345\vspace{0pt}
346\begin{lstlisting}
347type 'a vect
348type word = [`Sixteen] vect
349type byte = [`Eight] vect
350val split_word: word -> byte * word
351val split_byte: byte -> nibble * nibble
352\end{lstlisting}
353\end{minipage}
354\caption{Sample of O'Caml implementation and interface for bitvectors module}
355\label{fig.ocaml.implementation.bitvectors}
356\end{figure}
357
358The formalization of MCS-51 must deal with bytes (8-bits), words (16-bits), and also more esoteric quantities (7, 3 and 9-bits).
359To avoid difficult-to-trace size mismatch bugs, we represented all quantities using bitvectors, i.e. fixed length vectors of booleans.
360In the O'Caml emulator, we `faked' bitvectors using phantom types~\cite{leijen:domain:1999} implemented with polymorphic variants~\cite{garrigue:programming:1998}, as in Figure~\ref{fig.ocaml.implementation.bitvectors}.
361From within the bitvector module (top) bitvectors are just lists of bits and no guarantee is provided on sizes.
362However, the module's interface (bottom) enforces size invariants in the rest of the code.
363
364In Matita, we are able to use the full power of dependent types to always work with vectors of a known size:
365\begin{lstlisting}[frame=single]
366inductive Vector (A: Type[0]): nat $\rightarrow$ Type[0] ≝
367  VEmpty: Vector A O
368| VCons: $\forall$n: nat. A $\rightarrow$ Vector A n $\rightarrow$ Vector A (S n).
369\end{lstlisting}
370\texttt{BitVector} is a specialization of \texttt{Vector} to \texttt{bool}.
371We may use Matita's type system to provide precise typings for functions that are polymorphic in the size without code duplication:
372\begin{lstlisting}[frame=single]
373let rec split (A: Type[0]) (m,n: nat) on m: Vector A (m+n) $\rightarrow$ (Vector A m)$\times$(Vector A n) := ...
374\end{lstlisting}
375
376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377% SECTION                                                                      %
378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379\subsection{Representing memory}
380\label{subsect.representing.memory}
381
382The MCS-51 has numerous disjoint memory spaces addressed by differently sized pointers.
383In the O'Caml implementation, we use a map data structure (from the standard library) for each space.
384In Matita, we exploited dependent types to design a data structure which enforced the correspondence between the size of pointer and the size of the memory space.
385Further, we assumed that large swathes of memory would often be uninitialized (an assumption on the behaviour of the programmer, not the processor!)
386
387We use a modified form of trie of fixed height $h$.
388Paths are represented by bitvectors (used also for addresses and registers) of length $h$:
389\begin{lstlisting}[frame=single]
390inductive BitVectorTrie(A: Type[0]):nat $\rightarrow$ Type[0] :=
391  Leaf: A $\rightarrow$ BitVectorTrie A 0
392| Node: ∀n. BitVectorTrie A n $\rightarrow$ BitVectorTrie A n $\rightarrow$ BitVectorTrie A (S n)
393| Stub: ∀n. BitVectorTrie A n.
394\end{lstlisting}
395\texttt{Stub} is a constructor that can appear at any point in a trie.
396It represents `uninitialized data'.
397Performing a lookup in memory is now straight-forward.
398The only subtlety over normal trie lookup is how we handle \texttt{Stub}.
399We traverse a path, and upon encountering \texttt{Stub}, we return a default value.
400All manufacturer data sheets that we consulted were silent on the subject of what should be returned if we attempt to access uninitialized memory.
401We defaulted to simply returning zero, though our \texttt{lookup} function is parametric in this choice.
402This is reasonable, as SDCC for instance generates code which first `zeroes' memory in a preamble before executing the program proper.
403This is in line with the C standard, which guarantees that all global variables will be zero initialized piecewise.
404
405\texttt{BitVectorTrie} and \texttt{Vector}, and related functions, can be used in the formalisation of other microprocessors.
406
407%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408% SECTION                                                                      %
409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410\subsection{Labels and pseudoinstructions}
411\label{subsect.labels.pseudoinstructions}
412
413Aside from implementing the core MCS-51 instruction set, we also provided \emph{pseudoinstructions}, \emph{labels} and \emph{cost labels}.
414The purpose of \emph{cost labels} will be explained in Subsection~\ref{subsect.computation.cost.traces}.
415
416Introducing pseudoinstructions had the effect of simplifying a C compiler---another component of the CerCo project---that was being implemented in parallel with our implementation.
417To see why, consider the fact that the MCS-51's instruction set has numerous instructions for unconditional and conditional jumps to memory locations.
418For instance, the instructions \texttt{AJMP}, \texttt{JMP} and \texttt{LJMP} all perform unconditional jumps.
419However, these instructions differ in how large the maximum size of the offset of the jump to be performed can be.
420Further, all jump instructions require a concrete memory address---to jump to---to be specified.
421Compilers that support separate compilation cannot directly compute these offsets and select the appropriate jump instructions.
422These operations are also burdensome for compilers that do not do separate compilation and are handled by assemblers.
423We followed suit.
424
425While introducing pseudoinstructions, we also introduced labels for locations to jump to, and for global data.
426To specify global data via labels, we introduced a preamble before the program where labels and the size of reserved space for data is stored.
427A pseudoinstruction \texttt{Mov} moves (16-bit) data stored at a label into the MCS-51's 16-bit register, \texttt{DPTR}.
428(This register is used for indirect addressing of data stored in external memory.)
429
430The pseudoinstructions and labels induce an assembly language similar to that of SDCC's.
431All pseudoinstructions and labels are `assembled away' prior to program execution.
432Jumps are computed in two stages.
433A map associating memory addresses to labels is built, before replacing pseudojumps with concrete jumps to the correct address.
434The algorithm currently implemented does not try to minimize object code size by picking the shortest possible jump instruction.
435A better algorithm is left for future work.
436
437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438% SECTION                                                                      %
439%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440\subsection{Anatomy of the (Matita) emulator}
441\label{subsect.anatomy.matita.emulator}
442
443The Matita emulator's internal state is a record:
444\begin{lstlisting}[frame=single]
445record Status: Type[0] := {
446  code_memory: BitVectorTrie Byte 16;
447  low_internal_ram: BitVectorTrie Byte 7;
448  high_internal_ram: BitVectorTrie Byte 7;
449  external_ram: BitVectorTrie Byte 16;
450  program_counter: Word;
451  special_function_registers_8051: Vector Byte 19;
452  ... }.
453\end{lstlisting}
454This record encapsulates the current memory contents, the program counter, the state of the current SFRs, and so on.
455
456Here the MCS-51's memory model is implemented using four disjoint memory spaces, plus SFRs.
457From the programmer's point of view, what \emph{really} matters are the addressing modes that are in a many-to-many relationship with the spaces.
458\texttt{DIRECT} addressing can be used to address either lower IRAM (if the first bit is 0) or the SFRs (if the first bit is 1), for instance.
459That's why DIRECT uses 8-bit addresses but pointers to lower IRAM only use 7 bits.
460The complexity of the memory model is captured in a pair of functions, \texttt{get\_arg\_XX} and \texttt{set\_arg\_XX}, that `get' and `set' data of size \texttt{XX} from memory.
461
462%Overlapping, and checking which addressing modes can be used to address particular memory spaces, is handled through numerous \texttt{get\_arg\_XX} and \texttt{set\_arg\_XX} (for 1, 8 and 16 bits) functions.
463
464Both the Matita and O'Caml emulators follow the classic `fetch-decode-execute' model of processor operation.
465The next instruction to be processed, indexed by the program counter, is fetched from code memory with \texttt{fetch}.
466An updated program counter, along with its concrete cost in processor cycles, is also returned.
467These costs are taken from a Siemens Semiconductor Group data sheet for the MCS-51~\cite{siemens:2011}, and will likely vary between particular implementations.
468\begin{lstlisting}[frame=single]
469definition fetch: BitVectorTrie Byte 16 $\rightarrow$ Word $\rightarrow$ instruction $\times$ Word $\times$ nat
470\end{lstlisting}
471Instruction are assembled to bit encodings by \texttt{assembly1}:
472\begin{lstlisting}[frame=single]
473definition assembly1: instruction $\rightarrow$ list Byte
474\end{lstlisting}
475An assembly program---comprising a preamble containing global data and a list of pseudoinstructions---is assembled using \texttt{assembly}.
476Pseudoinstructions and labels are eliminated in favour of instructions from the MCS-51 instruction set.
477A map associating memory locations and cost labels (see Subsection~\ref{subsect.computation.cost.traces}) is produced.
478\begin{lstlisting}[frame=single]
479definition assembly: assembly_program $\rightarrow$ option (list Byte $\times$ (BitVectorTrie String 16))
480\end{lstlisting}
481A single fetch-decode-execute cycle is performed by \texttt{execute\_1}:
482\begin{lstlisting}[frame=single]
483definition execute_1: Status $\rightarrow$ Status
484\end{lstlisting}
485The \texttt{execute} functions performs a fixed number of cycles by iterating
486\texttt{execute\_1}:
487\begin{lstlisting}[frame=single]
488let rec execute (n: nat) (s: Status): Status := ...
489\end{lstlisting}
490This differs from the O'Caml emulator, which executed a program indefinitely.
491A callback function was also accepted as an argument, which `witnessed' the execution as it happened.
492Due to Matita's termination requirement, \texttt{execute} cannot execute a program indefinitely.
493An alternative would be to produce an infinite stream of statuses representing an execution trace using Matita's co-inductive types.
494
495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496% SECTION                                                                      %
497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498\subsection{Instruction set unorthogonality}
499\label{subsect.instruction.set.unorthogonality}
500
501A peculiarity of the MCS-51 is its unorthogonal instruction set; \texttt{MOV} can be invoked using one of 16 combinations of addressing modes out of a total of 361, for instance.
502
503% Show example of pattern matching with polymorphic variants
504
505Such unorthogonality in the instruction set was handled with the use of polymorphic variants in O'Caml~\cite{garrigue:programming:1998}.
506For instance, we introduced types corresponding to each addressing mode:
507\begin{lstlisting}
508type direct = [ `DIRECT of byte ]
509type indirect = [ `INDIRECT of bit ]
510...
511\end{lstlisting}
512Which were then combined in the inductive datatype for assembly preinstructions using the union operator `$|$':
513\begin{lstlisting}
514type 'addr preinstruction =
515[ `ADD of acc * [ reg | direct | indirect | data ]
516...
517| `MOV of (acc * [ reg| direct | indirect | data ],
518   [ reg | indirect ] * [ acc | direct | data ],
519   direct * [ acc | reg | direct | indirect | data ],
520   dptr * data16,
521   carry * bit,
522   bit * carry
523   ) union6
524...
525\end{lstlisting}
526Here, \texttt{union6} is a disjoint union type, defined as follows:
527\begin{lstlisting}
528type ('a,'b,'c,'d,'e,'f) union6 = [ `U1 of 'a | ... | `U6 of 'f ]
529\end{lstlisting}
530The types \texttt{union2}, \texttt{union3} and \texttt{union6} sufficed.
531
532This polymorphic variant machinery worked well: it introduced a certain level of type safety (the type of \texttt{MOV} above guarantees it cannot be invoked with arguments in the \texttt{carry} and \texttt{data16} addressing modes), and also allowed us to pattern match against instructions, when necessary.
533However, this polymorphic variant machinery is \emph{not} present in Matita.
534We needed some way to produce the same effect, which Matita supported.
535We used dependent types.
536
537We provided an inductive data type representing all addressing modes, a type that functions will pattern match against:
538\begin{lstlisting}[frame=single]
539inductive addressing_mode: Type[0] :=
540  DIRECT: Byte $\rightarrow$ addressing_mode
541| INDIRECT: Bit $\rightarrow$ addressing_mode
542...
543\end{lstlisting}
544We also wished to express in the type of functions the \emph{impossibility} of pattern matching against certain constructors.
545In order to do this, we introduced an inductive type of addressing mode `tags'.
546The constructors of \texttt{addressing\_mode\_tag} are in one-to-one correspondence with the constructors of \texttt{addressing\_mode}:
547\begin{lstlisting}[frame=single]
548inductive addressing_mode_tag : Type[0] :=
549  direct: addressing_mode_tag
550| indirect: addressing_mode_tag
551...
552\end{lstlisting}
553The \texttt{is\_a} function checks if an \texttt{addressing\_mode} matches an \texttt{addressing\_mode\_tag}:
554\begin{lstlisting}[frame=single]
555definition is_a :=
556  $\lambda$d: addressing_mode_tag. $\lambda$A: addressing_mode.
557match d with
558[ direct $\Rightarrow$ match A with [ DIRECT _ $\Rightarrow$ true | _ $\Rightarrow$ false ]
559| indirect $\Rightarrow$ match A with [ INDIRECT _ $\Rightarrow$ true | _ $\Rightarrow$ false ]
560...
561\end{lstlisting}
562The \texttt{is\_in} function checks if an \texttt{addressing\_mode} matches a set of tags represented as a vector.
563It simply extends the \texttt{is\_a} function in the obvious manner.
564
565A \texttt{subaddressing\_mode} is an \emph{ad hoc} non-empty $\Sigma$-type of \texttt{addressing\_mode}s in a set of tags:
566\begin{lstlisting}[frame=single]
567record subaddressing_mode (n: nat) (l: Vector addressing_mode_tag (S n)): Type[0] := {
568  subaddressing_modeel :> addressing_mode;
569  subaddressing_modein: bool_to_Prop (is_in $\ldots$ l subaddressing_modeel)
570}.
571\end{lstlisting}
572An implicit coercion~\cite{luo:coercive:1999} is provided to promote vectors of tags (denoted with $\llbracket - \rrbracket$) to the corresponding \texttt{subaddressing\_mode} so that we can use a syntax close to that of O'Caml to specify \texttt{preinstruction}s:
573\begin{lstlisting}[frame=single]
574inductive preinstruction (A: Type[0]): Type[0] ≝
575  ADD: $\llbracket$acc_a$\rrbracket$ $\rightarrow$ $\llbracket$register;direct;indirect;data$\rrbracket$ $\rightarrow$ preinstruction A
576| ADDC: $\llbracket$acc_a$\rrbracket$ $\rightarrow$ $\llbracket$register;direct;indirect;data$\rrbracket$ $\rightarrow$ preinstruction A
577...
578\end{lstlisting}
579The constructor \texttt{ADD} expects two parameters, the first being the accumulator A (\texttt{acc\_a}), the second being a register, direct, indirect or data addressing mode.
580
581% One of these coercions opens up a proof obligation which needs discussing
582% Have lemmas proving that if an element is a member of a sub, then it is a member of a superlist, and so on
583Finally, type coercions from \texttt{addressing\_mode} to \texttt{subaddressing\_mode} and from \texttt{Vector addressing\_mode\_tag} to \texttt{Type$\lbrack0\rbrack$}, are required.
584The first opens a proof obligation wherein we must prove that the provided value is in the admissible set, simulating PVS subset types~\cite{shankar:principles:1999}.
585%PVS introduced subset types, and these later featured in Coq as part of Russell~\cite{sozeau:subset:2006}.
586%All coercions in Matita can open proof obligations.
587
588Proof obligations require us to state and prove a few auxilliary lemmas related to the transitivity of subtyping.
589For instance, an \texttt{addressing\_mode} that belongs to an allowed set also belongs to any one of its supersets.
590At the moment, Matita's automation exploits these lemmas to completely solve all the proof obligations opened in the formalisation.
591The \texttt{execute\_1} function, for instance, opens over 200 proof obligations during type checking.
592
593The machinery just described allows us to restrict the set of \texttt{addressing\_mode}s expected by a function and use this information during pattern matching.
594This allows us to skip impossible cases.
595For instance, consider \texttt{set\_arg\_16}, which expects only a \texttt{DPTR}:
596\begin{lstlisting}[frame=single]
597definition set_arg_16: Status $\rightarrow$ Word $\rightarrow$ $\llbracket$dptr$\rrbracket$ $\rightarrow$ Status := $~\lambda$s, v, a.
598match a return $\lambda$x. bool_to_Prop (is_in ? $\llbracket$dptr$\rrbracket$ x) $\rightarrow$ ? with
599  [ DPTR $\Rightarrow$ $\lambda$_: True.
600    let $\langle$bu, bl$\rangle$ := split $\ldots$ eight eight v in
601    let status := set_8051_sfr s SFR_DPH bu in
602    let status := set_8051_sfr status SFR_DPL bl in
603      status
604  | _ $\Rightarrow$ $\lambda$_: False. $\bot$
605  ] $~$(subaddressing_modein $\ldots$ a).
606\end{lstlisting}
607We give a proof (the expression \texttt{(subaddressing\_modein} $\ldots$ \texttt{a)}) that the argument $a$ is in the set $\llbracket$ \texttt{dptr} $\rrbracket$ to the \texttt{match} expression.
608In every case but \texttt{DPTR}, the proof is a proof of \texttt{False}, and the system opens a proof obligation $\bot$ that can be discarded using \emph{ex falso}.
609Attempting to match against a disallowed addressing mode (replacing \texttt{False} with \texttt{True} in the branch) produces a type error.
610
611We tried other dependently and non-dependently typed solutions before settling on this approach.
612As we need a large number of different combinations of addressing modes to describe the instruction set, it is infeasible to declare a datatype for each one of these combinations.
613The current solution is closest to the corresponding O'Caml code, to the point that the translation from O'Caml to Matita is almost syntactical.
614% We would like to investigate the possibility of changing the code extraction procedure of Matita so that it recognises this programming pattern and outputs O'Caml code using polymorphic variants.
615
616% Talk about extraction to O'Caml code, which hopefully will allow us to extract back to using polymorphic variants, or when extracting vectors we could extract using phantom types
617% Discuss alternative approaches, i.e. Sigma types to piece together smaller types into larger ones, as opposed to using a predicate to `cut out' pieces of a larger type, which is what we did
618
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% SECTION                                                                      %
621%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622\subsection{I/O and timers}
623\label{subsect.i/o.timers}
624
625% `Real clock' for I/O and timers
626The O'Caml emulator has code for handling timers, asynchronous I/O and interrupts (these are not in the Matita emulator as they are not relevant to CerCo).
627All three of these features interact with each other in subtle ways.
628Interrupts can `fire' when an input is detected on the processor's UART port, and, in certain modes, timers reset when a high signal is detected on one of the MCS-51's communication pins.
629
630To accurately model timers and I/O, we add an unbounded integral field \texttt{clock} to the central \texttt{status} record.
631This field is only logical, since it does not represent any quantity stored in the physical processor, and is used to keep track of the current `processor time'.
632Before every execution step, \texttt{clock} is incremented by the number of processor cycles that the instruction just fetched will take to execute.
633The emulator executes the instruction then the code implementing timers and I/O (it isn't specified by the data sheets if I/O is handled at the beginning or the end of each cycle.)
634To model I/O, we store in \texttt{status} a \emph{continuation} which is a description of the behaviour of the environment:
635\begin{lstlisting}
636type line =
637[ `P1 of byte | `P3 of byte
638| `SerialBuff of [ `Eight of byte | `Nine of BitVectors.bit * byte ]  ]
639
640type continuation =
641[`In of time * line * epsilon * continuation] option *
642[`Out of (time -> line -> time * continuation)]
643\end{lstlisting}
644The second projection of the continuation $k$ describes how the environment will react to an output event performed in the future by the processor.
645Suppose $\pi_2(k)(\tau,o) = \langle \tau',k' \rangle$.
646If the emulator at time $\tau$ starts an asynchronous output $o$ either on the P1 or P3 output lines, or on the UART, then the environment will receive the output at time $\tau'$.
647Moreover \texttt{status} is immediately updated with the continuation $k'$.
648
649Further, if $\pi_1(k) = \mathtt{Some}~\langle \tau',i,\epsilon,k'\rangle$, then at time $\tau'$ the environment will send the asynchronous input $i$ to the emulator and \texttt{status} is updated with the continuation $k'$.
650This input is visible to the emulator only at time $\tau' + \epsilon$.
651
652The time required to perform an I/O operation is partially specified in the data sheets of the UART module.
653This computation is complex so we prefer to abstract over it.
654We leave the computation of the delay time to the environment.
655
656We use only the P1 and P3 lines.
657This is because the other lines become inoperable if XRAM is present (we assume it is).
658
659The UART port can work in several modes, depending on the how the SFRs are set.
660In an asyncrhonous mode, the UART transmits 8 bits at a time, using a ninth line for synchronisation.
661In a synchronous mode the ninth line is used to transmit an additional bit.
662All UART modes are formalised.
663
664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665% SECTION                                                                      %
666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667\subsection{Computation of cost traces}
668\label{subsect.computation.cost.traces}
669
670As mentioned in Subsection~\ref{subsect.labels.pseudoinstructions} we introduced a notion of \emph{cost label}.
671Cost labels are inserted by the prototype C compiler at specific locations in the object code.
672
673Cost labels are used to calculate a precise costing for a program by marking the start of basic blocks.
674During the assembly phase, where labels and pseudoinstructions are eliminated, a map is generated associating cost labels with memory locations.
675This map is later used in a separate analysis which computes the cost of a program by traversing through a program, fetching one instruction at a time, and computing the cost of blocks.
676When targetting more complex processors, this simple analysis will need to be replaced by a more sophisticated WCET analysis.
677These block costings are stored in another map, and will later be passed back to the prototype compiler.
678
679%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680% SECTION                                                                      %
681%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682\section{Validation}
683\label{sect.validation}
684
685%\begin{figure}[t]
686%\begin{scriptsize}
687%\begin{verbatim}
688%08: mov 81 #07
689%
690% Processor status:                               
691%
692%   ACC: 0   B: 0   PSW: 0
693%    with flags set as:
694%     CY: false    AC: false   FO: false   RS1: false
695%     RS0: false   OV: false   UD: false   P: false
696%   SP: 7   IP: 0   PC: 8   DPL: 0   DPH: 0   SCON: 0
697%   SBUF: 0   TMOD: 0   TCON: 0
698%   Registers:                                   
699%    R0: 0   R1: 0   R2: 0   R3: 0
700%    R4: 0   R5: 0   R6: 0   R7: 0
701%\end{verbatim}
702%\end{scriptsize}
703%\caption{An example snippet from an emulator execution trace}
704%\label{fig.execution.trace}
705%\end{figure}
706
707We attempted to ensure that what we have formalised is an accurate model of the MCS-51 microprocessor.
708
709We made use of multiple data sheets, each from a different manufacturer.
710This helped us triangulate errors in the specification of the processor's instruction set, and its behaviour, for instance, in a data sheet from Philips Semiconductor.
711
712The O'Caml emulator was especially useful for validation purposes.
713We wrote a module for parsing and loading Intel HEX format files.
714Intel HEX is a standard format that all compilers targetting the MCS-51, and similar processors, produce.
715It is essentially a snapshot of the processor's code memory in compressed form.
716Using this we were able to compile C programs with SDCC and load the resulting program directly into the emulator's code memory, ready for execution.
717Further, we can produce a HEX file from the emulator's code memory for loading into third party tools.
718After each step of execution, we can print out both the instruction that had been executed and a snapshot of the processor's state, including all flags and register contents.
719These traces were useful in spotting anything that was `obviously' wrong with the execution of the program.
720
721We further used MCU 8051 IDE as a reference, which allows a user to step through an assembly program one instruction at a time.
722With these execution traces, we could step through a compiled program in MCU 8051 IDE and compare the resulting execution trace with the trace produced by our emulator.
723
724We partially validated the assembler by proving in Matita that on all defined opcodes the \texttt{assembly\_1} and \texttt{fetch} functions are inverse.
725
726The Matita formalisation was largely copied from the O'Caml source code apart from the changes already mentioned.
727As the Matita emulator is executable we could perform further validation by comparing the trace of a program's execution in the Matita and O'Caml emulators.
728
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730% SECTION                                                                      %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732\section{Related work}
733\label{sect.related.work}
734A large body of literature on the formalisation of microprocessors exists.
735The majority of it deals with proving correctness of implementations of microprocessors at the microcode or gate level, with many considering `cycle accurate' models.
736We are interested in providing a precise specification of the behaviour of the microprocessor in order to prove the correctness of a compiler which will target the processor.
737In particular, we are interested in intentional properties of the processor; precise timings of instruction execution in clock cycles.
738We have also built a complete MCS-51 ecosystem: UART, I/O lines, and hardware timers, complete with an assembler.
739
740Work closely related to our own can be found in~\cite{fox:trustworthy:2010}.
741Here, the authors describe the formalisation, in HOL4, of the ARMv7 instruction set architecture.
742They further point to an excellent list of references to related work in the literature.
743This formalisation also considers the machine code level, opposed to their formalisation, which only considering an abstract assembly language.
744Instruction decoding is explicitly modeled inside HOL4's logic.
745We go further in also providing an assembly language, complete with assembler, to translate instructions and pseudoinstruction into machine code.
746
747Further, in~\cite{fox:trustworthy:2010} the authors validated their formalisation by using development boards and random testing.
748We currently rely on non-exhaustive testing against a third party emulator.
749We recognise the importance of this exhaustive testing, but currently leave it for future work.
750
751Executability is another key difference between our work and that of~\cite{fox:trustworthy:2010}.
752Our formalisation is executable: applying the emulation function to an input state eventually reduces to an output state.
753This is because Matita is based on a logic, CIC, which internalizes conversion.
754In~\cite{fox:trustworthy:2010} the authors provide an automation layer to derive single step theorems: if the processor is in a state that satisfies some preconditions, then after execution of an instruction it will reside in a state satisfying some postconditions.
755We will not need single step theorems of this form to prove properties of the assembly code.
756
757Our main difficulties resided in the non-uniformity of an old 8-bit architecture, in terms of the instruction set, addressing modes and memory models.
758In contrast, the various ARM instruction sets and memory models are relatively uniform.
759
760Other related projects CompCert~\cite{leroy:formally:2009} and the CLI Stack.
761CompCert concerns the certification of a C compiler and includes a formalisation in Coq of a subset of PowerPC.
762The CLI Stack consists of the design and verification of a whole chain of artefacts including a 32-bit microprocessor, the Piton assembler and two compilers.
763Like CerCo, the CLI Stack compilers gave the cost of high-level instructions in processor cycles.
764The CLI Stack high-level languages ($\mu$Gypsy and Nqthm Lisp) and FM9001 microprocessor were not commercial products, but bespoke designs for the purpose of verification (see~\cite{moore:grand:2005}).
765
766The CompCert C compiler is extracted to O'Caml using Coq's code extraction facility.
767Many other formalised emulators/compilers have also been extracted from proof assistants using similar technology (e.g. see~\cite{blanqui:designing:2010}).
768We aim to make use of a similar code extraction facility in Matita, but only if the extracted code exhibits the same degree of type safety, provided by polymorphic variants, and human readability that the O'Caml emulator posseses.
769This is because we aim to use the emulator as a library for non-certified software written directly in O'Caml.
770How we have used Matita's dependent types to handle the instruction set (Subsection~\ref{subsect.instruction.set.unorthogonality}) could enable code extraction to make use of polymorphic variants.
771Using Coq's current code extraction algorithm we could write assembly programs that would generate runtime errors when emulated.
772We leave this for future work.
773
774Despite the apparent similarity between CerCo and CompCert, the two formalisations do not have much in common.
775First, CompCert provides a formalisation at the assembly level (no instruction decoding).
776This impels them to trust an unformalised assembler and linker, whereas we provide our own.
777Our formalisation is \emph{directly} executable, while the one in CompCert only provides a relation that describes execution.
778In CompCert I/O is only described as a synchronous external function call and there is no I/O at the processor level.
779CompCert assumes an idealized abstract and uniform memory model; we take into account the complicated overlapping memory model of the MCS-51 architecture.
780Finally, 82 instructions of the more than 200 offered by the processor are formalised in CompCert, and the assembly language is augmented with macro instructions that are turned into `real' instructions only during communication with the external assembler.
781Even from a technical level the two formalisations differ: we use dependent types whilst CompCert largely sticks to non-dependent types.
782
783Atkey~\cite{atkey:coqjvm:2007} provides an executable specification of the JVM.
784Dependent types are used to remove spurious partiality from the model, and lower the need for over-specifying the behaviour of the processor in impossible cases.
785However, dependent types in the two formalisations are used differently.
786Further, our use of dependent types will also serve to enforce invariants in the CerCo compiler.
787
788Finally~\cite{sarkar:semantics:2009} provides an executable semantics for x86-CC multiprocessor machine code.
789This machine code exhibits a degree of non-uniformity similar to the MCS-51.
790Only a small subset of the instruction set is considered, and they over-approximate the possibilities of unorthogonality of the instruction set, largely dodging the problems we had to face.
791
792Further, it seems that the definition of the decode function is potentially error prone.
793A domain specific language of patterns is formalised in HOL4, similar to the specification language of the x86 instruction set found in manufacturer's data sheets.
794A decode function is implemented by copying lines from data sheets into the proof script, which are then partially evaluated to obtain a compiler.
795We are currently considering implementing a similar domain specific language in Matita.
796
797%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798% SECTION                                                                      %
799%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800\section{Conclusions}
801\label{sect.conclusions}
802
803In CerCo, we are interested in the certification of a compiler for C that induces a precise cost model on the source code.
804Our cost model assigns costs to blocks of instructions by tracing the way that blocks are compiled, and by computing exact costs on generated machine language.
805To perform this accurately, we have provided an executable semantics for the MCS-51 family of processors.
806An O'Caml and Matita formalisation was provided, and both capture the exact timings of the MCS-51 (according to a Siemen's data sheet).
807The O'Caml formalisation also considers timers and I/O.
808Adding support for I/O and timers in Matita is on-going work that will not present any problem, and was delayed only because the addition is not immediately useful for the formalisation of the CerCo compiler.
809
810The formalisation is done at machine level and not at assembly level; we also formalise fetching and decoding.
811We provide an assembly language, enhanched with labels and pseudoinstructions, and an assembler from this language to machine code.
812This assembly language is similar to those found in `industrial strength' compilers, such as SDCC.
813We introduce cost labels in the machine language to relate the data flow of the assembly program to that of the C source language, in order to associate costs to the C program.
814For the O'Caml version, we provide a parser and pretty printer from code memory to Intel HEX.
815Hence we can perform testing on programs compiled using any free or commercial compiler.
816
817Our main difficulty in formalising the MCS-51 was the unorthogonality of its memory model and instruction set.
818These problems are handled in O'Caml by using language features like polymorphic variants and phantom types, simulating Generalized Abstract Data Types~\cite{xi:guarded:2003}.
819Importantly, we searched for a manner of using dependent types to recover the same flexibility, reduce spurious partiality, and grant invariants that will be later useful in other formalisations in CerCo.
820
821The formalisation has been partially verified by computing execution traces on selected programs and comparing them with an existing emulator.
822All instructions have been tested at least once, but we have not yet pushed testing further, for example with random testing or by using development boards.
823I/O in particular has not been tested yet, and it is currently unclear how to provide exhaustive testing in the presence of I/O.
824Finally, we are aware of having over-specified the processor in several places, by fixing a behaviour hopefully consistent with the real machine, where manufacturer data sheets are ambiguous or under-specified.
825
826\bibliography{itp-2011}
827
828\end{document}
Note: See TracBrowser for help on using the repository browser.