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

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

Started branch of untrusted compiler with indexed labels

  • added indexing structure to CostLabel?
  • propagated changes to other modules
  • added indexing as parameter to labelling
  • loop indexes not implemented yet, so behaviour is still the same
File size: 4.8 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_addr (dstr1, dstr2, id, lbl) ->
48    Printf.sprintf "imm (%s, %s), %s --> %s"
49      (print_reg dstr1) (print_reg dstr2) id lbl
50  | RTL.St_stackaddr (dstr1, dstr2, lbl) ->
51    Printf.sprintf "imm (%s, %s), STACK --> %s"
52      (print_reg dstr1) (print_reg dstr2) lbl
53  | RTL.St_int (dstr, i, lbl) ->
54    Printf.sprintf "imm %s, %d --> %s" (print_reg dstr) i lbl
55  | RTL.St_move (dstr, srcr, lbl) ->
56    Printf.sprintf "move %s, %s --> %s"
57      (print_reg dstr) (print_reg srcr) lbl
58  | RTL.St_opaccs (opaccs, dstr1, dstr2, srcr1, srcr2, lbl) ->
59    Printf.sprintf "%s (%s, %s) %s, %s --> %s"
60      (I8051.print_opaccs opaccs)
61      (print_reg dstr1)
62      (print_reg dstr2)
63      (print_reg srcr1)
64      (print_reg srcr2)
65      lbl
66  | RTL.St_op1 (op1, dstr, srcr, lbl) ->
67    Printf.sprintf "%s %s, %s --> %s"
68      (I8051.print_op1 op1) (print_reg dstr) (print_reg srcr) lbl
69  | RTL.St_op2 (op2, dstr, srcr1, srcr2, lbl) ->
70    Printf.sprintf "%s %s, %s, %s --> %s"
71      (I8051.print_op2 op2)
72      (print_reg dstr)
73      (print_reg srcr1)
74      (print_reg srcr2)
75      lbl
76  | RTL.St_clear_carry lbl ->
77    Printf.sprintf "clear CARRY --> %s" lbl
78  | RTL.St_set_carry lbl ->
79    Printf.sprintf "set CARRY --> %s" lbl
80  | RTL.St_load (dstr, addr1, addr2, lbl) ->
81    Printf.sprintf "load %s, (%s, %s) --> %s"
82      (print_reg dstr)
83      (print_reg addr1)
84      (print_reg addr2)
85      lbl
86  | RTL.St_store (addr1, addr2, srcr, lbl) ->
87    Printf.sprintf "store (%s, %s), %s --> %s"
88      (print_reg addr1)
89      (print_reg addr2)
90      (print_reg srcr)
91      lbl
92  | RTL.St_call_id (f, args, dstrs, lbl) ->
93    Printf.sprintf "call \"%s\", %s, %s --> %s"
94      f
95      (print_args args)
96      (print_return dstrs)
97      lbl
98  | RTL.St_call_ptr (f1, f2, args, dstrs, lbl) ->
99    Printf.sprintf "call_ptr [%s ; %s], %s, %s --> %s"
100      (print_reg f1)
101      (print_reg f2)
102      (print_args args)
103      (print_return dstrs)
104      lbl
105  | RTL.St_tailcall_id (f, args) ->
106    Printf.sprintf "tailcall \"%s\", %s"
107      f
108      (print_args args)
109  | RTL.St_tailcall_ptr (f1, f2, args) ->
110    Printf.sprintf "tailcall_ptr [%s ; %s], %s"
111      (print_reg f1)
112      (print_reg f2)
113      (print_args args)
114  | RTL.St_cond (srcr, lbl_true, lbl_false) ->
115    Printf.sprintf "branch %s <> 0 --> %s, %s"
116      (print_reg srcr) lbl_true lbl_false
117  | RTL.St_return regs ->
118    Printf.sprintf "return %s" (print_return regs)
119
120
121let print_graph n c =
122  let f lbl stmt s =
123    Printf.sprintf "%s%s: %s\n%s"
124      (n_spaces n)
125      lbl
126      (print_statement stmt)
127      s in
128  Label.Map.fold f c ""
129
130
131let print_internal_decl n f def =
132
133  Printf.sprintf
134    "%s\"%s\"%s\n%slocals: %s\n%sresult: %s\n%sstacksize: %d\n%sentry: %s\n%sexit: %s\n\n%s"
135    (n_spaces n)
136    f
137    (print_params def.RTL.f_params)
138    (n_spaces (n+2))
139    (print_locals def.RTL.f_locals)
140    (n_spaces (n+2))
141    (print_result def.RTL.f_result)
142    (n_spaces (n+2))
143    def.RTL.f_stacksize
144    (n_spaces (n+2))
145    def.RTL.f_entry
146    (n_spaces (n+2))
147    def.RTL.f_exit
148    (print_graph (n+2) def.RTL.f_graph)
149
150
151let print_external_decl n f def =
152  Printf.sprintf "%sextern \"%s\": %s\n"
153    (n_spaces n)
154    f
155    (Primitive.print_sig def.AST.ef_sig)
156
157
158let print_fun_decl n (f, def) = match def with
159  | RTL.F_int def -> print_internal_decl n f def
160  | RTL.F_ext def -> print_external_decl n f def
161
162let print_fun_decls n functs =
163  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
164    functs
165
166
167let print_program p =
168  Printf.sprintf "program:\n\n\n%s\n\n%s"
169    (print_globals 2 p.RTL.vars)
170    (print_fun_decls 2 p.RTL.functs)
Note: See TracBrowser for help on using the repository browser.