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 | 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 *) |
---|
80 | include "ERTLptr/uses.ma". (* Included by the untrusted code *) |
---|
81 | axiom compute_fixpoint : fixpoint_computer. |
---|
82 | axiom colour_graph : coloured_graph_computer. |
---|
83 | |
---|
84 | include "common/AssocList.ma". |
---|
85 | |
---|
86 | (* Inefficient, replace with Trie lookup *) |
---|
87 | definition lookup_stack_cost ≝ |
---|
88 | λP,p,id. |
---|
89 | assoc_list_lookup ?? id (eq_identifier …) (stack_cost P p). |
---|
90 | |
---|
91 | definition 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 *) |
---|
114 | include "ASM/Policy.ma". |
---|
115 | |
---|
116 | definition 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 *) |
---|
128 | include "ASM/ASMCosts.ma". |
---|
129 | |
---|
130 | definition lift_cost_map_back_to_front : |
---|
131 | ∀clight, oc. |
---|
132 | let abstat ≝ OC_abstract_status oc in |
---|
133 | as_cost_map abstat → clight_cost_map clight ≝ |
---|
134 | λclight,oc,k,asm_cost_map. |
---|
135 | lift_sigma_map_id … 0 (* labels not present in out code get 0 *) |
---|
136 | (strong_decidable_in_codomain …) k asm_cost_map. |
---|
137 | |
---|
138 | (* Cost model computation *) |
---|
139 | include "ASM/ASMCostsSplit.ma". |
---|
140 | |
---|
141 | record compiler_output : Type[0] ≝ |
---|
142 | { c_labelled_object_code: labelled_object_code |
---|
143 | ; c_stack_cost: stack_cost_model |
---|
144 | ; c_max_stack: nat |
---|
145 | ; c_labelled_clight: clight_program |
---|
146 | ; c_clight_cost_map: clight_cost_map c_labelled_clight |
---|
147 | }. |
---|
148 | |
---|
149 | definition compile : observe_pass → clight_program → res compiler_output ≝ |
---|
150 | λobserve,p. |
---|
151 | ! 〈init_cost,p',p〉 ← front_end observe p; |
---|
152 | ! 〈p,stack_cost,max_stack〉 ← back_end observe init_cost p; |
---|
153 | ! p ← assembler observe p; |
---|
154 | let k ≝ ASM_cost_map p in |
---|
155 | let k' ≝ |
---|
156 | lift_cost_map_back_to_front p' p k in |
---|
157 | return mk_compiler_output p stack_cost max_stack p' k'. |
---|