Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/driver/main.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/driver/main.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1202 - (view) (download)

1 : jhr 33 (* main.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 33 * All rights reserved.
5 :     *)
6 :    
7 :     structure Main : sig
8 :    
9 :     val main : (string * string list) -> OS.Process.status
10 :    
11 :     end = struct
12 :    
13 : jhr 541 (* exception tracing magic *)
14 :     val _ = (
15 :     SMLofNJ.Internals.TDP.mode := true;
16 :     Coverage.install ();
17 :     BackTrace.install ())
18 :    
19 : jhr 33 fun err s = TextIO.output (TextIO.stdErr, s)
20 :     fun err1 c = TextIO.output1 (TextIO.stdErr, c)
21 :     fun errnl s = (err s; err1 #"\n")
22 :    
23 : jhr 429 exception ERROR
24 : jhr 33
25 : jhr 429 fun quitWithError srcFile = raise Fail("Error in compiling " ^ srcFile)
26 :    
27 : jhr 33 (* check for errors and report them if there are any *)
28 :     fun checkForErrors errStrm = (
29 :     Error.report (TextIO.stdErr, errStrm);
30 :     if Error.anyErrors errStrm
31 : jhr 429 then quitWithError (Error.sourceFile errStrm)
32 : jhr 33 else ())
33 :    
34 : jhr 1007
35 :     (* compiler front end (parsing, typechecking, and simplification *)
36 :     fun frontEnd filename = let
37 : jhr 33 val errStrm = Error.mkErrStream filename
38 : jhr 1007 val _ = if OS.FileSys.access(filename, [OS.FileSys.A_READ])
39 :     then ()
40 :     else (
41 :     err(concat["source file \"", filename, "\" does not exist or is not readable\n"]);
42 :     raise ERROR)
43 : jhr 460 (***** PARSING *****)
44 : jhr 1007 val parseTree = PhaseTimer.withTimer Timers.timeParser (fn () => let
45 :     val inS = TextIO.openIn filename
46 : jhr 460 val pt = Parser.parseFile (errStrm, inS)
47 :     in
48 :     TextIO.closeIn inS;
49 :     checkForErrors errStrm;
50 :     valOf pt
51 : jhr 1007 end) ()
52 : jhr 460 (***** TYPECHECKING *****)
53 : jhr 1007 val _ = PhaseTimer.start Timers.timeTypechecker
54 :     val ast = (Typechecker.check errStrm parseTree) handle Typechecker.Error => AST.Program[]
55 :     val _ = PhaseTimer.stop Timers.timeTypechecker
56 : jhr 460 val _ = checkForErrors errStrm
57 :     val _ = ASTPP.output (Log.logFile(), ast) (* DEBUG *)
58 :     (***** SIMPLIFY *****)
59 : jhr 1007 val _ = PhaseTimer.start Timers.timeSimplify
60 : jhr 1139 val simple = Simplify.transform (errStrm, ast)
61 :     val _ = checkForErrors errStrm
62 : jhr 1007 val _ = PhaseTimer.stop Timers.timeSimplify
63 : jhr 460 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
64 : jhr 1007 in
65 :     simple
66 :     end
67 :    
68 : jhr 1202 fun doFile (target : TargetUtil.target_desc, filename) = BackTrace.monitor (fn () => let
69 : jhr 1007 val baseName = (case OS.Path.splitBaseExt filename
70 :     of {base, ext=SOME "diderot"} => base
71 :     | _ => (errnl "expected diderot file"; quitWithError filename)
72 :     (* end case *))
73 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
74 : jhr 460 (***** TRANSLATION TO HIGH IL*****)
75 : jhr 1007 val _ = PhaseTimer.start Timers.timeTranslate
76 : jhr 460 val highIL = Translate.translate simple
77 : jhr 1007 val _ = PhaseTimer.stop Timers.timeTranslate
78 : jhr 460 (***** HIGH-IL OPTIMIZATION *****)
79 : jhr 1007 val _ = PhaseTimer.start Timers.timeHigh
80 : jhr 460 val highIL = HighOptimizer.optimize highIL
81 : jhr 1007 val _ = PhaseTimer.stop Timers.timeHigh
82 : jhr 460 (***** TRANSLATION TO MID IL *****)
83 : jhr 1036 val midIL = PhaseTimer.withTimer Timers.timeMid HighToMid.translate highIL
84 :     val midIL = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize midIL
85 : jhr 460 (***** TRANSLATION TO LOW IL *****)
86 : jhr 1036 val lowIL = PhaseTimer.withTimer Timers.timeLow MidToLow.translate midIL
87 :     val lowIL = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize lowIL
88 : jhr 33 in
89 : jhr 526 (***** CODE GENERATION *****)
90 : jhr 1172 PhaseTimer.withTimer Timers.timeCodegen
91 :     (BackEnd.generate target) (baseName, lowIL)
92 : jhr 1043 end)
93 : jhr 33
94 : jhr 1202 fun usage cmd = TextIO.output(TextIO.stdErr, Options.usage cmd)
95 : jhr 33
96 : jhr 238 fun main (name: string, args: string list) = let
97 : jhr 1202 val {help, log, target, defs, file} =
98 :     (Options.parseCmdLine args)
99 :     handle Options.Usage msg => (
100 :     err(concat[msg, "\n"]);
101 :     usage name;
102 :     OS.Process.exit OS.Process.failure)
103 : jhr 238 in
104 : jhr 1202 if help
105 : jhr 1007 then (
106 : jhr 1202 usage name;
107 : jhr 1007 OS.Process.success)
108 : jhr 1202 else let
109 :     val {base, ...} = OS.Path.splitBaseExt file
110 :     in
111 :     if Inputs.initFromArgs defs
112 :     then (
113 :     if log then Log.init(base ^ ".log") else ();
114 :     PhaseTimer.withTimer Timers.timeCompiler doFile (target, file);
115 :     Stats.report ();
116 :     Log.reportTiming Timers.timeCompiler;
117 :     OS.Process.success
118 :     ) handle exn => (
119 :     err (concat [
120 :     "uncaught exception ", General.exnName exn, " [", General.exnMessage exn, "]\n"]);
121 :     List.app (fn s => err (concat [" raised at ", s, "\n"]))
122 :     (SMLofNJ.exnHistory exn);
123 :     OS.Process.failure)
124 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
125 :     end
126 : jhr 238 end
127 : jhr 429 handle ERROR => OS.Process.failure
128 : jhr 238
129 : jhr 33 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0