d0ffcada04f76507f95d948909e9105b51287335
[libfirm] / ir / be / beasm_dump_globals.c
1 /**
2  * dumper for initialized and uninitialized global data. based on cggg's implementation.
3  * @author Hannes Jakschitsch
4  * @date 19.01.2005
5  */
6
7 #include <ctype.h>
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include "irprog_t.h"
12 #include "type.h"
13 #include "xmalloc.h"
14
15
16 #include "beasm_dump_globals.h"
17
18
19
20 /*
21  * returns the highest bit value
22  */
23 static unsigned highest_bit(unsigned v)
24 {
25   int res = -1;
26
27   if (v >= (1U << 16U)) {
28     res += 16;
29     v >>= 16;
30   }
31   if (v >= (1U << 8U)) {
32     res += 8;
33     v >>= 8;
34   }
35   if (v >= (1U << 4U)) {
36     res += 4;
37     v >>= 4;
38   }
39   if (v >= (1U << 2U)) {
40     res += 2;
41     v >>= 2;
42   }
43   if (v >= (1U << 1U)) {
44     res += 1;
45     v >>= 1;
46   }
47   if (v >= 1)
48     res += 1;
49
50   return res;
51 }
52
53
54 /**
55  * Determine if an entity is a string constant
56  * @param ent The entity
57  * @return 1 if it is a string constant, 0 otherwise
58  */
59 static int ent_is_string_const(entity *ent)
60 {
61   int res = 0;
62   type *ty;
63
64   ty = get_entity_type(ent);
65
66   /* if it's an array */
67   if (is_Array_type(ty)) {
68     type *elm_ty = get_array_element_type(ty);
69
70     /* and the array's alement type is primitive */
71     if (is_Primitive_type(elm_ty)) {
72       ir_mode *mode = get_type_mode(elm_ty);
73
74       /*
75        * and the mode of the element type is an int of
76        * the same size as the byte mode
77        */
78       if (mode_is_int(mode)
79          && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
80       {
81         int i, c, n;
82
83         n = get_compound_ent_n_values(ent);
84         for (i = 0; i < n; ++i) {
85           ir_node *irn = get_compound_ent_value(ent, i);
86           if(get_irn_opcode(irn) != iro_Const)
87             return 0;
88
89           c = (int) get_tarval_long(get_Const_tarval(irn));
90
91           if((i < n - 1 && !(isgraph(c) || isspace(c)))
92              || (i == n - 1 && c != '\0'))
93             return 0;
94         }
95
96         res = 1;
97       }
98     }
99   }
100   return res;
101 }
102
103
104 /*
105  * dump an atomic value
106  */
107 static void dump_atomic_value( assembler_t *assembler,
108                 asm_segment_t target_segment, ir_node *init)
109 {
110   ir_mode *mode = get_irn_mode(init);
111   int bytes     = get_mode_size_bytes(mode);
112   tarval *tv;
113
114   switch (get_irn_opcode(init)) {
115
116   case iro_Cast:
117     dump_atomic_value(assembler, target_segment, get_Cast_op(init));
118     return;
119
120   case iro_Conv:
121     dump_atomic_value(assembler, target_segment, get_Conv_op(init));
122     return;
123
124   case iro_Const:
125     tv = get_Const_tarval(init);
126
127     /* beware of old stuff */
128     assert(! mode_is_reference(mode));
129
130     /* it's a arithmetic value */
131     assembler->dump_arith_tarval(assembler->private_data, target_segment, tv, bytes);
132     return;
133
134   case iro_SymConst:
135     assembler->dump_symconst(assembler->private_data, target_segment, init);
136   /*
137     switch (get_SymConst_kind(init)) {
138     case symconst_addr_name:
139         assembler->dump_addr_name(assembler->private_data, target_segment, get_id_str(get_SymConst_name(init)));
140       break;
141
142     case symconst_addr_ent:
143         assembler->dump_addr_ent(assembler->private_data, target_segment, get_entity_ld_name(get_SymConst_entity(init)));
144       break;
145
146     case symconst_size:
147         assembler->dump_size(assembler->private_data, target_segment, get_type_size_bytes(get_SymConst_type(init)));
148       break;
149
150     default:
151       assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
152     } */
153     return;
154
155   case iro_Add:
156     dump_atomic_value(assembler, target_segment, get_Add_left(init));
157     assembler->dump_arith_op(assembler->private_data, target_segment, ASM_ARITH_OPERATION_ADD);
158     dump_atomic_value(assembler, target_segment, get_Add_right(init));
159     return;
160
161   case iro_Sub:
162     dump_atomic_value(assembler, target_segment, get_Sub_left(init));
163     assembler->dump_arith_op(assembler, target_segment, ASM_ARITH_OPERATION_SUB);
164     dump_atomic_value(assembler, target_segment, get_Sub_right(init));
165     return;
166
167   case iro_Mul:
168     dump_atomic_value(assembler, target_segment, get_Mul_left(init));
169     assembler->dump_arith_op(assembler, target_segment, ASM_ARITH_OPERATION_MUL);
170     dump_atomic_value(assembler, target_segment, get_Mul_right(init));
171     return;
172
173   default:
174     assert(0 && "dump_atomic_init(): unknown IR-node");
175   }
176 }
177
178
179 /*
180  * dump an atomic value
181  */
182 static void dump_atomic_init(assembler_t* assembler,
183                asm_segment_t target_segment, ir_node *init)
184 {
185
186   ir_mode *mode = get_irn_mode(init);
187   int bytes     = get_mode_size_bytes(mode);
188
189   assembler->dump_atomic_decl(assembler->private_data, target_segment, bytes);
190   dump_atomic_value(assembler, target_segment, init);
191   assembler->dump_newline(assembler->private_data, target_segment);
192 }
193
194
195
196 struct arr_info {
197   int n_elems;
198   int visit_cnt;
199   int size;
200 };
201
202
203
204 /*
205  * Dumps the initialization of global variables that are not
206  * "uninitialized".
207  */
208 static void asm_dump_global ( assembler_t *assembler, entity *ent)
209 {
210   type *ty            = get_entity_type(ent);
211   const char *ld_name = get_entity_ld_name(ent);
212   int align, is_constant, h;
213   int i,j,size = 0;
214
215   asm_segment_t target_segment = ASM_SEGMENT_DATA_INIT;
216
217   /*
218    * FIXME: did NOT work for partly constant values
219    */
220
221   /* ignore methods, they are emitted later */
222   if(is_Method_type(ty))
223           return;
224
225   /* get the properties of the entity */
226   ent_variability variability = get_entity_variability(ent);
227   ent_visibility  visibility  = get_entity_visibility(ent);
228
229   if (variability == variability_constant) {
230      /* a constant entity, put it into the const segment */
231      target_segment = ASM_SEGMENT_CONST;
232   }
233
234   /* check, wether it is initialized, if yes create data */
235   if (variability != variability_uninitialized ) {
236
237 /*      if (visibility == visibility_external_visible) {
238               assembler->dump_external_declaration(assembler->private_data, target_segment, ld_name);
239         //   obstack_printf(obst, ".globl\t%s\n", ld_name);
240         }
241         align = get_type_alignment_bytes(ty);
242
243     //  obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
244     //  obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
245         assembler->dump_declare_object_symbol(assembler->private_data, target_segment, ld_name, (get_type_size_bits(ty) + 7) >> 3);
246
247         align = get_type_alignment_bytes(ty);
248         assembler->dump_align(assembler->private_data, target_segment, align );
249         assembler->dump_object_symbol_init_decl(assembler->private_data, target_segment, ld_name);
250 */
251         align = get_type_alignment_bytes(ty);
252         assembler->dump_declare_initialized_symbol ( assembler->private_data, target_segment, ld_name, (get_type_size_bits(ty)+7)>>3, align, visibility );
253
254
255
256
257         /* dumps of the different entity type initializiations */
258
259         /* atomic types */
260         if (is_atomic_type(ty)) {
261                 if (get_entity_visibility(ent) != visibility_external_allocated)
262                         dump_atomic_init(assembler, target_segment, get_atomic_ent_value(ent));
263         }
264         else if(ent_is_string_const(ent)) {
265                 assembler->dump_string(assembler->private_data, target_segment, ent);
266         }
267         else if(is_Array_type(ty)) {
268
269                 int filler;
270
271         /* potential spare values should be already included! */
272                 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
273                         entity *step = get_compound_ent_value_member(ent, i);
274                         type *stype  = get_entity_type(step);
275
276                         if (get_type_mode(stype)) {
277
278                                 int align = (get_type_alignment_bits(stype) + 7) >> 3;
279                                 int n     = size % align;
280
281                                 if (n > 0) {
282                                         assembler->dump_zero_padding(assembler->private_data, target_segment, align-n);
283                                         size += align - n;
284                                 }
285                         }
286                         dump_atomic_init(assembler, target_segment, get_compound_ent_value(ent, i));
287                         size += get_type_size_bytes(stype);
288                 }
289
290                 filler = get_type_size_bytes(ty) - size;
291
292                 if (filler > 0)
293                         assembler->dump_zero_padding(assembler->private_data, target_segment, filler);
294         }
295         else if (is_compound_type(ty)) {
296
297                 ir_node **vals;
298                 int type_size, j;
299
300                 /* Compound entities are NOT sorted.
301                  * The sorting strategy used doesn't work for `value' compound fields nor
302                  * for partially_constant entities.
303                  */
304
305                 /*
306                  * in the worst case, every entity allocates one byte, so the type
307                  * size should be equal or bigger the number of fields
308                  */
309
310                 type_size = get_type_size_bytes(ty);
311                 vals      = xcalloc(type_size, sizeof(*vals));
312
313                 /* collect the values and store them at the offsets */
314                 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
315                     int graph_length, aipos, offset, stepsize;
316                     struct arr_info *ai;
317                     int found                 = 0;
318                     int all_n                 = 1;
319                     entity *member            = get_compound_ent_value_member(ent, i);
320                     compound_graph_path *path = get_compound_ent_value_path(ent, i);
321                     entity *node              = get_compound_graph_path_node(path, 0);
322                     type *node_type           = get_entity_type(node);
323
324                     /* get the access path to the costant value */
325                     graph_length = get_compound_graph_path_length(path);
326                     ai = xcalloc(graph_length, sizeof(struct arr_info));
327
328                     /* We wanna know how many arrays are on the path to the entity. We also have to know how
329                      * many elements each array holds to calculate the offset for the entity. */
330                     for (j = 0; j < graph_length; j++) {
331                       entity *step    = get_compound_graph_path_node(path, j);
332                       type *step_type = get_entity_type(step);
333                       int ty_size     = (get_type_size_bits(step_type) + 7) >> 3;
334                       int n           = 0;
335                       int k;
336
337                       if (is_Array_type(step_type))
338                         for (k = 0; k < get_array_n_dimensions(step_type); k++)
339                           n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
340                       if (n) all_n *= n;
341                       ai[j].n_elems = n ? all_n + 1 : 0;
342                       ai[j].visit_cnt = 0;
343                       ai[j].size = ty_size;
344                     }
345
346                     aipos = graph_length - 1;
347                     if (aipos) aipos--;
348
349                     for (offset = j = 0; j < graph_length; j++) {
350                       entity *step    = get_compound_graph_path_node(path, j);
351                       type *step_type = get_entity_type(step);
352                       int ent_ofs     = get_entity_offset_bytes(step);
353                       int stepsize    = 0;
354
355                       /* add all positive offsets (= offsets in structs) */
356                       if (ent_ofs >= 0) offset += ent_ofs;
357
358                       if (j == graph_length - 1) {
359                         stepsize = (get_type_size_bits(step_type) + 7) >> 3;
360
361                         /* Search the next free position in vals depending on the information from above (ai). */
362                         while (vals[offset]) {
363                           if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
364                             offset += stepsize;
365                             ai[aipos].visit_cnt++;
366                           }
367                           else
368                             while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
369                               stepsize = ai[aipos--].size;
370                               offset  += stepsize;
371                           }
372                         }
373
374                         assert(aipos >= 0 && "couldn't store entity");
375                         vals[offset] = get_compound_ent_value(ent, i);
376                       }
377                     }
378
379                     free(ai);
380                   }
381
382                   /* now write them sorted */
383                   for(i = 0; i < type_size; ) {
384                     if (vals[i]) {
385                       dump_atomic_init(assembler, target_segment, vals[i]);
386                       i += (get_mode_size_bytes(get_irn_mode(vals[i])));
387                     }
388                     else {
389                       /* a gap */
390                       //obstack_printf(obst, "\t.byte\t0\n");
391                       assembler->dump_zero_padding(assembler->private_data, target_segment, 1);
392                       ++i;
393                     }
394                   }
395                   free(vals);
396                 }
397                 else
398                   assert(0 && "unsupported type");
399
400                 assembler->dump_newline(assembler->private_data, target_segment);
401             }
402
403             /* uninitialized, but allocated here */
404             else {
405
406         /*      if (visibility != visibility_external_allocated) {
407
408                 if (visibility == visibility_local) {
409                         assembler->dump_local_declaration(assembler->private_data, ASM_SEGMENT_COMMON , ld_name);
410                 } */
411
412                 /* calculate the alignment */
413
414            target_segment = ASM_SEGMENT_DATA_UNINIT;
415
416                 align = get_type_alignment_bytes(ty);
417                 h = highest_bit(align);
418
419                 if ((1 << h) < align)
420                         ++h;
421                 align = (1 << h);
422                 if (align < 1)
423                         align = 1;
424
425                 // declare the symbol.
426                 assembler->dump_declare_uninitialized_symbol(assembler->private_data,  target_segment , ld_name, (get_type_size_bits(ty)+7)>>3, align, visibility);
427             }
428 }
429
430
431
432
433 /*
434  * dump the global symbols
435  **/
436
437 void asm_dump_globals ( assembler_t *assembler ) {
438
439   type *gt = get_glob_type();
440   int i, n = get_class_n_members(gt);
441
442   for (i = 0; i < n; i++)
443     asm_dump_global( assembler, get_class_member(gt, i) );
444
445 }