Skip to content

Commit

Permalink
feat(objectionary#540): add more opcode stack changes
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Oct 7, 2024
1 parent 79098f8 commit 76d93be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,25 +369,29 @@ public int stack() {
return -2;
case RETURN:
return 0;
// Get and put field
case IINC:
return 0;
case LDC:
final Type ldcType = Type.getType(this.args.get(0).getClass());
if (ldcType == Type.DOUBLE_TYPE || ldcType == Type.LONG_TYPE) {
return 2;
} else {
return 1;
}
case GETSTATIC:
final Type type = Type.getType(String.valueOf(this.args.get(2)));
if (type == Type.DOUBLE_TYPE || type == Type.LONG_TYPE) {
return 2;
} else {
return 1;
}
// Needs field descriptor to determine stack change
// Assuming we have a method to get field size (1 or 2 slots)
// For example:
// return getFieldSize(fieldDescriptor);
// For now, we'll assume it's 1 slot
// return 1;
case PUTSTATIC:
// Needs field descriptor
// Assuming it's 1 slot
return -1;

final Type putType = Type.getType(String.valueOf(this.args.get(2)));
if (putType == Type.DOUBLE_TYPE || putType == Type.LONG_TYPE) {
return -2;
} else {
return -1;
}
case GETFIELD:
// Pops objectref (1 slot), pushes field value
// Net change depends on field size
Expand Down Expand Up @@ -462,7 +466,6 @@ public int stack() {
case IFNONNULL:
// Pops objectref: -1
return -1;

// Default case
default:
// For unrecognized opcodes, return 0 or throw an exception
Expand Down
45 changes: 42 additions & 3 deletions src/test/resources/maxs/Maxs.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* SOFTWARE.
*/

import java.lang.Math;
import javax.validation.constraints.Max;

/**
* This class contains many different methods with different number of local
* variables and stack elements.
Expand All @@ -31,8 +34,10 @@
*/
public class Maxs {

public static final double DOUBLE_CONSTANT = 3.14d;
public static final float FLOAT_CONSTANT = 3.14f;
public static double DOUBLE_CONSTANT = 3.14d;
public static float FLOAT_CONSTANT = 3.14f;
private int someIntField = 42;
private long someLongField = 42L;

public static void main(String[] args) {
System.out.println(fortyTwo());
Expand Down Expand Up @@ -295,7 +300,7 @@ public String innerClassMethod() {
* This method has 3 local variables (including 'this') and 4 stack elements.
*/
public double loadDoubleConstant() {
double d = Maxs.DOUBLE_CONSTANT;
double d = Math.PI;
return d * 3d;
}

Expand All @@ -308,6 +313,40 @@ public float loadFloatConstant() {
return f * 3f;
}

/**
* Put double constant.
* This method has 1 local variables (including 'this') and 1 stack elements.
*/
public long putDoubleConstant() {
Maxs.DOUBLE_CONSTANT = 42L;
return 3L;
}

/**
* Put float constant.
* This method has 1 local variables (including 'this') and 1 stack elements.
*/
public int putFloatConstant() {
Maxs.FLOAT_CONSTANT = 42f;
return 3;
}

/**
* Loads int field
* This method has 1 local variables (including 'this') and 1 stack elements.
*/
public int loadIntField() {
return this.someIntField;
}

/**
* Loads long field
* This method has 1 local variables (including 'this') and 2 stack elements.
*/
public long loadLongField() {
return this.someLongField;
}

// Inner class to add complexity
private class Inner {
public String partOne() {
Expand Down

0 comments on commit 76d93be

Please sign in to comment.