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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/driver/main.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/driver/main.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3937 - (view) (download)

1 : jhr 3394 (* main.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Main : sig
10 :    
11 :     val main : (string * string list) -> OS.Process.status
12 :    
13 :     end = struct
14 :    
15 :     fun err s = TextIO.output (TextIO.stdErr, s)
16 :     fun err1 c = TextIO.output1 (TextIO.stdErr, c)
17 :     fun errnl s = (err s; err1 #"\n")
18 :    
19 : jhr 3445 fun quitWithError srcFile = raise Error.ERROR
20 : jhr 3394
21 : jhr 3731 (* messages in verbose mode *)
22 : jhr 3937 (* QUESTION: perhaps we should include timing info? *)
23 : jhr 3731 fun verbosePrint msg = if Controls.get Ctl.verbose
24 :     then TextIO.output(TextIO.stdErr, concat msg)
25 :     else ()
26 :    
27 : jhr 3394 (* check for errors and report them if there are any *)
28 :     fun checkForErrors errStrm =
29 :     if Error.anyErrors errStrm
30 :     then (
31 :     Error.report (TextIO.stdErr, errStrm);
32 :     quitWithError (Error.sourceFile errStrm))
33 :     else ()
34 :    
35 : jhr 3437 (* conditional printing of intermediate forms *)
36 : jhr 3508 fun dump ctl output phase prog = if Controls.get ctl
37 :     then output (Log.logFile(), "After "^phase, prog)
38 : jhr 3437 else ()
39 :    
40 : jhr 3394 (* compiler front end (parsing, typechecking, and simplification *)
41 : jhr 3445 fun frontEnd' (errStrm, filename) = let
42 : jhr 3394 val _ = if OS.FileSys.access(filename, [OS.FileSys.A_READ])
43 :     then ()
44 :     else (
45 :     err(concat["source file \"", filename, "\" does not exist or is not readable\n"]);
46 : jhr 3445 raise Error.ERROR)
47 : jhr 3394 (***** PARSING *****)
48 : jhr 3731 val _ = verbosePrint["parsing ... "];
49 : jhr 3394 val parseTree = PhaseTimer.withTimer Timers.timeParser (fn () => let
50 :     val inS = TextIO.openIn filename
51 :     val pt = Parser.parseFile (errStrm, inS)
52 :     in
53 :     TextIO.closeIn inS;
54 :     checkForErrors errStrm;
55 :     valOf pt
56 :     end) ()
57 : jhr 3731 val _ = verbosePrint["done\n"];
58 : jhr 3508 val _ = dump Ctl.dumpPT (ParseTreePP.output errStrm) "parsing" parseTree
59 : jhr 3394 (***** TYPECHECKING *****)
60 : jhr 3731 val _ = verbosePrint["type checking ... "];
61 : jhr 3394 val _ = PhaseTimer.start Timers.timeTypechecker
62 :     val ast = (Typechecker.check errStrm parseTree)
63 :     val _ = PhaseTimer.stop Timers.timeTypechecker
64 : jhr 3731 val _ = verbosePrint["done\n"];
65 : jhr 3394 val _ = checkForErrors errStrm
66 : jhr 3417 (* TODO: check AST for consistency *)
67 : jhr 3508 val _ = dump Ctl.dumpAST ASTPP.output "typechecking" ast
68 : jhr 3394 (***** SIMPLIFY *****)
69 : jhr 3731 val _ = verbosePrint["simplifying AST ... "];
70 : jhr 3508 val simple = SimpleOpt.checkAfter ("simplify", Simplify.transform (errStrm, ast))
71 : jhr 3437 val simple = SimpleOpt.transform simple
72 : jhr 3731 val _ = verbosePrint["done\n"];
73 : jhr 3394 in
74 : jhr 3506 simple
75 : jhr 3394 end
76 :    
77 : jhr 3445 (* a wrapper around the front-end that handles the Error.ERROR exception and reports
78 :     * the error messages.
79 :     *)
80 :     fun frontEnd filename = let
81 :     val errStrm = Error.mkErrStream filename
82 :     fun finish () = (
83 : jhr 3585 if Error.anyWarnings errStrm
84 : jhr 3445 then Error.report (TextIO.stdErr, errStrm)
85 :     else ())
86 :     in
87 :     (frontEnd' (errStrm, filename) handle exn => (finish (); raise exn))
88 :     before finish()
89 :     end
90 :    
91 : jhr 3813 fun doFile (target : TargetOptions.t, filename) = let
92 : jhr 3863 val {info, generate} = Targets.get target
93 : jhr 3394 val baseName = (case OS.Path.splitBaseExt filename
94 :     of {base, ext=SOME "diderot"} => base
95 :     | _ => (errnl "expected diderot file"; quitWithError filename)
96 :     (* end case *))
97 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
98 : jhr 3521 (***** TRANSLATION TO HIGH IR*****)
99 : jhr 3731 val _ = verbosePrint["translating to HighIR ... "];
100 : jhr 3814 val high = PhaseTimer.withTimer Timers.timeTranslate Translate.translate simple
101 : jhr 3731 val _ = verbosePrint["done\n"];
102 : jhr 3860 val high = HighOptimizer.checkAfter ("simple-to-high translation", high)
103 : jhr 3521 (***** HIGH-IR OPTIMIZATION *****)
104 : jhr 3731 val _ = verbosePrint["optimizing HighIR ... "];
105 : jhr 3814 val high = PhaseTimer.withTimer Timers.timeHigh HighOptimizer.optimize high
106 : jhr 3731 val _ = verbosePrint["done\n"];
107 : jhr 3521 (***** TRANSLATION TO MID IR *****)
108 : jhr 3731 val _ = verbosePrint["translating to MidIR ... "];
109 : jhr 3814 val mid = PhaseTimer.withTimer Timers.timeHighToMid HighToMid.translate high
110 : jhr 3731 val _ = verbosePrint["done\n"];
111 : jhr 3861 val mid = MidOptimizer.checkAfter ("high-to-mid translation", mid)
112 : jhr 3586 (***** MID-IR OPTIMIZATION *****)
113 : jhr 3731 val _ = verbosePrint["optimizing MidIR ... "];
114 : jhr 3814 val mid = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize mid
115 : jhr 3731 val _ = verbosePrint["done\n"];
116 : jhr 3521 (***** TRANSLATION TO LOW IR *****)
117 : jhr 3731 val _ = verbosePrint["translating to LowIR ... "];
118 : jhr 3814 val low = PhaseTimer.withTimer Timers.timeMidToLow MidToLow.translate mid
119 : jhr 3731 val _ = verbosePrint["done\n"];
120 : jhr 3861 val low = LowOptimizer.checkAfter ("mid-to-low translation", low)
121 : jhr 3747 (***** LOW-IR OPTIMIZATION *****)
122 :     val _ = verbosePrint["optimizing LowIR ... "];
123 : jhr 3814 val low = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize low
124 : jhr 3747 val _ = verbosePrint["done\n"];
125 : jhr 3753 (***** TRANSLATION TO TREE IR *****)
126 :     val _ = verbosePrint["translating to TreeIR ... "];
127 : jhr 3863 val tree = PhaseTimer.withTimer Timers.timeLowToTree LowToTree.translate (low, info)
128 : jhr 3753 val _ = verbosePrint["done\n"];
129 : jhr 3861 val tree = TreeOptimizer.checkAfter ("low-to-tree translation", tree)
130 :     (***** TREE-IR OPTIMIZATION *****)
131 :     val _ = verbosePrint["optimizing TreeIR ... "];
132 :     val tree = PhaseTimer.withTimer Timers.timeTree TreeOptimizer.optimize tree
133 :     val _ = verbosePrint["done\n"];
134 : jhr 3394 in
135 :     (***** CODE GENERATION *****)
136 : jhr 3863 PhaseTimer.withTimer Timers.timeCodegen generate tree
137 : jhr 3394 end
138 :    
139 : jhr 3411 fun usage (cmd, long) = TextIO.output(TextIO.stdErr, Options.usage (cmd, long))
140 : jhr 3394
141 : jhr 3445 fun handleExn Error.ERROR = OS.Process.failure
142 : jhr 3394 | handleExn exn = (
143 :     err (concat [
144 :     "uncaught exception ", General.exnName exn,
145 :     " [", General.exnMessage exn, "]\n"
146 :     ]);
147 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
148 :     OS.Process.failure)
149 :    
150 :     fun main (name: string, args: string list) = let
151 : jhr 3814 val _ = PhaseTimer.start Timers.timeCompiler
152 : jhr 3411 val {help, version, target, defs, file} =
153 : jhr 3394 (Options.parseCmdLine args)
154 :     handle Options.Usage msg => (
155 :     err(concat[msg, "\n"]);
156 : jhr 3411 usage (name, false);
157 : jhr 3394 OS.Process.exit OS.Process.failure)
158 :     in
159 : jhr 3417 Ctl.resolve();
160 : jhr 3411 case help
161 :     of SOME long => (usage (name, long); OS.Process.success)
162 :     | NONE => if version
163 :     then (print(Version.message ^ "\n"); OS.Process.success)
164 :     else let
165 :     val {base, ...} = OS.Path.splitBaseExt file
166 : jhr 3731 val logging = Controls.get Ctl.enableLog
167 : jhr 3411 in
168 :     (* set the default real type *)
169 :     if #double target then (RawTypes.realTy := RawTypes.RT_Double) else ();
170 :     if CmdLineConstants.initFromArgs defs
171 :     then (
172 : jhr 3731 if logging
173 : jhr 3411 then Log.init(base ^ ".log")
174 :     else ();
175 : jhr 3814 doFile (target, file);
176 : jhr 3882 PhaseTimer.stop Timers.timeCompiler;
177 : jhr 3731 if Controls.get Ctl.collectStats
178 :     then (
179 :     if (not logging)
180 :     then Log.init(base ^ ".stats")
181 :     else ();
182 :     Stats.report ())
183 :     else ();
184 : jhr 3411 Log.reportTiming Timers.timeCompiler;
185 :     OS.Process.success
186 :     ) handle exn => handleExn exn
187 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
188 :     end
189 :     (* end case *)
190 : jhr 3394 end
191 :    
192 :     end

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