Initial version of the memory disambiguator added
[libfirm] / ir / tv / tv.c
index 6c50d1d..21dab02 100644 (file)
@@ -98,8 +98,8 @@ static long long count = 0;
 /****************************************************************************
  *   private variables
  ****************************************************************************/
-static struct set *tarvals;   /* container for tarval structs */
-static struct set *values;    /* container for values */
+static struct set *tarvals = NULL;   /* container for tarval structs */
+static struct set *values = NULL;    /* container for values */
 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
 
 /****************************************************************************
@@ -137,13 +137,13 @@ INLINE static void tarval_verify(tarval *tv)
 }
 #endif /* NDEBUG */
 
-static int hash_tv(tarval *tv)
-{
+/** Hash a tarval. */
+static int hash_tv(tarval *tv) {
   return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
 }
 
-static int hash_val(const void *value, unsigned int length)
-{
+/** Hash a value. Treat it as a byte array. */
+static int hash_val(const void *value, unsigned int length) {
   unsigned int i;
   unsigned int hash = 0;
 
@@ -194,6 +194,8 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
               char *temp = alloca(sc_get_buffer_length());
               sc_val_from_ulong(-1, temp);
               sc_and(temp, value, temp);
+                         /* the sc_ module expects that all bits are set ... */
+              sign_extend(temp, mode);
               return get_tarval(temp, length, mode);
             }
           case TV_OVERFLOW_BAD:
@@ -906,7 +908,10 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m)
       switch (get_mode_sort(m)) {
         case irms_int_number:
         case irms_character:
-          return get_tarval_overflow(src->value, src->length, m);
+         buffer = alloca(sc_get_buffer_length());
+         memcpy(buffer, src->value, sc_get_buffer_length());
+         sign_extend(buffer, src->mode);
+          return get_tarval_overflow(buffer, src->length, m);
 
         case irms_internal_boolean:
           /* XXX C semantics */
@@ -921,7 +926,8 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m)
           /* decimal string representation because hexadecimal output is
            * interpreted unsigned by fc_val_from_str, so this is a HACK */
           snprintf(buffer, 100, "%s",
-                   sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
+                   sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
+          buffer[100 - 1] = '\0';
           switch (get_mode_size_bits(m))
           {
             case 32:
@@ -1474,23 +1480,26 @@ int tarval_snprintf(char *buf, size_t len, tarval *tv)
 
   switch (get_mode_sort(tv->mode))
   {
+    case irms_reference:
+      if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
+      /* fall through */
     case irms_int_number:
     case irms_character:
       switch (mode_info->mode_output) {
 
       case TVO_DECIMAL:
-        str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
-    break;
+        str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
+        break;
 
       case TVO_OCTAL:
-        str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
-    break;
+        str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
+        break;
 
       case TVO_HEX:
       case TVO_NATIVE:
       default:
-        str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
-    break;
+        str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
+        break;
       }
       return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
 
@@ -1509,23 +1518,6 @@ int tarval_snprintf(char *buf, size_t len, tarval *tv)
       }
       break;
 
-    case irms_reference:
-      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;
-      }
-      else
-        return snprintf(buf, len, "void");
-
     case irms_internal_boolean:
       switch (mode_info->mode_output) {
 
@@ -1703,15 +1695,6 @@ static const tarval_mode_info hex_output = {
   NULL,
 };
 
-/**
- * default mode_info for output as reference
- */
-static const tarval_mode_info reference_output = {
-  TVO_NATIVE,
-  "&(",
-  ")",
-};
-
 /*
  * Initialization of the tarval module: called before init_mode()
  */
@@ -1767,7 +1750,7 @@ void init_tarval_2(void)
   set_tarval_mode_output_option(mode_Iu, &hex_output);
   set_tarval_mode_output_option(mode_Ls, &hex_output);
   set_tarval_mode_output_option(mode_Lu, &hex_output);
-  set_tarval_mode_output_option(mode_P,  &reference_output);
+  set_tarval_mode_output_option(mode_P,  &hex_output);
 }
 
 /* free all memory occupied by tarval. */