Do not mark the transformed as visited. It makes no sense at all.
[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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "firm_config.h"
31
32 #include "bitset.h"
33
34 #include <ctype.h>
35
36 #include "lc_printf.h"
37 #include "firm_common.h"
38 #include "irnode_t.h"
39 #include "entity_t.h"
40 #include "irloop_t.h"
41 #include "tv_t.h"
42 #include "dbginfo_t.h"
43
44 /**
45  * identify a firm object type
46  */
47 static int firm_get_arg_type(const lc_arg_occ_t *occ) {
48         (void) occ;
49         /* Firm objects are always pointer */
50         return lc_arg_type_ptr;
51 }
52
53 static int firm_get_arg_type_int(const lc_arg_occ_t *occ) {
54         (void) occ;
55         return lc_arg_type_int;
56 }
57
58
59 static int bitset_get_arg_type(const lc_arg_occ_t *occ) {
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         bitset_pos_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 = arg->v_ptr;
95         dbg_info *dbg = get_irn_dbg_info(irn);
96
97         buf[0] = '\0';
98         if (dbg && __dbg_info_snprint) {
99                 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
100                         buf[0] = '\0';
101         }
102         return lc_arg_append(app, occ, buf, strlen(buf));
103 }
104
105 /**
106  * Beware: do not set the entity ld_name
107  */
108 static const char *get_entity_ld_name_ex(ir_entity *ent) {
109         if (ent->ld_name)
110                 return get_entity_ld_name(ent);
111         return get_entity_name(ent);
112 }
113
114 /**
115  * emit a Firm object
116  */
117 static int firm_emit(lc_appendable_t *app,
118     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
119 {
120 #define A(s)    occ->flag_hash ? s " ": ""
121
122         void *X = arg->v_ptr;
123         firm_kind *obj = X;
124         int i, n;
125         ir_node *block;
126         char add[64];
127         char buf[256];
128         char tv_buf[256];
129         ir_entity *ent;
130
131         buf[0] = '\0';
132         add[0] = '\0';
133
134         if (! X)
135                 strncpy(buf, "(null)", sizeof(buf));
136         else {
137                 switch (*obj) {
138         case k_BAD:
139                 snprintf(buf, sizeof(buf), "BAD");
140                 snprintf(add, sizeof(add), "[%p]", X);
141                 break;
142         case k_entity:
143                 snprintf(buf, sizeof(buf), "%s%s", A("ent"),
144                         isupper(occ->conversion) ? get_entity_ld_name_ex(X): get_entity_name(X));
145                 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
146                 break;
147         case k_type:
148                 snprintf(buf, sizeof(buf), "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
149                 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
150                 break;
151         case k_ir_graph:
152                 if (X == get_const_code_irg())
153                         snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
154                 else
155                         snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
156                 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
157                 break;
158         case k_ir_node:
159                 switch (occ->conversion) {
160                 case 'B':
161                         block = is_no_Block(X) ? get_nodes_block(X) : X;
162                         snprintf(buf, sizeof(buf), "%s%s%s", A("irn"), get_irn_opname(block),
163                                 get_mode_name(get_irn_mode(block)));
164                         snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
165                         break;
166                 case 'N':
167                         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(X));
168                         break;
169                 default:
170                         if (is_Const(X)) {
171                                 tarval *tv = get_Const_tarval(X);
172                                 if (tv)
173                                         tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
174                                 else
175                                         strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
176                                 snprintf(buf, sizeof(buf), "%s%s%s<%s>", A("irn"), get_irn_opname(X),
177                                         get_mode_name(get_irn_mode(X)), tv_buf);
178                         }
179                         else
180                                 snprintf(buf, sizeof(buf), "%s%s%s", A("irn"), get_irn_opname(X),
181                                 get_mode_name(get_irn_mode(X)));
182                         snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
183                         }
184                         break;
185         case k_ir_mode:
186                 snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(X));
187                 break;
188         case k_tarval:
189                 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
190                 snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
191                 break;
192         case k_ir_loop:
193                 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
194                 break;
195         case k_ir_op:
196                 snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(X));
197                 break;
198         case k_ir_compound_graph_path:
199                 n = get_compound_graph_path_length(X);
200
201                 for (i = 0; i < n; ++i) {
202                         ent = get_compound_graph_path_node(X, i);
203
204                         strncat(buf, ".", sizeof(buf));
205                         strncat(buf, get_entity_name(ent), sizeof(buf));
206                         if (is_Array_type(get_entity_owner(ent))) {
207                                 snprintf(add, sizeof(add), "[%d]",
208                                         get_compound_graph_path_array_index(X, i));
209                                 strncat(buf, add, sizeof(buf));
210                         }
211                 }
212                 add[0] = '\0';
213                 break;
214         case k_ir_extblk:
215                 snprintf(buf, sizeof(buf), "ExtBlock");
216                 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(get_extbb_leader(X)));
217                 break;
218
219         default:
220                 snprintf(buf, sizeof(buf), "UNKWN");
221                 snprintf(add, sizeof(add), "[%p]", X);
222                 }
223         }
224
225         if (occ->flag_plus)
226                 strncat(buf, add, sizeof(buf));
227
228         return lc_arg_append(app, occ, buf, strlen(buf));
229
230 #undef A
231 }
232
233 /**
234  * emit an ident
235  */
236 static int firm_emit_ident(lc_appendable_t *app,
237     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
238 {
239         ident *id = (ident *)arg->v_ptr;
240         const char *p = id ? get_id_str(id) : "(null)";
241
242         return lc_arg_append(app, occ, p, strlen(p));
243 }
244
245 /**
246  * Emit indent.
247  */
248 static int firm_emit_indent(lc_appendable_t *app,
249     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
250 {
251         int i;
252         int width  = occ->width > 0 ? occ->width : 1;
253         int amount = arg->v_int * width;
254
255         for (i = 0; i < amount; ++i)
256                 lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
257
258         return amount;
259 }
260
261 /**
262  * Emit pnc.
263  */
264 static int firm_emit_pnc(lc_appendable_t *app,
265     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
266 {
267         int value = arg->v_int;
268         const char *p = get_pnc_string(value);
269
270         return lc_arg_append(app, occ, p, strlen(p));
271 }
272
273 lc_arg_env_t *firm_get_arg_env(void)
274 {
275
276         static lc_arg_env_t *env = NULL;
277
278         static lc_arg_handler_t firm_handler   = { firm_get_arg_type, firm_emit };
279         static lc_arg_handler_t ident_handler  = { firm_get_arg_type, firm_emit_ident };
280         static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
281         static lc_arg_handler_t pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
282         static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
283         static lc_arg_handler_t debug_handler  = { firm_get_arg_type, firm_emit_dbg };
284
285         static struct {
286                 const char *name;
287                 char letter;
288         } args[] = {
289 #define X(name, letter) {"firm:" name, letter}
290                 X("type",      't'),
291                 X("entity",    'e'),
292                 X("entity_ld", 'E'),
293                 X("tarval",    'T'),
294                 X("irn",       'n'),
295                 X("op",        'O'),
296                 X("irn_nr",    'N'),
297                 X("mode",      'm'),
298                 X("block",     'B'),
299                 X("cg_path",   'P'),
300 #undef X
301         };
302
303         size_t i;
304
305         if (env == NULL) {
306                 env = lc_arg_new_env();
307                 lc_arg_add_std(env);
308
309                 lc_arg_register(env, "firm", 'F', &firm_handler);
310                 for (i = 0; i < sizeof(args)/sizeof(args[0]); ++i)
311                         lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
312
313                 lc_arg_register(env, "firm:ident",    'I', &ident_handler);
314                 lc_arg_register(env, "firm:indent",   'D', &indent_handler);
315                 lc_arg_register(env, "firm:dbg_info", 'G', &debug_handler);
316                 lc_arg_register(env, "firm:bitset",   'B', &bitset_handler);
317                 lc_arg_register(env, "firm:pnc",      '=', &pnc_handler);
318         }
319
320         return env;
321 }