diff --git a/main.cpp b/main.cpp index d147d04a91..2442cd1bc7 100644 --- 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 #include @@ -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 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 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) diff --git a/moth/qv4isel_moth_p.h b/moth/qv4isel_moth_p.h index bb024c58f3..1105eb1c73 100644 --- a/moth/qv4isel_moth_p.h +++ b/moth/qv4isel_moth_p.h @@ -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 ptrdiff_t InstructionSelection::addInstruction(const InstrData &data) { diff --git a/qmljs_engine.cpp b/qmljs_engine.cpp index 4415231934..40cf7f3294 100644 --- a/qmljs_engine.cpp +++ b/qmljs_engine.cpp @@ -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] diff --git a/qmljs_engine.h b/qmljs_engine.h index f111979f0d..4ecc386d00 100644 --- a/qmljs_engine.h +++ b/qmljs_engine.h @@ -41,6 +41,7 @@ #ifndef QMLJS_ENGINE_H #define QMLJS_ENGINE_H +#include #include #include #include @@ -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 unwindStack; - ExecutionEngine(); + ExecutionEngine(EValISelFactory *factory); ExecutionContext *newContext(); diff --git a/qmljs_environment.cpp b/qmljs_environment.cpp index 338006474e..4a4d8e1864 100644 --- a/qmljs_environment.cpp +++ b/qmljs_environment.cpp @@ -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)) diff --git a/qmljs_math.h b/qmljs_math.h index 492f7c8692..1fb9261835 100644 --- a/qmljs_math.h +++ b/qmljs_math.h @@ -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 diff --git a/qmljs_objects.cpp b/qmljs_objects.cpp index 290a4f793c..c34af50561 100644 --- a/qmljs_objects.cpp +++ b/qmljs_objects.cpp @@ -42,6 +42,7 @@ #include "qmljs_objects.h" #include "qv4ir_p.h" +#include "qv4isel_p.h" #include "qv4ecmaobjects_p.h" #include @@ -50,7 +51,6 @@ #include #include #include -#include #include #include @@ -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(addr); - size_t roundAddr = iaddr & ~(pageSize - static_cast(1)); - int mode = PROT_READ | PROT_WRITE | PROT_EXEC; - return mprotect(reinterpret_cast(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); - } + EvalInstructionSelection *isel = factory->create(vm, &module, code); - if (! protect(code, codeSize)) - Q_UNREACHABLE(); - } + foreach (IR::Function *function, module.functions) + isel->run(function); + + if (! isel->finishModule(codeSize)) + Q_UNREACHABLE(); + + delete isel; } if (! globalCode) diff --git a/qmljs_objects.h b/qmljs_objects.h index a2b956f567..083c124e85 100644 --- a/qmljs_objects.h +++ b/qmljs_objects.h @@ -46,6 +46,7 @@ #include "qmljs_environment.h" #include "qv4array_p.h" #include "qv4codegen_p.h" +#include "qv4isel_p.h" #include #include @@ -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 diff --git a/qmljs_value.h b/qmljs_value.h index b504af0889..e15b970257 100644 --- a/qmljs_value.h +++ b/qmljs_value.h @@ -50,7 +50,7 @@ namespace QQmlJS { namespace VM { -struct Array; +class Array; struct String; struct Object; struct BooleanObject; diff --git a/qv4isel_llvm_p.h b/qv4isel_llvm_p.h index 2142ea42c2..3f51a21b3f 100644 --- a/qv4isel_llvm_p.h +++ b/qv4isel_llvm_p.h @@ -54,6 +54,7 @@ # pragma clang diagnostic pop #endif // __clang__ +#include "qv4isel_p.h" #include "qv4ir_p.h" namespace QQmlJS { diff --git a/qv4isel_masm.cpp b/qv4isel_masm.cpp index 8a8e663e93..6290dd5990 100644 --- a/qv4isel_masm.cpp +++ b/qv4isel_masm.cpp @@ -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(addr); + size_t roundAddr = iaddr & ~(pageSize - static_cast(1)); + int mode = PROT_READ | PROT_WRITE | PROT_EXEC; + return mprotect(reinterpret_cast(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); diff --git a/qv4isel_masm_p.h b/qv4isel_masm_p.h index 82bc471be5..aaaf2e0f9a 100644 --- a/qv4isel_masm_p.h +++ b/qv4isel_masm_p.h @@ -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" @@ -54,14 +55,18 @@ 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 _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 index 0000000000..effd44fcf3 --- /dev/null +++ b/qv4isel_p.h @@ -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 + +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 9358bf489e..1a6e2a41ab 100644 --- 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 {