tags:

views:

183

answers:

3

LLVM is very modular and allows you to fairly easily define new backends. However most of the documentation/tutorials on creating an LLVM backend focus on adding a new processor instruction set and registers. I'm wondering what it would take to create a VHDL backend for LLVM? Are there examples of using LLVM to go from one higher level language to another?

Just to clarify: are there examples of translating LLVM IR to a higher level language instead of to an assembly language? For example: you could read in C with Clang, use LLVM to do some optimization and then write out code in another language like Java or maybe Fortran.

+1  A: 

There's nothing really special about the LLVM IR. It's a standard DAG with variable arity. Decompiling LLVM IR is a lot like decompiling machine language.

You might be able to leverage some frontend optimizations such as constant folding, but that sounds pretty minor compared to the whole task.

My only experience with LLVM was writing a binary translator for a class project, from a toy CISC to a custom RISC.

I'd say, since it's the closest thing to a standard IR (well, GCC GIMPLE is a close second), see if it fits with your algorithms and style and evaluate it as one alternative.

Note that GCC also started out prioritizing portability above all, and has also accomplished a lot.

Potatoswatter
A: 

Looks like the best place to start is with the CBackend in the LLVM source:

llvm/lib/Target/CBackend/CBackend.cpp

aneccodeal
+1  A: 

I'm not sure I follow how parts of your question relate one to another.

To target LLVM into a high-level language like C is very possible and you seem to have found one reference point.

VHDL is a whole other business however. Do you consider VHDL a high-level language? It may be, but but describing hardware/logic. Sure VHDL has some constructs that you can employ to actually program in it, but it's hardly a fruitful endeavor. VHDL describes hardware and thus makes translating LLVM IR into it a very hard problem, unless of course you design a CPU with a custom instruction set in VHDL and translate LLVM IR into your instructions.

Eli Bendersky
Yes, translating to VHDL is a hard problem because you're creating hardware. The CPU approach you mention is one way to do this. Other approaches might be more amenable to using a declarative (functional) input language and generate the hardware for it (Bluespec and Atom take this approach) - this works well for DSP algorithms, for example. However using that approach is probably not compatible with using LLVM.
aneccodeal
You have to define the domain of application for whom you would like to create vhdl. If they are not embarrassingly parallel than you have sequential part in them which has to work on CPU and you have to do hw-sw codesign. The parallel part would be placed on FPGA and the rest on a CPU. If you want to implement something like that with LLVM think about moving IR to bgl boost library because you will deal a lot with graphs and proper partitioning the design. Once you will find proper parallel part you can rewrite IR for that. I would say that starting from CB does not make sens.
name