Fix compile errors.
[libfirm] / ir / ir / irmode.c
index b441d57..49c14ac 100644 (file)
  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil
  * @version  $Id$
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#ifdef HAVE_STRING_H
-# include <string.h>
-#endif
+#include "config.h"
 
-# include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
 
-# include "irprog_t.h"
-# include "irmode_t.h"
-# include "ident.h"
-# include "tv_t.h"
-# include "obst.h"
-# include "irhooks.h"
-# include "irtools.h"
-# include "array.h"
+#include <stddef.h>
 
-/* * *
- * local values
- * * */
+#include "irprog_t.h"
+#include "irmode_t.h"
+#include "ident.h"
+#include "tv_t.h"
+#include "obst.h"
+#include "irhooks.h"
+#include "irtools.h"
+#include "array.h"
+#include "error.h"
 
-
-/** dynamic array to hold all modes */
+/** Obstack to hold all modes. */
 static struct obstack modes;
 
-/** number of defined modes */
+/** Number of defined modes. */
 static int num_modes = 0;
 
+/** The list of all currently existing modes. */
 static ir_mode **mode_list;
 
-/* * *
- * local functions
- * * */
+const char *get_mode_arithmetic_name(ir_mode_arithmetic ari)
+{
+#define X(a)    case a: return #a
+       switch (ari) {
+               X(irma_uninitialized);
+               X(irma_none);
+               X(irma_twos_complement);
+               X(irma_ones_complement);
+               X(irma_int_BCD);
+               X(irma_ieee754);
+               X(irma_float_BCD);
+               default: return "<unknown>";
+       }
+#undef X
+}
 
 /**
  * Compare modes that don't need to have their code field
@@ -68,7 +71,7 @@ static ir_mode **mode_list;
  *
  * TODO: Add other fields
  **/
