summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/BytecodeControlStructures.cpp
blob: 511e16752358b01d8edc051fbf001ee4fd515383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "frontend/BytecodeControlStructures.h"

#include "frontend/BytecodeEmitter.h"   // BytecodeEmitter
#include "frontend/EmitterScope.h"      // EmitterScope
#include "frontend/ForOfLoopControl.h"  // ForOfLoopControl
#include "frontend/SwitchEmitter.h"     // SwitchEmitter
#include "vm/Opcodes.h"                 // JSOp

using namespace js;
using namespace js::frontend;

using mozilla::Maybe;

NestableControl::NestableControl(BytecodeEmitter* bce, StatementKind kind)
    : Nestable<NestableControl>(&bce->innermostNestableControl),
      kind_(kind),
      emitterScope_(bce->innermostEmitterScopeNoCheck()) {}

BreakableControl::BreakableControl(BytecodeEmitter* bce, StatementKind kind)
    : NestableControl(bce, kind) {
  MOZ_ASSERT(is<BreakableControl>());
}

bool BreakableControl::patchBreaks(BytecodeEmitter* bce) {
  return bce->emitJumpTargetAndPatch(breaks);
}

LabelControl::LabelControl(BytecodeEmitter* bce, TaggedParserAtomIndex label,
                           BytecodeOffset startOffset)
    : BreakableControl(bce, StatementKind::Label),
      label_(label),
      startOffset_(startOffset) {}

LoopControl::LoopControl(BytecodeEmitter* bce, StatementKind loopKind)
    : BreakableControl(bce, loopKind), tdzCache_(bce) {
  MOZ_ASSERT(is<LoopControl>());

  LoopControl* enclosingLoop = findNearest<LoopControl>(enclosing());

  stackDepth_ = bce->bytecodeSection().stackDepth();
  loopDepth_ = enclosingLoop ? enclosingLoop->loopDepth_ + 1 : 1;
}

bool LoopControl::emitContinueTarget(BytecodeEmitter* bce) {
  // Note: this is always called after emitting the loop body so we must have
  // emitted all 'continues' by now.
  return bce->emitJumpTargetAndPatch(continues);
}

bool LoopControl::emitLoopHead(BytecodeEmitter* bce,
                               const Maybe<uint32_t>& nextPos) {
  // Insert a Nop if needed to ensure the script does not start with a
  // JSOp::LoopHead. This avoids JIT issues with prologue code + try notes
  // or OSR. See bug 1602390 and bug 1602681.
  if (bce->bytecodeSection().offset().toUint32() == 0) {
    if (!bce->emit1(JSOp::Nop)) {
      return false;
    }
  }

  if (nextPos) {
    if (!bce->updateSourceCoordNotes(*nextPos)) {
      return false;
    }
  }

  MOZ_ASSERT(loopDepth_ > 0);

  head_ = {bce->bytecodeSection().offset()};

  BytecodeOffset off;
  if (!bce->emitJumpTargetOp(JSOp::LoopHead, &off)) {
    return false;
  }
  SetLoopHeadDepthHint(bce->bytecodeSection().code(off), loopDepth_);

  return true;
}

bool LoopControl::emitLoopEnd(BytecodeEmitter* bce, JSOp op,
                              TryNoteKind tryNoteKind) {
  JumpList jump;
  if (!bce->emitJumpNoFallthrough(op, &jump)) {
    return false;
  }
  bce->patchJumpsToTarget(jump, head_);

  // Create a fallthrough for closing iterators, and as a target for break
  // statements.
  JumpTarget breakTarget;
  if (!bce->emitJumpTarget(&breakTarget)) {
    return false;
  }
  if (!patchBreaks(bce)) {
    return false;
  }
  if (!bce->addTryNote(tryNoteKind, bce->bytecodeSection().stackDepth(),
                       headOffset(), breakTarget.offset)) {
    return false;
  }
  return true;
}

TryFinallyControl::TryFinallyControl(BytecodeEmitter* bce, StatementKind kind)
    : NestableControl(bce, kind) {
  MOZ_ASSERT(is<TryFinallyControl>());
}

bool TryFinallyControl::allocateContinuation(NestableControl* target,
                                             NonLocalExitKind kind,
                                             uint32_t* idx) {
  for (uint32_t i = 0; i < continuations_.length(); i++) {
    if (continuations_[i].target_ == target &&
        continuations_[i].kind_ == kind) {
      *idx = i + SpecialContinuations::Count;
      return true;
    }
  }
  *idx = continuations_.length() + SpecialContinuations::Count;
  return continuations_.emplaceBack(target, kind);
}

