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
qv4compilercontrolflow_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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#ifndef QV4COMPILERCONTROLFLOW_P_H
6#define QV4COMPILERCONTROLFLOW_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qv4codegen_p.h>
20#include <private/qqmljsast_p.h>
21#include <private/qv4bytecodegenerator_p.h>
22
23QT_BEGIN_NAMESPACE
24
25namespace QV4 {
26
27namespace Compiler {
28
33
41
47
48 struct UnwindTarget {
51
52 // If the unwind has to pass a finally block, the finally block continues
53 // the unwind afterwards, depending on the value of completionRegister.
55 int completion = 0;
56 };
57
61
62 ControlFlow(Codegen *cg, Type type)
63 : cg(cg), parent(cg->controlFlow), type(type)
64 {
65 cg->controlFlow = this;
66 }
67
68 virtual ~ControlFlow() {
69 cg->controlFlow = parent;
70 }
71
72 UnwindTarget unwindTarget(UnwindType type, const QString &label = QString())
73 {
74 Q_ASSERT(type == Break || type == Continue || type == Return);
75 ControlFlow *flow = this;
76 int level = 0;
77 while (flow) {
78 BytecodeGenerator::Label l = flow->getUnwindTarget(type, label);
79 if (l.isValid())
80 return UnwindTarget{l, level};
81 UnwindTarget intercepted;
82 if (flow->interceptUnwind(type, label, level, &intercepted))
83 return intercepted;
84 if (flow->requiresUnwind())
85 ++level;
86 flow = flow->parent;
87 }
88 if (type == Return)
89 return UnwindTarget{ cg->returnLabel(), level };
90 return UnwindTarget();
91 }
92
93 // Jumps to the target, passing the handlers in between.
94 // Remembers pending completion in completionRegister.
95 void unwind(const UnwindTarget &target)
96 {
97 if (target.completionRegister >= 0) {
98 Reference::fromConst(cg, QV4::Encode(target.completion))
99 .storeOnStack(target.completionRegister);
100 }
101 generator()->unwindToLabel(target.unwindLevel, target.linkLabel);
102 }
103
104 virtual QString label() const { return QString(); }
105
106 bool hasLoop() const {
107 const ControlFlow *flow = this;
108 while (flow) {
109 if (flow->type == Loop)
110 return true;
111 flow = flow->parent;
112 }
113 return false;
114 }
115
116protected:
117 virtual BytecodeGenerator::Label getUnwindTarget(UnwindType, const QString & = QString()) {
118 return BytecodeGenerator::Label();
119 }
120 virtual bool requiresUnwind() {
121 return false;
122 }
123 virtual bool interceptUnwind(UnwindType, const QString &, int, UnwindTarget *) { return false; }
124
125public:
127 return parent ? parent->unwindHandler() : nullptr;
128 }
129
131 return parentUnwindHandler();
132 }
133
134
135protected:
137 QString label;
138 if (cg->_labelledStatement) {
139 label = cg->_labelledStatement->label.toString();
140 cg->_labelledStatement = nullptr;
141 }
142 return label;
143 }
145 return cg->bytecodeGenerator;
146 }
147};
148
150{
152
153 ControlFlowUnwind(Codegen *cg, Type type)
154 : ControlFlow(cg, type)
155 {
156 }
157
159 {
160 unwindLabel = generator()->newExceptionHandler();
161 }
162
164 {
165 Q_ASSERT(requiresUnwind());
166
167 Instruction::UnwindDispatch dispatch;
168 generator()->addInstruction(dispatch);
169 }
170
172 return unwindLabel.isValid() ? &unwindLabel : parentUnwindHandler();
173 }
174};
175
177{
178 std::function<void()> cleanup = nullptr;
179
180 ControlFlowUnwindCleanup(Codegen *cg, std::function<void()> cleanup, Type type = Block)
181 : ControlFlowUnwind(cg, type), cleanup(cleanup)
182 {
183 if (cleanup) {
185 generator()->setUnwindHandler(&unwindLabel);
186 }
187 }
188
190 if (cleanup) {
191 unwindLabel.link();
192 cleanup();
193 generator()->setUnwindHandler(parentUnwindHandler());
195 }
196 }
197
198 bool requiresUnwind() override {
199 return cleanup != nullptr;
200 }
201};
202
204{
208
209 ControlFlowLoop(Codegen *cg, BytecodeGenerator::Label *breakLabel, BytecodeGenerator::Label *continueLabel = nullptr, std::function<void()> cleanup = nullptr)
211 {
212 }
213
214 BytecodeGenerator::Label getUnwindTarget(UnwindType type, const QString &label) override {
215 switch (type) {
216 case Break:
217 if (breakLabel && (label.isEmpty() || label == loopLabel))
218 return *breakLabel;
219 break;
220 case Continue:
221 if (continueLabel && (label.isEmpty() || label == loopLabel))
222 return *continueLabel;
223 break;
224 default:
225 break;
226 }
227 return BytecodeGenerator::Label();
228 }
229
230 QString label() const override { return loopLabel; }
231};
232
233
235{
236 ControlFlowWith(Codegen *cg)
238 {
240
241 // assumes the with object is in the accumulator
242 Instruction::PushWithContext pushScope;
243 generator()->addInstruction(pushScope);
244 generator()->setUnwindHandler(&unwindLabel);
245 }
246
248 // emit code for unwinding
249 unwindLabel.link();
250
251 generator()->setUnwindHandler(parentUnwindHandler());
252 Instruction::PopContext pop;
253 generator()->addInstruction(pop);
254
256 }
257
258 bool requiresUnwind() override {
259 return true;
260 }
261
262
263};
264
266{
267 ControlFlowBlock(Codegen *cg, QQmlJS::AST::Node *ast)
269 {
270 block = cg->enterBlock(ast);
271 block->emitBlockHeader(cg);
272
273 if (block->requiresExecutionContext) {
275 generator()->setUnwindHandler(&unwindLabel);
276 }
277 }
278
279 virtual ~ControlFlowBlock() {
280 // emit code for unwinding
281 if (block->requiresExecutionContext) {
282 unwindLabel.link();
283 generator()->setUnwindHandler(parentUnwindHandler());
284 }
285
286 block->emitBlockFooter(cg);
287
288 if (block->requiresExecutionContext )
290 cg->leaveBlock();
291 }
292
293 virtual bool requiresUnwind() override {
294 return block->requiresExecutionContext;
295 }
296
298};
299
301{
303 bool insideCatch = false;
305
306 ControlFlowCatch(Codegen *cg, QQmlJS::AST::Catch *catchExpression)
309 {
310 generator()->setUnwindHandler(&exceptionLabel);
311 }
312
313 virtual bool requiresUnwind() override {
314 return true;
315 }
316
318 return insideCatch ? &unwindLabel : &exceptionLabel;
319 }
320
322 // emit code for unwinding
323 insideCatch = true;
325
326 Codegen::RegisterScope scope(cg);
327
328 // exceptions inside the try block go here
329 exceptionLabel.link();
330 BytecodeGenerator::Jump noException = generator()->jumpNoException();
331
332 Context *block = cg->enterBlock(catchExpression);
333
334 block->emitBlockHeader(cg);
335
336 generator()->setUnwindHandler(&unwindLabel);
337
338 if (catchExpression->patternElement->bindingIdentifier.isEmpty()) {
339 // destructuring pattern
340 cg->initializeAndDestructureBindingElement(
341 catchExpression->patternElement,
342 Reference::fromName(cg, QStringLiteral("@caught")), true);
343 }
344 // skip the additional block
345 cg->statementList(catchExpression->statement->statements);
346
347 // exceptions inside catch and break/return statements go here
348 unwindLabel.link();
349 block->emitBlockFooter(cg);
350
351 cg->leaveBlock();
352
353 noException.link();
354 generator()->setUnwindHandler(parentUnwindHandler());
355
357 insideCatch = false;
358 }
359};
360
362{
363 // A break, continue or return that passes the finally block does not leave anything in the
364 // stack frame while the finally block runs. Instead, it stores a number identifying itself in
365 // completionRegister and unwinds to the start of the finally block. After the finally block,
366 // we find the original unwind by that number and continue it.
372
374 bool insideFinally = false;
377
378 ControlFlowFinally(Codegen *cg, QQmlJS::AST::Finally *finally, bool hasCatchBlock)
380 {
381 Q_ASSERT(finally != nullptr);
383
384 // The register has to live as long as the finally block, not just the try block.
385 completionRegister = generator()->newRegister();
386
387 // No need to set the handler for the finally now if there is a catch block.
388 // In that case, a handler for the latter will be set immediately after this.
389 if (!hasCatchBlock) {
390 generator()->setUnwindHandler(&unwindLabel);
391 }
392 }
393
394 virtual bool requiresUnwind() override {
395 return !insideFinally;
396 }
397
398 bool interceptUnwind(UnwindType type, const QString &label, int level,
399 UnwindTarget *target) override
400 {
401 if (insideFinally)
402 return false;
403
404 const auto it =
405 std::find_if(completions.cbegin(), completions.cend(), [&](const Completion &c) {
406 return c.type == type && c.label == label;
407 });
408 const qsizetype index =
409 it == completions.cend() ? completions.size() : it - completions.cbegin();
410 if (it == completions.cend())
411 completions.append({ label, type });
412
413 target->linkLabel = unwindLabel;
414 target->unwindLevel = level;
416 target->completion = int(index) + 1;
417 return true;
418 }
419
421 return insideFinally ? parentUnwindHandler() : ControlFlowUnwind::unwindHandler();
422 }
423
425 if (!completions.isEmpty())
426 Reference::fromConst(cg, QV4::Encode(0)).storeOnStack(completionRegister);
427
428 // emit code for unwinding
429 unwindLabel.link();
430
431 Codegen::RegisterScope scope(cg);
432
433 insideFinally = true;
434 int returnValueTemp = -1;
435 if (cg->requiresReturnValue) {
436 returnValueTemp = generator()->newRegister();
437 Instruction::MoveReg move;
438 move.srcReg = cg->_returnAddress;
439 move.destReg = returnValueTemp;
440 generator()->addInstruction(move);
441 }
442 int exceptionTemp = generator()->newRegister();
443 Instruction::GetException instr;
444 generator()->addInstruction(instr);
445 Reference::fromStackSlot(cg, exceptionTemp).storeConsumeAccumulator();
446
447 generator()->setUnwindHandler(parentUnwindHandler());
448 cg->statement(finally->statement);
449 insideFinally = false;
450
451 if (cg->requiresReturnValue) {
452 Instruction::MoveReg move;
453 move.srcReg = returnValueTemp;
454 move.destReg = cg->_returnAddress;
455 generator()->addInstruction(move);
456 }
457 Reference::fromStackSlot(cg, exceptionTemp).loadInAccumulator();
458 Instruction::SetException setException;
459 generator()->addInstruction(setException);
460
462
463 // A pending exception is thrown by the UnwindDispatch above. Otherwise, continue with
464 // the break, continue or return that brought us here, if any.
465 insideFinally = true;
466 for (qsizetype i = 0, end = completions.size(); i != end; ++i) {
467 const Completion &completion = completions.at(i);
468 BytecodeGenerator::Label next = generator()->newLabel();
469 Reference::fromConst(cg, QV4::Encode(int(i) + 1)).loadInAccumulator();
470 generator()->jumpStrictNotEqual(Moth::StackSlot::createRegister(completionRegister),
471 next);
472 const UnwindTarget target = unwindTarget(completion.type, completion.label);
473 Q_ASSERT(target.linkLabel.isValid());
474 unwind(target);
475 next.link();
476 }
477 insideFinally = false;
478 }
479};
480
481} } // QV4::Compiler namespace
482
483QT_END_NAMESPACE
484
485#endif
Definition qjsvalue.h:24
ControlFlowBlock(Codegen *cg, QQmlJS::AST::Node *ast)
BytecodeGenerator::ExceptionHandler exceptionLabel
ControlFlowCatch(Codegen *cg, QQmlJS::AST::Catch *catchExpression)
BytecodeGenerator::ExceptionHandler * unwindHandler() override
bool interceptUnwind(UnwindType type, const QString &label, int level, UnwindTarget *target) override
BytecodeGenerator::ExceptionHandler * unwindHandler() override
QVarLengthArray< Completion, 4 > completions
ControlFlowFinally(Codegen *cg, QQmlJS::AST::Finally *finally, bool hasCatchBlock)
ControlFlowLoop(Codegen *cg, BytecodeGenerator::Label *breakLabel, BytecodeGenerator::Label *continueLabel=nullptr, std::function< void()> cleanup=nullptr)
BytecodeGenerator::Label * continueLabel
BytecodeGenerator::Label getUnwindTarget(UnwindType type, const QString &label) override
BytecodeGenerator::Label * breakLabel
ControlFlowUnwindCleanup(Codegen *cg, std::function< void()> cleanup, Type type=Block)
BytecodeGenerator::ExceptionHandler unwindLabel
virtual BytecodeGenerator::ExceptionHandler * unwindHandler() override
ControlFlow(Codegen *cg, Type type)
UnwindTarget unwindTarget(UnwindType type, const QString &label=QString())
void unwind(const UnwindTarget &target)
virtual BytecodeGenerator::ExceptionHandler * unwindHandler()
BytecodeGenerator * generator() const
virtual bool interceptUnwind(UnwindType, const QString &, int, UnwindTarget *)
BytecodeGenerator::ExceptionHandler * parentUnwindHandler()
virtual BytecodeGenerator::Label getUnwindTarget(UnwindType, const QString &=QString())