Skip to content
Snippets Groups Projects
Commit 3e223b3d authored by Régis Blanc's avatar Régis Blanc
Browse files

implement missing ArrayUpdated in codegen

parent dbf8b945
No related branches found
No related tags found
No related merge requests found
......@@ -346,6 +346,30 @@ trait CodeGeneration {
case _ => AALOAD
})
case au @ ArrayUpdated(a, i, v) =>
mkExpr(a, ch)
ch << DUP
ch << ARRAYLENGTH
val storeInstr = a.getType match {
case ArrayType(Int32Type) => ch << NewArray.primitive("T_INT"); IASTORE
case ArrayType(BooleanType) => ch << NewArray.primitive("T_BOOLEAN"); BASTORE
case ArrayType(other) => ch << NewArray(typeToJVM(other)); AASTORE
case other => throw CompilationException("Cannot compile finite array expression whose type is %s.".format(other))
}
//srcArrary and targetArray is on the stack
ch << DUP_X1 //insert targetArray under srcArray
ch << Ldc(0) << SWAP //srcArray, 0, targetArray
ch << DUP << ARRAYLENGTH //targetArray, length on stack
ch << Ldc(0) << SWAP //final arguments: src, 0, target, 0, length
ch << InvokeStatic("java/lang/System", "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V")
//targetArray remains on the stack
ch << DUP
mkExpr(i, ch)
mkExpr(v, ch)
ch << storeInstr
//returns targetArray
case a @ FiniteArray(es) =>
ch << Ldc(es.size)
val storeInstr = a.getType match {
......
import leon.Utils._
object Test {
def test(a: Array[Int]): Int = {
require(a.length > 0)
val a2 = a.updated(0, 2)
a2(0)
} ensuring(res => res == 2)
}
......@@ -377,7 +377,12 @@ class EvaluatorsTests extends LeonTestSuite {
val p = """|object Program {
| def boolArrayRead(bools : Array[Boolean], index : Int) : Boolean = bools(index)
|
| def intArrayRead(bools : Array[Int], index : Int) : Int = bools(index)
| def intArrayRead(ints : Array[Int], index : Int) : Int = ints(index)
|
| def intArrayUpdate(ints : Array[Int], index : Int, value: Int) : Int = {
| val na = ints.updated(index, value)
| na(index)
| }
|}
|""".stripMargin
......@@ -394,6 +399,9 @@ class EvaluatorsTests extends LeonTestSuite {
checkComp(e, mkCall("intArrayRead", ia, IL(1)), IL(42))
checkComp(e, ArrayLength(ia), IL(3))
checkComp(e, mkCall("intArrayUpdate", ia, IL(0), IL(13)), IL(13))
checkComp(e, mkCall("intArrayUpdate", ia, IL(1), IL(17)), IL(17))
checkError(e, mkCall("boolArrayRead", ba, IL(2)))
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment