Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/compiler/driver/main.sml
ViewVC logotype

Annotation of /trunk/src/compiler/driver/main.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 33 (* main.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 33 * 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 : jhr 1116 (* exception tracing magic *)
16 : jhr 1166 (*
17 : jhr 1116 val _ = (
18 :     SMLofNJ.Internals.TDP.mode := true;
19 :     Coverage.install ();
20 :     BackTrace.install ())
21 : jhr 1166 *)
22 : jhr 256
23 : jhr 33 fun err s = TextIO.output (TextIO.stdErr, s)
24 :     fun err1 c = TextIO.output1 (TextIO.stdErr, c)
25 :     fun errnl s = (err s; err1 #"\n")
26 :    
27 : jhr 429 exception ERROR
28 : jhr 33
29 : jhr 2356 fun quitWithError srcFile = raise ERROR
30 : jhr 429
31 : jhr 33 (* check for errors and report them if there are any *)
32 : jhr 2356 fun checkForErrors errStrm =
33 : jhr 3003 if Error.anyErrors errStrm
34 :     then (
35 :     Error.report (TextIO.stdErr, errStrm);
36 :     quitWithError (Error.sourceFile errStrm))
37 :     else ()
38 : jhr 33
39 : jhr 1116 (* compiler front end (parsing, typechecking, and simplification *)
40 :     fun frontEnd filename = let
41 : jhr 3003 val errStrm = Error.mkErrStream filename
42 :     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 :     raise ERROR)
47 :     (***** PARSING *****)
48 :     val parseTree = PhaseTimer.withTimer Timers.timeParser (fn () => let
49 :     val inS = TextIO.openIn filename
50 :     val pt = Parser.parseFile (errStrm, inS)
51 :     in
52 :     TextIO.closeIn inS;
53 :     checkForErrors errStrm;
54 :     valOf pt
55 :     end) ()
56 :     (***** TYPECHECKING *****)
57 :     val _ = PhaseTimer.start Timers.timeTypechecker
58 :     val ast = (Typechecker.check errStrm parseTree)
59 :     val _ = PhaseTimer.stop Timers.timeTypechecker
60 :     val _ = checkForErrors errStrm
61 :     val _ = ASTPP.output (Log.logFile(), ast) (* DEBUG *)
62 :     (***** SIMPLIFY *****)
63 :     val _ = PhaseTimer.start Timers.timeSimplify
64 :     val simple = Simplify.transform (errStrm, ast)
65 :     val _ = checkForErrors errStrm
66 :     val _ = PhaseTimer.stop Timers.timeSimplify
67 : jhr 2356 (* currently handled by Simplify
68 : jhr 3003 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
69 : jhr 2356 *)
70 : jhr 3003 in
71 :     (* check for warnings and report if necessary *)
72 :     if Error.anyWarnings errStrm
73 :     then Error.report (TextIO.stdErr, errStrm)
74 :     else ();
75 :     simple
76 :     end
77 : jhr 1116
78 : jhr 1570 fun doFile (target : TargetUtil.target_desc, filename, optOutput) = let
79 : jhr 3003 val baseName = (case OS.Path.splitBaseExt filename
80 :     of {base, ext=SOME "diderot"} => base
81 :     | _ => (errnl "expected diderot file"; quitWithError filename)
82 :     (* end case *))
83 :     val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
84 :     (***** TRANSLATION TO HIGH IL*****)
85 :     val _ = PhaseTimer.start Timers.timeTranslate
86 :     val highIL = Translate.translate simple
87 :     val _ = PhaseTimer.stop Timers.timeTranslate
88 :     (***** HIGH-IL OPTIMIZATION *****)
89 :     val _ = PhaseTimer.start Timers.timeHigh
90 :     val highIL = HighOptimizer.optimize highIL
91 :     val _ = PhaseTimer.stop Timers.timeHigh
92 :     (***** TRANSLATION TO MID IL *****)
93 :     val midIL = PhaseTimer.withTimer Timers.timeMid HighToMid.translate highIL
94 :     val midIL = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize midIL
95 :     (***** TRANSLATION TO LOW IL *****)
96 :     val lowIL = PhaseTimer.withTimer Timers.timeLow MidToLow.translate midIL
97 :     val lowIL = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize lowIL
98 :     in
99 :     (***** CODE GENERATION *****)
100 :     PhaseTimer.withTimer Timers.timeCodegen
101 :     (BackEnd.generate target)
102 : jhr 1570 (Option.getOpt(optOutput, baseName), lowIL)
103 : jhr 3003 end
104 : jhr 33
105 : jhr 1232 fun usage cmd = TextIO.output(TextIO.stdErr, Options.usage cmd)
106 : jhr 33
107 : jhr 2356 fun handleExn ERROR = OS.Process.failure
108 :     | handleExn exn = (
109 : jhr 3003 err (concat [
110 :     "uncaught exception ", General.exnName exn,
111 :     " [", General.exnMessage exn, "]\n"
112 :     ]);
113 :     List.app (fn s => err (concat [" raised at ", s, "\n"])) (SMLofNJ.exnHistory exn);
114 :     OS.Process.failure)
115 : jhr 2356
116 : jhr 238 fun main (name: string, args: string list) = let
117 : jhr 3003 val {help, version, log, target, output, defs, file} =
118 :     (Options.parseCmdLine args)
119 :     handle Options.Usage msg => (
120 :     err(concat[msg, "\n"]);
121 :     usage name;
122 :     OS.Process.exit OS.Process.failure)
123 :     in
124 :     if help
125 :     then (usage name; OS.Process.success)
126 :     else if version
127 :     then (print(Version.message ^ "\n"); OS.Process.success)
128 :     else let
129 :     val {base, ...} = OS.Path.splitBaseExt file
130 :     in
131 :     if CmdLineInputs.initFromArgs defs
132 :     then (
133 :     if log then Log.init(base ^ ".log") else ();
134 :     PhaseTimer.withTimer Timers.timeCompiler
135 : jhr 1570 doFile (target, file, output);
136 : jhr 3003 Stats.report ();
137 :     Log.reportTiming Timers.timeCompiler;
138 :     OS.Process.success
139 :     ) handle exn => handleExn exn
140 :     else (err "invalid command-line inputs\n"; OS.Process.failure)
141 :     end
142 :     end
143 : jhr 238
144 : jhr 33 end

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