A factory is now passed along to do the codegen for eval().
Change-Id: If15b1f28c9c0a8f8b6d18b56d6e7bc5d942927e5
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
#include "qv4vme_moth_p.h"
#include "qv4syntaxchecker_p.h"
#include "qv4ecmaobjects_p.h"
+#include "qv4isel_p.h"
#include <QtCore>
#include <private/qqmljsengine_p.h>
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")),
#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();
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)
#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"
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 *);
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)
{
namespace QQmlJS {
namespace VM {
-ExecutionEngine::ExecutionEngine()
+ExecutionEngine::ExecutionEngine(EValISelFactory *factory)
{
rootContext = newContext();
rootContext->init(this);
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]
#ifndef QMLJS_ENGINE_H
#define QMLJS_ENGINE_H
+#include <qv4isel_p.h>
#include <qmljs_objects.h>
#include <qmljs_environment.h>
#include <setjmp.h>
namespace VM {
struct Value;
-struct Array;
+class Array;
struct Object;
struct BooleanObject;
struct NumberObject;
QVector<ExceptionHandler> unwindStack;
- ExecutionEngine();
+ ExecutionEngine(EValISelFactory *factory);
ExecutionContext *newContext();
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) {
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))
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
#include "qmljs_objects.h"
#include "qv4ir_p.h"
+#include "qv4isel_p.h"
#include "qv4ecmaobjects_p.h"
#include <private/qqmljsengine_p.h>
#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>
}
-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()) {
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]
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;
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)
#include "qmljs_environment.h"
#include "qv4array_p.h"
#include "qv4codegen_p.h"
+#include "qv4isel_p.h"
#include <QtCore/QString>
#include <QtCore/QHash>
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
namespace QQmlJS {
namespace VM {
-struct Array;
+class Array;
struct String;
struct Object;
struct BooleanObject;
# pragma clang diagnostic pop
#endif // __clang__
+#include "qv4isel_p.h"
#include "qv4ir_p.h"
namespace QQmlJS {
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);
#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)
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
--- /dev/null
+/****************************************************************************
+**
+** 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
qv4ecmaobjects.cpp \
qv4array.cpp \
qv4isel_masm.cpp \
- llvm_runtime.cpp
+ llvm_runtime.cpp \
+ qv4isel_p.cpp
HEADERS += \
qv4codegen_p.h \
qv4ecmaobjects_p.h \
qv4array_p.h \
qv4isel_masm_p.h \
+ qv4isel_p.h \
qv4isel_util_p.h
llvm {