Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to auto-generate llvm c++ api code from LLVM-IR?

The clang 3.0 online demo page http://llvm.org/demo/index.cgi provides an option to output LLVM C++ API code" representing the LLVM-IR for the input program.

Is "produce LLVM C++ API code" output a clang option (and if so, what is it)?

Or is it an llvm tool option (which one)?

Is it possible to do the same thing but from LLVM-IR input? Basically I'd like to see the proper llvm c++ api calls needed to produce a particular given llvm-ir sequence. I'd like to learn backwards by example rather than forwards from the documentation.

Manual pages and --help and --help-hidden for clang, llvm-as and llvm-dis don't show anything obvious.

edit: OK now I see in the output on that web page, "generated by llvm2cpp". But I can't find that tool in recent llvm releases, only old releases, has a new tool in 2.9 and 3.0 taken over for llvm2cpp?

like image 340
Bogatyr Avatar asked Mar 23 '12 08:03

Bogatyr


People also ask

What is LLVM IR used for?

You can think of LLVM IR as a platform-independent assembly language with an infinite number of function local registers. When developing compilers there are huge benefits with compiling your source language to an intermediate representation (IR) 1 instead of compiling directly to a target architecture (e.g. x86).

What is LLVM IR code?

LLVM can provide the middle layers of a complete compiler system, taking intermediate representation (IR) code from a compiler and emitting an optimized IR. This new IR can then be converted and linked into machine-dependent assembly language code for a target platform.

Can I use LLVM with C?

C-like languages use the Clang front end. This component compiles C, C++, Objective C, and Objective C++ code into LLVM bitcode – and from there into object files, using LLVM.

Is LLVM IR cross platform?

LLVM IR can be cross-platform, with the obvious exceptions others have listed. However, that does not mean Clang generates cross-platform code. As you note, the preprocessor is almost universally used to only pass parts of the code to the C/C++ compiler, depending on the platform.


2 Answers

Yes. C++ backend is the tool which does this. Try "llc -march=cpp foo.bc"

like image 52
Anton Korobeynikov Avatar answered Sep 24 '22 05:09

Anton Korobeynikov


I ran into exactly the same problem and saw the CPPBuilder mentioned a couple of times. This approach unfortunately no longer works on recent LLVM versions as the CPPBackend was removed between 3.8 and 3.9.

If you want the CPP backend you (i) have to configure llvm and add cppbackend to -DLLVM_TARGETS_TO_BUILD during the initial configure and (ii) run an llvm <= 3.8.

The feature was removed because it did not use IRBuilder and almost nobody used it. My solution was to rely on the old version to get inspired, then implement it myself.

like image 25
Mathias Payer Avatar answered Sep 23 '22 05:09

Mathias Payer