tv.c 43.9 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.
 */

Michael Beck's avatar
Michael Beck committed
20
21
22
23
24
25
26
27
/**
 * @file
 * @brief    Representation of and static computations on target machine
 *           values.
 * @date     2003
 * @author   Mathias Heil
 * @version  $Id$
 * @summary
28
 *
Michael Beck's avatar
Michael Beck committed
29
30
31
32
 * Values are stored in a format depending upon chosen arithmetic
 * module. Default uses strcalc and fltcalc.
 * This implementation assumes:
 *  - target has IEEE-754 floating-point arithmetic.
33
 */
Boris Boesler's avatar
Boris Boesler committed
34
35
36
37
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

38
#include <assert.h>         /* assertions */
Michael Beck's avatar
Michael Beck committed
39
#include <stdlib.h>         /* atoi() */
Michael Beck's avatar
Michael Beck committed
40
41
42
#ifdef HAVE_STRING_H
# include <string.h>         /* nice things for strings */
#endif
43
#ifdef HAVE_STRINGS_H
44
#include <strings.h>        /* strings.h also includes bsd only function strcasecmp */
45
#endif
Michael Beck's avatar
Michael Beck committed
46
47
48
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
49

50
51
52
#include "tv_t.h"
#include "set.h"            /* to store tarvals in */
#include "entity_t.h"       /* needed to store pointers to entities */
Michael Beck's avatar
Michael Beck committed
53
#include "irmode_t.h"
54
#include "irnode.h"         /* defines boolean return values (pnc_number)*/
55
56
#include "strcalc.h"
#include "fltcalc.h"
57
#include "irtools.h"
Michael Beck's avatar
Michael Beck committed
58
#include "xmalloc.h"
Michael Beck's avatar
Michael Beck committed
59
#include "firm_common.h"
60
#include "error.h"
Christian Schäfer's avatar
Christian Schäfer committed
61

Götz Lindenmaier's avatar
Götz Lindenmaier committed
62
63
/** Size of hash tables.  Should correspond to average number of distinct constant
    target values */
64
#define N_CONSTANTS 2048
Götz Lindenmaier's avatar
Götz Lindenmaier committed
65

66
67
/* get the integer overflow mode */
#define GET_OVERFLOW_MODE() int_overflow_mode
68
69

/* unused, float to int doesn't work yet */
70
enum float_to_int_mode {
Michael Beck's avatar
Michael Beck committed
71
72
	TRUNCATE,
	ROUND
73
74
};

75
76
77
78
79
#define GET_FLOAT_TO_INT_MODE() TRUNCATE

#define SWITCH_NOINFINITY 0
#define SWITCH_NODENORMALS 0

80
81
82
83
84
85
86
87
/****************************************************************************
 *   local definitions and macros
 ****************************************************************************/
#ifndef NDEBUG
#  define TARVAL_VERIFY(a) tarval_verify((a))
#else
#  define TARVAL_VERIFY(a) ((void)0)
#endif
Christian Schäfer's avatar
Christian Schäfer committed
88

89
90
#define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
#define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
91

92
93
#define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
#define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
94

95
#define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
Michael Beck's avatar
Michael Beck committed
96

97
98
99
/****************************************************************************
 *   private variables
 ****************************************************************************/
100
101
static struct set *tarvals = NULL;   /* container for tarval structs */
static struct set *values = NULL;    /* container for values */
102
static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
103

104
105
/** if this is set non-zero, the constant folding for floating point is OFF */
static int no_float = 0;
Matthias Braun's avatar
Matthias Braun committed
106

107
108
109
110
111
112
113
/****************************************************************************
 *   private functions
 ****************************************************************************/
