source: src/joint/joint_printer.ma @ 3145

Last change on this file since 3145 was 3042, checked in by sacerdot, 7 years ago

Repaired.

File size: 12.0 KB
Line 
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
15include "joint/Joint.ma".
16
17inductive 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
38record 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 ; print_nat: nat → string
50 ; print_bitvector: ∀n. BitVector n → string }.
51
52record printing_params (string: Type[0]) (P: unserialized_params) : Type[0] ≝
53 { print_pass_ind:> printing_pass_independent_params string
54 ; print_acc_a_reg: acc_a_reg P → string
55 ; print_acc_b_reg: acc_b_reg P → string
56 ; print_acc_a_arg: acc_a_arg P → string
57 ; print_acc_b_arg: acc_b_arg P → string
58 ; print_dpl_reg: dpl_reg P → string   (* low address registers *)
59 ; print_dph_reg: dph_reg P → string   (* high address registers *)
60 ; print_dpl_arg: dpl_arg P → string   (* low address registers *)
61 ; print_dph_arg: dph_arg P → string   (* high address registers *)
62 ; print_snd_arg : snd_arg P → string  (* second argument of binary op *)
63 ; print_pair_move: pair_move P → string (* argument of move instructions *)
64 ; print_call_args: call_args P → string (* arguments of function calls *)
65 ; print_call_dest: call_dest P → string (* possible destination of function computation *)
66 (* other instructions not fitting in the general framework *)
67 ; print_ext_seq : ext_seq P → string
68 }.
69
70record print_serialization_params (string: Type[0]) (p:params) : Type[1] ≝
71 { print_succ: succ … p → string
72 ; print_code_point: code_point p → string
73 }.
74
75record code_iteration_params (string: Type[0]) (p:params) (statementT: Type[0])
76  (globals) : Type[1] ≝
77 { cip_print_serialization_params :> print_serialization_params string p
78 ; fold_code:
79    ∀A:Type[0]. (code_point p → statementT → A → A) → codeT p globals →
80     code_point p → A → A
81 ; print_statementT: statementT → string
82 }.
83
84definition pm_choose_with_pref:
85 ∀A:Type[0]. positive_map A → option Pos → option (Pos × A × (positive_map A)) ≝
86 λA,M,n.
87  match n with
88  [ None ⇒ pm_choose A M
89  | Some p ⇒
90     match pm_try_remove A p M with
91     [ None ⇒ pm_choose A M
92     | Some res ⇒ let 〈a,M'〉 ≝ res in Some … 〈p,a,M'〉 ]].
93
94let rec visit_graph (A:Type[0]) (next: A → option Pos) (B:Type[0])
95 (f: Pos → A → B → B) (b:B) (n: option Pos) (M: positive_map A) (x:nat) on x : B ≝
96 match x with
97 [ O ⇒ b (* Dummy, this should never happen for a large enough value of x *)
98 | S y ⇒
99    match pm_choose_with_pref A M n with
100    [ None ⇒ b
101    | Some res ⇒
102       let 〈pos,a,M'〉 ≝ res in
103        visit_graph A next B f (f pos a b) (next a) M' y ]].
104
105definition print_list:
106 ∀string. printing_pass_independent_params string → list string → string ≝
107 λstring,pp.
108  foldr ?? (print_concat … pp) (print_empty … pp). 
109
110definition print_joint_seq :
111 ∀p. ∀globals. ∀string. printing_params string p → joint_seq p globals → string ≝
112 λp,globals,string,pp,seq.
113  match seq with
114  [ COMMENT str ⇒
115     print_list … pp [print_keyword … pp kwCOMMENT; print_String … pp str]
116  | MOVE pm ⇒
117     print_list … pp [print_keyword … pp kwMOVE; print_pair_move … pp pm]
118  | POP arg ⇒
119     print_list … pp [print_keyword … pp kwPOP; print_acc_a_reg … pp arg]
120  | PUSH arg ⇒
121     print_list … pp [print_keyword … pp kwPUSH; print_acc_a_arg … pp arg]
122  | ADDRESS i Hi offset arg1 arg2 ⇒
123     print_list … pp
124      [print_keyword … pp kwADDRESS;
125       print_ident … pp i;
126       print_bitvector … pp … offset;
127       print_dpl_reg … pp arg1;
128       print_dph_reg … pp arg2]
129  | OPACCS opa arg1 arg2 arg3 arg4 ⇒
130     print_list … pp
131      [print_keyword … pp kwOPACCS;
132       print_OpAccs … pp opa;
133       print_acc_a_reg … pp arg1;
134       print_acc_b_reg … pp arg2;
135       print_acc_a_arg … pp arg3;
136       print_acc_b_arg … pp arg4]
137  | OP1 op1 arg1 arg2 ⇒
138     print_list … pp
139      [print_keyword … pp kwOP1;
140       print_Op1 … pp op1;
141       print_acc_a_reg … pp arg1;
142       print_acc_a_reg … pp arg2]
143  | OP2 op2 arg1 arg2 arg3 ⇒
144     print_list … pp
145      [print_keyword … pp kwOP2;
146       print_Op2 … pp op2;
147       print_acc_a_reg … pp arg1;
148       print_acc_a_arg … pp arg2;
149       print_snd_arg … pp arg3]
150  (* int done with generic move *)
151  | CLEAR_CARRY ⇒ print_keyword … pp kwCLEAR_CARRY
152  | SET_CARRY ⇒ print_keyword … pp kwSET_CARRY
153  | LOAD arg1 arg2 arg3 ⇒
154     print_list … pp
155      [print_keyword … pp kwLOAD;
156       print_acc_a_reg … pp arg1;
157       print_dpl_arg … pp arg2;
158       print_dph_arg … pp arg3]
159  | STORE arg1 arg2 arg3 ⇒
160     print_list … pp
161      [print_keyword … pp kwSTORE;
162       print_dpl_arg … pp arg1;
163       print_dph_arg … pp arg2;
164       print_acc_a_arg … pp arg3]
165  | extension_seq ext ⇒ print_ext_seq … pp ext].
166
167definition print_joint_step:
168 ∀p,globals. ∀string. printing_params string p → joint_step p globals → string ≝
169 λp,globals,string,pp,step.
170  match step with
171   [ COST_LABEL arg ⇒
172      print_list ? pp
173       [print_keyword … pp kwCOST_LABEL;
174        print_costlabel … pp arg]
175   | CALL arg1 arg2 arg3 ⇒
176      print_list … pp
177       [print_keyword … pp kwCALL;
178        match arg1 with
179        [ inl id ⇒ print_ident … pp id
180        | inr arg11_arg12 ⇒
181           print_concat … pp
182            (print_dpl_arg … pp (\fst arg11_arg12))
183            (print_dph_arg … pp (\snd arg11_arg12))];
184        print_call_args … pp arg2;
185        print_call_dest … pp arg3]
186   | COND arg1 arg2 ⇒
187      print_list … pp
188       [print_keyword … pp kwCOND;
189        print_acc_a_reg … pp arg1;
190        print_label … pp arg2]
191   | step_seq seq ⇒ print_joint_seq … pp seq ].
192
193definition print_joint_fin_step:
194 ∀p. ∀string. printing_params string p → joint_fin_step p → string ≝
195 λp,string,pp,fin.
196  match fin with
197   [ GOTO l ⇒
198      print_list ? pp
199       [ print_keyword … pp kwGOTO;
200         print_label … pp l]
201   | RETURN ⇒ print_keyword … pp kwRETURN
202   | TAILCALL has arg1 arg2 ⇒
203      print_list … pp
204       [print_keyword … pp kwTAILCALL;
205        match arg1 with
206        [ inl id ⇒ print_ident … pp id
207        | inr arg11_arg12 ⇒
208           print_concat … pp
209            (print_dpl_arg … pp (\fst arg11_arg12))
210            (print_dph_arg … pp (\snd arg11_arg12))];
211        print_call_args … pp arg2]].
212
213definition print_joint_statement:
214 ∀p:params.∀globals. ∀string. printing_params string p →
215  print_serialization_params string p → joint_statement p globals → string ≝
216 λp,globals,string,pp,cip,stmt.
217  match stmt with
218  [ sequential js arg1 ⇒
219     print_concat … pp (print_joint_step … pp js) (print_succ … cip arg1)
220  | final fin ⇒ print_joint_fin_step … pp fin
221  | FCOND has arg1 arg2 arg3 ⇒
222     print_list ? pp
223      [ print_keyword … pp kwFCOND;
224        print_acc_a_reg … pp arg1;
225        print_label … pp arg2;
226        print_label … pp arg3 ]].
227
228definition graph_print_serialization_params:
229 ∀string,graph_params.
230  printing_params string (graph_params_to_params graph_params) →
231   print_serialization_params string (graph_params_to_params graph_params) ≝
232 λstring,gp,pp.
233  mk_print_serialization_params … (print_label … pp) (print_label … pp).
234
235definition graph_code_iteration_params:
236 ∀string,graph_params,globals.
237  printing_params string (graph_params_to_params graph_params) →
238   code_iteration_params string (graph_params_to_params graph_params) … globals ≝
239 λstring,gp,globals,pp.
240  mk_code_iteration_params …
241   (graph_print_serialization_params … pp)
242   (λA.λf:code_point (graph_params_to_params …) → ?.λM.λinitl.λa.
243     visit_graph (joint_statement … gp globals)
244     (λstmt. match stmt_implicit_label ?? stmt with
245       [ None ⇒ None … | Some label ⇒ match label with [an_identifier p ⇒ Some … p]])
246     ? (λn. f (an_identifier … n)) a
247     (Some … (word_of_identifier … initl))
248     (match M with [an_id_map M' ⇒ M']) (id_map_size … M))
249     (print_joint_statement … pp … (graph_print_serialization_params … pp)).
250
251definition lin_print_serialization_params:
252 ∀string,lin_params.
253  printing_params string (lin_params_to_params lin_params) →
254   print_serialization_params string (lin_params_to_params lin_params) ≝
255 λstring,gp,pp.
256  mk_print_serialization_params … (λ_.print_empty … pp) (print_nat … pp).
257
258definition lin_code_iteration_params:
259 ∀string,lin_params,globals.
260  printing_params string (lin_params_to_params lin_params) →
261   code_iteration_params string (lin_params_to_params lin_params) ? globals ≝
262 λstring,lp,globals,pp.
263  mk_code_iteration_params ?? (? × (joint_statement ??)) ?
264   (lin_print_serialization_params … pp)
265   (λA.λf:code_point (lin_params_to_params …) → ?.λM:codeT lp globals.λ_.
266    λa.
267     \snd (foldl ??
268      (λres,x. let 〈pc,res'〉 ≝ res in 〈S pc,f pc x res'〉)
269      〈0,a〉 M))
270     (λlinstr.
271       match \fst linstr with
272       [ None ⇒
273          print_joint_statement … pp … (lin_print_serialization_params … pp)
274           (\snd linstr)
275       | Some l ⇒
276          print_list ? pp
277           [ print_label … pp l;
278             print_joint_statement … pp … (lin_print_serialization_params … pp)
279              (\snd linstr) ]]).
280
281definition print_joint_internal_function:
282 ∀p:params. ∀lines. ∀globals. ∀string. code_iteration_params string p lines globals →
283  printing_params string p → joint_internal_function p globals → list string
284≝ λp,lines,globals,string,cip,pp,f.
285   fold_code … cip …
286    (λcp.λstmt.λacc.
287      print_list ? pp
288       [ print_code_point … cip cp;
289         print_statementT … cip stmt ]::acc)
290    (joint_if_code … f) (joint_if_entry … f) [].
291
292definition print_joint_function:
293 ∀p:params. ∀lines. ∀globals. ∀functions. ∀string.
294  code_iteration_params string p lines (globals@functions) → printing_params string p →
295   joint_function p functions globals → list string ≝
296 λp,lines,globals,functions,string,cip,pp,f.
297  match f with
298  [ Internal f ⇒ print_joint_internal_function … cip … pp f
299  | External f ⇒ [] ].
300
301definition print_joint_program:
302 ∀p:params. ∀lines. ∀string.
303  printing_params string p →
304  ∀prog:joint_program p.
305   code_iteration_params string p lines (prog_names … prog) →
306   list (ident × (list string)) ≝
307 λp,lines,string,pp,prog,cip.
308  foldr ?? (λf,acc. 〈\fst f,print_joint_function … cip … pp (\snd f)〉 :: acc)
309   [] (prog_funct … prog).
Note: See TracBrowser for help on using the repository browser.