irmode.c 14.4 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
#include <stdlib.h>
12
#include <stdbool.h>
13

Moritz Kroll's avatar
Moritz Kroll committed
14
15
16
17
18
19
20
#include "irprog_t.h"
#include "irmode_t.h"
#include "ident.h"
#include "tv_t.h"
#include "obst.h"
#include "irhooks.h"
#include "array.h"
Matthias Braun's avatar
Matthias Braun committed
21
#include "panic.h"
22
#include "pattern_dmp.h"
23
#include "strcalc.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
	if (m->sort != n->sort)
		return false;
	if (m->sort == irms_auxiliary || m->sort == irms_data)
		return strcmp(m->name, n->name) == 0;
37
38
39
40
41
	return m->arithmetic        == n->arithmetic
	    && m->size              == n->size
	    && m->sign              == n->sign
	    && m->modulo_shift      == n->modulo_shift
	    && m->int_conv_overflow == n->int_conv_overflow;
42
}
Matthias Heil's avatar
Matthias Heil committed
43

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

59
60
61
62
ir_mode *mode_T;
ir_mode *mode_X;
ir_mode *mode_M;
ir_mode *mode_BB;
63
64
ir_mode *mode_ANY;
ir_mode *mode_BAD;
Christian Schäfer's avatar
Christian Schäfer committed
65

66
67
ir_mode *mode_F;
ir_mode *mode_D;
Christian Schäfer's avatar
Christian Schäfer committed
68

69
70
71
ir_mode *mode_Bs;
ir_mode *mode_Bu;
ir_mode *mode_Hs;
72
ir_mode *mode_Hu;
73
ir_mode *mode_Is;
74
ir_mode *mode_Iu;
75
ir_mode *mode_Ls;
76
ir_mode *mode_Lu;
Christian Schäfer's avatar
Christian Schäfer committed
77

78
79
ir_mode *mode_b;
ir_mode *mode_P;
Michael Beck's avatar
fixed:    
Michael Beck committed
80

Matthias Braun's avatar
Matthias Braun committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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_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_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;  }
97
98
ir_mode *get_modeANY(void) { return mode_ANY; }
ir_mode *get_modeBAD(void) { return mode_BAD; }
99

100
void set_modeP(ir_mode *p)
101
{
102
	assert(mode_is_reference(p));
103
	mode_P = p;
Michael Beck's avatar
Michael Beck committed
104
}
105

106
107
/*
 * Creates a new mode.
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
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;
	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;
	}
133

134
	mode->kind = k_ir_mode;
135
	mode->type = new_type_primitive(mode);
136
	ARR_APP1(ir_mode*, mode_list, mode);
137
	init_mode_values(mode);
138
	hook_new_mode(mode);
139
	return mode;
140
141
}

142
143
144
ir_mode *new_int_mode(const char *name, ir_mode_arithmetic arithmetic,
                      unsigned bit_size, int sign, unsigned modulo_shift)
{
145
	if (bit_size >= (unsigned)sc_get_precision())
146
		panic("cannot create mode: more bits than tarval module maximum");
147

148
149
150
151
	ir_mode *result = alloc_mode(name, irms_int_number, arithmetic, bit_size,
	                             sign, modulo_shift);
	return register_mode(result);
}
152

153
154
155
ir_mode *new_reference_mode(const char *name, ir_mode_arithmetic arithmetic,
                            unsigned bit_size, unsigned modulo_shift)
{
156
	if (bit_size >= (unsigned)sc_get_precision())
157
		panic("cannot create mode: more bits than tarval module maximum");
158

159
160
	ir_mode *result = alloc_mode(name, irms_reference, arithmetic, bit_size,
	                             0, modulo_shift);
161
162
163
164
165
166
167
168
169
170
	ir_mode *res = register_mode(result);

	/* construct the unsigned_eq mode */
	char buf[64];
	snprintf(buf, sizeof(buf), "%s_iu", name);
	ir_mode *unsigned_eq = alloc_mode(buf, irms_int_number, arithmetic,
	                                  bit_size, 0, modulo_shift);
	unsigned_eq = register_mode(unsigned_eq);
	set_reference_mode_unsigned_eq(res, unsigned_eq);
	return res;
171
}
172