#ifndef NDEBUG
static int hash_val(const void *value, unsigned int length);
static int hash_tv(tarval *tv);
static void _fail_verify(tarval *tv, const char* file, int line)
Christian Schäfer's avatar
Christian Schäfer committed
114
{
Michael Beck's avatar
Michael Beck committed
115
116
117
118
119
120
	/* print a memory image of the tarval and throw an assertion */
	if (tv)
		printf("%s:%d: Invalid tarval:\n  mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
	else
		printf("%s:%d: Invalid tarval (null)", file, line);
	assert(0);
Christian Schäfer's avatar
Christian Schäfer committed
121
}
Till Riedel's avatar
Till Riedel committed
122
123
124
#ifdef __GNUC__
INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
#endif
Christian Schäfer's avatar
Christian Schäfer committed
125

Till Riedel's avatar
Till Riedel committed
126
INLINE static void tarval_verify(tarval *tv)
Christian Schäfer's avatar
Christian Schäfer committed
127
{
Michael Beck's avatar
Michael Beck committed
128
129
130
	assert(tv);
	assert(tv->mode);
	assert(tv->value);
Christian Schäfer's avatar
Christian Schäfer committed
131

Michael Beck's avatar
Michael Beck committed
132
133
	if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
	if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
134

Michael Beck's avatar
Michael Beck committed
135
136
	if (!FIND_TARVAL(tv)) fail_verify(tv);
	if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
137

Michael Beck's avatar
Michael Beck committed
138
	return;
Christian Schäfer's avatar
Christian Schäfer committed
139
}
140
#endif /* NDEBUG */
Christian Schäfer's avatar
Christian Schäfer committed
141

142
143
/** Hash a tarval. */
static int hash_tv(tarval *tv) {
Michael Beck's avatar
Michael Beck committed
144
	return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
Christian Schäfer's avatar
Christian Schäfer committed
145
146
}

147
148
/** Hash a value. Treat it as a byte array. */
static int hash_val(const void *value, unsigned int length) {
Michael Beck's avatar
Michael Beck committed
149
150
	unsigned int i;
	unsigned int hash = 0;
151

Michael Beck's avatar
Michael Beck committed
152
153
154
155
156
	/* scramble the byte - array */
	for (i = 0; i < length; ++i) {
		hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
		hash += (hash << 11) ^ (hash >> 17);
	}
157

Michael Beck's avatar
Michael Beck committed
158
	return hash;
Christian Schäfer's avatar
Christian Schäfer committed
159
160
}

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
static int cmp_tv(const void *p1, const void *p2, size_t n) {
	const tarval *tv1 = p1;
	const tarval *tv2 = p2;
	(void) n;

	assert(tv1->kind == k_tarval);
	assert(tv2->kind == k_tarval);
	if(tv1->mode < tv2->mode)
		return -1;
	if(tv1->mode > tv2->mode)
		return 1;
	if(tv1->length < tv2->length)
		return -1;
	if(tv1->length > tv2->length)
		return 1;
	if(tv1->value < tv2->value)
		return -1;
	if(tv1->value > tv2->value)
		return 1;

	return 0;
}

Michael Beck's avatar
Michael Beck committed
184
/** finds tarval with value/mode or creates new tarval */
Michael Beck's avatar
Michael Beck committed
185
186
187
188
189
190
191
192
193
static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
	tarval tv;

	tv.kind   = k_tarval;
	tv.mode   = mode;
	tv.length = length;
	if (length > 0) {
		/* if there already is such a value, it is returned, else value
		 * is copied into the set */
194
195
		char *temp = alloca(length);
		memcpy(temp, value, length);
196
		if (get_mode_arithmetic(mode) == irma_twos_complement) {
197
198
			sign_extend(temp, mode);
		}
199
		tv.value = INSERT_VALUE(temp, length);
Michael Beck's avatar
Michael Beck committed
200
201
202
203
204
205
	} else {
		tv.value = value;
	}
	/* if there is such a tarval, it is returned, else tv is copied
	 * into the set */
	return (tarval *)INSERT_TARVAL(&tv);
Christian Schäfer's avatar
Christian Schäfer committed
206
207
}

208
209
210
/**
 * handle overflow
 */
