1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19 
20 /**
21  * @file
22  * @brief    Representation of and static computations on target machine
23  *           values.
24  * @date     2003
25  * @author   Mathias Heil
26  * @brief
27  *
28  * Values are stored in a format depending upon chosen arithmetic
29  * module. Default uses strcalc and fltcalc.
30  * This implementation assumes:
31  *  - target has IEEE-754 floating-point arithmetic.
32  */
33 #include "config.h"
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 
41 #include "bitfiddle.h"
42 #include "tv_t.h"
43 #include "set.h"
44 #include "entity_t.h"
45 #include "irmode_t.h"
46 #include "irnode.h"
47 #include "strcalc.h"
48 #include "fltcalc.h"
49 #include "util.h"
50 #include "xmalloc.h"
51 #include "firm_common.h"
52 #include "error.h"
53 
54 /** Size of hash tables.  Should correspond to average number of distinct constant
55     target values */
56 #define N_CONSTANTS 2048
57 
58 /* unused, float to int doesn't work yet */
59 typedef enum float_to_int_mode {
60 	TRUNCATE,
61 	ROUND
62 } float_to_int_mode;
63 
64 static float_to_int_mode current_float_to_int_mode = TRUNCATE;
65 
66 /* set this to true if infinity should be clipped to +/- MAX_FLOAT */
67 #define SWITCH_NOINFINITY 0
68 /* set this to true if denormals should be clipped to zero */
69 #define SWITCH_NODENORMALS 0
70 
71 /****************************************************************************
72  *   local definitions and macros
73  ****************************************************************************/
74 #ifndef NDEBUG
75 #  define TARVAL_VERIFY(a) tarval_verify((a))
76 #else
77 #  define TARVAL_VERIFY(a) ((void)0)
78 #endif
79 
80 #define INSERT_TARVAL(tv) (set_insert(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
81 #define FIND_TARVAL(tv) (set_find(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
82 
83 #define INSERT_VALUE(val, size) (set_insert(char, values, (val), size, hash_val((val), size)))
84 #define FIND_VALUE(val, size) (set_find(char, values, (val), size, hash_val((val), size)))
85 
86 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
87 
88 /** A set containing all existing tarvals. */
89 static struct set *tarvals = NULL;
90 /** A set containing all existing values. */
91 static struct set *values = NULL;
92 
93 /** The carry flag for SOME operations. -1 means UNDEFINED here */
94 static int carry_flag = -1;
95 
96 /** The integer overflow mode. */
97 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
98 
99 /** if this is set non-zero, the constant folding for floating point is OFF */
100 static int no_float = 0;
101 
102 /****************************************************************************
103  *   private functions
104  ****************************************************************************/
105 #ifndef NDEBUG
106 static unsigned hash_val(const void *value, size_t length);
107 static unsigned hash_tv(ir_tarval *tv);
_fail_verify(ir_tarval * tv,const char * file,int line)108 static void _fail_verify(ir_tarval *tv, const char* file, int line)
109 {
110 	/* print a memory image of the tarval and throw an assertion */
111 	if (tv)
112 		panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
113 	else
114 		panic("%s:%d: Invalid tarval (null)", file, line);
115 }
116 
117 inline static
118 #ifdef __GNUC__
119 	__attribute__((unused))
120 #endif
tarval_verify(ir_tarval * tv)121 void tarval_verify(ir_tarval *tv)
122 {
123 	assert(tv);
124 	assert(tv->mode);
125 	assert(tv->value);
126 
127 	if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
128 	if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
129 
130 	if (!FIND_TARVAL(tv)) fail_verify(tv);
131 	if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
132 }
133 #endif /* NDEBUG */
134 
135 /** Hash a tarval. */
hash_tv(ir_tarval * tv)136 static unsigned hash_tv(ir_tarval *tv)
137 {
138 	return (unsigned)((PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length);
139 }
140 
141 /** Hash a value. Treat it as a byte array. */
hash_val(const void * value,size_t length)142 static unsigned hash_val(const void *value, size_t length)
143 {
144 	size_t i;
145 	unsigned hash = 0;
146 
147 	/* scramble the byte - array */
148 	for (i = 0; i < length; ++i) {
149 		hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
150 		hash += (hash << 11) ^ (hash >> 17);
151 	}
152 
153 	return hash;
154 }
155 
cmp_tv(const void * p1,const void * p2,size_t n)156 static int cmp_tv(const void *p1, const void *p2, size_t n)
157 {
158 	const ir_tarval *tv1 = (const ir_tarval*) p1;
159 	const ir_tarval *tv2 = (const ir_tarval*) p2;
160 	(void) n;
161 
162 	assert(tv1->kind == k_tarval);
163 	assert(tv2->kind == k_tarval);
164 	if (tv1->mode < tv2->mode)
165 		return -1;
166 	if (tv1->mode > tv2->mode)
167 		return 1;
168 	if (tv1->length < tv2->length)
169 		return -1;
170 	if (tv1->length > tv2->length)
171 		return 1;
172 	if (tv1->value < tv2->value)
173 		return -1;
174 	if (tv1->value > tv2->value)
175 		return 1;
176 
177 	return 0;
178 }
179 
180 /** finds tarval with value/mode or creates new tarval */
get_tarval(const void * value,size_t length,ir_mode * mode)181 static ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
182 {
183 	ir_tarval tv;
184 
185 	tv.kind   = k_tarval;
186 	tv.mode   = mode;
187 	tv.length = length;
188 	if (length > 0) {
189 		/* if there already is such a value, it is returned, else value
190 		 * is copied into the set */
191 		char *temp = (char*) alloca(length);
192 		memcpy(temp, value, length);
193 		if (get_mode_arithmetic(mode) == irma_twos_complement) {
194 			sign_extend(temp, mode);
195 		}
196 		tv.value = INSERT_VALUE(temp, length);
197 	} else {
198 		tv.value = value;
199 	}
200 	/* if there is such a tarval, it is returned, else tv is copied
201 	 * into the set */
202 	return INSERT_TARVAL(&tv);
203 }
204 
205 /**
206  * handle overflow
207  */
get_tarval_overflow(const void * value,size_t length,ir_mode * mode)208 static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode *mode)
209 {
210 	char *temp;
211 
212 	switch (get_mode_sort(mode)) {
213 	case irms_reference:
214 		/* addresses always wrap around */
215 		temp = (char*) alloca(sc_get_buffer_length());
216 		memcpy(temp, value, sc_get_buffer_length());
217 		sc_truncate(get_mode_size_bits(mode), temp);
218 		/* the sc_ module expects that all bits are set ... */
219 		sign_extend(temp, mode);
220 		return get_tarval(temp, length, mode);
221 
222 	case irms_int_number:
223 		if (sc_comp(value, get_mode_max(mode)->value) == 1) {
224 			switch (tarval_get_integer_overflow_mode()) {
225 			case TV_OVERFLOW_SATURATE:
226 				return get_mode_max(mode);
227 			case TV_OVERFLOW_WRAP:
228 				temp = (char*) alloca(sc_get_buffer_length());
229 				memcpy(temp, value, sc_get_buffer_length());
230 				sc_truncate(get_mode_size_bits(mode), temp);
231 				/* the sc_ module expects that all bits are set ... */
232 				sign_extend(temp, mode);
233 				return get_tarval(temp, length, mode);
234 			case TV_OVERFLOW_BAD:
235 				return tarval_bad;
236 			default:
237 				return get_tarval(value, length, mode);
238 			}
239 		}
240 		if (sc_comp(value, get_mode_min(mode)->value) == -1) {
241 			switch (tarval_get_integer_overflow_mode()) {
242 			case TV_OVERFLOW_SATURATE:
243 				return get_mode_min(mode);
244 			case TV_OVERFLOW_WRAP: {
245 				temp = (char*) alloca(sc_get_buffer_length());
246 				memcpy(temp, value, sc_get_buffer_length());
247 				sc_truncate(get_mode_size_bits(mode), temp);
248 				return get_tarval(temp, length, mode);
249 			}
250 			case TV_OVERFLOW_BAD:
251 				return tarval_bad;
252 			default:
253 				return get_tarval(value, length, mode);
254 			}
255 		}
256 		break;
257 
258 	case irms_float_number:
259 #if SWITCH_NOINFINITY
260 		if (fc_is_inf((const fp_value*) value)) {
261 			/* clip infinity to maximum value */
262 			return fc_is_negative((const fp_value*) value) ? get_mode_min(mode) : get_mode_max(mode);
263 		}
264 #endif
265 #if SWITCH_NODENORMALS
266 		if (fc_is_subnormal((const fp_value*) value)) {
267 			/* clip denormals to zero */
268 			return get_mode_null(mode);
269 		}
270 #endif
271 		break;
272 
273 	default:
274 		break;
275 	}
276 	return get_tarval(value, length, mode);
277 }
278 
279 /*
280  *   public variables declared in tv.h
281  */
282 static ir_tarval reserved_tv[6];
283 
284 ir_tarval *tarval_b_false     = &reserved_tv[0];
285 ir_tarval *tarval_b_true      = &reserved_tv[1];
286 ir_tarval *tarval_bad         = &reserved_tv[2];
287 ir_tarval *tarval_undefined   = &reserved_tv[3];
288 ir_tarval *tarval_reachable   = &reserved_tv[4];
289 ir_tarval *tarval_unreachable = &reserved_tv[5];
290 
291 /**
292  * get the float descriptor for given mode.
293  */
get_descriptor(const ir_mode * mode)294 static const float_descriptor_t *get_descriptor(const ir_mode *mode)
295 {
296 	return &mode->float_desc;
297 }
298 
new_integer_tarval_from_str(const char * str,size_t len,char sign,unsigned char base,ir_mode * mode)299 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
300                                        unsigned char base, ir_mode *mode)
301 {
302 	void *buffer;
303 	int   ok;
304 
305 	buffer = alloca(sc_get_buffer_length());
306 
307 	ok = sc_val_from_str(sign, base, str, len, buffer);
308 	if (!ok)
309 		return tarval_bad;
310 
311 	return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
312 }
313 
new_tarval_from_str_int(const char * str,size_t len,ir_mode * mode)314 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
315                                           ir_mode *mode)
316 {
317 	void    *buffer;
318 	unsigned base = 10;
319 	char     sign = 1;
320 	int      ok;
321 
322 	/* skip leading spaces */
323 	while (len > 0 && str[0] == ' ') {
324 		++str;
325 		--len;
326 	}
327 	if (len == 0)
328 		return tarval_bad;
329 
330 	/* 1 sign character allowed */
331 	if (str[0] == '-') {
332 		sign = -1;
333 		++str;
334 		--len;
335 	} else if (str[0] == '+') {
336 		++str;
337 		--len;
338 	}
339 
340 	/* a number starting with '0x' is hexadeciaml,
341 	 * a number starting with '0' (and at least 1 more char) is octal */
342 	if (len >= 2 && str[0] == '0') {
343 		if (str[1] == 'x' || str[1] == 'X') {
344 			str += 2;
345 			len -= 2;
346 			base = 16;
347 		} else if (str[1] == 'b' || str[1] == 'B') {
348 			str += 2;
349 			len -= 2;
350 			base = 2;
351 		} else {
352 			++str;
353 			--len;
354 			base = 8;
355 		}
356 	}
357 	if (len == 0)
358 		return tarval_bad;
359 
360 	buffer = alloca(sc_get_buffer_length());
361 
362 	ok = sc_val_from_str(sign, base, str, len, buffer);
363 	if (!ok)
364 		return tarval_bad;
365 
366 	return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
367 }
368 
369 /*
370  * Constructors =============================================================
371  */
new_tarval_from_str(const char * str,size_t len,ir_mode * mode)372 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
373 {
374 	const float_descriptor_t *desc;
375 
376 	assert(str);
377 	assert(len);
378 	assert(mode);
379 
380 	switch (get_mode_sort(mode)) {
381 	case irms_internal_boolean:
382 		/* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
383 		if (!strcasecmp(str, "true"))
384 			return tarval_b_true;
385 		else if (!strcasecmp(str, "false"))
386 			return tarval_b_false;
387 		else
388 			/* XXX This is C semantics */
389 			return atoi(str) ? tarval_b_true : tarval_b_false;
390 
391 	case irms_float_number:
392 		desc = get_descriptor(mode);
393 		fc_val_from_str(str, len, desc, NULL);
394 		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
395 
396 	case irms_reference:
397 		if (!strcasecmp(str, "null"))
398 			return get_tarval_null(mode);
399 		/* FALLTHROUGH */
400 	case irms_int_number:
401 		return new_tarval_from_str_int(str, len, mode);
402 	default:
403 		panic("Unsupported tarval creation with mode %F", mode);
404 	}
405 }
406 
407 /*
408  * helper function, create a tarval from long
409  */
new_tarval_from_long(long l,ir_mode * mode)410 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
411 {
412 	assert(mode);
413 
414 	switch (get_mode_sort(mode))   {
415 	case irms_internal_boolean:
416 		/* XXX C semantics ! */
417 		return l ? tarval_b_true : tarval_b_false ;
418 
419 	case irms_reference:
420 		/* same as integer modes */
421 	case irms_int_number:
422 		sc_val_from_long(l, NULL);
423 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
424 
425 	case irms_float_number:
426 		return new_tarval_from_double((long double)l, mode);
427 
428 	default:
429 		panic("unsupported mode sort");
430 	}
431 }
432 
433 /* returns non-zero if can be converted to long */
tarval_is_long(ir_tarval * tv)434 int tarval_is_long(ir_tarval *tv)
435 {
436 	if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
437 		return 0;
438 
439 	if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
440 		/* the value might be too big to fit in a long */
441 		sc_max_from_bits(sizeof(long) << 3, 0, NULL);
442 		if (sc_comp(sc_get_buffer(), tv->value) == -1) {
443 			/* really doesn't fit */
444 			return 0;
445 		}
446 	}
447 	return 1;
448 }
449 
450 /* this might overflow the machine's long, so use only with small values */
get_tarval_long(ir_tarval * tv)451 long get_tarval_long(ir_tarval* tv)
452 {
453 	assert(tarval_is_long(tv) && "tarval too big to fit in long");
454 
455 	return sc_val_to_long(tv->value);
456 }
457 
new_tarval_from_long_double(long double d,ir_mode * mode)458 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
459 {
460 	const float_descriptor_t *desc;
461 
462 	assert(mode && (get_mode_sort(mode) == irms_float_number));
463 	desc = get_descriptor(mode);
464 	fc_val_from_ieee754(d, desc, NULL);
465 	return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
466 }
467 
new_tarval_from_double(double d,ir_mode * mode)468 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
469 {
470 	return new_tarval_from_long_double(d, mode);
471 }
472 
473 /* returns non-zero if can be converted to double */
tarval_is_double(ir_tarval * tv)474 int tarval_is_double(ir_tarval *tv)
475 {
476 	assert(tv);
477 
478 	return (get_mode_sort(tv->mode) == irms_float_number);
479 }
480 
get_tarval_long_double(ir_tarval * tv)481 long double get_tarval_long_double(ir_tarval *tv)
482 {
483 	assert(tarval_is_double(tv));
484 
485 	return fc_val_to_ieee754((const fp_value*) tv->value);
486 }
487 
get_tarval_double(ir_tarval * tv)488 double get_tarval_double(ir_tarval *tv)
489 {
490 	return get_tarval_long_double(tv);
491 }
492 
493 
494 /*
495  * Access routines for tarval fields ========================================
496  */
497 
498 /* get the mode of the tarval */
499 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
500 {
501 	return _get_tarval_mode(tv);
502 }
503 
504 /*
505  * Special value query functions ============================================
506  *
507  * These functions calculate and return a tarval representing the requested
508  * value.
509  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
510  * functions, but these are stored on initialization of the irmode module and
511  * therefore the irmode functions should be preferred to the functions below.
512  */
513 
514 ir_tarval *(get_tarval_bad)(void)
515 {
516 	return _get_tarval_bad();
517 }
518 
519 ir_tarval *(get_tarval_undefined)(void)
520 {
521 	return _get_tarval_undefined();
522 }
523 
524 ir_tarval *(get_tarval_b_false)(void)
525 {
526 	return _get_tarval_b_false();
527 }
528 
529 ir_tarval *(get_tarval_b_true)(void)
530 {
531 	return _get_tarval_b_true();
532 }
533 
534 ir_tarval *(get_tarval_reachable)(void)
535 {
536 	return _get_tarval_reachable();
537 }
538 
539 ir_tarval *(get_tarval_unreachable)(void)
540 {
541 	return _get_tarval_unreachable();
542 }
543 
get_tarval_max(ir_mode * mode)544 ir_tarval *get_tarval_max(ir_mode *mode)
545 {
546 	const float_descriptor_t *desc;
547 
548 	switch (get_mode_sort(mode)) {
549 	case irms_internal_boolean:
550 		return tarval_b_true;
551 
552 	case irms_float_number:
553 		desc = get_descriptor(mode);
554 		fc_get_max(desc, NULL);
555 		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
556 
557 	case irms_reference:
558 	case irms_int_number:
559 		sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
560 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
561 	default:
562 		panic("mode %F does not support maximum value", mode);
563 	}
564 }
565 
get_tarval_min(ir_mode * mode)566 ir_tarval *get_tarval_min(ir_mode *mode)
567 {
568 	const float_descriptor_t *desc;
569 
570 	switch (get_mode_sort(mode)) {
571 	case irms_internal_boolean:
572 		return tarval_b_false;
573 
574 	case irms_float_number:
575 		desc = get_descriptor(mode);
576 		fc_get_min(desc, NULL);
577 		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
578 
579 	case irms_reference:
580 	case irms_int_number:
581 		sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
582 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
583 	default:
584 		panic("mode %F does not support minimum value", mode);
585 	}
586 }
587 
588 /** The bit pattern for the pointer NULL */
589 static long _null_value = 0;
590 
get_tarval_null(ir_mode * mode)591 ir_tarval *get_tarval_null(ir_mode *mode)
592 {
593 	switch (get_mode_sort(mode)) {
594 	case irms_float_number:
595 		return new_tarval_from_double(0.0, mode);
596 
597 	case irms_internal_boolean:
598 	case irms_int_number:
599 		return new_tarval_from_long(0l,  mode);
600 
601 	case irms_reference:
602 		return new_tarval_from_long(_null_value, mode);
603 	default:
604 		panic("mode %F does not support null value", mode);
605 	}
606 }
607 
get_tarval_one(ir_mode * mode)608 ir_tarval *get_tarval_one(ir_mode *mode)
609 {
610 	switch (get_mode_sort(mode)) {
611 	case irms_internal_boolean:
612 		return tarval_b_true;
613 
614 	case irms_float_number:
615 		return new_tarval_from_double(1.0, mode);
616 
617 	case irms_reference:
618 	case irms_int_number:
619 		return new_tarval_from_long(1l, mode);
620 	default:
621 		panic("mode %F does not support one value", mode);
622 	}
623 }
624 
get_tarval_all_one(ir_mode * mode)625 ir_tarval *get_tarval_all_one(ir_mode *mode)
626 {
627 	switch (get_mode_sort(mode)) {
628 	case irms_int_number:
629 	case irms_internal_boolean:
630 	case irms_reference:
631 		return tarval_not(get_mode_null(mode));
632 
633 	case irms_float_number:
634 		return new_tarval_from_double(1.0, mode);
635 
636 	default:
637 		panic("mode %F does not support all-one value", mode);
638 	}
639 }
640 
tarval_is_constant(ir_tarval * tv)641 int tarval_is_constant(ir_tarval *tv)
642 {
643 	size_t const num_res = ARRAY_SIZE(reserved_tv);
644 
645 	/* reserved tarvals are NOT constants. Note that although
646 	   tarval_b_true and tarval_b_false are reserved, they are constants of course. */
647 	return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
648 }
649 
get_tarval_minus_one(ir_mode * mode)650 ir_tarval *get_tarval_minus_one(ir_mode *mode)
651 {
652 	switch (get_mode_sort(mode)) {
653 	case irms_reference:
654 		return tarval_bad;
655 
656 	case irms_float_number:
657 		return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
658 
659 	case irms_int_number:
660 		return new_tarval_from_long(-1l, mode);
661 
662 	default:
663 		panic("mode %F does not support minus one value", mode);
664 	}
665 }
666 
get_tarval_nan(ir_mode * mode)667 ir_tarval *get_tarval_nan(ir_mode *mode)
668 {
669 	const float_descriptor_t *desc;
670 
671 	if (get_mode_sort(mode) == irms_float_number) {
672 		desc = get_descriptor(mode);
673 		fc_get_qnan(desc, NULL);
674 		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
675 	} else
676 		panic("mode %F does not support NaN value", mode);
677 }
678 
get_tarval_plus_inf(ir_mode * mode)679 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
680 {
681 	if (get_mode_sort(mode) == irms_float_number) {
682 		const float_descriptor_t *desc = get_descriptor(mode);
683 		fc_get_plusinf(desc, NULL);
684 		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
685 	} else
686 		panic("mode %F does not support +inf value", mode);
687 }
688 
get_tarval_minus_inf(ir_mode * mode)689 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
690 {
691 	if (get_mode_sort(mode) == irms_float_number) {
692 		const float_descriptor_t *desc = get_descriptor(mode);
693 		fc_get_minusinf(desc, NULL);
694 		return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
695 	} else
696 		panic("mode %F does not support -inf value", mode);
697 }
698 
699 /*
700  * Arithmetic operations on tarvals ========================================
701  */
702 
703 /*
704  * test if negative number, 1 means 'yes'
705  */
tarval_is_negative(ir_tarval * a)706 int tarval_is_negative(ir_tarval *a)
707 {
708 	switch (get_mode_sort(a->mode)) {
709 	case irms_int_number:
710 		if (!mode_is_signed(a->mode)) return 0;
711 		else
712 			return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
713 
714 	case irms_float_number:
715 		return fc_is_negative((const fp_value*) a->value);
716 
717 	default:
718 		panic("mode %F does not support negation value", a->mode);
719 	}
720 }
721 
722 /*
723  * test if null, 1 means 'yes'
724  */
tarval_is_null(ir_tarval * a)725 int tarval_is_null(ir_tarval *a)
726 {
727 	return
728 		a != tarval_bad &&
729 		a == get_mode_null(get_tarval_mode(a));
730 }
731 
732 /*
733  * test if one, 1 means 'yes'
734  */
tarval_is_one(ir_tarval * a)735 int tarval_is_one(ir_tarval *a)
736 {
737 	return
738 		a != tarval_bad &&
739 		a == get_mode_one(get_tarval_mode(a));
740 }
741 
tarval_is_all_one(ir_tarval * tv)742 int tarval_is_all_one(ir_tarval *tv)
743 {
744 	return
745 		tv != tarval_bad &&
746 		tv == get_mode_all_one(get_tarval_mode(tv));
747 }
748 
749 /*
750  * test if one, 1 means 'yes'
751  */
tarval_is_minus_one(ir_tarval * a)752 int tarval_is_minus_one(ir_tarval *a)
753 {
754 	return
755 		a != tarval_bad &&
756 		a == get_mode_minus_one(get_tarval_mode(a));
757 }
758 
759 /*
760  * comparison
761  */
tarval_cmp(ir_tarval * a,ir_tarval * b)762 ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
763 {
764 	carry_flag = -1;
765 
766 	if (a == tarval_bad || b == tarval_bad) {
767 		panic("Comparison with tarval_bad");
768 	}
769 
770 	if (a == tarval_undefined || b == tarval_undefined)
771 		return ir_relation_false;
772 
773 	if (a->mode != b->mode)
774 		return ir_relation_false;
775 
776 	/* Here the two tarvals are unequal and of the same mode */
777 	switch (get_mode_sort(a->mode)) {
778 	case irms_float_number:
779 		/*
780 		 * BEWARE: we cannot compare a == b here, because
781 		 * a NaN is always Unordered to any other value, even to itself!
782 		 */
783 		switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
784 		case -1: return ir_relation_less;
785 		case  0: return ir_relation_equal;
786 		case  1: return ir_relation_greater;
787 		case  2: return ir_relation_unordered;
788 		default: return ir_relation_false;
789 		}
790 	case irms_reference:
791 	case irms_int_number:
792 		if (a == b)
793 			return ir_relation_equal;
794 		return sc_comp(a->value, b->value) == 1 ? ir_relation_greater : ir_relation_less;
795 
796 	case irms_internal_boolean:
797 		if (a == b)
798 			return ir_relation_equal;
799 		return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
800 
801 	default:
802 		panic("can't compare values of mode %F", a->mode);
803 	}
804 }
805 
806 /*
807  * convert to other mode
808  */
tarval_convert_to(ir_tarval * src,ir_mode * dst_mode)809 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
810 {
811 	char                    *buffer;
812 	fp_value                *res = NULL;
813 	const float_descriptor_t *desc;
814 	int                      len;
815 
816 	carry_flag = -1;
817 
818 	assert(src);
819 	assert(dst_mode);
820 
821 	if (src->mode == dst_mode)
822 		return src;
823 
824 	switch (get_mode_sort(src->mode)) {
825 	/* cast float to something */
826 	case irms_float_number:
827 		switch (get_mode_sort(dst_mode)) {
828 		case irms_float_number:
829 			desc = get_descriptor(dst_mode);
830 			fc_cast((const fp_value*) src->value, desc, NULL);
831 			return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
832 
833 		case irms_int_number:
834 			switch (current_float_to_int_mode) {
835 			case TRUNCATE:
836 				res = fc_int((const fp_value*) src->value, NULL);
837 				break;
838 			case ROUND:
839 				res = fc_rnd((const fp_value*) src->value, NULL);
840 				break;
841 			}
842 			buffer = (char*) alloca(sc_get_buffer_length());
843 			if (! fc_flt2int(res, buffer, dst_mode))
844 				return tarval_bad;
845 			return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
846 
847 		default:
848 			break;
849 		}
850 		/* the rest can't be converted */
851 		return tarval_bad;
852 
853 	/* cast int/characters to something */
854 	case irms_int_number:
855 		switch (get_mode_sort(dst_mode)) {
856 
857 		case irms_reference:
858 		case irms_int_number:
859 			buffer = (char*) alloca(sc_get_buffer_length());
860 			memcpy(buffer, src->value, sc_get_buffer_length());
861 			return get_tarval_overflow(buffer, src->length, dst_mode);
862 
863 		case irms_internal_boolean:
864 			/* XXX C semantics */
865 			if (src == get_mode_null(src->mode)) return tarval_b_false;
866 			else return tarval_b_true;
867 
868 		case irms_float_number:
869 			/* XXX floating point unit does not understand internal integer
870 			 * representation, convert to string first, then create float from
871 			 * string */
872 			buffer = (char*) alloca(100);
873 			/* decimal string representation because hexadecimal output is
874 			 * interpreted unsigned by fc_val_from_str, so this is a HACK */
875 			len = snprintf(buffer, 100, "%s",
876 				sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
877 			buffer[100 - 1] = '\0';
878 			desc = get_descriptor(dst_mode);
879 			fc_val_from_str(buffer, len, desc, NULL);
880 			return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
881 
882 		default:
883 			break;
884 		}
885 		break;
886 
887 	case irms_internal_boolean:
888 		/* beware: this is C semantic for the INTERNAL boolean mode */
889 		if (get_mode_sort(dst_mode) == irms_int_number)
890 			return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
891 		break;
892 
893 	case irms_reference:
894 		if (get_mode_sort(dst_mode) == irms_int_number) {
895 			buffer = (char*) alloca(sc_get_buffer_length());
896 			memcpy(buffer, src->value, sc_get_buffer_length());
897 			sign_extend(buffer, src->mode);
898 			return get_tarval_overflow(buffer, src->length, dst_mode);
899 		}
900 		break;
901 	default:
902 		return tarval_bad;
903 	}
904 
905 	return tarval_bad;
906 }
907 
908 /*
909  * bitwise negation
910  */
tarval_not(ir_tarval * a)911 ir_tarval *tarval_not(ir_tarval *a)
912 {
913 	char *buffer;
914 
915 	carry_flag = -1;
916 
917 	/* works for vector mode without changes */
918 
919 	switch (get_mode_sort(a->mode)) {
920 	case irms_reference:
921 	case irms_int_number:
922 		buffer = (char*) alloca(sc_get_buffer_length());
923 		sc_not(a->value, buffer);
924 		return get_tarval(buffer, a->length, a->mode);
925 
926 	case irms_internal_boolean:
927 		if (a == tarval_b_true)
928 			return tarval_b_false;
929 		if (a == tarval_b_false)
930 			return tarval_b_true;
931 		return tarval_bad;
932 
933 	default:
934 		panic("bitwise negation is only allowed for integer and boolean");
935 	}
936 }
937 
938 /*
939  * arithmetic negation
940  */
tarval_neg(ir_tarval * a)941 ir_tarval *tarval_neg(ir_tarval *a)
942 {
943 	char *buffer;
944 
945 	assert(mode_is_num(a->mode)); /* negation only for numerical values */
946 
947 	carry_flag = -1;
948 
949 	/* note: negation is allowed even for unsigned modes. */
950 
951 	switch (get_mode_sort(a->mode)) {
952 	case irms_int_number:
953 		buffer = (char*) alloca(sc_get_buffer_length());
954 		sc_neg(a->value, buffer);
955 		return get_tarval_overflow(buffer, a->length, a->mode);
956 
957 	case irms_float_number:
958 		/* it should be safe to enable this even if other arithmetic is disabled */
959 		/*if (no_float)
960 			return tarval_bad;*/
961 
962 		fc_neg((const fp_value*) a->value, NULL);
963 		return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
964 
965 	default:
966 		return tarval_bad;
967 	}
968 }
969 
970 /*
971  * addition
972  */
tarval_add(ir_tarval * a,ir_tarval * b)973 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
974 {
975 	char *buffer;
976 
977 	carry_flag = -1;
978 
979 	if (mode_is_reference(a->mode) && a->mode != b->mode) {
980 		b = tarval_convert_to(b, a->mode);
981 	} else if (mode_is_reference(b->mode) && b->mode != a->mode) {
982 		a = tarval_convert_to(a, b->mode);
983 	}
984 
985 	assert(a->mode == b->mode);
986 
987 	switch (get_mode_sort(a->mode)) {
988 	case irms_reference:
989 	case irms_int_number:
990 		/* modes of a,b are equal, so result has mode of a as this might be the character */
991 		buffer = (char*) alloca(sc_get_buffer_length());
992 		sc_add(a->value, b->value, buffer);
993 		carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
994 		return get_tarval_overflow(buffer, a->length, a->mode);
995 
996 	case irms_float_number:
997 		if (no_float)
998 			return tarval_bad;
999 
1000 		fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1001 		return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1002 
1003 	default:
1004 		return tarval_bad;
1005 	}
1006 }
1007 
1008 /*
1009  * subtraction
1010  */
tarval_sub(ir_tarval * a,ir_tarval * b,ir_mode * dst_mode)1011 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1012 {
1013 	char    *buffer;
1014 
1015 	carry_flag = -1;
1016 
1017 	if (dst_mode != NULL) {
1018 		if (a->mode != dst_mode)
1019 			a = tarval_convert_to(a, dst_mode);
1020 		if (b->mode != dst_mode)
1021 			b = tarval_convert_to(b, dst_mode);
1022 	}
1023 	assert(a->mode == b->mode);
1024 
1025 	switch (get_mode_sort(a->mode)) {
1026 	case irms_reference:
1027 	case irms_int_number:
1028 		/* modes of a,b are equal, so result has mode of a as this might be the character */
1029 		buffer = (char*) alloca(sc_get_buffer_length());
1030 		sc_sub(a->value, b->value, buffer);
1031 		carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1032 		return get_tarval_overflow(buffer, a->length, a->mode);
1033 
1034 	case irms_float_number:
1035 		if (no_float)
1036 			return tarval_bad;
1037 
1038 		fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1039 		return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1040 
1041 	default:
1042 		return tarval_bad;
1043 	}
1044 }
1045 
1046 /*
1047  * multiplication
1048  */
tarval_mul(ir_tarval * a,ir_tarval * b)1049 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1050 {
1051 	char *buffer;
1052 
1053 	assert(a->mode == b->mode);
1054 
1055 	carry_flag = -1;
1056 
1057 	switch (get_mode_sort(a->mode)) {
1058 	case irms_int_number:
1059 		/* modes of a,b are equal */
1060 		buffer = (char*) alloca(sc_get_buffer_length());
1061 		sc_mul(a->value, b->value, buffer);
1062 		return get_tarval_overflow(buffer, a->length, a->mode);
1063 
1064 	case irms_float_number:
1065 		if (no_float)
1066 			return tarval_bad;
1067 
1068 		fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1069 		return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1070 
1071 	default:
1072 		return tarval_bad;
1073 	}
1074 }
1075 
1076 /*
1077  * division
1078  * overflow is impossible, but look out for division by zero
1079  */
tarval_div(ir_tarval * a,ir_tarval * b)1080 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1081 {
1082 	ir_mode *mode = a->mode;
1083 	assert(mode == b->mode);
1084 
1085 	carry_flag = -1;
1086 
1087 	if (mode_is_int(mode)) {
1088 		/* x/0 error */
1089 		if (b == get_mode_null(mode))
1090 			return tarval_bad;
1091 
1092 		/* modes of a,b are equal */
1093 		sc_div(a->value, b->value, NULL);
1094 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1095 	} else {
1096 		assert(mode_is_float(mode));
1097 		fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1098 		return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
1099 	}
1100 }
1101 
1102 /*
1103  * remainder
1104  * overflow is impossible, but look out for division by zero
1105  */
tarval_mod(ir_tarval * a,ir_tarval * b)1106 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1107 {
1108 	assert((a->mode == b->mode) && mode_is_int(a->mode));
1109 
1110 	carry_flag = -1;
1111 
1112 	/* x/0 error */
1113 	if (b == get_mode_null(b->mode)) return tarval_bad;
1114 	/* modes of a,b are equal */
1115 	sc_mod(a->value, b->value, NULL);
1116 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1117 }
1118 
1119 /*
1120  * integer division AND remainder
1121  * overflow is impossible, but look out for division by zero
1122  */
tarval_divmod(ir_tarval * a,ir_tarval * b,ir_tarval ** mod)1123 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1124 {
1125 	int len = sc_get_buffer_length();
1126 	char *div_res = (char*) alloca(len);
1127 	char *mod_res = (char*) alloca(len);
1128 
1129 	assert((a->mode == b->mode) && mode_is_int(a->mode));
1130 
1131 	carry_flag = -1;
1132 
1133 	/* x/0 error */
1134 	if (b == get_mode_null(b->mode)) return tarval_bad;
1135 	/* modes of a,b are equal */
1136 	sc_divmod(a->value, b->value, div_res, mod_res);
1137 	*mod = get_tarval(mod_res, len, a->mode);
1138 	return get_tarval(div_res, len, a->mode);
1139 }
1140 
1141 /*
1142  * absolute value
1143  */
tarval_abs(ir_tarval * a)1144 ir_tarval *tarval_abs(ir_tarval *a)
1145 {
1146 	char *buffer;
1147 
1148 	carry_flag = -1;
1149 	assert(mode_is_num(a->mode));
1150 
1151 	switch (get_mode_sort(a->mode)) {
1152 	case irms_int_number:
1153 		if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1154 			buffer = (char*) alloca(sc_get_buffer_length());
1155 			sc_neg(a->value, buffer);
1156 			return get_tarval_overflow(buffer, a->length, a->mode);
1157 		}
1158 		return a;
1159 
1160 	case irms_float_number:
1161 		/* it should be safe to enable this even if other arithmetic is disabled */
1162 		/*if (no_float)
1163 			return tarval_bad;*/
1164 
1165 		if (fc_comp((const fp_value*) a->value,
1166 		    (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1167 			fc_neg((const fp_value*) a->value, NULL);
1168 			return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1169 		}
1170 		return a;
1171 
1172 	default:
1173 		break;
1174 	}
1175 	return tarval_bad;
1176 }
1177 
1178 /*
1179  * bitwise and
1180  */
tarval_and(ir_tarval * a,ir_tarval * b)1181 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1182 {
1183 	assert(a->mode == b->mode);
1184 
1185 	/* works even for vector modes */
1186 	carry_flag = 0;
1187 
1188 	switch (get_mode_sort(a->mode)) {
1189 	case irms_internal_boolean:
1190 		return (a == tarval_b_false) ? a : b;
1191 
1192 	case irms_reference:
1193 	case irms_int_number:
1194 		sc_and(a->value, b->value, NULL);
1195 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1196 
1197 	default:
1198 		panic("operation not defined on mode");
1199 	}
1200 }
1201 
tarval_andnot(ir_tarval * a,ir_tarval * b)1202 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1203 {
1204 	assert(a->mode == b->mode);
1205 
1206 	/* works even for vector modes */
1207 	carry_flag = 0;
1208 
1209 	switch (get_mode_sort(a->mode)) {
1210 	case irms_internal_boolean:
1211 		return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1212 
1213 	case irms_reference:
1214 	case irms_int_number:
1215 		sc_andnot(a->value, b->value, NULL);
1216 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1217 
1218 	default:
1219 		panic("operation not defined on mode");
1220 	}
1221 }
1222 
1223 /*
1224  * bitwise or
1225  */
tarval_or(ir_tarval * a,ir_tarval * b)1226 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1227 {
1228 	assert(a->mode == b->mode);
1229 
1230 	/* works even for vector modes */
1231 	carry_flag = 0;
1232 
1233 	switch (get_mode_sort(a->mode)) {
1234 	case irms_internal_boolean:
1235 		return (a == tarval_b_true) ? a : b;
1236 
1237 	case irms_reference:
1238 	case irms_int_number:
1239 		sc_or(a->value, b->value, NULL);
1240 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1241 
1242 	default:
1243 		panic("operation not defined on mode");
1244 	}
1245 }
1246 
1247 /*
1248  * bitwise exclusive or (xor)
1249  */
tarval_eor(ir_tarval * a,ir_tarval * b)1250 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1251 {
1252 	assert((a->mode == b->mode));
1253 
1254 	/* works even for vector modes */
1255 	carry_flag = 0;
1256 
1257 	switch (get_mode_sort(a->mode)) {
1258 	case irms_internal_boolean:
1259 		return (a == b)? tarval_b_false : tarval_b_true;
1260 
1261 	case irms_reference:
1262 	case irms_int_number:
1263 		sc_xor(a->value, b->value, NULL);
1264 		return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1265 
1266 	default:
1267 		panic("operation not defined on mode");
1268 	}
1269 }
1270 
1271 /*
1272  * bitwise left shift
1273  */
tarval_shl(ir_tarval * a,ir_tarval * b)1274 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1275 {
1276 	char *temp_val = NULL;
1277 
1278 	assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1279 
1280 	carry_flag = -1;
1281 
1282 	if (get_mode_modulo_shift(a->mode) != 0) {
1283 		temp_val = (char*) alloca(sc_get_buffer_length());
1284 
1285 		sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1286 		sc_mod(b->value, temp_val, temp_val);
1287 	} else
1288 		temp_val = (char*)b->value;
1289 
1290 	sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1291 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1292 }
1293 
tarval_shl_unsigned(ir_tarval * a,unsigned b)1294 ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b)
1295 {
1296 	ir_mode *mode   = a->mode;
1297 	unsigned modulo = get_mode_modulo_shift(mode);
1298 	if (modulo != 0)
1299 		b %= modulo;
1300 	assert((unsigned)(long)b==b);
1301 	sc_shlI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1302 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1303 }
1304 
1305 /*
1306  * bitwise unsigned right shift
1307  */
tarval_shr(ir_tarval * a,ir_tarval * b)1308 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1309 {
1310 	char *temp_val = NULL;
1311 
1312 	assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1313 
1314 	carry_flag = -1;
1315 
1316 	if (get_mode_modulo_shift(a->mode) != 0) {
1317 		temp_val = (char*) alloca(sc_get_buffer_length());
1318 
1319 		sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1320 		sc_mod(b->value, temp_val, temp_val);
1321 	} else
1322 		temp_val = (char*)b->value;
1323 
1324 	sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1325 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1326 }
1327 
tarval_shr_unsigned(ir_tarval * a,unsigned b)1328 ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b)
1329 {
1330 	ir_mode *mode   = a->mode;
1331 	unsigned modulo = get_mode_modulo_shift(mode);
1332 	if (modulo != 0)
1333 		b %= modulo;
1334 	assert((unsigned)(long)b==b);
1335 	sc_shrI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1336 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1337 }
1338 
1339 /*
1340  * bitwise signed right shift
1341  */
tarval_shrs(ir_tarval * a,ir_tarval * b)1342 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1343 {
1344 	char *temp_val = NULL;
1345 
1346 	assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1347 
1348 	carry_flag = -1;
1349 
1350 	if (get_mode_modulo_shift(a->mode) != 0) {
1351 		temp_val = (char*) alloca(sc_get_buffer_length());
1352 
1353 		sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1354 		sc_mod(b->value, temp_val, temp_val);
1355 	} else
1356 		temp_val = (char*)b->value;
1357 
1358 	sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1359 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1360 }
1361 
tarval_shrs_unsigned(ir_tarval * a,unsigned b)1362 ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
1363 {
1364 	ir_mode *mode   = a->mode;
1365 	unsigned modulo = get_mode_modulo_shift(mode);
1366 	if (modulo != 0)
1367 		b %= modulo;
1368 	assert((unsigned)(long)b==b);
1369 	sc_shrsI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1370 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1371 }
1372 
1373 /*
1374  * bitwise rotation to left
1375  */
tarval_rotl(ir_tarval * a,ir_tarval * b)1376 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1377 {
1378 	char *temp_val = NULL;
1379 
1380 	assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1381 
1382 	carry_flag = -1;
1383 
1384 	if (get_mode_modulo_shift(a->mode) != 0) {
1385 		temp_val = (char*) alloca(sc_get_buffer_length());
1386 
1387 		sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1388 		sc_mod(b->value, temp_val, temp_val);
1389 	} else
1390 		temp_val = (char*)b->value;
1391 
1392 	sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1393 	return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1394 }
1395 
1396 /*
1397  * carry flag of the last operation
1398  */
tarval_carry(void)1399 int tarval_carry(void)
1400 {
1401 	if (carry_flag == -1)
1402 		panic("Carry undefined for the last operation");
1403 	return carry_flag;
1404 }
1405 
1406 /*
1407  * Output of tarvals
1408  */
tarval_snprintf(char * buf,size_t len,ir_tarval * tv)1409 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1410 {
1411 	static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1412 
1413 	const char *str;
1414 	char tv_buf[100];
1415 	const tarval_mode_info *mode_info;
1416 	const char *prefix, *suffix;
1417 
1418 	mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1419 	if (! mode_info)
1420 		mode_info = &default_info;
1421 	prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1422 	suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1423 
1424 	switch (get_mode_sort(tv->mode)) {
1425 	case irms_reference:
1426 		if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1427 		/* FALLTHROUGH */
1428 	case irms_int_number:
1429 		switch (mode_info->mode_output) {
1430 
1431 		case TVO_DECIMAL:
1432 			str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1433 			break;
1434 
1435 		case TVO_OCTAL:
1436 			str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1437 			break;
1438 
1439 		case TVO_NATIVE:
1440 			prefix = "0x";
1441 		case TVO_HEX:
1442 		default:
1443 			str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1444 			break;
1445 		}
1446 		return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1447 
1448 	case irms_float_number:
1449 		switch (mode_info->mode_output) {
1450 		case TVO_HEX:
1451 			return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1452 
1453 		case TVO_HEXFLOAT:
1454 			return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1455 
1456 		case TVO_FLOAT:
1457 		case TVO_NATIVE:
1458 		default:
1459 			return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1460 		}
1461 
1462 	case irms_internal_boolean:
1463 		switch (mode_info->mode_output) {
1464 
1465 		case TVO_DECIMAL:
1466 		case TVO_OCTAL:
1467 		case TVO_HEX:
1468 		case TVO_BINARY:
1469 			return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1470 
1471 		case TVO_NATIVE:
1472 		default:
1473 			return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1474 		}
1475 
1476 	default:
1477 		if (tv == tarval_bad)
1478 			return snprintf(buf, len, "<TV_BAD>");
1479 		else if (tv == tarval_undefined)
1480 			return snprintf(buf, len, "<TV_UNDEFINED>");
1481 		else if (tv == tarval_reachable)
1482 			return snprintf(buf, len, "<TV_REACHABLE>");
1483 		else if (tv == tarval_unreachable)
1484 			return snprintf(buf, len, "<TV_UNREACHABLE>");
1485 		else
1486 			return snprintf(buf, len, "<TV_??""?>");
1487 	}
1488 }
1489 
1490 /**
1491  * Output of tarvals to stdio.
1492  */
tarval_printf(ir_tarval * tv)1493 int tarval_printf(ir_tarval *tv)
1494 {
1495 	char buf[1024];
1496 	int res;
1497 
1498 	res = tarval_snprintf(buf, sizeof(buf), tv);
1499 	assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1500 	printf("%s", buf);
1501 	return res;
1502 }
1503 
get_tarval_bitpattern(ir_tarval * tv)1504 char *get_tarval_bitpattern(ir_tarval *tv)
1505 {
1506 	int i, j, pos = 0;
1507 	int n = get_mode_size_bits(tv->mode);
1508 	int bytes = (n + 7) / 8;
1509 	char *res = XMALLOCN(char, n + 1);
1510 	unsigned char byte;
1511 
1512 	for (i = 0; i < bytes; i++) {
1513 		byte = get_tarval_sub_bits(tv, i);
1514 		for (j = 1; j < 256; j <<= 1)
1515 			if (pos < n)
1516 				res[pos++] = j & byte ? '1' : '0';
1517 	}
1518 
1519 	res[n] = '\0';
1520 
1521 	return res;
1522 }
1523 
1524 /*
1525  * access to the bitpattern
1526  */
get_tarval_sub_bits(ir_tarval * tv,unsigned byte_ofs)1527 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1528 {
1529 	switch (get_mode_arithmetic(tv->mode)) {
1530 	case irma_twos_complement:
1531 		return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1532 	case irma_ieee754:
1533 	case irma_x86_extended_float:
1534 		return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1535 	default:
1536 		panic("arithmetic mode not supported");
1537 	}
1538 }
1539 
1540 /*
1541  * Specify the output options of one mode.
1542  *
1543  * This functions stores the modinfo, so DO NOT DESTROY it.
1544  *
1545  * Returns zero on success.
1546  */
set_tarval_mode_output_option(ir_mode * mode,const tarval_mode_info * modeinfo)1547 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1548 {
1549 	assert(mode);
1550 
1551 	mode->tv_priv = modeinfo;
1552 	return 0;
1553 }
1554 
1555 /*
1556  * Returns the output options of one mode.
1557  *
1558  * This functions returns the mode info of a given mode.
1559  */
get_tarval_mode_output_option(ir_mode * mode)1560 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1561 {
1562 	assert(mode);
1563 
1564 	return (const tarval_mode_info*) mode->tv_priv;
1565 }
1566 
1567 /*
1568  * Returns non-zero if a given (integer) tarval has only one single bit
1569  * set.
1570  */
tarval_is_single_bit(ir_tarval * tv)1571 int tarval_is_single_bit(ir_tarval *tv)
1572 {
1573 	int i, l;
1574 	int bits;
1575 
1576 	if (!tv || tv == tarval_bad) return 0;
1577 	if (! mode_is_int(tv->mode)) return 0;
1578 
1579 	l = get_mode_size_bytes(tv->mode);
1580 	for (bits = 0, i = l - 1; i >= 0; --i) {
1581 		unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1582 
1583 		/* check for more than one bit in these */
1584 		if (v) {
1585 			if (v & (v-1))
1586 				return 0;
1587 			if (++bits > 1)
1588 				return 0;
1589 		}
1590 	}
1591 	return bits;
1592 }
1593 
1594 /*
1595  * Return the number of set bits in a given (integer) tarval.
1596  */
get_tarval_popcount(ir_tarval * tv)1597 int get_tarval_popcount(ir_tarval *tv)
1598 {
1599 	int i, l;
1600 	int bits;
1601 
1602 	if (!tv || tv == tarval_bad) return -1;
1603 	if (! mode_is_int(tv->mode)) return -1;
1604 
1605 	l = get_mode_size_bytes(tv->mode);
1606 	for (bits = 0, i = l - 1; i >= 0; --i) {
1607 		unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1608 
1609 		bits += popcount(v);
1610 	}
1611 	return bits;
1612 }
1613 
1614 /**
1615  * Return the number of the lowest set bit in a given (integer) tarval.
1616  *
1617  * @param tv    the tarval
1618  *
1619  * @return number of lowest set bit or -1 on error
1620  */
get_tarval_lowest_bit(ir_tarval * tv)1621 int get_tarval_lowest_bit(ir_tarval *tv)
1622 {
1623 	int i, l;
1624 
1625 	if (!tv || tv == tarval_bad) return -1;
1626 	if (! mode_is_int(tv->mode)) return -1;
1627 
1628 	l = get_mode_size_bytes(tv->mode);
1629 	for (i = 0; i < l; ++i) {
1630 		unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1631 
1632 		if (v)
1633 			return ntz(v) + i * 8;
1634 	}
1635 	return -1;
1636 }
1637 
1638 /*
1639  * Returns non-zero if the mantissa of a floating point IEEE-754
1640  * tarval is zero (i.e. 1.0Exxx)
1641  */
tarval_zero_mantissa(ir_tarval * tv)1642 int tarval_zero_mantissa(ir_tarval *tv)
1643 {
1644 	assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1645 	       || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1646 	return fc_zero_mantissa((const fp_value*) tv->value);
1647 }
1648 
1649 /* Returns the exponent of a floating point IEEE-754 tarval. */
tarval_get_exponent(ir_tarval * tv)1650 int tarval_get_exponent(ir_tarval *tv)
1651 {
1652 	assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1653 	       || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1654 	return fc_get_exponent((const fp_value*) tv->value);
1655 }
1656 
1657 /*
1658  * Check if the tarval can be converted to the given mode without
1659  * precision loss.
1660  */
tarval_ieee754_can_conv_lossless(ir_tarval * tv,ir_mode * mode)1661 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1662 {
1663 	const float_descriptor_t *desc = get_descriptor(mode);
1664 	return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1665 }
1666 
1667 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
tarval_ieee754_get_exact(void)1668 unsigned tarval_ieee754_get_exact(void)
1669 {
1670 	return fc_is_exact();
1671 }
1672 
1673 /* check if its the a floating point NaN */
tarval_is_NaN(ir_tarval * tv)1674 int tarval_is_NaN(ir_tarval *tv)
1675 {
1676 	if (! mode_is_float(tv->mode))
1677 		return 0;
1678 	return fc_is_nan((const fp_value*) tv->value);
1679 }
1680 
1681 /* check if its the a floating point +inf */
tarval_is_plus_inf(ir_tarval * tv)1682 int tarval_is_plus_inf(ir_tarval *tv)
1683 {
1684 	if (! mode_is_float(tv->mode))
1685 		return 0;
1686 	return fc_is_inf((const fp_value*) tv->value)
1687 		&& !fc_is_negative((const fp_value*) tv->value);
1688 }
1689 
1690 /* check if its the a floating point -inf */
tarval_is_minus_inf(ir_tarval * tv)1691 int tarval_is_minus_inf(ir_tarval *tv)
1692 {
1693 	if (! mode_is_float(tv->mode))
1694 		return 0;
1695 	return fc_is_inf((const fp_value*) tv->value)
1696 		&& fc_is_negative((const fp_value*) tv->value);
1697 }
1698 
1699 /* check if the tarval represents a finite value */
tarval_is_finite(ir_tarval * tv)1700 int tarval_is_finite(ir_tarval *tv)
1701 {
1702 	if (mode_is_float(tv->mode))
1703 		return !fc_is_nan((const fp_value*) tv->value)
1704 			&& !fc_is_inf((const fp_value*) tv->value);
1705 	return 1;
1706 }
1707 
1708 /*
1709  * Sets the overflow mode for integer operations.
1710  */
tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)1711 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1712 {
1713 	int_overflow_mode = ov_mode;
1714 }
1715 
1716 /* Get the overflow mode for integer operations. */
tarval_get_integer_overflow_mode(void)1717 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1718 {
1719 	return int_overflow_mode;
1720 }
1721 
1722 /* Enable/Disable floating point constant folding. */
tarval_enable_fp_ops(int enable)1723 void tarval_enable_fp_ops(int enable)
1724 {
1725 	no_float = !enable;
1726 }
1727 
tarval_fp_ops_enabled(void)1728 int tarval_fp_ops_enabled(void)
1729 {
1730 	return !no_float;
1731 }
1732 
1733 /**
1734  * default mode_info for output as HEX
1735  */
1736 static const tarval_mode_info hex_output = {
1737 	TVO_HEX,
1738 	"0x",
1739 	NULL,
1740 };
1741 
1742 /*
1743  * Initialization of the tarval module: called before init_mode()
1744  */
init_tarval_1(long null_value,int support_quad_precision)1745 void init_tarval_1(long null_value, int support_quad_precision)
1746 {
1747 	/* if these assertion fail, tarval_is_constant() will follow ... */
1748 	assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1749 	assert(tarval_b_true  == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1750 
1751 	_null_value = null_value;
1752 
1753 	/* initialize the sets holding the tarvals with a comparison function and
1754 	 * an initial size, which is the expected number of constants */
1755 	tarvals = new_set(cmp_tv, N_CONSTANTS);
1756 	values  = new_set(memcmp, N_CONSTANTS);
1757 	/* calls init_strcalc() with needed size */
1758 	init_fltcalc(support_quad_precision ? 112 : 64);
1759 }
1760 
1761 /*
1762  * Initialization of the tarval module: called after init_mode()
1763  */
init_tarval_2(void)1764 void init_tarval_2(void)
1765 {
1766 	tarval_bad->kind          = k_tarval;
1767 	tarval_bad->mode          = mode_BAD;
1768 	tarval_bad->value         = INT_TO_PTR(resid_tarval_bad);
1769 
1770 	tarval_undefined->kind    = k_tarval;
1771 	tarval_undefined->mode    = mode_ANY;
1772 	tarval_undefined->value   = INT_TO_PTR(resid_tarval_undefined);
1773 
1774 	tarval_b_true->kind       = k_tarval;
1775 	tarval_b_true->mode       = mode_b;
1776 	tarval_b_true->value      = INT_TO_PTR(resid_tarval_b_true);
1777 
1778 	tarval_b_false->kind      = k_tarval;
1779 	tarval_b_false->mode      = mode_b;
1780 	tarval_b_false->value     = INT_TO_PTR(resid_tarval_b_false);
1781 
1782 	tarval_unreachable->kind  = k_tarval;
1783 	tarval_unreachable->mode  = mode_X;
1784 	tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1785 
1786 	tarval_reachable->kind    = k_tarval;
1787 	tarval_reachable->mode    = mode_X;
1788 	tarval_reachable->value   = INT_TO_PTR(resid_tarval_reachable);
1789 
1790 	/*
1791 	 * assign output modes that are compatible with the
1792 	 * old implementation: Hex output
1793 	 */
1794 	set_tarval_mode_output_option(mode_Bs, &hex_output);
1795 	set_tarval_mode_output_option(mode_Bu, &hex_output);
1796 	set_tarval_mode_output_option(mode_Hs, &hex_output);
1797 	set_tarval_mode_output_option(mode_Hu, &hex_output);
1798 	set_tarval_mode_output_option(mode_Is, &hex_output);
1799 	set_tarval_mode_output_option(mode_Iu, &hex_output);
1800 	set_tarval_mode_output_option(mode_Ls, &hex_output);
1801 	set_tarval_mode_output_option(mode_Lu, &hex_output);
1802 	set_tarval_mode_output_option(mode_P,  &hex_output);
1803 }
1804 
1805 /* free all memory occupied by tarval. */
finish_tarval(void)1806 void finish_tarval(void)
1807 {
1808 	finish_strcalc();
1809 	finish_fltcalc();
1810 	del_set(tarvals); tarvals = NULL;
1811 	del_set(values);  values = NULL;
1812 }
1813 
1814 int (is_tarval)(const void *thing)
1815 {
1816 	return _is_tarval(thing);
1817 }
1818