annotate ppc/main.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* ppc/main.ml *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 open Print
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 open Mach
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 open Source
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 let debug = ref 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 let usage = "Usage: ppc [-b] [-d n] [-O] file.p"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 let spec =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 Arg.align
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 ["-b", Arg.Unit (function () -> Kgen.boundchk := true),
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 " enable bound checks";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 "-d", Arg.Int (function x -> debug := x), "n set debug level";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 "-O", Arg.Set Kgen.optflag, " enable peephole optimiser"]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 let main () =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 let fns = ref [] in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 Arg.parse spec (function s -> fns := !fns @ [s]) usage;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 if List.length !fns <> 1 then begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 fprintf stderr "$\n" [fStr usage]; exit 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 let in_file = List.hd !fns in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 let in_chan = open_in in_file in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 let lexbuf = Lexing.from_channel in_chan in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 Source.init in_file in_chan;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 Peepopt.debug := !debug;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 ignore (Parsing.set_trace (!debug > 2));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 let prog =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 try Parser.program Lexer.token lexbuf with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 Parsing.Parse_error ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 let tok = Lexing.lexeme lexbuf in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 Source.err_message "syntax error at token '$'" [fStr tok] !Lexer.lineno;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 exit 1 in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 if !debug > 0 then Tree.print_tree stdout "" prog;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 begin try Check.annotate prog with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 Check.Sem_error (fmt, args, ln) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 Source.err_message fmt args ln;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 exit 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 printf "MODULE Main 0 0\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 printf "IMPORT Lib 0\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 printf "ENDHDR\n\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 Kgen.translate prog;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 printf "! End\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 exit 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 let ppc = main ()