Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4bytecodegenerator.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#include <private/qv4bytecodegenerator_p.h>
6#include <private/qv4compilercontext_p.h>
7#include <private/qqmljsastfwd_p.h>
8
9QT_USE_NAMESPACE
10using namespace QV4;
11using namespace Moth;
12
13void BytecodeGenerator::setLocation(const QQmlJS::SourceLocation &loc)
14{
15 currentLine = static_cast<int>(loc.startLine);
16 currentSourceLocation = loc;
17 locationSetAfterLabel = true;
18}
19
20void BytecodeGenerator::incrementStatement()
21{
22 ++currentStatement;
23}
24
25int BytecodeGenerator::newRegister()
26{
27 int t = currentReg++;
28 if (regCount < currentReg)
29 regCount = currentReg;
30 return t;
31}
32
33int BytecodeGenerator::newRegisterArray(int n)
34{
35 int t = currentReg;
36 currentReg += n;
37 if (regCount < currentReg)
38 regCount = currentReg;
39 return t;
40}
41
42void BytecodeGenerator::packInstruction(I &i)
43{
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));
52 }
53 enum {
54 Normal,
55 Wide
56 } width = Normal;
57 for (int n = 0; n < nMembers; ++n) {
58 if (width == Normal && (static_cast<qint8>(instructionsAsInts[n]) != instructionsAsInts[n])) {
59 width = Wide;
60 break;
61 }
62 }
63 code = i.packed;
64 switch (width) {
65 case Normal:
66 code = Instr::pack(code, type);
67 for (int n = 0; n < nMembers; ++n) {
68 qint8 v = static_cast<qint8>(instructionsAsInts[n]);
69 memcpy(code, &v, 1);
70 code += 1;
71 }
72 i.size = code - i.packed;
73 if (i.offsetForJump != -1)
74 i.offsetForJump = i.size - 1;
75 break;
76 case Wide:
77 // nothing to do
78 break;
79 }
80}
81
82void BytecodeGenerator::adjustJumpOffsets()
83{
84 for (int index = 0; index < instructions.size(); ++index) {
85 auto &i = instructions[index];
86 if (i.offsetForJump == -1) // no jump
87 continue;
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);
92// qDebug() << "adjusting jump offset for instruction" << index << i.position << i.size << "offsetForJump" << i.offsetForJump << "target"
93// << labels.at(i.linkedLabel) << linkedInstruction.position << "jumpOffset" << jumpOffset;
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);
98 } else {
99 Q_ASSERT(i.offsetForJump == i.size - 1);
100 qint8 o = jumpOffset;
101 Q_ASSERT(o == jumpOffset);
102 *c = o;
103 }
104 }
105}
106
107void BytecodeGenerator::compressInstructions()
108{
109 // first round: compress all non jump instructions
110 int position = 0;
111 for (auto &i : instructions) {
112 i.position = position;
113 if (i.offsetForJump == -1)
114 packInstruction(i);
115 position += i.size;
116 }
117
118 adjustJumpOffsets();
119
120 // compress all jumps
121 position = 0;
122 for (auto &i : instructions) {
123 i.position = position;
124 if (i.offsetForJump != -1)
125 packInstruction(i);
126 position += i.size;
127 }
128
129 // adjust once again, as the packing above could have changed offsets
130 adjustJumpOffsets();
131}
132
133void BytecodeGenerator::finalize(Compiler::Context *context)
134{
135 if (debugInstructionPass)
136 debugInstructionPass(this);
137
138 compressInstructions();
139
140 // collect content and line numbers
141 QByteArray code;
142 QList<CompiledData::CodeOffsetToLineAndStatement> lineAndStatementNumbers;
143
144 currentLine = -1;
145 currentStatement = -1;
146
147 Q_UNUSED(startLine);
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);
157 }
158
159 if (m_sourceLocationTable)
160 m_sourceLocationTable->entries[i].offset = static_cast<quint32>(code.size());
161
162 code.append(reinterpret_cast<const char *>(instructions[i].packed), instructions[i].size);
163 }
164
165 context->code = code;
166 context->lineAndStatementNumberMapping = lineAndStatementNumbers;
167 context->sourceLocationTable = std::move(m_sourceLocationTable);
168
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);
173 }
174}
175
176int BytecodeGenerator::addInstructionHelper(Instr::Type type, const Instr &i, int offsetOfOffset) {
177 if (!reachable)
178 return -1;
179
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) {
183 // value is already in the accumulator
184 return -1;
185 }
186 }
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);
193 return -1;
194 }
195 }
196 }
197 lastInstrType = int(type);
198 lastInstr = i;
199
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 });
206
207 if (endsFlow(type))
208 reachable = false;
209
210 return pos;
211}