Set dump consts local to false for schedule dumping
[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   ent_variability variability;
215   visibility  visibility;
216
217   asm_segment_t target_segment = ASM_SEGMENT_DATA_INIT;
218
219   /*
220    * FIXME: did NOT work for partly constant values
221    */
222
223   /* ignore methods, they are emitted later */
224   if (is_Method_type(ty))
225           return;
226
227   /* get the properties of the entity */
228   variability = get_entity_variability(ent);
229   visibility  = get_entity_visibility(ent);
230
231   if (variability == variability_constant) {
232      /* a constant entity, put it into the const segment */
233      target_segment = ASM_SEGMENT_CONST;
234   }
235
236   /* check, whether it is initialized, if yes create data */
237   if (variability != variability_uninitialized ) {
238
239 /*      if (visibility == visibility_external_visible) {
240               assembler->dump_external_declaration(assembler->private_data, target_segment, ld_name);
241         //   obstack_printf(obst, ".globl\t%s\n", ld_name);
242         }
243         align = get_type_alignment_bytes(ty);
244
245     //  obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
246     //  obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
247         assembler->dump_declare_object_symbol(assembler->private_data, target_segment, ld_name, (get_type_size_bits(ty) + 7) >> 3);
248
249         align = get_type_alignment_bytes(ty);
250         assembler->dump_align(assembler->private_data, target_segment, align );
251         assembler->dump_object_symbol_init_decl(assembler->private_data, target_segment, ld_name);
252 */
253         align = get_type_alignment_bytes(ty);
254         assembler->dump_declare_initialized_symbol ( assembler->private_data, target_segment, ld_name, (get_type_size_bits(ty)+7)>>3, align, visibility );
255
256
257
258
259         /* dumps of the different entity type initializiations */
260
261         /* atomic types */
262         if (is_atomic_type(ty)) {
263                 if (get_entity_visibility(ent) != visibility_external_allocated)
264                         dump_atomic_init(assembler, target_segment, get_atomic_ent_value(ent));
265         }
266         else if(ent_is_string_const(ent)) {
267                 assembler->dump_string(assembler->private_data, target_segment, ent);
268         }
269         else if(is_Array_type(ty)) {
270
271                 int filler;
272
273         /* potential spare values should be already included! */
274                 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
275                         entity *step = get_compound_ent_value_member(ent, i);
276                         type *stype  = get_entity_type(step);
277
278                         if (get_type_mode(stype)) {
279
280                                 int align = (get_type_alignment_bits(stype) + 7) >> 3;
281                                 int n     = size % align;
282
283                                 if (n > 0) {
284                                         assembler->dump_zero_padding(assembler->private_data, target_segment, align-n);
285                                         size += align - n;
286                                 }
287                         }
288                         dump_atomic_init(assembler, target_segment, get_compound_ent_value(ent, i));
289                         size += get_type_size_bytes(stype);
290                 }
291
292                 filler = get_type_size_bytes(ty) - size;
293
294                 if (filler > 0)
295                         assembler->dump_zero_padding(assembler->private_data, target_segment, filler);
296         }
297         else if (is_compound_type(ty)) {
298
299                 ir_node **vals;
300                 int type_size, j;
301
302                 /* Compound entities are NOT sorted.
303                  * The sorting strategy used doesn't work for `value' compound fields nor
304                  * for partially_constant entities.
305                  */
306
307                 /*
308                  * in the worst case, every entity allocates one byte, so the type
309                  * size should be equal or bigger the number of fields
310                  */
311
312                 type_size = get_type_size_bytes(ty);
313                 vals      = xcalloc(type_size, sizeof(*vals));
314
315                 /* collect the values and store them at the offsets */
316                 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
317                   int graph_length, aipos, offset/*, stepsize*/;
318                     struct arr_info *ai;
319                     /*int found                 = 0;*/
320                     int all_n                 = 1;
321                     /*entity *member            = get_compound_ent_value_member(ent, i); */
322                     compound_graph_path *path = get_compound_ent_value_path(ent, i);
323                     entity *node              = get_compound_graph_path_node(path, 0);
324                     /*type *node_type           = get_entity_type(node);*/
325
326                     /* get the access path to the costant value */
327                     graph_length = get_compound_graph_path_length(path);
328                     ai = xcalloc(graph_length, sizeof(struct arr_info));
329
330                     /* We wanna know how many arrays are on the path to the entity. We also have to know how
331                      * many elements each array holds to calculate the offset for the entity. */
332                     for (j = 0; j < graph_length; j++) {
333                       entity *step    = get_compound_graph_path_node(path, j);
334                       type *step_type = get_entity_type(step);
335                       int ty_size     = (get_type_size_bits(step_type) + 7) >> 3;
336                       int n           = 0;
337                       int k;
338
339                       if (is_Array_type(step_type))
340                         for (k = 0; k < get_array_n_dimensions(step_type); k++)
341                           n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
342                       if (n) all_n *= n;
343                       ai[j].n_elems = n ? all_n + 1 : 0;
344                       ai[j].visit_cnt = 0;
345                       ai[j].size = ty_size;
346                     }
347
348                     aipos = graph_length - 1;
349                     if (aipos) aipos--;
350
351                     for (offset = j = 0; j < graph_length; j++) {
352                       entity *step    = get_compound_graph_path_node(path, j);
353                       type *step_type = get_entity_type(step);
354                       int ent_ofs     = get_entity_offset_bytes(step);
355                       int stepsize    = 0;
356
357                       /* add all positive offsets (= offsets in structs) */
358                       if (ent_ofs >= 0) offset += ent_ofs;
359
360                       if (j == graph_length - 1) {
361                         stepsize = (get_type_size_bits(step_type) + 7) >> 3;
362
363                         /* Search the next free position in vals depending on the information from above (ai). */
364                         while (vals[offset]) {
365                           if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
366                             offset += stepsize;
367                             ai[aipos].visit_cnt++;
368                           }
369                           else
370                             while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
371                               stepsize = ai[aipos--].size;
372                               offset  += stepsize;
373                           }
374                         }
375
376                         assert(aipos >= 0 && "couldn't store entity");
377                         vals[offset] = get_compound_ent_value(ent, i);
378                       }
379                     }
380
381                     free(ai);
382                   }
383
384                   /* now write them sorted */
385                   for(i = 0; i < type_size; ) {
386                     if (vals[i]) {
387                       dump_atomic_init(assembler, target_segment, vals[i]);
388                       i += (get_mode_size_bytes(get_irn_mode(vals[i])));
389                     }
390                     else {
391                       /* a gap */
392                       //obstack_printf(obst, "\t.byte\t0\n");
393                       assembler->dump_zero_padding(assembler->private_data, target_segment, 1);
394                       ++i;
395                     }
396                   }
397                   free(vals);
398                 }
399                 else
400                   assert(0 && "unsupported type");
401
402                 assembler->dump_newline(assembler->private_data, target_segment);
403             }
404
405             /* uninitialized, but allocated here */
406             else {
407
408         /*      if (visibility != visibility_external_allocated) {
409
410                 if (visibility == visibility_local) {
411                         assembler->dump_local_declaration(assembler->private_data, ASM_SEGMENT_COMMON , ld_name);
412                 } */
413
414                 /* calculate the alignment */
415
416            target_segment = ASM_SEGMENT_DATA_UNINIT;
417
418                 align = get_type_alignment_bytes(ty);
419                 h = highest_bit(align);
420
421                 if ((1 << h) < align)
422                         ++h;
423                 align = (1 << h);
424                 if (align < 1)
425                         align = 1;
426
427                 // declare the symbol.
428                 assembler->dump_declare_uninitialized_symbol(assembler->private_data,  target_segment , ld_name, (get_type_size_bits(ty)+7)>>3, align, visibility);
429             }
430 }
431
432
433
434
435 /*
436  * dump the global symbols
437  **/
438
439 void asm_dump_globals ( assembler_t *assembler ) {
440
441   type *gt = get_glob_type();
442   int i, n = get_class_n_members(gt);
443
444   for (i = 0; i < n; i++)
445     asm_dump_global( assembler, get_class_member(gt, i) );
446
447 }