Skip to content

Commit

Permalink
Fix increment return value, and support a few more situations
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Oct 16, 2024
1 parent 1b6abbb commit 671762a
Showing 1 changed file with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,30 @@ public static long increment(final VirtualFrame frame,
@Bind BytecodeNode bytecodeNode) {
try {
long currentValue = accessor.getLong(bytecodeNode, frame);
accessor.setLong(bytecodeNode, frame, Math.addExact(currentValue, incValue));
return currentValue;
long result = Math.addExact(currentValue, incValue);
accessor.setLong(bytecodeNode, frame, result);
return result;
} catch (UnexpectedResultException e) {
if (e.getResult() instanceof Long l) {
long result = Math.addExact(l, incValue);
accessor.setObject(bytecodeNode, frame, result);
return result;
}
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
}
}

@Specialization
public static double increment(final VirtualFrame frame,
final LocalAccessor accessor,
final double incValue,
@Bind BytecodeNode bytecodeNode) {
try {
double currentValue = accessor.getDouble(bytecodeNode, frame);
double result = currentValue + incValue;
accessor.setDouble(bytecodeNode, frame, result);
return result;
} catch (UnexpectedResultException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
Expand Down Expand Up @@ -666,10 +688,17 @@ public static long increment(final VirtualFrame frame,
MaterializedFrame ctx = ContextualNode.determineContext(frame, contextLevel);
try {
long currentValue = accessor.getLong(bytecodeNode, ctx);
accessor.setLong(bytecodeNode, ctx, Math.addExact(currentValue, incValue));
return currentValue;
long result = Math.addExact(currentValue, incValue);
accessor.setLong(bytecodeNode, ctx, result);
return result;
} catch (UnexpectedResultException e) {
CompilerDirectives.transferToInterpreter();

if (e.getResult() instanceof Long l) {
long result = Math.addExact(l, incValue);
accessor.setObject(bytecodeNode, ctx, result);
return result;
}
throw new RuntimeException(e);
}
}
Expand All @@ -686,8 +715,9 @@ public static long increment(final VirtualFrame frame,
@Bind BytecodeNode bytecodeNode) {
try {
long currentValue = accessor.getLong(bytecodeNode, frame);
accessor.setLong(bytecodeNode, frame, Math.addExact(currentValue, incValue));
return currentValue;
long result = Math.addExact(currentValue, incValue);
accessor.setLong(bytecodeNode, frame, result);
return result;
} catch (UnexpectedResultException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
Expand All @@ -710,8 +740,9 @@ public static long increment(final VirtualFrame frame,
MaterializedFrame ctx = ContextualNode.determineContext(frame, contextLevel);
try {
long currentValue = accessor.getLong(bytecodeNode, ctx);
accessor.setLong(bytecodeNode, ctx, Math.addExact(currentValue, incValue));
return currentValue;
long result = Math.addExact(currentValue, incValue);
accessor.setLong(bytecodeNode, ctx, result);
return result;
} catch (UnexpectedResultException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
Expand Down

0 comments on commit 671762a

Please sign in to comment.