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