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