(** This module provides a function to print [LTL] programs. *) let n_spaces n = String.make n ' ' let print_global n (x, size) = Printf.sprintf "%s\"%s\" [%d]" (n_spaces n) x size let print_globals eformat n globs = Eformat.printf eformat "%sglobals:\n" (n_spaces n) ; List.iter (fun g -> Eformat.printf eformat "%s\n" (print_global (n+2) g)) globs let print_reg = I8051.print_register let print_a = print_reg I8051.a let print_statement = function | LTL.St_skip lbl -> "--> " ^ lbl | LTL.St_comment (s, lbl) -> Printf.sprintf "*** %s *** --> %s" s lbl | LTL.St_cost (cost_lbl, lbl) -> Printf.sprintf "emit %s --> %s" cost_lbl lbl | LTL.St_int (dstr, i, lbl) -> Printf.sprintf "imm %s, %d --> %s" (print_reg dstr) i lbl | LTL.St_pop lbl -> Printf.sprintf "pop %s --> %s" print_a lbl | LTL.St_push lbl -> Printf.sprintf "push %s --> %s" print_a lbl | LTL.St_addr (id, lbl) -> Printf.sprintf "addr DPTR, %s --> %s" id lbl | LTL.St_from_acc (dstr, lbl) -> Printf.sprintf "move %s, %s --> %s" (print_reg dstr) print_a lbl | LTL.St_to_acc (srcr, lbl) -> Printf.sprintf "move %s, %s --> %s" print_a (print_reg srcr) lbl | LTL.St_opaccs (opaccs, lbl) -> Printf.sprintf "%s %s, %s --> %s" (I8051.print_opaccs opaccs) print_a (print_reg I8051.b) lbl | LTL.St_op1 (op1, lbl) -> Printf.sprintf "%s %s --> %s" (I8051.print_op1 op1) print_a lbl | LTL.St_op2 (op2, srcr, lbl) -> Printf.sprintf "%s %s, %s --> %s" (I8051.print_op2 op2) print_a (print_reg srcr) lbl | LTL.St_clear_carry lbl -> Printf.sprintf "clear CARRY --> %s" lbl | LTL.St_set_carry lbl -> Printf.sprintf "set CARRY --> %s" lbl | LTL.St_load lbl -> Printf.sprintf "movex %s, @DPTR --> %s" print_a lbl | LTL.St_store lbl -> Printf.sprintf "movex @DPTR, %s --> %s" print_a lbl | LTL.St_call_id (f, lbl) -> Printf.sprintf "call \"%s\" --> %s" f lbl | LTL.St_call_ptr lbl -> Printf.sprintf "call_ptr DPTR --> %s" lbl | LTL.St_condacc (lbl_true, lbl_false) -> Printf.sprintf "branch %s <> 0 --> %s, %s" print_a lbl_true lbl_false | LTL.St_return -> Printf.sprintf "return" let print_graph eformat n c = let f lbl stmt = Eformat.printf eformat "%s%s: %s\n" (n_spaces n) lbl (print_statement stmt) in Label.Map.iter f c let print_internal_decl eformat n f def = Eformat.printf eformat "%s\"%s\"\n%sstacksize: %d\n%sentry: %s\n%sexit: %s\n\n" (n_spaces n) f (n_spaces (n+2)) def.LTL.f_stacksize (n_spaces (n+2)) def.LTL.f_entry (n_spaces (n+2)) def.LTL.f_exit ; print_graph eformat (n+2) def.LTL.f_graph let print_external_decl eformat n f def = Eformat.printf eformat "%sextern \"%s\": %s\n" (n_spaces n) f (Primitive.print_sig def.AST.ef_sig) let print_fun_decl eformat n (f, def) = match def with | LTL.F_int def -> print_internal_decl eformat n f def | LTL.F_ext def -> print_external_decl eformat n f def let print_fun_decls eformat n functs = List.iter (fun f -> print_fun_decl eformat n f ; Eformat.printf eformat "\n\n") functs let print_program p = let eformat = Eformat.create () in Eformat.printf eformat "program:\n\n\n" ; print_globals eformat 2 p.LTL.vars ; Eformat.printf eformat "\n\n" ; print_fun_decls eformat 2 p.LTL.functs ; Eformat.get eformat