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