[LLVMdev] Operations on constant array value?

Eli Friedman eli.friedman at gmail.com
Mon Jan 11 07:29:09 PST 2010


On Mon, Jan 11, 2010 at 7:07 AM, James Williams <junk at giantblob.com> wrote:
> Hi,
>
> I've read http://llvm.org/docs/LangRef.html#t_array and
> http://llvm.org/docs/GetElementPtr.html and if I've understood right there
> are no operations that act directly on arrays - instead I need to use
> getelementptr on a pointer to an array to get a pointer to an array element.
> I also understand that there is no 'address of' operation.
>
> As a result I can't figure out how to use constant derived types without
> assigning them to a global. Say I want to use the C bindings function
> LLVMValueRef LLVMConstString(char *, int, int) to get an int8* pointer to a
> C string constant - there doesn't seem to be any way to directly use the
> resulting [N x i8] value directly and there's no operator that gives me its
> address.
>
> The only way I can see to get a pointer to the string constant array is to
> go through a global variable, for example:
>
> g = LLVMAddGlobal(module, LLVMTypeOf(v), "__string_" +
> string_literal_number);
> string_literal_number = string_literal_number + 1;
> v = LLVMConstString(string_literal, string_literal.Length, 0);
> LLVMSetInitializer(g, v);
> elements = { LLVMConstInt(LLVMInt32Type(), 0L, 0),
> LLVMConstInt(LLVMInt32Type(), 0L, 0) };
> return LLVMConstInBoundsGEP(g, elements, 2);
>
> Is it possible to get the address of an element of a constant array or
> struct without first initializing a global variable to the constant?

No.

-Eli



More information about the llvm-dev mailing list