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