GetElementPtrInst Again!

I'm trying to set up a call to printf in stacker and have managed to
confuse myself. Perhaps you can shed some light.

I've declared printf as a function taking a pointer to SByteTy with var
args and returning SIntTy:

// Create a function for output (int printf(format,...))
std::vector<Type*> params;
params.push_back( PointerType::get( Type::SByteTy ) );
FunctionType* printf_type = FunctionType::get( Type::IntTy, params, true );
ThePrintf = new Function( printf_type, GlobalValue::ExternalLinkage,
            "printf", TheModule);
                                                                                                                                                             
When I set up the call, I get the following from that pesky :slight_smile: verifier:

Call parameter type does not match function signature!
        getelementptr [3 x sbyte]* %_str_format_, long 0 ; <[3 x sbyte]*>:0 [#uses=1]
typesbyte* sbyte *

So, in LLVM are arrays and pointers not equivalent as in "C"?

What do I have to do to turn my little str_format array into a pointer?

I tried the FunctionType with an argument of [3 x sbyte] but that led to
much worse diagnostics.

Help! :slight_smile:

Reid.

I'm trying to set up a call to printf in stacker and have managed to
confuse myself. Perhaps you can shed some light.

:slight_smile:

I've declared printf as a function taking a pointer to SByteTy with var
args and returning SIntTy:

Sounds good.

When I set up the call, I get the following from that pesky :slight_smile: verifier:

Ah, but it's so helpful! :slight_smile:

Call parameter type does not match function signature!
        getelementptr [3 x sbyte]* %_str_format_, long 0 ; <[3 x sbyte]*>:0 [#uses=1]
typesbyte* sbyte *

So, in LLVM are arrays and pointers not equivalent as in "C"?

Absolutely not.

What do I have to do to turn my little str_format array into a pointer?

Try making a: getelementptr [3 x sbyte]* %_str_format_, long 0, long 0

This means:
   [3 x sbyte]* %_str_format_, ; Start from _str_format_
   long 0, ; Get the first [3 x sbyte] array pointed to
   long 0 ; Get the first element in the array

Also, you can try plunking the equivalent code into the C frontend (either
manually or through the demo page) to see what it makes, if you get
confused. :slight_smile:

-Chris

> When I set up the call, I get the following from that pesky :slight_smile: verifier:

Ah, but it's so helpful! :slight_smile:

It is actually. It is ensuring that my compiler is correct which is one
of the hardest things to do!

> So, in LLVM are arrays and pointers not equivalent as in "C"?

Absolutely not.

Aha!

> What do I have to do to turn my little str_format array into a pointer?

Try making a: getelementptr [3 x sbyte]* %_str_format_, long 0, long 0

This means:
   [3 x sbyte]* %_str_format_, ; Start from _str_format_
   long 0, ; Get the first [3 x sbyte] array pointed to
   long 0 ; Get the first element in the array

Okay, so getelementptr gives you a pointer to the last thing indexed,
right? So, if I just use a single long 0 operand on a global array, I
get a pointer to an array in its entirety. That is the pointer type is
[count x type]. If I use two long 0 operands, I get a pointer to the
first element of the array so that the type of the pointer is just
"type". I had assumed that, since the address of the 0th element of an
array and the address of the array are the same that this equivalence
would be recognized. But, apparently not. I can see why, however, a
pointer to the whole array and a pointer to its first element differ in
their type, even though the pointer values are the same. Am I getting
this now?

Also, you can try plunking the equivalent code into the C frontend (either
manually or through the demo page) to see what it makes, if you get
confused. :slight_smile:

Excellent idea .. I won't have to bug you as much :slight_smile:

Reid.

would be recognized. But, apparently not. I can see why, however, a
pointer to the whole array and a pointer to its first element differ in
their type, even though the pointer values are the same. Am I getting
this now?

Yup, that's exactly right.

-Chris