main.ml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ("dimreduce", Dim_reduce.phase, always,
  19. "Array dimension reduction");
  20. ("boolop", Bool_op.phase, always,
  21. "Convert bool operations");
  22. ("extern", Extern_vars.phase, always,
  23. "Create getters and setters for extern variables");
  24. ("constant", Constant_propagation.phase, when_optimize,
  25. "Constant propagation");
  26. ("index", Index_analysis.phase, always,
  27. "Index analysis");
  28. ("assemble", Assemble.phase, always,
  29. "Assembly");
  30. ("peephole", Peephole.phase, when_optimize,
  31. "Peephole optimization");
  32. ("output", Output.phase, always,
  33. "Output assembly");
  34. ]
  35. (* Compile CVC file to assembly code
  36. * in_channel -> int -> repr *)
  37. let compile () =
  38. let rec run_phases input = function
  39. | [] -> ()
  40. | (id, phase, cond, msg) :: tl ->
  41. let output = if cond () then (
  42. log_plain_line 2 (expand 13 ("- " ^ id ^ ":") ^ msg);
  43. let output = phase input in
  44. if id = args.endphase || args.verbose >= 2 then (
  45. let _ = Print.phase output in ()
  46. );
  47. output
  48. ) else input in
  49. if id = args.endphase then () else run_phases output tl
  50. in
  51. run_phases Empty phases
  52. (* Main function, returns exit status
  53. * Command-line arguments are stored in Util.args
  54. * () -> int *)
  55. let main () =
  56. let rec upto_usage = function
  57. | [] -> ""
  58. | (id, _, _, msg) :: tl ->
  59. "\n" ^ repeat " " 12 ^ expand 10 id ^ ": " ^ msg ^ (upto_usage tl)
  60. in
  61. let args_spec = [
  62. ("<file>", Arg.Rest (fun s -> ()),
  63. " Optional input file (default is to read from stdin)");
  64. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  65. "<file> Output file (defaults to foo.s for foo.cvc)");
  66. ("-v", Arg.Int (fun i -> args.verbose <- i),
  67. "<num> Set verbosity (0: nothing, 1: errors, 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. "<phase> Stop after the specified phase, and print the intermediate " ^
  78. "representation to stderr.\n " ^
  79. " Possible options are (in order of execution):" ^ 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. (* If no location is given, just stringify the node to at least give
  97. * some information *)
  98. let msg = if locof node = noloc then
  99. msg ^ "\nnode: " ^ Stringify.node2str node
  100. else msg in
  101. raise (LocError (locof node, msg))
  102. with
  103. | CompileError msg ->
  104. eprintf "Error: %s\n" msg;
  105. 1
  106. | LocError (loc, msg) ->
  107. prerr_loc_msg loc ("Error: " ^ msg);
  108. 1
  109. | EmptyError ->
  110. 1
  111. let _ = exit (main ())