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
qv4debuginstructions.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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
6
8
9namespace QV4 {
10namespace Moth {
11
12enum : int { NoJumpLine = -1, MultipleJumpLines = -2 };
13
14static int mergeJumpLines(int a, int b)
15{
16 if (a == NoJumpLine)
17 return b;
18 if (b == NoJumpLine || a == b)
19 return a;
20 return MultipleJumpLines;
21}
22
23static bool isFromOtherLine(int jumpLine, int line)
24{
25 return jumpLine == MultipleJumpLines || (jumpLine != NoJumpLine && jumpLine != line);
26}
27
28void DebugInstructions::insert(BytecodeGenerator *generator)
29{
30 DebugInstructions(generator).run();
31}
32
33void DebugInstructions::appendDebug(int line, const I &before)
34{
35 m_generator->instructions.append(makeInstruction(Instruction::Debug(), line, before.statement));
36}
37
38// The jump skips the Debug and the instruction after it, and lands on the instruction it belongs to.
39void DebugInstructions::appendJumpOverDebug(const I &before)
40{
41 QList<int> &labels = m_generator->labels;
42 I jump = makeJumpInstruction(Instruction::Jump(), before.line, before.statement);
43 jump.linkedLabel = labels.size();
44 labels.append(m_generator->instructions.size() + 2);
45 m_generator->labelReachable.append(true);
46 m_generator->instructions.append(jump);
47}
48
49void DebugInstructions::run()
50{
51 // Source locations are only stored for AOT compilation, which doesn't involve the debugger.
52 Q_ASSERT(!m_generator->m_sourceLocationTable);
53
54 const QList<I> instructions = std::exchange(m_generator->instructions, {});
55
56 QList<int> &labels = m_generator->labels;
57 const qsizetype labelCount = labels.size();
58
59 // The debugger checks for breakpoints when it executes a Debug instruction. A jump from
60 // another line has to land on one, or the target line is skipped. Record the lines of the
61 // jumps to each label. Only forward jumps count. Loops are entered by fall-through and have to
62 // take care of their backward jumps themselves. An exception handler is not entered by a
63 // jump, but when the exception happens.
64 std::vector<int> labelJumpLines(labelCount, NoJumpLine);
65 for (qsizetype i = 0, end = instructions.size(); i != end; ++i) {
66 const I &instruction = instructions[i];
67 if (instruction.linkedLabel == -1 || instruction.type == Instr::Type::SetUnwindHandler)
68 continue;
69 if (labels[instruction.linkedLabel] <= i)
70 continue;
71 int &jumpLine = labelJumpLines[instruction.linkedLabel];
72 jumpLine = mergeJumpLines(jumpLine, instruction.line);
73 }
74
75 // Several labels can be linked at the same position. Labels without a position are dead.
76 std::vector<int> positionJumpLines(instructions.size(), NoJumpLine);
77 for (qsizetype i = 0; i < labelCount; ++i) {
78 if (labels[i] < 0)
79 continue;
80 int &jumpLine = positionJumpLines[labels[i]];
81 jumpLine = mergeJumpLines(jumpLine, labelJumpLines[i]);
82 }
83
84 // Where each instruction ends up, and where the instructions inserted in front of it go.
85 struct Position
86 {
87 int first = -1;
88 int debug = -1;
89 int instruction = -1;
90 bool jumpOverDebug = false;
91 };
92 std::vector<Position> positions(instructions.size());
93
94 for (qsizetype i = 0, end = instructions.size(); i != end; ++i) {
95 Position &position = positions[i];
96 position.first = m_generator->instructions.size();
97 const I &instruction = instructions[i];
98
99 // Without an explicit location, the instruction is just a leftover with the line of the
100 // code before. Pausing on that line again would be wrong.
101 const bool jumpFromOtherLine = instruction.locationSetAfterLabel
102 && isFromOtherLine(positionJumpLines[i], instruction.line);
103
104 if (i == 0 || instruction.line != instructions[i - 1].line) {
105 appendDebug(instruction.line, instruction);
106 } else if (jumpFromOtherLine) {
107 // If the code before falls through to the instruction on the same line, it has passed
108 // a Debug for the line already. Let it jump over the new Debug.
109 if (!BytecodeGenerator::endsFlow(instructions[i - 1].type)) {
110 appendJumpOverDebug(instruction);
111 position.jumpOverDebug = true;
112 }
113 position.debug = m_generator->instructions.size();
114 appendDebug(instruction.line, instruction);
115 } else if (instruction.type == Instr::Type::Ret) {
116 // The negated line never matches a breakpoint.
117 appendDebug(-instruction.line, instruction);
118 }
119
120 position.instruction = m_generator->instructions.size();
121 m_generator->instructions.append(instruction);
122 }
123
124 for (qsizetype i = 0; i < labelCount; ++i) {
125 int &label = labels[i];
126 Q_ASSERT(label != BytecodeGenerator::Label::Unlinked);
127 // Unreachable labels don't have a position.
128 if (label == BytecodeGenerator::Label::Dead)
129 continue;
130 Q_ASSERT(label < instructions.size());
131 const Position &position = positions[label];
132 if (!position.jumpOverDebug) {
133 label = position.first;
134 continue;
135 }
136
137 // Loop heads, exception handlers, and the targets of jumps within the line are not entered
138 // by a jump from another line. They continue after the Debug.
139 label = isFromOtherLine(labelJumpLines[i], instructions[label].line)
140 ? position.debug
141 : position.instruction;
142 }
143}
144
145} // namespace Moth
146} // namespace QV4
147
148QT_END_NAMESPACE
Combined button and popup list for selecting options.
static bool isFromOtherLine(int jumpLine, int line)
static int mergeJumpLines(int a, int b)
Definition qjsvalue.h:24