source: Deliverables/D2.2/8051-indexed-labels-branch/src/RTL/RTLPrinter.ml @ 1340

Last change on this file since 1340 was 1340, checked in by tranquil, 8 years ago

work on RTLabs and RTL completed

File size: 4.9 KB
Line 
1
2(** This module provides a function to print [RTL] programs. *)
3
4
5let n_spaces n = String.make n ' '
6
7
8let print_global n (x, size) =
9  Printf.sprintf "%s\"%s\" [%d]" (n_spaces n) x size
10
11let print_globals n globs =
12  Printf.sprintf "%sglobals:\n%s"
13    (n_spaces n)
14    (List.fold_left (fun s g -> s ^ (print_global (n+2) g) ^ "\n") "" globs)
15
16
17let print_reg = Register.print
18
19let reg_set_to_list rs =
20  let f r l = l @ [r] in
21  Register.Set.fold f rs []
22
23let print_reg_list first last sep f rl =
24  Printf.sprintf "%s%s%s"
25    first (MiscPottier.string_of_list sep f rl) last
26
27let print_ptr rl = print_reg_list "[" "]" " ; " print_reg rl
28
29let print_args rl = print_reg_list "(" ")" ", " print_reg rl
30
31let print_return rl = print_reg_list "[" "]" " ; " print_reg rl
32
33let print_params rl = print_reg_list "(" ")" ", " Register.print rl
34
35let print_locals rs =
36  let rl = reg_set_to_list rs in
37  Printf.sprintf "%s" (print_reg_list "" "" ", " Register.print rl)
38
39let print_result rl = print_reg_list "[" "]" " ; " Register.print rl
40
41
42let print_statement = function
43  | RTL.St_skip lbl -> "--> " ^ lbl
44  | RTL.St_cost (cost_lbl, lbl) ->
45    let cost_lbl = CostLabel.string_of_cost_label ~pretty:true cost_lbl in
46    Printf.sprintf "emit %s --> %s" cost_lbl lbl
47  | RTL.St_ind_0 (i, lbl) ->
48    Printf.sprintf "index %d --> %s" i lbl
49  | RTL.St_ind_inc (i, lbl) ->
50    Printf.sprintf "increment %d --> %s" i lbl
51  | RTL.St_addr (dstr1, dstr2, id, lbl) ->
52    Printf.sprintf "imm (%s, %s), %s --> %s"
53      (print_reg dstr1) (print_reg dstr2) id lbl
54  | RTL.St_stackaddr (dstr1, dstr2, lbl) ->
55    Printf.sprintf "imm (%s, %s), STACK --> %s"
56      (print_reg dstr1) (print_reg dstr2) lbl
57  | RTL.St_int (dstr, i, lbl) ->
58    Printf.sprintf "imm %s, %d --> %s" (print_reg dstr) i lbl
59  | RTL.St_move (dstr, srcr, lbl) ->
60    Printf.sprintf "move %s, %s --> %s"
61      (print_reg dstr) (print_reg srcr) lbl
62  | RTL.St_opaccs (opaccs, dstr1, dstr2, srcr1, srcr2, lbl) ->
63    Printf.sprintf "%s (%s, %s) %s, %s --> %s"
64      (I8051.print_opaccs opaccs)
65      (print_reg dstr1)
66      (print_reg dstr2)
67      (print_reg srcr1)
68      (print_reg srcr2)
69      lbl
70  | RTL.St_op1 (op1, dstr, srcr, lbl) ->
71    Printf.sprintf "%s %s, %s --> %s"
72      (I8051.print_op1 op1) (print_reg dstr) (print_reg srcr) lbl
73  | RTL.St_op2 (op2, dstr, srcr1, srcr2, lbl) ->
74    Printf.sprintf "%s %s, %s, %s --> %s"
75      (I8051.print_op2 op2)
76      (print_reg dstr)
77      (print_reg srcr1)
78      (print_reg srcr2)
79      lbl
80  | RTL.St_clear_carry lbl ->
81    Printf.sprintf "clear CARRY --> %s" lbl
82  | RTL.St_set_carry lbl ->
83    Printf.sprintf "set CARRY --> %s" lbl
84  | RTL.St_load (dstr, addr1, addr2, lbl) ->
85    Printf.sprintf "load %s, (%s, %s) --> %s"
86      (print_reg dstr)
87      (print_reg addr1)
88      (print_reg addr2)
89      lbl
90  | RTL.St_store (addr1, addr2, srcr, lbl) ->
91    Printf.sprintf "store (%s, %s), %s --> %s"
92      (print_reg addr1)
93      (print_reg addr2)
94      (print_reg srcr)
95      lbl
96  | RTL.St_call_id (f, args, dstrs, lbl) ->
97    Printf.sprintf "call \"%s\", %s, %s --> %s"
98      f
99      (print_args args)
100      (print_return dstrs)
101      lbl
102  | RTL.St_call_ptr (f1, f2, args, dstrs, lbl) ->
103    Printf.sprintf "call_ptr [%s ; %s], %s, %s --> %s"
104      (print_reg f1)
105      (print_reg f2)
106      (print_args args)
107      (print_return dstrs)
108      lbl
109  | RTL.St_tailcall_id (f, args) ->
110    Printf.sprintf "tailcall \"%s\", %s"
111      f
112      (print_args args)
113  | RTL.St_tailcall_ptr (f1, f2, args) ->
114    Printf.sprintf "tailcall_ptr [%s ; %s], %s"
115      (print_reg f1)
116      (print_reg f2)
117      (print_args args)
118  | RTL.St_cond (srcr, lbl_true, lbl_false) ->
119    Printf.sprintf "branch %s <> 0 --> %s, %s"
120      (print_reg srcr) lbl_true lbl_false
121  | RTL.St_return regs ->
122    Printf.sprintf "return %s" (print_return regs)
123
124
125let print_graph n c =
126  let f lbl stmt s =
127    Printf.sprintf "%s%s: %s\n%s"
128      (n_spaces n)
129      lbl
130      (print_statement stmt)
131      s in
132  Label.Map.fold f c ""
133
134
135let print_internal_decl n f def =
136
137  Printf.sprintf
138    "%s\"%s\"%s\n%slocals: %s\n%sresult: %s\n%sstacksize: %d\n%sentry: %s\n%sexit: %s\n\n%s"
139    (n_spaces n)
140    f
141    (print_params def.RTL.f_params)
142    (n_spaces (n+2))
143    (print_locals def.RTL.f_locals)
144    (n_spaces (n+2))
145    (print_result def.RTL.f_result)
146    (n_spaces (n+2))
147    def.RTL.f_stacksize
148    (n_spaces (n+2))
149    def.RTL.f_entry
150    (n_spaces (n+2))
151    def.RTL.f_exit
152    (print_graph (n+2) def.RTL.f_graph)
153
154
155let print_external_decl n f def =
156  Printf.sprintf "%sextern \"%s\": %s\n"
157    (n_spaces n)
158    f
159    (Primitive.print_sig def.AST.ef_sig)
160
161
162let print_fun_decl n (f, def) = match def with
163  | RTL.F_int def -> print_internal_decl n f def
164  | RTL.F_ext def -> print_external_decl n f def
165
166let print_fun_decls n functs =
167  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
168    functs
169
170
171let print_program p =
172  Printf.sprintf "program:\n\n\n%s\n\n%s"
173    (print_globals 2 p.RTL.vars)
174    (print_fun_decls 2 p.RTL.functs)
Note: See TracBrowser for help on using the repository browser.