cleaned up doxygen comments
[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 #ifdef WITH_LIBCORE
31
32 #include "bitset.h"
33
34 #include <ctype.h>
35 #include <libcore/lc_printf.h>
36
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   /* Firm objects are always pointer */
49   return lc_arg_type_ptr;
50 }
51
52 static int firm_get_arg_type_int(const lc_arg_occ_t *occ) {
53   return lc_arg_type_int;
54 }
55
56
57 static int bitset_get_arg_type(const lc_arg_occ_t *occ) {
58   return lc_arg_type_ptr;
59 }
60
61 static int bitset_emit(lc_appendable_t *app,
62     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
63 {
64   int res = 2;
65   bitset_t *b = arg->v_ptr;
66   bitset_pos_t p;
67   char buf[32];
68   const char *prefix = "";
69
70   lc_arg_append(app, occ, "[", 1);
71   bitset_foreach(b, p) {
72     int n;
73
74     n = snprintf(buf, sizeof(buf), "%s%d", prefix, (int) p);
75     lc_arg_append(app, occ, buf, n);
76     prefix = ", ";
77     res += n;
78   }
79   lc_arg_append(app, occ, "]", 1);
80
81   return res;
82 }
83
84 /**
85  * emit an opaque Firm dbg_info object
86  */
87 static int firm_emit_dbg(lc_appendable_t *app,
88     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
89 {
90   char buf[1024];
91   ir_node *irn = arg->v_ptr;
92   dbg_info *dbg = get_irn_dbg_info(irn);
93
94   buf[0] = '\0';
95   if (dbg && __dbg_info_snprint) {
96     if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
97       buf[0] = '\0';
98   }
99   return lc_arg_append(app, occ, buf, strlen(buf));
100 }
101
102 /**
103  * Beware: do not set the entity ld_name
104  */
105 static const char *get_entity_ld_name_ex(ir_entity *ent) {
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       snprintf(buf, sizeof(buf), "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
146       snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
147       break;
148     case k_ir_graph:
149       if (X == get_const_code_irg())
150         snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
151       else
152         snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
153       snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
154       break;
155     case k_ir_node:
156       switch (occ->conversion) {
157       case 'B':
158         block = is_no_Block(X) ? get_nodes_block(X) : X;
159         snprintf(buf, sizeof(buf), "%s%s%s", A("irn"), get_irn_opname(block),
160             get_mode_name(get_irn_mode(block)));
161         snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
162         break;
163       case 'N':
164         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(X));
165         break;
166       default:
167         if (is_Const(X)) {
168           tarval *tv = get_Const_tarval(X);
169           if (tv)
170             tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
171           else
172             strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
173           snprintf(buf, sizeof(buf), "%s%s%s<%s>", A("irn"), get_irn_opname(X),
174             get_mode_name(get_irn_mode(X)), tv_buf);
175         }
176         else
177           snprintf(buf, sizeof(buf), "%s%s%s", A("irn"), get_irn_opname(X),
178             get_mode_name(get_irn_mode(X)));
179         snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
180       }
181       break;
182     case k_ir_mode:
183       snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(X));
184       break;
185     case k_tarval:
186       tarval_snprintf(tv_buf, sizeof(tv_buf), X);
187       snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
188       break;
189     case k_ir_loop:
190       snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
191       break;
192     case k_ir_op:
193       snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(X));
194       break;
195     case k_ir_compound_graph_path:
196       n = get_compound_graph_path_length(X);
197
198       for (i = 0; i < n; ++i) {
199         ent = get_compound_graph_path_node(X, i);
200
201         strncat(buf, ".", sizeof(buf));
202         strncat(buf, get_entity_name(ent), sizeof(buf));
203         if (is_Array_type(get_entity_owner(ent))) {
204           snprintf(add, sizeof(add), "[%d]",
205             get_compound_graph_path_array_index(X, i));
206           strncat(buf, add, sizeof(buf));
207         }
208       }
209       add[0] = '\0';
210       break;
211     case k_ir_extblk:
212       snprintf(buf, sizeof(buf), "ExtBlock");
213       snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(get_extbb_leader(X)));
214       break;
215
216     default:
217       snprintf(buf, sizeof(buf), "UNKWN");
218       snprintf(add, sizeof(add), "[%p]", X);
219     }
220   }
221
222   if (occ->flag_plus)
223         strncat(buf, add, sizeof(buf));
224
225   return lc_arg_append(app, occ, buf, strlen(buf));
226
227 #undef A
228 }
229
230 /**
231  * emit an ident
232  */
233 static int firm_emit_ident(lc_appendable_t *app,
234     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
235 {
236   ident *id = (ident *)arg->v_ptr;
237   const char *p = id ? get_id_str(id) : "(null)";
238
239   return lc_arg_append(app, occ, p, strlen(p));
240 }
241
242 /**
243  * Emit indent.
244  */
245 static int firm_emit_indent(lc_appendable_t *app,
246     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
247 {
248         int i;
249         int amount = arg->v_int * (occ->width > 0 ? occ->width : 1);
250
251         for(i = 0; i < amount; ++i)
252                 lc_appendable_chadd(app, ' ');
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 #define X(name, letter) {"firm:" name, letter}
272
273   static lc_arg_env_t *env = NULL;
274
275   static lc_arg_handler_t firm_handler   = { firm_get_arg_type, firm_emit };
276   static lc_arg_handler_t ident_handler  = { firm_get_arg_type, firm_emit_ident };
277   static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
278   static lc_arg_handler_t pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
279   static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
280   static lc_arg_handler_t debug_handler  = { firm_get_arg_type, firm_emit_dbg };
281
282   static struct {
283     const char *name;
284     char letter;
285   } args[] = {
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   };
297
298   int i;
299
300   if(env == NULL) {
301     env = lc_arg_new_env();
302     lc_arg_add_std(env);
303
304     lc_arg_register(env, "firm", 'F', &firm_handler);
305     for (i = 0; i < sizeof(args)/sizeof(args[0]); ++i)
306       lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
307
308     lc_arg_register(env, "firm:ident",    'I', &ident_handler);
309     lc_arg_register(env, "firm:indent",   '>', &indent_handler);
310     lc_arg_register(env, "firm:dbg_info", 'G', &debug_handler);
311     lc_arg_register(env, "firm:bitset",   'B', &bitset_handler);
312     lc_arg_register(env, "firm:pnc",      '=', &pnc_handler);
313   }
314
315   return env;
316 }
317
318 #endif /* WITH_LIBCORE */