I'm struggling to figure out how to assign an array element using the LLVM c++ API. consider this C code:
int main()
{
int aa[68];
aa[56] = 7;
return 0;
}
using
clang -S -emit-llvm main.c
I get the following IR (attributes and other things are skipped for simplicity):
define dso_local i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca [68 x i32], align 16
store i32 0, i32* %1, align 4
%3 = getelementptr inbounds [68 x i32], [68 x i32]* %2, i64 0, i64 56
store i32 7, i32* %3, align 16
ret i32 0
}
I already know how to create an inbounds GEP, but when storing a value (7) to the array the type is a pointer to i32.
my language is very similar to C, that's why I'm using C as an example (so far it's just C but with a different syntax). generated IR for my language is:
define i32 @main() {
%0 = alloca [2 x i32], align 4
%1 = getelementptr [2 x i32], [2 x i32]* %0, i32 1
store i32 1, [2 x i32]* %1, align 4
ret i32 0
}
how can I possibly turn [2 x i32]* into i32* when creating a store? this is how I create the store:
llvm::AllocaInst *stored = symbol_table[arr_name];
llvm::Value *result = ir_builder->CreateGEP(stored->getAllocatedType(), stored, idx_vals);
// idx_vals contains the index
ir_builder->CreateStore(val, result); // here val is a value stored in a symbol table
// and it's type is llvm::Value *
[2 x i32]through a[2 x i32]*in a single store instruction. Build a[2 x i32]with%A = insertvalue [2 x i32] undef, i32 7, 0%B = insertvalue [2 x i32] %A, i32 8, 1. You can callthing->dump()on any value or type to see what it is (for values it includes the type). You should be able to see that your%1was a[2 x i32]*and not ani32*, also that the%Bin my example is a[2 x i32]array.