irprog.c 11.6 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

Matthias Braun's avatar
Matthias Braun committed
20
21
22
/**
 * @file
 * @brief    Entry point to the representation of a whole program.
Michael Beck's avatar
Michael Beck committed
23
 * @author   Goetz Lindenmaier, Michael Beck
Matthias Braun's avatar
Matthias Braun committed
24
25
 * @date     2000
 * @version  $Id$
Götz Lindenmaier's avatar
Götz Lindenmaier committed
26
 */
Matthias Braun's avatar
Matthias Braun committed
27
#include "config.h"
Boris Boesler's avatar
added    
Boris Boesler committed
28

29
#include <string.h>
30

Michael Beck's avatar
Michael Beck committed
31
32
#include "irprog_t.h"
#include "irgraph_t.h"
Michael Beck's avatar
Michael Beck committed
33
#include "irpass_t.h"
Michael Beck's avatar
Michael Beck committed
34
#include "array.h"
yb9976's avatar
yb9976 committed
35
#include "error.h"
Michael Beck's avatar
Michael Beck committed
36
37
#include "obst.h"
#include "irop_t.h"
38
#include "irmemory.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
39

40
/** The initial name of the irp program. */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
41
#define INITAL_PROG_NAME "no_name_set"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
42

Götz Lindenmaier's avatar
Götz Lindenmaier committed
43
44
/* A variable from where everything in the ir can be accessed. */
ir_prog *irp;
45
ir_prog *get_irp(void) { return irp; }
46
47
48
49
void set_irp(ir_prog *new_irp)
{
	irp = new_irp;
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
50

51
52
53
/**
 *  Create a new incomplete ir_prog.
 */
54
55
56
static ir_prog *new_incomplete_ir_prog(void)
{
	ir_prog *res = XMALLOCZ(ir_prog);
Michael Beck's avatar
Michael Beck committed
57

58
59
60
61
62
	res->kind           = k_ir_prog;
	res->graphs         = NEW_ARR_F(ir_graph *, 0);
	res->types          = NEW_ARR_F(ir_type *, 0);
	res->modes          = NEW_ARR_F(ir_mode *, 0);
	res->opcodes        = NEW_ARR_F(ir_op *, 0);
63
	res->global_asms    = NEW_ARR_F(ident *, 0);
64
	res->last_region_nr = 0;
65
	res->last_label_nr  = 1;  /* 0 is reserved as non-label */
Matthias Braun's avatar
Matthias Braun committed
66
	res->max_irg_idx    = 0;
67
68
69
	res->max_node_nr    = 0;
#ifndef NDEBUG
	res->reserved_resources = 0;
Till Riedel's avatar
Till Riedel committed
70
71
#endif

72
	return res;
73
74
}

75
76
77
78
79
80
/**
 * Completes an incomplete irprog.
 *
 * @param irp          the (yet incomplete) irp
 * @param module_name  the (module) name for this irp
 */
81
82
83
static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name)
{
	ir_segment_t s;
84

Michael Beck's avatar
Michael Beck committed
85
86
#define IDENT(x)  new_id_from_chars(x, sizeof(x) - 1)

87
	irp->name = new_id_from_str(module_name);
Michael Beck's avatar
Michael Beck committed
88
	irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(IDENT("GlobalType"));
89
	irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
90
		= new_type_class(IDENT("ThreadLocal"));
91
	irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
92
		= new_type_class(IDENT("Constructors"));
93
	irp->segment_types[IR_SEGMENT_DESTRUCTORS]
94
		= new_type_class(IDENT("Destructors"));
95
96
	/* Remove these types from type list.  Must be treated differently than
	   other types. */
97
98
	for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
		remove_irp_type(irp->segment_types[s]);
99
	}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
100

101
	/* Set these flags for debugging. */
102
103
	irp->segment_types[IR_SEGMENT_GLOBAL]->flags       |= tf_global_type;
	irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
104
105
	irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
	irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags  |= tf_destructors;
106

107
108
	/* The global type is a class, but we cannot derive from it, so set
	   the final property to assist optimizations that checks for it. */
109
	set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
110

111
	irp->const_code_irg             = new_const_code_irg();
112
113
114
115
116
117
	irp->phase_state                = phase_building;
	irp->outs_state                 = outs_none;
	irp->ip_outedges                = NULL;
	irp->trouts_state               = outs_none;
	irp->class_cast_state           = ir_class_casts_transitive;
	irp->globals_entity_usage_state = ir_entity_usage_not_computed;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
