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 techniqu开发者_运维问答e 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_spec
table using TARGET_ATTRIBUTE_TABLE
, as described in the GCC internals documentation. The details of struct attribute_spec
can 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 in gcc/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 use lookup_attribute()
(see gcc/tree.h
again) 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_INFO
hook ("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 the symbol_ref
(as the docs suggest). You can define a predicate for testing this flag in the .md .
精彩评论