1
votes

I have a int32_t* type variable in llvm IR that stores the address of a place where a int32 is stored.

I want to set the value of this int32_t* variable in llvm ir

Let's say address is 1223. Then I tried the following. Is this correct? It doesn't seem to work

store i64 1223, i32** %1

I am storing the address 1223 in a int64_t constant int (since this is a 64-bit machine) and I am creating a store instruction to store this value in the memory where i32* is stored.

Is there a better way?

2

2 Answers

2
votes

All LLVM instructions are strictly typed and require the operands to maintain type correctness. Although your approach works in a language such as C, which has less strict type rules, it does not in LLVM. You have to explicitly convert your integer constant to a pointer:

%2 = inttoptr i64 1223 to i32*
store i32* %2, i32** %1

Converting an integer type to a pointer makes the program "type-unsafe" which is explicitly visible through the inttoptr instruction.

You can read more about the instruction in 3.6 Distinguishing safe and unsafe code: the cast instruction in The LLVM Instruction Set and Compilation Strategy: http://llvm.org/pubs/2002-08-09-LLVMCompilationStrategy.html

0
votes

I assume the Validate pass says you are doing something wrong and the types of your StoreInst mismatch.

You are trying to save a i64 variable to an i32 memory region what obviously is a problem. To make an integer variable a pointer you have to cast the address using an inttoptr cast:

store i32* inttoptr (i64 1223 to i32*), i32** %1