print.ml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. | Array (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 args.verbose >= 2 then "; " ^ comment else ""
  44. | InlineComment (instr, comment) ->
  45. if 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, scope, index) ->
  66. tab ^ prefix ctype ^ "load" ^ suffix scope ^ " " ^ si index
  67. | LoadConst (ctype, index) ->
  68. tab ^ prefix ctype ^ "loadc " ^ si index
  69. | LoadImm (BoolVal b) ->
  70. tab ^ "bloadc_" ^ (if b then "t" else "f")
  71. | LoadImm (IntVal i) when i < 0 ->
  72. tab ^ "iloadc_m" ^ si (-i)
  73. | LoadImm (IntVal i) ->
  74. tab ^ "iloadc_" ^ si i
  75. | LoadImm (FloatVal i) ->
  76. tab ^ "floadc_" ^ si (int_of_float i)
  77. (* Operators *)
  78. | Op (op, ctype) ->
  79. tab ^ prefix ctype ^ op2str op
  80. | Convert (src, tgt) ->
  81. tab ^ prefix src ^ "2" ^ prefix tgt
  82. | Inc (index, const) ->
  83. tab ^ "iinc " ^ si index ^ " " ^ si const
  84. | Dec (index, const) ->
  85. tab ^ "idec " ^ si index ^ " " ^ si const
  86. | IncOne index ->
  87. tab ^ "iinc_1 " ^ si index
  88. | DecOne index ->
  89. tab ^ "idec_1 " ^ si index
  90. (* Control flow *)
  91. | RtnInit scope ->
  92. tab ^ "isr" ^ suffix scope
  93. | RtnJmp scope ->
  94. tab ^ "jsr" ^ rtn_suffix scope
  95. | RtnEnter stack_len ->
  96. tab ^ "esr " ^ si stack_len
  97. | Ret ctype ->
  98. tab ^ prefix ctype ^ "return"
  99. | Branch (true, target) ->
  100. tab ^ "branch_t " ^ target
  101. | Branch (false, target) ->
  102. tab ^ "branch_f " ^ target
  103. | Jump target ->
  104. tab ^ "jump " ^ target
  105. (* Stack management *)
  106. | Pop ctype ->
  107. tab ^ prefix ctype ^ "pop"
  108. (* Arrays *)
  109. | NewArray (basetype, ndims) ->
  110. tab ^ prefix basetype ^ "newa " ^ si ndims
  111. | LoadArray basetype ->
  112. tab ^ prefix basetype ^ "loada"
  113. | StoreArray basetype ->
  114. tab ^ prefix basetype ^ "storea"
  115. | EmptyLine -> ""
  116. | DummyInstr -> tab ^ "<dummy>"
  117. | _ -> tab ^ "<unknown instruction>"
  118. let rec print_assembly oc instrs =
  119. let output_line line =
  120. output_string oc line;
  121. output_char oc '\n';
  122. in
  123. let endbuf = ref [] in
  124. let rec trav = function
  125. | [] -> ()
  126. | EmptyLine :: tl -> output_line ""; trav tl
  127. | hd :: tl ->
  128. let line = instr2str hd in
  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. ));
  134. trav tl
  135. in
  136. trav instrs;
  137. if List.length !endbuf > 1 then (
  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. ); ()
  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
  160. | _ -> raise (InvalidInput "print")