[LLVMdev] getting and setting array indices c interface

Andrew Ferguson andrewf at idlearts.com
Fri Jul 8 18:00:08 PDT 2011

I really can't figure out how to get and set array indices from the c

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")

All seems to results in is getting and setting the first element

the output 

define i32 @Atest(i32 %a) {
  %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, %
  %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

More information about the llvm-dev mailing list