Please expand the notes to include examples you find useful.
Code tree representation
Gcc maintains a single-node representation for many code components. For example the same identifier name is always represented by the same node. This is done through the tree interfaces (implemented) in
tree.c
. To get the unique node for an identifier, use
tree id = get_identifier (name)
, where
name
is a character string.
If you want to print out the string representation of the node type, you can use the following trick due to
TongxinBai. As an example cute code trick in Gcc, while the tree types are defined in
enum tree_code
in
tree.h
, the actual definition is not in
tree.h
but in
tree.def
, which includes a list of all node types and their (string) symbols. Looking at the code you'll quickly see how the trick works, and borrowing it you can easily construct an array of type strings (in your program file) as follows:
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) STRING,
char *type_names[] = {
#include "tree.def"
};
During debugging, you can use the node type as the index into the
type_names
array and pull up the string name that you so often crave for under the circumstance.
Data structures in Gcc
Vector
The file
vec.h
specifies the interface equivalent of a template class library for variable size vectors. For example
list = VEC_alloc (data_struct*, heap, num_elements);
VEC_quick_push (data_struct*, list, new_elem);
Hash table
The interface is specified in
include/hashtab.h
. To allocate and use, try exploring some uses in Gcc, one of which is the following in
cgraph_node()
in
cgraph.c
.
static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
if (!cgraph_hash)
cgraph_hash = htab_create_ggc (10, hash_node, eq_test, NULL);
slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);