-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy.ml
56 lines (40 loc) · 1.54 KB
/
toy.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(*===----------------------------------------------------------------------===
* Main driver code.
*===----------------------------------------------------------------------===*)
open Batteries
module L = Llvm
module E = Llvm_executionengine
module O = Llvm_scalar_opts
let filename = ref None
let set_filename s = filename := Some s
let speclist = [
("-f", Arg.String set_filename, ": input filename")
]
let main () =
Arg.parse speclist set_filename "usage";
ignore (E.initialize ());
(* Prime the first token. *)
print_string "ready> "; flush stdout;
(* Create the JIT. *)
let the_execution_engine = E.create Codegen.the_module in
let the_fpm = L.PassManager.create_function Codegen.the_module in
(* Set up the optimizer pipeline. *)
(* Do simple "peephole" optimizations and bit-twiddling optzn. *)
O.add_instruction_combination the_fpm;
(* reassociate expressions. *)
O.add_reassociation the_fpm;
(* Eliminate Common SubExpressions. *)
O.add_gvn the_fpm;
(* Simplify the control flow graph (deleting unreachable blocks, etc). *)
O.add_cfg_simplification the_fpm;
ignore (L.PassManager.initialize the_fpm);
(* Run the main "interpreter loop" now. *)
let fname, input = match !filename with
| None -> "<stdin>", Lexing.from_channel stdin
| Some s -> s, Lexing.from_channel @@ open_in s in
input.lex_curr_p <- { input.lex_curr_p with pos_fname = fname };
Toplevel.main_loop the_fpm the_execution_engine input;
(* Print out all the generated code. *)
L.dump_module Codegen.the_module
;;
main ()