entity.c 25.1 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
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   Representation of all program known entities.
 * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10
 */
11
12
#include <stdlib.h>
#include <stddef.h>
13

Michael Beck's avatar
Michael Beck committed
14
15
16
#include "xmalloc.h"
#include "entity_t.h"
#include "array.h"
17
#include "util.h"
Michael Beck's avatar
Michael Beck committed
18
19
20
21
#include "irhooks.h"
#include "irprog_t.h"
#include "ircons.h"
#include "tv_t.h"
22
23
24
#include "irdump.h"
#include "irgraph_t.h"
#include "callgraph.h"
25
#include "error.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
26

27
/** The name of the unknown entity. */
28
#define UNKNOWN_ENTITY_NAME "unknown_entity"
29

30
31
32
33
34
ir_entity *get_unknown_entity(void)
{
	return irp->unknown_entity;
}

35
36
37
/*-----------------------------------------------------------------*/
/* ENTITY                                                          */
/*-----------------------------------------------------------------*/
38

Matthias Braun's avatar
Matthias Braun committed
39
40
static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
                                    ident *name, ir_type *type, dbg_info *dbgi)
Christian Schäfer's avatar
Christian Schäfer committed
41
{
Matthias Braun's avatar
Matthias Braun committed
42
	assert(owner != NULL);
43

44
	ir_entity *res = XMALLOCZ(ir_entity);
45
46
47
48
49
	res->kind    = k_entity;
	res->name    = name;
	res->type    = type;
	res->owner   = owner;

Matthias Braun's avatar
Matthias Braun committed
50
	res->entity_kind          = kind;
51
	res->volatility           = volatility_non_volatile;
Matthias Braun's avatar
Matthias Braun committed
52
	res->aligned              = align_is_aligned;
53
	res->usage                = ir_usage_unknown;
Matthias Braun's avatar
Matthias Braun committed
54
	res->visibility           = ir_visibility_external;
55
56
57
58
59
#ifdef DEBUG_libfirm
	res->nr = get_irp_new_node_nr();
#endif

	/* Remember entity in its owner. */
60
	if (is_compound_type(owner))
61
62
63
64
65
66
67
68
69
70
71
		add_compound_member(owner, res);

	res->visit = 0;
	set_entity_dbg_info(res, dbgi);

	return res;
}

ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
                        dbg_info *db)
{
Matthias Braun's avatar
Matthias Braun committed
72
	ir_entity *res;
73
	if (is_Method_type(type)) {
74
		ir_graph *irg = get_const_code_irg();
75
		symconst_symbol sym;
Matthias Braun's avatar
Matthias Braun committed
76
		res = intern_new_entity(owner, IR_ENTITY_METHOD, name, type, db);
77
		sym.entity_p            = res;
Matthias Braun's avatar
Matthias Braun committed
78
79
80
81
82
83
84
		set_atomic_ent_value(res, new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent));
		res->linkage                     = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties    = get_method_additional_properties(type);
		res->attr.mtd_attr.vtable_number = IR_VTABLE_NUM_NOT_SET;
		res->attr.mtd_attr.param_access  = NULL;
		res->attr.mtd_attr.param_weight  = NULL;
		res->attr.mtd_attr.irg           = NULL;
85
	} else if (is_compound_type(owner) && !(owner->flags & tf_segment)) {
Matthias Braun's avatar
Matthias Braun committed
86
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type, db);
87
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
88
89
	} else {
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type, db);
90
91
	}

92
93
94
	hook_new_entity(res);
	return res;
}
95

96
97
98
99
ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
{
	return new_d_entity(owner, name, type, NULL);
}
100

101
102
103
104
105
106
static ident *make_parameter_entity_name(size_t pos)
{
	char buf[64];
	snprintf(buf, sizeof(buf), "parameter.%lu", (unsigned long) pos);
	return new_id_from_str(buf);
}
Christian Schäfer's avatar
Christian Schäfer committed
107

108
109
110
ir_entity *new_d_parameter_entity(ir_type *owner, size_t pos, ir_type *type,
                                  dbg_info *dbgi)
{
Matthias Braun's avatar
Matthias Braun committed
111
112
113
	ident     *name = make_parameter_entity_name(pos);
	ir_entity *res
		= intern_new_entity(owner, IR_ENTITY_PARAMETER, name, type, dbgi);
114
	res->attr.compound_member.offset = -1;
115
	res->attr.parameter.number = pos;
116
117
	hook_new_entity(res);
	return res;
118
}
119

