source: Deliverables/D2.2/8051/src/LTL/LTLPrinter.ml @ 1542

Last change on this file since 1542 was 1542, checked in by tranquil, 8 years ago

merge of indexed labels branch

File size: 3.3 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_call_ptr lbl ->
63    Printf.sprintf "call_ptr DPTR --> %s" lbl
64  | LTL.St_condacc (lbl_true, lbl_false) ->
65    Printf.sprintf "branch %s <> 0 --> %s, %s" print_a lbl_true lbl_false
66  | LTL.St_return -> Printf.sprintf "return"
67
68
69let print_graph n c =
70  let f lbl stmt s =
71    Printf.sprintf "%s%s: %s\n%s"
72      (n_spaces n)
73      lbl
74      (print_statement stmt)
75      s in
76  Label.Map.fold f c ""
77
78
79let print_internal_decl n f def =
80
81  Printf.sprintf
82    "%s\"%s\"\n%sstacksize: %d\n%sentry: %s\n%sexit: %s\n\n%s"
83    (n_spaces n)
84    f
85    (n_spaces (n+2))
86    def.LTL.f_stacksize
87    (n_spaces (n+2))
88    def.LTL.f_entry
89    (n_spaces (n+2))
90    def.LTL.f_exit
91    (print_graph (n+2) def.LTL.f_graph)
92
93
94let print_external_decl n f def =
95  Printf.sprintf "%sextern \"%s\": %s\n"
96    (n_spaces n)
97    f
98    (Primitive.print_sig def.AST.ef_sig)
99
100
101let print_fun_decl n (f, def) = match def with
102  | LTL.F_int def -> print_internal_decl n f def
103  | LTL.F_ext def -> print_external_decl n f def
104
105let print_fun_decls n functs =
106  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
107    functs
108
109
110let print_program p =
111  Printf.sprintf "program:\n\n\n%s\n\n%s"
112    (print_globals 2 p.LTL.vars)
113    (print_fun_decls 2 p.LTL.functs)
Note: See TracBrowser for help on using the repository browser.