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