I am writing a compiler for a language in which the user can choose between call by value or reference
The final stage is to produce llvm assembly. I read that arrays/vectors pass by reference but I cant figure out how to pass integers by reference.
I thought about creating a pointer and then pass the pointer, something like:
foo(ref var1) { var1 = var1 + 1 }
main { a=1; foo(a); }
being translated into
foo(int* var1) { (*var1) = (*var1) + 1 }
main {a=1; foo(&a ); }
but I cannot find how to do it in llvm.
Any ideas, either about the pointers or how to call an argument by reference would be awesome (I hope that it’s not too much of a RTFM question:/)
Try running the following code through http://llvm.org/demo/ with the optimization level set to None:
The answer is essentially just that you need to allocate memory via the alloca instruction.