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

Last change on this file since 1488 was 1488, checked in by ayache, 8 years ago

Function pointers in D2.2/8051. Bugged for now.

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