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 5547 - (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 : jhr 5547 * COPYRIGHT (c) 2018 The University of Chicago
6 : jhr 3394 * 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 3731 (* messages in verbose mode *)
20 : jhr 3937 (* QUESTION: perhaps we should include timing info? *)
21 : jhr 3731 fun verbosePrint msg = if Controls.get Ctl.verbose
22 : jhr 4317 then TextIO.output(TextIO.stdErr, concat msg)
23 :     else ()
24 : jhr 3731
25 : jhr 3394 (* check for errors and report them if there are any *)
26 :     fun checkForErrors errStrm =
27 : jhr 4317 if Error.anyErrors errStrm
28 :     then raise Error.ERROR
29 :     else ()
30 : jhr 3394
31 : jhr 3437 (* conditional printing of intermediate forms *)
32 : jhr 3508 fun dump ctl output phase prog = if Controls.get ctl
33 : jhr 4317 then output (Log.logFile(), "After "^phase, prog)
34 :     else ()
35 : jhr 3437
36 : jhr 3394 (* compiler front end (parsing, typechecking, and simplification *)
37 : jhr 5074 fun frontEnd' (errStrm, defs, filename) = let
38 : jhr 4317 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.ERROR)
43 :     (***** PARSING *****)
44 :     val _ = verbosePrint["parsing ... "]
45 : jhr 5540 val (version, parseTree) = PhaseTimer.withTimer Timers.timeParser (fn () => let
46 : jhr 4317 val inS = TextIO.openIn filename
47 :     val pt = Parser.parseFile (errStrm, inS)
48 :     in
49 :     TextIO.closeIn inS;
50 :     checkForErrors errStrm;
51 :     valOf pt
52 :     end) ()
53 :     val _ = verbosePrint["done\n"]
54 :     val _ = dump Ctl.dumpPT (ParseTreePP.output errStrm) "parsing" parseTree
55 :     (***** TYPECHECKING *****)
56 :     val _ = verbosePrint["type checking ... "]
57 :     val _ = PhaseTimer.start Timers.timeTypechecker
58 : jhr 5540 val (ast, gEnv) = (Typechecker.check defs errStrm (version, parseTree))
59 : jhr 4317 val _ = PhaseTimer.stop Timers.timeTypechecker
60 :     val _ = verbosePrint["done\n"];
61 :     val _ = checkForErrors errStrm
62 : jhr 5081 (* FIXME: check AST for consistency *)
63 : jhr 4317 val _ = dump Ctl.dumpAST ASTPP.output "typechecking" ast
64 :     (***** SIMPLIFY *****)
65 :     val _ = verbosePrint["simplifying AST ... "]
66 : jhr 4371 val simple = SimpleOpt.checkAfter ("simplify", Simplify.transform (errStrm, ast, gEnv))
67 : jhr 5319 val _ = checkForErrors errStrm
68 : jhr 4317 val simple = SimpleOpt.transform simple
69 :     val _ = verbosePrint["done\n"]
70 :     in
71 :     simple
72 :     end
73 : jhr 3394
74 : jhr 3445 (* a wrapper around the front-end that handles the Error.ERROR exception and reports
75 :     * the error messages.
76 :     *)
77 : jhr 5074 fun frontEnd (defs, filename) = let
78 : jhr 4317 val errStrm = Error.mkErrStream filename
79 :     fun finish () = (
80 :     if Error.anyErrors errStrm orelse Error.anyWarnings errStrm
81 :     then Error.report (TextIO.stdErr, errStrm)
82 :     else ())
83 :     in
84 : jhr 5074 (frontEnd' (errStrm, defs, filename) handle exn => (finish (); raise exn))
85 : jhr 4317 before finish()
86 :     end
87 : jhr 3445
88 : jhr 5074 fun doFile (target : TargetOptions.t, defs, filename) = let
89 : jhr 4317 val {info, generate} = Targets.get target
90 :     val baseName = (case OS.Path.splitBaseExt filename
91 :     of {base, ext=SOME "diderot"} => base
92 :     | {base, ext=SOME "ddro"} => base
93 :     | _ => (errnl "expected diderot file"; raise Error.ERROR)
94 :     (* end case *))
95 : jhr 5074 val simple = PhaseTimer.withTimer Timers.timeFront frontEnd (defs, filename)
96 : jhr 4317 (***** TRANSLATION TO HIGH IR*****)
97 :     val _ = verbosePrint["translating to HighIR ... "]
98 :     val high = PhaseTimer.withTimer Timers.timeTranslate Translate.translate simple
99 :     val _ = verbosePrint["done\n"]
100 :     val _ = verbosePrint["checking HighIR ... "]
101 :     val high = HighOptimizer.checkAfter ("simple-to-high translation", high)
102 :     val _ = verbosePrint["done\n"]
103 :     (***** HIGH-IR OPTIMIZATION *****)
104 :     val _ = verbosePrint["optimizing HighIR ... "]
105 :     val high = PhaseTimer.withTimer Timers.timeHigh HighOptimizer.optimize high
106 :     val _ = verbosePrint["done\n"]
107 :     (***** TRANSLATION TO MID IR *****)
108 :     val _ = verbosePrint["translating to MidIR ... "]
109 :     val mid = PhaseTimer.withTimer Timers.timeHighToMid HighToMid.translate high
110 :     val _ = verbosePrint["done\n"]
111 :     val _ = verbosePrint["checking MidIR ... "]
112 :     val mid = MidOptimizer.checkAfter ("high-to-mid translation", mid)
113 :     val _ = verbosePrint["done\n"]
114 :     (***** MID-IR OPTIMIZATION *****)
115 :     val _ = verbosePrint["optimizing MidIR ... "]
116 :     val mid = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize mid
117 :     val _ = verbosePrint["done\n"];
118 :     (***** TRANSLATION TO LOW IR *****)
119 :     val _ = verbosePrint["translating to LowIR ... "]
120 :     val low = PhaseTimer.withTimer Timers.timeMidToLow MidToLow.translate mid
121 :     val _ = verbosePrint["done\n"]
122 :     val _ = verbosePrint["checking LowIR ... "]
123 :     val low = LowOptimizer.checkAfter ("mid-to-low translation", low)
124 :     val _ = verbosePrint["done\n"]
125 :     (***** LOW-IR OPTIMIZATION *****)
126 :     val _ = verbosePrint["optimizing LowIR ... "]
127 :     val low = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize low
128 :     val _ = verbosePrint["done\n"]
129 :     (***** TRANSLATION TO TREE IR *****)
130 :     val _ = verbosePrint["translating to TreeIR ... "]
131 :     val tree = PhaseTimer.withTimer Timers.timeLowToTree LowToTree.translate (low, info)
132 :     val _ = verbosePrint["done\n"]
133 :     val _ = verbosePrint["checking TreeIR ... "]
134 :     val tree = TreeOptimizer.checkAfter ("low-to-tree translation", tree)
135 :     val _ = verbosePrint["done\n"]
136 :     (***** TREE-IR OPTIMIZATION *****)
137 :     val _ = verbosePrint["optimizing TreeIR ... "]
138 :     val tree = PhaseTimer.withTimer Timers.timeTree TreeOptimizer.optimize tree
139 :     val _ = verbosePrint["done\n"]
140 :     in
141 :     (***** CODE GENERATION *****)
142 :     verbosePrint["generating code ... "];
143 : jhr 5097 PhaseTimer.withTimer Timers.timeCodegen generate (defs, tree);
144 : jhr 4317 verbosePrint["done\n"]
145 :     end
146 : jhr 3394
147 : jhr 4116 fun usage (cmd, long, about) = (
148 : jhr 4317 TextIO.output(TextIO.stdErr, Options.usage (cmd, long));
149 :     if long orelse about
150 :     then (
151 :     TextIO.output(TextIO.stdErr, "==========\n");
152 :     TextIO.output(TextIO.stdErr, About.message);
153 :     TextIO.output(TextIO.stdErr, "==========\n"))
154 :     else ())
155 : jhr 3394
156 : jhr 3445 fun handleExn Error.ERROR = OS.Process.failure
157 : jhr 3394 | handleExn exn = (
158 : jhr 4317 err (concat [
159 :     "uncaught exception ", General.exnName exn,
160 :     " [", General.exnMessage exn, "]\n"
161 :     ]);
162 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
163 :     OS.Process.failure)
164 : jhr 3394
165 :     fun main (name: string, args: string list) = let
166 : jhr 4317 val _ = PhaseTimer.start Timers.timeCompiler
167 : jhr 5547 val {help, version, about, dumpBasis, target, defs, file} =
168 : jhr 4317 (Options.parseCmdLine args)
169 :     handle Options.Usage msg => (
170 :     err(concat["Error: ", msg, "\n"]);
171 :     usage (name, false, false);
172 :     OS.Process.exit OS.Process.failure)
173 :     in
174 :     Ctl.resolve();
175 : jhr 5547 case (help, version, about, dumpBasis)
176 :     of (SOME long, _, _, _) => (usage (name, long, about); OS.Process.success)
177 :     | (NONE, _, _, true) => (
178 :     (* FIXME: how should we specify the basis version on the command line? *)
179 :     DumpBasis.dump("basis.tex", [2]);
180 :     OS.Process.success)
181 :     | (NONE, true, false, _) => (print(Version.message ^ "\n"); OS.Process.success)
182 :     | (NONE, false, true, _) => (print(About.message ^ "\n"); OS.Process.success)
183 :     | (NONE, true, true, _) => (
184 : jhr 4317 print(concat["Version: ", Version.message, "\n"]);
185 :     print "==========\n";
186 :     print About.message;
187 :     print "==========\n";
188 :     OS.Process.success)
189 : jhr 5074 | _ => (let
190 : jhr 4317 val {base, ...} = OS.Path.splitBaseExt file
191 :     val logging = Controls.get Ctl.enableLog
192 :     in
193 : jhr 5215 if logging
194 :     then Log.init(base ^ ".log")
195 :     else ();
196 :     doFile (target, defs, file);
197 :     PhaseTimer.stop Timers.timeCompiler;
198 :     if Controls.get Ctl.collectStats
199 :     then (
200 :     if (not logging)
201 :     then Log.init(base ^ ".stats")
202 :     else ();
203 :     Stats.report ())
204 :     else ();
205 :     Log.reportTiming Timers.timeCompiler;
206 :     OS.Process.success
207 : jhr 5074 end handle exn => handleExn exn)
208 : jhr 4317 (* end case *)
209 :     end
210 : jhr 3394
211 :     end

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