source: Deliverables/D2.2/8051/src/LIN/LINPrinter.ml @ 1542

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

merge of indexed labels branch

File size: 5.0 KB
Line 
1
2(** This module provides a function to print [LIN] 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 = I8051.print_register
18
19let print_a = print_reg I8051.a
20
21
22let print_statement = function
23  | LIN.St_goto lbl -> "goto " ^ lbl
24  | LIN.St_label lbl -> lbl ^ ":"
25  | LIN.St_comment s ->
26    Printf.sprintf "*** %s ***" s
27  | LIN.St_cost cost_lbl ->
28    let cost_lbl = CostLabel.string_of_cost_label ~pretty:true cost_lbl in
29    Printf.sprintf "emit %s" cost_lbl
30  | LIN.St_ind_0 i ->
31    Printf.sprintf "index %d" i
32  | LIN.St_ind_inc i ->
33    Printf.sprintf "increment %d" i
34  | LIN.St_int (dstr, i) ->
35    Printf.sprintf "imm %s, %d" (print_reg dstr) i
36  | LIN.St_pop ->
37    Printf.sprintf "pop %s" print_a
38  | LIN.St_push ->
39    Printf.sprintf "push %s" print_a
40  | LIN.St_addr id ->
41    Printf.sprintf "addr DPTR, %s" id
42  | LIN.St_from_acc dstr ->
43    Printf.sprintf "move %s, %s" (print_reg dstr) print_a
44  | LIN.St_to_acc srcr ->
45    Printf.sprintf "move %s, %s" print_a (print_reg srcr)
46  | LIN.St_opaccs opaccs ->
47    Printf.sprintf "%s %s, %s"
48      (I8051.print_opaccs opaccs) print_a (print_reg I8051.b)
49  | LIN.St_op1 op1 ->
50    Printf.sprintf "%s %s" (I8051.print_op1 op1) print_a
51  | LIN.St_op2 (op2, srcr) ->
52    Printf.sprintf "%s %s, %s"
53      (I8051.print_op2 op2) print_a (print_reg srcr)
54  | LIN.St_clear_carry -> "clear CARRY"
55  | LIN.St_set_carry -> "set CARRY"
56  | LIN.St_load ->
57    Printf.sprintf "movex %s, @DPTR" print_a
58  | LIN.St_store ->
59    Printf.sprintf "movex @DPTR, %s" print_a
60  | LIN.St_call_id f -> Printf.sprintf "call \"%s\"" f
61  | LIN.St_call_ptr ->
62    Printf.sprintf "call_ptr DPTR"
63  | LIN.St_condacc lbl_true ->
64    Printf.sprintf "branch %s <> 0, %s" print_a lbl_true
65  | LIN.St_return -> "return"
66
67
68let print_code n c =
69  let f s stmt =
70    Printf.sprintf "%s\n%s%s" s (n_spaces n) (print_statement stmt) in
71  List.fold_left f "" c
72
73
74let print_internal_decl n f def =
75
76  Printf.sprintf
77    "%s\"%s\"\n\n%s"
78    (n_spaces n)
79    f
80    (print_code (n+2) def)
81
82
83let print_external_decl n f def =
84  Printf.sprintf "%sextern \"%s\": %s\n"
85    (n_spaces n)
86    f
87    (Primitive.print_sig def.AST.ef_sig)
88
89
90let print_fun_decl n (f, def) = match def with
91  | LIN.F_int def -> print_internal_decl n f def
92  | LIN.F_ext def -> print_external_decl n f def
93
94let print_fun_decls n functs =
95  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
96    functs
97
98
99let print_program p =
100  Printf.sprintf "program:\n\n\n%s\n\n%s"
101    (print_globals 2 p.LIN.vars)
102    (print_fun_decls 2 p.LIN.functs)
103
104(*
105(* Adapted from Pottier's PP compiler *)
106
107open Printf
108open PrintPottier
109open LIN
110
111let reg () r =
112  sprintf "$%s" (MIPS.print r)
113
114(*
115let slo () = function
116  | SlotLocal o ->
117      sprintf "local(%ld)" o
118  | SlotIncoming o ->
119      sprintf "in(%ld)" o
120  | SlotOutgoing o ->
121      sprintf "out(%ld)" o
122*)
123
124let load_op () = function
125  | AST.Byte -> "lb "
126  | AST.HalfWord -> "lhw"
127  | AST.Word -> "lw "
128
129let store_op () = function
130  | AST.Byte -> "sb "
131  | AST.HalfWord -> "shw"
132  | AST.Word -> "sw "
133
134let instruction () i =
135
136  match i with
137    | INewFrame ->
138        sprintf "newframe"
139    | IDeleteFrame ->
140        sprintf "delframe"
141(*
142    | IGetStack (destr, slot) ->
143        sprintf "gets  %a, %a" reg destr slo slot
144    | ISetStack (slot, sourcer) ->
145        sprintf "sets  %a, %a" slo slot reg sourcer
146*)
147    | IConst (r, i) ->
148        sprintf "li    %a, %ld" reg r i
149    | IUnOp (op, destr, sourcer) ->
150        sprintf "%a" (PrintOps.unop reg) (op, destr, sourcer)
151    | IBinOp (op, destr, sourcer1, sourcer2) ->
152        sprintf "%s %a, %a, %a" (PrintOps.binop op) reg destr reg sourcer1 reg sourcer2
153    | ILoadAddr (r, f) ->
154        sprintf "la    %a, %s" reg r f
155    | ICall rf ->
156        sprintf "call  %a" reg rf
157    | ITailCall rf ->
158        sprintf "tail  %a" reg rf
159    | ILoad (size, destr, sourcer, offset) ->
160        sprintf "%a   %a, %ld(%a)" load_op size reg destr offset reg sourcer
161    | IStore (size, addressr, offset, valuer) ->
162        sprintf "%a   %a, %ld(%a)" store_op size reg valuer offset reg addressr
163    | IGoto l ->
164        sprintf "j     %s" l
165    | IUnBranch (cond, sourcer, l) ->
166        sprintf "%a, %s" (PrintOps.uncon reg) (cond, sourcer) l
167    | IBinBranch (cond, sourcer1, sourcer2, l) ->
168        sprintf "%s %a, %a, %s" (PrintOps.bincon cond) reg sourcer1 reg sourcer2 l
169    | IReturn ->
170        sprintf "jr    $ra"
171    | ILabel l ->
172        sprintf "%s:" l
173    | ICost l ->
174        sprintf "%s:" l
175
176let proc () (name, proc) = match proc with
177  | LIN.F_int proc ->
178      sprintf "procedure %s(%ld)\nvar %ld\n%a"
179        name
180        proc.formals
181        proc.locals
182        (seplist nl instruction) proc.code
183  | LIN.F_ext proc ->
184      sprintf "extern %s: %s\n" name (Primitive.print_sig proc.AST.ef_sig)
185
186let map_to_association_list map =
187  let f k v l = (k, v) :: l in
188  StringTools.Map.fold f map []
189
190let print_program () p =
191  sprintf "program\n\nglobals %ld\n\n%a"
192    p.globals
193    (termlist nlnl proc) (map_to_association_list p.defs)
194*)
Note: See TracBrowser for help on using the repository browser.