added a resid to the reserved tarvals to allow the firmEvaluator to detect this ones
[libfirm] / ir / tv / tv.c
index eb4fc0e..6c50d1d 100644 (file)
@@ -34,7 +34,9 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>        /* strings.h also includes bsd only function strcasecmp */
 #endif
-#include <stdlib.h>
+#ifdef HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
 #ifdef HAVE_ALLOCA_H
 # include <alloca.h>
 #endif
 
 #include "tv_t.h"
 #include "set.h"            /* to store tarvals in */
-/* #include "tune.h" */          /* some constants */
 #include "entity_t.h"       /* needed to store pointers to entities */
 #include "irmode_t.h"
 #include "irnode.h"         /* defines boolean return values (pnc_number)*/
 #include "strcalc.h"
 #include "fltcalc.h"
+#include "irtools.h"
+#include "xmalloc.h"
+#include "firm_common.h"
 
 /** Size of hash tables.  Should correspond to average number of distinct constant
     target values */
 #define GET_OVERFLOW_MODE() int_overflow_mode
 
 /* unused, float to int doesn't work yet */
-#define TRUNCATE 1
-#define ROUND 2
+enum float_to_int_mode {
+  TRUNCATE,
+  ROUND
+};
+
 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
 
 #define SWITCH_NOINFINITY 0
@@ -132,7 +139,7 @@ INLINE static void tarval_verify(tarval *tv)
 
 static int hash_tv(tarval *tv)
 {
-  return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
+  return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
 }
 
 static int hash_val(const void *value, unsigned int length)
@@ -150,12 +157,13 @@ static int hash_val(const void *value, unsigned int length)
   return hash;
 }
 
