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 3711 - (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 :     (* check for errors and report them if there are any *)
22 :     fun checkForErrors errStrm =
23 :     if Error.anyErrors errStrm
24 :     then (
25 :     Error.report (TextIO.stdErr, errStrm);
26 :     quitWithError (Error.sourceFile errStrm))
27 :     else ()
28 :    
29 : jhr 3437 (* conditional printing of intermediate forms *)
30 : jhr 3508 fun dump ctl output phase prog = if Controls.get ctl
31 :     then output (Log.logFile(), "After "^phase, prog)
32 : jhr 3437 else ()
33 :    
34 : jhr 3394 (* compiler front end (parsing, typechecking, and simplification *)
35 : jhr 3445 fun frontEnd' (errStrm, filename) = let
36 : jhr 3394 val _ = if OS.FileSys.access(filename, [OS.FileSys.A_READ])
37 :     then ()
38 :     else (
39 :     err(concat["source file \"", filename, "\" does not exist or is not readable\n"]);
40 : jhr 3445 raise Error.ERROR)
41 : jhr 3394 (***** PARSING *****)
42 :     val parseTree = PhaseTimer.withTimer Timers.timeParser (fn () => let
43 :     val inS = TextIO.openIn filename
44 :     val pt = Parser.parseFile (errStrm, inS)
45 :     in
46 :     TextIO.closeIn inS;
47 :     checkForErrors errStrm;
48 :     valOf pt
49 :     end) ()
50 : jhr 3508 val _ = dump Ctl.dumpPT (ParseTreePP.output errStrm) "parsing" parseTree
51 : jhr 3394 (***** TYPECHECKING *****)
52 :     val _ = PhaseTimer.start Timers.timeTypechecker
53 :     val ast = (Typechecker.check errStrm parseTree)
54 :     val _ = PhaseTimer.stop Timers.timeTypechecker
55 :     val _ = checkForErrors errStrm
56 : jhr 3417 (* TODO: check AST for consistency *)
57 : jhr 3508 val _ = dump Ctl.dumpAST ASTPP.output "typechecking" ast
58 : jhr 3394 (***** SIMPLIFY *****)
59 : jhr 3508 val simple = SimpleOpt.checkAfter ("simplify", Simplify.transform (errStrm, ast))
60 : jhr 3437 val simple = SimpleOpt.transform simple
61 : jhr 3394 in
62 : jhr 3506 simple
63 : jhr 3394 end
64 :    
65 : jhr 3445 (* a wrapper around the front-end that handles the Error.ERROR exception and reports
66 :     * the error messages.
67 :     *)
68 :     fun frontEnd filename = let
69 :     val errStrm = Error.mkErrStream filename
70 :     fun finish () = (
71 : jhr 3585 if Error.anyWarnings errStrm
72 : jhr 3445 then Error.report (TextIO.stdErr, errStrm)
73 :     else ())
74 :     in
75 :     (frontEnd' (errStrm, filename) handle exn => (finish (); raise exn))
76 :     before finish()
77 :     end
78 :    
79 : jhr 3411 fun doFile (target : Target.desc, filename) = let
80 : jhr 3394 val baseName = (case OS.Path.splitBaseExt filename
81 :     of {base, ext=SOME "diderot"} => base
82 :     | _ => (errnl "expected diderot file"; quitWithError filename)
83 :     (* end case *))
84 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
85 : jhr 3521 (***** TRANSLATION TO HIGH IR*****)
86 : jhr 3481 val high = Translate.translate simple
87 : jhr 3521 val _ = dump Ctl.dumpHighIR HighPP.output "translate" high
88 :     (***** HIGH-IR OPTIMIZATION *****)
89 : jhr 3522 val high = HighOptimizer.optimize high
90 : jhr 3521 (***** TRANSLATION TO MID IR *****)
91 : jhr 3582 val mid = HighToMid.translate high
92 :     val _ = dump Ctl.dumpMidIR MidPP.output "high-to-mid" mid
93 : jhr 3586 (***** MID-IR OPTIMIZATION *****)
94 :     val mid = MidOptimizer.optimize mid
95 : jhr 3521 (***** TRANSLATION TO LOW IR *****)
96 : jhr 3709 val low = MidToLow.translate mid
97 :     val _ = dump Ctl.dumpLowIR LowPP.output "mid-to-low" low
98 : jhr 3394 (* TODO *)
99 :     in
100 :     (***** CODE GENERATION *****)
101 :     (* TODO *)()
102 :     end
103 :    
104 : jhr 3411 fun usage (cmd, long) = TextIO.output(TextIO.stdErr, Options.usage (cmd, long))
105 : jhr 3394
106 : jhr 3445 fun handleExn Error.ERROR = OS.Process.failure
107 : jhr 3394 | handleExn exn = (
108 :     err (concat [
109 :     "uncaught exception ", General.exnName exn,
110 :     " [", General.exnMessage exn, "]\n"
111 :     ]);
112 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
113 :     OS.Process.failure)
114 :    
115 :     fun main (name: string, args: string list) = let
116 : jhr 3411 val {help, version, target, defs, file} =
117 : jhr 3394 (Options.parseCmdLine args)
118 :     handle Options.Usage msg => (
119 :     err(concat[msg, "\n"]);
120 : jhr 3411 usage (name, false);
121 : jhr 3394 OS.Process.exit OS.Process.failure)
122 :     in
123 : jhr 3417 Ctl.resolve();
124 : jhr 3411 case help
125 :     of SOME long => (usage (name, long); OS.Process.success)
126 :     | NONE => if version
127 :     then (print(Version.message ^ "\n"); OS.Process.success)
128 :     else let
129 :     val {base, ...} = OS.Path.splitBaseExt file
130 :     in
131 :     (* set the default real type *)
132 :     if #double target then (RawTypes.realTy := RawTypes.RT_Double) else ();
133 :     if CmdLineConstants.initFromArgs defs
134 :     then (
135 :     if Controls.get Ctl.enableLog
136 :     then Log.init(base ^ ".log")
137 :     else ();
138 :     PhaseTimer.withTimer Timers.timeCompiler
139 :     doFile (target, file);
140 :     Stats.report ();
141 :     Log.reportTiming Timers.timeCompiler;
142 :     OS.Process.success
143 :     ) handle exn => handleExn exn
144 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
145 :     end
146 :     (* end case *)
147 : jhr 3394 end
148 :    
149 :     end

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