/3rd_party/llvm/examples/OCaml-Kaleidoscope/Chapter6/codegen.ml

https://code.google.com/p/softart/ · OCaml · 251 lines · 149 code · 45 blank · 57 comment · 4 complexity · 1e7439f89f77172b3e488332bca0993e MD5 · raw file

  1. (*===----------------------------------------------------------------------===
  2. * Code Generation
  3. *===----------------------------------------------------------------------===*)
  4. open Llvm
  5. exception Error of string
  6. let context = global_context ()
  7. let the_module = create_module context "my cool jit"
  8. let builder = builder context
  9. let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
  10. let double_type = double_type context
  11. let rec codegen_expr = function
  12. | Ast.Number n -> const_float double_type n
  13. | Ast.Variable name ->
  14. (try Hashtbl.find named_values name with
  15. | Not_found -> raise (Error "unknown variable name"))
  16. | Ast.Unary (op, operand) ->
  17. let operand = codegen_expr operand in
  18. let callee = "unary" ^ (String.make 1 op) in
  19. let callee =
  20. match lookup_function callee the_module with
  21. | Some callee -> callee
  22. | None -> raise (Error "unknown unary operator")
  23. in
  24. build_call callee [|operand|] "unop" builder
  25. | Ast.Binary (op, lhs, rhs) ->
  26. let lhs_val = codegen_expr lhs in
  27. let rhs_val = codegen_expr rhs in
  28. begin
  29. match op with
  30. | '+' -> build_fadd lhs_val rhs_val "addtmp" builder
  31. | '-' -> build_fsub lhs_val rhs_val "subtmp" builder
  32. | '*' -> build_fmul lhs_val rhs_val "multmp" builder
  33. | '<' ->
  34. (* Convert bool 0/1 to double 0.0 or 1.0 *)
  35. let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in
  36. build_uitofp i double_type "booltmp" builder
  37. | _ ->
  38. (* If it wasn't a builtin binary operator, it must be a user defined
  39. * one. Emit a call to it. *)
  40. let callee = "binary" ^ (String.make 1 op) in
  41. let callee =
  42. match lookup_function callee the_module with
  43. | Some callee -> callee
  44. | None -> raise (Error "binary operator not found!")
  45. in
  46. build_call callee [|lhs_val; rhs_val|] "binop" builder
  47. end
  48. | Ast.Call (callee, args) ->
  49. (* Look up the name in the module table. *)
  50. let callee =
  51. match lookup_function callee the_module with
  52. | Some callee -> callee
  53. | None -> raise (Error "unknown function referenced")
  54. in
  55. let params = params callee in
  56. (* If argument mismatch error. *)
  57. if Array.length params == Array.length args then () else
  58. raise (Error "incorrect # arguments passed");
  59. let args = Array.map codegen_expr args in
  60. build_call callee args "calltmp" builder
  61. | Ast.If (cond, then_, else_) ->
  62. let cond = codegen_expr cond in
  63. (* Convert condition to a bool by comparing equal to 0.0 *)
  64. let zero = const_float double_type 0.0 in
  65. let cond_val = build_fcmp Fcmp.One cond zero "ifcond" builder in
  66. (* Grab the first block so that we might later add the conditional branch
  67. * to it at the end of the function. *)
  68. let start_bb = insertion_block builder in
  69. let the_function = block_parent start_bb in
  70. let then_bb = append_block context "then" the_function in
  71. (* Emit 'then' value. *)
  72. position_at_end then_bb builder;
  73. let then_val = codegen_expr then_ in
  74. (* Codegen of 'then' can change the current block, update then_bb for the
  75. * phi. We create a new name because one is used for the phi node, and the
  76. * other is used for the conditional branch. *)
  77. let new_then_bb = insertion_block builder in
  78. (* Emit 'else' value. *)
  79. let else_bb = append_block context "else" the_function in
  80. position_at_end else_bb builder;
  81. let else_val = codegen_expr else_ in
  82. (* Codegen of 'else' can change the current block, update else_bb for the
  83. * phi. *)
  84. let new_else_bb = insertion_block builder in
  85. (* Emit merge block. *)
  86. let merge_bb = append_block context "ifcont" the_function in
  87. position_at_end merge_bb builder;
  88. let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
  89. let phi = build_phi incoming "iftmp" builder in
  90. (* Return to the start block to add the conditional branch. *)
  91. position_at_end start_bb builder;
  92. ignore (build_cond_br cond_val then_bb else_bb builder);
  93. (* Set a unconditional branch at the end of the 'then' block and the
  94. * 'else' block to the 'merge' block. *)
  95. position_at_end new_then_bb builder; ignore (build_br merge_bb builder);
  96. position_at_end new_else_bb builder; ignore (build_br merge_bb builder);
  97. (* Finally, set the builder to the end of the merge block. *)
  98. position_at_end merge_bb builder;
  99. phi
  100. | Ast.For (var_name, start, end_, step, body) ->
  101. (* Emit the start code first, without 'variable' in scope. *)
  102. let start_val = codegen_expr start in
  103. (* Make the new basic block for the loop header, inserting after current
  104. * block. *)
  105. let preheader_bb = insertion_block builder in
  106. let the_function = block_parent preheader_bb in
  107. let loop_bb = append_block context "loop" the_function in
  108. (* Insert an explicit fall through from the current block to the
  109. * loop_bb. *)
  110. ignore (build_br loop_bb builder);
  111. (* Start insertion in loop_bb. *)
  112. position_at_end loop_bb builder;
  113. (* Start the PHI node with an entry for start. *)
  114. let variable = build_phi [(start_val, preheader_bb)] var_name builder in
  115. (* Within the loop, the variable is defined equal to the PHI node. If it
  116. * shadows an existing variable, we have to restore it, so save it
  117. * now. *)
  118. let old_val =
  119. try Some (Hashtbl.find named_values var_name) with Not_found -> None
  120. in
  121. Hashtbl.add named_values var_name variable;
  122. (* Emit the body of the loop. This, like any other expr, can change the
  123. * current BB. Note that we ignore the value computed by the body, but
  124. * don't allow an error *)
  125. ignore (codegen_expr body);
  126. (* Emit the step value. *)
  127. let step_val =
  128. match step with
  129. | Some step -> codegen_expr step
  130. (* If not specified, use 1.0. *)
  131. | None -> const_float double_type 1.0
  132. in
  133. let next_var = build_add variable step_val "nextvar" builder in
  134. (* Compute the end condition. *)
  135. let end_cond = codegen_expr end_ in
  136. (* Convert condition to a bool by comparing equal to 0.0. *)
  137. let zero = const_float double_type 0.0 in
  138. let end_cond = build_fcmp Fcmp.One end_cond zero "loopcond" builder in
  139. (* Create the "after loop" block and insert it. *)
  140. let loop_end_bb = insertion_block builder in
  141. let after_bb = append_block context "afterloop" the_function in
  142. (* Insert the conditional branch into the end of loop_end_bb. *)
  143. ignore (build_cond_br end_cond loop_bb after_bb builder);
  144. (* Any new code will be inserted in after_bb. *)
  145. position_at_end after_bb builder;
  146. (* Add a new entry to the PHI node for the backedge. *)
  147. add_incoming (next_var, loop_end_bb) variable;
  148. (* Restore the unshadowed variable. *)
  149. begin match old_val with
  150. | Some old_val -> Hashtbl.add named_values var_name old_val
  151. | None -> ()
  152. end;
  153. (* for expr always returns 0.0. *)
  154. const_null double_type
  155. let codegen_proto = function
  156. | Ast.Prototype (name, args) | Ast.BinOpPrototype (name, args, _) ->
  157. (* Make the function type: double(double,double) etc. *)
  158. let doubles = Array.make (Array.length args) double_type in
  159. let ft = function_type double_type doubles in
  160. let f =
  161. match lookup_function name the_module with
  162. | None -> declare_function name ft the_module
  163. (* If 'f' conflicted, there was already something named 'name'. If it
  164. * has a body, don't allow redefinition or reextern. *)
  165. | Some f ->
  166. (* If 'f' already has a body, reject this. *)
  167. if block_begin f <> At_end f then
  168. raise (Error "redefinition of function");
  169. (* If 'f' took a different number of arguments, reject. *)
  170. if element_type (type_of f) <> ft then
  171. raise (Error "redefinition of function with different # args");
  172. f
  173. in
  174. (* Set names for all arguments. *)
  175. Array.iteri (fun i a ->
  176. let n = args.(i) in
  177. set_value_name n a;
  178. Hashtbl.add named_values n a;
  179. ) (params f);
  180. f
  181. let codegen_func the_fpm = function
  182. | Ast.Function (proto, body) ->
  183. Hashtbl.clear named_values;
  184. let the_function = codegen_proto proto in
  185. (* If this is an operator, install it. *)
  186. begin match proto with
  187. | Ast.BinOpPrototype (name, args, prec) ->
  188. let op = name.[String.length name - 1] in
  189. Hashtbl.add Parser.binop_precedence op prec;
  190. | _ -> ()
  191. end;
  192. (* Create a new basic block to start insertion into. *)
  193. let bb = append_block context "entry" the_function in
  194. position_at_end bb builder;
  195. try
  196. let ret_val = codegen_expr body in
  197. (* Finish off the function. *)
  198. let _ = build_ret ret_val builder in
  199. (* Validate the generated code, checking for consistency. *)
  200. Llvm_analysis.assert_valid_function the_function;
  201. (* Optimize the function. *)
  202. let _ = PassManager.run_function the_function the_fpm in
  203. the_function
  204. with e ->
  205. delete_function the_function;
  206. raise e