| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "frontend/TryEmitter.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 10 |
|
| 11 |
#include "frontend/BytecodeEmitter.h" // BytecodeEmitter |
| 12 |
#include "frontend/IfEmitter.h" // BytecodeEmitter |
| 13 |
#include "frontend/SharedContext.h" // StatementKind |
| 14 |
#include "vm/Opcodes.h" // JSOp |
| 15 |
|
| 16 |
using namespace js; |
| 17 |
using namespace js::frontend; |
| 18 |
|
| 19 |
using mozilla::Maybe; |
| 20 |
|
| 21 |
TryEmitter::TryEmitter(BytecodeEmitter* bce, Kind kind, ControlKind controlKind) |
| 22 |
: bce_(bce), |
| 23 |
kind_(kind), |
| 24 |
controlKind_(controlKind), |
| 25 |
depth_(0), |
| 26 |
tryOpOffset_(0) |
| 27 |
#ifdef DEBUG |
| 28 |
, |
| 29 |
state_(State::Start) |
| 30 |
#endif |
| 31 |
{ |
| 32 |
MOZ_ASSERT_IF(controlKind_ == ControlKind::Disposal, |
| 33 |
kind_ == Kind::TryFinally); |
| 34 |
|
| 35 |
if (requiresControlInfo()) { |
| 36 |
controlInfo_.emplace( |
| 37 |
bce_, hasFinally() ? StatementKind::Finally : StatementKind::Try); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#ifdef DEBUG |
| 42 |
bool TryEmitter::hasControlInfo() { return controlInfo_.isSome(); } |
| 43 |
#endif |
| 44 |
|
| 45 |
bool TryEmitter::emitTry() { |
| 46 |
MOZ_ASSERT(state_ == State::Start); |
| 47 |
|
| 48 |
// Since an exception can be thrown at any place inside the try block, |
| 49 |
// we need to restore the stack and the scope chain before we transfer |
| 50 |
// the control to the exception handler. |
| 51 |
// |
| 52 |
// For that we store in a try note associated with the catch or |
| 53 |
// finally block the stack depth upon the try entry. The interpreter |
| 54 |
// uses this depth to properly unwind the stack and the scope chain. |
| 55 |
depth_ = bce_->bytecodeSection().stackDepth(); |
| 56 |
|
| 57 |
tryOpOffset_ = bce_->bytecodeSection().offset(); |
| 58 |
if (!bce_->emit1(JSOp::Try)) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
#ifdef DEBUG |
| 63 |
state_ = State::Try; |
| 64 |
#endif |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
bool TryEmitter::emitJumpToFinallyWithFallthrough() { |
| 69 |
uint32_t stackDepthForNextBlock = bce_->bytecodeSection().stackDepth(); |
| 70 |
|
| 71 |
// The fallthrough continuation is special-cased with index 0. |
| 72 |
uint32_t idx = TryFinallyControl::SpecialContinuations::Fallthrough; |
| 73 |
if (!bce_->emitJumpToFinally(&controlInfo_->finallyJumps_, idx)) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
// Reset the stack depth for the following catch or finally block. |
| 78 |
bce_->bytecodeSection().setStackDepth(stackDepthForNextBlock); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
bool TryEmitter::emitTryEnd() { |
| 83 |
MOZ_ASSERT(state_ == State::Try); |
| 84 |
MOZ_ASSERT(depth_ == bce_->bytecodeSection().stackDepth()); |
| 85 |
|
| 86 |
if (hasFinally() && controlInfo_) { |
| 87 |
if (!emitJumpToFinallyWithFallthrough()) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
} else { |
| 91 |
// Emit jump over catch |
| 92 |
if (!bce_->emitJump(JSOp::Goto, &catchAndFinallyJump_)) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (!bce_->emitJumpTarget(&tryEnd_)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
bool TryEmitter::emitCatch(ExceptionStack stack) { |
| 105 |
MOZ_ASSERT(state_ == State::Try); |
| 106 |
if (!emitTryEnd()) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_); |
| 111 |
|
| 112 |
if (shouldUpdateRval()) { |
| 113 |
// Clear the frame's return value that might have been set by the |
| 114 |
// try block: |
| 115 |
// |
| 116 |
// eval("try { 1; throw 2 } catch(e) {}"); // undefined, not 1 |
| 117 |
if (!bce_->emit1(JSOp::Undefined)) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
if (!bce_->emit1(JSOp::SetRval)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (stack == ExceptionStack::No) { |
| 126 |
if (!bce_->emit1(JSOp::Exception)) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} else { |
| 130 |
if (!bce_->emit1(JSOp::ExceptionAndStack)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#ifdef DEBUG |
| 136 |
state_ = State::Catch; |
| 137 |
#endif |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
bool TryEmitter::emitCatchEnd() { |
| 142 |
MOZ_ASSERT(state_ == State::Catch); |
| 143 |
|
| 144 |
if (!controlInfo_) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
// Jump to <finally>, if required. |
| 149 |
if (hasFinally()) { |
| 150 |
if (!emitJumpToFinallyWithFallthrough()) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
bool TryEmitter::emitFinally( |
| 159 |
const Maybe<uint32_t>& finallyPos /* = Nothing() */) { |
| 160 |
// If we are using controlInfo_ (i.e., emitting a syntactic try |
| 161 |
// blocks), we must have specified up front if there will be a finally |
| 162 |
// close. For internal non-syntactic try blocks, like those emitted for |
| 163 |
// yield* and IteratorClose inside for-of loops, we can emitFinally even |
| 164 |
// without specifying up front, since the internal non-syntactic try |
| 165 |
// blocks emit no JSOp::Goto. |
| 166 |
if (!controlInfo_) { |
| 167 |
if (kind_ == Kind::TryCatch) { |
| 168 |
kind_ = Kind::TryCatchFinally; |
| 169 |
} |
| 170 |
} else { |
| 171 |
MOZ_ASSERT(hasFinally()); |
| 172 |
} |
| 173 |
|
| 174 |
if (!hasCatch()) { |
| 175 |
MOZ_ASSERT(state_ == State::Try); |
| 176 |
if (!emitTryEnd()) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
} else { |
| 180 |
MOZ_ASSERT(state_ == State::Catch); |
| 181 |
if (!emitCatchEnd()) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_); |
| 187 |
|
| 188 |
// Upon entry to the finally, there are three additional values on the stack: |
| 189 |
// a boolean value to indicate whether we're throwing an exception, the |
| 190 |
// exception stack (if we're throwing) or null, and either that exception (if |
| 191 |
// we're throwing) or a resume index to which we will return (if we're not |
| 192 |
// throwing). |
| 193 |
bce_->bytecodeSection().setStackDepth(depth_ + 3); |
| 194 |
|
| 195 |
if (!bce_->emitJumpTarget(&finallyStart_)) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
if (controlInfo_) { |
| 200 |
// Fix up the jumps to the finally code. |
| 201 |
bce_->patchJumpsToTarget(controlInfo_->finallyJumps_, finallyStart_); |
| 202 |
|
| 203 |
// Indicate that we're emitting a subroutine body. |
| 204 |
controlInfo_->setEmittingSubroutine(); |
| 205 |
} |
| 206 |
if (finallyPos) { |
| 207 |
if (!bce_->updateSourceCoordNotes(finallyPos.value())) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
if (!bce_->emit1(JSOp::Finally)) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
if (shouldUpdateRval()) { |
| 216 |
if (!bce_->emit1(JSOp::GetRval)) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
// Clear the frame's return value to make break/continue return |
| 221 |
// correct value even if there's no other statement before them: |
| 222 |
// |
| 223 |
// eval("x: try { 1 } finally { break x; }"); // undefined, not 1 |
| 224 |
if (!bce_->emit1(JSOp::Undefined)) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
if (!bce_->emit1(JSOp::SetRval)) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
#ifdef DEBUG |
| 233 |
state_ = State::Finally; |
| 234 |
#endif |
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
bool TryEmitter::emitFinallyEnd() { |
| 239 |
MOZ_ASSERT(state_ == State::Finally); |
| 240 |
|
| 241 |
if (shouldUpdateRval()) { |
| 242 |
if (!bce_->emit1(JSOp::SetRval)) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// [stack] RESUME_INDEX_OR_EXCEPTION, EXCEPTION_STACK, THROWING |
| 248 |
|
| 249 |
InternalIfEmitter ifThrowing(bce_); |
| 250 |
if (!ifThrowing.emitThenElse()) { |
| 251 |
// [stack] RESUME_INDEX_OR_EXCEPTION, EXCEPTION_STACK |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
if (!bce_->emit1(JSOp::ThrowWithStack)) { |
| 256 |
// [stack] |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
if (!ifThrowing.emitElse()) { |
| 261 |
// [stack] RESUME_INDEX_OR_EXCEPTION, EXCEPTION_STACK |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
if (!bce_->emit1(JSOp::Pop)) { |
| 266 |
// [stack] RESUME_INDEX_OR_EXCEPTION |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
if (controlInfo_ && !controlInfo_->continuations_.empty()) { |
| 271 |
if (!controlInfo_->emitContinuations(bce_)) { |
| 272 |
// [stack] |
| 273 |
return false; |
| 274 |
} |
| 275 |
} else { |
| 276 |
// If there are no non-local jumps, then the only possible jump target |
| 277 |
// is the code immediately following this finally block. Instead of |
| 278 |
// emitting a tableswitch, we can simply pop the continuation index |
| 279 |
// and fall through. |
| 280 |
if (!bce_->emit1(JSOp::Pop)) { |
| 281 |
// [stack] |
| 282 |
return false; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (!ifThrowing.emitEnd()) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
bce_->hasTryFinally = true; |
| 291 |
return true; |
| 292 |
} |
| 293 |
|
| 294 |
bool TryEmitter::emitEnd() { |
| 295 |
if (!hasFinally()) { |
| 296 |
MOZ_ASSERT(state_ == State::Catch); |
| 297 |
if (!emitCatchEnd()) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
} else { |
| 301 |
MOZ_ASSERT(state_ == State::Finally); |
| 302 |
if (!emitFinallyEnd()) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_); |
| 308 |
|
| 309 |
if (catchAndFinallyJump_.offset.valid()) { |
| 310 |
// Fix up the end-of-try/catch jumps to come here. |
| 311 |
if (!bce_->emitJumpTargetAndPatch(catchAndFinallyJump_)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
// Add the try note last, to let post-order give us the right ordering |
| 317 |
// (first to last for a given nesting level, inner to outer by level). |
| 318 |
if (hasCatch()) { |
| 319 |
if (!bce_->addTryNote(TryNoteKind::Catch, depth_, offsetAfterTryOp(), |
| 320 |
tryEnd_.offset)) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
// If we've got a finally, mark try+catch region with additional |
| 326 |
// trynote to catch exceptions (re)thrown from a catch block or |
| 327 |
// for the try{}finally{} case. |
| 328 |
if (hasFinally()) { |
| 329 |
if (!bce_->addTryNote(TryNoteKind::Finally, depth_, offsetAfterTryOp(), |
| 330 |
finallyStart_.offset)) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
#ifdef DEBUG |
| 336 |
state_ = State::End; |
| 337 |
#endif |
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
bool TryEmitter::shouldUpdateRval() const { |
| 342 |
return requiresControlInfo() && !bce_->sc->noScriptRval(); |
| 343 |
} |