[LLVMdev] [SOLVED] Semi-random crashes seemingly related to Arguments

Marcel Weiher marcel at metaobject.com
Thu Jan 25 20:52:33 PST 2007


Thanks to Chris, this is now working.  The problem was the way I had  
copied the sample code without fully understanding it and thus misused  
the automatic iterator->class-pointer conversion.  In the sample code,  
all the functions have only a single argument so the following code is  
perfectly OK:

	Argument *ArgX = FibF->arg_begin();

However, this actually does two things:  (1) gets the iterator and (2)  
obtains the Argument* pointer from the iterator.

In my original code, I actually do the conversion above and then start  
to "iterate" with the pointer instead of the iterator.  So in effect,  
I end up just incrementing the pointer and ignoring the real next  
element the iterator would have yielded.  What I needed to do was  
separate out steps (1) and (2).  Not also that there is now a cast  
from Function::arg_iterator to Argument* when sending the message.

So the corrected code is:

-argumentAtIndex:(int)argIndex
{
         if ( argIndex < numArgs ) {
                 Function::arg_iterator ArgX;			//  get the iterator,  
not the Argument* converted from the iterator
                 int i;
                 ArgX = ((Function*)function)->arg_begin();
                 for (i=0 ;i<argIndex;i++) {
                         ++ArgX;

                 }
                 return [MPWLLVMValue llvmValueWithLLVMValue: 
(Argument*)ArgX];  //  get actual Argument pointer from iterator
         } else {
                 [NSException raise:@"IndexOutOfBounds"  
format:@"requesting argument %d when only %d available",arg
Index,numArgs];
                 return NULL;
         }
}


On Jan 23, 2007, at 11:33 , Chris Lattner wrote:
>
> Your code looks pretty reasonable to me.  The only thing to be aware  
> of is
> that varargs functions will have fewer Argument nodes than calls to  
> the
> functions have (e.g. the values passed through the "..." won't have a
> corresponding Argument).  Other than that, I can't think of anything
> off-hand.
>
>> ------- my weird code.m ------------
>> -argumentAtIndex:(int)argIndex
>> {
>>         Argument *ArgX;
>>         if ( argIndex < numArgs ) {
>>                 int i;
>>                 ArgX = ((Function*)function)->arg_begin();
>>                 for (i=0 ;i<argIndex;i++) {
>>                         ++ArgX;
>>                 }
>>
>>                 [NSString stringWithFormat:@"get argument[%d/%d]=
>> %x",argIndex,numArgs,ArgX];     //   with this in place, it doesn't
>> crash
>>                 return [MPWLLVMValue
>> llvmValueWithLLVMValue:ArgX];  // wrap the arg
>>         } else {
>> 		//--- error handling, irrelevant here...
>>                 [NSException raise:@"IndexOutOfBounds"
>> format:@"requesting argument %d when only %d
>> available",argIndex,numArgs];
>>                 return nil;
>>         }
>> }



More information about the llvm-dev mailing list