1
votes

I am trying to build a null pointer using LLVM build store instruction builder for the following piece of code:

int *x;
x = 0;

The problem that I am facing is that when assigning zero to x I am getting an incompatible type error as 0 is of i32 type and x is of i32** type. I am using LLVMPointerType to create a pointer type for zero and using it in the BuildStore builder but LLVMPointerType returns an LLVMTypeRef whereas BuildStore needs LLVMValueRef Type of argument. I am stuck not being able to type cast and could really use some help.

Also is there another way of creating a NULL pointer in LLVM?

1

1 Answers

0
votes

If you're just after creating a null pointer, you're almost there -- you have a reference to the type that you want (i32* in this case), and now you have to create a constant of that type. In this case you can use the LLVMConstPointerNull function to get a value representing a null pointer of a given type.

More generally, if you're starting with an integer value and want to cast it to a pointer value, you can create an inttoptr constexpr or instruction using LLVMBuildIntToPtr.