Added comment
[libfirm] / ir / ir / irmode.c
index ec1711d..40d02b8 100644 (file)
@@ -56,12 +56,13 @@ static int num_modes;
 INLINE static 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 &&
-      m->size == n->size &&
-      m->align == n->align &&
-      m->sign == n->sign  &&
-      m->modulo_shift == n->modulo_shift)
+  if (m->sort         == n->sort &&
+      m->arithmetic   == n->arithmetic &&
+      m->size         == n->size &&
+      m->align        == n->align &&
+      m->sign         == n->sign  &&
+      m->modulo_shift == n->modulo_shift &&
+      m->vector_elem  == n->vector_elem)
     return 1;
 
   return 0;
@@ -75,9 +76,9 @@ static void *next_obstack_adr(struct obstack *o, void *p, size_t s)
   PTR_INT_TYPE adr = __PTR_TO_INT((char *)p);
   int mask = obstack_alignment_mask(o);
 
-  adr += s + o->alignment_mask;
+  adr += s + mask;
 
-  return __INT_TO_PTR(adr & ~o->alignment_mask);
+  return __INT_TO_PTR(adr & ~mask);
 }
 
 /**
@@ -125,6 +126,7 @@ static void set_mode_values(ir_mode* mode)
 {
   switch (get_mode_sort(mode))
   {
+    case irms_character:
     case irms_int_number:
     case irms_float_number:
       mode->min = get_tarval_min(mode);
@@ -143,11 +145,10 @@ static void set_mode_values(ir_mode* mode)
     case irms_reference:
       mode->min = tarval_bad;
       mode->max = tarval_bad;
-      mode->null = (get_mode_modecode(mode)==irm_P)?tarval_P_void:tarval_bad;
+      mode->null = (get_mode_modecode(mode) == irm_P) ? tarval_P_void : tarval_bad;
       mode->one = tarval_bad;
       break;
 
-    case irms_character:
     case irms_auxiliary:
     case irms_memory:
     case irms_control_flow:
@@ -223,12 +224,15 @@ INLINE ir_mode *get_modeANY(void) { ANNOUNCE(); return mode_ANY; }
 INLINE ir_mode *get_modeBAD(void) { ANNOUNCE(); return mode_BAD; }
 
 
-ir_mode *get_modeP_mach(void)  { ANNOUNCE(); return mode_P_mach; }
-void     set_modeP_mach(ir_mode *p) {
+ir_mode *(get_modeP_mach)(void) {
   ANNOUNCE();
-  assert(mode_is_reference(p));
-  mode_P_mach = p;
- }
+  return __get_modeP_mach();
+}
+
+void (set_modeP_mach)(ir_mode *p) {
+  ANNOUNCE();
+  __set_modeP_mach(p);
+}
 
 /**
  * Registers a new mode if not defined yet, else returns
@@ -242,7 +246,7 @@ static ir_mode *register_mode(const ir_mode* new_mode)
   assert(new_mode);
 
   /* copy mode struct to modes array */
-  mode=(ir_mode*) obstack_copy(&modes, new_mode, sizeof(ir_mode));
+  mode = (ir_mode*)obstack_copy(&modes, new_mode, sizeof(ir_mode));
 
   mode->kind = k_ir_mode;
   if(num_modes>=irm_max) mode->code = num_modes;
@@ -268,6 +272,7 @@ ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int align,
   mode_tmpl.align        = align;
   mode_tmpl.sign         = sign ? 1 : 0;
   mode_tmpl.modulo_shift = (mode_tmpl.sort == irms_int_number) ? modulo_shift : 0;
+  mode_tmpl.vector_elem  = 1;
   mode_tmpl.arithmetic   = arithmetic;
   mode_tmpl.link         = NULL;
   mode_tmpl.tv_priv      = NULL;
@@ -289,12 +294,63 @@ ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int align,
       return NULL;
 
     case irms_float_number:
