source: Papers/itp-2013/ccexec.tex @ 2611

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


File size: 51.3 KB
9% NB: might be worth removing this if changing class in favour of
10% a built-in definition.
14  {keywords={Definition,Lemma,Theorem,Remark,Qed,Save,Inductive,Record},
15   morekeywords={[2]if,then,else,forall,Prop,match,with,end,let},
16  }
19  morekeywords={int8_t}
23        keywordstyle=\color{red}\bfseries,
24        keywordstyle=[2]\color{blue},
25        commentstyle=\color{green},
26        stringstyle=\color{blue},
27        showspaces=false,showstringspaces=false,
28        xleftmargin=1em}
32\title{Certification of the Preservation of Structure by a Compiler's Back-end Pass\thanks{The project CerCo acknowledges the financial support of the Future and
33Emerging Technologies (FET) programme within the Seventh Framework
34Programme for Research of the European Commission, under FET-Open grant
35number: 243881}}
36\author{Paolo Tranquilli \and Claudio Sacerdoti Coen}
37\institute{Department of Computer Science and Engineering, University of Bologna,\\\email{}, \email{}}
40The labelling approach is a technique to lift cost models for non-functional
41properties of programs from the object code to the source code. It is based
42on the preservation of the structure of the high level program in every
43intermediate language used by the compiler. Such structure is captured by
44observables that are added to the semantics and that needs to be preserved
45by the forward simulation proof of correctness of the compiler. Additional
46special observables are required for function calls. In this paper we
47present a generic forward simulation proof that preserves all these observables.
48The proof statement is based on a new mechanized semantics that traces the
49structure of execution when the language is unstructured. The generic semantics
50and simulation proof have been mechanized in the interactive theorem prover
56The \emph{labelling approach} has been introduced in~\cite{easylabelling} as
57a technique to \emph{lift} cost models for non-functional properties of programs
58from the object code to the source code. Examples of non-functional properties
59are execution time, amount of stack/heap space consumed and energy required for
60communication. The basic idea of the approach is that it is impossible to
61provide a \emph{uniform} cost model for an high level language that is preserved
62\emph{precisely} by a compiler. For instance, two instances of an assignment
63$x = y$ in the source code can be compiled very differently according to the
64place (registers vs stack) where $x$ and $y$ are stored at the moment of
65execution. Therefore a precise cost model must assign a different cost
66to every occurrence, and the exact cost can only be known after compilation.
68According to the labeling approach, the compiler is free to compile and optimize
69the source code without any major restriction, but it must keep trace
70of what happens to basic blocks during the compilation. The cost model is
71then computed on the object code. It assigns a cost to every basic block.
72Finally, the compiler propagates back the cost model to the source level,
73assigning a cost to each basic block of the source code.
75Implementing the labelling approach in a certified compiler
76allows to reason formally on the high level source code of a program to prove
77non-functional properties that are granted to be preserved by the compiler
78itself. The trusted code base is then reduced to 1) the interactive theorem
79prover (or its kernel) used in the certification of the compiler and
802) the software used to certify the property on the source language, that
81can be itself certified further reducing the trusted code base.
82In~\cite{easylabelling} the authors provide an example of a simple
83certified compiler that implements the labelling approach for the
84imperative \texttt{While} language~\cite{while}, that does not have
85pointers and function calls.
87The labelling approach has been shown to scale to more interesting scenarios.
88In particular in~\cite{functionallabelling} it has been applied to a functional
89language and in~\cite{loopoptimizations} it has been shown that the approach
90can be slightly complicated to handle loop optimizations and, more generally,
91program optimizations that do not preserve the structure of basic blocks.
92On-going work also shows that the labelling approach is also compatible with
93the complex analyses required to obtain a cost model for object code
94on processors that implement advanced features like pipelining, superscalar
95architectures and caches.
97In the European Project CerCo (Certified Complexity~\footnote{\url{}})~\cite{cerco} we are certifying a labelling approach based compiler for a large subset of C to
988051 object code. The compiler is
99moderately optimizing and implements a compilation chain that is largely
100inspired to that of CompCert~\cite{compcert1,compcert2}. Compared to work done in~\cite{easylabelling}, the main novely and source of difficulties is due to the presence
101of function calls. Surprisingly, the addition of function calls require a
102rivisitation of the proof technique given in~\cite{easylabelling}. In
103particular, at the core of the labelling approach there is a forward
104simulation proof that, in the case of \texttt{While}, is only minimally
105more complex than the proof required for the preservation of the
106functional properties only. In the case of a programming language with
107function calls, instead, it turns out that the forward simulation proof for
108the back-end languages must grant a whole new set of invariants.
110In this paper we present a formalization in the Matita interactive theorem
111prover of a generic version of the simulation proof required for unstructured
112languages. All back-end languages of the CerCo compiler are unstructured
113languages, so the proof covers half of the correctness of the compiler.
114The statement of the generic proof is based on a new semantics
115for imperative unstructured languages that is based on \emph{structured
116traces} and that restores the preservation of structure in the observables of
117the semantics. The generic proof allows to almost completely split the
118part of the simulation that deals with functional properties only from the
119part that deals with the preservation of structure.
121The plan of this paper is the following. In Section~\ref{labelling} we
122sketch the labelling method and the problems derived from the application
123to languages with function calls. In Section~\ref{semantics} we introduce
124a generic description of an unstructured imperative language and the
125corresponding structured traces (the novel semantics). In
126Section~\ref{simulation} we describe the forward simulation proof.
127Conclusions and future works are in Section~\ref{conclusions}
129\section{The labelling approach}
131We briefly sketch here a simplified version of the labelling approach as
132introduced in~\cite{easylabelling}. The simplification strengthens the
133sufficient conditions given in~\cite{easylabelling} to allow a simpler
134explanation. The simplified conditions given here are also used in the
135CerCo compiler to simplify the proof.
137Let $\mathcal{P}$ be a programming language whose semantics is given in
138terms of observables: a run of a program yields a finite or infinite
139stream of observables. We also assume for the time being that function
140calls are not available in $\mathcal{P}$. We want to associate a cost
141model to a program $P$ written in $\mathcal{P}$. The first step is to
142extend the syntax of $\mathcal{P}$ with a new construct $\texttt{emit L}$
143where $L$ is a label distinct from all observables of $\mathcal{P}$.
144The semantics of $\texttt{emit L}$ is the emission of the observable
145\texttt{L} that is meant to signal the beginning of a basic block.
147There exists an automatic procedure that injects into the program $P$ an
148$\texttt{emit L}$ at the beginning of each basic block, using a fresh
149\texttt{L} for each block. In particular, the bodies of loops, both branches
150of \texttt{if-then-else}s and the targets of \texttt{goto}s must all start
151with an emission statement.
153Let now $C$ be a compiler from $\mathcal{P}$ to the object code $\mathcal{M}$,
154that is organized in passes. Let $\mathcal{Q}_i$ be the $i$-th intermediate
155language used by the compiler. We can easily extend every
156intermediate language (and its semantics) with an $\texttt{emit L}$ statement
157as we did for $\mathcal{P}$. The same is possible for $\mathcal{M}$ too, with
158the additional difficulty that the syntax of object code is given as a
159sequence of bytes. The injection of an emission statement in the object code
160can be done using a map that maps two consecutive code addresses with the
161statement. The intended semantics is that, if $(pc_1,pc_2) \mapsto \texttt{emit L}$ then the observable \texttt{L} is emitted after the execution of the
162instruction stored at $pc_1$ and before the execution of the instruction
163stored at $pc_2$. The two program counters are necessary because the
164instruction stored at $pc_1$ can have multiple possible successors (e.g.
165in case of a conditional branch or an indirect call). Dually, the instruction
166stored at $pc_2$ can have multiple possible predecessors (e.g. if it is the
167target of a jump).
169The compiler, to be functionally correct, must preserve the observational
170equivalence, i.e. executing the program after each compiler pass should
171yield the same stream of observables. After the injection of emission
172statements, observables now capture both functional and non-functional
174This correctness property is called in the litterature a forward simulation
175and is sufficient for correctness when the target language is
177We also require a stronger, non-functional preservation property: after each
178pass all basic blocks must start with an emission statement, and all labels
179\texttt{L} must be unique.
181Now let $M$ be the object code obtained for the program $P$. Let us suppose
182that we can statically inspect the code $M$ and associate to each basic block
183a cost (e.g. the number of clock cycles required to execute all instructions
184in the basic block, or an upper bound to that time). Every basic block is
185labelled with an unique label \texttt{L}, thus we can actually associate the
186cost to \texttt{L}. Let call it $k(\texttt{L})$.
188The function $k$ is defined as the cost model for the object code control
189blocks. It can be equally used as well as the cost model for the source
190control blocks. Indeed, if the semantics of $P$ is the stream
191$L_1 L_2 \ldots$, then, because of forward simulation, the semantics of $M$ is
192also $L_1 L_2 \ldots$ and its actual execution cost is $\Sigma_i k(L_i)$ because
193every instruction belongs to a control block and every control block is
194labelled. Thus it is correct to say that the execution cost of $P$ is also
195$\Sigma_i k(L_i)$. In other words, we have obtained a cost model $k$ for
196the blocks of the high level program $P$ that is preserved by compilation.
198How can the user profit from the high level cost model? Suppose, for instance,
199that he wants to prove that the WCET of his program is bounded by $c$. It
200is sufficient for him to prove that $\Sigma_i k(L_i) \leq c$, which is now
201a purely functional property of the code. He can therefore use any technique
202available to certify functional properties of the source code.
203What is suggested in~\cite{easylabelling} is to actually instrument the
204source code $P$ by replacing every label emission statement
205$\texttt{emit L}$ with the instruction $\texttt{cost += k(L)}$ that increments
206a global fresh variable \texttt{cost}. The bound is now proved by establishing
207the program invariant $\texttt{cost} \leq c$, which can be done for example
208using the Frama-C~\cite{framaC} suite if the source code is some variant of
211\subsection{Labelling function calls}
212We now want to extend the labelling approach to support function calls.
213On the high level, \emph{structured} programming language $\mathcal{P}$ there
214is not much to change.
215When a function is invoked, the current basic block is temporarily exited
216and the basic block the function starts with take control. When the function
217returns, the execution of the original basic block is resumed. Thus the only
218significant change is that basic blocks can now be nested. Let \texttt{E}
219be the label of the external block and \texttt{I} the label of a nested one.
220Since the external starts before the internal, the semantics observed will be
221\texttt{E I} and the cost associated to it on the source language will be
222$k(\texttt{E}) + k(\texttt{I})$, i.e. the cost of executing all instructions
223in the block \texttt{E} first plus the cost of executing all the instructions in
224the block \texttt{I}. However, we know that some instructions in \texttt{E} are
225executed after the last instruction in \texttt{I}. This is actually irrelevant
226because we are here assuming that costs are additive, so that we can freely
227permute them\footnote{The additivity assumption fails on modern processors that have stateful subsystems, like caches and pipelines. The extension of the labelling approach to those systems is therefore non trivial and under development in the CerCo project.}. Note that, in the present discussion, we are assuming that
228the function call terminates and yields back control to the basic block
229\texttt{E}. If the call diverges, the instrumentation
230$\texttt{cost += k(E)}$ executed at the beginning of \texttt{E} is still valid,
231but just as an upper bound to the real execution cost: only precision is lost.
233Let now consider what happens when we move down the compilation chain to an
234unstructured intermediate or final language. Here unstructured means that
235the only control operators are conditional and unconditional jumps, function
236calls and returns. Unlike a structured language, though, there is no guarantee
237that a function will return control just after the function call point.
238The semantics of the return statement, indeed, consists in fetching the
239return address from some internal structure (tipically the control stack) and
240jumping directly to it. The code can freely manipulate the control stack to
241make the procedure returns to whatever position. Indeed, it is also possible
242to break the well nesting of function calls/returns.
244Is it the case that the code produced by a correct compiler must respect the
245additional property that every function returns just after its function call
246point? The answer is negative and the property is not implied by forward
247simulation proofs. For instance, imagine to modify a correct compiler pass
248by sistematically adding one to the return address on the stack and by
249putting a \texttt{NOP} (or any other instruction that takes one byte) after
250every function call. The obtained code will be functionally indistinguishable,
251and the added instructions will all be dead code.
253This lack of structure in the semantics badly interferes with the labelling
254approach. The reason is the following: when a basic block labelled with
255\texttt{E} contains a function call, it no longer makes any sense to associate
256to a label \texttt{E} the sum of the costs of all the instructions in the block.
257Indeed, there is no guarantee that the function will return into the block and
258that the instructions that will be executed after the return will be the ones
259we are paying for in the cost model.
261How can we make the labelling approach work in this scenario? We only see two
262possible ways. The first one consists in injecting an emission statement after
263every function call: basic blocks no longer contain function calls, but are now
264terminated by them. This completely solves the problem and allows the compiler
265to break the structure of function calls/returns at will. However, the
266technique has several drawbacks. First of all, it greatly augments the number
267of cost labels that are injected in the source code and that become
268instrumentation statements. Thus, when reasoning on the source code to prove
269non-functional properties, the user (or the automation tool) will have to handle
270larger expressions. Second, the more labels are emitted, the more difficult it
271becomes to implement powerful optimizations respecting the code structure.
272Indeed, function calls are usually implemented in such a way that most registers
273are preserved by the call, so that the static analysis of the block is not
274interrupted by the call and an optimization can involve both the code before
275and after the function call. Third, instrumenting the source code may require
276unpleasant modification of it. Take, for example, the code
277\texttt{f(g(x));}. We need to inject an emission statement/instrumentation
278instruction just after the execution of \texttt{g}. The only way to do that
279is to rewrite the code as \texttt{y = g(x); emit L$_1$; f(y);} for some fresh
280variable \texttt{y}. It is pretty clear how in certain situations the obtained
281code would be more obfuscated and then more difficult to manually reason on.
283For the previous reasons, in this paper and in the CerCo project we adopt a
284different approach. We do not inject any more emission statement after every
285function call. However, we want to propagate a strong additional invariant in
286the forward simulation proof. The invariant is the propagation of the structure
287 of the original high level code, even if the target language is unstructured.
288The structure we want to propagate, that will become more clear in the next
289section, comprises 1) the property that every function should return just after
290the function call point, which in turns imply well nesting of function calls;
2912) the property that every basic block starts with a code emission statement.
293In the original labelling approach of~\cite{easylabelling}, the second property
294was granted statically (syntactically) as a property of the generated code.
295In our revised approach, instead, we will grant the property dinamically:
296it will be possible to generate code that does not respect it, as soon as
297all execution traces do respect it. For instance, dead code will no longer
298be required to have all basic blocks correctly labelled. The switch is suggested
299from the fact that the first of the two properties --- that related to
300function calls/returns --- can only be defined as a dinamic property, i.e.
301as a property of execution traces, not of the static code. The switch is
302beneficial to the proof because the original proof was made of two parts:
303the forward simulation proof and the proof that the static property was granted.
304In our revised approach the latter disappears and only the forward simulation
305is kept.
307In order to capture the structure semantics so that it is preserved
308by a forward simulation argument, we need to make the structure observable
309in the semantics. This is the topic of the next Section.
311\section{Structured traces}
313The program semantics adopted in the traditional labelling approach is based
314on labelled deductive systems. Given a set of observables $\mathcal{O}$ and
315a set of states $\mathcal{S}$, the semantics of one deterministic execution
316step is
317defined as a function $S \to S \times O^*$ where $O^*$ is a (finite) stream of
318observables. The semantics is then lifted compositionally to multiple (finite
319or infinite) execution steps.
320Finally, the semantics of a a whole program execution is obtained by forgetting
321about the final state (if any), yielding a function $S \to O^*$ that given an
322initial status returns the finite or infinite stream of observables in output.
324We present here a new definition of semantics where the structure of execution,
325as defined in the previous Section, is now observable. The idea is to replace
326the stream of observables with a structured data type that makes explicit
327function call and returns and that grants some additional invariants by
328construction. The data structure, called \emph{structured traces}, is
329defined inductively for terminating programs and coinductively for diverging
330ones. In the paper we focus only on the inductive structure, i.e. we assume
331that all programs that are given a semantics are total. The Matita formalization
332also shows the coinductive definitions. The semantics of a program is then
333defined as a function that maps an initial state into a structured trace.
335In order to have a definition that works on multiple intermediate languages,
336we abstract the type of structure traces over an abstract data type of
337abstract statuses:
339record abstract_status := {
340   S: Type[0]
341;  as_execute: S -> S -> Prop
342;  as_classify: S -> classification
343;  as_costed: S -> Prop
344;  as_after_return:
345    (Σs:as_status. as_classify s = Some ? cl_call) → as_status → Prop
349The predicate $\texttt{as\_execute}~s_1~s_2$ holds if $s_1$ evolves into
350$s_2$ in one step; $\texttt{as\_classify}~s~c$ holds if the next instruction
351to be executed in $s$ is classified according to $c \in
352\{\texttt{return,jump,call,other}\}$ (we omit tailcalls for simplicity);
353the predicate $\texttt{as\_costed}~s$ holds if the next instruction to be
354executed in $s$ is a cost emission statement (also classified
355as \texttt{other}); finally $(\texttt{as\_after\_return}~s_1~s_2)$ holds
356if the next instruction to be executed in $s_2$ follows the function call
357to be executed in (the witness of the $\Sigma$-type) $s_1$.
360The inductive type for structured traces is actually made by three multiple
361inductive types with the following semantics:
363 \item $(\texttt{trace\_label\_return}~s_1~s_2)$ is a trace that begins in
364   the state $s_1$ (included) and ends just before the state $s_2$ (excluded)
365   such that the instruction to be executed in $s_1$ is a label emission
366   statement and the one to be executed in the state before $s_2$ is a return
367   statement. Thus $s_2$ is the state after the return. The trace
368   may contain other label emission statements. It captures the structure of
369   the execution of function bodies: they must start with a cost emission
370   statement and must end with a return; they are obtained by concatenating
371   one or more basic blocks, all starting with a label emission
372   (e.g. in case of loops).
373 \item $(\texttt{trace\_any\_label}~b~s_1~s_2)$ is a trace that begins in
374   the state $s_1$ (included) and ends just before the state $s_2$ (excluded)
375   such that the instruction to be executed in $s_2$/in the state before
376   $s_2$ is either a label emission statement or
377   or a return, according to the boolean $b$. It must not contain
378   any label emission statement. It captures the notion of a suffix of a
379   basic block.
380 \item $(\texttt{trace\_label\_label}~b~s_1~s_2)$ is the special case of
381   $(\texttt{trace\_any\_label}~b~s_1~s_2)$ such that the instruction to be
382   executed in $s_1$ is a label emission statement. It captures the notion of
383   a basic block.
387 {\texttt{trace\_label\_label}~true~s_1~s_2}
388 {\texttt{trace\_label\_return}~s_1~s_2}
391 {\texttt{trace\_label\_label}~false~s_1~s_2 \andalso
392  \texttt{trace\_label\_return}~s_2~s_3
393 }
394 {\texttt{trace\_label\_return}~s_1~s_3}
397 {\texttt{trace\_any\_label}~b~s_1~s_2 \andalso
398  \texttt{as\_costed}~s_1
399 }
400 {\texttt{trace\_label\_label}~b~s_1~s_2}
403 {\texttt{as\_execute}~s_1~s_2 \andalso
404  \texttt{as\_classifier}~s_1~k \\
405  k \in \{\texttt{cl\_jump,cl\_other}\} \andalso
406  \texttt{as\_costed}~s_2
407 }
408 {\texttt{trace\_any\_label}~false~s_1~s_2}
411 {\texttt{as\_execute}~s_1~s_2 \andalso
412  \texttt{as\_classifier}~s_1~\texttt{cl\_return} \\
413 }
414 {\texttt{trace\_any\_label}~true~s_1~s_2}
417 {\texttt{as\_execute}~s_1~s_2 \andalso
418  \texttt{as\_classifier}~s_1~\texttt{cl\_call} \\
419  \texttt{as\_after\_return}~s_1~s_3 \andalso
420  \texttt{trace\_label\_return}~s_2~s_3 \andalso
421  \texttt{as\_costed}~s_3
422 }
423 {\texttt{trace\_any\_label}~false~s_1~s_3}
426 {\texttt{as\_execute}~s_1~s_2 \andalso
427  \texttt{as\_classifier}~s_1~\texttt{cl\_call} \\
428  \texttt{as\_after\_return}~s_1~s_3 \andalso
429  \texttt{trace\_label\_return}~s_2~s_3 \\
430  \lnot \texttt{as\_costed}~s_3 \andalso
431  \texttt{trace\_any\_label}~b~s_3~s_4
432 }
433 {\texttt{trace\_any\_label}~b~s_1~s_4}
436 {\texttt{as\_execute}~s_1~s_2 \andalso
437  \texttt{trace\_any\_label}~b~s_2~s_3
438  \texttt{as\_classifier}~s_1~\texttt{cl\_other} \\
439  \lnot \texttt{as\_costed}~s_2
440 }
441 {\texttt{trace\_any\_label}~b~s_1~s_3}
446inductive trace_label_return (S:abstract_status) : S → S → Type[0] ≝
447  | tlr_base:
448      ∀status_before: S.
449      ∀status_after: S.
450        trace_label_label S ends_with_ret status_before status_after →
451        trace_label_return S status_before status_after
452  | tlr_step:
453      ∀status_initial: S.
454      ∀status_labelled: S.
455      ∀status_final: S.
456        trace_label_label S doesnt_end_with_ret status_initial status_labelled →
457        trace_label_return S status_labelled status_final →
458          trace_label_return S status_initial status_final
459with trace_label_label: trace_ends_with_ret → S → S → Type[0] ≝
460  | tll_base:
461      ∀ends_flag: trace_ends_with_ret.
462      ∀start_status: S.
463      ∀end_status: S.
464        trace_any_label S ends_flag start_status end_status →
465        as_costed S start_status →
466          trace_label_label S ends_flag start_status end_status
467with trace_any_label: trace_ends_with_ret → S → S → Type[0] ≝
468  (* Single steps within a function which reach a label.
469     Note that this is the only case applicable for a jump. *)
470  | tal_base_not_return:
471      ∀start_status: S.
472      ∀final_status: S.
473        as_execute S start_status final_status →
474        (as_classifier S start_status cl_jump ∨
475         as_classifier S start_status cl_other) →
476        as_costed S final_status →
477          trace_any_label S doesnt_end_with_ret start_status final_status
478  | tal_base_return:
479      ∀start_status: S.
480      ∀final_status: S.
481        as_execute S start_status final_status →
482        as_classifier S start_status cl_return →
483          trace_any_label S ends_with_ret start_status final_status
484  (* A call followed by a label on return. *)
485  | tal_base_call:
486      ∀status_pre_fun_call: S.
487      ∀status_start_fun_call: S.
488      ∀status_final: S.
489        as_execute S status_pre_fun_call status_start_fun_call →
490        ∀H:as_classifier S status_pre_fun_call cl_call.
491          as_after_return S «status_pre_fun_call, H» status_final →
492          trace_label_return S status_start_fun_call status_final →
493          as_costed S status_final →
494            trace_any_label S doesnt_end_with_ret status_pre_fun_call status_final
495  (* A call followed by a non-empty trace. *)
496  | tal_step_call:
497      ∀end_flag: trace_ends_with_ret.
498      ∀status_pre_fun_call: S.
499      ∀status_start_fun_call: S.
500      ∀status_after_fun_call: S.
501      ∀status_final: S.
502        as_execute S status_pre_fun_call status_start_fun_call →
503        ∀H:as_classifier S status_pre_fun_call cl_call.
504          as_after_return S «status_pre_fun_call, H» status_after_fun_call →
505          trace_label_return S status_start_fun_call status_after_fun_call →
506          ¬ as_costed S status_after_fun_call →
507          trace_any_label S end_flag status_after_fun_call status_final →
508            trace_any_label S end_flag status_pre_fun_call status_final
509  | tal_step_default:
510      ∀end_flag: trace_ends_with_ret.
511      ∀status_pre: S.
512      ∀status_init: S.
513      ∀status_end: S.
514        as_execute S status_pre status_init →
515        trace_any_label S end_flag status_init status_end →
516        as_classifier S status_pre cl_other →
517        ¬ (as_costed S status_init) →
518          trace_any_label S end_flag status_pre status_end.
521A \texttt{trace\_label\_return} is isomorphic to a list of
522\texttt{trace\_label\_label}s that end with a cost emission followed by a
523\texttt{trace\_label\_label} that ends with a return.
525The interesting cases are those of $(\texttt{trace\_any\_label}~b~s_1~s_2)$.
526A \texttt{trace\_any\_label} is a sequence of steps built by a syntax directed
527definition on the classification of $s_1$. The constructors of the datatype
528impose several invariants that are meant to impose a structure to the
529otherwise unstructured execution. In particular, the following invariants are
532 \item the trace is never empty; it ends with a return iff $b$ is
533       true (\texttt{ends\_with\_ret})
534 \item a jump must always be the last instruction of the trace, and it must
535       be followed by a cost emission statement; i.e. the target of a jump
536       is always the beginning of a new basic block; as such it must start
537       with a cost emission statement
538 \item a cost emission statement can never occur inside the trace, only in
539       the status immediately after
540 \item the trace for a function call step is made from a subtrace of type
541       $(\texttt{trace\_label\_return}~s_1~s_2)$, possibly followed by the
542       rest of the trace for this basic block. The subtrace represents the
543       function execution. Being an inductive datum, it grants totality of
544       the function call. The status $s_2$ is the one that follows the return
545       statement. The next instruction of $s_2$ must follow the function call
546       instruction. As a consequence, function calls are also well nested.
549The three mutual structural recursive functions \texttt{flatten\_trace\_label\_return, flatten\_trace\_label\_label} and \texttt{flatten\_trance\_any\_label}
550allow to extract from a structured trace the list of states whose next
551instruction is a cost emission statement. We only show here the type of one
552of them:
555  (S: abstract_status)
556    (start_status: S) (final_status: S)
557      (the_trace: trace_label_return S start_status final_status)
558        on the_trace: list (as_cost_label S)
561\paragraph{Cost prediction on structured traces}
563The first main theorem of CerCo about structured traces
564(theorem \texttt{compute\_max\_trace\_label\_return\_cost\_ok\_with\_trace})
565holds for the
567of the structured traces to the concrete status of object code programs.
568Simplifying a bit, it states that
569$\forall s_1,s_2. \forall \tau: \texttt{trace\_label\_return}~s_1~s_2.
570  clock~s_2 = clock~s_1 + \Sigma_{s \in \texttt{flatten\_trace\_label\_return}~\tau} k(L(s))$ where $L$ maps a labelled state to its emitted label, and the
571cost model $k$ is statically computed from the object code
572by associating to each label \texttt{L} the sum of the cost of the instructions
573in the basic block that starts at \texttt{L} and ends before the next labelled
574instruction. The theorem is proved by structural induction over the structured
575trace, and is based on the invariant that
576iff the function that computes the cost model has analyzed the instruction
577to be executed at $s_2$ after the one to be executed at $s_1$, and if
578the structured trace starts with $s_1$, then eventually it will contain also
579$s_2$. When $s_1$ is not a function call, the result holds trivially because
580of the $(\texttt{as\_execute}~s_1~s_2)$ condition obtained by inversion on
581the trace. The only non
582trivial case is the one of function calls: the cost model computation function
583does recursion on the first instruction that follows that function call; the
584\texttt{as\_after\_return} condition of the \texttt{tal\_base\_call} and
585\texttt{tal\_step\_call} grants exactly that the execution will eventually reach
586this state.
588\paragraph{Structured traces similarity and cost prediction invariance}
590A compiler pass maps source to object code and initial states to initial
591states. The source code and initial state uniquely determine the structured
592trace of a program, if it exists. The structured trace fails to exists iff
593the structural conditions are violated by the program execution (e.g. a function
594body does not start with a cost emission statement). Let us assume that the
595target structured trace exists.
597What is the relation between the source and target structured traces?
598In general, the two traces can be arbitrarily different. However, we are
599interested only in those compiler passes that maps a trace $\tau_1$ to a trace
600$\tau_2$ such that
601\begin{displaymath}(\texttt{flatten\_trace\_label\_return}~\tau_1 = \texttt{flatten\_trace\_label\_return}~\tau_2)\label{condition1}\end{displaymath}
602The reason is that when this happens we obviously have the
603following corollary to the previous theorem:
604$$\forall s_1,s_2. \forall \tau: \texttt{trace\_label\_return}~s_1~s_2.
605  clock~s_2 - clock~s_1 = \Sigma_{s \in \texttt{flatten\_trace\_label\_return}~\tau_1} k(L(s)) = \Sigma_{s \in \texttt{flatten\_trace\_label\_return}~\tau_2} k(L(s))$$
606The corollary states that the actual execution time of the program can be computed equally well on the source or target language. Thus it becomes possible to
607transfer the cost model from the target to the source code and reason on the
608source code only.
610We are therefore interested in conditions stronger than~\ref{condition1}.
611Therefore we introduce here a similarity relation between traces that captures
612the fact that two traces have the same structure and that
613implies~\ref{condition1} (theorem \texttt{tlr\_rel\_to\_traces\_same\_flatten}). Two traces are similar when one can be obtained from
614the other by erasing or inserting steps classified as \texttt{other} into the
615\texttt{trace\_any\_label} blocks of the trace. In particular,
616the relation maps function calls to function calls to the same function,
617label emission statements to emissions of the same label, concatenation of
618subtraces to concatenation of subtraces of the same lenghth and starting with
619the same emissione statement, etc.
621The similarity relation definition if simple, but it involves a large
622number of cases. We only show here a few of the cases:
624let rec tlr_rel S1 st1 st1' S2 st2 st2'
625  (tlr1 : trace_label_return S1 st1 st1')
626  (tlr2 : trace_label_return S2 st2 st2') on tlr1 : Prop ≝
627match tlr1 with
628  [ tlr_base st1 st1' tll1 ⇒
629    match tlr2 with
630    [ tlr_base st2 st2' tll2 ⇒ tll_rel … tll1 tll2
631    | _ ⇒ False
632    ]
633  | tlr_step st1 st1' st1'' tll1 tl1 ⇒
634    match tlr2 with
635    [ tlr_step st2 st2' st2'' tll2 tl2 ⇒
636      tll_rel … tll1 tll2 ∧ tlr_rel … tl1 tl2
637    | _ ⇒ False
638    ]
639  ]
640and tll_rel S1 fl1 st1 st1' S2 fl2 st2 st2'
641 (tll1 : trace_label_label S1 fl1 st1 st1')
642 (tll2 : trace_label_label S2 fl2 st2 st2') on tll1 : Prop ≝
643  match tll1 with
644  [ tll_base fl1 st1 st1' tal1 H ⇒
645    match tll2 with
646    [ tll_base fl2 st2 st2 tal2 G ⇒
647      as_label_safe … («?, H») = as_label_safe … («?, G») ∧
648      tal_rel … tal1 tal2
649    ]
650  ]
651and tal_rel S1 fl1 st1 st1' S2 fl2 st2 st2'
652 (tal1 : trace_any_label S1 fl1 st1 st1')
653 (tal2 : trace_any_label S2 fl2 st2 st2')
654   on tal1 : Prop ≝
655  match tal1 with
656  [ tal_base_not_return st1 st1' _ _ _ ⇒
657    fl2 = doesnt_end_with_ret ∧
658    ∃st2mid,taa,H,G,K.
659    tal2 ≃ taa_append_tal ? st2 ??? taa
660      (tal_base_not_return ? st2mid st2' H G K)
661  | tal_base_return st1 st1' _ _ ⇒
662    fl2 = ends_with_ret ∧
663    ∃st2mid,taa,H,G.
664    tal2 ≃ taa_append_tal ? st2 ? st2mid st2' taa
665      (tal_base_return ? st2mid st2' H G)
666  | tal_base_call st1 st1' st1'' _ prf _ tlr1 _ ⇒
667    fl2 = doesnt_end_with_ret ∧
668    ∃st2mid,G.as_call_ident S2 («st2mid, G») = as_call_ident ? «st1, prf» ∧
669    ∃taa : trace_any_any ? st2 st2mid.∃st2mid',H.
670    (* we must allow a tal_base_call to be similar to a call followed
671      by a collapsable trace (trace_any_any followed by a base_not_return;
672      we cannot use trace_any_any as it disallows labels in the end as soon
673      as it is non-empty) *)
674    (∃K.∃tlr2 : trace_label_return ? st2mid' st2'.∃L.
675      tal2 ≃ taa @ (tal_base_call … H G K tlr2 L) ∧ tlr_rel … tlr1 tlr2) ∨
676    ∃st2mid'',K.∃tlr2 : trace_label_return ? st2mid' st2mid''.∃L.
677    ∃tl2 : trace_any_label … doesnt_end_with_ret st2mid'' st2'.
678      tal2 ≃ taa @ (tal_step_call … H G K tlr2 L tl2) ∧
679      tlr_rel … tlr1 tlr2 ∧ tal_collapsable … tl2
680  | tal_base_tailcall st1 st1' st1'' _ prf tlr1 ⇒
681    fl2 = ends_with_ret ∧
682    ∃st2mid,G.as_tailcall_ident S2 («st2mid, G») = as_tailcall_ident ? «st1, prf» ∧
683    ∃taa : trace_any_any ? st2 st2mid.∃st2mid',H.
684    ∃tlr2 : trace_label_return ? st2mid' st2'.
685      tal2 ≃ taa @ (tal_base_tailcall … H G tlr2) ∧ tlr_rel … tlr1 tlr2
686  | tal_step_call fl1 st1 st1' st1'' st1''' _ prf _ tlr1 _ tl1 ⇒
687    ∃st2mid,G.as_call_ident S2 («st2mid, G») = as_call_ident ? «st1, prf» ∧
688    ∃taa : trace_any_any ? st2 st2mid.∃st2mid',H.
689    (fl2 = doesnt_end_with_ret ∧ ∃K.∃tlr2 : trace_label_return ? st2mid' st2'.∃L.
690      tal2 ≃ taa @ tal_base_call … H G K tlr2 L ∧
691      tal_collapsable … tl1 ∧ tlr_rel … tlr1 tlr2) ∨
692    ∃st2mid'',K.∃tlr2 : trace_label_return ? st2mid' st2mid''.∃L.
693    ∃tl2 : trace_any_label ? fl2 st2mid'' st2'.
694      tal2 ≃ taa @ (tal_step_call … H G K tlr2 L tl2) ∧
695      tal_rel … tl1 tl2 ∧ tlr_rel … tlr1 tlr2
696  | tal_step_default fl1 st1 st1' st1'' _ tl1 _ _ ⇒
697    tal_rel … tl1 tal2 (* <- this makes it many to many *)
698  ].
701\section{Forward simulation}
704We summarize here the results of the previous sections. Each intermediate
705unstructured language can be given a semantics based on structured traces,
706that single out those runs that respect a certain number of invariants.
707A cost model can be computed on the object code and it can be used to predict
708the execution costs of runs that produce structured traces. The cost model
709can be lifted from the target to the source code of a pass if the pass maps
710structured traces to similar structured traces. The latter property is called
711a \emph{forward simulation}.
713As for labelled transition systems, in order to establish the forward
714simulation we are interested in (preservation of observables), we are
715forced to prove a stronger notion of forward simulation that introduces
716an explicit relation between states. The classical notion of a 1-to-0-or-many
717forward simulation is the existence of a relation $R$ over states such that
718if $s_1 R s_2$ and $s_1 \to^1 s_1'$ then there exists an $s_2'$ such that
719$s_1' \to^* s_2'$ and $s_1' R s_2'$. In our context, we need to replace the
720one and multi step transition relations $\to^n$ with the existence of
721a structured trace between the two states, and we need to add the request that
722the two structured traces are similar. Thus what we would like to state is
723something like:\\
724for all $s_1,s_2,s_1'$ such that there is a $\tau_1$ from
725$s_1$ to $s_1'$ and $s_1 R s_2$ there exists an $s_2'$ such that
726$s_1' R s_2'$ and a $\tau_2$ from $s_2$ to $s_2'$ such that
727$\tau_1 \approx \tau_2$.\\
729The statement just introduced, however, is too simplicistic and not provable
730in the general case. To understand why, consider the case of a function call
731and the pass that fixes the parameter passing conventions. A function
732call in the source code takes in input an arbitrary number of pseudo-registers (the actual parameters to pass) and returns an arbitrary number of pseudo-registers (where the result is stored). A function call in the target call has no
733input nor output parameters. The pass must add explicit code before and after
734the function call to move the pseudo-registers content from/to the hardware
735registers or the stack in order to implement the parameter passing strategy.
736Similarly, each function body must be augmented with a preamble and a postamble
737to complete/initiate the parameter passing strategy for the call/return phase.
738Therefore what used to be a call followed by the next instruction to execute
739after the function return, now becomes a sequence of instructions, followed by
740a call, followed by another sequence. The two states at the beginning of the
741first sequence and at the end of the second sequence are in relation with
742the status before/after the call in the source code, like in an usual forward
743simulation. How can we prove however the additional condition for function calls
744that asks that when the function returns the instruction immediately after the
745function call is called? To grant this invariant, there must be another relation
746between the address of the function call in the source and in the target code.
747This additional relation is to be used in particular to relate the two stacks.
749Another example is given by preservation of code emission statements. A single
750code emission instruction can be simulated by a sequence of steps, followed
751by a code emission, followed by another sequence. Clearly the initial and final
752statuses of the sequence are to be in relation with the status before/after the
753code emission in the source code. In order to preserve the structured traces
754invariants, however, we must consider a second relation between states that
755traces the preservation of the code emission statement.
757Therefore we now introduce an abstract notion of relation set between abstract
758statuses and an abstract notion of 1-to-0-or-many forward simulation. These
759two definitions enjoy the following remarkable properties:
761 \item they are generic enough to accomodate all passes of the CerCo compiler
762 \item they allow to lift the 1-to-0-or-many forward simulation to the
763   structured traces preserving forward simulation we are interested in
766The latter lifting is proved by the following theorem which has the
7671-to-0-or-many forward simulation statement as an hypothesis. Combined with
768the results in the previous section, it has as a corollary that the cost
769model can be computed on the object code and lifted to the source code to
770reason on non-functional properties iff a simpler 1-to-0-or-many forward
771simulation is proved. The statement of the latter is way more complex than a
772traditional 1-to-0-or-many forward simulation statement, but proofs have now
773about the same complexity. Indeed, it should be possible to largely reuse an
774existent traditional forward simulation proof. The lifting theorem below,
775proved once and for all in this paper, is the one that does all the job to
776put the labelling approach in motion.
779definition status_simulation ≝
780  λS1 : abstract_status.
781  λS2 : abstract_status.
782  λsim_status_rel : status_rel S1 S2.
783    ∀st1,st1',st2.as_execute S1 st1 st1' →
784    sim_status_rel st1 st2 →
785    match as_classify … st1 with
786    [ None ⇒ True
787    | Some cl ⇒
788      match cl with
789      [ cl_call ⇒ ∀prf.
790        (*
791             st1' ------------S----------\
792              ↑ \                         \
793             st1 \--L--\                   \
794              | \       \                   \
795              S  \-C-\  st2_after_call →taa→ st2'
796              |       \     ↑
797             st2 →taa→ st2_pre_call
798        *)
799        ∃st2_pre_call.
800        as_call_ident ? st2_pre_call = as_call_ident ? («st1, prf») ∧
801        call_rel ?? sim_status_rel «st1, prf» st2_pre_call ∧
802        ∃st2_after_call,st2'.
803        ∃taa2 : trace_any_any … st2 st2_pre_call.
804        ∃taa2' : trace_any_any … st2_after_call st2'.
805        as_execute … st2_pre_call st2_after_call ∧
806        sim_status_rel st1' st2' ∧
807        label_rel … st1' st2_after_call
808      | cl_tailcall ⇒ ∀prf.
809        (*
810             st1' ------------S----------\
811              ↑ \                         \
812             st1 \--L--\                   \
813              |         \                   \
814              S         st2_after_call →taa→ st2'
815              |             ↑
816             st2 →taa→ st2_pre_call
817        *)
818        ∃st2_pre_call.
819        as_tailcall_ident ? st2_pre_call = as_tailcall_ident ? («st1, prf») ∧
820        ∃st2_after_call,st2'.
821        ∃taa2 : trace_any_any … st2 st2_pre_call.
822        ∃taa2' : trace_any_any … st2_after_call st2'.
823        as_execute … st2_pre_call st2_after_call ∧
824        sim_status_rel st1' st2' ∧
825        label_rel … st1' st2_after_call
826      | cl_return ⇒
827        (*
828             st1
829            / ↓
830           | st1'----------S,L------------\
831           S   \                           \
832            \   \-----R-------\            |     
833             \                 |           |
834             st2 →taa→ st2_ret |           |
835                          ↓   /            |
836                     st2_after_ret →taaf→ st2'
838           we also ask that st2_after_ret be not labelled if the taaf tail is
839           not empty
840        *)
841        ∃st2_ret,st2_after_ret,st2'.
842        ∃taa2 : trace_any_any … st2 st2_ret.
843        ∃taa2' : trace_any_any_free … st2_after_ret st2'.
844        (if taaf_non_empty … taa2' then ¬as_costed … st2_after_ret else True) ∧
845        as_classifier … st2_ret cl_return ∧
846        as_execute … st2_ret st2_after_ret ∧ sim_status_rel st1' st2' ∧
847        ret_rel … sim_status_rel st1' st2_after_ret ∧
848        label_rel … st1' st2'
849      | cl_other ⇒
850          (*         
851          st1 → st1'
852            |      \
853            S      S,L
854            |        \
855           st2 →taaf→ st2'
857           the taaf can be empty (e.g. tunneling) but we ask it must not be the
858           case when both st1 and st1' are labelled (we would be able to collapse
859           labels otherwise)
860         *)
861        ∃st2'.
862        ∃taa2 : trace_any_any_free … st2 st2'.
863        (if taaf_non_empty … taa2 then True else (¬as_costed … st1 ∨ ¬as_costed … st1')) ∧
864        sim_status_rel st1' st2' ∧
865        label_rel … st1' st2'
866      | cl_jump ⇒
867        (* just like cl_other, but with a hypothesis more *)
868        as_costed … st1' →
869        ∃st2'.
870        ∃taa2 : trace_any_any_free … st2 st2'.
871        (if taaf_non_empty … taa2 then True else (¬as_costed … st1 ∨ ¬as_costed … st1')) ∧
872        sim_status_rel st1' st2' ∧
873        label_rel … st1' st2'
874      ]
875    ].
879status_simulation_produce_tlr S1 S2 R
880(* we start from this situation
881     st1 →→→→tlr→→→→ st1'
882      | \
883      L  \---S--\
884      |          \
885   st2_lab →taa→ st2   (the taa preamble is in general either empty or given
886                        by the preceding call)
888   and we produce
889     st1 →→→→tlr→→→→ st1'
890             \\      /  \
891             //     R    \-L,S-\
892             \\     |           \
893   st2_lab →tlr→ st2_mid →taaf→ st2'
895  st1 st1' st2_lab st2
896  (tlr1 : trace_label_return S1 st1 st1')
897  (taa2_pre : trace_any_any S2 st2_lab st2)
898  (sim_execute : status_simulation S1 S2 R)
899  on tlr1 : R st1 st2 → label_rel … st1 st2_lab →
900  ∃st2_mid.∃st2'.
901  ∃tlr2 : trace_label_return S2 st2_lab st2_mid.
902  ∃taa2 : trace_any_any_free … st2_mid st2'.
903  (if taaf_non_empty … taa2 then ¬as_costed … st2_mid else True) ∧
904  R st1' st2' ∧ ret_rel … R st1' st2_mid ∧ label_rel … st1' st2' ∧
905  tlr_rel … tlr1 tlr2
908\section{Conclusions and future works}
910The labelling approach is a technique to implement compilers that induce on
911the source code a non uniform cost model determined from the object code
912produced. The cost model assigns a cost to each basic block of the program.
913The main theorem of the labelling approach says that there is an exact
914correspondence between the sequence of basic blocks started in the source
915and object code, and that no instruction in the source or object code is
916executed outside a basic block. Thus the overall cost of object code execution
917can be computed precisely on the source code.
919In this paper we scale the labelling approach to cover a programming language
920with function calls. This introduces new difficulties only when the language
921is unstructured, i.e. it allows function calls to return anywhere in the code,
922destroying the hope of a static prediction of the cost of basic blocks.
923We restore static predictability by introducing a new semantics for unstructured
924programs that single outs well structured executions. The latter are represented
925by structured traces, a generalization of streams of observables that capture
926several structural invariants of the execution, like well nesting of functions
927or the fact that every basic block must start with a code emission statement.
928We show that structured traces are sufficiently structured to statically compute
929a precise cost model on the object code.
931We introduce a similarity relation on structured traces that must hold between
932source and target traces. When the relation holds for every program, we prove
933that the cost model can be lifted from the object to the source code.
935In order to prove that similarity holds, we present a generic proof of forward
936simulation that is aimed at pulling apart as much as possible the part of the
937simulation related to non-functional properties (preservation of structure)
938from that related to functional properties. In particular, we reduce the
939problem of preservation of structure to that of showing a 1-to-0-or-many
940forward simulation that only adds a few additional proof obligations to those
941of a traditional, function properties only, proof.
943All the results presented in the paper are part of a larger certification of a
944C compiler which is based on the labelling approach. The certification, done
945in Matita, is the main deliverable of the European Project CerCo.
947The short term future work consists in the completion of the certification of
948the CerCo compiler. In particular, the results presented in this paper will
949be used to certify all the passes of the back-end of the compiler.
951\paragraph{Related works}
952CerCo is the first certification project which explicitly tries to induce a
953precise cost model on the source code in order to establish non-functional
954properties of programs on an high level language. Traditional certifications
955of compilers, like~\cite{compcert2,piton}, only explicitly prove preservation
956of the functional properties via forward simulations.
958Usually forward simulations take the following form: for each transition
959from $s_1$ to $s_2$ in the source code, there exists an equivalent sequence of
960transitions in the target code of length $n$. The number $n$ of transition steps
961in the target code can just be the witness of the existential statement.
962An equivalent alternative when the proof of simulation is constructive consists
963in providing an explicit function, called \emph{clock function} in the
964litterature~\cite{clockfunctions}, that computes $n$ from $s_1$. Every clock
965function constitutes then a cost model for the source code, in the spirit of
966what we are doing in CerCo. However, we believe our solution to be superior
967in the following points: 1) the machinery of the labelling approach is
968insensible to the resource being measured. Indeed, any cost model computed on
969the object code can be lifted to the source code (e.g. stack space used,
970energy consumed, etc.). On the contrary, clock functions only talk about
971number of transition steps. In order to extend the approach with clock functions
972to other resources, additional functions must be introduced. Moreover, the
973additional functions would be handled differently in the proof.
9742) the cost models induced by the labelling approach have a simple presentation.
975In particular, they associate a number to each basic block. More complex
976models can be induced when the approach is scaled to cover, for instance,
977loop optimizations~\cite{loopoptimizations}, but the costs are still meant to
978be easy to understandable and manipulate in an interactive theorem prover.
979On the contrary, a clock function is a complex function of the state $s_1$
980which, as a function, is an opaque object that is difficult to reify as
981source code in order to reason on it.
Note: See TracBrowser for help on using the repository browser.