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

SCM Repository

[diderot] View of /branches/pure-cfg/src/compiler/driver/main.sml
ViewVC logotype

View of /branches/pure-cfg/src/compiler/driver/main.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1569 - (download) (annotate)
Fri Oct 28 19:02:11 2011 UTC (7 years, 9 months ago) by jhr
File size: 4813 byte(s)
  Added "-o" option to compiler.
(* main.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure Main : sig

    val main : (string * string list) -> OS.Process.status

  end = struct

  (* exception tracing magic *)
    val _ = (
        SMLofNJ.Internals.TDP.mode := true;
        Coverage.install ();
        BackTrace.install ())

    fun err s = TextIO.output (TextIO.stdErr, s)
    fun err1 c =  TextIO.output1 (TextIO.stdErr, c)
    fun errnl s = (err s; err1 #"\n")

    exception ERROR

    fun quitWithError srcFile = raise Fail("Error in compiling " ^ srcFile)

  (* check for errors and report them if there are any *)
    fun checkForErrors errStrm = (
	  Error.report (TextIO.stdErr, errStrm);
	  if Error.anyErrors errStrm
	    then quitWithError (Error.sourceFile errStrm)
	    else ())


  (* compiler front end (parsing, typechecking, and simplification *)
    fun frontEnd filename = let
	  val errStrm = Error.mkErrStream filename
	  val _ = if OS.FileSys.access(filename, [OS.FileSys.A_READ])
		then ()
		else (
		  err(concat["source file \"", filename, "\" does not exist or is not readable\n"]);
		  raise ERROR)
	(***** PARSING *****)
	  val parseTree = PhaseTimer.withTimer Timers.timeParser (fn () => let
		val inS = TextIO.openIn filename
		val pt = Parser.parseFile (errStrm, inS)
		in
		  TextIO.closeIn inS;
		  checkForErrors errStrm;
		  valOf pt
		end) ()
	(***** TYPECHECKING *****)
	  val _ = PhaseTimer.start Timers.timeTypechecker
	  val ast = (Typechecker.check errStrm parseTree) handle Typechecker.Error => AST.Program[]
	  val _ = PhaseTimer.stop Timers.timeTypechecker
	  val _ = checkForErrors errStrm
	  val _ = ASTPP.output (Log.logFile(), ast) (* DEBUG *)
	(***** SIMPLIFY *****)
	  val _ = PhaseTimer.start Timers.timeSimplify
	  val simple = Simplify.transform (errStrm, ast)
	  val _ = checkForErrors errStrm
	  val _ = PhaseTimer.stop Timers.timeSimplify
	  val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
	  in
	    simple
	  end

    fun doFile (target : TargetUtil.target_desc, filename, optOutput) =
          BackTrace.monitor (fn () => let
            val baseName = (case OS.Path.splitBaseExt filename
                   of {base, ext=SOME "diderot"} => base
                    | _ => (errnl "expected diderot file"; quitWithError filename)
                  (* end case *))
            val simple = PhaseTimer.withTimer Timers.timeFront frontEnd filename
          (***** TRANSLATION TO HIGH IL*****)
            val _ = PhaseTimer.start Timers.timeTranslate
            val highIL = Translate.translate simple
            val _ = PhaseTimer.stop Timers.timeTranslate
          (***** HIGH-IL OPTIMIZATION *****)
            val _ = PhaseTimer.start Timers.timeHigh
            val highIL = HighOptimizer.optimize highIL
            val _ = PhaseTimer.stop Timers.timeHigh
          (***** TRANSLATION TO MID IL *****)
            val midIL = PhaseTimer.withTimer Timers.timeMid HighToMid.translate highIL
            val midIL = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize midIL
          (***** TRANSLATION TO LOW IL *****)
            val lowIL = PhaseTimer.withTimer Timers.timeLow MidToLow.translate midIL
            val lowIL = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize lowIL
            in
            (***** CODE GENERATION *****)
              PhaseTimer.withTimer Timers.timeCodegen
                (BackEnd.generate target)
                  (Option.getOpt(optOutput, baseName), lowIL)
            end)

    fun usage cmd = TextIO.output(TextIO.stdErr, Options.usage cmd)

    fun main (name: string, args: string list) = let
	  val {help, log, target, output, defs, file} =
		(Options.parseCmdLine args)
		  handle Options.Usage msg => (
		    err(concat[msg, "\n"]);
		    usage name;
		    OS.Process.exit OS.Process.failure)
	  in
	    if help
	      then (
		usage name;
		OS.Process.success)
	      else let
		val {base, ...} = OS.Path.splitBaseExt file
		in
		  if Inputs.initFromArgs defs
		    then (
		      if log then Log.init(base ^ ".log") else ();
		      PhaseTimer.withTimer Timers.timeCompiler
                        doFile (target, file, output);
		      Stats.report ();
		      Log.reportTiming Timers.timeCompiler;
		      OS.Process.success
		    ) handle exn => (
			err (concat [
			    "uncaught exception ", General.exnName exn,
                            " [", General.exnMessage exn, "]\n"
                          ]);
                        List.app (fn s => err (concat ["  raised at ", s, "\n"]))
                          (SMLofNJ.exnHistory exn);
                        OS.Process.failure)
		    else (err "invalid command-line inputs\n"; OS.Process.failure)
		end
	  end
	    handle ERROR => OS.Process.failure

  end

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