assemble.ml 9.6 KB

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