source: Deliverables/D2.2/8051-indexed-labels-branch/src/LIN/LINPrinter.ml @ 1349

Last change on this file since 1349 was 1349, checked in by tranquil, 9 years ago
  • work on LIN completed
  • small implementation of extensible arrays
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_condacc lbl_true ->
62    Printf.sprintf "branch %s <> 0, %s" print_a lbl_true
63  | LIN.St_return -> "return"
64
65
66let print_code n c =
67  let f s stmt =
68    Printf.sprintf "%s\n%s%s" s (n_spaces n) (print_statement stmt) in
69  List.fold_left f "" c
70
71
72let print_internal_decl n f def =
73
74  Printf.sprintf
75    "%s\"%s\"\n\n%s"
76    (n_spaces n)
77    f
78    (print_code (n+2) def)
79
80
81let print_external_decl n f def =
82  Printf.sprintf "%sextern \"%s\": %s\n"
83    (n_spaces n)
84    f
85    (Primitive.print_sig def.AST.ef_sig)
86
87
88let print_fun_decl n (f, def) = match def with
89  | LIN.F_int def -> print_internal_decl n f def
90  | LIN.F_ext def -> print_external_decl n f def
91
92let print_fun_decls n functs =
93  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
94    functs
95
96
97let print_program p =
98  Printf.sprintf "program:\n\n\n%s\n\n%s"
99    (print_globals 2 p.LIN.vars)
100    (print_fun_decls 2 p.LIN.functs)
101
102(*
103(* Adapted from Pottier's PP compiler *)
104
105open Printf
106open PrintPottier
107open LIN
108
109let reg () r =
110  sprintf "$%s" (MIPS.print r)
111
112(*
113let slo () = function
114  | SlotLocal o ->
115      sprintf "local(%ld)" o
116  | SlotIncoming o ->
117      sprintf "in(%ld)" o
118  | SlotOutgoing o ->
119      sprintf "out(%ld)" o
120*)
121
122let load_op () = function
123  | AST.Byte -> "lb "
124  | AST.HalfWord -> "lhw"
125  | AST.Word -> "lw "
126
127let store_op () = function
128  | AST.Byte -> "sb "
129  | AST.HalfWord -> "shw"
130  | AST.Word -> "sw "
131
132let instruction () i =
133
134  match i with
135    | INewFrame ->
136        sprintf "newframe"
137    | IDeleteFrame ->
138        sprintf "delframe"
139(*
140    | IGetStack (destr, slot) ->
141        sprintf "gets  %a, %a" reg destr slo slot
142    | ISetStack (slot, sourcer) ->
143        sprintf "sets  %a, %a" slo slot reg sourcer
144*)
145    | IConst (r, i) ->
146        sprintf "li    %a, %ld" reg r i
147    | IUnOp (op, destr, sourcer) ->
148        sprintf "%a" (PrintOps.unop reg) (op, destr, sourcer)
149    | IBinOp (op, destr, sourcer1, sourcer2) ->
150        sprintf "%s %a, %a, %a" (PrintOps.binop op) reg destr reg sourcer1 reg sourcer2
151    | ILoadAddr (r, f) ->
152        sprintf "la    %a, %s" reg r f
153    | ICall rf ->
154        sprintf "call  %a" reg rf
155    | ITailCall rf ->
156        sprintf "tail  %a" reg rf
157    | ILoad (size, destr, sourcer, offset) ->
158        sprintf "%a   %a, %ld(%a)" load_op size reg destr offset reg sourcer
159    | IStore (size, addressr, offset, valuer) ->
160        sprintf "%a   %a, %ld(%a)" store_op size reg valuer offset reg addressr
161    | IGoto l ->
162        sprintf "j     %s" l
163    | IUnBranch (cond, sourcer, l) ->
164        sprintf "%a, %s" (PrintOps.uncon reg) (cond, sourcer) l
165    | IBinBranch (cond, sourcer1, sourcer2, l) ->
166        sprintf "%s %a, %a, %s" (PrintOps.bincon cond) reg sourcer1 reg sourcer2 l
167    | IReturn ->
168        sprintf "jr    $ra"
169    | ILabel l ->
170        sprintf "%s:" l
171    | ICost l ->
172        sprintf "%s:" l
173
174let proc () (name, proc) = match proc with
175  | LIN.F_int proc ->
176      sprintf "procedure %s(%ld)\nvar %ld\n%a"
177        name
178        proc.formals
179        proc.locals
180        (seplist nl instruction) proc.code
181  | LIN.F_ext proc ->
182      sprintf "extern %s: %s\n" name (Primitive.print_sig proc.AST.ef_sig)
183
184let map_to_association_list map =
185  let f k v l = (k, v) :: l in
186  StringTools.Map.fold f map []
187
188let print_program () p =
189  sprintf "program\n\nglobals %ld\n\n%a"
190    p.globals
191    (termlist nlnl proc) (map_to_association_list p.defs)
192*)
Note: See TracBrowser for help on using the repository browser.