118

119
120
	current_ir_graph = irp->const_code_irg;

121
	return irp;
Michael Beck's avatar
Michael Beck committed
122
#undef IDENT
123
124
}

125
/* initializes ir_prog. Constructs only the basic lists. */
126
127
void init_irprog_1(void)
{
128
	irp = new_incomplete_ir_prog();
129
130
131
}

/* Completes ir_prog. */
132
133
void init_irprog_2(void)
{
134
	(void)complete_ir_prog(irp, INITAL_PROG_NAME);
135
136
137
138
}

/* Create a new ir prog. Automatically called by init_firm through
   init_irprog. */
139
140
ir_prog *new_ir_prog(const char *name)
{
Michael Beck's avatar
Michael Beck committed
141
	return complete_ir_prog(new_incomplete_ir_prog(), name);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
142
143
}

144
/* frees all memory used by irp.  Types in type list, irgs in irg
145
   list and entities in global type must be freed by hand before. */
146
147
148
149
150
void free_ir_prog(void)
{
	ir_segment_t s;
	for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
		free_type(irp->segment_types[s]);
151
	}
152

yb9976's avatar
yb9976 committed
153
	free_ir_graph(irp->const_code_irg);
154
155
156
	DEL_ARR_F(irp->graphs);
	DEL_ARR_F(irp->types);
	DEL_ARR_F(irp->modes);
yb9976's avatar
yb9976 committed
157
158

	finish_op();
159
	DEL_ARR_F(irp->opcodes);
160
	DEL_ARR_F(irp->global_asms);
161

162
163
164
	irp->name           = NULL;
	irp->const_code_irg = NULL;
	irp->kind           = k_BAD;
165
166
}

Michael Beck's avatar
Michael Beck committed
167
/*- Functions to access the fields of ir_prog -*/
Götz Lindenmaier's avatar
Götz Lindenmaier committed
168
169
170


/* Access the main routine of the compiled program. */
171
172
ir_graph *get_irp_main_irg(void)
{
173
174
	assert(irp);
	return irp->main_irg;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
175
176
}

177
178
void set_irp_main_irg(ir_graph *main_irg)
{
179
	assert(irp);
180
	irp->main_irg = main_irg;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
181
182
}

183
184
ir_type *(get_segment_type)(ir_segment_t segment)
{
185
186
187
	return _get_segment_type(segment);
}

188
189
190
191
192
193
194
195
void set_segment_type(ir_segment_t segment, ir_type *new_type)
{
	assert(segment <= IR_SEGMENT_LAST);
	irp->segment_types[segment] = new_type;
	/* segment types are not in the type list... */
	remove_irp_type(new_type);
}

196
197
ir_type *(get_glob_type)(void)
{
198
	return _get_glob_type();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
199
200
}

201
202
ir_type *(get_tls_type)(void)
{
203
	return _get_tls_type();
204
205
}

Götz Lindenmaier's avatar
Götz Lindenmaier committed
206
/* Adds irg to the list of ir graphs in irp. */
207
208
void add_irp_irg(ir_graph *irg)
{
209
210
211
	assert(irg != NULL);
	assert(irp && irp->graphs);
	ARR_APP1(ir_graph *, irp->graphs, irg);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
212
213
}

214
/* Removes irg from the list or irgs, shrinks the list by one. */
215
216
void remove_irp_irg_from_list(ir_graph *irg)
{
217
	int i, l, found = 0;
218
219

	assert(irg);
220
221
	l = ARR_LEN(irp->graphs);
	for (i = 0; i < l; i++) {
222
223
		if (irp->graphs[i] == irg) {
			found = 1;
224
			for (; i < l - 1; i++) {
225
226
				irp->graphs[i] = irp->graphs[i+1];
			}
227
			ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
228
229
230
			break;
		}
	}
231
232
}

Boris Boesler's avatar
Boris Boesler committed
233
/* Removes irg from the list or irgs, shrinks the list by one. */
234
235
void remove_irp_irg(ir_graph *irg)
{
236
237
	free_ir_graph(irg);
	remove_irp_irg_from_list(irg);
Boris Boesler's avatar
Boris Boesler committed
238
239
}

240
241
int (get_irp_n_irgs)(void)
{
242
	return _get_irp_n_irgs();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
243
244
}

245
246
ir_graph *(get_irp_irg)(int pos)
{
247
	return _get_irp_irg(pos);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
248
249
}

250
251
int get_irp_last_idx(void)
{
Matthias Braun's avatar
Matthias Braun committed
252
253
254
	return irp->max_irg_idx;
}

255
256
void set_irp_irg(int pos, ir_graph *irg)
{
257
258
259
	assert(irp && irg);
	assert(pos < (ARR_LEN(irp->graphs)));
	irp->graphs[pos] = irg;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
260
261
}

Götz Lindenmaier's avatar
Götz Lindenmaier committed
262
/* Adds type to the list of types in irp. */
263
264
void add_irp_type(ir_type *typ)
{
265
266
	assert(typ != NULL);
	assert(irp);
Michael Beck's avatar
Michael Beck committed
267
	ARR_APP1(ir_type *, irp->types, typ);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
268
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
269

Michael Beck's avatar
Michael Beck committed
270
/* Remove type from the list of types in irp. */
271
272
void remove_irp_type(ir_type *typ)
{
273
274
	int i;
	assert(typ);
275

Michael Beck's avatar
Michael Beck committed
276
	for (i = ARR_LEN(irp->types) - 1; i >= 0; i--) {
277
		if (irp->types[i] == typ) {
278
			for (; i < (ARR_LEN(irp->types)) - 1; i++) {
279
280
281
282
283
284
				irp->types[i] = irp->types[i+1];
			}
			ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
			break;
		}
	}
285
286
}

287
288
int (get_irp_n_types) (void)
{
289
	return _get_irp_n_types();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
290
291
}

292
293
ir_type *(get_irp_type) (int pos)
{
294
	return _get_irp_type(pos);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
295
296
}

297
298
void set_irp_type(int pos, ir_type *typ)
{
299
300
301
	assert(irp && typ);
	assert(pos < (ARR_LEN((irp)->types)));
	irp->types[pos] = typ;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
302
303
}

Michael Beck's avatar
Michael Beck committed
304
/* Returns the number of all modes in the irp. */
305
306
int (get_irp_n_modes)(void)
{
307
	return _get_irp_n_modes();
Michael Beck's avatar
Michael Beck committed
308
309
310
}

/* Returns the mode at position pos in the irp. */
311
312
ir_mode *(get_irp_mode)(int pos)
{
313
	return _get_irp_mode(pos);
Michael Beck's avatar
Michael Beck committed
314
315
316
}

/* Adds mode to the list of modes in irp. */
317
318
void add_irp_mode(ir_mode *mode)
{
319
320
321
	assert(mode != NULL);
	assert(irp);
	ARR_APP1(ir_mode *, irp->modes, mode);
Michael Beck's avatar
Michael Beck committed
322
323
}

324
/* Adds opcode to the list of opcodes in irp. */
325
326
void add_irp_opcode(ir_op *opcode)
{
327
328
	int    len;
	size_t code;
329
330
	assert(opcode != NULL);
	assert(irp);
331
332
	len  = ARR_LEN(irp->opcodes);
	code = opcode->code;
333
	if ((int) code >= len) {
334
335
336
337
338
339
		ARR_RESIZE(ir_op*, irp->opcodes, code+1);
		memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
	}

	assert(irp->opcodes[code] == NULL && "opcode registered twice");
	irp->opcodes[code] = opcode;
340
341
342
}

/* Removes opcode from the list of opcodes and shrinks the list by one. */
343
344
void remove_irp_opcode(ir_op *opcode)
{
Matthias Braun's avatar
Matthias Braun committed
345
	assert((int) opcode->code < ARR_LEN(irp->opcodes));
346
	irp->opcodes[opcode->code] = NULL;
347
348
349
}

/* Returns the number of all opcodes in the irp. */
350
351
int (get_irp_n_opcodes)(void)
{
352
	return _get_irp_n_opcodes();
353
354
355
}

/* Returns the opcode at position pos in the irp. */
356
357
ir_op *(get_irp_opcode)(int pos)
{
358
	return _get_irp_opcode(pos);
359
360
}

361
/* Sets the generic function pointer of all opcodes to NULL */
362
363
void  clear_irp_opcodes_generic_func(void)
{
364
	int i;
365

366
367
368
369
	for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
		ir_op *op = get_irp_opcode(i);
		op->ops.generic = (op_func)NULL;
	}
370
}
371

Michael Beck's avatar
Michael Beck committed
372
/*- File name / executable name or the like -*/
373
374
void   set_irp_prog_name(ident *name)
{
375
	irp->name = name;
376
}
377
378
int irp_prog_name_is_set(void)
{
379
	return irp->name != new_id_from_str(INITAL_PROG_NAME);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
380
}
381
382
ident *get_irp_ident(void)
{
383
	return irp->name;
384
}
385
386
const char  *get_irp_name(void)
{
387
	return get_id_str(irp->name);
388
389
390
}


