irmode.c 15.9 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
9
/**
 * @file
 * @brief    Data modes of operations.
 * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil
Götz Lindenmaier's avatar
Götz Lindenmaier committed
10
 */
Moritz Kroll's avatar
Moritz Kroll committed
11
12
#include <stdlib.h>
#include <stddef.h>
13
#include <stdbool.h>
14

Moritz Kroll's avatar
Moritz Kroll committed
15
16
17
18
19
20
21
22
#include "irprog_t.h"
#include "irmode_t.h"
#include "ident.h"
#include "tv_t.h"
#include "obst.h"
#include "irhooks.h"
#include "array.h"
#include "error.h"
23
#include "pattern_dmp.h"
Christian Schäfer's avatar
Christian Schäfer committed
24

Michael Beck's avatar
Michael Beck committed
25
/** Obstack to hold all modes. */
26
static struct obstack modes;
Christian Schäfer's avatar
Christian Schäfer committed
27

Michael Beck's avatar
Michael Beck committed
28
/** The list of all currently existing modes. */
Matthias Braun's avatar
Matthias Braun committed
29
30
static ir_mode **mode_list;

31
static bool modes_are_equal(const ir_mode *m, const ir_mode *n)
32
{
33
34
35
36
37
	if (m->sort != n->sort)
		return false;
	if (m->sort == irms_auxiliary || m->sort == irms_data)
		return strcmp(m->name, n->name) == 0;
	return m->arithmetic   == n->arithmetic &&
38
39
40
	       m->size         == n->size &&
	       m->sign         == n->sign &&
	       m->modulo_shift == n->modulo_shift;
41
}
Matthias Heil's avatar
Matthias Heil committed
42

43
/**
44
 * searches the modes obstack for the given mode and returns
45
 * a pointer on an equal mode already in the array, NULL if
46
47
 * none found
 */
48
49
static ir_mode *find_mode(const ir_mode *m)
{
Matthias Braun's avatar
Matthias Braun committed
50
	for (size_t i = 0, n_modes = ARR_LEN(mode_list); i < n_modes; ++i) {
Matthias Braun's avatar
Matthias Braun committed
51
		ir_mode *n = mode_list[i];
52
53
54
		if (modes_are_equal(n, m))
			return n;
	}
Michael Beck's avatar
Michael Beck committed
55
56
57
	return NULL;
}

58
59
60
/**
 * sets special values of modes
 */
61
62
static void set_mode_values(ir_mode* mode)
{
Matthias Braun's avatar
Matthias Braun committed
63
	switch (get_mode_sort(mode)) {
64
	case irms_reference:
65
66
67
68
69
70
71
	case irms_int_number:
	case irms_float_number:
		mode->min  = get_tarval_min(mode);
		mode->max  = get_tarval_max(mode);
		mode->null = get_tarval_null(mode);
		mode->one  = get_tarval_one(mode);
		mode->minus_one = get_tarval_minus_one(mode);
72
		if (get_mode_sort(mode) != irms_float_number) {
73
74
75
76
			mode->all_one = get_tarval_all_one(mode);
		} else {
			mode->all_one = tarval_bad;
		}
77
78
79
80
81
82
83
84
		break;

	case irms_internal_boolean:
		mode->min  = tarval_b_false;
		mode->max  = tarval_b_true;
		mode->null = tarval_b_false;
		mode->one  = tarval_b_true;
		mode->minus_one = tarval_bad;
85
		mode->all_one = tarval_b_true;
86
87
		break;

88
89
	case irms_auxiliary:
	case irms_data:
90
91
92
93
94
95
96
		mode->min  = tarval_bad;
		mode->max  = tarval_bad;
		mode->null = tarval_bad;
		mode->one  = tarval_bad;
		mode->minus_one = tarval_bad;
		break;
	}
97
}
98

99
100
101
102
ir_mode *mode_T;
ir_mode *mode_X;
ir_mode *mode_M;
ir_mode *mode_BB;
103
104
ir_mode *mode_ANY;
ir_mode *mode_BAD;
Christian Schäfer's avatar
Christian Schäfer committed
105

