Write and read FIRM profiling information in little-endian format.
[libfirm] / ir / ir / irargs.c
1 /*
2  * Copyright (C) 1995-2011 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  */
25 #include "config.h"
26
27 #include "irargs_t.h"
28
29 #include <ctype.h>
30
31 #include "bitset.h"
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 {
45         (void) occ;
46         /* Firm objects are always pointer */
47         return lc_arg_type_ptr;
48 }
49
50 static int firm_get_arg_type_int(const lc_arg_occ_t *occ)
51 {
52         (void) occ;
53         return lc_arg_type_int;
54 }
55
56
57 static int bitset_get_arg_type(const lc_arg_occ_t *occ)
58 {
59         (void) occ;
60         return lc_arg_type_ptr;
61 }
62
63 static int bitset_emit(lc_appendable_t *app,
64     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
65 {
66         int res = 2;
67         bitset_t *b = (bitset_t*)arg->v_ptr;
68         size_t  p;
69         char buf[32];
70         const char *prefix = "";
71
72         lc_arg_append(app, occ, "[", 1);
73         bitset_foreach(b, p) {
74                 int n;
75
76                 n = snprintf(buf, sizeof(buf), "%s%d", prefix, (int) p);
77                 lc_arg_append(app, occ, buf, n);
78                 prefix = ", ";
79                 res += n;
80         }
81         lc_arg_append(app, occ, "]", 1);
82
83         return res;
84 }
85
86 /**
87  * emit an opaque Firm dbg_info object
88  */
89 static int firm_emit_dbg(lc_appendable_t *app,
90     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
91 {
92         char buf[1024];
93         ir_node *irn = (ir_node*)arg->v_ptr;
94         dbg_info *dbg = get_irn_dbg_info(irn);
95
96         ir_dbg_info_snprint(buf, sizeof(buf), dbg);
97         return lc_arg_append(app, occ, buf, strlen(buf));
98 }
99
100 /**
101  * Beware: do not set the entity ld_name
102  */
103 static const char *get_entity_ld_name_ex(ir_entity *ent)
104 {
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 = (void*)arg->v_ptr;
119         firm_kind *obj = (firm_kind*)X;
120         size_t 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 == NULL) {
131                 return lc_arg_append(app, occ, "(null)", 6);
132         }
133
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                 ir_entity *entity = (ir_entity*)X;
141                 snprintf(buf, sizeof(buf), "%s%s", A("ent"),
142                         isupper((unsigned char)occ->conversion) ? get_entity_ld_name_ex(entity): get_entity_name(entity));
143                 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(entity));
144                 break;
145         }
146         case k_type: {
147                 ir_type *type = (ir_type*)X;
148                 char type_name[256];
149                 ir_print_type(type_name, sizeof(type_name), type);
150                 snprintf(buf, sizeof(buf), "%s%s:%s", A("type"),
151                          get_type_tpop_name(type), type_name);
152                 snprintf(add, sizeof(add), "[%ld]", get_type_nr(type));
153                 break;
154         }
155         case k_ir_graph: {
156                 ir_graph *irg = (ir_graph*)X;
157                 if (irg == get_const_code_irg())
158                         snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
159                 else
160                         snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(irg)));
161                 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(irg));
162                 break;
163         }
164         case k_ir_node: {
165                 ir_node *node = (ir_node*)X;
166                 switch (occ->conversion) {
167                 case 'B':
168                         block = !is_Block(node) ? get_nodes_block(node) : node;
169                         snprintf(buf, sizeof(buf), "%s%s %s", A("irn"),
170                                  get_irn_opname(block), get_mode_name(get_irn_mode(block)));
171                         snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
172                         break;
173                 case 'N':
174                         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(node));
175                         break;
176                 default:
177                         if (is_Const(node)) {
178                                 ir_tarval *tv = get_Const_tarval(node);
179                                 if (tv)
180                                         tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
181                                 else
182                                         strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
183                                 snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(node),
184                                         get_mode_name(get_irn_mode(node)), tv_buf);
185                         } else if (is_SymConst_addr_ent(node)) {
186                                 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
187                                 get_mode_name(get_irn_mode(node)), get_entity_name(get_SymConst_entity(node)));
188                         } else if (is_Sel(node)) {
189                                 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
190                                 get_mode_name(get_irn_mode(node)), get_entity_name(get_Sel_entity(node)));
191                         } else if (is_Cmp(node)) {
192                                 ir_relation relation = get_Cmp_relation(node);
193                                 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node), get_relation_string(relation));
194                         } else {
195                                 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node),
196                                 get_mode_name(get_irn_mode(node)));
197                         }
198                         snprintf(add, sizeof(add), "[%ld:%u]", get_irn_node_nr(node), get_irn_idx(node));
199                 }
200                 break;
201         }
202         case k_ir_mode: {
203                 ir_mode *mode = (ir_mode*)X;
204                 snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(mode));
205                 break;
206         }
207         case k_tarval: {
208                 ir_tarval *tarval = (ir_tarval*)X;
209                 tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
210                 snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
211                 break;
212         }
213         case k_ir_loop: {
214                 ir_loop *loop = (ir_loop*)X;
215                 snprintf(buf, sizeof(buf), "loop[%ld:%u]", get_loop_loop_nr(loop), get_loop_depth(loop));
216                 break;
217         }
218         case k_ir_op: {
219                 ir_op *op = (ir_op*)X;
220                 snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(op));
221                 break;
222         }
223         case k_ir_compound_graph_path: {
224                 compound_graph_path *path = (compound_graph_path*)X;
225                 n = get_compound_graph_path_length(path);
226
227                 for (i = 0; i < n; ++i) {
228                         ent = get_compound_graph_path_node(path, i);
229
230                         strncat(buf, ".", sizeof(buf)-1);
231                         strncat(buf, get_entity_name(ent), sizeof(buf)-1);
232                         if (is_Array_type(get_entity_owner(ent))) {
233                                 snprintf(add, sizeof(add), "[%ld]",
234                                         get_compound_graph_path_array_index(path, i));
235                                 strncat(buf, add, sizeof(buf)-1);
236                         }
237                 }
238                 add[0] = '\0';
239                 break;
240         }
241         case k_ir_extblk: {
242                 ir_extblk *extblk = (ir_extblk*)X;
243                 snprintf(buf, sizeof(buf), "ExtBlock");
244                 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(get_extbb_leader(extblk)));
245                 break;
246         }
247
248         default:
249                 snprintf(buf, sizeof(buf), "UNKWN");
250                 snprintf(add, sizeof(add), "[%p]", X);
251         }
252
253         if (occ->flag_plus)
254                 strncat(buf, add, sizeof(buf)-1);
255
256         return lc_arg_append(app, occ, buf, strlen(buf));
257 #undef A
258 }
259
260 /**
261  * emit an ident
262  */
263 static int firm_emit_ident(lc_appendable_t *app,
264     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
265 {
266         ident *id = (ident *)arg->v_ptr;
267         const char *p = id ? get_id_str(id) : "(null)";
268
269         return lc_arg_append(app, occ, p, strlen(p));
270 }
271
272 /**
273  * Emit indent.
274  */
275 static int firm_emit_indent(lc_appendable_t *app,
276     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
277 {
278         int i;
279         int width  = occ->width > 0 ? occ->width : 1;
280         int amount = arg->v_int * width;
281
282         for (i = 0; i < amount; ++i)
283                 lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
284
285         return amount;
286 }
287
288 /**
289  * Emit pnc.
290  */
291 static int firm_emit_pnc(lc_appendable_t *app,
292     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
293 {
294         int value = arg->v_int;
295         const char *p = get_relation_string(value);
296
297         return lc_arg_append(app, occ, p, strlen(p));
298 }
299
300 lc_arg_env_t *firm_get_arg_env(void)
301 {
302
303         static lc_arg_env_t *env = NULL;
304
305         static lc_arg_handler_t firm_handler   = { firm_get_arg_type, firm_emit };
306         static lc_arg_handler_t ident_handler  = { firm_get_arg_type, firm_emit_ident };
307         static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
308         static lc_arg_handler_t pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
309         static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
310         static lc_arg_handler_t debug_handler  = { firm_get_arg_type, firm_emit_dbg };
311
312         static struct {
313                 const char *name;
314                 char letter;
315         } args[] = {
316                 {"firm:type",      't'},
317                 {"firm:entity",    'e'},
318                 {"firm:entity_ld", 'E'},
319                 {"firm:tarval",    'T'},
320                 {"firm:irn",       'n'},
321                 {"firm:op",        'O'},
322                 {"firm:irn_nr",    'N'},
323                 {"firm:mode",      'm'},
324                 {"firm:block",     'B'},
325                 {"firm:cg_path",   'P'},
326         };
327
328         size_t i;
329
330         if (env == NULL) {
331                 env = lc_arg_new_env();
332                 lc_arg_add_std(env);
333
334                 lc_arg_register(env, "firm", 'F', &firm_handler);
335                 for (i = 0; i < sizeof(args)/sizeof(args[0]); ++i)
336                         lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
337
338                 lc_arg_register(env, "firm:ident",    'I', &ident_handler);
339                 lc_arg_register(env, "firm:indent",   'D', &indent_handler);
340                 lc_arg_register(env, "firm:dbg_info", 'G', &debug_handler);
341                 lc_arg_register(env, "firm:bitset",   'B', &bitset_handler);
342                 lc_arg_register(env, "firm:pnc",      '=', &pnc_handler);
343         }
344
345         return env;
346 }