source: Deliverables/D2.2/8051-indexed-labels-branch/src/LTL/LTLPrinter.ml @ 1345

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

work on ERTL and LTL completed

File size: 3.2 KB
Line 
1
2(** This module provides a function to print [LTL] 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  | LTL.St_skip lbl -> "--> " ^ lbl
24  | LTL.St_comment (s, lbl) ->
25    Printf.sprintf "*** %s *** --> %s" s lbl
26  | LTL.St_cost (cost_lbl, lbl) ->
27    let cost_lbl = CostLabel.string_of_cost_label ~pretty:true cost_lbl in
28    Printf.sprintf "emit %s --> %s" cost_lbl lbl
29  | LTL.St_ind_0 (i, lbl) ->
30    Printf.sprintf "index %d --> %s" i lbl
31  | LTL.St_ind_inc (i, lbl) ->
32    Printf.sprintf "increment %d --> %s" i lbl
33  | LTL.St_int (dstr, i, lbl) ->
34    Printf.sprintf "imm %s, %d --> %s" (print_reg dstr) i lbl
35  | LTL.St_pop lbl ->
36    Printf.sprintf "pop %s --> %s" print_a lbl
37  | LTL.St_push lbl ->
38    Printf.sprintf "push %s --> %s" print_a lbl
39  | LTL.St_addr (id, lbl) ->
40    Printf.sprintf "addr DPTR, %s --> %s" id lbl
41  | LTL.St_from_acc (dstr, lbl) ->
42    Printf.sprintf "move %s, %s --> %s" (print_reg dstr) print_a lbl
43  | LTL.St_to_acc (srcr, lbl) ->
44    Printf.sprintf "move %s, %s --> %s" print_a (print_reg srcr) lbl
45  | LTL.St_opaccs (opaccs, lbl) ->
46    Printf.sprintf "%s %s, %s --> %s"
47      (I8051.print_opaccs opaccs) print_a (print_reg I8051.b) lbl
48  | LTL.St_op1 (op1, lbl) ->
49    Printf.sprintf "%s %s --> %s" (I8051.print_op1 op1) print_a lbl
50  | LTL.St_op2 (op2, srcr, lbl) ->
51    Printf.sprintf "%s %s, %s --> %s"
52      (I8051.print_op2 op2) print_a (print_reg srcr) lbl
53  | LTL.St_clear_carry lbl ->
54    Printf.sprintf "clear CARRY --> %s" lbl
55  | LTL.St_set_carry lbl ->
56    Printf.sprintf "set CARRY --> %s" lbl
57  | LTL.St_load lbl ->
58    Printf.sprintf "movex %s, @DPTR --> %s" print_a lbl
59  | LTL.St_store lbl ->
60    Printf.sprintf "movex @DPTR, %s --> %s" print_a lbl
61  | LTL.St_call_id (f, lbl) -> Printf.sprintf "call \"%s\" --> %s" f lbl
62  | LTL.St_condacc (lbl_true, lbl_false) ->
63    Printf.sprintf "branch %s <> 0 --> %s, %s" print_a lbl_true lbl_false
64  | LTL.St_return -> Printf.sprintf "return"
65
66
67let print_graph n c =
68  let f lbl stmt s =
69    Printf.sprintf "%s%s: %s\n%s"
70      (n_spaces n)
71      lbl
72      (print_statement stmt)
73      s in
74  Label.Map.fold f c ""
75
76
77let print_internal_decl n f def =
78
79  Printf.sprintf
80    "%s\"%s\"\n%sstacksize: %d\n%sentry: %s\n%sexit: %s\n\n%s"
81    (n_spaces n)
82    f
83    (n_spaces (n+2))
84    def.LTL.f_stacksize
85    (n_spaces (n+2))
86    def.LTL.f_entry
87    (n_spaces (n+2))
88    def.LTL.f_exit
89    (print_graph (n+2) def.LTL.f_graph)
90
91
92let print_external_decl n f def =
93  Printf.sprintf "%sextern \"%s\": %s\n"
94    (n_spaces n)
95    f
96    (Primitive.print_sig def.AST.ef_sig)
97
98
99let print_fun_decl n (f, def) = match def with
100  | LTL.F_int def -> print_internal_decl n f def
101  | LTL.F_ext def -> print_external_decl n f def
102
103let print_fun_decls n functs =
104  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
105    functs
106
107
108let print_program p =
109  Printf.sprintf "program:\n\n\n%s\n\n%s"
110    (print_globals 2 p.LTL.vars)
111    (print_fun_decls 2 p.LTL.functs)
Note: See TracBrowser for help on using the repository browser.