irargs.c 9.17 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
/**
 * @file
 * @brief    Support for libcore IR object output.
 * @author   Sebastian Hack
24
 */
Matthias Braun's avatar
Matthias Braun committed
25
#include "config.h"
26

27
#include "irargs_t.h"
28

29
30
#include <ctype.h>

31
#include "bitset.h"
Matthias Braun's avatar
Matthias Braun committed
32
#include "lc_printf.h"
33
#include "firm_common.h"
34
35
36
37
#include "irnode_t.h"
#include "entity_t.h"
#include "irloop_t.h"
#include "tv_t.h"
38
#include "dbginfo_t.h"
39
#include "util.h"
40
41
42
43

/**
 * identify a firm object type
 */
44
45
static int firm_get_arg_type(const lc_arg_occ_t *occ)
{
Michael Beck's avatar
Michael Beck committed
46
47
48
	(void) occ;
	/* Firm objects are always pointer */
	return lc_arg_type_ptr;
49
50
}

51
52
static int firm_get_arg_type_int(const lc_arg_occ_t *occ)
{
Michael Beck's avatar
Michael Beck committed
53
54
	(void) occ;
	return lc_arg_type_int;
55
56
}

57

58
59
static int bitset_get_arg_type(const lc_arg_occ_t *occ)
{
Michael Beck's avatar
Michael Beck committed
60
61
	(void) occ;
	return lc_arg_type_ptr;
62
63
}

Sebastian Hack's avatar
Sebastian Hack committed
64
65
static int bitset_emit(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
66
{
Michael Beck's avatar
Michael Beck committed
67
	int res = 2;
68
	bitset_t *b = (bitset_t*)arg->v_ptr;
69
	size_t  p;
Michael Beck's avatar
Michael Beck committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	char buf[32];
	const char *prefix = "";

	lc_arg_append(app, occ, "[", 1);
	bitset_foreach(b, p) {
		int n;

		n = snprintf(buf, sizeof(buf), "%s%d", prefix, (int) p);
		lc_arg_append(app, occ, buf, n);
		prefix = ", ";
		res += n;
	}
	lc_arg_append(app, occ, "]", 1);

	return res;
85
86
}

87
88
89
/**
 * emit an opaque Firm dbg_info object
 */
Sebastian Hack's avatar
Sebastian Hack committed
90
91
static int firm_emit_dbg(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
92
{
Michael Beck's avatar
Michael Beck committed
93
	char buf[1024];
94
	ir_node *irn = (ir_node*)arg->v_ptr;
Michael Beck's avatar
Michael Beck committed
95
96
	dbg_info *dbg = get_irn_dbg_info(irn);

97
	ir_dbg_info_snprint(buf, sizeof(buf), dbg);
Michael Beck's avatar
Michael Beck committed
98
	return lc_arg_append(app, occ, buf, strlen(buf));
99
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)
{
Michael Beck's avatar
Michael Beck committed
106
107
108
	if (ent->ld_name)
		return get_entity_ld_name(ent);
	return get_entity_name(ent);
109
110
}

111
112
113
/**
 * emit a Firm object
 */
Sebastian Hack's avatar
Sebastian Hack committed
114
115
static int firm_emit(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
116
{
117
#define A(s)    occ->flag_hash ? s " ": ""
118

119
120
	void *X = (void*)arg->v_ptr;
	firm_kind *obj = (firm_kind*)X;
Michael Beck's avatar
Michael Beck committed
121
	size_t i, n;
Michael Beck's avatar
Michael Beck committed
122
123
124
125
126
127
128
129
130
	ir_node *block;
	char add[64];
	char buf[256];
	char tv_buf[256];
	ir_entity *ent;

	buf[0] = '\0';
	add[0] = '\0';

131
132
133
134
135
	if (X == NULL) {
		return lc_arg_append(app, occ, "(null)", 6);
	}

	switch (*obj) {
Michael Beck's avatar
Michael Beck committed
136
137
138
139
	case k_BAD:
		snprintf(buf, sizeof(buf), "BAD");
		snprintf(add, sizeof(add), "[%p]", X);
		break;
140
141
	case k_entity: {
		ir_entity *entity = (ir_entity*)X;
Michael Beck's avatar
Michael Beck committed
142
		snprintf(buf, sizeof(buf), "%s%s", A("ent"),
143
			isupper((unsigned char)occ->conversion) ? get_entity_ld_name_ex(entity): get_entity_name(entity));
144
		snprintf(add, sizeof(add), "[%ld]", get_entity_nr(entity));
Michael Beck's avatar
Michael Beck committed
145
		break;
146
	}
147
	case k_type: {
148
		ir_type *type = (ir_type*)X;
149
		char type_name[256];
150
151
152
153
		ir_print_type(type_name, sizeof(type_name), type);
		snprintf(buf, sizeof(buf), "%s%s:%s", A("type"),
		         get_type_tpop_name(type), type_name);
		snprintf(add, sizeof(add), "[%ld]", get_type_nr(type));
Michael Beck's avatar
Michael Beck committed
154
		break;
155
	}
156
157
158
	case k_ir_graph: {
		ir_graph *irg = (ir_graph*)X;
		if (irg == get_const_code_irg())
Michael Beck's avatar
Michael Beck committed
159
160
			snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
		else
161
162
			snprintf(buf, sizeof(buf), "%s%s", A("irg"), get_entity_name(get_irg_entity(irg)));
		snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(irg));
Michael Beck's avatar
Michael Beck committed
163
		break;
164
165
166
	}
	case k_ir_node: {
		ir_node *node = (ir_node*)X;
Michael Beck's avatar
Michael Beck committed
167
168
		switch (occ->conversion) {
		case 'B':
169
			block = !is_Block(node) ? get_nodes_block(node) : node;
170
171
			snprintf(buf, sizeof(buf), "%s%s %s", A("irn"),
			         get_irn_opname(block), get_mode_name(get_irn_mode(block)));
Michael Beck's avatar
Michael Beck committed
172
173
174
			snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
			break;
		case 'N':
175
			snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(node));
Michael Beck's avatar
Michael Beck committed
176
177
			break;
		default:
178
179
			if (is_Const(node)) {
				ir_tarval *tv = get_Const_tarval(node);
Michael Beck's avatar
Michael Beck committed
180
181
182
183
				if (tv)
					tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
				else
					strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
184
185
186
187
188
189
				snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(node),
					get_mode_name(get_irn_mode(node)), tv_buf);
			} else if (is_SymConst_addr_ent(node)) {
				snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
				get_mode_name(get_irn_mode(node)), get_entity_name(get_SymConst_entity(node)));
			} else if (is_Sel(node)) {
190
191
192
193
				ir_entity *entity = get_Sel_entity(node);
				const char *entity_name = "(null)";
				if (entity != NULL)
					entity_name = get_entity_name(entity);
194
				snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
195
				get_mode_name(get_irn_mode(node)), entity_name);
196
197
198
			} else if (is_Cmp(node)) {
				ir_relation relation = get_Cmp_relation(node);
				snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node), get_relation_string(relation));
199
			} else {
200
201
				snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node),
				get_mode_name(get_irn_mode(node)));
Michael Beck's avatar
Michael Beck committed
202
			}
203
			snprintf(add, sizeof(add), "[%ld:%u]", get_irn_node_nr(node), get_irn_idx(node));
204
205
		}
		break;
206
207
208
209
	}
	case k_ir_mode: {
		ir_mode *mode = (ir_mode*)X;
		snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(mode));
Michael Beck's avatar
Michael Beck committed
210
		break;
211
212
213
214
	}
	case k_tarval: {
		ir_tarval *tarval = (ir_tarval*)X;
		tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
Michael Beck's avatar
Michael Beck committed
215
216
		snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
		break;
217
218
219
	}
	case k_ir_loop: {
		ir_loop *loop = (ir_loop*)X;
Matthias Braun's avatar
Matthias Braun committed
220
		snprintf(buf, sizeof(buf), "loop[%ld:%u]", get_loop_loop_nr(loop), get_loop_depth(loop));
Michael Beck's avatar
Michael Beck committed
221
		break;
222
223
224
225
	}
	case k_ir_op: {
		ir_op *op = (ir_op*)X;
		snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(op));
Michael Beck's avatar
Michael Beck committed
226
		break;
227
228
229
230
	}
	case k_ir_compound_graph_path: {
		compound_graph_path *path = (compound_graph_path*)X;
		n = get_compound_graph_path_length(path);
Michael Beck's avatar
Michael Beck committed
231
232

		for (i = 0; i < n; ++i) {
233
			ent = get_compound_graph_path_node(path, i);
Michael Beck's avatar
Michael Beck committed
234

235
236
			strncat(buf, ".", sizeof(buf)-1);
			strncat(buf, get_entity_name(ent), sizeof(buf)-1);
Michael Beck's avatar
Michael Beck committed
237
			if (is_Array_type(get_entity_owner(ent))) {
Matthias Braun's avatar
Matthias Braun committed
238
				snprintf(add, sizeof(add), "[%ld]",
239
					get_compound_graph_path_array_index(path, i));
240
				strncat(buf, add, sizeof(buf)-1);
Michael Beck's avatar
Michael Beck committed
241
242
243
244
			}
		}
		add[0] = '\0';
		break;
245
246
247
	}
	case k_ir_extblk: {
		ir_extblk *extblk = (ir_extblk*)X;
Michael Beck's avatar
Michael Beck committed
248
		snprintf(buf, sizeof(buf), "ExtBlock");
249
		snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(get_extbb_leader(extblk)));
