print.ml 5.1 KB

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