notices for using llvm bitcode

If someone use ANTLR3 and LLVM 2.7 to construct a compiler, I believe some of my notices/hints written here will be very very useful.

  1. 1. function's parameter is read only. you should use "alloca" to create a "local variable" to make function parameter writable.
  2. each "basic block" should have a "terminate" instruction. -> br and ret
  3. each "function module" should have only one "ret" instruction. only one!!!!!!!!!!
  4. here's my solution: create a "entry block" and "return block". puts every "alloca" to "entry block" and create a "alloca" for return variable. Puts "ret" instruction in "return block". If other statement need to return different return value, I just store "return variable" and "br" to "return block".
  5. ANTLR3's tree walker is great to write "multiple pass" symbol table constructor. But it's a little inconvenient for LLVM IR generator.
  6.  EX) conditional block, if() :I need to use IR Builder to create a conditional jump before it's successor not created.  When antlr3 walk to this point, my program still don't know how many elsif and else success behind me.
    My Solution:Create your own IR to present statement structure. (only statement)

LLVM is ssa system.

  1.  But it's impossible to let all of your expression be ssa form (just my experience). 
  2. I just using alloca to present cross-statement variable, store these variable to "stack" instead SSA is much good choice for me. 
  3. If you want to fully utilize SSA's benefit, you should write your own IR to present an expression. Using ANTLR3's grammar tree, you don't need to worry about your IR's operator priority. Just be careful for assignment's LHF and RHS.


symbol mangling (compiler)

in the past year (2010), I'm working for a "action script" like language compiler.
Integrate with LLVM and ANTLR3, It's really easy to come out a "worked and lots of buggies" compiler. My target code is not just execute on a hardware, and it's not designed for general purpose. According to above situation, design a GOOD enough environment to verify my works.

1. there's no entry point for these program. (main function). I need to design another way to invoke my function. Either integrate with "C/C++" or just using cheat mangling to create a main function.
2. Because our platform target is very specially, the function signatures are different between language level and "llvm bitcode" level. (for other purpose, will append function parameter and change parameter types). If I want to using C/C++ to call my llvm bitcode, I need to get correct script name mangling and invoke function with correct parameters.

........mmmmm just mumbling. After read NameMangling: http://en.wikipedia.org/wiki/Name_mangling#Complex_example .

I think i need to change my old/stupid mangling design.