120
ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
121
{
122
	return new_d_parameter_entity(owner, pos, type, NULL);
123
}
124

Matthias Braun's avatar
Matthias Braun committed
125
126
127
128
129
ir_entity *new_d_label_entity(ir_label_t label, dbg_info *dbgi)
{
	ident *name = id_unique("label_%u");
	ir_type *global_type = get_glob_type();
	ir_entity *res
130
		= intern_new_entity(global_type, IR_ENTITY_LABEL, name, get_code_type(),
Matthias Braun's avatar
Matthias Braun committed
131
132
133
134
135
136
137
138
139
140
141
		                    dbgi);
	res->attr.code_attr.label = label;
	hook_new_entity(res);
	return res;
}

ir_entity *new_label_entity(ir_label_t label)
{
	return new_d_label_entity(label, NULL);
}

142
143
144
145
146
/**
 * Free entity attributes.
 *
 * @param ent  the entity
 */
Matthias Braun's avatar
Matthias Braun committed
147
148
static void free_entity_attrs(ir_entity *ent)
{
149
150
151
	if (ent->overwrites != NULL) {
		DEL_ARR_F(ent->overwrites);
		ent->overwrites = NULL;
152
	}
153
154
155
156
157
	if (ent->overwrittenby != NULL) {
		DEL_ARR_F(ent->overwrittenby);
		ent->overwrittenby = NULL;
	}

Matthias Braun's avatar
Matthias Braun committed
158
159
160
	if (ent->initializer != NULL) {
		/* TODO: free initializers */
	}
161
	if (ent->entity_kind == IR_ENTITY_METHOD) {
162
163
164
165
166
167
168
169
170
		if (ent->attr.mtd_attr.param_access) {
			DEL_ARR_F(ent->attr.mtd_attr.param_access);
			ent->attr.mtd_attr.param_access = NULL;
		}
		if (ent->attr.mtd_attr.param_weight) {
			DEL_ARR_F(ent->attr.mtd_attr.param_weight);
			ent->attr.mtd_attr.param_weight = NULL;
		}
	}
171
}
Christian Schäfer's avatar
Christian Schäfer committed
172

173
174
175
176
177
178
179
/**
 * Creates a deep copy of an entity.
 */
static ir_entity *deep_entity_copy(ir_entity *old)
{
	ir_entity *newe = XMALLOC(ir_entity);

180
	*newe = *old;
Matthias Braun's avatar
Matthias Braun committed
181
182
	if (old->initializer != NULL) {
		/* FIXME: the initializers are NOT copied */
183
184
185
186
187
	} else if (is_method_entity(old)) {
		/* do NOT copy them, reanalyze. This might be the best solution */
		newe->attr.mtd_attr.param_access = NULL;
		newe->attr.mtd_attr.param_weight = NULL;
	}
188
189
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
190
191
192
193

#ifdef DEBUG_libfirm
	newe->nr = get_irp_new_node_nr();
#endif
194
	hook_new_entity(newe);
195
196
	return newe;
}
197

198
ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
199
{
200
201
	assert(is_entity(old));
	assert(is_compound_type(new_owner));
202
	assert(get_type_state(new_owner) != layout_fixed);
203
204
205
206
	if (old->owner == new_owner)
		return old;

	/* create a deep copy so we are safe of aliasing and double-freeing. */
Matthias Braun's avatar
Matthias Braun committed
207
	ir_entity *newe = deep_entity_copy(old);
208
	newe->owner = new_owner;
209
	add_compound_member(new_owner, newe);
210

211
	return newe;
212
}
213

214
ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
215
{
Matthias Braun's avatar
Matthias Braun committed
216
	assert(old->kind == k_entity);
217
218
219
	if (old->name == new_name)
		return old;

Matthias Braun's avatar
Matthias Braun committed
220
221
	ir_entity *newe = deep_entity_copy(old);
	newe->name    = new_name;
222
	newe->ld_name = NULL;
223
	add_compound_member(old->owner, newe);
224

225
	return newe;
226
}
227

228
void free_entity(ir_entity *ent)
229
{
230
	if (is_compound_type(ent->owner))
231
		remove_compound_member(ent->owner, ent);
232

Matthias Braun's avatar
Matthias Braun committed
233
	assert(ent->kind == k_entity);
234
	free_entity_attrs(ent);
235
#ifdef DEBUG_libfirm
236
	ent->kind = k_BAD;
237
#endif
238
	free(ent);
239
}
240

