[LLVMdev] Test compiler help

Dan Gohman gohman at apple.com
Tue Aug 18 10:52:10 PDT 2009


On Aug 16, 2009, at 12:39 PM, Renato Golin wrote:


> Hi all,
>
> I'm writing a test compiler to understand the overall structure of
> LLVM and I managed to produce IR for expressions, functions and
> function calls.
>
> I'm following the Kaleidoscope example and had a hard time de-tangling
> the language specifics from LLVM syntax. Anyhow, I got here and I'd
> like some more specific help to finish my example.
>
> My very simple language has global variables and static functions
> only, so I can focus on the code generation. My problems are below.
>
> 1. Kaleidoscope uses function calls, which I believe prepare the
> stack, saves the link register and so on, which I'm not interested at
> all, as everything is static. What's the correct way to create a
> simple jump instead of a call?

One way to do this is to just use one big Function and use branches
to do the jumping. If you're just making a simple state machine,
this may be the best approach.

Another way to do this is with tail calls. In short, you put a call at
the end of each function which "calls" the next function, marked with
with the "tail" keyword, and mark all the functions and calls with the
"fastcc" keyword. Check out LangRef.html for details on these
keywords. Then run llc with the -tailcallopt flag, and it should
optimize the call into a simple jump. This approach ought to be more
suitable for more involved functional-style programming languages,
though I don't personally have experience using LLVM in this way.

>
> 2. Each static function is called a "state". At the end, it MUST jump
> to the next state, even if it's itself. If it doesn't, the program
> must end. I also want create an end of the program whenever the token
> "end" is parsed. How can I do it?

The program exits after main returns, so with the tail call model,
the way to exit the program is to have a function just return,
instead of calling another function. For the branch model, just
return from main.

Dan




More information about the llvm-dev mailing list