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