1 | \documentclass[11pt,epsf,a4wide]{article} |
---|
2 | \usepackage[mathletters]{ucs} |
---|
3 | \usepackage[utf8x]{inputenc} |
---|
4 | \usepackage{stmaryrd} |
---|
5 | \usepackage{listings} |
---|
6 | \usepackage{../../style/cerco} |
---|
7 | \newcommand{\ocaml}{OCaml} |
---|
8 | \newcommand{\clight}{Clight} |
---|
9 | \newcommand{\matita}{Matita} |
---|
10 | \newcommand{\sdcc}{\texttt{sdcc}} |
---|
11 | |
---|
12 | \newcommand{\textSigma}{\ensuremath{\Sigma}} |
---|
13 | |
---|
14 | % LaTeX Companion, p 74 |
---|
15 | \newcommand{\todo}[1]{\marginpar{\raggedright - #1}} |
---|
16 | |
---|
17 | \lstdefinelanguage{coq} |
---|
18 | {keywords={Definition,Lemma,Theorem,Remark,Qed,Save,Inductive,Record}, |
---|
19 | morekeywords={[2]if,then,else}, |
---|
20 | } |
---|
21 | |
---|
22 | \lstdefinelanguage{matita} |
---|
23 | {keywords={definition,lemma,theorem,remark,inductive,record,qed,let,rec,match,with,Type,and,on}, |
---|
24 | morekeywords={[2]whd,normalize,elim,cases,destruct}, |
---|
25 | mathescape=true, |
---|
26 | morecomment=[n]{(*}{*)}, |
---|
27 | } |
---|
28 | |
---|
29 | \lstset{language=matita,basicstyle=\small\tt,columns=flexible,breaklines=false, |
---|
30 | keywordstyle=\color{red}\bfseries, |
---|
31 | keywordstyle=[2]\color{blue}, |
---|
32 | commentstyle=\color{green}, |
---|
33 | stringstyle=\color{blue}, |
---|
34 | showspaces=false,showstringspaces=false} |
---|
35 | |
---|
36 | \lstset{extendedchars=false} |
---|
37 | \lstset{inputencoding=utf8x} |
---|
38 | \DeclareUnicodeCharacter{8797}{:=} |
---|
39 | \DeclareUnicodeCharacter{10746}{++} |
---|
40 | \DeclareUnicodeCharacter{9001}{\ensuremath{\langle}} |
---|
41 | \DeclareUnicodeCharacter{9002}{\ensuremath{\rangle}} |
---|
42 | |
---|
43 | |
---|
44 | \title{ |
---|
45 | INFORMATION AND COMMUNICATION TECHNOLOGIES\\ |
---|
46 | (ICT)\\ |
---|
47 | PROGRAMME\\ |
---|
48 | \vspace*{1cm}Project FP7-ICT-2009-C-243881 \cerco{}} |
---|
49 | |
---|
50 | \date{ } |
---|
51 | \author{} |
---|
52 | |
---|
53 | \begin{document} |
---|
54 | \thispagestyle{empty} |
---|
55 | |
---|
56 | \vspace*{-1cm} |
---|
57 | \begin{center} |
---|
58 | \includegraphics[width=0.6\textwidth]{../../style/cerco_logo.png} |
---|
59 | \end{center} |
---|
60 | |
---|
61 | \begin{minipage}{\textwidth} |
---|
62 | \maketitle |
---|
63 | \end{minipage} |
---|
64 | |
---|
65 | |
---|
66 | \vspace*{0.5cm} |
---|
67 | \begin{center} |
---|
68 | \begin{LARGE} |
---|
69 | \bf |
---|
70 | Report n. D3.4\\ |
---|
71 | Front-end Correctness Proofs\\ |
---|
72 | \end{LARGE} |
---|
73 | \end{center} |
---|
74 | |
---|
75 | \vspace*{2cm} |
---|
76 | \begin{center} |
---|
77 | \begin{large} |
---|
78 | Version 1.0 |
---|
79 | \end{large} |
---|
80 | \end{center} |
---|
81 | |
---|
82 | \vspace*{0.5cm} |
---|
83 | \begin{center} |
---|
84 | \begin{large} |
---|
85 | Authors:\\ |
---|
86 | Brian~Campbell, Ilias~Garnier, James~McKinna, Ian~Stark |
---|
87 | \end{large} |
---|
88 | \end{center} |
---|
89 | |
---|
90 | \vspace*{\fill} |
---|
91 | \noindent |
---|
92 | Project Acronym: \cerco{}\\ |
---|
93 | Project full title: Certified Complexity\\ |
---|
94 | Proposal/Contract no.: FP7-ICT-2009-C-243881 \cerco{}\\ |
---|
95 | |
---|
96 | \clearpage \pagestyle{myheadings} \markright{\cerco{}, FP7-ICT-2009-C-243881} |
---|
97 | |
---|
98 | \newpage |
---|
99 | |
---|
100 | \vspace*{7cm} |
---|
101 | \paragraph{Abstract} |
---|
102 | We report on the correctness proofs for the front-end of the \cerco{} |
---|
103 | cost lifting compiler. First, we identify the core result we wish to |
---|
104 | prove, which says that the we correctly predict the precise execution |
---|
105 | time for particular parts of the execution called \emph{measurable} |
---|
106 | subtraces. Then we consider the three distinct parts of the task: |
---|
107 | showing that the \emph{annotated source code} output by the compiler |
---|
108 | has equivalent behaviour to the original input (up to the |
---|
109 | annotations); showing that a measurable subtrace of the |
---|
110 | annotated source code corresponds to an equivalent measurable subtrace |
---|
111 | in the code produced by the front-end, including costs; and finally |
---|
112 | showing that the enriched \emph{structured} execution traces required |
---|
113 | for cost correctness in the back-end can be constructed from the |
---|
114 | properties of the code produced by the front-end. |
---|
115 | |
---|
116 | A key part of our work is that the intensional correctness results that show |
---|
117 | that we get consistent cost measurements throughout the intermediate languages |
---|
118 | of the compiler can be layered on top of normal forward simulation results, |
---|
119 | if we split those results into local call-structure preserving simulations. |
---|
120 | |
---|
121 | This deliverable shows correctness results about the formalised |
---|
122 | compiler described in D3.2, using the source language semantics from |
---|
123 | D3.1 and intermediate language semantics from D3.3. It builds on |
---|
124 | earlier work on a toy compiler built to test the labelling approach in |
---|
125 | D2.1. Together with the companion deliverable about the correctness of |
---|
126 | the back-end, D4.4, we obtain results about the whole formalised |
---|
127 | compiler. |
---|
128 | |
---|
129 | % TODO: mention the deliverable about the extracted compiler et al? |
---|
130 | |
---|
131 | \newpage |
---|
132 | |
---|
133 | \tableofcontents |
---|
134 | |
---|
135 | % CHECK: clear up any -ize vs -ise |
---|
136 | % CHECK: clear up any "front end" vs "front-end" |
---|
137 | % CHECK: clear up any mentions of languages that aren't textsf'd. |
---|
138 | % CHECK: fix unicode in listings |
---|
139 | |
---|
140 | \section{Introduction} |
---|
141 | |
---|
142 | \todo{add stack space for more precise statement. Also do some |
---|
143 | translation validation on sound, precise labelling properties.} |
---|
144 | |
---|
145 | The \cerco{} compiler compiles C code, targeting microcontrollers |
---|
146 | implementing the Intel 8051 architecture. It produces both the object |
---|
147 | code and source code containing annotations describing the timing |
---|
148 | behaviour of the object code. There are two versions: first, an |
---|
149 | initial prototype implemented in \ocaml{}~\cite{d2.2}, and a version |
---|
150 | formalised in the \matita{} proof assistant~\cite{d3.2,d4.2} and then |
---|
151 | extracted to \ocaml{} code to produce an executable compiler. In this |
---|
152 | document we present results formalised in \matita{} about the |
---|
153 | front-end of the latter version of the compiler, and how that fits |
---|
154 | into the verification of the whole compiler. |
---|
155 | |
---|
156 | \todo{maybe mention stack space here? other additions? refer to |
---|
157 | "layering"?} A key part of this work was to separate the proofs |
---|
158 | about the compiled code's extensional behaviour (that is, the |
---|
159 | functional correctness of the compiler) from the intensional |
---|
160 | correctness that the costs given are correct. Unfortunately, the |
---|
161 | ambitious goal of completely verifying the entire compiler was not |
---|
162 | feasible within the time available, but thanks to this separation of |
---|
163 | extensional and intensional proofs we are able to axiomatize |
---|
164 | simulation results similar to those in other compiler verification |
---|
165 | projects and concentrate on the novel intensional proofs. The proofs |
---|
166 | were also made more tractable by introducing compile-time checks for |
---|
167 | the `sound and precise' cost labelling properties rather than proving |
---|
168 | that they are preserved throughout. |
---|
169 | |
---|
170 | The overall statement of correctness says that the annotated program has the |
---|
171 | same behaviour as the input, and that for any suitably well-structured part of |
---|
172 | the execution (which we call \emph{measurable}), the object code will execute |
---|
173 | the same behaviour taking precisely the time given by the cost annotations in |
---|
174 | the annotated source program. |
---|
175 | |
---|
176 | In the next section we recall the structure of the compiler and make the overall |
---|
177 | statement more precise. Following that, in Section~\ref{sec:fegoals} we |
---|
178 | describe the statements we need to prove about the intermediate \textsf{RTLabs} |
---|
179 | programs sufficient for the back-end proofs. \todo{rest of document structure} |
---|
180 | |
---|
181 | \section{The compiler and main goals} |
---|
182 | |
---|
183 | The unformalised \ocaml{} \cerco{} compiler was originally described |
---|
184 | in Deliverables 2.1 and 2.2. Its design was replicated in the formal |
---|
185 | \matita{} code, which was presented in Deliverables 3.2 and 4.2, for |
---|
186 | the front-end and back-end respectively. |
---|
187 | |
---|
188 | \begin{figure} |
---|
189 | \begin{center} |
---|
190 | \includegraphics{compiler-plain.pdf} |
---|
191 | \end{center} |
---|
192 | \caption{Languages in the \cerco{} compiler} |
---|
193 | \label{fig:compilerlangs} |
---|
194 | \end{figure} |
---|
195 | |
---|
196 | The compiler uses a number of intermediate languages, as outlined the |
---|
197 | middle two lines of Figure~\ref{fig:compilerlangs}. The upper line |
---|
198 | represents the front-end of the compiler, and the lower the back-end, |
---|
199 | finishing with 8051 binary code. Not all of the front-end passes |
---|
200 | introduces a new language, and Figure~\ref{fig:summary} presents a |
---|
201 | list of every pass involved. |
---|
202 | |
---|
203 | \begin{figure} |
---|
204 | \begin{center} |
---|
205 | \begin{minipage}{.8\linewidth} |
---|
206 | \begin{tabbing} |
---|
207 | \quad \= $\downarrow$ \quad \= \kill |
---|
208 | \textsf{C} (unformalized)\\ |
---|
209 | \> $\downarrow$ \> CIL parser (unformalized \ocaml)\\ |
---|
210 | \textsf{Clight}\\ |
---|
211 | %\> $\downarrow$ \> add runtime functions\\ |
---|
212 | \> $\downarrow$ \> \lstinline[language=C]'switch' removal\\ |
---|
213 | \> $\downarrow$ \> labelling\\ |
---|
214 | \> $\downarrow$ \> cast removal\\ |
---|
215 | \> $\downarrow$ \> stack variable allocation and control structure |
---|
216 | simplification\\ |
---|
217 | \textsf{Cminor}\\ |
---|
218 | %\> $\downarrow$ \> generate global variable initialization code\\ |
---|
219 | \> $\downarrow$ \> transform to RTL graph\\ |
---|
220 | \textsf{RTLabs}\\ |
---|
221 | \> $\downarrow$ \> check cost labelled properties of RTL graph\\ |
---|
222 | \> $\downarrow$ \> start of target specific back-end\\ |
---|
223 | \>\quad \vdots |
---|
224 | \end{tabbing} |
---|
225 | \end{minipage} |
---|
226 | \end{center} |
---|
227 | \caption{Front-end languages and compiler passes} |
---|
228 | \label{fig:summary} |
---|
229 | \end{figure} |
---|
230 | |
---|
231 | \label{page:switchintro} |
---|
232 | The annotated source code is taken after the cost labelling phase. |
---|
233 | Note that there is a pass to replace C \lstinline[language=C]'switch' |
---|
234 | statements before labelling --- we need to remove them because the |
---|
235 | simple form of labelling used in the formalised compiler is not quite |
---|
236 | capable of capturing their execution time costs, largely due to the |
---|
237 | fall-through behaviour. |
---|
238 | |
---|
239 | The cast removal phase which follows cost labelling simplifies |
---|
240 | expressions to prevent unnecessary arithmetic promotion which is |
---|
241 | specified by the C standard but costly for an 8-bit target. The |
---|
242 | transformation to \textsf{Cminor} and subsequently \textsf{RTLabs} |
---|
243 | bear considerable resemblance to some passes of the CompCert |
---|
244 | compiler\todo{cite}, although we use a simpler \textsf{Cminor} where |
---|
245 | all loops use \lstinline[language=C]'goto' statements, and the |
---|
246 | \textsf{RTLabs} language retains a target-independent flavour. The |
---|
247 | back-end takes \textsf{RTLabs} code as input. |
---|
248 | |
---|
249 | The whole compilation function returns the following information on success: |
---|
250 | \begin{lstlisting}[language=matita] |
---|
251 | record compiler_output : Type[0] := |
---|
252 | { c_labelled_object_code: labelled_object_code |
---|
253 | ; c_stack_cost: stack_cost_model |
---|
254 | ; c_max_stack: nat |
---|
255 | ; c_init_costlabel: costlabel |
---|
256 | ; c_labelled_clight: clight_program |
---|
257 | ; c_clight_cost_map: clight_cost_map |
---|
258 | }. |
---|
259 | \end{lstlisting} |
---|
260 | It consists of annotated 8051 object code, a mapping from function |
---|
261 | identifiers to the function's stack space usage\footnote{The compiled |
---|
262 | code's only stack usage is to allocate a fixed-size frame on each |
---|
263 | function entry and discard it on exit. No other dynamic allocation |
---|
264 | is provided by the compiler.}, the space available for the |
---|
265 | stack after global variable allocation, a cost label covering the |
---|
266 | execution time for the initialisation of global variables and the call |
---|
267 | to the \lstinline[language=C]'main' function, the annotated source |
---|
268 | code, and finally a mapping from cost labels to actual execution time |
---|
269 | costs. |
---|
270 | |
---|
271 | An \ocaml{} pretty printer is used to provide a concrete version of the output |
---|
272 | code. In the case of the annotated source code, it also inserts the actual |
---|
273 | costs alongside the cost labels, and optionally adds a global cost variable |
---|
274 | and instrumentation to support further reasoning. \todo{Cross-ref case study |
---|
275 | deliverables} |
---|
276 | |
---|
277 | \subsection{Revisions to the prototype compiler} |
---|
278 | |
---|
279 | Our focus on intensional properties prompted us to consider whether we |
---|
280 | could incorporate stack space into the costs presented to the user. |
---|
281 | We only allocate one fixed-size frame per function, so modelling this |
---|
282 | was relatively simple. It is the only form of dynamic memory |
---|
283 | allocation provided by the compiler, so we were able to strengthen the |
---|
284 | statement of the goal to guarantee successful execution whenever the |
---|
285 | stack space obeys a bound calculated by subtracting the global |
---|
286 | variable requirements from the total memory available. |
---|
287 | |
---|
288 | The cost checks at the end of Figure~\ref{fig:summary} have been |
---|
289 | introduced to reduce the proof burden, and are described in |
---|
290 | Section~\ref{sec:costchecks}. |
---|
291 | |
---|
292 | The use of dependent types to capture simple intermediate language |
---|
293 | invariants makes every front-end pass except \textsf{Clight} to |
---|
294 | \textsf{Cminor} and the cost checks total functions. Hence various |
---|
295 | well-formedness and type checks are dealt with once in that phase. |
---|
296 | With the benefit of hindsight we would have included an initial |
---|
297 | checking phase to produce a `well-formed' variant of \textsf{Clight}, |
---|
298 | conjecturing that this would simplify various parts of the proofs for |
---|
299 | the \textsf{Clight} stages which deal with potentially ill-formed |
---|
300 | code. |
---|
301 | |
---|
302 | \todo{move of initialisation code?} |
---|
303 | |
---|
304 | \subsection{Main goals} |
---|
305 | |
---|
306 | TODO: need an example for this |
---|
307 | |
---|
308 | Informally, our main intensional result links the time difference in a source |
---|
309 | code execution to the time difference in the object code, expressing the time |
---|
310 | for the source by summing the values for the cost labels in the trace, and the |
---|
311 | time for the target by a clock built in to the 8051 executable semantics. |
---|
312 | |
---|
313 | The availability of precise timing information for 8501 |
---|
314 | implementations and the design of the compiler allow it to give exact |
---|
315 | time costs in terms of processor cycles, not just upper bounds. |
---|
316 | However, these exact results are only available if the subtrace we |
---|
317 | measure starts and ends at suitable points. In particular, pure |
---|
318 | computation with no observable effects may be reordered and moved past |
---|
319 | cost labels, so we cannot measure time between arbitrary statements in |
---|
320 | the program. |
---|
321 | |
---|
322 | There is also a constraint on the subtraces that we |
---|
323 | measure due to the requirements of the correctness proof for the |
---|
324 | object code timing analysis. To be sure that the timings are assigned |
---|
325 | to the correct cost label, we need to know that each return from a |
---|
326 | function call must go to the correct return address. It is difficult |
---|
327 | to observe this property locally in the object code because it relies |
---|
328 | on much earlier stages in the compiler. To convey this information to |
---|
329 | the timing analysis extra structure is imposed on the subtraces, which |
---|
330 | is described in Section~\ref{sec:fegoals}. |
---|
331 | |
---|
332 | % Regarding the footnote, would there even be much point? |
---|
333 | % TODO: this might be quite easy to add ('just' subtract the |
---|
334 | % measurable subtrace from the second label to the end). Could also |
---|
335 | % measure other traces in this manner. |
---|
336 | These restrictions are reflected in the subtraces that we give timing |
---|
337 | guarantees on; they must start at a cost label and end at the return |
---|
338 | of the enclosing function of the cost label\footnote{We expect that |
---|
339 | this would generalise to subtraces between cost labels in the same |
---|
340 | function, but could not justify the extra complexity that would be |
---|
341 | required to show this.}. A typical example of such a subtrace is |
---|
342 | the execution of an entire function from the cost label at the start |
---|
343 | of the function until it returns. We call such any such subtrace |
---|
344 | \emph{measurable} if it (and the prefix of the trace from the start to |
---|
345 | the subtrace) can also be executed within the available stack space. |
---|
346 | |
---|
347 | Now we can give the main intensional statement for the compiler. |
---|
348 | Given a \emph{measurable} subtrace for a labelled \textsf{Clight} |
---|
349 | program, there is a subtrace of the 8051 object code program where the |
---|
350 | time differences match. Moreover, \emph{observable} parts of the |
---|
351 | trace also match --- these are the appearance of cost labels and |
---|
352 | function calls and returns. |
---|
353 | |
---|
354 | More formally, the definition of this statement in \matita{} is |
---|
355 | \begin{lstlisting}[language=matita] |
---|
356 | definition simulates := |
---|
357 | $\lambda$p: compiler_output. |
---|
358 | let initial_status := initialise_status $\dots$ (cm (c_labelled_object_code $\dots$ p)) in |
---|
359 | $\forall$m1,m2. |
---|
360 | measurable Clight_pcs (c_labelled_clight $\dots$ p) m1 m2 |
---|
361 | (lookup_stack_cost (c_stack_cost $\dots$ p)) (c_max_stack $\dots$ p) $\rightarrow$ |
---|
362 | $\forall$c1,c2. |
---|
363 | clock_after Clight_pcs (c_labelled_clight $\dots$ p) m1 (c_clight_cost_map $\dots$ p) = OK $\dots$ c1 $\rightarrow$ |
---|
364 | clock_after Clight_pcs (c_labelled_clight $\dots$ p) (m1+m2) (c_clight_cost_map $\dots$ p) = OK $\dots$ c2 $\rightarrow$ |
---|
365 | $\exists$n1,n2. |
---|
366 | observables Clight_pcs (c_labelled_clight $\dots$ p) m1 m2 = |
---|
367 | observables (OC_preclassified_system (c_labelled_object_code $\dots$ p)) |
---|
368 | (c_labelled_object_code $\dots$ p) n1 n2 |
---|
369 | $\wedge$ |
---|
370 | c2 - c1 = clock $\dots$ (execute n2 ? initial_status) - clock $\dots$ (execute n1 ? initial_status). |
---|
371 | \end{lstlisting} |
---|
372 | where the \lstinline'measurable', \lstinline'clock_after' and |
---|
373 | \lstinline'observables' definitions can be applied to multiple |
---|
374 | languages; in this case the \lstinline'Clight_pcs' record applies them |
---|
375 | to \textsf{Clight} programs. |
---|
376 | |
---|
377 | There is a second part to the statement, which says that the initial |
---|
378 | processing of the input program to produce the cost labelled version |
---|
379 | does not affect the semantics of the program: |
---|
380 | % Yes, I'm paraphrasing the result a tiny bit to remove the observe non-function |
---|
381 | \begin{lstlisting}[language=matita] |
---|
382 | $\forall$input_program,output. |
---|
383 | compile input_program = return output $\rightarrow$ |
---|
384 | not_wrong … (exec_inf … clight_fullexec input_program) $\rightarrow$ |
---|
385 | sim_with_labels |
---|
386 | (exec_inf … clight_fullexec input_program) |
---|
387 | (exec_inf … clight_fullexec (c_labelled_clight … output)) |
---|
388 | \end{lstlisting} |
---|
389 | That is, any successful compilation produces a labelled program that |
---|
390 | has identical behaviour to the original, so long as there is no |
---|
391 | `undefined behaviour'. |
---|
392 | |
---|
393 | Note that this provides full functional correctness, including |
---|
394 | preservation of (non-)termination. The intensional result above does |
---|
395 | not do this directly --- it does not guarantee the same result or same |
---|
396 | termination. There are two mitigating factors, however: first, to |
---|
397 | prove the intensional property you need local simulation results that |
---|
398 | can be pieced together to form full behavioural equivalence, only time |
---|
399 | constraints have prevented us from doing so. Second, if we wish to |
---|
400 | confirm a result, termination, or non-termination we could add an |
---|
401 | observable witness, such as a function that is only called if the |
---|
402 | correct result is given. The intensional result guarantees that the |
---|
403 | observable witness is preserved, so the program must behave correctly. |
---|
404 | |
---|
405 | \section{Intermediate goals for the front-end} |
---|
406 | \label{sec:fegoals} |
---|
407 | |
---|
408 | The essential parts of the intensional proof were outlined during work |
---|
409 | on a toy |
---|
410 | compiler~\cite{d2.1,springerlink:10.1007/978-3-642-32469-7_3}. These |
---|
411 | are |
---|
412 | \begin{enumerate} |
---|
413 | \item functional correctness, in particular preserving the trace of |
---|
414 | cost labels, |
---|
415 | \item the \emph{soundness} and \emph{precision} of the cost labelling |
---|
416 | on the object code, and |
---|
417 | \item the timing analysis on the object code produces a correct |
---|
418 | mapping from cost labels to time. |
---|
419 | \end{enumerate} |
---|
420 | |
---|
421 | However, that toy development did not include function calls. For the |
---|
422 | full \cerco{} compiler we also need to maintain the invariant that |
---|
423 | functions return to the correct program location in the caller, as we |
---|
424 | mentioned in the previous section. During work on the back-end timing |
---|
425 | analysis (describe in more detail in the companion deliverable, D4.4) |
---|
426 | the notion of a \emph{structured trace} was developed to enforce this |
---|
427 | return property, and also most of the cost labelling properties too. |
---|
428 | |
---|
429 | \begin{figure} |
---|
430 | \begin{center} |
---|
431 | \includegraphics[width=0.5\linewidth]{compiler.pdf} |
---|
432 | \end{center} |
---|
433 | \caption{The compiler and proof outline} |
---|
434 | \label{fig:compiler} |
---|
435 | \end{figure} |
---|
436 | |
---|
437 | Jointly, we generalised the structured traces to apply to any of the |
---|
438 | intermediate languages with some idea of program counter. This means |
---|
439 | that they are introduced part way through the compiler, see |
---|
440 | Figure~\ref{fig:compiler}. Proving that a structured trace can be |
---|
441 | constructed at \textsf{RTLabs} has several virtues: |
---|
442 | \begin{itemize} |
---|
443 | \item This is the first language where every operation has its own |
---|
444 | unique, easily addressable, statement. |
---|
445 | \item Function calls and returns are still handled implicitly in the |
---|
446 | language and so the structural properties are ensured by the |
---|
447 | semantics. |
---|
448 | \item Many of the back-end languages from \textsf{RTL} share a common |
---|
449 | core set of definitions, and using structured traces throughout |
---|
450 | increases this uniformity. |
---|
451 | \end{itemize} |
---|
452 | |
---|
453 | \begin{figure} |
---|
454 | \begin{center} |
---|
455 | \includegraphics[width=0.6\linewidth]{strtraces.pdf} |
---|
456 | \end{center} |
---|
457 | \caption{Nesting of functions in structured traces} |
---|
458 | \label{fig:strtrace} |
---|
459 | \end{figure} |
---|
460 | A structured trace is a mutually inductive data type which |
---|
461 | contains the steps from a normal program trace, but arranged into a |
---|
462 | nested structure which groups entire function calls together and |
---|
463 | aggregates individual steps between cost labels (or between the final |
---|
464 | cost label and the return from the function), see |
---|
465 | Figure~\ref{fig:strtrace}. This captures the idea that the cost labels |
---|
466 | only represent costs \emph{within} a function --- calls to other |
---|
467 | functions are accounted for in the nested trace for their execution, and we |
---|
468 | can locally regard function calls as a single step. |
---|
469 | |
---|
470 | These structured traces form the core part of the intermediate results |
---|
471 | that we must prove so that the back-end can complete the main |
---|
472 | intensional result stated above. In full, we provide the back-end |
---|
473 | with |
---|
474 | \begin{enumerate} |
---|
475 | \item A normal trace of the \textbf{prefix} of the program's execution |
---|
476 | before reaching the measurable subtrace. (This needs to be |
---|
477 | preserved so that we know that the stack space consumed is correct, |
---|
478 | and to set up the simulation results.) |
---|
479 | \item The \textbf{structured trace} corresponding to the measurable |
---|
480 | subtrace. |
---|
481 | \item An additional property about the structured trace that no |
---|
482 | `program counter' is \textbf{repeated} between cost labels. Together with |
---|
483 | the structure in the trace, this takes over from showing that |
---|
484 | cost labelling is sound and precise. |
---|
485 | \item A proof that the \textbf{observables} have been preserved. |
---|
486 | \item A proof that the \textbf{stack limit} is still observed by the prefix and |
---|
487 | the structure trace. (This is largely a consequence of the |
---|
488 | preservation of observables.) |
---|
489 | \end{enumerate} |
---|
490 | |
---|
491 | Following the outline in Figure~\ref{fig:compiler}, we will first deal |
---|
492 | with the transformations in \textsf{Clight} that produce the source |
---|
493 | program with cost labels, then show that measurable traces can be |
---|
494 | lifted to \textsf{RTLabs}, and finally that we can construct the |
---|
495 | properties listed above ready for the back-end proofs. |
---|
496 | |
---|
497 | \section{Input code to cost labelled program} |
---|
498 | |
---|
499 | As explained on page~\pageref{page:switchintro}, the costs of complex |
---|
500 | C \lstinline[language=C]'switch' statements cannot be represented with |
---|
501 | the simple labelling. Our first pass replaces these statements with |
---|
502 | simpler C code, allowing our second pass to perform the cost |
---|
503 | labelling. We show that the behaviour of programs is unchanged by |
---|
504 | these passes. |
---|
505 | |
---|
506 | TODO: both give one-step-sim-by-many forward sim results; switch |
---|
507 | removal tricky, uses aux var to keep result of expr, not central to |
---|
508 | intensional correctness so curtailed proof effort once reasonable |
---|
509 | level of confidence in code gained; labelling much simpler; don't care |
---|
510 | what the labels are at this stage, just need to know when to go |
---|
511 | through extra steps. Rolled up into a single result with a cofixpoint |
---|
512 | to obtain coinductive statement of equivalence (show). |
---|
513 | |
---|
514 | \section{Finding corresponding measurable subtraces} |
---|
515 | |
---|
516 | There follow the three main passes of the front-end: |
---|
517 | \begin{enumerate} |
---|
518 | \item simplification of casts in \textsf{Clight} code |
---|
519 | \item \textsf{Clight} to \textsf{Cminor} translation, performing stack |
---|
520 | variable allocation and simplifying control structures |
---|
521 | \item transformation to \textsf{RTLabs} control flow graph |
---|
522 | \end{enumerate} |
---|
523 | \todo{I keep mentioning forward sim results - I probably ought to say |
---|
524 | something about determinancy} We have taken a common approach to |
---|
525 | each pass: first we build (or axiomatise) forward simulation results |
---|
526 | that are similar to normal compiler proofs, but slightly more |
---|
527 | fine-grained so that we can see that the call structure and relative |
---|
528 | placement of cost labels is preserved. |
---|
529 | |
---|
530 | Then we instantiate a general result which shows that we can find a |
---|
531 | \emph{measurable} subtrace in the target of the pass that corresponds |
---|
532 | to the measurable subtrace in the source. By repeated application of |
---|
533 | this result we can find a measurable subtrace of the execution of the |
---|
534 | \textsf{RTLabs} code, suitable for the construction of a structured |
---|
535 | trace (see Section~\ref{sec:structuredtrace}. This is essentially an |
---|
536 | extra layer on top of the simulation proofs that provides us with the |
---|
537 | extra information required for our intensional correctness proof. |
---|
538 | |
---|
539 | \subsection{Generic measurable subtrace lifting proof} |
---|
540 | |
---|
541 | Our generic proof is parametrised on a record containing small-step |
---|
542 | semantics for the source and target language, a classification of |
---|
543 | states (the same form of classification is used when defining |
---|
544 | structured traces), a simulation relation which loosely respects the |
---|
545 | classification and cost labelling \todo{this isn't very helpful} and |
---|
546 | four simulation results: |
---|
547 | \begin{enumerate} |
---|
548 | \item a step from a `normal' state (which is not classified as a call |
---|
549 | or return) which is not a cost label is simulated by zero or more |
---|
550 | `normal' steps; |
---|
551 | \item a step from a `call' state followed by a cost label step is |
---|
552 | simulated by a step from a `call' state, a corresponding label step, |
---|
553 | then zero or more `normal' steps; |
---|
554 | \item a step from a `call' state not followed by a cost label |
---|
555 | similarly (note that this case cannot occur in a well-labelled |
---|
556 | program, but we do not have enough information locally to exploit |
---|
557 | this); and |
---|
558 | \item a cost label step is simulated by a cost label step. |
---|
559 | \end{enumerate} |
---|
560 | Finally, we need to know that a successfully translated program will |
---|
561 | have an initial state in the simulation relation with the original |
---|
562 | program's initial state. |
---|
563 | |
---|
564 | \begin{figure} |
---|
565 | \begin{center} |
---|
566 | \includegraphics[width=0.5\linewidth]{meassim.pdf} |
---|
567 | \end{center} |
---|
568 | \caption{Tiling of simulation for a measurable subtrace} |
---|
569 | \label{fig:tiling} |
---|
570 | \end{figure} |
---|
571 | |
---|
572 | To find the measurable subtrace in the target program's execution we |
---|
573 | walk along the original program's execution trace applying the |
---|
574 | appropriate simulation result by induction on the number of steps. |
---|
575 | While the number of steps taken varies, the overall structure is |
---|
576 | preserved, as illustrated in Figure~\ref{fig:tiling}. By preserving |
---|
577 | the structure we also maintain the same intensional observables. One |
---|
578 | delicate point is that the cost label following a call must remain |
---|
579 | directly afterwards\footnote{The prototype compiler allowed some |
---|
580 | straight-line code to appear before the cost label until a later |
---|
581 | stage of the compiler, but we must move the requirement forward to |
---|
582 | fit with the structured traces.} |
---|
583 | % Damn it, I should have just moved the cost label forwards in RTLabs, |
---|
584 | % like the prototype does in RTL to ERTL; the result would have been |
---|
585 | % simpler. Or was there some reason not to do that? |
---|
586 | (both in the program code and in the execution trace), even if we |
---|
587 | introduce extra steps, for example to store parameters in memory in |
---|
588 | \textsf{Cminor}. Thus we have a version of the call simulation |
---|
589 | that deals with both in one result. |
---|
590 | |
---|
591 | In addition to the subtrace we are interested in measuring we must |
---|
592 | also prove that the earlier part of the trace is also preserved in |
---|
593 | order to use the simulation from the initial state. It also |
---|
594 | guarantees that we do not run out of stack space before the subtrace |
---|
595 | we are interested in. The lemmas for this prefix and the measurable |
---|
596 | subtrace are similar, following the pattern above. However, the |
---|
597 | measurable subtrace also requires us to rebuild the termination |
---|
598 | proof. This is defined recursively: |
---|
599 | \label{prog:terminationproof} |
---|
600 | \begin{lstlisting}[language=matita] |
---|
601 | let rec will_return_aux C (depth:nat) |
---|
602 | (trace:list (cs_state … C × trace)) on trace : bool := |
---|
603 | match trace with |
---|
604 | [ nil $\Rightarrow$ false |
---|
605 | | cons h tl $\Rightarrow$ |
---|
606 | let $\langle$s,tr$\rangle$ := h in |
---|
607 | match cs_classify C s with |
---|
608 | [ cl_call $\Rightarrow$ will_return_aux C (S depth) tl |
---|
609 | | cl_return $\Rightarrow$ |
---|
610 | match depth with |
---|
611 | [ O $\Rightarrow$ match tl with [ nil $\Rightarrow$ true | _ $\Rightarrow$ false ] |
---|
612 | | S d $\Rightarrow$ will_return_aux C d tl |
---|
613 | ] |
---|
614 | | _ $\Rightarrow$ will_return_aux C depth tl |
---|
615 | ] |
---|
616 | ]. |
---|
617 | \end{lstlisting} |
---|
618 | The \lstinline'depth' is the number of return states we need to see |
---|
619 | before we have returned to the original function (initially zero) and |
---|
620 | \lstinline'trace' the measurable subtrace obtained from the running |
---|
621 | the semantics for the correct number of steps. This definition |
---|
622 | unfolds tail recursively for each step, and once the corresponding |
---|
623 | simulation result has been applied a new one for the target can be |
---|
624 | asserted by unfolding and applying the induction hypothesis on the |
---|
625 | shorter trace. |
---|
626 | |
---|
627 | This then gives us an overall result for any simulation fitting the |
---|
628 | requirements above (contained in the \lstinline'meas_sim' record): |
---|
629 | \begin{lstlisting}[language=matita] |
---|
630 | theorem measured_subtrace_preserved : |
---|
631 | $\forall$MS:meas_sim. |
---|
632 | $\forall$p1,p2,m,n,stack_cost,max. |
---|
633 | ms_compiled MS p1 p2 $\rightarrow$ |
---|
634 | measurable (ms_C1 MS) p1 m n stack_cost max $\rightarrow$ |
---|
635 | $\exists$m',n'. |
---|
636 | measurable (ms_C2 MS) p2 m' n' stack_cost max $\wedge$ |
---|
637 | observables (ms_C1 MS) p1 m n = observables (ms_C2 MS) p2 m' n'. |
---|
638 | \end{lstlisting} |
---|
639 | The stack space requirement that is embedded in \lstinline'measurable' |
---|
640 | is a consequence of the preservation of observables. |
---|
641 | |
---|
642 | TODO: how to deal with untidy edges wrt to sim rel; anything to |
---|
643 | say about obs? |
---|
644 | |
---|
645 | TODO: say something about termination measures; cost labels are |
---|
646 | statements/exprs in these languages; split call/return gives simpler |
---|
647 | simulations |
---|
648 | |
---|
649 | \subsection{Simulation results for each pass} |
---|
650 | |
---|
651 | \todo{don't use loop structures from CompCert, go straight to jumps} |
---|
652 | |
---|
653 | \paragraph{Cast simplification} |
---|
654 | |
---|
655 | The parser used in Cerco introduces a lot of explicit type casts. If left |
---|
656 | as they are, these casts can greatly hamper the quality of the generated |
---|
657 | code -- especially as the architecture we consider is an $8$ bit one. |
---|
658 | In \textsf{Clight}, casts are expressions. Hence, most of the work of |
---|
659 | this transformation proceeds on expressions, and the one-step simulation |
---|
660 | result one those is easily lifted to statement evaluations. |
---|
661 | |
---|
662 | \todo{introduce the explicit type signature of the functions below ?} |
---|
663 | |
---|
664 | The tranformation proceeds by recursively trying to coerce an expression to |
---|
665 | a particular type, which is in practice a integer type smaller than the |
---|
666 | original one. This functionality is implemented by two mutually recursive |
---|
667 | functions: \textbf{simplify\_expr} and \textbf{simplify\_inside}. The latter |
---|
668 | acts as a wrapper for the first one. Whenever \textbf{simplify\_inside} |
---|
669 | encounters a \textbf{Ecast} expression, it tries to coerce the sub-expression |
---|
670 | to the desired type using \textbf{simplify\_expr}, which tries to perform the |
---|
671 | actual coercion. In return, \textbf{simplify\_expr} calls back |
---|
672 | \textbf{simplify\_inside} in some particular positions, where we decided to |
---|
673 | be conservative in order to simplify the proofs. However, the current design |
---|
674 | allows to incrementally revert to a more aggressive version, by replacing |
---|
675 | recursive calls to \textbf{simplify\_inside} by calls to \textbf{simplify\_expr} |
---|
676 | \emph{and} proving the corresponding invariants -- where possible. |
---|
677 | |
---|
678 | The main problem encountered with this particular pass was dealing with inconsistently |
---|
679 | typed programs, a canonical case being a particular integer constant of a certain |
---|
680 | size typed with another size. This prompted the need to introduce numerous |
---|
681 | type checks, complexifying both the implementation and the proof. |
---|
682 | |
---|
683 | \paragraph{Switch removal} |
---|
684 | |
---|
685 | Our intermediate languages have no jump tables. Hence, we have to compile the |
---|
686 | \lstinline[language=C]'switch' constructs away before going from \textsf{Clight} to |
---|
687 | \textsf{Cminor}. Note that this is not necessarily a bad decision performance-wise |
---|
688 | for small switches. For instance, compilers such as GCC introduce balanced trees |
---|
689 | of ``if-then-else'' constructs. However, our implementation strategy is much simpler. |
---|
690 | Let us consider the following input statement. |
---|
691 | |
---|
692 | \begin{lstlisting}[language=C] |
---|
693 | switch(e) { |
---|
694 | case v1: |
---|
695 | stmt1; |
---|
696 | case v2: |
---|
697 | stmt2; |
---|
698 | default: |
---|
699 | stmt_default; |
---|
700 | } |
---|
701 | \end{lstlisting} |
---|
702 | |
---|
703 | Note that stmt1, stmt2, \ldots stmt\_default may contain "break" statements, |
---|
704 | which have the effect of exiting the switch statement. In the absence of break, |
---|
705 | the execution falls through each case sequentially. In our current implementation, |
---|
706 | we produce an equivalent sequence of ``if-then'' chained by gotos: |
---|
707 | \begin{lstlisting}[language=C] |
---|
708 | fresh = e; |
---|
709 | if(fresh == v1) { |
---|
710 | $\llbracket$stmt1$\rrbracket$; |
---|
711 | goto lbl_case2; |
---|
712 | }; |
---|
713 | if(fresh == v2) { |
---|
714 | lbl_case2: |
---|
715 | $\llbracket$stmt2;$\rrbracket$ |
---|
716 | goto lbl_case2; |
---|
717 | }; |
---|
718 | $\llbracket$stmt_default$\rrbracket$; |
---|
719 | exit_label: |
---|
720 | \end{lstlisting} |
---|
721 | |
---|
722 | The proof had to tackle the following points: |
---|
723 | \begin{itemize} |
---|
724 | \item the source and target memories are not the same (cf. fresh variable), |
---|
725 | \item the flow of control is changed in a non-local way (e.g. \textbf{goto} |
---|
726 | instead of \textbf{break}). |
---|
727 | \end{itemize} |
---|
728 | |
---|
729 | In order to tackle the first point, we implemented a version of memory |
---|
730 | extensions similar to Compcert's. What has been done is the simulation proof |
---|
731 | for all ``easy'' cases, that do not interact with the switch removal per se, |
---|
732 | plus a bit of the switch case. This comprises propagating the memory extension |
---|
733 | through each statement (except switch), as well as various invariants that |
---|
734 | are needed for the switch case (in particular, freshness hypotheses). The |
---|
735 | details of the evaluation process for the source switch statement and its |
---|
736 | target counterpart can be found in the file \textbf{switchRemoval.ma}, |
---|
737 | along more details on the transformation itself. |
---|
738 | |
---|
739 | Proving the correctness of the second point would require reasoning on the |
---|
740 | semantics of \lstinline[language=C]'goto' statements. In the \textsf{Clight} |
---|
741 | semantics, this is implemented as a function-wide lookup of the target label. |
---|
742 | The invariant we would need is the fact that a global label lookup on a freshly |
---|
743 | created goto is equivalent to a local lookup. This would in turn require the |
---|
744 | propagation of some freshness hypotheses on labels. For reasons expressed above, |
---|
745 | we decided to omit this part of the correctness proof. |
---|
746 | |
---|
747 | \paragraph{Clight to Cminor} |
---|
748 | |
---|
749 | This pass is the last one operating on the Clight intermediate language. |
---|
750 | Its input is a full \textsf{Clight} program, and its output is a \textsf{Cminor} |
---|
751 | program. Note that we do not use an equivalent of the C\#minor language: we |
---|
752 | translate directly to Cminor. This presents the advantage of not requiring the |
---|
753 | special loop constructs, nor the explicit block structure. Another salient |
---|
754 | point of our approach is that a significant part of the properties needed for |
---|
755 | the simulation proof were directly encoded in dependently typed translation |
---|
756 | functions. This concerns more precisely freshness conditions and well-typedness |
---|
757 | conditions. The main effects of the transformation from \textsf{Clight} to |
---|
758 | \textsf{Cminor} are listed below. |
---|
759 | |
---|
760 | \begin{itemize} |
---|
761 | \item Variables are classified as being either globals, stack-allocated |
---|
762 | locals or potentially register-allocated locals. The value of register-allocated |
---|
763 | local variables is moved out of the modelled memory and stored in a |
---|
764 | dedicated environment. |
---|
765 | \item In Clight, each local variable has a dedicated memory block, whereas |
---|
766 | stack-allocated locals are bundled together on a function-by-function basis. |
---|
767 | \item Loops are converted to jumps. |
---|
768 | \end{itemize} |
---|
769 | |
---|
770 | The first two points require memory injections which are more flexible that those |
---|
771 | needed in the switch removal case. In the remainder of this section, we briefly |
---|
772 | discuss our implementation of memory injections, and then the simulation proof. |
---|
773 | |
---|
774 | \subparagraph{Memory injections} |
---|
775 | |
---|
776 | Our memory injections are modelled after the work of Blazy \& Leroy. |
---|
777 | However, the corresponding paper is based on the first version of the |
---|
778 | Compcert memory model, whereas we use a much more concrete model, allowing byte-level |
---|
779 | manipulations (as in the later version of Compcert's memory model). We proved |
---|
780 | roughly 80 \% of the required lemmas. Some difficulties encountered were notably |
---|
781 | due to some too relaxed conditions on pointer validity (problem fixed during development). |
---|
782 | Some more conditions had to be added to take care of possible overflows when converting |
---|
783 | from \textbf{Z} block bounds to $16$ bit pointer offsets (in pratice, these overflows |
---|
784 | only occur in edge cases that are easily ruled out -- but this fact is not visible |
---|
785 | in memory injections). Concretely, some of the lemmas on the preservation of simulation of |
---|
786 | loads after writes were axiomatised, for lack of time. |
---|
787 | |
---|
788 | \subparagraph{Simulation proof} |
---|
789 | |
---|
790 | The correction proof for this transformation was not terminated. We proved the |
---|
791 | simulation result for expressions and for some subset of the critical statement cases. |
---|
792 | Notably lacking are the function entry and exit, where the memory injection is |
---|
793 | properly set up. As can be guessed, a significant amount of work has to be performed |
---|
794 | to show the conservation of all invariants at each simulation step. |
---|
795 | |
---|
796 | \todo{list cases, explain while loop, explain labeling problem} |
---|
797 | |
---|
798 | \section{Checking cost labelling properties} |
---|
799 | \label{sec:costchecks} |
---|
800 | |
---|
801 | Ideally, we would provide proofs that the cost labelling pass always |
---|
802 | produced programs that are soundly and precisely labelled and that |
---|
803 | each subsequent pass preserves these properties. This would match our |
---|
804 | use of dependent types to eliminate impossible sources of errors |
---|
805 | during compilation, in particular retaining intermediate language type |
---|
806 | information. |
---|
807 | |
---|
808 | However, given the limited amount of time available we realised that |
---|
809 | implementing a compile-time check for a sound and precise labelling of |
---|
810 | the \textsf{RTLabs} intermediate code would reduce the proof burden |
---|
811 | considerably. This is similar in spirit to the use of translation |
---|
812 | validation in certified compilation\todo{Cite some suitable work |
---|
813 | here}, which makes a similar trade-off between the potential for |
---|
814 | compile-time failure and the volume of proof required. |
---|
815 | |
---|
816 | The check cannot be pushed into a later stage of the compiler because |
---|
817 | much of the information is embedded into the structured traces. |
---|
818 | However, if an alternative method was used to show that function |
---|
819 | returns in the compiled code are sufficiently well-behaved, then we |
---|
820 | could consider pushing the cost property checks into the timing |
---|
821 | analysis itself. We leave this as a possible area for future work. |
---|
822 | |
---|
823 | \subsection{Implementation and correctness} |
---|
824 | |
---|
825 | For a cost labelling to be sound and precise we need a cost label at |
---|
826 | the start of each function, after each branch and at least one in |
---|
827 | every loop. The first two parts are trivial to check by examining the |
---|
828 | code. In \textsf{RTLabs} the last part is specified by saying |
---|
829 | that there is a bound on the number of successive instruction nodes in |
---|
830 | the CFG that you can follow before you encounter a cost label, and |
---|
831 | checking this is more difficult. |
---|
832 | |
---|
833 | The implementation works through the set of nodes in the graph, |
---|
834 | following successors until a cost label is found or a label-free cycle |
---|
835 | is discovered (in which case the property does not hold and we stop). |
---|
836 | This is made easier by the prior knowledge that any branch is followed |
---|
837 | by cost labels, so we do not need to search each branch. When a label |
---|
838 | is found, we remove the chain from the set and continue from another |
---|
839 | node in the set until it is empty, at which point we know that there |
---|
840 | is a bound for every node in the graph. |
---|
841 | |
---|
842 | Directly reasoning about the function that implements this would be |
---|
843 | rather awkward, so an inductive specification of a single step of its |
---|
844 | behaviour was written and proved to match the implementation. This |
---|
845 | was then used to prove the implementation sound and complete. |
---|
846 | |
---|
847 | While we have not attempted to proof that the cost labelled properties |
---|
848 | are established and preserved earlier in the compiler, we expect that |
---|
849 | the effort for the \textsf{Cminor} to \textsf{RTLabs} would be similar |
---|
850 | to the work outlined above, because it involves the change from |
---|
851 | requiring a cost label at particular positions to requiring cost |
---|
852 | labels to break loops in the CFG. As there are another three passes |
---|
853 | to consider (including the labelling itself), we believe that using |
---|
854 | the check above is much simpler overall. |
---|
855 | |
---|
856 | % TODO? Found some Clight to Cminor bugs quite quickly |
---|
857 | |
---|
858 | \section{Existence of a structured trace} |
---|
859 | \label{sec:structuredtrace} |
---|
860 | |
---|
861 | \emph{Structured traces} enrich the execution trace of a program by |
---|
862 | nesting function calls in a mixed-step style\todo{Can I find a |
---|
863 | justification for mixed-step} and embedding the cost properties of |
---|
864 | the program. It was originally designed to support the proof of |
---|
865 | correctness for the timing analysis of the object code in the |
---|
866 | back-end, then generalised to provide a common structure to use from |
---|
867 | the end of the front-end to the object code. See |
---|
868 | Figure~\ref{fig:strtrace} on page~\pageref{fig:strtrace} |
---|
869 | for an illustration of a structured trace. |
---|
870 | |
---|
871 | To make the definition generic we abstract over the semantics of the |
---|
872 | language, |
---|
873 | \begin{lstlisting}[language=matita] |
---|
874 | record abstract_status : Type[1] := |
---|
875 | { as_status :> Type[0] |
---|
876 | ; as_execute : relation as_status |
---|
877 | ; as_pc : DeqSet |
---|
878 | ; as_pc_of : as_status $\rightarrow$ as_pc |
---|
879 | ; as_classify : as_status $\rightarrow$ status_class |
---|
880 | ; as_label_of_pc : as_pc $\rightarrow$ option costlabel |
---|
881 | ; as_after_return : ($\Sigma$s:as_status. as_classify s = cl_call) $\rightarrow$ as_status $\rightarrow$ Prop |
---|
882 | ; as_result: as_status $\rightarrow$ option int |
---|
883 | ; as_call_ident : ($\Sigma$s:as_status.as_classify s = cl_call) $\rightarrow$ ident |
---|
884 | ; as_tailcall_ident : ($\Sigma$s:as_status.as_classify s = cl_tailcall) $\rightarrow$ ident |
---|
885 | }. |
---|
886 | \end{lstlisting} |
---|
887 | which gives a type of states, an execution relation, some notion of |
---|
888 | program counters with decidable equality, the classification of states |
---|
889 | and functions to extract the observable intensional information (cost |
---|
890 | labels and the identity of functions that are called). The |
---|
891 | \lstinline'as_after_return' property links the state before a function |
---|
892 | call with the state after return, providing the evidence that |
---|
893 | execution returns to the correct place. The precise form varies |
---|
894 | between stages; in \textsf{RTLabs} it insists the CFG, the pointer to |
---|
895 | the CFG node to execute next, and some call stack information is |
---|
896 | preserved. |
---|
897 | |
---|
898 | The structured traces are defined using three mutually inductive |
---|
899 | types. The core data structure is \lstinline'trace_any_label', which |
---|
900 | captures some straight-line execution until the next cost label or |
---|
901 | function return. Each function call is embedded as a single step, |
---|
902 | with its own trace nested inside and the before and after states |
---|
903 | linked by \lstinline'as_after_return', and states classified as a |
---|
904 | `jump' (in particular branches) must be followed by a cost label. |
---|
905 | |
---|
906 | The second type, \lstinline'trace_label_label', is a |
---|
907 | \lstinline'trace_any_label' where the initial state is cost labelled. |
---|
908 | Thus a trace in this type identifies a series of steps whose cost is |
---|
909 | entirely accounted for by the label at the start. |
---|
910 | |
---|
911 | Finally, \lstinline'trace_label_return' is a sequence of |
---|
912 | \lstinline'trace_label_label' values which end in the return from the |
---|
913 | function. These correspond to a measurable subtrace, and in |
---|
914 | particular include executions of an entire function call (and so are |
---|
915 | used for the nested calls in \lstinline'trace_any_label'). |
---|
916 | |
---|
917 | \subsection{Construction} |
---|
918 | |
---|
919 | The core part of the construction of a fixed trace is to use the proof |
---|
920 | of termination from the measurable trace (defined on |
---|
921 | page~\pageref{prog:terminationproof}) to `fold up' the execution into |
---|
922 | the nested form. |
---|
923 | |
---|
924 | TODO: design, basic structure from termination proof, how cost |
---|
925 | labelling props are baked in; unrepeating PCs, remainder of sound |
---|
926 | labellings; coinductive version for whole programs, reason/relevance, |
---|
927 | use of em (maybe a general comment about uses of classical reasoning |
---|
928 | in development) |
---|
929 | |
---|
930 | \section{Conclusion} |
---|
931 | |
---|
932 | TODO |
---|
933 | |
---|
934 | TODO: appendix on code layout? |
---|
935 | |
---|
936 | \bibliographystyle{plain} |
---|
937 | \bibliography{report} |
---|
938 | |
---|
939 | \end{document} |
---|