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);
109 * dump an arithmetic tarval
111 static void ppc32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
116 obstack_printf(obst, "\t.byte\t");
120 obstack_printf(obst, "\t.value\t");
124 obstack_printf(obst, "\t.long\t");
128 obstack_printf(obst, "\t.quad\t");
136 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
139 dump_arith_tarval(obst, tv, bytes);
144 * dump an atomic value
146 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
148 ir_mode *mode = get_irn_mode(init);
149 int bytes = get_mode_size_bytes(mode);
152 switch (get_irn_opcode(init)) {
155 do_dump_atomic_init(obst, get_Cast_op(init));
159 do_dump_atomic_init(obst, get_Conv_op(init));
163 tv = get_Const_tarval(init);
165 /* beware of old stuff */
166 assert(! mode_is_reference(mode));
168 /* it's a arithmetic value */
169 dump_arith_tarval(obst, tv, bytes);
173 switch (get_SymConst_kind(init)) {
174 case symconst_addr_name:
175 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
178 case symconst_addr_ent:
179 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
183 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
187 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
192 do_dump_atomic_init(obst, get_Add_left(init));
193 obstack_printf(obst, " + ");
194 do_dump_atomic_init(obst, get_Add_right(init));
198 do_dump_atomic_init(obst, get_Sub_left(init));
199 obstack_printf(obst, " - ");
200 do_dump_atomic_init(obst, get_Sub_right(init));
204 do_dump_atomic_init(obst, get_Mul_left(init));
205 obstack_printf(obst, " * ");
206 do_dump_atomic_init(obst, get_Mul_right(init));
210 assert(0 && "dump_atomic_init(): unknown IR-node");
215 * dump an atomic value
217 static void dump_atomic_init(struct obstack *obst, ir_node *init)
219 ir_mode *mode = get_irn_mode(init);
220 int bytes = get_mode_size_bytes(mode);
225 obstack_printf(obst, "\t.byte\t");
229 obstack_printf(obst, "\t.value\t");
233 obstack_printf(obst, "\t.long\t");
237 obstack_printf(obst, "\t.quad\t");
242 /* handled in arith */
246 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
250 do_dump_atomic_init(obst, init);
251 obstack_printf(obst, "\n");
254 /************************************************************************/
255 /* Routines to dump global variables */
256 /************************************************************************/
259 * Determine if an entity is a string constant
260 * @param ent The entity
261 * @return 1 if it is a string constant, 0 otherwise
263 static int ent_is_string_const(entity *ent)
268 ty = get_entity_type(ent);
270 /* if it's an array */
271 if (is_Array_type(ty)) {
272 ir_type *elm_ty = get_array_element_type(ty);
274 /* and the array's element type is primitive */
275 if (is_Primitive_type(elm_ty)) {
276 ir_mode *mode = get_type_mode(elm_ty);
279 * and the mode of the element type is an int of
280 * the same size as the byte mode
282 if (mode_is_int(mode)
283 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
287 n = get_compound_ent_n_values(ent);
288 for (i = 0; i < n; ++i) {
289 ir_node *irn = get_compound_ent_value(ent, i);
290 if(get_irn_opcode(irn) != iro_Const)
293 c = (int) get_tarval_long(get_Const_tarval(irn));
295 if((i < n - 1 && !(isgraph(c) || isspace(c)))
296 || (i == n - 1 && c != '\0'))
309 * Dump a atring constant.
310 * No checks are made!!
311 * @param obst The obst to dump on.
312 * @param ent The entity to dump.
314 static void dump_string_cst(struct obstack *obst, entity *ent)
318 obstack_printf(obst, "\t.asciz \"");
319 n = get_compound_ent_n_values(ent);
321 for (i = 0; i < n-1; ++i) {
325 irn = get_compound_ent_value(ent, i);
326 c = (int) get_tarval_long(get_Const_tarval(irn));
329 case '"' : obstack_printf(obst, "\\\""); break;
330 case '\n': obstack_printf(obst, "\\n"); break;
331 case '\r': obstack_printf(obst, "\\r"); break;
332 case '\t': obstack_printf(obst, "\\t"); break;
335 obstack_printf(obst, "%c", c);
337 obstack_printf(obst, "%O", c);
341 obstack_printf(obst, "\"\n");
351 * Dumps the initialization of global variables that are not
354 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
356 ir_type *ty = get_entity_type(ent);
357 const char *ld_name = get_entity_ld_name(ent);
359 struct obstack *obst = data_obstack;
362 * FIXME: did NOT work for partly constant values
364 if (! is_Method_type(ty)) {
365 ent_variability variability = get_entity_variability(ent);
366 visibility visibility = get_entity_visibility(ent);
368 if (variability == variability_constant) {
369 /* a constant entity, put it on the rdata */
370 obst = rdata_obstack;
373 /* check, whether it is initialized, if yes create data */
374 if (variability != variability_uninitialized) {
375 if (visibility == visibility_external_visible) {
376 obstack_printf(obst, ".globl\t%s\n", ld_name);
378 // obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
379 // obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
381 align = get_type_alignment_bytes(ty);
382 ppc32_dump_align(obst, align);
384 obstack_printf(obst, "%s:\n", ld_name);
386 if (is_atomic_type(ty)) {
387 if (get_entity_visibility(ent) != visibility_external_allocated)
388 dump_atomic_init(obst, get_atomic_ent_value(ent));
393 if (ent_is_string_const(ent)) {
394 dump_string_cst(obst, ent);
396 else if (is_Array_type(ty)) {
399 /* potential spare values should be already included! */
400 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
401 entity *step = get_compound_ent_value_member(ent, i);
402 ir_type *stype = get_entity_type(step);
404 if (get_type_mode(stype)) {
405 int align = (get_type_alignment_bits(stype) + 7) >> 3;
406 int n = size % align;
409 obstack_printf(obst, "\t.zero\t%d\n", align - n);
413 dump_atomic_init(obst, get_compound_ent_value(ent, i));
414 size += get_type_size_bytes(stype);
416 filler = get_type_size_bytes(ty) - size;
419 obstack_printf(obst, "\t.zero\t%d\n", filler);
421 else if (is_compound_type(ty)) {
425 /* Compound entities are NOT sorted.
426 * The sorting strategy used doesn't work for `value' compound fields nor
427 * for partially_constant entities.
431 * in the worst case, every entity allocates one byte, so the type
432 * size should be equal or bigger the number of fields
434 type_size = get_type_size_bytes(ty);
435 vals = xcalloc(type_size, sizeof(*vals));
437 /* collect the values and store them at the offsets */
438 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
439 int graph_length, aipos, offset;
442 compound_graph_path *path = get_compound_ent_value_path(ent, i);
444 /* get the access path to the costant value */
445 graph_length = get_compound_graph_path_length(path);
446 ai = xcalloc(graph_length, sizeof(struct arr_info));
448 /* We wanna know how many arrays are on the path to the entity. We also have to know how
449 * many elements each array holds to calculate the offset for the entity. */
450 for (j = 0; j < graph_length; j++) {
451 entity *step = get_compound_graph_path_node(path, j);
452 ir_type *step_type = get_entity_type(step);
453 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
456 if (is_Array_type(step_type))
457 for (k = 0; k < get_array_n_dimensions(step_type); k++)
458 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
460 ai[j].n_elems = n ? all_n + 1 : 0;
462 ai[j].size = ty_size;
465 aipos = graph_length - 1;
468 for (offset = j = 0; j < graph_length; j++) {
469 entity *step = get_compound_graph_path_node(path, j);
470 ir_type *step_type = get_entity_type(step);
471 int ent_ofs = get_entity_offset_bytes(step);
474 /* add all positive offsets (= offsets in structs) */
475 if (ent_ofs >= 0) offset += ent_ofs;
477 if (j == graph_length - 1) {
478 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
480 /* Search the next free position in vals depending on the information from above (ai). */
481 while (vals[offset]) {
482 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
484 ai[aipos].visit_cnt++;
487 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
488 stepsize = ai[aipos--].size;
493 assert(aipos >= 0 && "couldn't store entity");
494 vals[offset] = get_compound_ent_value(ent, i);
501 /* now write them sorted */
502 for(i = 0; i < type_size; ) {
504 dump_atomic_init(obst, vals[i]);
505 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
509 obstack_printf(obst, "\t.byte\t0\n");
516 assert(0 && "unsupported type");
519 obstack_printf(obst, "\n");
521 else if (visibility != visibility_external_allocated) {
522 if (visibility == visibility_local) {
523 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
526 /* calculate the alignment */
527 /* align = get_type_alignment_bytes(ty);
528 h = highest_bit(align);
530 if ((1 << h) < align)
537 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);*/
538 obstack_printf(comm_obstack, "\t.comm\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
544 * Dumps declarations of global variables and the initialization code.
546 void ppc32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
548 ir_type *gt = get_glob_type();
549 int i, n = get_class_n_members(gt);
551 for (i = 0; i < n; i++)
552 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
555 void ppc32_dump_indirect_symbols(struct obstack *isyms)
558 for(ent=pset_first(symbol_pset); ent; ent=pset_next(symbol_pset))
560 const char *ld_name = get_entity_ld_name(ent);
561 obstack_printf(isyms, ".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n",ld_name,ld_name);
565 /************************************************************************/
567 void ppc32_gen_decls(FILE *out) {
568 struct obstack rodata, data, comm, isyms;
572 obstack_init(&rodata);
576 ppc32_dump_globals(&rodata, &data, &comm);
578 size = obstack_object_size(&data);
579 cp = obstack_finish(&data);
581 fprintf(out, "\t.data\n");
582 fwrite(cp, 1, size, out);
585 size = obstack_object_size(&rodata);
586 cp = obstack_finish(&rodata);
588 fprintf(out, "\t.const_data\n");
589 fwrite(cp, 1, size, out);
592 size = obstack_object_size(&comm);
593 cp = obstack_finish(&comm);
595 // fprintf(out, "\t.common\n");
596 fwrite(cp, 1, size, out);
600 obstack_free(&rodata, NULL);
601 obstack_free(&data, NULL);
602 obstack_free(&comm, NULL);
604 obstack_init(&isyms);
606 ppc32_dump_indirect_symbols(&isyms);
608 size = obstack_object_size(&isyms);
609 cp = obstack_finish(&isyms);
611 fprintf(out, "\t.data\n");
612 fwrite(cp, 1, size, out);
615 obstack_free(&isyms,NULL);