So, I’m building a custom backend for GCC for a processor. This processor has 4 address spaces: local, global, mmm, and mmr. I want to make it such that when writing c code, you can do this:
int global x = 5;
which would cause the compiler to spit out an instruction like this:
ldi.g %reg, 5
I know that certain processors like blackfin and MeP do something similar to this, so I figure its possible to do, however I have no idea how to do it. The technique that should allow me to do this is a variable attribute.
Any suggestions on how I could go about doing this?
You can add target-specific attributes by registering a
struct attribute_spectable usingTARGET_ATTRIBUTE_TABLE, as described in the GCC internals documentation. The details ofstruct attribute_speccan be found in the source (gcc/tree.h).This handler doesn’t need to do anything beyond returning
NULL_TREE, although typically it will at least do some error checking. (Read the comments ingcc/tree.h, and look at examples in other targets.)Later, you can obtain the list of attributes for a declaration tree node with
DECL_ATTRIBUTES()(see the internals docs again), and uselookup_attribute()(seegcc/tree.hagain) to see if a given attribute in the list.You want to references to a symbol to generate different assembly based on your new attributes, so you probably want to use the
TARGET_ENCODE_SECTION_INFOhook (“Define this hook if references to a symbol or a constant must be treated differently depending on something about the variable or function named by the symbol”) to set a flag on thesymbol_ref(as the docs suggest). You can define a predicate for testing this flag in the .md .