source: src/RTLabs/RTLabsMatitaPrinter.ml @ 816

Last change on this file since 816 was 816, checked in by campbell, 9 years ago

Clight to Cminor compilation, modulo switch statements, temporary
generation, 32 to 8 bit translation and miscellaneous bugs.

Also, remove (unused) signatures from function call statements in Cminor
and RTLabs; and separate comparison of integers and pointers in Clight
semantics.

File size: 9.4 KB
Line 
1
2let next_id = ref 0
3let fresh () = let i = !next_id in next_id := i+1; i
4
5let n_spaces n = String.make n ' '
6
7
8let print_global (x, size) =
9  Printf.sprintf "pair ?? (pair ?? (pair ?? id_%s [Init_space %d ]) Any) it" x size
10
11let print_globals n globs =
12  Printf.sprintf "%s(*globals:*)\n%s[%s]"
13    (n_spaces n)
14    (n_spaces n)
15    (MiscPottier.string_of_list (";\n"^n_spaces n) print_global globs)
16
17
18let rec print_args args =
19  Printf.sprintf "[[%s]]" (MiscPottier.string_of_list "; " Register.print args)
20
21let print_result rl = Register.print rl
22
23let print_params rl =
24  Printf.sprintf "[%s]" (MiscPottier.string_of_list "; " Register.print rl)
25
26let print_locals rl =
27  Printf.sprintf "[%s]" (MiscPottier.string_of_list "; " Register.print rl)
28
29let print_quantity = function
30  | Memory.QInt 1 -> "Mint8unsigned"
31  | Memory.QInt 2 -> "Mint16unsigned"
32  | Memory.QInt 4 -> "Mint32"
33  | Memory.QPtr -> "Mpointer Any"
34(*
35let print_memory_q = function
36  | Memory.MQ_int8signed    -> "Mint8signed"
37  | Memory.MQ_int8unsigned  -> "Mint8unsigned"
38  | Memory.MQ_int16signed   -> "Mint16signed"
39  | Memory.MQ_int16unsigned -> "Mint16unsigned"
40  | Memory.MQ_int32         -> "Mint32"
41  | Memory.MQ_float32       -> "Mfloat32"
42  | Memory.MQ_float64       -> "Mfloat64"
43  | Memory.MQ_pointer       -> "Mpointer Any"
44  | Memory.MQ_chunk         -> "MXXX"
45*)
46
47
48let print_cmp = function
49  | AST.Cmp_eq -> "Ceq"
50  | AST.Cmp_ne -> "Cne"
51  | AST.Cmp_gt -> "Cgt"
52  | AST.Cmp_ge -> "Cge"
53  | AST.Cmp_lt -> "Clt"
54  | AST.Cmp_le -> "Cle"
55
56let print_cst = function
57  | AST.Cst_int i -> Printf.sprintf "Ointconst (repr %d)" i
58  | AST.Cst_float f -> Printf.sprintf "Ofloatconst %f" f
59  | AST.Cst_addrsymbol id -> Printf.sprintf "Oaddrsymbol id_%s (repr 0)" id
60  | AST.Cst_stackoffset off -> Printf.sprintf "Oaddrstack (repr %d)" off
61
62let print_op1 = function
63  | AST.Op_cast8unsigned -> "Ocast8unsigned"
64  | AST.Op_cast8signed -> "Ocast8signed"
65  | AST.Op_cast16unsigned -> "Ocast16unsigned"
66  | AST.Op_cast16signed -> "Ocast16signed"
67  | AST.Op_negint -> "Onegint"
68  | AST.Op_notbool -> "Onotbool"
69  | AST.Op_notint -> "Onotint"
70  | AST.Op_negf -> "Onegf"
71  | AST.Op_absf -> "Oabsf"
72  | AST.Op_singleoffloat -> "Osingleoffloat"
73  | AST.Op_intoffloat -> "Ointoffloat"
74  | AST.Op_intuoffloat -> "Ointuoffloat"
75  | AST.Op_floatofint -> "Ofloatofint"
76  | AST.Op_floatofintu -> "Ofloatofintu"
77  | AST.Op_id -> "Oid"
78  | AST.Op_ptrofint -> "Optrofint"
79  | AST.Op_intofptr -> "Ointofptr"
80
81let print_op2 = function
82  | AST.Op_add -> "Oadd"
83  | AST.Op_sub -> "Osub"
84  | AST.Op_mul -> "Omul"
85  | AST.Op_div -> "Odiv"
86  | AST.Op_divu -> "Odivu"
87  | AST.Op_mod -> "Omod"
88  | AST.Op_modu -> "Omodu"
89  | AST.Op_and -> "Oand"
90  | AST.Op_or -> "Oor"
91  | AST.Op_xor -> "Oxor"
92  | AST.Op_shl -> "Oshl"
93  | AST.Op_shr -> "Oshr"
94  | AST.Op_shru -> "Oshru"
95  | AST.Op_addf -> "Oaddf"
96  | AST.Op_subf -> "Osubf"
97  | AST.Op_mulf -> "Omulf"
98  | AST.Op_divf -> "Odivf"
99  | AST.Op_cmp cmp -> "(Ocmp " ^ print_cmp cmp ^ ")"
100  | AST.Op_cmpu cmp -> "(Ocmpu " ^ (print_cmp cmp) ^ ")"
101  | AST.Op_cmpf cmp -> "(Ocmpf " ^ (print_cmp cmp) ^ ")"
102  | AST.Op_addp -> "Oaddp"
103  | AST.Op_subp -> "Osubp"
104  | AST.Op_cmpp cmp -> (print_cmp cmp) ^ "Ocmpp"
105
106
107let print_addressing = function
108  | RTLabs.Aindexed off -> Printf.sprintf "Aindexed (repr %d)" off
109  | RTLabs.Aindexed2 -> "Aindexed2"
110  | RTLabs.Aglobal (id, off) -> Printf.sprintf "Aglobal id_%s (repr %d)" id off
111  | RTLabs.Abased (id, off) -> Printf.sprintf "Abased id_%s (repr %d)" id off
112  | RTLabs.Ainstack off -> Printf.sprintf "Ainstack (repr %d)" off
113
114
115let print_sig_type = function
116| AST.Sig_int -> "ASTint"
117| AST.Sig_float -> "ASTfloat"
118| AST.Sig_ptr -> "ASTptr Any"
119
120let print_sig s =
121  Printf.sprintf "mk_signature [%s] (%s)"
122    (MiscPottier.string_of_list "; " print_sig_type s.AST.args)
123    (match s.AST.res with AST.Type_ret t -> "Some ? " ^ print_sig_type t
124                        | AST.Type_void  -> "None ?")
125
126let rec print_table = function
127  | [] -> ""
128  | [lbl] -> lbl
129  | lbl :: tbl -> lbl ^ "; " ^ (print_table tbl)
130
131
132let print_statement = function
133  | RTLabs.St_skip lbl -> "make_St_skip " ^ lbl
134  | RTLabs.St_cost (cost_lbl, lbl) ->
135      Printf.sprintf "make_St_cost C%s %s" cost_lbl lbl
136  | RTLabs.St_cst (dests, cst, lbl) ->
137      Printf.sprintf "make_St_const %s (%s) %s"
138        (Register.print dests)
139        (print_cst cst)
140        lbl
141  | RTLabs.St_op1 (op1, dests, srcs, lbl) ->
142      Printf.sprintf "make_St_op1 %s %s %s %s"
143        (print_op1 op1)
144        (Register.print dests)
145        (Register.print srcs)
146        lbl
147  | RTLabs.St_op2 (op2, dests, srcs1, srcs2, lbl) ->
148      Printf.sprintf "make_St_op2 %s %s %s %s %s"
149        (print_op2 op2)
150        (Register.print dests)
151        (Register.print srcs1)
152        (Register.print srcs2)
153        lbl
154  | RTLabs.St_load (chunk, mode, args, dests, lbl) ->
155      Printf.sprintf "make_St_load %s (%s) %s %s %s"
156        (print_quantity chunk)
157        (print_addressing mode)
158        (print_args args)
159        (Register.print dests)
160        lbl
161  | RTLabs.St_store (chunk, mode, args, srcs, lbl) ->
162      Printf.sprintf "make_St_store %s (%s) %s %s %s"
163        (print_quantity chunk)
164        (print_addressing mode)
165        (print_args args)
166        (Register.print srcs)
167        lbl
168  | RTLabs.St_call_id (f, args, res, sg, lbl) ->
169      Printf.sprintf "make_St_call_id id_%s %s (Some ? %s) %s"
170        f
171        (print_params args)
172        (Register.print res)
173        lbl
174  | RTLabs.St_call_ptr (f, args, res, sg, lbl) ->
175      Printf.sprintf "make_St_call_ptr %s %s (Some ? %s) %s"
176        (Register.print f)
177        (print_params args)
178        (Register.print res)
179        lbl
180  | RTLabs.St_tailcall_id (f, args, sg) ->
181      Printf.sprintf "make_St_tailcall_id id_%s %s"
182        f
183        (print_params args)
184  | RTLabs.St_tailcall_ptr (f, args, sg) ->
185      Printf.sprintf "make_St_tailcall_ptr %s %s"
186        (Register.print f)
187        (print_params args)
188  | RTLabs.St_condcst (cst, lbl_true, lbl_false) ->
189      Printf.sprintf "make_St_condcst (%s) %s %s"
190        (print_cst cst)
191        lbl_true
192        lbl_false
193  | RTLabs.St_cond1 (op1, srcs, lbl_true, lbl_false) ->
194      Printf.sprintf "make_St_cond1 %s %s %s %s"
195        (print_op1 op1)
196        (Register.print srcs)
197        lbl_true
198        lbl_false
199  | RTLabs.St_cond2 (op2, srcs1, srcs2, lbl_true, lbl_false) ->
200      Printf.sprintf "make_St_cond2 %s %s %s %s %s"
201        (print_op2 op2)
202        (Register.print srcs1)
203        (Register.print srcs2)
204        lbl_true
205        lbl_false
206  | RTLabs.St_jumptable (rs, tbl) ->
207      Printf.sprintf "make_St_jumptable %s [%s]"
208        (Register.print rs)
209        (print_table tbl)
210  | RTLabs.St_return rs -> "make_St_return" (* rs should always be the function's result register, anyway *)
211
212let print_cost_labels n c =
213  let f _ stmt (i,s) =
214    match stmt with
215    | RTLabs.St_cost (lbl, _) ->
216        i+1,
217        Printf.sprintf "%sdefinition C%s := costlabel_of_nat %d.\n%s"
218          (n_spaces n)
219          lbl
220          i
221          s
222    | _ -> i,s
223  in
224  snd (Label.Map.fold f c (0,""))
225
226let print_labels n c =
227  let f lbl _ (i,s) =
228    i+1,
229    Printf.sprintf "%sdefinition %s := %d.\n%s"
230      (n_spaces n)
231      lbl
232      i
233      s
234  in
235  snd (Label.Map.fold f c (0,""))
236
237let print_graph n c =
238  let f lbl stmt s =
239    Printf.sprintf "%s(pair ?? %s (%s))%s\n%s"
240      (n_spaces n)
241      lbl
242      (print_statement stmt)
243      (if s = "" then "\n]" else ";")
244      s in
245  "[\n" ^ Label.Map.fold f c ""
246
247let print_internal_decl n f def =
248  Printf.sprintf
249    "%sdefinition id_%s := ident_of_nat %d.\n%s%s\n\n%sdefinition pre_%s := mk_pre_internal_function\n%s(%s)\n%s(Some ? %s)\n%s%s\n%s%s\n%s%s\n%s%d\n%s%s\n%s%s\n%s%s."
250    (n_spaces n)
251    f
252    (fresh ())
253    (print_labels n def.RTLabs.f_graph)
254    (print_cost_labels n def.RTLabs.f_graph)
255    (n_spaces n)
256    f
257    (n_spaces (n+2))
258    (print_sig def.RTLabs.f_sig)
259    (n_spaces (n+2))
260    (print_result def.RTLabs.f_result)
261    (n_spaces (n+2))
262    (print_params def.RTLabs.f_params)
263    (n_spaces (n+2))
264    (print_locals def.RTLabs.f_locals)
265    (n_spaces (n+2))
266    (print_locals def.RTLabs.f_ptrs)
267    (n_spaces (n+2))
268    def.RTLabs.f_stacksize
269    (n_spaces (n+2))
270    (print_graph (n+2) def.RTLabs.f_graph)
271    (n_spaces (n+2))
272    def.RTLabs.f_entry
273    (n_spaces (n+2))
274    def.RTLabs.f_exit
275
276
277
278let print_external_decl n f def =
279  Printf.sprintf "%sdefinition id_%s := ident_of_nat %d.\n%sdefinition f_%s := External (mk_external_function id_%s (%s)).\n"
280    (n_spaces n)
281    f
282    (fresh ())
283    (n_spaces n)
284    f
285    f
286    (print_sig def.AST.ef_sig)
287
288
289let print_fun_decl n (f, def) = match def with
290  | RTLabs.F_int def -> print_internal_decl n f def
291  | RTLabs.F_ext def -> print_external_decl n f def
292
293let print_fun_decls n =
294  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
295
296let print_fun n functs =
297  List.fold_left (fun s (f,_) ->
298    Printf.sprintf "%sdo f_%s \\larr make_internal_function pre_%s;\n%s"
299      (n_spaces n) 
300      f
301      f
302      s) "" functs
303
304let print_fun' n functs =
305  List.fold_left (fun s (f,def) ->
306    Printf.sprintf "%s(pair ?? id_%s f_%s)::\n%s"
307      (n_spaces n) 
308      f
309      f
310      s) "(nil ?)" functs
311
312let print_main n main =
313  Printf.sprintf "%s%s"
314    (n_spaces n)
315    (match main with Some f -> "id_" ^ f | None -> "ident_of_nat 0 (* None! *)")
316
317let define_var_id (id,_) =
318  Printf.sprintf "definition id_%s := ident_of_nat %d.\n" id (fresh ())
319
320let define_var_ids =
321  List.fold_left (fun s v -> s ^ (define_var_id v)) ""
322
323let print_program p =
324  Printf.sprintf "include \"RTLabs/import.ma\".\ninclude \"common/Animation.ma\".\n\n%s\n\n\n%s\n\ndefinition prog : res RTLabs_program :=\n%s\nOK ? (mk_program ??\n(%s)\n%s\n%s\n).\n"
325    (define_var_ids p.RTLabs.vars)
326    (print_fun_decls 2 p.RTLabs.functs)
327    (print_fun 2 p.RTLabs.functs)
328    (print_fun' 2 p.RTLabs.functs)
329    (print_main 2 p.RTLabs.main)
330    (print_globals 2 p.RTLabs.vars)
Note: See TracBrowser for help on using the repository browser.