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