106
107
108
ir_mode *mode_F;
ir_mode *mode_D;
ir_mode *mode_Q;
Christian Schäfer's avatar
Christian Schäfer committed
109

110
111
112
ir_mode *mode_Bs;
ir_mode *mode_Bu;
ir_mode *mode_Hs;
113
ir_mode *mode_Hu;
114
ir_mode *mode_Is;
115
ir_mode *mode_Iu;
116
ir_mode *mode_Ls;
117
ir_mode *mode_Lu;
118
ir_mode *mode_LLs;
119
ir_mode *mode_LLu;
Christian Schäfer's avatar
Christian Schäfer committed
120

121
122
ir_mode *mode_b;
ir_mode *mode_P;
Michael Beck's avatar
fixed:    
Michael Beck committed
123

124
125
ir_mode *mode_P_code;
ir_mode *mode_P_data;
126

Matthias Braun's avatar
Matthias Braun committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
ir_mode *get_modeT(void)   { return mode_T;   }
ir_mode *get_modeF(void)   { return mode_F;   }
ir_mode *get_modeD(void)   { return mode_D;   }
ir_mode *get_modeQ(void)   { return mode_Q;   }
ir_mode *get_modeBs(void)  { return mode_Bs;  }
ir_mode *get_modeBu(void)  { return mode_Bu;  }
ir_mode *get_modeHs(void)  { return mode_Hs;  }
ir_mode *get_modeHu(void)  { return mode_Hu;  }
ir_mode *get_modeIs(void)  { return mode_Is;  }
ir_mode *get_modeIu(void)  { return mode_Iu;  }
ir_mode *get_modeLs(void)  { return mode_Ls;  }
ir_mode *get_modeLu(void)  { return mode_Lu;  }
ir_mode *get_modeLLs(void) { return mode_LLs; }
ir_mode *get_modeLLu(void) { return mode_LLu; }
ir_mode *get_modeb(void)   { return mode_b;   }
ir_mode *get_modeP(void)   { return mode_P;   }
ir_mode *get_modeX(void)   { return mode_X;   }
ir_mode *get_modeM(void)   { return mode_M;   }
ir_mode *get_modeBB(void)  { return mode_BB;  }
146
147
ir_mode *get_modeANY(void) { return mode_ANY; }
ir_mode *get_modeBAD(void) { return mode_BAD; }
148

149

150
151
ir_mode *(get_modeP_code)(void)
{
152
	return get_modeP_code_();
Michael Beck's avatar
Michael Beck committed
153
154
}

155
156
ir_mode *(get_modeP_data)(void)
{
157
	return get_modeP_data_();
158
159
}

160
161
void set_modeP_code(ir_mode *p)
{
162
163
	assert(mode_is_reference(p));
	mode_P_code = p;
164
165
}

166
167
void set_modeP_data(ir_mode *p)
{
168
169
	assert(mode_is_reference(p));
	mode_P_data = p;
170
	mode_P = p;
Michael Beck's avatar
Michael Beck committed
171
}
172

173
174
/*
 * Creates a new mode.
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
static ir_mode *alloc_mode(const char *name, ir_mode_sort sort,
                           ir_mode_arithmetic arithmetic, unsigned bit_size,
                           int sign, unsigned modulo_shift)
{
	ir_mode *mode_tmpl = OALLOCZ(&modes, ir_mode);

	mode_tmpl->name         = new_id_from_str(name);
	mode_tmpl->sort         = sort;
	mode_tmpl->size         = bit_size;
	mode_tmpl->sign         = sign ? 1 : 0;
	mode_tmpl->modulo_shift = modulo_shift;
	mode_tmpl->arithmetic   = arithmetic;
	mode_tmpl->link         = NULL;
	return mode_tmpl;
}

static ir_mode *register_mode(ir_mode *mode)
{
	/* does any of the existing modes have the same properties? */
	ir_mode *old = find_mode(mode);
	if (old != NULL) {
		/* remove new mode from obstack */
		obstack_free(&modes, mode);
		return old;
	}
