Moved the survive dce stuff to irgopt.[ch]
[libfirm] / ir / common / debug.h
1 /**
2  * Debug facility.
3  * @author Michael Beck, Sebastian Hack
4  * @date 15.12.2004
5  *
6  * $Id$
7  */
8
9 #ifndef _FIRM_DEBUG_H
10 #define _FIRM_DEBUG_H
11
12 #include "firm_config.h"
13
14 #ifdef WITH_LIBCORE
15
16 #define DBG(x) _LC_DBG(x)
17 #define DB(x)  _LC_DB(x)
18 #include <libcore/lc_debug.h>
19
20 /* use the newer debug implementation in libcore */
21 typedef lc_dbg_module_t firm_dbg_module_t;
22
23 extern firm_dbg_module_t *firm_dbg_register(const char *name);
24
25 #define firm_dbg_set_mask(module, mask) lc_dbg_set_mask(module, mask)
26 #define firm_dbg_get_mask(module)       lc_dbg_get_mask(module)
27 #define firm_dbg_set_file(module, file) lc_dbg_set_file(module, file)
28
29 #define LEVEL_DEFAULT    LC_LEVEL_DEFAULT
30 #define LEVEL_1          LC_LEVEL_1
31 #define LEVEL_2          LC_LEVEL_2
32 #define LEVEL_3          LC_LEVEL_3
33 #define LEVEL_4          LC_LEVEL_4
34 #define LEVEL_5          LC_LEVEL_5
35 #define SET_LEVEL_0      LC_SET_LEVEL_0
36 #define SET_LEVEL_1      LC_SET_LEVEL_1
37 #define SET_LEVEL_2      LC_SET_LEVEL_2
38 #define SET_LEVEL_3      LC_SET_LEVEL_3
39 #define SET_LEVEL_4      LC_SET_LEVEL_4
40 #define SET_LEVEL_5      LC_SET_LEVEL_5
41 #define SET_LEVEL_ALL    LC_SET_LEVEL_ALL
42
43 #else
44 /* use the builtin debug implementation */
45
46 #include <stdio.h>
47
48 enum firm_dbg_level_t {
49         LEVEL_DEFAULT = 0, /**< Prints always. Use with DBG(). */
50         LEVEL_1 = 1,
51         LEVEL_2 = 2,
52         LEVEL_3 = 4,
53         LEVEL_4 = 8,
54         LEVEL_5 = 16,
55
56   SET_LEVEL_0 = 0,   /**< use with firm_dbg_set_mask(). */
57         SET_LEVEL_1 = 1,
58         SET_LEVEL_2 = 3,
59         SET_LEVEL_3 = 7,
60         SET_LEVEL_4 = 15,
61         SET_LEVEL_5 = 31,
62   SET_LEVEL_ALL = SET_LEVEL_5
63 };
64
65 typedef struct _firm_dbg_module_t firm_dbg_module_t;
66
67 /* Internal function to the debug module. */
68 void *_firm_dbg_make_msg(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
69
70 /* Internal function to the debug module. */
71 void _firm_dbg_print_msg(const char *filename, int line, const char *func, void *data);
72
73 /* Internal function to the debug module. */
74 void _firm_dbg_print(const firm_dbg_module_t *mod, unsigned mask, const char *fmt, ...);
75
76 /**
77  * Register a module to the firm debug facility.
78  * If the module has already been registered, no new module is allocated
79  * but the handle is returned. By default, all messages go to @c stderr
80  * and the debug mask is set to 0, i.e. the module is muted.
81  * @param name The name of the module to register.
82  * @return The module handle.
83  */
84 firm_dbg_module_t *firm_dbg_register(const char *name);
85
86 /**
87  * Set the mask of a module.
88  * @param module The module.
89  * @param mask The new mask for the module.
90  */
91 void firm_dbg_set_mask(firm_dbg_module_t *module, unsigned mask);
92
93 /**
94  * Get the mask of a module.
95  * @param module The module handle.
96  * @return The mask currently used by the module.
97  */
98 unsigned firm_dbg_get_mask(const firm_dbg_module_t *module);
99
100 /**
101  * Set the output file of a module.
102  * @param module The module handle.
103  * @param file The new file to use by this handle.
104  */
105 void firm_dbg_set_file(firm_dbg_module_t *module, FILE *file);
106
107 #define _DBG_MAIN(func,args) \
108         _firm_dbg_print_msg(__FILE__, __LINE__, func, _firm_dbg_make_msg args)
109
110 /* If we have C99 use the __func__ variable for calling functions name. */
111 #if defined(__STD_VERSION__) && __STD_VERSION >= 199901L
112 #define _DBG(args)      _DBG_MAIN(__func__, args)
113 #else
114
115 /* Else, check for gcc and use the proprietary __FUNCTION__ macro. */
116 #ifdef __GNUC__
117 #define _DBG(args)      _DBG_MAIN(__FUNCTION__, args)
118 #else
119
120 /* Else go without the name of the calling function. */
121 #define _DBG(args)      _DBG_MAIN("", args)
122 #endif  /* __GNUC__ */
123 #endif
124
125 #define _DB(args) _firm_dbg_print args
126
127 /**
128  * Debug messages issued with this macro are always printed, even in
129  * retail versions.
130  * @see DBG()
131  */
132 #define DBG_RETAIL(args)                _DBG(args)
133 #define DB_RETAIL(args)           _DB(args)
134
135 #ifdef DEBUG_libfirm
136
137 /**
138  * Issue a debug message.
139  * @param args The arguments.
140  *
141  * The arguments is a list surrounded by parentheses. The items
142  * of the list are:
143  * - The module handle as returned by firm_dbg_register().
144  * - The debug mask that you want associate with this message.
145  * - A format string for the message to pass to ir_printf().
146  * - Further optional arguments are passed to ir_printf().
147  *
148  * The mask is anded against the module's mask. If both have some bits
149  * in common, the message is issued. If the given mask is 0, the message
150  * is always dumped regardless of the module's mask. You can also use
151  * the mask in a level based manner, see firm_dbg_level_t.
152  *
153  * Here is an example:
154  * @code
155  * DBG((my_mod, MASK_ERR, "ir node %n is not green", node))
156  * ...
157  * DBG((my_mod, LEVEL_DEFAULT, "entity %e has type %t", ent, type))
158  * @endcode
159  */
160 #define DBG(args)                                       _DBG(args)
161 #define DB(args)                                          _DB(args)
162
163 #else
164 #define DBG(args)
165 #define DB(args)
166 #endif /* DEBUG_libfirm */
167
168 #endif /* WITH_LIBCORE */
169
170 #endif /* _FIRM_DEBUG_H */