241
long get_entity_nr(const ir_entity *ent)
242
{
Matthias Braun's avatar
Matthias Braun committed
243
	assert(ent->kind == k_entity);
244
#ifdef DEBUG_libfirm
245
	return ent->nr;
246
#else
247
	return (long)PTR_TO_INT(ent);
248
#endif
249
}
250

251
252
const char *(get_entity_name)(const ir_entity *ent)
{
253
	return _get_entity_name(ent);
Matthias Braun's avatar
Matthias Braun committed
254
}
Christian Schäfer's avatar
Christian Schäfer committed
255

256
257
ident *(get_entity_ident)(const ir_entity *ent)
{
258
	return _get_entity_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
259
}
Michael Beck's avatar
Michael Beck committed
260

261
262
void (set_entity_ident)(ir_entity *ent, ident *id)
{
263
	_set_entity_ident(ent, id);
Matthias Braun's avatar
Matthias Braun committed
264
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
265

266
267
ir_type *(get_entity_owner)(const ir_entity *ent)
{
268
	return _get_entity_owner(ent);
Matthias Braun's avatar
Matthias Braun committed
269
}
Christian Schäfer's avatar
Christian Schäfer committed
270

271
void set_entity_owner(ir_entity *ent, ir_type *owner)
272
{
273
274
	assert(is_entity(ent));
	assert(is_compound_type(owner));
275
276
277

	remove_compound_member(ent->owner, ent);
	add_compound_member(owner, ent);
278
	ent->owner = owner;
Matthias Braun's avatar
Matthias Braun committed
279
}
Christian Schäfer's avatar
Christian Schäfer committed
280

Matthias Braun's avatar
Matthias Braun committed
281
282
ident *(get_entity_ld_ident)(const ir_entity *ent)
{
283
	return _get_entity_ld_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
284
}
285

286
287
void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
{
288
	_set_entity_ld_ident(ent, ld_ident);
Matthias Braun's avatar
Matthias Braun committed
289
}
Christian Schäfer's avatar
Christian Schäfer committed
290

Matthias Braun's avatar
Matthias Braun committed
291
292
const char *(get_entity_ld_name)(const ir_entity *ent)
{
293
	return _get_entity_ld_name(ent);
Matthias Braun's avatar
Matthias Braun committed
294
}
295

296
297
298
299
300
int entity_has_ld_ident(const ir_entity *entity)
{
	return entity->ld_name != NULL;
}

301
302
ir_type *(get_entity_type)(const ir_entity *ent)
{
303
	return _get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
304
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
305

Matthias Braun's avatar
Matthias Braun committed
306
307
308
309
310
311
312
313
314
315
void set_entity_type(ir_entity *ent, ir_type *type)
{
	switch (ent->entity_kind) {
	case IR_ENTITY_METHOD:
		assert(is_Method_type(type));
		break;
	case IR_ENTITY_NORMAL:
		assert(!is_Method_type(type));
		break;
	case IR_ENTITY_LABEL:
316
		assert(type == get_code_type());
Matthias Braun's avatar
Matthias Braun committed
317
318
319
320
321
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		break;
	}
	ent->type = type;
Matthias Braun's avatar
Matthias Braun committed
322
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
323

324
325
ir_volatility (get_entity_volatility)(const ir_entity *ent)
{
326
	return _get_entity_volatility(ent);
Matthias Braun's avatar
Matthias Braun committed
327
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
328

329
330
void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
{
331
	_set_entity_volatility(ent, vol);
Matthias Braun's avatar
Matthias Braun committed
332
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
333

334
const char *get_volatility_name(ir_volatility var)
335
{
336
#define X(a)    case a: return #a
337
338
339
340
	switch (var) {
	X(volatility_non_volatile);
	X(volatility_is_volatile);
	}
341
#undef X
Matthias Braun's avatar
Matthias Braun committed
342
	return "BAD VALUE";
343
}
344

345
346
ir_align (get_entity_aligned)(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
347
348
	return _get_entity_aligned(ent);
}
349

350
351
void (set_entity_aligned)(ir_entity *ent, ir_align a)
{
Matthias Braun's avatar
Matthias Braun committed
352
353
	_set_entity_aligned(ent, a);
}
354

355
356
unsigned (get_entity_alignment)(const ir_entity *ent)
{
357
358
359
	return _get_entity_alignment(ent);
}

360
361
void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
{
362
363
364
	_set_entity_alignment(ent, alignment);
}

Michael Beck's avatar
Michael Beck committed
365
366
367
368
369
370
371
372
const char *get_align_name(ir_align a)
{
#define X(a)    case a: return #a
	switch (a) {
	X(align_non_aligned);
	X(align_is_aligned);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
373
	return "BAD VALUE";
374
}
Michael Beck's avatar
Michael Beck committed
375

376
void set_entity_label(ir_entity *ent, ir_label_t label)
377
{
Matthias Braun's avatar
Matthias Braun committed
378
	assert(ent->entity_kind == IR_ENTITY_LABEL);
379
380
381
382
383
	ent->attr.code_attr.label = label;
}

ir_label_t get_entity_label(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
384
	assert(ent->entity_kind == IR_ENTITY_LABEL);
385
386
387
	return ent->attr.code_attr.label;
}

388
389
390
391
392
393
394
void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
{
	entity->visibility = visibility;
}

ir_visibility get_entity_visibility(const ir_entity *entity)
{
395
	return (ir_visibility)entity->visibility;
396
397
}

Matthias Braun's avatar
Matthias Braun committed
398
399
400
401
402
403
404
405
406
void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage = linkage;
}

ir_linkage (get_entity_linkage)(const ir_entity *entity)
{
	return get_entity_linkage(entity);
}
407

Matthias Braun's avatar
Matthias Braun committed
408
409
410
411
void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage |= linkage;
}
412

Matthias Braun's avatar
Matthias Braun committed
413
414
415
416
void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage &= ~linkage;
}
417

418
419
int (is_entity_compiler_generated)(const ir_entity *ent)
{
Michael Beck's avatar
Michael Beck committed
420
	return _is_entity_compiler_generated(ent);
421
}
422

423
424
void (set_entity_compiler_generated)(ir_entity *ent, int flag)
{
Michael Beck's avatar
Michael Beck committed
425
	_set_entity_compiler_generated(ent, flag);
426
}
427

428
429
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
430
431
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
432

433
434
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
435
436
	_set_entity_usage(ent, flags);
}
437

438
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
439
440
{
	ir_initializer_t *initializer = get_entity_initializer(entity);
441

Matthias Braun's avatar
Matthias Braun committed
442
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
443
444
445
446
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
447

Matthias Braun's avatar
Matthias Braun committed
448
449
450
451
452
453
454
	switch (get_initializer_kind(initializer)) {
	case IR_INITIALIZER_NULL: {
		ir_type *type = get_entity_type(entity);
		ir_mode *mode = get_type_mode(type);
		return new_r_Const(get_const_code_irg(), get_mode_null(mode));
	}
	case IR_INITIALIZER_TARVAL: {
Matthias Braun's avatar
Matthias Braun committed
455
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
456
457
458
459
460
461
462
463
		return new_r_Const(get_const_code_irg(), tv);
	}
	case IR_INITIALIZER_CONST:
		return get_initializer_const_value(initializer);
	case IR_INITIALIZER_COMPOUND:
		panic("compound initializer in atomic entity not allowed (%+F)", entity);
	}

464
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
465
466
467
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
468
{
Matthias Braun's avatar
Matthias Braun committed
469
470
	assert(is_atomic_entity(entity));
	assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
Matthias Braun's avatar
Matthias Braun committed
471
	ir_initializer_t *initializer = create_initializer_const(val);
Matthias Braun's avatar
Matthias Braun committed
472
473
	entity->initializer = initializer;
}
474

475
476
int is_irn_const_expression(ir_node *n)
{
477
	/* we are in danger iff an exception will arise. TODO: be more precisely,
Matthias Braun's avatar
Matthias Braun committed
478
	 * for instance Div. will NOT rise if divisor != 0 */
479
480
481
482
483
484
485
486
487
488
489
490
491
492
	if (is_binop(n) && !is_fragile_op(n))
		return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));

	switch (get_irn_opcode(n)) {
	case iro_Const:
	case iro_SymConst:
	case iro_Unknown:
		return 1;
	case iro_Conv:
		return is_irn_const_expression(get_irn_n(n, 0));
	default:
		break;
	}
	return 0;
493
}
Matthias Heil's avatar
Matthias Heil committed
494

495
ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
496
{
497
	ir_graph *irg = get_irn_irg(block);
498

Matthias Braun's avatar
Matthias Braun committed
499
500
501
502
	/* @@@ GL I think we should implement this using the routines from irgopt
	 * for dead node elimination/inlineing. */
	ir_mode *m = get_irn_mode(n);
	ir_node *nn;
503
504
	switch (get_irn_opcode(n)) {
	case iro_Const:
505
		nn = new_rd_Const(dbg, irg, get_Const_tarval(n));
506
507
		break;
	case iro_SymConst:
508
		nn = new_rd_SymConst(dbg, irg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n));
509
510
		break;
	case iro_Add:
511
512
513
514
		nn = new_rd_Add(dbg, block,
		                copy_const_value(dbg, get_Add_left(n), block),
		                copy_const_value(dbg, get_Add_right(n), block), m);
		break;
515
	case iro_Sub:
516
517
518
519
		nn = new_rd_Sub(dbg, block,
		                copy_const_value(dbg, get_Sub_left(n), block),
		                copy_const_value(dbg, get_Sub_right(n), block), m);
		break;
520
	case iro_Mul:
521
522
523
524
		nn = new_rd_Mul(dbg, block,
		                copy_const_value(dbg, get_Mul_left(n), block),
		                copy_const_value(dbg, get_Mul_right(n), block), m);
		break;
525
	case iro_And:
526
527
528
529
		nn = new_rd_And(dbg, block,
		                copy_const_value(dbg, get_And_left(n), block),
		                copy_const_value(dbg, get_And_right(n), block), m);
		break;
530
	case iro_Or:
531
532
533
534
		nn = new_rd_Or(dbg, block,
		               copy_const_value(dbg, get_Or_left(n), block),
		               copy_const_value(dbg, get_Or_right(n), block), m);
		break;
535
	case iro_Eor:
536
537
538
539
		nn = new_rd_Eor(dbg, block,
		                copy_const_value(dbg, get_Eor_left(n), block),
		                copy_const_value(dbg, get_Eor_right(n), block), m);
		break;
540
	case iro_Conv:
541
542
543
		nn = new_rd_Conv(dbg, block,
		                 copy_const_value(dbg, get_Conv_op(n), block), m);
		break;
544
545
546
547
548
549
550
551
	case iro_Minus:
		nn = new_rd_Minus(dbg, block,
		                  copy_const_value(dbg, get_Minus_op(n), block), m);
		break;
	case iro_Not:
		nn = new_rd_Not(dbg, block,
		                copy_const_value(dbg, get_Not_op(n), block), m);
		break;
552
	case iro_Unknown:
Matthias Braun's avatar
Matthias Braun committed
553
554
		nn = new_r_Unknown(irg, m);
		break;
555
	default:
556
		panic("opcode invalid or not implemented %+F", n);
557
558
	}
	return nn;
559
}
560