bool TryFinallyControl::emitContinuations(BytecodeEmitter* bce) {
  SwitchEmitter::TableGenerator tableGen(bce);
  for (uint32_t i = 0; i < continuations_.length(); i++) {
    if (!tableGen.addNumber(i + SpecialContinuations::Count)) {
      return false;
    }
  }
  tableGen.finish(continuations_.length());
  MOZ_RELEASE_ASSERT(tableGen.isValid());

  InternalSwitchEmitter se(bce);
  if (!se.validateCaseCount(continuations_.length())) {
    return false;
  }
  if (!se.emitTable(tableGen)) {
    return false;
  }

  // Continuation index 0 is special-cased to be the fallthrough block.
  // Non-default switch cases are numbered 1-N.
  uint32_t caseIdx = SpecialContinuations::Count;
  for (TryFinallyContinuation& continuation : continuations_) {
    if (!se.emitCaseBody(caseIdx++, tableGen)) {
      return false;
    }
    // Resume the non-local control flow that was intercepted by
    // this finally.
    NonLocalExitControl nle(bce, continuation.kind_);
    if (!nle.emitNonLocalJump(continuation.target_, this)) {
      return false;
    }
  }

  // The only unhandled case is the fallthrough case, which is handled
  // by the switch default.
  if (!se.emitDefaultBody()) {
    return false;
  }
  if (!se.emitEnd()) {
    return false;
  }
  return true;
}

NonLocalExitControl::NonLocalExitControl(BytecodeEmitter* bce,
                                         NonLocalExitKind kind)
    : bce_(bce),
      savedScopeNoteIndex_(bce->bytecodeSection().scopeNoteList().length()),
      savedDepth_(bce->bytecodeSection().stackDepth()),
      openScopeNoteIndex_(bce->innermostEmitterScope()->noteIndex()),
      kind_(kind) {}

NonLocalExitControl::~NonLocalExitControl() {
  for (uint32_t n = savedScopeNoteIndex_;
       n < bce_->bytecodeSection().scopeNoteList().length(); n++) {
    bce_->bytecodeSection().scopeNoteList().recordEnd(
        n, bce_->bytecodeSection().offset());
  }
  bce_->bytecodeSection().setStackDepth(savedDepth_);
}

bool NonLocalExitControl::emitReturn(BytecodeOffset setRvalOffset) {
  MOZ_ASSERT(kind_ == NonLocalExitKind::Return);
  setRvalOffset_ = setRvalOffset;
  return emitNonLocalJump(nullptr);
}

bool NonLocalExitControl::leaveScope(EmitterScope* es) {
  if (!es->leave(bce_, /* nonLocal = */ true)) {
    return false;
  }

  // As we pop each scope due to the non-local jump, emit notes that
  // record the extent of the enclosing scope. These notes will have
  // their ends recorded in ~NonLocalExitControl().
  GCThingIndex enclosingScopeIndex = ScopeNote::NoScopeIndex;
  if (es->enclosingInFrame()) {
    enclosingScopeIndex = es->enclosingInFrame()->index();
  }
  if (!bce_->bytecodeSection().scopeNoteList().append(
          enclosingScopeIndex, bce_->bytecodeSection().offset(),
          openScopeNoteIndex_)) {
    return false;
  }
  openScopeNoteIndex_ = bce_->bytecodeSection().scopeNoteList().length() - 1;

  return true;
}

/*
 * Emit additional bytecode(s) for non-local jumps.
 */