211
static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
212
{
213
214
	char *temp;

Michael Beck's avatar
Michael Beck committed
215
	switch (get_mode_sort(mode)) {
216
217
218
	case irms_reference:
		/* addresses always wrap around */
		temp = alloca(sc_get_buffer_length());
219
		memcpy(temp, value, sc_get_buffer_length());
220
		sc_truncate(get_mode_size_bits(mode), temp);
221
222
223
224
		/* the sc_ module expects that all bits are set ... */
		sign_extend(temp, mode);
		return get_tarval(temp, length, mode);

Michael Beck's avatar
Michael Beck committed
225
226
227
228
229
230
	case irms_int_number:
		if (sc_comp(value, get_mode_max(mode)->value) == 1) {
			switch (GET_OVERFLOW_MODE()) {
			case TV_OVERFLOW_SATURATE:
				return get_mode_max(mode);
			case TV_OVERFLOW_WRAP:
231
				temp = alloca(sc_get_buffer_length());
232
				memcpy(temp, value, sc_get_buffer_length());
233
				sc_truncate(get_mode_size_bits(mode), temp);
234
235
236
				/* the sc_ module expects that all bits are set ... */
				sign_extend(temp, mode);
				return get_tarval(temp, length, mode);
Michael Beck's avatar
Michael Beck committed
237
238
239
240
241
242
243
244
245
246
247
248
			case TV_OVERFLOW_BAD:
				return tarval_bad;
			default:
				return get_tarval(value, length, mode);
			}
		}
		if (sc_comp(value, get_mode_min(mode)->value) == -1) {
			switch (GET_OVERFLOW_MODE()) {
			case TV_OVERFLOW_SATURATE:
				return get_mode_min(mode);
			case TV_OVERFLOW_WRAP: {
				char *temp = alloca(sc_get_buffer_length());
249
				memcpy(temp, value, sc_get_buffer_length());
250
				sc_truncate(get_mode_size_bits(mode), temp);
Michael Beck's avatar
Michael Beck committed
251
252
253
254
255
256
257
258
259
260
261
262
				return get_tarval(temp, length, mode);
			}
			case TV_OVERFLOW_BAD:
				return tarval_bad;
			default:
				return get_tarval(value, length, mode);
			}
		}
		break;

	case irms_float_number:
		if (SWITCH_NOINFINITY && fc_is_inf(value)) {
263
264
			/* clip infinity to maximum value */
			return fc_is_negative(value) ? get_mode_min(mode) : get_mode_max(mode);
Michael Beck's avatar
Michael Beck committed
265
266
267
		}

		if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
268
			/* clip denormals to zero */
Michael Beck's avatar
Michael Beck committed
269
270
271
272
273
274
275
276
			return get_mode_null(mode);
		}
		break;

	default:
		break;
	}
	return get_tarval(value, length, mode);
Christian Schäfer's avatar
Christian Schäfer committed
277
278
}

279
/*
280
 *   public variables declared in tv.h
281
 */
282
static tarval reserved_tv[4];
283
284
285
286
287

tarval *tarval_bad       = &reserved_tv[0];
tarval *tarval_undefined = &reserved_tv[1];
tarval *tarval_b_false   = &reserved_tv[2];
tarval *tarval_b_true    = &reserved_tv[3];
Christian Schäfer's avatar
Christian Schäfer committed
288

289
/*
290
 *   public functions declared in tv.h
291
292
 */

293
294
295
296
297
/*
 * Constructors =============================================================
 */
tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
{
Michael Beck's avatar
Michael Beck committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
	assert(str);
	assert(len);
	assert(mode);

	switch (get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		assert(0);
		break;

	case irms_internal_boolean:
		/* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
		if (strcasecmp(str, "true")) return tarval_b_true;
		else if (strcasecmp(str, "false")) return tarval_b_true;
		else
			/* XXX This is C semantics */
			return atoi(str) ? tarval_b_true : tarval_b_false;

	case irms_float_number:
318
		switch (get_mode_size_bits(mode)) {
Michael Beck's avatar
Michael Beck committed
319
320
321
322
323
324
325
		case 32:
			fc_val_from_str(str, len, 8, 23, NULL);
			break;
		case 64:
			fc_val_from_str(str, len, 11, 52, NULL);
			break;
		case 80:
326
		case 96:
Michael Beck's avatar
Michael Beck committed
327
328
			fc_val_from_str(str, len, 15, 64, NULL);
			break;
329
330
		default:
			panic("Unsupported mode in new_tarval_from_str()");
Michael Beck's avatar
Michael Beck committed
331
332
333
		}
		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);

Michael Beck's avatar
Michael Beck committed
334
335
336
337
338
	case irms_reference:
		/* same as integer modes */
	case irms_int_number:
		sc_val_from_str(str, len, NULL, mode);
		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
Michael Beck's avatar
Michael Beck committed
339
340
341
342
	}

	assert(0);  /* can't be reached, can it? */
	return NULL;
343
}
Christian Schäfer's avatar
Christian Schäfer committed
344

345
/*
346
 * helper function, create a tarval from long
347
 */
