irprog.c 6.07 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Matthias Braun's avatar
Matthias Braun committed
6
7
8
/**
 * @file
 * @brief    Entry point to the representation of a whole program.
Michael Beck's avatar
Michael Beck committed
9
 * @author   Goetz Lindenmaier, Michael Beck
Matthias Braun's avatar
Matthias Braun committed
10
 * @date     2000
Götz Lindenmaier's avatar
Götz Lindenmaier committed
11
 */
12
#include "ident_t.h"
Michael Beck's avatar
Michael Beck committed
13
14
15
16
17
#include "irprog_t.h"
#include "irgraph_t.h"
#include "array.h"
#include "obst.h"
#include "irop_t.h"
18
#include "irmemory.h"
19
#include "ircons.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
20

21
/** The initial name of the irp program. */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
22
#define INITAL_PROG_NAME "no_name_set"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
23

Götz Lindenmaier's avatar
Götz Lindenmaier committed
24
ir_prog *irp;
25
ir_prog *get_irp(void) { return irp; }
26
27
28
29
void set_irp(ir_prog *new_irp)
{
	irp = new_irp;
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
30

31
32
33
/**
 *  Create a new incomplete ir_prog.
 */
34
35
36
static ir_prog *new_incomplete_ir_prog(void)
{
	ir_prog *res = XMALLOCZ(ir_prog);
Michael Beck's avatar
Michael Beck committed
37

38
39
	res->graphs         = NEW_ARR_F(ir_graph *, 0);
	res->types          = NEW_ARR_F(ir_type *, 0);
40
	res->global_asms    = NEW_ARR_F(ident *, 0);
41
	res->last_label_nr  = 1;  /* 0 is reserved as non-label */
Matthias Braun's avatar
Matthias Braun committed
42
	res->max_irg_idx    = 0;
43
44
	res->max_node_nr    = 0;
#ifndef NDEBUG
45
	res->reserved_resources = IRP_RESOURCE_NONE;
Till Riedel's avatar
Till Riedel committed
46
#endif
Matthias Braun's avatar
Matthias Braun committed
47
	res->globals        = pmap_create();
Till Riedel's avatar
Till Riedel committed
48

49
	return res;
50
51
}

52
53
54
55
56
57
/**
 * Completes an incomplete irprog.
 *
 * @param irp          the (yet incomplete) irp
 * @param module_name  the (module) name for this irp
 */
58
static void complete_ir_prog(ir_prog *irp, const char *module_name)
59
{
60
61
	ir_init_type(irp);
	ir_init_entity(irp);
Michael Beck's avatar
Michael Beck committed
62

63
	irp->name = new_id_from_str(module_name);
64
65
66
67
68
	irp->segment_types[IR_SEGMENT_GLOBAL]       = new_type_segment(NEW_IDENT("GlobalType"),          tf_global_type);
	irp->segment_types[IR_SEGMENT_THREAD_LOCAL] = new_type_segment(NEW_IDENT("ThreadLocal"),         tf_tls_type);
	irp->segment_types[IR_SEGMENT_CONSTRUCTORS] = new_type_segment(NEW_IDENT("Constructors"),        tf_info);
	irp->segment_types[IR_SEGMENT_DESTRUCTORS]  = new_type_segment(NEW_IDENT("Destructors"),         tf_info);
	irp->segment_types[IR_SEGMENT_JCR]          = new_type_segment(NEW_IDENT("Java Class Registry"), tf_info);
69

70
	irp->const_code_irg             = new_const_code_irg();
71
	irp->globals_entity_usage_state = ir_entity_usage_not_computed;
72
73
}

74
75
void init_irprog_1(void)
{
76
	irp = new_incomplete_ir_prog();
77
78
}

79
80
void init_irprog_2(void)
{
81
	complete_ir_prog(irp, INITAL_PROG_NAME);
82
83
}

84
85
ir_prog *new_ir_prog(const char *name)
{
86
87
88
	ir_prog *irp = new_incomplete_ir_prog();
	complete_ir_prog(irp, name);
	return irp;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
89
90
}

91
92
void free_ir_prog(void)
{
93
94
95
	if (irp == NULL)
		return;

96
	/* must iterate backwards here */
97
98
99
	foreach_irp_irg_r(i, irg) {
		free_ir_graph(irg);
	}
100

101
102
	/* free entities first to avoid entity types being destroyed before
	 * the entities using them */
103
	for (size_t i = get_irp_n_types(); i > 0;)
104
105
		free_type_entities(get_irp_type(--i));

106
	for (size_t i = get_irp_n_types(); i > 0;)
107
		free_type(get_irp_type(--i));
108

yb9976's avatar
yb9976 committed
109
	free_ir_graph(irp->const_code_irg);
Matthias Braun's avatar
Matthias Braun committed
110
111
112

	ir_finish_type(irp);

113
114
	DEL_ARR_F(irp->graphs);
	DEL_ARR_F(irp->types);
yb9976's avatar
yb9976 committed
115

116
	DEL_ARR_F(irp->global_asms);
117

Matthias Braun's avatar
Matthias Braun committed
118
119
	pmap_destroy(irp->globals);

120
121
	irp->name           = NULL;
	irp->const_code_irg = NULL;
Matthias Braun's avatar
Matthias Braun committed
122
123
	free(irp);
	irp = NULL;
124
125
}

126
127
ir_graph *get_irp_main_irg(void)
{
128
129
	assert(irp);
	return irp->main_irg;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
130
131
}

132
133
void set_irp_main_irg(ir_graph *main_irg)
{
134
	assert(irp);
135
	irp->main_irg = main_irg;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
136
137
}

138
139
ir_type *(get_segment_type)(ir_segment_t segment)
{
140
	return get_segment_type_(segment);
141
142
}

143
144
145
146
147
148
void set_segment_type(ir_segment_t segment, ir_type *new_type)
{
	assert(segment <= IR_SEGMENT_LAST);
	irp->segment_types[segment] = new_type;
}

149
150
ir_type *(get_glob_type)(void)
{
151
	return get_glob_type_();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
152
153
}

154
155
ir_type *(get_tls_type)(void)
{
156
	return get_tls_type_();
157
158
}

Matthias Braun's avatar
Matthias Braun committed
159
160
161
162
163
ir_entity *ir_get_global(ident *name)
{
	return pmap_get(ir_entity, irp->globals, name);
}

164
165
void add_irp_irg(ir_graph *irg)
{
166
167
168
	assert(irg != NULL);
	assert(irp && irp->graphs);
	ARR_APP1(ir_graph *, irp->graphs, irg);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
169
170
}

171
void remove_irp_irg(ir_graph *irg)
172
{
173
	size_t i, l;
174
175

	assert(irg);
176
	l = ARR_LEN(irp->graphs);
177
	for (i = 0; i < l; ++i) {
178
		if (irp->graphs[i] == irg) {
179
			for (; i < l - 1; ++i) {
180
181
				irp->graphs[i] = irp->graphs[i+1];
			}
182
			ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
183
184
185
			break;
		}
	}
186
187
}

Matthias Braun's avatar
Matthias Braun committed
188
size_t (get_irp_n_irgs)(void)
189
{
190
	return get_irp_n_irgs_();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
191
192
}

Matthias Braun's avatar
Matthias Braun committed
193
ir_graph *(get_irp_irg)(size_t pos)
194
{
195
	return get_irp_irg_(pos);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
196
197
}

198
size_t get_irp_last_idx(void)
199
{
Matthias Braun's avatar
Matthias Braun committed
200
201
202
	return irp->max_irg_idx;
}

Matthias Braun's avatar
Matthias Braun committed
203
void set_irp_irg(size_t pos, ir_graph *irg)
204
{
205
	assert(irp && irg);
Matthias Braun's avatar
Matthias Braun committed
206
	assert(pos < ARR_LEN(irp->graphs));
207
	irp->graphs[pos] = irg;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
208
209
}

210
211
void add_irp_type(ir_type *typ)
{
212
213
	assert(typ != NULL);
	assert(irp);
Michael Beck's avatar
Michael Beck committed
214
	ARR_APP1(ir_type *, irp->types, typ);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
215
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
216

217
218
void remove_irp_type(ir_type *typ)
{
219
	size_t i, l;
220
	assert(typ);
221

222
223
	l = ARR_LEN(irp->types);
	for (i = 0; i < l; ++i) {
224
		if (irp->types[i] == typ) {
225
			for (; i < l - 1; ++i) {
226
227
				irp->types[i] = irp->types[i+1];
			}
228
			ARR_SETLEN(ir_type *, irp->types, l - 1);
229
230
231
			break;
		}
	}
232
233
}

Matthias Braun's avatar
Matthias Braun committed
234
size_t (get_irp_n_types) (void)
235
{
236
	return get_irp_n_types_();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
237
238
}

Matthias Braun's avatar
Matthias Braun committed
239
ir_type *(get_irp_type) (size_t pos)
240
{
241
	return get_irp_type_(pos);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
242
243
}

Matthias Braun's avatar
Matthias Braun committed
244
void set_irp_type(size_t pos, ir_type *typ)
245
{
246
	assert(irp && typ);
Matthias Braun's avatar
Matthias Braun committed
247
	assert(pos < ARR_LEN((irp)->types));
248
	irp->types[pos] = typ;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
249
250
}

Matthias Braun's avatar
Matthias Braun committed
251
void set_irp_prog_name(ident *name)
252
{
253
	irp->name = name;
254
}
255
256
int irp_prog_name_is_set(void)
{
257
	return irp->name != new_id_from_str(INITAL_PROG_NAME);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
258
}
259
260
ident *get_irp_ident(void)
{
261
	return irp->name;
262
}
263
264
const char  *get_irp_name(void)
{
265
	return get_id_str(irp->name);
266
267
268
}


269
270
ir_graph *(get_const_code_irg)(void)
{
271
	return get_const_code_irg_();
272
}
273

274
275
irg_callee_info_state get_irp_callee_info_state(void)
{
276
	return irp->callee_info_state;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
277
278
}

279
280
void set_irp_callee_info_state(irg_callee_info_state s)
{
281
282
283
	irp->callee_info_state = s;
}

284
285
ir_label_t (get_irp_next_label_nr)(void)
{
286
	return get_irp_next_label_nr_();
Michael Beck's avatar
Michael Beck committed
287
}
288

289
290
void add_irp_asm(ident *asm_string)
{
291
292
293
	ARR_APP1(ident *, irp->global_asms, asm_string);
}

Matthias Braun's avatar
Matthias Braun committed
294
size_t get_irp_n_asms(void)
295
{
296
297
298
	return ARR_LEN(irp->global_asms);
}

Matthias Braun's avatar
Matthias Braun committed
299
ident *get_irp_asm(size_t pos)
300
{
Matthias Braun's avatar
Matthias Braun committed
301
	assert(pos < get_irp_n_asms());
302
303
	return irp->global_asms[pos];
}
304

305
void (irp_reserve_resources)(ir_prog *irp, irp_resources_t resources)
306
{
307
	irp_reserve_resources_(irp, resources);
308
309
}

310
void (irp_free_resources)(ir_prog *irp, irp_resources_t resources)
311
{
312
	irp_free_resources_(irp, resources);
313
314
}

315
irp_resources_t (irp_resources_reserved)(const ir_prog *irp)
316
{
317
	return irp_resources_reserved_(irp);
318
}