More space efficient virtual function calls
Matthew Wilcox
willy@debian.org
Sun Jul 7 10:03:00 GMT 2002
On Sat, 6 Jul 2002 17:29:11 -0700, Wink Saville wrote:
> I was wondering if there was switch in gcc that would cause it to generate
> smaller code when invoking virtual functions. I'm using GCC 3.1 ARM cross
> compiler with -O3 and -fvtable-thunks=3, and it does the following when
> calling a virtual function with no parameters:
>
> LDR R1,[R4] /* Fetch the vtable pointer */
> MOV R0,R4 /* R0 = this */
> MOV LR, PC /* Set of the link register */
> LDR PC,[R1, #4] /* Call virtual function 1, the second entry */
>
> This is good code but it takes 20 bytes, what I was hoping for when I
> enabled -fvtable-thunks=3 the compiler would use intermediate "thunks" to
> invoke functions such as:
>
> LDR R0,R4 /* R0 = this */
> BL __VTFunc1 /* Branch and Link to virtual thunk function 1 */
>
> ...
>
> __VTFunc0:
> MOV R5,[R0] /* Get vtable pointer */
> LDR PC,[R5,#0] /* Jump to the function */
> __VTFunc1:
> MOV R5,[R0] /* Get vtable pointer */
> LDR PC,[R5,#4] /* Jump to the function */
> __VTFunc2:
> MOV R5,[R0] /* Get vtable pointer */
> LDR PC,[R5,#8] /* Jump to the function */
The current code takes 16 bytes, not 20. The proposed code above is buggy;
I think what you meant was:
MOV r0, r4 /* r0 = this */
BL __VTFunc1
...
__VTFunc0:
LDR r5, [r0] /* fetch vtable pointer */
LDR pc, [r5, #0] /* jump to the function */
__VTFunc1:
LDR r5, [r0]
LDR pc, [r5, #4]
__VTFunc2:
LDR r5, [r0]
LDR pc, [r5, #8]
if you use a call-clobbered register instead of r5 (i don't remember
the apcs right now), you don't even need to reserve r5.
i have no idea how to change gcc to do what you want though ;-)
--
Revolutions do not require corporate support.
More information about the Gcc
mailing list