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

Last change on this file since 1291 was 1291, checked in by tranquil, 9 years ago

Started branch of untrusted compiler with indexed labels

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