Andrew Ferguson
2011-Jul-09 01:00 UTC
[LLVMdev] getting and setting array indices c interface
I really can't figure out how to get and set array indices from the c
interface.
so to get an element I'm calling
tindex = *fn\SymbolTable(*index\name)
index = LLVMBuildLoad(builder,tindex,"index")
arr = *fn\SymbolTable(*array\name)
arrptr = LLVMBuildLoad(Builder,arr,"arrayptr")
tmp = LLVMBuildGEP(Builder,arrptr,index,0,"ptr")
ptr = llvmBuildload(Builder,tmp,"ele")
and to set
tarr = *fn\SymbolTable(*array\name)
Arr = LLVMBuildLoad(builder,tarr,"array")
tval = *fn\SymbolTable(*value\name)
val = LLVMBuildLoad(builder,tval,"val")
tindex = *fn\SymbolTable(*index\name)
index = LLVMBuildLoad(builder,tindex,"index")
ptr = LLVMBuildGEP(builder,Arr,index,0,"ptr")
LLVMBuildStore(builder,val,ptr)
All seems to results in is getting and setting the first element
the output
define i32 @Atest(i32 %a) {
Entry:
%a1 = alloca i32
store i32 %a, i32* %a1
%ptrMyArray = alloca i32*
%myArray = alloca i32, i32 25
store i32* %myArray, i32** %ptrMyArray
%x = alloca i32
store i32 0, i32* %x
br label %ForEntry1
ForEntry1: ; preds = %Entry
store i32 1, i32* %x
br label %For1
For1: ; preds = %ForBody1, %
ForEntry1
%x2 = load i32* %x
%cond = icmp sle i32 %x2, 25
br i1 %cond, label %ForBody1, label %ForEnd1
ForBody1: ; preds = %For1
%array = load i32** %ptrMyArray
%val = load i32* %x
%index = load i32* %x
%ptr = getelementptr i32* %array
store i32 %val, i32* %ptr
%ctr = load i32* %x
%next = add i32 %ctr, 1
store i32 %next, i32* %x
br label %For1
ForEnd1: ; preds = %For1
%ind = load i32* %a1
%array3 = load i32** %ptrMyArray
%ptr = getelementptr i32* %array3
%ele = load i32* %ptr
ret i32 %ele
}
--
Andrew Ferguson
+649 372 6039
www.idlearts.com
Eli Friedman
2011-Jul-09 01:14 UTC
[LLVMdev] getting and setting array indices c interface
On Fri, Jul 8, 2011 at 6:00 PM, Andrew Ferguson <andrewf at idlearts.com> wrote:> I really can't figure out how to get and set array indices from the c > interface. > > so to get an element I'm calling > > tindex = *fn\SymbolTable(*index\name) > index = LLVMBuildLoad(builder,tindex,"index") > arr = *fn\SymbolTable(*array\name) > arrptr = LLVMBuildLoad(Builder,arr,"arrayptr") > tmp = LLVMBuildGEP(Builder,arrptr,index,0,"ptr") > ptr = llvmBuildload(Builder,tmp,"ele") > > and to set > > tarr = *fn\SymbolTable(*array\name) > Arr = LLVMBuildLoad(builder,tarr,"array") > tval = *fn\SymbolTable(*value\name) > val = LLVMBuildLoad(builder,tval,"val") > tindex = *fn\SymbolTable(*index\name) > index = LLVMBuildLoad(builder,tindex,"index") > ptr = LLVMBuildGEP(builder,Arr,index,0,"ptr") > LLVMBuildStore(builder,val,ptr) > > All seems to results in is getting and setting the first elementThe signature of LLVMBuildGEP is the following: LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, LLVMValueRef *Indices, unsigned NumIndices, const char *Name); If NumIndices is zero, you're ending up with a no-op GEP. NumIndices needs to be at least 1 to create a meaningful GEP. -Eli