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