2 * Copyright (C) 1995-2007 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
30 #include "firm_config.h"
37 #include <libcore/lc_printf.h>
39 #include "firm_common.h"
44 #include "dbginfo_t.h"
47 * identify a firm object type
49 static int firm_get_arg_type(const lc_arg_occ_t *occ) {
51 /* Firm objects are always pointer */
52 return lc_arg_type_ptr;
55 static int firm_get_arg_type_int(const lc_arg_occ_t *occ) {
57 return lc_arg_type_int;
61 static int bitset_get_arg_type(const lc_arg_occ_t *occ) {
63 return lc_arg_type_ptr;
66 static int bitset_emit(lc_appendable_t *app,
67 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
70 bitset_t *b = arg->v_ptr;
73 const char *prefix = "";
75 lc_arg_append(app, occ, "[", 1);
76 bitset_foreach(b, p) {
79 n = snprintf(buf, sizeof(buf), "%s%d", prefix, (int) p);
80 lc_arg_append(app, occ, buf, n);
84 lc_arg_append(app, occ, "]", 1);
90 * emit an opaque Firm dbg_info object
92 static int firm_emit_dbg(lc_appendable_t *app,
93 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
96 ir_node *irn = arg->v_ptr;
97 dbg_info *dbg = get_irn_dbg_info(irn);
100 if (dbg && __dbg_info_snprint) {
101 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
104 return lc_arg_append(app, occ, buf, strlen(buf));
108 * Beware: do not set the entity ld_name
110 static const char *get_entity_ld_name_ex(ir_entity *ent) {
112 return get_entity_ld_name(ent);
113 return get_entity_name(ent);
119 static int firm_emit(lc_appendable_t *app,
120 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
122 #define A(s) occ->flag_hash ? s " ": ""
124 void *X = arg->v_ptr;
137 strncpy(buf, "(null)", sizeof(buf));
141 snprintf(buf, sizeof(buf), "BAD");
142 snprintf(add, sizeof(add), "[%p]", X);
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));
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));
154 if (X == get_const_code_irg())
155 snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
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));
161 switch (occ->conversion) {
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));
169 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(X));
173 tarval *tv = get_Const_tarval(X);
175 tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
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);
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));
188 snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(X));
191 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
192 snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
195 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
198 snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(X));
200 case k_ir_compound_graph_path:
201 n = get_compound_graph_path_length(X);
203 for (i = 0; i < n; ++i) {
204 ent = get_compound_graph_path_node(X, i);
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));
217 snprintf(buf, sizeof(buf), "ExtBlock");
218 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(get_extbb_leader(X)));
222 snprintf(buf, sizeof(buf), "UNKWN");
223 snprintf(add, sizeof(add), "[%p]", X);
228 strncat(buf, add, sizeof(buf));
230 return lc_arg_append(app, occ, buf, strlen(buf));
238 static int firm_emit_ident(lc_appendable_t *app,
239 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
241 ident *id = (ident *)arg->v_ptr;
242 const char *p = id ? get_id_str(id) : "(null)";
244 return lc_arg_append(app, occ, p, strlen(p));
250 static int firm_emit_indent(lc_appendable_t *app,
251 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
254 int width = occ->width > 0 ? occ->width : 1;
255 int amount = arg->v_int * width;
257 for(i = 0; i < amount; ++i)
258 lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
266 static int firm_emit_pnc(lc_appendable_t *app,
267 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
269 int value = arg->v_int;
270 const char *p = get_pnc_string(value);
272 return lc_arg_append(app, occ, p, strlen(p));
275 lc_arg_env_t *firm_get_arg_env(void)
278 static lc_arg_env_t *env = NULL;
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 };
291 #define X(name, letter) {"firm:" name, letter}
308 env = lc_arg_new_env();
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);
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);
325 #endif /* WITH_LIBCORE */