Skip to content

Commit c3d3b02

Browse files
committed
Refactor tests for import/exports
1 parent af72ccf commit c3d3b02

7 files changed

Lines changed: 437 additions & 301 deletions

File tree

usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInterpreter.kt

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -498,20 +498,80 @@ class TsInterpreter(
498498
val isDflt = stmt.location.method.name == DEFAULT_ARK_METHOD_NAME &&
499499
stmt.location.method.enclosingClass?.name == DEFAULT_ARK_CLASS_NAME
500500
if (isDflt) {
501-
val lhv = stmt.lhv
502-
check(lhv is EtsLocal) {
503-
"All assignments in %dflt::%dflt should be to locals, but got: $stmt"
504-
}
505-
if (!lhv.name.startsWith("%") && !lhv.name.startsWith("_tmp") && lhv.name != "this") {
506-
val file = stmt.location.method.enclosingClass!!.declaringFile!!
507-
logger.info {
508-
"Assigning to a global variable: ${lhv.name} in $file"
501+
when (val lhv = stmt.lhv) {
502+
is EtsLocal -> {
503+
val name = lhv.name
504+
if (!name.startsWith("%") && !name.startsWith("_tmp") && name != "this") {
505+
val file = stmt.location.method.enclosingClass!!.declaringFile!!
506+
logger.info {
507+
"Assigning to a global variable: $name in $file"
508+
}
509+
val dfltObject = getDfltObject(file)
510+
val lValue = mkFieldLValue(expr.sort, dfltObject, name)
511+
memory.write(lValue, expr.cast(), guard = trueExpr)
512+
saveSortForDfltObjectField(file, name, expr.sort)
513+
return@calcOnState Unit
514+
}
515+
}
516+
517+
is EtsInstanceFieldRef -> {
518+
val name = lhv.instance.name
519+
if (!name.startsWith("%") && !name.startsWith("_tmp") && name != "this") {
520+
val file = stmt.location.method.enclosingClass!!.declaringFile!!
521+
logger.info {
522+
"Assigning to a field of a global variable: $name.${lhv.field.name} in $file"
523+
}
524+
val dfltObject = getDfltObject(file)
525+
val lValue = mkFieldLValue(addressSort, dfltObject, name)
526+
val instance = memory.read(lValue)
527+
val fieldLValue = mkFieldLValue(expr.sort, instance, lhv.field)
528+
memory.write(fieldLValue, expr.cast(), guard = trueExpr)
529+
return@calcOnState Unit
530+
}
531+
}
532+
533+
is EtsArrayAccess -> {
534+
val name = lhv.array.name
535+
if (!name.startsWith("%") && !name.startsWith("_tmp") && name != "this") {
536+
val file = stmt.location.method.enclosingClass!!.declaringFile!!
537+
logger.info {
538+
"Assigning to an element of a global array variable: $name[${lhv.index}] in $file"
539+
}
540+
val dfltObject = getDfltObject(file)
541+
val lValue = mkFieldLValue(addressSort, dfltObject, name)
542+
val array = memory.read(lValue)
543+
val resolvedIndex = exprResolver.resolve(lhv.index)
544+
?: return@calcOnState null
545+
val index = resolvedIndex.asExpr(fp64Sort)
546+
val bvIndex = mkFpToBvExpr(
547+
roundingMode = fpRoundingModeSortDefaultValue(),
548+
value = index,
549+
bvSize = 32,
550+
isSigned = true
551+
).asExpr(sizeSort)
552+
val arrayType = if (isAllocatedConcreteHeapRef(array)) {
553+
memory.typeStreamOf(array).first()
554+
} else {
555+
lhv.array.type
556+
}
557+
check(arrayType is EtsArrayType) {
558+
"Expected EtsArrayType, got: ${lhv.array.type}"
559+
}
560+
val elementSort = typeToSort(arrayType.elementType)
561+
val elementLValue = mkArrayIndexLValue(
562+
sort = elementSort,
563+
ref = array,
564+
index = bvIndex.asExpr(sizeSort),
565+
type = arrayType,
566+
)
567+
memory.write(elementLValue, expr.cast(), guard = trueExpr)
568+
return@calcOnState Unit
569+
}
570+
}
571+
572+
else -> {
573+
error("LHV of type ${lhv::class.java} is not supported in %dflt::%dflt: $lhv")
509574
}
510-
val dfltObject = getDfltObject(file)
511-
val lValue = mkFieldLValue(expr.sort, dfltObject, lhv.name)
512-
memory.write(lValue, expr.cast(), guard = trueExpr)
513-
saveSortForDfltObjectField(file, lhv.name, expr.sort)
514-
return@calcOnState Unit
515575
}
516576
}
517577

0 commit comments

Comments
 (0)