aboutsummaryrefslogtreecommitdiffstats
path: root/tools/v4/main.cpp
blob: e61931cea4cdeea7c0114e169156d2c86ba55c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/****************************************************************************
**
** 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$
**
****************************************************************************/

#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"

#include <QtCore>
#include <private/qqmljsengine_p.h>
#include <private/qqmljslexer_p.h>
#include <private/qqmljsparser_p.h>
#include <private/qqmljsast_p.h>

#include <iostream>

namespace builtins {

using namespace QQmlJS::VM;

struct Print: FunctionObject
{
    Print(ExecutionContext *scope): FunctionObject(scope) {
        vtbl = &static_vtbl;
        name = scope->engine->newString("print");
    }

    static Value call(Managed *, ExecutionContext *ctx, const Value &, Value *args, int argc)
    {
        for (int i = 0; i < argc; ++i) {
            String *s = args[i].toString(ctx);
            if (i)
                std::cout << ' ';
            std::cout << qPrintable(s->toQString());
        }
        std::cout << std::endl;
        return Value::undefinedValue();
    }

    static const ManagedVTable static_vtbl;
};

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);

} // 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, source, program, &module);

    int (*exec)(void *) = outputType == LLVMOutputJit ? executeLLVMCode : 0;
    return compileWithLLVM(&module, fileName, outputType, exec);
}

int compileFiles(const QStringList &files, QQmlJS::LLVMOutputType outputType)
{
    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;
        }
    }
    return EXIT_SUCCESS;
}

int evaluateCompiledCode(const QStringList &files)
{
    using namespace QQmlJS;

    foreach (const QString &libName, files) {
        QFileInfo libInfo(libName);
        QLibrary lib(libInfo.absoluteFilePath());
        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;
    }

    return EXIT_SUCCESS;
}
#endif // QMLJS_WITH_LLVM


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;
        }
    }

    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:
        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;

        QQmlJS::VM::Object *globalObject = vm.globalObject;
        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();

                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);
                    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;
            }
        }

        vm.memoryManager->dumpStats();
    } return EXIT_SUCCESS;
    } // switch (mode)
}