bool NonLocalExitControl::emitNonLocalJump(NestableControl* target,
                                           NestableControl* startingAfter) {
  NestableControl* startingControl = startingAfter
                                         ? startingAfter->enclosing()
                                         : bce_->innermostNestableControl;
  EmitterScope* es = startingAfter ? startingAfter->emitterScope()
                                   : bce_->innermostEmitterScope();

  int npops = 0;

  AutoCheckUnstableEmitterScope cues(bce_);

  // We emit IteratorClose bytecode inline. 'continue' statements do
  // not call IteratorClose for the loop they are continuing.
  bool emitIteratorCloseAtTarget = kind_ != NonLocalExitKind::Continue;

  auto flushPops = [&npops](BytecodeEmitter* bce) {
    if (npops && !bce->emitPopN(npops)) {
      return false;
    }
    npops = 0;
    return true;
  };

  // If we are closing multiple for-of loops, the resulting
  // TryNoteKind::ForOfIterClose trynotes must be appropriately nested. Each
  // TryNoteKind::ForOfIterClose starts when we close the corresponding for-of
  // iterator, and continues until the actual jump.
  Vector<BytecodeOffset, 4> forOfIterCloseScopeStarts(bce_->fc);

  // If we have to execute a finally block, then we will jump there now and
  // continue the non-local jump from the end of the finally block.
  bool jumpingToFinally = false;

  // Walk the nestable control stack and patch jumps.
  for (NestableControl* control = startingControl;
       control != target && !jumpingToFinally; control = control->enclosing()) {
    // Walk the scope stack and leave the scopes we entered. Leaving a scope
    // may emit administrative ops like JSOp::PopLexicalEnv but never anything
    // that manipulates the stack.
    for (; es != control->emitterScope(); es = es->enclosingInFrame()) {
      if (!leaveScope(es)) {
        return false;
      }
    }

    switch (control->kind()) {
      case StatementKind::Finally: {
        TryFinallyControl& finallyControl = control->as<TryFinallyControl>();
        if (finallyControl.emittingSubroutine()) {
          /*
           * There's a [resume-index-or-exception, exception-stack, throwing]
           * triple on the stack that we need to pop. If the script is not a
           * noScriptRval script, we also need to pop the cached rval.
           */
          if (bce_->sc->noScriptRval()) {
            npops += 3;
          } else {
            npops += 4;
          }
        } else {
          jumpingToFinally = true;

          if (!flushPops(bce_)) {
            return false;
          }
          uint32_t idx;
          if (!finallyControl.allocateContinuation(target, kind_, &idx)) {
            return false;
          }
          if (!bce_->emitJumpToFinally(&finallyControl.finallyJumps_, idx)) {
            return false;
          }
        }
        break;
      }

      case StatementKind::ForOfLoop: {
        if (!flushPops(bce_)) {
          return false;
        }
        BytecodeOffset tryNoteStart;
        ForOfLoopControl& loopinfo = control->as<ForOfLoopControl>();
        if (!loopinfo.emitPrepareForNonLocalJumpFromScope(
                bce_, *es,
                /* isTarget = */ false, &tryNoteStart)) {
          //      [stack] ...
          return false;
        }
        if (!forOfIterCloseScopeStarts.append(tryNoteStart)) {
          return false;
        }
        break;
      }

      case StatementKind::ForInLoop:
        if (!flushPops(bce_)) {
          return false;
        }

        // The iterator and the current value are on the stack.
        if (!bce_->emit1(JSOp::EndIter)) {
          //        [stack] ...
          return false;
        }
        break;

      default:
        break;
    }
  }

  if (!flushPops(bce_)) {
    return false;
  }

  if (!jumpingToFinally) {
    if (target && emitIteratorCloseAtTarget && target->is<ForOfLoopControl>()) {
      BytecodeOffset tryNoteStart;
      ForOfLoopControl& loopinfo = target->as<ForOfLoopControl>();
      if (!loopinfo.emitPrepareForNonLocalJumpFromScope(bce_, *es,
                                                        /* isTarget = */ true,
                                                        &tryNoteStart)) {
        //            [stack] ... UNDEF UNDEF UNDEF
        return false;
      }
      if (!forOfIterCloseScopeStarts.append(tryNoteStart)) {
        return false;
      }
    }

    EmitterScope* targetEmitterScope =
        target ? target->emitterScope() : bce_->varEmitterScope;
    for (; es != targetEmitterScope; es = es->enclosingInFrame()) {
      if (!leaveScope(es)) {
        return false;
      }
    }
    switch (kind_) {
      case NonLocalExitKind::Continue: {
        LoopControl* loop = &target->as<LoopControl>();
        if (!bce_->emitJump(JSOp::Goto, &loop->continues)) {
          return false;
        }
        break;
      }
      case NonLocalExitKind::Break: {
        BreakableControl* breakable = &target->as<BreakableControl>();
        if (!bce_->emitJump(JSOp::Goto, &breakable->breaks)) {
          return false;
        }
        break;
      }
      case NonLocalExitKind::Return:
        MOZ_ASSERT(!target);
        if (!bce_->finishReturn(setRvalOffset_)) {
          return false;
        }
        break;
    }
  }

  // Close TryNoteKind::ForOfIterClose trynotes.
  BytecodeOffset end = bce_->bytecodeSection().offset();
  for (BytecodeOffset start : forOfIterCloseScopeStarts) {
    if (!bce_->addTryNote(TryNoteKind::ForOfIterClose, 0, start, end)) {
      return false;
    }
  }

  return true;
}