201

202
	mode->kind = k_ir_mode;
203
	mode->type = new_type_primitive(mode);
204
	ARR_APP1(ir_mode*, mode_list, mode);
205
	set_mode_values(mode);
206
	hook_new_mode(mode);
207
	return mode;
208
209
}

210
211
212
213
214
215
216
ir_mode *new_int_mode(const char *name, ir_mode_arithmetic arithmetic,
                      unsigned bit_size, int sign, unsigned modulo_shift)
{
	ir_mode *result = alloc_mode(name, irms_int_number, arithmetic, bit_size,
	                             sign, modulo_shift);
	return register_mode(result);
}
217

218
219
220
221
222
223
224
ir_mode *new_reference_mode(const char *name, ir_mode_arithmetic arithmetic,
                            unsigned bit_size, unsigned modulo_shift)
{
	ir_mode *result = alloc_mode(name, irms_reference, arithmetic, bit_size,
	                             0, modulo_shift);
	return register_mode(result);
}
225

226
227
228
229
230
231
232
233
234
235
236
ir_mode *new_float_mode(const char *name, ir_mode_arithmetic arithmetic,
                        unsigned exponent_size, unsigned mantissa_size)
{
	bool     explicit_one = false;
	unsigned bit_size     = exponent_size + mantissa_size + 1;

	if (arithmetic == irma_x86_extended_float) {
		explicit_one = true;
		bit_size++;
	} else if (arithmetic != irma_ieee754) {
		panic("Arithmetic %s invalid for float");
237
	}
238
239
240
241
242
	if (exponent_size >= 256)
		panic("Exponents >= 256 bits not supported");
	if (mantissa_size >= 256)
		panic("Mantissa >= 256 bits not supported");

Matthias Braun's avatar
Matthias Braun committed
243
244
	ir_mode *result
		= alloc_mode(name, irms_float_number, arithmetic, bit_size, 1, 0);
245
246
247
248
	result->float_desc.exponent_size = exponent_size;
	result->float_desc.mantissa_size = mantissa_size;
	result->float_desc.explicit_one  = explicit_one;
	return register_mode(result);
249
250
}

251
252
253
254
255
256
257
258
259
260
261
262
ir_mode *new_non_arithmetic_mode(const char *name)
{
	ir_mode *result = alloc_mode(name, irms_data, irma_none, 0, 0, 0);
	return register_mode(result);
}

static ir_mode *new_non_data_mode(const char *name)
{
	ir_mode *result = alloc_mode(name, irms_auxiliary, irma_none, 0, 0, 0);
	return register_mode(result);
}

263
264
ident *(get_mode_ident)(const ir_mode *mode)
{
265
	return get_mode_ident_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
266
267
}

268
269
const char *get_mode_name(const ir_mode *mode)
{
270
	return get_id_str(mode->name);
Christian Schäfer's avatar
Christian Schäfer committed
271
272
}

273
274
unsigned (get_mode_size_bits)(const ir_mode *mode)
{
275
	return get_mode_size_bits_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
276
277
}

278
279
unsigned (get_mode_size_bytes)(const ir_mode *mode)
{
280
	return get_mode_size_bytes_(mode);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
281
282
}

283
284
int (get_mode_sign)(const ir_mode *mode)
{
285
	return get_mode_sign_(mode);
286
287
}

288
289
ir_mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode)
{
290
	return get_mode_arithmetic_(mode);
291
292
}

293

294
295
unsigned int (get_mode_modulo_shift)(const ir_mode *mode)
{
296
	return get_mode_modulo_shift_(mode);
297
298
}

299
300
void *(get_mode_link)(const ir_mode *mode)
{
301
	return get_mode_link_(mode);
302
303
}

304
305
void (set_mode_link)(ir_mode *mode, void *l)
{
306
	set_mode_link_(mode, l);
307
308
}

