Generating C code using LLVM

Hi,

I am trying to use LLVM to generate C (partly, Intel AVX) code to test some abstractions. Apparently, most of the documentation is related to generating LLVM-IR. Are there any tools to do this? (I have generated AST from these abstractions. I want to generate C code from it).

Thank you! :slight_smile:

PS: If this is a wrong mailinglist, help me with the right one.

From what input? Usually Clang is the tool for “rewriting C” (making source to source conversions), but it’s not clear if that’s what you are trying to achieve, so some more information would be useful.

Afaict, LLVM won’t generate C source code tho’.

Hi,

Thanks for the response.

Hi,
Thanks for the response.

From what input? Usually Clang is the tool for "rewriting C" (making
source to source conversions), but it's not clear if that's what you are
trying to achieve, so some more information would be useful.

I have successfully generated AST from my "hypothetical language" which
can be translated to C. I want to use LLVM instead of building my own
compiler. Rewriting "C" may work (I may have to try). I don't want to
generate IR as it is hard to read whether the C output is exactly what I
meant to write.

Afaict, LLVM won't generate C source code tho'.

Oh. No chance?
Thank you! :slight_smile:

LLVM used to have a C backend in-tree. It was removed a few releases ago
due to disuse and code rot.

Someone maintains a fork where the C backend is alive:
https://github.com/draperlaboratory/llvm-cbe -- so you may want to check
that.

I'm curious why you need this though. Are you planning to go through LLVM
just for the sake of its IR-level optimizations?

Eli

A version of it is also used in
multicoreware / cppamp-driver-ng / wiki / Home — Bitbucket to
generate OpenCL from C++AMP.

When I see all these projects using it under the hood, I wonder whether
it would not worth to have it back as a first class citizen...
Just need a code owner for Tom. :slight_smile:

Thank you very much!! :slight_smile:

If you have your code at the AST level, you might as well use CLANG to
work at the AST level and create C code as output.
CLANG is closely linked to LLVM.
There seems very little point in lowering the AST to LLVM-IR then
output to C. You will loose useful structural information.
E.g. By using something like this with CLANG would be better:
http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang

Also, if you can create AST in a format CLANG understands, it can then
automatically lower it to LLVM-IR, and you will have your completed
compiler for your own language.

The mailing list for CLANG is
mailto: cfe-dev@cs.uiuc.edu

Kind Regards

James

I got the impression that the AST was more like the AST of Kaleidoscope than the AST of Clang - reading between the lines in the posts above.

But yes, Clang has much better tools for generating C code out of AST than LLVM-IR will ever have. For example differnet types of loops [even when they are “strange forms”] will be retained in their original form - so loops using for(;:wink: as a forever loop will not turn into a while(true) or a goto loop. In LLVM-IR, this sort of construction would become lost, since the IR for any “endless loop” is identical.

> Hi,
> I am trying to use LLVM to generate C (partly, Intel AVX) code to test
some
> abstractions. Apparently, most of the documentation is related to
generating
> LLVM-IR. Are there any tools to do this? (I have generated AST from these
> abstractions. I want to generate C code from it).
> Thank you! :slight_smile:
>
> PS: If this is a wrong mailinglist, help me with the right one.
>

If you have your code at the AST level, you might as well use CLANG to
work at the AST level and create C code as output.
CLANG is closely linked to LLVM.

Cool.

There seems very little point in lowering the AST to LLVM-IR then
output to C. You will loose useful structural information.

I am trying to do AST->C. It's one way. You won't hit stages you already
passed.

E.g. By using something like this with CLANG would be better:

Basic source-to-source transformation with Clang - Eli Bendersky's website

Also, if you can create AST in a format CLANG understands, it can then
automatically lower it to LLVM-IR, and you will have your completed
compiler for your own language.

Thank you! :slight_smile: