33 *===----------------------------------------------------------------------===*)
44
55open Llvm
6+ open Llvm_executionengine
67
78(* top ::= definition | external | expression | ';' *)
8- let rec main_loop stream =
9+ let rec main_loop the_fpm the_execution_engine stream =
910 match Stream. peek stream with
1011 | None -> ()
1112
1213 (* ignore top-level semicolons. *)
1314 | Some (Token. Kwd ';' ) ->
1415 Stream. junk stream;
15- main_loop stream
16+ main_loop the_fpm the_execution_engine stream
1617
1718 | Some token ->
1819 begin
1920 try match token with
2021 | Token. Def ->
2122 let e = Parser. parse_definition stream in
2223 print_endline " parsed a function definition." ;
23- dump_value (Codegen. codegen_func e);
24+ dump_value (Codegen. codegen_func the_fpm e);
2425 | Token. Extern ->
2526 let e = Parser. parse_extern stream in
2627 print_endline " parsed an extern." ;
@@ -29,11 +30,20 @@ let rec main_loop stream =
2930 (* Evaluate a top-level expression into an anonymous function. *)
3031 let e = Parser. parse_toplevel stream in
3132 print_endline " parsed a top-level expr" ;
32- dump_value (Codegen. codegen_func e);
33+ let the_function = Codegen. codegen_func the_fpm e in
34+ dump_value the_function;
35+
36+ (* JIT the function, returning a function pointer. *)
37+ let result = ExecutionEngine. run_function the_function [||]
38+ the_execution_engine in
39+
40+ print_string " Evaluated to " ;
41+ print_float (GenericValue. as_float Codegen. double_type result);
42+ print_newline () ;
3343 with Stream. Error s | Codegen. Error s ->
3444 (* Skip token for error recovery. *)
3545 Stream. junk stream;
3646 print_endline s;
3747 end ;
3848 print_string " ready> " ; flush stdout;
39- main_loop stream
49+ main_loop the_fpm the_execution_engine stream
0 commit comments