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 3586 - (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 checkForErrors errStrm;
72 :     if Error.anyWarnings errStrm
73 : jhr 3445 then Error.report (TextIO.stdErr, errStrm)
74 :     else ())
75 :     in
76 :     (frontEnd' (errStrm, filename) handle exn => (finish (); raise exn))
77 :     before finish()
78 :     end
79 :    
80 : jhr 3411 fun doFile (target : Target.desc, filename) = let
81 : jhr 3394 val baseName = (case OS.Path.splitBaseExt filename
82 :     of {base, ext=SOME "diderot"} => base
83 :     | _ => (errnl "expected diderot file"; quitWithError filename)
84 :     (* end case *))
85 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
86 : jhr 3521 (***** TRANSLATION TO HIGH IR*****)
87 : jhr 3481 val high = Translate.translate simple
88 : jhr 3521 val _ = dump Ctl.dumpHighIR HighPP.output "translate" high
89 :     (***** HIGH-IR OPTIMIZATION *****)
90 : jhr 3522 val high = HighOptimizer.optimize high
91 : jhr 3521 (***** TRANSLATION TO MID IR *****)
92 : jhr 3582 val mid = HighToMid.translate high
93 :     val _ = dump Ctl.dumpMidIR MidPP.output "high-to-mid" mid
94 : jhr 3586 (***** MID-IR OPTIMIZATION *****)
95 :     val mid = MidOptimizer.optimize mid
96 : jhr 3521 (***** TRANSLATION TO LOW IR *****)
97 : jhr 3394 (* TODO *)
98 :     in
99 :     (***** CODE GENERATION *****)
100 :     (* TODO *)()
101 :     end
102 :    
103 : jhr 3411 fun usage (cmd, long) = TextIO.output(TextIO.stdErr, Options.usage (cmd, long))
104 : jhr 3394
105 : jhr 3445 fun handleExn Error.ERROR = OS.Process.failure
106 : jhr 3394 | handleExn exn = (
107 :     err (concat [
108 :     "uncaught exception ", General.exnName exn,
109 :     " [", General.exnMessage exn, "]\n"
110 :     ]);
111 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
112 :     OS.Process.failure)
113 :    
114 :     fun main (name: string, args: string list) = let
115 : jhr 3411 val {help, version, target, defs, file} =
116 : jhr 3394 (Options.parseCmdLine args)
117 :     handle Options.Usage msg => (
118 :     err(concat[msg, "\n"]);
119 : jhr 3411 usage (name, false);
120 : jhr 3394 OS.Process.exit OS.Process.failure)
121 :     in
122 : jhr 3417 Ctl.resolve();
123 : jhr 3411 case help
124 :     of SOME long => (usage (name, long); OS.Process.success)
125 :     | NONE => if version
126 :     then (print(Version.message ^ "\n"); OS.Process.success)
127 :     else let
128 :     val {base, ...} = OS.Path.splitBaseExt file
129 :     in
130 :     (* set the default real type *)
131 :     if #double target then (RawTypes.realTy := RawTypes.RT_Double) else ();
132 :     if CmdLineConstants.initFromArgs defs
133 :     then (
134 :     if Controls.get Ctl.enableLog
135 :     then Log.init(base ^ ".log")
136 :     else ();
137 :     PhaseTimer.withTimer Timers.timeCompiler
138 :     doFile (target, file);
139 :     Stats.report ();
140 :     Log.reportTiming Timers.timeCompiler;
141 :     OS.Process.success
142 :     ) handle exn => handleExn exn
143 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
144 :     end
145 :     (* end case *)
146 : jhr 3394 end
147 :    
148 :     end

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