173
ir_mode *new_float_mode(const char *name, ir_mode_arithmetic arithmetic,
174
175
                        unsigned exponent_size, unsigned mantissa_size,
                        float_int_conversion_overflow_style_t conv_overflow)
176
177
178
179
180
181
182
{
	bool     explicit_one = false;
	unsigned bit_size     = exponent_size + mantissa_size + 1;

	if (arithmetic == irma_x86_extended_float) {
		explicit_one = true;
	} else if (arithmetic != irma_ieee754) {
183
		panic("arithmetic %s invalid for float");
184
	}
185
	if (exponent_size >= 256)
186
		panic("exponents >= 256 bits not supported");
187
	if (mantissa_size >= 256)
188
		panic("mantissa >= 256 bits not supported");
189
	if (exponent_size >= (unsigned)sc_get_precision())
190
		panic("cannot create mode: more bits than tarval module maximum");
191
	if (mantissa_size >= (unsigned)sc_get_precision())
192
		panic("cannot create mode: more bits than tarval module maximum");
193

Matthias Braun's avatar
Matthias Braun committed
194
195
	ir_mode *result
		= alloc_mode(name, irms_float_number, arithmetic, bit_size, 1, 0);
196
	result->int_conv_overflow        = conv_overflow;
197
198
199
200
	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);
201
202
}

203
ir_mode *new_non_arithmetic_mode(const char *name, unsigned bit_size)
204
{
205
	ir_mode *result = alloc_mode(name, irms_data, irma_none, bit_size, 0, 0);
206
207
208
209
210
211
212
213
214
	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);
}

215
216
ident *(get_mode_ident)(const ir_mode *mode)
{
217
	return get_mode_ident_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
218
219
}

220
221
const char *get_mode_name(const ir_mode *mode)
{
222
	return get_id_str(mode->name);
Christian Schäfer's avatar
Christian Schäfer committed
223
224
}

225
226
unsigned (get_mode_size_bits)(const ir_mode *mode)
{
227
	return get_mode_size_bits_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
228
229
}

230
231
unsigned (get_mode_size_bytes)(const ir_mode *mode)
{
232
	return get_mode_size_bytes_(mode);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
233
234
}

235
236
int (get_mode_sign)(const ir_mode *mode)
{
237
	return get_mode_sign_(mode);
238
239
}

240
241
ir_mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode)
{
242
	return get_mode_arithmetic_(mode);
243
244
}

245

246
247
unsigned int (get_mode_modulo_shift)(const ir_mode *mode)
{
248
	return get_mode_modulo_shift_(mode);
249
250
}

yb9976's avatar
yb9976 committed
251
ir_tarval *get_mode_min(const ir_mode *mode)
252
{
253
	assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
254

255
	return mode->min;
Christian Schäfer's avatar
Christian Schäfer committed
256
257
}

yb9976's avatar
yb9976 committed
258
ir_tarval *get_mode_max(const ir_mode *mode)
259
{
260
	assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
261

262
	return mode->max;
Christian Schäfer's avatar
Christian Schäfer committed
263
264
}

265
ir_tarval *get_mode_null(const ir_mode *mode)
266
{
267
	assert(mode_is_data(mode));
268
	return mode->null;
Christian Schäfer's avatar
Christian Schäfer committed
269
270
}

271
ir_tarval *get_mode_one(const ir_mode *mode)
272
{
273
	assert(mode_is_data(mode));
274
	return mode->one;
Christian Schäfer's avatar
Christian Schäfer committed
275
276
}

yb9976's avatar
yb9976 committed
277
ir_tarval *get_mode_all_one(const ir_mode *mode)
278
{
279
	assert(mode_is_data(mode));
280
281
282
	return mode->all_one;
}

283
ir_tarval *get_mode_infinite(const ir_mode *mode)
284
{
285
	assert(mode_is_float(mode));
286
	return mode->infinity;
Christian Schäfer's avatar
Christian Schäfer committed
287
288
}

289
ir_tarval *get_mode_NAN(const ir_mode *mode)
290
{
291
	assert(mode_is_float(mode));
292
	return mode->nan;
Christian Schäfer's avatar
Christian Schäfer committed
293
294
}

295
296
int (mode_is_signed)(const ir_mode *mode)
{
297
	return mode_is_signed_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
298
299
}

