make code C++ warning clean (again)
[libfirm] / ir / ir / irargs.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Support for libcore IR object output.
23  * @author   Sebastian Hack
24  */
25 #include "config.h"
26
27 #include "irargs_t.h"
28
29 #include <ctype.h>
30
31 #include "bitset.h"
32 #include "lc_printf.h"
33 #include "firm_common.h"
34 #include "irnode_t.h"
35 #include "entity_t.h"
36 #include "irloop_t.h"
37 #include "tv_t.h"
38 #include "dbginfo_t.h"
39 #include "util.h"
40
41 /**
42  * identify a firm object type
43  */
44 static int firm_get_arg_type(const lc_arg_occ_t *occ)
45 {
46         (void) occ;
47         /* Firm objects are always pointer */
48         return lc_arg_type_ptr;
49 }
50
51 static int firm_get_arg_type_int(const lc_arg_occ_t *occ)
52 {
53         (void) occ;
54         return lc_arg_type_int;
55 }
56
57
58 static int bitset_get_arg_type(const lc_arg_occ_t *occ)
59 {
60         (void) occ;
61         return lc_arg_type_ptr;
62 }
63
64 static int bitset_emit(lc_appendable_t *app,
65     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
66 {
67         int res = 2;
68         bitset_t *b = (bitset_t*)arg->v_ptr;
69         size_t  p;
70         char buf[32];
71         const char *prefix = "";
72
73         lc_arg_append(app, occ, "[", 1);
74         bitset_foreach(b, p) {
75                 int n;
76
77                 n = snprintf(buf, sizeof(buf), "%s%d", prefix, (int) p);
78                 lc_arg_append(app, occ, buf, n);
79                 prefix = ", ";
80                 res += n;
81         }
82         lc_arg_append(app, occ, "]", 1);
83
84         return res;
85 }
86
87 /**
88  * emit an opaque Firm dbg_info object
89  */
90 static int firm_emit_dbg(lc_appendable_t *app,
91     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
92 {
93         char buf[1024];
94         ir_node *irn = (ir_node*)arg->v_ptr;
95         dbg_info *dbg = get_irn_dbg_info(irn);
96
97         ir_dbg_info_snprint(buf, sizeof(buf), dbg);
98         return lc_arg_append(app, occ, buf, strlen(buf));
99 }
100
101 /**
102  * Beware: do not set the entity ld_name
103  */
104 static const char *get_entity_ld_name_ex(ir_entity *ent)
105 {
106         if (ent->ld_name)
107                 return get_entity_ld_name(ent);
108         return get_entity_name(ent);
109 }
110
111 /**
112  * emit a Firm object
113  */
114 static int firm_emit(lc_appendable_t *app,
115     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
116 {
117 #define A(s)    occ->flag_hash ? s " ": ""
118
119         void *X = (void*)arg->v_ptr;
120         firm_kind *obj = (firm_kind*)X;
121         ir_node *block;
122         char add[64];
123         char buf[256];
124         char tv_buf[256];
125
126         buf[0] = '\0';
127         add[0] = '\0';
128
129         if (X == NULL) {
130                 return lc_arg_append(app, occ, "(null)", 6);
131         }
132
133         switch (*obj) {
134         case k_BAD:
135                 snprintf(buf, sizeof(buf), "BAD");
136                 snprintf(add, sizeof(add), "[%p]", X);
137                 break;
138         case k_entity: {
139                 ir_entity *entity = (ir_entity*)X;
140                 snprintf(buf, sizeof(buf), "%s%s", A("ent"),
141                         isupper((unsigned char)occ->conversion) ? get_entity_ld_name_ex(entity): get_entity_name(entity));
142                 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(entity));
143                 break;
144         }
145         case k_type: {
146                 ir_type *type = (ir_type*)X;
147                 char type_name[256];
148                 ir_print_type(type_name, sizeof(type_name), type);
149                 snprintf(buf, sizeof(buf), "%s%s:%s", A("type"),
150                          get_type_tpop_name(type), type_name);
151                 snprintf(add, sizeof(add), "[%ld]", get_type_nr(type));
152                 break;
153         }
154         case k_ir_graph: {
155                 ir_graph *irg = (ir_graph*)X;
156                 if (irg == get_const_code_irg())
157                         snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
158                 else
159                         snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(irg)));
160                 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(irg));
161                 break;
162         }
163         case k_ir_node: {
164                 ir_node *node = (ir_node*)X;
165                 switch (occ->conversion) {
166                 case 'B':
167                         block = !is_Block(node) ? get_nodes_block(node) : node;
168                         snprintf(buf, sizeof(buf), "%s%s %s", A("irn"),
169                                  get_irn_opname(block), get_mode_name(get_irn_mode(block)));
170                         snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
171                         break;
172                 case 'N':
173                         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(node));
174                         break;
175                 default:
176                         if (is_Const(node)) {
177                                 ir_tarval *tv = get_Const_tarval(node);
178                                 if (tv)
179                                         tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
180                                 else
181                                         strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
182                                 snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(node),
183                                         get_mode_name(get_irn_mode(node)), tv_buf);
184                         } else if (is_SymConst_addr_ent(node)) {
185                                 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
186                                 get_mode_name(get_irn_mode(node)), get_entity_name(get_SymConst_entity(node)));
187                         } else if (is_Sel(node)) {
188                                 ir_entity *entity = get_Sel_entity(node);
189                                 const char *entity_name = "(null)";
190                                 if (entity != NULL)
191                                         entity_name = get_entity_name(entity);
192                                 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
193                                 get_mode_name(get_irn_mode(node)), entity_name);
194                         } else if (is_Cmp(node)) {
195                                 ir_relation relation = get_Cmp_relation(node);
196                                 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node), get_relation_string(relation));
197                         } else {
198                                 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node),
199                                 get_mode_name(get_irn_mode(node)));
200                         }
201                         snprintf(add, sizeof(add), "[%ld:%u]", get_irn_node_nr(node), get_irn_idx(node));
202                 }
203                 break;
204         }
205         case k_ir_mode: {
206                 ir_mode *mode = (ir_mode*)X;
207                 snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(mode));
208                 break;
209         }
210         case k_tarval: {
211                 ir_tarval *tarval = (ir_tarval*)X;
212                 tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
213                 snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
214                 break;
215         }
216         case k_ir_loop: {
217                 ir_loop *loop = (ir_loop*)X;
218                 snprintf(buf, sizeof(buf), "loop[%ld:%u]", get_loop_loop_nr(loop), get_loop_depth(loop));
219                 break;
220         }
221         case k_ir_op: {
222                 ir_op *op = (ir_op*)X;
223                 snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(op));
224                 break;
225         }
226
227         default:
228                 snprintf(buf, sizeof(buf), "UNKWN");
229                 snprintf(add, sizeof(add), "[%p]", X);
230         }
231
232         if (occ->flag_plus)
233                 strncat(buf, add, sizeof(buf)-1);
234
235         return lc_arg_append(app, occ, buf, strlen(buf));
236 #undef A
237 }
238
239 /**
240  * emit an ident
241  */
242 static int firm_emit_ident(lc_appendable_t *app,
243     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
244 {
245         ident *id = (ident *)arg->v_ptr;
246         const char *p = id ? get_id_str(id) : "(null)";
247
248         return lc_arg_append(app, occ, p, strlen(p));
249 }
250
251 /**
252  * Emit indent.
253  */
254 static int firm_emit_indent(lc_appendable_t *app,
255     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
256 {
257         int i;
258         int width  = occ->width > 0 ? occ->width : 1;
259         int amount = arg->v_int * width;
260
261         for (i = 0; i < amount; ++i)
262                 lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
263
264         return amount;
265 }
266
267 /**
268  * Emit pnc.
269  */
270 static int firm_emit_pnc(lc_appendable_t *app,
271     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
272 {
273         ir_relation value = (ir_relation)arg->v_int;
274         const char *p = get_relation_string(value);
275
276         return lc_arg_append(app, occ, p, strlen(p));
277 }
278
279 lc_arg_env_t *firm_get_arg_env(void)
280 {
281
282         static lc_arg_env_t *env = NULL;
283
284         static lc_arg_handler_t firm_handler   = { firm_get_arg_type, firm_emit };
285         static lc_arg_handler_t ident_handler  = { firm_get_arg_type, firm_emit_ident };
286         static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
287         static lc_arg_handler_t pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
288         static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
289         static lc_arg_handler_t debug_handler  = { firm_get_arg_type, firm_emit_dbg };
290
291         static struct {
292                 const char *name;
293                 char letter;
294         } args[] = {
295                 {"firm:type",      't'},
296                 {"firm:entity",    'e'},
297                 {"firm:entity_ld", 'E'},
298                 {"firm:tarval",    'T'},
299                 {"firm:irn",       'n'},
300                 {"firm:op",        'O'},
301                 {"firm:irn_nr",    'N'},
302                 {"firm:mode",      'm'},
303                 {"firm:block",     'B'},
304         };
305
306         size_t i;
307
308         if (env == NULL) {
309                 env = lc_arg_new_env();
310                 lc_arg_add_std(env);
311
312                 lc_arg_register(env, "firm", 'F', &firm_handler);
313                 for (i = 0; i != ARRAY_SIZE(args); ++i)
314                         lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
315
316                 lc_arg_register(env, "firm:ident",    'I', &ident_handler);
317                 lc_arg_register(env, "firm:indent",   'D', &indent_handler);
318                 lc_arg_register(env, "firm:dbg_info", 'G', &debug_handler);
319                 lc_arg_register(env, "firm:bitset",   'B', &bitset_handler);
320                 lc_arg_register(env, "firm:pnc",      '=', &pnc_handler);
321         }
322
323         return env;
324 }