aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: ed4985feac5ecfae6a421bf4a7cdb41310278bbe (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

#include "qmljs_objects.h"
#include "qv4codegen_p.h"
#include "qv4isel_x86_64_p.h"
#include "qv4isel_moth_p.h"
#include "qv4vme_moth_p.h"
#include "qv4syntaxchecker_p.h"
#include "qv4ecmaobjects_p.h"

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

#include <sys/mman.h>
#include <iostream>

#ifndef QMLJS_NO_LLVM
#  include "qv4isel_llvm_p.h"

#  include <llvm/PassManager.h>
#  include <llvm/Analysis/Passes.h>
#  include <llvm/Transforms/Scalar.h>
#  include <llvm/Transforms/IPO.h>
#  include <llvm/Assembly/PrintModulePass.h>
#  include <llvm/Support/raw_ostream.h>
#  include <llvm/Support/FormattedStream.h>
#  include <llvm/Support/Host.h>
#  include <llvm/Support/TargetRegistry.h>
#  include <llvm/Support/TargetSelect.h>
#  include <llvm/Target/TargetMachine.h>
#  include <llvm/Target/TargetData.h>
#endif

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

namespace builtins {

using namespace QQmlJS::VM;

struct Print: FunctionObject
{
    Print(Context *scope): FunctionObject(scope) {}

    virtual void call(Context *ctx)
    {
        for (unsigned int i = 0; i < ctx->argumentCount; ++i) {
            String *s = ctx->argument(i).toString(ctx);
            if (i)
                std::cout << ' ';
            std::cout << qPrintable(s->toQString());
        }
        std::cout << std::endl;
    }
};

} // builtins

#ifndef QMLJS_NO_LLVM
void compile(const QString &fileName, const QString &source)
{
    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) {
        using namespace AST;
        Program *program = AST::cast<Program *>(parser.rootNode());

        Codegen cg;
        /*IR::Function *globalCode =*/ cg(program, &module);

        LLVMInstructionSelection llvmIsel(llvm::getGlobalContext());
        if (llvm::Module *llvmModule = llvmIsel.getLLVMModule(&module)) {
            llvm::PassManager PM;

            const std::string triple = llvm::sys::getDefaultTargetTriple();

            LLVMInitializeX86TargetInfo();
            LLVMInitializeX86Target();
            LLVMInitializeX86AsmPrinter();
            LLVMInitializeX86AsmParser();
            LLVMInitializeX86Disassembler();
            LLVMInitializeX86TargetMC();

            std::string err;
            const llvm::Target *target = llvm::TargetRegistry::lookupTarget(triple, err);
            if (! err.empty()) {
                std::cerr << err << ", triple: " << triple << std::endl;
                assert(!"cannot create target for the host triple");
            }


            std::string cpu;
            std::string features;
            llvm::TargetOptions options;
            llvm::TargetMachine *targetMachine = target->createTargetMachine(triple, cpu, features, options, llvm::Reloc::PIC_);
            assert(targetMachine);

            llvm::formatted_raw_ostream out(llvm::outs());
            PM.add(llvm::createScalarReplAggregatesPass());
            PM.add(llvm::createInstructionCombiningPass());
            PM.add(llvm::createGlobalOptimizerPass());
            PM.add(llvm::createFunctionInliningPass(25));
            targetMachine->addPassesToEmitFile(PM, out, llvm::TargetMachine::CGFT_AssemblyFile);
            PM.run(*llvmModule);
            delete llvmModule;
        }
    }
}

int compileFiles(const QStringList &files)
{
    foreach (const QString &fileName, files) {
        QFile file(fileName);
        if (file.open(QFile::ReadOnly)) {
            QString source = QString::fromUtf8(file.readAll());
            compile(fileName, source);
        }
    }
    return 0;
}

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("_25_entry");
        void (*code)(VM::Context *) = (void (*)(VM::Context *)) ptr;

        VM::ExecutionEngine vm;
        VM::Context *ctx = vm.rootContext;

        QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue;
        globalObject->setProperty(ctx, vm.identifier(QStringLiteral("print")),
                                  QQmlJS::VM::Value::fromObject(new builtins::Print(ctx)));

        code(ctx);

        if (ctx->hasUncaughtException) {
            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 0;
}

#endif

void evaluate(QQmlJS::VM::ExecutionEngine *vm, const QString &fileName, const QString &source)
{
    using namespace QQmlJS;

    IR::Module module;
    IR::Function *globalCode = 0;

    const size_t codeSize = 400 * getpagesize();
    uchar *code = (uchar *) malloc(codeSize);
    assert(! (size_t(code) & 15));

    static bool useMoth = !qgetenv("USE_MOTH").isNull();

    {
        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) {
            using namespace AST;
            Program *program = AST::cast<Program *>(parser.rootNode());

            Codegen cg;
            globalCode = cg(program, &module);

            if (useMoth) {
                Moth::InstructionSelection isel(vm, &module, code);
                foreach (IR::Function *function, module.functions)
                    isel(function);
            } else {
                x86_64::InstructionSelection isel(vm, &module, code);
                foreach (IR::Function *function, module.functions)
                    isel(function);
            }
        }

        if (! globalCode)
            return;
    }

    if (!useMoth) {
        if (! protect(code, codeSize))
            Q_UNREACHABLE();
    }

    VM::Context *ctx = vm->rootContext;

    ctx->varCount = globalCode->locals.size();
    if (ctx->varCount) {
        ctx->locals = new VM::Value[ctx->varCount];
        ctx->vars = new VM::String*[ctx->varCount];
        std::fill(ctx->locals, ctx->locals + ctx->varCount, VM::Value::undefinedValue());
        for (unsigned int i = 0; i < ctx->varCount; ++i)
            ctx->vars[i] = ctx->engine->identifier(*globalCode->locals.at(i));
    }

    if (useMoth) {
        Moth::VME vme;
        vme(ctx, code);
    } else {
        globalCode->code(ctx);
    }

    if (ctx->hasUncaughtException) {
        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;
    }

    delete[] ctx->locals;
    delete[] ctx->vars;
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QStringList args = app.arguments();
    args.removeFirst();

#ifndef QMLJS_NO_LLVM
    if (args.isEmpty()) {
        std::cerr << "Usage: v4 [--compile|--aot] file..." << std::endl;
        return 0;
    }

    if (args.first() == QLatin1String("--compile")) {
        args.removeFirst();
        return compileFiles(args);
    } else if (args.first() == QLatin1String("--aot")) {
        args.removeFirst();
        return evaluateCompiledCode(args);
    }
#endif

    foreach (const QString &fn, args) {
        QFile file(fn);
        if (file.open(QFile::ReadOnly)) {
            const QString code = QString::fromUtf8(file.readAll());
            file.close();
            QQmlJS::VM::ExecutionEngine vm;
            QQmlJS::VM::Context *ctx = vm.rootContext;

            QQmlJS::VM::Object *globalObject = vm.globalObject.objectValue;
            globalObject->setProperty(ctx, vm.identifier(QStringLiteral("print")),
                                      QQmlJS::VM::Value::fromObject(new builtins::Print(ctx)));

            evaluate(&vm, fn, code);
        }
    }
}