561
562
563
564
565
566
567
568
569
570
const char *get_initializer_kind_name(ir_initializer_kind_t ini)
{
#define X(a)    case a: return #a
	switch (ini) {
	X(IR_INITIALIZER_CONST);
	X(IR_INITIALIZER_TARVAL);
	X(IR_INITIALIZER_NULL);
	X(IR_INITIALIZER_COMPOUND);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
571
	return "BAD VALUE";
572
573
}

574
575
576
577
578
579
580
581
582
583
584
585
static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };

ir_initializer_t *get_initializer_null(void)
{
	return &null_initializer;
}

ir_initializer_t *create_initializer_const(ir_node *value)
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
586
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
587
588
589
590
591
592
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
593
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
594
595
596
597
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
598
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
599
600
601
602
603
604
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

605
ir_initializer_t *create_initializer_compound(size_t n_entries)
606
607
608
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
609
610
611
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
612

613
614
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
615
616
617
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
618
	for (size_t i = 0; i < n_entries; ++i) {
619
620
621
622
623
624
625
626
627
		initializer->compound.initializers[i] = get_initializer_null();
	}

	return initializer;
}

ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
{
	assert(initializer->kind == IR_INITIALIZER_CONST);
628
	return skip_Id(initializer->consti.value);
629
630
}

