source: src/compiler.ma @ 3021

Last change on this file since 3021 was 3021, checked in by campbell, 7 years ago

Replace clight_clock_after with a more sensible definition that uses
an intensional trace. Also drop the proof argument for the clight cost
map to avoid proving that costs come from the clight program - it doesn't
seem to give us much of a benefit.

File size: 5.4 KB
Line 
1(* Front-end related includes *)
2include "Clight/label.ma".
3include "Clight/SimplifyCasts.ma".
4include "Clight/switchRemoval.ma".
5include "Clight/toCminor.ma".
6include "Cminor/toRTLabs.ma".
7include "RTLabs/CostCheck.ma".
8include "RTLabs/CostInj.ma".
9
10(* Back-end related includes *)
11include "RTLabs/RTLabsToRTL.ma".
12include "RTL/RTLToERTL.ma".
13include "ERTL/ERTLToLTL.ma".
14include "LTL/LTLToLIN.ma".
15include "LIN/LINToASM.ma".
16
17(* List of all passes whose outputs can be observed *)
18inductive pass : Type[0] ≝
19   clight_pass: pass
20 | clight_switch_removed_pass: pass
21 | clight_label_pass: pass
22 | clight_simplified_pass: pass
23 | cminor_pass: pass
24 | rtlabs_pass: pass
25 | rtl_separate_pass: pass
26 | rtl_uniq_pass: pass
27 | ertl_pass: pass
28 | ltl_pass: pass
29 | lin_pass: pass
30 | assembly_pass: pass
31 | object_code_pass: pass.
32
33definition with_stack_model : Type[0] → Type[0] ≝
34 λA:Type[0].A × (ident → option ℕ).
35
36definition syntax_of_pass : pass → Type[0] ≝
37 λpass.
38  match pass with
39  [ clight_pass ⇒ clight_program
40  | clight_switch_removed_pass ⇒ clight_program
41  | clight_label_pass ⇒ clight_program
42  | clight_simplified_pass ⇒ clight_program
43  | cminor_pass ⇒ Cminor_program
44  | rtlabs_pass ⇒ RTLabs_program
45  | rtl_separate_pass ⇒ with_stack_model rtl_program
46  | rtl_uniq_pass ⇒ with_stack_model rtl_program
47  | ertl_pass ⇒ with_stack_model ertl_program
48  | ltl_pass ⇒ with_stack_model ltl_program
49  | lin_pass ⇒ with_stack_model lin_program
50  | assembly_pass ⇒
51     pseudo_assembly_program × (Word → Word) × (Word → bool)
52  | object_code_pass ⇒ labelled_object_code ].
53
54definition observe_pass ≝ ∀pass. syntax_of_pass pass → unit.
55
56(* The compiler front-end *)
57definition front_end :
58 observe_pass → clight_program → res (costlabel × clight_program × RTLabs_program) ≝
59λobserve,p.
60  let i ≝ observe clight_pass p in
61  let p ≝ program_switch_removal p in
62  let i ≝ observe clight_switch_removed_pass p in
63  let 〈p',init_cost〉 ≝ clight_label p in
64  let i ≝ observe clight_label_pass p' in
65  let p ≝ simplify_program p' in
66  let i ≝ observe clight_simplified_pass p in
67  ! p ← clight_to_cminor p;
68  let i ≝ observe cminor_pass p in
69  let p ≝ cminor_to_rtlabs p in
70  let i ≝ observe rtlabs_pass p in
71  if check_cost_program p then
72    if check_program_cost_injectivity p then
73      (return 〈init_cost,p',p〉)
74    else
75      (Error ? (msg RepeatedCostLabel))
76  else
77    (Error ? (msg BadCostLabelling)).
78
79(* The compiler back-end *)
80include "ERTL/uses.ma".  (* Included by the untrusted code *)
81axiom compute_fixpoint : fixpoint_computer.
82axiom colour_graph : coloured_graph_computer.
83
84include "common/AssocList.ma".
85
86(* Inefficient, replace with Trie lookup *)
87definition lookup_stack_cost ≝
88 λP,p,id.
89  assoc_list_lookup ?? id (eq_identifier …) (stack_cost P p).
90
91definition back_end :
92 observe_pass → costlabel → RTLabs_program →
93  res (pseudo_assembly_program × stack_cost_model × nat) ≝
94λobserve,init_cost,p.
95  let p ≝ rtlabs_to_rtl init_cost p in
96  let st ≝ lookup_stack_cost … p in
97  let i ≝ observe rtl_separate_pass 〈p,st〉 in
98  let i ≝ observe rtl_uniq_pass 〈p,st〉 in
99  let p ≝ rtl_to_ertl p in
100  let st ≝ lookup_stack_cost … p in 
101  let i ≝ observe ertl_pass 〈p,st〉 in
102  let 〈p,stack_cost,max_stack〉 ≝ ertl_to_ltl compute_fixpoint colour_graph p in
103  (* The two stack models are the same *)
104  let st ≝ lookup_stack_cost … p in
105  let i ≝ observe ltl_pass 〈p,st〉 in
106  let st ≝ lookup_stack_cost … p in
107  let p ≝ ltl_to_lin p in
108  let st ≝ lookup_stack_cost … p in
109  let i ≝ observe lin_pass 〈p,st〉 in
110   ! p ← opt_to_res ? (msg AssemblyTooLarge) (lin_to_asm p) ;
111   return 〈p,stack_cost,max_stack〉.
112
113(* The assembler *)
114include "ASM/Policy.ma".
115
116definition assembler :
117 observe_pass → pseudo_assembly_program → res labelled_object_code ≝
118λobserve,p.
119  ! sigma_pol ← opt_to_res ? (msg Jump_expansion_failed) (jump_expansion' p);
120  let sigma ≝ λppc. \fst sigma_pol ppc in
121  let pol ≝ λppc. \snd sigma_pol ppc in
122  let i ≝ observe assembly_pass 〈p,sigma,pol〉 in
123  let p ≝ assembly p sigma pol in
124  let i ≝ observe object_code_pass p in
125  OK ? p.
126
127(* Cost lifting *)
128include "ASM/ASMCosts.ma".
129
130definition lift_out_of_sigma :
131  ∀A,B : Type[0].∀P_out : A → Prop.B →
132    (∀a.P_out a + ¬ P_out a) →
133  ((Σa.P_out a) → B) → A → B ≝ λA,B,P_out,dflt,dec,m,a_sig.
134   match dec a_sig with
135   [ inl prf ⇒ m «a_sig, prf»
136   | inr _ ⇒ dflt (* labels not present in out code get 0 *)
137   ].
138
139definition lift_cost_map_back_to_front :
140  ∀oc.
141    let abstat ≝ OC_abstract_status oc in
142  as_cost_map abstat → clight_cost_map ≝
143  λoc,asm_cost_map.
144  lift_out_of_sigma … 0 (* labels not present in out code get 0 *)
145    (strong_decidable_in_codomain …) asm_cost_map.
146
147(* Cost model computation *)
148include "ASM/ASMCostsSplit.ma".
149
150record compiler_output : Type[0] ≝
151 { c_labelled_object_code: labelled_object_code
152 ; c_stack_cost: stack_cost_model
153 ; c_max_stack: nat
154 ; c_labelled_clight: clight_program
155 ; c_clight_cost_map: clight_cost_map
156 }.
157
158definition compile : observe_pass → clight_program → res compiler_output ≝
159λobserve,p.
160  ! 〈init_cost,p',p〉 ← front_end observe p;
161  ! 〈p,stack_cost,max_stack〉 ← back_end observe init_cost p;
162  ! p ← assembler observe p;
163  let k ≝ ASM_cost_map p in
164  let k' ≝
165   lift_cost_map_back_to_front p k in
166  return mk_compiler_output p stack_cost max_stack p' k'.
Note: See TracBrowser for help on using the repository browser.