DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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
/* -*- 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/. */

#ifndef frontend_CForEmitter_h
#define frontend_CForEmitter_h

#include "mozilla/Attributes.h"  // MOZ_STACK_CLASS, MOZ_MUST_USE
#include "mozilla/Maybe.h"       // mozilla::Maybe

#include <stdint.h>  // uint32_t

#include "frontend/BytecodeControlStructures.h"  // LoopControl
#include "frontend/BytecodeOffset.h"             // BytecodeOffset
#include "frontend/TDZCheckCache.h"              // TDZCheckCache

namespace js {
namespace frontend {

struct BytecodeEmitter;
class EmitterScope;

// Class for emitting bytecode for c-style for block.
//
// Usage: (check for the return value is omitted for simplicity)
//
//   `for (init; cond; update) body`
//     CForEmitter cfor(this, headLexicalEmitterScopeForLet or nullptr);
//     cfor.emitInit(Some(offset_of_init));
//     emit(init); // without pushing value
//     cfor.emitBody(CForEmitter::Cond::Present, Some(offset_of_body));
//     emit(body);
//     cfor.emitUpdate(CForEmitter::Update::Present, Some(offset_of_update)));
//     emit(update);
//     cfor.emitCond(Some(offset_of_for),
//                   Some(offset_of_cond),
//                   Some(offset_of_end));
//     emit(cond);
//     cfor.emitEnd();
//
//   `for (;;) body`
//     CForEmitter cfor(this, nullptr);
//     cfor.emitInit(Nothing());
//     cfor.emitBody(CForEmitter::Cond::Missing, Some(offset_of_body));
//     emit(body);
//     cfor.emitUpdate(CForEmitter::Update::Missing, Nothing());
//     cfor.emitCond(Some(offset_of_for),
//                   Nothing(),
//                   Some(offset_of_end));
//     cfor.emitEnd();
//
class MOZ_STACK_CLASS CForEmitter {
  // Basic structure of the bytecode (not complete).
  //
  // If `cond` is not empty:
  //     {init}
  //     JSOP_GOTO entry
  //   loop:
  //     {body}
  //     {update}
  //   entry:
  //     {cond}
  //     JSOP_IFNE loop
  //
  // If `cond` is empty:
  //     {init}
  //   loop:
  //     {body}
  //     {update}
  //     JSOP_GOTO loop
  //
 public:
  enum class Cond { Missing, Present };
  enum class Update { Missing, Present };

 private:
  BytecodeEmitter* bce_;

  // The source note index for SRC_FOR.
  unsigned noteIndex_ = 0;

  // The bytecode offset of loop condition.
  // Not the bytecode offset of loop condition expression itself.
  BytecodeOffset condOffset_;

  // The base bytecode offset used by SRC_FOR.
  BytecodeOffset biasedTop_;

  // Whether the c-style for loop has `cond` and `update`.
  Cond cond_ = Cond::Missing;
  Update update_ = Update::Missing;

  mozilla::Maybe<LoopControl> loopInfo_;

  // The lexical scope to be freshened for each iteration.
  // See the comment in `emitBody` for more details.
  //
  // ### Scope freshening
  //
  // Each iteration of a `for (let V...)` loop creates a fresh loop variable
  // binding for V, even if the loop is a C-style `for(;;)` loop:
  //
  //     var funcs = [];
  //     for (let i = 0; i < 2; i++)
  //         funcs.push(function() { return i; });
  //     assertEq(funcs[0](), 0);  // the two closures capture...
  //     assertEq(funcs[1](), 1);  // ...two different `i` bindings
  //
  // This is implemented by "freshening" the implicit block -- changing the
  // scope chain to a fresh clone of the instantaneous block object -- each
  // iteration, just before evaluating the "update" in for(;;) loops.
  //
  // ECMAScript doesn't freshen in `for (const ...;;)`.  Lack of freshening
  // isn't directly observable in-language because `const`s can't be mutated,
  // but it *can* be observed in the Debugger API.
  const EmitterScope* headLexicalEmitterScopeForLet_;

  mozilla::Maybe<TDZCheckCache> tdzCache_;

#ifdef DEBUG
  // The state of this emitter.
  //
  // +-------+ emitInit +------+ emitBody +------+ emitUpdate +--------+
  // | Start |--------->| Init |--------->| Body |----------->| Update |-+
  // +-------+          +------+          +------+            +--------+ |
  //                                                                     |
  //                                   +---------------------------------+
  //                                   |
  //                                   | emitCond +------+ emitEnd +-----+
  //                                   +--------->| Cond |-------->| End |
  //                                              +------+         +-----+
  enum class State {
    // The initial state.
    Start,

    // After calling emitInit.
    Init,

    // After calling emitBody.
    Body,

    // After calling emitUpdate.
    Update,

    // After calling emitCond.
    Cond,

    // After calling emitEnd.
    End
  };
  State state_ = State::Start;
#endif

 public:
  CForEmitter(BytecodeEmitter* bce,
              const EmitterScope* headLexicalEmitterScopeForLet);

  // Parameters are the offset in the source code for each character below:
  //
  //   for ( x = 10 ; x < 20 ; x ++ ) { f(x); }
  //   ^     ^        ^        ^      ^       ^
  //   |     |        |        |      |       |
  //   |     |        |        |      |       endPos
  //   |     |        |        |      |
  //   |     |        |        |      bodyPos
  //   |     |        |        |
  //   |     |        |        updatePos
  //   |     |        |
  //   |     |        condPos
  //   |     |
  //   |     initPos
  //   |
  //   forPos
  //
  // Can be Nothing() if not available.
  MOZ_MUST_USE bool emitInit(const mozilla::Maybe<uint32_t>& initPos);
  MOZ_MUST_USE bool emitBody(Cond cond,
                             const mozilla::Maybe<uint32_t>& bodyPos);
  MOZ_MUST_USE bool emitUpdate(Update update,
                               const mozilla::Maybe<uint32_t>& updatePos);
  MOZ_MUST_USE bool emitCond(const mozilla::Maybe<uint32_t>& forPos,
                             const mozilla::Maybe<uint32_t>& condPos,
                             const mozilla::Maybe<uint32_t>& endPos);
  MOZ_MUST_USE bool emitEnd();
};

} /* namespace frontend */
} /* namespace js */

#endif /* frontend_CForEmitter_h */