-      assert(0 && "not yet implemented");
+    case irms_int_number:
+    case irms_reference:
+    case irms_character:
+      return register_mode(&mode_tmpl);
+  }
+  return NULL; /* to shut up gcc */
+}
+
+/*
+ * 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 align, int sign,
+                    mode_arithmetic arithmetic, unsigned int modulo_shift )
+{
+  ir_mode mode_tmpl;
+  ir_mode *mode;
+
+  mode_tmpl.name         = new_id_from_str(name);
+  mode_tmpl.sort         = sort;
+  mode_tmpl.size         = bit_size * num_of_elem;
+  mode_tmpl.align        = align;
+  mode_tmpl.sign         = sign ? 1 : 0;
+  mode_tmpl.modulo_shift = (mode_tmpl.sort == irms_int_number) ? modulo_shift : 0;
+  mode_tmpl.vector_elem  = num_of_elem;
+  mode_tmpl.arithmetic   = arithmetic;
+  mode_tmpl.link         = NULL;
+  mode_tmpl.tv_priv      = NULL;
+
+  mode = find_mode(&mode_tmpl);
+  if (mode)
+    return mode;
+
+  if (num_of_elem <= 1) {
+    assert(0 && "vector modes should have at least 2 elements");
+    return NULL;
+  }
+
+  /* sanity checks */
+  switch (sort)
+  {
+    case irms_auxiliary:
+    case irms_control_flow:
+    case irms_memory:
+    case irms_internal_boolean:
+      assert(0 && "internal modes cannot be user defined");
       return NULL;
 
-    case irms_int_number:
     case irms_reference:
     case irms_character:
+      assert(0 && "only integer and floating point modes can be vectorized");
+      return NULL;
+
+    case irms_float_number:
+      assert(0 && "not yet implemented");
+      return NULL;
+
+    case irms_int_number:
       return register_mode(&mode_tmpl);
   }
   return NULL; /* to shut up gcc */
@@ -302,17 +358,17 @@ ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int align,
 
 /* Functions for the direct access to all attributes od a ir_mode */
 modecode
-get_mode_modecode(const ir_mode *mode)
+(get_mode_modecode)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->code;
+  return __get_mode_modecode(mode);
 }
 
 ident *
-get_mode_ident(const ir_mode *mode)
+(get_mode_ident)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->name;
+  return __get_mode_ident(mode);
 }
 
 const char *
@@ -323,44 +379,44 @@ get_mode_name(const ir_mode *mode)
 }
 
 mode_sort
-get_mode_sort(const ir_mode* mode)
+(get_mode_sort)(const ir_mode* mode)
 {
   ANNOUNCE();
-  return mode->sort;
+  return __get_mode_sort(mode);
 }
 
-INLINE int
-get_mode_size_bits(const ir_mode *mode)
+int
+(get_mode_size_bits)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->size;
+  return __get_mode_size_bits(mode);
 }
 
-int get_mode_size_bytes(const ir_mode *mode) {
-  int size = get_mode_size_bits(mode);
+int
+(get_mode_size_bytes)(const ir_mode *mode) {
   ANNOUNCE();
-  if ((size & 7) != 0) return -1;
-  return size >> 3;
+  return __get_mode_size_bytes(mode);
 }
 
 int
-get_mode_align (const ir_mode *mode)
+(get_mode_align)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->align;
+  return __get_mode_align(mode);
 }
 
 int
-get_mode_sign (const ir_mode *mode)
+(get_mode_sign)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->sign;
+  return __get_mode_sign(mode);
 }
 
-int get_mode_arithmetic (const ir_mode *mode)
+int
+(get_mode_arithmetic)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->arithmetic;
+  return get_mode_arithmetic(mode);
 }
 
 
@@ -368,20 +424,27 @@ int get_mode_arithmetic (const ir_mode *mode)
  *  whether shift applies modulo to value of bits to shift.  Asserts
  *  if mode is not irms_int_number.
  */
