Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/compiler/qv4bytecodegenerator_p.h |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2017 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtQml module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #ifndef QV4BYTECODEGENERATOR_P_H | - | ||||||
41 | #define QV4BYTECODEGENERATOR_P_H | - | ||||||
42 | - | |||||||
43 | // | - | ||||||
44 | // W A R N I N G | - | ||||||
45 | // ------------- | - | ||||||
46 | // | - | ||||||
47 | // This file is not part of the Qt API. It exists purely as an | - | ||||||
48 | // implementation detail. This header file may change from version to | - | ||||||
49 | // version without notice, or even be removed. | - | ||||||
50 | // | - | ||||||
51 | // We mean it. | - | ||||||
52 | // | - | ||||||
53 | #include <private/qv4instr_moth_p.h> | - | ||||||
54 | - | |||||||
55 | QT_BEGIN_NAMESPACE | - | ||||||
56 | - | |||||||
57 | namespace QQmlJS { | - | ||||||
58 | namespace AST { | - | ||||||
59 | class SourceLocation; | - | ||||||
60 | } | - | ||||||
61 | } | - | ||||||
62 | - | |||||||
63 | namespace QV4 { | - | ||||||
64 | namespace Moth { | - | ||||||
65 | - | |||||||
66 | class BytecodeGenerator { | - | ||||||
67 | public: | - | ||||||
68 | BytecodeGenerator(int line, bool debug) | - | ||||||
69 | : startLine(line), debugMode(debug) {} executed 3390270 times by 140 tests: end of block Executed by:
| 3390270 | ||||||
70 | - | |||||||
71 | struct Label { | - | ||||||
72 | enum LinkMode { | - | ||||||
73 | LinkNow, | - | ||||||
74 | LinkLater | - | ||||||
75 | }; | - | ||||||
76 | Label() = default; | - | ||||||
77 | Label(BytecodeGenerator *generator, LinkMode mode = LinkNow) | - | ||||||
78 | : generator(generator), | - | ||||||
79 | index(generator->labels.size()) { | - | ||||||
80 | generator->labels.append(-1); | - | ||||||
81 | if (mode == LinkNow)
| 535758-4133500 | ||||||
82 | link(); executed 535902 times by 65 tests: link(); Executed by:
| 535902 | ||||||
83 | } executed 4672015 times by 74 tests: end of block Executed by:
| 4672015 | ||||||
84 | - | |||||||
85 | void link() { | - | ||||||
86 | Q_ASSERT(index >= 0); | - | ||||||
87 | Q_ASSERT(generator->labels[index] == -1); | - | ||||||
88 | generator->labels[index] = generator->instructions.size(); | - | ||||||
89 | generator->clearLastInstruction(); | - | ||||||
90 | } executed 4673238 times by 74 tests: end of block Executed by:
| 4673238 | ||||||
91 | bool isValid() const { return generator != nullptr; } executed 1328129 times by 48 tests: return generator != nullptr; Executed by:
| 1328129 | ||||||
92 | - | |||||||
93 | BytecodeGenerator *generator = nullptr; | - | ||||||
94 | int index = -1; | - | ||||||
95 | }; | - | ||||||
96 | - | |||||||
97 | struct Jump { | - | ||||||
98 | Jump(BytecodeGenerator *generator, int instruction) | - | ||||||
99 | : generator(generator), | - | ||||||
100 | index(instruction) | - | ||||||
101 | { Q_ASSERT(generator && index != -1); } executed 2765737 times by 73 tests: end of block Executed by:
| 2765737 | ||||||
102 | - | |||||||
103 | ~Jump() { | - | ||||||
104 | Q_ASSERT(index == -1 || generator->instructions[index].linkedLabel != -1); // make sure link() got called | - | ||||||
105 | } executed 2771367 times by 73 tests: end of block Executed by:
| 2771367 | ||||||
106 | - | |||||||
107 | Jump(Jump &&j) { | - | ||||||
108 | std::swap(generator, j.generator); | - | ||||||
109 | std::swap(index, j.index); | - | ||||||
110 | } never executed: end of block | 0 | ||||||
111 | - | |||||||
112 | BytecodeGenerator *generator = nullptr; | - | ||||||
113 | int index = -1; | - | ||||||
114 | - | |||||||
115 | void link() { | - | ||||||
116 | link(generator->label()); | - | ||||||
117 | } executed 508291 times by 62 tests: end of block Executed by:
| 508291 | ||||||
118 | void link(Label l) { | - | ||||||
119 | Q_ASSERT(l.index >= 0); | - | ||||||
120 | Q_ASSERT(generator->instructions[index].linkedLabel == -1); | - | ||||||
121 | generator->instructions[index].linkedLabel = l.index; | - | ||||||
122 | } executed 2771846 times by 73 tests: end of block Executed by:
| 2771846 | ||||||
123 | - | |||||||
124 | private: | - | ||||||
125 | // make this type move-only: | - | ||||||
126 | Q_DISABLE_COPY(Jump) | - | ||||||
127 | // we never move-assign this type anywhere, so disable it: | - | ||||||
128 | Jump &operator=(Jump &&) = delete; | - | ||||||
129 | }; | - | ||||||
130 | - | |||||||
131 | struct ExceptionHandler : public Label { | - | ||||||
132 | ExceptionHandler() = default; | - | ||||||
133 | ExceptionHandler(BytecodeGenerator *generator) | - | ||||||
134 | : Label(generator, LinkLater) | - | ||||||
135 | { | - | ||||||
136 | } executed 242605 times by 22 tests: end of block Executed by:
| 242605 | ||||||
137 | ~ExceptionHandler() | - | ||||||
138 | { | - | ||||||
139 | Q_ASSERT(!generator || generator->currentExceptionHandler != this); | - | ||||||
140 | } executed 2060038 times by 77 tests: end of block Executed by:
| 2060038 | ||||||
141 | bool isValid() const { return generator != nullptr; } executed 31311 times by 17 tests: return generator != nullptr; Executed by:
| 31311 | ||||||
142 | }; | - | ||||||
143 | - | |||||||
144 | Label label() { | - | ||||||
145 | return Label(this, Label::LinkNow); executed 535518 times by 65 tests: return Label(this, Label::LinkNow); Executed by:
| 535518 | ||||||
146 | } | - | ||||||
147 | - | |||||||
148 | Label newLabel() { | - | ||||||
149 | return Label(this, Label::LinkLater); executed 3880907 times by 74 tests: return Label(this, Label::LinkLater); Executed by:
| 3880907 | ||||||
150 | } | - | ||||||
151 | - | |||||||
152 | ExceptionHandler newExceptionHandler() { | - | ||||||
153 | return ExceptionHandler(this); executed 242734 times by 22 tests: return ExceptionHandler(this); Executed by:
| 242734 | ||||||
154 | } | - | ||||||
155 | - | |||||||
156 | template<int InstrT> | - | ||||||
157 | void addInstruction(const InstrData<InstrT> &data) | - | ||||||
158 | { | - | ||||||
159 | Instr genericInstr; | - | ||||||
160 | InstrMeta<InstrT>::setData(genericInstr, data); | - | ||||||
161 | addInstructionHelper(Moth::Instr::Type(InstrT), genericInstr); | - | ||||||
162 | } executed 43407084 times by 140 tests: end of block Executed by:
| 43407084 | ||||||
163 | - | |||||||
164 | Q_REQUIRED_RESULT Jump jump() | - | ||||||
165 | { | - | ||||||
166 | Instruction::Jump data; | - | ||||||
167 | return addJumpInstruction(data); executed 544896 times by 65 tests: return addJumpInstruction(data); Executed by:
| 544896 | ||||||
168 | } | - | ||||||
169 | - | |||||||
170 | Q_REQUIRED_RESULT Jump jumpTrue() | - | ||||||
171 | { | - | ||||||
172 | Instruction::JumpTrue data; | - | ||||||
173 | return addJumpInstruction(data); executed 184644 times by 10 tests: return addJumpInstruction(data); Executed by:
| 184644 | ||||||
174 | } | - | ||||||
175 | - | |||||||
176 | Q_REQUIRED_RESULT Jump jumpFalse() | - | ||||||
177 | { | - | ||||||
178 | Instruction::JumpFalse data; | - | ||||||
179 | return addJumpInstruction(data); executed 453037 times by 58 tests: return addJumpInstruction(data); Executed by:
| 453037 | ||||||
180 | } | - | ||||||
181 | - | |||||||
182 | Q_REQUIRED_RESULT Jump jumpNotUndefined() | - | ||||||
183 | { | - | ||||||
184 | Instruction::JumpNotUndefined data; | - | ||||||
185 | return addJumpInstruction(data); executed 17398 times by 1 test: return addJumpInstruction(data); Executed by:
| 17398 | ||||||
186 | } | - | ||||||
187 | - | |||||||
188 | Q_REQUIRED_RESULT Jump jumpNoException() | - | ||||||
189 | { | - | ||||||
190 | Instruction::JumpNoException data; | - | ||||||
191 | return addJumpInstruction(data); executed 113948 times by 21 tests: return addJumpInstruction(data); Executed by:
| 113948 | ||||||
192 | } | - | ||||||
193 | - | |||||||
194 | void jumpStrictEqual(const StackSlot &lhs, const Label &target) | - | ||||||
195 | { | - | ||||||
196 | Instruction::CmpStrictEqual cmp; | - | ||||||
197 | cmp.lhs = lhs; | - | ||||||
198 | addInstruction(cmp); | - | ||||||
199 | addJumpInstruction(Instruction::JumpTrue()).link(target); | - | ||||||
200 | } executed 80922 times by 6 tests: end of block Executed by:
| 80922 | ||||||
201 | - | |||||||
202 | void jumpStrictNotEqual(const StackSlot &lhs, const Label &target) | - | ||||||
203 | { | - | ||||||
204 | Instruction::CmpStrictNotEqual cmp; | - | ||||||
205 | cmp.lhs = lhs; | - | ||||||
206 | addInstruction(cmp); | - | ||||||
207 | addJumpInstruction(Instruction::JumpTrue()).link(target); | - | ||||||
208 | } never executed: end of block | 0 | ||||||
209 | - | |||||||
210 | void setUnwindHandler(ExceptionHandler *handler) | - | ||||||
211 | { | - | ||||||
212 | currentExceptionHandler = handler; | - | ||||||
213 | Instruction::SetUnwindHandler data; | - | ||||||
214 | data.offset = 0; | - | ||||||
215 | if (!handler)
| 127805-244127 | ||||||
216 | addInstruction(data); executed 127940 times by 22 tests: addInstruction(data); Executed by:
| 127940 | ||||||
217 | else | - | ||||||
218 | addJumpInstruction(data).link(*handler); executed 243938 times by 22 tests: addJumpInstruction(data).link(*handler); Executed by:
| 243938 | ||||||
219 | } | - | ||||||
220 | - | |||||||
221 | void unwindToLabel(int level, const Label &target) | - | ||||||
222 | { | - | ||||||
223 | if (level) {
| 17441-83555 | ||||||
224 | Instruction::UnwindToLabel unwind; | - | ||||||
225 | unwind.level = level; | - | ||||||
226 | addJumpInstruction(unwind).link(target); | - | ||||||
227 | } else { executed 83688 times by 13 tests: end of block Executed by:
| 83688 | ||||||
228 | jump().link(target); | - | ||||||
229 | } executed 17543 times by 10 tests: end of block Executed by:
| 17543 | ||||||
230 | } | - | ||||||
231 | - | |||||||
232 | - | |||||||
233 | - | |||||||
234 | void setLocation(const QQmlJS::AST::SourceLocation &loc); | - | ||||||
235 | - | |||||||
236 | ExceptionHandler *exceptionHandler() const { | - | ||||||
237 | return currentExceptionHandler; never executed: return currentExceptionHandler; | 0 | ||||||
238 | } | - | ||||||
239 | - | |||||||
240 | int newRegister(); | - | ||||||
241 | int newRegisterArray(int n); | - | ||||||
242 | int registerCount() const { return regCount; } executed 3397860 times by 140 tests: return regCount; Executed by:
| 3397860 | ||||||
243 | int currentRegister() const { return currentReg; } executed 10356220 times by 140 tests: return currentReg; Executed by:
| 10356220 | ||||||
244 | - | |||||||
245 | void finalize(Compiler::Context *context); | - | ||||||
246 | - | |||||||
247 | template<int InstrT> | - | ||||||
248 | Jump addJumpInstruction(const InstrData<InstrT> &data) | - | ||||||
249 | { | - | ||||||
250 | Instr genericInstr; | - | ||||||
251 | InstrMeta<InstrT>::setData(genericInstr, data); | - | ||||||
252 | return Jump(this, addInstructionHelper(Moth::Instr::Type(InstrT), genericInstr, offsetof(InstrData<InstrT>, offset))); executed 2755121 times by 73 tests: return Jump(this, addInstructionHelper(Moth::Instr::Type(InstrT), genericInstr, __builtin_offsetof ( InstrData<InstrT> , offset ) )); Executed by:
| 2755121 | ||||||
253 | } | - | ||||||
254 | - | |||||||
255 | void addCJumpInstruction(bool jumpOnFalse, const Label *trueLabel, const Label *falseLabel) | - | ||||||
256 | { | - | ||||||
257 | if (jumpOnFalse)
| 107438-910379 | ||||||
258 | addJumpInstruction(Instruction::JumpFalse()).link(*falseLabel); executed 909101 times by 52 tests: addJumpInstruction(Instruction::JumpFalse()).link(*falseLabel); Executed by:
| 909101 | ||||||
259 | else | - | ||||||
260 | addJumpInstruction(Instruction::JumpTrue()).link(*trueLabel); executed 106961 times by 18 tests: addJumpInstruction(Instruction::JumpTrue()).link(*trueLabel); Executed by:
| 106961 | ||||||
261 | } | - | ||||||
262 | - | |||||||
263 | void clearLastInstruction() | - | ||||||
264 | { | - | ||||||
265 | lastInstrType = -1; | - | ||||||
266 | } executed 4671888 times by 74 tests: end of block Executed by:
| 4671888 | ||||||
267 | - | |||||||
268 | private: | - | ||||||
269 | friend struct Jump; | - | ||||||
270 | friend struct Label; | - | ||||||
271 | friend struct ExceptionHandler; | - | ||||||
272 | - | |||||||
273 | int addInstructionHelper(Moth::Instr::Type type, const Instr &i, int offsetOfOffset = -1); | - | ||||||
274 | - | |||||||
275 | struct I { | - | ||||||
276 | Moth::Instr::Type type; | - | ||||||
277 | short size; | - | ||||||
278 | uint position; | - | ||||||
279 | int line; | - | ||||||
280 | int offsetForJump; | - | ||||||
281 | int linkedLabel; | - | ||||||
282 | unsigned char packed[sizeof(Instr) + 2]; // 2 for instruction type | - | ||||||
283 | }; | - | ||||||
284 | - | |||||||
285 | void compressInstructions(); | - | ||||||
286 | void packInstruction(I &i); | - | ||||||
287 | void adjustJumpOffsets(); | - | ||||||
288 | - | |||||||
289 | QVector<I> instructions; | - | ||||||
290 | QVector<int> labels; | - | ||||||
291 | ExceptionHandler *currentExceptionHandler = nullptr; | - | ||||||
292 | int regCount = 0; | - | ||||||
293 | public: | - | ||||||
294 | int currentReg = 0; | - | ||||||
295 | private: | - | ||||||
296 | int startLine = 0; | - | ||||||
297 | int currentLine = 0; | - | ||||||
298 | bool debugMode = false; | - | ||||||
299 | - | |||||||
300 | int lastInstrType = -1; | - | ||||||
301 | Moth::Instr lastInstr; | - | ||||||
302 | }; | - | ||||||
303 | - | |||||||
304 | } | - | ||||||
305 | } | - | ||||||
306 | - | |||||||
307 | QT_END_NAMESPACE | - | ||||||
308 | - | |||||||
309 | #endif | - | ||||||
Source code | Switch to Preprocessed file |