Matthias Braun's avatar
Matthias Braun committed
631
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
632
633
634
635
636
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

637
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
638
639
640
641
642
643
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
644
                                    size_t index, ir_initializer_t *value)
645
646
647
648
649
650
651
652
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	initializer->compound.initializers[index] = value;
}

ir_initializer_t *get_initializer_compound_value(
653
		const ir_initializer_t *initializer, size_t index)
654
655
656
657
658
659
660
661
662
663
664
665
666
667
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	return initializer->compound.initializers[index];
}

ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
{
	return initializer->kind;
}

static void check_entity_initializer(ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
668
669
#ifndef NDEBUG
	ir_initializer_t *initializer = entity->initializer;
670
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
671
672
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
673
		assert(is_compound_type(entity_tp) || is_Array_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
674
675
		break;
	case IR_INITIALIZER_CONST:
676
677
678
		/* methods are initialized by a SymConst */
		assert(is_atomic_type(entity_tp) || is_Method_type(entity_tp));
		break;
Matthias Braun's avatar
Matthias Braun committed
679
	case IR_INITIALIZER_TARVAL:
680
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
681
682
683
684
		break;
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
685
686
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
687
#endif
688
689
690
691
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
Matthias Braun's avatar
Matthias Braun committed
692
	entity->initializer = initializer;
693
694
695
	check_entity_initializer(entity);
}

696
697
int has_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
698
	return entity->initializer != NULL;
699
700
}

