2 let n_spaces n = String.make n ' '
5 let rec print_size = function
6 | AST.SQ q -> Memory.string_of_quantity q
7 | AST.SProd l -> "struct {" ^ (print_size_list l) ^ "}"
8 | AST.SSum l -> "union {" ^ (print_size_list l) ^ "}"
9 | AST.SArray (i, se) ->
10 (print_size se) ^ "[" ^ (string_of_int i) ^ "]"
11 and print_size_list l =
12 MiscPottier.string_of_list ", " print_size l
14 let print_global_size = print_size
16 let print_data = function
18 | Data_reserve n -> Printf.sprintf "[%d]" n
20 | AST.Data_int8 i -> Printf.sprintf "(int8) %d" i
21 | AST.Data_int16 i -> Printf.sprintf "(int16) %d" i
22 | AST.Data_int32 i -> Printf.sprintf "%d" i
23 | AST.Data_float32 f -> Printf.sprintf "%f" f
24 | AST.Data_float64 f -> Printf.sprintf "(float64) %f" f
26 let print_datas init =
27 let rec aux = function
29 | [data] -> print_data data
30 | data :: datas -> Printf.sprintf "%s, %s" (print_data data) (aux datas)
32 Printf.sprintf "{%s}" (aux init)
34 let print_datas_opt = function
36 | Some init -> " = " ^ (print_datas init)
38 let print_global n (id, size, init_opt) =
39 Printf.sprintf "%s\"%s\" : %s%s;\n"
40 (n_spaces n) id (print_global_size size) (print_datas_opt init_opt)
42 let print_globals eformat n =
43 List.iter (fun v -> Eformat.printf eformat "%s" (print_global n v))
46 let print_reg = Register.print
48 let print_oreg = function
50 | Some r -> print_reg r
52 let print_decl (r, t) =
53 (Primitive.print_type t) ^ " " ^ (Register.print r)
55 let rec print_args args =
56 Printf.sprintf "[%s]" (MiscPottier.string_of_list ", " print_reg args)
58 let print_result = function
60 | Some (r, t) -> (Primitive.print_type t) ^ " " ^ (Register.print r)
63 Printf.sprintf "(%s)" (MiscPottier.string_of_list ", " print_decl r)
66 Printf.sprintf "%s" (MiscPottier.string_of_list ", " print_decl r)
69 let print_cmp = function
77 let rec print_size = function
78 | AST.SQ q -> Memory.string_of_quantity q
79 | AST.SProd l -> "struct {" ^ (print_size_list l) ^ "}"
80 | AST.SSum l -> "union {" ^ (print_size_list l) ^ "}"
81 | AST.SArray (i, se) ->
82 (print_size se) ^ "[" ^ (string_of_int i) ^ "]"
83 and print_size_list l =
84 MiscPottier.string_of_list ", " print_size l
86 let print_stacksize = print_size
88 let print_offset (size, depth) =
89 (print_size size) ^ ", " ^ (string_of_int depth)
91 let print_sizeof = print_size
93 let print_cst = function
94 | AST.Cst_int i -> Printf.sprintf "imm_int %d" i
95 | AST.Cst_float f -> Printf.sprintf "imm_float %f" f
96 | AST.Cst_addrsymbol id -> Printf.sprintf "imm_addr \"%s\"" id
97 | AST.Cst_stack -> "imm_addr STACK"
98 | AST.Cst_offset off -> Printf.sprintf "imm_offset { %s }" (print_offset off)
99 | AST.Cst_sizeof t -> "imm_sizeof (" ^ (print_size t) ^ ")"
101 let string_of_signedness = function
103 | AST.Unsigned -> "u"
105 let string_of_int_type (size, sign) =
106 Printf.sprintf "%d%s" size (string_of_signedness sign)
108 let print_op1 = function
109 | AST.Op_cast (int_type, dest_size) ->
110 Printf.sprintf "int%sto%d" (string_of_int_type int_type) dest_size
111 | AST.Op_negint -> "negint"
112 | AST.Op_notbool -> "notbool"
113 | AST.Op_notint -> "notint"
115 | AST.Op_ptrofint -> "ptrofint"
116 | AST.Op_intofptr -> "intofptr"
118 let print_op2 = function
119 | AST.Op_add -> "add"
120 | AST.Op_sub -> "sub"
121 | AST.Op_mul -> "mul"
122 | AST.Op_div -> "div"
123 | AST.Op_divu -> "/u"
124 | AST.Op_mod -> "mod"
125 | AST.Op_modu -> "modu"
126 | AST.Op_and -> "and"
128 | AST.Op_xor -> "xor"
129 | AST.Op_shl -> "shl"
130 | AST.Op_shr -> "shr"
131 | AST.Op_shru -> "shru"
132 | AST.Op_cmp cmp -> print_cmp cmp
133 | AST.Op_addp -> "addp"
134 | AST.Op_subp -> "subp"
135 | AST.Op_subpp -> "subpp"
136 | AST.Op_cmpp cmp -> (print_cmp cmp) ^ "p"
137 | AST.Op_cmpu cmp -> (print_cmp cmp) ^ "u"
141 let print_addressing = function
142 | RTLabs.Aindexed off -> Printf.sprintf "{ %s }" (print_offset off)
143 | RTLabs.Aindexed2 -> "add"
144 | RTLabs.Aglobal (id, off) ->
145 Printf.sprintf "{ %s }(\"%s\")" (print_offset off) id
146 | RTLabs.Abased (id, off) ->
147 Printf.sprintf "add, { %s }(\"%s\")" (print_offset off) id
148 | RTLabs.Ainstack off -> Printf.sprintf "{ %s }(STACK)" (print_offset off)
152 let rec print_table = function
155 | lbl :: tbl -> lbl ^ ", " ^ (print_table tbl)
158 let print_statement = function
159 | RTLabs.St_skip lbl -> "--> " ^ lbl
160 | RTLabs.St_cost (cost_lbl, lbl) ->
161 Printf.sprintf "emit %s --> %s" cost_lbl lbl
162 | RTLabs.St_cst (destr, cst, lbl) ->
163 Printf.sprintf "imm %s, %s --> %s"
167 | RTLabs.St_op1 (op1, destr, srcr, lbl) ->
168 Printf.sprintf "%s %s, %s --> %s"
173 | RTLabs.St_op2 (op2, destr, srcr1, srcr2, lbl) ->
174 Printf.sprintf "%s %s, %s, %s --> %s"
180 | RTLabs.St_load (q, addr, destr, lbl) ->
181 Printf.sprintf "load %s, %s, %s --> %s"
182 (Memory.string_of_quantity q)
186 | RTLabs.St_store (q, addr, srcr, lbl) ->
187 Printf.sprintf "store %s, %s, %s --> %s"
188 (Memory.string_of_quantity q)
192 | RTLabs.St_call_id (f, args, res, sg, lbl) ->
193 Printf.sprintf "call \"%s\", %s, %s: %s --> %s"
197 (Primitive.print_sig sg)
199 | RTLabs.St_call_ptr (f, args, res, sg, lbl) ->
200 Printf.sprintf "call_ptr %s, %s, %s: %s --> %s"
204 (Primitive.print_sig sg)
206 | RTLabs.St_tailcall_id (f, args, sg) ->
207 Printf.sprintf "tailcall \"%s\", %s: %s"
210 (Primitive.print_sig sg)
211 | RTLabs.St_tailcall_ptr (f, args, sg) ->
212 Printf.sprintf "tailcall_ptr \"%s\", %s: %s"
215 (Primitive.print_sig sg)
216 | RTLabs.St_cond (r, lbl_true, lbl_false) ->
217 Printf.sprintf "%s? --> %s, %s"
222 | RTLabs.St_condcst (cst, t, lbl_true, lbl_false) ->
223 Printf.sprintf "(%s) %s --> %s, %s"
224 (Primitive.print_type t)
228 | RTLabs.St_cond1 (op1, srcr, lbl_true, lbl_false) ->
229 Printf.sprintf "%s %s --> %s, %s"
234 | RTLabs.St_cond2 (op2, srcr1, srcr2, lbl_true, lbl_false) ->
235 Printf.sprintf "%s %s, %s --> %s, %s"
242 | RTLabs.St_jumptable (r, tbl) ->
243 Printf.sprintf "j_tbl %s --> %s"
246 | RTLabs.St_return None -> Printf.sprintf "return"
247 | RTLabs.St_return (Some r) -> Printf.sprintf "return %s" (print_reg r)
250 let print_graph eformat n c =
252 Eformat.printf eformat "%s%s: %s\n"
255 (print_statement stmt) in
259 let print_internal_decl eformat n f def =
260 Eformat.printf eformat
261 "%s\"%s\"%s\n%slocals: %s\n%sresult: %s\n%sstacksize: %s\n%sentry: %s\n%sexit: %s\n\n"
264 (print_params def.RTLabs.f_params)
266 (print_locals def.RTLabs.f_locals)
268 (print_result def.RTLabs.f_result)
270 (print_stacksize def.RTLabs.f_stacksize)
275 print_graph eformat (n+2) def.RTLabs.f_graph
278 let print_external_decl eformat n f def =
279 Eformat.printf eformat "%sextern \"%s\": %s\n"
282 (Primitive.print_sig def.AST.ef_sig)
285 let print_fun_decl eformat n (f, def) = match def with
286 | RTLabs.F_int def -> print_internal_decl eformat n f def
287 | RTLabs.F_ext def -> print_external_decl eformat n f def
289 let print_fun_decls eformat n functs =
291 (fun f -> print_fun_decl eformat n f ; Eformat.printf eformat "\n\n")
295 let print_program p =
296 let eformat = Eformat.create () in
297 Eformat.printf eformat "program:\n\n\n" ;
298 print_globals eformat 2 p.RTLabs.vars ;
299 Eformat.printf eformat "\n\n" ;
300 print_fun_decls eformat 2 p.RTLabs.functs ;