Michael Beck's avatar
Michael Beck committed
348
349
350
tarval *new_tarval_from_long(long l, ir_mode *mode) {
	assert(mode);

351
	switch (get_mode_sort(mode))   {
Michael Beck's avatar
Michael Beck committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	case irms_internal_boolean:
		/* XXX C semantics ! */
		return l ? tarval_b_true : tarval_b_false ;

	case irms_reference:
		/* same as integer modes */
	case irms_int_number:
		sc_val_from_long(l, NULL);
		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);

	case irms_float_number:
		return new_tarval_from_double((long double)l, mode);

	default:
		assert(0 && "unsupported mode sort");
	}
	return NULL;
Christian Schäfer's avatar
Christian Schäfer committed
369
}
Michael Beck's avatar
Michael Beck committed
370

371
/* returns non-zero if can be converted to long */
Michael Beck's avatar
Michael Beck committed
372
int tarval_is_long(tarval *tv) {
373
374
	if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
		return 0;
Michael Beck's avatar
Michael Beck committed
375

Matthias Braun's avatar
Matthias Braun committed
376
	if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
Michael Beck's avatar
Michael Beck committed
377
378
379
380
381
382
383
384
		/* the value might be too big to fit in a long */
		sc_max_from_bits(sizeof(long) << 3, 0, NULL);
		if (sc_comp(sc_get_buffer(), tv->value) == -1) {
			/* really doesn't fit */
			return 0;
		}
	}
	return 1;
385
}
Michael Beck's avatar
Michael Beck committed
386
387

/* this might overflow the machine's long, so use only with small values */
Michael Beck's avatar
Michael Beck committed
388
389
long get_tarval_long(tarval* tv) {
	assert(tarval_is_long(tv) && "tarval too big to fit in long");
Christian Schäfer's avatar
Christian Schäfer committed
390

Michael Beck's avatar
Michael Beck committed
391
	return sc_val_to_long(tv->value);
392
}
Christian Schäfer's avatar
Christian Schäfer committed
393

Michael Beck's avatar
Michael Beck committed
394
395
396
397
398
tarval *new_tarval_from_double(long double d, ir_mode *mode) {
	assert(mode && (get_mode_sort(mode) == irms_float_number));

	switch (get_mode_size_bits(mode)) {
	case 32:
Michael Beck's avatar
Michael Beck committed
399
		fc_val_from_ieee754(d, 8, 23, NULL);
Michael Beck's avatar
Michael Beck committed
400
401
		break;
	case 64:
Michael Beck's avatar
Michael Beck committed
402
		fc_val_from_ieee754(d, 11, 52, NULL);
Michael Beck's avatar
Michael Beck committed
403
404
		break;
	case 80:
405
	case 96:
Michael Beck's avatar
Michael Beck committed
406
		fc_val_from_ieee754(d, 15, 64, NULL);
Michael Beck's avatar
Michael Beck committed
407
		break;
408
409
	default:
		panic("Unsupported mode in new_tarval_from_double()");
Michael Beck's avatar
Michael Beck committed
410
411
	}
	return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
Christian Schäfer's avatar
Christian Schäfer committed
412
}
Michael Beck's avatar
Michael Beck committed
413

414
/* returns non-zero if can be converted to double */
Michael Beck's avatar
Michael Beck committed
415
416
int tarval_is_double(tarval *tv) {
	assert(tv);
Christian Schäfer's avatar
Christian Schäfer committed
417

Michael Beck's avatar
Michael Beck committed
418
	return (get_mode_sort(tv->mode) == irms_float_number);
419
}
Michael Beck's avatar
Michael Beck committed
420

Michael Beck's avatar
Michael Beck committed
421
422
long double get_tarval_double(tarval *tv) {
	assert(tarval_is_double(tv));
Christian Schäfer's avatar
Christian Schäfer committed
423

Michael Beck's avatar
Michael Beck committed
424
	return fc_val_to_ieee754(tv->value);
425
}
Christian Schäfer's avatar
Christian Schäfer committed
426

Matthias Heil's avatar
Matthias Heil committed
427

428
429
430
/*
 * Access routines for tarval fields ========================================
 */
Christian Schäfer's avatar
Christian Schäfer committed
431

Michael Beck's avatar
Michael Beck committed
432
433
/* get the mode of the tarval */
ir_mode *(get_tarval_mode)(const tarval *tv) {
Michael Beck's avatar
Michael Beck committed
434
	return _get_tarval_mode(tv);
435
436
}

