main.ml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. open Printf
  2. open Lexing
  3. open Types
  4. open Util
  5. let always _ = true
  6. let when_optimize _ = args.optimize
  7. let phases = [
  8. ("load", Load.phase, always,
  9. "Load input file");
  10. ("parse", Parse.phase, always,
  11. "Parse input");
  12. ("desug", Desug.phase, always,
  13. "Desugaring");
  14. ("context", Context_analysis.phase, always,
  15. "Context analysis");
  16. ("typecheck", Typecheck.phase, always,
  17. "Type checking");
  18. ("expand", Expand_dims.phase, always,
  19. "Expand array dimensions");
  20. ("boolop", Bool_op.phase, always,
  21. "Convert bool operations");
  22. ("dimreduce", Dim_reduce.phase, always,
  23. "Array dimension reduction");
  24. ("extern", Extern_vars.phase, always,
  25. "Create getters and setters for extern variables");
  26. ("constant", Constant_propagation.phase, when_optimize,
  27. "Constant propagation");
  28. ("index", Index_analysis.phase, always,
  29. "Index analysis");
  30. ("assemble", Assemble.phase, always,
  31. "Assembly");
  32. ("peephole", Peephole.phase, when_optimize,
  33. "Peephole optimization");
  34. ("output", Output.phase, always,
  35. "Output assembly");
  36. ]
  37. (* Compile CVC file to assembly code
  38. * in_channel -> int -> repr *)
  39. let compile () =
  40. let rec run_phases input = function
  41. | [] -> ()
  42. | (id, phase, cond, msg) :: tl ->
  43. let output = if cond () then (
  44. log_plain_line 1 (expand 13 ("- " ^ id ^ ":") ^ msg);
  45. let output = phase input in
  46. if id = args.endphase || args.verbose = 2 then (
  47. let _ = Print.phase output in ()
  48. );
  49. output
  50. ) else input in
  51. if id = args.endphase then () else run_phases output tl
  52. in
  53. run_phases Empty phases
  54. (* Main function, returns exit status
  55. * Command-line arguments are stored in Util.args
  56. * () -> int *)
  57. let main () =
  58. let rec upto_usage = function
  59. | [] -> ""
  60. | (id, _, _, msg) :: tl ->
  61. "\n" ^ repeat " " 8 ^ expand 10 id ^ ": " ^ msg ^ (upto_usage tl)
  62. in
  63. let args_spec = [
  64. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  65. "Output file (defaults to foo.s for foo.cvc)");
  66. ("-v", Arg.Int (fun i -> args.verbose <- i),
  67. "Set verbosity (0: nothing, 1: phase titles, 2: intermediate, 3: debug)");
  68. ("-nocpp", Arg.Unit (fun _ -> args.cpp <- false),
  69. "Disable C preprocessor");
  70. ("-cpp", Arg.Unit (fun _ -> args.cpp <- true),
  71. "Enable C preprocessor (overwrite earlier -nocpp)");
  72. ("-noopt", Arg.Unit (fun _ -> args.optimize <- false),
  73. "Disable optimization");
  74. ("-opt", Arg.Unit (fun _ -> args.optimize <- true),
  75. "Enable optimization (overwrite earlier -nocpp)");
  76. ("-upto", Arg.String (fun s -> args.endphase <- s),
  77. "Stop after the specified phase, and print the intermediate " ^
  78. "representation to stderr.\n " ^
  79. "Possible options are:" ^ upto_usage phases);
  80. ] in
  81. let usage =
  82. "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-nocpp] [-noopt] " ^
  83. " [-v <verbosity>] [-upto <phase>] [<file>]"
  84. in
  85. try
  86. try
  87. Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
  88. compile ();
  89. 0
  90. with
  91. (*| InvalidNode ->
  92. raise (CompileError "invalid node")*)
  93. | InvalidInput name ->
  94. raise (CompileError ("invalid input for phase \"" ^ name ^ "\""))
  95. | NodeError (node, msg) ->
  96. raise (LocError (locof node, msg))
  97. with
  98. | CompileError msg ->
  99. eprintf "Error: %s\n" msg;
  100. 1
  101. | LocError (loc, msg) ->
  102. prerr_loc_msg loc ("Error: " ^ msg);
  103. 1
  104. | EmptyError ->
  105. 1
  106. let _ = exit (main ())