assemble.ml 8.8 KB

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