https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89148

            Bug ID: 89148
           Summary: [AVR] Merge plugin to place C++ vtables in flash
                    memory
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.mach4 at gmail dot com
  Target Milestone: ---

Hello,

C++ code using virtual functions and compiled with AVR-G++ results in excessive
SRAM usage on AVR devices. This is because vtables are copied into SRAM, when
it would instead be sufficient to access them from flash memory. This behaviour
has been known and discussed (e.g. in #43745) for quite a few years.

I have run into this issue myself, and know that it is a pain point of using
C++ on AVR for many in the Arduino and AVR communities. It may even contribute
to the reduced usage of C++ on AVR. (Illustration: searching 'AVR' on Github
returns 4440 results in C and only 1251 in C++.)

Anyway, I recently stumbled across a very elegant looking solution to this
problem, in the form of a GCC/G++ plugin. According to the plugin author
(https://github.com/jcmvbkbc/avr-flash-vtbl), it effectively adds the __flash
attribute to C++ vtables and vtable pointer types. Further analysis of the
issue is at https://habrahabr.ru/company/amperka/blog/264041 (need Google
translate because it's in Russian).

The full source of the plugin is just 18 lines of C:

#include <gcc-plugin.h>
#include <cp/cp-tree.h>

int plugin_is_GPL_compatible = 1;

static void fn(void *gcc_data, void *user_data) {
    TYPE_ADDR_SPACE (TREE_TYPE (vtbl_type_node)) = 1;
    TYPE_ADDR_SPACE (TREE_TYPE (vtbl_ptr_type_node)) = 1;
}

int plugin_init (struct plugin_name_args *plugin_info,
                 struct plugin_gcc_version *version) {
    register_callback("", PLUGIN_START_UNIT, fn, NULL);
    return 0;
}

So my question is, is there any way that this wonderful solution to a long
standing problem could be integrated into mainline G++, for example as a
target-specific flag (e.g. -fflash-vtables)? That way, it would be accessible
to the wider AVR community, many of whom would not know how to compile a GCC
plugin.

Many thanks,
Max

Reply via email to