5#include <private/qv4bytecodegenerator_p.h>
6#include <private/qv4compilercontext_p.h>
7#include <private/qqmljsastfwd_p.h>
13void BytecodeGenerator::setLocation(
const QQmlJS::SourceLocation &loc)
15 currentLine =
static_cast<
int>(loc.startLine);
16 currentSourceLocation = loc;
17 locationSetAfterLabel =
true;
20void BytecodeGenerator::incrementStatement()
25int BytecodeGenerator::newRegister()
28 if (regCount < currentReg)
29 regCount = currentReg;
33int BytecodeGenerator::newRegisterArray(
int n)
37 if (regCount < currentReg)
38 regCount = currentReg;
42void BytecodeGenerator::packInstruction(I &i)
44 Instr::Type type = Instr::unpack(i.packed);
45 Q_ASSERT(
int(type) <
int(Instr::Type::Count));
46 type = Instr::narrowInstructionType(type);
47 int instructionsAsInts[Instr::MaxArgCount] = {};
48 int nMembers = Moth::InstrInfo::argumentCount[
static_cast<
int>(i.type)];
49 uchar *code = i.packed + Instr::encodedLength(type);
50 for (
int j = 0; j < nMembers; ++j) {
51 instructionsAsInts[j] = qFromLittleEndian<qint32>(code + j *
sizeof(
int));
57 for (
int n = 0; n < nMembers; ++n) {
58 if (width == Normal && (
static_cast<qint8>(instructionsAsInts[n]) != instructionsAsInts[n])) {
66 code = Instr::pack(code, type);
67 for (
int n = 0; n < nMembers; ++n) {
68 qint8 v =
static_cast<qint8>(instructionsAsInts[n]);
72 i.size = code - i.packed;
73 if (i.offsetForJump != -1)
74 i.offsetForJump = i.size - 1;
82void BytecodeGenerator::adjustJumpOffsets()
84 for (
int index = 0; index < instructions.size(); ++index) {
85 auto &i = instructions[index];
86 if (i.offsetForJump == -1)
88 Q_ASSERT(i.linkedLabel != -1 && isLabelValid(i.linkedLabel));
89 const auto &linkedInstruction = instructions.at(labels.at(i.linkedLabel));
90 qint8 *c =
reinterpret_cast<qint8*>(i.packed + i.offsetForJump);
91 int jumpOffset = linkedInstruction.position - (i.position + i.size);
94 Instr::Type type = Instr::unpack(i.packed);
95 if (Instr::isWide(type)) {
96 Q_ASSERT(i.offsetForJump == i.size - 4);
97 qToLittleEndian<qint32>(jumpOffset, c);
99 Q_ASSERT(i.offsetForJump == i.size - 1);
100 qint8 o = jumpOffset;
101 Q_ASSERT(o == jumpOffset);
107void BytecodeGenerator::compressInstructions()
111 for (
auto &i : instructions) {
112 i.position = position;
113 if (i.offsetForJump == -1)
122 for (
auto &i : instructions) {
123 i.position = position;
124 if (i.offsetForJump != -1)
133void BytecodeGenerator::finalize(Compiler::Context *context)
135 if (debugInstructionPass)
136 debugInstructionPass(
this);
138 compressInstructions();
142 QList<CompiledData::CodeOffsetToLineAndStatement> lineAndStatementNumbers;
145 currentStatement = -1;
148 for (qsizetype i = 0; i < instructions.size(); i++) {
149 if (instructions[i].line != currentLine || instructions[i].statement != currentStatement) {
150 currentLine = instructions[i].line;
151 currentStatement = instructions[i].statement;
152 CompiledData::CodeOffsetToLineAndStatement entry;
153 entry.codeOffset = code.size();
154 entry.line = currentLine;
155 entry.statement = currentStatement;
156 lineAndStatementNumbers.append(entry);
159 if (m_sourceLocationTable)
160 m_sourceLocationTable->entries[i].offset =
static_cast<quint32>(code.size());
162 code.append(
reinterpret_cast<
const char *>(instructions[i].packed), instructions[i].size);
165 context->code = code;
166 context->lineAndStatementNumberMapping = lineAndStatementNumbers;
167 context->sourceLocationTable = std::move(m_sourceLocationTable);
169 context->labelInfo.reserve(context->labelInfo.size() + _labelInfos.size());
170 for (
const auto &li : _labelInfos) {
171 if (labelReachable.at(li.labelIndex))
172 context->labelInfo.push_back(instructions.at(labels.at(li.labelIndex)).position);
176int BytecodeGenerator::addInstructionHelper(Instr::Type type,
const Instr &i,
int offsetOfOffset) {
180 if (lastInstrType ==
int(Instr::Type::StoreReg)) {
181 if (type == Instr::Type::LoadReg) {
182 if (i.as<Instruction::LoadReg>().reg == lastInstr.as<Instruction::StoreReg>().reg) {
187 if (type == Instr::Type::MoveReg) {
188 const auto move = i.as<Instruction::MoveReg>();
189 if (move.srcReg == lastInstr.as<Instruction::StoreReg>().reg) {
190 Instruction::StoreReg store;
191 store.reg = move.destReg;
192 addInstruction(store);
197 lastInstrType =
int(type);
200 const int pos = instructions.size();
201 I instr = makeInstruction(type, i, offsetOfOffset, currentLine, currentStatement);
202 instr.locationSetAfterLabel = locationSetAfterLabel;
203 instructions.append(instr);
204 if (m_sourceLocationTable)
205 m_sourceLocationTable->entries.append({ 0, currentSourceLocation });