renamed some firm enum types to have the ir_ prefix
[libfirm] / ir / be / ia32 / ia32_gen_decls.c
index f6ef7d6..65e693e 100644 (file)
 #include "entity.h"
 #include "irprog.h"
 
+#include "../be.h"
+
+#include "ia32_emitter.h"
 #include "ia32_gen_decls.h"
 
+typedef struct obstack obstack_t;
+
+typedef struct _ia32_decl_env {
+       obstack_t *rodata_obst;
+       obstack_t *data_obst;
+       obstack_t *comm_obst;
+       obstack_t *ctor_obst;
+       const be_main_env_t *main_env;
+} ia32_decl_env_t;
+
 /************************************************************************/
 
 /*
  */
 static unsigned highest_bit(unsigned v)
 {
-  int res = -1;
-
-  if (v >= (1U << 16U)) {
-    res += 16;
-    v >>= 16;
-  }
-  if (v >= (1U << 8U)) {
-    res += 8;
-    v >>= 8;
-  }
-  if (v >= (1U << 4U)) {
-    res += 4;
-    v >>= 4;
-  }
-  if (v >= (1U << 2U)) {
-    res += 2;
-    v >>= 2;
-  }
-  if (v >= (1U << 1U)) {
-    res += 1;
-    v >>= 1;
-  }
-  if (v >= 1)
-    res += 1;
-
-  return res;
+       int res = -1;
+
+       if (v >= (1U << 16U)) {
+               res += 16;
+               v >>= 16;
+       }
+       if (v >= (1U << 8U)) {
+               res += 8;
+               v >>= 8;
+       }
+       if (v >= (1U << 4U)) {
+               res += 4;
+               v >>= 4;
+       }
+       if (v >= (1U << 2U)) {
+               res += 2;
+               v >>= 2;
+       }
+       if (v >= (1U << 1U)) {
+               res += 1;
+               v >>= 1;
+       }
+       if (v >= 1)
+               res += 1;
+
+       return res;
 }
 
-/*
- * output the alignment
+static void ia32_dump_comm(obstack_t *obst, const char *name, ir_visibility vis, int size, int align) {
+       switch (asm_flavour) {
+       case ASM_LINUX_GAS:
+               if (vis == visibility_local)
+                       obstack_printf(obst, "\t.local\t%s\n", name);
+               obstack_printf(obst, "\t.comm\t%s,%d,%d\n", name, size, align);
+               break;
+       case ASM_MINGW_GAS:
+               if (vis == visibility_local)
+                       obstack_printf(obst, "\t.lcomm\t%s,%d\n", name, size);
+               else
+                       obstack_printf(obst, "\t.comm\t%s,%d\n", name, size);
+               break;
+       default:
+               break;
+       }
+}
+
+/**
+ * output the alignment to an obstack
  */
-static void ia32_dump_align(struct obstack *obst, int align)
+static void ia32_dump_align(obstack_t *obst, int align)
 {
-  int h = highest_bit(align);
+       int h = highest_bit(align);
 
-  if ((1 << h) < align)
-    ++h;
-  align = (1 << h);
+       if ((1 << h) < align)
+               ++h;
+       align = (1 << h);
 
-  if (align > 1)
-    obstack_printf(obst, "\t.align %d\n", align);
+       if (align > 1)
+               obstack_printf(obst, "\t.align %d\n", align);
 }
 
