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

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

Deliverable D2.2

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