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