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 1017 - (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 : jhr 35 val test : string -> unit
12 :    
13 : jhr 33 end = struct
14 :    
15 : jhr 541 (* exception tracing magic *)
16 :     val _ = (
17 :     SMLofNJ.Internals.TDP.mode := true;
18 :     Coverage.install ();
19 :     BackTrace.install ())
20 :    
21 : jhr 256 structure HighPP = SSAPPFn (HighIL)
22 : jhr 367 structure MidPP = SSAPPFn (MidIL)
23 : jhr 460 structure LowPP = SSAPPFn (LowIL)
24 : jhr 256
25 : jhr 33 fun err s = TextIO.output (TextIO.stdErr, s)
26 :     fun err1 c = TextIO.output1 (TextIO.stdErr, c)
27 :     fun errnl s = (err s; err1 #"\n")
28 :    
29 : jhr 429 exception ERROR
30 : jhr 33
31 : jhr 429 fun quitWithError srcFile = raise Fail("Error in compiling " ^ srcFile)
32 :    
33 : jhr 33 (* check for errors and report them if there are any *)
34 :     fun checkForErrors errStrm = (
35 :     Error.report (TextIO.stdErr, errStrm);
36 :     if Error.anyErrors errStrm
37 : jhr 429 then quitWithError (Error.sourceFile errStrm)
38 : jhr 33 else ())
39 :    
40 : jhr 1007
41 :     (* compiler front end (parsing, typechecking, and simplification *)
42 :     fun frontEnd filename = let
43 : jhr 33 val errStrm = Error.mkErrStream filename
44 : jhr 1007 val _ = if OS.FileSys.access(filename, [OS.FileSys.A_READ])
45 :     then ()
46 :     else (
47 :     err(concat["source file \"", filename, "\" does not exist or is not readable\n"]);
48 :     raise ERROR)
49 : jhr 460 (***** PARSING *****)
50 : jhr 1007 val parseTree = PhaseTimer.withTimer Timers.timeParser (fn () => let
51 :     val inS = TextIO.openIn filename
52 : jhr 460 val pt = Parser.parseFile (errStrm, inS)
53 :     in
54 :     TextIO.closeIn inS;
55 :     checkForErrors errStrm;
56 :     valOf pt
57 : jhr 1007 end) ()
58 : jhr 460 (***** TYPECHECKING *****)
59 : jhr 1007 val _ = PhaseTimer.start Timers.timeTypechecker
60 :     val ast = (Typechecker.check errStrm parseTree) handle Typechecker.Error => AST.Program[]
61 :     val _ = PhaseTimer.stop Timers.timeTypechecker
62 : jhr 460 val _ = checkForErrors errStrm
63 :     val _ = ASTPP.output (Log.logFile(), ast) (* DEBUG *)
64 :     (***** SIMPLIFY *****)
65 : jhr 1007 val _ = PhaseTimer.start Timers.timeSimplify
66 : jhr 460 val simple = Simplify.transform ast
67 : jhr 1007 val _ = PhaseTimer.stop Timers.timeSimplify
68 : jhr 460 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
69 : jhr 1007 in
70 :     simple
71 :     end
72 :    
73 :     fun doFile filename = let
74 :     val baseName = (case OS.Path.splitBaseExt filename
75 :     of {base, ext=SOME "diderot"} => base
76 :     | _ => (errnl "expected diderot file"; quitWithError filename)
77 :     (* end case *))
78 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
79 : jhr 460 (***** TRANSLATION TO HIGH IL*****)
80 : jhr 1007 val _ = PhaseTimer.start Timers.timeTranslate
81 : jhr 460 val highIL = Translate.translate simple
82 : jhr 1007 val _ = PhaseTimer.stop Timers.timeTranslate
83 : jhr 460 val _ = ( (* DEBUG *)
84 :     HighPP.output (Log.logFile(), "HighIL after translation", highIL);
85 : jhr 517 if CheckHighIL.check ("after translation to HighIL", highIL)
86 : jhr 460 then quitWithError filename
87 :     else ())
88 :     (***** HIGH-IL OPTIMIZATION *****)
89 : jhr 1007 val _ = PhaseTimer.start Timers.timeHigh
90 : jhr 460 val highIL = HighOptimizer.optimize highIL
91 : jhr 1007 val _ = PhaseTimer.stop Timers.timeHigh
92 : jhr 460 val _ = ( (* DEBUG *)
93 :     HighPP.output (Log.logFile(), "HighIL after optimization", highIL);
94 : jhr 517 if CheckHighIL.check ("after HighIL optimization", highIL)
95 : jhr 460 then quitWithError filename
96 :     else ())
97 :     (***** TRANSLATION TO MID IL *****)
98 : jhr 1007 val _ = PhaseTimer.start Timers.timeMid
99 : jhr 460 val midIL = HighToMid.translate highIL
100 : jhr 1007 val _ = PhaseTimer.stop Timers.timeMid
101 : jhr 460 val _ = ( (* DEBUG *)
102 :     MidPP.output (Log.logFile(), "MidIL after translation", midIL);
103 : jhr 517 if CheckMidIL.check ("after translation to MidIL", midIL)
104 : jhr 460 then quitWithError filename
105 :     else ())
106 :     (***** TRANSLATION TO LOW IL *****)
107 : jhr 1007 val _ = PhaseTimer.start Timers.timeLow
108 : jhr 460 val lowIL = MidToLow.translate midIL
109 : jhr 1007 val _ = PhaseTimer.stop Timers.timeLow
110 : jhr 460 val _ = ( (* DEBUG *)
111 :     LowPP.output (Log.logFile(), "LowIL after translation", lowIL);
112 : jhr 517 if CheckLowIL.check ("after translation to LowIL", lowIL)
113 : jhr 460 then quitWithError filename
114 :     else ())
115 : jhr 1017 val _ = PhaseTimer.start Timers.timeLow
116 :     val lowIL = LowOptimizer.optimize lowIL
117 :     val _ = PhaseTimer.stop Timers.timeLow
118 :     val _ = ( (* DEBUG *)
119 :     LowPP.output (Log.logFile(), "LowIL after optimization", lowIL);
120 :     if CheckLowIL.check ("after LowIL optimization", lowIL)
121 :     then quitWithError filename
122 :     else ())
123 : jhr 33 in
124 : jhr 526 (***** CODE GENERATION *****)
125 : jhr 1007 PhaseTimer.withTimer Timers.timeCodegen CBackEnd.generate (baseName, lowIL)
126 : jhr 33 end
127 :    
128 : jhr 238 fun doOptions args = let
129 :     val log = ref false
130 :     val defs = ref []
131 :     fun doOpts [file] = {log = !log, defs = !defs, file = file}
132 :     | doOpts ("-log"::r) = (log := true; doOpts r)
133 : jhr 340 | doOpts (opt::r) =
134 :     if Inputs.isCmdLineInput opt
135 :     then (defs := opt :: !defs; doOpts r)
136 :     else (
137 :     err(concat["invalid command-line input \"", opt, "\"\n"]);
138 : jhr 429 raise ERROR)
139 : jhr 238 in
140 :     doOpts args
141 :     end
142 : jhr 33
143 : jhr 238 fun main (name: string, args: string list) = let
144 :     val {log, defs, file} = doOptions args
145 :     val {base, ...} = OS.Path.splitBaseExt file
146 :     in
147 :     if Inputs.initFromArgs defs
148 : jhr 1007 then (
149 :     Log.init(base ^ ".log");
150 :     PhaseTimer.withTimer Timers.timeCompiler doFile file;
151 :     Log.reportTiming Timers.timeCompiler;
152 :     OS.Process.success)
153 : jhr 238 handle exn => (
154 :     err (concat [
155 :     "uncaught exception ", General.exnName exn, " [", General.exnMessage exn, "]\n"]);
156 :     List.app (fn s => err (concat [" raised at ", s, "\n"]))
157 :     (SMLofNJ.exnHistory exn);
158 : jhr 429 OS.Process.failure)
159 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
160 : jhr 238 end
161 : jhr 429 handle ERROR => OS.Process.failure
162 : jhr 238
163 : jhr 35 fun test file = (main ("", [file]); print "** Success!!\n")
164 :    
165 : jhr 33 end

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