Main.scala 2.57 KB
Newer Older
Sapphie's avatar
Sapphie committed
1
2
package l3

Sapphie's avatar
Sapphie committed
3
import java.io.PrintWriter
Sapphie's avatar
Sapphie committed
4
5
6
7
import java.nio.file.{ Files, Paths }

import l3.SymbolicCL3TreeModule.Tree

Sapphie's avatar
Sapphie committed
8
9
10
11
import CL3TreeFormatter._ // Implicits required for CL3 tree printing
import CPSTreeFormatter._ // Implicits required for CPS tree printing
import CPSTreeChecker._   // Implicits required for CPS tree checking

Sapphie's avatar
Sapphie committed
12
13
object Main {
  def main(args: Array[String]): Unit = {
Sapphie's avatar
Sapphie committed
14
    val stats = new Statistics()
Sapphie's avatar
Sapphie committed
15
    val backEnd: Tree => TerminalPhaseResult = (
Sapphie's avatar
Sapphie committed
16
      // CL3Interpreter
Sapphie's avatar
Sapphie committed
17
      CL3ToCPSTranslator
Sapphie's avatar
Sapphie committed
18
        // andThen treePrinter("---------- After translation to CPS")
19
        andThen CPSOptimizerHigh
Sapphie's avatar
Sapphie committed
20
21
        // andThen treePrinter("---------- After high optimization")
        // andThen CPSInterpreterHigh
Sapphie's avatar
Sapphie committed
22
        andThen CPSValueRepresenter
Sapphie's avatar
Sapphie committed
23
        // andThen treeChecker
Sapphie's avatar
Sapphie committed
24
        andThen CPSHoister
Sapphie's avatar
Sapphie committed
25
        // andThen treePrinter("---------- After value representation")
Sapphie's avatar
Sapphie committed
26
        andThen CPSOptimizerLow
Sapphie's avatar
Sapphie committed
27
        // andThen treePrinter("---------- After low optimization")
Sapphie's avatar
Sapphie committed
28
29
30
31
32
        // andThen treeChecker
        // andThen (new CPSInterpreterLow(stats.log _))
        andThen CPSConstantNamer
        andThen CPSRegisterAllocator
        andThen CPSToASMTranslator
Sapphie's avatar
Sapphie committed
33
        // andThen seqPrinter("---------- After translation to assembly")
Sapphie's avatar
Sapphie committed
34
        andThen ASMLabelResolver
Sapphie's avatar
Sapphie committed
35
        // andThen ASMInterpreter
Sapphie's avatar
Sapphie committed
36
37
        andThen ASMFileWriter(Option(System.getProperty("l3.out-asm-file"))
                                .getOrElse("out.l3a"))
Sapphie's avatar
Sapphie committed
38
39
40
    )

    val basePath = Paths.get(".").toAbsolutePath
41
    Either.cond(! args.isEmpty, args.toIndexedSeq, "no input file given")
Sapphie's avatar
Sapphie committed
42
43
44
45
46
47
      .flatMap(L3FileReader.readFilesExpandingModules(basePath, _))
      .flatMap(p => L3Parser.parse(p._1, p._2))
      .flatMap(CL3NameAnalyzer)
      .flatMap(backEnd) match {
      case Right((retCode, maybeMsg)) =>
        maybeMsg foreach println
Sapphie's avatar
Sapphie committed
48
        // println(stats)
Sapphie's avatar
Sapphie committed
49
50
51
52
53
54
55
56
57
58
        sys.exit(retCode)
      case Left(errMsg) =>
        println(s"Error: $errMsg")
        sys.exit(1)
    }
  }

  private lazy val outPrintWriter =
    new PrintWriter(System.out, true)

Sapphie's avatar
Sapphie committed
59
60
61
  private def treeChecker[T <: CPSTreeModule](implicit c: CPSTreeChecker[T]) =
    passThrough(c)

Sapphie's avatar
Sapphie committed
62
  private def treePrinter[T](msg: String)(implicit f: Formatter[T]): T => T =
Sapphie's avatar
Sapphie committed
63
    passThrough { tree =>
Sapphie's avatar
Sapphie committed
64
65
66
      outPrintWriter.println(msg)
      f.toDoc(tree).writeTo(80, outPrintWriter)
      outPrintWriter.println()
Sapphie's avatar
Sapphie committed
67
68
69
70
71
72
73
74
75
76
77
    }

  private def seqPrinter[T](msg: String): Seq[T] => Seq[T] =
    passThrough { program =>
      outPrintWriter.println(msg)
      program foreach outPrintWriter.println
    }

  private def passThrough[T](f: T => Unit): T => T =
    { t: T => f(t); t }
}