[LLVMdev] addPassesToEmitFile

Álvaro Castro Castilla alvaro.castro.castilla at gmail.com
Sun Mar 8 12:54:01 PDT 2009


Hi,


Long time ago (llvm-svn june 2008) I asked here about a way to output
the assembly code of my JIT generated code to a string, so I could use
it to read it on the screen. I came up with this solution:

    std::string Err;
    const llvm::TargetMachineRegistry::entry* _arch =
llvm::TargetMachineRegistry::getClosestTargetForJIT(Err);

    std::string FeaturesStr;
    llvm::TargetMachine* target(_arch->CtorFn(*_module, FeaturesStr));
    assert(target && "Could not allocate target machine!");

    std::ostringstream os;

    target->addPassesToEmitFile(*_passManager, os,
llvm::TargetMachine::AssemblyFile, true);
    target->addPassesToEmitFileFinish(*_passManager, 0, true);



However, in newer versions of llvm, like 2.5 that I'm testing now, I
had to change the code to something like this:


    std::string Err;
    const llvm::TargetMachineRegistry::entry* _arch =
llvm::TargetMachineRegistry::getClosestTargetForJIT(Err);

    std::string FeaturesStr;
    std::auto_ptr<llvm::TargetMachine> target(_arch->CtorFn(*_module,
FeaturesStr));

    std::ostringstream os;
    llvm::raw_ostream *Out = new llvm::raw_os_ostream(os);
    target->addPassesToEmitFile(*_passManager, *Out,
llvm::TargetMachine::AssemblyFile, true);
    target->addPassesToEmitFileFinish(*_passManager, 0, true);



Both versions were based on the code from llc of its release.
Everything compiles perfectly in both versions. However, when the

_passManager->run(*_module);

is called, I get a Segmentation fault that I'm unable to figure out,
as I can't track it with gdb. The first version was working perfectly.


I would really appreciate if someone could give me a clue of what I'm
doing wrong...

Thank you,


Bests,



More information about the llvm-dev mailing list