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