300
301
int (mode_is_float)(const ir_mode *mode)
{
302
	return mode_is_float_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
303
304
}

305
306
int (mode_is_int)(const ir_mode *mode)
{
307
	return mode_is_int_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
308
309
}

310
311
int (mode_is_reference)(const ir_mode *mode)
{
312
	return mode_is_reference_(mode);
313
314
}

315
316
int (mode_is_num)(const ir_mode *mode)
{
317
	return mode_is_num_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
318
319
}

320
321
int (mode_is_data)(const ir_mode *mode)
{
322
	return mode_is_data_(mode);
Christian Schäfer's avatar
Christian Schäfer committed
323
324
}

325
326
327
328
329
330
331
332
333
334
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);
}

335
336
337
338
339
340
float_int_conversion_overflow_style_t get_mode_float_int_overflow(
		const ir_mode *mode)
{
	return mode->int_conv_overflow;
}

341
342
int smaller_mode(const ir_mode *sm, const ir_mode *lm)
{
Matthias Braun's avatar
Matthias Braun committed
343
344
345
	assert(sm != NULL);
	assert(lm != NULL);
	if (sm == lm) return true;
346
347
348
349
350
351

	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
352
				return false;
353
354
355
356
357

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

			/* integers are convertable if
358
359
			 * - both have the same sign and lm is the larger one
			 * - lm is signed and is at least one bit larger (the sign) */
Matthias Braun's avatar
Matthias Braun committed
360
361
			unsigned sm_bits = get_mode_size_bits(sm);
			unsigned lm_bits = get_mode_size_bits(lm);
362
363
			if (mode_is_signed(sm)) {
				if (!mode_is_signed(lm))
364
					return false;
365
			} else {
366
				if (mode_is_signed(lm))
367
368
					return sm_bits < lm_bits;
			}
369
			return sm_bits <= lm_bits;
370

371
372
373
374
		case irms_auxiliary:
		case irms_data:
		case irms_internal_boolean:
		case irms_reference:
375
376
		case irms_float_number:
			/* int to float works if the float is large enough */
Matthias Braun's avatar
Matthias Braun committed
377
			return false;
378
		}
379
		panic("invalid mode_sort");
380
381

	case irms_float_number:
382
		return get_mode_arithmetic(sm) == get_mode_arithmetic(lm)
383
		    && mode_is_float(lm)
384
		    && get_mode_size_bits(lm) >= get_mode_size_bits(sm);
385

386
387
388
	case irms_auxiliary:
	case irms_data:
	case irms_internal_boolean:
389
	case irms_reference:
Michael Beck's avatar
Michael Beck committed
390
		/* do exist machines out there with different pointer lengths ?*/
Matthias Braun's avatar
Matthias Braun committed
391
		return false;
392
393
	}

394
	panic("invalid mode_sort");
395
396
}

397
398
int values_in_mode(const ir_mode *sm, const ir_mode *lm)
{
Matthias Braun's avatar
Matthias Braun committed
399
400
	assert(sm != NULL);
	assert(lm != NULL);
401
402
	if (sm == lm)
		return true;
403

Moritz Kroll's avatar
Moritz Kroll committed
404
	if (sm == mode_b)
405
406
407
408
409
410
411
412
		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
413
			return get_mode_size_bits(sm) <= get_mode_size_bits(lm);
414
		} else if (sarith == irma_twos_complement) {
Matthias Braun's avatar
Matthias Braun committed
415
416
			unsigned int_mantissa
				= get_mode_size_bits(sm) - (mode_is_signed(sm) ? 1 : 0);
417
418
419
420
421
			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
422
		if (sarith == irma_twos_complement)
423
424
425
426
			return get_mode_size_bits(sm) <= get_mode_size_bits(lm);
		break;
	case irma_none:
		break;
427
	}
428
	return false;
429
430
}

yb9976's avatar
yb9976 committed
431
ir_mode *get_reference_mode_unsigned_eq(const ir_mode *mode)
432
{
433
434
435
436
	assert(mode_is_reference(mode));
	return mode->eq_unsigned;
}

437
438
void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode)
{
439
440
441
	assert(mode_is_reference(ref_mode));
	assert(mode_is_int(int_mode));
	ref_mode->eq_unsigned = int_mode;
442
443
}