-static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
+/**
+ * output the alignment to a FILE
+ */
+static void ia32_dump_align_f(FILE *f, int align)
 {
-  switch (bytes) {
-
-  case 1:
-    obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
-    break;
-
-  case 2:
-    obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-    break;
-
-  case 4:
-    obstack_printf(obst, "0x%02x%02x%02x%02x",
-       get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-    break;
-
-  case 8:
-    obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
-       get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
-       get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-    break;
-
-  case 10:
-  case 12:
-    break;
-
-  default:
-    fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
-    assert(0);
-  }
+       int h = highest_bit(align);
+
+       if ((1 << h) < align)
+               ++h;
+       align = (1 << h);
+
+       if (align > 1)
+               fprintf(f, "\t.align %d\n", align);
 }
 
-/*
- * dump an atomic value
+/**
+ * output a tarval
  */
-static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
+static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
 {
-  ir_mode *mode = get_irn_mode(init);
-  int bytes     = get_mode_size_bytes(mode);
-  tarval *tv;
-
-  switch (get_irn_opcode(init)) {
-
-  case iro_Cast:
-    do_dump_atomic_init(obst, get_Cast_op(init));
-    return;
-
-  case iro_Conv:
-    do_dump_atomic_init(obst, get_Conv_op(init));
-    return;
-
-  case iro_Const:
-    tv = get_Const_tarval(init);
-
-    /* beware of old stuff */
-    assert(! mode_is_reference(mode));
-
-    /* it's a arithmetic value */
-    dump_arith_tarval(obst, tv, bytes);
-    return;
-
-  case iro_SymConst:
-    switch (get_SymConst_kind(init)) {
-    case symconst_addr_name:
-      obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
-      break;
-
-    case symconst_addr_ent:
-      obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
-      break;
-
-    case symconst_size:
-      obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
-      break;
-
-    default:
-      assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
-    }
-    return;
-
-  case iro_Add:
-    do_dump_atomic_init(obst, get_Add_left(init));
-    obstack_printf(obst, " + ");
-    do_dump_atomic_init(obst, get_Add_right(init));
-    return;
-
-  case iro_Sub:
-    do_dump_atomic_init(obst, get_Sub_left(init));
-    obstack_printf(obst, " - ");
-    do_dump_atomic_init(obst, get_Sub_right(init));
-    return;
-
-  case iro_Mul:
-    do_dump_atomic_init(obst, get_Mul_left(init));
-    obstack_printf(obst, " * ");
-    do_dump_atomic_init(obst, get_Mul_right(init));
-    return;
-
-  default:
-    assert(0 && "dump_atomic_init(): unknown IR-node");
-  }
+       switch (bytes) {
+
+       case 1:
+               obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
+               break;
+
+       case 2:
+               obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
+               break;
+
+       case 4:
+               obstack_printf(obst, "0x%02x%02x%02x%02x",
+                       get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
+               break;
+
+       case 8:
+               obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
+                       get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
+                       get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
+               break;
+
+       case 10:
+       case 12:
+               break;
+
+       default:
+               fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
+               assert(0);
+       }
 }
 
 /*
  * dump an atomic value
  */
-static void dump_atomic_init(struct obstack *obst, ir_node *init)
+static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
 {
-  ir_mode *mode = get_irn_mode(init);
-  int bytes     = get_mode_size_bytes(mode);
+       ir_mode *mode = get_irn_mode(init);
+       int bytes     = get_mode_size_bytes(mode);
+       tarval *tv;
+
+       switch (get_irn_opcode(init)) {
+
+       case iro_Cast:
+               do_dump_atomic_init(obst, get_Cast_op(init));
+               return;
+
+       case iro_Conv:
+               do_dump_atomic_init(obst, get_Conv_op(init));
+               return;
+
+       case iro_Const:
+               tv = get_Const_tarval(init);
+
+               /* beware of old stuff */
+               //assert(! mode_is_reference(mode));
+
+               /* it's a arithmetic value */
+               dump_arith_tarval(obst, tv, bytes);
+               return;
+
+       case iro_SymConst:
+               switch (get_SymConst_kind(init)) {
+               case symconst_addr_name:
+                       obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
+                       break;
+
+               case symconst_addr_ent:
+                       obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
+                       break;
+
+               case symconst_type_size:
+                       obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
+                       break;
+
+               case symconst_type_align:
+                       obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
+                       break;
+
+               case symconst_enum_const:
+                       tv = get_enumeration_value(get_SymConst_enum(init));
+                       dump_arith_tarval(obst, tv, bytes);
+                       break;
+
+               default:
+                       assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
+               }
+               return;
+
+               case iro_Add:
+                       do_dump_atomic_init(obst, get_Add_left(init));
+                       obstack_printf(obst, " + ");
+                       do_dump_atomic_init(obst, get_Add_right(init));
+                       return;
+
+               case iro_Sub:
+                       do_dump_atomic_init(obst, get_Sub_left(init));
+                       obstack_printf(obst, " - ");
+                       do_dump_atomic_init(obst, get_Sub_right(init));
+                       return;
+
+               case iro_Mul:
+                       do_dump_atomic_init(obst, get_Mul_left(init));
+                       obstack_printf(obst, " * ");
+                       do_dump_atomic_init(obst, get_Mul_right(init));
+                       return;
+
+               default:
+                       assert(0 && "dump_atomic_init(): unknown IR-node");
+       }
+}
 
-  switch (bytes) {
+/*
+ * dumps the type for given size (.byte, .long, ...)
+ */
+static void dump_size_type(obstack_t *obst, int size) {
+       switch (size) {
 
-  case 1:
-    obstack_printf(obst, "\t.byte\t");
-    break;
+       case 1:
+               obstack_printf(obst, "\t.byte\t");
+               break;
 
-  case 2:
-    obstack_printf(obst, "\t.value\t");
-    break;
+       case 2:
+               obstack_printf(obst, "\t.value\t");
+               break;
 
-  case 4:
-    obstack_printf(obst, "\t.long\t");
-    break;
+       case 4:
+               obstack_printf(obst, "\t.long\t");
+               break;
 
-  case 8:
-    obstack_printf(obst, "\t.quad\t");
-    break;
+       case 8:
+               obstack_printf(obst, "\t.quad\t");
+               break;
 
-  case 10:
-  case 12:
-    /* handled in arith */
-    break;
+       case 10:
+       case 12:
+               /* handled in arith */
+               break;
 
-  default:
-    fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
-    assert(0);
-  }
+       default:
+               fprintf(stderr, "Try to dump a type with %d bytes\n", size);
+               assert(0);
+       }
+}
+
+/*
+ * dump an atomic value
+ */
+static void dump_atomic_init(obstack_t *obst, ir_node *init)
+{
+       ir_mode *mode = get_irn_mode(init);
+       int bytes     = get_mode_size_bytes(mode);
 
-  do_dump_atomic_init(obst, init);
-  obstack_printf(obst, "\n");
+       dump_size_type(obst, bytes);
+       do_dump_atomic_init(obst, init);
+       obstack_printf(obst, "\n");
 }
 
 /************************************************************************/
@@ -223,47 +287,47 @@ static void dump_atomic_init(struct obstack *obst, ir_node *init)
  */
 static int ent_is_string_const(entity *ent)
 {
-  int res = 0;
-  ir_type *ty;
-
-  ty = get_entity_type(ent);
-
-  /* if it's an array */
-  if (is_Array_type(ty)) {
-    ir_type *elm_ty = get_array_element_type(ty);
-
-    /* and the array's element type is primitive */
-    if (is_Primitive_type(elm_ty)) {
-      ir_mode *mode = get_type_mode(elm_ty);
-
-      /*
-       * and the mode of the element type is an int of
-       * the same size as the byte mode
-       */
-      if (mode_is_int(mode)
-        && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
-      {
-       int i, c, n;
-
-       n = get_compound_ent_n_values(ent);
-       for (i = 0; i < n; ++i) {
-         ir_node *irn = get_compound_ent_value(ent, i);
-         if(get_irn_opcode(irn) != iro_Const)
-           return 0;
-
-         c = (int) get_tarval_long(get_Const_tarval(irn));
-
-         if((i < n - 1 && !(isgraph(c) || isspace(c)))
-            || (i == n - 1 && c != '\0'))
-           return 0;
+       int res = 0;
+       ir_type *ty;
+
+       ty = get_entity_type(ent);
+
+       /* if it's an array */
+       if (is_Array_type(ty)) {
+               ir_type *elm_ty = get_array_element_type(ty);
+
+               /* and the array's element type is primitive */
+               if (is_Primitive_type(elm_ty)) {
+                       ir_mode *mode = get_type_mode(elm_ty);
+
+                       /*
+                       * and the mode of the element type is an int of
+                       * the same size as the byte mode
+                       */
+                       if (mode_is_int(mode)
+                               && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
+                       {
+                               int i, c, n;
+
+                               n = get_compound_ent_n_values(ent);
+                               for (i = 0; i < n; ++i) {
+                                       ir_node *irn = get_compound_ent_value(ent, i);
+                                       if(get_irn_opcode(irn) != iro_Const)
+                                               return 0;
+
+                                       c = (int) get_tarval_long(get_Const_tarval(irn));
+
+                                       if((i < n - 1 && !(isgraph(c) || isspace(c)))
+                                               || (i == n - 1 && c != '\0'))
+                                               return 0;
+                               }
+
+                               res = 1;
+                       }
+               }
        }
 
-       res = 1;
-      }
-    }
-  }
-
-  return res;
+       return res;
 }
 
 /**
@@ -272,283 +336,358 @@ static int ent_is_string_const(entity *ent)
  * @param obst The obst to dump on.
  * @param ent The entity to dump.
  */
-static void dump_string_cst(struct obstack *obst, entity *ent)
+static void dump_string_cst(obstack_t *obst, entity *ent)
 {
-  int i, n;
-
-  obstack_printf(obst, "\t.string \"");
-  n = get_compound_ent_n_values(ent);
-
-  for (i = 0; i < n-1; ++i) {
-    ir_node *irn;
-    int c;
-
-    irn = get_compound_ent_value(ent, i);
-    c = (int) get_tarval_long(get_Const_tarval(irn));
-
-    switch (c) {
-    case '"' : obstack_printf(obst, "\\\""); break;
-    case '\n': obstack_printf(obst, "\\n"); break;
-    case '\r': obstack_printf(obst, "\\r"); break;
-    case '\t': obstack_printf(obst, "\\t"); break;
-    default  :
-      if (isprint(c))
-       obstack_printf(obst, "%c", c);
-      else
-       obstack_printf(obst, "%O", c);
-      break;
-    }
-  }
-  obstack_printf(obst, "\"\n");
+       int i, n;
+
+       obstack_printf(obst, "\t.string \"");
+       n = get_compound_ent_n_values(ent);
+
+       for (i = 0; i < n-1; ++i) {
+               ir_node *irn;
+               int c;
+
+               irn = get_compound_ent_value(ent, i);
+               c = (int) get_tarval_long(get_Const_tarval(irn));
+
+               switch (c) {
+               case '"' : obstack_printf(obst, "\\\""); break;
+               case '\n': obstack_printf(obst, "\\n"); break;
+               case '\r': obstack_printf(obst, "\\r"); break;
+               case '\t': obstack_printf(obst, "\\t"); break;
+               case '\\': obstack_printf(obst, "\\\\"); break;
+               default  :
+                       if (isprint(c))
+                               obstack_printf(obst, "%c", c);
+                       else
+                               obstack_printf(obst, "%O", c);
+                       break;
+               }
+       }
+       obstack_printf(obst, "\"\n");
 }
 
 struct arr_info {
-  int n_elems;
-  int visit_cnt;
-  int size;
+       int n_elems;
+       int visit_cnt;
+       int size;
 };
 
+/**
+ * Dump the size of an object
+ */
+static void dump_object_size(obstack_t *obst, const char *name, int size) {
+       switch (asm_flavour) {
+       case ASM_LINUX_GAS:
+               obstack_printf(obst, "\t.type\t%s,@object\n", name);
+               obstack_printf(obst, "\t.size\t%s,%d\n", name, size);
+               break;
+       default:
+               break;
+       }
+}
+
 /*
  * Dumps the initialization of global variables that are not
  * "uninitialized".
  */
-static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
+static void dump_global(const be_main_env_t *main_env,
+                                               obstack_t *rdata_obstack, obstack_t *data_obstack,
+                                               obstack_t *comm_obstack, obstack_t *ctor_obstack,
+                                               entity *ent)
 {
-  ir_type *ty         = get_entity_type(ent);
-  const char *ld_name = get_entity_ld_name(ent);
-  int align, h;
-  struct obstack *obst = data_obstack;
-
-  /*
-   * FIXME: did NOT work for partly constant values
-   */
-  if (! is_Method_type(ty)) {
-    ent_variability variability = get_entity_variability(ent);
-    visibility visibility = get_entity_visibility(ent);
-
-    if (variability == variability_constant) {
-      /* a constant entity, put it on the rdata */
-      obst = rdata_obstack;
-    }
-
-    /* check, wether it is initialized, if yes create data */
-    if (variability != variability_uninitialized) {
-      if (visibility == visibility_external_visible) {
-        obstack_printf(obst, ".globl\t%s\n", ld_name);
-      }
-      obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
-      obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
-
-      align = get_type_alignment_bytes(ty);
-      ia32_dump_align(obst, align);
-
-      obstack_printf(obst, "%s:\n", ld_name);
-
-      if (is_atomic_type(ty)) {
-       if (get_entity_visibility(ent) != visibility_external_allocated)
-          dump_atomic_init(obst, get_atomic_ent_value(ent));
-      }
-      else {
-       int i, size = 0;
-
-       if (ent_is_string_const(ent)) {
-         dump_string_cst(obst, ent);
-       }
-       else if (is_Array_type(ty)) {
-          int filler;
-
-          /* potential spare values should be already included! */
-                 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
-            entity *step = get_compound_ent_value_member(ent, i);
-            ir_type *stype = get_entity_type(step);
-
-            if (get_type_mode(stype)) {
-              int align = (get_type_alignment_bits(stype) + 7) >> 3;
-              int n     = size % align;
-
-              if (n > 0) {
-                obstack_printf(obst, "\t.zero\t%d\n", align - n);
-                size += align - n;
-              }
-            }
-            dump_atomic_init(obst, get_compound_ent_value(ent, i));
-            size += get_type_size_bytes(stype);
-         }
-          filler = get_type_size_bytes(ty) - size;
-
-          if (filler > 0)
-            obstack_printf(obst, "\t.zero\t%d\n", filler);
-        }
-        else if (is_compound_type(ty)) {
-          ir_node **vals;
-          int type_size, j;
-
-          /* Compound entities are NOT sorted.
-           * The sorting strategy used doesn't work for `value' compound fields nor
-           * for partially_constant entities.
-           */
-
-          /*
-           * in the worst case, every entity allocates one byte, so the type
-           * size should be equal or bigger the number of fields
-           */
-          type_size = get_type_size_bytes(ty);
-          vals      = xcalloc(type_size, sizeof(*vals));
-
-          /* collect the values and store them at the offsets */
-          for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
-            int                 graph_length, aipos, offset;
-            struct arr_info     *ai;
-            int                 all_n = 1;
-            compound_graph_path *path = get_compound_ent_value_path(ent, i);
-
-            /* get the access path to the costant value */
-            graph_length = get_compound_graph_path_length(path);
-            ai = xcalloc(graph_length, sizeof(struct arr_info));
-
-            /* We wanna know how many arrays are on the path to the entity. We also have to know how
-             * many elements each array holds to calculate the offset for the entity. */
-            for (j = 0; j < graph_length; j++) {
-              entity  *step      = get_compound_graph_path_node(path, j);
-              ir_type *step_type = get_entity_type(step);
-              int     ty_size    = (get_type_size_bits(step_type) + 7) >> 3;
-              int     k, n       = 0;
-
-              if (is_Array_type(step_type))
-                for (k = 0; k < get_array_n_dimensions(step_type); k++)
-                  n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
-              if (n) all_n *= n;
-              ai[j].n_elems = n ? all_n + 1 : 0;
-              ai[j].visit_cnt = 0;
-              ai[j].size = ty_size;
-            }
-
-            aipos = graph_length - 1;
-            if (aipos) aipos--;
-
-            for (offset = j = 0; j < graph_length; j++) {
-              entity *step       = get_compound_graph_path_node(path, j);
-              ir_type *step_type = get_entity_type(step);
-              int ent_ofs        = get_entity_offset_bytes(step);
-              int stepsize       = 0;
-
-              /* add all positive offsets (= offsets in structs) */
-              if (ent_ofs >= 0) offset += ent_ofs;
-
-              if (j == graph_length - 1) {
-                stepsize = (get_type_size_bits(step_type) + 7) >> 3;
-
-                /* Search the next free position in vals depending on the information from above (ai). */
-                while (vals[offset]) {
-                  if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
-                    offset += stepsize;
-                    ai[aipos].visit_cnt++;
-                  }
-                  else
-                    while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
-                      stepsize = ai[aipos--].size;
-                      offset  += stepsize;
-                  }
-                }
-
-                assert(aipos >= 0 && "couldn't store entity");
-                vals[offset] = get_compound_ent_value(ent, i);
-              }
-            }
-
-            free(ai);
-          }
-
-          /* now write them sorted */
-          for(i = 0; i < type_size; ) {
-            if (vals[i]) {
-              dump_atomic_init(obst, vals[i]);
-              i += (get_mode_size_bytes(get_irn_mode(vals[i])));
-            }
-            else {
-              /* a gap */
-              obstack_printf(obst, "\t.byte\t0\n");
-              ++i;
-            }
-          }
-          free(vals);
-        }
-        else {
-          assert(0 && "unsupported type");
-        }
-      }
-      obstack_printf(obst, "\n");
-    }
-    else if (visibility != visibility_external_allocated) {
-      if (visibility == visibility_local) {
-        obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
-      }
-
-      /* calculate the alignment */
-      align = get_type_alignment_bytes(ty);
-      h = highest_bit(align);
-
-      if ((1 << h) < align)
-       ++h;
-      align = (1 << h);
-
-      if (align < 1)
-       align = 1;
-
-      obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
-    }
-  }
+       ir_type *ty         = get_entity_type(ent);
+       const char *ld_name = get_entity_ld_name(ent);
+       obstack_t *obst     = data_obstack;
+       int align, h;
+       const arch_env_t *arch_env = main_env->arch_env;
+
+       /*
+        * FIXME: did NOT work for partly constant values
+        */
+       if (! is_Method_type(ty)) {
+               ir_variability variability = get_entity_variability(ent);
+               ir_visibility visibility = get_entity_visibility(ent);
+
+               if (variability == variability_constant) {
+                       /* a constant entity, put it on the rdata */
+                       obst = rdata_obstack;
+               }
+
+               /* check, whether it is initialized, if yes create data */
+               if (variability != variability_uninitialized) {
+                       be_dbg_global(main_env->db_handle, obst, ent);
+
+                       if (visibility == visibility_external_visible) {
+                               obstack_printf(obst, ".globl\t%s\n", ld_name);
+                       }
+                       dump_object_size(obst, ld_name, get_type_size_bytes(ty));
+
+                       align = get_type_alignment_bytes(ty);
+                       ia32_dump_align(obst, align);
+
+                       obstack_printf(obst, "%s:\n", ld_name);
+
+                       if (is_atomic_type(ty)) {
+                               if (get_entity_visibility(ent) != visibility_external_allocated)
+                                       dump_atomic_init(obst, get_atomic_ent_value(ent));
+                       }
+                       else {
+                               int i, size = 0;
+
+                               if (ent_is_string_const(ent)) {
+                                       dump_string_cst(obst, ent);
+                               }
+                               else if (is_Array_type(ty)) {
+                                       int filler;
+
+                                       /* potential spare values should be already included! */
+                                       for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
+                                               entity *step = get_compound_ent_value_member(ent, i);
+                                               ir_type *stype = get_entity_type(step);
+
+                                               if (get_type_mode(stype)) {
+                                                       int align = (get_type_alignment_bits(stype) + 7) >> 3;
+                                                       int n     = size % align;
+
+                                                       if (n > 0) {
+                                                               obstack_printf(obst, "\t.zero\t%d\n", align - n);
+                                                               size += align - n;
+                                                       }
+                                               }
+                                               dump_atomic_init(obst, get_compound_ent_value(ent, i));
+                                               size += get_type_size_bytes(stype);
+                                       }
+                                       filler = get_type_size_bytes(ty) - size;
+
+                                       if (filler > 0)
+                                               obstack_printf(obst, "\t.zero\t%d\n", filler);
+                               }
+                               else if (is_compound_type(ty)) {
+                                       ir_node **vals;
+                                       int type_size, j;
+
+                                       /* Compound entities are NOT sorted.
+                                        * The sorting strategy used doesn't work for `value' compound fields nor
+                                        * for partially_constant entities.
+                                        */
+
+                                       /*
+                                        * in the worst case, every entity allocates one byte, so the type
+                                        * size should be equal or bigger the number of fields
+                                        */
+                                       type_size = get_type_size_bytes(ty);
+                                       vals      = xcalloc(type_size, sizeof(*vals));
+
+                                       /* collect the values and store them at the offsets */
+                                       for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
+                                               int                 graph_length, aipos, offset;
+                                               struct arr_info     *ai;
+                                               int                 all_n = 1;
+                                               compound_graph_path *path = get_compound_ent_value_path(ent, i);
+
+                                               /* get the access path to the costant value */
+                                               graph_length = get_compound_graph_path_length(path);
+                                               ai = xcalloc(graph_length, sizeof(struct arr_info));
+
+                                               /* We wanna know how many arrays are on the path to the entity. We also have to know how
+                                                * many elements each array holds to calculate the offset for the entity. */
+                                               for (j = 0; j < graph_length; j++) {
+                                                       entity  *step      = get_compound_graph_path_node(path, j);
+                                                       ir_type *step_type = get_entity_type(step);
+                                                       int     ty_size    = (get_type_size_bits(step_type) + 7) >> 3;
+                                                       int     k, n       = 0;
+
+                                                       if (is_Array_type(step_type))
+                                                               for (k = 0; k < get_array_n_dimensions(step_type); k++)
+                                                                       n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
+                                                               if (n) all_n *= n;
+                                                               ai[j].n_elems = n ? all_n + 1 : 0;
+                                                               ai[j].visit_cnt = 0;
+                                                               ai[j].size = ty_size;
+                                               }
+
+                                               aipos = graph_length - 1;
+                                               if (aipos) aipos--;
+
+                                               for (offset = j = 0; j < graph_length; j++) {
+                                                       entity *step       = get_compound_graph_path_node(path, j);
+                                                       ir_type *step_type = get_entity_type(step);
+                                                       int ent_ofs        = get_entity_offset_bytes(step);
+                                                       int stepsize       = 0;
+
+                                                       /* add all positive offsets (= offsets in structs) */
+                                                       if (ent_ofs >= 0) offset += ent_ofs;
+
+                                                       if (j == graph_length - 1) {
+                                                               stepsize = (get_type_size_bits(step_type) + 7) >> 3;
+
+                                                               /* Search the next free position in vals depending on the information from above (ai). */
+                                                               while (vals[offset] && aipos >= 0) {
+                                                                       if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
+                                                                               offset += stepsize;
+                                                                               ai[aipos].visit_cnt++;
+                                                                       }
+                                                                       else
+                                                                               while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
+                                                                                       stepsize = ai[aipos--].size;
+                                                                                       offset  += stepsize;
+                                                                               }
+                                                               }
+
+                                                               assert(aipos >= 0 && "couldn't store entity");
+                                                               vals[offset] = get_compound_ent_value(ent, i);
+                                                       }
+                                               }
+
+                                               free(ai);
+                                       }
+
+                                       /* now write them sorted */
+                                       for(i = 0; i < type_size; ) {
+                                               if (vals[i]) {
+                                                       dump_atomic_init(obst, vals[i]);
+                                                       i += (get_mode_size_bytes(get_irn_mode(vals[i])));
+                                               }
+                                               else {
+                                                       /* a gap */
+                                                       obstack_printf(obst, "\t.byte\t0\n");
+                                                       ++i;
+                                               }
+                                       }
+                                       free(vals);
+                               }
+                               else {
+                                       assert(0 && "unsupported type");
+                               }
+                       }
+                       obstack_printf(obst, "\n");
+               }
+               else if (visibility != visibility_external_allocated) {
+                       be_dbg_global(main_env->db_handle, comm_obstack, ent);
+
+                       /* uninitialized and NOT external */
+                       if (get_entity_owner(ent) != get_tls_type()) {
+                               /* calculate the alignment */
+                               align = get_type_alignment_bytes(ty);
+                               h = highest_bit(align);
+
+                               if ((1 << h) < align)
+                                       ++h;
+                               align = (1 << h);
+
+                               if (align < 1)
+                                       align = 1;
+
+                               ia32_dump_comm(comm_obstack, ld_name, visibility,
+                                       get_type_size_bytes(ty), align);
+                       } else {
+                               /* TLS */
+                               if (visibility == visibility_external_visible) {
+                                       obstack_printf(comm_obstack, ".globl\t%s\n", ld_name);
+                               }
+                               dump_object_size(comm_obstack, ld_name, get_type_size_bytes(ty));
+                               align = get_type_alignment_bytes(ty);
+                               ia32_dump_align(comm_obstack, align);
+                               obstack_printf(comm_obstack, "%s:\n\t.zero %d\n", ld_name, get_type_size_bytes(ty));
+                       }
+               }
+       } /* ! is method type */
+       else if (ctor_obstack && get_method_img_section(ent) == section_constructors) {
+               ia32_dump_align(ctor_obstack, get_type_alignment_bytes(ty));
+               dump_size_type(ctor_obstack, get_type_alignment_bytes(ty));
+               obstack_printf(ctor_obstack, "%s\n", ld_name);
+       }
 }
 
