Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

clang - Method to create LLVM IR

I am creating clang tool and I want to generate LLVM IR from clang AST. I am aware of -emit-llvm option that I can use to get *.ll file, but is there way to generate IR inside code? Some method that I can call that takes clang AST or AST context and returns llvm::Module? I cannot find any example that shows this.

Edited: So I tried using CodeGenAction for this, but I can't get it to work. I end up with unresolved external symbol error. Am I missing something?

#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
", i->getName().str().c_str());

    return 0;
}

Errors I got during linking:

LNK1120: 2 unresolved externals 

LNK2001: unresolved external symbol "public: __thiscall clang::EmitLLVMOnlyAction::EmitLLVMOnlyAction(class llvm::LLVMContext *)" (??0EmitLLVMOnlyAction@clang@@QAE@PAVLLVMContext@llvm@@@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)" (?takeModule@CodeGenAction@clang@@QAE?AV?$unique_ptr@VModule@llvm@@U?$default_delete@VModule@llvm@@@std@@@std@@XZ)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In clang the code generation happens in the CodeGenModule class. There you can get the current module through llvm::Module & getModule () const. CodeGenModule handles code generation on module level while CodeGenFunction does this for a particular function. There you find Emit* functions for each node of the AST.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...