Matthias Braun's avatar
Matthias Braun committed
309
ir_tarval *get_mode_min(ir_mode *mode)
310
{
311
	assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
312

313
	return mode->min;
Christian Schäfer's avatar
Christian Schäfer committed
314
315
}

Matthias Braun's avatar
Matthias Braun committed
316
ir_tarval *get_mode_max(ir_mode *mode)
317
{
318
	assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
319

320
	return mode->max;
Christian Schäfer's avatar
Christian Schäfer committed
321
322
}

Matthias Braun's avatar
Matthias Braun committed
323
ir_tarval *get_mode_null(ir_mode *mode)
324
{
Matthias Braun's avatar
Matthias Braun committed
325
	assert(mode_is_data(mode) || mode == mode_b);
Christian Schäfer's avatar
Christian Schäfer committed
326

327
	return mode->null;
Christian Schäfer's avatar
Christian Schäfer committed
328
329
}

Matthias Braun's avatar
Matthias Braun committed
330
ir_tarval *get_mode_one(ir_mode *mode)
331
{
Matthias Braun's avatar
Matthias Braun committed
332
	assert(mode_is_data(mode) || mode == mode_b);
Christian Schäfer's avatar
Christian Schäfer committed
333

334
	return mode->one;
Christian Schäfer's avatar
Christian Schäfer committed
335
336
}

Matthias Braun's avatar
Matthias Braun committed
337
ir_tarval *get_mode_minus_one(ir_mode *mode)
338
{
339
	assert(mode_is_data(mode));
Michael Beck's avatar
Michael Beck committed
340

341
	return mode->minus_one;
Michael Beck's avatar
Michael Beck committed
342
343
}

Matthias Braun's avatar
Matthias Braun committed
344
ir_tarval *get_mode_all_one(ir_mode *mode)
345
{
Matthias Braun's avatar
Matthias Braun committed
346
	assert(mode_is_data(mode) || mode == mode_b);
347
348
349
	return mode->all_one;
}

Matthias Braun's avatar
Matthias Braun committed
350
ir_tarval *get_mode_infinite(ir_mode *mode)
351
{
352
	assert(mode_is_float(mode));
353

354
	return get_tarval_plus_inf(mode);
Christian Schäfer's avatar
Christian Schäfer committed
355
356
}

Matthias Braun's avatar
Matthias Braun committed
357
ir_tarval *get_mode_NAN(ir_mode *mode)
358
{
359
	assert(mode_is_float(mode));
360

361
	return get_tarval_nan(mode);
Christian Schäfer's avatar
Christian Schäfer committed
362
363
}

364
365
int is_mode(const void *thing)
{
Michael Beck's avatar
Michael Beck committed
366
	return get_kind(thing) == k_ir_mode;
367
368
}

369
370
int (mode_is_signed)(const ir_mode *mode)
{
371
	return mode_is_signed_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
372
373
}

374
375
int (mode_is_float)(const ir_mode *mode)
{
376
	return mode_is_float_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
377
378
}

379
380
int (mode_is_int)(const ir_mode *mode)
{
381
	return mode_is_int_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
382
383
}

384
385
int (mode_is_reference)(const ir_mode *mode)
{
386
	return mode_is_reference_(mode);
387
388
}

389
390
int (mode_is_num)(const ir_mode *mode)
{
391
	return mode_is_num_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
392
393
}

394
395
int (mode_is_data)(const ir_mode *mode)
{
396
	return mode_is_data_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
397
398
}

399
400
401
402
403
404
405
406
407
408
unsigned (get_mode_mantissa_size)(const ir_mode *mode)
{
	return get_mode_mantissa_size_(mode);
}

unsigned (get_mode_exponent_size)(const ir_mode *mode)
{
	return get_mode_exponent_size_(mode);
}

