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

Last change on this file since 1291 was 1291, checked in by tranquil, 8 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: 3.1 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_int (dstr, i, lbl) ->
30    Printf.sprintf "imm %s, %d --> %s" (print_reg dstr) i lbl
31  | LTL.St_pop lbl ->
32    Printf.sprintf "pop %s --> %s" print_a lbl
33  | LTL.St_push lbl ->
34    Printf.sprintf "push %s --> %s" print_a lbl
35  | LTL.St_addr (id, lbl) ->
36    Printf.sprintf "addr DPTR, %s --> %s" id lbl
37  | LTL.St_from_acc (dstr, lbl) ->
38    Printf.sprintf "move %s, %s --> %s" (print_reg dstr) print_a lbl
39  | LTL.St_to_acc (srcr, lbl) ->
40    Printf.sprintf "move %s, %s --> %s" print_a (print_reg srcr) lbl
41  | LTL.St_opaccs (opaccs, lbl) ->
42    Printf.sprintf "%s %s, %s --> %s"
43      (I8051.print_opaccs opaccs) print_a (print_reg I8051.b) lbl
44  | LTL.St_op1 (op1, lbl) ->
45    Printf.sprintf "%s %s --> %s" (I8051.print_op1 op1) print_a lbl
46  | LTL.St_op2 (op2, srcr, lbl) ->
47    Printf.sprintf "%s %s, %s --> %s"
48      (I8051.print_op2 op2) print_a (print_reg srcr) lbl
49  | LTL.St_clear_carry lbl ->
50    Printf.sprintf "clear CARRY --> %s" lbl
51  | LTL.St_set_carry lbl ->
52    Printf.sprintf "set CARRY --> %s" lbl
53  | LTL.St_load lbl ->
54    Printf.sprintf "movex %s, @DPTR --> %s" print_a lbl
55  | LTL.St_store lbl ->
56    Printf.sprintf "movex @DPTR, %s --> %s" print_a lbl
57  | LTL.St_call_id (f, lbl) -> Printf.sprintf "call \"%s\" --> %s" f lbl
58  | LTL.St_condacc (lbl_true, lbl_false) ->
59    Printf.sprintf "branch %s <> 0 --> %s, %s" print_a lbl_true lbl_false
60  | LTL.St_return -> Printf.sprintf "return"
61
62
63let print_graph n c =
64  let f lbl stmt s =
65    Printf.sprintf "%s%s: %s\n%s"
66      (n_spaces n)
67      lbl
68      (print_statement stmt)
69      s in
70  Label.Map.fold f c ""
71
72
73let print_internal_decl n f def =
74
75  Printf.sprintf
76    "%s\"%s\"\n%sstacksize: %d\n%sentry: %s\n%sexit: %s\n\n%s"
77    (n_spaces n)
78    f
79    (n_spaces (n+2))
80    def.LTL.f_stacksize
81    (n_spaces (n+2))
82    def.LTL.f_entry
83    (n_spaces (n+2))
84    def.LTL.f_exit
85    (print_graph (n+2) def.LTL.f_graph)
86
87
88let print_external_decl n f def =
89  Printf.sprintf "%sextern \"%s\": %s\n"
90    (n_spaces n)
91    f
92    (Primitive.print_sig def.AST.ef_sig)
93
94
95let print_fun_decl n (f, def) = match def with
96  | LTL.F_int def -> print_internal_decl n f def
97  | LTL.F_ext def -> print_external_decl n f def
98
99let print_fun_decls n functs =
100  List.fold_left (fun s f -> s ^ (print_fun_decl n f) ^ "\n\n") ""
101    functs
102
103
104let print_program p =
105  Printf.sprintf "program:\n\n\n%s\n\n%s"
106    (print_globals 2 p.LTL.vars)
107    (print_fun_decls 2 p.LTL.functs)
Note: See TracBrowser for help on using the repository browser.