main.ml 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. open Printf
  2. open Lexing
  3. open Types
  4. open Util
  5. (* Compile CVC file to assembly code
  6. * in_channel -> int -> repr *)
  7. let compile () =
  8. let rec run_phases input = function
  9. | [] -> ()
  10. | h::t -> run_phases (h input) t
  11. in
  12. run_phases Empty [
  13. Load.phase;
  14. Print.phase;
  15. Parse.phase;
  16. (*Print.phase;*)
  17. Desug.phase;
  18. (*Print.phase;*)
  19. Context_analysis.phase;
  20. (*Print.phase;*)
  21. Typecheck.phase;
  22. (*Print.phase;*)
  23. Expand_dims.phase;
  24. (*Print.phase;*)
  25. Bool_op.phase;
  26. (*Print.phase;*)
  27. Dim_reduce.phase;
  28. (*Print.phase;*)
  29. Extern_vars.phase;
  30. (*Print.phase;*)
  31. Constant_propagation.phase;
  32. Print.phase;
  33. Depth_analysis.phase;
  34. Print.phase;
  35. Assemble.phase;
  36. Print.phase;
  37. (*
  38. Peephole.phase;
  39. Print.phase;
  40. *)
  41. Output.phase;
  42. ]
  43. (* Main function, returns exit status
  44. * Command-line arguments are stored in Util.args
  45. * () -> int *)
  46. let main () =
  47. let args_spec = [
  48. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  49. "Output file (defaults to foo.s for foo.cvc)");
  50. ("-v", Arg.Int (fun i -> args.verbose <- i),
  51. "Set verbosity (0: nothing, 1: phase titles, 2: intermediate, 3: debug)");
  52. ("-nocpp", Arg.Unit (fun _ -> args.cpp <- false),
  53. "Disable C preprocessor");
  54. ("-noopt", Arg.Unit (fun _ -> args.optimize <- false),
  55. "Disable optimization");
  56. ] in
  57. let usage =
  58. "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-nocpp] [-noopt] " ^
  59. " [-v <verbosity>] [<file>]"
  60. in
  61. try
  62. try
  63. Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
  64. compile ();
  65. 0
  66. with
  67. (*| InvalidNode ->
  68. raise (CompileError "invalid node")*)
  69. | InvalidInput name ->
  70. raise (CompileError ("invalid input for phase \"" ^ name ^ "\""))
  71. | NodeError (node, msg) ->
  72. raise (LocError (locof node, msg))
  73. with
  74. | CompileError msg ->
  75. eprintf "Error: %s\n" msg;
  76. 1
  77. | LocError (loc, msg) ->
  78. prerr_loc_msg loc ("Error: " ^ msg);
  79. 1
  80. | EmptyError ->
  81. 1
  82. let _ = exit (main ())