437
438
439
440
441
442
443
/*
 * Special value query functions ============================================
 *
 * These functions calculate and return a tarval representing the requested
 * value.
 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
 * functions, but these are stored on initialization of the irmode module and
Michael Beck's avatar
Michael Beck committed
444
 * therefore the irmode functions should be preferred to the functions below.
445
 */
Christian Schäfer's avatar
Christian Schäfer committed
446

Michael Beck's avatar
Michael Beck committed
447
tarval *(get_tarval_bad)(void) {
Michael Beck's avatar
Michael Beck committed
448
	return _get_tarval_bad();
449
}
Michael Beck's avatar
Michael Beck committed
450
451

tarval *(get_tarval_undefined)(void) {
Michael Beck's avatar
Michael Beck committed
452
	return _get_tarval_undefined();
453
}
Michael Beck's avatar
Michael Beck committed
454
455

tarval *(get_tarval_b_false)(void) {
Michael Beck's avatar
Michael Beck committed
456
	return _get_tarval_b_false();
457
}
Michael Beck's avatar
Michael Beck committed
458
459

tarval *(get_tarval_b_true)(void) {
Michael Beck's avatar
Michael Beck committed
460
	return _get_tarval_b_true();
461
}
Michael Beck's avatar
Michael Beck committed
462

Michael Beck's avatar
Michael Beck committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
tarval *get_tarval_max(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	switch(get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		assert(0);
		break;

	case irms_internal_boolean:
		return tarval_b_true;

	case irms_float_number:
		switch(get_mode_size_bits(mode)) {
		case 32:
			fc_get_max(8, 23, NULL);
			break;
		case 64:
			fc_get_max(11, 52, NULL);
			break;
		case 80:
490
		case 96:
Michael Beck's avatar
Michael Beck committed
491
492
			fc_get_max(15, 64, NULL);
			break;
493
494
		default:
			panic("Unsupported mode in get_tarval_max()");
Michael Beck's avatar
Michael Beck committed
495
496
497
		}
		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);

498
499
500
501
	case irms_reference:
	case irms_int_number:
		sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
Michael Beck's avatar
Michael Beck committed
502
503
	}
	return tarval_bad;
504
505
}

Michael Beck's avatar
Michael Beck committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
tarval *get_tarval_min(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	switch(get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		assert(0);
		break;

	case irms_internal_boolean:
		return tarval_b_false;

	case irms_float_number:
		switch(get_mode_size_bits(mode)) {
		case 32:
			fc_get_min(8, 23, NULL);
			break;
		case 64:
			fc_get_min(11, 52, NULL);
			break;
		case 80:
533
		case 96:
Michael Beck's avatar
Michael Beck committed
534
535
			fc_get_min(15, 64, NULL);
			break;
536
537
		default:
			panic("Unsupported mode in get_tarval_min()");
Michael Beck's avatar
Michael Beck committed
538
539
540
		}
		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);

541
542
543
544
	case irms_reference:
	case irms_int_number:
		sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
Michael Beck's avatar
Michael Beck committed
545
546
	}
	return tarval_bad;
Christian Schäfer's avatar
Christian Schäfer committed
547
548
}

Michael Beck's avatar
Michael Beck committed
549
/** The bit pattern for the pointer NULL */
550
static long _null_value = 0;
551

Michael Beck's avatar
Michael Beck committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
tarval *get_tarval_null(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	switch(get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		assert(0);
		break;

	case irms_float_number:
		return new_tarval_from_double(0.0, mode);

570
	case irms_internal_boolean:
Michael Beck's avatar
Michael Beck committed
571
572
573
574
575
576
577
	case irms_int_number:
		return new_tarval_from_long(0l,  mode);

	case irms_reference:
		return new_tarval_from_long(_null_value, mode);
	}
	return tarval_bad;
Christian Schäfer's avatar
Christian Schäfer committed
578
579
}

Michael Beck's avatar
Michael Beck committed
580
581
582
583
584
tarval *get_tarval_one(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
585
		assert(0);
Michael Beck's avatar
Michael Beck committed
586
587
588
589
590
591
592
593
594
595
		return tarval_bad;
	}

	switch(get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		assert(0);
		break;

596
597
598
	case irms_internal_boolean:
		return tarval_b_true;

Michael Beck's avatar
Michael Beck committed
599
600
601
	case irms_float_number:
		return new_tarval_from_double(1.0, mode);

602
	case irms_reference:
Michael Beck's avatar
Michael Beck committed
603
604
605
606
	case irms_int_number:
		return new_tarval_from_long(1l, mode);
	}
	return tarval_bad;
