# GNU C Compiler Internals/GEM Framework 4 1
### Hooks

GEM framework is designed to facilitate development of compiler extensions. The idea of GEM is similar to the idea of Linux Security Modules (LSM), a project that defines hooks throughout Linux kernel that allow one to enforce a security policy.
GEM defines a number of hooks throughout GCC's source code. It is implemented as a patch to GCC. With GEM, a compiler extension is developed as a stand-alone program. It is compiled into a dynamically-linked module which is specified as the command line argument when GCC is invoked. GCC loads the module and calls its initialization function. The module then registers its hooks that are call-back functions in GCC.
In addition to the compiler hooks, GEM provides macros and functions that simplify extension development. In this chapter we will first introduce the hooks that GEM framework adds to GCC. Then we describe the typical issues in extension programming.
The project home page is at http://research.alexeysmirnov.name/gem
GEM adds several hooks throughout GCC source code. New hooks are added to GEM as necessary.
* Hook gem_handle_option to function handle_option which processes each command line option. The hook takes the current option as its argument. If the hook returns value GEM_RETURN then GCC ignores the option.
* Hook gem_c_common_nodes_and_builtins which is called after all standard types are created. The GCC extension can create additional types.
* Hook gem_macro_name allows one to save the name of the macro being defined. Another GEM hook gem_macro_def is called when the macro definition is parsed. Using the macro name of the new macro definition it is possible to re-define the macro. This hook is added to function create_iso_definition.
* Hooks gem_start_decl and gem_start_function are called when a function or variable declaration/definition starts.
* Hook gem_build_function_call allows one to modify the name and the arguments of a function call.
* Hook gem_finish_function is inserted to finish_function which is called from from grammar file. The compiler extension receives the function body of the function before it is translated into RTL.
* Hooks gem_output_asm_insn and gem_final_start_function are added to function output_asm_insn which is called for each instruction of the assembly code and function final_start_function called when the assembly code is written to the file, respectively. The former hook receives the text that is written to the file which allows it to modify the output. The latter hook can modify function's prolog.
| Take home: | GEM hooks are defined mostly at the AST level. A few hooks are defined at the assembly level. The new hooks are added as necessary. |

### Traversing an AST

When the function's AST is constructed one can instrument it. GEM's gem_finish_function hook receives the AST of a function. The idea is to traverse the AST and instrument the AST nodes as necessary. Function walk_tree takes the AST, the callback function, the optional data, NULL by default, and the walk_subtrees parameter, NULL by default. The callback function is called for each node of the AST before the operands are traversed. If the callback function modifies the walk_subtree variable then the operands are not processed.
The following code demonstrates the idea:
| Take home: | Function walk_tree traverses an AST applying user-defined callback function to each tree node. |

### Instrumenting an AST

In this section we describe functions that create new tree nodes and how to add the new nodes to an AST.

#### Building Tree Nodes

The walk_tree callback function can instrument the AST. Functions build1 and build construct new tree nodes. The former function takes one operand, the latter one takes more then one operand. The following code computes the address of the operand, same as '&' C operator:
The following example refers to an array element arr[0]:
The following example builds an integer constant:
Building a string constant is more difficult. The following example demonstrates the idea:
To build a function call one needs to find the function's declaration and build the list of arguments. Then the CALL_EXPR is constructed:
If you want to build a list of statements { stmt1; stmt2; ... }, then you need to use function append_to_statement_list:

#### Adding Nodes to a Tree

GCC 4.1 has an interface that allows one to add a chain of nodes into another chain of nodes implemented in file tree-iterator.c. Functions tsi_start and tsi_last create a tree statement iterator and assigns it to the first or the last tree in the list, respectively. Functions tsi_link_before and tsi_link_after link a statement using the iterator either before or after the current statement. There is also function append_to_statement_list that adds a node to a list. If the specified list argument is NULL_TREE then a new statement list is allocated.

#### Building Function and Variable Declarations

A global declaration is added in hook gem_c_common_nodes_and_builtins. In this following example we build a structure type and create a global variable of this type. The structure has a field of type unsigned int and a function pointer field.

### When to Instrument

In this section we will describe when each of GEM hooks is used.
* Add new function and type declarations in hook gem_c_common_nodes_and_builtins.
* Instrument an AST after it is parsed in hook gem_finish_function.
* Modify attributes of a declaration in hooks gem_start_decl and gem_finish_decl. Let us say we would like to replace local array declarations char arr[10] with a heap array char *arr=(char*)malloc(10);
* Replace function call with a proxy function

### Function Prolog/Epilog

The assembly instructions are written to the assembly file:
| Take home: | Assembly instructions are added to function prolog and epilog using hooks gem_output_asm_insn and gem_final_start_function. |
| Prev: Stackguard | GEM Framework | Next: Function Overloading in C |