GCC Bugzilla – Bug 43745
[avr] g++ puts VTABLES in SRAM
Last modified: 2014-02-16 13:17:02 UTC
On AVR target g++ generates code which copies object’s VTABLES from FLASH to SRAM wasting the memory. Due to the Harvard architecture of AVR processors the solution is not trivial. This behavior can be observed in any c++ program which has object with virtual method, e.g:
virtual void example();
The VTABLE of class test will be generated in FLASH and next copied to SRAM, any reference to virtual example() method will take the method address from SRAM.
What is your suggestion?
(In reply to comment #1)
> What is your suggestion?
Because VTABLES are generated during compile time and they are const data there is no need to copy them into SRAM. Appropriate addresses of virtual methods can be read directly from FLASH and used to make indirect call as usual. As far as I know the problem is that AVR port of gcc doesn’t have appropriate isns to do that. The problem is extremely important because for example my program uses 964 bytes of SRAM, from which 860 bytes are used for VTABLES (wasted). This is a typical situation for any C++ program on AVR.
The only problem is speed penalty, LD instruction on AVR takes 1 cycle, whereas LPM instruction needed for FLASH access 3 cycles, which gives total 2 or 6 cycles to read virtual method address respectively. So probably the best solution is to add another gcc option (or pragma) to choose between storing VTABLES only in FLASH (slower code execution) or moving it to SRAM (faster execution for the cost of higher SRAM memory occupation).
Another idea is to simplify virtual calls.
At the final link, probably most of the virtual calls could be converted into regural calls (as whole program is known, and some base classes may have only one (or zero) descendant, so all virtual calls will affect only it).
(In reply to comment #0)
> Class test
> virtual void example();
Would you please post some real code that shows the problem and actually *compiles* with, e.g.
avr-g++ -c vtable.cpp
instead of the lines above that trigger syntax error?
Ok, here is the code:
virtual void vfunction();
After compilation 6 bytes of SRAM is occupied by test object vtable. Here is a resulting part of map file:
.data 0x00800100 0x0 c:/winavr-20100110/bin/../lib/gcc/avr/4.3.3/../../../../avr/lib/avr51/crtm128.o
.data 0x00800100 0x6 gpp.o
0x00800100 vtable for test
.data 0x00800106 0x0 c:/winavr-20100110/bin/../lib/gcc/avr/4.3.3/avr51\libgcc.a(_exit.o)
I'm also experiencing this issue and would like a patch or something to be able to directly access vtables from flash on need.
I'd like to know if anyone has found even a temporary fix to that (even though it means losing to a non portable solution/bad code ...).
I'm currently facing the exact same problem. I have a several classes with virtual methods and 50% of my RAM is taken by the vtables of these classes.
I'm still willing to know if someone has come up with a solution/workaround for this.
Thank you !
This needs extensions in the C++ front => Component = c++
*** Bug 260998 has been marked as a duplicate of this bug. ***
Seen from the domain http://volichat.com
Page where seen: http://volichat.com/adult-chat-rooms
Marked for reference. Resolved as fixed @bugzilla.