This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Code Gen problem calling functions through pointers.


David Williams wrote:
> #define TEST_FN_VECTOR  0x100
> #define TestFunction()  (*(void (*)(void))(TEST_FN_VECTOR<<2))()
> 
> I have found that the compiler sometimes generates the correct code. Eg.
> 
>         move.w #256,%a2
>         jsr (%a2)
> 
> But at other times generates
> 
>         jsr 256.w
> 
> The second case will execute code at the vector address rather than using
> the contents of the vector as a pointer to the location to start executing
> from.
> 
> I have not been able to determine when or why the compiler decides to
> generate one form or the other. I did notice that the functions that had
> the erroneous code where generally simpler.
> 
> This seems like a code generation bug to me.
> 
> Is my cast from an integer to a function pointer OK?

Hmm if I can recall my 68k (without digging out the manual), both of
these code sequences do the same thing.  They both will try to execute
code at address 256.  I know the compiler will tend to put the address
of a function in a register and call using the register, if the function
is called more than once from the calling function.  Also, did you enter
the example correctly?  seems like the compile ought to be computing
address
1024 (TEST_FN_VECTOR<<2).

I think that you want to have an extra dereference in your declaration:
 #define TestFunction()  (**(void (**)(void))(TEST_FN_VECTOR<<2))()

Think of the constant TEST_FN_VECTOR<<2 as being equivalent to a
variable holding the value of the constant (a pointer to address
TEST_FN_VECTOR<<2).

Art
_______________________________________________
New CrossGCC FAQ: http://www.objsw.com/CrossGCC
_______________________________________________
To remove yourself from the crossgcc list, send
mail to crossgcc-request@cygnus.com with the
text 'unsubscribe' (without the quotes) in the
body of the message.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]