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