409
410
int smaller_mode(const ir_mode *sm, const ir_mode *lm)
{
Matthias Braun's avatar
Matthias Braun committed
411
412
413
	assert(sm != NULL);
	assert(lm != NULL);
	if (sm == lm) return true;
414
415
416
417
418
419

	switch (get_mode_sort(sm)) {
	case irms_int_number:
		switch (get_mode_sort(lm)) {
		case irms_int_number:
			if (get_mode_arithmetic(sm) != get_mode_arithmetic(lm))
Matthias Braun's avatar
Matthias Braun committed
420
				return false;
421
422
423
424
425
426
427
428

			/* only two complement implemented */
			assert(get_mode_arithmetic(sm) == irma_twos_complement);

			/* integers are convertable if
			 *   - both have the same sign and lm is the larger one
			 *   - lm is the signed one and is at least two bits larger
			 *     (one for the sign, one for the highest bit of sm)
Matthias Braun's avatar
Matthias Braun committed
429
430
			 *   - sm & lm are two_complement and lm has greater or equal
			 *     number of bits
431
			 */
Matthias Braun's avatar
Matthias Braun committed
432
433
			unsigned sm_bits = get_mode_size_bits(sm);
			unsigned lm_bits = get_mode_size_bits(lm);
434
435
436
437
438
439
440
441
442
443
444
			if (mode_is_signed(sm)) {
				if (!mode_is_signed(lm))
					return 0;
				return sm_bits <= lm_bits;
			} else {
				if (mode_is_signed(lm)) {
					return sm_bits < lm_bits;
				}
				return sm_bits <= lm_bits;
			}

445
446
447
448
		case irms_auxiliary:
		case irms_data:
		case irms_internal_boolean:
		case irms_reference:
449
450
		case irms_float_number:
			/* int to float works if the float is large enough */
Matthias Braun's avatar
Matthias Braun committed
451
			return false;
452
		}
453
		panic("invalid mode_sort");
454
455

	case irms_float_number:
456
457
458
		return get_mode_arithmetic(sm) == get_mode_arithmetic(lm)
		    && get_mode_sort(lm) == irms_float_number
		    && get_mode_size_bits(lm) >= get_mode_size_bits(sm);
459

460
461
462
	case irms_auxiliary:
	case irms_data:
	case irms_internal_boolean:
463
	case irms_reference:
Michael Beck's avatar
Michael Beck committed
464
		/* do exist machines out there with different pointer lengths ?*/
Matthias Braun's avatar
Matthias Braun committed
465
		return false;
466
467
	}

468
	panic("invalid mode_sort");
469
470
}

471
472
int values_in_mode(const ir_mode *sm, const ir_mode *lm)
{
Matthias Braun's avatar
Matthias Braun committed
473
474
	assert(sm != NULL);
	assert(lm != NULL);
475
476
	if (sm == lm)
		return true;
477

Moritz Kroll's avatar
Moritz Kroll committed
478
	if (sm == mode_b)
479
480
481
482
483
484
485
486
		return mode_is_int(lm) || mode_is_float(lm);

	ir_mode_arithmetic larith = get_mode_arithmetic(lm);
	ir_mode_arithmetic sarith = get_mode_arithmetic(sm);
	switch (larith) {
	case irma_x86_extended_float:
	case irma_ieee754:
		if (sarith == irma_ieee754 || sarith == irma_x86_extended_float) {
Moritz Kroll's avatar
Moritz Kroll committed
487
			return get_mode_size_bits(sm) <= get_mode_size_bits(lm);
488
		} else if (sarith == irma_twos_complement) {
Matthias Braun's avatar
Matthias Braun committed
489
490
			unsigned int_mantissa
				= get_mode_size_bits(sm) - (mode_is_signed(sm) ? 1 : 0);
491
492
493
494
495
			unsigned float_mantissa = get_mode_mantissa_size(lm) + 1;
			return int_mantissa <= float_mantissa;
		}
		break;
	case irma_twos_complement:
Matthias Braun's avatar
Matthias Braun committed
496
		if (sarith == irma_twos_complement)
497
498
499
500
			return get_mode_size_bits(sm) <= get_mode_size_bits(lm);
		break;
	case irma_none:
		break;
501
	}
502
	return false;
503
504
}

