bedump_minir.c 4.72 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
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
/*
 * 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"
#include "bearch.h"

static const arch_env_t *arch_env;
static FILE             *out;
static int               indent;
static int               item_indent_add;
static bool              had_list_item;
static bool              no_newline;

static void newline(void)
{
	int i;

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

	fputc('\n', out);
	for (i = 0; i < indent + item_indent_add; ++i) {
		fputs("  ", out);
	}
}

static void begin_key(const char *name)
{
	newline();
	fprintf(out, "%s: ", name);
}

static void end_key(const char *name)
{
	/* TODO verify */
	(void) name;
}

static void begin_block_sequence(const char *name)
{
	begin_key(name);
	indent++;
}

static void end_block_sequence(const char *name)
{
	end_key(name);
	indent--;
	item_indent_add = 0;
}

static void block_sequence_item(void)
{
	item_indent_add = 0;
	newline();
	fputs("- ", out);
	item_indent_add = 1;
	no_newline = true;
}

static void begin_mapping(const char *name)
{
	begin_key(name);
	indent++;
}

static void end_maping(const char *name)
{
	end_key(name);
	indent--;
}

static void value(const char *val)
{
	fputs(val, out);
}

static void key_value(const char *name, const char *val)
{
	begin_key(name);
	value(val);
	end_key(name);
}

static void begin_list(const char *name)
{
	begin_key(name);
	fputs("[", out);
	had_list_item = false;
}

static void end_list(const char *name)
{
	/* TODO: verify that we started a list */
	fputs("]", out);
	end_key(name);
}

static void list_item(void)
{
	if (had_list_item)
		fputs(", ", out);
	had_list_item = true;
}

static void list_item_value(const char *val)
{
	list_item();
	value(val);
}

static void print_regclasses(void)
{
	int n_classes = arch_env_get_n_reg_class(arch_env);
	int c;

	begin_block_sequence("regclasses");
	for (c = 0; c < n_classes; ++c) {
		int                          n_regs;
		int                          r;
		const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, c);
		if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
			continue;

		n_regs = arch_register_class_n_regs(cls);

		block_sequence_item();

		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);
			list_item_value(reg->name);
		}
		end_list("registers");

		begin_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);
					list_item_value("reserved");
					list_item_value("nossa"); /* do we need this? */
					end_list(reg->name);
				}
			}
		end_maping("flags");
	}
	end_block_sequence("regclasses");
}

static void dump_block(ir_node *block, void *data)
{
183
	ir_graph *irg = get_irn_irg(block);
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
	(void) data;

	block_sequence_item();
	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];
		snprintf(name, sizeof(name), "bb%ld", get_irn_node_nr(block));
		key_value("label", name);
	}

	begin_block_sequence("ops");
	end_block_sequence("ops");
}

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

	block_sequence_item();

	key_value("label", get_entity_name(entity));
	begin_list("entries"); list_item_value("start"); end_list("entries");
	begin_list("exit"); list_item_value("end"); end_list("entries");

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

void be_export_minir(const arch_env_t *new_arch_env, FILE *new_out, ir_graph *irg)
{
	arch_env = new_arch_env;
	out      = new_out;

	no_newline = true;

	print_regclasses();

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

	assert(indent == 0);
	fputs("\n", out);

}