1 | \documentclass[11pt,epsf,a4wide]{article} |
---|
2 | \usepackage[mathletters]{ucs} |
---|
3 | \usepackage[utf8x]{inputenc} |
---|
4 | \usepackage{listings} |
---|
5 | \usepackage{../../style/cerco} |
---|
6 | \newcommand{\ocaml}{OCaml} |
---|
7 | \newcommand{\clight}{Clight} |
---|
8 | \newcommand{\matita}{Matita} |
---|
9 | \newcommand{\sdcc}{\texttt{sdcc}} |
---|
10 | |
---|
11 | \newcommand{\textSigma}{\ensuremath{\Sigma}} |
---|
12 | |
---|
13 | % LaTeX Companion, p 74 |
---|
14 | \newcommand{\todo}[1]{\marginpar{\raggedright - #1}} |
---|
15 | |
---|
16 | \lstdefinelanguage{coq} |
---|
17 | {keywords={Definition,Lemma,Theorem,Remark,Qed,Save,Inductive,Record}, |
---|
18 | morekeywords={[2]if,then,else}, |
---|
19 | } |
---|
20 | |
---|
21 | \lstdefinelanguage{matita} |
---|
22 | {keywords={definition,lemma,theorem,remark,inductive,record,qed,let,rec,match,with,Type,and,on}, |
---|
23 | morekeywords={[2]whd,normalize,elim,cases,destruct}, |
---|
24 | mathescape=true, |
---|
25 | morecomment=[n]{(*}{*)}, |
---|
26 | } |
---|
27 | |
---|
28 | \lstset{language=matita,basicstyle=\small\tt,columns=flexible,breaklines=false, |
---|
29 | keywordstyle=\color{red}\bfseries, |
---|
30 | keywordstyle=[2]\color{blue}, |
---|
31 | commentstyle=\color{green}, |
---|
32 | stringstyle=\color{blue}, |
---|
33 | showspaces=false,showstringspaces=false} |
---|
34 | |
---|
35 | \lstset{extendedchars=false} |
---|
36 | \lstset{inputencoding=utf8x} |
---|
37 | \DeclareUnicodeCharacter{8797}{:=} |
---|
38 | \DeclareUnicodeCharacter{10746}{++} |
---|
39 | \DeclareUnicodeCharacter{9001}{\ensuremath{\langle}} |
---|
40 | \DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}} |
---|
41 | |
---|
42 | |
---|
43 | \title{ |
---|
44 | INFORMATION AND COMMUNICATION TECHNOLOGIES\\ |
---|
45 | (ICT)\\ |
---|
46 | PROGRAMME\\ |
---|
47 | \vspace*{1cm}Project FP7-ICT-2009-C-243881 \cerco{}} |
---|
48 | |
---|
49 | \date{ } |
---|
50 | \author{} |
---|
51 | |
---|
52 | \begin{document} |
---|
53 | \thispagestyle{empty} |
---|
54 | |
---|
55 | \vspace*{-1cm} |
---|
56 | \begin{center} |
---|
57 | \includegraphics[width=0.6\textwidth]{../../style/cerco_logo.png} |
---|
58 | \end{center} |
---|
59 | |
---|
60 | \begin{minipage}{\textwidth} |
---|
61 | \maketitle |
---|
62 | \end{minipage} |
---|
63 | |
---|
64 | |
---|
65 | \vspace*{0.5cm} |
---|
66 | \begin{center} |
---|
67 | \begin{LARGE} |
---|
68 | \bf |
---|
69 | Report n. D3.4\\ |
---|
70 | Front-end Correctness Proofs\\ |
---|
71 | \end{LARGE} |
---|
72 | \end{center} |
---|
73 | |
---|
74 | \vspace*{2cm} |
---|
75 | \begin{center} |
---|
76 | \begin{large} |
---|
77 | Version 1.0 |
---|
78 | \end{large} |
---|
79 | \end{center} |
---|
80 | |
---|
81 | \vspace*{0.5cm} |
---|
82 | \begin{center} |
---|
83 | \begin{large} |
---|
84 | Authors:\\ |
---|
85 | Brian~Campbell, Ilias~Garnier, James~McKinna, Ian~Stark |
---|
86 | \end{large} |
---|
87 | \end{center} |
---|
88 | |
---|
89 | \vspace*{\fill} |
---|
90 | \noindent |
---|
91 | Project Acronym: \cerco{}\\ |
---|
92 | Project full title: Certified Complexity\\ |
---|
93 | Proposal/Contract no.: FP7-ICT-2009-C-243881 \cerco{}\\ |
---|
94 | |
---|
95 | \clearpage \pagestyle{myheadings} \markright{\cerco{}, FP7-ICT-2009-C-243881} |
---|
96 | |
---|
97 | \newpage |
---|
98 | |
---|
99 | \vspace*{7cm} |
---|
100 | \paragraph{Abstract} |
---|
101 | We report on the correctness proofs for the front-end of the \cerco{} cost |
---|
102 | lifting compiler, considering three distinct parts of the task: showing that |
---|
103 | the \emph{annotated source code} output by the compiler has equivalent |
---|
104 | behaviour to the original input (up to the annotations); showing that a |
---|
105 | \emph{measurable} subtrace of the annotated source code corresponds to an |
---|
106 | equivalent measurable subtrace in the code produced by the front-end, including |
---|
107 | costs; and finally showing that the enriched \emph{structured} execution traces |
---|
108 | required for cost correctness in the back-end can be constructed from the |
---|
109 | properties of the code produced by the front-end. |
---|
110 | |
---|
111 | A key part of our work is that the intensional correctness results that show |
---|
112 | that we get consistent cost measurements throughout the intermediate languages |
---|
113 | of the compiler can be layered on top of normal forward simulation results, |
---|
114 | if we split them into local call-structure preserving results. |
---|
115 | |
---|
116 | This deliverable shows correctness results about the formalised compiler |
---|
117 | described in D3.2, using the source language semantics from D3.1 and |
---|
118 | intermediate language semantics from D3.3. Together with the companion |
---|
119 | deliverable about the correctness of the back-end, D4.4, we obtain results |
---|
120 | about the whole formalised compiler. |
---|
121 | |
---|
122 | % TODO: mention the deliverable about the extracted compiler et al? |
---|
123 | |
---|
124 | \newpage |
---|
125 | |
---|
126 | \tableofcontents |
---|
127 | |
---|
128 | % CHECK: clear up any -ize vs -ise |
---|
129 | % CHECK: clear up any "front end" vs "front-end" |
---|
130 | % CHECK: clear up any mentions of languages that aren't textsf'd. |
---|
131 | % CHECK: fix unicode in listings |
---|
132 | |
---|
133 | \section{Introduction} |
---|
134 | |
---|
135 | \todo{add stack space for more precise statement. Also do some |
---|
136 | translation validation on sound, precise labelling properties.} |
---|
137 | |
---|
138 | The \cerco{} compiler compiles C code targeting microcontrollers implementing |
---|
139 | the Intel 8051 architecture, which produces both the object code and source |
---|
140 | code containing annotations describing the timing behavior of the object code. |
---|
141 | There are two versions: first, an initial prototype implemented in |
---|
142 | \ocaml{}~\cite{d2.2}, and a version formalised in the \matita{} proof |
---|
143 | assistant~\cite{d3.2,d4.2} and then extracted to \ocaml{} code to produce an |
---|
144 | executable compiler. In this document we present results formalised in |
---|
145 | \matita{} about the front-end of that version of the compiler, and how that fits |
---|
146 | into the verification of the whole compiler. |
---|
147 | |
---|
148 | \todo{maybe mention stack space here? other additions? refer to "layering"?} |
---|
149 | A key part of this work was to separate the proofs about the compiled code's |
---|
150 | extensional behaviour (that is, the functional correctness of the compiler) |
---|
151 | from the intensional correctness that the costs given are correct. |
---|
152 | Unfortunately, the ambitious goal of completely verifying the compiler was not |
---|
153 | feasible within the time available, but thanks to this separation of extensional |
---|
154 | and intensional proofs we are able to axiomatize simulation results similar to |
---|
155 | those in other compiler verification projects and concentrate on the novel |
---|
156 | intensional proofs. The proofs were also made more tractable by introducing |
---|
157 | compile-time checks for the `sound and precise' cost labelling properties |
---|
158 | rather than proving that they are preserved throughout. |
---|
159 | |
---|
160 | The overall statement of correctness says that the annotated program has the |
---|
161 | same behaviour as the input, and that for any suitably well-structured part of |
---|
162 | the execution (which we call \emph{measurable}), the object code will execute |
---|
163 | the same behaviour taking precisely the time given by the cost annotations in |
---|
164 | the annotated source program. |
---|
165 | |
---|
166 | In the next section we recall the structure of the compiler and make the overall |
---|
167 | statement more precise. Following that, in Section~\ref{sec:fegoals} we |
---|
168 | describe the statements we need to prove about the intermediate \textsf{RTLabs} |
---|
169 | programs sufficient for the back-end proofs. \todo{rest of document structure} |
---|
170 | |
---|
171 | \section{The compiler and main goals} |
---|
172 | |
---|
173 | TODO: outline compiler, maybe figure from talk, maybe something like the figure |
---|
174 | below. |
---|
175 | |
---|
176 | TODO: might want a version of this figure |
---|
177 | \begin{figure} |
---|
178 | \begin{center} |
---|
179 | \begin{minipage}{.8\linewidth} |
---|
180 | \begin{tabbing} |
---|
181 | \quad \= $\downarrow$ \quad \= \kill |
---|
182 | \textsf{C} (unformalized)\\ |
---|
183 | \> $\downarrow$ \> CIL parser (unformalized \ocaml)\\ |
---|
184 | \textsf{Clight}\\ |
---|
185 | %\> $\downarrow$ \> add runtime functions\\ |
---|
186 | \> $\downarrow$ \> \lstinline[language=C]'switch' removal\\ |
---|
187 | \> $\downarrow$ \> labelling\\ |
---|
188 | \> $\downarrow$ \> cast removal\\ |
---|
189 | \> $\downarrow$ \> stack variable allocation and control structure |
---|
190 | simplification\\ |
---|
191 | \textsf{Cminor}\\ |
---|
192 | %\> $\downarrow$ \> generate global variable initialization code\\ |
---|
193 | \> $\downarrow$ \> transform to RTL graph\\ |
---|
194 | \textsf{RTLabs}\\ |
---|
195 | \> $\downarrow$ \> check cost labelled properties of RTL graph\\ |
---|
196 | \> $\downarrow$ \> start of target specific back-end\\ |
---|
197 | \>\quad \vdots |
---|
198 | \end{tabbing} |
---|
199 | \end{minipage} |
---|
200 | \end{center} |
---|
201 | \caption{Front-end languages and compiler passes} |
---|
202 | \label{fig:summary} |
---|
203 | \end{figure} |
---|
204 | |
---|
205 | The compiler function returns the following record on success: |
---|
206 | \begin{lstlisting}[language=matita] |
---|
207 | record compiler_output : Type[0] := |
---|
208 | { c_labelled_object_code: labelled_object_code |
---|
209 | ; c_stack_cost: stack_cost_model |
---|
210 | ; c_max_stack: nat |
---|
211 | ; c_init_costlabel: costlabel |
---|
212 | ; c_labelled_clight: clight_program |
---|
213 | ; c_clight_cost_map: clight_cost_map |
---|
214 | }. |
---|
215 | \end{lstlisting} |
---|
216 | It consists of annotated 8051 object code, a mapping from function |
---|
217 | identifiers to the function's stack space usage\footnote{The compiled |
---|
218 | code's only stack usage is to allocate a fixed-size frame on each |
---|
219 | function entry and discard it on exit.}, a cost label covering the |
---|
220 | initialisation of global variables and calling the |
---|
221 | \lstinline[language=C]'main' function, the annotated source code, and |
---|
222 | finally a mapping from cost labels to actual execution time costs. |
---|
223 | |
---|
224 | An \ocaml{} pretty printer is used to provide a concrete version of the output |
---|
225 | code. In the case of the annotated source code, it also inserts the actual |
---|
226 | costs alongside the cost labels, and optionally adds a global cost variable |
---|
227 | and instrumentation to support further reasoning. \todo{Cross-ref case study |
---|
228 | deliverables} |
---|
229 | |
---|
230 | \subsection{Revisions to the prototype compiler} |
---|
231 | |
---|
232 | TODO: could be a good idea to have this again; stack space, |
---|
233 | initialisation, cost checks, had we dropped cminor loops in previous |
---|
234 | writing?, check mailing list in case I've forgotten something |
---|
235 | |
---|
236 | \subsection{Main goals} |
---|
237 | |
---|
238 | TODO: need an example for this |
---|
239 | |
---|
240 | Informally, our main intensional result links the time difference in a source |
---|
241 | code execution to the time difference in the object code, expressing the time |
---|
242 | for the source by summing the values for the cost labels in the trace, and the |
---|
243 | time for the target by a clock built in to the 8051 executable semantics. |
---|
244 | |
---|
245 | The availability of precise timing information for 8501 |
---|
246 | implementations and the design of the compiler allow it to give exact |
---|
247 | time costs in terms of processor cycles. However, these exact results |
---|
248 | are only available if the subtrace we measure starts and ends at |
---|
249 | suitable points. In particular, pure computation with no observable |
---|
250 | effects may be reordered and moved past cost labels, so we cannot |
---|
251 | measure time between arbitrary statements in the program. |
---|
252 | |
---|
253 | There is also a constraint on the subtraces that we |
---|
254 | measure due to the requirements of the correctness proof for the |
---|
255 | object code timing analysis. To be sure that the timings are assigned |
---|
256 | to the correct cost label, we need to know that each return from a |
---|
257 | function call must go to the correct return address. It is difficult |
---|
258 | to observe this property locally in the object code because it relies |
---|
259 | on much earlier stages in the compiler. To convey this information to |
---|
260 | the timing analysis extra structure is imposed on the subtraces, which |
---|
261 | we will give more details on in Section~\ref{sec:fegoals}. |
---|
262 | |
---|
263 | % Regarding the footnote, would there even be much point? |
---|
264 | These restrictions are reflected in the subtraces that we give timing |
---|
265 | guarantees on; they must start at a cost label and end at the return |
---|
266 | of the enclosing function of the cost label\footnote{We expect that |
---|
267 | this would generalise to subtraces between cost labels in the same |
---|
268 | function, but could not justify the extra complexity that would be |
---|
269 | required to show this.}. A typical example of such a subtrace is |
---|
270 | the execution of an entire function from the cost label at the start |
---|
271 | of the function until it returns. We call such any such subtrace |
---|
272 | \emph{measurable} if it (and the prefix of the trace before it) can |
---|
273 | also be executed within the available stack space. |
---|
274 | |
---|
275 | Now we can give the main intensional statement for the compiler. |
---|
276 | Given a \emph{measurable} subtrace for a labelled \textsf{Clight} |
---|
277 | program, there is a subtrace of the 8051 object code program where the |
---|
278 | time differences match. Moreover, \emph{observable} parts of the |
---|
279 | trace also match --- these are the appearance of cost labels and |
---|
280 | function calls and returns. |
---|
281 | |
---|
282 | More formally, the definition of this statement in \matita{} is |
---|
283 | \begin{lstlisting}[language=matita] |
---|
284 | definition simulates := |
---|
285 | $\lambda$p: compiler_output. |
---|
286 | let initial_status := initialise_status $\dots$ (cm (c_labelled_object_code $\dots$ p)) in |
---|
287 | $\forall$m1,m2. |
---|
288 | measurable Clight_pcs (c_labelled_clight $\dots$ p) m1 m2 |
---|
289 | (lookup_stack_cost (c_stack_cost $\dots$ p)) (c_max_stack $\dots$ p) $\rightarrow$ |
---|
290 | $\forall$c1,c2. |
---|
291 | clock_after Clight_pcs (c_labelled_clight $\dots$ p) m1 (c_clight_cost_map $\dots$ p) = OK $\dots$ c1 $\rightarrow$ |
---|
292 | clock_after Clight_pcs (c_labelled_clight $\dots$ p) (m1+m2) (c_clight_cost_map $\dots$ p) = OK $\dots$ c2 $\rightarrow$ |
---|
293 | $\exists$n1,n2. |
---|
294 | observables Clight_pcs (c_labelled_clight $\dots$ p) m1 m2 = |
---|
295 | observables (OC_preclassified_system (c_labelled_object_code $\dots$ p)) |
---|
296 | (c_labelled_object_code $\dots$ p) n1 n2 |
---|
297 | $\wedge$ |
---|
298 | c2 - c1 = clock $\dots$ (execute n2 ? initial_status) - clock $\dots$ (execute n1 ? initial_status). |
---|
299 | \end{lstlisting} |
---|
300 | where the \lstinline'measurable', \lstinline'clock_after' and |
---|
301 | \lstinline'observables' definitions can be applied to multiple |
---|
302 | languages; in this case the \lstinline'Clight_pcs' record applies them |
---|
303 | to \textsf{Clight} programs. |
---|
304 | |
---|
305 | There is a second part to the statement, which says that the initial |
---|
306 | processing of the input program to produce the cost labelled version |
---|
307 | does not affect the semantics of the program: |
---|
308 | % Yes, I'm paraphrasing the result a tiny bit to remove the observe non-function |
---|
309 | \begin{lstlisting}[language=matita] |
---|
310 | $\forall$input_program,output. |
---|
311 | compile input_program = return output $\rightarrow$ |
---|
312 | not_wrong … (exec_inf … clight_fullexec input_program) $\rightarrow$ |
---|
313 | sim_with_labels |
---|
314 | (exec_inf … clight_fullexec input_program) |
---|
315 | (exec_inf … clight_fullexec (c_labelled_clight … output)) |
---|
316 | \end{lstlisting} |
---|
317 | That is, any successful compilation produces a labelled program that |
---|
318 | has identical behaviour to the original, so long as there is no |
---|
319 | `undefined behaviour'. |
---|
320 | |
---|
321 | Note that this provides full functional correctness, including |
---|
322 | preservation of (non-)termination. The intensional result above does |
---|
323 | not do this directly --- it does not guarantee the same result or same |
---|
324 | termination. There are two mitigating factors, however: first, to |
---|
325 | prove the intensional property you need local simulation results that |
---|
326 | can be pieced together to form full behavioural equivalence, only time |
---|
327 | constraints have prevented us from doing so. Second, if we wish to |
---|
328 | confirm a result, termination, or non-termination we could add an |
---|
329 | observable witness, such as a function that is only called if the |
---|
330 | correct result is given. The intensional result guarantees that the |
---|
331 | observable witness is preserved, so the program must behave correctly. |
---|
332 | |
---|
333 | \section{Intermediate goals for the front-end} |
---|
334 | \label{sec:fegoals} |
---|
335 | |
---|
336 | The essential parts of the intensional proof were outlined during work |
---|
337 | on a toy |
---|
338 | compiler~\cite{d2.1,springerlink:10.1007/978-3-642-32469-7_3}. These |
---|
339 | are |
---|
340 | \begin{enumerate} |
---|
341 | \item functional correctness, in particular preserving the trace of |
---|
342 | cost labels, |
---|
343 | \item the \emph{soundness} and \emph{precision} of the cost labelling |
---|
344 | on the object code, and |
---|
345 | \item the timing analysis on the object code produces a correct |
---|
346 | mapping from cost labels to time. |
---|
347 | \end{enumerate} |
---|
348 | |
---|
349 | However, that toy development did not include function calls. For the |
---|
350 | full \cerco{} compiler we also need to maintain the invariant that |
---|
351 | functions return to the correct program location in the caller, as we |
---|
352 | mentioned in the previous section. During work on the back-end timing |
---|
353 | analysis (describe in more detail in the companion deliverable, D4.4) |
---|
354 | the notion of a \emph{structured trace} was developed to enforce this |
---|
355 | return property, and also most of the cost labelling properties too. |
---|
356 | |
---|
357 | \begin{figure} |
---|
358 | \begin{center} |
---|
359 | \includegraphics[width=0.5\linewidth]{compiler.pdf} |
---|
360 | \end{center} |
---|
361 | \caption{The compiler and proof outline} |
---|
362 | \label{fig:compiler} |
---|
363 | \end{figure} |
---|
364 | |
---|
365 | Jointly, we generalised the structured traces to apply to any of the |
---|
366 | intermediate languages with some idea of program counter. This means |
---|
367 | that they are introduced part way through the compiler, see |
---|
368 | Figure~\ref{fig:compiler}. Proving that a structured trace can be |
---|
369 | constructed at \textsf{RTLabs} has several virtues: |
---|
370 | \begin{itemize} |
---|
371 | \item This is the first language where every operation has its own |
---|
372 | unique, easily addressable, statement. |
---|
373 | \item Function calls and returns are still handled in the language and |
---|
374 | so the structural properties are ensured by the semantics. |
---|
375 | \item Many of the back-end languages from \textsf{RTL} share a common |
---|
376 | core set of definitions, and using structured traces throughout |
---|
377 | increases this uniformity. |
---|
378 | \end{itemize} |
---|
379 | |
---|
380 | \begin{figure} |
---|
381 | \begin{center} |
---|
382 | \includegraphics[width=0.6\linewidth]{strtraces.pdf} |
---|
383 | \end{center} |
---|
384 | \caption{Nesting of functions in structured traces} |
---|
385 | \label{fig:strtrace} |
---|
386 | \end{figure} |
---|
387 | A structured trace is a mutually inductive data type which principally |
---|
388 | contains the steps from a normal program trace, but arranged into a |
---|
389 | nested structure which groups entire function calls together and |
---|
390 | aggregates individual steps between cost labels (or between the final |
---|
391 | cost label and the return from the function), see |
---|
392 | Figure~\ref{fig:strtrace}. This capture the idea that the cost labels |
---|
393 | only represent costs \emph{within} a function --- calls to other |
---|
394 | functions are accounted for in the trace for their execution, and we |
---|
395 | can locally regard function calls as a single step. |
---|
396 | |
---|
397 | These structured traces form the core part of the intermediate results |
---|
398 | that we must prove so that the back-end can complete the main |
---|
399 | intensional result stated above. In full, we provide the back-end |
---|
400 | with |
---|
401 | \begin{enumerate} |
---|
402 | \item A normal trace of the \textbf{prefix} of the program's execution |
---|
403 | before reaching the measurable subtrace. (This needs to be |
---|
404 | preserved so that we know that the stack space consumed is correct.) |
---|
405 | \item The \textbf{structured trace} corresponding to the measurable |
---|
406 | subtrace. |
---|
407 | \item An additional property about the structured trace that no |
---|
408 | `program counter' is \textbf{repeated} between cost labels. Together with |
---|
409 | the structure in the trace, this takes over from showing that |
---|
410 | cost labelling is sound and precise. |
---|
411 | \item A proof that the \textbf{observables} have been preserved. |
---|
412 | \item A proof that the \textbf{stack limit} is still observed by the prefix and |
---|
413 | the structure trace. (This is largely a consequence of the |
---|
414 | preservation of observables.) |
---|
415 | \end{enumerate} |
---|
416 | |
---|
417 | Following the outline in Figure~\ref{fig:compiler}, we will first deal |
---|
418 | with the transformations in \textsf{Clight} that produce the source |
---|
419 | program with cost labels, then show that measurable traces can be |
---|
420 | lifted to \textsf{RTLabs}, and finally that we can construct the |
---|
421 | properties listed above ready for the back-end proofs. |
---|
422 | |
---|
423 | \section{Input code to cost labelled program} |
---|
424 | |
---|
425 | The simple form of labelling used in the formalised compiler is not |
---|
426 | quite capable of capturing costs arising from complex C |
---|
427 | \lstinline[language=C]'switch' statements, largely due to the |
---|
428 | fall-through behaviour. Our first pass replaces these statements with |
---|
429 | simpler C code, allowing our second pass to perform the cost |
---|
430 | labelling. We show that the behaviour of programs is unchanged by |
---|
431 | these passes. |
---|
432 | |
---|
433 | TODO: both give one-step-sim-by-many forward sim results; switch |
---|
434 | removal tricky, uses aux var to keep result of expr, not central to |
---|
435 | intensional correctness so curtailed proof effort once reasonable |
---|
436 | level of confidence in code gained; labelling much simpler; don't care |
---|
437 | what the labels are at this stage, just need to know when to go |
---|
438 | through extra steps. Rolled up into a single result with a cofixpoint |
---|
439 | to obtain coinductive statement of equivalence (show). |
---|
440 | |
---|
441 | \section{Finding corresponding measurable subtraces} |
---|
442 | |
---|
443 | There follow the three main passes of the front-end: |
---|
444 | \begin{enumerate} |
---|
445 | \item simplification of casts in \textsf{Clight} code |
---|
446 | \item \textsf{Clight} to \textsf{Cminor} translation, performing stack |
---|
447 | variable allocation and simplifying control structures |
---|
448 | \item transformation to \textsf{RTLabs} control flow graph |
---|
449 | \end{enumerate} |
---|
450 | \todo{I keep mentioning forward sim results - I probably ought to say |
---|
451 | something about determinancy} We have taken a common approach to |
---|
452 | each pass: first we build (or axiomatise) forward simulation results |
---|
453 | that are similar to normal compiler proofs, but slightly more |
---|
454 | fine-grained so that we can see that the call structure and relative |
---|
455 | placement of cost labels is preserved. |
---|
456 | |
---|
457 | Then we instantiate a general result which shows that we can find a |
---|
458 | \emph{measurable} subtrace in the target of the pass that corresponds |
---|
459 | to the measurable subtract in the source. By repeated application of |
---|
460 | this result we can find a measurable subtrace of the execution of the |
---|
461 | \textsf{RTLabs} code, suitable for the construction of a structured |
---|
462 | trace (see Section~\ref{sec:structuredtrace}. This is essentially an |
---|
463 | extra layer on top of the simulation proofs that provides us with the |
---|
464 | extra information required for our intensional correctness proof. |
---|
465 | |
---|
466 | \subsection{Generic measurable subtrace lifting proof} |
---|
467 | |
---|
468 | Our generic proof is parametrised on a record containing small-step |
---|
469 | semantics for the source and target language, a classification of |
---|
470 | states (the same form of classification is used when defining |
---|
471 | structured traces), a simulation relation which loosely respects the |
---|
472 | classification and cost labelling \todo{this isn't very helpful} and |
---|
473 | four simulation results: |
---|
474 | \begin{enumerate} |
---|
475 | \item a step from a `normal' state (which is not classified as a call |
---|
476 | or return) which is not a cost label is simulated by zero or more |
---|
477 | `normal' steps; |
---|
478 | \item a step from a `call' state followed by a cost label step is |
---|
479 | simulated by a step from a `call' state, a corresponding label step, |
---|
480 | then zero or more `normal' steps; |
---|
481 | \item a step from a `call' state not followed by a cost label |
---|
482 | similarly (note that this case cannot occur in a well-labelled |
---|
483 | program, but we do not have enough information locally to exploit |
---|
484 | this); and |
---|
485 | \item a cost label step is simulated by a cost label step. |
---|
486 | \end{enumerate} |
---|
487 | Finally, we need to know that a successfully translated program will |
---|
488 | have an initial state in the simulation relation with the original |
---|
489 | program's initial state. |
---|
490 | |
---|
491 | \begin{figure} |
---|
492 | \begin{center} |
---|
493 | \includegraphics[width=0.5\linewidth]{meassim.pdf} |
---|
494 | \end{center} |
---|
495 | \caption{Tiling of simulation for a measurable subtrace} |
---|
496 | \label{fig:tiling} |
---|
497 | \end{figure} |
---|
498 | |
---|
499 | To find the measurable subtrace in the target program's execution we |
---|
500 | walk along the original program's execution trace applying the |
---|
501 | appropriate simulation result by induction on the number of steps. |
---|
502 | While the number of steps taken varies, the overall structure is |
---|
503 | preserved, as illustrated in Figure~\ref{fig:tiling}. By preserving |
---|
504 | the structure we also maintain the same intensional observables. One |
---|
505 | delicate point is that the cost label following a call must remain |
---|
506 | directly afterwards\footnote{The prototype compiler allowed some |
---|
507 | straight-line code to appear before the cost label until a later |
---|
508 | stage of the compiler, but we must move the requirement forward to |
---|
509 | fit with the structured traces.} |
---|
510 | % Damn it, I should have just moved the cost label forwards in RTLabs, |
---|
511 | % like the prototype does in RTL to ERTL; the result would have been |
---|
512 | % simpler. Or was there some reason not to do that? |
---|
513 | (both in the program code and in the execution trace), even if we |
---|
514 | introduce extra steps, for example to store parameters in memory in |
---|
515 | \textsf{Cminor}. Thus we have a version of the call simulation |
---|
516 | that deals with both in one result. |
---|
517 | |
---|
518 | In addition to the subtrace we are interested in measuring we must |
---|
519 | also prove that the earlier part of the trace is also preserved in |
---|
520 | order to use the simulation from the initial state. It also |
---|
521 | guarantees that we do not run out of stack space before the subtrace |
---|
522 | we are interested in. The lemmas for this prefix and the measurable |
---|
523 | subtrace are similar, following the pattern above. However, the |
---|
524 | measurable subtrace also requires us to rebuild the termination |
---|
525 | proof. This has a recursive form: |
---|
526 | \begin{lstlisting}[language=matita] |
---|
527 | let rec will_return_aux C (depth:nat) |
---|
528 | (trace:list (cs_state … C × trace)) on trace : bool := |
---|
529 | match trace with |
---|
530 | [ nil $\Rightarrow$ false |
---|
531 | | cons h tl $\Rightarrow$ |
---|
532 | let $\langle$s,tr$\rangle$ := h in |
---|
533 | match cs_classify C s with |
---|
534 | [ cl_call $\Rightarrow$ will_return_aux C (S depth) tl |
---|
535 | | cl_return $\Rightarrow$ |
---|
536 | match depth with |
---|
537 | [ O $\Rightarrow$ match tl with [ nil $\Rightarrow$ true | _ $\Rightarrow$ false ] |
---|
538 | | S d $\Rightarrow$ will_return_aux C d tl |
---|
539 | ] |
---|
540 | | _ $\Rightarrow$ will_return_aux C depth tl |
---|
541 | ] |
---|
542 | ]. |
---|
543 | \end{lstlisting} |
---|
544 | The \lstinline'depth' is the number of return states we need to see |
---|
545 | before we have returned to the original function (initially zero) and |
---|
546 | \lstinline'trace' the measurable subtrace obtained from the running |
---|
547 | the semantics for the correct number of steps. This definition |
---|
548 | unfolds tail recursively for each step, and once the corresponding |
---|
549 | simulation result has been applied a new one for the target can be |
---|
550 | asserted by unfolding and applying the induction hypothesis on the |
---|
551 | shorter trace. |
---|
552 | |
---|
553 | This then gives us an overall result for any simulation fitting the |
---|
554 | requirements above (contained in the \lstinline'meas_sim' record): |
---|
555 | \begin{lstlisting}[language=matita] |
---|
556 | theorem measured_subtrace_preserved : |
---|
557 | $\forall$MS:meas_sim. |
---|
558 | $\forall$p1,p2,m,n,stack_cost,max. |
---|
559 | ms_compiled MS p1 p2 $\rightarrow$ |
---|
560 | measurable (ms_C1 MS) p1 m n stack_cost max $\rightarrow$ |
---|
561 | $\exists$m',n'. |
---|
562 | measurable (ms_C2 MS) p2 m' n' stack_cost max $\wedge$ |
---|
563 | observables (ms_C1 MS) p1 m n = observables (ms_C2 MS) p2 m' n'. |
---|
564 | \end{lstlisting} |
---|
565 | The stack space requirement that is embedded in \lstinline'measurable' |
---|
566 | is a consequence of the preservation of observables. |
---|
567 | |
---|
568 | TODO: how to deal with untidy edges wrt to sim rel; anything to |
---|
569 | say about obs? |
---|
570 | |
---|
571 | TODO: say something about termination measures; cost labels are |
---|
572 | statements/exprs in these languages; split call/return gives simpler |
---|
573 | simulations |
---|
574 | |
---|
575 | \subsection{Simulation results for each pass} |
---|
576 | |
---|
577 | \todo{don't use loop structures from CompCert, go straight to jumps} |
---|
578 | |
---|
579 | \section{Checking cost labelling properties} |
---|
580 | |
---|
581 | TODO: ideal for this development would be to prove at labelling stage, |
---|
582 | preserve throughout, fits with use of dep types to reduce partiality |
---|
583 | of phases; time pressures suggest different approach: t.v. style, |
---|
584 | runtime check only do proof in one place rather than three; can't push |
---|
585 | further due to structured traces, possible future investigation if ... |
---|
586 | |
---|
587 | \section{Existence of a structured trace} |
---|
588 | \label{sec:structuredtrace} |
---|
589 | |
---|
590 | TODO: design, basic structure from termination proof, how cost |
---|
591 | labelling props are baked in; unrepeating PCs, remainder of sound |
---|
592 | labellings; coinductive version for whole programs, reason/relevance, |
---|
593 | use of em (maybe a general comment about uses of classical reasoning |
---|
594 | in development) |
---|
595 | |
---|
596 | \section{Conclusion} |
---|
597 | |
---|
598 | TODO |
---|
599 | |
---|
600 | TODO: appendix on code layout? |
---|
601 | |
---|
602 | \bibliographystyle{plain} |
---|
603 | \bibliography{report} |
---|
604 | |
---|
605 | \end{document} |
---|