print.ml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. open Types
  2. open Util
  3. open Stringify
  4. let tab = " "
  5. let max_instr_width = 26
  6. let si = string_of_int
  7. let op2str = function
  8. | Neg -> "neg"
  9. | Not -> "not"
  10. | Add -> "add"
  11. | Sub -> "sub"
  12. | Mul -> "mul"
  13. | Div -> "div"
  14. | Mod -> "rem"
  15. | Eq -> "eq"
  16. | Ne -> "ne"
  17. | Lt -> "lt"
  18. | Le -> "le"
  19. | Gt -> "gt"
  20. | Ge -> "ge"
  21. | _ -> raise (FatalError (Msg "operator unsupported by VM"))
  22. let prefix = function
  23. | Bool _ -> "b"
  24. | Int _ -> "i"
  25. | Float _ -> "f"
  26. | Void -> ""
  27. | _ -> "a"
  28. let suffix = function
  29. | Glob -> "g"
  30. | Current -> ""
  31. | Local -> "l"
  32. | Rel nesting -> "n " ^ si nesting
  33. let rtn_suffix = function
  34. | ExternFun index -> "e " ^ si index
  35. | LocalFun (size, label) -> " " ^ si size ^ " " ^ label
  36. let rec instr2str = function
  37. (* Global / directives *)
  38. | Comment comment ->
  39. if Globals.args.verbose >= 2 then "; " ^ comment else ""
  40. | InlineComment (instr, comment) ->
  41. if Globals.args.verbose >= 2 then
  42. expand max_instr_width (instr2str instr) ^ " ; " ^ comment
  43. else
  44. instr2str instr
  45. | Label name ->
  46. name ^ ":"
  47. | Export (name, ret_type, arg_types, label) ->
  48. let types = List.map type2str (ret_type :: arg_types) in
  49. ".export \"" ^ name ^ "\" " ^ (String.concat " " types) ^ " " ^ label
  50. | Import (name, ret_type, arg_types) ->
  51. let types = List.map type2str (ret_type :: arg_types) in
  52. ".import \"" ^ name ^ "\" " ^ (String.concat " " types)
  53. | Global ctype ->
  54. ".global " ^ (type2str ctype)
  55. | ConstDef value ->
  56. ".const " ^ type2str (const_type value) ^ " " ^ const2str value
  57. (* Store *)
  58. | Store (ctype, scope, index) ->
  59. tab ^ prefix ctype ^ "store" ^ suffix scope ^ " " ^ si index
  60. (* Load *)
  61. | Load (ctype, Current, index) when index >= 0 & index <= 3 ->
  62. tab ^ prefix ctype ^ "load_" ^ si index
  63. | Load (ctype, scope, index) ->
  64. tab ^ prefix ctype ^ "load" ^ suffix scope ^ " " ^ si index
  65. | LoadConst (ctype, index) ->
  66. tab ^ prefix ctype ^ "loadc " ^ si index
  67. | LoadImm (BoolVal b) ->
  68. tab ^ "bloadc_" ^ (if b then "t" else "f")
  69. | LoadImm (IntVal i) when i < 0 ->
  70. tab ^ "iloadc_m" ^ si (-i)
  71. | LoadImm (IntVal i) ->
  72. tab ^ "iloadc_" ^ si i
  73. | LoadImm (FloatVal i) ->
  74. tab ^ "floadc_" ^ si (int_of_float i)
  75. (* Operators *)
  76. | Op (op, ctype) ->
  77. tab ^ prefix ctype ^ op2str op
  78. | Convert (src, tgt) ->
  79. tab ^ prefix src ^ "2" ^ prefix tgt
  80. | Inc (index, const) ->
  81. tab ^ "iinc " ^ si index ^ " " ^ si const
  82. | Dec (index, const) ->
  83. tab ^ "idec " ^ si index ^ " " ^ si const
  84. | IncOne index ->
  85. tab ^ "iinc_1 " ^ si index
  86. | DecOne index ->
  87. tab ^ "idec_1 " ^ si index
  88. (* Control flow *)
  89. | RtnInit scope ->
  90. tab ^ "isr" ^ suffix scope
  91. | RtnJmp scope ->
  92. tab ^ "jsr" ^ rtn_suffix scope
  93. | RtnEnter stack_len ->
  94. tab ^ "esr " ^ si stack_len
  95. | Ret ctype ->
  96. tab ^ prefix ctype ^ "return"
  97. | Branch (true, target) ->
  98. tab ^ "branch_t " ^ target
  99. | Branch (false, target) ->
  100. tab ^ "branch_f " ^ target
  101. | Jump target ->
  102. tab ^ "jump " ^ target
  103. (* Stack management *)
  104. | Pop ctype ->
  105. tab ^ prefix ctype ^ "pop"
  106. (* Arrays *)
  107. | NewArray (basetype, ndims) ->
  108. tab ^ prefix basetype ^ "newa " ^ si ndims
  109. | ArraySize index ->
  110. tab ^ "asize " ^ si index
  111. | LoadArray basetype ->
  112. tab ^ prefix basetype ^ "loada"
  113. | StoreArray basetype ->
  114. tab ^ prefix basetype ^ "storea"
  115. | EmptyLine -> ""
  116. (* Print assembly instructions to the given file pointer. *)
  117. let rec print_assembly oc instrs =
  118. let output_line line =
  119. output_string oc line;
  120. output_char oc '\n';
  121. in
  122. let endbuf = ref [] in
  123. let rec trav = function
  124. | [] -> ()
  125. | EmptyLine :: tl -> output_line ""; trav tl
  126. | hd :: tl ->
  127. let line = instr2str hd in
  128. begin
  129. if String.length line > 0 && line.[0] = '.' then
  130. endbuf := line :: !endbuf
  131. else if String.length line > 0 then
  132. output_line line
  133. end;
  134. trav tl
  135. in
  136. trav instrs;
  137. if List.length !endbuf > 1 then begin
  138. output_line (instr2str (Comment ("globals:")));
  139. let cmp a b = compare (String.sub b 0 7) (String.sub a 0 7) in
  140. List.iter output_line (List.sort cmp (List.rev !endbuf))
  141. end
  142. let phase = function
  143. | Ast node as input ->
  144. prerr_endline hline;
  145. prerr_endline (node2str node);
  146. prerr_endline hline;
  147. input
  148. | FileContent (display_name, content) as input ->
  149. prerr_endline hline;
  150. prerr_endline (display_name ^ ":\n");
  151. prerr_endline content;
  152. prerr_endline hline;
  153. input
  154. | Assembly instrs as input ->
  155. prerr_endline hline;
  156. print_assembly stderr instrs;
  157. prerr_endline hline;
  158. input
  159. | Empty -> Empty