[1778] | 1 | \documentclass[11pt,a4paper]{article} |
---|
[1794] | 2 | |
---|
[1716] | 3 | \usepackage{a4wide} |
---|
| 4 | \usepackage{amsfonts} |
---|
| 5 | \usepackage{amsmath} |
---|
| 6 | \usepackage{amssymb} |
---|
[1781] | 7 | \usepackage{array} |
---|
[1716] | 8 | \usepackage[english]{babel} |
---|
[1781] | 9 | \usepackage{../../style/cerco} |
---|
[1723] | 10 | \usepackage{color} |
---|
[1717] | 11 | \usepackage{diagrams} |
---|
[1723] | 12 | \usepackage{graphicx} |
---|
[1781] | 13 | \usepackage[utf8x]{inputenc} |
---|
[1723] | 14 | \usepackage{listings} |
---|
[1716] | 15 | \usepackage{microtype} |
---|
[1794] | 16 | \usepackage{pdfpages} |
---|
[1718] | 17 | \usepackage{skull} |
---|
| 18 | \usepackage{stmaryrd} |
---|
[1781] | 19 | \usepackage{wasysym} |
---|
| 20 | |
---|
[1785] | 21 | \newcolumntype{b}{\at{}>{{}}} |
---|
| 22 | \newcolumntype{B}{\at{}>{{}}c<{{}}\at{}} |
---|
| 23 | \newcolumntype{h}[1]{\at{\hspace{#1}}} |
---|
[1750] | 24 | \newcolumntype{L}{>{$}l<{$}} |
---|
| 25 | \newcolumntype{C}{>{$}c<{$}} |
---|
| 26 | \newcolumntype{R}{>{$}r<{$}} |
---|
| 27 | \newcolumntype{S}{>{$(}r<{)$}} |
---|
[1785] | 28 | \newcolumntype{n}{\at{}} |
---|
[1716] | 29 | |
---|
[1781] | 30 | \lstdefinelanguage{matita-ocaml} |
---|
| 31 | {keywords={definition,coercion,lemma,theorem,remark,inductive,record,qed,let,in,rec,match,return,with,Type,try,on,to}, |
---|
| 32 | morekeywords={[2]whd,normalize,elim,cases,destruct}, |
---|
| 33 | morekeywords={[3]type,of,val,assert,let,function}, |
---|
| 34 | mathescape=true, |
---|
| 35 | } |
---|
| 36 | \lstset{language=matita-ocaml,basicstyle=\tt,columns=flexible,breaklines=false, |
---|
| 37 | keywordstyle=\bfseries, %\color{red}\bfseries, |
---|
| 38 | keywordstyle=[2]\bfseries, %\color{blue}, |
---|
| 39 | keywordstyle=[3]\bfseries, %\color{blue}\bfseries, |
---|
| 40 | % commentstyle=\color{green}, |
---|
| 41 | % stringstyle=\color{blue}, |
---|
| 42 | showspaces=false,showstringspaces=false} |
---|
| 43 | \DeclareUnicodeCharacter{8797}{:=} |
---|
| 44 | \DeclareUnicodeCharacter{10746}{++} |
---|
| 45 | \DeclareUnicodeCharacter{9001}{\ensuremath{\langle}} |
---|
| 46 | \DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}} |
---|
| 47 | |
---|
| 48 | \lstset{ |
---|
| 49 | extendedchars=false, |
---|
| 50 | inputencoding=utf8x, |
---|
| 51 | tabsize=1 |
---|
| 52 | } |
---|
| 53 | |
---|
[1778] | 54 | \hypersetup{bookmarksopenlevel=2} |
---|
[1750] | 55 | |
---|
[1778] | 56 | \title{ |
---|
| 57 | INFORMATION AND COMMUNICATION TECHNOLOGIES\\ |
---|
| 58 | (ICT)\\ |
---|
| 59 | PROGRAMME\\ |
---|
| 60 | \vspace*{1cm}Project FP7-ICT-2009-C-243881 {\cerco}} |
---|
[1723] | 61 | |
---|
[1778] | 62 | \date{ } |
---|
| 63 | \author{} |
---|
[1716] | 64 | |
---|
| 65 | \begin{document} |
---|
[1778] | 66 | \thispagestyle{empty} |
---|
[1716] | 67 | |
---|
[1778] | 68 | \vspace*{-1cm} |
---|
| 69 | \begin{center} |
---|
| 70 | \includegraphics[width=0.6\textwidth]{../../style/cerco_logo.png} |
---|
| 71 | \end{center} |
---|
| 72 | |
---|
| 73 | \begin{minipage}{\textwidth} |
---|
[1716] | 74 | \maketitle |
---|
[1778] | 75 | \end{minipage} |
---|
[1716] | 76 | |
---|
[1778] | 77 | |
---|
| 78 | \vspace*{0.5cm} |
---|
| 79 | \begin{center} |
---|
| 80 | \begin{LARGE} |
---|
| 81 | \bf |
---|
[1790] | 82 | Proof outline for the correctness of\\the CerCo compiler |
---|
[1778] | 83 | \end{LARGE} |
---|
| 84 | \end{center} |
---|
| 85 | |
---|
| 86 | \vspace*{2cm} |
---|
| 87 | \begin{center} |
---|
| 88 | \begin{large} |
---|
| 89 | Version 1.0 |
---|
| 90 | \end{large} |
---|
| 91 | \end{center} |
---|
| 92 | |
---|
| 93 | \vspace*{0.5cm} |
---|
| 94 | \begin{center} |
---|
| 95 | \begin{large} |
---|
| 96 | Main Authors:\\ |
---|
[1790] | 97 | J. Boender, B. Campbell, D. Mulligan, P. Tranquilli, C. Sacerdoti Coen |
---|
[1778] | 98 | \end{large} |
---|
| 99 | \end{center} |
---|
| 100 | |
---|
| 101 | \vspace*{\fill} |
---|
| 102 | \noindent |
---|
| 103 | Project Acronym: {\cerco}\\ |
---|
| 104 | Project full title: Certified Complexity\\ |
---|
| 105 | Proposal/Contract no.: FP7-ICT-2009-C-243881 {\cerco}\\ |
---|
| 106 | |
---|
| 107 | \clearpage \pagestyle{myheadings} \markright{{\cerco}, FP7-ICT-2009-C-243881} |
---|
| 108 | |
---|
| 109 | \tableofcontents |
---|
| 110 | |
---|
[1716] | 111 | \section{Introduction} |
---|
| 112 | \label{sect.introduction} |
---|
| 113 | |
---|
[1790] | 114 | In the last project review of the CerCo project, the project reviewers recommended that we briefly outline a pencil-and-paper correctness proof for each of the stages of the CerCo compiler in order to facilitate an estimation of the complexity and time required to complete the formalisation of the proof. |
---|
| 115 | This has been possible starting from month eighteen, as we have now completed the formalisation in Matita of the data structures and code of the compiler. |
---|
[1728] | 116 | |
---|
[1790] | 117 | In this document we provide a very high-level, pencil-and-paper sketch of what we view as the best path to completing the correctness proof for the compiler. |
---|
| 118 | In particular, for every translation between two intermediate languages, in both the front- and back-ends, we identify the key translation steps, and identify some invariants that we view as being important for the correctness proof. |
---|
| 119 | We sketch the overall correctness results, and also briefly describe the parts of the proof that have already been completed at the end of the First Period. |
---|
[1749] | 120 | |
---|
[1790] | 121 | Finally, in the last section we present an estimation of the effort required for the certification in Matita of the compiler and draw conclusions. |
---|
[1749] | 122 | |
---|
[1731] | 123 | \section{Front-end: Clight to RTLabs} |
---|
| 124 | |
---|
| 125 | The front-end of the CerCo compiler consists of several stages: |
---|
| 126 | |
---|
| 127 | \begin{center} |
---|
| 128 | \begin{minipage}{.8\linewidth} |
---|
| 129 | \begin{tabbing} |
---|
| 130 | \quad \= $\downarrow$ \quad \= \kill |
---|
| 131 | \textsf{Clight}\\ |
---|
| 132 | \> $\downarrow$ \> cast removal\\ |
---|
| 133 | \> $\downarrow$ \> add runtime functions\footnote{Following the last project |
---|
| 134 | meeting we intend to move this transformation to the back-end}\\ |
---|
| 135 | \> $\downarrow$ \> cost labelling\\ |
---|
[1790] | 136 | \> $\downarrow$ \> loop optimisations\footnote{\label{lab:opt2}To be ported from the untrusted compiler and certified only in case of early completion of the certification of the other passes.} (an endo-transformation)\\ |
---|
[1741] | 137 | \> $\downarrow$ \> partial redundancy elimination$^{\mbox{\scriptsize \ref{lab:opt2}}}$ (an endo-transformation)\\ |
---|
[1731] | 138 | \> $\downarrow$ \> stack variable allocation and control structure |
---|
| 139 | simplification\\ |
---|
| 140 | \textsf{Cminor}\\ |
---|
| 141 | \> $\downarrow$ \> generate global variable initialisation code\\ |
---|
| 142 | \> $\downarrow$ \> transform to RTL graph\\ |
---|
| 143 | \textsf{RTLabs}\\ |
---|
[1741] | 144 | \> $\downarrow$ \> \\ |
---|
| 145 | \>\,\vdots |
---|
[1731] | 146 | \end{tabbing} |
---|
| 147 | \end{minipage} |
---|
| 148 | \end{center} |
---|
| 149 | |
---|
[1740] | 150 | Here, by `endo-transformation', we mean a mapping from language back to itself: |
---|
[1790] | 151 | the loop optimisation step maps the Clight language to itself. |
---|
[1740] | 152 | |
---|
[1731] | 153 | %Our overall statements of correctness with respect to costs will |
---|
| 154 | %require a correctly labelled program |
---|
| 155 | There are three layers in most of the proofs proposed: |
---|
| 156 | \begin{enumerate} |
---|
| 157 | \item invariants closely tied to the syntax and transformations using |
---|
[1758] | 158 | dependent types (such as the presence of variable names in environments), |
---|
| 159 | \item a forward simulation proof relating each small-step of the |
---|
| 160 | source to zero or more steps of the target, and |
---|
| 161 | \item proofs about syntactic properties of the cost labelling. |
---|
[1731] | 162 | \end{enumerate} |
---|
[1758] | 163 | The first will support both functional correctness and allow us to |
---|
| 164 | show the totality of some of the compiler stages (that is, those |
---|
| 165 | stages of the compiler cannot fail). The second provides the main |
---|
| 166 | functional correctness result, including the preservation of cost |
---|
| 167 | labels in the traces, and the last will be crucial for applying |
---|
| 168 | correctness results about the costings from the back-end by showing |
---|
| 169 | that they appear in enough places so that we can assign all of the |
---|
| 170 | execution costs to them. |
---|
[1731] | 171 | |
---|
| 172 | We will also prove that a suitably labelled RTLabs trace can be turned |
---|
| 173 | into a \emph{structured trace} which splits the execution trace into |
---|
[1746] | 174 | cost-label to cost-label chunks with nested function calls. This |
---|
[1731] | 175 | structure was identified during work on the correctness of the |
---|
| 176 | back-end cost analysis as retaining important information about the |
---|
| 177 | structure of the execution that is difficult to reconstruct later in |
---|
| 178 | the compiler. |
---|
| 179 | |
---|
[1746] | 180 | \subsection{Clight cast removal} |
---|
[1731] | 181 | |
---|
[1746] | 182 | This transformation removes some casts inserted by the parser to make |
---|
| 183 | arithmetic promotion explicit but which are superfluous (such as |
---|
| 184 | \lstinline[language=C]'c = (short)((int)a + (int)b);' where |
---|
| 185 | \lstinline'a' and \lstinline'b' are \lstinline[language=C]'short'). |
---|
| 186 | This is necessary for producing good code for our target architecture. |
---|
[1731] | 187 | |
---|
[1746] | 188 | It only affects Clight expressions, recursively detecting casts that |
---|
| 189 | can be safely eliminated. The semantics provides a big-step |
---|
| 190 | definition for expression, so we should be able to show a lock-step |
---|
| 191 | forward simulation between otherwise identical states using a lemma |
---|
| 192 | showing that cast elimination does not change the evaluation of |
---|
| 193 | expressions. This lemma will follow from a structural induction on |
---|
| 194 | the source expression. We have already proved a few of the underlying |
---|
| 195 | arithmetic results necessary to validate the approach. |
---|
[1731] | 196 | |
---|
| 197 | \subsection{Clight cost labelling} |
---|
| 198 | |
---|
| 199 | This adds cost labels before and after selected statements and |
---|
| 200 | expressions, and the execution traces ought to be equivalent modulo |
---|
[1746] | 201 | the new cost labels. Hence it requires a simple forward simulation |
---|
| 202 | with a limited amount of stuttering whereever a new cost label is |
---|
| 203 | introduced. A bound can be given for the amount of stuttering allowed |
---|
[1731] | 204 | based on the statement or continuation to be evaluated next. |
---|
| 205 | |
---|
| 206 | We also intend to show three syntactic properties about the cost |
---|
| 207 | labelling: |
---|
[1746] | 208 | \begin{enumerate} |
---|
[1731] | 209 | \item every function starts with a cost label, |
---|
[1746] | 210 | \item every branching instruction is followed by a cost label (note that |
---|
[1731] | 211 | exiting a loop is treated as a branch), and |
---|
| 212 | \item the head of every loop (and any \lstinline'goto' destination) is |
---|
| 213 | a cost label. |
---|
[1746] | 214 | \end{enumerate} |
---|
[1731] | 215 | These can be shown by structural induction on the source term. |
---|
| 216 | |
---|
| 217 | \subsection{Clight to Cminor translation} |
---|
| 218 | |
---|
[1732] | 219 | This translation is the first to introduce some invariants, with the |
---|
| 220 | proofs closely tied to the implementation by dependent typing. These |
---|
| 221 | are largely complete and show that the generated code enjoys: |
---|
| 222 | \begin{itemize} |
---|
| 223 | \item some minimal type safety shown by explicit checks on the |
---|
| 224 | Cminor types during the transformation (a little more work remains |
---|
| 225 | to be done here, but follows the same form); |
---|
| 226 | \item that variables named in the parameter and local variable |
---|
| 227 | environments are distinct from one another, again by an explicit |
---|
| 228 | check; |
---|
| 229 | \item that variables used in the generated code are present in the |
---|
| 230 | resulting environment (either by checking their presence in the |
---|
[1746] | 231 | source environment, or from a list of freshly generated temporary variables); |
---|
[1732] | 232 | and |
---|
| 233 | \item that all \lstinline[language=C]'goto' labels are present (by |
---|
| 234 | checking them against a list of source labels and proving that all |
---|
| 235 | source labels are preserved). |
---|
| 236 | \end{itemize} |
---|
| 237 | |
---|
| 238 | The simulation will be similar to the relevant stages of CompCert |
---|
| 239 | (Clight to Csharpminor and Csharpminor to Cminor --- in the event that |
---|
[1746] | 240 | the direct proof is unwieldy we could introduce an intermediate |
---|
| 241 | language corresponding to Csharpminor). During early experimentation |
---|
| 242 | with porting CompCert definitions to the Matita proof assistant we |
---|
| 243 | found little difficulty reproving the results for the memory model, so |
---|
| 244 | we plan to port the memory injection properties and use them to relate |
---|
| 245 | Clight in-memory variables with either the value of the local variable or a |
---|
| 246 | stack slot, depending on how it was classified. |
---|
[1732] | 247 | |
---|
| 248 | This should be sufficient to show the equivalence of (big-step) |
---|
| 249 | expression evaluation. The simulation can then be shown by relating |
---|
| 250 | corresponding blocks of statement and continuations with their Cminor |
---|
| 251 | counterparts and proving that a few steps reaches the next matching |
---|
| 252 | state. |
---|
| 253 | |
---|
| 254 | The syntactic properties required for cost labels remain similar and a |
---|
| 255 | structural induction on the function bodies should be sufficient to |
---|
| 256 | show that they are preserved. |
---|
| 257 | |
---|
[1731] | 258 | \subsection{Cminor global initialisation code} |
---|
| 259 | |
---|
[1732] | 260 | This short phase replaces the global variable initialisation data with |
---|
| 261 | code that executes when the program starts. Each piece of |
---|
| 262 | initialisation data in the source is matched by a new statement |
---|
| 263 | storing that data. As each global variable is allocated a distinct |
---|
| 264 | memory block, the program state after the initialisation statements |
---|
| 265 | will be the same as the original program's state at the start of |
---|
| 266 | execution, and will proceed in the same manner afterwards. |
---|
| 267 | |
---|
| 268 | % Actually, the above is wrong... |
---|
| 269 | % ... this ought to be in a fresh main function with a fresh cost label |
---|
| 270 | |
---|
[1731] | 271 | \subsection{Cminor to RTLabs translation} |
---|
| 272 | |
---|
[1733] | 273 | In this part of the compiler we transform the program's functions into |
---|
| 274 | control flow graphs. It is closely related to CompCert's Cminorsel to |
---|
| 275 | RTL transformation, albeit with target-independent operations. |
---|
| 276 | |
---|
| 277 | We already enforce several invariants with dependent types: some type |
---|
[1746] | 278 | safety, mostly shown using the type information from Cminor; and |
---|
[1733] | 279 | that the graph is closed (by showing that each successor was recently |
---|
| 280 | added, or corresponds to a \lstinline[language=C]'goto' label which |
---|
| 281 | are all added before the end). Note that this relies on a |
---|
| 282 | monotonicity property; CompCert maintains a similar property in a |
---|
| 283 | similar way while building RTL graphs. We will also add a result |
---|
| 284 | showing that all of the pseudo-register names are distinct for use by |
---|
| 285 | later stages using the same method as Cminor. |
---|
| 286 | |
---|
[1746] | 287 | The simulation will relate Cminor states to RTLabs states which are about to |
---|
| 288 | execute the code corresponding to the Cminor statement or continuation. |
---|
[1733] | 289 | Each Cminor statement becomes zero or more RTLabs statements, with a |
---|
| 290 | decreasing measure based on the statement and continuations similar to |
---|
| 291 | CompCert's. We may also follow CompCert in using a relational |
---|
| 292 | specification of this stage so as to abstract away from the functional |
---|
| 293 | (and highly dependently typed) definition. |
---|
| 294 | |
---|
| 295 | The first two labelling properties remain as before; we will show that |
---|
| 296 | cost labels are preserved, so the function entry point will be a cost |
---|
[1746] | 297 | label, and successors to any statement that are cost labels map still |
---|
| 298 | map to cost labels, preserving the condition on branches. We replace |
---|
| 299 | the property for loops with the notion that we will always reach a |
---|
| 300 | cost label or the end of the function after following a bounded number of |
---|
| 301 | successors. This can be easily seen in Cminor using the requirement |
---|
| 302 | for cost labels at the head of loops and after gotos. It remains to |
---|
| 303 | show that this is preserved by the translation to RTLabs. % how? |
---|
[1733] | 304 | |
---|
[1731] | 305 | \subsection{RTLabs structured trace generation} |
---|
| 306 | |
---|
[1733] | 307 | This proof-only step incorporates the function call structure and cost |
---|
| 308 | labelling properties into the execution trace. As the function calls |
---|
| 309 | are nested within the trace, we need to distinguish between |
---|
[1746] | 310 | terminating and non-terminating function calls. Thus we use the |
---|
| 311 | excluded middle (specialised to a function termination property) to do |
---|
| 312 | this. |
---|
[1733] | 313 | |
---|
[1746] | 314 | Structured traces for terminating functions are built by following the |
---|
| 315 | flat trace, breaking it into chunks between cost labels and |
---|
| 316 | recursively processing function calls. The main difficulties here are |
---|
| 317 | the non-structurally recursive nature of the function (instead we use |
---|
| 318 | the size of the termination proof as a measure) and using the RTLabs |
---|
| 319 | cost labelling properties to show that the constraints of the |
---|
| 320 | structured traces are observed. We also show that the lower stack |
---|
| 321 | frames are preserved during function calls in order to prove that |
---|
| 322 | after returning from a function call we resume execution of the |
---|
| 323 | correct code. This part of the work has already been constructed, but |
---|
| 324 | still requires a simple proof to show that flattening the structured |
---|
[1733] | 325 | trace recreates the original flat trace. |
---|
| 326 | |
---|
[1746] | 327 | The non-terminating case follows the trace like the terminating |
---|
| 328 | version to build up chunks of trace from cost-label to cost-label |
---|
| 329 | (which, by the finite distance to a cost label property shown before, |
---|
| 330 | can be represented by an inductive type). These chunks are chained |
---|
| 331 | together in a coinductive data structure that can represent |
---|
| 332 | non-terminating traces. The excluded middle is used to decide whether |
---|
| 333 | function calls terminate, in which case the function described above |
---|
| 334 | constructs an inductive terminating structured trace which is nested |
---|
| 335 | in the caller's trace. Otherwise, another coinductive constructor is |
---|
| 336 | used to embed the non-terminating trace of the callee, generated by |
---|
| 337 | corecursion. This part of the trace transformation is currently under |
---|
| 338 | construction, and will also need a flattening result to show that it |
---|
| 339 | is correct. |
---|
[1733] | 340 | |
---|
[1746] | 341 | |
---|
[1734] | 342 | \section{Backend: RTLabs to machine code} |
---|
| 343 | \label{sect.backend.rtlabs.machine.code} |
---|
[1716] | 344 | |
---|
[1734] | 345 | The compiler backend consists of the following intermediate languages, and stages of translation: |
---|
| 346 | |
---|
| 347 | \begin{center} |
---|
| 348 | \begin{minipage}{.8\linewidth} |
---|
| 349 | \begin{tabbing} |
---|
[1741] | 350 | \quad \=\,\vdots\= \\ |
---|
| 351 | \> $\downarrow$ \>\\ |
---|
| 352 | \> $\downarrow$ \quad \= \kill |
---|
[1734] | 353 | \textsf{RTLabs}\\ |
---|
[1790] | 354 | \> $\downarrow$ \> copy propagation\footnote{\label{lab:opt}To be ported from the untrusted compiler and certified only in the case of an early completion of the certification of the other passes.} (an endo-transformation) \\ |
---|
[1734] | 355 | \> $\downarrow$ \> instruction selection\\ |
---|
[1748] | 356 | \> $\downarrow$ \> change of memory models in compiler\\ |
---|
[1734] | 357 | \textsf{RTL}\\ |
---|
[1739] | 358 | \> $\downarrow$ \> constant propagation$^{\mbox{\scriptsize \ref{lab:opt}}}$ (an endo-transformation) \\ |
---|
[1734] | 359 | \> $\downarrow$ \> calling convention made explicit \\ |
---|
[1741] | 360 | \> $\downarrow$ \> layout of activation records \\ |
---|
[1734] | 361 | \textsf{ERTL}\\ |
---|
| 362 | \> $\downarrow$ \> register allocation and spilling\\ |
---|
| 363 | \> $\downarrow$ \> dead code elimination\\ |
---|
| 364 | \textsf{LTL}\\ |
---|
| 365 | \> $\downarrow$ \> function linearisation\\ |
---|
| 366 | \> $\downarrow$ \> branch compression (an endo-transformation) \\ |
---|
| 367 | \textsf{LIN}\\ |
---|
| 368 | \> $\downarrow$ \> relabeling\\ |
---|
| 369 | \textsf{ASM}\\ |
---|
| 370 | \> $\downarrow$ \> pseudoinstruction expansion\\ |
---|
[1741] | 371 | \textsf{MCS-51 machine code}\\ |
---|
[1734] | 372 | \end{tabbing} |
---|
| 373 | \end{minipage} |
---|
| 374 | \end{center} |
---|
| 375 | |
---|
[1769] | 376 | \subsection{Graph translations} |
---|
[1790] | 377 | RTLabs and most intermediate languages in the back-end have a graph representation: the code of each function is represented by a graph of instructions. |
---|
| 378 | The graph maps a set of labels (the names of the nodes) to the instruction stored at that label (the nodes of the graph). |
---|
| 379 | Instructions reference zero or more additional labels that are the immediate successors of the instruction: zero for return from functions, more than one for conditional jumps and calls, one in all other cases. |
---|
| 380 | The references from one instruction to its immediate successors are the arcs of the graph. |
---|
[1769] | 381 | |
---|
[1790] | 382 | The statuses of graph languages always contain a program counter that holds a representation of a reference to the current instruction. |
---|
[1773] | 383 | |
---|
[1790] | 384 | A translation between two consecutive graph languages maps each instruction stored at location $l$ in the first graph and with immediate successors $\{l_1,\ldots,l_n\}$ to a subgraph of the output graph that has a single entry point at location $l$ and exit arcs to $\{l_1,\ldots,l_n\}$. |
---|
| 385 | Moreover, the labels of all non-entry nodes in the subgraph are distinct from all the labels in the source graph. |
---|
[1773] | 386 | |
---|
[1790] | 387 | In order to simplify the translations and the relative proofs of forward simulation, after the release of D4.2 and D4.3, we have provided: |
---|
[1773] | 388 | \begin{itemize} |
---|
[1790] | 389 | \item |
---|
| 390 | A new data type (called \texttt{blist}) that represents a sequence of instructions to be added to the output graph. |
---|
| 391 | The ``b'' in the name stands for binder, since a \texttt{blist} is either empty, an extension of a \texttt{blist} with an instruction at the front, or the generation of a fresh quantity followed by a \texttt{blist}. |
---|
| 392 | The latter feature is used, for instance, to generate fresh register names. |
---|
| 393 | The instructions in the list are unlabelled and all of them but the last one are also sequential, like in a linear program. |
---|
| 394 | \item |
---|
| 395 | A new iterator (called \texttt{b\_graph\_translate}) of type |
---|
[1773] | 396 | \begin{displaymath} |
---|
| 397 | \mathtt{b\_graph\_translate}: (\mathtt{label} \rightarrow \mathtt{blist}) |
---|
| 398 | \rightarrow \mathtt{graph} \rightarrow \mathtt{graph} |
---|
| 399 | \end{displaymath} |
---|
[1790] | 400 | The iterator transform the input graph in the output graph by replacing each node with the graph that corresponds to the linear \texttt{blist} obtained by applying the function in input to the node label. |
---|
[1773] | 401 | \end{itemize} |
---|
| 402 | |
---|
[1790] | 403 | Using the iterator above, the code can be written in such a way that the programmer does not see any distinction between writing a transformatio on linear or graph languages. |
---|
[1773] | 404 | |
---|
[1790] | 405 | In order to prove simulations for translations obtained using the iterator, we will prove the following theorem: |
---|
[1773] | 406 | |
---|
| 407 | \begin{align*} |
---|
| 408 | \mathtt{theorem} &\ \mathtt{b\_graph\_translate\_ok}: \\ |
---|
| 409 | & \forall f.\forall G_{i}.\mathtt{let}\ G_{\sigma} := \mathtt{b\_graph\_translate}\ f\ G_{i}\ \mathtt{in} \\ |
---|
| 410 | & \forall l \in G_{i}.\mathtt{subgraph}\ (f\ l)\ l\ (next \ l \ G_i)\ G_{\sigma} |
---|
| 411 | \end{align*} |
---|
| 412 | |
---|
[1790] | 413 | Here \texttt{subgraph} is a computational predicate that given a \texttt{blist} $[i_1, \ldots, i_n]$, an entry label $l$, an exit label $l'$ and a graph $G$ expands to the fact that fetching from $G$ at address $l$ one retrieves a node $i_1$ with a successor $l_1$ that, when fetched, yields a node $i_2$ with a successor $l_2$ such that \ldots. The successor of $i_n$ is $l'$. |
---|
[1773] | 414 | |
---|
[1790] | 415 | Proving a forward simulation diagram of the following kind using the aforementioned theorem is now as straightforward as doing the same using standard small-step operational semantics over linear languages. |
---|
[1773] | 416 | |
---|
| 417 | \begin{align*} |
---|
| 418 | \mathtt{lemma} &\ \mathtt{execute\_1\_step\_ok}: \\ |
---|
| 419 | & \forall s. \mathtt{let}\ s' := s\ \sigma\ \mathtt{in} \\ |
---|
| 420 | & \mathtt{let}\ l := pc\ s\ \mathtt{in} \\ |
---|
| 421 | & s \stackrel{1}{\rightarrow} s^{*} \Rightarrow \exists n. s' \stackrel{n}{\rightarrow} s'^{*} \wedge s'^{*} = s'\ \sigma |
---|
| 422 | \end{align*} |
---|
| 423 | |
---|
[1790] | 424 | Because graph translations preserve entry and exit labels of translated statements, the state translation function $\sigma$ will simply preserve the value of the program counter. |
---|
| 425 | The program code, which is part of the state, is translated using the iterator. |
---|
[1773] | 426 | |
---|
[1790] | 427 | The proof is then roughly as follows. |
---|
| 428 | Let $l$ be the program counter of the input state $s$. |
---|
| 429 | We proceed by cases on the current instruction of $s$. |
---|
| 430 | Let $[i_1, \ldots, i_n]$ be the \texttt{blist} associated to $l$ and $s$ by the translation function. |
---|
| 431 | The witness required for the existential statement is simply $n$. |
---|
| 432 | By applying the theorem above we know that the next $n$ instructions that will be fetched from $s\ \sigma$ will be $[i_1, \ldots, i_n]$ and it is now sufficient to prove that they simulate the original instruction. |
---|
[1773] | 433 | |
---|
[1734] | 434 | \subsection{The RTLabs to RTL translation} |
---|
| 435 | \label{subsect.rtlabs.rtl.translation} |
---|
| 436 | |
---|
[1786] | 437 | \subsubsection*{Translation of values and memory} |
---|
| 438 | |
---|
[1748] | 439 | The RTLabs to RTL translation pass marks the frontier between the two memory models used in the CerCo project. |
---|
| 440 | As a result, we require some method of translating between the values that the two memory models permit. |
---|
| 441 | Suppose we have such a translation, $\sigma$. |
---|
| 442 | Then the translation between values of the two memory models may be pictured with: |
---|
[1716] | 443 | |
---|
| 444 | \begin{displaymath} |
---|
[1752] | 445 | \mathtt{Value} ::= \bot \mid \mathtt{int(size)} \mid \mathtt{float} \mid \mathtt{null} \mid \mathtt{ptr} \quad\stackrel{\sigma}{\longrightarrow}\quad \mathtt{BEValue} ::= \bot \mid \mathtt{byte} \mid \mathtt{null}_i \mid \mathtt{ptr}_i |
---|
[1716] | 446 | \end{displaymath} |
---|
| 447 | |
---|
[1790] | 448 | In the front-end, we have both integer and float values, where integer values are `sized', along with null values and pointers. |
---|
| 449 | Some front-end values are representables in a byte, but some others require more bits. |
---|
[1748] | 450 | |
---|
[1752] | 451 | In the back-end model all values are meant to be represented in a single byte. |
---|
[1790] | 452 | Values can thefore be undefined, be one byte long integers or be indexed fragments of a pointer, null or not. |
---|
| 453 | Floats values are no longer present, as floating point arithmetic is not supported by the CerCo compiler. |
---|
[1716] | 454 | |
---|
[1790] | 455 | The $\sigma$ map implements a one-to-many relation: a single front-end value is mapped to a sequence of back-end values when its size is more then one byte. |
---|
[1752] | 456 | |
---|
[1790] | 457 | We further require a map, $\sigma$, which maps the front-end \texttt{Memory} and the back-end's notion of \texttt{BEMemory}. |
---|
| 458 | Both kinds of memory can be thought as an instance of a generic \texttt{Mem} data type parameterized over the kind of values stored in memory: |
---|
[1752] | 459 | |
---|
[1716] | 460 | \begin{displaymath} |
---|
| 461 | \mathtt{Mem}\ \alpha = \mathtt{Block} \rightarrow (\mathbb{Z} \rightarrow \alpha) |
---|
| 462 | \end{displaymath} |
---|
[1748] | 463 | Here, \texttt{Block} consists of a \texttt{Region} paired with an identifier. |
---|
[1716] | 464 | |
---|
| 465 | \begin{displaymath} |
---|
[1752] | 466 | \mathtt{Block} ::= \mathtt{Region} \times \mathtt{ID} |
---|
[1716] | 467 | \end{displaymath} |
---|
[1748] | 468 | We now have what we need for defining what is meant by the `memory' in the backend memory model. |
---|
| 469 | Namely, we instantiate the previously defined \texttt{Mem} type with the type of back-end memory values. |
---|
| 470 | |
---|
[1716] | 471 | \begin{displaymath} |
---|
[1768] | 472 | \mathtt{BEMem} = \mathtt{Mem}~\mathtt{BEValue} |
---|
[1716] | 473 | \end{displaymath} |
---|
[1748] | 474 | Memory addresses consist of a pair of back-end memory values: |
---|
| 475 | |
---|
[1716] | 476 | \begin{displaymath} |
---|
| 477 | \mathtt{Address} = \mathtt{BEValue} \times \mathtt{BEValue} \\ |
---|
| 478 | \end{displaymath} |
---|
[1751] | 479 | The back- and front-end memory models differ in how they represent sized integeer values in memory. |
---|
| 480 | In particular, the front-end stores integer values as a header, with size information, followed by a string of `continuation' blocks, marking out the full representation of the value in memory. |
---|
| 481 | In contrast, the layout of sized integer values in the back-end memory model consists of a series of byte-sized `chunks': |
---|
[1752] | 482 | |
---|
[1716] | 483 | \begin{center} |
---|
[1752] | 484 | \begin{picture}(0, 25) |
---|
| 485 | \put(-125,0){\framebox(25,25)[c]{\texttt{v,4}}} |
---|
| 486 | \put(-100,0){\framebox(25,25)[c]{\texttt{cont}}} |
---|
| 487 | \put(-75,0){\framebox(25,25)[c]{\texttt{cont}}} |
---|
| 488 | \put(-50,0){\framebox(25,25)[c]{\texttt{cont}}} |
---|
| 489 | \put(-15,10){\vector(1, 0){30}} |
---|
| 490 | \put(25,0){\framebox(25,25)[c]{\texttt{\texttt{v$_1$}}}} |
---|
| 491 | \put(50,0){\framebox(25,25)[c]{\texttt{\texttt{v$_2$}}}} |
---|
| 492 | \put(75,0){\framebox(25,25)[c]{\texttt{\texttt{v$_3$}}}} |
---|
| 493 | \put(100,0){\framebox(25,25)[c]{\texttt{\texttt{v$_4$}}}} |
---|
[1716] | 494 | \end{picture} |
---|
| 495 | \end{center} |
---|
[1752] | 496 | |
---|
[1760] | 497 | Chunks for pointers are pairs made of the original pointer and the index of the chunk. |
---|
| 498 | Therefore, when assembling the chunks together, we can always recognize if all chunks refer to the same value or if the operation is meaningless. |
---|
[1752] | 499 | |
---|
[1751] | 500 | The differing memory representations of values in the two memory models imply the need for a series of lemmas on the actions of \texttt{load} and \texttt{store} to ensure correctness. |
---|
| 501 | The first lemma required has the following statement: |
---|
[1716] | 502 | \begin{displaymath} |
---|
[1791] | 503 | \mathtt{load\ s\ a\ M} = \mathtt{Some\ v} \rightarrow \forall i \leq s.\ \mathtt{load\ s\ (a + i)\ \sigma(M)} = \mathtt{Some\ v_i} |
---|
[1716] | 504 | \end{displaymath} |
---|
[1768] | 505 | That is, if we are successful in reading a value of size $s$ from memory at address $a$ in front-end memory, then we should successfully be able to read all of its chunks from memory in the back-end memory at appropriate address (from address $a$ up to and including address $a + i$, where $i \leq s$). |
---|
[1716] | 506 | |
---|
[1760] | 507 | Next, we must show that \texttt{store} properly commutes with the $\sigma$-map between memory spaces: |
---|
[1716] | 508 | \begin{displaymath} |
---|
[1791] | 509 | \sigma(\mathtt{store\ a\ v\ M}) = \mathtt{store\ \sigma(v)\ \sigma(a)\ \sigma(M)} |
---|
[1716] | 510 | \end{displaymath} |
---|
[1760] | 511 | That is, if we store a value \texttt{v} in the front-end memory \texttt{M} at address \texttt{a} and transform the resulting memory with $\sigma$, then this is equivalent to storing a transformed value $\mathtt{\sigma(v)}$ at address $\mathtt{\sigma(a)}$ into the back-end memory $\mathtt{\sigma(M)}$. |
---|
[1716] | 512 | |
---|
[1768] | 513 | Finally, the commutation properties between \texttt{load} and \texttt{store} are weakened in the $\sigma$-image of the memory. |
---|
| 514 | Writing \texttt{load}$^*$ for the multiple consecutive iterations of \texttt{load} used to fetch all chunks of a value, we must prove that, when $a \neq a'$: |
---|
[1716] | 515 | \begin{displaymath} |
---|
[1791] | 516 | \mathtt{load^* \sigma(a)\ (\mathtt{store}\ \sigma(a')\ \sigma(v)\ \sigma(M))} = \mathtt{load^*\ \sigma(s)\ \sigma(a)\ \sigma(M)} |
---|
[1743] | 517 | \end{displaymath} |
---|
[1790] | 518 | That is, suppose we store a transformed value $\mathtt{\sigma(v)}$ into a back-end memory $\mathtt{\sigma(M)}$ at address $\mathtt{\sigma(a')}$, using \texttt{store}, and then load from the address $\sigma(a)$. Even if $a$ and $a'$ are distinct by hypothesis, there is \emph{a priori} no guarantee that the consecutive bytes for the value stored at $\sigma(a)$ are disjoint from those for the values stored at $\sigma(a')$. |
---|
| 519 | The fact that this holds is a non-trivial property of $\sigma$ that must be explicitly proved. |
---|
[1743] | 520 | |
---|
[1786] | 521 | \subsubsection*{Translation of RTLabs states} |
---|
[1790] | 522 | |
---|
[1763] | 523 | RTLabs states come in three flavours: |
---|
[1743] | 524 | \begin{displaymath} |
---|
[1716] | 525 | \begin{array}{rll} |
---|
[1792] | 526 | \mathtt{StateRTLabs} & ::= & (\mathtt{State} : \mathtt{Frame}^* \times \mathtt{Frame} \\ |
---|
[1716] | 527 | & \mid & \mathtt{Call} : \mathtt{Frame}^* \times \mathtt{Args} \times \mathtt{Return} \times \mathtt{Fun} \\ |
---|
| 528 | & \mid & \mathtt{Return} : \mathtt{Frame}^* \times \mathtt{Value} \times \mathtt{Return}) \times \mathtt{Mem} |
---|
| 529 | \end{array} |
---|
| 530 | \end{displaymath} |
---|
[1790] | 531 | \texttt{State} is the default state in which RTLabs programs are almost always in for the duration of their execution. |
---|
[1763] | 532 | The \texttt{Call} state is only entered when a call instruction is being executed, and then we immediately return to being in \texttt{State}. |
---|
| 533 | Similarly, \texttt{Return} is only entered when a return instruction is being executed, before returning immediately to \texttt{State}. |
---|
| 534 | All RTLabs states are accompanied by a memory, \texttt{Mem}, with \texttt{Call} and \texttt{Return} keeping track of arguments, return addresses and the results of functions. |
---|
| 535 | \texttt{State} keeps track of a list of stack frames. |
---|
[1716] | 536 | |
---|
[1763] | 537 | RTL states differ from their RTLabs counterparts, in including a program counter \texttt{PC}, stack-pointer \texttt{SP}, internal stack pointer \texttt{ISP}, a carry flag \texttt{CARRY} and a set of registers \texttt{REGS}: |
---|
[1790] | 538 | |
---|
[1716] | 539 | \begin{displaymath} |
---|
[1792] | 540 | \mathtt{StateRTL} ::= (\mathtt{Frame}^* \times \mathtt{PC} \times \mathtt{SP} \times \mathtt{ISP} \times \mathtt{CARRY} \times \mathtt{REGS}) \times \mathtt{Mem} |
---|
[1716] | 541 | \end{displaymath} |
---|
[1763] | 542 | The internal stack pointer \texttt{ISP}, and its relationship with the stack pointer \texttt{SP}, needs some comment. |
---|
| 543 | Due to the design of the MCS-51, and its minuscule stack, it was decided that the compiler would implement an emulated stack in external memory. |
---|
| 544 | As a result, we have two stack pointers in our state: \texttt{ISP}, which is the real, hardware stack, and \texttt{SP}, which is the stack pointer of the emulated stack in memory. |
---|
| 545 | The emulated stack is used for pushing and popping stack frames when calling or returning from function calls, however this is done using the hardware stack, indexed by \texttt{ISP} as an intermediary. |
---|
[1790] | 546 | Instructions like \texttt{LCALL} and \texttt{ACALL} are hardwired by the processor's design to push the return address on to the hardware stack. |
---|
| 547 | Therefore after a call has been made, and before a call returns, the compiler emits code to move the return address back and forth the two stacks. |
---|
| 548 | Parameters, return values and local variables are only present in the external stack. |
---|
[1763] | 549 | As a result, for most of the execution of the processor, the hardware stack is empty, or contains a single item ready to be moved into external memory. |
---|
[1716] | 550 | |
---|
[1768] | 551 | Once more, we require a relation $\sigma$ between RTLabs states and RTL states. |
---|
[1790] | 552 | Because $\sigma$ is one-to-many and, morally, a multivalued function, we use in the sequel the functional notation for $\sigma$, using $\star$ as a distinct marker in the range of $\sigma$ to mean that any value is accepted. |
---|
[1716] | 553 | \begin{displaymath} |
---|
[1792] | 554 | \mathtt{StateRTLabs} \stackrel{\sigma}{\longrightarrow} \mathtt{StateRTL}s |
---|
[1716] | 555 | \end{displaymath} |
---|
| 556 | |
---|
[1763] | 557 | Translating an RTLabs state to an RTL state proceeds by cases on the particular type of state we are trying to translate, either a \texttt{State}, \texttt{Call} or a \texttt{Return}. |
---|
| 558 | For \texttt{State} we perform a further case analysis of the top stack frame, which decomposes into a tuple holding the current program counter value, the current stack pointer and the value of the registers: |
---|
[1717] | 559 | \begin{displaymath} |
---|
[1791] | 560 | \sigma(\mathtt{State} (\mathtt{Frame}^* \times \mathtt{\langle PC, REGS, SP, \ldots \rangle})) \longrightarrow ((\sigma(\mathtt{Frame}^*), \sigma(\mathtt{PC}), \sigma(\mathtt{SP}), \star, \star, \sigma(\mathtt{REGS})), \sigma(\mathtt{Mem})) |
---|
[1717] | 561 | \end{displaymath} |
---|
[1790] | 562 | Translation then proceeds by translating the remaining stack frames, as well as the contents of the top stack frame. |
---|
| 563 | An arbitrary value for the internal stack pointer and the carry bit is admitted. |
---|
[1717] | 564 | |
---|
[1763] | 565 | Translating \texttt{Call} and \texttt{Return} states is more involved, as a commutation between a single step of execution and the translation process must hold: |
---|
[1717] | 566 | \begin{displaymath} |
---|
| 567 | \sigma(\mathtt{Return}(-)) \longrightarrow \sigma \circ \text{return one step} |
---|
| 568 | \end{displaymath} |
---|
| 569 | |
---|
| 570 | \begin{displaymath} |
---|
[1718] | 571 | \sigma(\mathtt{Call}(-)) \longrightarrow \sigma \circ \text{call one step} |
---|
[1717] | 572 | \end{displaymath} |
---|
| 573 | |
---|
[1763] | 574 | Here \emph{return one step} and \emph{call one step} refer to a pair of commuting diagrams relating the one-step execution of a call and return state and translation of both. |
---|
[1790] | 575 | We provide the one step commuting diagrams in Figure~\ref{fig.commuting.diagrams}. |
---|
| 576 | The fact that one execution step in the source language is not performed in the target language is not problematic for preservation of divergence because it is easy to show that every step from a \texttt{Call} or \texttt{Return} state is always preceeded or followed by one step that is always simulated. |
---|
[1718] | 577 | |
---|
[1763] | 578 | \begin{figure} |
---|
[1718] | 579 | \begin{displaymath} |
---|
[1717] | 580 | \begin{diagram} |
---|
[1793] | 581 | s & \rTo^1 & s' \\ |
---|
[1718] | 582 | & \rdTo & \dTo \\ |
---|
| 583 | & & \llbracket s'' \rrbracket |
---|
[1717] | 584 | \end{diagram} |
---|
[1718] | 585 | \end{displaymath} |
---|
[1717] | 586 | |
---|
[1793] | 587 | \caption{The one-step commuting diagrams for \texttt{Call} and \texttt{Return} state translations.} |
---|
[1763] | 588 | \label{fig.commuting.diagrams} |
---|
| 589 | \end{figure} |
---|
[1718] | 590 | |
---|
[1786] | 591 | \subsubsection*{The forward simulation proof} |
---|
[1790] | 592 | |
---|
| 593 | The forward simulation proofs for all steps that do not involve function calls are lengthy, but routine. |
---|
[1771] | 594 | They consist of simulating a front-end operation on front-end pseudo-registers and the front-end memory with sequences of back-end operations on the back-end pseudo-registers and back-end memory. |
---|
| 595 | The properties of $\sigma$ presented before that relate values and memories will need to be heavily exploited. |
---|
[1768] | 596 | |
---|
[1771] | 597 | The simulation of invocation of functions and returns from functions is less obvious. |
---|
| 598 | We sketch here what happens on the source code and on its translation. |
---|
[1769] | 599 | |
---|
[1786] | 600 | \subparagraph{Function call/return in RTLabs} |
---|
| 601 | |
---|
[1718] | 602 | \begin{displaymath} |
---|
[1743] | 603 | \begin{array}{rcl} |
---|
[1792] | 604 | \mathtt{CALL(id,\ args,\ dst,\ pc) \in State(Frame^*, Frame)} & \longrightarrow & \mathtt{PUSH(Frame[pc := after\_return])}, \\ |
---|
| 605 | & & \mathtt{Call(M(args), dst)} |
---|
[1743] | 606 | \end{array} |
---|
[1718] | 607 | \end{displaymath} |
---|
[1763] | 608 | Suppose we are given a \texttt{State} with a list of stack frames, with the top frame being \texttt{Frame}. |
---|
[1791] | 609 | Suppose also that the program counter in \texttt{Frame} points to a \texttt{CALL} instruction, complete with arguments and destination address. |
---|
[1771] | 610 | Then this is executed by entering into a \texttt{Call} state where the arguments are loaded from memory, and the address pointing to the instruction immediately following the \texttt{Call} instruction is filled in, with the current stack frame being pushed on top of the stack with the return address substituted for the program counter. |
---|
[1718] | 611 | |
---|
[1763] | 612 | Now, what happens next depends on whether we are executing an internal or an external function. |
---|
[1743] | 613 | In the case where the call is to an external function, we have: |
---|
[1718] | 614 | \begin{displaymath} |
---|
| 615 | \begin{array}{rcl} |
---|
[1792] | 616 | \mathtt{PUSH(Frame[pc := after\_return])}, & \stackrel{\mathtt{ret\_val = f(M(args))}}{\longrightarrow} & \mathtt{Return(ret\_val,\ dst,\ PUSH(...))} \\ |
---|
| 617 | \mathtt{Call(M(args), dst)} & & |
---|
[1743] | 618 | \end{array} |
---|
| 619 | \end{displaymath} |
---|
[1766] | 620 | That is, the call to the external function enters a return state after first computing the return value by executing the external function on the arguments. |
---|
[1767] | 621 | Then the return state restores the program counter by popping the stack, and execution proceeds in a new \texttt{State}: |
---|
[1743] | 622 | \begin{displaymath} |
---|
| 623 | \begin{array}{rcl} |
---|
[1767] | 624 | \mathtt{Return(ret\_val,\ dst,\ PUSH(...))} & \longrightarrow & \mathtt{pc = POP\_STACK(regs[dst := M(ret\_val)],\ pc)} \\ |
---|
| 625 | & & \mathtt{State(regs[dst := M(ret\_val),\ pc)} |
---|
[1743] | 626 | \end{array} |
---|
| 627 | \end{displaymath} |
---|
| 628 | |
---|
[1767] | 629 | Suppose we are executing an internal function, however: |
---|
[1743] | 630 | \begin{displaymath} |
---|
| 631 | \begin{array}{rcl} |
---|
[1786] | 632 | \mathtt{Call(M(args), dst)} & \longrightarrow & \mathtt{sp = alloc,\ regs = \emptyset[- := params]} \\ |
---|
[1791] | 633 | \mathtt{PUSH(Frame[pc := after\_return])} & & \mathtt{State(regs,\ sp,\ pc_\emptyset,\ dst)} |
---|
[1743] | 634 | \end{array} |
---|
| 635 | \end{displaymath} |
---|
[1786] | 636 | A new stack frame is allocated and its address is stored in the stack pointer. |
---|
| 637 | The register map is initialized first to the empty map, assigning an undefined value to all register, before the value of the parameters is inserted into the map into the argument registers, and a new \texttt{State} follows. |
---|
| 638 | |
---|
| 639 | |
---|
[1790] | 640 | Eventually, a \texttt{RET} instruction is faced, the return value is fetched from the registers map, the stack frame is deallocated and a \texttt{Return} state is entered: |
---|
[1743] | 641 | \begin{displaymath} |
---|
| 642 | \begin{array}{rcl} |
---|
[1790] | 643 | \mathtt{RET(id,\ args,\ dst,\ pc) \in State(Frame^*, Frame)} & \longrightarrow & \mathtt{free(sp)} \\ |
---|
[1786] | 644 | & & \mathtt{Return(M(ret\_val), dst, Frames)} |
---|
[1743] | 645 | \end{array} |
---|
| 646 | \end{displaymath} |
---|
[1786] | 647 | |
---|
[1790] | 648 | Then the \texttt{Return} state restores the program counter by popping the stack, and execution proceeds in a new \texttt{State}, like the case for external functions: |
---|
[1743] | 649 | \begin{displaymath} |
---|
| 650 | \begin{array}{rcl} |
---|
| 651 | \mathtt{free(sp)} & \longrightarrow & \mathtt{pc = POP\_STACK(regs[dst := M(ret\_val)],\ pc)} \\ |
---|
[1791] | 652 | \mathtt{Return(M(ret\_val), dst, Frame^*)} & & \mathtt{State(regs[dst := M(ret\_val),\ pc)} |
---|
[1743] | 653 | \end{array} |
---|
| 654 | \end{displaymath} |
---|
| 655 | |
---|
[1786] | 656 | \subparagraph{The RTLabs to RTL translation for function calls} |
---|
[1790] | 657 | |
---|
[1786] | 658 | Return instructions are translated to return instructions: |
---|
[1718] | 659 | \begin{displaymath} |
---|
[1790] | 660 | \mathtt{RET} \longrightarrow \mathtt{RET} |
---|
[1777] | 661 | \end{displaymath} |
---|
| 662 | |
---|
[1790] | 663 | \texttt{CALL} instructions are translated to \texttt{CALL\_ID} instructions: |
---|
[1777] | 664 | \begin{displaymath} |
---|
[1790] | 665 | \mathtt{CALL(id,\ args,\ dst,\ pc)} \longrightarrow \mathtt{CALL\_ID(id,\ \Sigma'(args),\ \Sigma(dst),\ pc)} |
---|
[1777] | 666 | \end{displaymath} |
---|
[1790] | 667 | Here $\Sigma$ is the map, computed by the compiler, that translate pseudo-registers holding front-end values to list of pseudo-registers holding the chunks for the front-end values. |
---|
[1786] | 668 | The specification for $\Sigma$ is that for every state $s$, |
---|
[1790] | 669 | \begin{displaymath} |
---|
| 670 | \sigma(s(r)) = (\sigma(s))(\Sigma(r)) |
---|
| 671 | \end{displaymath} |
---|
[1777] | 672 | |
---|
[1786] | 673 | \subparagraph{Function call/return in RTL} |
---|
[1777] | 674 | |
---|
| 675 | In the case of RTL, execution proceeds as follows. |
---|
| 676 | Suppose we are executing a \texttt{CALL\_ID} instruction. |
---|
| 677 | Then a case split occurs depending on whether we are executing an internal or an external function, as in the RTLabs case: |
---|
| 678 | \begin{displaymath} |
---|
[1786] | 679 | \hspace{-3.5cm} |
---|
[1718] | 680 | \begin{diagram} |
---|
[1724] | 681 | & & \llbracket \mathtt{CALL\_ID}(\mathtt{id}, \mathtt{args}, \mathtt{dst}, \mathtt{pc})\rrbracket & & \\ |
---|
| 682 | & \ldTo^{\text{external}} & & \rdTo^{\text{internal}} & \\ |
---|
[1786] | 683 | \skull & & & & |
---|
| 684 | \begin{array}{l} |
---|
| 685 | \mathtt{sp = alloc,\ regs = \emptyset[- := params]} \\ |
---|
| 686 | \mathtt{PUSH}(\mathtt{carry}, \mathtt{regs}, \mathtt{dst}, \mathtt{return\_addr}), \mathtt{pc}_{0}, \mathtt{regs}, \mathtt{sp} |
---|
| 687 | \end{array} |
---|
[1718] | 688 | \end{diagram} |
---|
| 689 | \end{displaymath} |
---|
[1777] | 690 | Here, however, we differ from RTLabs when we attempt to execute an external function, in that we use a daemon (i.e. an axiom that can close any goal) to artificially close the case, as we have not yet implemented external functions in the backend. |
---|
| 691 | The reason for this lack of implementation is as follows. |
---|
| 692 | Though we have implemented an optimising assembler as the target of the compiler's backend, we have not yet implemented a linker for that assembler, so external functions can not yet be called. |
---|
| 693 | Whilst external functions are carried forth throughout the entirety of the compiler's frontend, we choose not to do the same for the backend, instead eliminating them in RTL. |
---|
| 694 | However, it is plausible that we could have carried external functions forth, in order to eliminate them at a later stage (i.e. when translating from LIN to assembly). |
---|
[1718] | 695 | |
---|
[1777] | 696 | In the case of an internal function being executed, we proceed as follows. |
---|
| 697 | The register map is initialized to the empty map, where all registers are assigned the undefined value, and then the registers corresponding to the function parameters are assigned the value of the parameters. |
---|
| 698 | Further, the stack pointer is reallocated to make room for an extra stack frame, then a frame is pushed onto the stack with the correct address to jump back to in place of the program counter. |
---|
| 699 | |
---|
| 700 | Note, in particular, that this final act of pushing a frame on the stack leaves us in an identical state to the RTLabs case, where the instruction |
---|
| 701 | \begin{displaymath} |
---|
[1792] | 702 | \mathtt{PUSH(Frame[pc := after\_return])} |
---|
[1777] | 703 | \end{displaymath} |
---|
[1790] | 704 | was executed. |
---|
| 705 | To summarize, up to the different numer of transitions required to do the job, the RTL code for internal function calls closely simulates the RTLabs code. |
---|
[1777] | 706 | |
---|
| 707 | The execution of \texttt{Return} in RTL is similarly straightforward, with the return address, stack pointer, and so on, being computed by popping off the top of the stack, and the return value computed by the function being retrieved from memory: |
---|
[1724] | 708 | \begin{align*} |
---|
| 709 | \mathtt{return\_addr} & := \mathtt{top}(\mathtt{stack}) \\ |
---|
[1786] | 710 | v^* & := M(\mathtt{rv\_regs}) \\ |
---|
[1724] | 711 | \mathtt{dst}, \mathtt{sp}, \mathtt{carry}, \mathtt{regs} & := \mathtt{pop} \\ |
---|
[1786] | 712 | \mathtt{regs}[v^* / \mathtt{dst}] \\ |
---|
[1724] | 713 | \end{align*} |
---|
| 714 | |
---|
[1786] | 715 | To summarize, the forward simulation diagrams for function call/return |
---|
[1793] | 716 | have the following form where the triangle is the one given in |
---|
| 717 | Figure~\ref{fig.commuting.diagrams}, the next instruction to be |
---|
| 718 | executed in state $s$ is either a function call or return and the intermediate |
---|
| 719 | state $s'$ is either a \texttt{Call} or a \texttt{Return} state. |
---|
[1724] | 720 | \begin{displaymath} |
---|
| 721 | \begin{diagram} |
---|
| 722 | s & \rTo^1 & s' & \rTo^1 & s'' \\ |
---|
| 723 | \dTo & & & \rdTo & \dTo \\ |
---|
| 724 | \llbracket s \rrbracket & \rTo(1,3)^1 & & & \llbracket s'' \rrbracket \\ |
---|
| 725 | \end{diagram} |
---|
| 726 | \end{displaymath} |
---|
[1793] | 727 | Two steps of execution are simulated by a single step. |
---|
[1724] | 728 | |
---|
[1780] | 729 | \subsection{The RTL to ERTL translation} |
---|
| 730 | \label{subsect.rtl.ertl.translation} |
---|
| 731 | |
---|
| 732 | We map RTL statuses to ERTL statuses as follows: |
---|
[1724] | 733 | \begin{align*} |
---|
[1780] | 734 | \mathtt{sp} & = \mathtt{RegisterSPH} / \mathtt{RegisterSPL} \\ |
---|
[1791] | 735 | \mathtt{graph} & = \mathtt{graph} + \mathtt{prologue}(s) + \mathtt{epilogue}(s) \\ |
---|
[1727] | 736 | & \mathrm{where}\ s = \mathrm{callee\ saved} + \nu \mathrm{RA} \\ |
---|
| 737 | \end{align*} |
---|
[1780] | 738 | The 16-bit RTL stack pointer \texttt{SP} is mapped to a pair of 8-bit hardware registers \texttt{RegisterSPH} and \texttt{RegisterSPL}. |
---|
| 739 | The internal function graphs of RTL are augmented with an epilogue and a prologue, indexed by a set of registers, consisting of a fresh pair of registers \texttt{RA} and the set of registers that must be saved by the callee of a function. |
---|
[1727] | 740 | |
---|
[1780] | 741 | The prologue and epilogue that are added to the function graph do the following: |
---|
[1727] | 742 | \begin{align*} |
---|
| 743 | \mathtt{prologue}(s) = & \mathtt{create\_new\_frame}; \\ |
---|
| 744 | & \mathtt{pop\ ra}; \\ |
---|
| 745 | & \mathtt{save\ callee\_saved}; \\ |
---|
| 746 | & \mathtt{get\_params} \\ |
---|
| 747 | & \ \ \mathtt{reg\_params}: \mathtt{move} \\ |
---|
| 748 | & \ \ \mathtt{stack\_params}: \mathtt{push}/\mathtt{pop}/\mathtt{move} \\ |
---|
| 749 | \end{align*} |
---|
[1780] | 750 | That is, the prologue first creates a new stack frame, pops the return address from the stack, saves all the callee saved registers (i.e. the set \texttt{s}), fetches the parameters that are passed via registers and the stack and moves them into the correct registers. |
---|
| 751 | In other words, the prologue of a function correctly sets up the calling convention used in the compiler when calling a function. |
---|
| 752 | On the other hand, the epilogue undoes the action of the prologue: |
---|
[1727] | 753 | \begin{align*} |
---|
| 754 | \mathtt{epilogue}(s) = & \mathtt{save\ return\ to\ tmp\ real\ regs}; \\ |
---|
| 755 | & \mathtt{restore\_registers}; \\ |
---|
| 756 | & \mathtt{push\ ra}; \\ |
---|
| 757 | & \mathtt{delete\_frame}; \\ |
---|
| 758 | & \mathtt{save return} \\ |
---|
| 759 | \end{align*} |
---|
[1780] | 760 | That is, the epilogue first saves the return value to a temporary register, restores all the registers, pushes the return address on to the stack, deletes the stack frame that the prologue created, and saves the return value. |
---|
[1727] | 761 | |
---|
[1780] | 762 | The \texttt{CALL} instruction is translated as follows: |
---|
[1727] | 763 | \begin{displaymath} |
---|
[1738] | 764 | \mathtt{CALL}\ id \mapsto \mathtt{set\_params};\ \mathtt{CALL}\ id;\ \mathtt{fetch\_result} |
---|
[1727] | 765 | \end{displaymath} |
---|
[1780] | 766 | Here, \texttt{set\_params} and \texttt{fetch\_result} are functions that implement what the caller of the function needs to do when calling a function, as opposed to the epilogue and prologue which implement what the callee must do. |
---|
[1727] | 767 | |
---|
[1780] | 768 | The translation from RTL to ERTL and execution functions must satisfy the following properties for \texttt{CALL} and \texttt{RETURN} instructions appropriately: |
---|
| 769 | \begin{displaymath} |
---|
| 770 | \begin{diagram} |
---|
| 771 | \mathtt{CALL} & \rTo^1 & \mathtt{inside\ function} \\ |
---|
| 772 | \dTo & & \dTo \\ |
---|
| 773 | \underbrace{\ldots}_{\llbracket \mathtt{CALL} \rrbracket} & \rTo & |
---|
| 774 | \underbrace{\ldots}_{\mathtt{prologue}} \\ |
---|
| 775 | \end{diagram} |
---|
| 776 | \end{displaymath} |
---|
| 777 | That is, if we start in a RTL \texttt{CALL} instruction, and translate this to an ERTL \texttt{CALL} instruction, then executing the RTL \texttt{CALL} instruction for one step and translating should land us in the prologue of the translated function. |
---|
| 778 | A similar property for \texttt{RETURN} should also hold, substituting the prologue for the epilogue of the function being translated: |
---|
| 779 | \begin{displaymath} |
---|
| 780 | \begin{diagram} |
---|
| 781 | \mathtt{RETURN} & \rTo^1 & \mathtt{.} \\ |
---|
| 782 | \dTo & & \dTo \\ |
---|
| 783 | \underbrace{\ldots}_{\mathtt{epilogue}} & \rTo & |
---|
| 784 | \underbrace{\ldots} \\ |
---|
| 785 | \end{diagram} |
---|
| 786 | \end{displaymath} |
---|
| 787 | |
---|
[1734] | 788 | \subsection{The ERTL to LTL translation} |
---|
| 789 | \label{subsect.ertl.ltl.translation} |
---|
[1790] | 790 | During the ERTL to LTL translation pseudo-registers are stored in hardware registers or spilled onto the stack frame. |
---|
| 791 | The decision is based on a liveness analysis performed on the ERTL code to determine what pair of pseudo-registers are live at the same time for a given location. |
---|
| 792 | A colouring algorithm is then used to choose where to store the pseudo-registers, permitting pseudo-registers that are deemed never to be live at the same time to share the same location. |
---|
[1786] | 793 | |
---|
[1790] | 794 | We will not certify any colouring algorithm or control flow analysis. |
---|
| 795 | Instead, we axiomatically assume the existence of `oracles' that implement the colouring and liveness analyses. |
---|
| 796 | In a later phase we plan to validate the solutions by writing and certifying the code of a validator. |
---|
[1786] | 797 | |
---|
[1790] | 798 | We describe the liveness analysis and colouring analysis first and then the ERTL to LTL translation after. |
---|
[1786] | 799 | |
---|
[1790] | 800 | Throughout this section, we denote pseudoregisters with the type $\mathtt{register}$ and hardware registers with $\mathtt{hdwregister}$. |
---|
[1786] | 801 | \subsubsection{Liveness analysis} |
---|
[1750] | 802 | \newcommand{\declsf}[1]{\expandafter\newcommand\expandafter{\csname #1\endcsname}{\mathop{\mathsf{#1}}\nolimits}} |
---|
| 803 | \declsf{Livebefore} |
---|
| 804 | \declsf{Liveafter} |
---|
| 805 | \declsf{Defined} |
---|
| 806 | \declsf{Used} |
---|
| 807 | \declsf{Eliminable} |
---|
| 808 | \declsf{StatementSem} |
---|
[1785] | 809 | |
---|
[1790] | 810 | For the liveness analysis, we aim to construct a map |
---|
[1750] | 811 | $\ell \in \mathtt{label} \mapsto $ live registers at $\ell$. |
---|
[1790] | 812 | We define the following operators on ERTL statements: |
---|
| 813 | |
---|
| 814 | \begin{displaymath} |
---|
[1750] | 815 | \begin{array}{lL>{(ex. $}L<{)$}} |
---|
[1785] | 816 | \Defined(\ell) & registers defined at $\ell$ & \ell:r_1\leftarrow r_2+r_3 \mapsto \{r_1,C\}, \ell:\mathtt{CALL}~id\mapsto \text{caller-save} |
---|
[1750] | 817 | \\ |
---|
[1785] | 818 | \Used(\ell) & registers used at $\ell$ & \ell:r_1\leftarrow r_2+r_3 \mapsto \{r_2,r_3\}, \ell:\mathtt{CALL}~id\mapsto \text{parameters} |
---|
[1750] | 819 | \end{array} |
---|
[1790] | 820 | \end{displaymath} |
---|
| 821 | Given $LA:\mathtt{label}\to\mathtt{lattice}$ (where $\mathtt{lattice}$ is the type of sets of registers\footnote{More precisely, it is the lattice $\mathtt{set}(\mathtt{register}) \times \mathtt{set}(\mathtt{hdwregister})$, with pointwise operations.}), we also have have the following predicates: |
---|
| 822 | \begin{displaymath} |
---|
[1750] | 823 | \begin{array}{lL} |
---|
[1785] | 824 | \Eliminable_{LA}(\ell) & iff executing $\ell$ has side-effects only on $r\notin LA(\ell)$ |
---|
[1750] | 825 | \\& |
---|
[1785] | 826 | (ex.\ $\ell : r_1\leftarrow r_2+r_3 \mapsto (\{r_1,C\}\cap LA(\ell)\neq\emptyset), |
---|
[1750] | 827 | \mathtt{CALL}id\mapsto \text{never}$) |
---|
| 828 | \\ |
---|
| 829 | \Livebefore_{LA}(\ell) &$:= |
---|
| 830 | \begin{cases} |
---|
| 831 | LA(\ell) &\text{if $\Eliminable_{LA}(\ell)$,}\\ |
---|
[1785] | 832 | (LA(\ell)\setminus \Defined(\ell))\cup \Used(\ell) &\text{otherwise}. |
---|
[1750] | 833 | \end{cases}$ |
---|
| 834 | \end{array} |
---|
[1790] | 835 | \end{displaymath} |
---|
| 836 | In particular, $\Livebefore$ has type $(\mathtt{label}\to\mathtt{lattice})\to \mathtt{label}\to\mathtt{lattice}$. |
---|
[1716] | 837 | |
---|
[1790] | 838 | The equation upon which we build the fixpoint is then |
---|
| 839 | \begin{displaymath} |
---|
| 840 | \Liveafter(\ell) \doteq \bigcup_{\ell <_1 \ell'} \Livebefore_{\Liveafter}(\ell') |
---|
| 841 | \end{displaymath} |
---|
| 842 | where $\ell <_1 \ell'$ denotes that $\ell'$ is an immediate successor of $\ell$ in the graph. |
---|
| 843 | We do not require the fixpoint to be the least one, so the hypothesis on $\Liveafter$ that we require is |
---|
[1785] | 844 | \begin{equation} |
---|
| 845 | \label{eq:livefixpoint} |
---|
| 846 | \Liveafter(\ell) \supseteq \bigcup_{\ell <_1 \ell'} \Livebefore(\ell') |
---|
| 847 | \end{equation} |
---|
[1790] | 848 | (for brevity we drop the subscript from $\Livebefore$). |
---|
[1785] | 849 | |
---|
| 850 | \subsubsection{Colouring} |
---|
[1790] | 851 | |
---|
[1785] | 852 | \declsf{Colour} |
---|
| 853 | \newcommand{\at}{\mathrel{@}} |
---|
[1790] | 854 | |
---|
| 855 | The aim of liveness analysis is to define what properties we need of the colouring function, which is a map (computed separately for each internal function) |
---|
| 856 | \begin{displaymath} |
---|
| 857 | \Colour:\mathtt{register}\to\mathtt{hdwregister}+\mathtt{nat} |
---|
| 858 | \end{displaymath} |
---|
| 859 | which identifies pseudoregisters with hardware ones if it is able to, otherwise it spills them to the stack. |
---|
| 860 | We will just state what property we require from such a map. |
---|
| 861 | First, we extend the definition to all types of registers by: |
---|
| 862 | \begin{displaymath} |
---|
| 863 | \begin{aligned} |
---|
[1785] | 864 | \Colour^+:\mathtt{hdwregister}+\mathtt{register} &\to \mathtt{hdwregister}+\mathtt{nat}\\ |
---|
| 865 | r & \mapsto |
---|
| 866 | \begin{cases} |
---|
| 867 | \Colour(r) &\text{if $r\in\mathtt{register}$,}\\ |
---|
| 868 | r &\text{if $r\in\mathtt{hdwregister}$,}. |
---|
| 869 | \end{cases} |
---|
[1790] | 870 | \end{aligned} |
---|
| 871 | \end{displaymath} |
---|
| 872 | The other piece of information we compute for each function is a \emph{similarity} relation, which is an equivalence relation on all kinds of registers which depends on the point of the program. |
---|
| 873 | We write |
---|
| 874 | \begin{displaymath} |
---|
| 875 | x\sim y \at \ell |
---|
| 876 | \end{displaymath} |
---|
| 877 | to state that registers $x$ and $y$ are similar at $\ell$. |
---|
| 878 | The formal definition of this relation's property will be given next, but intuitively it means that those two registers \emph{must} have the same value after $\ell$. |
---|
| 879 | The analysis that produces this information can be coarse: in our case, we just set two different registers to be similar at $\ell$ if at $\ell$ itself there is a move instruction between the two. |
---|
[1785] | 880 | |
---|
| 881 | The property required of colouring is the following: |
---|
| 882 | \begin{equation} |
---|
| 883 | \label{eq:colourprop} |
---|
| 884 | \forall \ell.\forall x,y. x,y\in \Liveafter(\ell)\Rightarrow |
---|
| 885 | \Colour^+(x)=\Colour^+(y) \Rightarrow x\sim y \at\ell. |
---|
| 886 | \end{equation} |
---|
| 887 | |
---|
| 888 | \subsubsection{The translation} |
---|
[1790] | 889 | |
---|
[1785] | 890 | For example: |
---|
[1790] | 891 | \begin{displaymath} |
---|
| 892 | \ell : r_1\leftarrow r_2+r_3 \mapsto \begin{cases} |
---|
[1785] | 893 | \varepsilon & \text{if $\Eliminable(\ell)$},\\ |
---|
| 894 | \Colour(r_1) \leftarrow \Colour(r_2) + \Colour(r_3) & \text{otherwise}. |
---|
[1790] | 895 | \end{cases} |
---|
| 896 | \end{displaymath} |
---|
| 897 | where $\varepsilon$ is the empty block of instructions (i.e.\ a \texttt{GOTO}), and $\Colour(r_1) \leftarrow \Colour(r_2) + \Colour(r_3)$ is a notation for a block of instructions that take into account: |
---|
[1785] | 898 | \begin{itemize} |
---|
[1790] | 899 | \item |
---|
| 900 | Load and store ops on the stack if any colouring is in fact a spilling; |
---|
| 901 | \item |
---|
| 902 | Using the accumulator to store intermediate values. |
---|
[1785] | 903 | \end{itemize} |
---|
[1790] | 904 | The overall effect is that if $T$ is an LTL state with $\ell(T)=\ell$ then we will have $T\to^+T'$ where $T'(\Colour(r_1))=T(\Colour(r_2))+T(\Colour(r_2))$, while $T'(y)=T(y)$ for any $y$ \emph{in the image of $\Colour$} different from $\Colour(r_1)$. |
---|
| 905 | Some hardware registers that are used for book-keeping and which are explicitly excluded from colouring may have different values. |
---|
[1785] | 906 | |
---|
[1788] | 907 | We skip the details of correctly dealing with the stack and its size. |
---|
[1790] | 908 | |
---|
[1785] | 909 | \subsubsection{The relation between ERTL and LTL states} |
---|
[1790] | 910 | |
---|
| 911 | Given a state $S$ in ERTL, we abuse notation by using $S$ as the underlying map |
---|
| 912 | \begin{displaymath} |
---|
| 913 | S : \mathtt{hdwregister}+\mathtt{register} \to \mathtt{Value} |
---|
| 914 | \end{displaymath} |
---|
| 915 | We write $\ell(S)$ for the program counter in $S$. |
---|
| 916 | At this point we can state the property asked from similarity: |
---|
[1785] | 917 | \begin{equation} |
---|
| 918 | \label{eq:similprop} |
---|
| 919 | \forall S,S'.S\to S' \Rightarrow \forall x,y.x\sim y \at \ell(S) \Rightarrow S'(x) = S'(y). |
---|
| 920 | \end{equation} |
---|
| 921 | |
---|
[1790] | 922 | Next, we relate ERTL states with LTL ones. |
---|
| 923 | For a state $T$ in LTL we again abuse notation using $T$ as a map |
---|
| 924 | \begin{displaymath} |
---|
| 925 | T: \mathtt{hdwregister}+\mathtt{nat} \to \mathtt{Value} |
---|
| 926 | \end{displaymath} |
---|
| 927 | which maps hardware registers and \emph{local stack offsets} to values (in particular, $T$ as a map depends on the saved frames for computing the correct absolute stack values). |
---|
[1785] | 928 | |
---|
[1790] | 929 | The relation existing between the states at the two sides of this translation step, which depends on liveness and colouring, is then defined as |
---|
| 930 | \begin{displaymath} |
---|
| 931 | S\mathrel\sigma T \iff \ldots \wedge \forall x. x\in \Livebefore(\ell(S))\Rightarrow T(\Colour^+(x)) = S(x) |
---|
| 932 | \end{displaymath} |
---|
| 933 | The ellipsis stands for other straightforward preservation, among which the properties $\ell(T) = \ell(S)$ and, inductively, the preservation of frames. |
---|
[1785] | 934 | |
---|
| 935 | \subsubsection{Proof of preservation} |
---|
[1790] | 936 | |
---|
[1785] | 937 | We will prove the following proposition: |
---|
[1790] | 938 | \begin{displaymath} |
---|
| 939 | \forall S, T. S \mathrel\sigma T \Rightarrow S \to S' \Rightarrow \exists T'.T\to^+ T' \wedge S'\mathrel\sigma T' |
---|
| 940 | \end{displaymath} |
---|
| 941 | (with appropriate cost-labelled trace preservation which we omit). |
---|
| 942 | We will call $S\mathrel \sigma T$ the inductive hypothsis, as it will be such in the complete proof by induction on the trace of the program. |
---|
| 943 | As usual, this step is done by cases on the statement at $\ell(S)$ and how it is translated. |
---|
| 944 | We carry out the case of a binary operation on registers in some detail. |
---|
[1785] | 945 | |
---|
| 946 | Suppose that $\ell(S):r_1 \leftarrow r_2+r_3$, so that |
---|
[1790] | 947 | \begin{displaymath} |
---|
| 948 | S'(x)=\begin{cases}S(r_1)+S(r_2) &\text{if $x=r_1$,}\\S(x) &\text{otherwise.}\end{cases} |
---|
| 949 | \end{displaymath} |
---|
| 950 | |
---|
[1785] | 951 | \paragraph*{Case $\Eliminable(\ell(S))$.} |
---|
| 952 | |
---|
[1790] | 953 | By definition we have $r_1\notin \Liveafter(\ell(S))$, and the translation of the operation yields a \texttt{GOTO}. |
---|
| 954 | We take $T'$ the immediate successor of $T$. |
---|
| 955 | |
---|
[1785] | 956 | Now in order to prove $S'\mathrel\sigma T'$, take any |
---|
[1790] | 957 | \begin{displaymath} |
---|
| 958 | x\in\Livebefore(\ell(S'))\subseteq \Liveafter(\ell(S)) = \Livebefore(\ell(S)) |
---|
| 959 | \end{displaymath} |
---|
| 960 | where we have used property~\eqref{eq:livefixpoint} and the definition of $\Livebefore$ when $\Eliminable(\ell(S))$. |
---|
| 961 | We get the following chain of equalities: |
---|
| 962 | |
---|
| 963 | \begin{displaymath} |
---|
| 964 | T'(\Colour^+(x))\stackrel 1=T(\Colour^+(x))\stackrel 2=S(x) \stackrel 3= S'(x) |
---|
| 965 | \end{displaymath} |
---|
[1785] | 966 | where |
---|
| 967 | \begin{enumerate} |
---|
[1790] | 968 | \item |
---|
| 969 | follows as $T'$ has the same store as $T$, |
---|
| 970 | \item |
---|
| 971 | follows from the inductive hypothesis as $x\in\Livebefore(\ell(S))$, |
---|
| 972 | \item |
---|
| 973 | follows as $x\neq r_1$, as $r_1\notin \Liveafter(\ell(S))\ni x$. |
---|
[1785] | 974 | \end{enumerate} |
---|
[1790] | 975 | |
---|
[1785] | 976 | \paragraph*{Case $\neg\Eliminable(\ell(S))$.} |
---|
[1790] | 977 | |
---|
[1785] | 978 | We then have $r_1\in\Liveafter(\ell(S))$, and |
---|
| 979 | |
---|
[1790] | 980 | \begin{displaymath} |
---|
| 981 | \Livebefore(\ell(S))=(\Liveafter(\ell(S))\setminus\{r_1\})\cup\{r_2,r_3\} |
---|
| 982 | \end{displaymath} |
---|
| 983 | Moreover the statement is translated to $\Colour(r_1)\leftarrow\Colour(r_2)+\Colour(r_3)$, and we take the $T'\leftarrow^+T$ such that $T'(\Colour(r_1))=T(\Colour(r_2))+T(\Colour(r_3))$ and $T'(\Colour^+(x))=T(\Colour^+(x))$ for all $x$ with $\Colour^+(x)\neq\Colour(r_1)$. |
---|
| 984 | |
---|
[1785] | 985 | Take any $x\in\Livebefore(\ell(S'))\subseteq \Liveafter(\ell(S))$ (by property~\eqref{eq:livefixpoint}). |
---|
| 986 | |
---|
[1790] | 987 | If $\Colour^+(x)=\Colour(r_1)$, we have by property~\eqref{eq:colourprop} that $x\sim r_1\at \ell(S)$ (as both $r_1,x\in\Liveafter(\ell(S))$, so that |
---|
| 988 | \begin{displaymath} |
---|
| 989 | T'(\Colour^+(x))=T(\Colour(r_2))+T(\Colour(r_3))\stackrel 1=S(r_2)+S(r_3)=S'(r_1)\stackrel 2=S(x) |
---|
| 990 | \end{displaymath} |
---|
[1785] | 991 | where |
---|
| 992 | \begin{enumerate} |
---|
[1790] | 993 | \item |
---|
| 994 | follows from two uses of the inductive hypothesis, as $r_2,r_3\in\Livebefore(\ell(S))$, |
---|
| 995 | \item |
---|
| 996 | follows from property~\eqref{eq:similprop}\footnote{Notice that in our particular implementation for this case of binary op $x\sim r_1\at\ell(S)$ implies $x=r_1$. |
---|
| 997 | However, nothing prevents us from employing more finegrained heuristics for similarity.}. |
---|
[1785] | 998 | \end{enumerate} |
---|
| 999 | |
---|
[1790] | 1000 | If $\Colour^+(x)\neq\Colour(r_1)$ (so in particular $x\neq r_1$), then $x\in\Livebefore(\ell(S))$, so by inductive hypothesis we have |
---|
| 1001 | \begin{displaymath} |
---|
| 1002 | T'(\Colour^+(x))=T(\Colour^+(x))=S(x)=S'(x) |
---|
| 1003 | \end{displaymath} |
---|
[1785] | 1004 | |
---|
[1734] | 1005 | \subsection{The LTL to LIN translation} |
---|
| 1006 | \label{subsect.ltl.lin.translation} |
---|
[1790] | 1007 | As detailed elsewhere in the reports, due to the parameterised representation of the back-end languages, the pass described here is actually much more generic than the translation from LTL to LIN. |
---|
| 1008 | It consists in a linearisation pass that maps any graph-based back-end language to its corresponding linear form, preserving its semantics. |
---|
| 1009 | In the rest of the section, however, we will keep the names LTL and LIN for the two partial instantiations of the parameterized language for convenience. |
---|
[1716] | 1010 | |
---|
[1721] | 1011 | We require a map, $\sigma$, from LTL statuses, where program counters are represented as labels in a graph data structure, to LIN statuses, where program counters are natural numbers: |
---|
| 1012 | \begin{displaymath} |
---|
| 1013 | \mathtt{pc : label} \stackrel{\sigma}{\longrightarrow} \mathbb{N} |
---|
| 1014 | \end{displaymath} |
---|
| 1015 | |
---|
[1723] | 1016 | The LTL to LIN translation pass also linearises the graph data structure into a list of instructions. |
---|
| 1017 | Pseudocode for the linearisation process is as follows: |
---|
[1721] | 1018 | |
---|
[1723] | 1019 | \begin{lstlisting} |
---|
| 1020 | let rec linearise graph visited required generated todo := |
---|
| 1021 | match todo with |
---|
| 1022 | | l::todo -> |
---|
| 1023 | if l $\in$ visited then |
---|
| 1024 | let generated := generated $\cup\ \{$ Goto l $\}$ in |
---|
| 1025 | let required := required $\cup$ l in |
---|
| 1026 | linearise graph visited required generated todo |
---|
| 1027 | else |
---|
[1725] | 1028 | -- Get the instruction at label `l' in the graph |
---|
[1723] | 1029 | let lookup := graph(l) in |
---|
| 1030 | let generated := generated $\cup\ \{$ lookup $\}$ in |
---|
[1725] | 1031 | -- Find the successor of the instruction at label `l' in the graph |
---|
[1723] | 1032 | let successor := succ(l, graph) in |
---|
| 1033 | let todo := successor::todo in |
---|
| 1034 | linearise graph visited required generated todo |
---|
| 1035 | | [] -> (required, generated) |
---|
| 1036 | \end{lstlisting} |
---|
[1721] | 1037 | |
---|
[1725] | 1038 | It is easy to see that this linearisation process eventually terminates. |
---|
| 1039 | In particular, the size of the visited label set is monotonically increasing, and is bounded above by the size of the graph that we are linearising. |
---|
[1721] | 1040 | |
---|
[1725] | 1041 | The initial call to \texttt{linearise} sees the \texttt{visited}, \texttt{required} and \texttt{generated} sets set to the empty set, and \texttt{todo} initialized with the singleton list consisting of the entry point of the graph. |
---|
| 1042 | We envisage needing to prove the following invariants on the linearisation function above: |
---|
| 1043 | |
---|
| 1044 | \begin{enumerate} |
---|
| 1045 | \item |
---|
| 1046 | $\mathtt{visited} \approx \mathtt{generated}$, where $\approx$ is \emph{multiset} equality, as \texttt{generated} is a set of instructions where instructions may mention labels multiple times, and \texttt{visited} is a set of labels, |
---|
| 1047 | \item |
---|
| 1048 | $\forall \mathtt{l} \in \mathtt{generated}.\ \mathtt{succ(l,\ graph)} \subseteq \mathtt{required} \cup \mathtt{todo}$, |
---|
| 1049 | \item |
---|
| 1050 | $\mathtt{required} \subseteq \mathtt{visited}$, |
---|
| 1051 | \item |
---|
| 1052 | $\mathtt{visited} \cap \mathtt{todo} = \emptyset$. |
---|
| 1053 | \end{enumerate} |
---|
| 1054 | |
---|
| 1055 | The invariants collectively imply the following properties, crucial to correctness, about the linearisation process: |
---|
| 1056 | |
---|
| 1057 | \begin{enumerate} |
---|
| 1058 | \item |
---|
| 1059 | Every graph node is visited at most once, |
---|
| 1060 | \item |
---|
| 1061 | Every instruction that is generated is generated due to some graph node being visited, |
---|
| 1062 | \item |
---|
| 1063 | The successor instruction of every instruction that has been visited already will eventually be visited too. |
---|
| 1064 | \end{enumerate} |
---|
| 1065 | |
---|
[1790] | 1066 | Note, because the LTL to LIN transformation is the first time the code of a function is linearised in the back-end, we must discover a notion of `well-formed function code' suitable for linearised forms. |
---|
| 1067 | In particular, we see the notion of well-formedness (yet to be formally defined) resting on the following conditions: |
---|
[1725] | 1068 | |
---|
| 1069 | \begin{enumerate} |
---|
| 1070 | \item |
---|
[1762] | 1071 | For every jump to a label in a linearised function code, the target label exists at some point in the function code, |
---|
[1725] | 1072 | \item |
---|
[1762] | 1073 | Each label is unique, appearing only once in the function code, |
---|
[1725] | 1074 | \item |
---|
[1790] | 1075 | The final instruction of a function code must be a return or an unconditional jump. |
---|
[1725] | 1076 | \end{enumerate} |
---|
| 1077 | |
---|
[1726] | 1078 | We assume that these properties will be easy consequences of the invariants on the linearisation function defined above. |
---|
| 1079 | |
---|
[1725] | 1080 | The final condition above is potentially a little opaque, so we explain further. |
---|
[1762] | 1081 | The only instructions that can reasonably appear in final position at the end of a function code are returns or backward jumps, as any other instruction would cause execution to `fall out' of the end of the program (for example, when a function invoked with \texttt{CALL} returns, it returns to the next instruction past the \texttt{CALL} that invoked it). |
---|
[1725] | 1082 | |
---|
[1734] | 1083 | \subsection{The LIN to ASM and ASM to MCS-51 machine code translations} |
---|
| 1084 | \label{subsect.lin.asm.translation} |
---|
[1716] | 1085 | |
---|
[1721] | 1086 | The LIN to ASM translation step is trivial, being almost the identity function. |
---|
| 1087 | The only non-trivial feature of the LIN to ASM translation is that all labels are `named apart' so that there is no chance of freshly generated labels from different namespaces clashing with labels from another namespace. |
---|
[1716] | 1088 | |
---|
[1721] | 1089 | The ASM to MCS-51 machine code translation step, and the required statements of correctness, are found in an unpublished manuscript attached to this document. |
---|
[1790] | 1090 | This is the most complex translation because of the huge number of cases to be addressed and because of the complexity of the two semantics. |
---|
| 1091 | Moreover, in the assembly code we have conditional and unconditional jumps to arbitrary locations in the code, which are not supported by the MCS-51 instruction set. |
---|
| 1092 | The latter has several kind of jumps characterized by a different instruction size and execution time, but limited in range. |
---|
| 1093 | For instance, conditional jumps to locations whose destination is more than $2^7$ bytes away from the jump instruction location are not supported at all and need to be emulated with a code transformation. |
---|
| 1094 | This problem, which is known in the literature as branch displacement and is a universal problem for all architectures of microcontroller, is known to be computationally hard, often lying inside NP, depending on the exact characteristics of the target architecture. |
---|
| 1095 | As far as we know, we will provide the first formally verified proof of correctness for an assembler that implements branch displacement. |
---|
| 1096 | We are also providing the first verified proof of correctness of a mildly optimising branch displacement algorithm that, at the moment, is almost finished, but not described in the companion paper. |
---|
| 1097 | This proof, in isolation, took around 6 man months. |
---|
[1721] | 1098 | |
---|
[1757] | 1099 | \section{Correctness of cost prediction} |
---|
| 1100 | |
---|
[1790] | 1101 | Roughly speaking, the proof of correctness of cost prediction shows that the cost of executing a labelled object-code program is the same as the summation over all labels in the program execution trace of the cost statically associated to the label and computed on the object code itself. |
---|
[1757] | 1102 | |
---|
[1790] | 1103 | In the presence of object-level function calls, the previous statement is, however, incorrect. |
---|
| 1104 | The reason is twofold. |
---|
| 1105 | First of all, a function call may diverge. |
---|
| 1106 | However, to the labels that appears just before a call, we also associate the cost of the instructions that follow the call. |
---|
| 1107 | Therefore, in the summation over all labels, when we meet a label we pre-pay for the instructions that appear after function calls, assuming all calls to be terminating. |
---|
[1757] | 1108 | |
---|
[1790] | 1109 | This choice is driven by several considerations on the source code. |
---|
| 1110 | Namely, functions can be called inside expressions, and it would be too disruptive to put labels inside expressions to capture the cost of instructions that follow a call. |
---|
| 1111 | Moreover, adding a label after each call would produce a much higher number of proof obligations in the certification of source programs using Frama-C. |
---|
| 1112 | The proof obligations, further, would be guarded by a requirement to demonstrate the termination of all functions involved, that also generates lots of additional complex proof obligations that have little to do with execution costs. |
---|
| 1113 | |
---|
| 1114 | With our approach, instead, we put less burden on the user, at the price of proving a weaker statement: the estimated and actual costs will be the same if and only if the high-level program is converging. |
---|
| 1115 | For prefixes of diverging programs we can provide a similar result where the equality is replaced by an inequality (loss of precision). |
---|
| 1116 | |
---|
| 1117 | Assuming totality of functions is however not a sufficiently strong condition at the object-level. |
---|
| 1118 | Even if a function returns, there is no guarantee that it will transfer control back to the calling point. |
---|
| 1119 | For instance, the function could have manipulated the return address from its stack frame. |
---|
| 1120 | Moreover, an object-level program can forge any address and transfer control to it, with no guarantees about the execution behaviour and labelling properties of the called program. |
---|
| 1121 | |
---|
| 1122 | To solve the problem, we introduced the notion of a \emph{structured trace} that come in two flavours: structured traces for total programs (an inductive type) and structured traces for diverging programs (a co-inductive type based on the previous one). |
---|
| 1123 | Roughly speaking, a structured trace represents the execution of a well behaved program that is subject to several constraints, such as: |
---|
[1761] | 1124 | \begin{enumerate} |
---|
[1790] | 1125 | \item |
---|
| 1126 | All function calls return control just after the calling point, |
---|
| 1127 | \item |
---|
| 1128 | The execution of all function bodies start with a label and end with a \texttt{RET} instruction (even those reached by invoking a function pointer), |
---|
| 1129 | \item |
---|
| 1130 | All instructions are covered by a label (required by the correctness of the labelling approach), |
---|
| 1131 | \item |
---|
| 1132 | The target of all conditional jumps must be labelled (a sufficient but not necessary condition for precision of the labelling approach) |
---|
| 1133 | \item |
---|
| 1134 | \label{prop5} |
---|
| 1135 | Two structured traces with the same structure yield the same cost traces. |
---|
[1761] | 1136 | \end{enumerate} |
---|
| 1137 | |
---|
[1790] | 1138 | Correctness of cost predictions is only proved for structured execution traces, i.e. well behaved programs. |
---|
| 1139 | The forward simulation proof for all back-end passes will actually be a proof of preservation of the structure of the structured traces that, because of property \ref{prop5}, will imply correctness of the cost prediction for the back-end. |
---|
| 1140 | The Clight to RTLabs correctness proof will also include a proof that associates to each converging execution its converging structured trace and to each diverging execution its diverging structured trace. |
---|
[1761] | 1141 | |
---|
[1790] | 1142 | There are also two more issues that invalidate the na\"ive statement of correctness for cost prediciton given above. |
---|
| 1143 | The algorithm that statically computes the costs of blocks is correct only when the object code is \emph{well-formed} and the program counter is \emph{reachable}. |
---|
| 1144 | A well-formed object-code is such that the program counter will never overflow after any execution step of the processor. |
---|
| 1145 | An overflow that occurs during fetching but is overwritten during execution is, however, correct and necessary to accept correct programs that are as large as the processor's code memory. |
---|
| 1146 | Temporary overflows add complications to the proof. |
---|
| 1147 | A reachable address is an address that can be obtained by fetching (\emph{not executing!}) a finite number of times from the beginning of the code memory without ever overflowing. |
---|
| 1148 | The complication is that the static prediction traverses the code memory assuming that the memory will be read sequentially from the beginning and that all jumps jump only to reachable addresses. |
---|
| 1149 | When this property is violated, the way the code memory is interpreted is incorrect and the cost computed is meaningless. |
---|
| 1150 | The reachability relation is closed by fetching for well-formed programs. |
---|
| 1151 | The property that states that function pointers only target reachable and well-labelled locations, however, is not statically predictable and it is therefore enforced in the structured trace. |
---|
[1761] | 1152 | |
---|
[1790] | 1153 | The proof of correctness for cost predictions, and even discovering the correct statement, has been quite complex. |
---|
| 1154 | Setting up good invariants (i.e. structured traces, well formed programs, reachability) and completing the proof has required more than 3 man months, while the initally estimated effort was much lower. |
---|
| 1155 | In the paper-and-pencil proof for IMP, the corresponding proof was `obvious' and only took two lines. |
---|
[1761] | 1156 | |
---|
[1790] | 1157 | The proof itself is quite involved. |
---|
| 1158 | We must show, as an important lemma, that the sum of the execution costs over a structured trace, where the costs are summed in execution order, is equivalent to the sum of the execution costs in the order of pre-payment. |
---|
| 1159 | The two orders are quite different and the proof is by mutual recursion over the definition of the converging structured traces, which is a family of three mutual inductive types. |
---|
| 1160 | The fact that this property only holds for converging function calls is hidden in the definition of the structured traces. |
---|
| 1161 | Then we need to show that the order of pre-payment corresponds to the order induced by the cost traces extracted from the structured trace. |
---|
| 1162 | Finally, we need to show that the statically computed cost for one block corresponds to the cost dynamically computed in pre-payment order. |
---|
[1761] | 1163 | |
---|
[1758] | 1164 | \section{Overall results} |
---|
| 1165 | |
---|
[1790] | 1166 | Functional correctness of the compiled code can be shown by composing the simulations to show that the target behaviour matches the behaviour of the source program, if the source program does not `go wrong'. |
---|
| 1167 | More precisely, we show that there is a forward simulation between the source trace and a (flattened structured) trace of the output, and conclude equivalence because the target's semantics are in the form of an executable function, and hence deterministic. |
---|
[1758] | 1168 | |
---|
[1790] | 1169 | Combining this with the correctness of the assignment of costs to cost labels at the ASM level for a structured trace, we can show that the cost of executing any compiled function (including the top-level, main function of a C program) is equal to the sum of all the values for cost labels encountered in the \emph{source code's} trace of the function. |
---|
[1758] | 1170 | |
---|
[1741] | 1171 | \section{Estimated effort} |
---|
| 1172 | |
---|
[1790] | 1173 | Based on a rough analysis performed so far we can estimate the total effort required for the certification of the whole compiler. |
---|
| 1174 | We obtain this estimation by combining, for each pass: |
---|
| 1175 | \begin{enumerate} |
---|
| 1176 | \item |
---|
| 1177 | The number of lines of code to be certified, |
---|
| 1178 | \item |
---|
| 1179 | The ratio of number of lines of proof to number of lines of code from the CompCert project~\cite{compcert} for the CompCert pass that is closest in spirit to our own, |
---|
| 1180 | \item |
---|
| 1181 | An estimation of the complexity of the pass according to the analysis above. |
---|
| 1182 | \end{enumerate} |
---|
| 1183 | The result is shown in Table~\ref{table}. |
---|
| 1184 | |
---|
[1775] | 1185 | \begin{table}{h} |
---|
[1791] | 1186 | \begin{center} |
---|
[1756] | 1187 | \begin{tabular}{lrlrr} |
---|
[1747] | 1188 | Pass origin & Code lines & CompCert ratio & Estimated effort & Estimated effort \\ |
---|
| 1189 | & & & (based on CompCert) & \\ |
---|
[1744] | 1190 | \hline |
---|
[1756] | 1191 | Common & 4864 & 4.25 \permil & 20.67 & 17.0 \\ |
---|
| 1192 | Cminor & 1057 & 5.23 \permil & 5.53 & 6.0 \\ |
---|
| 1193 | Clight & 1856 & 5.23 \permil & 9.71 & 10.0 \\ |
---|
| 1194 | RTLabs & 1252 & 1.17 \permil & 1.48 & 5.0 \\ |
---|
| 1195 | RTL & 469 & 4.17 \permil & 1.95 & 2.0 \\ |
---|
[1754] | 1196 | ERTL & 789 & 3.01 \permil & 2.38 & 2.5 \\ |
---|
| 1197 | LTL & 92 & 5.94 \permil & 0.55 & 0.5 \\ |
---|
[1756] | 1198 | LIN & 354 & 6.54 \permil & 2.31 & 1.0 \\ |
---|
| 1199 | ASM & 984 & 4.80 \permil & 4.72 & 10.0 \\ |
---|
[1744] | 1200 | \hline |
---|
[1756] | 1201 | Total common & 4864 & 4.25 \permil & 20.67 & 17.0 \\ |
---|
| 1202 | Total front-end & 2913 & 5.23 \permil & 15.24 & 16.0 \\ |
---|
| 1203 | Total back-end & 6853 & 4.17 \permil & 13.39 & 21.0 \\ |
---|
[1747] | 1204 | \hline |
---|
[1756] | 1205 | Total & 14630 & 3.75 \permil & 49.30 & 54.0 \\ |
---|
[1741] | 1206 | \end{tabular} |
---|
[1791] | 1207 | \end{center} |
---|
[1786] | 1208 | \caption{\label{table} Estimated effort} |
---|
[1775] | 1209 | \end{table} |
---|
[1741] | 1210 | |
---|
[1790] | 1211 | We provide now some additional informations on the methodology used in the computation. |
---|
| 1212 | The passes in Cerco and CompCert front-end closely match each other. |
---|
| 1213 | However, there is no clear correspondence between the two back-ends. |
---|
| 1214 | For instance, we enforce the calling convention immediately after instruction selection, whereas in CompCert this is performed in a later phase. |
---|
| 1215 | Further, we linearise the code at the very end, whereas CompCert performs linearisation as soon as possible. |
---|
| 1216 | Therefore, the first part of the estimation exercise has consisted of shuffling and partitioning the CompCert code in order to assign to each CerCo pass the CompCert code that performs a similar transformation. |
---|
[1745] | 1217 | |
---|
[1790] | 1218 | After this preliminary step, using the data given in~\cite{compcert} (which refers to an early version of CompCert) we computed the ratio between man months and lines of code in CompCert for each CerCo pass. |
---|
| 1219 | This is shown in the third column of Table~\ref{table}. |
---|
| 1220 | For those CerCo passes that have no correspondence in CompCert (like the optimising assembler) or where we have insufficient data, we have used the average of the ratios computed above. |
---|
[1745] | 1221 | |
---|
[1790] | 1222 | The first column of the table shows the number of lines of code for each pass in CerCo. |
---|
| 1223 | The third column is obtained multiplying the first with the CompCert ratio. |
---|
| 1224 | It provides an estimate of the effort required (in man months) if the complexity of the proofs for CerCo and CompCert would be the same. |
---|
[1745] | 1225 | |
---|
[1790] | 1226 | The two proof styles, however, are purposefully completely different. |
---|
| 1227 | Where CompCert uses non-executable semantics, describing the various semantics of languages with inductive types, we have preferred executable semantics. |
---|
| 1228 | Therefore, CompCert proofs by induction and inversion become proofs by functional inversion, performed using the Russell methodology (now called Program in Coq, but whose behaviour differs from Matita's one). |
---|
| 1229 | Moreover, CompCert code is written using only types that belong to the Hindley-Milner fragment, whereas we have heavily exploited dependent types throughout the codebase. |
---|
| 1230 | The dependent type discipline offers many advantages, especially from the point of view of clarity of the invariants involved and early detection of errors. |
---|
| 1231 | It also naturally combines well with the Russell approach which is based on dependent types. |
---|
| 1232 | However, it is also known to introduce several technical problems, like the need to explicitly prove type equalities to be able to manipulate expressions in certain ways. |
---|
| 1233 | In many situations, the difficulties encountered with manipulating dependent types are better addressed by improving the Matita system, according to the formalisation driven system development. |
---|
| 1234 | For this reason, and assuming a pessimistic estimation of our performance, the fourth columns presents the final estimation of the effort required, that also takes into account the complexity of the proof suggested by the informal proofs sketched in the previous section. |
---|
[1745] | 1235 | |
---|
[1774] | 1236 | \subsection{Contingency plan} |
---|
| 1237 | |
---|
[1790] | 1238 | On the basis of the proof strategy sketched in this document and the estimated effort, we can refine our contingency plan. |
---|
| 1239 | In case we will end the certification of the basic compiler in advance we will have the choice of either proving loop optimisations and/or partial redundancy elimination correct (both tasks seem difficult to achieve in a short time) or considering the MCS-51 specific extensions introduced during the first period and under-used in the formalised prototype. |
---|
| 1240 | Yet another possibility would be to better study retargeting of the code and the commutation property between different compiler passes. |
---|
| 1241 | The latter study is easily enabled by our approach where all back-end languages are instances of the same parameterized language. |
---|
[1774] | 1242 | |
---|
[1790] | 1243 | In the case of a consistent delay in the certification of some components, we will first address the passes that are more likely to have undetected bugs and we will follow a top-down approach, axiomatizing the properties of the data structures used in the compiler to focus more on the algorithms. |
---|
| 1244 | The rationale is that data structures are easier than algorithms to test using well-known methodologies. |
---|
| 1245 | The effort table clearly shows that common definitions and data structures are one quarter of the size of the current codebase code and require slightly less than one third of the total effort. |
---|
| 1246 | At least half of this effort really goes into simple data structures (vectors, bounded and unbounded integers, tries and maps) whose certification is not interesting and whose code could be taken without re-proving it from the library of any other theorem prover. |
---|
| 1247 | |
---|
[1762] | 1248 | \section{Conclusions} |
---|
| 1249 | |
---|
[1790] | 1250 | The overall exercise, whose details have only been sketched here, has been very useful. |
---|
| 1251 | It has brought to light some errors in our proof that have required major changes in the proof plan. |
---|
| 1252 | It has also shown that the last passes of the compilation (e.g. assembly) and cost prediction on the object-code are much more involved than more high-level passes. |
---|
[1762] | 1253 | |
---|
[1790] | 1254 | The final estimation for the effort required to complete the proof suffers from a low degree of confidence engendered in the numbers, due to the difficulty in relating our work, and compiler design, with that of CompCert. |
---|
| 1255 | It is however sufficient to conclude that the effort required is in line with the man power that was scheduled for the second half of the second period and for the third period. |
---|
| 1256 | Compared to the number of man months declared in Annex I of the contract, we will need more man months. |
---|
| 1257 | However, at both UNIBO and UEDIN there have been major differences in hiring with respect to the Annex. |
---|
| 1258 | Therefore both sites now have more manpower available, though with the associated trade-off of a lower level of maturity for the people employed. |
---|
[1762] | 1259 | |
---|
[1790] | 1260 | Our reviewers suggested that we use this estimation to compare two possible scenarios: a) proceed as planned, porting all the CompCert proofs to Matita or b) port D3.1 and D4.1 to Coq and re-use the CompCert proofs. |
---|
| 1261 | We remark here again that the back-end of the two compilers, from the memory model on, are quite different: we are not re-proving correctness of the same piece of code. |
---|
| 1262 | Moreover, the proof techniques are different for the front-end too. |
---|
| 1263 | Switching to the CompCert formalisation would imply the abandoning of the untrusted compiler, the abandoning of the experiment with a different proof technique, the abandoning of the quest for an open-source proof, and the abandoning of the co-development of the formalisation and the Matita proof assistant. |
---|
| 1264 | In the Commitment Letter~\cite{letter}, delivered to the Officer in May, we clarified our personal perspectives on the project goals and objectives. |
---|
| 1265 | We do not re-describe here the point of view presented in the letter, other than the condensed soundbite that ``we value diversity''. |
---|
[1762] | 1266 | |
---|
[1790] | 1267 | Clearly, if the execise would have suggested the infeasability in terms of effort of concluding the formalisation, or getting close to that, we would have abandoned our path and embraced the reviewer's suggestion. |
---|
| 1268 | However, we have been comforted in the analysis that we did in Autumn and further progress completed during the winter does not yet show any major delay with respect to the proof schedule. |
---|
| 1269 | We are thus planning to continue the certification according to the more detailed proof plan that came out from the exercise reported in this manuscript. |
---|
| 1270 | |
---|
[1776] | 1271 | \begin{thebibliography}{2} |
---|
| 1272 | \bibitem{compcert} X. Leroy, ``A Formally Verified Compiler back-end'', |
---|
| 1273 | Journal of Automated Reasoning 43(4)):363-446, 2009. |
---|
| 1274 | |
---|
| 1275 | \bibitem{letter}The CerCo team, ``Commitment to the Consideration of Reviewer's Reccomendation'', 16/05/2011. |
---|
| 1276 | \end{thebibliography} |
---|
| 1277 | |
---|
[1794] | 1278 | \newpage |
---|
[1776] | 1279 | |
---|
[1794] | 1280 | \includepdf[pages={-}]{cpp-2011.pdf} |
---|
| 1281 | |
---|
| 1282 | |
---|
[1716] | 1283 | \end{document} |
---|