-/*
+/**
  * Dumps declarations of global variables and the initialization code.
  */
-void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
+static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env)
 {
-  ir_type *gt = get_glob_type();
-  int i, n = get_class_n_members(gt);
+       int i, n = get_compound_n_members(gt);
 
-  for (i = 0; i < n; i++)
-    dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
+       for (i = 0; i < n; i++)
+               dump_global(env->main_env, env->rodata_obst, env->data_obst, env->comm_obst, env->ctor_obst,
+                       get_compound_member(gt, i));
 }
 
 /************************************************************************/
 
-void ia32_gen_decls(FILE *out) {
-  struct obstack rodata, data, comm;
-  int    size;
-  char   *cp;
-
-  obstack_init(&rodata);
-  obstack_init(&data);
-  obstack_init(&comm);
-
-  fprintf(out, "\t.intel_syntax\n");
-
-  ia32_dump_globals(&rodata, &data, &comm);
-
-  size = obstack_object_size(&data);
-  cp   = obstack_finish(&data);
-  if (size > 0) {
-    fprintf(out, "\t.data\n");
-    fwrite(cp, 1, size, out);
-  }
-
-  size = obstack_object_size(&rodata);
-  cp   = obstack_finish(&rodata);
-  if (size > 0) {
-    fprintf(out, "\t.section\t.rodata\n");
-    fwrite(cp, 1, size, out);
-  }
-
-  size = obstack_object_size(&comm);
-  cp   = obstack_finish(&comm);
-  if (size > 0) {
-    fprintf(out, "\t.text\n");
-    fwrite(cp, 1, size, out);
-  }
-
-  obstack_free(&rodata, NULL);
-  obstack_free(&data, NULL);
-  obstack_free(&comm, NULL);
+void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) {
+       ia32_decl_env_t env;
+       obstack_t rodata, data, comm, ctor;
+       int    size;
+       char   *cp;
+
+       /* dump the global type */
+       obstack_init(&rodata);
+       obstack_init(&data);
+       obstack_init(&comm);
+
+       if (main_env->options->opt_profile)
+               obstack_init(&ctor);
+
+       env.rodata_obst = &rodata;
+       env.data_obst   = &data;
+       env.comm_obst   = &comm;
+       env.ctor_obst   = main_env->options->opt_profile ? &ctor : NULL;
+       env.main_env    = main_env;
+
+       ia32_dump_globals(get_glob_type(), &env);
+
+       size = obstack_object_size(&data);
+       cp   = obstack_finish(&data);
+       if (size > 0) {
+               ia32_switch_section(out, SECTION_DATA);
+               fwrite(cp, 1, size, out);
+       }
+
+       size = obstack_object_size(&rodata);
+       cp   = obstack_finish(&rodata);
+       if (size > 0) {
+               ia32_switch_section(out, SECTION_RODATA);
+               fwrite(cp, 1, size, out);
+       }
+
+       size = obstack_object_size(&comm);
+       cp   = obstack_finish(&comm);
+       if (size > 0) {
+               ia32_switch_section(out, SECTION_COMMON);
+               fwrite(cp, 1, size, out);
+       }
+
+       if (main_env->options->opt_profile) {
+               size = obstack_object_size(&ctor);
+               cp   = obstack_finish(&ctor);
+               if (size > 0) {
+                       ia32_switch_section(out, SECTION_CTOR);
+                       fwrite(cp, 1, size, out);
+               }
+               obstack_free(&ctor, NULL);
+       }
+
+       obstack_free(&rodata, NULL);
+       obstack_free(&data, NULL);
+       obstack_free(&comm, NULL);
+
+       /* dump the Thread Local Storage */
+       obstack_init(&data);
+
+       env.rodata_obst = &data;
+       env.data_obst   = &data;
+       env.comm_obst   = &data;
+       env.ctor_obst   = NULL;
+
+       ia32_dump_globals(get_tls_type(), &env);
+
+       size = obstack_object_size(&data);
+       cp   = obstack_finish(&data);
+       if (size > 0) {
+               ia32_switch_section(out, SECTION_TLS);
+               ia32_dump_align_f(out, 32);
+               fwrite(cp, 1, size, out);
+       }
+
+       obstack_free(&data, NULL);
 }