source: Deliverables/D2.2/8051/src/RTL/RTLPrinter.ml @ 1568

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