# GNU C Compiler Internals/GEM Framework 3 4
### 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.
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 string constant is more difficult. The following example demonstrates the idea:
To build a function call one needs to build the list of arguments. Then the CALL_EXPR is constructed:
The constructed tree node is added to the AST as a statement or as an expression. A statement is added to the linked list of statements using TREE_CHAIN:
Adding a tree node as an expression is the same as using '' C operator. The new expression is added as the first argument of the operator. The result of the operator is the result of its second argument:
If you want to add the call after t then you need to build a compound statement. This is equivalent to using curly brackets in C:
| Take home: | Use GCC's functions to build new nodes and add them to the AST, either as a statement or as an expression. |

### 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.

### 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 |