Skip to content

Commit

Permalink
Bug 885514: Don't emit retsub if all paths fall through r=jandem
Browse files Browse the repository at this point in the history
This means that we don't have to implement retsub in Warp to handle simple cases.

Differential Revision: https://phabricator.services.mozilla.com/D142037
  • Loading branch information
iainireland committed Mar 30, 2022
1 parent 308be2f commit 5757da4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion js/src/frontend/BytecodeControlStructures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ bool LoopControl::emitLoopEnd(BytecodeEmitter* bce, JSOp op,
}

TryFinallyControl::TryFinallyControl(BytecodeEmitter* bce, StatementKind kind)
: NestableControl(bce, kind), emittingSubroutine_(false) {
: NestableControl(bce, kind) {
MOZ_ASSERT(is<TryFinallyControl>());
}
6 changes: 5 additions & 1 deletion js/src/frontend/BytecodeControlStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ inline bool NestableControl::is<LoopControl>() const {
}

class TryFinallyControl : public NestableControl {
bool emittingSubroutine_;
bool emittingSubroutine_ = false;
bool hasNonLocalJumps_ = false;

public:
// Offset of the last jump to this `finally`.
Expand All @@ -155,6 +156,9 @@ class TryFinallyControl : public NestableControl {
void setEmittingSubroutine() { emittingSubroutine_ = true; }

bool emittingSubroutine() const { return emittingSubroutine_; }

void setHasNonLocalJumps() { hasNonLocalJumps_ = true; }
bool hasNonLocalJumps() const { return hasNonLocalJumps_; }
};
template <>
inline bool NestableControl::is<TryFinallyControl>() const {
Expand Down
1 change: 1 addition & 0 deletions js/src/frontend/BytecodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ bool NonLocalExitControl::prepareForNonLocalJump(NestableControl* target) {
// [stack] ...
return false;
}
finallyControl.setHasNonLocalJumps();
}
break;
}
Expand Down
13 changes: 11 additions & 2 deletions js/src/frontend/TryEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,17 @@ bool TryEmitter::emitFinallyEnd() {
return false;
}

if (!bce_->emit1(JSOp::Retsub)) {
return false;
if (controlInfo_ && controlInfo_->hasNonLocalJumps()) {
if (!bce_->emit1(JSOp::Retsub)) {
return false;
}
} else {
// If there are no non-local jumps, then the only possible jump target
// is the code immediately following this finally block. Instead of
// emitting a retsub, we can simply pop the resume index and fall through.
if (!bce_->emit1(JSOp::Pop)) {
return false;
}
}

if (!ifThrowing.emitEnd()) {
Expand Down

0 comments on commit 5757da4

Please sign in to comment.