source: Deliverables/D4.2-4.3/reports/D4-2.tex @ 1420

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

more changes

File size: 36.6 KB
Line 
1\documentclass[11pt, epsf, a4wide]{article}
2
3\usepackage{../../style/cerco}
4
5\usepackage{amsfonts}
6\usepackage{amsmath}
7\usepackage{amssymb} 
8\usepackage[english]{babel}
9\usepackage{graphicx}
10\usepackage[utf8x]{inputenc}
11\usepackage{listings}
12\usepackage{lscape}
13\usepackage{stmaryrd}
14\usepackage{threeparttable}
15\usepackage{url}
16
17\title{
18INFORMATION AND COMMUNICATION TECHNOLOGIES\\
19(ICT)\\
20PROGRAMME\\
21\vspace*{1cm}Project FP7-ICT-2009-C-243881 \cerco{}}
22
23\lstdefinelanguage{matita-ocaml}
24  {keywords={definition,coercion,lemma,theorem,remark,inductive,record,qed,let,in,rec,match,return,with,Type,try},
25   morekeywords={[2]whd,normalize,elim,cases,destruct},
26   morekeywords={[3]type,of},
27   mathescape=true,
28  }
29
30\lstset{language=matita-ocaml,basicstyle=\small\tt,columns=flexible,breaklines=false,
31        keywordstyle=\color{red}\bfseries,
32        keywordstyle=[2]\color{blue},
33        keywordstyle=[3]\color{blue}\bfseries,
34        commentstyle=\color{green},
35        stringstyle=\color{blue},
36        showspaces=false,showstringspaces=false}
37
38\lstset{extendedchars=false}
39\lstset{inputencoding=utf8x}
40\DeclareUnicodeCharacter{8797}{:=}
41\DeclareUnicodeCharacter{10746}{++}
42\DeclareUnicodeCharacter{9001}{\ensuremath{\langle}}
43\DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}}
44
45\date{}
46\author{}
47
48\begin{document}
49
50\thispagestyle{empty}
51
52\vspace*{-1cm}
53\begin{center}
54\includegraphics[width=0.6\textwidth]{../../style/cerco_logo.png}
55\end{center}
56
57\begin{minipage}{\textwidth}
58\maketitle
59\end{minipage}
60
61\vspace*{0.5cm}
62\begin{center}
63\begin{LARGE}
64\textbf{
65Report n. D4.2\\
66Functional encoding in the Calculus of Constructions
67}
68\end{LARGE} 
69\end{center}
70
71\vspace*{2cm}
72\begin{center}
73\begin{large}
74Version 1.0
75\end{large}
76\end{center}
77
78\vspace*{0.5cm}
79\begin{center}
80\begin{large}
81Main Authors:\\
82Dominic P. Mulligan and Claudio Sacerdoti Coen
83\end{large}
84\end{center}
85
86\vspace*{\fill}
87
88\noindent
89Project Acronym: \cerco{}\\
90Project full title: Certified Complexity\\
91Proposal/Contract no.: FP7-ICT-2009-C-243881 \cerco{}\\
92
93\clearpage
94\pagestyle{myheadings}
95\markright{\cerco{}, FP7-ICT-2009-C-243881}
96
97\newpage
98
99\vspace*{7cm}
100\paragraph{Abstract}
101We describe the encoding, in the Calculus of Constructions, of the intermediate languages for the CerCo compiler.
102The CerCo backend consists of four distinct intermediate languages---RTL, ERTL, LTL and LIN, respectively---though we also handle the translations from the last frontend intermediate language, RTLabs, into RTL, and the translation of LIN into assembly language.
103Where feasible, we have made use of dependent types to enforce invariants and to make otherwise partial functions total.
104\newpage
105
106\tableofcontents
107
108\newpage
109
110%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
111% SECTION.                                                                    %
112%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
113\section{Task}
114\label{sect.task}
115
116The Grant Agreement states that Task T4.2, entitled `Functional encoding in the Calculus of Constructions' has associated Deliverable D4.2, consisting of the following:
117\begin{quotation}
118CIC encoding: Back-end: Functional Specification in the internal language of the Proof Assistant (the Calculus of Inductive Construction) of the back end of the compiler. This unit is meant to be composable with the front-end of deliverable D3.2, to obtain a full working compiler for Milestone M2. A first validation of the design principles and implementation choices for the Untrusted Cost-annotating OCaml Compiler D2.2 is achieved and reported in the deliverable, possibly triggering updates of the Untrusted Cost-annotating OCaml Compiler sources.
119\end{quotation}
120This report details our implementation of this deliverable.
121
122%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
123% SECTION.                                                                    %
124%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
125\subsection{Connections with other deliverables}
126\label{subsect.connections.with.other.deliverables}
127
128Deliverable D4.2 enjoys a close relationship with three other deliverables, namely deliverables D2.2, D4.3 and D4.4.
129
130Deliverable D2.2, the O'Caml implementation of a cost preserving compiler for a large subset of the C programming language, is the basis upon which we have implemented the current deliverable.
131In particular, the architecture of the compiler, its intermediate languages, and the overall implementation of the Matita encodings has been taken from the O'Caml compiler.
132Any variations from the O'Caml design are due to bugs identified in the prototype compiler during the Matita implementation, our identification of code that can be abstracted and made generic, or our use of Matita's much stronger type system to enforce invariants through the use of dependent types.
133
134Deliverable D4.3 can be seen as a `sister' deliverable to the deliverable reported on herein.
135In particular, where this deliverable reports on the encoding in the Calculus of Constructions of the backend translations, D4.3 is the encoding in the Calculus of Constructions of the semantics of those languages.
136As a result, a substantial amount of Matita code is shared between the two deliverables.
137
138Deliverable D4.4, the backend correctness proofs, is the immediate successor of this deliverable.
139
140%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
141% SECTION.                                                                    %
142%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
143\subsection{A brief overview of the backend compilation chain}
144\label{subsect.brief.overview.backend.compilation.chain}
145
146The Matita compiler's backend consists of five distinct intermediate languages: RTL, RTLntl, ERTL, LTL and LIN.
147A sixth language, RTLabs, serves as the entry point of the backend and the exit point of the frontend.
148RTL, RTLntl, ERTL and LTL are `control flow graph based' languages, whereas LIN is a linearised language, the final language before translation to assembly.
149
150We now briefly discuss the properties of the intermediate languages, and discuss the various transformations that take place during the translation process:
151
152\paragraph{RTLabs ((Abstract) Register Transfer Language)}
153As mentioned, this is the final language of the compiler's frontend and the entry point for the backend.
154This language uses pseudoregisters, not hardware registers.\footnote{There are an unbounded number of pseudoregisters.  Pseudoregisters are converted to hardware registers or stack positions during register allocation.}
155Functions still use stackframes, where arguments are passed on the stack and results are stored in addresses.
156During the pass to RTL instruction selection is carried out.
157
158\paragraph{RTL (Register Transfer Language)}
159This language uses pseudoregisters, not hardware registers.
160Tailcall elimination is carried out during the translation from RTL to RTLntl.
161
162\paragraph{RTLntl (Register Transfer Language --- No Tailcalls)}
163This language is a pseudoregister, graph based language where all tailcalls are eliminated.
164RTLntl is not present in the O'Caml compiler.
165
166\paragraph{ERTL (Explicit Register Transfer Language)}
167This is a language very similar to RTLntl.
168However, the calling convention is made explicit, in that functions no longer receive and return inputs and outputs via a high-level mechanism, but rather use stack slots or hadware registers.
169The ERTL to LTL pass performs the following transformations: liveness analysis, register colouring and register/stack slot allocation.
170
171\paragraph{LTL (Linearisable Transfer Language)}
172Another graph based language, but uses hardware registers instead of pseudoregisters.
173Tunnelling (branch compression) should be implemented here.
174
175\paragraph{LIN (Linearised)}
176This is a linearised form of the LTL language; function graphs have been linearised into lists of statements.
177All registers have been translated into hardware registers or stack addresses.
178This is the final stage of compilation before translating directly into assembly language.
179
180%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
181% SECTION.                                                                    %
182%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
183\section{The backend intermediate languages in Matita}
184\label{sect.backend.intermediate.languages.matita}
185
186We now discuss the encoding of the compiler backend languages in the Calculus of Constructions proper.
187We pay particular heed to changes that we made from the O'Caml prototype.
188In particular, many aspects of the backend languages have been unified into a single `joint' language.
189We have also made heavy use of dependent types to reduce `spurious partiality' and to encode invariants.
190
191%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
192% SECTION.                                                                    %
193%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
194\subsection{Abstracting related languages}
195\label{subsect.abstracting.related.languages}
196
197The O'Caml compiler is written in the following manner.
198Each intermediate language has its own dedicated syntax, notions of internal function, and so on.
199Here, we make a distinction between `internal functions'---other functions that are explicitly written by the programmer, and `external functions', which belong to external library and require explictly linking.
200Internal functions are represented as a record, consisting of a sequential structure, of some description, of statements, entry and exit points to this structure, and other book keeping devices.
201Translations between intermediate language map syntaxes to syntaxes, and internal function representations to internal function representations explicitly.
202
203This is a perfectly valid way to write a compiler, where everything is made explicit, but writing a \emph{verified} compiler poses new challenges.
204In particular, we must look ahead to see how our choice of encodings will affect the size and complexity of the forthcoming proofs of correctness.
205We now discuss some abstractions, introduced in the Matita code, which we hope will make our proofs shorter, amongst other benefits.
206
207\paragraph{Changes between languages made explicit}
208Due to the bureaucracy inherent in explicating each intermediate language's syntax in the O'Caml compiler, it can often be hard to see exactly what changes between each successive intermediate language.
209By abstracting the syntax of the RTL, ERTL, LTL and LIN intermediate languages, we make these changes much clearer.
210
211Our abstraction takes the following form:
212\begin{lstlisting}
213inductive joint_instruction (p: params__) (globals: list ident): Type[0] :=
214  | COMMENT: String $\rightarrow$ joint_instruction p globals
215  ...
216  | INT: generic_reg p $\rightarrow$ Byte $\rightarrow$ joint_instruction p globals
217  ...
218  | OP1: Op1 → acc_a_reg p → acc_a_reg p → joint_instruction p globals
219  ...
220  | extension: extend_statements p $\rightarrow$ joint_instruction p globals.
221\end{lstlisting}
222We first note that for the majority of intermediate languages, many instructions are shared.
223However, these instructions expect different register types (either a pseudoregister or a hardware register) as arguments.
224We must therefore parameterise the joint syntax with a record of parameters that will be specialised to each intermediate language.
225In the type above, this parameterisation is realised with the \texttt{params\_\_} record.
226As a result of this parameterisation, we have also added a degree of `type safety' to the intermediate languages' syntaxes.
227In particular, we note that the \texttt{OP1} constructor expects quite a specific type, in that the two register arguments must both be the accumulator A.
228Contrast this with the \texttt{INT} constructor, which expects a \texttt{generic\_reg}, corresponding to an `arbitrary' register type.
229
230Further, we note that some intermediate languages have language specific instructions (i.e. the instructions that change between languages).
231We therefore add a new constructor to the syntax, \texttt{extension}, which expects a value of type \texttt{extend\_statements p}.
232As \texttt{p} varies between intermediate languages, we can provide language specific extensions to the syntax of the joint language.
233For example, ERTL's extended syntax consists of the following extra statements:
234\begin{lstlisting}
235inductive ertl_statement_extension: Type[0] :=
236  | ertl_st_ext_new_frame: ertl_statement_extension
237  | ertl_st_ext_del_frame: ertl_statement_extension
238  | ertl_st_ext_frame_size: register $\rightarrow$ ertl_statement_extension.
239\end{lstlisting}
240These are further packaged into an ERTL specific instance of \texttt{params\_\_} as follows:
241\begin{lstlisting}
242definition ertl_params__: params__ :=
243  mk_params__ register register ... ertl_statement_extension.
244\end{lstlisting}
245
246\paragraph{Shared code, reduced proofs}
247Many features of individual backend intermediate languages are shared with other intermediate languages.
248For instance, RTLabs, RTL, ERTL and LTL are all graph based languages, where functions are represented as a graph of statements that form their bodies.
249Functions for adding statements to a graph, searching the graph, and so on, are remarkably similar across all languages, but are duplicated in the O'Caml code.
250
251As a result, we chose to abstract the representation of internal functions for the RTL, ERTL, LTL and LIN intermediate languages into a `joint' representation.
252This representation is parameterised by a record that dictates the layout of the function body for each intermediate language.
253For instance, in RTL, the layout is graph like, whereas in LIN, the layout is a linearised list of statements.
254Further, a generalised way of accessing the successor statement to the one currently under consideration is needed, and so forth.
255
256Our joint internal function record looks like so:
257\begin{lstlisting}
258record joint_internal_function (globals: list ident) (p:params globals) : Type[0] ≝
259{ 
260  ...
261  joint_if_params   : paramsT p;
262  joint_if_locals   : localsT p;
263  ...
264  joint_if_code     : codeT … p;
265  ...
266}.
267\end{lstlisting}
268In particular, everything that can vary between differing intermediate languages has been parameterised.
269Here, we see the number of parameters, the listing of local variables, and the internal code representation has been parameterised.
270Other particulars are also parameterised, though here omitted.
271
272Hopefully this abstraction process will reduce the number of proofs that need to be written, dealing with internal functions.
273We only need to prove once that fetching a statement's successor is `correct', and we inherit this property for free for every intermediate language.
274
275\paragraph{Dependency on instruction selection}
276We note that the backend languages are all essentially `post instruction selection languages'.
277The `joint' syntax makes this especially clear.
278For instance, in the definition:
279\begin{lstlisting}
280inductive joint_instruction (p:params__) (globals: list ident): Type[0] ≝
281  ...
282  | INT: generic_reg p → Byte → joint_instruction p globals
283  | MOVE: pair_reg p → joint_instruction p globals
284  ...
285  | PUSH: acc_a_reg p → joint_instruction p globals
286  ...
287  | extension: extend_statements p → joint_instruction p globals.
288\end{lstlisting}
289The capitalised constructors---\texttt{INT}, \texttt{MOVE}, and so on---are all machine specific instructions.
290Retargetting the compiler to another microprocessor would entail replacing these constructors with constructors that correspond to the instructions of the new target.
291We feel that this makes which instructions are target dependent, and which are not (i.e. those language specific instructions that fall inside the \texttt{extension} constructor) much more explicit.
292
293\paragraph{Independent development and testing}
294We have essentially modularised the intermediate languages in the compiler backend.
295As with any form of modularisation, we reap benefits in the ability to independently test and develop each intermediate language separately.
296
297\paragraph{Future reuse for other compiler projects}
298Another advantage of our modularisation scheme is the ability to quickly use and reuse intermediate languages for other compiler projects.
299For instance, in creating a cost-preserving compiler for a functional language, we may choose to target the RTL language directly.
300Naturally, the register requirements for a functional language may differ from those of an imperative language, a reconfiguration which our parameterisation makes easy.
301
302\paragraph{Easy addition of new compiler passes}
303Under our modularisation and abstraction scheme, new compiler passes can easily be injected into the backend.
304We have a concrete example of this in the RTLntl language, an intermediate language that was not present in the original O'Caml code.
305To specify a new intermediate language we must simply specify, through the use of the statement extension mechanism, what differs in the new intermediate language from the `joint' language, and configure a new notion of internal function record, by specialising parameters, to the new language.
306As generic code for the `joint' language exists, for example to add statements to control flow graphs, this code can be reused for the new intermediate language.
307
308\paragraph{Possible language commutations}
309The backend translation passes of the CerCo compiler differ quite a bit from the CompCert compiler.
310In the CompCert compiler, linearisation occurs much earlier in the compilation chain, and passes such as register colouring and allocation are carried out on a linearised form of program.
311Contrast this with our own approach, where the code is represented as a graph for much longer.
312
313However, by abstracting the representation of intermediate functions, we are now much more free to reorder translation passes as we see fit.
314The linearisation process, for instance, now no longer cares about the specific representation of code in the source and target languages.
315It just relies on a common interface.
316We are therefore, in theory, free to pick where we wish to linearise our representation.
317This adds an unusual flexibility into the compilation process, and allows us to freely experiment with different orderings of translation passes.
318
319%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
320% SECTION.                                                                    %
321%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
322\subsection{Use of dependent types}
323\label{subsect.use.of.dependent.types}
324
325We see three potential ways in which a compiler can fail to compile a program:
326\begin{enumerate}
327\item
328The program is malformed, and there is no hope of making sense of the program.
329\item
330A heuristic or algorithm in the compiler is implemented incorrectly, in which case an otherwise correct source program fails to be compiler to correct assembly code.
331\item
332An invariant in the compiler is invalidated.
333\end{enumerate}
334The first source of failure we are unable to do anything about.
335The latter two sources of failure should be interpreted as a compiler bug, and, as part of a verified compiler project, we'd like to rule out all such bugs.
336In CerCo, we aim to use dependent types to help us enforce invariants and prove our heuristics and algorithms correct.
337
338First, we encode informal invariants, or uses of \texttt{assert false} in the O'Caml code, with dependent types, converting partial functions into total functions.
339There are numerous examples of this throughout the backend.
340For example, in the \texttt{RTLabs} to \texttt{RTL} transformation pass, many functions only `make sense' when lists of registers passed to them as arguments conform to some specific length.
341For instance, the \texttt{translate\_negint} function, which translates a negative integer constant:
342\begin{lstlisting}
343definition translate_negint ≝
344  $\lambda$globals: list ident.
345  $\lambda$destrs: list register.
346  $\lambda$srcrs: list register.
347  $\lambda$start_lbl: label.
348  $\lambda$dest_lbl: label.
349  $\lambda$def: rtl_internal_function globals.
350  $\lambda$prf: |destrs| = |srcrs|. (* assert here *)
351    ...
352\end{lstlisting}
353The last argument to the function, \texttt{prf}, is a proof that the lengths of the lists of source and destination registers are the same.
354This was an assertion in the O'Caml code.
355
356Secondly, we make use of dependent types to make the Matita code easier to read, and eventually the proofs of correctness for the compiler easier to write.
357For instance, many intermediate languages in the backend of the compiler, from RTLabs to LTL, are graph based languages.
358Here, function definitions consist of a graph (i.e. a map from labels to statements) and a pair of labels denoting the entry and exit points of this graph.
359Practically, we would always like to ensure that the entry and exit labels are present in the statement graph.
360We ensure that this is so with a dependent sum type in the \texttt{joint\_internal\_function} record, which all graph based languages specialise to obtain their own internal function representation:
361\begin{lstlisting}
362record joint_internal_function (globals: list ident) (p: params globals): Type[0] :=
363{
364  ...
365  joint_if_code     : codeT $\ldots$ p;
366  joint_if_entry    : $\Sigma$l: label. lookup $\ldots$ joint_if_code l $\neq$ None $\ldots$;
367  ...
368}.
369\end{lstlisting}
370Here, \texttt{codeT} is a parameterised type representing the `structure' of the function's body (a graph in graph based languages, and a list in the linearised LIN language).
371Specifically, the \texttt{joint\_if\_entry} is a dependent pair consisting of a label and a proof that the label in question is a vertex in the function's graph.
372A similar device exists for the exit label.
373
374Finally, we make use of dependent types for another reason: experimentation.
375Namely, CompCert makes little use of dependent types to encode invariants.
376In contrast, we wish to make as much use of dependent types as possible, both to experiment with different ways of encoding compilers in a proof assistant, but also as a way of `stress testing' Matita's support for dependent types.
377
378%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
379% SECTION.                                                                    %
380%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
381\subsection{What we do not implement}
382\label{subsect.what.we.do.not.implement}
383
384There are several classes of functionality that we have chosen not to implement in the backend languages:
385\begin{itemize}
386\item
387\textbf{Datatypes and functions over these datatypes that are not supported by the compiler.}
388In particular, the compiler does not support the floating point datatype, nor accompanying functions over that datatype.
389At the moment, frontend languages within the compiler possess constructors corresponding to floating point code.
390These are removed during instruction selection (in the RTLabs to RTL transformation) using a daemon.\footnote{A Girardism.  An axiom of type \texttt{False}, from which we can prove anything.}
391However, at some point, we would like the front end of the compiler to recognise programs that use floating point code and reject them as being invalid.
392\item
393\textbf{Axiomatised components that will be implemented using external oracles.}
394Several large, complex pieces of compiler infrastructure, most noticably register colouring and fixed point calculation during liveness analysis have been axiomatised.
395This was already agreed upon before the start of the project, and is clearly marked in the project proposal, following comments by those involved with the CompCert project about the difficulty in formalising register colouring in that project.
396Instead, these components are axiomatised, along with the properties that they need to satisfy in order for the rest of the compilation chain to be correct.
397These axiomatised components are found in the ERTL to LTL pass.
398
399It should be noted that these axiomatised components fall into the following pattern: whilst their implementation is complex, and their proof of correctness is difficult, we are able to quickly and easily verify that any answer that they provide is correct.
400As a result, we do not see this axiomatisation process as being too onerous.
401\item
402\textbf{A few non-computational proof obligations.}
403A few difficult-to-close, but non-computational (i.e. they do not prevent us from executing the compiler inside Matita), proof obligations have been closed using daemons in the backend.
404These proof obligations originate with our use of dependent types for expressing invariants in the compiler.
405However, here, it should be mentioned that many open proof obligations are simply impossible to close until we start to obtain stronger invariants from the proof of correctness for the compiler proper.
406In particular, in the RTLabs to RTL pass, several proof obligations relating to lists of registers stored in a `local environment' appear to fall into this pattern.
407\item
408\textbf{Branch compression (tunnelling).}
409This was a feature of the O'Caml compiler.
410It is not yet currently implemented in the Matita compiler.
411This feature is only an optimisation, and will not affect the correctness of the compiler.
412\item
413\textbf{`Real' tailcalls}
414For the time being, tailcalls in the backend are translated to `vanilla' function calls during the ERTL to LTL pass.
415This follows the O'Caml compiler, which did not implement tailcalls, and did this simplification step.
416`Real' tailcalls are being implemented in the O'Caml compiler, and when this implementation is complete, we aim to port this code to the Matita compiler.
417\end{itemize}
418
419%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
420% SECTION.                                                                    %
421%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
422\section{Associated changes to O'Caml compiler}
423\label{sect.associated.changes.to.ocaml.compiler}
424
425At the moment, no changes we have made in the Matita backend have made their way back into the O'Caml compiler.
426We do not see the heavy process of modularisation and abstraction as making its way back into the O'Caml codebase, as this is a significant rewrite of the backend code.
427However, several bugfixes, and the identification of `hidden invariants' in the O'Caml code will be incorporated back into the prototype.
428
429%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
430% SECTION.                                                                    %
431%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
432\section{Future work}
433\label{sect.future.work}
434
435As mentioned in Section~\ref{subsect.what.we.do.not.implement}, there are several unimplemented features in the compiler, and several aspects of the Matita code that can be improved in order to make currently partial functions total.
436We summarise this future work here:
437\begin{itemize}
438\item
439We plan to make use of dependent types to identify `floating point' free programs and make all functions total over such programs.
440This will remove a swathe of uses of daemons.
441This should be routine.
442\item
443We plan to move expansion of integer modulus, and other related functions, into the instruction selection (RTLabs to RTL) phase.
444This will also help to remove a swathe of uses of daemons, as well as potentially introduce new opportunities for optimisations that we currently miss in expanding these instructions at the C-light level.
445\item
446We plan to close all existing proof obligations that are closed using daemons, arising from our use of dependent types in the backend.
447However, many may not be closable until we have completed Deliverable D4.4, the certification of the whole compiler, as we may not have invariants strong enough at the present time.
448\item
449We plan to port the O'Caml compiler's implementation of tailcalls when this is completed, and eventually port the branch compression code currently in the O'Caml compiler to the Matita implementation.
450This should not cause any major problems.
451\item
452We plan to validate the backend translations, removing any obvious bugs, by executing the translation inside Matita on small C programs.
453This is not critical, as the certification process will find all bugs anyway.
454\end{itemize}
455
456\newpage
457
458%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
459% SECTION.                                                                    %
460%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
461\section{Code listing}
462\label{sect.code.listing}
463
464%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
465% SECTION.                                                                    %
466%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
467\subsection{Listing of files}
468\label{subsect.listing.files}
469
470Translation specific files (files relating to language semantics have been omitted) are presented in Table~\ref{table.syntax}.
471\begin{table}
472\begin{threeparttable}
473\begin{tabular}{llll}
474Title & Description & O'Caml & Ratio \\
475\hline
476\texttt{RTLabs/syntax.ma} & The syntax of RTLabs & \texttt{RTLabs/RTLabs.mli} & 0.65 \\
477\texttt{joint/Joint.ma} & Abstracted syntax for backend languages & N/A & N/A \\
478\texttt{RTL/RTL.ma} & The syntax of RTL & \texttt{RTL/RTL.mli} & 1.85\tnote{b} \\
479\texttt{ERTL/ERTL.ma} & The syntax of ERTL & \texttt{ERTL/ERTL.mli} & 1.04\tnote{b} \\
480\texttt{LIN/joint\_LTL\_LIN.ma} & The syntax of the abstracted combined LTL and LIN language & N/A & N/A \\
481\texttt{LTL/LTL.ma} & The specialisation of the above file to the syntax of LTL & \texttt{LTL/LTL.mli} & 1.86\tnote{a} \\
482\texttt{LIN/LIN.ma} & The specialisation of the above file to the syntax of LIN & \texttt{LIN/LIN.mli} & 2.27\tnote{a} \\
483\end{tabular}
484\begin{tablenotes}
485  \item[a] Includes \texttt{joint/Joint\_LTL\_LIN.ma} and \texttt{joint/Joint.ma}.
486  \item[b] Includes \texttt{joint/Joint.ma}.
487\end{tablenotes}
488\end{threeparttable}
489\caption{Syntax files}
490\label{table.syntax}
491\end{table}
492Here, the O'Caml column denotes the O'Caml source file in the prototype compiler's implementation that corresponds to the Matita script in question.
493The ratios are the linecounts of the Matita file divided by the line counts of the corresponding O'Caml file.
494These are computed with \texttt{wc -l}, a standard Unix tool.
495Translations and utilities are presented in Table~\ref{table.translation.utilities}.
496\begin{landscape}
497\begin{table}
498\begin{threeparttable}
499\begin{tabular}{llll}
500Title & Description & O'Caml & Ratio \\
501\hline
502\texttt{RTLabs/RTLabsToRTL.ma} & Translation from RTLabs to RTL & \texttt{RTLabs/RTLabsToRTL.ml} & 1.61 \\
503\texttt{joint/TranslateUtils.ma} & Generic translation utilities & N/A & N/A \\
504\texttt{joint/Joint\_LTL\_LIN.ma} & Generic code for LTL and LIN languages & N/A & N/A \\
505\texttt{RTL/RTLToERTL.ma} & Translation from RTL to ERTL & \texttt{RTL/RTLToERTL.ml} & 1.38\tnote{a}\tnote{b}\tnote{c} \\
506\texttt{RTL/RTLtailcall.ma} & Elimination of tailcalls & \texttt{RTL/RTLtailcall.ml} & 2.01 \\
507\texttt{ERTL/ERTLToLTL.ma} & Translation from ERTL to LTL & \texttt{ERTL/ERTLToLTL.ml} & 1.39\tnote{a}\tnote{b}\tnote{c}\tnote{d} \\
508\texttt{ERTL/Interference.ma} & Axiomatised graph colouring component & \texttt{common/interference.ml} & 0.23\tnote{b} \\
509\texttt{ERTL/liveness.ma} & Liveness analysis & \texttt{ERTL/liveness.ml} & 1.46\tnote{b} \\
510\texttt{LTL/LTLToLIN.ma} & Translation from LTL to LIN & \texttt{LTL/LTLToLIN.ml} & 1.10 \tnote{a}\tnote{b}\tnote{c}\tnote{e}\tnote{f} \\
511\texttt{LIN/LINToASM.ma} & Translation from LIN to assembly & \texttt{LIN/LINToASM.ml} & 4.06\tnote{a}\tnote{b}\tnote{c}\tnote{f}
512\end{tabular}
513\begin{tablenotes}
514  \item[a] Includes \texttt{joint/TranslateUtils.ma}.
515  \item[b] Includes \texttt{joint/Joint.ma}.
516  \item[c] Includes \texttt{joint/TranslateUtil.ma}.
517  \item[d] Includes \texttt{ERTL/ERTLToLTLI.ml}.
518  \item[e] Includes \texttt{LTL/LTLToLINI.ml}.
519  \item[f] Includes \texttt{joint/joint\_LTL\_LIN.ma}.
520\end{tablenotes}
521\end{threeparttable}
522\caption{Translation files.}
523\label{table.translation.utilities}
524\end{table}
525\end{landscape}
526Given that Matita code is much more verbose than O'Caml code, with explicit typing and inline proofs, we have achieved respectable line count ratios in the translation.
527Some of these ratio, however, need explanation.
528In particular, the RTLabs to RTL translation stands out.
529Note that RTLabs is not subject to our abstraction process, and the language's syntax is fully explicated.
530Further, the RTLabs to RTL translation is quite involved, including instruction selection.
531
532Other, longer translations include the file \texttt{ERTLtoLTL.ma}.
533This is actually the concatenation of the files \texttt{ERTLtoLTLI.ml} and \texttt{ERTLtoLTL.ml} in the O'Caml source, hence its length.
534
535Further, many translations are actually significantly shorter than their O'Caml counterparts due to axiomatisation, and the lack of structure and functor declarations in Matita.
536
537We note that the O'Caml backend codebase consists of 6770 lines of O'Caml code (including comments).
538The Matita codebase consists of 6447 lines of Matita code (including comments).
539This is a ratio of 0.95.
540
541%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
542% SECTION.                                                                    %
543%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
544\subsection{Listing of important functions and axioms}
545\label{subsect.listing.important.functions.and.axioms}
546
547We list some important functions and axioms in the backend compilation:
548
549\paragraph{From RTL/RTLabsToRTL.ma}
550\begin{center}
551\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
552Title & Description \\
553\hline
554\texttt{translate\_stmt} & Translation of an RTLabs statement to an RTL statement \\
555\texttt{translate\_internal} & Translation of an RTLabs internal function to an RTL internal function \\
556\texttt{rtlabs\_to\_rtl} & Translation of an RTLabs program to an RTL program
557\end{tabular*}
558\end{center}
559
560\paragraph{From joint/TranslateUtils.ma}
561\begin{center}
562\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
563Title & Description \\
564\hline
565\texttt{fresh\_regs} & Generic fresh pseudoregister generation, for any intermediate language \\
566\texttt{adds\_graph} & Generic means of adding a statement to a graph, for any intermediate language
567\end{tabular*}
568\end{center}
569
570\paragraph{From RTL/RTLTailcall.ma}
571\begin{center}
572\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
573Title & Description \\
574\hline
575\texttt{simplify\_statement} & Remove a single tailcall \\
576\texttt{simplify\_graph} & Remove all tailcalls in the function graph \\
577\texttt{tailcall\_simplify} & Simplify an RTL program by removing tailcalls
578\end{tabular*}
579\end{center}
580
581\paragraph{From RTL/RTLToERTL.ma}
582\begin{center}
583\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
584Title & Description \\
585\hline
586\texttt{translate\_stmt} & Translation of an RTL statement to an ERTL statement \\
587\texttt{translate\_funct\_internal} & Translation of an RTL internal function to an ERTL internal function \\
588\texttt{translate} & Translation of an RTL program to an ERTL program
589\end{tabular*}
590\end{center}
591
592\paragraph{From ERTL/liveness.ma}
593\begin{center}
594\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
595Title & Description \\
596\hline
597\texttt{analyse} & Dead code analysis
598\end{tabular*}
599\end{center}
600
601\paragraph{From ERTL/Interference.ma}
602\begin{center}
603\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
604Title & Description \\
605\hline
606\texttt{build} & The (axiomatised) graph colouring for register and stack slot allocation
607\end{tabular*}
608\end{center}
609
610\paragraph{From ERTL/ERTLToLTL.ma}
611\begin{center}
612\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
613Title & Description \\
614\hline
615\texttt{translate\_statement} & Translation of an ERTL statement into multiple LTL statements \\
616\texttt{translate\_internal} & Translation of an ERTL internal function into an LTL internal function \\
617\texttt{ertl\_to\_ltl} & Translation of an ERTL program into an LTL program
618\end{tabular*}
619\end{center}
620
621\paragraph{From LTL/LTLToLIN.ma}
622\begin{center}
623\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
624Title & Description \\
625\hline
626\texttt{visit} & Visits, in order, every node in the statement graph for linearisation \\
627\texttt{translate\_stmt} & Translation of an LTL statement to a LIN statement \\
628\texttt{branch\_compress} & Place holder (currently identity) function for branch compression \\
629\texttt{translate\_internal} & Translation of an LTL internal function into a LIN internal function \\
630\texttt{ltl\_to\_lin} & Translation of an LTL program to a LIN program
631\end{tabular*}
632\end{center}
633
634\paragraph{From LIN/LINToASM.ma}
635\begin{center}
636\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
637Title & Description \\
638\hline
639\texttt{translate\_statements} & Translation of a LIN statement to mutliple assembly instructions \\
640\texttt{translate\_fun\_def} & Translation of a LIN internal function definition into assembly \\
641\texttt{translate} & Translation of a LIN program into assembly
642\end{tabular*}
643\end{center}
644
645\end{document}
Note: See TracBrowser for help on using the repository browser.