ExpressionStatementEmitter.cpp (1488B)
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/ExpressionStatementEmitter.h" 8 9 #include "frontend/BytecodeEmitter.h" 10 #include "vm/Opcodes.h" 11 12 using namespace js; 13 using namespace js::frontend; 14 15 ExpressionStatementEmitter::ExpressionStatementEmitter(BytecodeEmitter* bce, 16 ValueUsage valueUsage) 17 : bce_(bce), valueUsage_(valueUsage) {} 18 19 bool ExpressionStatementEmitter::prepareForExpr(uint32_t beginPos) { 20 MOZ_ASSERT(state_ == State::Start); 21 22 if (!bce_->updateSourceCoordNotes(beginPos)) { 23 return false; 24 } 25 26 #ifdef DEBUG 27 depth_ = bce_->bytecodeSection().stackDepth(); 28 state_ = State::Expr; 29 #endif 30 return true; 31 } 32 33 bool ExpressionStatementEmitter::emitEnd() { 34 MOZ_ASSERT(state_ == State::Expr); 35 MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_ + 1); 36 37 // [stack] VAL 38 39 JSOp op = valueUsage_ == ValueUsage::WantValue ? JSOp::SetRval : JSOp::Pop; 40 if (!bce_->emit1(op)) { 41 // [stack] # if WantValue 42 // [stack] VAL 43 // [stack] # otherwise 44 // [stack] 45 return false; 46 } 47 48 #ifdef DEBUG 49 state_ = State::End; 50 #endif 51 return true; 52 }