assemble.ml 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. open Printf
  2. open Types
  3. open Util
  4. open Stringify
  5. let get_scope node = function
  6. | GlobalDec _ -> Extern
  7. | dec ->
  8. match (depthof dec, depthof node) with
  9. | (0, _) -> Glob
  10. | (a, b) when a = b -> Current
  11. | (a, b) -> Rel (b - a)
  12. let comline comment = InlineComment (EmptyLine, comment)
  13. let assemble program =
  14. let labcounter = ref 0 in
  15. let genlabel suffix =
  16. labcounter := !labcounter + 1;
  17. string_of_int !labcounter ^ "_" ^ suffix
  18. in
  19. let consts = Hashtbl.create 20 in
  20. let rec trav node =
  21. let rec trav_all = function
  22. | [] -> []
  23. | hd :: tl -> trav hd @ (trav_all tl)
  24. in
  25. let rec traverse_localfuns = function
  26. | LocalFuns body -> List.concat (List.map trav body)
  27. | Block body -> List.concat (List.map traverse_localfuns body)
  28. | _ -> []
  29. in
  30. match node with
  31. (* Global *)
  32. | Program (decls, _) ->
  33. trav_all decls
  34. | GlobalDef (false, ctype, name, _, _) ->
  35. [Comment (sprintf "global var \"%s\" at index %d" name (indexof node));
  36. Global ctype]
  37. | GlobalDef (true, ctype, name, _, _) ->
  38. [Comment (sprintf "exported var \"%s\" at index %d" name (indexof node));
  39. Global ctype;
  40. ExportVar (name, indexof node)]
  41. | GlobalDec (ctype, name, _) ->
  42. [Comment (sprintf "imported var \"%s\" at index %d" name (indexof node));
  43. ImportVar (name, ctype)]
  44. | FunDec (ret_type, name, params, _) ->
  45. [Comment (sprintf "imported fun \"%s\" at index %d" name (indexof node));
  46. ImportFun (name, ret_type, List.map typeof params)]
  47. | FunDef (export, ret_type, name, params, body, _) ->
  48. let label = labelof node in
  49. begin
  50. if export
  51. then [ExportFun (name, ret_type, List.map typeof params, label)]
  52. else []
  53. end @
  54. [Comment (sprintf "function \"%s\" with %d parameters and %d local vars"
  55. label (List.length params) (indexof node));
  56. Label label;
  57. RtnEnter (indexof node)] @
  58. (trav_all params) @
  59. (trav body) @
  60. (match ret_type with Void -> [Ret Void] | _ -> []) @
  61. [EmptyLine] @
  62. (traverse_localfuns body)
  63. | VarDec (_, name, _, _) ->
  64. [comline (sprintf "local var \"%s\" at index %d" name (indexof node))]
  65. | Param (_, name, _) ->
  66. [comline (sprintf "parameter \"%s\" at index %d" name (indexof node))]
  67. | LocalFuns _ -> []
  68. | Block body | VarDecs body -> trav_all body
  69. (* Statements *)
  70. | VarLet (dec, None, value, _) ->
  71. let store = Store (typeof dec, get_scope node dec, indexof dec) in
  72. trav value @ [InlineComment (store, node2str node)]
  73. | Return (value, _) ->
  74. trav value @ [InlineComment (Ret (typeof value), node2str node)]
  75. | If (cond, body, _) ->
  76. let endlabel = genlabel "end" in
  77. (trav cond) @
  78. [Branch (false, endlabel);
  79. comline ("if (" ^ (node2str cond) ^ ") {")] @
  80. (trav body) @
  81. [comline "}";
  82. Label endlabel]
  83. | IfElse (cond, true_body, false_body, _) ->
  84. let elselabel = genlabel "else" in
  85. let endlabel = genlabel "end" in
  86. (trav cond) @
  87. [Branch (false, elselabel);
  88. comline ("if (" ^ (node2str cond) ^ ") {")] @
  89. (trav true_body) @
  90. [Jump endlabel;
  91. comline "} else {";
  92. Label elselabel] @
  93. (trav false_body) @
  94. [comline "}";
  95. Label endlabel]
  96. | While (cond, body, _) ->
  97. let startlabel = genlabel "while" in
  98. let endlabel = genlabel "end" in
  99. let com = ("while (" ^ (node2str cond) ^ ") {") in
  100. [Label startlabel] @
  101. (trav cond) @
  102. [InlineComment (Branch (false, endlabel), com)] @
  103. (trav body) @
  104. [Jump startlabel;
  105. Label endlabel;
  106. comline "}"]
  107. | DoWhile (cond, body, _) ->
  108. let startlabel = genlabel "dowhile" in
  109. let com = ("} while (" ^ (node2str cond) ^ ");") in
  110. [comline "do {";
  111. Label startlabel] @
  112. (trav body) @
  113. (trav cond) @
  114. [InlineComment (Branch (true, startlabel), com)]
  115. (* Expression statement pops the disregarded expression value from the
  116. * stack, if any *)
  117. | Expr value ->
  118. let pop = match typeof value with
  119. | Void -> []
  120. | ctype -> [InlineComment (Pop ctype, "disregard return value")]
  121. in
  122. trav value @ pop
  123. (* Expressions *)
  124. (* Immediate values are handled here, and not in the peephole optimizer, for
  125. * convenience: the indices in the constant table would be altered, so
  126. * entries cannot be removed. By this early detection (also during index
  127. * analysis), they are not added in the first place *)
  128. | Const (value, _) when is_immediate_const value ->
  129. [InlineComment (LoadImm value, node2str node)]
  130. | Const (value, _) ->
  131. Hashtbl.replace consts value (indexof node);
  132. let load = LoadConst (typeof node, indexof node) in
  133. [InlineComment (load, node2str node)]
  134. | VarUse (GlobalDec (ctype, _, _) as dec, None, _) ->
  135. let load = Load (ctype, Extern, indexof dec) in
  136. [InlineComment (load, node2str node)]
  137. | VarUse (dec, None, _) ->
  138. let load =
  139. match (depthof dec, depthof node) with
  140. | (0, _) -> Load (typeof dec, Glob, indexof dec)
  141. | (a, b) when a = b -> Load (typeof dec, Current, indexof dec)
  142. | (a, b) -> Load (typeof dec, Rel (b - a), indexof dec)
  143. in
  144. [InlineComment (load, node2str node)]
  145. | Monop (op, value, _) ->
  146. (trav value) @
  147. [InlineComment (Op (op, typeof value), op2str op)]
  148. | Binop (op, left, right, _) ->
  149. (trav left) @
  150. (trav right) @
  151. [InlineComment (Op (op, typeof left), op2str op)]
  152. | TypeCast (ctype, value, _) ->
  153. let vtype = typeof value in
  154. begin
  155. match (ctype, vtype) with
  156. | (Float, Int) | (Int, Float) -> ()
  157. | _ ->
  158. let msg = sprintf
  159. "invalid typecast: %s -> %s"
  160. (type2str vtype) (type2str ctype)
  161. in
  162. raise (FatalError (NodeMsg (node, msg)))
  163. end;
  164. trav value @ [Convert (vtype, ctype)]
  165. (* Function calls *)
  166. | FunUse (dec, args, _) ->
  167. let init =
  168. match (depthof dec, depthof node) with
  169. | (0, _) -> RtnInit Glob
  170. | (a, b) when a = b - 1 -> RtnInit Current
  171. | (a, b) when a = b -> RtnInit Local
  172. | (a, b) -> RtnInit (Rel (b - a - 1))
  173. in
  174. let jmp =
  175. match dec with
  176. | FunDec _ -> RtnJmp (ExternFun (indexof dec))
  177. | FunDef _ -> RtnJmp (LocalFun (List.length args, labelof dec))
  178. | _ -> raise InvalidNode
  179. in
  180. InlineComment (init, nameof dec) ::
  181. (trav_all args) @
  182. [InlineComment (jmp, node2str node)]
  183. | Arg value -> trav value
  184. (* Conditional expression (short-circuit evaluation) *)
  185. (* <cond>
  186. * branch_f else
  187. * <true_expr>
  188. * jump end
  189. * else:
  190. * <false_expr>
  191. * end:
  192. *)
  193. | Cond (cond, texp, fexp, _) ->
  194. let elselabel = genlabel "false_expr" in
  195. let endlabel = genlabel "end" in
  196. (trav cond) @
  197. [Branch (false, elselabel)] @
  198. (trav texp) @
  199. [Jump (endlabel);
  200. Label (elselabel)] @
  201. (trav fexp) @
  202. [InlineComment (Label (endlabel), node2str node)]
  203. (* Arrays *)
  204. | Allocate (dec, [dim], _) ->
  205. let store =
  206. match (depthof dec, depthof node) with
  207. | (0, _) -> Store (typeof dec, Glob, indexof dec)
  208. | (a, b) when a = b -> Store (typeof dec, Current, indexof dec)
  209. | _ -> raise InvalidNode
  210. in
  211. trav dim @
  212. [NewArray (basetypeof dec);
  213. InlineComment (store, node2str node)]
  214. | Allocate _ ->
  215. raise (FatalError (NodeMsg (node, "invalid number of array dimensions \
  216. (should be one-dimensional)")))
  217. | VarUse (dec, Some dims, _) ->
  218. let load = Load (typeof dec, get_scope node dec, indexof dec) in
  219. (trav_all (List.rev dims)) @ (* push dimensions *)
  220. [InlineComment (load, nameof dec)] @ (* push array reference *)
  221. [InlineComment (LoadArray (basetypeof dec), node2str node)]
  222. | VarLet (dec, Some dims, value, _) ->
  223. let load = Load (typeof dec, get_scope node dec, indexof dec) in
  224. (trav_all (List.rev dims)) @ (* push dimensions *)
  225. (trav value) @ (* push value *)
  226. (trav_all dims) @ (* push dimensions *)
  227. [InlineComment (load, nameof dec)] @ (* push array reference *)
  228. [InlineComment (StoreArray (basetypeof dec), node2str node)]
  229. | _ -> raise InvalidNode
  230. in
  231. let instrs = trav program in
  232. (* Sort aggregated constants and add definitions
  233. * We might want to rewrite this in the future because it's a little
  234. * cumbersome right now... *)
  235. let pairs = ref [] in
  236. let add_pair value index =
  237. let com = sprintf "index %d" index in
  238. pairs := (InlineComment (ConstDef value, com), index) :: !pairs
  239. in
  240. Hashtbl.iter add_pair consts;
  241. let cmp (_, i) (_, j) = compare i j in
  242. let sorted_pairs = List.sort cmp !pairs in
  243. let const_defs = List.map fst sorted_pairs in
  244. instrs @ const_defs
  245. let phase = function
  246. | Ast node -> Assembly (assemble node)
  247. | _ -> raise InvalidInput