Fix isel for eval and a whole bunch of other warnings.
authorErik Verbruggen <erik.verbruggen@digia.com>
Mon, 19 Nov 2012 12:15:25 +0000 (13:15 +0100)
committerLars Knoll <lars.knoll@digia.com>
Mon, 19 Nov 2012 13:02:58 +0000 (14:02 +0100)
A factory is now passed along to do the codegen for eval().

Change-Id: If15b1f28c9c0a8f8b6d18b56d6e7bc5d942927e5
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
14 files changed:
main.cpp
moth/qv4isel_moth_p.h
qmljs_engine.cpp
qmljs_engine.h
qmljs_environment.cpp
qmljs_math.h
qmljs_objects.cpp
qmljs_objects.h
qmljs_value.h
qv4isel_llvm_p.h
qv4isel_masm.cpp
qv4isel_masm_p.h
qv4isel_p.h [new file with mode: 0644]
v4.pro

index d147d04..2442cd1 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -51,6 +51,7 @@
 #include "qv4vme_moth_p.h"
 #include "qv4syntaxchecker_p.h"
 #include "qv4ecmaobjects_p.h"
+#include "qv4isel_p.h"
 
 #include <QtCore>
 #include <private/qqmljsengine_p.h>
@@ -110,10 +111,11 @@ int executeLLVMCode(void *codePtr)
 
     if (!codePtr)
         return EXIT_FAILURE;
-    void (*code)(VM::Context *) = (void (*)(VM::Context *)) codePtr;
+    void (*code)(VM::ExecutionContext *) = (void (*)(VM::ExecutionContext *)) codePtr;
 
