1 | (* Front-end related includes *) |
---|
2 | include "Clight/label.ma". |
---|
3 | include "Clight/SimplifyCasts.ma". |
---|
4 | include "Clight/switchRemoval.ma". |
---|
5 | include "Clight/toCminor.ma". |
---|
6 | include "Cminor/toRTLabs.ma". |
---|
7 | include "RTLabs/CostCheck.ma". |
---|
8 | include "RTLabs/CostInj.ma". |
---|
9 | |
---|
10 | (* Back-end related includes *) |
---|
11 | include "RTLabs/RTLabsToRTL.ma". |
---|
12 | include "RTL/RTLToERTL.ma". |
---|
13 | include "ERTL/ERTLToLTL.ma". |
---|
14 | include "LTL/LTLToLIN.ma". |
---|
15 | include "LIN/LINToASM.ma". |
---|
16 | |
---|
17 | (* List of all passes whose outputs can be observed *) |
---|
18 | inductive 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 | |
---|
33 | definition with_stack_model : Type[0] → Type[0] ≝ |
---|
34 | λA:Type[0].A × (ident → option ℕ). |
---|
35 | |
---|
36 | definition 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 | |
---|
54 | definition observe_pass ≝ ∀pass. syntax_of_pass pass → unit. |
---|
55 | |
---|
56 | (* The compiler front-end *) |
---|
57 | definition 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 | ! WCL ← check_cost_program_prf p; |
---|
72 | ! INJ ← check_program_cost_injectivity_prf p; |
---|
73 | return 〈init_cost,p',p〉. |
---|
74 | |
---|
75 | (* The compiler back-end *) |
---|
76 | include "ERTL/uses.ma". (* Included by the untrusted code *) |
---|
77 | axiom compute_fixpoint : fixpoint_computer. |
---|
78 | axiom colour_graph : coloured_graph_computer. |
---|
79 | |
---|
80 | include "common/AssocList.ma". |
---|
81 | |
---|
82 | (* Inefficient, replace with Trie lookup *) |
---|
83 | definition lookup_stack_cost ≝ |
---|
84 | λP,p.stack_sizes … (stack_cost P p). |
---|
85 | |
---|
86 | definition back_end : |
---|
87 | observe_pass → costlabel → RTLabs_program → |
---|
88 | res (pseudo_assembly_program × costlabel × stack_cost_model × nat) ≝ |
---|
89 | λobserve,init_cost,p. |
---|
90 | let p ≝ rtlabs_to_rtl init_cost p in |
---|
91 | let st ≝ lookup_stack_cost … p in |
---|
92 | let i ≝ observe rtl_separate_pass 〈p,st〉 in |
---|
93 | let i ≝ observe rtl_uniq_pass 〈p,st〉 in |
---|
94 | let p ≝ rtl_to_ertl p in |
---|
95 | let st ≝ lookup_stack_cost … p in |
---|
96 | let i ≝ observe ertl_pass 〈p,st〉 in |
---|
97 | let 〈p,stack_cost,max_stack〉 ≝ ertl_to_ltl compute_fixpoint colour_graph p in |
---|
98 | (* The two stack models are the same *) |
---|
99 | let st ≝ lookup_stack_cost … p in |
---|
100 | let i ≝ observe ltl_pass 〈p,st〉 in |
---|
101 | let st ≝ lookup_stack_cost … p in |
---|
102 | let p ≝ ltl_to_lin p in |
---|
103 | let st ≝ lookup_stack_cost … p in |
---|
104 | let i ≝ observe lin_pass 〈p,st〉 in |
---|
105 | ! p ← opt_to_res ? (msg AssemblyTooLarge) (lin_to_asm p) ; |
---|
106 | return 〈〈p,init_cost〉,stack_cost,max_stack〉. |
---|
107 | |
---|
108 | (* The assembler *) |
---|
109 | include "ASM/Policy.ma". |
---|
110 | |
---|
111 | definition assembler : |
---|
112 | observe_pass → pseudo_assembly_program → res labelled_object_code ≝ |
---|
113 | λobserve,p. |
---|
114 | ! sigma_pol ← opt_to_res ? (msg Jump_expansion_failed) (jump_expansion' p); |
---|
115 | let sigma ≝ λppc. \fst sigma_pol ppc in |
---|
116 | let pol ≝ λppc. \snd sigma_pol ppc in |
---|
117 | let i ≝ observe assembly_pass 〈p,sigma,pol〉 in |
---|
118 | let p ≝ assembly p sigma pol in |
---|
119 | let i ≝ observe object_code_pass p in |
---|
120 | OK ? p. |
---|
121 | |
---|
122 | (* Cost model computation *) |
---|
123 | include "ASM/ASMCostsSplit.ma". |
---|
124 | |
---|
125 | record compiler_output : Type[0] ≝ |
---|
126 | { c_labelled_object_code: labelled_object_code |
---|
127 | ; c_stack_cost: stack_cost_model |
---|
128 | ; c_max_stack: nat |
---|
129 | ; c_init_costlabel: costlabel |
---|
130 | ; c_labelled_clight: clight_program |
---|
131 | ; c_clight_cost_map: clight_cost_map |
---|
132 | }. |
---|
133 | |
---|
134 | definition compile : observe_pass → clight_program → res compiler_output ≝ |
---|
135 | λobserve,p. |
---|
136 | ! 〈init_cost,p',p〉 ← front_end observe p; |
---|
137 | ! 〈p_init_costlabel,stack_cost,max_stack〉 ← back_end observe init_cost p; |
---|
138 | let 〈p,init_costlabel〉 ≝ p_init_costlabel in |
---|
139 | ! p ← assembler observe p; |
---|
140 | let k ≝ ASM_cost_map p in |
---|
141 | return mk_compiler_output p stack_cost max_stack init_costlabel p' k. |
---|