2014-12-13 249 views
1

我正在创建铛工具,我想从ngng AST生成LLVM IR。我知道-emit-llvm选项,我可以用它来获得* .ll文件,但有办法在代码中生成IR?我可以调用的一些方法需要使用AST或AST上下文,并返回llvm::Module?我找不到任何显示此内容的示例。创建LLVM IR的方法

编辑: 所以我尝试使用CodeGenAction这个,但我不能让它工作。我结束了无法解析的外部符号错误。我错过了什么吗?

#include <clang/CodeGen/CodeGenAction.h> 
#include <clang/Frontend/CompilerInstance.h> 
#include <clang/Frontend/CompilerInvocation.h> 
#include <clang/Basic/DiagnosticOptions.h> 
#include <clang/Frontend/TextDiagnosticPrinter.h> 
#include <llvm/ADT/IntrusiveRefCntPtr.h> 
#include <llvm/IR/Module.h> 
#include <llvm/IR/LLVMContext.h> 

using namespace std; 

clang::CodeGenAction * getAction(void) 
{ 
    // Path to the C file 
    string inputPath = "test.c"; 

    // Arguments to pass to the clang frontend 
    vector<const char *> args; 
    args.push_back(inputPath.c_str()); 

    // The compiler invocation needs a DiagnosticsEngine so it can report problems 
    clang::DiagnosticOptions opt = clang::DiagnosticOptions(); 
    clang::TextDiagnosticPrinter *DiagClient = new clang::TextDiagnosticPrinter(llvm::errs(), &opt); 
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs()); 
    clang::DiagnosticsEngine Diags(DiagID, &opt, DiagClient); 

    // Create the compiler invocation 
    clang::CompilerInvocation* CI(new clang::CompilerInvocation()); 
    clang::CompilerInvocation::CreateFromArgs(*CI, &args[0], &args[0] + args.size(), Diags); 

    // Create the compiler instance 
    clang::CompilerInstance Clang; 
    Clang.setInvocation(CI); 

    // Get ready to report problems 
    Clang.createDiagnostics(&Diags.getDiagnosticOptions()); 
    if (!Clang.hasDiagnostics()) 
     return NULL; 

    // Create an action and make the compiler instance carry it out 
    clang::CodeGenAction *Act = new clang::EmitLLVMOnlyAction(&llvm::getGlobalContext()); 
    if (!Clang.ExecuteAction(*Act)) 
     return NULL; 

    return Act; 
} 

int main(void) 
{ 
    clang::CodeGenAction* Act = getAction(); 

    // Grab the module built by the EmitLLVMOnlyAction 
    std::unique_ptr<llvm::Module> module = Act->takeModule(); 

    // Print all functions in the module 
    for (llvm::Module::FunctionListType::iterator i = module->getFunctionList().begin(); i != module->getFunctionList().end(); ++i) 
     printf("%s\n", i->getName().str().c_str()); 

    return 0; 
} 

错误,我在连接过程中有:

LNK1120: 2 unresolved externals 

LNK2001: unresolved external symbol "public: __thiscall clang::EmitLLVMOnlyAction::EmitLLVMOnlyAction(class llvm::LLVMContext *)" ([email protected]@@[email protected]@[email protected]@@Z) 

LNK2001: unresolved external symbol "public: class std::unique_ptr<class llvm::Module,struct std::default_delete<class llvm::Module> > __thiscall clang::CodeGenAction::takeModule(void)" ([email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@@[email protected]@XZ) 

回答

1

在铿锵的代码生成发生在CodeGenModule类。在那里你可以通过llvm::Module & getModule() const获得当前模块。 CodeGenModule在模块级别处理代码生成,而CodeGenFunction为特定功能执行此操作。您可以在AST的每个节点找到Emit*函数。

+0

谢谢,我试过了,但是我无法完成它的工作。我结束了无法解决的外部错误,所以我用'CodeGenAction'试了一下,结果出现了类似的错误。我编辑了原始答案。任何提示? – benderto 2014-12-16 10:26:08

+0

@benderto缺少的符号在CodeGenAction.cpp中声明,因此您必须:使用您的应用程序编译CodeGenAction.cpp,或者b:将叮当CodeGen库链接到您的应用程序。 – 2014-12-16 10:42:56