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 3741 - (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 3411 fun doFile (target : Target.desc, 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 3481 val high = 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 3522 val high = 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 3582 val mid = 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 3586 val mid = MidOptimizer.optimize mid
114 : jhr 3731 val _ = verbosePrint["done\n"];
115 : jhr 3521 (***** TRANSLATION TO LOW IR *****)
116 : cchiw 3741
117 : jhr 3731 val _ = verbosePrint["translating to LowIR ... "];
118 : jhr 3709 val low = MidToLow.translate mid
119 : jhr 3731 val _ = verbosePrint["done\n"];
120 : jhr 3709 val _ = dump Ctl.dumpLowIR LowPP.output "mid-to-low" low
121 : cchiw 3741
122 : jhr 3394 (* TODO *)
123 :     in
124 :     (***** CODE GENERATION *****)
125 :     (* TODO *)()
126 :     end
127 :    
128 : jhr 3411 fun usage (cmd, long) = TextIO.output(TextIO.stdErr, Options.usage (cmd, long))
129 : jhr 3394
130 : jhr 3445 fun handleExn Error.ERROR = OS.Process.failure
131 : jhr 3394 | handleExn exn = (
132 :     err (concat [
133 :     "uncaught exception ", General.exnName exn,
134 :     " [", General.exnMessage exn, "]\n"
135 :     ]);
136 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
137 :     OS.Process.failure)
138 :    
139 :     fun main (name: string, args: string list) = let
140 : jhr 3411 val {help, version, target, defs, file} =
141 : jhr 3394 (Options.parseCmdLine args)
142 :     handle Options.Usage msg => (
143 :     err(concat[msg, "\n"]);
144 : jhr 3411 usage (name, false);
145 : jhr 3394 OS.Process.exit OS.Process.failure)
146 :     in
147 : jhr 3417 Ctl.resolve();
148 : jhr 3411 case help
149 :     of SOME long => (usage (name, long); OS.Process.success)
150 :     | NONE => if version
151 :     then (print(Version.message ^ "\n"); OS.Process.success)
152 :     else let
153 :     val {base, ...} = OS.Path.splitBaseExt file
154 : jhr 3731 val logging = Controls.get Ctl.enableLog
155 : jhr 3411 in
156 :     (* set the default real type *)
157 :     if #double target then (RawTypes.realTy := RawTypes.RT_Double) else ();
158 :     if CmdLineConstants.initFromArgs defs
159 :     then (
160 : jhr 3731 if logging
161 : jhr 3411 then Log.init(base ^ ".log")
162 :     else ();
163 :     PhaseTimer.withTimer Timers.timeCompiler
164 :     doFile (target, file);
165 : jhr 3731 if Controls.get Ctl.collectStats
166 :     then (
167 :     if (not logging)
168 :     then Log.init(base ^ ".stats")
169 :     else ();
170 :     Stats.report ())
171 :     else ();
172 : jhr 3411 Log.reportTiming Timers.timeCompiler;
173 :     OS.Process.success
174 :     ) handle exn => handleExn exn
175 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
176 :     end
177 :     (* end case *)
178 : jhr 3394 end
179 :    
180 :     end

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