391
392
ir_graph *(get_const_code_irg)(void)
{
393
	return _get_const_code_irg();
394
}
395

396
397
irg_phase_state get_irp_phase_state(void)
{
398
	return irp->phase_state;
399
}
400

401
402
void set_irp_phase_state(irg_phase_state s)
{
403
	irp->phase_state = s;
404
405
}

Michael Beck's avatar
Michael Beck committed
406
407
408
409
410
411
412
413
struct pass_t {
	ir_prog_pass_t  pass;
	irg_phase_state state;
};

/**
 * Wrapper for setting the state of a whole ir_prog.
 */
414
415
static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
{
Michael Beck's avatar
Michael Beck committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
	struct pass_t  *pass  = context;
	irg_phase_state state = pass->state;
	int             i;

	(void)irp;

	/* set the phase of all graphs */
	for (i = get_irp_n_irgs() - 1; i >= 0; --i)
		set_irg_phase_state(get_irp_irg(i), state);

	/* set the irp phase */
	set_irp_phase_state(state);

	return 0;
}

432
433
ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
{
Michael Beck's avatar
Michael Beck committed
434
435
436
437
438
439
440
441
442
443
444
445
446
	struct pass_t *pass = XMALLOCZ(struct pass_t);

	def_prog_pass_constructor(
		&pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
	pass->state = state;

	/* no dump/verify */
	pass->pass.verify_irprog = ir_prog_no_verify;
	pass->pass.dump_irprog   = ir_prog_no_dump;

	return &pass->pass;
}

447
448
irg_outs_state get_irp_ip_outs_state(void)
{
449
	return irp->outs_state;
450
}
451

452
453
void set_irp_ip_outs_inconsistent(void)
{
454
	irp->outs_state = outs_inconsistent;
455
}
456

457
458
void set_irp_ip_outedges(ir_node ** ip_outedges)
{
459
	irp->ip_outedges = ip_outedges;
460
461
}

462
463
ir_node** get_irp_ip_outedges(void)
{
464
	return irp->ip_outedges;
465
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
466

467
468
irg_callee_info_state get_irp_callee_info_state(void)
{
469
	return irp->callee_info_state;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
470
471
}

472
473
void set_irp_callee_info_state(irg_callee_info_state s)
{
474
475
476
477
	irp->callee_info_state = s;
}

/* Returns a new, unique exception region number. */
478
479
ir_exc_region_t (get_irp_next_region_nr)(void)
{
480
	return _get_irp_next_region_nr();
Götz Lindenmaier's avatar
Götz Lindenmaier committed
481
}
Michael Beck's avatar
Michael Beck committed
482
483

/* Returns a new, unique label number. */
484
485
ir_label_t (get_irp_next_label_nr)(void)
{
Michael Beck's avatar
Michael Beck committed
486
487
	return _get_irp_next_label_nr();
}
488
489

/* Add a new global asm include */
490
491
void add_irp_asm(ident *asm_string)
{
492
493
494
495
	ARR_APP1(ident *, irp->global_asms, asm_string);
}

/* Return the number of global asm includes. */
496
497
int get_irp_n_asms(void)
{
498
499
500
501
	return ARR_LEN(irp->global_asms);
}

/* Return the global asm include at position pos. */
502
503
ident *get_irp_asm(int pos)
{
Michael Beck's avatar
Michael Beck committed
504
	assert(0 <= pos && pos < get_irp_n_asms());
505
506
	return irp->global_asms[pos];
}
507
508

#ifndef NDEBUG
509
510
void irp_reserve_resources(ir_prog *irp, ir_resources_t resources)
{
Michael Beck's avatar
Michael Beck committed
511
	assert((resources & ~IR_RESOURCE_GLOBAL_MASK) == 0);
512
513
514
515
	assert((irp->reserved_resources & resources) == 0);
	irp->reserved_resources |= resources;
}

516
517
void irp_free_resources(ir_prog *irp, ir_resources_t resources)
{
518
519
520
521
	assert((irp->reserved_resources & resources) == resources);
	irp->reserved_resources &= ~resources;
}

522
523
ir_resources_t irp_resources_reserved(const ir_prog *irp)
{
524
525
526
	return irp->reserved_resources;
}
#endif