I'm writing a JIT for my toy language, and I'm confused by one thing. How do I, with IR Builder, Insert a pointer constant? For the first version of my jit, I really only need LLVM to pass around opaque types and then pass those variables to C functions that return other opaque types. So for instance, I have a Int object that wraps the GNU GMP routines. A want to be able to tell IRBuilder "create a pointer named x and set it's value to this memory location". I know I'd have to re-compile everytime I reload my project, but for now, and considering it is a JIT, I don't really care. Timothy -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth)
You'll want to use alloca to create the local variable x, and then use a store instruction to store an inttoptr instruction. Reid On Fri, Jul 1, 2011 at 7:30 AM, Timothy Baldridge <tbaldridge at gmail.com> wrote:> I'm writing a JIT for my toy language, and I'm confused by one thing. > How do I, with IR Builder, Insert a pointer constant? For the first > version of my jit, I really only need LLVM to pass around opaque types > and then pass those variables to C functions that return other opaque > types. So for instance, I have a Int object that wraps the GNU GMP > routines. A want to be able to tell IRBuilder "create a pointer named > x and set it's value to this memory location". > > I know I'd have to re-compile everytime I reload my project, but for > now, and considering it is a JIT, I don't really care. > > Timothy > > -- > “One of the main causes of the fall of the Roman Empire was > that–lacking zero–they had no way to indicate successful termination > of their C programs.” > (Robert Firth) > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
1. Create an extern global variable of the type being pointed to. Use ExecutionEngine::addGlobalMapping to map that extern global variable to the pointer. Then your IR code can refer to the extern global variable and get the pointer. 2. Convert the pointer to an int. Create an int constant in IR. Then add an inttoptr instruction, and your IR has the pointer constant. Last I checked, though, the generated X86 code doesn't treat the resulting pointer as a constant... for instance, if you use it as a function pointer, the X86 code ends up being an indirect call through a function pointer in a register, rather than a direct call. On Fri, Jul 1, 2011 at 9:30 AM, Timothy Baldridge <tbaldridge at gmail.com> wrote:> I'm writing a JIT for my toy language, and I'm confused by one thing. > How do I, with IR Builder, Insert a pointer constant? For the first > version of my jit, I really only need LLVM to pass around opaque types > and then pass those variables to C functions that return other opaque > types. So for instance, I have a Int object that wraps the GNU GMP > routines. A want to be able to tell IRBuilder "create a pointer named > x and set it's value to this memory location". > > I know I'd have to re-compile everytime I reload my project, but for > now, and considering it is a JIT, I don't really care. > > Timothy > > -- > “One of the main causes of the fall of the Roman Empire was > that–lacking zero–they had no way to indicate successful termination > of their C programs.” > (Robert Firth) > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
I believe lldb does something similar to #2 for complex expression evaluation. It might be worth having a look there for some examples. Jim On Jul 2, 2011, at 7:19 AM, Kenneth Uildriks <kennethuil at gmail.com> wrote:> 1. Create an extern global variable of the type being pointed to. Use > ExecutionEngine::addGlobalMapping to map that extern global variable > to the pointer. Then your IR code can refer to the extern global > variable and get the pointer. > 2. Convert the pointer to an int. Create an int constant in IR. Then > add an inttoptr instruction, and your IR has the pointer constant. > Last I checked, though, the generated X86 code doesn't treat the > resulting pointer as a constant... for instance, if you use it as a > function pointer, the X86 code ends up being an indirect call through > a function pointer in a register, rather than a direct call. > > On Fri, Jul 1, 2011 at 9:30 AM, Timothy Baldridge <tbaldridge at gmail.com> wrote: >> I'm writing a JIT for my toy language, and I'm confused by one thing. >> How do I, with IR Builder, Insert a pointer constant? For the first >> version of my jit, I really only need LLVM to pass around opaque types >> and then pass those variables to C functions that return other opaque >> types. So for instance, I have a Int object that wraps the GNU GMP >> routines. A want to be able to tell IRBuilder "create a pointer named >> x and set it's value to this memory location". >> >> I know I'd have to re-compile everytime I reload my project, but for >> now, and considering it is a JIT, I don't really care. >> >> Timothy >> >> -- >> “One of the main causes of the fall of the Roman Empire was >> that–lacking zero–they had no way to indicate successful termination >> of their C programs.” >> (Robert Firth) >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev