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