summaryrefslogtreecommitdiffhomepage
path: root/ir/ir/irargs.c
blob: 0bf2f8735646f65ff3230f4be346e9a8d7a4b87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief    Support for libcore IR object output.
 * @author   Sebastian Hack
 */
#include "irargs_t.h"

#include "bitset.h"
#include "dbginfo_t.h"
#include "entity_t.h"
#include "firm_common.h"
#include "irloop_t.h"
#include "irnode_t.h"
#include "irprintf.h"
#include "lc_printf.h"
#include "tv_t.h"
#include "util.h"
#include <ctype.h>

/**
 * identify a firm object type
 */
static int firm_get_arg_type(const lc_arg_occ_t *occ)
{
	(void)occ;
	/* Firm objects are always pointer */
	return lc_arg_type_ptr;
}

static int firm_get_arg_type_int(const lc_arg_occ_t *occ)
{
	(void)occ;
	return lc_arg_type_int;
}


static int bitset_get_arg_type(const lc_arg_occ_t *occ)
{
	(void)occ;
	return lc_arg_type_ptr;
}

static int bitset_emit(lc_appendable_t *app, const lc_arg_occ_t *occ,
                       const lc_arg_value_t *arg)
{
	int res = 0;
	lc_arg_append(app, occ, "[", 1);
	++res;
	const char *prefix = "";
	bitset_t *b = (bitset_t*)arg->v_ptr;
	bitset_foreach(b, p) {
		char buf[32];
		int  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);
	++res;

	return res;
}

/**
 * emit an opaque Firm dbg_info object
 */
static int firm_emit_dbg(lc_appendable_t *app, const lc_arg_occ_t *occ,
                         const lc_arg_value_t *arg)
{
	ir_node  *irn = (ir_node*)arg->v_ptr;
	dbg_info *dbg = get_irn_dbg_info(irn);

	char buf[1024];
	ir_dbg_info_snprint(buf, sizeof(buf), dbg);
	size_t size = strlen(buf);
	lc_arg_append(app, occ, buf, size);
	return size;
}

static void print_entity_name(char *buffer, size_t buffer_size,
                              char const *const add,
                              ir_entity const *const entity)
{
	switch (entity->kind) {
	case IR_ENTITY_PARAMETER:
		snprintf(buffer, buffer_size, "%sparameter.%lu", add,
		         (unsigned long)get_entity_parameter_number(entity));
		return;
	case IR_ENTITY_LABEL:
		snprintf(buffer, buffer_size, "%slabel", add);
		return;
	case IR_ENTITY_SPILLSLOT:
		snprintf(buffer, buffer_size, "%sspillslot", add);
		return;
	default:
		snprintf(buffer, buffer_size, "%s%s", add, get_entity_ld_name(entity));
		return;
	}
}

static const char *get_entity_name_or_null_str(const ir_entity *const ent)
{
	return ent != NULL ? get_entity_name(ent) : "(null)";
}

/**
 * emit a Firm object
 */
static int firm_emit(lc_appendable_t *app, const lc_arg_occ_t *occ,
                     const lc_arg_value_t *arg)
{
	void *X = (void*)arg->v_ptr;
	if (X == NULL)
		return lc_arg_append(app, occ, "(null)", 6);

	char buf[256];
	buf[0] = '\0';
	char add[64];
	add[0] = '\0';

#define A(s)    occ->flag_hash ? s " ": ""
	firm_kind *obj = (firm_kind*)X;
	switch (*obj) {
	case k_BAD:
		snprintf(buf, sizeof(buf), "BAD");
		snprintf(add, sizeof(add), "[%p]", X);
		break;
	case k_entity: {
		ir_entity const *const entity = (ir_entity const*)X;
		print_entity_name(buf, sizeof(buf), A("ent"), entity);
		snprintf(add, sizeof(add), "[%ld]", get_entity_nr(entity));
		break;
	}
	case k_type: {
		ir_type *type = (ir_type*)X;
		char type_name[256];
		ir_print_type(type_name, sizeof(type_name), type);
		tp_opcode opcode = get_type_opcode(type);
		snprintf(buf, sizeof(buf), "%s%s:%s", A("type"),
		         get_type_opcode_name(opcode), type_name);
		snprintf(add, sizeof(add), "[%ld]", get_type_nr(type));
		break;
	}
	case k_ir_graph: {
		ir_graph *irg = (ir_graph*)X;
		if (irg == get_const_code_irg())
			snprintf(buf, sizeof(buf), "%s<ConstCodeIrg>", A("irg"));
		else
			ir_snprintf(buf, sizeof(buf), "%s%F", A("irg"), get_irg_entity(irg));
		snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(irg));
		break;
	}
	case k_ir_node: {
		ir_node *node = (ir_node*)X;
		switch (occ->conversion) {
		case 'B': {
			ir_node *const block = get_block(node);
			snprintf(buf, sizeof(buf), "%s%s %s", A("irn"),
			         get_irn_opname(block), get_mode_name(get_irn_mode(block)));
			snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
			break;
		}
		case 'N':
			snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(node));
			break;
		default:
			if (is_Const(node)) {
				char tv_buf[256];
				ir_tarval *tv = get_Const_tarval(node);
				if (tv)
					tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
				else
					strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
				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_Address(node)) {
				const char *entity_name = get_entity_name_or_null_str(get_Address_entity(node));
				snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
				         get_mode_name(get_irn_mode(node)), entity_name);
			} else if (is_Member(node)) {
				const char *entity_name = get_entity_name_or_null_str(get_Member_entity(node));
				snprintf(buf, sizeof(buf), "%s%s %s[%s]", A("irn"), get_irn_opname(node),
				         get_mode_name(get_irn_mode(node)), entity_name);
			} 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));
			} else {
				snprintf(buf, sizeof(buf), "%s%s %s", A("irn"), get_irn_opname(node),
				         get_mode_name(get_irn_mode(node)));
			}
			snprintf(add, sizeof(add), "[%ld:%u]", get_irn_node_nr(node), get_irn_idx(node));
		}
		break;
	}
	case k_ir_mode: {
		ir_mode *mode = (ir_mode*)X;
		snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(mode));
		break;
	}
	case k_tarval: {
		ir_tarval *tarval = (ir_tarval*)X;
		char tv_buf[256];
		tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
		snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
		break;
	}
	case k_ir_loop: {
		ir_loop *loop = (ir_loop*)X;
		snprintf(buf, sizeof(buf), "loop[%ld:%u]", get_loop_loop_nr(loop), get_loop_depth(loop));
		break;
	}

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

	if (occ->flag_plus)
		strncat(buf, add, sizeof(buf)-strlen(buf)-1);

	size_t size = strlen(buf);
	lc_arg_append(app, occ, buf, size);
	return size;
#undef A
}

/**
 * emit an ident
 */
static int firm_emit_ident(lc_appendable_t *app, const lc_arg_occ_t *occ,
                           const lc_arg_value_t *arg)
{
	ident      *id   = (ident *)arg->v_ptr;
	const char *p    = id != NULL ? get_id_str(id) : "(null)";
	size_t      size = strlen(p);
	lc_arg_append(app, occ, p, size);
	return size;
}

/**
 * Emit indent.
 */
static int firm_emit_indent(lc_appendable_t *app, const lc_arg_occ_t *occ,
                            const lc_arg_value_t *arg)
{
	int width  = MAX(1, occ->width);
	int amount = arg->v_int * width;
	for (int i = 0; i < amount; ++i)
		lc_appendable_chadd(app, (i % width) == 0 ? '|' : ' ');
	return amount;
}

/**
 * Emit pnc.
 */
static int firm_emit_pnc(lc_appendable_t *app, const lc_arg_occ_t *occ,
                         const lc_arg_value_t *arg)
{
	ir_relation value = (ir_relation)arg->v_int;
	const char *p = get_relation_string(value);

	size_t size = strlen(p);
	lc_arg_append(app, occ, p, size);
	return size;
}

lc_arg_env_t *firm_get_arg_env(void)
{
	static lc_arg_handler_t const firm_handler   = { firm_get_arg_type, firm_emit };
	static lc_arg_handler_t const ident_handler  = { firm_get_arg_type, firm_emit_ident };
	static lc_arg_handler_t const indent_handler = { firm_get_arg_type_int, firm_emit_indent };
	static lc_arg_handler_t const pnc_handler    = { firm_get_arg_type_int, firm_emit_pnc };
	static lc_arg_handler_t const bitset_handler = { bitset_get_arg_type, bitset_emit };
	static lc_arg_handler_t const debug_handler  = { firm_get_arg_type, firm_emit_dbg };

	static struct {
		const char *name;
		char letter;
	} args[] = {
		{"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'},
	};

	static lc_arg_env_t *env = NULL;
	if (env == NULL) {
		env = lc_arg_new_env();
		lc_arg_add_std(env);

		lc_arg_register(env, "firm", 'F', &firm_handler);
		for (size_t i = 0; i != ARRAY_SIZE(args); ++i)
			lc_arg_register(env, args[i].name, args[i].letter, &firm_handler);

		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);
	}

	return env;
}