Commit 527efc2b authored by Sapphie's avatar Sapphie
Browse files

Add optimisation details

parent 33e844bf
......@@ -122,6 +122,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
shrunkBody
} else {
val allLitOpt = replacedArgs.map(_.asLiteral)
val areAllLit: Boolean = allLitOpt.map(_.isDefined).foldLeft(true)((x, y) => x && y)
lazy val asLit = allLitOpt.map(_.get)
if (!unstable(prim) && !impure(prim) && s.eInvEnv.contains((prim, replacedArgs))) {
......@@ -131,7 +132,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
shrink(body, s.withASubst(name, replacedArgs(0)))
} else replacedArgs match {
// Constant folding
case Seq(AtomL(l1), AtomL(l2)) if vEvaluator.isDefinedAt((prim, asLit)) =>
case _ if areAllLit && vEvaluator.isDefinedAt((prim, asLit)) =>
shrink(body, s.withASubst(name, vEvaluator((prim, asLit))))
// Same argument reduction
case Seq(a1, a2) if a1 == a2 && sameArgReduce.isDefinedAt(prim, a1) =>
......@@ -472,7 +473,7 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
protected val leftNeutral: Set[(Literal, ValuePrimitive)] = Set(
(IntLit(L3Int(0)), IntAdd),
(IntLit(L3Int(1)), IntMul),
(IntLit(L3Int((-1 << 1) >> 1)), IntBitwiseAnd),
(IntLit(L3Int(~0)), IntBitwiseAnd),
(IntLit(L3Int(0)), IntBitwiseOr),
(IntLit(L3Int(0)), IntBitwiseXOr)
)
......@@ -483,7 +484,7 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
(IntDiv, IntLit(L3Int(1))),
(IntShiftLeft, IntLit(L3Int(0))),
(IntShiftRight, IntLit(L3Int(0))),
(IntBitwiseAnd, IntLit(L3Int((-1 << 1) >> 1))),
(IntBitwiseAnd, IntLit(L3Int(~0))),
(IntBitwiseOr, IntLit(L3Int(0))),
(IntBitwiseXOr, IntLit(L3Int(0))),
)
......@@ -492,15 +493,16 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
(IntLit(L3Int(0)), IntMul),
(IntLit(L3Int(0)), IntMod),
(IntLit(L3Int(0)), IntBitwiseAnd),
(IntLit(L3Int((-1 << 1) >> 1)), IntBitwiseOr),
(IntLit(L3Int(~0)), IntBitwiseOr),
(IntLit(L3Int(0)), IntShiftLeft),
(IntLit(L3Int(0)), IntShiftRight)
(IntLit(L3Int(0)), IntShiftRight),
(IntLit(L3Int(0)), IntDiv)
)
protected val rightAbsorbing: Set[(ValuePrimitive, Literal)] = Set(
(IntMul, IntLit(L3Int(0))),
(IntBitwiseAnd, IntLit(L3Int(0))),
(IntBitwiseOr, IntLit(L3Int((-1 << 1) >> 1)))
(IntBitwiseOr, IntLit(L3Int(~0)))
)
protected val sameArgReduce: PartialFunction[(ValuePrimitive, Atom), Atom] = {
......@@ -529,6 +531,8 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
case IntShiftLeft => IntLit(x << y)
case IntShiftRight => IntLit(x >> y)
}
case (IntToChar, Seq(IntLit(i))) => CharLit(i.toInt.toChar)
case (CharToInt, Seq(CharLit(c))) => IntLit(L3Int(c.toInt))
}
protected val cEvaluator: PartialFunction[(TestPrimitive, Seq[Literal]),
......@@ -536,6 +540,15 @@ object CPSOptimizerHigh extends CPSOptimizer(SymbolicCPSTreeModule)
case (IntLe, Seq(IntLit(x), IntLit(y))) => x <= y
case (IntLt, Seq(IntLit(x), IntLit(y))) => x < y
case (Eq, Seq(l1, l2)) => l1 == l2
case (IntP, Seq(IntLit(_))) => true
case (CharP, Seq(CharLit(_))) => true
case (BoolP, Seq(BooleanLit(_))) => true
case (UnitP, Seq(UnitLit)) => true
case (BlockP, Seq(_)) => false
case (IntP, Seq(_)) => false
case (CharP, Seq(_)) => false
case (BoolP, Seq(_)) => false
case (UnitP, Seq(_)) => false
}
}
......
......@@ -11,6 +11,7 @@ import CPSTreeChecker._ // Implicits required for CPS tree checking
object Main {
def main(args: Array[String]): Unit = {
val stats = new Statistics()
val backEnd: Tree => TerminalPhaseResult = (
// CL3Interpreter
CL3ToCPSTranslator
......@@ -21,17 +22,18 @@ object Main {
andThen CPSValueRepresenter
andThen CPSHoister
// andThen treePrinter("---------- After value representation")
andThen CPSOptimizerLow
// andThen CPSOptimizerLow
// andThen treePrinter("---------- After low optimization")
// andThen CPSInterpreterLow
andThen CPSConstantNamer
andThen CPSRegisterAllocator
andThen CPSToASMTranslator
andThen treeChecker
andThen (new CPSInterpreterLow(stats.log _))
// andThen CPSConstantNamer
// andThen CPSRegisterAllocator
// andThen CPSToASMTranslator
// andThen seqPrinter("---------- After translation to assembly")
andThen ASMLabelResolver
// andThen ASMLabelResolver
// andThen ASMInterpreter
andThen ASMFileWriter(Option(System.getProperty("l3.out-asm-file"))
.getOrElse("out.l3a"))
// andThen ASMFileWriter(Option(System.getProperty("l3.out-asm-file"))
// .getOrElse("out.l3a"))
)
val basePath = Paths.get(".").toAbsolutePath
......@@ -42,6 +44,7 @@ object Main {
.flatMap(backEnd) match {
case Right((retCode, maybeMsg)) =>
maybeMsg foreach println
println(stats)
sys.exit(retCode)
case Left(errMsg) =>
println(s"Error: $errMsg")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment