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