HOOK_BACKEND added
[libfirm] / ir / ir / irhooks.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irhooks.c
4  * Purpose:     Generic hooks for various libFirm functions.
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (C) 1998-2005 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 /**
13  * @file irhooks.c
14  *
15  * Generic hooks for various libFirm functions.
16  *
17  * @author Michael Beck
18  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "irhooks.h"
24
25 #ifdef FIRM_ENABLE_HOOKS
26
27 /* the hooks */
28 hook_entry_t *hooks[hook_last];
29
30 /* register a hook */
31 void register_hook(hook_type_t hook, hook_entry_t *entry) {
32   /* check if a hook function is specified. It's a union, so no matter which one */
33   if (! entry->hook._hook_turn_into_id)
34     return;
35
36   entry->next = hooks[hook];
37   hooks[hook] = entry;
38 }
39
40 /* unregister a hook */
41 void unregister_hook(hook_type_t hook, hook_entry_t *entry) {
42   hook_entry_t *p;
43
44   if (hooks[hook] == entry) {
45     hooks[hook] = entry->next;
46     entry->next = NULL;
47     return;
48   }
49
50   for (p = hooks[hook]; p && p->next != entry; p = p->next);
51
52   if (p) {
53     p->next     = entry->next;
54     entry->next = NULL;
55   }
56 }
57
58
59 #else
60
61 void register_hook(hook_type_t hook, hook_entry_t *entry) {}
62 void unregister_hook(hook_type_t hook, hook_entry_t *entry) {}
63
64 #endif /* FIRM_ENABLE_HOOKS */
65
66 int firm_init_hooks(void) {
67   /* this strange code assures that both functions are available
68      in a shared library even if none of them is called.
69      Meanwhile not needed anymore but ... */
70   return (int)register_hook + (int)unregister_hook;
71 }