toy.ml revision 49457b81589c6a3ef384caa8fdf1463ad2ada7c7
149457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar(*===----------------------------------------------------------------------===
249457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar * Main driver code.
349457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar *===----------------------------------------------------------------------===*)
449457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
549457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaaropen Llvm
649457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaaropen Llvm_executionengine
749457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaaropen Llvm_target
849457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaaropen Llvm_scalar_opts
949457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
1049457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaarlet main () =
1149457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  ignore (initialize_native_target ());
1249457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
1349457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Install standard binary operators.
1449457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar   * 1 is the lowest precedence. *)
1549457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  Hashtbl.add Parser.binop_precedence '<' 10;
1649457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  Hashtbl.add Parser.binop_precedence '+' 20;
1749457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  Hashtbl.add Parser.binop_precedence '-' 20;
1849457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  Hashtbl.add Parser.binop_precedence '*' 40;    (* highest. *)
1949457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
2049457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Prime the first token. *)
2149457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  print_string "ready> "; flush stdout;
2249457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  let stream = Lexer.lex (Stream.of_channel stdin) in
2349457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
2449457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Create the JIT. *)
2549457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  let the_execution_engine = ExecutionEngine.create Codegen.the_module in
2649457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  let the_fpm = PassManager.create_function Codegen.the_module in
2749457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
2849457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Set up the optimizer pipeline.  Start with registering info about how the
2949457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar   * target lays out data structures. *)
3049457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
3149457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
3249457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
3349457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  add_instruction_combination the_fpm;
3449457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
3549457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* reassociate expressions. *)
3649457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  add_reassociation the_fpm;
3749457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
3849457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Eliminate Common SubExpressions. *)
3949457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  add_gvn the_fpm;
4049457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
4149457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
4249457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  add_cfg_simplification the_fpm;
4349457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
4449457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  ignore (PassManager.initialize the_fpm);
4549457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
4649457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Run the main "interpreter loop" now. *)
4749457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  Toplevel.main_loop the_fpm the_execution_engine stream;
4849457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
4949457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  (* Print out all the generated code. *)
5049457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar  dump_module Codegen.the_module
5149457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar;;
5249457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaar
5349457b81589c6a3ef384caa8fdf1463ad2ada7c7Erick Tryzelaarmain ()
54