701
702
ir_initializer_t *get_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
703
	return entity->initializer;
704
705
}

706
707
int (get_entity_offset)(const ir_entity *ent)
{
708
	return _get_entity_offset(ent);
709
}
Michael Beck's avatar
Michael Beck committed
710

711
712
void (set_entity_offset)(ir_entity *ent, int offset)
{
713
	_set_entity_offset(ent, offset);
714
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
715

716
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
717
{
718
	return _get_entity_bitfield_offset(ent);
719
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
720

721
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
722
{
723
724
725
726
727
728
729
730
731
732
733
	_set_entity_bitfield_offset(ent, offset);
}

unsigned (get_entity_bitfield_size)(const ir_entity *ent)
{
	return _get_entity_bitfield_size(ent);
}

void (set_entity_bitfield_size)(ir_entity *ent, unsigned size)
{
	_set_entity_bitfield_size(ent, size);
734
}
Michael Beck's avatar
Michael Beck committed
735

736
737
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
738
739
740
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
741
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
742
743
744
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
745
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
746
}
747

748
size_t get_entity_n_overwrites(const ir_entity *ent)
749
{
750
751
752
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
753
}
754

755
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
756
{
Matthias Braun's avatar
Matthias Braun committed
757
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
758
759
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
760
	}
761
	return (size_t)-1;
762
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
763

764
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
765
{
766
767
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
768
}
769

770
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
771
{
772
773
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
774
}
775

776
777
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
778
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
779
		if (ent->overwrites[i] == overwritten) {
780
			for (; i < n - 1; i++)
781
				ent->overwrites[i] = ent->overwrites[i+1];
782
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
783
784
			break;
		}
785
	}
786
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
787

788

789
size_t get_entity_n_overwrittenby(const ir_entity *ent)
790
{
791
792
	if (ent->overwrittenby == NULL)
		return 0;
793
	return ARR_LEN(ent->overwrittenby);
794
}
795

796
797
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
798
{
Matthias Braun's avatar
Matthias Braun committed
799
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
800
801
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
802
	}
803
	return (size_t)-1;
804
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
805

806
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
807
{
808
809
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
810
}
811

812
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
813
{
814
815
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
816
}
817

818
819
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
820
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
821
		if (ent->overwrittenby[i] == overwrites) {
822
			for (; i < n - 1; ++i)
823
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
824
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
825
826
			break;
		}
827
	}
828
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
829

830
831
void *(get_entity_link)(const ir_entity *ent)
{
832
	return _get_entity_link(ent);
833
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
834

835
836
void (set_entity_link)(ir_entity *ent, void *l)
{
837
	_set_entity_link(ent, l);
838
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
839

840
841
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
842
	return _get_entity_irg(ent);
843
}
Christian Schäfer's avatar
Christian Schäfer committed
844

845
846
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
847
	assert(is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
848
	assert(get_entity_peculiarity(ent) == peculiarity_existent);
849
	ent->attr.mtd_attr.irg = irg;
850
}
851

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
int (is_parameter_entity)(const ir_entity *entity)
{
	return _is_parameter_entity(entity);
}

size_t (get_entity_parameter_number)(const ir_entity *entity)
{
	return _get_entity_parameter_number(entity);
}

void set_entity_parameter_number(ir_entity *entity, size_t n)
{
	assert(is_parameter_entity(entity));
	entity->attr.parameter.number = n;
}

868
869
unsigned get_entity_vtable_number(const ir_entity *ent)
{
870
871
	assert(is_method_entity((ir_entity *)ent));
	return ent->attr.mtd_attr.vtable_number;
872
}
873

874
875
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
876
877
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
878
}
879

880
881
882
883
884
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}