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