Skip to content

Commit

Permalink
Remove BytecodeLocal argument from TryCatch/FinallyTryCatch
Browse files Browse the repository at this point in the history
  • Loading branch information
DSouzaM committed Jul 5, 2024
1 parent 4b9c74e commit c4c7282
Showing 1 changed file with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2263,8 +2263,7 @@ public void emitYieldFrom(Runnable generatorOrCoroutineProducer) {
b.beginBlock();
BytecodeLabel loopEnd = b.createLabel();
// Step 2: yield yieldValue to the caller
BytecodeLocal thrownException = b.createLocal();
b.beginTryCatch(thrownException);
b.beginTryCatch();

// try clause: yield
b.beginStoreLocal(sentValue);
Expand All @@ -2275,7 +2274,7 @@ public void emitYieldFrom(Runnable generatorOrCoroutineProducer) {
b.beginIfThenElse();
b.beginYieldFromThrow(yieldValue, returnValue);
b.emitLoadLocal(generator);
b.emitLoadLocal(thrownException);
b.emitLoadException();
b.endYieldFromThrow();

// StopIteration was raised; go to the end.
Expand Down Expand Up @@ -4043,9 +4042,7 @@ public Void visit(StmtTy.Try node) {
* reraise uncaught_ex
* }
*/
BytecodeLocal uncaughtException = b.createLocal();
BytecodeLocal handlerException = b.createLocal();
b.beginFinallyTryCatch(uncaughtException, () -> {
b.beginFinallyTryCatch(() -> {
b.beginBlock(); // finally
visitSequence(node.finalBody);
b.endBlock();
Expand All @@ -4056,32 +4053,32 @@ public Void visit(StmtTy.Try node) {
b.beginBlock(); // catch
BytecodeLocal savedException = b.createLocal();
emitSaveCurrentException(savedException);
emitSetCurrentException(uncaughtException);
emitSetCurrentException();
// Mark this location for the stack trace.
b.beginMarkExceptionAsCaught();
b.emitLoadLocal(uncaughtException);
b.emitLoadException();
b.endMarkExceptionAsCaught();

b.beginFinallyTryCatch(handlerException, () -> emitSetCurrentException(savedException));
b.beginFinallyTryCatch(() -> emitRestoreCurrentException(savedException));
b.beginBlock(); // try
visitSequence(node.finalBody);
b.endBlock(); // try

b.beginBlock(); // catch
emitSetCurrentException(savedException);
emitRestoreCurrentException(savedException);

b.beginMarkExceptionAsCaught();
b.emitLoadLocal(handlerException);
b.emitLoadException();
b.endMarkExceptionAsCaught();

b.beginReraise();
b.emitLoadLocal(handlerException);
b.emitLoadException();
b.endReraise();
b.endBlock(); // catch
b.endFinallyTryCatch();

b.beginReraise();
b.emitLoadLocal(uncaughtException);
b.emitLoadException();
b.endReraise();
b.endBlock(); // catch
b.endFinallyTryCatch();
Expand Down Expand Up @@ -4130,11 +4127,11 @@ private void emitTryExceptElse(StmtTy.Try node) {
* handler_1_body
* } finally {
* unbind handler_1_name
* } catch handler_ex {
* } catch handler_1_ex {
* unbind handler_1_name
* // Freeze the bci before it gets rethrown.
* markCaught(handler_ex)
* throw handler_ex
* throw handler_1_ex
* }
* goto afterElse
* }
Expand All @@ -4160,26 +4157,24 @@ private void emitTryExceptElse(StmtTy.Try node) {
*/
b.beginBlock(); // outermost block

BytecodeLocal exception = b.createLocal();
BytecodeLocal savedException = b.createLocal();
BytecodeLabel afterElse = b.createLabel();

b.beginTryCatch(exception);
b.beginTryCatch();

b.beginBlock(); // try
visitSequence(node.body);
b.endBlock(); // try

b.beginBlock(); // catch
emitSaveCurrentException(savedException);
emitSetCurrentException(exception);
emitSetCurrentException();
// Mark this location for the stack trace.
b.beginMarkExceptionAsCaught();
b.emitLoadLocal(exception);
b.emitLoadException(); // ex
b.endMarkExceptionAsCaught();

BytecodeLocal handlerException = b.createLocal();
b.beginFinallyTryCatch(handlerException, () -> emitSetCurrentException(savedException));
b.beginFinallyTryCatch(() -> emitRestoreCurrentException(savedException));
b.beginBlock(); // try
SourceRange bareExceptRange = null;
for (ExceptHandlerTy h : node.handlers) {
Expand All @@ -4192,7 +4187,7 @@ private void emitTryExceptElse(StmtTy.Try node) {
if (handler.type != null) {
b.beginIfThen();
b.beginExceptMatch();
b.emitLoadLocal(exception);
b.emitLoadException(); // ex
handler.type.accept(this);
b.endExceptMatch();
} else {
Expand All @@ -4205,11 +4200,11 @@ private void emitTryExceptElse(StmtTy.Try node) {
// Assign exception to handler name.
beginStoreLocal(handler.name, b);
b.beginUnwrapException();
b.emitLoadLocal(exception);
b.emitLoadException(); // ex
b.endUnwrapException();
endStoreLocal(handler.name, b);

b.beginFinallyTryCatch(handlerException, () -> emitUnbindHandlerVariable(handler));
b.beginFinallyTryCatch(() -> emitUnbindHandlerVariable(handler));
b.beginBlock(); // try
visitSequence(handler.body);
b.endBlock(); // try
Expand All @@ -4218,11 +4213,11 @@ private void emitTryExceptElse(StmtTy.Try node) {
emitUnbindHandlerVariable(handler);

b.beginMarkExceptionAsCaught();
b.emitLoadLocal(handlerException);
b.emitLoadException(); // handler_i_ex
b.endMarkExceptionAsCaught();

b.beginThrow();
b.emitLoadLocal(handlerException);
b.emitLoadException(); // handler_i_ex
b.endThrow();
b.endBlock(); // catch
b.endFinallyTryCatch();
Expand All @@ -4245,14 +4240,14 @@ private void emitTryExceptElse(StmtTy.Try node) {
b.endBlock(); // try

b.beginBlock(); // catch
emitSetCurrentException(savedException);
emitRestoreCurrentException(savedException);

b.beginMarkExceptionAsCaught();
b.emitLoadLocal(handlerException);
b.emitLoadException(); // handler_ex
b.endMarkExceptionAsCaught();

b.beginReraise();
b.emitLoadLocal(handlerException);
b.emitLoadException(); // handler_ex
b.endReraise();
b.endBlock(); // catch
b.endFinallyTryCatch();
Expand All @@ -4265,7 +4260,7 @@ private void emitTryExceptElse(StmtTy.Try node) {
*/
if (bareExceptRange == null) {
b.beginReraise();
b.emitLoadLocal(exception);
b.emitLoadException(); // ex
b.endReraise();
}

Expand Down Expand Up @@ -4295,9 +4290,15 @@ private void emitSaveCurrentException(BytecodeLocal savedException) {
b.endStoreLocal();
}

private void emitSetCurrentException(BytecodeLocal newException) {
private void emitSetCurrentException() {
b.beginSetCurrentException();
b.emitLoadLocal(newException);
b.emitLoadException();
b.endSetCurrentException();
}

private void emitRestoreCurrentException(BytecodeLocal savedException) {
b.beginSetCurrentException();
b.emitLoadLocal(savedException);
b.endSetCurrentException();
}

Expand Down Expand Up @@ -4410,7 +4411,6 @@ private void visitWithRecurse(WithItemTy[] items, int index, StmtTy[] body, bool
b.endContextManagerEnter();
}

BytecodeLocal ex = b.createLocal();
Runnable finallyHandler;
if (async) {
finallyHandler = () -> emitAwait(() -> {
Expand All @@ -4430,7 +4430,7 @@ private void visitWithRecurse(WithItemTy[] items, int index, StmtTy[] body, bool
b.endContextManagerExit();
};
}
b.beginFinallyTryCatch(ex, finallyHandler);
b.beginFinallyTryCatch(finallyHandler);
b.beginBlock(); // try
if (item.optionalVars != null) {
item.optionalVars.accept(new StoreVisitor(() -> b.emitLoadLocal(value)));
Expand All @@ -4446,17 +4446,17 @@ private void visitWithRecurse(WithItemTy[] items, int index, StmtTy[] body, bool

// Mark this location for the stack trace.
b.beginMarkExceptionAsCaught();
b.emitLoadLocal(ex);
b.emitLoadException();
b.endMarkExceptionAsCaught();

// exceptional exit
if (async) {
// call, await, and handle result of __aexit__
b.beginAsyncContextManagerExit();
b.emitLoadLocal(ex);
b.emitLoadException();
emitAwait(() -> {
b.beginAsyncContextManagerCallExit();
b.emitLoadLocal(ex);
b.emitLoadException();
b.emitLoadLocal(exit);
b.emitLoadLocal(contextManager);
b.endAsyncContextManagerCallExit();
Expand All @@ -4465,7 +4465,7 @@ private void visitWithRecurse(WithItemTy[] items, int index, StmtTy[] body, bool
} else {
// call __exit__
b.beginContextManagerExit();
b.emitLoadLocal(ex);
b.emitLoadException();
b.emitLoadLocal(exit);
b.emitLoadLocal(contextManager);
b.endContextManagerExit();
Expand Down

0 comments on commit c4c7282

Please sign in to comment.