[LLVMdev] understanding of getelementptr with a const string

edA-qa mort-ora-y eda-qa at disemia.com
Sun Oct 28 05:03:43 PDT 2012


I'm working through some very basic IR code and got a bit confused by
the string printing example. In particular, if you have a constant
string like:

	@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00"

To pass this as a pointer to a C-function like printf you need to use 2
indexes in getelementptr:

	%sp = getelementptr [4 x i8]* @.str, i32 0, i32 0

I think I understand now, but would be gracious if somebody could
correct/clarify for me:

1. "@.str" represents a pointer to the data when it is used
2. the first 0 index to getelementptr is thus of type [4 x i18], if I
stopped here the type of %sp would b e [4 x i8]*
3. the second 0 index is to the first i8 of that array, by doing this
the resulting type is now i8* (suitable for printf)


Are the following two forms, to get the one-past-end pointer, also
equivalent: they result in the same pointer value and type and are correct:

	%sp = getelementptr [4 x i8]* @.str, i32 1, i32 0
	%sp = getelementptr [4 x i8]* @.str, i32 0, i32 4


-- 
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121028/f4999304/attachment.sig>


More information about the llvm-dev mailing list