1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 open Clflags
17 let write_asm_file = ref false
18
19 let compile_file filename =
20 if !write_asm_file then begin
21 let out_name = Filename.chop_extension filename ^ ".s" in
22 Emitaux.output_channel := open_out out_name
23 end; (* otherwise, stdout *)
24 Compilenv.reset "test";
25 Emit.begin_assembly();
26 let ic = open_in filename in
27 let lb = Lexing.from_channel ic in
28 lb.Lexing.lex_curr_p <- Lexing.{ lb.lex_curr_p with pos_fname = filename };
29 try
30 while true do
31 Asmgen.compile_phrase ~ppf_dump:Format.std_formatter
32 (Parsecmm.phrase Lexcmm.token lb)
33 done
34 with
35 End_of_file ->
36 close_in ic; Emit.end_assembly();
37 if !write_asm_file then close_out !Emitaux.output_channel
38 | Lexcmm.Error msg ->
39 close_in ic; Lexcmm.report_error lb msg
40 | Parsing.Parse_error ->
41 close_in ic;
42 let start_p = Lexing.lexeme_start_p lb in
43 let end_p = Lexing.lexeme_end_p lb in
44 Printf.eprintf "File \"%s\", line %i, characters %i-%i:\n\
45 Syntax error.\n%!"
46 filename
47 start_p.Lexing.pos_lnum
48 (start_p.Lexing.pos_cnum - start_p.Lexing.pos_bol)
49 (end_p.Lexing.pos_cnum - start_p.Lexing.pos_bol)
50 | Parsecmmaux.Error msg ->
51 close_in ic; Parsecmmaux.report_error msg
52 | x ->
53 close_in ic; raise x
54
55 let usage = "Usage: codegen <options> <files>\noptions are:"
56
57 let main() =
58 Arg.parse [
59 "-S", Arg.Set write_asm_file,
60 " Output file to filename.s (default is stdout)";
61 "-g", Arg.Set Clflags.debug, "";
62 "-dcmm", Arg.Set dump_cmm, "";
63 "-dcse", Arg.Set dump_cse, "";
64 "-dsel", Arg.Set dump_selection, "";
65 "-dlive", Arg.Unit(fun () -> dump_live := true ), "";
66 "-dspill", Arg.Set dump_spill, "";
67 "-dsplit", Arg.Set dump_split, "";
68 "-dinterf", Arg.Set dump_interf, "";
69 "-dprefer", Arg.Set dump_prefer, "";
70 "-dalloc", Arg.Set dump_regalloc, "";
71 "-dreload", Arg.Set dump_reload, "";
72 "-dscheduling", Arg.Set dump_scheduling, "";
73 "-dlinear", Arg.Set dump_linear, "";
74 "-dtimings", Arg.Unit (fun () -> profile_columns := [ `Time ]), "";
75 ] compile_file usage
76
77 let () =
78 main ();
79 Profile.print Format.std_formatter !Clflags.profile_columns;
80 exit 0
81