xref: /aosp_15_r20/external/llvm/examples/OCaml-Kaleidoscope/Chapter5/toy.ml (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker(*===----------------------------------------------------------------------===
2*9880d681SAndroid Build Coastguard Worker * Main driver code.
3*9880d681SAndroid Build Coastguard Worker *===----------------------------------------------------------------------===*)
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Workeropen Llvm
6*9880d681SAndroid Build Coastguard Workeropen Llvm_executionengine
7*9880d681SAndroid Build Coastguard Workeropen Llvm_target
8*9880d681SAndroid Build Coastguard Workeropen Llvm_scalar_opts
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Workerlet main () =
11*9880d681SAndroid Build Coastguard Worker  ignore (initialize_native_target ());
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard Worker  (* Install standard binary operators.
14*9880d681SAndroid Build Coastguard Worker   * 1 is the lowest precedence. *)
15*9880d681SAndroid Build Coastguard Worker  Hashtbl.add Parser.binop_precedence '<' 10;
16*9880d681SAndroid Build Coastguard Worker  Hashtbl.add Parser.binop_precedence '+' 20;
17*9880d681SAndroid Build Coastguard Worker  Hashtbl.add Parser.binop_precedence '-' 20;
18*9880d681SAndroid Build Coastguard Worker  Hashtbl.add Parser.binop_precedence '*' 40;    (* highest. *)
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker  (* Prime the first token. *)
21*9880d681SAndroid Build Coastguard Worker  print_string "ready> "; flush stdout;
22*9880d681SAndroid Build Coastguard Worker  let stream = Lexer.lex (Stream.of_channel stdin) in
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker  (* Create the JIT. *)
25*9880d681SAndroid Build Coastguard Worker  let the_execution_engine = ExecutionEngine.create Codegen.the_module in
26*9880d681SAndroid Build Coastguard Worker  let the_fpm = PassManager.create_function Codegen.the_module in
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker  (* Set up the optimizer pipeline.  Start with registering info about how the
29*9880d681SAndroid Build Coastguard Worker   * target lays out data structures. *)
30*9880d681SAndroid Build Coastguard Worker  DataLayout.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker  (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
33*9880d681SAndroid Build Coastguard Worker  add_instruction_combination the_fpm;
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker  (* reassociate expressions. *)
36*9880d681SAndroid Build Coastguard Worker  add_reassociation the_fpm;
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker  (* Eliminate Common SubExpressions. *)
39*9880d681SAndroid Build Coastguard Worker  add_gvn the_fpm;
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker  (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
42*9880d681SAndroid Build Coastguard Worker  add_cfg_simplification the_fpm;
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker  ignore (PassManager.initialize the_fpm);
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker  (* Run the main "interpreter loop" now. *)
47*9880d681SAndroid Build Coastguard Worker  Toplevel.main_loop the_fpm the_execution_engine stream;
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker  (* Print out all the generated code. *)
50*9880d681SAndroid Build Coastguard Worker  dump_module Codegen.the_module
51*9880d681SAndroid Build Coastguard Worker;;
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Workermain ()
54