[LLVMdev] getting and setting array indices c interface

Eli Friedman eli.friedman at gmail.com
Fri Jul 8 18:14:30 PDT 2011


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 element

The 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




More information about the llvm-dev mailing list