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