print.ml 4.7 KB

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