2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Support for libcore IR object output.
23 * @author Sebastian Hack
32 #include "lc_printf.h"
33 #include "firm_common.h"
38 #include "dbginfo_t.h"
42 * identify a firm object type
44 static int firm_get_arg_type(const lc_arg_occ_t *occ)
47 /* Firm objects are always pointer */
48 return lc_arg_type_ptr;
51 static int firm_get_arg_type_int(const lc_arg_occ_t *occ)
54 return lc_arg_type_int;
58 static int bitset_get_arg_type(const lc_arg_occ_t *occ)
61 return lc_arg_type_ptr;
64 static int bitset_emit(lc_appendable_t *app,
65 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
68 bitset_t *b = (bitset_t*)arg->v_ptr;
70 const char *prefix = "";
72 lc_arg_append(app, occ, "[", 1);
73 bitset_foreach(b, p) {
76 n = snprintf(buf, sizeof(buf), "%s%d", prefix, (int) p);
77 lc_arg_append(app, occ, buf, n);
81 lc_arg_append(app, occ, "]", 1);
87 * emit an opaque Firm dbg_info object
89 static int firm_emit_dbg(lc_appendable_t *app,
90 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
93 ir_node *irn = (ir_node*)arg->v_ptr;
94 dbg_info *dbg = get_irn_dbg_info(irn);
96 ir_dbg_info_snprint(buf, sizeof(buf), dbg);
97 return lc_arg_append(app, occ, buf, strlen(buf));
101 * Beware: do not set the entity ld_name
103 static const char *get_entity_ld_name_ex(ir_entity *ent)
106 return get_entity_ld_name(ent);
107 return get_entity_name(ent);
113 static int firm_emit(lc_appendable_t *app,
114 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
116 #define A(s) occ->flag_hash ? s " ": ""
118 void *X = (void*)arg->v_ptr;
119 firm_kind *obj = (firm_kind*)X;
129 return lc_arg_append(app, occ, "(null)", 6);
134 snprintf(buf, sizeof(buf), "BAD");
135 snprintf(add, sizeof(add), "[%p]", X);
138 ir_entity *entity = (ir_entity*)X;
139 snprintf(buf, sizeof(buf), "%s%s", A("ent"),
140 isupper((unsigned char)occ->conversion) ? get_entity_ld_name_ex(entity): get_entity_name(entity));
141 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(entity));
145 ir_type *type = (ir_type*)X;
147 ir_print_type(type_name, sizeof(type_name), type);
148 snprintf(buf, sizeof(buf), "%s%s:%s", A("type"),
149 get_type_tpop_name(type), type_name);
150 snprintf(add, sizeof(add), "[%ld]", get_type_nr(type));
154 ir_graph *irg = (ir_graph*)X;
155 if (irg == get_const_code_irg())
156 snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
158 snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(irg)));
159 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(irg));
163 ir_node *node = (ir_node*)X;
164 switch (occ->conversion) {
166 block = !is_Block(node) ? get_nodes_block(node) : node;
167 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"),
168 get_irn_opname(block), get_mode_name(get_irn_mode(block)));
169 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
172 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(node));
175 if (is_Const(node)) {
176 ir_tarval *tv = get_Const_tarval(node);
178 tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
180 strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
181 snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(node),
182 get_mode_name(get_irn_mode(node)), tv_buf);
183 } else if (is_SymConst_addr_ent(node)) {
184 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
185 get_mode_name(get_irn_mode(node)), get_entity_name(get_SymConst_entity(node)));
186 } else if (is_Sel(node)) {
187 ir_entity *entity = get_Sel_entity(node);
188 const char *entity_name = "(null)";
190 entity_name = get_entity_name(entity);
191 snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
192 get_mode_name(get_irn_mode(node)), entity_name);
193 } else if (is_Cmp(node)) {
194 ir_relation relation = get_Cmp_relation(node);
195 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node), get_relation_string(relation));
197 snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node),
198 get_mode_name(get_irn_mode(node)));
200 snprintf(add, sizeof(add), "[%ld:%u]", get_irn_node_nr(node), get_irn_idx(node));
205 ir_mode *mode = (ir_mode*)X;
206 snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(mode));
210 ir_tarval *tarval = (ir_tarval*)X;
211 tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
212 snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
216 ir_loop *loop = (ir_loop*)X;
217 snprintf(buf, sizeof(buf), "loop[%ld:%u]", get_loop_loop_nr(loop), get_loop_depth(loop));
221 ir_op *op = (ir_op*)X;
222 snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(op));
227 snprintf(buf, sizeof(buf), "UNKWN");
228 snprintf(add, sizeof(add), "[%p]", X);
232 strncat(buf, add, sizeof(buf)-1);
234 return lc_arg_append(app, occ, buf, strlen(buf));
241 static int firm_emit_ident(lc_appendable_t *app,
242 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
244 ident *id = (ident *)arg->v_ptr;
245 const char *p = id ? get_id_str(id) : "(null)";
247 return lc_arg_append(app, occ, p, strlen(p));
253 static int firm_emit_indent(lc_appendable_t *app,
254 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
257 int width = occ->width > 0 ? occ->width : 1;
258 int amount = arg->v_int * width;
260 for (i = 0; i < amount; ++i)
261 lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
269 static int firm_emit_pnc(lc_appendable_t *app,
270 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
272 ir_relation value = (ir_relation)arg->v_int;
273 const char *p = get_relation_string(value);
275 return lc_arg_append(app, occ, p, strlen(p));
278 lc_arg_env_t *firm_get_arg_env(void)
281 static lc_arg_env_t *env = NULL;
283 static lc_arg_handler_t firm_handler = { firm_get_arg_type, firm_emit };
284 static lc_arg_handler_t ident_handler = { firm_get_arg_type, firm_emit_ident };
285 static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
286 static lc_arg_handler_t pnc_handler = { firm_get_arg_type_int, firm_emit_pnc };
287 static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
288 static lc_arg_handler_t debug_handler = { firm_get_arg_type, firm_emit_dbg };
295 {"firm:entity", 'e'},
296 {"firm:entity_ld", 'E'},
297 {"firm:tarval", 'T'},
300 {"firm:irn_nr", 'N'},
308 env = lc_arg_new_env();
311 lc_arg_register(env, "firm", 'F', &firm_handler);
312 for (i = 0; i != ARRAY_SIZE(args); ++i)
313 lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
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);