allow Cmp, Neg, Abs for floating point even if no_float is set: these Operations...
[libfirm] / ir / tv / tv.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Representation of and static computations on target machine
23  *           values.
24  * @date     2003
25  * @author   Mathias Heil
26  * @version  $Id$
27  * @summary
28  *
29  * Values are stored in a format depending upon chosen arithmetic
30  * module. Default uses strcalc and fltcalc.
31  * This implementation assumes:
32  *  - target has IEEE-754 floating-point arithmetic.
33  */
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>         /* assertions */
39 #include <stdlib.h>         /* atoi() */
40 #ifdef HAVE_STRING_H
41 # include <string.h>         /* nice things for strings */
42 #endif
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h>        /* strings.h also includes bsd only function strcasecmp */
45 #endif
46 #ifdef HAVE_STDLIB_H
47 # include <stdlib.h>
48 #endif
49
50 #include "tv_t.h"
51 #include "set.h"            /* to store tarvals in */
52 #include "entity_t.h"       /* needed to store pointers to entities */
53 #include "irmode_t.h"
54 #include "irnode.h"         /* defines boolean return values (pnc_number)*/
55 #include "strcalc.h"
56 #include "fltcalc.h"
57 #include "irtools.h"
58 #include "xmalloc.h"
59 #include "firm_common.h"
60 #include "error.h"
61
62 /** Size of hash tables.  Should correspond to average number of distinct constant
63     target values */
64 #define N_CONSTANTS 2048
65
66 /* get the integer overflow mode */
67 #define GET_OVERFLOW_MODE() int_overflow_mode
68
69 /* unused, float to int doesn't work yet */
70 enum float_to_int_mode {
71         TRUNCATE,
72         ROUND
73 };
74
75 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
76
77 #define SWITCH_NOINFINITY 0
78 #define SWITCH_NODENORMALS 0
79
80 /****************************************************************************
81  *   local definitions and macros
82  ****************************************************************************/
83 #ifndef NDEBUG
84 #  define TARVAL_VERIFY(a) tarval_verify((a))
85 #else
86 #  define TARVAL_VERIFY(a) ((void)0)
87 #endif
88
89 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
90 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
91
92 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
93 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
94
95 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
96
97 /****************************************************************************
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                 /* it should be safe to enable this even if other arithmetic is disabled */
830                 /*if (no_float)
831                         return pn_Cmp_False;*/
832                 /*
833                  * BEWARE: we cannot compare a == b here, because
834                  * a NaN is always Unordered to any other value, even to itself!
835                  */
836                 switch (fc_comp(a->value, b->value)) {
837                 case -1: return pn_Cmp_Lt;
838                 case  0: return pn_Cmp_Eq;
839                 case  1: return pn_Cmp_Gt;
840                 case  2: return pn_Cmp_Uo;
841                 default: return pn_Cmp_False;
842                 }
843         case irms_reference:
844         case irms_int_number:
845                 if (a == b)
846                         return pn_Cmp_Eq;
847                 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
848
849         case irms_internal_boolean:
850                 if (a == b)
851                         return pn_Cmp_Eq;
852                 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
853         }
854         return pn_Cmp_False;
855 }
856
857 /*
858  * convert to other mode
859  */
860 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
861         char *buffer;
862         fp_value *res;
863
864         assert(src);
865         assert(dst_mode);
866
867         if (src->mode == dst_mode) return src;
868
869         if (get_mode_n_vector_elems(src->mode) > 1) {
870                 /* vector arithmetic not implemented yet */
871                 return tarval_bad;
872         }
873
874         switch (get_mode_sort(src->mode)) {
875         case irms_control_flow:
876         case irms_memory:
877         case irms_auxiliary:
878                 break;
879
880                 /* cast float to something */
881         case irms_float_number:
882                 switch (get_mode_sort(dst_mode)) {
883                 case irms_float_number:
884                         switch (get_mode_size_bits(dst_mode)) {
885                         case 32:
886                                 fc_cast(src->value, 8, 23, NULL);
887                                 break;
888                         case 64:
889                                 fc_cast(src->value, 11, 52, NULL);
890                                 break;
891                         case 80:
892                         case 96:
893                                 fc_cast(src->value, 15, 64, NULL);
894                                 break;
895                         default:
896                                 panic("Unsupported mode in tarval_convert_to()");
897                         }
898                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
899
900                 case irms_int_number:
901                         switch (GET_FLOAT_TO_INT_MODE()) {
902                         case TRUNCATE:
903                                 res = fc_int(src->value, NULL);
904                                 break;
905                         case ROUND:
906                                 res = fc_rnd(src->value, NULL);
907                                 break;
908                         default:
909                                 assert(0);
910                                 break;
911                         }
912                         buffer = alloca(sc_get_buffer_length());
913                         if (! fc_flt2int(res, buffer, dst_mode))
914                                 return tarval_bad;
915                         return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
916
917                 default:
918                         /* the rest can't be converted */
919                         return tarval_bad;
920                 }
921                 break;
922
923         /* cast int/characters to something */
924         case irms_int_number:
925                 switch (get_mode_sort(dst_mode)) {
926
927                 case irms_reference:
928                 case irms_int_number:
929                         buffer = alloca(sc_get_buffer_length());
930                         memcpy(buffer, src->value, sc_get_buffer_length());
931                         sign_extend(buffer, dst_mode);
932                         return get_tarval_overflow(buffer, src->length, dst_mode);
933
934                 case irms_internal_boolean:
935                         /* XXX C semantics */
936                         if (src == get_mode_null(src->mode)) return tarval_b_false;
937                         else return tarval_b_true;
938
939                 case irms_float_number:
940                         /* XXX floating point unit does not understand internal integer
941                          * representation, convert to string first, then create float from
942                          * string */
943                         buffer = alloca(100);
944                         /* decimal string representation because hexadecimal output is
945                          * interpreted unsigned by fc_val_from_str, so this is a HACK */
946                         snprintf(buffer, 100, "%s",
947                                 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
948                         buffer[100 - 1] = '\0';
949                         switch (get_mode_size_bits(dst_mode)) {
950                         case 32:
951                                 fc_val_from_str(buffer, 0, 8, 23, NULL);
952                                 break;
953                         case 64:
954                                 fc_val_from_str(buffer, 0, 11, 52, NULL);
955                                 break;
956                         case 80:
957                         case 96:
958                                 fc_val_from_str(buffer, 0, 15, 64, NULL);
959                                 break;
960                         default:
961                                 panic("Unsupported mode in tarval_convert_to()");
962                         }
963                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
964
965                 default:
966                         break;
967                 }
968                 break;
969
970         case irms_internal_boolean:
971                 /* beware: this is C semantic for the INTERNAL boolean mode */
972                 if (get_mode_sort(dst_mode) == irms_int_number)
973                         return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
974                 break;
975
976         case irms_reference:
977                 if (get_mode_sort(dst_mode) == irms_int_number) {
978                         buffer = alloca(sc_get_buffer_length());
979                         memcpy(buffer, src->value, sc_get_buffer_length());
980                         sign_extend(buffer, src->mode);
981                         return get_tarval_overflow(buffer, src->length, dst_mode);
982                 }
983                 break;
984         }
985
986         return tarval_bad;
987 }
988
989 /*
990  * bitwise negation
991  */
992 tarval *tarval_not(tarval *a) {
993         char *buffer;
994
995         assert(a);
996
997         /* works for vector mode without changes */
998
999         switch (get_mode_sort(a->mode)) {
1000         case irms_reference:
1001         case irms_int_number:
1002                 buffer = alloca(sc_get_buffer_length());
1003                 sc_not(a->value, buffer);
1004                 return get_tarval(buffer, a->length, a->mode);
1005
1006         case irms_internal_boolean:
1007                 if (a == tarval_b_true)
1008                         return tarval_b_false;
1009                 if (a == tarval_b_false)
1010                         return tarval_b_true;
1011                 return tarval_bad;
1012
1013         default:
1014                 assert(0 && "bitwise negation is only allowed for integer and boolean");
1015                 return tarval_bad;
1016         }
1017 }
1018
1019 /*
1020  * arithmetic negation
1021  */
1022 tarval *tarval_neg(tarval *a) {
1023         char *buffer;
1024
1025         assert(a);
1026         assert(mode_is_num(a->mode)); /* negation only for numerical values */
1027
1028         /* note: negation is allowed even for unsigned modes. */
1029
1030         if (get_mode_n_vector_elems(a->mode) > 1) {
1031                 /* vector arithmetic not implemented yet */
1032                 return tarval_bad;
1033         }
1034
1035         switch (get_mode_sort(a->mode)) {
1036         case irms_int_number:
1037                 buffer = alloca(sc_get_buffer_length());
1038                 sc_neg(a->value, buffer);
1039                 return get_tarval_overflow(buffer, a->length, a->mode);
1040
1041         case irms_float_number:
1042                 /* it should be safe to enable this even if other arithmetic is disabled */
1043                 /*if (no_float)
1044                         return tarval_bad;*/
1045
1046                 fc_neg(a->value, NULL);
1047                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1048
1049         default:
1050                 return tarval_bad;
1051         }
1052 }
1053
1054 /*
1055  * addition
1056  */
1057 tarval *tarval_add(tarval *a, tarval *b) {
1058         char *buffer;
1059
1060         assert(a);
1061         assert(b);
1062         assert(a->mode == b->mode);
1063
1064         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1065                 /* vector arithmetic not implemented yet */
1066                 return tarval_bad;
1067         }
1068
1069         switch (get_mode_sort(a->mode)) {
1070         case irms_int_number:
1071                 /* modes of a,b are equal, so result has mode of a as this might be the character */
1072                 buffer = alloca(sc_get_buffer_length());
1073                 sc_add(a->value, b->value, buffer);
1074                 return get_tarval_overflow(buffer, a->length, a->mode);
1075
1076         case irms_float_number:
1077                 if (no_float)
1078                         return tarval_bad;
1079
1080                 fc_add(a->value, b->value, NULL);
1081                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1082
1083         default:
1084                 return tarval_bad;
1085         }
1086 }
1087
1088 /*
1089  * subtraction
1090  */
1091 tarval *tarval_sub(tarval *a, tarval *b) {
1092         char *buffer;
1093
1094         assert(a);
1095         assert(b);
1096         assert(a->mode == b->mode);
1097
1098         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1099                 /* vector arithmetic not implemented yet */
1100                 return tarval_bad;
1101         }
1102         switch (get_mode_sort(a->mode)) {
1103         case irms_int_number:
1104                 /* modes of a,b are equal, so result has mode of a as this might be the character */
1105                 buffer = alloca(sc_get_buffer_length());
1106                 sc_sub(a->value, b->value, buffer);
1107                 return get_tarval_overflow(buffer, a->length, a->mode);
1108
1109         case irms_float_number:
1110                 if (no_float)
1111                         return tarval_bad;
1112
1113                 fc_sub(a->value, b->value, NULL);
1114                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1115
1116         default:
1117                 return tarval_bad;
1118         }
1119 }
1120
1121 /*
1122  * multiplication
1123  */
1124 tarval *tarval_mul(tarval *a, tarval *b) {
1125         char *buffer;
1126
1127         assert(a);
1128         assert(b);
1129         assert(a->mode == b->mode);
1130
1131         if (get_mode_n_vector_elems(a->mode) > 1) {
1132                 /* vector arithmetic not implemented yet */
1133                 return tarval_bad;
1134         }
1135
1136         switch (get_mode_sort(a->mode)) {
1137         case irms_int_number:
1138                 /* modes of a,b are equal */
1139                 buffer = alloca(sc_get_buffer_length());
1140                 sc_mul(a->value, b->value, buffer);
1141                 return get_tarval_overflow(buffer, a->length, a->mode);
1142
1143         case irms_float_number:
1144                 if (no_float)
1145                         return tarval_bad;
1146
1147                 fc_mul(a->value, b->value, NULL);
1148                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1149
1150         default:
1151                 return tarval_bad;
1152         }
1153 }
1154
1155 /*
1156  * floating point division
1157  */
1158 tarval *tarval_quo(tarval *a, tarval *b) {
1159         assert(a);
1160         assert(b);
1161         assert((a->mode == b->mode) && mode_is_float(a->mode));
1162
1163         if (no_float)
1164                 return tarval_bad;
1165
1166         if (get_mode_n_vector_elems(a->mode) > 1) {
1167                 /* vector arithmetic not implemented yet */
1168                 return tarval_bad;
1169         }
1170
1171         fc_div(a->value, b->value, NULL);
1172         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1173 }
1174
1175 /*
1176  * integer division
1177  * overflow is impossible, but look out for division by zero
1178  */
1179 tarval *tarval_div(tarval *a, tarval *b) {
1180         assert(a);
1181         assert(b);
1182         assert((a->mode == b->mode) && mode_is_int(a->mode));
1183
1184         if (get_mode_n_vector_elems(a->mode) > 1) {
1185                 /* vector arithmetic not implemented yet */
1186                 return tarval_bad;
1187         }
1188
1189         /* x/0 error */
1190         if (b == get_mode_null(b->mode)) return tarval_bad;
1191         /* modes of a,b are equal */
1192         sc_div(a->value, b->value, NULL);
1193         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1194 }
1195
1196 /*
1197  * remainder
1198  * overflow is impossible, but look out for division by zero
1199  */
1200 tarval *tarval_mod(tarval *a, tarval *b) {
1201         assert(a);
1202         assert(b);
1203         assert((a->mode == b->mode) && mode_is_int(a->mode));
1204
1205         if (get_mode_n_vector_elems(a->mode) > 1) {
1206                 /* vector arithmetic not implemented yet */
1207                 return tarval_bad;
1208         }
1209
1210         /* x/0 error */
1211         if (b == get_mode_null(b->mode)) return tarval_bad;
1212         /* modes of a,b are equal */
1213         sc_mod(a->value, b->value, NULL);
1214         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1215 }
1216
1217 /*
1218  * integer division AND remainder
1219  * overflow is impossible, but look out for division by zero
1220  */
1221 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1222         int len = sc_get_buffer_length();
1223         char *div_res = alloca(len);
1224         char *mod_res = alloca(len);
1225
1226         assert(a);
1227         assert(b);
1228         assert((a->mode == b->mode) && mode_is_int(a->mode));
1229
1230         if (get_mode_n_vector_elems(a->mode) > 1) {
1231                 /* vector arithmetic not implemented yet */
1232                 return tarval_bad;
1233         }
1234
1235
1236         /* x/0 error */
1237         if (b == get_mode_null(b->mode)) return tarval_bad;
1238         /* modes of a,b are equal */
1239         sc_divmod(a->value, b->value, div_res, mod_res);
1240         *mod = get_tarval(mod_res, len, a->mode);
1241         return get_tarval(div_res, len, a->mode);
1242 }
1243
1244 /*
1245  * absolute value
1246  */
1247 tarval *tarval_abs(tarval *a) {
1248         char *buffer;
1249
1250         assert(a);
1251         assert(mode_is_num(a->mode));
1252
1253         if (get_mode_n_vector_elems(a->mode) > 1) {
1254                 /* vector arithmetic not implemented yet */
1255                 return tarval_bad;
1256         }
1257
1258         switch (get_mode_sort(a->mode)) {
1259         case irms_int_number:
1260                 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1261                         buffer = alloca(sc_get_buffer_length());
1262                         sc_neg(a->value, buffer);
1263                         return get_tarval_overflow(buffer, a->length, a->mode);
1264                 }
1265                 return a;
1266
1267         case irms_float_number:
1268                 /* it should be safe to enable this even if other arithmetic is disabled */
1269                 /*if (no_float)
1270                         return tarval_bad;*/
1271
1272                 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1273                         fc_neg(a->value, NULL);
1274                         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1275                 }
1276                 return a;
1277
1278         default:
1279                 return tarval_bad;
1280         }
1281         return tarval_bad;
1282 }
1283
1284 /*
1285  * bitwise and
1286  */
1287 tarval *tarval_and(tarval *a, tarval *b) {
1288         assert(a);
1289         assert(b);
1290         assert(a->mode == b->mode);
1291
1292         /* works even for vector modes */
1293
1294         switch(get_mode_sort(a->mode)) {
1295         case irms_internal_boolean:
1296                 return (a == tarval_b_false) ? a : b;
1297
1298         case irms_int_number:
1299                 sc_and(a->value, b->value, NULL);
1300                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1301
1302         default:
1303                 assert(0 && "operation not defined on mode");
1304                 return tarval_bad;
1305         }
1306 }
1307
1308 /*
1309  * bitwise or
1310  */
1311 tarval *tarval_or (tarval *a, tarval *b) {
1312         assert(a);
1313         assert(b);
1314         assert(a->mode == b->mode);
1315
1316         /* works even for vector modes */
1317
1318         switch (get_mode_sort(a->mode)) {
1319         case irms_internal_boolean:
1320                 return (a == tarval_b_true) ? a : b;
1321
1322         case irms_int_number:
1323                 sc_or(a->value, b->value, NULL);
1324                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1325
1326         default:
1327                 assert(0 && "operation not defined on mode");
1328                 return tarval_bad;
1329         }
1330 }
1331
1332 /*
1333  * bitwise exclusive or (xor)
1334  */
1335 tarval *tarval_eor(tarval *a, tarval *b) {
1336         assert(a);
1337         assert(b);
1338         assert((a->mode == b->mode));
1339
1340         /* works even for vector modes */
1341
1342         switch (get_mode_sort(a->mode)) {
1343         case irms_internal_boolean:
1344                 return (a == b)? tarval_b_false : tarval_b_true;
1345
1346         case irms_int_number:
1347                 sc_xor(a->value, b->value, NULL);
1348                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1349
1350         default:
1351                 assert(0 && "operation not defined on mode");
1352                 return tarval_bad;;
1353         }
1354 }
1355
1356 /*
1357  * bitwise left shift
1358  */
1359 tarval *tarval_shl(tarval *a, tarval *b) {
1360         char *temp_val = NULL;
1361
1362         assert(a);
1363         assert(b);
1364         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1365
1366         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1367                 /* vector arithmetic not implemented yet */
1368                 return tarval_bad;
1369         }
1370
1371         if (get_mode_modulo_shift(a->mode) != 0) {
1372                 temp_val = alloca(sc_get_buffer_length());
1373
1374                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1375                 sc_mod(b->value, temp_val, temp_val);
1376         } else
1377                 temp_val = (char*)b->value;
1378
1379         sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1380         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1381 }
1382
1383 /*
1384  * bitwise unsigned right shift
1385  */
1386 tarval *tarval_shr(tarval *a, tarval *b) {
1387         char *temp_val = NULL;
1388
1389         assert(a);
1390         assert(b);
1391         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1392
1393         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1394                 /* vector arithmetic not implemented yet */
1395                 return tarval_bad;
1396         }
1397
1398         if (get_mode_modulo_shift(a->mode) != 0) {
1399                 temp_val = alloca(sc_get_buffer_length());
1400
1401                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1402                 sc_mod(b->value, temp_val, temp_val);
1403         } else
1404                 temp_val = (char*)b->value;
1405
1406         sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1407         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1408 }
1409
1410 /*
1411  * bitwise signed right shift
1412  */
1413 tarval *tarval_shrs(tarval *a, tarval *b) {
1414         char *temp_val = NULL;
1415
1416         assert(a);
1417         assert(b);
1418         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1419
1420         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1421                 /* vector arithmetic not implemented yet */
1422                 return tarval_bad;
1423         }
1424
1425         if (get_mode_modulo_shift(a->mode) != 0) {
1426                 temp_val = alloca(sc_get_buffer_length());
1427
1428                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1429                 sc_mod(b->value, temp_val, temp_val);
1430         } else
1431                 temp_val = (char*)b->value;
1432
1433         sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1434         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1435 }
1436
1437 /*
1438  * bitwise rotation
1439  */
1440 tarval *tarval_rot(tarval *a, tarval *b) {
1441         char *temp_val = NULL;
1442
1443         assert(a);
1444         assert(b);
1445         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1446
1447         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1448                 /* vector arithmetic not implemented yet */
1449                 return tarval_bad;
1450         }
1451
1452         if (get_mode_modulo_shift(a->mode) != 0) {
1453                 temp_val = alloca(sc_get_buffer_length());
1454
1455                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1456                 sc_mod(b->value, temp_val, temp_val);
1457         } else
1458                 temp_val = (char*)b->value;
1459
1460         sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1461         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1462 }
1463
1464 /*
1465  * carry flag of the last operation
1466  */
1467 int tarval_carry(void) {
1468         return sc_had_carry();
1469 }
1470
1471 /*
1472  * Output of tarvals
1473  */
1474 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1475         static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1476
1477         const char *str;
1478         char tv_buf[100];
1479         const tarval_mode_info *mode_info;
1480         const char *prefix, *suffix;
1481
1482         mode_info = tv->mode->tv_priv;
1483         if (! mode_info)
1484                 mode_info = &default_info;
1485         prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1486         suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1487
1488         switch (get_mode_sort(tv->mode)) {
1489         case irms_reference:
1490                 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1491                 /* fall through */
1492         case irms_int_number:
1493                 switch (mode_info->mode_output) {
1494
1495                 case TVO_DECIMAL:
1496                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1497                         break;
1498
1499                 case TVO_OCTAL:
1500                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1501                         break;
1502
1503                 case TVO_HEX:
1504                 case TVO_NATIVE:
1505                 default:
1506                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1507                         break;
1508                 }
1509                 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1510
1511         case irms_float_number:
1512                 switch (mode_info->mode_output) {
1513                 case TVO_HEX:
1514                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1515
1516                 case TVO_HEXFLOAT:
1517                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1518
1519                 case TVO_FLOAT:
1520                 case TVO_NATIVE:
1521                 default:
1522                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1523                 }
1524                 break;
1525
1526         case irms_internal_boolean:
1527                 switch (mode_info->mode_output) {
1528
1529                 case TVO_DECIMAL:
1530                 case TVO_OCTAL:
1531                 case TVO_HEX:
1532                 case TVO_BINARY:
1533                         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1534
1535                 case TVO_NATIVE:
1536                 default:
1537                         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1538                 }
1539
1540         case irms_control_flow:
1541         case irms_memory:
1542         case irms_auxiliary:
1543                 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1544         }
1545
1546         return 0;
1547 }
1548
1549 /**
1550  * Output of tarvals to stdio.
1551  */
1552 int tarval_printf(tarval *tv) {
1553         char buf[1024];
1554         int res;
1555
1556         res = tarval_snprintf(buf, sizeof(buf), tv);
1557         assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1558         printf(buf);
1559         return res;
1560 }
1561
1562 char *get_tarval_bitpattern(tarval *tv) {
1563         int i, j, pos = 0;
1564         int n = get_mode_size_bits(tv->mode);
1565         int bytes = (n + 7) / 8;
1566         char *res = xmalloc((n + 1) * sizeof(char));
1567         unsigned char byte;
1568
1569         for(i = 0; i < bytes; i++) {
1570                 byte = get_tarval_sub_bits(tv, i);
1571                 for(j = 1; j < 256; j <<= 1)
1572                         if(pos < n)
1573                                 res[pos++] = j & byte ? '1' : '0';
1574         }
1575
1576         res[n] = '\0';
1577
1578         return res;
1579 }
1580
1581 /*
1582  * access to the bitpattern
1583  */
1584 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1585         switch (get_mode_arithmetic(tv->mode)) {
1586         case irma_twos_complement:
1587                 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1588         case irma_ieee754:
1589                 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1590         default:
1591                 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1592         }
1593 }
1594
1595 /*
1596  * Specify the output options of one mode.
1597  *
1598  * This functions stores the modinfo, so DO NOT DESTROY it.
1599  *
1600  * Returns zero on success.
1601  */
1602 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1603         assert(mode);
1604
1605         mode->tv_priv = modeinfo;
1606         return 0;
1607 }
1608
1609 /*
1610  * Returns the output options of one mode.
1611  *
1612  * This functions returns the mode info of a given mode.
1613  */
1614 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1615         assert(mode);
1616
1617         return mode->tv_priv;
1618 }
1619
1620 /*
1621  * Returns non-zero if a given (integer) tarval has only one single bit
1622  * set.
1623  */
1624 int tarval_is_single_bit(tarval *tv) {
1625         int i, l;
1626         int bits;
1627
1628         if (!tv || tv == tarval_bad) return 0;
1629         if (! mode_is_int(tv->mode)) return 0;
1630
1631         l = get_mode_size_bytes(tv->mode);
1632         for (bits = 0, i = l - 1; i >= 0; --i) {
1633                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1634
1635                 /* check for more than one bit in these */
1636                 if (v) {
1637                         if (v & (v-1))
1638                                 return 0;
1639                         if (++bits > 1)
1640                                 return 0;
1641                 }
1642         }
1643         return bits;
1644 }
1645
1646 /*
1647  * Returns non-zero if the mantissa of a floating point IEEE-754
1648  * tarval is zero (i.e. 1.0Exxx)
1649  */
1650 int tarval_ieee754_zero_mantissa(tarval *tv) {
1651         assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1652         return fc_zero_mantissa(tv->value);
1653 }
1654
1655 /* Returns the exponent of a floating point IEEE-754 tarval. */
1656 int tarval_ieee754_get_exponent(tarval *tv) {
1657         assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1658         return fc_get_exponent(tv->value);
1659 }
1660
1661 /* Set the immediate precision for IEEE-754 results. */
1662 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1663         return fc_set_immediate_precision(bits);
1664 }
1665
1666 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1667 unsigned tarval_ieee754_get_exact(void) {
1668         return fc_is_exact();
1669 }
1670
1671 /* check if its the a floating point NaN */
1672 int tarval_is_NaN(tarval *tv) {
1673         if (! mode_is_float(tv->mode))
1674                 return 0;
1675         return fc_is_nan(tv->value);
1676 }
1677
1678 /* check if its the a floating point +inf */
1679 int tarval_is_plus_inf(tarval *tv) {
1680         if (! mode_is_float(tv->mode))
1681                 return 0;
1682         return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1683 }
1684
1685 /* check if its the a floating point -inf */
1686 int tarval_is_minus_inf(tarval *tv) {
1687         if (! mode_is_float(tv->mode))
1688                 return 0;
1689         return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1690 }
1691
1692 /* check if the tarval represents a finite value */
1693 int tarval_is_finite(tarval *tv) {
1694         if (mode_is_float(tv->mode))
1695                 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1696         return 1;
1697 }
1698
1699 /*
1700  * Sets the overflow mode for integer operations.
1701  */
1702 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1703         int_overflow_mode = ov_mode;
1704 }
1705
1706 /* Get the overflow mode for integer operations. */
1707 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1708         return int_overflow_mode;
1709 }
1710
1711 /* Enable/Disable floating point constant folding. */
1712 int tarval_enable_fp_ops(int enable) {
1713         int old = !no_float;
1714
1715         no_float = !enable;
1716         return old;
1717 }
1718
1719 /**
1720  * default mode_info for output as HEX
1721  */
1722 static const tarval_mode_info hex_output = {
1723         TVO_HEX,
1724         "0x",
1725         NULL,
1726 };
1727
1728 /*
1729  * Initialization of the tarval module: called before init_mode()
1730  */
1731 void init_tarval_1(long null_value) {
1732         _null_value = null_value;
1733
1734         /* initialize the sets holding the tarvals with a comparison function and
1735          * an initial size, which is the expected number of constants */
1736         tarvals = new_set(memcmp, N_CONSTANTS);
1737         values  = new_set(memcmp, N_CONSTANTS);
1738         /* init strcalc with precision of 68 to support floating point values with 64
1739          * bit mantissa (needs extra bits for rounding and overflow) */
1740         init_strcalc(68);
1741         init_fltcalc(0);
1742 }
1743
1744 /*
1745  * Initialization of the tarval module: called after init_mode()
1746  */
1747 void init_tarval_2(void) {
1748         tarval_bad->kind        = k_tarval;
1749         tarval_bad->mode        = mode_BAD;
1750         tarval_bad->value       = INT_TO_PTR(resid_tarval_bad);
1751
1752         tarval_undefined->kind  = k_tarval;
1753         tarval_undefined->mode  = mode_ANY;
1754         tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1755
1756         tarval_b_true->kind     = k_tarval;
1757         tarval_b_true->mode     = mode_b;
1758         tarval_b_true->value    = INT_TO_PTR(resid_tarval_b_true);
1759
1760         tarval_b_false->kind    = k_tarval;
1761         tarval_b_false->mode    = mode_b;
1762         tarval_b_false->value   = INT_TO_PTR(resid_tarval_b_false);
1763
1764         /*
1765          * assign output modes that are compatible with the
1766          * old implementation: Hex output
1767          */
1768         set_tarval_mode_output_option(mode_Bs, &hex_output);
1769         set_tarval_mode_output_option(mode_Bu, &hex_output);
1770         set_tarval_mode_output_option(mode_Hs, &hex_output);
1771         set_tarval_mode_output_option(mode_Hu, &hex_output);
1772         set_tarval_mode_output_option(mode_Is, &hex_output);
1773         set_tarval_mode_output_option(mode_Iu, &hex_output);
1774         set_tarval_mode_output_option(mode_Ls, &hex_output);
1775         set_tarval_mode_output_option(mode_Lu, &hex_output);
1776         set_tarval_mode_output_option(mode_P,  &hex_output);
1777 }
1778
1779 /* free all memory occupied by tarval. */
1780 void finish_tarval(void) {
1781         finish_strcalc();
1782         finish_fltcalc();
1783         del_set(tarvals); tarvals = NULL;
1784         del_set(values);  values = NULL;
1785 }
1786
1787 /****************************************************************************
1788  *   end of tv.c
1789  ****************************************************************************/