2 (** This module provides a function to print [LIN] programs. *)
5 let n_spaces n = String.make n ' '
8 let print_global n (x, size) =
9 Printf.sprintf "%s\"%s\" [%d]" (n_spaces n) x size
11 let print_globals eformat n globs =
12 Eformat.printf eformat "%sglobals:\n" (n_spaces n) ;
14 (fun g -> Eformat.printf eformat "%s\n" (print_global (n+2) g)) globs
17 let print_reg = I8051.print_register
19 let print_a = print_reg I8051.a
22 let print_statement = function
23 | LIN.St_goto lbl -> "goto " ^ lbl
24 | LIN.St_label lbl -> lbl ^ ":"
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
32 Printf.sprintf "pop %s" print_a
34 Printf.sprintf "push %s" print_a
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)
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_set_carry -> "set CARRY"
52 Printf.sprintf "movex %s, @DPTR" print_a
54 Printf.sprintf "movex @DPTR, %s" print_a
55 | LIN.St_call_id f -> Printf.sprintf "call \"%s\"" f
57 Printf.sprintf "call_ptr DPTR"
58 | LIN.St_condacc lbl_true ->
59 Printf.sprintf "branch %s <> 0, %s" print_a lbl_true
60 | LIN.St_return -> "return"
63 let print_code eformat n c =
65 Eformat.printf eformat "\n%s%s" (n_spaces n) (print_statement stmt) in
69 let print_internal_decl eformat n f def =
70 Eformat.printf eformat "%s\"%s\"\n\n" (n_spaces n) f ;
71 print_code eformat (n+2) def
74 let print_external_decl eformat n f def =
75 Eformat.printf eformat "%sextern \"%s\": %s\n"
78 (Primitive.print_sig def.AST.ef_sig)
81 let print_fun_decl eformat n (f, def) = match def with
82 | LIN.F_int def -> print_internal_decl eformat n f def
83 | LIN.F_ext def -> print_external_decl eformat n f def
85 let print_fun_decls eformat n functs =
87 (fun f -> print_fun_decl eformat n f ; Eformat.printf eformat "\n\n") functs
91 let eformat = Eformat.create () in
92 Eformat.printf eformat "program:\n\n\n" ;
93 print_globals eformat 2 p.LIN.vars ;
94 Eformat.printf eformat "\n\n" ;
95 print_fun_decls eformat 2 p.LIN.functs ;