qtdeclarative/tools/v4/main.cpp

403 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
**
** 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
#ifdef QMLJS_WITH_LLVM
# include "private/qv4_llvm_p.h"
#endif // QMLJS_WITH_LLVM
#include "private/debugging.h"
#include "private/qv4object.h"
#include "private/qv4runtime.h"
#include "private/qv4functionobject.h"
#include "private/qv4errorobject.h"
#include "private/qv4globalobject.h"
#include "private/qv4codegen_p.h"
#include "private/qv4isel_masm_p.h"
#include "private/qv4isel_moth_p.h"
#include "private/qv4vme_moth_p.h"
#include "private/qv4syntaxchecker_p.h"
#include "private/qv4objectproto.h"
#include "private/qv4isel_p.h"
#include "private/qv4mm.h"
#include "private/qv4context.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>
#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
struct Print: FunctionObject
2012-05-07 14:05:05 +00:00
{
Print(ExecutionContext *scope): FunctionObject(scope) {
vtbl = &static_vtbl;
name = scope->engine->newString("print");
}
2012-05-13 11:50:55 +00:00
static Value call(Managed *, ExecutionContext *ctx, const Value &, Value *args, int argc)
2012-05-07 14:05:05 +00:00
{
for (int i = 0; i < argc; ++i) {
String *s = args[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;
return Value::undefinedValue();
2012-05-07 14:05:05 +00:00
}
static const ManagedVTable static_vtbl;
2012-05-07 14:05:05 +00:00
};
2012-05-09 09:04:57 +00:00
DEFINE_MANAGED_VTABLE(Print);
struct GC: public FunctionObject
{
GC(ExecutionContext* scope)
: FunctionObject(scope)
{
vtbl = &static_vtbl;
name = scope->engine->newString("gc");
}
static Value call(Managed *, ExecutionContext *ctx, const Value &, Value *, int)
{
ctx->engine->memoryManager->runGC();
return Value::undefinedValue();
}
static const ManagedVTable static_vtbl;
};
DEFINE_MANAGED_VTABLE(GC);
2012-05-07 14:05:05 +00:00
} // builtins
static void showException(QQmlJS::VM::ExecutionContext *ctx, const QQmlJS::VM::Value &exception)
{
QQmlJS::VM::ErrorObject *e = exception.asErrorObject();
if (!e) {
std::cerr << "Uncaught exception: " << qPrintable(exception.toString(ctx)->toQString()) << std::endl;
return;
}
if (QQmlJS::VM::SyntaxErrorObject *err = e->asSyntaxError()) {
QQmlJS::VM::DiagnosticMessage *msg = err->message();
if (!msg) {
std::cerr << "Uncaught exception: Syntax error" << std::endl;
return;
}
for (; msg; msg = msg->next) {
std::cerr << qPrintable(msg->buildFullMessage(ctx)->toQString()) << std::endl;
}
} else {
std::cerr << "Uncaught exception: " << qPrintable(e->get(ctx, ctx->engine->newString(QStringLiteral("message")), 0).toString(ctx)->toQString()) << std::endl;
}
}
#ifdef QMLJS_WITH_LLVM
int executeLLVMCode(void *codePtr)
{
using namespace QQmlJS;
if (!codePtr)
return EXIT_FAILURE;
void (*code)(VM::ExecutionContext *) = (void (*)(VM::ExecutionContext *)) codePtr;
QScopedPointer<QQmlJS::EvalISelFactory> iSelFactory(new QQmlJS::Moth::ISelFactory);
VM::ExecutionEngine vm(iSelFactory.data());
VM::ExecutionContext *ctx = vm.rootContext;
#if THIS_NEEDS_TO_BE_FIXED
QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue();
globalObject->__put__(ctx, vm.newIdentifier(QStringLiteral("print")),
QQmlJS::VM::Value::fromObject(new (ctx->engine->memoryManager) builtins::Print(ctx)));
void * buf = __qmljs_create_exception_handler(ctx);
if (setjmp(*(jmp_buf *)buf)) {
showException(ctx);
return EXIT_FAILURE;
}
code(ctx);
#else
Q_UNUSED(ctx);
Q_UNUSED(code);
#endif
return EXIT_SUCCESS;
}
int compile(const QString &fileName, const QString &source, QQmlJS::LLVMOutputType outputType)
{
using namespace QQmlJS;
IR::Module module;
QQmlJS::Engine ee, *engine = &ee;
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;
}
if (!parsed)
return EXIT_FAILURE;
using namespace AST;
Program *program = AST::cast<Program *>(parser.rootNode());
class MyErrorHandler: public ErrorHandler {
public:
virtual void syntaxError(QQmlJS::VM::DiagnosticMessage *message) {
for (; message; message = message->next) {
std::cerr << qPrintable(message->fileName) << ':'
<< message->startLine << ':'
<< message->startColumn << ": "
<< (message->type == QQmlJS::VM::DiagnosticMessage::Error ? "error" : "warning") << ": "
<< qPrintable(message->message) << std::endl;
}
}
} errorHandler;
Codegen cg(&errorHandler, false);
// FIXME: if the program is empty, we should we generate an empty %entry, or give an error?
/*IR::Function *globalCode =*/ cg(fileName, program, &module);
int (*exec)(void *) = outputType == LLVMOutputJit ? executeLLVMCode : 0;
return compileWithLLVM(&module, fileName, outputType, exec);
}
2012-06-05 16:32:52 +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());
int result = compile(fileName, source, outputType);
if (result != EXIT_SUCCESS)
return result;
} else {
std::cerr << "Error: cannot open file " << fileName.toUtf8().constData() << std::endl;
return EXIT_FAILURE;
2012-06-05 16:32:52 +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();
QFunctionPointer ptr = lib.resolve("%entry");
// qDebug("_%%entry resolved to address %p", ptr);
int result = executeLLVMCode((void *) ptr);
if (result != EXIT_SUCCESS)
return result;
2012-06-05 16:32:52 +00:00
}
return EXIT_SUCCESS;
2012-06-05 16:32:52 +00:00
}
#endif // QMLJS_WITH_LLVM
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();
enum {
use_masm,
use_moth,
use_llvm_compiler,
use_llvm_runtime,
use_llvm_jit
} mode = use_masm;
#ifdef QMLJS_WITH_LLVM
QQmlJS::LLVMOutputType fileType = QQmlJS::LLVMOutputObject;
#endif // QMLJS_WITH_LLVM
bool enableDebugging = false;
if (!args.isEmpty()) {
if (args.first() == QLatin1String("-d") || args.first() == QLatin1String("--debug")) {
enableDebugging = true;
args.removeFirst();
}
}
if (!args.isEmpty()) {
if (args.first() == QLatin1String("--jit")) {
mode = use_masm;
args.removeFirst();
}
if (args.first() == QLatin1String("--interpret")) {
mode = use_moth;
args.removeFirst();
}
#ifdef QMLJS_WITH_LLVM
if (args.first() == QLatin1String("--compile")) {
mode = use_llvm_compiler;
args.removeFirst();
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();
}
}
if (args.first() == QLatin1String("--aot")) {
mode = use_llvm_runtime;
args.removeFirst();
}
if (args.first() == QLatin1String("--llvm-jit")) {
mode = use_llvm_jit;
args.removeFirst();
}
#endif // QMLJS_WITH_LLVM
if (args.first() == QLatin1String("--help")) {
std::cerr << "Usage: v4 [|--debug|-d] [|--jit|--interpret|--compile|--aot|--llvm-jit] file..." << std::endl;
return EXIT_SUCCESS;
}
2012-06-05 16:32:52 +00:00
}
switch (mode) {
#ifdef QMLJS_WITH_LLVM
case use_llvm_jit:
return compileFiles(args, QQmlJS::LLVMOutputJit);
case use_llvm_compiler:
return compileFiles(args, fileType);
case use_llvm_runtime:
2012-06-05 16:32:52 +00:00
return evaluateCompiledCode(args);
#else // !QMLJS_WITH_LLVM
case use_llvm_compiler:
case use_llvm_runtime:
case use_llvm_jit:
std::cerr << "LLVM backend was not built, compiler is unavailable." << std::endl;
return EXIT_FAILURE;
#endif // QMLJS_WITH_LLVM
case use_masm:
case use_moth: {
QQmlJS::EvalISelFactory* iSelFactory = 0;
if (mode == use_moth) {
iSelFactory = new QQmlJS::Moth::ISelFactory;
} else {
iSelFactory = new QQmlJS::MASM::ISelFactory;
}
QQmlJS::VM::ExecutionEngine vm(iSelFactory);
QScopedPointer<QQmlJS::Debugging::Debugger> debugger;
if (enableDebugging)
debugger.reset(new QQmlJS::Debugging::Debugger(&vm));
vm.debugger = debugger.data();
QQmlJS::VM::ExecutionContext *ctx = vm.rootContext;
2012-06-05 16:32:52 +00:00
QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue();
QQmlJS::VM::Object *print = new (ctx->engine->memoryManager) builtins::Print(ctx);
print->prototype = ctx->engine->objectPrototype;
globalObject->put(ctx, vm.newIdentifier(QStringLiteral("print")),
QQmlJS::VM::Value::fromObject(print));
QQmlJS::VM::Object *gc = new (ctx->engine->memoryManager) builtins::GC(ctx);
gc->prototype = ctx->engine->objectPrototype;
globalObject->put(ctx, vm.newIdentifier(QStringLiteral("gc")),
QQmlJS::VM::Value::fromObject(gc));
foreach (const QString &fn, args) {
QFile file(fn);
if (file.open(QFile::ReadOnly)) {
const QString code = QString::fromUtf8(file.readAll());
file.close();
Implement JavaScript exceptions using C++ exceptions Instead of registering catch handlers with setjmp and throwing JS exceptions with longjmp, they are now thrown and caught as C++ exceptions. This allows for tight interoperability between C++ and JS in the future and allows for clear semantics with regards to cleaning up memory in the engine when throwing exceptions. (destructors are guaranteed to be called, unlike with setjmp/longjmp). The recent unwind table additions allow for the exceptions to be thrown through JIT generated code. Catching the exception is done by re-using the existing IR semantics where the beginning of a try block is marked by registering an exception handler. Execution after the registration continues conditionally, based on the return value of builtin_create_exception_handler. A return value of is 0 the try block(s) are executed. If an exception is thrown during that time, execution resumes at the point where builtin_create_exception_handler returns, but with a return value of 1. If an exception is thrown within the catch handler, the execution resumes again at the same point, but the inCatch IR variable will guide execution straight to the finally block(s), which calls delete_exception_handler. In the JIT as well as the interpreter this is implemented by entering a C++ code section that contains a C++ try {} catch {} block, in which the calling function is called again and continues right at the next instruction (or the interpreter loop is recursively entered). An exception will throw us out of that scope and back into the try {} catch {} wrapper, which can call again into the calling function. The IR guarantees that delete_exception_handler is always called, regardless of how the try or catch blocks are terminated. That is where in the JIT and interpreter we return from the nested function call and return back into the original stack frame, effectively unregistering the catch handler. Further cleanups with regards to the naming and the exception handler stack will come in subsequent patches, this is merely the minimal patch set to change to the new mechanism. This patch set breaks ARM until ARM exception handler tables are implemented. The interpreter changes are based on a patchset from Erik from https://codereview.qt-project.org/#change,45750 Change-Id: I543f2bd37b2186f7e48ffcab177d57b5ce932a0c Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-03-01 16:04:21 +00:00
try {
QQmlJS::VM::Function *f = QQmlJS::VM::EvalFunction::parseSource(ctx, fn, code, QQmlJS::Codegen::GlobalCode,
/*strictMode =*/ false, /*inheritContext =*/ false);
if (!f)
continue;
QQmlJS::VM::Value result = vm.run(f);
Implement JavaScript exceptions using C++ exceptions Instead of registering catch handlers with setjmp and throwing JS exceptions with longjmp, they are now thrown and caught as C++ exceptions. This allows for tight interoperability between C++ and JS in the future and allows for clear semantics with regards to cleaning up memory in the engine when throwing exceptions. (destructors are guaranteed to be called, unlike with setjmp/longjmp). The recent unwind table additions allow for the exceptions to be thrown through JIT generated code. Catching the exception is done by re-using the existing IR semantics where the beginning of a try block is marked by registering an exception handler. Execution after the registration continues conditionally, based on the return value of builtin_create_exception_handler. A return value of is 0 the try block(s) are executed. If an exception is thrown during that time, execution resumes at the point where builtin_create_exception_handler returns, but with a return value of 1. If an exception is thrown within the catch handler, the execution resumes again at the same point, but the inCatch IR variable will guide execution straight to the finally block(s), which calls delete_exception_handler. In the JIT as well as the interpreter this is implemented by entering a C++ code section that contains a C++ try {} catch {} block, in which the calling function is called again and continues right at the next instruction (or the interpreter loop is recursively entered). An exception will throw us out of that scope and back into the try {} catch {} wrapper, which can call again into the calling function. The IR guarantees that delete_exception_handler is always called, regardless of how the try or catch blocks are terminated. That is where in the JIT and interpreter we return from the nested function call and return back into the original stack frame, effectively unregistering the catch handler. Further cleanups with regards to the naming and the exception handler stack will come in subsequent patches, this is merely the minimal patch set to change to the new mechanism. This patch set breaks ARM until ARM exception handler tables are implemented. The interpreter changes are based on a patchset from Erik from https://codereview.qt-project.org/#change,45750 Change-Id: I543f2bd37b2186f7e48ffcab177d57b5ce932a0c Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2013-03-01 16:04:21 +00:00
if (!result.isUndefined()) {
if (! qgetenv("SHOW_EXIT_VALUE").isEmpty())
std::cout << "exit value: " << qPrintable(result.toString(ctx)->toQString()) << std::endl;
}
} catch (QQmlJS::VM::Exception& ex) {
ex.accept(ctx);
showException(ctx, ex.value());
return EXIT_FAILURE;
}
} else {
std::cerr << "Error: cannot open file " << fn.toUtf8().constData() << std::endl;
return EXIT_FAILURE;
}
2012-04-16 19:23:25 +00:00
}
vm.memoryManager->dumpStats();
} return EXIT_SUCCESS;
} // switch (mode)
2012-04-16 19:23:25 +00:00
}