source: src/joint/joint_printer.ma @ 2996

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

Printing of graphs now starts from the entry point.

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