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

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

correct ratios for semantics calculated

File size: 31.0 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,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.3\\
66Formal semantics of intermediate languages
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 semantics of the CerCo compiler's backend intermediate languages.
102The CerCo backend consists of five distinct languages: RTL, RTLntl, ERTL, LTL and LIN.
103We describe a process of heavy abstraction of the intermediate languages and their semantics.
104We hope that this process will ease the burden of Deliverable D4.4, the proof of correctness for the compiler.
105
106\newpage
107
108\tableofcontents
109
110\newpage
111
112%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
113% SECTION.                                                                    %
114%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
115\section{Task}
116\label{sect.task}
117
118The Grant Agreement states that Task T4.3, entitled `Formal semantics of intermediate languages' has associated Deliverable D4.3, consisting of the following:
119\begin{quotation}
120Executable Formal Semantics of back-end intermediate languages: This prototype is the formal counterpart of deliverable D2.1 for the back end side of the compiler and validates it.
121\end{quotation}
122This report details our implementation of this deliverable.
123
124%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
125% SECTION.                                                                    %
126%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
127\subsection{Connections with other deliverables}
128\label{subsect.connections.with.other.deliverables}
129
130Deliverable D4.3 enjoys a close relationship with three other deliverables, namely deliverables D2.2, D4.3 and D4.4.
131
132Deliverable 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.
133In particular, the architecture of the compiler, its intermediate languages and their semantics, and the overall implementation of the Matita encodings has been taken from the O'Caml compiler.
134Any 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.
135
136Deliverable D4.2 can be seen as a `sister' deliverable to the deliverable reported on herein.
137In particular, where this deliverable reports on the encoding in the Calculus of Constructions of the backend semantics, D4.2 is the encoding in the Calculus of Constructions of the mutual translations of those languages.
138As a result, a substantial amount of Matita code is shared between the two deliverables.
139
140Deliverable D4.4, the backend correctness proofs, is the immediate successor of this deliverable.
141
142%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
143% SECTION.                                                                    %
144%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
145\section{The backend intermediate languages' semantics in Matita}
146\label{sect.backend.intermediate.languages.semantics.matita}
147
148%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
149% SECTION.                                                                    %
150%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
151\subsection{Abstracting related languages}
152\label{subsect.abstracting.related.languages}
153
154As mentioned in the report for Deliverable D4.2, a systematic process of abstraction, over the O'Caml code, has taken place in the Matita encoding.
155In particular, we have merged many of the syntaxes of the intermediate languages (i.e. RTL, ERTL, LTL and LIN) into a single `joint' syntax, which is parameterised by various types.
156Equivalent intermediate languages to those present in the O'Caml code can be recovered by specialising this joint structure.
157
158As mentioned in the report for Deliverable D4.2, there are a number of advantages that this process of abstraction brings, from code reuse to allowing us to get a clearer view of the intermediate languages and their structure.
159However, the semantics of the intermediate languages allow us to concretely demonstrate this improvement in clarity, by noting that the semantics of the LTL and the semantics of the LIN languages are identical.
160In particular, the semantics of both LTL and LIN are implemented in exactly the same way.
161The only difference between the two languages is how the next instruction to be interpreted is fetched.
162In LTL, this involves looking up in a graph, whereas in LTL, this involves fetching from a list of instructions.
163
164As a result, we see that the semantics of LIN and LTL are both instances of a single, more general language that is parametric in how the next instruction is fetched.
165Furthermore, any prospective proof that the semantics of LTL and LIN are identical is not almost trivial, saving a deal of work in Deliverable D4.4.
166
167%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
168% SECTION.                                                                    %
169%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
170\subsection{Type parameters, and their purpose}
171\label{subsect.type.parameters.their.purpose}
172
173We mentioned in the Deliverable D4.2 report that all joint languages are parameterised by a number of types, which are later specialised to each distinct intermediate language.
174As this parameterisation process is also dependent on designs decisions in the language semantics, we have so far held off summarising the role of each parameter.
175
176We begin the abstraction process with the \texttt{params\_\_} record.
177This holds the types of the representations of the different register varieties in the intermediate languages:
178\begin{lstlisting}
179record params__: Type[1] ≝
180{
181  acc_a_reg: Type[0];
182  acc_b_reg: Type[0];
183  dpl_reg: Type[0];
184  dph_reg: Type[0];
185  pair_reg: Type[0];
186  generic_reg: Type[0];
187  call_args: Type[0];
188  call_dest: Type[0];
189  extend_statements: Type[0]
190}.
191\end{lstlisting}
192We summarise what these types mean, and how they are used in both the semantics and the translation process:
193\begin{center}
194\begin{tabular*}{\textwidth}{p{4cm}p{11cm}}
195Type & Explanation \\
196\hline
197\texttt{acc\_a\_reg} & The type of the accumulator A register.  In some languages this is implemented as the hardware accumulator, whereas in others this is a pseudoregister.\\
198\texttt{acc\_b\_reg} & Similar to the accumulator A field, but for the processor's auxilliary accumulator, B. \\
199\texttt{dpl\_reg} & The type of the representation of the low eight bit register of the MCS-51's single 16 bit register, DPL.  Can be either a pseudoregister or the hardware DPL register. \\
200\texttt{dph\_reg} & Similar to the DPL register but for the eight high bits of the 16-bit register. \\
201\texttt{pair\_reg} & Various different `move' instructions have been merged into a single move instruction in the joint language.  A value can either be moved to or from the accumulator in some languages, or moved to and from an arbitrary pseudoregister in others.  This type encodes how we should move data around the registers and accumulators. \\
202\texttt{generic\_reg} & The representation of generic registers (i.e. those that are not devoted to a specific task). \\
203\texttt{call\_args} & The number of arguments to a function.  For some languages this is irrelevant. \\
204\texttt{call\_dest} & \\
205\texttt{extend\_statements} & Instructions that are specific to a particular intermediate language, and which cannot be abstracted into the joint language.
206\end{tabular*}
207\end{center}
208
209As mentioned in the report for Deliverable D4.2, the record \texttt{params\_\_} is enough to be able to specify the instructions of the joint languages:
210\begin{lstlisting}
211inductive joint_instruction (p: params__) (globals: list ident): Type[0] :=
212  | COMMENT: String → joint_instruction p globals
213  | COST_LABEL: costlabel → joint_instruction p globals
214  ...
215\end{lstlisting}
216
217We extend \texttt{params\_\_} with a type corresponding to labels, \texttt{succ}, obtaining a new record type of parameters called \texttt{params\_}:
218\begin{lstlisting}
219record params_: Type[1] ≝
220{
221  pars__ :> params__;
222  succ: Type[0]
223}.
224\end{lstlisting}
225The type \texttt{succ} corresponds to labels, in the case of control flow graph based languages, or is instantiated to the unit type for the linearised language, LIN.
226Using \texttt{param\_} we can define statements of the joint language:
227\begin{lstlisting}
228inductive joint_statement (p:params_) (globals: list ident): Type[0] :=
229  | sequential: joint_instruction p globals → succ p → joint_statement p globals
230  | GOTO: label → joint_statement p globals
231  | RETURN: joint_statement p globals.
232\end{lstlisting}
233Note that in the joint language, instructions are `linear', in that they have an immediate successor.
234Statements, on the other hand, consist of either a linear instruction, or a \texttt{GOTO} or \texttt{RETURN} statement, both of which can jump to an arbitrary place in the program.
235
236For the semantics, we need further parametererised types.
237In particular, we parameterise the result and parameter type of an internal function call in \texttt{params0}:
238\begin{lstlisting}
239record params0: Type[1] ≝
240 { pars__' :> params__
241 ; resultT: Type[0]
242 ; paramsT: Type[0]
243 }.
244\end{lstlisting}
245We further extend \texttt{params0} with a type for local variables in internal function calls:
246\begin{lstlisting}
247record params1 : Type[1] ≝
248 { pars0 :> params0
249 ; localsT: Type[0]
250 }.
251\end{lstlisting}
252Again, we expand our parameters with types corresponding to the code representation (either a control flow graph or a list of statements).
253Further, we hypothesise a generic method for looking up the next instruction in the graph, called \texttt{lookup}.
254Note that \texttt{lookup} may fail, and returns an \texttt{option} type:
255\begin{lstlisting}
256record params (globals: list ident): Type[1] ≝
257 { succ_ : Type[0]
258 ; pars1 :> params1
259 ; codeT: Type[0]
260 ; lookup: codeT → label → option (joint_statement (mk_params_ pars1 succ_) globals)
261 }.
262\end{lstlisting}
263We now have what we need to define internal functions for the joint language.
264The first two `universe' fields are only used in the compilation process, for generating fresh names, and do not affect the semantics.
265The rest of the fields affect both compilation and semantics.
266In particular, we have parameterised result types, function parameter types and the type of local variables.
267Note also that we have lifted the hypothesised \texttt{lookup} function from \texttt{params} into a dependent sigma type, which combines a label (the entry and exit points of the control flow graph or list) combined with a proof that the label is in the graph structure:
268\begin{lstlisting}
269record joint_internal_function (globals: list ident) (p:params globals) : Type[0] :=
270{
271  joint_if_luniverse: universe LabelTag;
272  joint_if_runiverse: universe RegisterTag;
273  joint_if_result   : resultT p;
274  joint_if_params   : paramsT p;
275  joint_if_locals   : localsT p;
276  joint_if_stacksize: nat;
277  joint_if_code     : codeT … p;
278  joint_if_entry    : $\Sigma$l: label. lookup … joint_if_code l ≠ None ?;
279  joint_if_exit     : $\Sigma$l: label. lookup … joint_if_code l ≠ None ?
280}.
281\end{lstlisting}
282Naturally, a question arises as to why we have chosen to split up the parameterisation into so many intermediate records, each slightly extending earlier ones.
283The reason is because some intermediate languages share a host of parameters, and only differ on some others.
284For instance, in instantiating the ERTL language, certain parameters are shared with RTL, whilst others are ERTL specific:
285\begin{lstlisting}
286...
287definition ertl_params__: params__ :=
288 mk_params__ register register register register (move_registers × move_registers)
289  register nat unit ertl_statement_extension.
290...
291definition ertl_params1: params1 := rtl_ertl_params1 ertl_params0.
292definition ertl_params: ∀globals. params globals ≝ rtl_ertl_params ertl_params0.
293...
294definition ertl_statement := joint_statement ertl_params_.
295
296definition ertl_internal_function :=
297  $\lambda$globals.joint_internal_function … (ertl_params globals).
298\end{lstlisting}
299Here, \texttt{rtl\_ertl\_params1} are the common parameters of the ERTL and RTL languages:
300\begin{lstlisting}
301definition rtl_ertl_params1 := $\lambda$pars0. mk_params1 pars0 (list register).
302\end{lstlisting}
303
304The record \texttt{more\_sem\_params} bundles together functions that store and retrieve values in various forms of register:
305\begin{lstlisting}
306record more_sem_params (p:params_): Type[1] :=
307{
308  framesT: Type[0];
309  empty_framesT: framesT;
310  regsT: Type[0];
311  empty_regsT: regsT;
312  call_args_for_main: call_args p;
313  call_dest_for_main: call_dest p;
314  succ_pc: succ p → address → res address;
315  greg_store_: generic_reg p → beval → regsT → res regsT;
316  greg_retrieve_: regsT → generic_reg p → res beval;
317  acca_store_: acc_a_reg p → beval → regsT → res regsT;
318  acca_retrieve_: regsT → acc_a_reg p → res beval;
319  ...
320  dpl_store_: dpl_reg p → beval → regsT → res regsT;
321  dpl_retrieve_: regsT → dpl_reg p → res beval;
322  ...
323  pair_reg_move_: regsT → pair_reg p → res regsT;
324  pointer_of_label: label → $\Sigma$p:pointer. ptype p = Code
325}.
326\end{lstlisting}
327For instance, \texttt{greg\_store\_} and \texttt{greg\_retrieve\_} store and retrieve values from a generic register, respectively.
328Similarly, \texttt{pair\_reg\_move} implements the generic move instruction of the joint language.
329Here \texttt{framesT} is the type of stack frames, with \texttt{empty\_framesT} an empty stack frame.
330
331The two hypothesised values \texttt{call\_args\_for\_main} and \texttt{call\_dest\_for\_main} deal with problems with the \texttt{main} function of the program, and how it is handled.
332In particular, we need to know when the \texttt{main} function has finished executing.
333But this is complicated, in C, by the fact that the \texttt{main} function is explicitly allowed to be recursive (disallowed in C++).
334Therefore, to understand whether the exiting \texttt{main} function is really exiting, or just recursively calling itself, we need to remember the address at which \texttt{main} is located.
335This is done with \texttt{call\_dest\_for\_main}, whereas \texttt{call\_args\_for\_main} holds the \texttt{main} function's arguments.
336
337We extend \texttt{more\_sem\_params} with yet more parameters via \texttt{more\_sem\_params2}:
338\begin{lstlisting}
339record more_sem_params2 (globals: list ident) (p: params globals) : Type[1] :=
340{
341  more_sparams1 :> more_sem_params p;
342  fetch_statement:
343    genv … p → state (mk_sem_params … more_sparams1) →
344    res (joint_statement (mk_sem_params … more_sparams1) globals);
345  ...
346  save_frame:
347    address → nat → paramsT … p → call_args p → call_dest p →
348    state (mk_sem_params … more_sparams1) →
349    res (state (mk_sem_params … more_sparams1));
350  pop_frame:
351    genv globals p → state (mk_sem_params … more_sparams1) →
352    res ((state (mk_sem_params … more_sparams1)));
353  ...
354  set_result:
355    list val → state (mk_sem_params … more_sparams1) →
356    res (state (mk_sem_params … more_sparams1));
357  exec_extended:
358    genv globals p → extend_statements (mk_sem_params … more_sparams1) →
359    succ p → state (mk_sem_params … more_sparams1) →
360    IO io_out io_in (trace × (state (mk_sem_params … more_sparams1)))
361 }.
362\end{lstlisting}
363Here, \texttt{fetch\_statement} fetches the next statement to be executed, \texttt{save\_frame} and \texttt{pop\_frame} manipulate stack frames, \texttt{set\_result} saves the result of the function computation, and \texttt{exec\_extended} is a function that executes the extended statements, peculiar to each individual intermediate language.
364
365We bundle \texttt{params} and \texttt{sem\_params} together into a single record.
366This will be used in the function \texttt{eval\_statement} which executes a single statement of the joint language:
367\begin{lstlisting}
368record sem_params2 (globals: list ident): Type[1] :=
369{
370  p2 :> params globals;
371  more_sparams2 :> more_sem_params2 globals p2
372}.
373\end{lstlisting}
374\noindent
375The \texttt{state} record holds the current state of the interpreter:
376\begin{lstlisting}
377record state (p: sem_params): Type[0] :=
378{
379  st_frms: framesT ? p;
380  pc: address;
381  sp: pointer;
382  carry: beval;
383  regs: regsT ? p;
384  m: bemem
385}.
386\end{lstlisting}
387Here \texttt{st\_frms} represent stack frames, \texttt{pc} the program counter, \texttt{sp} the stack pointer, \texttt{carry} the carry flag, \texttt{regs} the generic registers and \texttt{m} external RAM.
388We use the function \texttt{eval\_statement} to evaluate a single joint statement:
389\begin{lstlisting}
390definition eval_statement:
391  ∀globals: list ident.∀p:sem_params2 globals.
392    genv globals p → state p → IO io_out io_in (trace × (state p)) :=
393...
394\end{lstlisting}
395We examine the type of this function.
396Note that it returns a monadic action, \texttt{IO}, denoting that it may have an IO \emph{side effect}, where the program reads or writes to some external device or memory address.
397Monads and their use are further discussed in Subsection~\ref{subsect.use.of.monads}.
398Further, the function returns a new state, updated by the single step of execution of the program.
399Finally, a \emph{trace} is also returned, which records the trace of cost labels that the program passes through, in order to calculate the cost model for the CerCo compiler.
400
401%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
402% SECTION.                                                                    %
403%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
404\subsection{Use of monads}
405\label{subsect.use.of.monads}
406
407Monads are a categorical notion that have recently gained an amount of traction in functional programming circles.
408In particular, it was noted by Moggi that monads could be used to sequence \emph{effectful} computations in a pure manner.
409Here, `effectful computations' cover a lot of ground, from writing to files, generating fresh names, or updating an ambient notion of state.
410
411A monad can be characterised by the following:
412\begin{itemize}
413\item
414A data type, $M$.
415For instance, the \texttt{option} type in O'Caml or Matita.
416\item
417A way to `inject' or `lift' pure values into this data type (usually called \texttt{return}).
418We call this function \texttt{return} and say that it must have type $\alpha \rightarrow M \alpha$, where $M$ is the name of the monad.
419In our example, the `lifting' function for the \texttt{option} monad can be implemented as:
420\begin{lstlisting}
421let return x = Some x
422\end{lstlisting}
423\item
424A way to `sequence' monadic functions together, to form another monadic function, usually called \texttt{bind}.
425Bind has type $M \alpha \rightarrow (\alpha \rightarrow M \beta) \rightarrow M \beta$.
426We can see that bind `unpacks' a monadic value, applies a function after unpacking, and `repacks' the new value in the monad.
427In our example, the sequencing function for the \texttt{option} monad can be implemented as:
428\begin{lstlisting}
429let bind o f =
430  match o with
431    None -> None
432    Some s -> f s
433\end{lstlisting}
434\item
435A series of algebraic laws that relate \texttt{return} and \texttt{bind}, ensuring that the sequencing operation `does the right thing' by retaining the order of effects.
436These \emph{monad laws} should also be useful in reasoning about monadic computations in the proof of correctness of the compiler.
437\end{itemize}
438In the semantics of both front and backend intermediate languages, we make use of monads.
439This monadic infrastructure is shared between the frontend and backend languages.
440
441In particular, an `IO' monad, signalling the emission or reading of data to some external location or memory address, is heavily used in the semantics of the intermediate languages.
442Here, the monad's sequencing operation ensures that emissions and reads are maintained in the correct order.
443Most functions in the intermediate language semantics fall into the IO monad.
444In particular, we have already seen how the \texttt{eval\_statement} function of the joint language is monadic, with type:
445\begin{lstlisting}
446definition eval_statement:
447  ∀globals: list ident.∀p:sem_params2 globals.
448    genv globals p → state p → IO io_out io_in (trace × (state p)) :=
449...
450\end{lstlisting}
451If we in the body of \texttt{eval\_statement}, we may also see how the monad sequences effects.
452For instance, in the case for the \texttt{LOAD} statement, we have the following:
453\begin{lstlisting}
454definition eval_statement:
455  ∀globals: list ident. ∀p:sem_params2 globals.
456    genv globals p → state p → IO io_out io_in (trace × (state p)) :=
457  $\lambda$globals, p, ge, st.
458  ...
459  match s with
460  | LOAD dst addrl addrh ⇒
461    ! vaddrh $\leftarrow$ dph_retrieve … st addrh;
462    ! vaddrl $\leftarrow$ dpl_retrieve … st addrl;
463    ! vaddr $\leftarrow$ pointer_of_address 〈vaddrl,vaddrh〉;
464    ! v $\leftarrow$ opt_to_res … (msg FailedLoad) (beloadv (m … st) vaddr);
465    ! st $\leftarrow$ acca_store p … dst v st;
466    ! st $\leftarrow$ next … l st ;
467      ret ? $\langle$E0, st$\rangle$
468\end{lstlisting}
469Here, we employ a certain degree of syntactic sugaring.
470The syntax
471\begin{lstlisting}
472  ...
473! vaddrh $\leftarrow$ dph_retrieve … st addrh;
474! vaddrl $\leftarrow$ dpl_retrieve … st addrl;
475  ...
476\end{lstlisting}
477is sugaring for the \texttt{IO} monad's binding operation.
478We can expand this sugaring to the following much more verbose code:
479\begin{lstlisting}
480  ...
481  bind (dph_retrieve … st addrh) ($\lambda$vaddrh. bind (dpl_retrieve … st addrl)
482    ($\lambda$vaddrl. ...))
483\end{lstlisting}
484Note also that the function \texttt{ret} is implementing the `lifting', or return function of the \texttt{IO} monad.
485
486We believe the sugaring for the monadic bind operation makes the program much more readable, and therefore easier to reason about.
487In particular, note that the functions \texttt{dph\_retrieve}, \texttt{pointer\_of\_address}, \texttt{acca\_store} and \texttt{next} are all monadic.
488The function \texttt{opt\_to\_res} is also --- this is a `utility' function that lifts an option type into the \texttt{IO} monad.
489
490\subsection{Memory models}
491\label{subsect.memory.models}
492
493Currently, the semantics of the front and backend intermediate languages are built around two distinct memory models.
494The frontend languages reuse the CompCert memory model, whereas the backend languages employ a bespoke model tailored to their needs.
495This split between the memory models is reflective of the fact that the front and backend language have different requirements from their memory models, to a certain extent.
496
497In particular, the CompCert memory model places quite heavy restrictions on where in memory one can read from.
498To read a value in this memory model, you must supply an address, complete with a number of `chunks' to read following that address.
499The read is only successful if you attempt to read at a genuine `value boundary', and read the appropriate number of memory chunks for that value.
500As a result, with the CompCert memory model you are unable to read the third byte of a 32-bit integer value directly from memory, for instance.
501This has some consequences for the CompCert compiler, namely an inability to write a \texttt{memcpy} routine.
502
503However, the CerCo memory model operates differently, as we need to move data `piecemeal' between stacks in the backend of the compiler.
504As a result, the bespoke memory model allows one to read data at any memory location, not just on value boundaries.
505This has the advantage that we can successfully write a \texttt{memcpy} routine with the CerCo compiler (remembering that \texttt{memcpy} is nothing more than `read a byte, copy a byte' repeated in a loop), an advantage over CompCert.
506
507Right now, the two memory models are interfaced during the translation from RTLabs to RTL.
508It is an open question whether we will unify the two memory models, using only the backend, bespoke memory model throughout the compiler, as the CompCert memory model seems to work fine for the frotend, where such byte-by-byte copying is not needed.
509However, should we decide to port the frontend to the new memory model, it has been written in such an abstract way that doing so would be relatively straightforward.
510
511%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
512% SECTION.                                                                    %
513%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
514\section{Future work}
515\label{sect.future.work}
516
517A few small axioms remain to be closed.
518These relate to fetching the next instruction to be interpreted from the control flow graph, or linearised representation, of the language.
519Closing these axioms should not be a problem.
520
521Further, tailcalls to external functions are currently axiomatised.
522This is due to there being a difficulty with how stackframes are handled with external function calls.
523We leave this for further work, due to there being no pressing need to implement this feature at the present time.
524
525There is also, as mentioned, an open problem as to whether the frontend languages should use the same memory model as the backend languages, as opposed to reusing the CompCert memory model.
526Should this decision be taken, this will likely be straightforward but potentially time consuming.
527
528\newpage
529
530%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
531% SECTION.                                                                    %
532%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
533\section{Code listing}
534\label{sect.code.listing}
535
536%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
537% SECTION.                                                                    %
538%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
539\subsection{Listing of files}
540\label{subsect.listing.files}
541
542Semantics specific files (files relating to language translations ommitted).
543Syntax specific files are presented in Table~\ref{table.syntax}.
544\begin{landscape}
545\begin{table}
546\begin{threeparttable}
547\begin{tabular}{llll}
548Title & Description & O'Caml & Ratio \\
549\hline
550\texttt{RTLabs/syntax.ma} & The syntax of RTLabs & \texttt{RTLabs/RTLabs.mli} & 0.65 \\
551\texttt{joint/Joint.ma} & Abstracted syntax for backend languages & N/A & N/A \\
552\texttt{RTL/RTL.ma} & The syntax of RTL & \texttt{RTL/RTL.mli} & 1.85\tnote{b} \\
553\texttt{ERTL/ERTL.ma} & The syntax of ERTL & \texttt{ERTL/ERTL.mli} & 1.04\tnote{b} \\
554\texttt{LIN/joint\_LTL\_LIN.ma} & The syntax of the abstracted combined LTL and LIN language & N/A & N/A \\
555\texttt{LTL/LTL.ma} & The specialisation of the above file to the syntax of LTL & \texttt{LTL/LTL.mli} & 1.86\tnote{a} \\
556\texttt{LIN/LIN.ma} & The specialisation of the above file to the syntax of LIN & \texttt{LIN/LIN.mli} & 2.27\tnote{a}
557\end{tabular}
558\begin{tablenotes}
559  \item[a] Includes \texttt{joint/Joint\_LTL\_LIN.ma} and \texttt{joint/Joint.ma}.
560  \item[b] Includes \texttt{joint/Joint.ma}.
561\end{tablenotes}
562\end{threeparttable}
563\caption{Syntax specific files in the intermediate language semantics}
564\label{table.syntax}
565\end{table}
566\end{landscape}
567Here, the O'Caml column denotes the O'Caml source file in the prototype compiler's implementation that corresponds to the Matita script in question.
568The ratios are the linecounts of the Matita file divided by the line counts of the corresponding O'Caml file.
569These are computed with \texttt{wc -l}, a standard Unix tool.
570
571Semantics specific files are presented in Table~\ref{table.semantics}.
572\begin{landscape}
573\begin{table}
574\begin{threeparttable}
575\begin{tabular}{llll}
576Title & Description & O'Caml & Ratio \\
577\hline
578\texttt{RTLabs/semantics.ma} & Semantics of RTLabs & \texttt{RTLabs/RTLabsInterpret.ml} & 0.63 \\
579\texttt{joint/semantics.ma} & Semantics of the abstracted languages & N/A & N/A  \\
580\texttt{joint/SemanticUtils.ma} & Generic utilities used in semantics `joint' languages & N/A & N/A \\
581\texttt{RTL/semantics.ma} & Semantics of RTL & \texttt{RTL/RTLInterpret.ml} & 1.88\tnote{a} \\
582\texttt{ERTL/semantics.ma} & Semantics of ERTL & \texttt{ERTL/ERTLInterpret.ml} & 1.22\tnote{a} \\
583\texttt{LIN/joint\_LTL\_LIN\_semantics.ma} & Semantics of the joint LTL-LIN language & N/A & N/A \\
584\texttt{LTL/semantics.ma} & Semantics of LTL & \texttt{LTL/LTLInterpret.ml} & 1.25\tnote{a}\tnote{b} \\
585\texttt{LIN/semantics.ma} & Semantics of LIN & \texttt{LIN/LINInterpret.ml} & 1.52\tnote{a}\tnote{b}
586\end{tabular}
587\begin{tablenotes}
588  \item{a} Includes \texttt{joint/semantics.ma} and \texttt{joint/SemanticUtils.ma}.
589  \item{b} Includes \texttt{joint/joint\_LTL\_LIN\_semantics.ma}.
590\end{tablenotes}
591\end{threeparttable}
592\caption{Semantics specific files in the intermediate language semantics}
593\label{table.semantics}
594\end{table}
595\end{landscape}
596
597%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
598% SECTION.                                                                    %
599%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
600\subsection{Listing of important functions and axioms}
601\label{subsect.listing.important.functions.axioms}
602
603We list some important functions and axioms in the backend semantics:
604
605\end{document}
Note: See TracBrowser for help on using the repository browser.