Skip to content

Commit

Permalink
Lazily define object parameter constants which are needed by the Stac…
Browse files Browse the repository at this point in the history
…kMapTable attribute, in case the attribute isn't needed at all.
  • Loading branch information
broneill committed Aug 28, 2024
1 parent 88281eb commit 7049ba8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v2.5.11
-------
* Added a variable decrement method.
* Avoid generating a conversion instruction when comparing against a constant primitive value.
* Reduce constant pool pollution when the StackMapTable attribute isn't needed.

v2.5.10 (2024-08-07)
-------
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/org/cojen/maker/StackMapTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@
* @author Brian S O'Neill
*/
class StackMapTable extends Attribute {
private final Frame mInitFrame;
private Frame mFirstFrame, mLastFrame;
private int mNumFrames;
private BytesOut mFinished;

/**
* @param initCodes can be null
*/
StackMapTable(ConstantPool cp, int[] initCodes) {
StackMapTable(ConstantPool cp) {
super(cp); // set the name later; see finish()
mInitFrame = new Frame(Integer.MIN_VALUE, initCodes, null);
}

/**
Expand Down Expand Up @@ -80,19 +78,34 @@ void reset() {
/**
* @return false if table is empty and should not be written
*/
boolean finish() {
boolean finish(TheMethodMaker.ParamVar[] params) {
if (mFirstFrame == null) {
return false;
}

// Lazily set the name such that it's added to the ConstantPool only when needed.
name("StackMapTable");

// Make the initial frame to represent the method parameters.
Frame prev;
{
int[] initCodes;
if (params.length == 0) {
initCodes = null;
} else {
initCodes = new int[params.length];
for (int i=0; i<initCodes.length; i++) {
initCodes[i] = params[i].smCodeInit();
}
}

prev = new Frame(Integer.MIN_VALUE, initCodes, null);
}

var out = new BytesOut(null, mNumFrames * 4);

try {
out.writeShort(mNumFrames);
Frame prev = mInitFrame;
Frame frame = mFirstFrame;
do {
frame.writeTo(prev, out);
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/org/cojen/maker/TheMethodMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,9 @@ void doFinish() {

mVars = varList.toArray(new LocalVar[varList.size()]);

// Prepare the StackMapTable.
{
Arrays.sort(mVars); // sort by slot

int[] initCodes;
if (mParams.length == 0) {
initCodes = null;
} else {
initCodes = new int[mParams.length];
for (int i=0; i<initCodes.length; i++) {
initCodes[i] = mParams[i].smCode();
}
}
Arrays.sort(mVars); // sort by slot, as required by Lab.appendTo

mStackMapTable = new StackMapTable(mConstants, initCodes);
}
mStackMapTable = new StackMapTable(mConstants);

mCode = new byte[Math.min(MAX_CODE_LENGTH, opCount * 2)];
mStack = new LocalVar[8];
Expand Down Expand Up @@ -257,7 +244,6 @@ void doFinish() {
new Flow(varList, varUsage).run(mFirstOp);
}

mParams = null;
mFirstOp = null;
mLastOp = null;
mReturnLabel = null;
Expand All @@ -277,11 +263,13 @@ void doFinish() {

mExceptionHandlers = null;

if (mStackMapTable.finish()) {
if (mStackMapTable.finish(mParams)) {
codeAttr.addAttribute(mStackMapTable);
mStackMapTable = null;
}

mParams = null;

if (mLineNumberTable != null && mLineNumberTable.finish(mCodeLen)) {
codeAttr.addAttribute(mLineNumberTable);
}
Expand Down Expand Up @@ -5378,6 +5366,13 @@ class ParamVar extends LocalVar {
mIndex = index;
}

/**
* Needed for StackMapTable. Returns the SM code at the start of the method.
*/
int smCodeInit() {
return smCode();
}

@Override
public LocalVar name(String name) {
super.name(name);
Expand Down Expand Up @@ -5560,6 +5555,11 @@ int smCode() {
return mSmCode;
}

@Override
int smCodeInit() {
return SM_UNINIT_THIS;
}

void ready() {
if (mSmCode != SM_UNINIT_THIS) {
throw finishFail("Super or this constructor invoked multiple times");
Expand Down

0 comments on commit 7049ba8

Please sign in to comment.