/3rd_party/llvm/examples/OCaml-Kaleidoscope/Chapter7/toy.ml

https://code.google.com/p/softart/ · OCaml · 57 lines · 26 code · 15 blank · 16 comment · 0 complexity · 9f32059e86c5bc8ae0130ab2e947601d MD5 · raw file

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