9f4b02dffe790991f15855f606267c4c1fbf3290
[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  * test if one, 1 means 'yes'
724  */
725 int tarval_is_one(tarval *a)
726 {
727   ir_mode *m = get_tarval_mode(a);
728
729   return a == get_tarval_one(m);
730 }
731
732 /*
733  * comparison
734  */
735 pnc_number tarval_cmp(tarval *a, tarval *b)
736 {
737   ANNOUNCE();
738   assert(a);
739   assert(b);
740
741   if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
742   if (a == tarval_undefined || b == tarval_undefined) return False;
743   if (a == b) return Eq;
744   if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
745
746   /* Here the two tarvals are unequal and of the same mode */
747   switch (get_mode_sort(a->mode))
748   {
749     case irms_control_flow:
750     case irms_memory:
751     case irms_auxiliary:
752     case irms_reference:
753       return False;
754
755     case irms_float_number:
756       switch (fc_comp(a->value, b->value)) {
757         case -1: return Lt;
758         case  0: assert(0 && "different tarvals compare equal"); return Eq;
759         case  1: return Gt;
760         case  2: return Uo;
761         default: return False;
762       }
763     case irms_int_number:
764     case irms_character:
765       return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
766
767     case irms_internal_boolean:
768       return (a == tarval_b_true)?(Gt):(Lt);
769   }
770   return False;
771 }
772
773 /*
774  * convert to other mode
775  */
776 tarval *tarval_convert_to(tarval *src, ir_mode *m)
777 {
778   char *buffer;
779
780   ANNOUNCE();
781   assert(src);
782   assert(m);
783
784   if (src->mode == m) return src;
785
786   switch (get_mode_sort(src->mode))
787   {
788     case irms_control_flow:
789     case irms_memory:
790     case irms_auxiliary:
791       break;
792
793     /* cast float to something */
794     case irms_float_number:
795       switch (get_mode_sort(m)) {
796         case irms_float_number:
797           switch (get_mode_size_bits(m))
798           {
799             case 32:
800               fc_cast(src->value, 8, 23, NULL);
801               break;
802             case 64:
803               fc_cast(src->value, 11, 52, NULL);
804               break;
805             case 80:
806               fc_cast(src->value, 15, 64, NULL);
807               break;
808             default:
809               break;
810           }
811           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
812           break;
813
814         case irms_int_number:
815           switch (GET_FLOAT_TO_INT_MODE())
816           {
817             case TRUNCATE:
818               fc_int(src->value, NULL);
819               break;
820             case ROUND:
821               fc_rnd(src->value, NULL);
822               break;
823             default:
824               break;
825           }
826           /* XXX floating point unit can't produce a value in integer
827            * representation
828            * an intermediate representation is needed here first. */
829           /*  return get_tarval(); */
830           return tarval_bad;
831           break;
832
833         default:
834           /* the rest can't be converted */
835           return tarval_bad;
836       }
837       break;
838
839     /* cast int to something */
840     case irms_int_number:
841       switch (get_mode_sort(m)) {
842         case irms_int_number:
843         case irms_character:
844           return get_tarval_overflow(src->value, src->length, m);
845
846         case irms_internal_boolean:
847           /* XXX C semantics */
848           if (src == get_mode_null(src->mode)) return tarval_b_false;
849           else return tarval_b_true;
850
851         case irms_float_number:
852           /* XXX floating point unit does not understand internal integer
853            * representation, convert to string first, then create float from
854            * string */
855           buffer = alloca(100);
856           /* decimal string representation because hexadecimal output is
857            * interpreted unsigned by fc_val_from_str, so this is a HACK */
858           snprintf(buffer, 100, "%s",
859                    sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
860           switch (get_mode_size_bits(m))
861           {
862             case 32:
863               fc_val_from_str(buffer, 0, 8, 23, NULL);
864               break;
865             case 64:
866               fc_val_from_str(buffer, 0, 11, 52, NULL);
867               break;
868             case 80:
869               fc_val_from_str(buffer, 0, 15, 64, NULL);
870               break;
871           }
872           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
873
874         default:
875           break;
876       }
877       break;
878
879     case irms_internal_boolean:
880       switch (get_mode_sort(m))
881       {
882         case irms_int_number:
883           if (src == tarval_b_true) return get_mode_one(m);
884           else return get_mode_null(m);
885
886         default:
887           break;
888       }
889       break;
890
891     case irms_character:
892       break;
893     case irms_reference:
894       break;
895   }
896
897   return tarval_bad;
898 }
899
900 /*
901  * bitwise negation
902  */
903 tarval *tarval_not(tarval *a)
904 {
905   char *buffer;
906
907   ANNOUNCE();
908   assert(a);
909   assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
910
911   switch (get_mode_sort(a->mode))
912   {
913     case irms_int_number:
914       buffer = alloca(sc_get_buffer_length());
915       sc_not(a->value, buffer);
916       return get_tarval(buffer, a->length, a->mode);
917
918     default:
919       return tarval_bad;
920   }
921 }
922
923 /*
924  * arithmetic negation
925  */
926 tarval *tarval_neg(tarval *a)
927 {
928   char *buffer;
929
930   ANNOUNCE();
931   assert(a);
932   assert(mode_is_num(a->mode)); /* negation only for numerical values */
933   assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
934
935   switch (get_mode_sort(a->mode))
936   {
937     case irms_int_number:
938       buffer = alloca(sc_get_buffer_length());
939       sc_neg(a->value, buffer);
940       return get_tarval_overflow(buffer, a->length, a->mode);
941
942     case irms_float_number:
943       fc_neg(a->value, NULL);
944       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
945
946     default:
947       return tarval_bad;
948   }
949 }
950
951 /*
952  * addition
953  */
954 tarval *tarval_add(tarval *a, tarval *b)
955 {
956   char *buffer;
957
958   ANNOUNCE();
959   assert(a);
960   assert(b);
961   assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
962
963   switch (get_mode_sort(a->mode))
964   {
965     case irms_character:
966     case irms_int_number:
967       /* modes of a,b are equal, so result has mode of a as this might be the character */
968       buffer = alloca(sc_get_buffer_length());
969       sc_add(a->value, b->value, buffer);
970       return get_tarval_overflow(buffer, a->length, a->mode);
971
972     case irms_float_number:
973       fc_add(a->value, b->value, NULL);
974       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
975
976     default:
977       return tarval_bad;
978   }
979 }
980
981 /*
982  * subtraction
983  */
984 tarval *tarval_sub(tarval *a, tarval *b)
985 {
986   char *buffer;
987
988   ANNOUNCE();
989   assert(a);
990   assert(b);
991   assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
992
993   switch (get_mode_sort(a->mode))
994   {
995     case irms_character:
996     case irms_int_number:
997       /* modes of a,b are equal, so result has mode of a as this might be the character */
998       buffer = alloca(sc_get_buffer_length());
999       sc_sub(a->value, b->value, buffer);
1000       return get_tarval_overflow(buffer, a->length, a->mode);
1001
1002     case irms_float_number:
1003       fc_sub(a->value, b->value, NULL);
1004       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1005
1006     default:
1007       return tarval_bad;
1008   }
1009 }
1010
1011 /*
1012  * multiplication
1013  */
1014 tarval *tarval_mul(tarval *a, tarval *b)
1015 {
1016   char *buffer;
1017
1018   ANNOUNCE();
1019   assert(a);
1020   assert(b);
1021   assert((a->mode == b->mode) && mode_is_num(a->mode));
1022
1023   switch (get_mode_sort(a->mode))
1024   {
1025     case irms_int_number:
1026       /* modes of a,b are equal */
1027       buffer = alloca(sc_get_buffer_length());
1028       sc_mul(a->value, b->value, buffer);
1029       return get_tarval_overflow(buffer, a->length, a->mode);
1030
1031     case irms_float_number:
1032       fc_mul(a->value, b->value, NULL);
1033       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1034
1035     default:
1036       return tarval_bad;
1037   }
1038 }
1039
1040 /*
1041  * floating point division
1042  */
1043 tarval *tarval_quo(tarval *a, tarval *b)
1044 {
1045   ANNOUNCE();
1046   assert(a);
1047   assert(b);
1048   assert((a->mode == b->mode) && mode_is_float(a->mode));
1049
1050   fc_div(a->value, b->value, NULL);
1051   return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1052 }
1053
1054 /*
1055  * integer division
1056  * overflow is impossible, but look out for division by zero
1057  */
1058 tarval *tarval_div(tarval *a, tarval *b)
1059 {
1060   ANNOUNCE();
1061   assert(a);
1062   assert(b);
1063   assert((a->mode == b->mode) && mode_is_int(a->mode));
1064
1065   /* x/0 error */
1066   if (b == get_mode_null(b->mode)) return tarval_bad;
1067   /* modes of a,b are equal */
1068   sc_div(a->value, b->value, NULL);
1069   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1070 }
1071
1072 /*
1073  * remainder
1074  * overflow is impossible, but look out for division by zero
1075  */
1076 tarval *tarval_mod(tarval *a, tarval *b)
1077 {
1078   ANNOUNCE();
1079   assert(a);
1080   assert(b);
1081   assert((a->mode == b->mode) && mode_is_int(a->mode));
1082
1083   /* x/0 error */
1084   if (b == get_mode_null(b->mode)) return tarval_bad;
1085   /* modes of a,b are equal */
1086   sc_mod(a->value, b->value, NULL);
1087   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1088 }
1089
1090 /*
1091  * absolute value
1092  */
1093 tarval *tarval_abs(tarval *a)
1094 {
1095   char *buffer;
1096
1097   ANNOUNCE();
1098   assert(a);
1099   assert(mode_is_num(a->mode));
1100
1101   switch (get_mode_sort(a->mode))
1102   {
1103     case irms_int_number:
1104       if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1105       {
1106         buffer = alloca(sc_get_buffer_length());
1107         sc_neg(a->value, buffer);
1108         return get_tarval_overflow(buffer, a->length, a->mode);
1109       }
1110       return a;
1111
1112     case irms_float_number:
1113       if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1114       {
1115         fc_neg(a->value, NULL);
1116         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1117       }
1118       return a;
1119
1120     default:
1121       return tarval_bad;
1122   }
1123   return tarval_bad;
1124 }
1125
1126 /*
1127  * bitwise and
1128  */
1129 tarval *tarval_and(tarval *a, tarval *b)
1130 {
1131   ANNOUNCE();
1132   assert(a);
1133   assert(b);
1134   assert(a->mode == b->mode);
1135
1136   switch(get_mode_sort(a->mode))
1137   {
1138     case irms_internal_boolean:
1139       return (a == tarval_b_false) ? a : b;
1140
1141     case irms_int_number:
1142       sc_and(a->value, b->value, NULL);
1143       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1144
1145     default:
1146       assert(0 && "operation not defined on mode");
1147       return tarval_bad;
1148   }
1149 }
1150
1151 /*
1152  * bitwise or
1153  */
1154 tarval *tarval_or (tarval *a, tarval *b)
1155 {
1156   ANNOUNCE();
1157   assert(a);
1158   assert(b);
1159   assert(a->mode == b->mode);
1160
1161   switch (get_mode_sort(a->mode))
1162   {
1163     case irms_internal_boolean:
1164       return (a == tarval_b_true) ? a : b;
1165
1166     case irms_int_number:
1167       sc_or(a->value, b->value, NULL);
1168       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1169
1170     default:
1171       assert(0 && "operation not defined on mode");
1172       return tarval_bad;;
1173   }
1174 }
1175
1176 /*
1177  * bitwise exclusive or (xor)
1178  */
1179 tarval *tarval_eor(tarval *a, tarval *b)
1180 {
1181   ANNOUNCE();
1182   assert(a);
1183   assert(b);
1184   assert((a->mode == b->mode));
1185
1186   switch (get_mode_sort(a->mode))
1187   {
1188     case irms_internal_boolean:
1189       return (a == b)? tarval_b_false : tarval_b_true;
1190
1191     case irms_int_number:
1192       sc_xor(a->value, b->value, NULL);
1193       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1194
1195     default:
1196       assert(0 && "operation not defined on mode");
1197       return tarval_bad;;
1198   }
1199 }
1200
1201 /*
1202  * bitwise left shift
1203  */
1204 tarval *tarval_shl(tarval *a, tarval *b)
1205 {
1206   char *temp_val = NULL;
1207   ANNOUNCE();
1208   assert(a);
1209   assert(b);
1210   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1211
1212   if (get_mode_modulo_shift(a->mode) != 0)
1213   {
1214     temp_val = alloca(sc_get_buffer_length());
1215
1216     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1217     sc_mod(b->value, temp_val, temp_val);
1218   }
1219   else
1220     temp_val = (char*)b->value;
1221
1222   sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1223   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1224 }
1225
1226 /*
1227  * bitwise unsigned right shift
1228  */
1229 tarval *tarval_shr(tarval *a, tarval *b)
1230 {
1231   char *temp_val = NULL;
1232   ANNOUNCE();
1233   assert(a);
1234   assert(b);
1235   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1236
1237   if (get_mode_modulo_shift(a->mode) != 0)
1238   {
1239     temp_val = alloca(sc_get_buffer_length());
1240
1241     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1242     sc_mod(b->value, temp_val, temp_val);
1243   }
1244   else
1245     temp_val = (char*)b->value;
1246
1247   sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1248   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1249 }
1250
1251 /*
1252  * bitwise signed right shift
1253  */
1254 tarval *tarval_shrs(tarval *a, tarval *b)
1255 {
1256   char *temp_val = NULL;
1257   ANNOUNCE();
1258   assert(a);
1259   assert(b);
1260   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1261
1262   if (get_mode_modulo_shift(a->mode) != 0)
1263   {
1264     temp_val = alloca(sc_get_buffer_length());
1265
1266     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1267     sc_mod(b->value, temp_val, temp_val);
1268   }
1269   else
1270     temp_val = (char*)b->value;
1271
1272   sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1273   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1274 }
1275
1276 /*
1277  * bitwise rotation
1278  */
1279 tarval *tarval_rot(tarval *a, tarval *b)
1280 {
1281   char *temp_val = NULL;
1282   ANNOUNCE();
1283   assert(a);
1284   assert(b);
1285   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1286
1287   if (get_mode_modulo_shift(a->mode) != 0)
1288   {
1289     temp_val = alloca(sc_get_buffer_length());
1290
1291     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1292     sc_mod(b->value, temp_val, temp_val);
1293   }
1294   else
1295     temp_val = (char*)b->value;
1296
1297   sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1298   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1299 }
1300
1301
1302 /*
1303  * Output of tarvals
1304  */
1305 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1306 {
1307   static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1308
1309   const char *str;
1310   char tv_buf[100];
1311   const tarval_mode_info *mode_info;
1312   const char *prefix, *suffix;
1313
1314   ANNOUNCE();
1315
1316   mode_info = tv->mode->tv_priv;
1317   if (! mode_info)
1318     mode_info = &default_info;
1319   prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1320   suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1321
1322   switch (get_mode_sort(tv->mode))
1323   {
1324     case irms_int_number:
1325     case irms_character:
1326       switch (mode_info->mode_output) {
1327
1328       case TVO_DECIMAL:
1329         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1330         break;
1331
1332       case TVO_OCTAL:
1333         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1334         break;
1335
1336       case TVO_HEX:
1337       case TVO_NATIVE:
1338       default:
1339         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1340         break;
1341       }
1342       return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1343
1344     case irms_float_number:
1345       switch (mode_info->mode_output) {
1346         case TVO_HEX:
1347           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1348
1349         case TVO_HEXFLOAT:
1350           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1351
1352         case TVO_FLOAT:
1353         case TVO_NATIVE:
1354         default:
1355           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1356       }
1357       break;
1358
1359     case irms_reference:
1360       if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1361       if (tv->value != NULL)
1362         if (tarval_is_entity(tv)) {
1363           if (get_entity_peculiarity((entity *)tv->value) != peculiarity_description)
1364             return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1365           else {
1366             if (mode_info->mode_output == TVO_NATIVE)
1367               return snprintf(buf, len, "NULL");
1368             else
1369               return snprintf(buf, len, "0");
1370           }
1371         }
1372         else {
1373           if (size > tv->length) {
1374             memcpy(buf, tv->value, tv->length);
1375             buf[tv->length] = '\0';
1376           }
1377           else {
1378             /* truncated */
1379             memcpy(buf, tv->value, size-1);
1380             buf[size-1] = '\0';
1381           }
1382           return tv->length;
1383         }
1384       else
1385         return snprintf(buf, len, "void");
1386
1387     case irms_internal_boolean:
1388       switch (mode_info->mode_output) {
1389
1390       case TVO_DECIMAL:
1391       case TVO_OCTAL:
1392       case TVO_HEX:
1393       case TVO_BINARY:
1394         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1395
1396       case TVO_NATIVE:
1397       default:
1398         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1399       }
1400
1401     case irms_control_flow:
1402     case irms_memory:
1403     case irms_auxiliary:
1404       return snprintf(buf, len, "<BAD>");
1405   }
1406
1407   return 0;
1408 }
1409
1410
1411 /**
1412  * Output of tarvals to stdio.
1413  */
1414 int tarval_printf(tarval *tv) {
1415   char buf[1024];
1416   int res;
1417
1418   res = tarval_snprintf(buf, sizeof(buf), tv);
1419   assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1420   printf(buf);
1421   return res;
1422 }
1423
1424
1425 char *tarval_bitpattern(tarval *tv)
1426 {
1427   return NULL;
1428 }
1429
1430 /*
1431  * access to the bitpattern
1432  */
1433 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1434 {
1435   switch (get_mode_sort(tv->mode)) {
1436     case irms_int_number:
1437     case irms_character:
1438       return sc_sub_bits(tv->value, tv->length, byte_ofs);
1439
1440     case irms_float_number:
1441       return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1442
1443     default:
1444       return 0;
1445   }
1446 }
1447
1448 /*
1449  * Specify the output options of one mode.
1450  *
1451  * This functions stores the modinfo, so DO NOT DESTROY it.
1452  *
1453  * Returns zero on success.
1454  */
1455 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1456 {
1457   assert(mode);
1458
1459   mode->tv_priv = modeinfo;
1460   return 0;
1461 }
1462
1463 /*
1464  * Returns the output options of one mode.
1465  *
1466  * This functions returns the modinfo of a given mode.
1467  */
1468 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1469 {
1470   assert(mode);
1471
1472   return mode->tv_priv;
1473 }
1474
1475 /*
1476  * Identifying tarvals values for algebraic simplifications.
1477  *
1478  * Returns:
1479  *   - TV_CLASSIFY_NULL    for additive neutral,
1480  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
1481  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1482  *   - TV_CLASSIFY_OTHER   else
1483  */
1484 tarval_classification_t tarval_classify(tarval *tv)
1485 {
1486   ANNOUNCE();
1487   if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1488
1489   if (tv == get_mode_null(tv->mode))
1490     return TV_CLASSIFY_NULL;
1491   else if (tv == get_mode_one(tv->mode))
1492     return TV_CLASSIFY_ONE;
1493   else if ((get_mode_sort(tv->mode) == irms_int_number)
1494            && (tv == new_tarval_from_long(-1, tv->mode)))
1495     return TV_CLASSIFY_ALL_ONE;
1496
1497   return TV_CLASSIFY_OTHER;
1498 }
1499
1500 /**
1501  * default mode_info for output as HEX
1502  */
1503 static const tarval_mode_info hex_output = {
1504   TVO_HEX,
1505   "0x",
1506   NULL,
1507 };
1508
1509 /**
1510  * default mode_info for output as reference
1511  */
1512 static const tarval_mode_info reference_output = {
1513   TVO_NATIVE,
1514   "&(",
1515   ")",
1516 };
1517
1518
1519 /*
1520  * Initialization of the tarval module: called before init_mode()
1521  */
1522 void init_tarval_1(void)
1523 {
1524   ANNOUNCE();
1525   /* initialize the sets holding the tarvals with a comparison function and
1526    * an initial size, which is the expected number of constants */
1527   tarvals = new_set(memcmp, N_CONSTANTS);
1528   values  = new_set(memcmp, N_CONSTANTS);
1529   /* init strcalc with precision of 68 to support floating point values with 64
1530    * bit mantissa (needs extra bits for rounding and overflow) */
1531   init_strcalc(68);
1532   init_fltcalc(0);
1533 }
1534
1535 /*
1536  * Initialization of the tarval module: called after init_mode()
1537  */
1538 void init_tarval_2(void)
1539 {
1540   ANNOUNCE();
1541
1542   tarval_bad->mode       = mode_BAD;
1543   tarval_undefined->mode = mode_ANY;
1544   tarval_b_true->mode    = mode_b;
1545   tarval_b_false->mode   = mode_b;
1546   tarval_P_void->mode    = mode_P;
1547
1548   /*
1549    * assign output modes that are compatible with the
1550    * old implementation: Hex output
1551    */
1552   tarval_set_mode_output_option(mode_U,  &hex_output);
1553   tarval_set_mode_output_option(mode_C,  &hex_output);
1554   tarval_set_mode_output_option(mode_Bs, &hex_output);
1555   tarval_set_mode_output_option(mode_Bu, &hex_output);
1556   tarval_set_mode_output_option(mode_Hs, &hex_output);
1557   tarval_set_mode_output_option(mode_Hu, &hex_output);
1558   tarval_set_mode_output_option(mode_Is, &hex_output);
1559   tarval_set_mode_output_option(mode_Iu, &hex_output);
1560   tarval_set_mode_output_option(mode_Ls, &hex_output);
1561   tarval_set_mode_output_option(mode_Lu, &hex_output);
1562   tarval_set_mode_output_option(mode_P,  &reference_output);
1563 }
1564
1565 /****************************************************************************
1566  *   end of tv.c
1567  ****************************************************************************/