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)
238 if (qmlVerboseCompiler())
239 qWarning() << Q_FUNC_INFO << "unexpected type";
244 void QV4CompilerPrivate::visitString(IR::String *e)
246 registerLiteralString(currentReg, e->value);
249 void QV4CompilerPrivate::visitName(IR::Name *e)
252 // fetch the object and store it in reg.
253 traceExpression(e->base, currentReg);
255 _subscribeName.clear();
258 if (e->storage == IR::Name::RootStorage) {
260 Instr::LoadRoot instr;
261 instr.reg = currentReg;
264 if (e->symbol == IR::Name::IdObject) {
265 // The ID is a reference to the root object
269 } else if (e->storage == IR::Name::ScopeStorage) {
271 Instr::LoadScope instr;
272 instr.reg = currentReg;
275 _subscribeName << contextName();
277 } else if (e->storage == IR::Name::IdStorage) {
280 instr.reg = currentReg;
281 instr.index = e->idObject->idIndex;
284 _subscribeName << QLatin1String("$$$ID_") + *e->id;
286 if (blockNeedsSubscription(_subscribeName)) {
287 Instr::SubscribeId sub;
288 sub.reg = currentReg;
289 sub.offset = subscriptionIndex(_subscribeName);
290 sub.index = instr.index;
300 case IR::Name::Unbound:
301 case IR::Name::IdObject:
303 case IR::Name::Object: {
304 Q_ASSERT(!"Unreachable");
308 case IR::Name::AttachType: {
309 _subscribeName << *e->id;
311 Instr::LoadAttached attached;
312 attached.output = currentReg;
313 attached.reg = currentReg;
314 attached.exceptionId = exceptionId(e->line, e->column);
315 if (e->declarativeType->attachedPropertiesId() == -1)
317 attached.id = e->declarativeType->attachedPropertiesId();
321 case IR::Name::ModuleObject: {
323 Existing module object lookup methods include:
324 1. string -> module object (search via importCache->query(name))
325 2. QQmlMetaType::ModuleApi -> module object (via QQmlEnginePrivate::moduleApiInstance() cache)
326 We currently use 1, which is not ideal for performance
328 _subscribeName << *e->id;
330 registerLiteralString(currentReg, e->id);
332 Instr::LoadModuleObject module;
333 module.reg = currentReg;
337 case IR::Name::Property: {
338 _subscribeName << *e->id;
340 if (e->property->coreIndex == -1) {
342 e->property->load(prop, QQmlEnginePrivate::get(engine));
345 const int propTy = e->property->propType;
346 QQmlRegisterType regType;
349 case QMetaType::QReal:
352 case QMetaType::Bool:
358 case QMetaType::QString:
359 regType = QStringType;
361 case QMetaType::QUrl:
364 case QMetaType::QColor:
365 regType = QColorType;
369 if (propTy == QQmlMetaType::QQuickAnchorLineMetaTypeId()) {
370 regType = PODValueType;
371 } else if (QQmlMetaType::isQObject(propTy)) {
372 regType = QObjectStarType;
374 if (qmlVerboseCompiler())
375 qWarning() << "Discard unsupported property type:" << QMetaType::typeName(propTy);
376 discard(); // Unsupported type
383 if (e->property->hasAccessors()) {
384 Instr::FetchAndSubscribe fetch;
385 fetch.reg = currentReg;
386 fetch.subscription = subscriptionIndex(_subscribeName);
387 fetch.exceptionId = exceptionId(e->line, e->column);
388 fetch.valueType = regType;
389 fetch.property = *e->property;
392 if (blockNeedsSubscription(_subscribeName) && e->property->notifyIndex != -1) {
393 Instr::Subscribe sub;
394 sub.reg = currentReg;
395 sub.offset = subscriptionIndex(_subscribeName);
396 sub.index = e->property->notifyIndex;
401 fetch.reg = currentReg;
402 fetch.index = e->property->coreIndex;
403 fetch.exceptionId = exceptionId(e->line, e->column);
404 fetch.valueType = regType;
412 void QV4CompilerPrivate::visitTemp(IR::Temp *e)
414 if (currentReg != e->index) {
422 void QV4CompilerPrivate::visitUnop(IR::Unop *e)
424 quint8 src = currentReg;
426 if (IR::Temp *temp = e->expr->asTemp()) {
429 traceExpression(e->expr, src);
434 Q_ASSERT(!"unreachable");
438 convertToBool(e->expr, src);
439 if (src != currentReg) {
449 convertToBool(e->expr, src);
450 i.output = currentReg;
456 if (e->expr->type == IR::RealType) {
457 Instr::UnaryMinusReal i;
458 i.output = currentReg;
461 } else if (e->expr->type == IR::IntType) {
462 convertToReal(e->expr, currentReg);
463 Instr::UnaryMinusReal i;
464 i.output = currentReg;
473 if (e->expr->type == IR::RealType) {
474 Instr::UnaryPlusReal i;
475 i.output = currentReg;
478 } else if (e->expr->type == IR::IntType) {
479 convertToReal(e->expr, currentReg);
480 Instr::UnaryPlusReal i;
481 i.output = currentReg;
511 case IR::OpStrictEqual:
512 case IR::OpStrictNotEqual:
515 Q_ASSERT(!"unreachable");
520 void QV4CompilerPrivate::convertToReal(IR::Expr *expr, int reg)
522 if (expr->type == IR::RealType)
525 switch (expr->type) {
527 Instr::ConvertBoolToReal i;
528 i.output = i.src = reg;
533 Instr::ConvertIntToReal i;
534 i.output = i.src = reg;
548 void QV4CompilerPrivate::convertToInt(IR::Expr *expr, int reg)
550 if (expr->type == IR::IntType)
553 switch (expr->type) {
555 Instr::ConvertBoolToInt i;
556 i.output = i.src = reg;
565 Instr::ConvertRealToInt i;
566 i.output = i.src = reg;
576 void QV4CompilerPrivate::convertToBool(IR::Expr *expr, int reg)
578 if (expr->type == IR::BoolType)
581 switch (expr->type) {
587 Instr::ConvertIntToBool i;
588 i.output = i.src = reg;
593 Instr::ConvertRealToBool i;
594 i.output = i.src = reg;
598 case IR::StringType: {
599 Instr::ConvertStringToBool i;
600 i.output = i.src = reg;
604 case IR::ColorType: {
605 Instr::ConvertColorToBool i;
606 i.output = i.src = reg;
616 quint8 QV4CompilerPrivate::instructionOpcode(IR::Binop *e)
620 return V4Instr::Noop;
627 return V4Instr::Noop;
630 return V4Instr::BitAndInt;
633 return V4Instr::BitOrInt;
636 return V4Instr::BitXorInt;
639 if (e->type == IR::StringType)
640 return V4Instr::AddString;
641 return V4Instr::AddReal;
644 return V4Instr::SubReal;
647 return V4Instr::MulReal;
650 return V4Instr::DivReal;
653 return V4Instr::ModReal;
656 return V4Instr::LShiftInt;
659 return V4Instr::RShiftInt;
662 return V4Instr::URShiftInt;
665 if (e->left->type == IR::StringType)
666 return V4Instr::GtString;
667 return V4Instr::GtReal;
670 if (e->left->type == IR::StringType)
671 return V4Instr::LtString;
672 return V4Instr::LtReal;
675 if (e->left->type == IR::StringType)
676 return V4Instr::GeString;
677 return V4Instr::GeReal;
680 if (e->left->type == IR::StringType)
681 return V4Instr::LeString;
682 return V4Instr::LeReal;
685 if (e->left->type == IR::StringType)
686 return V4Instr::EqualString;
687 return V4Instr::EqualReal;
690 if (e->left->type == IR::StringType)
691 return V4Instr::NotEqualString;
692 return V4Instr::NotEqualReal;
694 case IR::OpStrictEqual:
695 if (e->left->type == IR::StringType)
696 return V4Instr::StrictEqualString;
697 return V4Instr::StrictEqualReal;
699 case IR::OpStrictNotEqual:
700 if (e->left->type == IR::StringType)
701 return V4Instr::StrictNotEqualString;
702 return V4Instr::StrictNotEqualReal;
706 return V4Instr::Noop;
710 return V4Instr::Noop;
713 void QV4CompilerPrivate::visitBinop(IR::Binop *e)
715 if (e->type == IR::InvalidType) {
720 int left = currentReg;
721 int right = currentReg + 1;
723 traceExpression(e->left, left);
724 traceExpression(e->right, right);
726 // At this point it is possible that the type of the
727 // subexpressions is different. This can happen because
728 // we keep BINOP expressions in HIR.
750 convertToInt(e->left, left);
751 convertToInt(e->right, right);
755 if (e->type != IR::StringType) {
756 convertToReal(e->left, left);
757 convertToReal(e->right, right);
765 convertToReal(e->left, left);
766 convertToReal(e->right, right);
775 case IR::OpStrictEqual:
776 case IR::OpStrictNotEqual:
777 if (e->left->type != IR::StringType) {
778 convertToReal(e->left, left);
779 convertToReal(e->right, right);
785 discard(); // ### unreachable
789 const quint8 opcode = instructionOpcode(e);
790 if (opcode != V4Instr::Noop) {
792 instr.binaryop.output = currentReg;
793 instr.binaryop.left = left;
794 instr.binaryop.right = right;
795 gen(static_cast<V4Instr::Type>(opcode), instr);
799 void QV4CompilerPrivate::visitCall(IR::Call *call)
801 if (IR::Name *name = call->base->asName()) {
802 IR::Expr *arg = call->onlyArgument();
803 if (arg != 0 && arg->type == IR::RealType) {
804 traceExpression(arg, currentReg);
806 switch (name->builtin) {
807 case IR::NoBuiltinSymbol:
810 case IR::MathSinBultinFunction: {
811 Instr::MathSinReal i;
812 i.output = i.src = currentReg;
816 case IR::MathCosBultinFunction: {
817 Instr::MathCosReal i;
818 i.output = i.src = currentReg;
822 case IR::MathAbsBuiltinFunction: {
823 Instr::MathAbsReal i;
824 i.output = i.src = currentReg;
828 case IR::MathRoundBultinFunction: {
829 Instr::MathRoundReal i;
830 i.output = i.src = currentReg;
834 case IR::MathFloorBultinFunction: {
835 Instr::MathFloorReal i;
836 i.output = i.src = currentReg;
840 case IR::MathCeilBuiltinFunction: {
841 Instr::MathCeilReal i;
842 i.output = i.src = currentReg;
846 case IR::MathPIBuiltinConstant:
851 if (name->builtin == IR::MathMaxBuiltinFunction ||
852 name->builtin == IR::MathMinBuiltinFunction) {
854 //only handles the most common case of exactly two arguments
855 if (call->args && call->args->next && !call->args->next->next) {
856 IR::Expr *arg1 = call->args->expr;
857 IR::Expr *arg2 = call->args->next->expr;
859 if (arg1 != 0 && arg1->type == IR::RealType &&
860 arg2 != 0 && arg2->type == IR::RealType) {
862 traceExpression(arg1, currentReg);
863 traceExpression(arg2, currentReg + 1);
865 if (name->builtin == IR::MathMaxBuiltinFunction) {
866 Instr::MathMaxReal i;
868 i.right = currentReg + 1;
869 i.output = currentReg;
872 } else if (name->builtin == IR::MathMinBuiltinFunction) {
873 Instr::MathMinReal i;
875 i.right = currentReg + 1;
876 i.output = currentReg;
886 if (qmlVerboseCompiler())
887 qWarning() << "TODO:" << Q_FUNC_INFO << __LINE__;
895 void QV4CompilerPrivate::visitExp(IR::Exp *s)
897 traceExpression(s->expr, currentReg);
900 void QV4CompilerPrivate::visitMove(IR::Move *s)
902 IR::Temp *target = s->target->asTemp();
903 Q_ASSERT(target != 0);
905 quint8 dest = target->index;
907 if (target->type != s->source->type) {
910 if (IR::Temp *t = s->source->asTemp())
913 traceExpression(s->source, dest);
915 V4Instr::Type opcode = V4Instr::Noop;
916 IR::Type targetTy = s->target->type;
917 IR::Type sourceTy = s->source->type;
919 if (sourceTy == IR::UrlType) {
923 // nothing to do. V4 will generate optimized
924 // url-to-xxx conversions.
927 // generate a UrlToString conversion and fix
928 // the type of the source expression.
930 conv.unaryop.output = V4Instr::ConvertUrlToString;
931 conv.unaryop.src = src;
934 sourceTy = IR::StringType;
940 if (targetTy == IR::BoolType) {
942 case IR::IntType: opcode = V4Instr::ConvertIntToBool; break;
943 case IR::RealType: opcode = V4Instr::ConvertRealToBool; break;
944 case IR::StringType: opcode = V4Instr::ConvertStringToBool; break;
945 case IR::UrlType: opcode = V4Instr::ConvertUrlToBool; break;
946 case IR::ColorType: opcode = V4Instr::ConvertColorToBool; break;
949 } else if (targetTy == IR::IntType) {
951 case IR::BoolType: opcode = V4Instr::ConvertBoolToInt; break;
953 if (s->isMoveForReturn)
954 opcode = V4Instr::MathRoundReal;
956 opcode = V4Instr::ConvertRealToInt;
959 case IR::StringType: opcode = V4Instr::ConvertStringToInt; break;
962 } else if (targetTy == IR::RealType) {
964 case IR::BoolType: opcode = V4Instr::ConvertBoolToReal; break;
965 case IR::IntType: opcode = V4Instr::ConvertIntToReal; break;
966 case IR::StringType: opcode = V4Instr::ConvertStringToReal; break;
969 } else if (targetTy == IR::StringType) {
971 case IR::BoolType: opcode = V4Instr::ConvertBoolToString; break;
972 case IR::IntType: opcode = V4Instr::ConvertIntToString; break;
973 case IR::RealType: opcode = V4Instr::ConvertRealToString; break;
974 case IR::UrlType: opcode = V4Instr::ConvertUrlToString; break;
975 case IR::ColorType: opcode = V4Instr::ConvertColorToString; break;
978 } else if (targetTy == IR::UrlType) {
979 V4Instr convToString;
980 convToString.unaryop.output = dest;
981 convToString.unaryop.src = src;
983 // try to convert the source expression to a string.
985 case IR::BoolType: gen(V4Instr::ConvertBoolToString, convToString); sourceTy = IR::StringType; break;
986 case IR::IntType: gen(V4Instr::ConvertIntToString, convToString); sourceTy = IR::StringType; break;
987 case IR::RealType: gen(V4Instr::ConvertRealToString, convToString); sourceTy = IR::StringType; break;
988 case IR::ColorType: gen(V4Instr::ConvertColorToString, convToString); sourceTy = IR::StringType; break;
992 if (sourceTy == IR::StringType)
993 opcode = V4Instr::ConvertStringToUrl;
994 } else if (targetTy == IR::ColorType) {
996 case IR::StringType: opcode = V4Instr::ConvertStringToColor; break;
1000 if (opcode != V4Instr::Noop) {
1002 conv.unaryop.output = dest;
1003 conv.unaryop.src = src;
1006 if (s->isMoveForReturn && opcode == V4Instr::ConvertStringToUrl) {
1008 resolveUrl.unaryop.output = dest;
1009 resolveUrl.unaryop.src = dest;
1010 gen(V4Instr::ResolveUrl, resolveUrl);
1016 traceExpression(s->source, dest);
1020 void QV4CompilerPrivate::visitJump(IR::Jump *s)
1022 patches.append(Patch(s->target, bytecode.size()));
1025 i.offset = 0; // ### backpatch
1029 void QV4CompilerPrivate::visitCJump(IR::CJump *s)
1031 traceExpression(s->cond, currentReg);
1033 patches.append(Patch(s->iftrue, bytecode.size()));
1035 Instr::BranchTrue i;
1037 i.offset = 0; // ### backpatch
1041 void QV4CompilerPrivate::visitRet(IR::Ret *s)
1043 Q_ASSERT(s->expr != 0);
1045 int storeReg = currentReg;
1047 if (IR::Temp *temp = s->expr->asTemp()) {
1048 storeReg = temp->index;
1050 traceExpression(s->expr, storeReg);
1053 if (qmlBindingsTest) {
1054 Instr::TestV4Store test;
1055 test.reg = storeReg;
1057 case IR::StringType:
1058 test.regType = QMetaType::QString;
1061 test.regType = QMetaType::QUrl;
1064 test.regType = QMetaType::QColor;
1066 case IR::SGAnchorLineType:
1067 test.regType = QQmlMetaType::QQuickAnchorLineMetaTypeId();
1069 case IR::ObjectType:
1070 test.regType = QMetaType::QObjectStar;
1073 test.regType = QMetaType::Bool;
1076 test.regType = QMetaType::Int;
1079 test.regType = QMetaType::QReal;
1090 store.index = expression->property->index;
1091 store.reg = storeReg;
1092 store.exceptionId = exceptionId(s->line, s->column);
1096 void QV4Compiler::dump(const QByteArray &programData)
1098 const QV4Program *program = (const QV4Program *)programData.constData();
1100 qWarning() << "Program.bindings:" << program->bindings;
1101 qWarning() << "Program.dataLength:" << program->dataLength;
1102 qWarning() << "Program.subscriptions:" << program->subscriptions;
1103 qWarning() << "Program.indentifiers:" << program->identifiers;
1105 const int programSize = program->instructionCount;
1106 const char *start = program->instructions();
1107 const char *end = start + programSize;
1109 bc.dump(start, end);
1113 Clear the state associated with attempting to compile a specific binding.
1114 This does not clear the global "committed binding" states.
1116 void QV4CompilerPrivate::resetInstanceState()
1118 data = committed.data;
1119 exceptions = committed.exceptions;
1120 usedSubscriptionIds.clear();
1121 subscriptionIds = committed.subscriptionIds;
1122 registeredStrings = committed.registeredStrings;
1130 Mark the last compile as successful, and add it to the "committed data"
1133 Returns the index for the committed binding.
1135 int QV4CompilerPrivate::commitCompile()
1137 int rv = committed.count();
1138 committed.offsets << committed.bytecode.count();
1139 committed.dependencies << usedSubscriptionIds;
1140 committed.bytecode.append(bytecode.constData(), bytecode.size());
1141 committed.data = data;
1142 committed.exceptions = exceptions;
1143 committed.subscriptionIds = subscriptionIds;
1144 committed.registeredStrings = registeredStrings;
1148 bool QV4CompilerPrivate::compile(QQmlJS::AST::Node *node)
1150 resetInstanceState();
1152 if (expression->property->type == -1)
1155 AST::SourceLocation location;
1156 if (AST::ExpressionNode *astExpression = node->expressionCast()) {
1157 location = astExpression->firstSourceLocation();
1158 } else if (AST::Statement *astStatement = node->statementCast()) {
1159 if (AST::Block *block = AST::cast<AST::Block *>(astStatement))
1160 location = block->lbraceToken;
1161 else if (AST::IfStatement *ifStmt = AST::cast<AST::IfStatement *>(astStatement))
1162 location = ifStmt->ifToken;
1169 IR::Function thisFunction(&pool), *function = &thisFunction;
1171 QV4IRBuilder irBuilder(expression, engine);
1172 if (!irBuilder(function, node))
1175 bool discarded = false;
1176 qSwap(_discarded, discarded);
1177 qSwap(_function, function);
1178 trace(location.startLine, location.startColumn);
1179 qSwap(_function, function);
1180 qSwap(_discarded, discarded);
1182 if (qmlVerboseCompiler()) {
1183 QTextStream qerr(stderr, QIODevice::WriteOnly);
1185 qerr << "======== TODO ====== " << endl;
1187 qerr << "==================== " << endl;
1188 qerr << "\tline: " << location.startLine
1189 << "\tcolumn: " << location.startColumn
1191 foreach (IR::BasicBlock *bb, function->basicBlocks)
1196 if (discarded || subscriptionIds.count() > 0xFFFF || registeredStrings.count() > 0xFFFF || registerCount > 31)
1203 int QV4CompilerPrivate::registerLiteralString(quint8 reg, const QStringRef &str)
1205 // ### string cleanup
1207 QByteArray strdata((const char *)str.constData(), str.length() * sizeof(QChar));
1208 int offset = data.count();
1211 Instr::LoadString string;
1213 string.offset = offset;
1214 string.length = str.length();
1220 // Returns an identifier offset
1221 int QV4CompilerPrivate::registerString(const QString &string)
1223 Q_ASSERT(!string.isEmpty());
1225 QPair<int, int> *iter = registeredStrings.value(string);
1228 quint32 len = string.length();
1229 QByteArray lendata((const char *)&len, sizeof(quint32));
1230 QByteArray strdata((const char *)string.constData(), string.length() * sizeof(QChar));
1231 strdata.prepend(lendata);
1232 int rv = data.count();
1235 iter = ®isteredStrings[string];
1236 *iter = qMakePair(registeredStrings.count(), rv);
1239 Instr::InitString reg;
1240 reg.offset = iter->first;
1241 reg.dataIdx = iter->second;
1247 Returns true if the current expression has not already subscribed to \a sub in currentBlockMask.
1249 bool QV4CompilerPrivate::blockNeedsSubscription(const QStringList &sub)
1251 QString str = sub.join(QLatin1String("."));
1253 int *iter = subscriptionIds.value(str);
1257 quint32 *uiter = usedSubscriptionIds.value(*iter);
1261 return !(*uiter & currentBlockMask);
1264 int QV4CompilerPrivate::subscriptionIndex(const QStringList &sub)
1266 QString str = sub.join(QLatin1String("."));
1267 int *iter = subscriptionIds.value(str);
1269 int count = subscriptionIds.count();
1270 iter = &subscriptionIds[str];
1273 quint32 &u = usedSubscriptionIds[*iter];
1274 if (!(u & currentBlockMask)) {
1275 u |= currentBlockMask;
1276 usedSubscriptionIdsChanged = true;
1281 quint32 QV4CompilerPrivate::subscriptionBlockMask(const QStringList &sub)
1283 QString str = sub.join(QLatin1String("."));
1285 int *iter = subscriptionIds.value(str);
1286 Q_ASSERT(iter != 0);
1288 quint32 *uiter = usedSubscriptionIds.value(*iter);
1289 Q_ASSERT(uiter != 0);
1294 quint8 QV4CompilerPrivate::exceptionId(quint32 line, quint32 column)
1297 if (exceptions.count() < 0xFF) {
1298 rv = (quint8)exceptions.count();
1302 exceptions.append(e);
1307 quint8 QV4CompilerPrivate::exceptionId(QQmlJS::AST::ExpressionNode *n)
1310 if (n && exceptions.count() < 0xFF) {
1311 QQmlJS::AST::SourceLocation l = n->firstSourceLocation();
1312 rv = exceptionId(l.startLine, l.startColumn);
1317 QV4Compiler::QV4Compiler()
1318 : d(new QV4CompilerPrivate)
1320 qmlBindingsTest |= qmlBindingsTestEnv();
1323 QV4Compiler::~QV4Compiler()
1329 Returns true if any bindings were compiled.
1331 bool QV4Compiler::isValid() const
1333 return !d->committed.bytecode.isEmpty();
1337 -1 on failure, otherwise the binding index to use.
1339 int QV4Compiler::compile(const Expression &expression, QQmlEnginePrivate *engine)
1341 if (!expression.expression.asAST()) return false;
1343 if (!qmlExperimental() && expression.property->isValueTypeSubProperty)
1346 if (qmlDisableOptimizer() || !qmlEnableV4)
1349 d->expression = &expression;
1352 if (d->compile(expression.expression.asAST())) {
1353 return d->commitCompile();
1359 QByteArray QV4CompilerPrivate::buildSignalTable() const
1361 QHash<int, QList<QPair<int, quint32> > > table;
1363 for (int ii = 0; ii < committed.count(); ++ii) {
1364 const QQmlAssociationList<int, quint32> &deps = committed.dependencies.at(ii);
1365 for (QQmlAssociationList<int, quint32>::const_iterator iter = deps.begin(); iter != deps.end(); ++iter)
1366 table[iter->first].append(qMakePair(ii, iter->second));
1369 QVector<quint32> header;
1370 QVector<quint32> data;
1371 for (int ii = 0; ii < committed.subscriptionIds.count(); ++ii) {
1372 header.append(committed.subscriptionIds.count() + data.count());
1373 const QList<QPair<int, quint32> > &bindings = table[ii];
1374 data.append(bindings.count());
1375 for (int jj = 0; jj < bindings.count(); ++jj) {
1376 data.append(bindings.at(jj).first);
1377 data.append(bindings.at(jj).second);
1382 return QByteArray((const char *)header.constData(), header.count() * sizeof(quint32));
1385 QByteArray QV4CompilerPrivate::buildExceptionData() const
1388 rv.resize(committed.exceptions.count() * sizeof(quint64));
1389 ::memcpy(rv.data(), committed.exceptions.constData(), rv.size());
1394 Returns the compiled program.
1396 QByteArray QV4Compiler::program() const
1398 QByteArray programData;
1402 prog.bindings = d->committed.count();
1405 QV4CompilerPrivate::Instr::Jump jump;
1408 for (int ii = 0; ii < d->committed.count(); ++ii) {
1409 jump.count = d->committed.count() - ii - 1;
1410 jump.count*= V4InstrMeta<V4Instr::Jump>::Size;
1411 jump.count+= d->committed.offsets.at(ii);
1416 QByteArray bytecode;
1417 bytecode.reserve(bc.size() + d->committed.bytecode.size());
1418 bytecode.append(bc.constData(), bc.size());
1419 bytecode.append(d->committed.bytecode.constData(), d->committed.bytecode.size());
1421 QByteArray data = d->committed.data;
1422 while (data.count() % 4) data.append('\0');
1423 prog.signalTableOffset = data.count();
1424 data += d->buildSignalTable();
1425 while (data.count() % 4) data.append('\0');
1426 prog.exceptionDataOffset = data.count();
1427 data += d->buildExceptionData();
1429 prog.dataLength = 4 * ((data.size() + 3) / 4);
1430 prog.subscriptions = d->committed.subscriptionIds.count();
1431 prog.identifiers = d->committed.registeredStrings.count();
1432 prog.instructionCount = bytecode.count();
1433 int size = sizeof(QV4Program) + bytecode.count();
1434 size += prog.dataLength;
1436 programData.resize(size);
1437 memcpy(programData.data(), &prog, sizeof(QV4Program));
1438 if (prog.dataLength)
1439 memcpy((char *)((QV4Program *)programData.data())->data(), data.constData(),
1441 memcpy((char *)((QV4Program *)programData.data())->instructions(), bytecode.constData(),
1445 if (bindingsDump()) {
1446 qWarning().nospace() << "Subscription slots:";
1448 for (QQmlAssociationList<QString, int>::ConstIterator iter = d->committed.subscriptionIds.begin();
1449 iter != d->committed.subscriptionIds.end();
1451 qWarning().nospace() << " " << iter->first << "\t-> " << iter->second;
1454 QV4Compiler::dump(programData);
1460 void QV4Compiler::enableBindingsTest(bool e)
1463 qmlBindingsTest = true;
1465 qmlBindingsTest = qmlBindingsTestEnv();
1468 void QV4Compiler::enableV4(bool e)