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