/**************************************************************************** ** ** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtQml module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or (at your option) the GNU General ** Public license version 3 or any later version approved by the KDE Free ** Qt Foundation. The licenses are as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-2.0.html and ** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QV4BYTECODEGENERATOR_P_H #define QV4BYTECODEGENERATOR_P_H // // W A R N I N G // ------------- // // This file is not part of the Qt API. It exists purely as an // implementation detail. This header file may change from version to // version without notice, or even be removed. // // We mean it. // #include QT_BEGIN_NAMESPACE namespace QQmlJS { namespace AST { class SourceLocation; } } namespace QV4 { namespace Moth { class BytecodeGenerator { public: struct Label { enum LinkMode { LinkNow, LinkLater }; Label() = default; Label(BytecodeGenerator *generator, LinkMode mode = LinkNow) : generator(generator), index(generator->labels.size()) { generator->labels.append(mode == LinkNow ? generator->instructions.size() : -1); } static Label returnLabel() { Label l; l.index = INT_MAX; return l; } bool isReturn() const { return index == INT_MAX; } void link() { Q_ASSERT(index >= 0); Q_ASSERT(generator->labels[index] == -1); generator->labels[index] = generator->instructions.size(); } BytecodeGenerator *generator = 0; int index = -1; }; struct Jump { Jump(BytecodeGenerator *generator, int instruction, int offset) : generator(generator), index(generator->jumps.size()) { generator->jumps.append({ instruction, offset, -1 }); } ~Jump() { Q_ASSERT(generator->jumps[index].linkedLabel != -1); } BytecodeGenerator *generator; int index; void link() { link(generator->label()); } void link(Label l) { Q_ASSERT(l.index >= 0); Q_ASSERT(generator->jumps[index].linkedLabel == -1); generator->jumps[index].linkedLabel = l.index; } }; struct ExceptionHandler : public Label { ExceptionHandler(BytecodeGenerator *generator) : Label(generator, LinkLater) { } ~ExceptionHandler() { Q_ASSERT(generator->currentExceptionHandler != this); } }; Label label() { return Label(this, Label::LinkNow); } Label newLabel() { return Label(this, Label::LinkLater); } ExceptionHandler newExceptionHandler() { return ExceptionHandler(this); } template void addInstruction(const InstrData &data) { Instr genericInstr; genericInstr.common.instructionType = static_cast(InstrT); InstrMeta::setDataNoCommon(genericInstr, data); addInstructionHelper(InstrMeta::Size, genericInstr); } Q_REQUIRED_RESULT Jump jump() { Instruction::Jump data; return addJumpInstruction(data); } Q_REQUIRED_RESULT Jump jumpEq() { Instruction::JumpEq data; return addJumpInstruction(data); } Q_REQUIRED_RESULT Jump jumpNe() { Instruction::JumpNe data; return addJumpInstruction(data); } Q_REQUIRED_RESULT Jump jumpStrictEqual(const StackSlot &lhs) { Instruction::JumpStrictEqual data; data.lhs = lhs; return addJumpInstruction(data); } Q_REQUIRED_RESULT Jump jumpStrictNotEqual(const StackSlot &lhs) { Instruction::JumpStrictNotEqual data; data.lhs = lhs; return addJumpInstruction(data); } Q_REQUIRED_RESULT Jump jumpStrictEqualStackSlotInt(const StackSlot &lhs, int rhs) { Instruction::JumpStrictEqualStackSlotInt data; data.lhs = lhs; data.rhs = rhs; return addJumpInstruction(data); } Q_REQUIRED_RESULT Jump jumpStrictNotEqualStackSlotInt(const StackSlot &lhs, int rhs) { Instruction::JumpStrictNotEqualStackSlotInt data; data.lhs = lhs; data.rhs = rhs; return addJumpInstruction(data); } void setExceptionHandler(ExceptionHandler *handler) { currentExceptionHandler = handler; Instruction::SetExceptionHandler data; data.offset = 0; if (!handler) addInstruction(data); else addJumpInstruction(data).link(*handler); } void setLocation(const QQmlJS::AST::SourceLocation &loc); ExceptionHandler *exceptionHandler() const { return currentExceptionHandler; } int newRegister(); int newRegisterArray(int n); int registerCount() const { return regCount; } QByteArray finalize(); template Jump addJumpInstruction(const InstrData &data) { Instr genericInstr; genericInstr.common.instructionType = static_cast(InstrT); InstrMeta::setDataNoCommon(genericInstr, data); return Jump(this, addInstructionHelper(InstrMeta::Size, genericInstr), offsetof(InstrData, offset)); } private: friend struct Jump; friend struct Label; friend struct ExceptionHandler; int addInstructionHelper(uint size, const Instr &i) { int pos = instructions.size(); instructions.append({size, i}); return pos; } struct JumpData { int instructionIndex; int offset; int linkedLabel; }; struct I { uint size; Instr instr; }; QVector instructions; QVector labels; QVector jumps; ExceptionHandler *currentExceptionHandler; int regCount = 0; public: int currentReg = 0; private: int currentLine = -1; }; } } QT_END_NAMESPACE #endif