1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef QQMLJSCODEGENERATOR_P_H |
5 | #define QQMLJSCODEGENERATOR_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | |
17 | #include <private/qqmljscompiler_p.h> |
18 | #include <private/qqmljstypepropagator_p.h> |
19 | #include <private/qqmljstyperesolver_p.h> |
20 | |
21 | #include <private/qv4bytecodehandler_p.h> |
22 | #include <private/qv4codegen_p.h> |
23 | |
24 | #include <QtCore/qstring.h> |
25 | |
26 | #include <memory> |
27 | |
28 | QT_BEGIN_NAMESPACE |
29 | |
30 | class Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSCodeGenerator : public QQmlJSCompilePass |
31 | { |
32 | public: |
33 | QQmlJSCodeGenerator(const QV4::Compiler::Context *compilerContext, |
34 | const QV4::Compiler::JSUnitGenerator *unitGenerator, |
35 | const QQmlJSTypeResolver *typeResolver, |
36 | QQmlJSLogger *logger); |
37 | ~QQmlJSCodeGenerator() = default; |
38 | |
39 | QQmlJSAotFunction run(const Function *function, const InstructionAnnotations *annotations, |
40 | QQmlJS::DiagnosticMessage *error); |
41 | |
42 | protected: |
43 | struct CodegenState : public State |
44 | { |
45 | QString accumulatorVariableIn; |
46 | QString accumulatorVariableOut; |
47 | }; |
48 | |
49 | // This is an RAII helper we can use to automatically convert the result of "inflexible" |
50 | // operations to the desired type. For example GetLookup can only retrieve the type of |
51 | // the property we're looking up. If we want to store a different type, we need to convert. |
52 | struct Q_QMLCOMPILER_PRIVATE_EXPORT AccumulatorConverter |
53 | { |
54 | Q_DISABLE_COPY_MOVE(AccumulatorConverter); |
55 | AccumulatorConverter(QQmlJSCodeGenerator *generator); |
56 | ~AccumulatorConverter(); |
57 | |
58 | private: |
59 | const QQmlJSRegisterContent accumulatorOut; |
60 | const QString accumulatorVariableIn; |
61 | const QString accumulatorVariableOut; |
62 | QQmlJSCodeGenerator *generator = nullptr; |
63 | }; |
64 | |
65 | virtual QString metaObject(const QQmlJSScope::ConstPtr &objectType); |
66 | virtual QString metaType(const QQmlJSScope::ConstPtr &type); |
67 | |
68 | void generate_Ret() override; |
69 | void generate_Debug() override; |
70 | void generate_LoadConst(int index) override; |
71 | void generate_LoadZero() override; |
72 | void generate_LoadTrue() override; |
73 | void generate_LoadFalse() override; |
74 | void generate_LoadNull() override; |
75 | void generate_LoadUndefined() override; |
76 | void generate_LoadInt(int value) override; |
77 | void generate_MoveConst(int constIndex, int destTemp) override; |
78 | void generate_LoadReg(int reg) override; |
79 | void generate_StoreReg(int reg) override; |
80 | void generate_MoveReg(int srcReg, int destReg) override; |
81 | void generate_LoadImport(int index) override; |
82 | void generate_LoadLocal(int index) override; |
83 | void generate_StoreLocal(int index) override; |
84 | void generate_LoadScopedLocal(int scope, int index) override; |
85 | void generate_StoreScopedLocal(int scope, int index) override; |
86 | void generate_LoadRuntimeString(int stringId) override; |
87 | void generate_MoveRegExp(int regExpId, int destReg) override; |
88 | void generate_LoadClosure(int value) override; |
89 | void generate_LoadName(int nameIndex) override; |
90 | void generate_LoadGlobalLookup(int index) override; |
91 | void generate_LoadQmlContextPropertyLookup(int index) override; |
92 | void generate_StoreNameSloppy(int nameIndex) override; |
93 | void generate_StoreNameStrict(int name) override; |
94 | void generate_LoadElement(int base) override; |
95 | void generate_StoreElement(int base, int index) override; |
96 | void generate_LoadProperty(int nameIndex) override; |
97 | void generate_LoadOptionalProperty(int name, int offset) override; |
98 | void generate_GetLookup(int index) override; |
99 | void generate_GetOptionalLookup(int index, int offset) override; |
100 | void generate_StoreProperty(int name, int baseReg) override; |
101 | void generate_SetLookup(int index, int base) override; |
102 | void generate_LoadSuperProperty(int property) override; |
103 | void generate_StoreSuperProperty(int property) override; |
104 | void generate_Yield() override; |
105 | void generate_YieldStar() override; |
106 | void generate_Resume(int) override; |
107 | |
108 | void generate_CallValue(int name, int argc, int argv) override; |
109 | void generate_CallWithReceiver(int name, int thisObject, int argc, int argv) override; |
110 | void generate_CallProperty(int name, int base, int argc, int argv) override; |
111 | void generate_CallPropertyLookup(int lookupIndex, int base, int argc, int argv) override; |
112 | void generate_CallName(int name, int argc, int argv) override; |
113 | void generate_CallPossiblyDirectEval(int argc, int argv) override; |
114 | void generate_CallGlobalLookup(int index, int argc, int argv) override; |
115 | void generate_CallQmlContextPropertyLookup(int index, int argc, int argv) override; |
116 | void generate_CallWithSpread(int func, int thisObject, int argc, int argv) override; |
117 | void generate_TailCall(int func, int thisObject, int argc, int argv) override; |
118 | void generate_Construct(int func, int argc, int argv) override; |
119 | void generate_ConstructWithSpread(int func, int argc, int argv) override; |
120 | void generate_SetUnwindHandler(int offset) override; |
121 | void generate_UnwindDispatch() override; |
122 | void generate_UnwindToLabel(int level, int offset) override; |
123 | void generate_DeadTemporalZoneCheck(int name) override; |
124 | void generate_ThrowException() override; |
125 | void generate_GetException() override; |
126 | void generate_SetException() override; |
127 | void generate_CreateCallContext() override; |
128 | void generate_PushCatchContext(int index, int name) override; |
129 | void generate_PushWithContext() override; |
130 | void generate_PushBlockContext(int index) override; |
131 | void generate_CloneBlockContext() override; |
132 | void generate_PushScriptContext(int index) override; |
133 | void generate_PopScriptContext() override; |
134 | void generate_PopContext() override; |
135 | void generate_GetIterator(int iterator) override; |
136 | void generate_IteratorNext(int value, int done) override; |
137 | void generate_IteratorNextForYieldStar(int iterator, int object) override; |
138 | void generate_IteratorClose(int done) override; |
139 | void generate_DestructureRestElement() override; |
140 | void generate_DeleteProperty(int base, int index) override; |
141 | void generate_DeleteName(int name) override; |
142 | void generate_TypeofName(int name) override; |
143 | void generate_TypeofValue() override; |
144 | void generate_DeclareVar(int varName, int isDeletable) override; |
145 | void generate_DefineArray(int argc, int args) override; |
146 | void generate_DefineObjectLiteral(int internalClassId, int argc, int args) override; |
147 | void generate_CreateClass(int classIndex, int heritage, int computedNames) override; |
148 | void generate_CreateMappedArgumentsObject() override; |
149 | void generate_CreateUnmappedArgumentsObject() override; |
150 | void generate_CreateRestParameter(int argIndex) override; |
151 | void generate_ConvertThisToObject() override; |
152 | void generate_LoadSuperConstructor() override; |
153 | void generate_ToObject() override; |
154 | void generate_Jump(int offset) override; |
155 | void generate_JumpTrue(int offset) override; |
156 | void generate_JumpFalse(int offset) override; |
157 | void generate_JumpNoException(int offset) override; |
158 | void generate_JumpNotUndefined(int offset) override; |
159 | void generate_CheckException() override; |
160 | void generate_CmpEqNull() override; |
161 | void generate_CmpNeNull() override; |
162 | void generate_CmpEqInt(int lhs) override; |
163 | void generate_CmpNeInt(int lhs) override; |
164 | void generate_CmpEq(int lhs) override; |
165 | void generate_CmpNe(int lhs) override; |
166 | void generate_CmpGt(int lhs) override; |
167 | void generate_CmpGe(int lhs) override; |
168 | void generate_CmpLt(int lhs) override; |
169 | void generate_CmpLe(int lhs) override; |
170 | void generate_CmpStrictEqual(int lhs) override; |
171 | void generate_CmpStrictNotEqual(int lhs) override; |
172 | void generate_CmpIn(int lhs) override; |
173 | void generate_CmpInstanceOf(int lhs) override; |
174 | void generate_As(int lhs) override; |
175 | void generate_UNot() override; |
176 | void generate_UPlus() override; |
177 | void generate_UMinus() override; |
178 | void generate_UCompl() override; |
179 | void generate_Increment() override; |
180 | void generate_Decrement() override; |
181 | void generate_Add(int lhs) override; |
182 | void generate_BitAnd(int lhs) override; |
183 | void generate_BitOr(int lhs) override; |
184 | void generate_BitXor(int lhs) override; |
185 | void generate_UShr(int lhs) override; |
186 | void generate_Shr(int lhs) override; |
187 | void generate_Shl(int lhs) override; |
188 | void generate_BitAndConst(int rhs) override; |
189 | void generate_BitOrConst(int rhs) override; |
190 | void generate_BitXorConst(int rhs) override; |
191 | void generate_UShrConst(int rhs) override; |
192 | void generate_ShrConst(int value) override; |
193 | void generate_ShlConst(int rhs) override; |
194 | void generate_Exp(int lhs) override; |
195 | void generate_Mul(int lhs) override; |
196 | void generate_Div(int lhs) override; |
197 | void generate_Mod(int lhs) override; |
198 | void generate_Sub(int lhs) override; |
199 | void generate_InitializeBlockDeadTemporalZone(int firstReg, int count) override; |
200 | void generate_ThrowOnNullOrUndefined() override; |
201 | void generate_GetTemplateObject(int index) override; |
202 | |
203 | Verdict startInstruction(QV4::Moth::Instr::Type) override; |
204 | void endInstruction(QV4::Moth::Instr::Type) override; |
205 | |
206 | void addInclude(const QString &include) |
207 | { |
208 | Q_ASSERT(!include.isEmpty()); |
209 | m_includes.append(t: include); |
210 | } |
211 | |
212 | QString conversion(const QQmlJSRegisterContent &from, |
213 | const QQmlJSRegisterContent &to, |
214 | const QString &variable); |
215 | |
216 | QString conversion(const QQmlJSScope::ConstPtr &from, |
217 | const QQmlJSRegisterContent &to, |
218 | const QString &variable) |
219 | { |
220 | const QQmlJSScope::ConstPtr contained = m_typeResolver->containedType(container: to); |
221 | if (m_typeResolver->equals(a: to.storedType(), b: contained) |
222 | || m_typeResolver->isNumeric(type: to.storedType()) |
223 | || to.storedType()->isReferenceType() |
224 | || m_typeResolver->equals(a: from, b: contained)) { |
225 | // If: |
226 | // * the output is not actually wrapped at all, or |
227 | // * the output is a number (as there are no internals to a number) |
228 | // * the output is a QObject pointer, or |
229 | // * we merely wrap the value into a new container, |
230 | // we can convert by stored type. |
231 | return convertStored(from, to: to.storedType(), variable); |
232 | } else { |
233 | return convertContained(from: m_typeResolver->globalType(type: from), to, variable); |
234 | } |
235 | } |
236 | |
237 | QString convertStored(const QQmlJSScope::ConstPtr &from, |
238 | const QQmlJSScope::ConstPtr &to, |
239 | const QString &variable); |
240 | |
241 | QString convertContained(const QQmlJSRegisterContent &from, |
242 | const QQmlJSRegisterContent &to, |
243 | const QString &variable); |
244 | |
245 | QString errorReturnValue(); |
246 | void reject(const QString &thing); |
247 | |
248 | QString metaTypeFromType(const QQmlJSScope::ConstPtr &type) const; |
249 | QString metaTypeFromName(const QQmlJSScope::ConstPtr &type) const; |
250 | QString contentPointer(const QQmlJSRegisterContent &content, const QString &var); |
251 | QString contentType(const QQmlJSRegisterContent &content, const QString &var); |
252 | |
253 | void generateSetInstructionPointer(); |
254 | void generateLookup(const QString &lookup, const QString &initialization, |
255 | const QString &resultPreparation = QString()); |
256 | QString getLookupPreparation( |
257 | const QQmlJSRegisterContent &content, const QString &var, int lookup); |
258 | QString setLookupPreparation( |
259 | const QQmlJSRegisterContent &content, const QString &arg, int lookup); |
260 | void generateEnumLookup(int index); |
261 | |
262 | QString registerVariable(int index) const; |
263 | QString consumedRegisterVariable(int index) const; |
264 | QString consumedAccumulatorVariableIn() const; |
265 | |
266 | QString changedRegisterVariable() const; |
267 | QQmlJSRegisterContent registerType(int index) const; |
268 | |
269 | QString m_body; |
270 | CodegenState m_state; |
271 | |
272 | void resetState() { m_state = CodegenState(); } |
273 | |
274 | private: |
275 | void generateExceptionCheck(); |
276 | void generateEqualityOperation(int lhs, const QString &function, bool invert); |
277 | void generateCompareOperation(int lhs, const QString &cppOperator); |
278 | void generateArithmeticOperation(int lhs, const QString &cppOperator); |
279 | void generateShiftOperation(int lhs, const QString &cppOperator); |
280 | void generateArithmeticOperation( |
281 | const QString &lhs, const QString &rhs, const QString &cppOperator); |
282 | void generateArithmeticConstOperation(int lhsConst, const QString &cppOperator); |
283 | void generateJumpCodeWithTypeConversions(int relativeOffset); |
284 | void generateUnaryOperation(const QString &cppOperator); |
285 | void generateInPlaceOperation(const QString &cppOperator); |
286 | void generateMoveOutVar(const QString &outVar); |
287 | void generateTypeLookup(int index); |
288 | void generateVariantEqualityComparison(const QQmlJSRegisterContent &nonStorable, |
289 | const QString ®isterName, bool invert); |
290 | void rejectIfNonQObjectOut(const QString &error); |
291 | |
292 | QString eqIntExpression(int lhsConst); |
293 | QString argumentsList(int argc, int argv, QString *outVar); |
294 | QString castTargetName(const QQmlJSScope::ConstPtr &type) const; |
295 | |
296 | bool inlineStringMethod(const QString &name, int base, int argc, int argv); |
297 | bool inlineTranslateMethod(const QString &name, int argc, int argv); |
298 | bool inlineMathMethod(const QString &name, int argc, int argv); |
299 | bool inlineConsoleMethod(const QString &name, int argc, int argv); |
300 | bool inlineArrayMethod(const QString &name, int base, int argc, int argv); |
301 | |
302 | QQmlJSScope::ConstPtr mathObject() const |
303 | { |
304 | using namespace Qt::StringLiterals; |
305 | return m_typeResolver->jsGlobalObject()->property(name: u"Math"_s ).type(); |
306 | } |
307 | |
308 | QQmlJSScope::ConstPtr consoleObject() const |
309 | { |
310 | using namespace Qt::StringLiterals; |
311 | return m_typeResolver->jsGlobalObject()->property(name: u"console"_s ).type(); |
312 | } |
313 | |
314 | QQmlJSScope::ConstPtr arrayPrototype() const |
315 | { |
316 | return m_typeResolver->arrayType()->baseType(); |
317 | } |
318 | |
319 | QString resolveValueTypeContentPointer( |
320 | const QQmlJSScope::ConstPtr &required, const QQmlJSRegisterContent &actual, |
321 | const QString &variable, const QString &errorMessage); |
322 | QString resolveQObjectPointer( |
323 | const QQmlJSScope::ConstPtr &required, const QQmlJSRegisterContent &actual, |
324 | const QString &variable, const QString &errorMessage); |
325 | bool generateContentPointerCheck( |
326 | const QQmlJSScope::ConstPtr &required, const QQmlJSRegisterContent &actual, |
327 | const QString &variable, const QString &errorMessage); |
328 | |
329 | // map from instruction offset to sequential label number |
330 | QHash<int, QString> m_labels; |
331 | |
332 | const QV4::Compiler::Context *m_context = nullptr; |
333 | const InstructionAnnotations *m_annotations = nullptr; |
334 | |
335 | bool m_skipUntilNextLabel = false; |
336 | |
337 | QStringList m_includes; |
338 | |
339 | struct RegisterVariablesKey |
340 | { |
341 | QString internalName; |
342 | int registerIndex = -1; |
343 | |
344 | private: |
345 | friend size_t qHash(const RegisterVariablesKey &key, size_t seed = 0) noexcept |
346 | { |
347 | return qHashMulti(seed, args: key.internalName, args: key.registerIndex); |
348 | } |
349 | |
350 | friend bool operator==( |
351 | const RegisterVariablesKey &lhs, const RegisterVariablesKey &rhs) noexcept |
352 | { |
353 | return lhs.registerIndex == rhs.registerIndex && lhs.internalName == rhs.internalName; |
354 | } |
355 | |
356 | friend bool operator!=( |
357 | const RegisterVariablesKey &lhs, const RegisterVariablesKey &rhs) noexcept |
358 | { |
359 | return !(lhs == rhs); |
360 | } |
361 | }; |
362 | |
363 | struct RegisterVariablesValue |
364 | { |
365 | QString variableName; |
366 | QQmlJSScope::ConstPtr storedType; |
367 | int numTracked = 0; |
368 | }; |
369 | |
370 | QHash<RegisterVariablesKey, RegisterVariablesValue> m_registerVariables; |
371 | }; |
372 | |
373 | QT_END_NAMESPACE |
374 | |
375 | #endif // QQMLJSCODEGENERATOR_P_H |
376 | |