-unsigned int get_mode_modulo_shift(const ir_mode *mode) {
-  return mode->modulo_shift;
+unsigned int
+(get_mode_modulo_shift)(const ir_mode *mode) {
+  return __get_mode_modulo_shift(mode);
 }
 
-void* get_mode_link(const ir_mode *mode)
+unsigned int
+(get_mode_vector_elems)(const ir_mode *mode) {
+  return __get_mode_vector_elems(mode);
+}
+
+void *
+(get_mode_link)(const ir_mode *mode)
 {
   ANNOUNCE();
-  return mode->link;
+  return __get_mode_link(mode);
 }
 
-void set_mode_link(ir_mode *mode, void *l)
+void
+(set_mode_link)(ir_mode *mode, void *l)
 {
-  mode->link=l;
-  return;
+  __set_mode_link(mode, l);
 }
 
 tarval *
@@ -459,149 +522,93 @@ is_mode (void *thing) {
     return 0;
 }
 
-/* Functions to check, whether a modecode is signed, float, int, num, data,
-   datab or dataM. For more exact definitions read the corresponding pages
-   in the firm documentation or the followingenumeration
-
-   The set of "float" is defined as:
-   ---------------------------------
-   float = {irm_F, irm_D, irm_E}
-
-   The set of "int" is defined as:
-   -------------------------------
-   int   = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
-
-   The set of "num" is defined as:
-   -------------------------------
-   num   = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
-            irm_Is, irm_Iu, irm_Ls, irm_Lu}
-            = {float || int}
-
-   The set of "data" is defined as:
-   -------------------------------
-   data  = {irm_F, irm_D, irm_E irm_Bs, irm_Bu, irm_Hs, irm_Hu,
-            irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P}
-            = {num || irm_C || irm_U || irm_P}
-
-   The set of "datab" is defined as:
-   ---------------------------------
-   datab = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
-            irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P, irm_b}
-            = {data || irm_b }
-
-   The set of "dataM" is defined as:
-   ---------------------------------
-   dataM = {irm_F, irm_D, irm_E, irm_Bs, irm_Bu, irm_Hs, irm_Hu,
-            irm_Is, irm_Iu, irm_Ls, irm_Lu, irm_C, irm_U, irm_P, irm_M}
-            = {data || irm_M}
-*/
-
-#ifdef MODE_ACCESS_DEFINES
-#  undef mode_is_signed
-#  undef mode_is_float
-#  undef mode_is_int
-#  undef mode_is_num
-#  undef mode_is_numP
-#  undef mode_is_data
-#  undef mode_is_datab
-#  undef mode_is_dataM
-#endif
 int
-mode_is_signed (const ir_mode *mode)
-{
+(mode_is_signed)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return mode->sign;
+  return __mode_is_signed(mode);
 }
 
 int
-mode_is_float (const ir_mode *mode)
-{
+(mode_is_float)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (get_mode_sort(mode) == irms_float_number);
+  return __mode_is_float(mode);
 }
 
 int
-mode_is_int (const ir_mode *mode)
-{
+(mode_is_int)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (get_mode_sort(mode) == irms_int_number);
+  return __mode_is_int(mode);
 }
 
-int mode_is_character (const ir_mode *mode)
-{
+int
+(mode_is_character)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (get_mode_sort(mode) == irms_character);
+  return __mode_is_character(mode);
 }
 
-int mode_is_reference (const ir_mode *mode)
-{
+int
+(mode_is_reference)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (get_mode_sort(mode) == irms_reference);
+  return __mode_is_reference(mode);
 }
 
 int
-mode_is_num (const ir_mode *mode)
-{
+(mode_is_num)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (mode_is_int(mode) || mode_is_float(mode));
+  return __mode_is_num(mode);
 }
 
 int
-mode_is_numP (const ir_mode *mode)
-{
+(mode_is_numP)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (mode_is_int(mode) || mode_is_float(mode) || mode_is_reference(mode));
+  return __mode_is_numP(mode);
 }
 
 int
-mode_is_data (const ir_mode *mode)
-{
+(mode_is_data)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (mode_is_num(mode) || get_mode_sort(mode) == irms_character || get_mode_sort(mode) == irms_reference);
+  return __mode_is_data(mode);
 }
 
 int
-mode_is_datab (const ir_mode *mode)
-{
+(mode_is_datab)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (mode_is_data(mode) || get_mode_sort(mode) == irms_internal_boolean);
+  return __mode_is_datab(mode);
 }
 
 int
-mode_is_dataM (const ir_mode *mode)
-{
+(mode_is_dataM)(const ir_mode *mode) {
   ANNOUNCE();
-  assert(mode);
-  return (mode_is_data(mode) || get_mode_modecode(mode) == irm_M);
-}
-#ifdef MODE_ACCESS_DEFINES
-#  define mode_is_signed(mode) (mode)->sign
-#  define mode_is_float(mode) ((mode)->sort == irms_float_number)
-#  define mode_is_int(mode) ((mode)->sort == irms_int_number)
-#  define mode_is_num(mode) (((mode)->sort == irms_float_number) || ((mode)->sort == irms_int_number))
-#  define mode_is_data(mode) (((mode)->sort == irms_float_number) || ((mode)->sort == irms_int_number) || ((mode)->sort == irms_character) || ((mode)->sort == irms_reference))
-#  define mode_is_datab(mode) (((mode)->sort == irms_float_number) || ((mode)->sort == irms_int_number) || ((mode)->sort == irms_character) || ((mode)->sort == irms_reference) || ((mode)->sort == irms_internal_boolean))
-#  define mode_is_dataM(mode) (((mode)->sort == irms_float_number) || ((mode)->sort == irms_int_number) || ((mode)->sort == irms_character) || ((mode)->sort == irms_reference) || ((mode)->code == irm_M))
-#endif
+  return __mode_is_dataM(mode);
+}
+
+int
+(mode_is_float_vector)(const ir_mode *mode) {
+  ANNOUNCE();
+  return __mode_is_float_vector(mode);
+}
+
+int
+(mode_is_int_vector)(const ir_mode *mode) {
+  ANNOUNCE();
+  return __mode_is_int_vector(mode);
+}
+
 /* Returns true if sm can be converted to lm without loss. */
 int
 smaller_mode(const ir_mode *sm, const ir_mode *lm)
 {
+  int sm_bits, lm_bits;
+
   ANNOUNCE();
   assert(sm);
   assert(lm);
 
   if (sm == lm) return 1;
 
+  sm_bits = get_mode_size_bits(sm);
+  lm_bits = get_mode_size_bits(lm);
+
   switch(get_mode_sort(sm))
   {
     case irms_int_number:
@@ -612,18 +619,23 @@ smaller_mode(const ir_mode *sm, const ir_mode *lm)
            *   - both have the same sign and lm is the larger one
            *   - lm is the signed one and is at least two bits larger
            *     (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 (   get_mode_arithmetic(sm) == get_mode_arithmetic(lm)
+             && get_mode_arithmetic(sm) == irma_twos_complement) {
+           return lm_bits >= sm_bits;
+         }
+          else if (mode_is_signed(sm))
           {
-            if ( mode_is_signed(lm) && (get_mode_size_bits(lm) > get_mode_size_bits(sm)) )
+            if ( mode_is_signed(lm) && (lm_bits >= sm_bits) )
               return 1;
           }
           else if (mode_is_signed(lm))
           {
-            if (get_mode_size_bits(lm) > get_mode_size_bits(sm) + 1)
+            if (lm_bits > sm_bits + 1)
               return 1;
           }
-          else if (get_mode_size_bits(lm) > get_mode_size_bits(sm))
+          else if (lm_bits >= sm_bits)
           {
             return 1;
           }
@@ -639,11 +651,11 @@ smaller_mode(const ir_mode *sm, const ir_mode *lm)
       break;
 
     case irms_float_number:
-      /* XXX currently only the three standard 32,64,80 bit floats
-       * are supported which can safely be converted */
-      if ( (get_mode_sort(lm) == irms_float_number)
-           && (get_mode_size_bits(lm) > get_mode_size_bits(sm)) )
-         return 1;
+      if (get_mode_arithmetic(sm) == get_mode_arithmetic(lm)) {
+        if ( (get_mode_sort(lm) == irms_float_number)
+           && (get_mode_size_bits(lm) >= get_mode_size_bits(sm)) )
+          return 1;
+      }
       break;
 
     case irms_reference:
@@ -677,6 +689,7 @@ init_mode (void)
   newmode.align        = 0;
   newmode.sign         = 0;
   newmode.modulo_shift = 0;
+  newmode.vector_elem  = 0;
   newmode.link         = NULL;
   newmode.tv_priv      = NULL;
 
@@ -735,6 +748,7 @@ init_mode (void)
   mode_b = register_mode(&newmode);
 
 /* Data Modes */
+  newmode.vector_elem = 1;
 
   /* Float Number Modes */
   newmode.sort    = irms_float_number;
@@ -893,3 +907,35 @@ init_mode (void)
   /* set the machine specific modes to the predifined ones */
   mode_P_mach = mode_P;
 }
+
+
+void finish_mode(void) {
+  obstack_free(&modes, 0);
+
+  mode_T = NULL;
+  mode_X = NULL;
+  mode_M = NULL;
+  mode_BB = NULL;
+  mode_ANY = NULL;
+  mode_BAD = NULL;
+
+  mode_F = NULL;
+  mode_D = NULL;
+  mode_E = NULL;
+
+  mode_Bs = NULL;
+  mode_Bu = NULL;
+  mode_Hs = NULL;
+  mode_Hu = NULL;
+  mode_Is = NULL;
+  mode_Iu = NULL;
+  mode_Ls = NULL;
+  mode_Lu = NULL;
+
+  mode_C = NULL;
+  mode_U = NULL;
+  mode_b = NULL;
+  mode_P = NULL;
+
+  mode_P_mach = NULL;
+}