2 * Dumps global variables and constants as ppc assembler.
21 #include "ppc32_gen_decls.h"
23 extern pset *symbol_pset;
25 /************************************************************************/
28 * returns the highest bit value
30 static unsigned highest_bit(unsigned v)
34 if (v >= (1U << 16U)) {
38 if (v >= (1U << 8U)) {
42 if (v >= (1U << 4U)) {
46 if (v >= (1U << 2U)) {
50 if (v >= (1U << 1U)) {
61 * output the alignment
63 static void ppc32_dump_align(struct obstack *obst, int align)
65 int h = highest_bit(align);
72 obstack_printf(obst, "\t.align %d\n", align);
75 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
80 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
84 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
88 obstack_printf(obst, "0x%02x%02x%02x%02x",
89 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
93 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
94 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
95 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
103 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
110 * dump an arithmetic tarval
112 static void ppc32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
117 obstack_printf(obst, "\t.byte\t");
121 obstack_printf(obst, "\t.value\t");
125 obstack_printf(obst, "\t.long\t");
129 obstack_printf(obst, "\t.quad\t");
137 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
140 dump_arith_tarval(obst, tv, bytes);
145 * dump an atomic value
147 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
149 ir_mode *mode = get_irn_mode(init);
150 int bytes = get_mode_size_bytes(mode);
153 switch (get_irn_opcode(init)) {
156 do_dump_atomic_init(obst, get_Cast_op(init));
160 do_dump_atomic_init(obst, get_Conv_op(init));
164 tv = get_Const_tarval(init);
166 /* beware of old stuff */
167 assert(! mode_is_reference(mode));
169 /* it's a arithmetic value */
170 dump_arith_tarval(obst, tv, bytes);
174 switch (get_SymConst_kind(init)) {
175 case symconst_addr_name:
176 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
179 case symconst_addr_ent:
180 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
183 case symconst_ofs_ent:
184 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
187 case symconst_type_size:
188 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
191 case symconst_type_align:
192 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
195 case symconst_enum_const:
196 tv = get_enumeration_value(get_SymConst_enum(init));
197 dump_arith_tarval(obst, tv, bytes);
201 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
206 do_dump_atomic_init(obst, get_Add_left(init));
207 obstack_printf(obst, " + ");
208 do_dump_atomic_init(obst, get_Add_right(init));
212 do_dump_atomic_init(obst, get_Sub_left(init));
213 obstack_printf(obst, " - ");
214 do_dump_atomic_init(obst, get_Sub_right(init));
218 do_dump_atomic_init(obst, get_Mul_left(init));
219 obstack_printf(obst, " * ");
220 do_dump_atomic_init(obst, get_Mul_right(init));
224 assert(0 && "dump_atomic_init(): unknown IR-node");
229 * dump an atomic value
231 static void dump_atomic_init(struct obstack *obst, ir_node *init)
233 ir_mode *mode = get_irn_mode(init);
234 int bytes = get_mode_size_bytes(mode);
239 obstack_printf(obst, "\t.byte\t");
243 obstack_printf(obst, "\t.value\t");
247 obstack_printf(obst, "\t.long\t");
251 obstack_printf(obst, "\t.quad\t");
256 /* handled in arith */
260 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
264 do_dump_atomic_init(obst, init);
265 obstack_printf(obst, "\n");
268 /************************************************************************/
269 /* Routines to dump global variables */
270 /************************************************************************/
273 * Determine if an entity is a string constant
274 * @param ent The entity
275 * @return 1 if it is a string constant, 0 otherwise
277 static int ent_is_string_const(ir_entity *ent)
282 ty = get_entity_type(ent);
284 /* if it's an array */
285 if (is_Array_type(ty)) {
286 ir_type *elm_ty = get_array_element_type(ty);
288 /* and the array's element type is primitive */
289 if (is_Primitive_type(elm_ty)) {
290 ir_mode *mode = get_type_mode(elm_ty);
293 * and the mode of the element type is an int of
294 * the same size as the byte mode
296 if (mode_is_int(mode)
297 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
301 n = get_compound_ent_n_values(ent);
302 for (i = 0; i < n; ++i) {
303 ir_node *irn = get_compound_ent_value(ent, i);
304 if(get_irn_opcode(irn) != iro_Const)
307 c = (int) get_tarval_long(get_Const_tarval(irn));
309 if((i < n - 1 && !(isgraph(c) || isspace(c)))
310 || (i == n - 1 && c != '\0'))
323 * Dump a atring constant.
324 * No checks are made!!
325 * @param obst The obst to dump on.
326 * @param ent The entity to dump.
328 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
332 obstack_printf(obst, "\t.asciz \"");
333 n = get_compound_ent_n_values(ent);
335 for (i = 0; i < n-1; ++i) {
339 irn = get_compound_ent_value(ent, i);
340 c = (int) get_tarval_long(get_Const_tarval(irn));
343 case '"' : obstack_printf(obst, "\\\""); break;
344 case '\n': obstack_printf(obst, "\\n"); break;
345 case '\r': obstack_printf(obst, "\\r"); break;
346 case '\t': obstack_printf(obst, "\\t"); break;
349 obstack_printf(obst, "%c", c);
351 obstack_printf(obst, "%O", c);
355 obstack_printf(obst, "\"\n");
365 * Dumps the initialization of global variables that are not
368 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
370 ir_type *ty = get_entity_type(ent);
371 const char *ld_name = get_entity_ld_name(ent);
373 struct obstack *obst = data_obstack;
376 * FIXME: did NOT work for partly constant values
378 if (! is_Method_type(ty)) {
379 ir_variability variability = get_entity_variability(ent);
380 ir_visibility visibility = get_entity_visibility(ent);
382 if (variability == variability_constant) {
383 /* a constant entity, put it on the rdata */
384 obst = rdata_obstack;
387 /* check, whether it is initialized, if yes create data */
388 if (variability != variability_uninitialized) {
389 if (visibility == visibility_external_visible) {
390 obstack_printf(obst, ".globl\t%s\n", ld_name);
392 // obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
393 // obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
395 align = get_type_alignment_bytes(ty);
396 ppc32_dump_align(obst, align);
398 obstack_printf(obst, "%s:\n", ld_name);
400 if (is_atomic_type(ty)) {
401 if (get_entity_visibility(ent) != visibility_external_allocated)
402 dump_atomic_init(obst, get_atomic_ent_value(ent));
407 if (ent_is_string_const(ent)) {
408 dump_string_cst(obst, ent);
410 else if (is_Array_type(ty)) {
413 /* potential spare values should be already included! */
414 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
415 ir_entity *step = get_compound_ent_value_member(ent, i);
416 ir_type *stype = get_entity_type(step);
418 if (get_type_mode(stype)) {
419 int align = (get_type_alignment_bits(stype) + 7) >> 3;
420 int n = size % align;
423 obstack_printf(obst, "\t.zero\t%d\n", align - n);
427 dump_atomic_init(obst, get_compound_ent_value(ent, i));
428 size += get_type_size_bytes(stype);
430 filler = get_type_size_bytes(ty) - size;
433 obstack_printf(obst, "\t.zero\t%d\n", filler);
435 else if (is_compound_type(ty)) {
439 /* Compound entities are NOT sorted.
440 * The sorting strategy used doesn't work for `value' compound fields nor
441 * for partially_constant entities.
445 * in the worst case, every entity allocates one byte, so the type
446 * size should be equal or bigger the number of fields
448 type_size = get_type_size_bytes(ty);
449 vals = xcalloc(type_size, sizeof(*vals));
451 /* collect the values and store them at the offsets */
452 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
453 int graph_length, aipos, offset;
456 compound_graph_path *path = get_compound_ent_value_path(ent, i);
458 /* get the access path to the costant value */
459 graph_length = get_compound_graph_path_length(path);
460 ai = xcalloc(graph_length, sizeof(struct arr_info));
462 /* We wanna know how many arrays are on the path to the entity. We also have to know how
463 * many elements each array holds to calculate the offset for the entity. */
464 for (j = 0; j < graph_length; j++) {
465 ir_entity *step = get_compound_graph_path_node(path, j);
466 ir_type *step_type = get_entity_type(step);
467 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
470 if (is_Array_type(step_type))
471 for (k = 0; k < get_array_n_dimensions(step_type); k++)
472 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
474 ai[j].n_elems = n ? all_n + 1 : 0;
476 ai[j].size = ty_size;
479 aipos = graph_length - 1;
482 for (offset = j = 0; j < graph_length; j++) {
483 ir_entity *step = get_compound_graph_path_node(path, j);
484 ir_type *step_type = get_entity_type(step);
485 int ent_ofs = get_entity_offset(step);
488 /* add all positive offsets (= offsets in structs) */
489 if (ent_ofs >= 0) offset += ent_ofs;
491 if (j == graph_length - 1) {
492 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
494 /* Search the next free position in vals depending on the information from above (ai). */
495 while (vals[offset]) {
496 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
498 ai[aipos].visit_cnt++;
501 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
502 stepsize = ai[aipos--].size;
507 assert(aipos >= 0 && "couldn't store entity");
508 vals[offset] = get_compound_ent_value(ent, i);
515 /* now write them sorted */
516 for(i = 0; i < type_size; ) {
518 dump_atomic_init(obst, vals[i]);
519 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
523 obstack_printf(obst, "\t.byte\t0\n");
530 assert(0 && "unsupported type");
533 obstack_printf(obst, "\n");
535 else if (visibility != visibility_external_allocated) {
536 if (visibility == visibility_local) {
537 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
540 /* calculate the alignment */
541 /* align = get_type_alignment_bytes(ty);
542 h = highest_bit(align);
544 if ((1 << h) < align)
551 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);*/
552 obstack_printf(comm_obstack, "\t.comm\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
558 * Dumps declarations of global variables and the initialization code.
560 void ppc32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
562 ir_type *gt = get_glob_type();
563 int i, n = get_class_n_members(gt);
565 for (i = 0; i < n; i++)
566 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
569 void ppc32_dump_indirect_symbols(struct obstack *isyms)
573 foreach_pset(symbol_pset, ent) {
574 const char *ld_name = get_entity_ld_name(ent);
575 obstack_printf(isyms, ".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n",ld_name,ld_name);
579 /************************************************************************/
581 void ppc32_gen_decls(FILE *out) {
582 struct obstack rodata, data, comm, isyms;
586 obstack_init(&rodata);
590 ppc32_dump_globals(&rodata, &data, &comm);
592 size = obstack_object_size(&data);
593 cp = obstack_finish(&data);
595 fprintf(out, "\t.data\n");
596 fwrite(cp, 1, size, out);
599 size = obstack_object_size(&rodata);
600 cp = obstack_finish(&rodata);
602 fprintf(out, "\t.const_data\n");
603 fwrite(cp, 1, size, out);
606 size = obstack_object_size(&comm);
607 cp = obstack_finish(&comm);
609 // fprintf(out, "\t.common\n");
610 fwrite(cp, 1, size, out);
614 obstack_free(&rodata, NULL);
615 obstack_free(&data, NULL);
616 obstack_free(&comm, NULL);
618 obstack_init(&isyms);
620 ppc32_dump_indirect_symbols(&isyms);
622 size = obstack_object_size(&isyms);
623 cp = obstack_finish(&isyms);
625 fprintf(out, "\t.data\n");
626 fwrite(cp, 1, size, out);
629 obstack_free(&isyms,NULL);