qtdeclarative/main.cpp

285 lines
8.8 KiB
C++
Raw Normal View History

2012-04-16 19:23:25 +00:00
#include "qmljs_objects.h"
#include "qv4codegen_p.h"
#include "qv4isel_x86_64_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-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>
#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>
2012-06-06 08:36:59 +00:00
# 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
2012-05-07 12:46:37 +00:00
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;
}
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-05-13 11:50:55 +00:00
Print(Context *scope): FunctionObject(scope) {}
2012-05-07 14:05:05 +00:00
virtual void call(Context *ctx)
{
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-05-07 14:05:05 +00:00
} // builtins
#ifndef QMLJS_NO_LLVM
2012-06-05 16:32:52 +00:00
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;
2012-06-06 08:36:59 +00:00
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());
2012-06-06 09:06:00 +00:00
PM.add(llvm::createFunctionInliningPass(25));
2012-06-06 08:36:59 +00:00
targetMachine->addPassesToEmitFile(PM, out, llvm::TargetMachine::CGFT_AssemblyFile);
PM.run(*llvmModule);
delete llvmModule;
}
}
}
2012-06-05 16:32:52 +00:00
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) {
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("_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
2012-05-08 11:21:52 +00:00
void evaluate(QQmlJS::VM::ExecutionEngine *vm, const QString &fileName, const QString &source)
2012-05-08 11:21:52 +00:00
{
using namespace QQmlJS;
IR::Module module;
IR::Function *globalCode = 0;
2012-05-08 11:21:52 +00:00
const size_t codeSize = 400 * getpagesize();
uchar *code = (uchar *) malloc(codeSize);
assert(! (size_t(code) & 15));
2012-05-08 11:21:52 +00:00
2012-06-08 19:09:13 +00:00
static bool useMoth = !qgetenv("USE_MOTH").isNull();
{
QQmlJS::Engine ee, *engine = &ee;
Lexer lexer(engine);
lexer.setCode(source, 1, false);
Parser parser(engine);
2012-05-08 11:21:52 +00:00
const bool parsed = parser.parseProgram();
2012-05-08 11:21:52 +00:00
foreach (const DiagnosticMessage &m, parser.diagnosticMessages()) {
std::cerr << qPrintable(fileName) << ':' << m.loc.startLine << ':' << m.loc.startColumn
<< ": error: " << qPrintable(m.message) << std::endl;
}
2012-05-08 11:21:52 +00:00
if (parsed) {
using namespace AST;
Program *program = AST::cast<Program *>(parser.rootNode());
2012-05-08 11:21:52 +00:00
Codegen cg;
globalCode = cg(program, &module);
2012-06-08 19:09:13 +00:00
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);
2012-06-12 17:13:55 +00:00
if (! protect(code, codeSize))
Q_UNREACHABLE();
}
2012-05-08 11:21:52 +00:00
}
if (! globalCode)
return;
}
2012-05-08 11:21:52 +00:00
VM::Context *ctx = vm->rootContext;
ctx->hasUncaughtException = false;
2012-05-16 17:00:31 +00:00
2012-06-11 16:58:52 +00:00
if (useMoth) {
Moth::VME vme;
vme(ctx, code);
} else {
2012-06-12 17:13:55 +00:00
globalCode->code(ctx, globalCode->codeData);
2012-06-11 16:58:52 +00:00
}
2012-05-16 17:00:31 +00:00
2012-05-25 09:55:50 +00:00
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;
} else if (! ctx->result.isUndefined()) {
if (! qgetenv("SHOW_EXIT_VALUE").isNull())
std::cout << "exit value: " << qPrintable(ctx->result.toString(ctx)->toQString()) << std::endl;
2012-05-25 09:55:50 +00:00
}
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-06-05 16:32:52 +00:00
#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
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)));
2012-04-16 19:23:25 +00:00
foreach (const QString &fn, args) {
QFile file(fn);
if (file.open(QFile::ReadOnly)) {
const QString code = QString::fromUtf8(file.readAll());
file.close();
2012-06-05 16:32:52 +00:00
evaluate(&vm, fn, code);
2012-04-16 19:23:25 +00:00
}
}
}