-INLINE static int modes_are_equal(const ir_mode *m, const ir_mode *n) {
+static inline int modes_are_equal(const ir_mode *m, const ir_mode *n) {
        if (m == n) return 1;
        if (m->sort         == n->sort &&
                m->arithmetic   == n->arithmetic &&
@@ -87,17 +90,34 @@ INLINE static int modes_are_equal(const ir_mode *m, const ir_mode *n) {
  * none found
  */
 static ir_mode *find_mode(const ir_mode *m) {
-       unsigned len = ARR_LEN(mode_list);
-       unsigned i;
-
-       for(i = 0; i < len; ++i) {
+       int i;
+       for (i = ARR_LEN(mode_list) - 1; i >= 0; --i) {
                ir_mode *n = mode_list[i];
                if (modes_are_equal(n, m))
                        return n;
        }
+       return NULL;
+}
+
+#ifdef FIRM_STATISTICS
+/* return the mode index, only needed for statistics */
+int stat_find_mode_index(const ir_mode *m) {
+       int i;
+       for (i = ARR_LEN(mode_list) - 1; i >= 0; --i) {
+               ir_mode *n = mode_list[i];
+               if (modes_are_equal(n, m))
+                       return i;
+       }
+       return -1;
+}
 
+/* return the mode for a given index, only needed for statistics */
+ir_mode *stat_mode_for_index(int idx) {
+       if (0 <= idx  && idx < ARR_LEN(mode_list))
+               return mode_list[idx];
        return NULL;
 }
+#endif
 
 /**
  * sets special values of modes
@@ -255,8 +275,8 @@ static ir_mode *register_mode(const ir_mode *new_mode) {
 /*
  * Creates a new mode.
  */
-ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int sign,
-                     mode_arithmetic arithmetic, unsigned int modulo_shift)
+ir_mode *new_ir_mode(const char *name, ir_mode_sort sort, int bit_size, int sign,
+                     ir_mode_arithmetic arithmetic, unsigned int modulo_shift)
 {
        ir_mode mode_tmpl;
        ir_mode *mode = NULL;
@@ -283,22 +303,23 @@ ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int sign,
        case irms_control_flow:
        case irms_memory:
        case irms_internal_boolean:
-               assert(0 && "internal modes cannot be user defined");
-               break;
+               panic("internal modes cannot be user defined");
 
        case irms_float_number:
        case irms_int_number:
        case irms_reference:
                mode = register_mode(&mode_tmpl);
+               break;
        }
+       assert(mode != NULL);
        return mode;
 }
 
 /*
  * Creates a new vector mode.
  */
-ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsigned num_of_elem, int sign,
-                            mode_arithmetic arithmetic, unsigned int modulo_shift)
+ir_mode *new_ir_vector_mode(const char *name, ir_mode_sort sort, int bit_size, unsigned num_of_elem, int sign,
+                            ir_mode_arithmetic arithmetic, unsigned int modulo_shift)
 {
        ir_mode mode_tmpl;
        ir_mode *mode = NULL;
@@ -330,25 +351,23 @@ ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsi
        case irms_control_flow:
        case irms_memory:
        case irms_internal_boolean:
-               assert(0 && "internal modes cannot be user defined");
-               break;
+               panic("internal modes cannot be user defined");
 
        case irms_reference:
-               assert(0 && "only integer and floating point modes can be vectorized");
-               break;
+               panic("only integer and floating point modes can be vectorized");
 
        case irms_float_number:
-               assert(0 && "not yet implemented");
-               break;
+               panic("not yet implemented");
 
        case irms_int_number:
                mode = register_mode(&mode_tmpl);
        }
+       assert(mode != NULL);
        return mode;
 }
 
 /* Functions for the direct access to all attributes of an ir_mode */
-modecode (get_mode_modecode)(const ir_mode *mode) {
+ir_modecode (get_mode_modecode)(const ir_mode *mode) {
        return _get_mode_modecode(mode);
 }
 
@@ -360,7 +379,7 @@ const char *get_mode_name(const ir_mode *mode) {
        return get_id_str(mode->name);
 }
 
-mode_sort (get_mode_sort)(const ir_mode* mode) {
+ir_mode_sort (get_mode_sort)(const ir_mode* mode) {
        return _get_mode_sort(mode);
 }
 
@@ -376,7 +395,7 @@ int (get_mode_sign)(const ir_mode *mode) {
        return _get_mode_sign(mode);
 }
 
-mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode) {
+ir_mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode) {
        return get_mode_arithmetic(mode);
 }
 
@@ -403,7 +422,7 @@ void (set_mode_link)(ir_mode *mode, void *l) {
 
 tarval *get_mode_min(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_data(mode));
 
        return mode->min;
@@ -411,7 +430,7 @@ tarval *get_mode_min(ir_mode *mode) {
 
 tarval *get_mode_max(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_data(mode));
 
        return mode->max;
@@ -419,7 +438,7 @@ tarval *get_mode_max(ir_mode *mode) {
 
 tarval *get_mode_null(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_datab(mode));
 
        return mode->null;
@@ -427,7 +446,7 @@ tarval *get_mode_null(ir_mode *mode) {
 
 tarval *get_mode_one(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_datab(mode));
 
        return mode->one;
@@ -435,7 +454,7 @@ tarval *get_mode_one(ir_mode *mode) {
 
 tarval *get_mode_minus_one(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_data(mode));
 
        return mode->minus_one;
@@ -443,14 +462,14 @@ tarval *get_mode_minus_one(ir_mode *mode) {
 
 tarval *get_mode_all_one(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_datab(mode));
        return mode->all_one;
 }
 
 tarval *get_mode_infinite(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_float(mode));
 
        return get_tarval_plus_inf(mode);
@@ -458,17 +477,14 @@ tarval *get_mode_infinite(ir_mode *mode) {
 
 tarval *get_mode_NAN(ir_mode *mode) {
        assert(mode);
-       assert(get_mode_modecode(mode) < (modecode) num_modes);
+       assert(get_mode_modecode(mode) < (ir_modecode) num_modes);
        assert(mode_is_float(mode));
 
        return get_tarval_nan(mode);
 }
 
-int is_mode(void *thing) {
-       if (get_kind(thing) == k_ir_mode)
-               return 1;
-       else
-               return 0;
+int is_mode(const void *thing) {
+       return get_kind(thing) == k_ir_mode;
 }
 
 int (mode_is_signed)(const ir_mode *mode) {
@@ -527,11 +543,11 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) {
        case irms_int_number:
                switch (get_mode_sort(lm)) {
                case irms_int_number:
-                       if(get_mode_arithmetic(sm) != get_mode_arithmetic(lm))
+                       if (get_mode_arithmetic(sm) != get_mode_arithmetic(lm))
                                return 0;
 
                        /* only two complement implemented */
-                       assert(get_mode_arithmetic(sm)==irma_twos_complement);
+                       assert(get_mode_arithmetic(sm) == irma_twos_complement);
 
                        /* integers are convertable if
                         *   - both have the same sign and lm is the larger one
@@ -539,12 +555,12 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) {
                         *     (one for the sign, one for the highest bit of sm)
                         *   - sm & lm are two_complement and lm has greater or equal number of bits
                         */
-                       if(mode_is_signed(sm)) {
-                               if(!mode_is_signed(lm))
+                       if (mode_is_signed(sm)) {
+                               if (!mode_is_signed(lm))
                                        return 0;
                                return sm_bits <= lm_bits;
                        } else {
-                               if(mode_is_signed(lm)) {
+                               if (mode_is_signed(lm)) {
                                        return sm_bits < lm_bits;
                                }
                                return sm_bits <= lm_bits;
@@ -569,7 +585,7 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) {
                break;
 
        case irms_reference:
-               /* do exist machines out there with different pointer lenghts ?*/
+               /* do exist machines out there with different pointer lengths ?*/
                return 0;
 
        case irms_internal_boolean:
@@ -583,6 +599,37 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) {
        return 0;
 }
 
+/* Returns true if a value of mode sm can be converted into mode lm
+   and backwards without loss. */
+int values_in_mode(const ir_mode *sm, const ir_mode *lm) {
+       int sm_bits, lm_bits;
+       ir_mode_arithmetic arith;
+
+       assert(sm);
+       assert(lm);
+
+       if (sm == lm) return 1;
+
+       if (sm == mode_b)
+               return mode_is_int(lm);
+
+       sm_bits = get_mode_size_bits(sm);
+       lm_bits = get_mode_size_bits(lm);
+
+       arith = get_mode_arithmetic(sm);
+       if (arith != get_mode_arithmetic(lm))
+               return 0;
+
+       switch (arith) {
+               case irma_twos_complement:
+               case irma_ieee754:
+                       return get_mode_size_bits(sm) <= get_mode_size_bits(lm);
+
+               default:
+                       return 0;
+       }
+}
+
 /* Return the signed integer equivalent mode for an reference mode. */
 ir_mode *get_reference_mode_signed_eq(ir_mode *mode) {
        assert(mode_is_reference(mode));
@@ -711,7 +758,9 @@ void init_mode(void) {
        newmode.name    = new_id_from_chars("E", 1);
        newmode.code    = irm_E;
        newmode.sign    = 1;
-       newmode.size    = 80;
+       /* note that the tarval module is calculating with 80 bits, but we use
+        * 96 bits, as that is what will be stored to memory by most hardware */
+       newmode.size    = 96;
 
        mode_E = register_mode(&newmode);
 
@@ -834,7 +883,11 @@ void init_mode(void) {
 ir_mode *find_unsigned_mode(const ir_mode *mode) {
        ir_mode n = *mode;
 
-       assert(mode->sort == irms_int_number);
+       /* allowed for reference mode */
+       if (mode->sort == irms_reference)
+               n.sort = irms_int_number;
+
+       assert(n.sort == irms_int_number);
        n.sign = 0;
        return find_mode(&n);
 }
@@ -894,6 +947,25 @@ int mode_wrap_around(const ir_mode *mode) {
        return mode_is_int(mode);
 }
 
+/*
+ * Returns non-zero if the cast from mode src to mode dst is a
+ * reinterpret cast (ie. only the bit pattern is reinterpreted,
+ * no conversion is done)
+ */
+int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst) {
+       ir_mode_arithmetic ma;
+
+       if (src == dst)
+               return 1;
+       if (get_mode_size_bits(src) != get_mode_size_bits(dst))
+               return 0;
+       ma = get_mode_arithmetic(src);
+       if (ma != get_mode_arithmetic(dst))
+               return 0;
+
+       return ma == irma_twos_complement || ma == irma_ones_complement;
+}
+
 void finish_mode(void) {
        obstack_free(&modes, 0);
        DEL_ARR_F(mode_list);