Skip to content

Commit 40a2c80

Browse files
DaniilStepanovlehvolk
authored andcommitted
Simple variables assigment fix
1 parent 46cc6df commit 40a2c80

1 file changed

Lines changed: 113 additions & 60 deletions

File tree

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt

Lines changed: 113 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ class RawInstListBuilder(
223223
private val mp = TraceMethodVisitor(printer)
224224

225225
fun printMethod(methodNode: MethodNode): String {
226-
val inList = methodNode.instructions;
226+
val inList = methodNode.instructions;
227227
var s = ""
228228
s += methodNode.name + "\n"
229-
for(i in 0 until methodNode.instructions.size()){
229+
for (i in 0 until methodNode.instructions.size()) {
230230
s += insnToString(inList.get(i))
231231
}
232232
return s
@@ -402,8 +402,43 @@ class RawInstListBuilder(
402402
return currentFrame.locals.getValue(variable)
403403
}
404404

405-
private fun local(variable: Int, expr: JcRawValue, insn: AbstractInsnNode, override: Boolean = false): JcRawAssignInst? {
406-
val oldVar = currentFrame.locals[variable]
405+
// fun LabelNode.isBetween(labelStart: LabelNode, labelEnd: LabelNode): Boolean {
406+
// var curNode: AbstractInsnNode = labelStart
407+
// while (curNode != labelEnd && curNode != null) {
408+
// if (this == curNode) {
409+
// return true
410+
// }
411+
// curNode = curNode.next
412+
// }
413+
// return this == curNode
414+
// }
415+
//
416+
// val instrLabel = methodNode.instructions.takeWhile { it != insn }.last { it is LabelNode } as LabelNode
417+
// val locals = methodNode.localVariables.filter { it.index == variable }
418+
// locals.map { instrLabel.isBetween(it.start, it.end) }
419+
// locals
420+
421+
422+
// val oldVar = currentFrame.locals[variable]?.let {
423+
// if (expr.typeName.isPrimitive.xor(it.typeName.isPrimitive)) {
424+
// null
425+
// } else {
426+
// it
427+
// }
428+
// }
429+
private fun local(
430+
variable: Int,
431+
expr: JcRawValue,
432+
insn: AbstractInsnNode,
433+
override: Boolean = false
434+
): JcRawAssignInst? {
435+
val oldVar = currentFrame.locals[variable]?.let {
436+
if (expr.typeName.isPrimitive.xor(it.typeName.isPrimitive)) {
437+
null
438+
} else {
439+
it
440+
}
441+
}
407442
return if (oldVar != null) {
408443
if (oldVar.typeName == expr.typeName || (expr is JcRawNullConstant && !oldVar.typeName.isPrimitive)) {
409444
if (override) {
@@ -574,11 +609,13 @@ class RawInstListBuilder(
574609
val value = pop()
575610
val index = pop()
576611
val arrayRef = pop()
577-
addInstruction(insn, JcRawAssignInst(
578-
method,
579-
JcRawArrayAccess(arrayRef, index, arrayRef.typeName.elementType()),
580-
value
581-
))
612+
addInstruction(
613+
insn, JcRawAssignInst(
614+
method,
615+
JcRawArrayAccess(arrayRef, index, arrayRef.typeName.elementType()),
616+
value
617+
)
618+
)
582619
}
583620

584621
private fun buildPop(insn: InsnNode) {
@@ -729,7 +766,7 @@ class RawInstListBuilder(
729766
val rightName = right.typeName
730767
val max = maxOfPrimitiveTypes(leftName, rightName)
731768
return when {
732-
max.lessThen(PredefinedPrimitives.Int)-> TypeNameImpl(PredefinedPrimitives.Int)
769+
max.lessThen(PredefinedPrimitives.Int) -> TypeNameImpl(PredefinedPrimitives.Int)
733770
else -> TypeNameImpl(max)
734771
}
735772
}
@@ -743,6 +780,7 @@ class RawInstListBuilder(
743780
val resolvedType = maxOfPrimitiveTypes(operand.typeName.typeName, PredefinedPrimitives.Int)
744781
JcRawNegExpr(TypeNameImpl(resolvedType), operand)
745782
}
783+
746784
Opcodes.ARRAYLENGTH -> JcRawLengthExpr(PredefinedPrimitives.Int.typeName(), operand)
747785
else -> error("Unknown unary opcode $opcode")
748786
}
@@ -783,23 +821,27 @@ class RawInstListBuilder(
783821
}
784822

785823
private fun buildReturn(insn: InsnNode) {
786-
addInstruction(insn, when (val opcode = insn.opcode) {
787-
Opcodes.RETURN -> JcRawReturnInst(method, null)
788-
in Opcodes.IRETURN..Opcodes.ARETURN -> JcRawReturnInst(method, pop())
789-
else -> error("Unknown return opcode: $opcode")
790-
})
824+
addInstruction(
825+
insn, when (val opcode = insn.opcode) {
826+
Opcodes.RETURN -> JcRawReturnInst(method, null)
827+
in Opcodes.IRETURN..Opcodes.ARETURN -> JcRawReturnInst(method, pop())
828+
else -> error("Unknown return opcode: $opcode")
829+
}
830+
)
791831
}
792832

793833
private fun buildMonitor(insn: InsnNode) {
794834
val monitor = pop() as JcRawSimpleValue
795-
addInstruction(insn, when (val opcode = insn.opcode) {
796-
Opcodes.MONITORENTER -> {
797-
JcRawEnterMonitorInst(method, monitor)
798-
}
835+
addInstruction(
836+
insn, when (val opcode = insn.opcode) {
837+
Opcodes.MONITORENTER -> {
838+
JcRawEnterMonitorInst(method, monitor)
839+
}
799840

800-
Opcodes.MONITOREXIT -> JcRawExitMonitorInst(method, monitor)
801-
else -> error("Unknown monitor opcode $opcode")
802-
})
841+
Opcodes.MONITOREXIT -> JcRawExitMonitorInst(method, monitor)
842+
else -> error("Unknown monitor opcode $opcode")
843+
}
844+
)
803845
}
804846

805847
private fun buildThrow(insn: InsnNode) {
@@ -823,7 +865,7 @@ class RawInstListBuilder(
823865
val value = pop()
824866
val instance = pop()
825867
val fieldRef = JcRawFieldRef(instance, declaringClass, fieldName, fieldType)
826-
addInstruction(insnNode, JcRawAssignInst(method, fieldRef, value))
868+
addInstruction(insnNode, JcRawAssignInst(method, fieldRef, value))
827869
}
828870

829871
Opcodes.GETSTATIC -> {
@@ -1245,29 +1287,33 @@ class RawInstListBuilder(
12451287
is String -> push(JcRawStringConstant(cst, STRING_CLASS.typeName()))
12461288
is Type -> {
12471289
val assignment = nextRegister(CLASS_CLASS.typeName())
1248-
addInstruction(insnNode, JcRawAssignInst(
1249-
method,
1250-
assignment,
1251-
when (cst.sort) {
1252-
Type.METHOD -> JcRawMethodType(
1253-
cst.argumentTypes.map { it.descriptor.typeName() },
1254-
cst.returnType.descriptor.typeName(),
1255-
METHOD_TYPE_CLASS.typeName()
1256-
)
1290+
addInstruction(
1291+
insnNode, JcRawAssignInst(
1292+
method,
1293+
assignment,
1294+
when (cst.sort) {
1295+
Type.METHOD -> JcRawMethodType(
1296+
cst.argumentTypes.map { it.descriptor.typeName() },
1297+
cst.returnType.descriptor.typeName(),
1298+
METHOD_TYPE_CLASS.typeName()
1299+
)
12571300

1258-
else -> ldcValue(cst)
1259-
}
1260-
))
1301+
else -> ldcValue(cst)
1302+
}
1303+
)
1304+
)
12611305
push(assignment)
12621306
}
12631307

12641308
is Handle -> {
12651309
val assignment = nextRegister(CLASS_CLASS.typeName())
1266-
addInstruction(insnNode, JcRawAssignInst(
1267-
method,
1268-
assignment,
1269-
ldcValue(cst)
1270-
))
1310+
addInstruction(
1311+
insnNode, JcRawAssignInst(
1312+
method,
1313+
assignment,
1314+
ldcValue(cst)
1315+
)
1316+
)
12711317
push(assignment)
12721318
}
12731319

@@ -1292,17 +1338,19 @@ class RawInstListBuilder(
12921338

12931339
else -> {
12941340
val lookupAssignment = nextRegister(METHOD_HANDLES_LOOKUP_CLASS.typeName())
1295-
addInstruction(insnNode, JcRawAssignInst(
1296-
method,
1297-
lookupAssignment,
1298-
JcRawStaticCallExpr(
1299-
METHOD_HANDLES_CLASS.typeName(),
1300-
"lookup",
1301-
emptyList(),
1302-
METHOD_HANDLES_LOOKUP_CLASS.typeName(),
1303-
emptyList()
1341+
addInstruction(
1342+
insnNode, JcRawAssignInst(
1343+
method,
1344+
lookupAssignment,
1345+
JcRawStaticCallExpr(
1346+
METHOD_HANDLES_CLASS.typeName(),
1347+
"lookup",
1348+
emptyList(),
1349+
METHOD_HANDLES_LOOKUP_CLASS.typeName(),
1350+
emptyList()
1351+
)
13041352
)
1305-
))
1353+
)
13061354
JcRawStaticCallExpr(
13071355
methodHande.owner.typeName(),
13081356
methodHande.name,
@@ -1428,11 +1476,13 @@ class RawInstListBuilder(
14281476
Opcodes.ANEWARRAY -> {
14291477
val length = pop()
14301478
val assignment = nextRegister(type.asArray())
1431-
addInstruction(insnNode, JcRawAssignInst(
1432-
method,
1433-
assignment,
1434-
JcRawNewArrayExpr(type.asArray(), length)
1435-
))
1479+
addInstruction(
1480+
insnNode, JcRawAssignInst(
1481+
method,
1482+
assignment,
1483+
JcRawNewArrayExpr(type.asArray(), length)
1484+
)
1485+
)
14361486
push(assignment)
14371487
}
14381488

@@ -1444,11 +1494,13 @@ class RawInstListBuilder(
14441494

14451495
Opcodes.INSTANCEOF -> {
14461496
val assignment = nextRegister(PredefinedPrimitives.Boolean.typeName())
1447-
addInstruction(insnNode, JcRawAssignInst(
1448-
method,
1449-
assignment,
1450-
JcRawInstanceOfExpr(PredefinedPrimitives.Boolean.typeName(), pop(), type)
1451-
))
1497+
addInstruction(
1498+
insnNode, JcRawAssignInst(
1499+
method,
1500+
assignment,
1501+
JcRawInstanceOfExpr(PredefinedPrimitives.Boolean.typeName(), pop(), type)
1502+
)
1503+
)
14521504
push(assignment)
14531505
}
14541506

@@ -1468,6 +1520,7 @@ class RawInstListBuilder(
14681520
in Opcodes.ILOAD..Opcodes.ALOAD -> {
14691521
push(local(variable))
14701522
}
1523+
14711524
else -> error("Unknown opcode ${insnNode.opcode} in VarInsnNode")
14721525
}
14731526
}

0 commit comments

Comments
 (0)