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