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