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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/driver/main.sml
ViewVC logotype

View of /branches/charisee/src/compiler/driver/main.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3174 - (download) (annotate)
Mon Mar 30 11:46:58 2015 UTC (3 years, 11 months ago) by cchiw
File size: 6166 byte(s)
hack
(* 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 ERROR

    val testing=1
    fun testp e=(case testing
        of 0=>1
        | _=> ( print(String.concat ["\n\t\t------------",e,"-----------\n"]);1))

  (* check for errors and report them if there are any *)
    fun checkForErrors errStrm =
          if Error.anyErrors errStrm
            then (
              Error.report (TextIO.stdErr, errStrm);
              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)
          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
(* currently handled by Simplify
          val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
*)
          in
          (* check for warnings and report if necessary *)
            if Error.anyWarnings errStrm
              then Error.report (TextIO.stdErr, errStrm)
              else ();
            simple
          end

    fun doFile (target : TargetUtil.target_desc, filename, optOutput) = 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 _=testp"In HighIL"
          val _ = PhaseTimer.start Timers.timeHigh
          val _=testp "High Op"
          val highIL = HighOptimizer.optimize highIL
          val _ = PhaseTimer.stop Timers.timeHigh
          val _=PhaseTimer.report(TextIO.stdOut,Timers.timeHigh)
        (***** TRANSLATION TO MID IL *****)
          val _=testp  "High to mid"
          val midIL = PhaseTimer.withTimer Timers.timeMid HighToMid.translate highIL
          val _=testp  "Mid Op"
          val midIL = PhaseTimer.withTimer Timers.timeMid MidOptimizer.optimize midIL

         val _=PhaseTimer.report(TextIO.stdOut,Timers.timeMid)
        (***** TRANSLATION TO LOW IL *****)
          val _=testp  "Mid to Low IL"
          val lowIL = PhaseTimer.withTimer Timers.timeLow MidToLow.translate midIL
          val _=testp  "Low IL Op"
          val lowIL = PhaseTimer.withTimer Timers.timeLow LowOptimizer.optimize lowIL
          val _ = testp  "Code Gen"
                     val _=PhaseTimer.report(TextIO.stdOut,Timers.timeLow)
          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 handleExn ERROR = OS.Process.failure
      | handleExn 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)

    fun main (name: string, args: string list) = let
          val {help, version, 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 if version
              then (print(Version.message ^ "\n"); OS.Process.success)
              else let
                val {base, ...} = OS.Path.splitBaseExt file
                in
                  if CmdLineInputs.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 => handleExn exn
                    else (err "invalid command-line inputs\n"; OS.Process.failure)
                end
          end

  end

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