Skip to content

Commit b5059ca

Browse files
committed
Added more tests and code cleanups
1 parent b5499b0 commit b5059ca

39 files changed

Lines changed: 392 additions & 383 deletions

src/main/java/de/mirkosertic/metair/ir/Add.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ public class Add extends Value {
66

77
Add(final ClassDesc type, final Value arg1, final Value arg2) {
88
super(type);
9+
10+
if (!arg1.type.equals(type)) {
11+
illegalArgument("Cannot add non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg1.type) + " for arg1");
12+
}
13+
if (!arg2.type.equals(type)) {
14+
illegalArgument("Cannot add non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg2.type) + " for arg2");
15+
}
16+
917
use(arg1, new ArgumentUse(0));
1018
use(arg2, new ArgumentUse(1));
1119
}

src/main/java/de/mirkosertic/metair/ir/ArrayLength.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public class ArrayLength extends Value {
66

77
ArrayLength(final Value array) {
88
super(ConstantDescs.CD_int);
9+
10+
if (!array.type.isArray()) {
11+
illegalArgument("Cannot get array length of non array of type " + TypeUtils.toString(array.type));
12+
}
13+
914
use(array, new ArgumentUse(0));
1015
}
1116

src/main/java/de/mirkosertic/metair/ir/BitOperation.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ public enum Operation {
1212

1313
BitOperation(final ClassDesc type, final Operation operation, final Value arg1, final Value arg2) {
1414
super(type);
15+
16+
if (!arg1.type.equals(type)) {
17+
illegalArgument("Cannot use non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg1.type) + " for bit operation " + operation + " on arg1");
18+
}
19+
if (!arg2.type.equals(type)) {
20+
illegalArgument("Cannot use non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg2.type) + " for bit operation " + operation + " on arg2");
21+
}
22+
1523
this.operation = operation;
1624
use(arg1, new ArgumentUse(0));
1725
use(arg2, new ArgumentUse(1));

src/main/java/de/mirkosertic/metair/ir/Convert.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class Convert extends Value {
88

99
Convert(final ClassDesc to, final Value arg1, final ClassDesc from) {
1010
super(to);
11+
12+
if (!arg1.type.equals(from)) {
13+
illegalArgument("Expected a value of type " + TypeUtils.toString(from) + " but got " + TypeUtils.toString(arg1.type));
14+
}
15+
1116
use(arg1, new ArgumentUse(0));
1217

1318
this.from = from;

src/main/java/de/mirkosertic/metair/ir/Div.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ public class Div extends Value {
66

77
Div(final ClassDesc type, final Value arg1, final Value arg2) {
88
super(type);
9+
10+
if (!arg1.type.equals(type)) {
11+
illegalArgument("Cannot divide non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg1.type) + " for arg1");
12+
}
13+
if (!arg2.type.equals(type)) {
14+
illegalArgument("Cannot divide non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg2.type) + " for arg2");
15+
}
16+
917
use(arg1, new ArgumentUse(0));
1018
use(arg2, new ArgumentUse(1));
1119
}

src/main/java/de/mirkosertic/metair/ir/GetField.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class GetField extends Value {
99

1010
GetField(final ClassDesc owner, final ClassDesc fieldType, final String fieldName, final Value source) {
1111
super(fieldType);
12+
13+
if (source.type.isPrimitive() || source.type.isArray()) {
14+
illegalArgument("Cannot get field " + fieldName + " from non object source " + TypeUtils.toString(source.type));
15+
}
16+
1217
this.owner = owner;
1318
this.fieldName = fieldName;
1419

src/main/java/de/mirkosertic/metair/ir/MethodAnalyzer.java

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,6 @@ protected void parse_IINC(final int slot, final int constant, final Frame frame)
584584
if (value == null) {
585585
illegalState("No local value for slot " + slot);
586586
}
587-
if (!ConstantDescs.CD_int.equals(value.type)) {
588-
illegalState("IINC expects an int value for slot " + slot + ", got " + TypeUtils.toString(value.type));
589-
}
590587
frame.out.setLocal(slot, new Add(ConstantDescs.CD_int, value, ir.definePrimitiveInt(constant)));
591588
}
592589
@@ -836,9 +833,6 @@ protected void visitThrowInstruction(final Frame frame) {
836833
assertMinimumStackSize(outgoing, 1);
837834
838835
final Value v = outgoing.pop();
839-
if (v.type.isPrimitive()) {
840-
illegalState("Cannot throw a primitive value of type " + TypeUtils.toString(v.type));
841-
}
842836
final Throw t = new Throw(v);
843837
outgoing.control = outgoing.control.controlFlowsTo(t, ControlType.FORWARD);
844838
outgoing.memory = outgoing.memory.memoryFlowsTo(t);
@@ -850,10 +844,6 @@ protected void visitNewPrimitiveArray(final TypeKind kind, final Frame frame) {
850844
assertMinimumStackSize(outgoing, 1);
851845
852846
final Value length = frame.out.pop();
853-
if (!ConstantDescs.CD_int.equals(length.type)) {
854-
illegalState("Array length must be int, but was " + TypeUtils.toString(length.type));
855-
}
856-
857847
final ClassDesc type;
858848
switch (kind) {
859849
case BYTE -> type = ConstantDescs.CD_byte.arrayType();
@@ -915,11 +905,7 @@ protected void visitNewObjectArray(final ClassDesc componentType, final Frame fr
915905
final Status outgoing = frame.copyIncomingToOutgoing();
916906
assertMinimumStackSize(outgoing, 1);
917907
918-
final Value length = outgoing.pop();
919-
if (!length.type.equals(ConstantDescs.CD_int)) {
920-
illegalState("Array length must be int, but was " + TypeUtils.toString(length.type));
921-
}
922-
final NewArray newArray = new NewArray(componentType, length);
908+
final NewArray newArray = new NewArray(componentType, outgoing.pop());
923909
outgoing.push(newArray);
924910
outgoing.memory = outgoing.memory.memoryFlowsTo(newArray);
925911
frame.entryPoint = newArray;
@@ -954,11 +940,7 @@ protected void visitNewMultiArray(final ClassDesc arrayType, final int dimension
954940
955941
final List<Value> dimensions = new ArrayList<>();
956942
for (int i = 0; i < dimensionSize; i++) {
957-
final Value v = outgoing.pop();
958-
if (!v.type.equals(ConstantDescs.CD_int)) {
959-
illegalState("Array dimension must be int, but was " + TypeUtils.toString(v.type) + " for dimension " + (i + 1));
960-
}
961-
dimensions.add(v);
943+
dimensions.add(outgoing.stack.pop());
962944
}
963945
final NewMultiArray newMultiArray = new NewMultiArray(arrayType, dimensions);
964946
outgoing.push(newMultiArray);
@@ -1319,9 +1301,6 @@ private void parse_GETFIELD(final ClassDesc owner, final ClassDesc fieldType, fi
13191301
assertMinimumStackSize(outgoing, 1);
13201302

13211303
final Value v = outgoing.pop();
1322-
if (v.type.isPrimitive() || v.type.isArray()) {
1323-
illegalState("Cannot load field " + fieldName + " from non object value " + TypeUtils.toString(v.type));
1324-
}
13251304
final GetField get = new GetField(owner, fieldType, fieldName, v);
13261305
outgoing.push(get);
13271306

@@ -1334,9 +1313,6 @@ private void parse_PUTFIELD(final ClassDesc owner, final ClassDesc fieldType, fi
13341313

13351314
final Value v = outgoing.pop();
13361315
final Value target = outgoing.pop();
1337-
if (target.type.isPrimitive() || target.type.isArray()) {
1338-
illegalState("Cannot put field " + fieldName + " on non object value " + TypeUtils.toString(v.type));
1339-
}
13401316

13411317
final PutField put = new PutField(owner, fieldType, fieldName, target, v);
13421318

@@ -1407,12 +1383,6 @@ private void parse_ARETURN(final Frame frame) {
14071383

14081384
final Value v = outgoing.pop();
14091385

1410-
if (v.type.isPrimitive()) {
1411-
illegalState("Expecting type " + TypeUtils.toString(methodTypeDesc.returnType()) + " on stack, got " + TypeUtils.toString(v.type));
1412-
}
1413-
1414-
// TODO: Maybe we should check downcastability according to the type hierarchy
1415-
14161386
final ReturnValue next = new ReturnValue(v.type, v);
14171387
outgoing.control = outgoing.control.controlFlowsTo(next, ControlType.FORWARD);
14181388
outgoing.memory = outgoing.memory.memoryFlowsTo(next);
@@ -1423,9 +1393,6 @@ private void parse_RETURN_X(final Frame frame, final ClassDesc type) {
14231393
assertMinimumStackSize(outgoing, 1);
14241394

14251395
final Value v = outgoing.pop();
1426-
if (!v.type.equals(type)) {
1427-
illegalState("Expecting type " + TypeUtils.toString(type) + " on stack, got " + TypeUtils.toString(v.type));
1428-
}
14291396

14301397
final ReturnValue next = new ReturnValue(type, v);
14311398
outgoing.control = outgoing.control.controlFlowsTo(next, ControlType.FORWARD);
@@ -1630,34 +1597,16 @@ private void parse_SWAP(final Frame frame) {
16301597
}
16311598

16321599
private void parse_ADD_X(final Frame frame, final Value value1, final Value value2, final ClassDesc desc) {
1633-
if (!value1.type.equals(desc)) {
1634-
illegalState("Cannot add non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " as value1");
1635-
}
1636-
if (!value2.type.equals(desc)) {
1637-
illegalState("Cannot add non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value2.type) + " as value2");
1638-
}
16391600
final Add add = new Add(desc, value1, value2);
16401601
frame.out.push(add);
16411602
}
16421603

16431604
private void parse_SUB_X(final Frame frame, final Value value1, final Value value2, final ClassDesc desc) {
1644-
if (!value1.type.equals(desc)) {
1645-
illegalState("Cannot subtract non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " as value1");
1646-
}
1647-
if (!value2.type.equals(desc)) {
1648-
illegalState("Cannot subtract non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value2.type) + " as value2");
1649-
}
16501605
final Sub sub = new Sub(desc, value1, value2);
16511606
frame.out.push(sub);
16521607
}
16531608

16541609
private void parse_MUL_X(final Frame frame, final Value value1, final Value value2, final ClassDesc desc) {
1655-
if (!value1.type.equals(desc)) {
1656-
illegalState("Cannot multiplicate non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " for value1");
1657-
}
1658-
if (!value2.type.equals(desc)) {
1659-
illegalState("Cannot multiplicate non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value2.type) + " for value2");
1660-
}
16611610
final Mul mul = new Mul(desc, value1, value2);
16621611
frame.out.push(mul);
16631612
}
@@ -1666,29 +1615,17 @@ private void parse_ARRAYLENGTH(final Frame frame) {
16661615
final Status outgoing = frame.copyIncomingToOutgoing();
16671616

16681617
final Value array = outgoing.pop();
1669-
if (!array.type.isArray()) {
1670-
illegalState("Cannot get array length of non array value " + array);
1671-
}
16721618
outgoing.push(new ArrayLength(array));
16731619
}
16741620

16751621
private void parse_NEG_X(final Frame frame, final ClassDesc desc) {
16761622
final Status outgoing = frame.copyIncomingToOutgoing();
16771623

16781624
final Value a = outgoing.pop();
1679-
if (!a.type.equals(desc)) {
1680-
illegalState("Cannot negate non " + TypeUtils.toString(desc) + " value " + a + " of type " + TypeUtils.toString(a.type));
1681-
}
16821625
outgoing.push(new Negate(desc, a));
16831626
}
16841627

16851628
private void parse_DIV_X(final Frame frame, final Value value1, final Value value2, final ClassDesc desc) {
1686-
if (!value1.type.equals(desc)) {
1687-
illegalState("Cannot divide non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " for value1");
1688-
}
1689-
if (!value2.type.equals(desc)) {
1690-
illegalState("Cannot divide non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value2.type) + " for value2");
1691-
}
16921629
final Div div = new Div(desc, value1, value2);
16931630

16941631
final Status outgoing = frame.out;
@@ -1698,39 +1635,18 @@ private void parse_DIV_X(final Frame frame, final Value value1, final Value valu
16981635
}
16991636

17001637
private void parse_REM_X(final Frame frame, final Value value1, final Value value2, final ClassDesc desc) {
1701-
if (!value1.type.equals(desc)) {
1702-
illegalState("Cannot make remainder on non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " for value1");
1703-
}
1704-
if (!value2.type.equals(desc)) {
1705-
illegalState("Cannot make remainder on non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value2.type) + " for value2");
1706-
}
17071638
final Rem rem = new Rem(desc, value1, value2);
17081639
final Status outgoing = frame.out;
17091640
outgoing.control = outgoing.control.controlFlowsTo(rem, ControlType.FORWARD);
17101641
outgoing.push(rem);
17111642
}
17121643

17131644
private void parse_BITOPERATION_X(final Frame frame, final Value value1, final Value value2, final ClassDesc desc, final BitOperation.Operation operation) {
1714-
if (!value1.type.equals(desc)) {
1715-
illegalState("Cannot use non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " for bit operation " + operation + " on value1");
1716-
}
1717-
if (!value2.type.equals(desc)) {
1718-
illegalState("Cannot use non " + TypeUtils.toString(desc) + " value " + TypeUtils.toString(value1.type) + " for bit operation " + operation + " on value2");
1719-
}
17201645
final BitOperation rem = new BitOperation(desc, operation, value1, value2);
17211646
frame.out.push(rem);
17221647
}
17231648

17241649
protected void parse_NUMERICCOMPARE_X(final Frame frame, final Value value1, final Value value2, final ClassDesc compareType, final NumericCompare.Mode mode) {
1725-
1726-
if (!value1.type.equals(compareType)) {
1727-
illegalState("Cannot compare non " + TypeUtils.toString(compareType) + " value " + TypeUtils.toString(value1.type) + " for value1");
1728-
}
1729-
1730-
if (!value2.type.equals(compareType)) {
1731-
illegalState("Cannot compare non " + TypeUtils.toString(compareType) + " value " + TypeUtils.toString(value2.type) + " for value2");
1732-
}
1733-
17341650
final NumericCompare compare = new NumericCompare(mode, compareType, value1, value2);
17351651
frame.out.push(compare);
17361652
}
@@ -1740,9 +1656,6 @@ private void parse_MONITORENTER(final Frame frame) {
17401656
assertMinimumStackSize(outgoing, 1);
17411657

17421658
final Value v = outgoing.pop();
1743-
if (v.type.isPrimitive()) {
1744-
illegalState("Expecting non primitive type for monitorenter on stack, got " + TypeUtils.toString(v.type));
1745-
}
17461659
outgoing.control = outgoing.control.controlFlowsTo(new MonitorEnter(v), ControlType.FORWARD);
17471660
}
17481661

@@ -1751,9 +1664,6 @@ private void parse_MONITOREXIT(final Frame frame) {
17511664
assertMinimumStackSize(outgoing, 1);
17521665

17531666
final Value v = outgoing.pop();
1754-
if (v.type.isPrimitive()) {
1755-
illegalState("Expecting non primitive type for monitorexit on stack, got " + TypeUtils.toString(v.type));
1756-
}
17571667
outgoing.control = outgoing.control.controlFlowsTo(new MonitorExit(v), ControlType.FORWARD);
17581668
}
17591669

@@ -1816,11 +1726,7 @@ protected void parse_AALOAD(final ArrayLoadInstruction node, final Frame frame)
18161726
private void parse_CONVERT_X(final Frame frame, final ClassDesc from, final ClassDesc to) {
18171727
final Status outgoing = frame.copyIncomingToOutgoing();
18181728

1819-
final Value value = outgoing.pop();
1820-
if (!value.type.equals(from)) {
1821-
illegalState("Expected a value of type " + from + " but got " + value.type);
1822-
}
1823-
outgoing.push(new Convert(to, value, from));
1729+
outgoing.push(new Convert(to, outgoing.pop(), from));
18241730
}
18251731

18261732
public Method ir() {

src/main/java/de/mirkosertic/metair/ir/MonitorEnter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
public class MonitorEnter extends Node {
44

55
MonitorEnter(final Value object) {
6+
7+
if (object.type.isPrimitive()) {
8+
illegalArgument("Expecting non primitive type for monitorenter on stack, got " + TypeUtils.toString(object.type));
9+
}
10+
611
use(object, new ArgumentUse(0));
712
}
813

src/main/java/de/mirkosertic/metair/ir/MonitorExit.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
public class MonitorExit extends Node {
44

55
MonitorExit(final Value object) {
6+
7+
if (object.type.isPrimitive()) {
8+
illegalArgument("Expecting non primitive type for monitorexit on stack, got " + TypeUtils.toString(object.type));
9+
}
10+
611
use(object, new ArgumentUse(0));
712
}
813

src/main/java/de/mirkosertic/metair/ir/Mul.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ public class Mul extends Value {
66

77
Mul(final ClassDesc type, final Value arg1, final Value arg2) {
88
super(type);
9+
10+
if (!arg1.type.equals(type)) {
11+
illegalArgument("Cannot multiply non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg1.type) + " for arg1");
12+
}
13+
if (!arg2.type.equals(type)) {
14+
illegalArgument("Cannot multiply non " + TypeUtils.toString(type) + " value " + TypeUtils.toString(arg2.type) + " for arg2");
15+
}
16+
917
use(arg1, new ArgumentUse(0));
1018
use(arg2, new ArgumentUse(1));
1119
}

0 commit comments

Comments
 (0)