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