main.ml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. Peephole.phase;
  38. Print.phase;
  39. Output.phase;
  40. ]
  41. (* Main function, returns exit status
  42. * Command-line arguments are stored in Util.args
  43. * () -> int *)
  44. let main () =
  45. let args_spec = [
  46. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  47. "Output file (defaults to foo.s for foo.cvc)");
  48. ("-v", Arg.Int (fun i -> args.verbose <- i),
  49. "Set verbosity (0: nothing, 1: phase titles, 2: intermediate, 3: debug)");
  50. ("-nocpp", Arg.Unit (fun _ -> args.cpp <- false),
  51. "Disable C preprocessor");
  52. ("-noopt", Arg.Unit (fun _ -> args.optimize <- false),
  53. "Disable optimization");
  54. ] in
  55. let usage =
  56. "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-nocpp] [-noopt] " ^
  57. " [-v <verbosity>] [<file>]"
  58. in
  59. try
  60. try
  61. Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
  62. compile ();
  63. 0
  64. with
  65. (*| InvalidNode ->
  66. raise (CompileError "invalid node")*)
  67. | InvalidInput name ->
  68. raise (CompileError ("invalid input for phase \"" ^ name ^ "\""))
  69. | NodeError (node, msg) ->
  70. raise (LocError (locof node, msg))
  71. with
  72. | CompileError msg ->
  73. eprintf "Error: %s\n" msg;
  74. 1
  75. | LocError (loc, msg) ->
  76. prerr_loc_msg loc ("Error: " ^ msg);
  77. 1
  78. | EmptyError ->
  79. 1
  80. let _ = exit (main ())