1 | (**************************************************************************) |
---|
2 | (* ___ *) |
---|
3 | (* ||M|| *) |
---|
4 | (* ||A|| A project by Andrea Asperti *) |
---|
5 | (* ||T|| *) |
---|
6 | (* ||I|| Developers: *) |
---|
7 | (* ||T|| The HELM team. *) |
---|
8 | (* ||A|| http://helm.cs.unibo.it *) |
---|
9 | (* \ / *) |
---|
10 | (* \ / This file is distributed under the terms of the *) |
---|
11 | (* v GNU General Public License Version 2 *) |
---|
12 | (* *) |
---|
13 | (**************************************************************************) |
---|
14 | |
---|
15 | include "joint/Joint.ma". |
---|
16 | |
---|
17 | inductive keyword : Type[0] ≝ |
---|
18 | | kwCOMMENT: keyword |
---|
19 | | kwMOVE: keyword |
---|
20 | | kwPOP: keyword |
---|
21 | | kwPUSH: keyword |
---|
22 | | kwADDRESS: keyword |
---|
23 | | kwOPACCS: keyword |
---|
24 | | kwOP1: keyword |
---|
25 | | kwOP2: keyword |
---|
26 | | kwCLEAR_CARRY: keyword |
---|
27 | | kwSET_CARRY: keyword |
---|
28 | | kwLOAD: keyword |
---|
29 | | kwSTORE: keyword |
---|
30 | | kwCOST_LABEL: keyword |
---|
31 | | kwCOND: keyword |
---|
32 | | kwCALL: keyword |
---|
33 | | kwGOTO: keyword |
---|
34 | | kwRETURN: keyword |
---|
35 | | kwTAILCALL: keyword |
---|
36 | | kwFCOND: keyword. |
---|
37 | |
---|
38 | record printing_pass_independent_params (string: Type[0]) : Type[0] ≝ |
---|
39 | { print_String: String → string |
---|
40 | ; print_keyword: keyword → string |
---|
41 | ; print_concat: string → string → string |
---|
42 | ; print_empty: string |
---|
43 | ; print_ident: ident → string |
---|
44 | ; print_costlabel: costlabel → string |
---|
45 | ; print_label: label → string |
---|
46 | ; print_OpAccs: OpAccs → string |
---|
47 | ; print_Op1: Op1 → string |
---|
48 | ; print_Op2: Op2 → string }. |
---|
49 | |
---|
50 | record printing_params (string: Type[0]) (P: unserialized_params) : Type[0] ≝ |
---|
51 | { print_pass_ind:> printing_pass_independent_params string |
---|
52 | ; print_acc_a_reg: acc_a_reg P → string |
---|
53 | ; print_acc_b_reg: acc_b_reg P → string |
---|
54 | ; print_acc_a_arg: acc_a_arg P → string |
---|
55 | ; print_acc_b_arg: acc_b_arg P → string |
---|
56 | ; print_dpl_reg: dpl_reg P → string (* low address registers *) |
---|
57 | ; print_dph_reg: dph_reg P → string (* high address registers *) |
---|
58 | ; print_dpl_arg: dpl_arg P → string (* low address registers *) |
---|
59 | ; print_dph_arg: dph_arg P → string (* high address registers *) |
---|
60 | ; print_snd_arg : snd_arg P → string (* second argument of binary op *) |
---|
61 | ; print_pair_move: pair_move P → string (* argument of move instructions *) |
---|
62 | ; print_call_args: call_args P → string (* arguments of function calls *) |
---|
63 | ; print_call_dest: call_dest P → string (* possible destination of function computation *) |
---|
64 | (* other instructions not fitting in the general framework *) |
---|
65 | ; print_ext_seq : ext_seq P → string |
---|
66 | }. |
---|
67 | |
---|
68 | record code_iteration_params (string: Type[0]) (p:params) (globals) : Type[1] ≝ |
---|
69 | { fold_code: |
---|
70 | ∀A:Type[0]. |
---|
71 | (code_point p → joint_statement p globals → A → A) → |
---|
72 | codeT p globals → A → A |
---|
73 | ; print_succ: succ … p → string |
---|
74 | ; print_code_point: code_point p → string |
---|
75 | }. |
---|
76 | |
---|
77 | definition pm_choose_with_pref: |
---|
78 | ∀A:Type[0]. positive_map A → option Pos → option (Pos × A × (positive_map A)) ≝ |
---|
79 | λA,M,n. |
---|
80 | match n with |
---|
81 | [ None ⇒ pm_choose A M |
---|
82 | | Some p ⇒ |
---|
83 | match pm_try_remove A p M with |
---|
84 | [ None ⇒ pm_choose A M |
---|
85 | | Some res ⇒ let 〈a,M'〉 ≝ res in Some … 〈p,a,M'〉 ]]. |
---|
86 | |
---|
87 | let rec visit_graph (A:Type[0]) (next: A → option Pos) (B:Type[0]) |
---|
88 | (f: Pos → A → B → B) (b:B) (n: option Pos) (M: positive_map A) (x:nat) on x : B ≝ |
---|
89 | match x with |
---|
90 | [ O ⇒ b (* Dummy, this should never happen for a large enough value of x *) |
---|
91 | | S y ⇒ |
---|
92 | match pm_choose_with_pref A M n with |
---|
93 | [ None ⇒ b |
---|
94 | | Some res ⇒ |
---|
95 | let 〈pos,a,M'〉 ≝ res in |
---|
96 | visit_graph A next B f (f pos a b) (next a) M' y ]]. |
---|
97 | |
---|
98 | definition graph_code_iteration_params: |
---|
99 | ∀string,graph_params,globals. |
---|
100 | printing_params string (graph_params_to_params graph_params) → |
---|
101 | code_iteration_params string (graph_params_to_params graph_params) globals ≝ |
---|
102 | λstring,gp,globals,pp. |
---|
103 | mk_code_iteration_params ??? |
---|
104 | (λA.λf:code_point (graph_params_to_params …) → ?.λM.λa. |
---|
105 | visit_graph (joint_statement … gp globals) |
---|
106 | (λstmt. match stmt_implicit_label ?? stmt with |
---|
107 | [ None ⇒ None … | Some label ⇒ match label with [an_identifier p ⇒ Some … p]]) |
---|
108 | ? (λn. f (an_identifier … n)) a (Some … one) |
---|
109 | (match M with [an_id_map M' ⇒ M']) (id_map_size … M)) |
---|
110 | (print_label … pp) (print_label … pp). |
---|
111 | |
---|
112 | definition print_list: |
---|
113 | ∀string. printing_pass_independent_params string → list string → string ≝ |
---|
114 | λstring,pp. |
---|
115 | foldr ?? (print_concat … pp) (print_empty … pp). |
---|
116 | |
---|
117 | definition print_joint_seq : |
---|
118 | ∀p. ∀globals. ∀string. printing_params string p → joint_seq p globals → string ≝ |
---|
119 | λp,globals,string,pp,seq. |
---|
120 | match seq with |
---|
121 | [ COMMENT str ⇒ |
---|
122 | print_list … pp [print_keyword … pp kwCOMMENT; print_String … pp str] |
---|
123 | | MOVE pm ⇒ |
---|
124 | print_list … pp [print_keyword … pp kwMOVE; print_pair_move … pp pm] |
---|
125 | | POP arg ⇒ |
---|
126 | print_list … pp [print_keyword … pp kwPOP; print_acc_a_reg … pp arg] |
---|
127 | | PUSH arg ⇒ |
---|
128 | print_list … pp [print_keyword … pp kwPUSH; print_acc_a_arg … pp arg] |
---|
129 | | ADDRESS i Hi arg1 arg2 ⇒ |
---|
130 | print_list … pp |
---|
131 | [print_keyword … pp kwADDRESS; |
---|
132 | print_ident … pp i; |
---|
133 | print_dpl_reg … pp arg1; |
---|
134 | print_dph_reg … pp arg2] |
---|
135 | | OPACCS opa arg1 arg2 arg3 arg4 ⇒ |
---|
136 | print_list … pp |
---|
137 | [print_keyword … pp kwOPACCS; |
---|
138 | print_OpAccs … pp opa; |
---|
139 | print_acc_a_reg … pp arg1; |
---|
140 | print_acc_b_reg … pp arg2; |
---|
141 | print_acc_a_arg … pp arg3; |
---|
142 | print_acc_b_arg … pp arg4] |
---|
143 | | OP1 op1 arg1 arg2 ⇒ |
---|
144 | print_list … pp |
---|
145 | [print_keyword … pp kwOP1; |
---|
146 | print_Op1 … pp op1; |
---|
147 | print_acc_a_reg … pp arg1; |
---|
148 | print_acc_a_reg … pp arg2] |
---|
149 | | OP2 op2 arg1 arg2 arg3 ⇒ |
---|
150 | print_list … pp |
---|
151 | [print_keyword … pp kwOP2; |
---|
152 | print_Op2 … pp op2; |
---|
153 | print_acc_a_reg … pp arg1; |
---|
154 | print_acc_a_arg … pp arg2; |
---|
155 | print_snd_arg … pp arg3] |
---|
156 | (* int done with generic move *) |
---|
157 | | CLEAR_CARRY ⇒ print_keyword … pp kwCLEAR_CARRY |
---|
158 | | SET_CARRY ⇒ print_keyword … pp kwSET_CARRY |
---|
159 | | LOAD arg1 arg2 arg3 ⇒ |
---|
160 | print_list … pp |
---|
161 | [print_keyword … pp kwLOAD; |
---|
162 | print_acc_a_reg … pp arg1; |
---|
163 | print_dpl_arg … pp arg2; |
---|
164 | print_dph_arg … pp arg3] |
---|
165 | | STORE arg1 arg2 arg3 ⇒ |
---|
166 | print_list … pp |
---|
167 | [print_keyword … pp kwSTORE; |
---|
168 | print_dpl_arg … pp arg1; |
---|
169 | print_dph_arg … pp arg2; |
---|
170 | print_acc_a_arg … pp arg3] |
---|
171 | | extension_seq ext ⇒ print_ext_seq … pp ext]. |
---|
172 | |
---|
173 | definition print_joint_step: |
---|
174 | ∀p,globals. ∀string. printing_params string p → joint_step p globals → string ≝ |
---|
175 | λp,globals,string,pp,step. |
---|
176 | match step with |
---|
177 | [ COST_LABEL arg ⇒ |
---|
178 | print_list ? pp |
---|
179 | [print_keyword … pp kwCOST_LABEL; |
---|
180 | print_costlabel … pp arg] |
---|
181 | | CALL arg1 arg2 arg3 ⇒ |
---|
182 | print_list … pp |
---|
183 | [print_keyword … pp kwCALL; |
---|
184 | match arg1 with |
---|
185 | [ inl id ⇒ print_ident … pp id |
---|
186 | | inr arg11_arg12 ⇒ |
---|
187 | print_concat … pp |
---|
188 | (print_dpl_arg … pp (\fst arg11_arg12)) |
---|
189 | (print_dph_arg … pp (\snd arg11_arg12))]; |
---|
190 | print_call_args … pp arg2; |
---|
191 | print_call_dest … pp arg3] |
---|
192 | | COND arg1 arg2 ⇒ |
---|
193 | print_list … pp |
---|
194 | [print_keyword … pp kwCOND; |
---|
195 | print_acc_a_reg … pp arg1; |
---|
196 | print_label … pp arg2] |
---|
197 | | step_seq seq ⇒ print_joint_seq … pp seq ]. |
---|
198 | |
---|
199 | definition print_joint_fin_step: |
---|
200 | ∀p. ∀string. printing_params string p → joint_fin_step p → string ≝ |
---|
201 | λp,string,pp,fin. |
---|
202 | match fin with |
---|
203 | [ GOTO l ⇒ |
---|
204 | print_list ? pp |
---|
205 | [ print_keyword … pp kwGOTO; |
---|
206 | print_label … pp l] |
---|
207 | | RETURN ⇒ print_keyword … pp kwRETURN |
---|
208 | | TAILCALL has arg1 arg2 ⇒ |
---|
209 | print_list … pp |
---|
210 | [print_keyword … pp kwTAILCALL; |
---|
211 | match arg1 with |
---|
212 | [ inl id ⇒ print_ident … pp id |
---|
213 | | inr arg11_arg12 ⇒ |
---|
214 | print_concat … pp |
---|
215 | (print_dpl_arg … pp (\fst arg11_arg12)) |
---|
216 | (print_dph_arg … pp (\snd arg11_arg12))]; |
---|
217 | print_call_args … pp arg2]]. |
---|
218 | |
---|
219 | definition print_joint_statement: |
---|
220 | ∀p:params.∀globals. ∀string. printing_params string p → |
---|
221 | code_iteration_params string p globals → joint_statement p globals → string ≝ |
---|
222 | λp,globals,string,pp,cip,stmt. |
---|
223 | match stmt with |
---|
224 | [ sequential js arg1 ⇒ |
---|
225 | print_concat … pp (print_joint_step … pp js) (print_succ … cip arg1) |
---|
226 | | final fin ⇒ print_joint_fin_step … pp fin |
---|
227 | | FCOND has arg1 arg2 arg3 ⇒ |
---|
228 | print_list ? pp |
---|
229 | [ print_keyword … pp kwFCOND; |
---|
230 | print_acc_a_reg … pp arg1; |
---|
231 | print_label … pp arg2; |
---|
232 | print_label … pp arg3 ]]. |
---|
233 | |
---|
234 | definition print_joint_internal_function: |
---|
235 | ∀p:params. ∀globals. ∀string. code_iteration_params string p globals → |
---|
236 | printing_params string p → joint_internal_function p globals → list string |
---|
237 | ≝ λp,globals,string,cip,pp,f. |
---|
238 | fold_code … cip … |
---|
239 | (λcp.λstmt.λacc. |
---|
240 | print_list … pp |
---|
241 | [ print_code_point … cip cp; |
---|
242 | print_joint_statement … pp cip stmt ]::acc) |
---|
243 | (joint_if_code … f) []. |
---|
244 | |
---|
245 | definition print_joint_function: |
---|
246 | ∀p:params. ∀globals. ∀string. code_iteration_params string p globals → printing_params string p → |
---|
247 | joint_function p globals → list string ≝ |
---|
248 | λp,globals,string,cip,pp,f. |
---|
249 | match f with |
---|
250 | [ Internal f ⇒ print_joint_internal_function … cip … pp f |
---|
251 | | External f ⇒ [] ]. |
---|
252 | |
---|
253 | definition print_joint_program: |
---|
254 | ∀p:params. ∀string. |
---|
255 | printing_params string p → |
---|
256 | ∀prog:joint_program p. |
---|
257 | code_iteration_params string p (map … (λx. \fst (\fst x)) (prog_vars ?? prog)) → |
---|
258 | list (ident × (list string)) ≝ |
---|
259 | λp,string,pp,prog,cip. |
---|
260 | foldr ?? (λf,acc. 〈\fst f,print_joint_function … cip … pp (\snd f)〉 :: acc) |
---|
261 | [] (prog_funct … prog). |
---|