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