1 | include "ASM/I8051.ma". |
---|
2 | include "common/CostLabel.ma". |
---|
3 | include "common/AST.ma". |
---|
4 | include "common/Registers.ma". |
---|
5 | include "common/Graphs.ma". |
---|
6 | |
---|
7 | (* Here is the structure of parameter records (downward edges are coercions, |
---|
8 | the ↓ edges are the only explicitly defined coercions). lin_params and |
---|
9 | graph_params are simple wrappers of unserialized_params, and the coercions |
---|
10 | from them to params instantiate the missing bits with values for linarized |
---|
11 | programs and graph programs respectively. |
---|
12 | |
---|
13 | lin_params graph_params |
---|
14 | | \_____ /____ | |
---|
15 | | / \ | |
---|
16 | \_______ / ↓ ↓ |
---|
17 | \ _\____ params |
---|
18 | \_/ \ | |
---|
19 | / \ \ ↓ |
---|
20 | _____/ unserialized_params |
---|
21 | / _______/ | |
---|
22 | / / | |
---|
23 | stmt_params / local_params |
---|
24 | | __/ | |
---|
25 | inst_params funct_params |
---|
26 | |
---|
27 | inst_params : types needed to define instructions |
---|
28 | stmt_params : adds successor type needed to define statements |
---|
29 | funct_params : types of result register and parameters of function |
---|
30 | local_params : adds types of local registers |
---|
31 | params : adds type of code and lookup function |
---|
32 | graph_params : is made just of inst_params and local_params, and the coercion |
---|
33 | to params adds structure common to graph languages *) |
---|
34 | |
---|
35 | record inst_params : Type[1] ≝ |
---|
36 | { acc_a_reg: Type[0] (* registers that will eventually need to be A *) |
---|
37 | ; acc_b_reg: Type[0] (* registers that will eventually need to be B *) |
---|
38 | ; acc_a_arg: Type[0] (* arguments that will eventually need to be A *) |
---|
39 | ; acc_b_arg: Type[0] (* arguments that will eventually need to be B *) |
---|
40 | ; dpl_reg: Type[0] (* low address registers *) |
---|
41 | ; dph_reg: Type[0] (* high address registers *) |
---|
42 | ; dpl_arg: Type[0] (* low address registers *) |
---|
43 | ; dph_arg: Type[0] (* high address registers *) |
---|
44 | ; snd_arg : Type[0] (* second argument of binary op *) |
---|
45 | ; pair_move: Type[0] (* argument of move instructions *) |
---|
46 | ; call_args: Type[0] (* arguments of function calls *) |
---|
47 | ; call_dest: Type[0] (* possible destination of function computation *) |
---|
48 | ; ext_instruction: Type[0] (* other instructions not fitting in the general framework *) |
---|
49 | ; ext_forall_labels : (label → Prop) → ext_instruction → Prop |
---|
50 | ; ext_fin_instruction: Type[0] (* final instructions (e.g. tailcalls) not fitting in the general framework *) |
---|
51 | ; ext_fin_forall_labels : (label → Prop) → ext_fin_instruction → Prop |
---|
52 | }. |
---|
53 | |
---|
54 | inductive joint_instruction (p:inst_params) (globals: list ident): Type[0] ≝ |
---|
55 | | COMMENT: String → joint_instruction p globals |
---|
56 | | COST_LABEL: costlabel → joint_instruction p globals |
---|
57 | | MOVE: pair_move p → joint_instruction p globals |
---|
58 | | POP: acc_a_reg p → joint_instruction p globals |
---|
59 | | PUSH: acc_a_arg p → joint_instruction p globals |
---|
60 | | ADDRESS: ∀i: ident. (member i (eq_identifier ?) globals) → dpl_reg p → dph_reg p → joint_instruction p globals |
---|
61 | | OPACCS: OpAccs → acc_a_reg p → acc_b_reg p → acc_a_arg p → acc_b_arg p → joint_instruction p globals |
---|
62 | | OP1: Op1 → acc_a_reg p → acc_a_reg p → joint_instruction p globals |
---|
63 | | OP2: Op2 → acc_a_reg p → acc_a_arg p → snd_arg p → joint_instruction p globals |
---|
64 | (* int done with generic move *) |
---|
65 | (*| INT: generic_reg p → Byte → joint_instruction p globals *) |
---|
66 | | CLEAR_CARRY: joint_instruction p globals |
---|
67 | | SET_CARRY: joint_instruction p globals |
---|
68 | | LOAD: acc_a_reg p → dpl_arg p → dph_arg p → joint_instruction p globals |
---|
69 | | STORE: dpl_arg p → dph_arg p → acc_a_arg p → joint_instruction p globals |
---|
70 | | CALL_ID: ident → call_args p → call_dest p → joint_instruction p globals |
---|
71 | | COND: acc_a_reg p → label → joint_instruction p globals |
---|
72 | | extension: ext_instruction p → joint_instruction p globals. |
---|
73 | |
---|
74 | coercion extension_to_instruction : |
---|
75 | ∀p,globals.∀c : ext_instruction p.joint_instruction p globals ≝ |
---|
76 | extension |
---|
77 | on _c : ext_instruction ? to joint_instruction ??. |
---|
78 | |
---|
79 | notation "r ← a1 .op. a2" with precedence 50 for |
---|
80 | @{'op2 $op $r $a1 $a2}. |
---|
81 | notation "r ← . op . a" with precedence 50 for |
---|
82 | @{'op1 $op $r $a}. |
---|
83 | notation "r ← a" with precedence 50 for |
---|
84 | @{'mov $r $a}. (* to be set in individual languages *) |
---|
85 | notation "❮r, s❯ ← a1 . op . a2" with precedence 50 for |
---|
86 | @{'opaccs $op $r $s $a1 $a2}. |
---|
87 | |
---|
88 | interpretation "op2" 'op2 op r a1 a2 = (OP2 ? ? op r a1 a2). |
---|
89 | interpretation "op1" 'op1 op r a = (OP1 ? ? op r a). |
---|
90 | interpretation "opaccs" 'opaccs op r s a1 a2 = (OPACCS ? ? op r s a1 a2). |
---|
91 | |
---|
92 | |
---|
93 | definition inst_forall_labels : ∀p : inst_params.∀globals. |
---|
94 | (label → Prop) → joint_instruction p globals → Prop ≝ |
---|
95 | λp,g,P,inst. match inst with |
---|
96 | [ COND _ l ⇒ P l |
---|
97 | | extension ext_s ⇒ ext_forall_labels p P ext_s |
---|
98 | | _ ⇒ True |
---|
99 | ]. |
---|
100 | |
---|
101 | record funct_params : Type[1] ≝ |
---|
102 | { resultT : Type[0] |
---|
103 | ; paramsT : Type[0] |
---|
104 | }. |
---|
105 | |
---|
106 | record local_params : Type[1] ≝ |
---|
107 | { funct_pars :> funct_params |
---|
108 | ; localsT: Type[0] |
---|
109 | }. |
---|
110 | |
---|
111 | record unserialized_params : Type[1] ≝ |
---|
112 | { u_inst_pars :> inst_params |
---|
113 | ; u_local_pars :> local_params |
---|
114 | }. |
---|
115 | |
---|
116 | record stmt_params : Type[1] ≝ |
---|
117 | { unserialized_pars :> unserialized_params |
---|
118 | ; succ : Type[0] |
---|
119 | ; succ_forall_labels : (label → Prop) → succ → Prop |
---|
120 | }. |
---|
121 | |
---|
122 | inductive joint_statement (p: stmt_params) (globals: list ident): Type[0] ≝ |
---|
123 | | sequential: joint_instruction p globals → succ p → joint_statement p globals |
---|
124 | | GOTO: label → joint_statement p globals |
---|
125 | | RETURN: joint_statement p globals |
---|
126 | | extension_fin : ext_fin_instruction p → joint_statement p globals. |
---|
127 | |
---|
128 | coercion extension_fin_to_statement : |
---|
129 | ∀p : stmt_params.∀globals.∀c : ext_fin_instruction p.joint_statement p globals ≝ |
---|
130 | extension_fin |
---|
131 | on _c : ext_fin_instruction ? to joint_statement ??. |
---|
132 | |
---|
133 | record params : Type[1] ≝ |
---|
134 | { stmt_pars :> stmt_params |
---|
135 | ; codeT: list ident → Type[0] |
---|
136 | ; code_has_label: ∀globals.codeT globals → label → Prop |
---|
137 | ; forall_statements : ∀globals.(joint_statement stmt_pars globals → Prop) → |
---|
138 | codeT globals → Prop |
---|
139 | }. |
---|
140 | |
---|
141 | |
---|
142 | definition stmt_forall_labels : ∀p : stmt_params.∀globals. |
---|
143 | (label → Prop) → joint_statement p globals → Prop ≝ |
---|
144 | λp,g,P,stmt. match stmt with |
---|
145 | [ sequential inst next ⇒ inst_forall_labels p g P inst ∧ succ_forall_labels p P next |
---|
146 | | GOTO l ⇒ P l |
---|
147 | | RETURN ⇒ True |
---|
148 | | extension_fin c ⇒ ext_fin_forall_labels … P c |
---|
149 | ]. |
---|
150 | |
---|
151 | record lin_params : Type[1] ≝ |
---|
152 | { l_u_pars :> unserialized_params }. |
---|
153 | |
---|
154 | include "utilities/option.ma". |
---|
155 | |
---|
156 | definition lin_params_to_params ≝ |
---|
157 | λlp : lin_params. |
---|
158 | mk_params |
---|
159 | (mk_stmt_params lp unit (λ_.λ_.True)) |
---|
160 | (* codeT ≝ *)(λglobals.list ((option label) × (joint_statement ? globals))) |
---|
161 | (* code_has_label ≝ *)(λglobals,code,lbl.Exists ? |
---|
162 | (λl_stmt. \fst l_stmt = Some ? lbl) code) |
---|
163 | (* forall_statements ≝ *)(λglobals,P,code.All ? |
---|
164 | (λl_stmt. P (\snd l_stmt)) code). |
---|
165 | |
---|
166 | coercion lp_to_p : ∀lp : lin_params.params ≝ lin_params_to_params |
---|
167 | on _lp : lin_params to params. |
---|
168 | |
---|
169 | record graph_params : Type[1] ≝ |
---|
170 | { g_u_pars :> unserialized_params }. |
---|
171 | |
---|
172 | (* One common instantiation of params via Graphs of joint_statements |
---|
173 | (all languages but LIN) *) |
---|
174 | definition graph_params_to_params ≝ |
---|
175 | λgp : graph_params. |
---|
176 | mk_params |
---|
177 | (mk_stmt_params gp label (λP.P)) |
---|
178 | (* codeT ≝ *)(λglobals.graph (joint_statement ? globals)) |
---|
179 | (* code_has_label ≝ *)(λglobals,code,lbl.lookup … code lbl ≠ None ?) |
---|
180 | (* forall_statements ≝ *)(λglobals,P,code. |
---|
181 | ∀l,s.lookup … code l = Some ? s → P s). |
---|
182 | |
---|
183 | coercion gp_to_p : ∀gp:graph_params.params ≝ graph_params_to_params |
---|
184 | on _gp : graph_params to params. |
---|
185 | |
---|
186 | |
---|
187 | definition labels_present : ∀globals.∀p : params. |
---|
188 | codeT p globals → (joint_statement p globals) → Prop ≝ |
---|
189 | λglobals,p,code,s. stmt_forall_labels p globals |
---|
190 | (λl.code_has_label ?? code l) s. |
---|
191 | |
---|
192 | definition code_closed : ∀p : params.∀globals. |
---|
193 | codeT p globals → Prop ≝ λp,globals,code. |
---|
194 | forall_statements … (labels_present … code) code. |
---|
195 | |
---|
196 | (* CSC: special case where localsT is a list of registers (RTL and ERTL) *) |
---|
197 | definition rtl_ertl_params : ?→?→params ≝ λinst_pars,funct_pars. |
---|
198 | (mk_graph_params (mk_unserialized_params inst_pars (mk_local_params funct_pars (list register)))). |
---|
199 | |
---|
200 | record joint_internal_function (globals: list ident) (p:params) : Type[0] ≝ |
---|
201 | { joint_if_luniverse: universe LabelTag; (*CSC: used only for compilation*) |
---|
202 | joint_if_runiverse: universe RegisterTag; (*CSC: used only for compilation*) |
---|
203 | (* Paolo: if we want this machinery to work for RTLabs too, we will need the |
---|
204 | following, right? *) |
---|
205 | (* joint_if_sig: signature; -- dropped in front end *) |
---|
206 | joint_if_result : resultT p; |
---|
207 | joint_if_params : paramsT p; |
---|
208 | joint_if_locals : localsT p; |
---|
209 | (*CSC: XXXXX stacksize unused for LTL-...*) |
---|
210 | joint_if_stacksize: nat; |
---|
211 | joint_if_code : codeT p globals ; |
---|
212 | (*CSC: XXXXX entry unused for LIN, but invariant in that case... *) |
---|
213 | joint_if_entry : Σl: label. code_has_label … joint_if_code l; |
---|
214 | (*CSC: XXXXX exit only used up to RTL (and only for tailcall removal) *) |
---|
215 | joint_if_exit : Σl: label. code_has_label … … joint_if_code l |
---|
216 | }. |
---|
217 | |
---|
218 | definition joint_closed_internal_function ≝ |
---|
219 | λglobals,p. |
---|
220 | Σdef : joint_internal_function globals p. code_closed … (joint_if_code … def). |
---|
221 | |
---|
222 | (* Currified form *) |
---|
223 | definition set_joint_if_exit ≝ |
---|
224 | λglobals,pars. |
---|
225 | λexit: label. |
---|
226 | λp:joint_internal_function globals pars. |
---|
227 | λprf: code_has_label … (joint_if_code … p) exit. |
---|
228 | mk_joint_internal_function globals pars |
---|
229 | (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p) |
---|
230 | (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p) |
---|
231 | (joint_if_code … p) (joint_if_entry … p) (mk_Sig … exit prf). |
---|
232 | |
---|
233 | definition set_joint_code ≝ |
---|
234 | λglobals: list ident. |
---|
235 | λpars: params. |
---|
236 | λint_fun: joint_internal_function globals pars. |
---|
237 | λgraph: codeT pars globals. |
---|
238 | λentry. |
---|
239 | λexit. |
---|
240 | mk_joint_internal_function globals pars |
---|
241 | (joint_if_luniverse … int_fun) (joint_if_runiverse … int_fun) (joint_if_result … int_fun) |
---|
242 | (joint_if_params … int_fun) (joint_if_locals … int_fun) (joint_if_stacksize … int_fun) |
---|
243 | graph entry exit. |
---|
244 | |
---|
245 | definition set_joint_if_graph ≝ |
---|
246 | λglobals,pars. |
---|
247 | λgraph. |
---|
248 | λp:joint_internal_function globals pars. |
---|
249 | λentry_prf: code_has_label … graph (joint_if_entry … p). |
---|
250 | λexit_prf: code_has_label … graph (joint_if_exit … p). |
---|
251 | set_joint_code globals pars p |
---|
252 | graph |
---|
253 | (mk_Sig … (joint_if_entry … p) entry_prf) |
---|
254 | (mk_Sig … (joint_if_exit … p) exit_prf). |
---|
255 | |
---|
256 | definition set_luniverse ≝ |
---|
257 | λglobals,pars. |
---|
258 | λp : joint_internal_function globals pars. |
---|
259 | λluniverse: universe LabelTag. |
---|
260 | mk_joint_internal_function globals pars |
---|
261 | luniverse (joint_if_runiverse … p) (joint_if_result … p) |
---|
262 | (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p) |
---|
263 | (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p). |
---|
264 | |
---|
265 | definition set_runiverse ≝ |
---|
266 | λglobals,pars. |
---|
267 | λp : joint_internal_function globals pars. |
---|
268 | λruniverse: universe RegisterTag. |
---|
269 | mk_joint_internal_function globals pars |
---|
270 | (joint_if_luniverse … p) runiverse (joint_if_result … p) |
---|
271 | (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p) |
---|
272 | (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p). |
---|
273 | |
---|
274 | (* Paolo: probably should move these elsewhere *) |
---|
275 | definition graph_dom ≝ λX.λg : graph X.Σl : label.lookup … g l ≠ None ?. |
---|
276 | definition graph_dom_add_incl : ∀X.∀g : graph X.∀l : label.∀x : X. |
---|
277 | graph_dom ? g → graph_dom ? (add … g l x) ≝ λX,g,l,x,sig_l. |
---|
278 | mk_Sig … (pi1 ?? sig_l) ?. |
---|
279 | @graph_add_lookup |
---|
280 | @(pi2 … sig_l) |
---|
281 | qed. |
---|
282 | |
---|
283 | (* Specialized for graph_params *) |
---|
284 | definition add_graph ≝ |
---|
285 | λg_pars : graph_params.λglobals.λl:label.λstmt. |
---|
286 | λp:joint_internal_function globals g_pars. |
---|
287 | let code ≝ add … (joint_if_code … p) l stmt in |
---|
288 | mk_joint_internal_function ? g_pars |
---|
289 | (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p) |
---|
290 | (joint_if_params … p) (joint_if_locals … p) (joint_if_stacksize … p) |
---|
291 | code |
---|
292 | (graph_dom_add_incl … (joint_if_entry … p)) |
---|
293 | (graph_dom_add_incl … (joint_if_exit … p)). |
---|
294 | |
---|
295 | definition set_locals ≝ |
---|
296 | λglobals,pars. |
---|
297 | λp : joint_internal_function globals pars. |
---|
298 | λlocals. |
---|
299 | mk_joint_internal_function globals pars |
---|
300 | (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p) |
---|
301 | (joint_if_params … p) locals (joint_if_stacksize … p) |
---|
302 | (joint_if_code … p) (joint_if_entry … p) (joint_if_exit … p). |
---|
303 | |
---|
304 | definition joint_function ≝ λp,globals. fundef (joint_internal_function p globals). |
---|
305 | |
---|
306 | definition joint_program ≝ |
---|
307 | λp:params. program (λglobals. joint_function globals p) nat. |
---|