// Run the main "interpreter loop" now. MainLoop();
return0; }
都是些程式化的调用,需要注意TheJIT其实是自定义的KaleidoscopeJIT实例,而不直接来自LLVM API,想要了解细节应该深入到该类的内部,这是《Building a JIT in LLVM》中讨论的问题。
具体解析执行的代码如下:
staticvoidHandleTopLevelExpression(){ …… if (auto FnAST = ParseTopLevelExpr()) { if (FnAST->codegen()) { // 将包含顶级表达式的模块添加到JIT auto H = TheJIT->addModule(std::move(TheModule)); InitializeModuleAndPassManager();
// Search the JIT for the __anon_expr symbol. auto ExprSymbol = TheJIT->findSymbol("__anon_expr"); ……
// Get the symbol's address and cast it to the right type (takes no // arguments, returns a double) so we can call it as a native function. double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress()); fprintf(stderr, "Evaluated to %f\n", FP());
// Delete the anonymous expression module from the JIT. TheJIT->removeModule(H); } } …… }
// Initialize the target registry etc. InitializeAllTargetInfos(); InitializeAllTargets(); InitializeAllTargetMCs(); InitializeAllAsmParsers(); InitializeAllAsmPrinters();
auto TargetTriple = sys::getDefaultTargetTriple(); TheModule->setTargetTriple(TargetTriple); …… auto Target = TargetRegistry::lookupTarget(TargetTriple, Error); …… // 指定目标机器 auto CPU = "generic"; auto Features = "";
TargetOptions opt; auto RM = Optional<Reloc::Model>(); auto TheTargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);