2012-10-12 08:12:24 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the V4VM module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
2012-04-16 19:23:25 +00:00
|
|
|
|
2012-10-12 11:45:35 +00:00
|
|
|
#ifndef QMLJS_NO_LLVM
|
2012-10-18 09:07:10 +00:00
|
|
|
# include "qv4_llvm_p.h"
|
2012-10-12 11:45:35 +00:00
|
|
|
#endif
|
|
|
|
|
2012-04-16 19:23:25 +00:00
|
|
|
#include "qmljs_objects.h"
|
2012-10-21 22:18:31 +00:00
|
|
|
#include "qmljs_runtime.h"
|
2012-04-16 19:23:25 +00:00
|
|
|
#include "qv4codegen_p.h"
|
2012-09-23 08:28:13 +00:00
|
|
|
#include "qv4isel_masm_p.h"
|
2012-06-08 19:09:13 +00:00
|
|
|
#include "qv4isel_moth_p.h"
|
2012-06-11 16:58:52 +00:00
|
|
|
#include "qv4vme_moth_p.h"
|
2012-05-08 11:21:52 +00:00
|
|
|
#include "qv4syntaxchecker_p.h"
|
2012-05-14 14:03:10 +00:00
|
|
|
#include "qv4ecmaobjects_p.h"
|
2012-11-19 12:15:25 +00:00
|
|
|
#include "qv4isel_p.h"
|
2012-04-16 19:23:25 +00:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <private/qqmljsengine_p.h>
|
|
|
|
#include <private/qqmljslexer_p.h>
|
|
|
|
#include <private/qqmljsparser_p.h>
|
|
|
|
#include <private/qqmljsast_p.h>
|
|
|
|
|
2012-05-07 12:46:37 +00:00
|
|
|
#include <sys/mman.h>
|
2012-04-16 19:23:25 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
namespace builtins {
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
using namespace QQmlJS::VM;
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
struct Print: FunctionObject
|
|
|
|
{
|
2012-11-16 22:07:10 +00:00
|
|
|
Print(ExecutionContext *scope): FunctionObject(scope) {}
|
2012-05-13 11:50:55 +00:00
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
virtual void call(ExecutionContext *ctx)
|
2012-05-07 14:05:05 +00:00
|
|
|
{
|
2012-11-18 22:33:06 +00:00
|
|
|
for (unsigned int i = 0; i < ctx->argumentCount(); ++i) {
|
2012-05-18 13:28:59 +00:00
|
|
|
String *s = ctx->argument(i).toString(ctx);
|
2012-05-07 14:05:05 +00:00
|
|
|
if (i)
|
|
|
|
std::cout << ' ';
|
2012-05-18 13:28:59 +00:00
|
|
|
std::cout << qPrintable(s->toQString());
|
2012-05-07 14:05:05 +00:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
};
|
2012-05-09 09:04:57 +00:00
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
struct TestHarnessError: FunctionObject
|
|
|
|
{
|
2012-11-16 22:07:10 +00:00
|
|
|
TestHarnessError(ExecutionContext *scope, bool &errorInTestHarness): FunctionObject(scope), errorOccurred(errorInTestHarness) {}
|
2012-11-08 11:45:40 +00:00
|
|
|
|
2012-11-16 22:07:10 +00:00
|
|
|
virtual void call(ExecutionContext *ctx)
|
2012-11-08 11:45:40 +00:00
|
|
|
{
|
|
|
|
errorOccurred = true;
|
|
|
|
|
2012-11-18 22:33:06 +00:00
|
|
|
for (unsigned int i = 0; i < ctx->argumentCount(); ++i) {
|
2012-11-08 11:45:40 +00:00
|
|
|
String *s = ctx->argument(i).toString(ctx);
|
|
|
|
if (i)
|
|
|
|
std::cerr << ' ';
|
|
|
|
std::cerr << qPrintable(s->toQString());
|
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool &errorOccurred;
|
2012-06-26 13:36:03 +00:00
|
|
|
};
|
|
|
|
|
2012-05-07 14:05:05 +00:00
|
|
|
} // builtins
|
|
|
|
|
2012-06-05 10:15:41 +00:00
|
|
|
#ifndef QMLJS_NO_LLVM
|
2012-11-12 11:22:22 +00:00
|
|
|
int executeLLVMCode(void *codePtr)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS;
|
|
|
|
|
|
|
|
if (!codePtr)
|
|
|
|
return EXIT_FAILURE;
|
2012-11-19 12:15:25 +00:00
|
|
|
void (*code)(VM::ExecutionContext *) = (void (*)(VM::ExecutionContext *)) codePtr;
|
2012-11-12 11:22:22 +00:00
|
|
|
|
2012-11-19 12:15:25 +00:00
|
|
|
QScopedPointer<QQmlJS::EValISelFactory> iSelFactory(new QQmlJS::Moth::ISelFactory);
|
|
|
|
VM::ExecutionEngine vm(iSelFactory.data());
|
|
|
|
VM::ExecutionContext *ctx = vm.rootContext;
|
2012-11-12 11:22:22 +00:00
|
|
|
|
|
|
|
QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue();
|
|
|
|
globalObject->__put__(ctx, vm.identifier(QStringLiteral("print")),
|
|
|
|
QQmlJS::VM::Value::fromObject(new builtins::Print(ctx)));
|
|
|
|
|
|
|
|
void * buf = __qmljs_create_exception_handler(ctx);
|
|
|
|
if (setjmp(*(jmp_buf *)buf)) {
|
|
|
|
if (VM::ErrorObject *e = ctx->result.asErrorObject())
|
|
|
|
std::cerr << "Uncaught exception: " << qPrintable(e->value.toString(ctx)->toQString()) << std::endl;
|
|
|
|
else
|
|
|
|
std::cerr << "Uncaught exception: " << qPrintable(ctx->result.toString(ctx)->toQString()) << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
code(ctx);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2012-11-13 08:54:24 +00:00
|
|
|
int compile(const QString &fileName, const QString &source, QQmlJS::LLVMOutputType outputType)
|
2012-06-05 10:15:41 +00:00
|
|
|
{
|
|
|
|
using namespace QQmlJS;
|
|
|
|
|
|
|
|
IR::Module module;
|
|
|
|
QQmlJS::Engine ee, *engine = ⅇ
|
|
|
|
Lexer lexer(engine);
|
|
|
|
lexer.setCode(source, 1, false);
|
|
|
|
Parser parser(engine);
|
|
|
|
|
|
|
|
const bool parsed = parser.parseProgram();
|
|
|
|
|
|
|
|
foreach (const DiagnosticMessage &m, parser.diagnosticMessages()) {
|
|
|
|
std::cerr << qPrintable(fileName) << ':' << m.loc.startLine << ':' << m.loc.startColumn
|
|
|
|
<< ": error: " << qPrintable(m.message) << std::endl;
|
|
|
|
}
|
|
|
|
|
2012-10-18 09:07:10 +00:00
|
|
|
if (!parsed)
|
2012-11-12 11:22:22 +00:00
|
|
|
return EXIT_FAILURE;
|
2012-10-18 09:07:10 +00:00
|
|
|
|
|
|
|
using namespace AST;
|
|
|
|
Program *program = AST::cast<Program *>(parser.rootNode());
|
|
|
|
|
|
|
|
Codegen cg;
|
|
|
|
/*IR::Function *globalCode =*/ cg(program, &module);
|
|
|
|
|
2012-11-13 08:54:24 +00:00
|
|
|
int (*exec)(void *) = outputType == LLVMOutputJit ? executeLLVMCode : 0;
|
|
|
|
return compileWithLLVM(&module, fileName, outputType, exec);
|
2012-06-05 10:15:41 +00:00
|
|
|
}
|
2012-06-05 16:32:52 +00:00
|
|
|
|
2012-11-13 08:54:24 +00:00
|
|
|
int compileFiles(const QStringList &files, QQmlJS::LLVMOutputType outputType)
|
2012-06-05 16:32:52 +00:00
|
|
|
{
|
|
|
|
foreach (const QString &fileName, files) {
|
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QString source = QString::fromUtf8(file.readAll());
|
2012-11-13 08:54:24 +00:00
|
|
|
int result = compile(fileName, source, outputType);
|
2012-11-12 11:22:22 +00:00
|
|
|
if (result != EXIT_SUCCESS)
|
|
|
|
return result;
|
2012-11-13 12:44:16 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Error: cannot open file " << fileName.toUtf8().constData() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2012-06-05 16:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-08 11:45:40 +00:00
|
|
|
return EXIT_SUCCESS;
|
2012-06-05 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int evaluateCompiledCode(const QStringList &files)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS;
|
|
|
|
|
|
|
|
foreach (const QString &libName, files) {
|
2012-06-06 16:02:34 +00:00
|
|
|
QFileInfo libInfo(libName);
|
|
|
|
QLibrary lib(libInfo.absoluteFilePath());
|
2012-06-05 16:32:52 +00:00
|
|
|
lib.load();
|
2012-10-19 13:03:28 +00:00
|
|
|
QFunctionPointer ptr = lib.resolve("%entry");
|
|
|
|
// qDebug("_%%entry resolved to address %p", ptr);
|
2012-11-12 11:22:22 +00:00
|
|
|
int result = executeLLVMCode((void *) ptr);
|
|
|
|
if (result != EXIT_SUCCESS)
|
|
|
|
return result;
|
2012-06-05 16:32:52 +00:00
|
|
|
}
|
2012-10-24 08:09:30 +00:00
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
return EXIT_SUCCESS;
|
2012-06-05 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 10:15:41 +00:00
|
|
|
#endif
|
2012-05-08 11:21:52 +00:00
|
|
|
|
|
|
|
|
2012-04-16 19:23:25 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
QStringList args = app.arguments();
|
|
|
|
args.removeFirst();
|
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
enum {
|
|
|
|
use_masm,
|
|
|
|
use_moth,
|
|
|
|
use_llvm_compiler,
|
2012-11-12 11:22:22 +00:00
|
|
|
use_llvm_runtime,
|
|
|
|
use_llvm_jit
|
2012-11-08 11:45:40 +00:00
|
|
|
} mode = use_masm;
|
|
|
|
|
2012-11-13 08:54:24 +00:00
|
|
|
#ifndef QMLJS_NO_LLVM
|
|
|
|
QQmlJS::LLVMOutputType fileType = QQmlJS::LLVMOutputObject;
|
|
|
|
#endif // QMLJS_NO_LLVM
|
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
if (!args.isEmpty()) {
|
|
|
|
if (args.first() == QLatin1String("--jit")) {
|
|
|
|
mode = use_masm;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.first() == QLatin1String("--interpret")) {
|
|
|
|
mode = use_moth;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
|
|
|
|
2012-06-05 16:32:52 +00:00
|
|
|
#ifndef QMLJS_NO_LLVM
|
2012-11-08 11:45:40 +00:00
|
|
|
if (args.first() == QLatin1String("--compile")) {
|
|
|
|
mode = use_llvm_compiler;
|
|
|
|
args.removeFirst();
|
2012-11-13 08:54:24 +00:00
|
|
|
|
|
|
|
if (!args.isEmpty() && args.first() == QLatin1String("-t")) {
|
|
|
|
args.removeFirst();
|
|
|
|
// Note: keep this list in sync with the enum!
|
|
|
|
static QStringList fileTypes = QStringList() << QLatin1String("ll") << QLatin1String("bc") << QLatin1String("asm") << QLatin1String("obj");
|
|
|
|
if (args.isEmpty() || !fileTypes.contains(args.first())) {
|
|
|
|
std::cerr << "file types: ll, bc, asm, obj" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
fileType = (QQmlJS::LLVMOutputType) fileTypes.indexOf(args.first());
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
2012-11-08 11:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args.first() == QLatin1String("--aot")) {
|
|
|
|
mode = use_llvm_runtime;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
2012-11-12 11:22:22 +00:00
|
|
|
|
|
|
|
if (args.first() == QLatin1String("--llvm-jit")) {
|
|
|
|
mode = use_llvm_jit;
|
|
|
|
args.removeFirst();
|
|
|
|
}
|
2012-11-08 11:45:40 +00:00
|
|
|
#endif // QMLJS_NO_LLVM
|
|
|
|
if (args.first() == QLatin1String("--help")) {
|
2012-11-12 11:22:22 +00:00
|
|
|
std::cerr << "Usage: v4 [|--jit|--interpret|--compile|--aot|--llvm-jit] file..." << std::endl;
|
2012-11-08 11:45:40 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-06-05 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
switch (mode) {
|
|
|
|
#ifdef QMLJS_NO_LLVM
|
|
|
|
case use_llvm_compiler:
|
|
|
|
case use_llvm_runtime:
|
2012-11-12 11:22:22 +00:00
|
|
|
case use_llvm_jit:
|
2012-11-08 11:45:40 +00:00
|
|
|
std::cerr << "LLVM backend was not built, compiler is unavailable." << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
#else // QMLJS_NO_LLVM
|
2012-11-12 11:22:22 +00:00
|
|
|
case use_llvm_jit:
|
2012-11-13 08:54:24 +00:00
|
|
|
return compileFiles(args, QQmlJS::LLVMOutputJit);
|
2012-11-08 11:45:40 +00:00
|
|
|
case use_llvm_compiler:
|
2012-11-13 08:54:24 +00:00
|
|
|
return compileFiles(args, fileType);
|
2012-11-08 11:45:40 +00:00
|
|
|
case use_llvm_runtime:
|
2012-06-05 16:32:52 +00:00
|
|
|
return evaluateCompiledCode(args);
|
2012-11-08 11:45:40 +00:00
|
|
|
#endif // QMLJS_NO_LLVM
|
|
|
|
case use_masm:
|
|
|
|
case use_moth: {
|
2012-11-19 12:15:25 +00:00
|
|
|
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());
|
2012-11-16 22:07:10 +00:00
|
|
|
QQmlJS::VM::ExecutionContext *ctx = vm.rootContext;
|
2012-06-05 16:32:52 +00:00
|
|
|
|
2012-11-08 11:45:40 +00:00
|
|
|
QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue();
|
|
|
|
globalObject->__put__(ctx, vm.identifier(QStringLiteral("print")),
|
|
|
|
QQmlJS::VM::Value::fromObject(new builtins::Print(ctx)));
|
|
|
|
|
|
|
|
bool errorInTestHarness = false;
|
|
|
|
if (!qgetenv("IN_TEST_HARNESS").isEmpty())
|
|
|
|
globalObject->__put__(ctx, vm.identifier(QStringLiteral("$ERROR")),
|
|
|
|
QQmlJS::VM::Value::fromObject(new builtins::TestHarnessError(ctx, errorInTestHarness)));
|
|
|
|
|
|
|
|
foreach (const QString &fn, args) {
|
|
|
|
QFile file(fn);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
const QString code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
|
|
|
|
2012-11-19 12:15:25 +00:00
|
|
|
int exitCode = QQmlJS::VM::EvalFunction::evaluate(vm.rootContext, fn, code, iSelFactory.data(), QQmlJS::Codegen::GlobalCode);
|
2012-11-08 11:45:40 +00:00
|
|
|
if (exitCode != EXIT_SUCCESS)
|
|
|
|
return exitCode;
|
|
|
|
if (errorInTestHarness)
|
|
|
|
return EXIT_FAILURE;
|
2012-11-13 12:44:16 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Error: cannot open file " << fn.toUtf8().constData() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2012-11-08 11:45:40 +00:00
|
|
|
}
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
2012-11-08 11:45:40 +00:00
|
|
|
} return EXIT_SUCCESS;
|
2012-04-16 19:23:25 +00:00
|
|
|
}
|
|
|
|
}
|