11open Core
22
3- let setup_execution_engine () =
4- ( match Llvm_executionengine. initialize () with
5- | true -> ()
6- | false -> raise_s [% message " failed to initialize" ] ) ;
7- Llvm_executionengine. create Codegen. the_module
8-
9- let setup_pass_manager () =
10- let the_fpm = Llvm.PassManager. create_function Codegen. the_module in
11- (* Promote allocas to registers. *)
12- Llvm_scalar_opts. add_memory_to_register_promotion the_fpm ;
13- (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
14- Llvm_scalar_opts. add_instruction_combination the_fpm ;
15- (* reassociate expressions. *)
16- Llvm_scalar_opts. add_reassociation the_fpm ;
17- (* Eliminate Common SubExpressions. *)
18- Llvm_scalar_opts. add_gvn the_fpm ;
19- (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
20- Llvm_scalar_opts. add_cfg_simplification the_fpm ;
21- Llvm.PassManager. initialize the_fpm |> ignore ;
22- the_fpm
23-
24- let new_incremental () = Parser.Incremental. toplevel Lexing. dummy_pos
25-
26- let run_main input =
3+ let run_main in_channel ~the_fpm ~the_execution_engine =
274 let anonymous_func_count = ref 0 in
5+ let supplier =
6+ Parser.MenhirInterpreter. lexer_lexbuf_to_supplier Lexer. read
7+ (Lexing. from_channel in_channel)
8+ in
289 let rec run_loop the_fpm the_execution_engine supplier =
29- let incremental = new_incremental () in
10+ let incremental = Parser.Incremental. toplevel Lexing. dummy_pos in
3011 printf " \n ready> " ;
3112 Out_channel. flush stdout ;
3213 ( try
@@ -39,7 +20,7 @@ let run_main input =
3920 Llvm_executionengine. add_module Codegen. the_module
4021 the_execution_engine ;
4122 anonymous_func_count := ! anonymous_func_count + 1 ;
42- let tmp_name = sprintf " __anon_func_ %d" ! anonymous_func_count in
23+ let tmp_name = sprintf " __toplevel %d" ! anonymous_func_count in
4324 let tmp_func = Ast. set_func_name tmp_name func in
4425 let the_function = Codegen. codegen_func the_fpm tmp_func in
4526 Llvm. dump_value the_function ;
@@ -77,23 +58,36 @@ let run_main input =
7758 Out_channel. flush Out_channel. stdout ;
7859 run_loop the_fpm the_execution_engine supplier
7960 in
61+ run_loop the_fpm the_execution_engine supplier
62+
63+ let main input =
8064 (* Install standard binary operators.
8165 * 1 is the lowest precedence. *)
66+ Hashtbl. add_exn Ast. binop_precedence ~key: '=' ~data: 2 ;
8267 Hashtbl. add_exn Ast. binop_precedence ~key: '<' ~data: 10 ;
8368 Hashtbl. add_exn Ast. binop_precedence ~key: '+' ~data: 20 ;
8469 Hashtbl. add_exn Ast. binop_precedence ~key: '-' ~data: 20 ;
8570 Hashtbl. add_exn Ast. binop_precedence ~key: '*' ~data: 40 ;
86- Hashtbl. add_exn Ast. binop_precedence ~key: '=' ~data: 2 ;
87- let f in_channel =
88- let supplier =
89- Parser.MenhirInterpreter. lexer_lexbuf_to_supplier Lexer. read
90- (Lexing. from_channel in_channel)
91- in
92- (* Create the JIT. *)
93- let the_execution_engine = setup_execution_engine () in
94- let the_fpm = setup_pass_manager () in
95- run_loop the_fpm the_execution_engine supplier
71+ (* Create the JIT *)
72+ let the_execution_engine =
73+ ( match Llvm_executionengine. initialize () with
74+ | true -> ()
75+ | false -> raise_s [% message " failed to initialize" ] ) ;
76+ Llvm_executionengine. create Codegen. the_module
9677 in
78+ let the_fpm = Llvm.PassManager. create_function Codegen. the_module in
79+ (* Promote allocas to registers. *)
80+ Llvm_scalar_opts. add_memory_to_register_promotion the_fpm ;
81+ (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
82+ Llvm_scalar_opts. add_instruction_combination the_fpm ;
83+ (* reassociate expressions. *)
84+ Llvm_scalar_opts. add_reassociation the_fpm ;
85+ (* Eliminate Common SubExpressions. *)
86+ Llvm_scalar_opts. add_gvn the_fpm ;
87+ (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
88+ Llvm_scalar_opts. add_cfg_simplification the_fpm ;
89+ Llvm.PassManager. initialize the_fpm |> ignore ;
9790 match input with
98- | `Stdin -> f In_channel. stdin
99- | `File file -> In_channel. with_file file ~f
91+ | `Stdin -> run_main ~the_execution_engine ~the_fpm In_channel. stdin
92+ | `File file ->
93+ In_channel. with_file file ~f: (run_main ~the_execution_engine ~the_fpm )
0 commit comments