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