main.ml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. open Printf
  2. open Lexing
  3. open Types
  4. open Util
  5. (* For some reason OCaml wants me to redefine this type's implementation -.- *)
  6. type phase_func = Types.intermediate -> Types.intermediate
  7. let always _ = true
  8. let when_optimize _ = Globals.args.optimize
  9. (* List of all phases, which will be executed in the order defined here. *)
  10. let phases = [
  11. ("load", Load.phase, always,
  12. "Load input file");
  13. ("parse", Parse.phase, always,
  14. "Parse input");
  15. ("desug", Desug.phase, always,
  16. "Desugaring");
  17. ("context", Context.phase, always,
  18. "Context analysis");
  19. ("typecheck", Typecheck.phase, always,
  20. "Type checking");
  21. ("dimreduce", Dimreduce.phase, always,
  22. "Array dimension reduction");
  23. ("boolop", Boolop.phase, always,
  24. "Convert bool operations");
  25. ("extern", Extern.phase, always,
  26. "Create getters and setters for extern variables");
  27. ("constprop", Constprop.phase, when_optimize,
  28. "Constant propagation");
  29. ("index", Index.phase, always,
  30. "Index analysis");
  31. ("assemble", Assemble.phase, always,
  32. "Assembly");
  33. ("peephole", Peephole.phase, when_optimize,
  34. "Peephole optimization");
  35. ("output", Output.phase, always,
  36. "Output assembly");
  37. ]
  38. (* Parse command-line arguments *)
  39. let parse_args () =
  40. let rec upto_usage = function
  41. | [] -> ""
  42. | (id, _, _, msg) :: tl ->
  43. "\n" ^ repeat " " 12 ^ expand 10 id ^ ": " ^ msg ^ (upto_usage tl)
  44. in
  45. let args_spec = [
  46. ("<file>", Arg.Rest (fun s -> ()),
  47. " Optional input file (default is to read from stdin)");
  48. ("-o", Arg.String (fun s -> Globals.args.outfile <- Some s),
  49. "<file> Output file (defaults to foo.s for foo.cvc)");
  50. ("-v", Arg.Int (fun i -> Globals.args.verbose <- i),
  51. "<num> Set verbosity (0: nothing, 1: errors, 2: intermediate, 3: debug)");
  52. ("-nocpp", Arg.Unit (fun _ -> Globals.args.cpp <- false),
  53. " Disable C preprocessor");
  54. ("-cpp", Arg.Unit (fun _ -> Globals.args.cpp <- true),
  55. " Enable C preprocessor (overwrite earlier -nocpp)");
  56. ("-noopt", Arg.Unit (fun _ -> Globals.args.optimize <- false),
  57. " Disable optimization");
  58. ("-opt", Arg.Unit (fun _ -> Globals.args.optimize <- true),
  59. " Enable optimization (overwrite earlier -nocpp)");
  60. ("-upto", Arg.String (fun s -> Globals.args.endphase <- s),
  61. "<phase> Stop after the specified phase, and print the intermediate \
  62. representation to stderr.\n \
  63. Possible options are (in order of execution):" ^ upto_usage phases);
  64. ] in
  65. let usage =
  66. "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-nocpp] [-noopt] \
  67. [-v <verbosity>] [-upto <phase>] [<file>]"
  68. in
  69. Arg.parse args_spec (fun s -> Globals.args.infile <- Some s) usage
  70. (* Compile CVC file to assembly code *)
  71. let compile () =
  72. let rec run_phases input = function
  73. | [] -> ()
  74. | (id, phase, cond, msg) :: tl ->
  75. let output =
  76. if cond () then begin
  77. log_plain_line 2 (expand 13 ("- " ^ id ^ ":") ^ msg);
  78. let output = phase input in
  79. if id = Globals.args.endphase || Globals.args.verbose >= 2 then begin
  80. ignore (Print.phase output)
  81. end;
  82. output
  83. end else
  84. input
  85. in
  86. if id = Globals.args.endphase then () else run_phases output tl
  87. in
  88. run_phases Empty phases
  89. (* Main function, returns exit status
  90. * Command-line arguments are stored in lobals.args *)
  91. let main () =
  92. try
  93. try
  94. parse_args ();
  95. compile ();
  96. 0
  97. with
  98. (*| InvalidNode ->
  99. raise (CompileError "invalid node")*)
  100. | InvalidInput name ->
  101. raise (CompileError ("invalid input for phase \"" ^ name ^ "\""))
  102. | NodeError (node, msg) ->
  103. (* If no location is given, just stringify the node to at least give
  104. * some information *)
  105. let msg = if locof node = noloc then
  106. msg ^ "\nnode: " ^ Stringify.node2str node
  107. else msg in
  108. raise (LocError (locof node, msg))
  109. with
  110. | CompileError msg ->
  111. eprintf "Error: %s\n" msg;
  112. 1
  113. | LocError (loc, msg) ->
  114. prerr_loc_msg loc ("Error: " ^ msg);
  115. 1
  116. | EmptyError ->
  117. 1
  118. let _ = exit (main ())