-/* finds tarval with value/mode or creates new tarval */
+/** finds tarval with value/mode or creates new tarval */
 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
 {
   tarval tv;
 
-  tv.mode = mode;
+  tv.kind   = k_tarval;
+  tv.mode   = mode;
   tv.length = length;
   if (length > 0) {
     /* if there already is such a value, it is returned, else value
@@ -184,13 +192,8 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
           case TV_OVERFLOW_WRAP:
             {
               char *temp = alloca(sc_get_buffer_length());
-              char *diff = alloca(sc_get_buffer_length());
-              sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
-              sc_val_from_ulong(1, temp);
-              sc_add(diff, temp, diff);
-              sc_sub(value, diff, temp);
-              while (sc_comp(temp, get_mode_max(mode)->value) == 1)
-                sc_sub(temp, diff, temp);
+              sc_val_from_ulong(-1, temp);
+              sc_and(temp, value, temp);
               return get_tarval(temp, length, mode);
             }
           case TV_OVERFLOW_BAD:
@@ -206,13 +209,8 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
           case TV_OVERFLOW_WRAP:
             {
               char *temp = alloca(sc_get_buffer_length());
-              char *diff = alloca(sc_get_buffer_length());
-              sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
-              sc_val_from_ulong(1, temp);
-              sc_add(diff, temp, diff);
-              sc_add(value, diff, temp);
-              while (sc_comp(temp, get_mode_max(mode)->value) == 1)
-                sc_add(temp, diff, temp);
+              sc_val_from_ulong(-1, temp);
+              sc_and(temp, value, temp);
               return get_tarval(temp, length, mode);
             }
           case TV_OVERFLOW_BAD:
@@ -234,23 +232,22 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
         return get_mode_null(mode);
       }
       break;
+
     default:
       break;
   }
   return get_tarval(value, length, mode);
 }
 
-
 /*
  *   public variables declared in tv.h
  */
-static tarval reserved_tv[5];
+static tarval reserved_tv[4];
 
 tarval *tarval_bad       = &reserved_tv[0];
 tarval *tarval_undefined = &reserved_tv[1];
 tarval *tarval_b_false   = &reserved_tv[2];
 tarval *tarval_b_true    = &reserved_tv[3];
-tarval *tarval_P_void    = &reserved_tv[4];
 
 /*
  *   public functions declared in tv.h
@@ -296,13 +293,12 @@ tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
       }
       return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
 
+    case irms_reference:
+      /* same as integer modes */
     case irms_int_number:
     case irms_character:
-      sc_val_from_str(str, len, NULL);
+      sc_val_from_str(str, len, NULL, mode);
       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
-
-    case irms_reference:
-      return get_tarval(str, len, mode);
   }
 
   assert(0);  /* can't be reached, can it? */
@@ -315,7 +311,7 @@ tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
 tarval *new_tarval_from_long(long l, ir_mode *mode)
 {
   ANNOUNCE();
-  assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
+  assert(mode);
 
   switch(get_mode_sort(mode))
   {
@@ -323,6 +319,8 @@ tarval *new_tarval_from_long(long l, ir_mode *mode)
       /* XXX C semantics ! */
       return l ? tarval_b_true : tarval_b_false ;
 
+    case irms_reference:
+      /* same as integer modes */
     case irms_int_number:
     case irms_character:
       sc_val_from_long(l, NULL);
@@ -331,11 +329,8 @@ tarval *new_tarval_from_long(long l, ir_mode *mode)
     case irms_float_number:
       return new_tarval_from_double((long double)l, mode);
 
-    case irms_reference:
-      return l ? tarval_bad : get_tarval(NULL, 0, mode);  /* null pointer or tarval_bad */
-
     default:
-      assert(0);
+      assert(0 && "unsupported mode sort");
   }
   return NULL;
 }
@@ -343,8 +338,10 @@ tarval *new_tarval_from_long(long l, ir_mode *mode)
 /* returns non-zero if can be converted to long */
 int tarval_is_long(tarval *tv)
 {
+  mode_sort sort = get_mode_sort(tv->mode);
+
   ANNOUNCE();
-  if (get_mode_sort(tv->mode) != irms_int_number) return 0;
+  if (sort != irms_int_number && sort != irms_character) return 0;
 
   if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
   {
@@ -408,21 +405,11 @@ long double get_tarval_double(tarval *tv)
 /*
  * Access routines for tarval fields ========================================
  */
-ir_mode *get_tarval_mode (tarval *tv)       /* get the mode of the tarval */
-{
-  ANNOUNCE();
-  assert(tv);
-  return tv->mode;
-}
 
-/*
-void *get_tarval_link (tarval *tv)
-{
-  ANNOUNCE ();
-  assert (tv);
-  return (tv->link);
+/* get the mode of the tarval */
+ir_mode *(get_tarval_mode)(const tarval *tv) {
+  return _get_tarval_mode(tv);
 }
-*/
 
 /*
  * Special value query functions ============================================
@@ -434,30 +421,20 @@ void *get_tarval_link (tarval *tv)
  * therefore the irmode functions should be prefered to the functions below.
  */
 
-tarval *get_tarval_bad(void)
-{
-  ANNOUNCE();
-  return tarval_bad;
+tarval *(get_tarval_bad)(void) {
+  return _get_tarval_bad();
 }
-tarval *get_tarval_undefined(void)
-{
-  ANNOUNCE();
-  return tarval_undefined;
-}
-tarval *get_tarval_b_false(void)
-{
-  ANNOUNCE();
-  return tarval_b_false;
+
+tarval *(get_tarval_undefined)(void) {
+  return _get_tarval_undefined();
 }
-tarval *get_tarval_b_true(void)
-{
-  ANNOUNCE();
-  return tarval_b_true;
+
+tarval *(get_tarval_b_false)(void) {
+  return _get_tarval_b_false();
 }
-tarval *get_tarval_P_void(void)
-{
-  ANNOUNCE();
-  return tarval_P_void;
+
+tarval *(get_tarval_b_true)(void) {
+  return _get_tarval_b_true();
 }
 
 tarval *get_tarval_max(ir_mode *mode)
@@ -550,6 +527,8 @@ tarval *get_tarval_min(ir_mode *mode)
   return tarval_bad;
 }
 
+static long _null_value;
+
 tarval *get_tarval_null(ir_mode *mode)
 {
   ANNOUNCE();
@@ -577,7 +556,7 @@ tarval *get_tarval_null(ir_mode *mode)
       return new_tarval_from_long(0l,  mode);
 
     case irms_reference:
-      return tarval_P_void;
+      return new_tarval_from_long(_null_value, mode);
   }
   return tarval_bad;
 }
@@ -799,10 +778,16 @@ pn_Cmp tarval_cmp(tarval *a, tarval *b)
   assert(a);
   assert(b);
 
-  if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
-  if (a == tarval_undefined || b == tarval_undefined) return pn_Cmp_False;
-  if (a == b) return pn_Cmp_Eq;
-  if (a->mode != b->mode) return pn_Cmp_False;
+  if (a == tarval_bad || b == tarval_bad) {
+    assert(0 && "Comparison with tarval_bad");
+    return pn_Cmp_False;
+  }
+
+  if (a == tarval_undefined || b == tarval_undefined)
+    return pn_Cmp_False;
+
+  if (a->mode != b->mode)
+    return pn_Cmp_False;
 
   if (get_mode_n_vector_elems(a->mode) > 1) {
     /* vector arithmetic not implemented yet */
@@ -816,21 +801,31 @@ pn_Cmp tarval_cmp(tarval *a, tarval *b)
     case irms_memory:
     case irms_auxiliary:
     case irms_reference:
+      if (a == b)
+        return pn_Cmp_Eq;
       return pn_Cmp_False;
 
     case irms_float_number:
+      /*
+       * BEWARE: we cannot compare a == b here, because
+       * a NaN is always Unordered to any other value, even to itself!
+       */
       switch (fc_comp(a->value, b->value)) {
         case -1: return pn_Cmp_Lt;
-        case  0: assert(0 && "different tarvals compare equal"); return pn_Cmp_Eq;
+        case  0: return pn_Cmp_Eq;
         case  1: return pn_Cmp_Gt;
         case  2: return pn_Cmp_Uo;
         default: return pn_Cmp_False;
       }
     case irms_int_number:
     case irms_character:
+      if (a == b)
+        return pn_Cmp_Eq;
       return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
 
     case irms_internal_boolean:
+      if (a == b)
+        return pn_Cmp_Eq;
       return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
   }
   return pn_Cmp_False;
@@ -891,9 +886,10 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m)
               fc_rnd(src->value, NULL);
               break;
             default:
+              assert(0);
               break;
           }
-          /* XXX floating point unit can't produce a value in integer
+          /* FIXME: floating point unit can't produce a value in integer
            * representation
            * an intermediate representation is needed here first. */
           /*  return get_tarval(); */
@@ -940,6 +936,12 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m)
           }
           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
 
+        case irms_reference:
+          /* allow 0 to be casted */
+          if (src == get_mode_null(src->mode))
+            return get_mode_null(m);
+          break;
+
         default:
           break;
       }
