fadbbfd5335980cb26a2d22ea63e7cec20152b23
[libfirm] / ir / ir / irargs.c
1 /*
2  * Copyright (C) 1995-2008 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  * @version  $Id$
25  */
26 #include "config.h"
27
28 #include "irargs_t.h"
29
30 #include <ctype.h>
31
32 #include "bitset.h"
33 #include "lc_printf.h"
34 #include "firm_common.h"
35 #include "irnode_t.h"
36 #include "entity_t.h"
37 #include "irloop_t.h"
38 #include "tv_t.h"
39 #include "dbginfo_t.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 = arg->v_ptr;
69         unsigned  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 = 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 = arg->v_ptr;
120         firm_kind *obj = X;
121         int i, n;
122         ir_node *block;
123         char add[64];
124         char buf[256];
125         char tv_buf[256];
126         ir_entity *ent;
127
128         buf[0] = '\0';
129         add[0] = '\0';
130
131         if (! X)
132                 strncpy(buf, "(null)", sizeof(buf));
133         else {
134                 switch (*obj) {
135         case k_BAD:
136                 snprintf(buf, sizeof(buf), "BAD");
137                 snprintf(add, sizeof(add), "[%p]", X);
138                 break;
139         case k_entity:
140                 snprintf(buf, sizeof(buf), "%s%s", A("ent"),
141                         isupper(occ->conversion) ? get_entity_ld_name_ex(X): get_entity_name(X));
142                 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
143                 break;
144         case k_type: {
145                 char type_name[256];
146                 ir_print_type(type_name, sizeof(type_name), X);
147                 snprintf(buf, sizeof(buf), "%s%s:%s", A("type"), get_type_tpop_name(X),
148                          type_name);
149                 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
150                 break;
151         }
152         case k_ir_graph:
153                 if (X == get_const_code_irg())
154                         snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
155                 else
156                         snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
157                 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
158                 break;
159         case k_ir_node:
160                 switch (occ->conversion) {
161                 case 'B':
162                         block = !is_Block(X) ? get_nodes_block(X) : X;
163                         snprintf(buf, sizeof(buf), "%s%s %s", A("irn"),
164                                  get_irn_opname(block), get_mode_name(get_irn_mode(block)));
165                         snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
166                         break;
167                 case 'N':
168                         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(X));
169                         break;
170                 default:
171                         if (is_Const(X)) {
172                                 ir_tarval *tv = get_Const_tarval(X);
173                                 if (tv)
174                                         tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
175                                 else
176                                         strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
177                                 snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(X),
178                                         get_mode_name(get_irn_mode(X)), tv_buf);
179                         } else if (is_SymConst_addr_ent(X)) {
180                                 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(X),
181                                 get_mode_name(get_irn_mode(X)), get_entity_name(get_SymConst_entity(X)));
182                         } else if (is_Sel(X)) {
183                                 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(X),
184                                 get_mode_name(get_irn_mode(X)), get_entity_name(get_Sel_entity(X)));
185                         } else {
186                                 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(X),
187                                 get_mode_name(get_irn_mode(X)));
188                         }
189                         snprintf(add, sizeof(add), "[%ld:%u]", get_irn_node_nr(X), get_irn_idx(X));
190                 }
191                 break;
192         case k_ir_mode:
193                 snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(X));
194                 break;
195         case k_tarval:
196                 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
197                 snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
198                 break;
199         case k_ir_loop:
200                 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
201                 break;
202         case k_ir_op:
203                 snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(X));
204                 break;
205         case k_ir_compound_graph_path:
206                 n = get_compound_graph_path_length(X);
207
208                 for (i = 0; i < n; ++i) {
209                         ent = get_compound_graph_path_node(X, i);
210
211                         strncat(buf, ".", sizeof(buf)-1);
212                         strncat(buf, get_entity_name(ent), sizeof(buf)-1);
213                         if (is_Array_type(get_entity_owner(ent))) {
214                                 snprintf(add, sizeof(add), "[%d]",
215                                         get_compound_graph_path_array_index(X, i));
216                                 strncat(buf, add, sizeof(buf)-1);
217                         }
218                 }
219                 add[0] = '\0';
220                 break;
221         case k_ir_extblk:
222                 snprintf(buf, sizeof(buf), "ExtBlock");
223                 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(get_extbb_leader(X)));
224                 break;
225
226         default:
227                 snprintf(buf, sizeof(buf), "UNKWN");
228                 snprintf(add, sizeof(add), "[%p]", X);
229                 }
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
237 #undef A
238 }
239
240 /**
241  * emit an ident
242  */
243 static int firm_emit_ident(lc_appendable_t *app,
244     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
245 {
246         ident *id = (ident *)arg->v_ptr;
247         const char *p = id ? get_id_str(id) : "(null)";
248
249         return lc_arg_append(app, occ, p, strlen(p));
250 }
251
252 /**
253  * Emit indent.
254  */
255 static int firm_emit_indent(lc_appendable_t *app,
256     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
257 {
258         int i;
259         int width  = occ->width > 0 ? occ->width : 1;
260         int amount = arg->v_int * width;
261
262         for (i = 0; i < amount; ++i)
263                 lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
264
265         return amount;
266 }
267
268 /**
269  * Emit pnc.
270  */
271 static int firm_emit_pnc(lc_appendable_t *app,
272     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
273 {
274         int value = arg->v_int;
275         const char *p = get_pnc_string(value);
276
277         return lc_arg_append(app, occ, p, strlen(p));
278 }
279
280 lc_arg_env_t *firm_get_arg_env(void)
281 {
282
283         static lc_arg_env_t *env = NULL;
284
285         static lc_arg_handler_t firm_handler   = { firm_get_arg_type, firm_emit };
286         static lc_arg_handler_t ident_handler  = { firm_get_arg_type, firm_emit_ident };
287         static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
288         static lc_arg_handler_t pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
289         static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
290         static lc_arg_handler_t debug_handler  = { firm_get_arg_type, firm_emit_dbg };
291
292         static struct {
293                 const char *name;
294                 char letter;
295         } args[] = {
296 #define X(name, letter) {"firm:" name, letter}
297                 X("type",      't'),
298                 X("entity",    'e'),
299                 X("entity_ld", 'E'),
300                 X("tarval",    'T'),
301                 X("irn",       'n'),
302                 X("op",        'O'),
303                 X("irn_nr",    'N'),
304                 X("mode",      'm'),
305                 X("block",     'B'),
306                 X("cg_path",   'P'),
307 #undef X
308         };
309
310         size_t i;
311
312         if (env == NULL) {
313                 env = lc_arg_new_env();
314                 lc_arg_add_std(env);
315
316                 lc_arg_register(env, "firm", 'F', &firm_handler);
317                 for (i = 0; i < sizeof(args)/sizeof(args[0]); ++i)
318                         lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
319
320                 lc_arg_register(env, "firm:ident",    'I', &ident_handler);
321                 lc_arg_register(env, "firm:indent",   'D', &indent_handler);
322                 lc_arg_register(env, "firm:dbg_info", 'G', &debug_handler);
323                 lc_arg_register(env, "firm:bitset",   'B', &bitset_handler);
324                 lc_arg_register(env, "firm:pnc",      '=', &pnc_handler);
325         }
326
327         return env;
328 }