[LLVMdev] Can I use Clang to parse snippets of C++ code?

Andreas Färber andreas.faerber at web.de
Sat Feb 26 03:04:46 PST 2011


Hello,

Am 23.02.2011 um 01:46 schrieb Félix Cloutier:

> I believe that what I'm trying to do with Clang is fairly simple;  
> the final goal, however, might be a little harder.
>
> Knowing myself, there are chances I'll never go through with this  
> project (like Mikael who posted earlier, I'm nothing but an  
> enthusiastic student with lots of time on my hands), but it feels  
> cool enough to me to announce my idea. Besides, I'll probably need  
> help from more knowledgeable people anyways.
>
> The way LLVM works makes it pretty easy and straightforward to  
> generate code from nested structures (like ASTs), which is totally  
> commendable since LLVM is a compiler back-end. However, in the past  
> months I've set myself to make an emulator back-end with LLVM that  
> would translate machine code to LLBC then compile it to native code  
> with the JIT, and my experience haven't been so great, especially  
> because of the following:
> 	• it's stupid-hard to debug just-in-time generated code with the  
> version of gdb that ships with Xcode (it repeatedly crashed on me);
> 	• the sheer number of cases to treat is, in itself, rebuking  
> enough: a NES 6502 is 'fine' with just less than 60 distinct  
> operations, but the full-fledged PowerPC you get with a GameCube has  
> roughly 6 times more;
> 	• when faced with subtle bugs, it's much easier to deal with C++  
> code representing what you want to do (like interpreter code) than  
> IRBuilder::Create* calls.
>
> I figured that while I can't do much about the first, if I could get  
> LLVM to generate code that would generate code, the two others would  
> be much less cumbersome.
>
> So my plan is to write a tool that accepts a specification of how  
> instructions should be interpreted, with handlers written in C++,  
> and turn that into an usable recompiler (that would also use LLVM  
> libraries). The grammar would be a shell for C++ code, and I'd use  
> Clang to turn the actual code into LLBC; then, I would pass through  
> the code (à la llvm2cpp), and create calls to an IRBuilder to  
> generate equivalent code. Once this generated class compiled  
> (through regular means), clients would call the appropriate methods  
> on the object to generate code, and will finally be able to get a  
> Function to use with the JIT.
>
> I've joined an example grammar and an example expected output (made  
> on the train, it's not actually working, but it gives a good idea).
>
> <example.txt>

Sounds a lot like LLVM's TableGen .td file format, no? It uses  
[{ ... }] syntax to include snippets of C++ code.

If this is specifically about emulating PowerPC, I would recommend  
extending an existing emulator like QEMU, which also emulates some of  
the hardware you'll need for the CPU to do anything useful. Having  
said that, it might be interesting to use LLVM as TCG backend though.

Andreas



More information about the llvm-dev mailing list