Skip to content
Snippets Groups Projects
Commit 1cd050ac authored by Sapphie's avatar Sapphie
Browse files

Implement division and modulo

parent fbf8978a
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
case H.If(cond, args, thenC, elseC) =>
transformIf(cond, args, thenC, elseC)
case _ => throw new Exception(tree.getClass.toString)
case _ => throw new Exception("Unimplemented: " + tree.getClass.toString)
}
private def transformIf(cond: L3, args: Seq[H.Atom], thenC: H.Name, elseC: H.Name): L.Tree = {
......@@ -101,9 +101,27 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
}
}
// Transforms value primitives that take 2 arguments
private def applyLetPBinary(n: H.Name, prim: L3, x: H.Atom, y: H.Atom, body: H.Tree): L.LetP = {
val lAtomOne: Either[H.Atom, L.Atom] = Right(L.AtomL(1))
// Generates the LetP tree for a brute "untag, apply, retag" operation
def rawTree(op: CPS): L.LetP = {
// Untag both values
tempLetP(CPS.ShiftRight, Seq(Left(x), lAtomOne)) { x1 =>
tempLetP(CPS.ShiftRight, Seq(Left(y), lAtomOne)) { y1 =>
// Apply the actual operation
tempLetP(op, Seq(Right(x1), Right(y1))) { truDiv =>
// Retag the result
tempLetP(CPS.ShiftLeft, Seq(Right(truDiv), lAtomOne)) { shiftedRes =>
L.LetP(n, CPS.Add, Seq(shiftedRes, L.AtomL(1)), apply(body))
}
}
}
}
}
prim match {
case L3.BlockGet =>
// In theory, x has to be a name, since there cannot be pointer literals
......@@ -129,8 +147,8 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
}
// I don't think there is a way to do this in a smart way
case L3.IntDiv => ???
case L3.IntMod => ???
case L3.IntDiv => rawTree(CPS.Div)
case L3.IntMod => rawTree(CPS.Mod)
case L3.IntShiftLeft =>
tempLetP(CPS.Sub, Seq(Left(x), lAtomOne)) { x1 =>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment