bedump_minir.c 7.8 KB
Newer Older
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
/*
 * Copyright (C) 2010 University of Karlsruhe.  All right reserved.
 *
 * 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.
 */

/**
 * @file
 * @brief       Dump MinIR format for register coalescing
 * @author      Matthias Braun
 * @version     $Id$
 */
#include "config.h"

#include <stdbool.h>

#include "irgwalk.h"
31
#include "iredges_t.h"
32
#include "bearch.h"
33
34
35
36
37
38
39
40
41
42
#include "besched.h"
#include "bedump_minir.h"

typedef enum yaml_state_t {
	STATE_BLOCK_MAPPING,
	STATE_BLOCK_SEQUENCE,
	STATE_LIST,
	STATE_MAPPING,
	STATE_EXPECT_VALUE
} yaml_state_t;
43
44
45
46
47

static const arch_env_t *arch_env;
static FILE             *out;
static int               indent;
static bool              had_list_item;
48
static bool              had_dict_item;
49
static bool              no_newline;
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
static unsigned          state_pos;
static yaml_state_t      state_stack[1024];
static yaml_state_t      state;

static void init_yaml(FILE *new_out)
{
	out       = new_out;
	indent    = 0;
	state_pos = 0;
	state     = STATE_BLOCK_MAPPING;
}

static void exit_yaml(void)
{
	assert(indent == 0);
	assert(state_pos == 0);
	fputs("\n", out);
}

static void push_state(yaml_state_t new_state)
{
	assert(state_pos < sizeof(state_stack)/sizeof(yaml_state_t));
	state_stack[state_pos++] = state;
	state = new_state;
}

static void pop_state(void)
{
	assert(state_pos > 0);
	state = state_stack[--state_pos];
}
81
82
83
84
85
86
87
88
89
90
91

static void newline(void)
{
	int i;

	if (no_newline) {
		no_newline = false;
		return;
	}

	fputc('\n', out);
92
	for (i = 0; i < indent; ++i) {
93
94
95
96
		fputs("  ", out);
	}
}

97
98
static void list_item(void);
static void mapping_item(const char *name);
99

100
static void start(const char *name)
101
{
102
103
104
105
106
107
108
109
	if (name != NULL) {
		mapping_item(name);
	} else if (state == STATE_LIST || state == STATE_BLOCK_SEQUENCE) {
		list_item();
	}

	assert(state == STATE_EXPECT_VALUE);
	pop_state();
110
111
112
113
}

static void begin_block_sequence(const char *name)
{
114
115
	start(name);
	push_state(STATE_BLOCK_SEQUENCE);
116
117
118
119
}

static void end_block_sequence(const char *name)
{
120
121
122
	(void) name;
	assert(state == STATE_BLOCK_SEQUENCE);
	pop_state();
123
124
}

125
static void begin_block_mapping(const char *name)
126
{
127
128
129
130
131
132
	start(name);
	++indent;
	push_state(STATE_BLOCK_MAPPING);
	if (name == NULL) {
		no_newline = true;
	}
133
134
}

135
static void end_block_mapping(const char *name)
136
{
137
138
139
140
	(void) name;
	--indent;
	assert(state == STATE_BLOCK_MAPPING);
	pop_state();
141
142
}

143
static void mapping_item(const char *name)
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
	if (state == STATE_BLOCK_MAPPING) {
		newline();
	} else {
		assert(state == STATE_MAPPING);
		if (had_dict_item)
			fputs(", ", out);
		had_dict_item = true;
	}
	fprintf(out, "%s: ", name);
	push_state(STATE_EXPECT_VALUE);
}

static void list_item(void)
{
	if (state == STATE_BLOCK_SEQUENCE) {
		newline();
		fputs("- ", out);
	} else {
		assert(state == STATE_LIST);
		if (had_list_item)
			fputs(", ", out);
		had_list_item = true;
	}
	push_state(STATE_EXPECT_VALUE);
169
170
171
172
}

static void value(const char *val)
{
173
174
175
176
177
178
179
	if (state == STATE_BLOCK_SEQUENCE
			|| state == STATE_LIST) {
		list_item();
	}

	assert(state == STATE_EXPECT_VALUE);
	pop_state();
180
181
182
183
184
	fputs(val, out);
}

static void key_value(const char *name, const char *val)
{
185
	mapping_item(name);
186
187
188
189
190
	value(val);
}

static void begin_list(const char *name)
{
191
	start(name);
192
193
	fputs("[", out);
	had_list_item = false;
194
	push_state(STATE_LIST);
195
196
197
198
199
}

static void end_list(const char *name)
{
	fputs("]", out);
200
201
202
	(void) name;
	assert(state == STATE_LIST);
	pop_state();
203
204
}

205
static void begin_mapping(const char *name)
206
{
207
208
209
210
	start(name);
	fputs("{", out);
	had_dict_item = false;
	push_state(STATE_MAPPING);
211
212
}

213
static void end_mapping(const char *name)
214
{
215
216
217
218
	fputs("}", out);
	(void) name;
	assert(state == STATE_MAPPING);
	pop_state();
219
220
221
222
}

static void print_regclasses(void)
{
223
	int n_classes = arch_env->n_register_classes;
224
225
226
227
228
229
	int c;

	begin_block_sequence("regclasses");
	for (c = 0; c < n_classes; ++c) {
		int                          n_regs;
		int                          r;
230
		const arch_register_class_t *cls = &arch_env->register_classes[c];
231
232
233
234
235
		if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
			continue;

		n_regs = arch_register_class_n_regs(cls);

236
		begin_block_mapping(NULL);
237
238
239
240
241

		key_value("name", cls->name);
		begin_list("registers");
		for (r = 0; r < n_regs; ++r) {
			const arch_register_t *reg = arch_register_for_index(cls, r);
242
			value(reg->name);
243
244
245
		}
		end_list("registers");

246
247
248
249
250
251
252
253
254
		begin_block_mapping("flags");
		for (r = 0; r < n_regs; ++r) {
			const arch_register_t *reg  = arch_register_for_index(cls, r);
			unsigned               type = reg->type;
			if (type & arch_register_type_ignore) {
				begin_list(reg->name);
				value("reserved");
				value("nossa"); /* do we need this? */
				end_list(reg->name);
255
			}
256
257
258
259
		}
		end_block_mapping("flags");

		end_block_mapping(NULL);
260
261
262
263
	}
	end_block_sequence("regclasses");
}

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
314
315
316
317
318
319
320
321
322
323
324
325
static void print_value_name(ir_node *node)
{
	char name[128];
	const arch_register_req_t *req = arch_get_register_req_out(node);
	snprintf(name, sizeof(name), "V%ld.%s", get_irn_node_nr(node),
	         req->cls->name);

	value(name);
}

static void print_node(ir_node *node)
{
	ir_op *op = get_irn_op(node);
	int    arity;
	int    i;

	begin_mapping(NULL);

	mapping_item("op");
	value(get_op_name(op));

	mapping_item("defs");
	begin_list(NULL);
	if (get_irn_mode(node) == mode_T) {
		const ir_edge_t *edge;
		foreach_out_edge(node, edge) {
			ir_node *proj = get_edge_src_irn(edge);
			const arch_register_req_t *req = arch_get_register_req_out(proj);

			if (req->cls == NULL || (req->type & arch_register_req_type_ignore))
				continue;

			list_item();
			print_value_name(proj);
		}
	} else {
		const arch_register_req_t *req = arch_get_register_req_out(node);
		if (req->cls != NULL && !(req->type & arch_register_req_type_ignore)) {
			list_item();
			print_value_name(node);
		}
	}
	end_list(NULL);

	mapping_item("uses");
	begin_list(NULL);
	arity = get_irn_arity(node);
	for (i = 0; i < arity; ++i) {
		const arch_register_req_t *req = arch_get_register_req(node, i);
		ir_node                   *op  = get_irn_n(node, i);

		if (req->cls == NULL || (req->type & arch_register_req_type_ignore))
			continue;

		list_item();
		print_value_name(op);
	}
	end_list(NULL);

	end_mapping(NULL);
}

326
327
static void dump_block(ir_node *block, void *data)
{
328
	ir_graph *irg = get_irn_irg(block);
329
	ir_node  *node;
330
331
	(void) data;

332
333
	begin_block_mapping(NULL);

334
335
336
337
338
339
	if (block == get_irg_start_block(irg)) {
		key_value("label", "start");
	} else if (block == get_irg_end_block(irg)) {
		key_value("label", "end");
	} else {
		char name[128];
340
		snprintf(name, sizeof(name), "BB%ld", get_irn_node_nr(block));
341
342
343
344
		key_value("label", name);
	}

	begin_block_sequence("ops");
345
346
347
348
349

	sched_foreach(block, node) {
		print_node(node);
	}

350
	end_block_sequence("ops");
351
352

	end_block_mapping(NULL);
353
354
355
356
357
358
}

static void print_function(ir_graph *irg)
{
	ir_entity *entity = get_irg_entity(irg);

359
	begin_block_mapping(NULL);
360
361

	key_value("label", get_entity_name(entity));
362
363
	begin_list("entries"); value("start"); end_list("entries");
	begin_list("exit");    value("end");   end_list("exit");
364
365
366
367

	begin_block_sequence("bbs");
	irg_block_walk_graph(irg, dump_block, NULL, NULL);
	end_block_sequence("bbs");
368
369

	end_block_mapping(NULL);
370
371
}

372
void be_export_minir(FILE *out, ir_graph *irg)
373
{
374
	arch_env = be_get_irg_arch_env(irg);
375
	init_yaml(out);
376
377
378
379
380
381
382

	print_regclasses();

	begin_block_sequence("functions");
	print_function(irg);
	end_block_sequence("functions");

383
	exit_yaml();
384
}