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.

Header

Mercurial (409f3966645a)

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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * 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/WhileEmitter.h"

#include "frontend/BytecodeEmitter.h"
#include "frontend/SourceNotes.h"
#include "vm/Opcodes.h"

using namespace js;
using namespace js::frontend;

using mozilla::Maybe;

WhileEmitter::WhileEmitter(BytecodeEmitter* bce)
  : bce_(bce)
{}

bool
WhileEmitter::emitBody(const Maybe<uint32_t>& whilePos,
                       const Maybe<uint32_t>& bodyPos,
                       const Maybe<uint32_t>& endPos)
{
    MOZ_ASSERT(state_ == State::Start);

    // Minimize bytecodes issued for one or more iterations by jumping to
    // the condition below the body and closing the loop if the condition
    // is true with a backward branch. For iteration count i:
    //
    //  i    test at the top                 test at the bottom
    //  =    ===============                 ==================
    //  0    ifeq-pass                       goto; ifne-fail
    //  1    ifeq-fail; goto; ifne-pass      goto; ifne-pass; ifne-fail
    //  2    2*(ifeq-fail; goto); ifeq-pass  goto; 2*ifne-pass; ifne-fail
    //  . . .
    //  N    N*(ifeq-fail; goto); ifeq-pass  goto; N*ifne-pass; ifne-fail

    // If we have a single-line while, like "while (x) ;", we want to
    // emit the line note before the initial goto, so that the
    // debugger sees a single entry point.  This way, if there is a
    // breakpoint on the line, it will only fire once; and "next"ing
    // will skip the whole loop.  However, for the multi-line case we
    // want to emit the line note after the initial goto, so that
    // "cont" stops on each iteration -- but without a stop before the
    // first iteration.
    if (whilePos && endPos &&
        bce_->parser->errorReporter().lineAt(*whilePos) ==
        bce_->parser->errorReporter().lineAt(*endPos))
    {
        if (!bce_->updateSourceCoordNotes(*whilePos))
            return false;
    }

    JumpTarget top = { -1 };
    if (!bce_->emitJumpTarget(&top))
        return false;

    loopInfo_.emplace(bce_, StatementKind::WhileLoop);
    loopInfo_->setContinueTarget(top.offset);

    if (!bce_->newSrcNote(SRC_WHILE, &noteIndex_))
        return false;

    if (!loopInfo_->emitEntryJump(bce_))
        return false;

    if (!loopInfo_->emitLoopHead(bce_, bodyPos))
        return false;

    tdzCacheForBody_.emplace(bce_);

#ifdef DEBUG
    state_ = State::Body;
#endif
    return true;
}

bool
WhileEmitter::emitCond(const Maybe<uint32_t>& condPos)
{
    MOZ_ASSERT(state_ == State::Body);

    tdzCacheForBody_.reset();

    if (!loopInfo_->emitLoopEntry(bce_, condPos))
        return false;

#ifdef DEBUG
    state_ = State::Cond;
#endif
    return true;
}

bool
WhileEmitter::emitEnd()
{
    MOZ_ASSERT(state_ == State::Cond);

    if (!loopInfo_->emitLoopEnd(bce_, JSOP_IFNE))
        return false;

    if (!bce_->tryNoteList.append(JSTRY_LOOP, bce_->stackDepth, loopInfo_->headOffset(),
                                  loopInfo_->breakTargetOffset()))
    {
        return false;
    }

    if (!bce_->setSrcNoteOffset(noteIndex_, SrcNote::While::BackJumpOffset,
                                loopInfo_->loopEndOffsetFromEntryJump()))
    {
        return false;
    }

    if (!loopInfo_->patchBreaksAndContinues(bce_))
        return false;

    loopInfo_.reset();

#ifdef DEBUG
    state_ = State::End;
#endif
    return true;
}