source: src/RTLabs/RTLabsMatitaPrinter.ml @ 1067

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

Update RTLabs pretty printer and examples.

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