2014-12-13 127 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