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