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