Michael Beck's avatar
Michael Beck committed
250
		break;
251
	}
Michael Beck's avatar
Michael Beck committed
252
253
254
255
256
257
258

	default:
		snprintf(buf, sizeof(buf), "UNKWN");
		snprintf(add, sizeof(add), "[%p]", X);
	}

	if (occ->flag_plus)
259
		strncat(buf, add, sizeof(buf)-1);
Michael Beck's avatar
Michael Beck committed
260
261

	return lc_arg_append(app, occ, buf, strlen(buf));
262
263
264
265
266
267
#undef A
}

/**
 * emit an ident
 */
Sebastian Hack's avatar
Sebastian Hack committed
268
269
static int firm_emit_ident(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
270
{
Michael Beck's avatar
Michael Beck committed
271
272
	ident *id = (ident *)arg->v_ptr;
	const char *p = id ? get_id_str(id) : "(null)";
273

Michael Beck's avatar
Michael Beck committed
274
	return lc_arg_append(app, occ, p, strlen(p));
275
276
}

277
278
279
/**
 * Emit indent.
 */
Sebastian Hack's avatar
Sebastian Hack committed
280
281
static int firm_emit_indent(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
282
283
{
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
284
285
	int width  = occ->width > 0 ? occ->width : 1;
	int amount = arg->v_int * width;
286

Michael Beck's avatar
Michael Beck committed
287
	for (i = 0; i < amount; ++i)
Sebastian Hack's avatar
Sebastian Hack committed
288
		lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
289
290
291
292

	return amount;
}

293
294
295
/**
 * Emit pnc.
 */
Sebastian Hack's avatar
Sebastian Hack committed
296
297
static int firm_emit_pnc(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
298
{
Michael Beck's avatar
Michael Beck committed
299
	int value = arg->v_int;
300
	const char *p = get_relation_string(value);
301

Michael Beck's avatar
Michael Beck committed
302
	return lc_arg_append(app, occ, p, strlen(p));
303
304
}

Sebastian Hack's avatar
Sebastian Hack committed
305
lc_arg_env_t *firm_get_arg_env(void)
306
307
{

Michael Beck's avatar
Michael Beck committed
308
	static lc_arg_env_t *env = NULL;
309

Michael Beck's avatar
Michael Beck committed
310
311
312
313
314
315
	static lc_arg_handler_t firm_handler   = { firm_get_arg_type, firm_emit };
	static lc_arg_handler_t ident_handler  = { firm_get_arg_type, firm_emit_ident };
	static lc_arg_handler_t indent_handler = { firm_get_arg_type_int, firm_emit_indent };
	static lc_arg_handler_t pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
	static lc_arg_handler_t bitset_handler = { bitset_get_arg_type, bitset_emit };
	static lc_arg_handler_t debug_handler  = { firm_get_arg_type, firm_emit_dbg };
316

Michael Beck's avatar
Michael Beck committed
317
318
319
320
	static struct {
		const char *name;
		char letter;
	} args[] = {
321
322
323
324
325
326
327
328
329
330
		{"firm:type",      't'},
		{"firm:entity",    'e'},
		{"firm:entity_ld", 'E'},
		{"firm:tarval",    'T'},
		{"firm:irn",       'n'},
		{"firm:op",        'O'},
		{"firm:irn_nr",    'N'},
		{"firm:mode",      'm'},
		{"firm:block",     'B'},
		{"firm:cg_path",   'P'},
Michael Beck's avatar
Michael Beck committed
331
	};
332

Michael Beck's avatar
Michael Beck committed
333
	size_t i;
334

Michael Beck's avatar
Michael Beck committed
335
336
337
	if (env == NULL) {
		env = lc_arg_new_env();
		lc_arg_add_std(env);
338

Michael Beck's avatar
Michael Beck committed
339
		lc_arg_register(env, "firm", 'F', &firm_handler);
340
		for (i = 0; i != ARRAY_SIZE(args); ++i)
Michael Beck's avatar
Michael Beck committed
341
			lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);
342

Michael Beck's avatar
Michael Beck committed
343
344
345
346
347
348
		lc_arg_register(env, "firm:ident",    'I', &ident_handler);
		lc_arg_register(env, "firm:indent",   'D', &indent_handler);
		lc_arg_register(env, "firm:dbg_info", 'G', &debug_handler);
		lc_arg_register(env, "firm:bitset",   'B', &bitset_handler);
		lc_arg_register(env, "firm:pnc",      '=', &pnc_handler);
	}
349

Michael Beck's avatar
Michael Beck committed
350
	return env;
351
}