summaryrefslogtreecommitdiffhomepage
path: root/ir/tr/typewalk.c
blob: 0623495714c9353d25e7a180cd5f8a753478340e (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief   Functionality to modify the type graph.
 * @author  Goetz Lindenmaier
 * @brief
 *
 * Traverse the type information.  The walker walks the whole ir graph
 * to find the distinct type trees in the type graph forest.
 * - execute the pre function before recursion
 * - execute the post function after recursion
 */
#include "entity_t.h"
#include "ircons.h"
#include "irgraph_t.h"
#include "irgwalk.h"
#include "irnode_t.h"
#include "irprog_t.h"
#include "panic.h"
#include "type_t.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * The walker environment
 */
typedef struct {
	type_walk_func *pre;    /**< Pre-walker function */
	type_walk_func *post;   /**< Post-walker function */
	void           *env;    /**< environment for walker functions */
} type_walk_env;

/** a walker for irn's */
static void irn_type_walker(ir_node *node, type_walk_func *pre,
                            type_walk_func *post, void *env);

static void walk_initializer(ir_initializer_t *initializer,
                             type_walk_func *pre, type_walk_func *post,
                             void *env)
{
	switch (initializer->kind) {
	case IR_INITIALIZER_CONST:
		irn_type_walker(initializer->consti.value, pre, post, env);
		return;
	case IR_INITIALIZER_TARVAL:
	case IR_INITIALIZER_NULL:
		return;

	case IR_INITIALIZER_COMPOUND: {
		for (size_t i = 0; i < initializer->compound.n_initializers; ++i) {
			ir_initializer_t *subinitializer
				= initializer->compound.initializers[i];
			walk_initializer(subinitializer, pre, post, env);
		}
		return;
	}
	}
	panic("invalid initializer found");
}

/**
 * Main walker: walks over all used types/entities of a
 * type entity.
 */
static void do_type_walk(ir_type *const tp, ir_entity *const ent,
                         type_walk_func *pre, type_walk_func *post, void *env)
{
	/* marked? */
	if (ent) {
		if (entity_visited(ent))
			return;
		mark_entity_visited(ent);
	} else {
		if (type_visited(tp))
			return;
		mark_type_visited(tp);
	}

	/* execute pre method */
	if (pre)
		pre(tp, ent, env);

	/* iterate */
	if (ent) {
		do_type_walk(get_entity_owner(ent), NULL, pre, post, env);
		do_type_walk(get_entity_type(ent),  NULL, pre, post, env);

		switch (get_entity_kind(ent)) {
		case IR_ENTITY_ALIAS: {
			ir_entity *const e = get_entity_alias(ent);
			if (e)
				do_type_walk(NULL, e, pre, post, env);
			break;
		}

		case IR_ENTITY_NORMAL: {
			/* walk over the value types */
			ir_initializer_t *const init = get_entity_initializer(ent);
			if (init)
				walk_initializer(init, pre, post, env);
			break;
		}

		case IR_ENTITY_METHOD:
		case IR_ENTITY_UNKNOWN:
		case IR_ENTITY_PARAMETER:
		case IR_ENTITY_LABEL:
		case IR_ENTITY_COMPOUND_MEMBER:
		case IR_ENTITY_SPILLSLOT:
			break;
		}
	} else {
		switch (get_type_opcode(tp)) {
		case tpo_class:
			for (size_t i = 0, n_types = get_class_n_supertypes(tp);
			     i < n_types; ++i) {
				do_type_walk(get_class_supertype(tp, i), NULL, pre, post, env);
			}
			for (size_t i = 0, n_mem = get_compound_n_members(tp);
			     i < n_mem; ++i) {
				do_type_walk(NULL, get_compound_member(tp, i), pre, post, env);
			}
			for (size_t i = 0, n_types = get_class_n_subtypes(tp);
			     i < n_types; ++i) {
				do_type_walk(get_class_subtype(tp, i), NULL, pre, post, env);
			}
			break;

		case tpo_struct:
		case tpo_union:
		case tpo_segment:
			for (size_t i = 0, n_mem = get_compound_n_members(tp);
			     i < n_mem; ++i) {
				do_type_walk(NULL, get_compound_member(tp, i), pre, post, env);
			}
			break;

		case tpo_method:
			for (size_t i = 0, n_params = get_method_n_params(tp);
			     i < n_params; ++i) {
				do_type_walk(get_method_param_type(tp, i), NULL, pre, post, env);
			}
			for (size_t i = 0, n_res = get_method_n_ress(tp); i < n_res; ++i) {
				do_type_walk(get_method_res_type(tp, i), NULL, pre, post, env);
			}
			break;

		case tpo_array:
			do_type_walk(get_array_element_type(tp), NULL, pre, post, env);
			break;

		case tpo_pointer:
			do_type_walk(get_pointer_points_to_type(tp), NULL, pre, post, env);
			break;

		case tpo_code:
		case tpo_primitive:
		case tpo_unknown:
			/* a leave. */
			break;
		case tpo_uninitialized:
			panic("faulty type");
		}
	}

	/* execute post method */
	if (post)
		post(tp, ent, env);
}

/**
 * Check whether node contains types or entities as an attribute.
 * If so start a walk over that information.
 */
static void irn_type_walker(ir_node *node, type_walk_func *pre,
                            type_walk_func *post, void *env)
{
	ir_entity *const ent = get_irn_entity_attr(node);
	if (ent)
		do_type_walk(NULL, ent, pre, post, env);
	ir_type *const typ = get_irn_type_attr(node);
	if (typ)
		do_type_walk(typ, NULL, pre, post, env);
}

/**
 * Check whether node contains types or entities as an attribute.
 * If so start a walk over that information.
 */
static void start_type_walk(ir_node *node, void *ctx)
{
	type_walk_env  *env  = (type_walk_env*)ctx;
	type_walk_func *pre  = env->pre;
	type_walk_func *post = env->post;
	void           *envi = env->env;
	irn_type_walker(node, pre, post, envi);
}

void type_walk(type_walk_func *pre, type_walk_func *post, void *env)
{
	irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	inc_master_type_visited();
	for (size_t i = 0, n_types = get_irp_n_types(); i < n_types; ++i) {
		do_type_walk(get_irp_type(i), NULL, pre, post, env);
	}
	do_type_walk(get_glob_type(), NULL, pre, post, env);
	irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
}

void type_walk_irg(ir_graph *irg, type_walk_func *pre, type_walk_func *post,
                   void *env)
{
	/* this is needed to pass the parameters to the walker that actually
	   walks the type information */
	type_walk_env type_env;
	type_env.pre  = pre;
	type_env.post = post;
	type_env.env  = env;

	/* We walk over the irg to find all IR-nodes that contain an attribute
	 * with type information.  If we find one we call a type walker to
	 * touch the reachable type information.  The same type can be referenced
	 * by several IR-nodes.  To avoid repeated visits of the same type node we
	 * must decrease the type visited flag for each walk.  This is done in
	 * start_type_walk().  Here we initially increase the flag.  We only call
	 * do_type_walk that does not increase the flag.
	 */
	irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	inc_master_type_visited();
	irg_walk(get_irg_end(irg), start_type_walk, NULL, &type_env);

	do_type_walk(NULL, get_irg_entity(irg), pre, post, env);

	do_type_walk(get_irg_frame_type(irg), NULL, pre, post, env);
	irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
}

static void type_walk_s2s_2(ir_type *const tp, type_walk_func *pre,
                            type_walk_func *post, void *env)
{
	if (type_visited(tp))
		return;

	/* iterate */
	mark_type_visited(tp);
	if (is_Class_type(tp)) {
		for (size_t i = 0, n = get_class_n_supertypes(tp); i < n; ++i) {
			type_walk_s2s_2(get_class_supertype(tp, i), pre, post, env);
		}
		/* execute pre method */
		if (pre)
			pre(tp, NULL, env);

		for (size_t i = 0, n = get_class_n_subtypes(tp); i < n; ++i) {
			type_walk_s2s_2(get_class_subtype(tp, i), pre, post, env);
		}

		/* execute post method */
		if (post)
			post(tp, NULL, env);
	}
}

void type_walk_super2sub(type_walk_func *pre, type_walk_func *post, void *env)
{
	irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	inc_master_type_visited();
	type_walk_s2s_2(get_glob_type(), pre, post, env);
	for (size_t i = 0, n_types = get_irp_n_types(); i < n_types; ++i) {
		type_walk_s2s_2(get_irp_type(i), pre, post, env);
	}
	irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
}

static void type_walk_super_2(ir_type *const tp, type_walk_func *pre,
                              type_walk_func *post, void *env)
{
	if (type_visited(tp))
		return;

	/* iterate */
	mark_type_visited(tp);
	if (is_Class_type(tp)) {
		/* execute pre method */
		if (pre)
			pre(tp, NULL, env);

		for (size_t i = 0, n = get_class_n_supertypes(tp); i < n; ++i) {
			type_walk_super_2(get_class_supertype(tp, i), pre, post, env);
		}

		/* execute post method */
		if (post)
			post(tp, NULL, env);
	}
}

void type_walk_super(type_walk_func *pre, type_walk_func *post, void *env)
{
	irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	inc_master_type_visited();
	type_walk_super_2(get_glob_type(), pre, post, env);
	for (size_t i = 0, n_types = get_irp_n_types(); i < n_types; ++i) {
		type_walk_super_2(get_irp_type(i), pre, post, env);
	}
	irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
}

static void class_walk_s2s_2(ir_type *tp, class_walk_func *pre,
                             class_walk_func *post, void *env)
{
	/* marked? */
	if (type_visited(tp))
		return;

	/* Assure all supertypes are visited before */
	for (size_t i = 0, n = get_class_n_supertypes(tp); i < n; ++i) {
		if (!type_visited(get_class_supertype(tp, i)))
			return;
	}

	mark_type_visited(tp);

	/* execute pre method */
	if (pre)
		pre(tp, env);

	for (size_t i = 0, n = get_class_n_subtypes(tp); i < n; ++i) {
		class_walk_s2s_2(get_class_subtype(tp, i), pre, post, env);
	}
	/* execute post method */
	if (post)
		post(tp, env);
}

void class_walk_super2sub(class_walk_func *pre, class_walk_func *post,
                          void *env)
{
	irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	inc_master_type_visited();
	for (size_t i = 0, n_types = get_irp_n_types(); i < n_types; i++) {
		ir_type *tp = get_irp_type(i);
		if (is_Class_type(tp) &&
		    (get_class_n_supertypes(tp) == 0) &&
		    !type_visited(tp) &&
		    (! is_frame_type(tp)) &&
		    (tp != get_glob_type())) {
			class_walk_s2s_2(tp, pre, post, env);
		}
	}
	irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
}

void walk_types_entities(ir_type *tp, entity_walk_func *doit, void *env)
{
	switch (get_type_opcode(tp)) {
	case tpo_class:
	case tpo_struct:
	case tpo_union:
	case tpo_segment:
		for (size_t i = 0, n = get_compound_n_members(tp); i < n; ++i)
			doit(get_compound_member(tp, i), env);
		return;
	case tpo_array:
	case tpo_code:
	case tpo_method:
	case tpo_pointer:
	case tpo_primitive:
	case tpo_uninitialized:
	case tpo_unknown:
		return;
	}
	panic("invalid type");
}