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

Last change on this file since 818 was 818, checked in by ayache, 9 years ago

32 and 16 bits operations support in D2.2/8051

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