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