505
506
ir_mode *get_reference_mode_signed_eq(ir_mode *mode)
{
507
508
509
510
	assert(mode_is_reference(mode));
	return mode->eq_signed;
}

511
512
void set_reference_mode_signed_eq(ir_mode *ref_mode, ir_mode *int_mode)
{
513
514
515
516
517
	assert(mode_is_reference(ref_mode));
	assert(mode_is_int(int_mode));
	ref_mode->eq_signed = int_mode;
}

518
519
ir_mode *get_reference_mode_unsigned_eq(ir_mode *mode)
{
520
521
522
523
	assert(mode_is_reference(mode));
	return mode->eq_unsigned;
}

524
525
void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode)
{
526
527
528
	assert(mode_is_reference(ref_mode));
	assert(mode_is_int(int_mode));
	ref_mode->eq_unsigned = int_mode;
529
530
}

531
532
void init_mode(void)
{
533
	obstack_init(&modes);
Matthias Braun's avatar
Matthias Braun committed
534
	mode_list = NEW_ARR_F(ir_mode*, 0);
535

536
	/* initialize predefined modes */
537
538
539
540
541
542
543
544
	mode_BB  = new_non_data_mode("BB");
	mode_X   = new_non_data_mode("X");
	mode_M   = new_non_data_mode("M");
	mode_T   = new_non_data_mode("T");
	mode_ANY = new_non_data_mode("ANY");
	mode_BAD = new_non_data_mode("BAD");
	mode_b   = alloc_mode("b", irms_internal_boolean, irma_none, 0, 0, 0);
	mode_b   = register_mode(mode_b);
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

	mode_F   = new_float_mode("F", irma_ieee754,  8, 23);
	mode_D   = new_float_mode("D", irma_ieee754, 11, 52);
	mode_Q   = new_float_mode("Q", irma_ieee754, 15, 112);

	mode_Bs  = new_int_mode("Bs",  irma_twos_complement, 8,   1, 32);
	mode_Bu  = new_int_mode("Bu",  irma_twos_complement, 8,   0, 32);
	mode_Hs  = new_int_mode("Hs",  irma_twos_complement, 16,  1, 32);
	mode_Hu  = new_int_mode("Hu",  irma_twos_complement, 16,  0, 32);
	mode_Is  = new_int_mode("Is",  irma_twos_complement, 32,  1, 32);
	mode_Iu  = new_int_mode("Iu",  irma_twos_complement, 32,  0, 32);
	mode_Ls  = new_int_mode("Ls",  irma_twos_complement, 64,  1, 64);
	mode_Lu  = new_int_mode("Lu",  irma_twos_complement, 64,  0, 64);
	mode_LLs = new_int_mode("LLs", irma_twos_complement, 128, 1, 128);
	mode_LLu = new_int_mode("LLu", irma_twos_complement, 128, 0, 128);

	mode_P   = new_reference_mode("P", irma_twos_complement, 32, 32);
562
563
	set_reference_mode_signed_eq(mode_P, mode_Is);
	set_reference_mode_unsigned_eq(mode_P, mode_Iu);
564
565
566
567

	/* set the machine specific modes to the predefined ones */
	mode_P_code = mode_P;
	mode_P_data = mode_P;
568
}
569

570
571
ir_mode *find_unsigned_mode(const ir_mode *mode)
{
572
	ir_mode n = *mode;
573

574
575
576
577
578
	/* allowed for reference mode */
	if (mode->sort == irms_reference)
		n.sort = irms_int_number;

	assert(n.sort == irms_int_number);
579
580
	n.sign = 0;
	return find_mode(&n);
581
582
}

583
584
ir_mode *find_signed_mode(const ir_mode *mode)
{
585
	ir_mode n = *mode;
586

587
588
589
	assert(mode->sort == irms_int_number);
	n.sign = 1;
	return find_mode(&n);
590
591
}

