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 3835 - (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 :     (* FIXME: perhaps we should include timing info? *)
23 :     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 3394 val baseName = (case OS.Path.splitBaseExt filename
93 :     of {base, ext=SOME "diderot"} => base
94 :     | _ => (errnl "expected diderot file"; quitWithError filename)
95 :     (* end case *))
96 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
97 : jhr 3521 (***** TRANSLATION TO HIGH IR*****)
98 : jhr 3731 val _ = verbosePrint["translating to HighIR ... "];
99 : jhr 3814 val high = PhaseTimer.withTimer Timers.timeTranslate Translate.translate simple
100 : jhr 3731 val _ = verbosePrint["done\n"];
101 : jhr 3521 val _ = dump Ctl.dumpHighIR HighPP.output "translate" high
102 :     (***** HIGH-IR OPTIMIZATION *****)
103 : jhr 3731 val _ = verbosePrint["optimizing HighIR ... "];
104 : jhr 3814 val high = PhaseTimer.withTimer Timers.timeHigh HighOptimizer.optimize high
105 : jhr 3731 val _ = verbosePrint["done\n"];
106 : jhr 3521 (***** TRANSLATION TO MID IR *****)
107 : jhr 3731 val _ = verbosePrint["translating to MidIR ... "];
108 : jhr 3814 val mid = PhaseTimer.withTimer Timers.timeHighToMid HighToMid.translate high
109 : jhr 3731 val _ = verbosePrint["done\n"];
110 : jhr 3582 val _ = dump Ctl.dumpMidIR MidPP.output "high-to-mid" mid
111 : jhr 3586 (***** MID-IR OPTIMIZATION *****)
112 : jhr 3731 val _ = verbosePrint["optimizing MidIR ... "];
113 : jhr 3814 val mid = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize mid
114 : jhr 3731 val _ = verbosePrint["done\n"];
115 : jhr 3521 (***** TRANSLATION TO LOW IR *****)
116 : jhr 3731 val _ = verbosePrint["translating to LowIR ... "];
117 : jhr 3814 val low = PhaseTimer.withTimer Timers.timeMidToLow MidToLow.translate mid
118 : jhr 3731 val _ = verbosePrint["done\n"];
119 : jhr 3709 val _ = dump Ctl.dumpLowIR LowPP.output "mid-to-low" low
120 : jhr 3747 (***** LOW-IR OPTIMIZATION *****)
121 :     val _ = verbosePrint["optimizing LowIR ... "];
122 : jhr 3814 val low = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize low
123 : jhr 3747 val _ = verbosePrint["done\n"];
124 : jhr 3753 (***** TRANSLATION TO TREE IR *****)
125 :     val _ = verbosePrint["translating to TreeIR ... "];
126 : jhr 3814 val tree = PhaseTimer.withTimer Timers.timeLowToTree LowToTree.translate low
127 : jhr 3753 val _ = verbosePrint["done\n"];
128 : jhr 3835 val _ = dump Ctl.dumpTreeIR TreePP.output "low-to-tree" tree
129 : jhr 3394 in
130 :     (***** CODE GENERATION *****)
131 :     (* TODO *)()
132 :     end
133 :    
134 : jhr 3411 fun usage (cmd, long) = TextIO.output(TextIO.stdErr, Options.usage (cmd, long))
135 : jhr 3394
136 : jhr 3445 fun handleExn Error.ERROR = OS.Process.failure
137 : jhr 3394 | handleExn exn = (
138 :     err (concat [
139 :     "uncaught exception ", General.exnName exn,
140 :     " [", General.exnMessage exn, "]\n"
141 :     ]);
142 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
143 :     OS.Process.failure)
144 :    
145 :     fun main (name: string, args: string list) = let
146 : jhr 3814 val _ = PhaseTimer.start Timers.timeCompiler
147 : jhr 3411 val {help, version, target, defs, file} =
148 : jhr 3394 (Options.parseCmdLine args)
149 :     handle Options.Usage msg => (
150 :     err(concat[msg, "\n"]);
151 : jhr 3411 usage (name, false);
152 : jhr 3394 OS.Process.exit OS.Process.failure)
153 :     in
154 : jhr 3417 Ctl.resolve();
155 : jhr 3411 case help
156 :     of SOME long => (usage (name, long); OS.Process.success)
157 :     | NONE => if version
158 :     then (print(Version.message ^ "\n"); OS.Process.success)
159 :     else let
160 :     val {base, ...} = OS.Path.splitBaseExt file
161 : jhr 3731 val logging = Controls.get Ctl.enableLog
162 : jhr 3411 in
163 :     (* set the default real type *)
164 :     if #double target then (RawTypes.realTy := RawTypes.RT_Double) else ();
165 :     if CmdLineConstants.initFromArgs defs
166 :     then (
167 : jhr 3731 if logging
168 : jhr 3411 then Log.init(base ^ ".log")
169 :     else ();
170 : jhr 3814 doFile (target, file);
171 : jhr 3731 if Controls.get Ctl.collectStats
172 :     then (
173 :     if (not logging)
174 :     then Log.init(base ^ ".stats")
175 :     else ();
176 :     Stats.report ())
177 :     else ();
178 : jhr 3814 PhaseTimer.stop Timers.timeCompiler;
179 : jhr 3411 Log.reportTiming Timers.timeCompiler;
180 :     OS.Process.success
181 :     ) handle exn => handleExn exn
182 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
183 :     end
184 :     (* end case *)
185 : jhr 3394 end
186 :    
187 :     end

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