1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qv4compiler_p.h"
43 #include "qv4compiler_p_p.h"
44 #include "qv4program_p.h"
46 #include "qv4irbuilder_p.h"
48 #include <private/qqmlglobal_p.h>
49 #include <private/qqmljsast_p.h>
50 #include <private/qqmlaccessors_p.h>
51 #include <private/qqmljsengine_p.h>
55 DEFINE_BOOL_CONFIG_OPTION(bindingsDump, QML_BINDINGS_DUMP)
56 DEFINE_BOOL_CONFIG_OPTION(qmlDisableOptimizer, QML_DISABLE_OPTIMIZER)
57 DEFINE_BOOL_CONFIG_OPTION(qmlExperimental, QML_EXPERIMENTAL)
58 DEFINE_BOOL_CONFIG_OPTION(qmlVerboseCompiler, QML_VERBOSE_COMPILER)
59 DEFINE_BOOL_CONFIG_OPTION(qmlBindingsTestEnv, QML_BINDINGS_TEST)
61 static bool qmlBindingsTest = false;
62 static bool qmlEnableV4 = true;
64 using namespace QQmlJS;
65 QV4CompilerPrivate::QV4CompilerPrivate()
66 : _function(0) , _block(0) , _discarded(false), registerCount(0)
73 void QV4CompilerPrivate::trace(int line, int column)
77 this->currentReg = _function->tempCount;
78 this->registerCount = qMax(this->registerCount, this->currentReg);
80 foreach (IR::BasicBlock *bb, _function->basicBlocks) {
81 if (! bb->isTerminated() && (bb->index + 1) < _function->basicBlocks.size())
82 bb->JUMP(_function->basicBlocks.at(bb->index + 1));
85 QVector<IR::BasicBlock *> blocks;
87 currentBlockMask = 0x00000001;
90 for (int i = 0; !_discarded && i < blocks.size(); ++i) {
91 IR::BasicBlock *block = blocks.at(i);
92 IR::BasicBlock *next = i + 1 < blocks.size() ? blocks.at(i + 1) : 0;
93 if (IR::Stmt *terminator = block->terminator()) {
94 if (IR::CJump *cj = terminator->asCJump()) {
95 if (cj->iffalse != next) {
96 IR::Jump *jump = _function->pool->New<IR::Jump>();
97 jump->init(cj->iffalse);
98 block->statements.append(jump);
100 } else if (IR::Jump *j = terminator->asJump()) {
101 if (j->target == next) {
102 block->statements.resize(block->statements.size() - 1);
107 block->offset = bytecode.size();
109 if (bytecode.isEmpty()) {
110 if (qmlBindingsTest || bindingsDump()) {
117 if (qmlBindingsTest) {
118 QString str = expression->expression.asScript();
119 QByteArray strdata((const char *)str.constData(), str.length() * sizeof(QChar));
120 int offset = data.count();
123 Instr::EnableV4Test test;
125 test.offset = offset;
126 test.length = str.length();
132 int patchesCount = patches.count();
133 qSwap(usedSubscriptionIdsChanged, usic);
135 int blockopIndex = bytecode.size();
136 Instr::Block blockop;
137 blockop.block = currentBlockMask;
140 foreach (IR::Stmt *s, block->statements) {
145 qSwap(usedSubscriptionIdsChanged, usic);
148 if (currentBlockMask == 0x80000000) {
152 currentBlockMask <<= 1;
153 } else if (! _discarded) {
154 const int adjust = bytecode.remove(blockopIndex);
156 for (int ii = patchesCount; ii < patches.count(); ++ii)
157 patches[ii].offset -= adjust;
161 #ifdef DEBUG_IR_STRUCTURE
163 for (int i = 0; i < blocks.size(); ++i) {
164 dump.basicblock(blocks.at(i));
171 foreach (const Patch &patch, patches) {
172 V4Instr &instr = bytecode[patch.offset];
173 int size = V4Instr::size(instructionType(&instr));
174 instr.branchop.offset = patch.block->offset - patch.offset - size;
181 void QV4CompilerPrivate::trace(QVector<IR::BasicBlock *> *blocks)
183 for (int i = 0; i < _function->basicBlocks.size(); ++i) {
184 IR::BasicBlock *block = _function->basicBlocks.at(i);
186 while (! blocks->contains(block)) {
187 blocks->append(block);
189 if (IR::Stmt *terminator = block->terminator()) {
190 if (IR::CJump *cj = terminator->asCJump())
192 else if (IR::Jump *j = terminator->asJump())
199 void QV4CompilerPrivate::traceExpression(IR::Expr *e, quint8 r)
204 qSwap(currentReg, r);
206 qSwap(currentReg, r);
213 void QV4CompilerPrivate::visitConst(IR::Const *e)
244 if (qmlVerboseCompiler())
245 qWarning() << Q_FUNC_INFO << "unexpected type";
250 void QV4CompilerPrivate::visitString(IR::String *e)
252 registerLiteralString(currentReg, e->value);
255 void QV4CompilerPrivate::visitName(IR::Name *e)
258 // fetch the object and store it in reg.
259 traceExpression(e->base, currentReg);
261 _subscribeName.clear();
264 if (e->storage == IR::Name::RootStorage) {
266 Instr::LoadRoot instr;
267 instr.reg = currentReg;
270 if (e->symbol == IR::Name::IdObject) {
271 // The ID is a reference to the root object
275 } else if (e->storage == IR::Name::ScopeStorage) {
277 Instr::LoadScope instr;
278 instr.reg = currentReg;
281 _subscribeName << contextName();
283 } else if (e->storage == IR::Name::IdStorage) {
286 instr.reg = currentReg;
287 instr.index = e->idObject->idIndex;
290 _subscribeName << QLatin1String("$$$ID_") + *e->id;
292 if (blockNeedsSubscription(_subscribeName)) {
293 Instr::SubscribeId sub;
294 sub.reg = currentReg;
295 sub.offset = subscriptionIndex(_subscribeName);
296 sub.index = instr.index;
306 case IR::Name::Unbound:
307 case IR::Name::IdObject:
309 case IR::Name::Object: {
310 Q_ASSERT(!"Unreachable");
314 case IR::Name::AttachType: {
315 _subscribeName << *e->id;
317 Instr::LoadAttached attached;
318 attached.output = currentReg;
319 attached.reg = currentReg;
320 attached.exceptionId = exceptionId(e->line, e->column);
321 if (e->declarativeType->attachedPropertiesId() == -1)
323 attached.id = e->declarativeType->attachedPropertiesId();
327 case IR::Name::ModuleObject: {
329 Existing module object lookup methods include:
330 1. string -> module object (search via importCache->query(name))
331 2. QQmlMetaType::ModuleApi -> module object (via QQmlEnginePrivate::moduleApiInstance() cache)
332 We currently use 1, which is not ideal for performance
334 _subscribeName << *e->id;
336 registerLiteralString(currentReg, e->id);
338 Instr::LoadModuleObject module;
339 module.reg = currentReg;
343 case IR::Name::Property: {
344 _subscribeName << *e->id;
346 if (e->property->coreIndex == -1) {
348 e->property->load(prop, QQmlEnginePrivate::get(engine));
351 const int propTy = e->property->propType;
352 QQmlRegisterType regType;
355 case QMetaType::QReal:
358 case QMetaType::Bool:
364 case QMetaType::QString:
365 regType = QStringType;
367 case QMetaType::QUrl:
370 case QMetaType::QColor:
371 regType = QColorType;
375 if (propTy == QQmlMetaType::QQuickAnchorLineMetaTypeId()) {
376 regType = PODValueType;
377 } else if (QQmlMetaType::isQObject(propTy)) {
378 regType = QObjectStarType;
380 if (qmlVerboseCompiler())
381 qWarning() << "Discard unsupported property type:" << QMetaType::typeName(propTy);
382 discard(); // Unsupported type
389 if (e->property->hasAccessors()) {
390 Instr::FetchAndSubscribe fetch;
391 fetch.reg = currentReg;
392 fetch.subscription = subscriptionIndex(_subscribeName);
393 fetch.exceptionId = exceptionId(e->line, e->column);
394 fetch.valueType = regType;
395 fetch.property = *e->property;
398 if (blockNeedsSubscription(_subscribeName) && e->property->notifyIndex != -1) {
399 Instr::Subscribe sub;
400 sub.reg = currentReg;
401 sub.offset = subscriptionIndex(_subscribeName);
402 sub.index = e->property->notifyIndex;
407 fetch.reg = currentReg;
408 fetch.index = e->property->coreIndex;
409 fetch.exceptionId = exceptionId(e->line, e->column);
410 fetch.valueType = regType;
418 void QV4CompilerPrivate::visitTemp(IR::Temp *e)
420 if (currentReg != e->index) {
428 void QV4CompilerPrivate::visitUnop(IR::Unop *e)
430 quint8 src = currentReg;
432 if (IR::Temp *temp = e->expr->asTemp()) {
435 traceExpression(e->expr, src);
440 Q_ASSERT(!"unreachable");
444 convertToBool(e->expr, src);
445 if (src != currentReg) {
455 convertToBool(e->expr, src);
456 i.output = currentReg;
462 if (e->expr->type == IR::RealType) {
463 Instr::UnaryMinusReal i;
464 i.output = currentReg;
467 } else if (e->expr->type == IR::IntType) {
468 convertToReal(e->expr, currentReg);
469 Instr::UnaryMinusReal i;
470 i.output = currentReg;
479 if (e->expr->type == IR::RealType) {
480 Instr::UnaryPlusReal i;
481 i.output = currentReg;
484 } else if (e->expr->type == IR::IntType) {
485 convertToReal(e->expr, currentReg);
486 Instr::UnaryPlusReal i;
487 i.output = currentReg;
517 case IR::OpStrictEqual:
518 case IR::OpStrictNotEqual:
521 Q_ASSERT(!"unreachable");
526 void QV4CompilerPrivate::convertToReal(IR::Expr *expr, int reg)
528 if (expr->type == IR::RealType)
531 switch (expr->type) {
533 Instr::ConvertBoolToReal i;
534 i.output = i.src = reg;
539 Instr::ConvertIntToReal i;
540 i.output = i.src = reg;
554 void QV4CompilerPrivate::convertToInt(IR::Expr *expr, int reg)
556 if (expr->type == IR::IntType)
559 switch (expr->type) {
561 Instr::ConvertBoolToInt i;
562 i.output = i.src = reg;
571 Instr::ConvertRealToInt i;
572 i.output = i.src = reg;
582 void QV4CompilerPrivate::convertToBool(IR::Expr *expr, int reg)
584 if (expr->type == IR::BoolType)
587 switch (expr->type) {
593 Instr::ConvertIntToBool i;
594 i.output = i.src = reg;
599 Instr::ConvertRealToBool i;
600 i.output = i.src = reg;
604 case IR::StringType: {
605 Instr::ConvertStringToBool i;
606 i.output = i.src = reg;
610 case IR::ColorType: {
611 Instr::ConvertColorToBool i;
612 i.output = i.src = reg;
622 quint8 QV4CompilerPrivate::instructionOpcode(IR::Binop *e)
626 return V4Instr::Noop;
633 return V4Instr::Noop;
636 return V4Instr::BitAndInt;
639 return V4Instr::BitOrInt;
642 return V4Instr::BitXorInt;
645 if (e->type == IR::StringType)
646 return V4Instr::AddString;
647 return V4Instr::AddReal;
650 return V4Instr::SubReal;
653 return V4Instr::MulReal;
656 return V4Instr::DivReal;
659 return V4Instr::ModReal;
662 return V4Instr::LShiftInt;
665 return V4Instr::RShiftInt;
668 return V4Instr::URShiftInt;
671 if (e->left->type == IR::StringType)
672 return V4Instr::GtString;
673 return V4Instr::GtReal;
676 if (e->left->type == IR::StringType)
677 return V4Instr::LtString;
678 return V4Instr::LtReal;
681 if (e->left->type == IR::StringType)
682 return V4Instr::GeString;
683 return V4Instr::GeReal;
686 if (e->left->type == IR::StringType)
687 return V4Instr::LeString;
688 return V4Instr::LeReal;
691 if (e->left->type == IR::ObjectType || e->right->type == IR::ObjectType)
692 return V4Instr::EqualObject;
693 if (e->left->type == IR::StringType)
694 return V4Instr::EqualString;
695 return V4Instr::EqualReal;
698 if (e->left->type == IR::ObjectType || e->right->type == IR::ObjectType)
699 return V4Instr::NotEqualObject;
700 if (e->left->type == IR::StringType)
701 return V4Instr::NotEqualString;
702 return V4Instr::NotEqualReal;
704 case IR::OpStrictEqual:
705 if (e->left->type == IR::ObjectType || e->right->type == IR::ObjectType)
706 return V4Instr::StrictEqualObject;
707 if (e->left->type == IR::StringType)
708 return V4Instr::StrictEqualString;
709 return V4Instr::StrictEqualReal;
711 case IR::OpStrictNotEqual:
712 if (e->left->type == IR::ObjectType || e->right->type == IR::ObjectType)
713 return V4Instr::StrictNotEqualObject;
714 if (e->left->type == IR::StringType)
715 return V4Instr::StrictNotEqualString;
716 return V4Instr::StrictNotEqualReal;
720 return V4Instr::Noop;
724 return V4Instr::Noop;
727 void QV4CompilerPrivate::visitBinop(IR::Binop *e)
729 if (e->type == IR::InvalidType) {
734 int left = currentReg;
735 int right = currentReg + 1;
737 traceExpression(e->left, left);
738 traceExpression(e->right, right);
740 // At this point it is possible that the type of the
741 // subexpressions is different. This can happen because
742 // we keep BINOP expressions in HIR.
764 convertToInt(e->left, left);
765 convertToInt(e->right, right);
769 if (e->type != IR::StringType) {
770 convertToReal(e->left, left);
771 convertToReal(e->right, right);
779 convertToReal(e->left, left);
780 convertToReal(e->right, right);
789 case IR::OpStrictEqual:
790 case IR::OpStrictNotEqual:
791 if (e->left->type >= IR::FirstNumberType) {
792 convertToReal(e->left, left);
793 convertToReal(e->right, right);
799 discard(); // ### unreachable
803 const quint8 opcode = instructionOpcode(e);
804 if (opcode != V4Instr::Noop) {
806 instr.binaryop.output = currentReg;
807 instr.binaryop.left = left;
808 instr.binaryop.right = right;
809 gen(static_cast<V4Instr::Type>(opcode), instr);
813 void QV4CompilerPrivate::visitCall(IR::Call *call)
815 if (IR::Name *name = call->base->asName()) {
816 IR::Expr *arg = call->onlyArgument();
817 if (arg != 0 && arg->type == IR::RealType) {
818 traceExpression(arg, currentReg);
820 switch (name->builtin) {
821 case IR::NoBuiltinSymbol:
824 case IR::MathSinBultinFunction: {
825 Instr::MathSinReal i;
826 i.output = i.src = currentReg;
830 case IR::MathCosBultinFunction: {
831 Instr::MathCosReal i;
832 i.output = i.src = currentReg;
836 case IR::MathAbsBuiltinFunction: {
837 Instr::MathAbsReal i;
838 i.output = i.src = currentReg;
842 case IR::MathRoundBultinFunction: {
843 Instr::MathRoundReal i;
844 i.output = i.src = currentReg;
848 case IR::MathFloorBultinFunction: {
849 Instr::MathFloorReal i;
850 i.output = i.src = currentReg;
854 case IR::MathCeilBuiltinFunction: {
855 Instr::MathCeilReal i;
856 i.output = i.src = currentReg;
860 case IR::MathPIBuiltinConstant:
865 if (name->builtin == IR::MathMaxBuiltinFunction ||
866 name->builtin == IR::MathMinBuiltinFunction) {
868 //only handles the most common case of exactly two arguments
869 if (call->args && call->args->next && !call->args->next->next) {
870 IR::Expr *arg1 = call->args->expr;
871 IR::Expr *arg2 = call->args->next->expr;
873 if (arg1 != 0 && arg1->type == IR::RealType &&
874 arg2 != 0 && arg2->type == IR::RealType) {
876 traceExpression(arg1, currentReg);
877 traceExpression(arg2, currentReg + 1);
879 if (name->builtin == IR::MathMaxBuiltinFunction) {
880 Instr::MathMaxReal i;
882 i.right = currentReg + 1;
883 i.output = currentReg;
886 } else if (name->builtin == IR::MathMinBuiltinFunction) {
887 Instr::MathMinReal i;
889 i.right = currentReg + 1;
890 i.output = currentReg;
900 if (qmlVerboseCompiler())
901 qWarning() << "TODO:" << Q_FUNC_INFO << __LINE__;
909 void QV4CompilerPrivate::visitExp(IR::Exp *s)
911 traceExpression(s->expr, currentReg);
914 void QV4CompilerPrivate::visitMove(IR::Move *s)
916 IR::Temp *target = s->target->asTemp();
917 Q_ASSERT(target != 0);
919 quint8 dest = target->index;
921 if (target->type != s->source->type) {
924 if (IR::Temp *t = s->source->asTemp())
927 traceExpression(s->source, dest);
929 V4Instr::Type opcode = V4Instr::Noop;
930 IR::Type targetTy = s->target->type;
931 IR::Type sourceTy = s->source->type;
933 if (sourceTy == IR::UrlType) {
937 // nothing to do. V4 will generate optimized
938 // url-to-xxx conversions.
941 // generate a UrlToString conversion and fix
942 // the type of the source expression.
944 conv.unaryop.output = V4Instr::ConvertUrlToString;
945 conv.unaryop.src = src;
948 sourceTy = IR::StringType;
954 if (targetTy == IR::BoolType) {
956 case IR::IntType: opcode = V4Instr::ConvertIntToBool; break;
957 case IR::RealType: opcode = V4Instr::ConvertRealToBool; break;
958 case IR::StringType: opcode = V4Instr::ConvertStringToBool; break;
959 case IR::UrlType: opcode = V4Instr::ConvertUrlToBool; break;
960 case IR::ColorType: opcode = V4Instr::ConvertColorToBool; break;
963 } else if (targetTy == IR::IntType) {
965 case IR::BoolType: opcode = V4Instr::ConvertBoolToInt; break;
967 if (s->isMoveForReturn)
968 opcode = V4Instr::MathRoundReal;
970 opcode = V4Instr::ConvertRealToInt;
973 case IR::StringType: opcode = V4Instr::ConvertStringToInt; break;
976 } else if (targetTy == IR::RealType) {
978 case IR::BoolType: opcode = V4Instr::ConvertBoolToReal; break;
979 case IR::IntType: opcode = V4Instr::ConvertIntToReal; break;
980 case IR::StringType: opcode = V4Instr::ConvertStringToReal; break;
983 } else if (targetTy == IR::StringType) {
985 case IR::BoolType: opcode = V4Instr::ConvertBoolToString; break;
986 case IR::IntType: opcode = V4Instr::ConvertIntToString; break;
987 case IR::RealType: opcode = V4Instr::ConvertRealToString; break;
988 case IR::UrlType: opcode = V4Instr::ConvertUrlToString; break;
989 case IR::ColorType: opcode = V4Instr::ConvertColorToString; break;
992 } else if (targetTy == IR::UrlType) {
993 V4Instr convToString;
994 convToString.unaryop.output = dest;
995 convToString.unaryop.src = src;
997 // try to convert the source expression to a string.
999 case IR::BoolType: gen(V4Instr::ConvertBoolToString, convToString); sourceTy = IR::StringType; break;
1000 case IR::IntType: gen(V4Instr::ConvertIntToString, convToString); sourceTy = IR::StringType; break;
1001 case IR::RealType: gen(V4Instr::ConvertRealToString, convToString); sourceTy = IR::StringType; break;
1002 case IR::ColorType: gen(V4Instr::ConvertColorToString, convToString); sourceTy = IR::StringType; break;
1006 if (sourceTy == IR::StringType)
1007 opcode = V4Instr::ConvertStringToUrl;
1008 } else if (targetTy == IR::ColorType) {
1010 case IR::StringType: opcode = V4Instr::ConvertStringToColor; break;
1014 if (opcode != V4Instr::Noop) {
1016 conv.unaryop.output = dest;
1017 conv.unaryop.src = src;
1020 if (s->isMoveForReturn && opcode == V4Instr::ConvertStringToUrl) {
1022 resolveUrl.unaryop.output = dest;
1023 resolveUrl.unaryop.src = dest;
1024 gen(V4Instr::ResolveUrl, resolveUrl);
1030 traceExpression(s->source, dest);
1034 void QV4CompilerPrivate::visitJump(IR::Jump *s)
1036 patches.append(Patch(s->target, bytecode.size()));
1039 i.offset = 0; // ### backpatch
1043 void QV4CompilerPrivate::visitCJump(IR::CJump *s)
1045 traceExpression(s->cond, currentReg);
1047 patches.append(Patch(s->iftrue, bytecode.size()));
1049 Instr::BranchTrue i;
1051 i.offset = 0; // ### backpatch
1055 void QV4CompilerPrivate::visitRet(IR::Ret *s)
1057 Q_ASSERT(s->expr != 0);
1059 int storeReg = currentReg;
1061 if (IR::Temp *temp = s->expr->asTemp()) {
1062 storeReg = temp->index;
1064 traceExpression(s->expr, storeReg);
1067 if (qmlBindingsTest) {
1068 Instr::TestV4Store test;
1069 test.reg = storeReg;
1071 case IR::StringType:
1072 test.regType = QMetaType::QString;
1075 test.regType = QMetaType::QUrl;
1078 test.regType = QMetaType::QColor;
1080 case IR::SGAnchorLineType:
1081 test.regType = QQmlMetaType::QQuickAnchorLineMetaTypeId();
1083 case IR::ObjectType:
1084 test.regType = QMetaType::QObjectStar;
1087 test.regType = QMetaType::Bool;
1090 test.regType = QMetaType::Int;
1093 test.regType = QMetaType::QReal;
1104 store.index = expression->property->index;
1105 store.reg = storeReg;
1106 store.exceptionId = exceptionId(s->line, s->column);
1110 void QV4Compiler::dump(const QByteArray &programData)
1112 const QV4Program *program = (const QV4Program *)programData.constData();
1114 qWarning() << "Program.bindings:" << program->bindings;
1115 qWarning() << "Program.dataLength:" << program->dataLength;
1116 qWarning() << "Program.subscriptions:" << program->subscriptions;
1117 qWarning() << "Program.indentifiers:" << program->identifiers;
1119 const int programSize = program->instructionCount;
1120 const char *start = program->instructions();
1121 const char *end = start + programSize;
1123 bc.dump(start, end);
1127 Clear the state associated with attempting to compile a specific binding.
1128 This does not clear the global "committed binding" states.
1130 void QV4CompilerPrivate::resetInstanceState()
1132 data = committed.data;
1133 exceptions = committed.exceptions;
1134 usedSubscriptionIds.clear();
1135 subscriptionIds = committed.subscriptionIds;
1136 registeredStrings = committed.registeredStrings;
1144 Mark the last compile as successful, and add it to the "committed data"
1147 Returns the index for the committed binding.
1149 int QV4CompilerPrivate::commitCompile()
1151 int rv = committed.count();
1152 committed.offsets << committed.bytecode.count();
1153 committed.dependencies << usedSubscriptionIds;
1154 committed.bytecode.append(bytecode.constData(), bytecode.size());
1155 committed.data = data;
1156 committed.exceptions = exceptions;
1157 committed.subscriptionIds = subscriptionIds;
1158 committed.registeredStrings = registeredStrings;
1162 bool QV4CompilerPrivate::compile(QQmlJS::AST::Node *node)
1164 resetInstanceState();
1166 if (expression->property->type == -1)
1169 AST::SourceLocation location;
1170 if (AST::ExpressionNode *astExpression = node->expressionCast()) {
1171 location = astExpression->firstSourceLocation();
1172 } else if (AST::Statement *astStatement = node->statementCast()) {
1173 if (AST::Block *block = AST::cast<AST::Block *>(astStatement))
1174 location = block->lbraceToken;
1175 else if (AST::IfStatement *ifStmt = AST::cast<AST::IfStatement *>(astStatement))
1176 location = ifStmt->ifToken;
1183 IR::Function thisFunction(&pool), *function = &thisFunction;
1185 QV4IRBuilder irBuilder(expression, engine);
1186 if (!irBuilder(function, node))
1189 bool discarded = false;
1190 qSwap(_discarded, discarded);
1191 qSwap(_function, function);
1192 trace(location.startLine, location.startColumn);
1193 qSwap(_function, function);
1194 qSwap(_discarded, discarded);
1196 if (qmlVerboseCompiler()) {
1197 QTextStream qerr(stderr, QIODevice::WriteOnly);
1199 qerr << "======== TODO ====== " << endl;
1201 qerr << "==================== " << endl;
1202 qerr << "\tline: " << location.startLine
1203 << "\tcolumn: " << location.startColumn
1205 foreach (IR::BasicBlock *bb, function->basicBlocks)
1210 if (discarded || subscriptionIds.count() > 0xFFFF || registeredStrings.count() > 0xFFFF || registerCount > 31)
1217 int QV4CompilerPrivate::registerLiteralString(quint8 reg, const QStringRef &str)
1219 // ### string cleanup
1221 QByteArray strdata((const char *)str.constData(), str.length() * sizeof(QChar));
1222 int offset = data.count();
1225 Instr::LoadString string;
1227 string.offset = offset;
1228 string.length = str.length();
1234 // Returns an identifier offset
1235 int QV4CompilerPrivate::registerString(const QString &string)
1237 Q_ASSERT(!string.isEmpty());
1239 QPair<int, int> *iter = registeredStrings.value(string);
1242 quint32 len = string.length();
1243 QByteArray lendata((const char *)&len, sizeof(quint32));
1244 QByteArray strdata((const char *)string.constData(), string.length() * sizeof(QChar));
1245 strdata.prepend(lendata);
1246 int rv = data.count();
1249 iter = ®isteredStrings[string];
1250 *iter = qMakePair(registeredStrings.count(), rv);
1253 Instr::InitString reg;
1254 reg.offset = iter->first;
1255 reg.dataIdx = iter->second;
1261 Returns true if the current expression has not already subscribed to \a sub in currentBlockMask.
1263 bool QV4CompilerPrivate::blockNeedsSubscription(const QStringList &sub)
1265 QString str = sub.join(QLatin1String("."));
1267 int *iter = subscriptionIds.value(str);
1271 quint32 *uiter = usedSubscriptionIds.value(*iter);
1275 return !(*uiter & currentBlockMask);
1278 int QV4CompilerPrivate::subscriptionIndex(const QStringList &sub)
1280 QString str = sub.join(QLatin1String("."));
1281 int *iter = subscriptionIds.value(str);
1283 int count = subscriptionIds.count();
1284 iter = &subscriptionIds[str];
1287 quint32 &u = usedSubscriptionIds[*iter];
1288 if (!(u & currentBlockMask)) {
1289 u |= currentBlockMask;
1290 usedSubscriptionIdsChanged = true;
1295 quint32 QV4CompilerPrivate::subscriptionBlockMask(const QStringList &sub)
1297 QString str = sub.join(QLatin1String("."));
1299 int *iter = subscriptionIds.value(str);
1300 Q_ASSERT(iter != 0);
1302 quint32 *uiter = usedSubscriptionIds.value(*iter);
1303 Q_ASSERT(uiter != 0);
1308 quint8 QV4CompilerPrivate::exceptionId(quint32 line, quint32 column)
1311 if (exceptions.count() < 0xFF) {
1312 rv = (quint8)exceptions.count();
1316 exceptions.append(e);
1321 quint8 QV4CompilerPrivate::exceptionId(QQmlJS::AST::ExpressionNode *n)
1324 if (n && exceptions.count() < 0xFF) {
1325 QQmlJS::AST::SourceLocation l = n->firstSourceLocation();
1326 rv = exceptionId(l.startLine, l.startColumn);
1331 QV4Compiler::QV4Compiler()
1332 : d(new QV4CompilerPrivate)
1334 qmlBindingsTest |= qmlBindingsTestEnv();
1337 QV4Compiler::~QV4Compiler()
1343 Returns true if any bindings were compiled.
1345 bool QV4Compiler::isValid() const
1347 return !d->committed.bytecode.isEmpty();
1351 -1 on failure, otherwise the binding index to use.
1353 int QV4Compiler::compile(const Expression &expression, QQmlEnginePrivate *engine)
1355 if (!expression.expression.asAST()) return false;
1357 if (!qmlExperimental() && expression.property->isValueTypeSubProperty)
1360 if (qmlDisableOptimizer() || !qmlEnableV4)
1363 d->expression = &expression;
1366 if (d->compile(expression.expression.asAST())) {
1367 return d->commitCompile();
1373 QByteArray QV4CompilerPrivate::buildSignalTable() const
1375 QHash<int, QList<QPair<int, quint32> > > table;
1377 for (int ii = 0; ii < committed.count(); ++ii) {
1378 const QQmlAssociationList<int, quint32> &deps = committed.dependencies.at(ii);
1379 for (QQmlAssociationList<int, quint32>::const_iterator iter = deps.begin(); iter != deps.end(); ++iter)
1380 table[iter->first].append(qMakePair(ii, iter->second));
1383 QVector<quint32> header;
1384 QVector<quint32> data;
1385 for (int ii = 0; ii < committed.subscriptionIds.count(); ++ii) {
1386 header.append(committed.subscriptionIds.count() + data.count());
1387 const QList<QPair<int, quint32> > &bindings = table[ii];
1388 data.append(bindings.count());
1389 for (int jj = 0; jj < bindings.count(); ++jj) {
1390 data.append(bindings.at(jj).first);
1391 data.append(bindings.at(jj).second);
1396 return QByteArray((const char *)header.constData(), header.count() * sizeof(quint32));
1399 QByteArray QV4CompilerPrivate::buildExceptionData() const
1402 rv.resize(committed.exceptions.count() * sizeof(quint64));
1403 ::memcpy(rv.data(), committed.exceptions.constData(), rv.size());
1408 Returns the compiled program.
1410 QByteArray QV4Compiler::program() const
1412 QByteArray programData;
1416 prog.bindings = d->committed.count();
1419 QV4CompilerPrivate::Instr::Jump jump;
1422 for (int ii = 0; ii < d->committed.count(); ++ii) {
1423 jump.count = d->committed.count() - ii - 1;
1424 jump.count*= V4InstrMeta<V4Instr::Jump>::Size;
1425 jump.count+= d->committed.offsets.at(ii);
1430 QByteArray bytecode;
1431 bytecode.reserve(bc.size() + d->committed.bytecode.size());
1432 bytecode.append(bc.constData(), bc.size());
1433 bytecode.append(d->committed.bytecode.constData(), d->committed.bytecode.size());
1435 QByteArray data = d->committed.data;
1436 while (data.count() % 4) data.append('\0');
1437 prog.signalTableOffset = data.count();
1438 data += d->buildSignalTable();
1439 while (data.count() % 4) data.append('\0');
1440 prog.exceptionDataOffset = data.count();
1441 data += d->buildExceptionData();
1443 prog.dataLength = 4 * ((data.size() + 3) / 4);
1444 prog.subscriptions = d->committed.subscriptionIds.count();
1445 prog.identifiers = d->committed.registeredStrings.count();
1446 prog.instructionCount = bytecode.count();
1447 int size = sizeof(QV4Program) + bytecode.count();
1448 size += prog.dataLength;
1450 programData.resize(size);
1451 memcpy(programData.data(), &prog, sizeof(QV4Program));
1452 if (prog.dataLength)
1453 memcpy((char *)((QV4Program *)programData.data())->data(), data.constData(),
1455 memcpy((char *)((QV4Program *)programData.data())->instructions(), bytecode.constData(),
1459 if (bindingsDump()) {
1460 qWarning().nospace() << "Subscription slots:";
1462 for (QQmlAssociationList<QString, int>::ConstIterator iter = d->committed.subscriptionIds.begin();
1463 iter != d->committed.subscriptionIds.end();
1465 qWarning().nospace() << " " << iter->first << "\t-> " << iter->second;
1468 QV4Compiler::dump(programData);
1474 void QV4Compiler::enableBindingsTest(bool e)
1477 qmlBindingsTest = true;
1479 qmlBindingsTest = qmlBindingsTestEnv();
1482 void QV4Compiler::enableV4(bool e)