592
593
ir_mode *find_double_bits_int_mode(const ir_mode *mode)
{
594
	ir_mode n = *mode;
Michael Beck's avatar
Michael Beck committed
595

Matthias Braun's avatar
Matthias Braun committed
596
597
	assert(mode->sort == irms_int_number
	       && mode->arithmetic == irma_twos_complement);
Michael Beck's avatar
Michael Beck committed
598

599
600
	n.size = 2*mode->size;
	return find_mode(&n);
Michael Beck's avatar
Michael Beck committed
601
}
602

603
604
605
606
607
int mode_has_signed_zero(const ir_mode *mode)
{
	switch (mode->arithmetic) {
	case irma_ieee754:
	case irma_x86_extended_float:
Matthias Braun's avatar
Matthias Braun committed
608
		return true;
609
610
	case irma_none:
	case irma_twos_complement:
Matthias Braun's avatar
Matthias Braun committed
611
		return false;
612
	}
613
	panic("invalid arithmetic mode");
614
615
}

616
617
int mode_overflow_on_unary_Minus(const ir_mode *mode)
{
618
619
	switch (mode->arithmetic) {
	case irma_twos_complement:
Matthias Braun's avatar
Matthias Braun committed
620
		return true;
621
622
623
	case irma_ieee754:
	case irma_x86_extended_float:
	case irma_none:
Matthias Braun's avatar
Matthias Braun committed
624
		return false;
625
626
	}
	panic("invalid arithmetic mode");
627
628
}

629
630
int mode_wrap_around(const ir_mode *mode)
{
631
632
633
	switch (mode->arithmetic) {
	case irma_twos_complement:
	case irma_none:
Matthias Braun's avatar
Matthias Braun committed
634
		return true;
635
636
	case irma_ieee754:
	case irma_x86_extended_float:
Matthias Braun's avatar
Matthias Braun committed
637
		return false;
638
639
	}
	panic("invalid arithmetic mode");
Michael Beck's avatar
Michael Beck committed
640
641
}

642
643
int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst)
{
644
	if (src == dst)
Matthias Braun's avatar
Matthias Braun committed
645
		return true;
646
	if (get_mode_size_bits(src) != get_mode_size_bits(dst))
Matthias Braun's avatar
Matthias Braun committed
647
648
		return false;
	ir_mode_arithmetic ma = get_mode_arithmetic(src);
649
	if (ma != get_mode_arithmetic(dst))
Matthias Braun's avatar
Matthias Braun committed
650
		return false;
651

652
	return ma == irma_twos_complement;
653
654
}

655
656
657
658
659
ir_type *(get_type_for_mode) (const ir_mode *mode)
{
	return get_type_for_mode_(mode);
}

Matthias Braun's avatar
Matthias Braun committed
660
661
662
663
664
665
666
667
668
669
670
size_t ir_get_n_modes(void)
{
	return ARR_LEN(mode_list);
}

ir_mode *ir_get_mode(size_t num)
{
	assert(num < ARR_LEN(mode_list));
	return mode_list[num];
}

671
672
void finish_mode(void)
{
673
	obstack_free(&modes, 0);
Matthias Braun's avatar
Matthias Braun committed
674
	DEL_ARR_F(mode_list);
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

	mode_T   = NULL;
	mode_X   = NULL;
	mode_M   = NULL;
	mode_BB  = NULL;
	mode_ANY = NULL;
	mode_BAD = NULL;

	mode_F   = NULL;
	mode_D   = NULL;

	mode_Bs  = NULL;
	mode_Bu  = NULL;
	mode_Hs  = NULL;
	mode_Hu  = NULL;
	mode_Is  = NULL;
	mode_Iu  = NULL;
	mode_Ls  = NULL;
	mode_Lu  = NULL;

	mode_b   = NULL;

697
	mode_P      = NULL;
698
699
	mode_P_code = NULL;
	mode_P_data = NULL;
700
}