1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 "qv4irbuilder_p.h"
43 #include "qv4compiler_p_p.h"
45 #include <private/qdeclarativemetatype_p.h>
46 #include <private/qdeclarativetypenamecache_p.h>
48 DEFINE_BOOL_CONFIG_OPTION(qmlVerboseCompiler, QML_VERBOSE_COMPILER)
52 using namespace QDeclarativeJS;
54 static IR::Type irTypeFromVariantType(int t, QDeclarativeEnginePrivate *engine, const QMetaObject * /* meta */)
63 case QMetaType::QReal:
66 case QMetaType::QString:
67 return IR::StringType;
73 if (t == qMetaTypeId<QDeclarative1AnchorLine>())
74 return IR::AnchorLineType;
75 else if (t == QDeclarativeMetaType::QQuickAnchorLineMetaTypeId())
76 return IR::SGAnchorLineType;
77 else if (engine->metaObjectForType(t)) {
78 return IR::ObjectType;
81 return IR::InvalidType;
85 QV4IRBuilder::QV4IRBuilder(const QV4Compiler::Expression *expr,
86 QDeclarativeEnginePrivate *engine)
87 : m_expression(expr), m_engine(engine), _function(0), _block(0), _discard(false)
91 bool QV4IRBuilder::operator()(QDeclarativeJS::IR::Function *function,
92 QDeclarativeJS::AST::Node *ast)
94 bool discarded = false;
96 IR::BasicBlock *block = function->newBasicBlock();
98 qSwap(_discard, discarded);
99 qSwap(_function, function);
100 qSwap(_block, block);
103 AST::SourceLocation location;
104 if (AST::ExpressionNode *asExpr = ast->expressionCast()) {
105 r = expression(asExpr);
106 location = asExpr->firstSourceLocation();
107 } else if (AST::Statement *asStmt = ast->statementCast()) {
108 r = statement(asStmt);
109 location = asStmt->firstSourceLocation();
112 //_block->MOVE(_block->TEMP(IR::InvalidType), r.code);
114 const QMetaObject *m = 0;
115 const IR::Type targetType = irTypeFromVariantType(m_expression->property->type, m_engine, m);
116 if (targetType != r.type()) {
117 IR::Expr *x = _block->TEMP(targetType);
118 _block->MOVE(x, r, true);
121 _block->RET(r.code, targetType, location.startLine, location.startColumn);
124 qSwap(_block, block);
125 qSwap(_function, function);
126 qSwap(_discard, discarded);
131 bool QV4IRBuilder::buildName(QList<QStringRef> &name,
133 QList<AST::ExpressionNode *> *nodes)
135 if (node->kind == AST::Node::Kind_IdentifierExpression) {
136 name << static_cast<AST::IdentifierExpression*>(node)->name;
137 if (nodes) *nodes << static_cast<AST::IdentifierExpression*>(node);
138 } else if (node->kind == AST::Node::Kind_FieldMemberExpression) {
139 AST::FieldMemberExpression *expr =
140 static_cast<AST::FieldMemberExpression *>(node);
142 if (!buildName(name, expr->base, nodes))
146 if (nodes) *nodes << expr;
154 void QV4IRBuilder::discard()
159 QV4IRBuilder::ExprResult
160 QV4IRBuilder::expression(AST::ExpressionNode *ast)
168 if (r.is(IR::InvalidType))
171 Q_ASSERT(r.hint == r.format);
178 void QV4IRBuilder::condition(AST::ExpressionNode *ast, IR::BasicBlock *iftrue, IR::BasicBlock *iffalse)
182 ExprResult r(iftrue, iffalse);
187 if (r.format != ExprResult::cx) {
191 Q_ASSERT(r.hint == ExprResult::cx);
192 Q_ASSERT(r.format == ExprResult::ex);
194 if (r.type() != IR::BoolType) {
195 IR::Temp *t = _block->TEMP(IR::BoolType);
200 _block->CJUMP(_block->UNOP(IR::OpIfTrue, r), iftrue, iffalse);
204 QV4IRBuilder::ExprResult
205 QV4IRBuilder::statement(AST::Statement *ast)
213 if (r.is(IR::InvalidType))
216 Q_ASSERT(r.hint == r.format);
223 void QV4IRBuilder::sourceElement(AST::SourceElement *ast)
228 void QV4IRBuilder::implicitCvt(ExprResult &expr, IR::Type type)
230 if (expr.type() == type)
231 return; // nothing to do
233 IR::Expr *x = _block->TEMP(type);
234 _block->MOVE(x, expr.code);
239 bool QV4IRBuilder::visit(AST::UiProgram *)
241 Q_ASSERT(!"unreachable");
245 bool QV4IRBuilder::visit(AST::UiImportList *)
247 Q_ASSERT(!"unreachable");
251 bool QV4IRBuilder::visit(AST::UiImport *)
253 Q_ASSERT(!"unreachable");
257 bool QV4IRBuilder::visit(AST::UiPublicMember *)
259 Q_ASSERT(!"unreachable");
263 bool QV4IRBuilder::visit(AST::UiSourceElement *)
265 Q_ASSERT(!"unreachable");
269 bool QV4IRBuilder::visit(AST::UiObjectDefinition *)
271 Q_ASSERT(!"unreachable");
275 bool QV4IRBuilder::visit(AST::UiObjectInitializer *)
277 Q_ASSERT(!"unreachable");
281 bool QV4IRBuilder::visit(AST::UiObjectBinding *)
283 Q_ASSERT(!"unreachable");
287 bool QV4IRBuilder::visit(AST::UiScriptBinding *)
289 Q_ASSERT(!"unreachable");
293 bool QV4IRBuilder::visit(AST::UiArrayBinding *)
295 Q_ASSERT(!"unreachable");
299 bool QV4IRBuilder::visit(AST::UiObjectMemberList *)
301 Q_ASSERT(!"unreachable");
305 bool QV4IRBuilder::visit(AST::UiArrayMemberList *)
307 Q_ASSERT(!"unreachable");
311 bool QV4IRBuilder::visit(AST::UiQualifiedId *)
313 Q_ASSERT(!"unreachable");
319 bool QV4IRBuilder::visit(AST::Program *)
321 Q_ASSERT(!"unreachable");
325 bool QV4IRBuilder::visit(AST::SourceElements *)
327 Q_ASSERT(!"unreachable");
331 bool QV4IRBuilder::visit(AST::FunctionSourceElement *)
333 Q_ASSERT(!"unreachable");
337 bool QV4IRBuilder::visit(AST::StatementSourceElement *)
339 Q_ASSERT(!"unreachable");
344 bool QV4IRBuilder::visit(AST::PropertyNameAndValueList *)
346 Q_ASSERT(!"unreachable");
350 bool QV4IRBuilder::visit(AST::IdentifierPropertyName *)
352 Q_ASSERT(!"unreachable");
356 bool QV4IRBuilder::visit(AST::StringLiteralPropertyName *)
358 Q_ASSERT(!"unreachable");
362 bool QV4IRBuilder::visit(AST::NumericLiteralPropertyName *)
364 Q_ASSERT(!"unreachable");
370 bool QV4IRBuilder::visit(AST::ElementList *)
372 Q_ASSERT(!"unreachable");
376 bool QV4IRBuilder::visit(AST::Elision *)
378 Q_ASSERT(!"unreachable");
384 bool QV4IRBuilder::visit(AST::ArgumentList *)
386 Q_ASSERT(!"unreachable");
391 bool QV4IRBuilder::visit(AST::ObjectLiteral *)
396 bool QV4IRBuilder::visit(AST::ArrayLiteral *)
401 bool QV4IRBuilder::visit(AST::ThisExpression *)
406 bool QV4IRBuilder::visit(AST::IdentifierExpression *ast)
408 const quint32 line = ast->identifierToken.startLine;
409 const quint32 column = ast->identifierToken.startColumn;
411 const QString name = ast->name.toString();
413 if (name.at(0) == QLatin1Char('u') && name.length() == 9 && name == QLatin1String("undefined")) {
414 _expr.code = _block->CONST(IR::UndefinedType, 0); // ### undefined value
415 } else if (m_engine->v8engine()->illegalNames().contains(name) ) {
416 if (qmlVerboseCompiler()) qWarning() << "*** illegal symbol:" << name;
418 } else if (const QDeclarativeScript::Object *obj = m_expression->ids->value(name)) {
419 IR::Name *code = _block->ID_OBJECT(name, obj, line, column);
420 if (obj == m_expression->component)
421 code->storage = IR::Name::RootStorage;
425 QDeclarativeTypeNameCache::Result r = m_expression->importCache->query(name);
428 _expr.code = _block->ATTACH_TYPE(name, r.type, IR::Name::ScopeStorage, line, column);
430 // We don't support anything else
434 if (m_expression->context != m_expression->component) {
435 // RootStorage is more efficient than ScopeStorage, so prefer that if they are the same
436 QDeclarativePropertyCache *cache = m_expression->context->synthCache;
437 const QMetaObject *metaObject = m_expression->context->metaObject();
438 if (!cache) cache = m_engine->cache(metaObject);
440 QDeclarativePropertyData *data = cache->property(name);
442 if (data && data->revision != 0) {
443 if (qmlVerboseCompiler())
444 qWarning() << "*** versioned symbol:" << name;
449 if (data && !data->isFunction()) {
450 IR::Type irType = irTypeFromVariantType(data->propType, m_engine, metaObject);
451 _expr.code = _block->SYMBOL(irType, name, metaObject, data, IR::Name::ScopeStorage, line, column);
457 QDeclarativePropertyCache *cache = m_expression->component->synthCache;
458 const QMetaObject *metaObject = m_expression->component->metaObject();
459 if (!cache) cache = m_engine->cache(metaObject);
461 QDeclarativePropertyData *data = cache->property(name);
463 if (data && data->revision != 0) {
464 if (qmlVerboseCompiler())
465 qWarning() << "*** versioned symbol:" << name;
470 if (data && !data->isFunction()) {
471 IR::Type irType = irTypeFromVariantType(data->propType, m_engine, metaObject);
472 _expr.code = _block->SYMBOL(irType, name, metaObject, data, IR::Name::RootStorage, line, column);
477 if (!found && qmlVerboseCompiler())
478 qWarning() << "*** unknown symbol:" << name;
482 if (_expr.code && _expr.hint == ExprResult::cx) {
483 _expr.format = ExprResult::cx;
485 if (_expr.type() != IR::BoolType) {
486 IR::Temp *t = _block->TEMP(IR::BoolType);
487 _block->MOVE(t, _expr);
491 _block->CJUMP(_expr.code, _expr.iftrue, _expr.iffalse);
498 bool QV4IRBuilder::visit(AST::NullExpression *)
500 // ### TODO: cx format
501 _expr.code = _block->CONST(IR::NullType, 0);
505 bool QV4IRBuilder::visit(AST::TrueLiteral *)
507 // ### TODO: cx format
508 _expr.code = _block->CONST(IR::BoolType, 1);
512 bool QV4IRBuilder::visit(AST::FalseLiteral *)
514 // ### TODO: cx format
515 _expr.code = _block->CONST(IR::BoolType, 0);
519 bool QV4IRBuilder::visit(AST::StringLiteral *ast)
521 // ### TODO: cx format
522 _expr.code = _block->STRING(ast->value);
526 bool QV4IRBuilder::visit(AST::NumericLiteral *ast)
528 if (_expr.hint == ExprResult::cx) {
529 _expr.format = ExprResult::cx;
530 _block->JUMP(ast->value ? _expr.iftrue : _expr.iffalse);
532 _expr.code = _block->CONST(ast->value);
537 bool QV4IRBuilder::visit(AST::RegExpLiteral *)
542 bool QV4IRBuilder::visit(AST::NestedExpression *)
544 return true; // the value of the nested expression
547 bool QV4IRBuilder::visit(AST::ArrayMemberExpression *)
552 bool QV4IRBuilder::visit(AST::FieldMemberExpression *ast)
554 if (IR::Expr *left = expression(ast->base)) {
555 if (IR::Name *baseName = left->asName()) {
556 const quint32 line = ast->identifierToken.startLine;
557 const quint32 column = ast->identifierToken.startColumn;
559 QString name = ast->name.toString();
561 switch(baseName->symbol) {
562 case IR::Name::Unbound:
565 case IR::Name::AttachType:
566 if (name.at(0).isUpper()) {
567 QByteArray utf8Name = name.toUtf8();
568 const char *enumName = utf8Name.constData();
570 const QMetaObject *meta = baseName->declarativeType->metaObject();
572 for (int ii = 0; !found && ii < meta->enumeratorCount(); ++ii) {
573 QMetaEnum e = meta->enumerator(ii);
574 for (int jj = 0; !found && jj < e.keyCount(); ++jj) {
575 if (0 == strcmp(e.key(jj), enumName)) {
577 _expr.code = _block->CONST(IR::IntType, e.value(jj));
582 if (!found && qmlVerboseCompiler())
583 qWarning() << "*** unresolved enum:"
584 << (*baseName->id + QLatin1String(".") + ast->name.toString());
585 } else if(const QMetaObject *attachedMeta = baseName->declarativeType->attachedPropertiesType()) {
586 QDeclarativePropertyCache *cache = m_engine->cache(attachedMeta);
587 QDeclarativePropertyData *data = cache->property(name);
589 if (!data || data->isFunction())
590 return false; // Don't support methods (or non-existing properties ;)
592 if(!data->isFinal()) {
593 if (qmlVerboseCompiler())
594 qWarning() << "*** non-final attached property:"
595 << (*baseName->id + QLatin1String(".") + ast->name.toString());
596 return false; // We don't know enough about this property
599 IR::Type irType = irTypeFromVariantType(data->propType, m_engine, attachedMeta);
600 _expr.code = _block->SYMBOL(baseName, irType, name, attachedMeta, data, line, column);
604 case IR::Name::IdObject: {
605 const QDeclarativeScript::Object *idObject = baseName->idObject;
606 QDeclarativePropertyCache *cache =
607 idObject->synthCache?idObject->synthCache:m_engine->cache(idObject->metaObject());
609 QDeclarativePropertyData *data = cache->property(name);
611 if (!data || data->isFunction())
612 return false; // Don't support methods (or non-existing properties ;)
614 if (data->revision != 0) {
615 if (qmlVerboseCompiler())
616 qWarning() << "*** versioned symbol:" << name;
621 IR::Type irType = irTypeFromVariantType(data->propType, m_engine, idObject->metaObject());
622 _expr.code = _block->SYMBOL(baseName, irType, name,
623 idObject->metaObject(), data, line, column);
627 case IR::Name::Property:
628 if (baseName->type == IR::ObjectType) {
629 QDeclarativePropertyData *data = baseName->property;
630 if (!data || data->isFunction())
631 return false; // Don't support methods (or non-existing properties ;)
633 if(!data->isFinal()) {
634 if (qmlVerboseCompiler())
635 qWarning() << "*** non-final property access:"
636 << (*baseName->id + QLatin1String(".") + ast->name.toString());
637 return false; // We don't know enough about this property
640 IR::Type irType = irTypeFromVariantType(data->propType, m_engine, baseName->meta);
641 _expr.code = _block->SYMBOL(baseName, irType, name,
642 baseName->meta, data, line, column);
646 case IR::Name::Object:
656 bool QV4IRBuilder::preVisit(AST::Node *)
661 bool QV4IRBuilder::visit(AST::NewMemberExpression *)
666 bool QV4IRBuilder::visit(AST::NewExpression *)
671 bool QV4IRBuilder::visit(AST::CallExpression *ast)
673 QList<QStringRef> names;
674 QList<AST::ExpressionNode *> nameNodes;
677 nameNodes.reserve(4);
679 if (buildName(names, ast->base, &nameNodes)) {
680 //ExprResult base = expression(ast->base);
682 for (int i = 0; i < names.size(); ++i) {
684 id += QLatin1Char('.');
687 const AST::SourceLocation loc = nameNodes.last()->firstSourceLocation();
688 IR::Expr *base = _block->NAME(id, loc.startLine, loc.startColumn);
690 IR::ExprList *args = 0, **argsInserter = &args;
691 for (AST::ArgumentList *it = ast->arguments; it; it = it->next) {
692 IR::Expr *arg = expression(it->expression);
693 *argsInserter = _function->pool->New<IR::ExprList>();
694 (*argsInserter)->init(arg);
695 argsInserter = &(*argsInserter)->next;
698 IR::Temp *r = _block->TEMP(IR::InvalidType);
699 IR::Expr *call = _block->CALL(base, args);
700 _block->MOVE(r, call);
701 r->type = call->type;
708 bool QV4IRBuilder::visit(AST::PostIncrementExpression *)
713 bool QV4IRBuilder::visit(AST::PostDecrementExpression *)
718 bool QV4IRBuilder::visit(AST::DeleteExpression *)
723 bool QV4IRBuilder::visit(AST::VoidExpression *)
728 bool QV4IRBuilder::visit(AST::TypeOfExpression *)
733 bool QV4IRBuilder::visit(AST::PreIncrementExpression *)
738 bool QV4IRBuilder::visit(AST::PreDecrementExpression *)
743 bool QV4IRBuilder::visit(AST::UnaryPlusExpression *ast)
745 ExprResult expr = expression(ast->expression);
746 if (expr.isNot(IR::InvalidType)) {
747 if (expr.code->asConst() != 0) {
752 IR::Expr *code = _block->UNOP(IR::OpUPlus, expr);
753 _expr.code = _block->TEMP(code->type);
754 _block->MOVE(_expr, code);
760 bool QV4IRBuilder::visit(AST::UnaryMinusExpression *ast)
762 ExprResult expr = expression(ast->expression);
763 if (expr.isNot(IR::InvalidType)) {
764 if (IR::Const *c = expr.code->asConst()) {
766 _expr.code = _block->CONST(-c->value);
770 IR::Expr *code = _block->UNOP(IR::OpUMinus, expr);
771 _expr.code = _block->TEMP(code->type);
772 _block->MOVE(_expr, code);
778 bool QV4IRBuilder::visit(AST::TildeExpression *ast)
780 ExprResult expr = expression(ast->expression);
781 if (expr.isNot(IR::InvalidType)) {
782 if (IR::Const *c = expr.code->asConst()) {
784 _expr.code = _block->CONST(~int(c->value));
787 IR::Expr *code = _block->UNOP(IR::OpCompl, expr);
788 _expr.code = _block->TEMP(code->type);
789 _block->MOVE(_expr, code);
795 bool QV4IRBuilder::visit(AST::NotExpression *ast)
797 ExprResult expr = expression(ast->expression);
799 if (expr.isNot(IR::InvalidType)) {
800 if (IR::Const *c = expr.code->asConst()) {
802 _expr.code = _block->CONST(!c->value);
806 IR::Expr *code = _block->UNOP(IR::OpNot, expr);
807 _expr.code = _block->TEMP(code->type);
808 _block->MOVE(_expr, code);
810 } else if (expr.hint == ExprResult::cx) {
811 expr.format = ExprResult::cx;
812 _block->CJUMP(_block->UNOP(IR::OpNot, expr), _expr.iftrue, _expr.iffalse);
819 void QV4IRBuilder::binop(AST::BinaryExpression *ast, ExprResult left, ExprResult right)
821 if (IR::Type t = maxType(left.type(), right.type())) {
822 implicitCvt(left, t);
823 implicitCvt(right, t);
825 if (_expr.hint == ExprResult::cx) {
826 _expr.format = ExprResult::cx;
827 _block->CJUMP(_block->BINOP(IR::binaryOperator(ast->op), left, right), _expr.iftrue, _expr.iffalse);
829 IR::Expr *code = _block->BINOP(IR::binaryOperator(ast->op), left, right);
830 _expr.code = _block->TEMP(code->type);
831 _block->MOVE(_expr.code, code);
836 bool QV4IRBuilder::visit(AST::BinaryExpression *ast)
839 case QSOperator::And: {
840 if (_expr.hint == ExprResult::cx) {
841 _expr.format = ExprResult::cx;
843 Q_ASSERT(_expr.iffalse != 0);
844 Q_ASSERT(_expr.iftrue != 0);
846 IR::BasicBlock *iftrue = _function->newBasicBlock();
847 condition(ast->left, iftrue, _expr.iffalse);
850 condition(ast->right, _expr.iftrue, _expr.iffalse);
852 IR::BasicBlock *iftrue = _function->newBasicBlock();
853 IR::BasicBlock *iffalse = _function->newBasicBlock();
854 IR::BasicBlock *endif = _function->newBasicBlock();
856 condition(ast->left, iftrue, iffalse);
858 IR::Temp *r = _block->TEMP(IR::InvalidType);
861 _block->MOVE(r, _block->CONST(0)); // ### use the right null value
865 ExprResult right = expression(ast->right);
866 _block->MOVE(r, right);
871 r->type = right.type(); // ### not exactly, it can be IR::BoolType.
876 case QSOperator::Or: {
877 IR::BasicBlock *iftrue = _function->newBasicBlock();
878 IR::BasicBlock *endif = _function->newBasicBlock();
880 ExprResult left = expression(ast->left);
881 IR::Temp *r = _block->TEMP(left.type());
882 _block->MOVE(r, left);
885 if (r->type != IR::BoolType) {
886 cond = _block->TEMP(IR::BoolType);
887 _block->MOVE(cond, r);
890 _block->CJUMP(_block->UNOP(IR::OpNot, cond), iftrue, endif);
893 ExprResult right = expression(ast->right);
894 _block->MOVE(r, right);
896 if (left.type() != right.type())
907 case QSOperator::Ge: {
908 ExprResult left = expression(ast->left);
909 ExprResult right = expression(ast->right);
910 if (left.type() == IR::StringType && right.type() == IR::StringType) {
911 binop(ast, left, right);
912 } else if (left.isValid() && right.isValid()) {
913 implicitCvt(left, IR::RealType);
914 implicitCvt(right, IR::RealType);
915 binop(ast, left, right);
919 case QSOperator::NotEqual:
920 case QSOperator::Equal: {
921 ExprResult left = expression(ast->left);
922 ExprResult right = expression(ast->right);
923 if ((left.type() == IR::NullType || left.type() == IR::UndefinedType) &&
924 (right.type() == IR::NullType || right.type() == IR::UndefinedType)) {
925 const bool isEq = ast->op == QSOperator::Equal;
926 if (_expr.hint == ExprResult::cx) {
927 _expr.format = ExprResult::cx;
928 _block->JUMP(isEq ? _expr.iftrue : _expr.iffalse);
930 _expr.code = _block->CONST(IR::BoolType, isEq ? 1 : 0);
932 } else if ((left.type() == IR::StringType && right.type() >= IR::FirstNumberType) ||
933 (left.type() >= IR::FirstNumberType && right.type() == IR::StringType)) {
934 implicitCvt(left, IR::RealType);
935 implicitCvt(right, IR::RealType);
936 binop(ast, left, right);
937 } else if (left.type() == IR::BoolType || right.type() == IR::BoolType) {
938 implicitCvt(left, IR::BoolType);
939 implicitCvt(right, IR::BoolType);
940 } else if (left.isValid() && right.isValid()) {
941 binop(ast, left, right);
945 case QSOperator::StrictEqual:
946 case QSOperator::StrictNotEqual: {
947 ExprResult left = expression(ast->left);
948 ExprResult right = expression(ast->right);
949 if (left.type() == right.type()) {
950 binop(ast, left, right);
951 } else if (left.type() >= IR::BoolType && right.type() >= IR::BoolType) {
952 // left and right have numeric type (int or real)
953 binop(ast, left, right);
954 } else if (left.isValid() && right.isValid()) {
955 const bool isEq = ast->op == QSOperator::StrictEqual;
956 if (_expr.hint == ExprResult::cx) {
957 _expr.format = ExprResult::cx;
958 _block->JUMP(isEq ? _expr.iftrue : _expr.iffalse);
960 _expr.code = _block->CONST(IR::BoolType, isEq ? 1 : 0);
965 case QSOperator::BitAnd:
966 case QSOperator::BitOr:
967 case QSOperator::BitXor:
968 case QSOperator::LShift:
969 case QSOperator::RShift:
970 case QSOperator::URShift: {
971 ExprResult left = expression(ast->left);
972 if (left.is(IR::InvalidType))
975 ExprResult right = expression(ast->right);
976 if (right.is(IR::InvalidType))
979 implicitCvt(left, IR::IntType);
980 implicitCvt(right, IR::IntType);
982 IR::Expr *code = _block->BINOP(IR::binaryOperator(ast->op), left, right);
983 _expr.code = _block->TEMP(code->type);
984 _block->MOVE(_expr.code, code);
988 case QSOperator::Add: {
989 ExprResult left = expression(ast->left);
990 if (left.is(IR::InvalidType))
993 ExprResult right = expression(ast->right);
994 if (right.is(IR::InvalidType))
997 if (left.isPrimitive() && right.isPrimitive()) {
998 if (left.type() == IR::StringType || right.type() == IR::StringType) {
999 implicitCvt(left, IR::StringType);
1000 implicitCvt(right, IR::StringType);
1002 binop(ast, left, right);
1006 case QSOperator::Div:
1007 case QSOperator::Mod:
1008 case QSOperator::Mul:
1009 case QSOperator::Sub: {
1010 ExprResult left = expression(ast->left);
1011 if (left.is(IR::InvalidType))
1014 ExprResult right = expression(ast->right);
1015 if (right.is(IR::InvalidType))
1018 IR::Type t = maxType(left.type(), right.type());
1019 if (t >= IR::FirstNumberType) {
1020 implicitCvt(left, IR::RealType);
1021 implicitCvt(right, IR::RealType);
1023 IR::Expr *code = _block->BINOP(IR::binaryOperator(ast->op), left, right);
1024 _expr.code = _block->TEMP(code->type);
1025 _block->MOVE(_expr.code, code);
1029 case QSOperator::In:
1030 case QSOperator::InstanceOf:
1031 case QSOperator::Assign:
1032 case QSOperator::InplaceAnd:
1033 case QSOperator::InplaceSub:
1034 case QSOperator::InplaceDiv:
1035 case QSOperator::InplaceAdd:
1036 case QSOperator::InplaceLeftShift:
1037 case QSOperator::InplaceMod:
1038 case QSOperator::InplaceMul:
1039 case QSOperator::InplaceOr:
1040 case QSOperator::InplaceRightShift:
1041 case QSOperator::InplaceURightShift:
1042 case QSOperator::InplaceXor:
1043 // yup, we don't do those.
1050 bool QV4IRBuilder::visit(AST::ConditionalExpression *ast)
1052 IR::BasicBlock *iftrue = _function->newBasicBlock();
1053 IR::BasicBlock *iffalse = _function->newBasicBlock();
1054 IR::BasicBlock *endif = _function->newBasicBlock();
1056 condition(ast->expression, iftrue, iffalse);
1058 IR::Temp *r = _block->TEMP(IR::InvalidType);
1060 qSwap(_block, iftrue);
1061 ExprResult ok = expression(ast->ok);
1062 _block->MOVE(r, ok);
1063 _block->JUMP(endif);
1064 qSwap(_block, iftrue);
1066 qSwap(_block, iffalse);
1067 ExprResult ko = expression(ast->ko);
1068 _block->MOVE(r, ko);
1069 _block->JUMP(endif);
1070 qSwap(_block, iffalse);
1072 r->type = maxType(ok.type(), ko.type());
1080 bool QV4IRBuilder::visit(AST::Expression *ast)
1082 _block->EXP(expression(ast->left));
1083 _expr = expression(ast->right);
1090 bool QV4IRBuilder::visit(AST::Block *ast)
1092 if (ast->statements && ! ast->statements->next) {
1093 // we have one and only one statement
1094 accept(ast->statements->statement);
1100 bool QV4IRBuilder::visit(AST::StatementList *)
1105 bool QV4IRBuilder::visit(AST::VariableStatement *)
1110 bool QV4IRBuilder::visit(AST::VariableDeclarationList *)
1115 bool QV4IRBuilder::visit(AST::VariableDeclaration *)
1120 bool QV4IRBuilder::visit(AST::EmptyStatement *)
1125 bool QV4IRBuilder::visit(AST::ExpressionStatement *ast)
1127 if (ast->expression) {
1128 // return the value of this expression
1135 bool QV4IRBuilder::visit(AST::IfStatement *ast)
1138 // This is an if statement without an else branch.
1141 IR::BasicBlock *iftrue = _function->newBasicBlock();
1142 IR::BasicBlock *iffalse = _function->newBasicBlock();
1143 IR::BasicBlock *endif = _function->newBasicBlock();
1145 condition(ast->expression, iftrue, iffalse);
1147 IR::Temp *r = _block->TEMP(IR::InvalidType);
1149 qSwap(_block, iftrue);
1150 ExprResult ok = statement(ast->ok);
1151 _block->MOVE(r, ok);
1152 _block->JUMP(endif);
1153 qSwap(_block, iftrue);
1155 qSwap(_block, iffalse);
1156 ExprResult ko = statement(ast->ko);
1157 _block->MOVE(r, ko);
1158 _block->JUMP(endif);
1159 qSwap(_block, iffalse);
1161 r->type = maxType(ok.type(), ko.type());
1170 bool QV4IRBuilder::visit(AST::DoWhileStatement *)
1175 bool QV4IRBuilder::visit(AST::WhileStatement *)
1180 bool QV4IRBuilder::visit(AST::ForStatement *)
1185 bool QV4IRBuilder::visit(AST::LocalForStatement *)
1190 bool QV4IRBuilder::visit(AST::ForEachStatement *)
1195 bool QV4IRBuilder::visit(AST::LocalForEachStatement *)
1201 bool QV4IRBuilder::visit(AST::ContinueStatement *)
1206 bool QV4IRBuilder::visit(AST::BreakStatement *)
1211 bool QV4IRBuilder::visit(AST::ReturnStatement *ast)
1213 if (ast->expression) {
1214 // return the value of the expression
1221 bool QV4IRBuilder::visit(AST::WithStatement *)
1226 bool QV4IRBuilder::visit(AST::SwitchStatement *)
1231 bool QV4IRBuilder::visit(AST::CaseBlock *)
1236 bool QV4IRBuilder::visit(AST::CaseClauses *)
1241 bool QV4IRBuilder::visit(AST::CaseClause *)
1246 bool QV4IRBuilder::visit(AST::DefaultClause *)
1251 bool QV4IRBuilder::visit(AST::LabelledStatement *)
1256 bool QV4IRBuilder::visit(AST::ThrowStatement *)
1261 bool QV4IRBuilder::visit(AST::TryStatement *)
1266 bool QV4IRBuilder::visit(AST::Catch *)
1271 bool QV4IRBuilder::visit(AST::Finally *)
1276 bool QV4IRBuilder::visit(AST::FunctionDeclaration *)
1281 bool QV4IRBuilder::visit(AST::FunctionExpression *)
1286 bool QV4IRBuilder::visit(AST::FormalParameterList *)
1291 bool QV4IRBuilder::visit(AST::FunctionBody *)
1296 bool QV4IRBuilder::visit(AST::DebuggerStatement *)