annotate lab4/main.ml @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
mike@0 1 (* lab4/main.ml *)
mike@0 2 (* Copyright (c) 2017 J. M. Spivey *)
mike@0 3
mike@0 4 open Print
mike@0 5 open Mach
mike@0 6 open Source
mike@0 7
mike@0 8 let debug = ref 0
mike@0 9
mike@0 10 let usage = "Usage: ppc0 [-v] [-t] file"
mike@0 11
mike@0 12 let spec =
mike@0 13 Arg.align
mike@0 14 ["-b", Arg.Unit (fun () -> Tgen.boundchk := true),
mike@0 15 " enable bound checks";
mike@0 16 "-d", Arg.Int (function x -> debug := x), "n set debug level";
mike@0 17 "-O", Arg.Unit (fun () -> Tgen.optlevel := 1),
mike@0 18 " enable optimiser (simplifier, jump opt)";
mike@0 19 "-O2", Arg.Unit (fun () -> Tgen.optlevel := 2),
mike@0 20 " more optimisation (common subexpressions)";
mike@0 21 "-noregvars", Arg.Unit (fun () -> Check.regvars := false),
mike@0 22 " disable register variables"]
mike@0 23
mike@0 24 let main () =
mike@0 25 let fns = ref [] in
mike@0 26 Arg.parse spec (function s -> fns := !fns @ [s]) usage;
mike@0 27 if List.length !fns <> 1 then begin
mike@0 28 fprintf stderr "$\n" [fStr usage]; exit 2
mike@0 29 end;
mike@0 30 let in_file = List.hd !fns in
mike@0 31 let in_chan = open_in in_file in
mike@0 32 let lexbuf = Lexing.from_channel in_chan in
mike@0 33 Source.init in_file in_chan;
mike@0 34 Tgen.debug := !debug;
mike@0 35 Tran.debug := !debug;
mike@0 36 ignore (Parsing.set_trace (!debug > 2));
mike@0 37
mike@0 38 (* Parse the program *)
mike@0 39 let prog =
mike@0 40 try Parser.program Lexer.token lexbuf with
mike@0 41 Parsing.Parse_error ->
mike@0 42 let tok = Lexing.lexeme lexbuf in
mike@0 43 Source.err_message "syntax error at token '$'" [fStr tok] !Lexer.lineno;
mike@0 44 exit 1 in
mike@0 45
mike@0 46 if !debug > 2 then begin
mike@0 47 printf "$Abstract syntax tree:\n" [fStr Mach.comment];
mike@0 48 Tree.print_tree stdout Mach.comment prog;
mike@0 49 printf "\n" []
mike@0 50 end;
mike@0 51
mike@0 52 (* Semantic analysis *)
mike@0 53 begin try Check.annotate prog with
mike@0 54 Check.Sem_error (fmt, args, ln) ->
mike@0 55 Source.err_message fmt args ln;
mike@0 56 exit 1
mike@0 57 end;
mike@0 58
mike@0 59 (* Translate the program *)
mike@0 60 Tgen.translate prog;
mike@0 61 exit 0
mike@0 62
mike@0 63 let ppc = main ()