1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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/qdeclarativejsast_p.h>
49 #include <private/qdeclarativeaccessors_p.h>
50 #include <private/qdeclarativejsengine_p.h>
54 DEFINE_BOOL_CONFIG_OPTION(bindingsDump, QML_BINDINGS_DUMP)
55 DEFINE_BOOL_CONFIG_OPTION(qmlDisableOptimizer, QML_DISABLE_OPTIMIZER)
56 DEFINE_BOOL_CONFIG_OPTION(qmlExperimental, QML_EXPERIMENTAL)
57 DEFINE_BOOL_CONFIG_OPTION(qmlVerboseCompiler, QML_VERBOSE_COMPILER)
58 DEFINE_BOOL_CONFIG_OPTION(qmlBindingsTestEnv, QML_BINDINGS_TEST)
60 static bool qmlBindingsTest = false;
61 static bool qmlEnableV4 = true;
63 using namespace QDeclarativeJS;
64 QV4CompilerPrivate::QV4CompilerPrivate()
65 : _function(0) , _block(0) , _discarded(false)
72 void QV4CompilerPrivate::trace(int line, int column)
76 this->currentReg = _function->tempCount;
78 foreach (IR::BasicBlock *bb, _function->basicBlocks) {
79 if (! bb->isTerminated() && (bb->index + 1) < _function->basicBlocks.size())
80 bb->JUMP(_function->basicBlocks.at(bb->index + 1));
83 QVector<IR::BasicBlock *> blocks;
85 currentBlockMask = 0x00000001;
88 for (int i = 0; !_discarded && i < blocks.size(); ++i) {
89 IR::BasicBlock *block = blocks.at(i);
90 IR::BasicBlock *next = i + 1 < blocks.size() ? blocks.at(i + 1) : 0;
91 if (IR::Stmt *terminator = block->terminator()) {
92 if (IR::CJump *cj = terminator->asCJump()) {
93 if (cj->iffalse != next) {
94 IR::Jump *jump = _function->pool->New<IR::Jump>();
95 jump->init(cj->iffalse);
96 block->statements.append(jump);
98 } else if (IR::Jump *j = terminator->asJump()) {
99 if (j->target == next) {
100 block->statements.resize(block->statements.size() - 1);
105 block->offset = bytecode.size();
107 if (bytecode.isEmpty()) {
108 if (qmlBindingsTest || bindingsDump()) {
115 if (qmlBindingsTest) {
116 QString str = expression->expression.asScript();
117 QByteArray strdata((const char *)str.constData(), str.length() * sizeof(QChar));
118 int offset = data.count();
121 Instr::EnableV4Test test;
123 test.offset = offset;
124 test.length = str.length();
130 int patchesCount = patches.count();
131 qSwap(usedSubscriptionIdsChanged, usic);
133 int blockopIndex = bytecode.size();
134 Instr::Block blockop;
135 blockop.block = currentBlockMask;
138 foreach (IR::Stmt *s, block->statements) {
143 qSwap(usedSubscriptionIdsChanged, usic);
146 if (currentBlockMask == 0x80000000) {
150 currentBlockMask <<= 1;
151 } else if (! _discarded) {
152 const int adjust = bytecode.remove(blockopIndex);
154 for (int ii = patchesCount; ii < patches.count(); ++ii)
155 patches[ii].offset -= adjust;
159 #ifdef DEBUG_IR_STRUCTURE
161 for (int i = 0; i < blocks.size(); ++i) {
162 dump.basicblock(blocks.at(i));
169 foreach (const Patch &patch, patches) {
170 V4Instr &instr = bytecode[patch.offset];
171 int size = V4Instr::size(instructionType(&instr));
172 instr.branchop.offset = patch.block->offset - patch.offset - size;
179 void QV4CompilerPrivate::trace(QVector<IR::BasicBlock *> *blocks)
181 for (int i = 0; i < _function->basicBlocks.size(); ++i) {
182 IR::BasicBlock *block = _function->basicBlocks.at(i);
184 while (! blocks->contains(block)) {
185 blocks->append(block);
187 if (IR::Stmt *terminator = block->terminator()) {
188 if (IR::CJump *cj = terminator->asCJump())
190 else if (IR::Jump *j = terminator->asJump())
197 void QV4CompilerPrivate::traceExpression(IR::Expr *e, quint8 r)
202 qSwap(currentReg, r);
204 qSwap(currentReg, r);
211 void QV4CompilerPrivate::visitConst(IR::Const *e)
236 if (qmlVerboseCompiler())
237 qWarning() << Q_FUNC_INFO << "unexpected type";
242 void QV4CompilerPrivate::visitString(IR::String *e)
244 registerLiteralString(currentReg, e->value);
247 void QV4CompilerPrivate::visitName(IR::Name *e)
250 // fetch the object and store it in reg.
251 traceExpression(e->base, currentReg);
253 _subscribeName.clear();
256 if (e->storage == IR::Name::RootStorage) {
258 Instr::LoadRoot instr;
259 instr.reg = currentReg;
262 if (e->symbol == IR::Name::IdObject) {
263 // The ID is a reference to the root object
267 } else if (e->storage == IR::Name::ScopeStorage) {
269 Instr::LoadScope instr;
270 instr.reg = currentReg;
273 _subscribeName << contextName();
275 } else if (e->storage == IR::Name::IdStorage) {
278 instr.reg = currentReg;
279 instr.index = e->idObject->idIndex;
282 _subscribeName << QLatin1String("$$$ID_") + *e->id;
284 if (blockNeedsSubscription(_subscribeName)) {
285 Instr::SubscribeId sub;
286 sub.reg = currentReg;
287 sub.offset = subscriptionIndex(_subscribeName);
288 sub.index = instr.index;
298 case IR::Name::Unbound:
299 case IR::Name::IdObject:
301 case IR::Name::Object: {
302 Q_ASSERT(!"Unreachable");
306 case IR::Name::AttachType: {
307 _subscribeName << *e->id;
309 Instr::LoadAttached attached;
310 attached.output = currentReg;
311 attached.reg = currentReg;
312 attached.exceptionId = exceptionId(e->line, e->column);
313 if (e->declarativeType->attachedPropertiesId() == -1)
315 attached.id = e->declarativeType->attachedPropertiesId();
319 case IR::Name::Property: {
320 _subscribeName << *e->id;
322 if (e->property->coreIndex == -1) {
324 e->property->load(prop, QDeclarativeEnginePrivate::get(engine));
327 const int propTy = e->property->propType;
328 QDeclarativeRegisterType regType;
331 case QMetaType::QReal:
334 case QMetaType::Bool:
340 case QMetaType::QString:
341 regType = QStringType;
343 case QMetaType::QUrl:
348 if (propTy == qMetaTypeId<QDeclarative1AnchorLine>()) {
349 regType = PODValueType;
350 } else if (propTy == QDeclarativeMetaType::QQuickAnchorLineMetaTypeId()) {
351 regType = PODValueType;
352 } else if (QDeclarativeMetaType::isQObject(propTy)) {
353 regType = QObjectStarType;
355 if (qmlVerboseCompiler())
356 qWarning() << "Discard unsupported property type:" << QMetaType::typeName(propTy);
357 discard(); // Unsupported type
364 if (e->property->hasAccessors()) {
365 Instr::FetchAndSubscribe fetch;
366 fetch.reg = currentReg;
367 fetch.subscription = subscriptionIndex(_subscribeName);
368 fetch.exceptionId = exceptionId(e->line, e->column);
369 fetch.valueType = regType;
370 fetch.property = *e->property;
373 if (blockNeedsSubscription(_subscribeName) && e->property->notifyIndex != -1) {
374 Instr::Subscribe sub;
375 sub.reg = currentReg;
376 sub.offset = subscriptionIndex(_subscribeName);
377 sub.index = e->property->notifyIndex;
382 fetch.reg = currentReg;
383 fetch.index = e->property->coreIndex;
384 fetch.exceptionId = exceptionId(e->line, e->column);
385 fetch.valueType = regType;
393 void QV4CompilerPrivate::visitTemp(IR::Temp *e)
395 if (currentReg != e->index) {
403 void QV4CompilerPrivate::visitUnop(IR::Unop *e)
405 quint8 src = currentReg;
407 if (IR::Temp *temp = e->expr->asTemp()) {
410 traceExpression(e->expr, src);
415 Q_ASSERT(!"unreachable");
419 convertToBool(e->expr, src);
420 if (src != currentReg) {
430 convertToBool(e->expr, src);
431 i.output = currentReg;
437 if (e->expr->type == IR::RealType) {
438 Instr::UnaryMinusReal i;
439 i.output = currentReg;
442 } else if (e->expr->type == IR::IntType) {
443 convertToReal(e->expr, currentReg);
444 Instr::UnaryMinusReal i;
445 i.output = currentReg;
454 if (e->expr->type == IR::RealType) {
455 Instr::UnaryPlusReal i;
456 i.output = currentReg;
459 } else if (e->expr->type == IR::IntType) {
460 convertToReal(e->expr, currentReg);
461 Instr::UnaryPlusReal i;
462 i.output = currentReg;
492 case IR::OpStrictEqual:
493 case IR::OpStrictNotEqual:
496 Q_ASSERT(!"unreachable");
501 void QV4CompilerPrivate::convertToReal(IR::Expr *expr, int reg)
503 if (expr->type == IR::RealType)
506 switch (expr->type) {
508 Instr::ConvertBoolToReal i;
509 i.output = i.src = reg;
514 Instr::ConvertIntToReal i;
515 i.output = i.src = reg;
529 void QV4CompilerPrivate::convertToInt(IR::Expr *expr, int reg)
531 if (expr->type == IR::IntType)
534 switch (expr->type) {
536 Instr::ConvertBoolToInt i;
537 i.output = i.src = reg;
546 Instr::ConvertRealToInt i;
547 i.output = i.src = reg;
557 void QV4CompilerPrivate::convertToBool(IR::Expr *expr, int reg)
559 if (expr->type == IR::BoolType)
562 switch (expr->type) {
568 Instr::ConvertIntToBool i;
569 i.output = i.src = reg;
574 Instr::ConvertRealToBool i;
575 i.output = i.src = reg;
579 case IR::StringType: {
580 Instr::ConvertStringToBool i;
581 i.output = i.src = reg;
591 quint8 QV4CompilerPrivate::instructionOpcode(IR::Binop *e)
595 return V4Instr::Noop;
602 return V4Instr::Noop;
605 return V4Instr::BitAndInt;
608 return V4Instr::BitOrInt;
611 return V4Instr::BitXorInt;
614 if (e->type == IR::StringType)
615 return V4Instr::AddString;
616 return V4Instr::AddReal;
619 return V4Instr::SubReal;
622 return V4Instr::MulReal;
625 return V4Instr::DivReal;
628 return V4Instr::ModReal;
631 return V4Instr::LShiftInt;
634 return V4Instr::RShiftInt;
637 return V4Instr::URShiftInt;
640 if (e->left->type == IR::StringType)
641 return V4Instr::GtString;
642 return V4Instr::GtReal;
645 if (e->left->type == IR::StringType)
646 return V4Instr::LtString;
647 return V4Instr::LtReal;
650 if (e->left->type == IR::StringType)
651 return V4Instr::GeString;
652 return V4Instr::GeReal;
655 if (e->left->type == IR::StringType)
656 return V4Instr::LeString;
657 return V4Instr::LeReal;
660 if (e->left->type == IR::StringType)
661 return V4Instr::EqualString;
662 return V4Instr::EqualReal;
665 if (e->left->type == IR::StringType)
666 return V4Instr::NotEqualString;
667 return V4Instr::NotEqualReal;
669 case IR::OpStrictEqual:
670 if (e->left->type == IR::StringType)
671 return V4Instr::StrictEqualString;
672 return V4Instr::StrictEqualReal;
674 case IR::OpStrictNotEqual:
675 if (e->left->type == IR::StringType)
676 return V4Instr::StrictNotEqualString;
677 return V4Instr::StrictNotEqualReal;
681 return V4Instr::Noop;
685 return V4Instr::Noop;
688 void QV4CompilerPrivate::visitBinop(IR::Binop *e)
690 int left = currentReg;
691 int right = currentReg + 1;
693 if (e->left->asTemp() && e->type != IR::StringType) // Not sure if the e->type != String test is needed
694 left = e->left->asTemp()->index;
696 traceExpression(e->left, left);
698 if (IR::Temp *t = e->right->asTemp())
701 traceExpression(e->right, right);
703 if (e->left->type != e->right->type) {
704 if (qmlVerboseCompiler())
705 qWarning().nospace() << "invalid operands to binary operator " << IR::binaryOperator(e->op)
706 << "(`" << IR::binaryOperator(e->left->type)
708 << IR::binaryOperator(e->right->type)
734 convertToInt(e->left, left);
735 convertToInt(e->right, right);
739 if (e->type != IR::StringType) {
740 convertToReal(e->left, left);
741 convertToReal(e->right, right);
749 convertToReal(e->left, left);
750 convertToReal(e->right, right);
759 case IR::OpStrictEqual:
760 case IR::OpStrictNotEqual:
761 if (e->left->type != IR::StringType) {
762 convertToReal(e->left, left);
763 convertToReal(e->right, right);
769 discard(); // ### unreachable
773 const quint8 opcode = instructionOpcode(e);
774 if (opcode != V4Instr::Noop) {
776 instr.binaryop.output = currentReg;
777 instr.binaryop.left = left;
778 instr.binaryop.right = right;
779 gen(static_cast<V4Instr::Type>(opcode), instr);
783 void QV4CompilerPrivate::visitCall(IR::Call *call)
785 if (IR::Name *name = call->base->asName()) {
786 IR::Expr *arg = call->onlyArgument();
787 if (arg != 0 && arg->type == IR::RealType) {
788 traceExpression(arg, currentReg);
790 switch (name->builtin) {
791 case IR::NoBuiltinSymbol:
794 case IR::MathSinBultinFunction: {
795 Instr::MathSinReal i;
796 i.output = i.src = currentReg;
800 case IR::MathCosBultinFunction: {
801 Instr::MathCosReal i;
802 i.output = i.src = currentReg;
806 case IR::MathRoundBultinFunction: {
807 Instr::MathRoundReal i;
808 i.output = i.src = currentReg;
812 case IR::MathFloorBultinFunction: {
813 Instr::MathFloorReal i;
814 i.output = i.src = currentReg;
818 case IR::MathPIBuiltinConstant:
824 if (qmlVerboseCompiler())
825 qWarning() << "TODO:" << Q_FUNC_INFO << __LINE__;
833 void QV4CompilerPrivate::visitExp(IR::Exp *s)
835 traceExpression(s->expr, currentReg);
838 void QV4CompilerPrivate::visitMove(IR::Move *s)
840 IR::Temp *target = s->target->asTemp();
841 Q_ASSERT(target != 0);
843 quint8 dest = target->index;
845 if (target->type != s->source->type) {
848 if (IR::Temp *t = s->source->asTemp())
851 traceExpression(s->source, dest);
853 V4Instr::Type opcode = V4Instr::Noop;
854 IR::Type targetTy = s->target->type;
855 IR::Type sourceTy = s->source->type;
857 if (sourceTy == IR::UrlType) {
861 // nothing to do. V4 will generate optimized
862 // url-to-xxx conversions.
865 // generate a UrlToString conversion and fix
866 // the type of the source expression.
868 conv.unaryop.output = V4Instr::ConvertUrlToString;
869 conv.unaryop.src = src;
872 sourceTy = IR::StringType;
878 if (targetTy == IR::BoolType) {
880 case IR::IntType: opcode = V4Instr::ConvertIntToBool; break;
881 case IR::RealType: opcode = V4Instr::ConvertRealToBool; break;
882 case IR::StringType: opcode = V4Instr::ConvertStringToBool; break;
883 case IR::UrlType: opcode = V4Instr::ConvertUrlToBool; break;
886 } else if (targetTy == IR::IntType) {
888 case IR::BoolType: opcode = V4Instr::ConvertBoolToInt; break;
890 if (s->isMoveForReturn)
891 opcode = V4Instr::MathRoundReal;
893 opcode = V4Instr::ConvertRealToInt;
896 case IR::StringType: opcode = V4Instr::ConvertStringToInt; break;
899 } else if (targetTy == IR::RealType) {
901 case IR::BoolType: opcode = V4Instr::ConvertBoolToReal; break;
902 case IR::IntType: opcode = V4Instr::ConvertIntToReal; break;
903 case IR::StringType: opcode = V4Instr::ConvertStringToReal; break;
906 } else if (targetTy == IR::StringType) {
908 case IR::BoolType: opcode = V4Instr::ConvertBoolToString; break;
909 case IR::IntType: opcode = V4Instr::ConvertIntToString; break;
910 case IR::RealType: opcode = V4Instr::ConvertRealToString; break;
911 case IR::UrlType: opcode = V4Instr::ConvertUrlToString; break;
914 } else if (targetTy == IR::UrlType) {
915 V4Instr convToString;
916 convToString.unaryop.output = dest;
917 convToString.unaryop.src = src;
919 // try to convert the source expression to a string.
921 case IR::BoolType: gen(V4Instr::ConvertBoolToString, convToString); sourceTy = IR::StringType; break;
922 case IR::IntType: gen(V4Instr::ConvertIntToString, convToString); sourceTy = IR::StringType; break;
923 case IR::RealType: gen(V4Instr::ConvertRealToString, convToString); sourceTy = IR::StringType; break;
927 if (sourceTy == IR::StringType)
928 opcode = V4Instr::ConvertStringToUrl;
930 if (opcode != V4Instr::Noop) {
932 conv.unaryop.output = dest;
933 conv.unaryop.src = src;
936 if (s->isMoveForReturn && opcode == V4Instr::ConvertStringToUrl) {
938 resolveUrl.unaryop.output = dest;
939 resolveUrl.unaryop.src = dest;
940 gen(V4Instr::ResolveUrl, resolveUrl);
946 traceExpression(s->source, dest);
950 void QV4CompilerPrivate::visitJump(IR::Jump *s)
952 patches.append(Patch(s->target, bytecode.size()));
955 i.offset = 0; // ### backpatch
959 void QV4CompilerPrivate::visitCJump(IR::CJump *s)
961 traceExpression(s->cond, currentReg);
963 patches.append(Patch(s->iftrue, bytecode.size()));
967 i.offset = 0; // ### backpatch
971 void QV4CompilerPrivate::visitRet(IR::Ret *s)
973 Q_ASSERT(s->expr != 0);
975 int storeReg = currentReg;
977 if (IR::Temp *temp = s->expr->asTemp()) {
978 storeReg = temp->index;
980 traceExpression(s->expr, storeReg);
983 if (qmlBindingsTest) {
984 Instr::TestV4Store test;
988 test.regType = QMetaType::QString;
991 test.regType = QMetaType::QUrl;
993 case IR::AnchorLineType:
994 test.regType = qMetaTypeId<QDeclarative1AnchorLine>();
996 case IR::SGAnchorLineType:
997 test.regType = QDeclarativeMetaType::QQuickAnchorLineMetaTypeId();
1000 test.regType = QMetaType::QObjectStar;
1003 test.regType = QMetaType::Bool;
1006 test.regType = QMetaType::Int;
1009 test.regType = QMetaType::QReal;
1020 store.index = expression->property->index;
1021 store.reg = storeReg;
1022 store.exceptionId = exceptionId(s->line, s->column);
1026 void QV4Compiler::dump(const QByteArray &programData)
1028 const QV4Program *program = (const QV4Program *)programData.constData();
1030 qWarning() << "Program.bindings:" << program->bindings;
1031 qWarning() << "Program.dataLength:" << program->dataLength;
1032 qWarning() << "Program.subscriptions:" << program->subscriptions;
1033 qWarning() << "Program.indentifiers:" << program->identifiers;
1035 const int programSize = program->instructionCount;
1036 const char *start = program->instructions();
1037 const char *end = start + programSize;
1039 bc.dump(start, end);
1043 Clear the state associated with attempting to compile a specific binding.
1044 This does not clear the global "committed binding" states.
1046 void QV4CompilerPrivate::resetInstanceState()
1048 data = committed.data;
1049 exceptions = committed.exceptions;
1050 usedSubscriptionIds.clear();
1051 subscriptionIds = committed.subscriptionIds;
1052 registeredStrings = committed.registeredStrings;
1060 Mark the last compile as successful, and add it to the "committed data"
1063 Returns the index for the committed binding.
1065 int QV4CompilerPrivate::commitCompile()
1067 int rv = committed.count();
1068 committed.offsets << committed.bytecode.count();
1069 committed.dependencies << usedSubscriptionIds;
1070 committed.bytecode.append(bytecode.constData(), bytecode.size());
1071 committed.data = data;
1072 committed.exceptions = exceptions;
1073 committed.subscriptionIds = subscriptionIds;
1074 committed.registeredStrings = registeredStrings;
1078 bool QV4CompilerPrivate::compile(QDeclarativeJS::AST::Node *node)
1080 resetInstanceState();
1082 if (expression->property->type == -1)
1085 AST::SourceLocation location;
1086 if (AST::ExpressionNode *astExpression = node->expressionCast()) {
1087 location = astExpression->firstSourceLocation();
1088 } else if (AST::Statement *astStatement = node->statementCast()) {
1089 if (AST::Block *block = AST::cast<AST::Block *>(astStatement))
1090 location = block->lbraceToken;
1091 else if (AST::IfStatement *ifStmt = AST::cast<AST::IfStatement *>(astStatement))
1092 location = ifStmt->ifToken;
1099 IR::Function thisFunction(&pool), *function = &thisFunction;
1101 QV4IRBuilder irBuilder(expression, engine);
1102 if (!irBuilder(function, node))
1105 bool discarded = false;
1106 qSwap(_discarded, discarded);
1107 qSwap(_function, function);
1108 trace(location.startLine, location.startColumn);
1109 qSwap(_function, function);
1110 qSwap(_discarded, discarded);
1112 if (qmlVerboseCompiler()) {
1113 QTextStream qerr(stderr, QIODevice::WriteOnly);
1115 qerr << "======== TODO ====== " << endl;
1117 qerr << "==================== " << endl;
1118 qerr << "\tline: " << location.startLine
1119 << "\tcolumn: " << location.startColumn
1121 foreach (IR::BasicBlock *bb, function->basicBlocks)
1126 if (discarded || subscriptionIds.count() > 0xFFFF || registeredStrings.count() > 0xFFFF)
1133 int QV4CompilerPrivate::registerLiteralString(quint8 reg, const QStringRef &str)
1135 // ### string cleanup
1137 QByteArray strdata((const char *)str.constData(), str.length() * sizeof(QChar));
1138 int offset = data.count();
1141 Instr::LoadString string;
1143 string.offset = offset;
1144 string.length = str.length();
1150 // Returns an identifier offset
1151 int QV4CompilerPrivate::registerString(const QString &string)
1153 Q_ASSERT(!string.isEmpty());
1155 QPair<int, int> *iter = registeredStrings.value(string);
1158 quint32 len = string.length();
1159 QByteArray lendata((const char *)&len, sizeof(quint32));
1160 QByteArray strdata((const char *)string.constData(), string.length() * sizeof(QChar));
1161 strdata.prepend(lendata);
1162 int rv = data.count();
1165 iter = ®isteredStrings[string];
1166 *iter = qMakePair(registeredStrings.count(), rv);
1169 Instr::InitString reg;
1170 reg.offset = iter->first;
1171 reg.dataIdx = iter->second;
1177 Returns true if the current expression has not already subscribed to \a sub in currentBlockMask.
1179 bool QV4CompilerPrivate::blockNeedsSubscription(const QStringList &sub)
1181 QString str = sub.join(QLatin1String("."));
1183 int *iter = subscriptionIds.value(str);
1187 quint32 *uiter = usedSubscriptionIds.value(*iter);
1191 return !(*uiter & currentBlockMask);
1194 int QV4CompilerPrivate::subscriptionIndex(const QStringList &sub)
1196 QString str = sub.join(QLatin1String("."));
1197 int *iter = subscriptionIds.value(str);
1199 int count = subscriptionIds.count();
1200 iter = &subscriptionIds[str];
1203 quint32 &u = usedSubscriptionIds[*iter];
1204 if (!(u & currentBlockMask)) {
1205 u |= currentBlockMask;
1206 usedSubscriptionIdsChanged = true;
1211 quint32 QV4CompilerPrivate::subscriptionBlockMask(const QStringList &sub)
1213 QString str = sub.join(QLatin1String("."));
1215 int *iter = subscriptionIds.value(str);
1216 Q_ASSERT(iter != 0);
1218 quint32 *uiter = usedSubscriptionIds.value(*iter);
1219 Q_ASSERT(uiter != 0);
1224 quint8 QV4CompilerPrivate::exceptionId(quint32 line, quint32 column)
1227 if (exceptions.count() < 0xFF) {
1228 rv = (quint8)exceptions.count();
1232 exceptions.append(e);
1237 quint8 QV4CompilerPrivate::exceptionId(QDeclarativeJS::AST::ExpressionNode *n)
1240 if (n && exceptions.count() < 0xFF) {
1241 QDeclarativeJS::AST::SourceLocation l = n->firstSourceLocation();
1242 rv = exceptionId(l.startLine, l.startColumn);
1247 QV4Compiler::QV4Compiler()
1248 : d(new QV4CompilerPrivate)
1250 qmlBindingsTest |= qmlBindingsTestEnv();
1253 QV4Compiler::~QV4Compiler()
1259 Returns true if any bindings were compiled.
1261 bool QV4Compiler::isValid() const
1263 return !d->committed.bytecode.isEmpty();
1267 -1 on failure, otherwise the binding index to use.
1269 int QV4Compiler::compile(const Expression &expression, QDeclarativeEnginePrivate *engine)
1271 if (!expression.expression.asAST()) return false;
1273 if (!qmlExperimental() && expression.property->isValueTypeSubProperty)
1276 if (qmlDisableOptimizer() || !qmlEnableV4)
1279 d->expression = &expression;
1282 if (d->compile(expression.expression.asAST())) {
1283 return d->commitCompile();
1289 QByteArray QV4CompilerPrivate::buildSignalTable() const
1291 QHash<int, QList<QPair<int, quint32> > > table;
1293 for (int ii = 0; ii < committed.count(); ++ii) {
1294 const QDeclarativeAssociationList<int, quint32> &deps = committed.dependencies.at(ii);
1295 for (QDeclarativeAssociationList<int, quint32>::const_iterator iter = deps.begin(); iter != deps.end(); ++iter)
1296 table[iter->first].append(qMakePair(ii, iter->second));
1299 QVector<quint32> header;
1300 QVector<quint32> data;
1301 for (int ii = 0; ii < committed.subscriptionIds.count(); ++ii) {
1302 header.append(committed.subscriptionIds.count() + data.count());
1303 const QList<QPair<int, quint32> > &bindings = table[ii];
1304 data.append(bindings.count());
1305 for (int jj = 0; jj < bindings.count(); ++jj) {
1306 data.append(bindings.at(jj).first);
1307 data.append(bindings.at(jj).second);
1312 return QByteArray((const char *)header.constData(), header.count() * sizeof(quint32));
1315 QByteArray QV4CompilerPrivate::buildExceptionData() const
1318 rv.resize(committed.exceptions.count() * sizeof(quint64));
1319 ::memcpy(rv.data(), committed.exceptions.constData(), rv.size());
1324 Returns the compiled program.
1326 QByteArray QV4Compiler::program() const
1328 QByteArray programData;
1332 prog.bindings = d->committed.count();
1335 QV4CompilerPrivate::Instr::Jump jump;
1338 for (int ii = 0; ii < d->committed.count(); ++ii) {
1339 jump.count = d->committed.count() - ii - 1;
1340 jump.count*= V4InstrMeta<V4Instr::Jump>::Size;
1341 jump.count+= d->committed.offsets.at(ii);
1346 QByteArray bytecode;
1347 bytecode.reserve(bc.size() + d->committed.bytecode.size());
1348 bytecode.append(bc.constData(), bc.size());
1349 bytecode.append(d->committed.bytecode.constData(), d->committed.bytecode.size());
1351 QByteArray data = d->committed.data;
1352 while (data.count() % 4) data.append('\0');
1353 prog.signalTableOffset = data.count();
1354 data += d->buildSignalTable();
1355 while (data.count() % 4) data.append('\0');
1356 prog.exceptionDataOffset = data.count();
1357 data += d->buildExceptionData();
1359 prog.dataLength = 4 * ((data.size() + 3) / 4);
1360 prog.subscriptions = d->committed.subscriptionIds.count();
1361 prog.identifiers = d->committed.registeredStrings.count();
1362 prog.instructionCount = bytecode.count();
1363 int size = sizeof(QV4Program) + bytecode.count();
1364 size += prog.dataLength;
1366 programData.resize(size);
1367 memcpy(programData.data(), &prog, sizeof(QV4Program));
1368 if (prog.dataLength)
1369 memcpy((char *)((QV4Program *)programData.data())->data(), data.constData(),
1371 memcpy((char *)((QV4Program *)programData.data())->instructions(), bytecode.constData(),
1375 if (bindingsDump()) {
1376 qWarning().nospace() << "Subscription slots:";
1378 for (QDeclarativeAssociationList<QString, int>::ConstIterator iter = d->committed.subscriptionIds.begin();
1379 iter != d->committed.subscriptionIds.end();
1381 qWarning().nospace() << " " << iter->first << "\t-> " << iter->second;
1384 QV4Compiler::dump(programData);
1390 void QV4Compiler::enableBindingsTest(bool e)
1393 qmlBindingsTest = true;
1395 qmlBindingsTest = qmlBindingsTestEnv();
1398 void QV4Compiler::enableV4(bool e)