Christian Schäfer's avatar
Christian Schäfer committed
607
608
}

609
tarval *get_tarval_all_one(ir_mode *mode) {
Michael Beck's avatar
Michael Beck committed
610
611
612
613
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
614
		assert(0);
Michael Beck's avatar
Michael Beck committed
615
616
617
618
619
620
621
		return tarval_bad;
	}

	switch(get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
622
623
624
625
		assert(0);
		return tarval_bad;

	case irms_int_number:
Michael Beck's avatar
Michael Beck committed
626
627
	case irms_internal_boolean:
	case irms_reference:
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
		return tarval_not(get_mode_null(mode));


	case irms_float_number:
		return new_tarval_from_double(1.0, mode);
	}
	return tarval_bad;
}

tarval *get_tarval_minus_one(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	switch(get_mode_sort(mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
	case irms_internal_boolean:
Michael Beck's avatar
Michael Beck committed
650
651
652
		assert(0);
		break;

653
654
655
	case irms_reference:
		return tarval_bad;

Michael Beck's avatar
Michael Beck committed
656
657
658
659
	case irms_float_number:
		return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;

	case irms_int_number:
660
		return new_tarval_from_long(-1l, mode);
Michael Beck's avatar
Michael Beck committed
661
662
	}
	return tarval_bad;
663
664
}

Michael Beck's avatar
Michael Beck committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
tarval *get_tarval_nan(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	if (get_mode_sort(mode) == irms_float_number) {
		switch(get_mode_size_bits(mode)) {
		case 32:
			fc_get_qnan(8, 23, NULL);
			break;
		case 64:
			fc_get_qnan(11, 52, NULL);
			break;
		case 80:
682
		case 96:
Michael Beck's avatar
Michael Beck committed
683
684
			fc_get_qnan(15, 64, NULL);
			break;
685
686
		default:
			panic("Unsupported mode in get_tarval_nan()");
Michael Beck's avatar
Michael Beck committed
687
688
689
690
691
692
		}
		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
	} else {
		assert(0 && "tarval is not floating point");
		return tarval_bad;
	}
Christian Schäfer's avatar
Christian Schäfer committed
693
694
}

Michael Beck's avatar
Michael Beck committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
tarval *get_tarval_plus_inf(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	if (get_mode_sort(mode) == irms_float_number) {
		switch(get_mode_size_bits(mode)) {
		case 32:
			fc_get_plusinf(8, 23, NULL);
			break;
		case 64:
			fc_get_plusinf(11, 52, NULL);
			break;
		case 80:
712
		case 96:
Michael Beck's avatar
Michael Beck committed
713
714
			fc_get_plusinf(15, 64, NULL);
			break;
715
716
		default:
			panic("Unsupported mode in get_tarval_plus_inf()");
Michael Beck's avatar
Michael Beck committed
717
718
719
720
721
722
		}
		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
	} else {
		assert(0 && "tarval is not floating point");
		return tarval_bad;
	}
Christian Schäfer's avatar
Christian Schäfer committed
723
724
}

Michael Beck's avatar
Michael Beck committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
tarval *get_tarval_minus_inf(ir_mode *mode) {
	assert(mode);

	if (get_mode_n_vector_elems(mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	if (get_mode_sort(mode) == irms_float_number) {
		switch(get_mode_size_bits(mode)) {
		case 32:
			fc_get_minusinf(8, 23, NULL);
			break;
		case 64:
			fc_get_minusinf(11, 52, NULL);
			break;
		case 80:
742
		case 96:
Michael Beck's avatar
Michael Beck committed
743
744
			fc_get_minusinf(15, 64, NULL);
			break;
745
746
		default:
			panic("Unsupported mode in get_tarval_minus_inf()");
Michael Beck's avatar
Michael Beck committed
747
748
749
750
751
752
		}
		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
	} else {
		assert(0 && "tarval is not floating point");
		return tarval_bad;
	}
753
754
}

755
756
757
/*
 * Arithmethic operations on tarvals ========================================
 */
Christian Schäfer's avatar
Christian Schäfer committed
758

Michael Beck's avatar
Michael Beck committed
759
760
761
/*
 * test if negative number, 1 means 'yes'
 */
Michael Beck's avatar
Michael Beck committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
int tarval_is_negative(tarval *a) {
	assert(a);

	if (get_mode_n_vector_elems(a->mode) > 1) {
		/* vector arithmetic not implemented yet */
		assert(0 && "tarval_is_negative is not allowed for vector modes");
		return 0;
	}

	switch (get_mode_sort(a->mode)) {
	case irms_int_number:
		if (!mode_is_signed(a->mode)) return 0;
		else
			return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;

	case irms_float_number:
778
		return fc_is_negative(a->value);
Michael Beck's avatar
Michael Beck committed
779
780
781
782
783

	default:
		assert(0 && "not implemented");
		return 0;
	}
Christian Schäfer's avatar
Christian Schäfer committed
784
785
}

Michael Beck's avatar
Michael Beck committed
786
787
788
/*
 * test if null, 1 means 'yes'
 */
Michael Beck's avatar
Michael Beck committed
789
int tarval_is_null(tarval *a) {
790
791
	return
		a != tarval_bad &&
792
		a == get_mode_null(get_tarval_mode(a));
Michael Beck's avatar
Michael Beck committed
793
794
}

Michael Beck's avatar
Michael Beck committed
795
796
797
/*
 * test if one, 1 means 'yes'
 */
Michael Beck's avatar
Michael Beck committed
798
int tarval_is_one(tarval *a) {
799
800
	return
		a != tarval_bad &&
801
		a == get_mode_one(get_tarval_mode(a));
802
}
Michael Beck's avatar
Michael Beck committed
803

804
805
806
807
int tarval_is_all_one(tarval *tv) {
	return
		tv != tarval_bad &&
		tv == get_mode_all_one(get_tarval_mode(tv));
Michael Beck's avatar
Michael Beck committed
808
809
}

Michael Beck's avatar
Michael Beck committed
810
811
812
813
814
815
/*
 * test if one, 1 means 'yes'
 */
int tarval_is_minus_one(tarval *a) {
	return
		a != tarval_bad &&
816
		a == get_mode_minus_one(get_tarval_mode(a));
Michael Beck's avatar
Michael Beck committed
817
818
}

Michael Beck's avatar
Michael Beck committed
819
820
821
/*
 * comparison
 */
Michael Beck's avatar
Michael Beck committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
pn_Cmp tarval_cmp(tarval *a, tarval *b) {
	assert(a);
	assert(b);

	if (a == tarval_bad || b == tarval_bad) {
		assert(0 && "Comparison with tarval_bad");
		return pn_Cmp_False;
	}

	if (a == tarval_undefined || b == tarval_undefined)
		return pn_Cmp_False;

	if (a->mode != b->mode)
		return pn_Cmp_False;

	if (get_mode_n_vector_elems(a->mode) > 1) {
		/* vector arithmetic not implemented yet */
		assert(0 && "cmp not implemented for vector modes");
	}

	/* Here the two tarvals are unequal and of the same mode */
	switch (get_mode_sort(a->mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		if (a == b)
			return pn_Cmp_Eq;
		return pn_Cmp_False;

	case irms_float_number:
852
853
854
		/* it should be safe to enable this even if other arithmetic is disabled */
		/*if (no_float)
			return pn_Cmp_False;*/
Michael Beck's avatar
Michael Beck committed
855
856
857
858
859
860
861
862
863
864
865
		/*
		 * BEWARE: we cannot compare a == b here, because
		 * a NaN is always Unordered to any other value, even to itself!
		 */
		switch (fc_comp(a->value, b->value)) {
		case -1: return pn_Cmp_Lt;
		case  0: return pn_Cmp_Eq;
		case  1: return pn_Cmp_Gt;
		case  2: return pn_Cmp_Uo;
		default: return pn_Cmp_False;
		}
866
	case irms_reference:
Michael Beck's avatar
Michael Beck committed
867
868
869
870
871
872
873
874
875
876
877
	case irms_int_number:
		if (a == b)
			return pn_Cmp_Eq;
		return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;

	case irms_internal_boolean:
		if (a == b)
			return pn_Cmp_Eq;
		return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
	}
	return pn_Cmp_False;
878
879
}

Michael Beck's avatar
Michael Beck committed
880
881
882
/*
 * convert to other mode
 */
883
tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
Michael Beck's avatar
Michael Beck committed
884
	char *buffer;
885
	fp_value *res;
Michael Beck's avatar
Michael Beck committed
886
887

	assert(src);
888
	assert(dst_mode);
Michael Beck's avatar
Michael Beck committed
889

890
	if (src->mode == dst_mode) return src;
Michael Beck's avatar
Michael Beck committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904

	if (get_mode_n_vector_elems(src->mode) > 1) {
		/* vector arithmetic not implemented yet */
		return tarval_bad;
	}

	switch (get_mode_sort(src->mode)) {
	case irms_control_flow:
	case irms_memory:
	case irms_auxiliary:
		break;

		/* cast float to something */
	case irms_float_number:
905
		switch (get_mode_sort(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
906
		case irms_float_number:
907
			switch (get_mode_size_bits(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
908
909
910
911
912
913
914
			case 32:
				fc_cast(src->value, 8, 23, NULL);
				break;
			case 64:
				fc_cast(src->value, 11, 52, NULL);
				break;
			case 80:
915
			case 96:
Michael Beck's avatar
Michael Beck committed
916
917
918
				fc_cast(src->value, 15, 64, NULL);
				break;
			default:
919
				panic("Unsupported mode in tarval_convert_to()");
Michael Beck's avatar
Michael Beck committed
920
			}
921
			return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
Michael Beck's avatar
Michael Beck committed
922
923
924
925

		case irms_int_number:
			switch (GET_FLOAT_TO_INT_MODE()) {
			case TRUNCATE:
926
				res = fc_int(src->value, NULL);
Michael Beck's avatar
Michael Beck committed
927
928
				break;
			case ROUND:
929
				res = fc_rnd(src->value, NULL);
Michael Beck's avatar
Michael Beck committed
930
931
				break;
			default:
932
				panic("Unsupported float to int conversion mode in tarval_convert_to()");
Michael Beck's avatar
Michael Beck committed
933
934
				break;
			}
935
936
			buffer = alloca(sc_get_buffer_length());
			if (! fc_flt2int(res, buffer, dst_mode))
937
				return tarval_bad;
938
			return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
Michael Beck's avatar
Michael Beck committed
939
940
941
942
943
944
945

		default:
			/* the rest can't be converted */
			return tarval_bad;
		}
		break;

946
	/* cast int/characters to something */
Michael Beck's avatar
Michael Beck committed
947
	case irms_int_number:
948
		switch (get_mode_sort(dst_mode)) {
949
950

		case irms_reference:
Michael Beck's avatar
Michael Beck committed
951
952
953
		case irms_int_number:
			buffer = alloca(sc_get_buffer_length());
			memcpy(buffer, src->value, sc_get_buffer_length());
954
955
			sign_extend(buffer, dst_mode);
			return get_tarval_overflow(buffer, src->length, dst_mode);
Michael Beck's avatar
Michael Beck committed
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971

		case irms_internal_boolean:
			/* XXX C semantics */
			if (src == get_mode_null(src->mode)) return tarval_b_false;
			else return tarval_b_true;

		case irms_float_number:
			/* XXX floating point unit does not understand internal integer
			 * representation, convert to string first, then create float from
			 * string */
			buffer = alloca(100);
			/* decimal string representation because hexadecimal output is
			 * interpreted unsigned by fc_val_from_str, so this is a HACK */
			snprintf(buffer, 100, "%s",
				sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
			buffer[100 - 1] = '\0';
972
			switch (get_mode_size_bits(dst_mode)) {
Michael Beck's avatar
Michael Beck committed
973
974
975
976
977
978
979
			case 32:
				fc_val_from_str(buffer, 0, 8, 23, NULL);
				break;
			case 64:
				fc_val_from_str(buffer, 0, 11, 52, NULL);
				break;
			case 80:
980
			case 96:
Michael Beck's avatar
Michael Beck committed
981
982
				fc_val_from_str(buffer, 0, 15, 64, NULL);
				break;
983
984
			default:
				panic("Unsupported mode in tarval_convert_to()");
Michael Beck's avatar
Michael Beck committed
985
			}
986
			return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
Michael Beck's avatar
Michael Beck committed
987
988
989
990
991
992
993

		default:
			break;
		}
		break;

	case irms_internal_boolean:
994
		/* beware: this is C semantic for the INTERNAL boolean mode */
995
996
		if (get_mode_sort(dst_mode) == irms_int_number)
			return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
Michael Beck's avatar
Michael Beck committed
997
998
999
		break;

	case irms_reference:
1000
		if (get_mode_sort(dst_mode) == irms_int_number) {
For faster browsing, not all history is shown. View entire blame