Using an AST as a higher level interface to LLVM: getting started

Hi,

For an experimental project at work, I want to try my hand at code generation. For the moment, I want my program to generate ASTs, not get them by parsing C files. Then I'd like to translate them into code and call into them. This is all on amd64. For now Linux, if it proves successful, Windows as well.

How should I get started? A couple questions:

- How easy is it to create an AST? Can I just new the nodes I need and point them at each other? Is there any non-obvious setup I need to do first?

- If I want to see the AST that corresponds to a given C construct, is here a way to hack clang to print out the AST? Where would I put that function?

- Once I have my AST, how do I compile it, load it & run it?

Best,
Martin

How should I get started?

By reading the documentation, tutorials and FAQs:
http://llvm.org/docs/
http://llvm.org/docs/tutorial/
http://llvm.org/docs/FAQ.html
http://clang.llvm.org/get_started.html
http://clang.llvm.org/docs/UsersManual.html

I've done a similar "experimental project" last year, hope that helps:
http://www.systemcall.org/rengolin/stuff/compiler/
http://www.systemcall.org/rengolin/stuff/compiler/download/LLVM-pet-project.pdf

Can I just new the nodes I need and
point them at each other? Is there any non-obvious setup I need to do
first?

Using regular pointers shouldn't be a problem... Normally, you build
the AST once, don't delete nodes, use it once and discard everything
altogether.

cheers,
--renato

http://systemcall.org/

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm