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

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