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

Last change on this file since 1446 was 1446, checked in by sacerdot, 9 years ago

...

File size: 37.3 KB
RevLine
[1356]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}
[1414]12\usepackage{lscape}
[1356]13\usepackage{stmaryrd}
[1414]14\usepackage{threeparttable}
[1356]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}
[1399]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},
[1356]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,
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
95\markright{\cerco{}, FP7-ICT-2009-C-243881}
96
97\newpage
98
99\vspace*{7cm}
100\paragraph{Abstract}
[1398]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.
[1356]105
106\newpage
107
108\tableofcontents
109
110\newpage
[1365]111
112%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
113% SECTION.                                                                    %
114%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
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
[1366]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
[1365]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
[1373]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
[1394]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.
[1427]165Furthermore, any prospective proof that the semantics of LTL and LIN are identical is now almost trivial, saving a deal of work in Deliverable D4.4.
[1394]166
[1365]167%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
168% SECTION.                                                                    %
169%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
[1397]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
[1400]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:
[1397]178\begin{lstlisting}
[1434]179record params__: Type[1] :=
[1397]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}
[1400]192We summarise what these types mean, and how they are used in both the semantics and the translation process:
[1398]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. \\
[1400]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). \\
[1427]203\texttt{call\_args} & The actual arguments passed to a function.  For some languages this is simply the number of arguments passed to the function. \\
204\texttt{call\_dest} & The destination of the function call. \\
[1400]205\texttt{extend\_statements} & Instructions that are specific to a particular intermediate language, and which cannot be abstracted into the joint language.
[1398]206\end{tabular*}
207\end{center}
[1397]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] :=
[1434]212  | COMMENT: String $\rightarrow$ joint_instruction p globals
213  | COST_LABEL: costlabel $\rightarrow$ joint_instruction p globals
[1397]214  ...
[1434]215  | OP1: Op1 $\rightarrow$ acc_a_reg p $\rightarrow$ acc_a_reg p $\rightarrow$ joint_instruction p globals
[1446]216  | COND: acc_a_reg p $\rightarrow$ label $\rightarrow$ joint_instruction p globals
[1427]217  ...
[1397]218\end{lstlisting}
[1427]219Here, we see that the instruction \texttt{OP1} (a unary operation on the accumulator A) can be given quite a specific type, through the use of the \texttt{params\_\_} data structure.
[1397]220
[1427]221Joint statements can be split into two subclasses: those who simply pass the flow of control onto their successor statement, and those that jump to a potentially remote location in the program.
222Naturally, as some intermediate languages are graph based, and others linearised, the passing act of passing control on to the successor' instruction can either be the act of following a graph edge in a control flow graph, or incrementing an index into a list.
223We make a distinction between instructions that pass control onto their immediate successors, and those that jump elsewhere in the program, through the use of \texttt{succ}, denoting the immediate successor of the current instruction, in the \texttt{params\_} record described below.
[1397]224\begin{lstlisting}
[1434]225record params_: Type[1] :=
[1397]226{
227  pars__ :> params__;
228  succ: Type[0]
229}.
230\end{lstlisting}
[1398]231The 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.
232Using \texttt{param\_} we can define statements of the joint language:
[1397]233\begin{lstlisting}
234inductive joint_statement (p:params_) (globals: list ident): Type[0] :=
[1434]235  | sequential: joint_instruction p globals $\rightarrow$ succ p $\rightarrow$ joint_statement p globals
236  | GOTO: label $\rightarrow$ joint_statement p globals
[1397]237  | RETURN: joint_statement p globals.
238\end{lstlisting}
[1398]239Note that in the joint language, instructions are linear', in that they have an immediate successor.
[1446]240Statements, 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. The conditional jump instruction COND is linear', since it
241has an immediate successor, but it also takes an arbitrary location (a label)
[1397]243
[1398]244For the semantics, we need further parametererised types.
245In particular, we parameterise the result and parameter type of an internal function call in \texttt{params0}:
[1397]246\begin{lstlisting}
[1434]247record params0: Type[1] :=
248{
249  pars__' :> params__;
250  resultT: Type[0];
251  paramsT: Type[0]
252}.
[1397]253\end{lstlisting}
[1427]254Here, \texttt{resultT} and \texttt{resultT} typically are the (pseudo)registers that store the parameters and result of a function.
255
[1398]256We further extend \texttt{params0} with a type for local variables in internal function calls:
[1397]257\begin{lstlisting}
[1434]258record params1 : Type[1] :=
259{
260  pars0 :> params0;
261  localsT: Type[0]
262}.
[1397]263\end{lstlisting}
[1399]264Again, we expand our parameters with types corresponding to the code representation (either a control flow graph or a list of statements).
265Further, we hypothesise a generic method for looking up the next instruction in the graph, called \texttt{lookup}.
266Note that \texttt{lookup} may fail, and returns an \texttt{option} type:
[1397]267\begin{lstlisting}
[1434]268record params (globals: list ident): Type[1] :=
269{
270  succ_ : Type[0];
271  pars1 :> params1;
272  codeT : Type[0];
273  lookup: codeT $\rightarrow$ label $\rightarrow$ option (joint_statement (mk_params_ pars1 succ_) globals)
274}.
[1397]275\end{lstlisting}
[1399]276We now have what we need to define internal functions for the joint language.
277The first two universe' fields are only used in the compilation process, for generating fresh names, and do not affect the semantics.
278The rest of the fields affect both compilation and semantics.
[1427]279In particular, we have a description of the result, parameters and the local variables of a function.
[1399]280Note 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:
[1397]281\begin{lstlisting}
282record joint_internal_function (globals: list ident) (p:params globals) : Type[0] :=
[1403]283{
284  joint_if_luniverse: universe LabelTag;
285  joint_if_runiverse: universe RegisterTag;
[1397]286  joint_if_result   : resultT p;
287  joint_if_params   : paramsT p;
288  joint_if_locals   : localsT p;
289  joint_if_stacksize: nat;
[1434]290  joint_if_code     : codeT ... p;
291  joint_if_entry    : $\Sigma$l: label. lookup ... joint_if_code l $\neq$ None ?;
292  joint_if_exit     : $\Sigma$l: label. lookup ... joint_if_code l $\neq$ None ?
[1397]293}.
294\end{lstlisting}
[1399]295Naturally, a question arises as to why we have chosen to split up the parameterisation into so many intermediate records, each slightly extending earlier ones.
296The reason is because some intermediate languages share a host of parameters, and only differ on some others.
297For instance, in instantiating the ERTL language, certain parameters are shared with RTL, whilst others are ERTL specific:
298\begin{lstlisting}
299...
300definition ertl_params__: params__ :=
[1434]301 mk_params__ register register register register (move_registers $\times$ move_registers)
[1399]302  register nat unit ertl_statement_extension.
303...
304definition ertl_params1: params1 := rtl_ertl_params1 ertl_params0.
[1434]305definition ertl_params: ∀globals. params globals := rtl_ertl_params ertl_params0.
[1399]306...
307definition ertl_statement := joint_statement ertl_params_.
[1397]308
[1399]309definition ertl_internal_function :=
[1434]310  $\lambda$globals.joint_internal_function ... (ertl_params globals).
[1399]311\end{lstlisting}
312Here, \texttt{rtl\_ertl\_params1} are the common parameters of the ERTL and RTL languages:
313\begin{lstlisting}
314definition rtl_ertl_params1 := $\lambda$pars0. mk_params1 pars0 (list register).
315\end{lstlisting}
316
[1407]317The record \texttt{more\_sem\_params} bundles together functions that store and retrieve values in various forms of register:
[1403]318\begin{lstlisting}
319record more_sem_params (p:params_): Type[1] :=
320{
321  framesT: Type[0];
[1413]322  empty_framesT: framesT;
[1427]323
[1403]324  regsT: Type[0];
[1413]325  empty_regsT: regsT;
[1427]326
[1413]327  call_args_for_main: call_args p;
328  call_dest_for_main: call_dest p;
[1427]329
[1434]330  succ_pc: succ p $\rightarrow$ address $\rightarrow$ res address;
[1427]331
[1434]332  greg_store_: generic_reg p $\rightarrow$ beval $\rightarrow$ regsT $\rightarrow$ res regsT;
333  greg_retrieve_: regsT $\rightarrow$ generic_reg p $\rightarrow$ res beval;
334  acca_store_: acc_a_reg p $\rightarrow$ beval $\rightarrow$ regsT $\rightarrow$ res regsT;
335  acca_retrieve_: regsT $\rightarrow$ acc_a_reg p $\rightarrow$ res beval;
[1413]336  ...
[1434]337  dpl_store_: dpl_reg p $\rightarrow$ beval $\rightarrow$ regsT $\rightarrow$ res regsT;
338  dpl_retrieve_: regsT $\rightarrow$ dpl_reg p $\rightarrow$ res beval;
[1413]339  ...
[1434]340  pair_reg_move_: regsT $\rightarrow$ pair_reg p $\rightarrow$ res regsT;
341  pointer_of_label: label $\rightarrow$ $\Sigma$p:pointer. ptype p = Code
[1403]342}.
343\end{lstlisting}
[1427]344Here, the fields \texttt{empty\_framesT}, \texttt{empty\_regsT}, \texttt{call\_args\_for\_main} and \texttt{call\_dest\_for\_main} are used for state initialisation.
345
346The field \texttt{succ\_pc} takes an address, and a successor' label, and returns the address of the instruction immediately succeeding the one at hand.
347
348The fields \texttt{greg\_store\_} and \texttt{greg\_retrieve\_} store and retrieve values from a generic register, respectively.
[1407]349Similarly, \texttt{pair\_reg\_move} implements the generic move instruction of the joint language.
[1413]350Here \texttt{framesT} is the type of stack frames, with \texttt{empty\_framesT} an empty stack frame.
[1403]351
[1413]352The 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.
353In particular, we need to know when the \texttt{main} function has finished executing.
354But this is complicated, in C, by the fact that the \texttt{main} function is explicitly allowed to be recursive (disallowed in C++).
[1427]355Therefore, to understand whether the exiting \texttt{main} function is really exiting, or just recursively calling itself, we need to remember the address to which \texttt{main} will return control once the initial call to \texttt{main} has finished executing.
[1413]356This is done with \texttt{call\_dest\_for\_main}, whereas \texttt{call\_args\_for\_main} holds the \texttt{main} function's arguments.
357
[1407]358We extend \texttt{more\_sem\_params} with yet more parameters via \texttt{more\_sem\_params2}:
[1403]359\begin{lstlisting}
360record more_sem_params2 (globals: list ident) (p: params globals) : Type[1] :=
361{
362  more_sparams1 :> more_sem_params p;
[1407]363  fetch_statement:
[1434]364    genv ... p $\rightarrow$ state (mk_sem_params ... more_sparams1) $\rightarrow$
365    res (joint_statement (mk_sem_params ... more_sparams1) globals);
[1407]366  ...
367  save_frame:
[1434]368    address $\rightarrow$ nat $\rightarrow$ paramsT ... p $\rightarrow$ call_args p $\rightarrow$ call_dest p $\rightarrow$
369    state (mk_sem_params ... more_sparams1) $\rightarrow$
370    res (state (mk_sem_params ... more_sparams1));
[1407]371  pop_frame:
[1434]372    genv globals p $\rightarrow$ state (mk_sem_params ... more_sparams1) $\rightarrow$
373    res ((state (mk_sem_params ... more_sparams1)));
[1407]374  ...
375  set_result:
[1434]376    list val $\rightarrow$ state (mk_sem_params ... more_sparams1) $\rightarrow$
377    res (state (mk_sem_params ... more_sparams1));
[1407]378  exec_extended:
[1434]379    genv globals p $\rightarrow$ extend_statements (mk_sem_params ... more_sparams1) $\rightarrow$
380    succ p $\rightarrow$ state (mk_sem_params ... more_sparams1) $\rightarrow$
381    IO io_out io_in (trace $\times$ (state (mk_sem_params ... more_sparams1)))
[1403]382 }.
383\end{lstlisting}
[1427]384Here, \texttt{fetch\_statement} fetches the next statement to be executed.
385The fields \texttt{save\_frame} and \texttt{pop\_frame} manipulate stack frames.
386In particular, \texttt{save\_frame} creates a new stack frame on the top of the stack, saving the destination and parameters of a function, and returning an updated state.
387The field \texttt{pop\_frame} destructively pops a stack frame from the stack, returning an updated state.
388Further, \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.
[1403]389
[1407]390We bundle \texttt{params} and \texttt{sem\_params} together into a single record.
391This will be used in the function \texttt{eval\_statement} which executes a single statement of the joint language:
[1403]392\begin{lstlisting}
393record sem_params2 (globals: list ident): Type[1] :=
394{
395  p2 :> params globals;
396  more_sparams2 :> more_sem_params2 globals p2
397}.
398\end{lstlisting}
[1407]399\noindent
[1405]400The \texttt{state} record holds the current state of the interpreter:
401\begin{lstlisting}
402record state (p: sem_params): Type[0] :=
403{
404  st_frms: framesT ? p;
406  sp: pointer;
[1427]407  isp: pointer;
[1405]408  carry: beval;
409  regs: regsT ? p;
410  m: bemem
411}.
412\end{lstlisting}
[1427]413Here \texttt{st\_frms} represent stack frames, \texttt{pc} the program counter, \texttt{sp} the stack pointer, \texttt{isp} the internal stack pointer, \texttt{carry} the carry flag, \texttt{regs} the registers (hardware and pseudoregisters) and \texttt{m} external RAM.
414Note that we have two stack pointers, as we have two stacks: the physical stack of the MCS-51 microprocessor, and an emulated stack in external RAM.
415The MCS-51's own stack is minuscule, therefore it is usual to emulate a much larger, more useful stack in external RAM.
416We require two stack pointers as the MCS-51's \texttt{PUSH} and \texttt{POP} instructions manipulate the physical stack, and not the emulated one.
417
[1405]418We use the function \texttt{eval\_statement} to evaluate a single joint statement:
419\begin{lstlisting}
420definition eval_statement:
421  ∀globals: list ident.∀p:sem_params2 globals.
[1434]422    genv globals p $\rightarrow$ state p $\rightarrow$ IO io_out io_in (trace $\times$ (state p)) :=
[1405]423...
424\end{lstlisting}
[1407]425We examine the type of this function.
426Note 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.
428Further, the function returns a new state, updated by the single step of execution of the program.
[1427]429Finally, a \emph{trace} is also returned, which records externally observable events', such as the calling of external functions and the emission of cost labels.
[1405]430
[1397]431%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
432% SECTION.                                                                    %
433%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
[1365]436
[1391]437Monads are a categorical notion that have recently gained an amount of traction in functional programming circles.
438In particular, it was noted by Moggi that monads could be used to sequence \emph{effectful} computations in a pure manner.
439Here, effectful computations' cover a lot of ground, from writing to files, generating fresh names, or updating an ambient notion of state.
[1365]440
[1407]441A monad can be characterised by the following:
442\begin{itemize}
[1373]443\item
[1407]444A data type, $M$.
445For instance, the \texttt{option} type in O'Caml or Matita.
446\item
447A way to inject' or lift' pure values into this data type (usually called \texttt{return}).
448We call this function \texttt{return} and say that it must have type $\alpha \rightarrow M \alpha$, where $M$ is the name of the monad.
449In our example, the lifting' function for the \texttt{option} monad can be implemented as:
[1391]450\begin{lstlisting}
[1407]451let return x = Some x
[1391]452\end{lstlisting}
453\item
[1407]454A way to sequence' monadic functions together, to form another monadic function, usually called \texttt{bind}.
455Bind has type $M \alpha \rightarrow (\alpha \rightarrow M \beta) \rightarrow M \beta$.
456We can see that bind unpacks' a monadic value, applies a function after unpacking, and repacks' the new value in the monad.
457In our example, the sequencing function for the \texttt{option} monad can be implemented as:
458\begin{lstlisting}
459let bind o f =
460  match o with
461    None -> None
462    Some s -> f s
463\end{lstlisting}
464\item
465A 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.
466These \emph{monad laws} should also be useful in reasoning about monadic computations in the proof of correctness of the compiler.
467\end{itemize}
468In the semantics of both front and backend intermediate languages, we make use of monads.
[1394]469This monadic infrastructure is shared between the frontend and backend languages.
[1373]470
[1427]471In particular, an IO' monad, signalling the emission of a cost label, or the calling of an external function, is heavily used in the semantics of the intermediate languages.
472Here, the monad's sequencing operation ensures that cost label emissions and function calls are maintained in the correct order.
473We have already seen how the \texttt{eval\_statement} function of the joint language is monadic, with type:
[1407]474\begin{lstlisting}
475definition eval_statement:
476  ∀globals: list ident.∀p:sem_params2 globals.
[1434]477    genv globals p $\rightarrow$ state p $\rightarrow$ IO io_out io_in (trace $\times$ (state p)) :=
[1407]478...
479\end{lstlisting}
[1427]480If we examine the body of \texttt{eval\_statement}, we may also see how the monad sequences effects.
[1407]481For instance, in the case for the \texttt{LOAD} statement, we have the following:
482\begin{lstlisting}
483definition eval_statement:
484  ∀globals: list ident. ∀p:sem_params2 globals.
[1434]485    genv globals p $\rightarrow$ state p $\rightarrow$ IO io_out io_in (trace $\times$ (state p)) :=
[1407]486  $\lambda$globals, p, ge, st.
487  ...
488  match s with
[1434]490    ! vaddrh $\leftarrow$ dph_retrieve ... st addrh;
491    ! vaddrl $\leftarrow$ dpl_retrieve ... st addrl;
492    ! vaddr $\leftarrow$ pointer_of_address $\langle$vaddrl,vaddrh$\rangle$;
493    ! v $\leftarrow$ opt_to_res ... (msg FailedLoad) (beloadv (m ... st) vaddr);
494    ! st $\leftarrow$ acca_store p ... dst v st;
495    ! st $\leftarrow$ next ... l st ;
[1407]496      ret ? $\langle$E0, st$\rangle$
497\end{lstlisting}
498Here, we employ a certain degree of syntactic sugaring.
499The syntax
500\begin{lstlisting}
501  ...
[1434]502! vaddrh $\leftarrow$ dph_retrieve ... st addrh;
503! vaddrl $\leftarrow$ dpl_retrieve ... st addrl;
[1407]504  ...
505\end{lstlisting}
506is sugaring for the \texttt{IO} monad's binding operation.
507We can expand this sugaring to the following much more verbose code:
508\begin{lstlisting}
509  ...
[1434]510  bind (dph_retrieve ... st addrh) ($\lambda$vaddrh. bind (dpl_retrieve ... st addrl)
[1407]511    ($\lambda$vaddrl. ...))
512\end{lstlisting}
513Note also that the function \texttt{ret} is implementing the lifting', or return function of the \texttt{IO} monad.
514
515We believe the sugaring for the monadic bind operation makes the program much more readable, and therefore easier to reason about.
516In particular, note that the functions \texttt{dph\_retrieve}, \texttt{pointer\_of\_address}, \texttt{acca\_store} and \texttt{next} are all monadic.
517
[1427]518Note, however, that inside this monadic code, there is also another monad hiding.
519The \texttt{res} monad signals failure, along with an error message.
520The monad's sequencing operation ensures the order of error messages does not get rearranged.
521The function \texttt{opt\_to\_res} lifts an option type into this monad, with an error message to be used in case of failure.
522The \texttt{res} monad is then coerced into the \texttt{IO} monad, ensuring the whole code snippet typechecks.
523
[1413]524\subsection{Memory models}
525\label{subsect.memory.models}
526
527Currently, the semantics of the front and backend intermediate languages are built around two distinct memory models.
528The frontend languages reuse the CompCert memory model, whereas the backend languages employ a bespoke model tailored to their needs.
529This 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.
530
531In particular, the CompCert memory model places quite heavy restrictions on where in memory one can read from.
532To read a value in this memory model, you must supply an address, complete with a number of chunks' to read following that address.
533The 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.
534As 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.
535This has some consequences for the CompCert compiler, namely an inability to write a \texttt{memcpy} routine.
536
537However, the CerCo memory model operates differently, as we need to move data piecemeal' between stacks in the backend of the compiler.
538As a result, the bespoke memory model allows one to read data at any memory location, not just on value boundaries.
539This 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.
540
541Right now, the two memory models are interfaced during the translation from RTLabs to RTL.
[1427]542It 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 frontend, where such byte-by-byte copying is not needed.
[1413]543However, 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.
544
[1365]545%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
546% SECTION.                                                                    %
547%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
548\section{Future work}
549\label{sect.future.work}
550
[1391]551A few small axioms remain to be closed.
552These relate to fetching the next instruction to be interpreted from the control flow graph, or linearised representation, of the language.
553Closing these axioms should not be a problem.
554
[1427]555Most things related to external function calls are currently axiomatised.
[1413]556This is due to there being a difficulty with how stackframes are handled with external function calls.
557We leave this for further work, due to there being no pressing need to implement this feature at the present time.
558
559There 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.
560Should this decision be taken, this will likely be straightforward but potentially time consuming.
561
[1365]562%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
563% SECTION.                                                                    %
564%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
565\section{Code listing}
566\label{sect.code.listing}
567
[1373]568%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
569% SECTION.                                                                    %
570%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
571\subsection{Listing of files}
572\label{subsect.listing.files}
573
[1409]574Semantics specific files (files relating to language translations ommitted).
[1414]575Syntax specific files are presented in Table~\ref{table.syntax}.
[1446]576Here, the O'Caml column denotes the O'Caml source file(s) in the prototype compiler's implementation that corresponds to the Matita script in question.
577The ratios are the linecounts of the Matita file divided by the line counts of the corresponding O'Caml file.
578These are computed with \texttt{wc -l}, a standard Unix tool.
579
580Individual file's ratios are an over approximation, due to the fact that it's hard to relate an individual O'Caml file to the abstracted Matita code that has been spread across multiple files.
581The ratio between total Matita code lines and total O'Caml code lines is more reflective of the compressed and abstracted state of the Matita translation.
582
583Semantics specific files are presented in Table~\ref{table.semantics}.
[1414]584\begin{landscape}
585\begin{table}
586\begin{threeparttable}
587\begin{tabular}{llll}
588Title & Description & O'Caml & Ratio \\
[1365]589\hline
[1427]590\texttt{joint/Joint.ma} & Abstracted syntax for backend languages & N/A & N/A \\
[1414]591\texttt{RTLabs/syntax.ma} & The syntax of RTLabs & \texttt{RTLabs/RTLabs.mli} & 0.65 \\
[1418]592\texttt{RTL/RTL.ma} & The syntax of RTL & \texttt{RTL/RTL.mli} & 1.85\tnote{b} \\
593\texttt{ERTL/ERTL.ma} & The syntax of ERTL & \texttt{ERTL/ERTL.mli} & 1.04\tnote{b} \\
[1414]594\texttt{LIN/joint\_LTL\_LIN.ma} & The syntax of the abstracted combined LTL and LIN language & N/A & N/A \\
[1418]595\texttt{LTL/LTL.ma} & The specialisation of the above file to the syntax of LTL & \texttt{LTL/LTL.mli} & 1.86\tnote{a} \\
596\texttt{LIN/LIN.ma} & The specialisation of the above file to the syntax of LIN & \texttt{LIN/LIN.mli} & 2.27\tnote{a}
[1414]597\end{tabular}
598\begin{tablenotes}
[1418]599  \item[a] Includes \texttt{joint/Joint\_LTL\_LIN.ma} and \texttt{joint/Joint.ma}.
[1427]600  \item[b] Includes \texttt{joint/Joint.ma}. \\
601  Total lines of Matita code for the above files: 347. \\
602  Total lines of O'Caml code for the above files: 616. \\
603  Ration of total lines: 0.56.
[1414]604\end{tablenotes}
605\end{threeparttable}
606\caption{Syntax specific files in the intermediate language semantics}
607\label{table.syntax}
608\end{table}
609\end{landscape}
610\begin{landscape}
611\begin{table}
612\begin{threeparttable}
613\begin{tabular}{llll}
614Title & Description & O'Caml & Ratio \\
[1409]615\hline
[1418]616\texttt{joint/semantics.ma} & Semantics of the abstracted languages & N/A & N/A  \\
617\texttt{joint/SemanticUtils.ma} & Generic utilities used in semantics joint' languages & N/A & N/A \\
[1427]618\texttt{RTLabs/semantics.ma} & Semantics of RTLabs & \texttt{RTLabs/RTLabsInterpret.ml} & 0.63 \\
[1418]619\texttt{RTL/semantics.ma} & Semantics of RTL & \texttt{RTL/RTLInterpret.ml} & 1.88\tnote{a} \\
620\texttt{ERTL/semantics.ma} & Semantics of ERTL & \texttt{ERTL/ERTLInterpret.ml} & 1.22\tnote{a} \\
[1446]621\texttt{LIN/joint\_LTL\_LIN\_semantics.ma} & Semantics of the joint LTL-LIN language & N/A & N/A \\
[1427]622\texttt{LTL/semantics.ma} & Semantics of LTL & \texttt{LTL/LTLInterpret.ml} & 1.25\tnote{c} \\
623\texttt{LIN/semantics.ma} & Semantics of LIN & \texttt{LIN/LINInterpret.ml} & 1.52\tnote{c}
[1414]624\end{tabular}
[1418]625\begin{tablenotes}
626  \item{a} Includes \texttt{joint/semantics.ma} and \texttt{joint/SemanticUtils.ma}.
627  \item{b} Includes \texttt{joint/joint\_LTL\_LIN\_semantics.ma}.
[1427]628  \item{c} Includes \texttt{joint/semantics.ma}, \texttt{joint/SemanticUtils.ma} and \texttt{joint/joint\_LTL\_LIN\_semantics.ma}. \\
[1446]629 \begin{tabular}{ll}
630  Total lines of Matita code for the above files:& 1125 \\
631  Total lines of O'Caml code for the above files:& 1978 \\
632  Ration of total lines:& 0.57
633 \end{tabular}
[1418]634\end{tablenotes}
[1414]635\end{threeparttable}
636\caption{Semantics specific files in the intermediate language semantics}
637\label{table.semantics}
638\end{table}
639\end{landscape}
[1365]640
641%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
642% SECTION.                                                                    %
643%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%-%
644\subsection{Listing of important functions and axioms}
645\label{subsect.listing.important.functions.axioms}
646
[1373]647We list some important functions and axioms in the backend semantics:
648
[1428]649\paragraph{From RTLabs/semantics.ma}
650\begin{center}
651\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
652Title & Description \\
653\hline
654\texttt{make\_initial\_state} & Build an initial state \\
655\texttt{eval\_statement} & Evaluate a single RTLabs statement \\
656\texttt{is\_final} & Check whether a state is in a final' configuration \\
657\texttt{RTLabs\_exec} & Execute an RTLabs program
658\end{tabular*}
659\end{center}
660
661\paragraph{From RTL/semantics.ma}
662\begin{center}
663\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
664Title & Description \\
665\hline
666\texttt{rtl\_exec\_extended} & Execute a single step of the RTL language's extended instructions \\
667\texttt{rtl\_fullexec} & Execute an RTL program
668\end{tabular*}
669\end{center}
670
671\paragraph{From ERTL/semantics.ma}
672\begin{center}
673\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
674Title & Description \\
675\hline
676\texttt{ertl\_exec\_extended} & Execute a single step of the ERTL language's extended instructions \\
677\texttt{ertl\_fullexec} & Execute an ERTL program
678\end{tabular*}
679\end{center}
680
681\paragraph{From LTL/semantics.ma}
682\begin{center}
683\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
684Title & Description \\
685\hline
686\texttt{ltl\_fullexec} & Execute an LTL program
687\end{tabular*}
688\end{center}
689
690\paragraph{From LIN/semantics.ma}
691\begin{center}
692\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
693Title & Description \\
694\hline
695\texttt{lin\_fullexec} & Execute a LIN program
696\end{tabular*}
697\end{center}
698
699\paragraph{From LIN/joint\_LTL\_LIN\_semantics.ma}
700\begin{center}
701\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
702Title & Description \\
703\hline
704\texttt{ltl\_lin\_exec\_extended} & Execute a single step of the joint LTL-LIN language's extended instructions \\
705\texttt{ltl\_lin\_fullexec} & Execute a joint LTL-LIN language program
706\end{tabular*}
707\end{center}
708
709\paragraph{From joint/semantics.ma}
710\begin{center}
711\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
712Title & Description \\
713\hline
714\texttt{eval\_statement} & Evaluate a single joint language statement \\
715\texttt{is\_final} & Check whether a state is in a `final' configuration \\
716\texttt{joint\_fullexec} & Execute a joint language program
717\end{tabular*}
718\end{center}
719
720\paragraph{From joint/SemanticUtils.ma}
721\begin{center}
722\begin{tabular*}{0.9\textwidth}{p{5cm}p{8cm}}
723Title & Description \\
724\hline
725\texttt{graph\_fetch\_statement} & Fetch a statement from a control flow graph
726\end{tabular*}
727\end{center}
728
[1356]729\end{document}
Note: See TracBrowser for help on using the repository browser.