@@ -975,7 +977,6 @@ tarval *tarval_not(tarval *a)
 
   ANNOUNCE();
   assert(a);
-  assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
 
   /* works for vector mode without changes */
 
@@ -986,7 +987,15 @@ tarval *tarval_not(tarval *a)
       sc_not(a->value, buffer);
       return get_tarval(buffer, a->length, a->mode);
 
+    case irms_internal_boolean:
+      if (a == tarval_b_true)
+        return tarval_b_false;
+      if (a == tarval_b_false)
+        return tarval_b_true;
+      return tarval_bad;
+
     default:
+      assert(0 && "bitwise negation is only allowed for integer and boolean");
       return tarval_bad;
   }
 }
@@ -1501,21 +1510,21 @@ int tarval_snprintf(char *buf, size_t len, tarval *tv)
       break;
 
     case irms_reference:
-      if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
-      if (tv->value != NULL){
-      if (len > tv->length) {
-        memcpy(buf, tv->value, tv->length);
-        buf[tv->length] = '\0';
-      }
-      else {
-        /* truncated */
-        memcpy(buf, tv->value, len-1);
-        buf[len-1] = '\0';
+      if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
+      if (tv->value != NULL) {
+        if (len > tv->length) {
+          memcpy(buf, tv->value, tv->length);
+          buf[tv->length] = '\0';
+        }
+        else {
+          /* truncated */
+          memcpy(buf, tv->value, len-1);
+          buf[len-1] = '\0';
+        }
+        return tv->length;
       }
-      return tv->length;
-         }
       else
-    return snprintf(buf, len, "void");
+        return snprintf(buf, len, "void");
 
     case irms_internal_boolean:
       switch (mode_info->mode_output) {
@@ -1560,7 +1569,7 @@ char *get_tarval_bitpattern(tarval *tv)
   int i, j, pos = 0;
   int n = get_mode_size_bits(tv->mode);
   int bytes = (n + 7) / 8;
-  char *res = malloc((n + 1) * sizeof(char));
+  char *res = xmalloc((n + 1) * sizeof(char));
   unsigned char byte;
 
   for(i = 0; i < bytes; i++) {
@@ -1624,7 +1633,7 @@ const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
  * Identifying tarvals values for algebraic simplifications.
  *
  * Returns:
- *   - TV_CLASSIFY_NULL    for additive neutral,
+ *   - TV_CLASSIFY_NULL    for additive neutral or the NULL tarval for reference modes,
  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
  *   - TV_CLASSIFY_OTHER   else
@@ -1645,7 +1654,33 @@ tarval_classification_t classify_tarval(tarval *tv)
   return TV_CLASSIFY_OTHER;
 }
 
-/**
+/*
+ * Returns non-zero if a given (integer) tarval has only one single bit
+ * set.
+ */
+int is_single_bit_tarval(tarval *tv) {
+  int i, l;
+  int bits;
+
+  if (!tv || tv == tarval_bad) return 0;
+  if (! mode_is_int(tv->mode)) return 0;
+
+  l = get_mode_size_bytes(tv->mode);
+  for (bits = 0, i = l - 1; i >= 0; --i) {
+    unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
+
+    /* check for more than one bit in these */
+    if (v) {
+      if (v & (v-1))
+        return 0;
+      if (++bits > 1)
+        return 0;
+    }
+  }
+  return bits;
+}
+
+/*
  * Sets the overflow mode for integer operations.
  */
 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
@@ -1677,13 +1712,14 @@ static const tarval_mode_info reference_output = {
   ")",
 };
 
-
 /*
  * Initialization of the tarval module: called before init_mode()
  */
-void init_tarval_1(void)
+void init_tarval_1(long null_value)
 {
   ANNOUNCE();
+  _null_value = null_value;
+
   /* initialize the sets holding the tarvals with a comparison function and
    * an initial size, which is the expected number of constants */
   tarvals = new_set(memcmp, N_CONSTANTS);
@@ -1701,11 +1737,21 @@ void init_tarval_2(void)
 {
   ANNOUNCE();
 
-  tarval_bad->mode       = mode_BAD;
-  tarval_undefined->mode = mode_ANY;
-  tarval_b_true->mode    = mode_b;
-  tarval_b_false->mode   = mode_b;
-  tarval_P_void->mode    = mode_P;
+  tarval_bad->kind        = k_tarval;
+  tarval_bad->mode        = mode_BAD;
+  tarval_bad->value       = INT_TO_PTR(resid_tarval_bad);
+
+  tarval_undefined->kind  = k_tarval;
+  tarval_undefined->mode  = mode_ANY;
+  tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
+
+  tarval_b_true->kind     = k_tarval;
+  tarval_b_true->mode     = mode_b;
+  tarval_b_true->value    = INT_TO_PTR(resid_tarval_b_true);
+
+  tarval_b_false->kind    = k_tarval;
+  tarval_b_false->mode    = mode_b;
+  tarval_b_false->value   = INT_TO_PTR(resid_tarval_b_false);
 
   /*
    * assign output modes that are compatible with the