-    VM::ExecutionEngine vm;
-    VM::Context *ctx = vm.rootContext;
+    QScopedPointer<QQmlJS::EValISelFactory> iSelFactory(new QQmlJS::Moth::ISelFactory);
+    VM::ExecutionEngine vm(iSelFactory.data());
+    VM::ExecutionContext *ctx = vm.rootContext;
 
     QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue();
     globalObject->__put__(ctx, vm.identifier(QStringLiteral("print")),
@@ -280,8 +282,12 @@ int main(int argc, char *argv[])
 #endif // QMLJS_NO_LLVM
     case use_masm:
     case use_moth: {
-        bool useInterpreter = mode == use_moth;
-        QQmlJS::VM::ExecutionEngine vm;
+        QScopedPointer<QQmlJS::EValISelFactory> iSelFactory;
+        if (mode == use_moth)
+            iSelFactory.reset(new QQmlJS::Moth::ISelFactory);
+        else
+            iSelFactory.reset(new QQmlJS::MASM::ISelFactory);
+        QQmlJS::VM::ExecutionEngine vm(iSelFactory.data());
         QQmlJS::VM::ExecutionContext *ctx = vm.rootContext;
 
         QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue();
@@ -299,7 +305,7 @@ int main(int argc, char *argv[])
                 const QString code = QString::fromUtf8(file.readAll());
                 file.close();
 
-                int exitCode = QQmlJS::VM::EvalFunction::evaluate(vm.rootContext, fn, code, useInterpreter, QQmlJS::Codegen::GlobalCode);
+                int exitCode = QQmlJS::VM::EvalFunction::evaluate(vm.rootContext, fn, code, iSelFactory.data(), QQmlJS::Codegen::GlobalCode);
                 if (exitCode != EXIT_SUCCESS)
                     return exitCode;
                 if (errorInTestHarness)
index bb024c5..1105eb1 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef QV4ISEL_MOTH_P_H
 #define QV4ISEL_MOTH_P_H
 
+#include "qv4isel_p.h"
 #include "qv4ir_p.h"
 #include "qmljs_objects.h"
 #include "qv4instr_moth_p.h"
@@ -8,13 +9,18 @@
 namespace QQmlJS {
 namespace Moth {
 
-class InstructionSelection : public IR::StmtVisitor
+class InstructionSelection : public IR::StmtVisitor, public EvalInstructionSelection
 {
 public:
     InstructionSelection(VM::ExecutionEngine *engine, IR::Module *module, uchar *code);
     ~InstructionSelection();
 
-    void operator()(IR::Function *function);
+    virtual void run(IR::Function *function)
+    { this->operator()(function); }
+    virtual void operator()(IR::Function *function);
+
+    virtual bool finishModule(size_t)
+    { return true; }
 
 protected:
     virtual void visitExp(IR::Exp *);
@@ -57,6 +63,14 @@ private:
     uchar *_ccode;
 };
 
+class ISelFactory: public EValISelFactory
+{
+public:
+    virtual ~ISelFactory() {}
+    virtual EvalInstructionSelection *create(VM::ExecutionEngine *engine, IR::Module *module, uchar *code)
+    { return new InstructionSelection(engine, module, code); }
+};
+
 template<int InstrT>
 ptrdiff_t InstructionSelection::addInstruction(const InstrData<InstrT> &data)
 {
index 4415231..40cf7f3 100644 (file)
@@ -45,7 +45,7 @@
 namespace QQmlJS {
 namespace VM {
 
-ExecutionEngine::ExecutionEngine()
+ExecutionEngine::ExecutionEngine(EValISelFactory *factory)
 {
     rootContext = newContext();
     rootContext->init(this);
@@ -160,7 +160,7 @@ ExecutionEngine::ExecutionEngine()
     glo->__put__(rootContext, identifier(QStringLiteral("undefined")), Value::undefinedValue());
     glo->__put__(rootContext, identifier(QStringLiteral("NaN")), Value::fromDouble(nan("")));
     glo->__put__(rootContext, identifier(QStringLiteral("Infinity")), Value::fromDouble(INFINITY));
-    glo->__put__(rootContext, identifier(QStringLiteral("eval")), Value::fromObject(new EvalFunction(rootContext)));
+    glo->__put__(rootContext, identifier(QStringLiteral("eval")), Value::fromObject(new EvalFunction(rootContext, factory)));
 
     // TODO: parseInt [15.1.2.2]
     // TODO: parseFloat [15.1.2.3]
index f111979..4ecc386 100644 (file)
@@ -41,6 +41,7 @@
 #ifndef QMLJS_ENGINE_H
 #define QMLJS_ENGINE_H
 
+#include <qv4isel_p.h>
 #include <qmljs_objects.h>
 #include <qmljs_environment.h>
 #include <setjmp.h>
@@ -49,7 +50,7 @@ namespace QQmlJS {
 namespace VM {
 
 struct Value;
-struct Array;
+class Array;
 struct Object;
 struct BooleanObject;
 struct NumberObject;
@@ -136,7 +137,7 @@ struct ExecutionEngine
 
     QVector<ExceptionHandler> unwindStack;
 
-    ExecutionEngine();
+    ExecutionEngine(EValISelFactory *factory);
 
     ExecutionContext *newContext();
 
index 3380064..4a4d8e1 100644 (file)
@@ -109,7 +109,7 @@ void DeclarativeEnvironment::createMutableBinding(String *name, bool deletable)
     deletableLocals.insert(name->toQString(), Value::undefinedValue());
 }
 
-void DeclarativeEnvironment::setMutableBinding(String *name, Value value, bool strict)
+void DeclarativeEnvironment::setMutableBinding(String *name, Value value, bool /*strict*/)
 {
     // ### throw if strict is true, and it would change an immutable binding
     for (unsigned int i = 0; i < varCount; ++i) {
@@ -132,7 +132,7 @@ void DeclarativeEnvironment::setMutableBinding(String *name, Value value, bool s
     assert(false);
 }
 
-Value DeclarativeEnvironment::getBindingValue(String *name, bool strict) const
+Value DeclarativeEnvironment::getBindingValue(String *name, bool /*strict*/) const
 {
     for (unsigned int i = 0; i < varCount; ++i) {
         if (__qmljs_string_equal(vars[i], name))
index 492f7c8..1fb9261 100644 (file)
@@ -98,9 +98,9 @@ static inline Value mul_int32(int a, int b)
         return Value::fromInt32(aa);
     return Value::fromDouble((double)a * (double)b);
 }
-#endif
 
 } // namespace VM
 } // namespace QQmlJS
 
-#endif
+#endif // !defined(QMLJS_LLVM_RUNTIME) && 1 //CPU(X86_64)
+#endif // QMLJS_MATH_H
index 290a4f7..c34af50 100644 (file)
@@ -42,6 +42,7 @@
 
 #include "qmljs_objects.h"
 #include "qv4ir_p.h"
+#include "qv4isel_p.h"
 #include "qv4ecmaobjects_p.h"
 
 #include <private/qqmljsengine_p.h>
@@ -50,7 +51,6 @@
 #include <private/qqmljsast_p.h>
 #include <qv4ir_p.h>
 #include <qv4codegen_p.h>
-#include <qv4isel_masm_p.h>
 
 #include <QtCore/qmath.h>
 #include <QtCore/QDebug>
@@ -459,7 +459,7 @@ void ScriptFunction::call(VM::ExecutionContext *ctx)
 }
 
 
-Value EvalFunction::call(ExecutionContext *context, Value thisObject, Value *args, int argc, bool strictMode)
+Value EvalFunction::call(ExecutionContext *context, Value /*thisObject*/, Value *args, int argc, bool strictMode)
 {
     Value s = context->argument(0);
     if (!s.isString()) {
@@ -481,10 +481,15 @@ Value EvalFunction::call(ExecutionContext *context, Value thisObject, Value *arg
         ctx = context;
     }
     // ##### inline and do this in the correct scope
-    evaluate(ctx, QStringLiteral("eval code"), code, /*useInterpreter*/ false, QQmlJS::Codegen::EvalCode);
+    int result = evaluate(ctx, QStringLiteral("eval code"), code, _factory, QQmlJS::Codegen::EvalCode);
 
     if (strictMode)
         ctx->leaveCallContext();
+
+    if (result == EXIT_SUCCESS)
+        return ctx->result;
+    else
+        return Value::undefinedValue();
 }
 
 /// isNaN [15.1.2.4]
@@ -503,18 +508,8 @@ Value IsFiniteFunction::call(ExecutionContext * /*context*/, Value /*thisObject*
     return Value::fromBoolean(v.isDouble() ? std::isfinite(v.doubleValue()) : true);
 }
 
-static inline bool protect(const void *addr, size_t size)
-{
-    size_t pageSize = sysconf(_SC_PAGESIZE);
-    size_t iaddr = reinterpret_cast<size_t>(addr);
-    size_t roundAddr = iaddr & ~(pageSize - static_cast<size_t>(1));
-    int mode = PROT_READ | PROT_WRITE | PROT_EXEC;
-    return mprotect(reinterpret_cast<void*>(roundAddr), size + (iaddr - roundAddr), mode) == 0;
-}
-
-
 int EvalFunction::evaluate(QQmlJS::VM::ExecutionContext *ctx, const QString &fileName,
-                           const QString &source, bool useInterpreter,
+                           const QString &source, EValISelFactory *factory,
                            QQmlJS::Codegen::Mode mode)
 {
     using namespace QQmlJS;
@@ -550,20 +545,15 @@ int EvalFunction::evaluate(QQmlJS::VM::ExecutionContext *ctx, const QString &fil
             Codegen cg;
             globalCode = cg(program, &module, mode);
 
-//            if (useInterpreter) {
-//                Moth::InstructionSelection isel(vm, &module, code);
-//                foreach (IR::Function *function, module.functions)
-//                    isel(function);
-//            } else
-            {
-                foreach (IR::Function *function, module.functions) {
-                    MASM::InstructionSelection isel(vm, &module, code);
-                    isel(function);
-                }
-
-                if (! protect(code, codeSize))
-                    Q_UNREACHABLE();
-            }
+            EvalInstructionSelection *isel = factory->create(vm, &module, code);
+
+            foreach (IR::Function *function, module.functions)
+                isel->run(function);
+
+            if (! isel->finishModule(codeSize))
+                Q_UNREACHABLE();
+
+            delete isel;
         }
 
         if (! globalCode)
index a2b956f..083c124 100644 (file)
@@ -46,6 +46,7 @@
 #include "qmljs_environment.h"
 #include "qv4array_p.h"
 #include "qv4codegen_p.h"
+#include "qv4isel_p.h"
 
 #include <QtCore/QString>
 #include <QtCore/QHash>
@@ -523,13 +524,16 @@ struct ScriptFunction: FunctionObject {
 
 struct EvalFunction : FunctionObject
 {
-    EvalFunction(ExecutionContext *scope): FunctionObject(scope) {}
+    EvalFunction(ExecutionContext *scope, EValISelFactory *factory): FunctionObject(scope), _factory(factory) {}
 
     static int evaluate(QQmlJS::VM::ExecutionContext *ctx, const QString &fileName,
-                        const QString &source, bool useInterpreter,
+                        const QString &source, EValISelFactory *factory,
                         QQmlJS::Codegen::Mode mode);
 
     virtual Value call(ExecutionContext *context, Value thisObject, Value *args, int argc, bool strictMode = false);
+
+private:
+    EValISelFactory *_factory;
 };
 
 struct IsNaNFunction: FunctionObject
index b504af0..e15b970 100644 (file)
@@ -50,7 +50,7 @@
 namespace QQmlJS {
 namespace VM {
 
-struct Array;
+class Array;
 struct String;
 struct Object;
 struct BooleanObject;
index 2142ea4..3f51a21 100644 (file)
@@ -54,6 +54,7 @@
 #  pragma clang diagnostic pop
 #endif // __clang__
 
+#include "qv4isel_p.h"
 #include "qv4ir_p.h"
 
 namespace QQmlJS {
index 8a8e663..6290dd5 100644 (file)
@@ -170,6 +170,20 @@ void InstructionSelection::operator()(IR::Function *function)
     qSwap(_function, function);
 }
 
+static inline bool protect(const void *addr, size_t size)
+{
+    size_t pageSize = sysconf(_SC_PAGESIZE);
+    size_t iaddr = reinterpret_cast<size_t>(addr);
+    size_t roundAddr = iaddr & ~(pageSize - static_cast<size_t>(1));
+    int mode = PROT_READ | PROT_WRITE | PROT_EXEC;
+    return mprotect(reinterpret_cast<void*>(roundAddr), size + (iaddr - roundAddr), mode) == 0;
+}
+
+bool InstructionSelection::finishModule(size_t size)
+{
+    return protect(_code, size);
+}
+
 String *InstructionSelection::identifier(const QString &s)
 {
     return _engine->identifier(s);
index 82bc471..aaaf2e0 100644 (file)
@@ -42,6 +42,7 @@
 #define QV4ISEL_MASM_P_H
 
 #include "qv4ir_p.h"
+#include "qv4isel_p.h"
 #include "qv4isel_util_p.h"
 #include "qmljs_objects.h"
 #include "qmljs_runtime.h"
 namespace QQmlJS {
 namespace MASM {
 
-class InstructionSelection: protected IR::StmtVisitor, public JSC::MacroAssembler
+class InstructionSelection: protected IR::StmtVisitor, public JSC::MacroAssembler, public EvalInstructionSelection
 {
 public:
     InstructionSelection(VM::ExecutionEngine *engine, IR::Module *module, uchar *code);
     ~InstructionSelection();
 
+    virtual void run(IR::Function *function)
+    { this->operator()(function); }
     void operator()(IR::Function *function);
 
+    virtual bool finishModule(size_t size);
+
 protected:
 #if CPU(X86)
 
@@ -637,6 +642,14 @@ private:
     QList<CallToLink> _callsToLink;
 };
 
+class ISelFactory: public EValISelFactory
+{
+public:
+    virtual ~ISelFactory() {}
+    virtual EvalInstructionSelection *create(VM::ExecutionEngine *engine, IR::Module *module, uchar *code)
+    { return new InstructionSelection(engine, module, code); }
+};
+
 } // end of namespace MASM
 } // end of namespace QQmlJS
 
diff --git a/qv4isel_p.h b/qv4isel_p.h
new file mode 100644 (file)
index 0000000..effd44f
--- /dev/null
@@ -0,0 +1,64 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef QV4ISEL_P_H
+#define QV4ISEL_P_H
+
+#include <qglobal.h>
+
+namespace QQmlJS {
+
+namespace IR {
+struct Function;
+struct Module;
+} // namespace IR;
+
+namespace VM {
+struct ExecutionEngine;
+} // namespace VM
+
+class EvalInstructionSelection
+{
+public:
+    virtual ~EvalInstructionSelection() = 0;
+
+    virtual void run(IR::Function *function) = 0;
+    virtual bool finishModule(size_t codeSize) = 0;
+};
+
+class EValISelFactory
+{
+public:
+    virtual ~EValISelFactory() = 0;
+    virtual EvalInstructionSelection *create(VM::ExecutionEngine *engine, IR::Module *module, uchar *code) = 0;
+};
+
+} // namespace QQmlJS
+
+#endif // QV4ISEL_P_H
diff --git a/v4.pro b/v4.pro
index 9358bf4..1a6e2a4 100644 (file)
--- a/v4.pro
+++ b/v4.pro
@@ -22,7 +22,8 @@ SOURCES += main.cpp \
     qv4ecmaobjects.cpp \
     qv4array.cpp \
     qv4isel_masm.cpp \
-    llvm_runtime.cpp
+    llvm_runtime.cpp \
+    qv4isel_p.cpp
 
 HEADERS += \
     qv4codegen_p.h \
@@ -37,6 +38,7 @@ HEADERS += \
     qv4ecmaobjects_p.h \
     qv4array_p.h \
     qv4isel_masm_p.h \
+    qv4isel_p.h \
     qv4isel_util_p.h
 
 llvm {