Add Math.ceil and Math.abs support to v4.
authorMichael Brasser <michael.brasser@nokia.com>
Wed, 7 Mar 2012 23:16:07 +0000 (09:16 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 12 Mar 2012 22:28:15 +0000 (23:28 +0100)
Change-Id: I23e7c8294abf80914d4529740af6e9124f66c5bf
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
src/qml/qml/v4/qv4bindings.cpp
src/qml/qml/v4/qv4compiler.cpp
src/qml/qml/v4/qv4instruction.cpp
src/qml/qml/v4/qv4instruction_p.h
src/qml/qml/v4/qv4ir.cpp
src/qml/qml/v4/qv4ir_p.h

index 4fd8494..3f3b666 100644 (file)
@@ -1172,6 +1172,15 @@ void QV4Bindings::run(int instrIndex, quint32 &executedBlocks,
     }
     QML_V4_END_INSTR(MathCosReal, unaryop)
 
+    QML_V4_BEGIN_INSTR(MathAbsReal, unaryop)
+    {
+        const Register &src = registers[instr->unaryop.src];
+        Register &output = registers[instr->unaryop.output];
+        if (src.isUndefined()) output.setUndefined();
+        else output.setqreal(qAbs(src.getqreal()));
+    }
+    QML_V4_END_INSTR(MathAbsReal, unaryop)
+
     QML_V4_BEGIN_INSTR(MathRoundReal, unaryop)
     {
         const Register &src = registers[instr->unaryop.src];
@@ -1190,6 +1199,15 @@ void QV4Bindings::run(int instrIndex, quint32 &executedBlocks,
     }
     QML_V4_END_INSTR(MathFloorReal, unaryop)
 
+    QML_V4_BEGIN_INSTR(MathCeilReal, unaryop)
+    {
+        const Register &src = registers[instr->unaryop.src];
+        Register &output = registers[instr->unaryop.output];
+        if (src.isUndefined()) output.setUndefined();
+        else output.setint(qCeil(src.getqreal()));
+    }
+    QML_V4_END_INSTR(MathCeilReal, unaryop)
+
     QML_V4_BEGIN_INSTR(MathPIReal, unaryop)
     {
         static const qreal qmlPI = 2.0 * qAsin(1.0);
index 608368a..ce83c2d 100644 (file)
@@ -803,6 +803,12 @@ void QV4CompilerPrivate::visitCall(IR::Call *call)
                 gen(i);
                 } return;
 
+            case IR::MathAbsBuiltinFunction: {
+                Instr::MathAbsReal i;
+                i.output = i.src = currentReg;
+                gen(i);
+                } return;
+
             case IR::MathRoundBultinFunction: {
                 Instr::MathRoundReal i;
                 i.output = i.src = currentReg;
@@ -815,6 +821,12 @@ void QV4CompilerPrivate::visitCall(IR::Call *call)
                 gen(i);
                 } return;
 
+            case IR::MathCeilBuiltinFunction: {
+                Instr::MathCeilReal i;
+                i.output = i.src = currentReg;
+                gen(i);
+                } return;
+
             case IR::MathPIBuiltinConstant:
                 break;
             } // switch
index efbd2b2..309ae90 100644 (file)
@@ -195,12 +195,18 @@ void Bytecode::dump(const V4Instr *i, int address) const
     case V4Instr::MathCosReal:
         INSTR_DUMP << "\t" << "MathCosReal" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
         break;
+    case V4Instr::MathAbsReal:
+        INSTR_DUMP << "\t" << "MathAbsReal" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
+        break;
     case V4Instr::MathRoundReal:
         INSTR_DUMP << "\t" << "MathRoundReal" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
         break;
     case V4Instr::MathFloorReal:
         INSTR_DUMP << "\t" << "MathFloorReal" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
         break;
+    case V4Instr::MathCeilReal:
+        INSTR_DUMP << "\t" << "MathCeilReal" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
+        break;
     case V4Instr::MathPIReal:
         INSTR_DUMP << "\t" << "MathPIReal" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
         break;
index 964c955..fef8565 100644 (file)
@@ -100,8 +100,10 @@ QT_BEGIN_NAMESPACE
     F(ResolveUrl, unaryop) \
     F(MathSinReal, unaryop) \
     F(MathCosReal, unaryop) \
+    F(MathAbsReal, unaryop) \
     F(MathRoundReal, unaryop) \
     F(MathFloorReal, unaryop) \
+    F(MathCeilReal, unaryop) \
     F(MathPIReal, unaryop) \
     F(LoadReal, real_value) \
     F(LoadInt, int_value) \
index 68175d3..051df56 100644 (file)
@@ -219,10 +219,14 @@ void Name::init(Name *base, Type type, const QString *id, Symbol symbol, quint32
         builtin = MathSinBultinFunction;
     } else if (id->length() == 8 && *id == QLatin1String("Math.cos")) {
         builtin = MathCosBultinFunction;
+    } else if (id->length() == 8 && *id == QLatin1String("Math.abs")) {
+        builtin = MathAbsBuiltinFunction;
     } else if (id->length() == 10 && *id == QLatin1String("Math.round")) {
         builtin = MathRoundBultinFunction;
     } else if (id->length() == 10 && *id == QLatin1String("Math.floor")) {
         builtin = MathFloorBultinFunction;
+    } else if (id->length() == 9 && *id == QLatin1String("Math.ceil")) {
+        builtin = MathCeilBuiltinFunction;
     } else if (id->length() == 7 && *id == QLatin1String("Math.PI")) {
         builtin = MathPIBuiltinConstant;
         this->type = RealType;
@@ -353,10 +357,12 @@ Type Call::typeForFunction(Expr *base)
         switch (name->builtin) {
         case MathSinBultinFunction:
         case MathCosBultinFunction:
+        case MathAbsBuiltinFunction:    //### type could also be Int if input was Int
             return RealType;
 
         case MathRoundBultinFunction:
         case MathFloorBultinFunction:
+        case MathCeilBuiltinFunction:
             return IntType;
 
         case NoBuiltinSymbol:
index 3d3288b..6520131 100644 (file)
@@ -239,6 +239,8 @@ enum BuiltinSymbol {
     MathCosBultinFunction,
     MathRoundBultinFunction,
     MathFloorBultinFunction,
+    MathCeilBuiltinFunction,
+    MathAbsBuiltinFunction,
 
     MathPIBuiltinConstant
 };