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