444
445
void init_mode(void)
{
446
	obstack_init(&modes);
Matthias Braun's avatar
Matthias Braun committed
447
	mode_list = NEW_ARR_F(ir_mode*, 0);
448

449
	/* initialize predefined modes */
450
451
452
453
454
455
	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");
456
	mode_b   = alloc_mode("b", irms_internal_boolean, irma_none, 1, 0, 0);
457
	mode_b   = register_mode(mode_b);
458

459
460
	mode_F   = new_float_mode("F", irma_ieee754,  8, 23, ir_overflow_min_max);
	mode_D   = new_float_mode("D", irma_ieee754, 11, 52, ir_overflow_min_max);
461
462
463
464
465
466
467
468
469
470
471

	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_P   = new_reference_mode("P", irma_twos_complement, 32, 32);
472
}
473

474
475
ir_mode *find_unsigned_mode(const ir_mode *mode)
{
476
	ir_mode n = *mode;
477

478
479
480
481
	/* allowed for reference mode */
	if (mode->sort == irms_reference)
		n.sort = irms_int_number;

482
	assert(mode_is_int(&n));
483
484
	n.sign = 0;
	return find_mode(&n);
485
486
}

487
488
ir_mode *find_signed_mode(const ir_mode *mode)
{
489
	assert(mode_is_int(mode));
490
491
492
	ir_mode n = *mode;
	n.sign = 1;
	return find_mode(&n);
493
494
}

495
496
ir_mode *find_double_bits_int_mode(const ir_mode *mode)
{
497
	assert(mode_is_int(mode) && mode->arithmetic == irma_twos_complement);
498
499
	ir_mode n = *mode;
	n.size = 2*mode->size;
500
501
	if (n.modulo_shift != 0 && n.modulo_shift < n.size)
		n.modulo_shift = n.size;
502
	return find_mode(&n);
Michael Beck's avatar
Michael Beck committed
503
}
504

505
506
507
508
509
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
510
		return true;
511
512
	case irma_none:
	case irma_twos_complement:
Matthias Braun's avatar
Matthias Braun committed
513
		return false;
514
	}
515
	panic("invalid arithmetic mode");
516
517
}

518
519
int mode_overflow_on_unary_Minus(const ir_mode *mode)
{
520
521
	switch (mode->arithmetic) {
	case irma_twos_complement:
Matthias Braun's avatar
Matthias Braun committed
522
		return true;
523
524
525
	case irma_ieee754:
	case irma_x86_extended_float:
	case irma_none:
Matthias Braun's avatar
Matthias Braun committed
526
		return false;
527
528
	}
	panic("invalid arithmetic mode");
529
530
}

531
532
int mode_wrap_around(const ir_mode *mode)
{
533
534
535
	switch (mode->arithmetic) {
	case irma_twos_complement:
	case irma_none:
Matthias Braun's avatar
Matthias Braun committed
536
		return true;
537
538
	case irma_ieee754:
	case irma_x86_extended_float:
Matthias Braun's avatar
Matthias Braun committed
539
		return false;
540
541
	}
	panic("invalid arithmetic mode");
Michael Beck's avatar
Michael Beck committed
542
543
}

544
545
int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst)
{
546
	if (src == dst)
Matthias Braun's avatar
Matthias Braun committed
547
		return true;
548
	if (get_mode_size_bits(src) != get_mode_size_bits(dst))
Matthias Braun's avatar
Matthias Braun committed
549
550
		return false;
	ir_mode_arithmetic ma = get_mode_arithmetic(src);
551
	if (ma != get_mode_arithmetic(dst))
Matthias Braun's avatar
Matthias Braun committed
552
		return false;
553

554
	return ma == irma_twos_complement;
555
556
}

557
558
559
560
561
ir_type *(get_type_for_mode) (const ir_mode *mode)
{
	return get_type_for_mode_(mode);
}

Matthias Braun's avatar
Matthias Braun committed
562
563
564
565
566
567
568
569
570
571
572
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];
}

573
574
void finish_mode(void)
{
575
	obstack_free(&modes, 0);
Matthias Braun's avatar
Matthias Braun committed
576
	DEL_ARR_F(mode_list);
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

	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;

599
	mode_P   = NULL;
600
}