Commit 324b72dc authored by Luca Bataillard's avatar Luca Bataillard
Browse files

fix array out of bounds issue

parent cf388c4a
...@@ -56,11 +56,7 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) { ...@@ -56,11 +56,7 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
} }
} }
private def rewriteIndex(idx: H.Atom): L.Atom = idx match {
case H.AtomN(n) => L.AtomN(n)
case H.AtomL(IntLit(i)) => L.AtomL(i.toInt)
case _ => throw new Exception(f"Cannot rewrite atom {idx} which is not Name or Int Literal")
}
private def getMaskR(numBits: Int): Either[H.Atom, L.Atom] = Right(L.AtomL((1 << numBits) -1)) private def getMaskR(numBits: Int): Either[H.Atom, L.Atom] = Right(L.AtomL((1 << numBits) -1))
...@@ -87,15 +83,6 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) { ...@@ -87,15 +83,6 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
} }
prim match { prim match {
case L3.BlockSet =>
val block = rewrite(x)
val idx = rewriteIndex(y)
val value = rewrite(z)
L.LetP(n, CPS.BlockSet, Seq(block, idx, value), apply(body))
case L3.BlockGet =>
val block = rewrite(x)
val idx = rewriteIndex(y)
L.LetP(n, CPS.BlockGet, Seq(block, idx), apply(body))
case L3.IntAdd => case L3.IntAdd =>
tempLetP(CPS.Sub, Seq(Left(x), lAtomOne)) { x1 => tempLetP(CPS.Sub, Seq(Left(x), lAtomOne)) { x1 =>
L.LetP(n, CPS.Add, Seq(x1, rewrite(y)), apply(body)) L.LetP(n, CPS.Add, Seq(x1, rewrite(y)), apply(body))
...@@ -146,21 +133,29 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) { ...@@ -146,21 +133,29 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
case L3.BlockAlloc(tag) => case L3.BlockAlloc(tag) =>
tempLetP(CPS.ShiftRight, Seq(Right(rewrite(x)), lAtomOne)) { t1 => tempLetP(CPS.ShiftRight, Seq(Left(x), lAtomOne)) { t1 =>
L.LetP(n, CPS.BlockAlloc(tag), Seq(t1), apply(body)) L.LetP(n, CPS.BlockAlloc(tag), Seq(t1), apply(body))
} }
case L3.BlockTag => case L3.BlockTag =>
tempLetP(CPS.BlockTag, args map (x => Right(rewrite(x)))) { t1 => tempLetP(CPS.BlockTag, args map (Left(_))) { t1 =>
tempLetP(CPS.ShiftLeft, Seq(Right(t1), lAtomOne)) { t2 => tempLetP(CPS.ShiftLeft, Seq(Right(t1), lAtomOne)) { t2 =>
L.LetP(n, CPS.Add, Seq(t2, L.AtomL(1)), apply(body)) L.LetP(n, CPS.Add, Seq(t2, L.AtomL(1)), apply(body))
} }
} }
case L3.BlockLength => case L3.BlockLength =>
tempLetP(CPS.BlockLength, args map (x => Left(x))) { t1 => tempLetP(CPS.BlockLength, args map (Left(_))) { t1 =>
tempLetP(CPS.ShiftLeft, Seq(Right(t1), Right(L.AtomL(1)))) { t2 => tempLetP(CPS.ShiftLeft, Seq(Right(t1), Right(L.AtomL(1)))) { t2 =>
L.LetP(n, CPS.Add, Seq(t2, L.AtomL(1)), apply(body)) L.LetP(n, CPS.Add, Seq(t2, L.AtomL(1)), apply(body))
} }
} }
case L3.BlockSet =>
val block = rewrite(x)
val value = rewrite(z)
rewriteIndex(y) {idx => L.LetP(n, CPS.BlockSet, Seq(block, idx, value), apply(body))}
case L3.BlockGet =>
val block = rewrite(x)
rewriteIndex(y){idx => L.LetP(n, CPS.BlockGet, Seq(block, idx), apply(body))}
case L3.ByteRead => case L3.ByteRead =>
tempLetP(CPS.ByteRead, Seq()){ t1 => tempLetP(CPS.ByteRead, Seq()){ t1 =>
...@@ -211,4 +206,11 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) { ...@@ -211,4 +206,11 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
L.AtomL(0x0a) // 01010 L.AtomL(0x0a) // 01010
case H.AtomL(UnitLit) => L.AtomL(2) case H.AtomL(UnitLit) => L.AtomL(2)
} }
private def rewriteIndex(idx: H.Atom)(cont: L.Atom => L.LetP): L.LetP = idx match {
case H.AtomN(n) =>
tempLetP(CPS.ShiftRight, Seq(Left(idx), Right(L.AtomL(1))))(cont)
case H.AtomL(IntLit(i)) => cont(L.AtomL(i.toInt))
case _ => throw new Exception(f"Cannot rewrite atom {idx} which is not Name or Int Literal")
}
} }
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