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

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