2 * Dumps global variables and constants as ppc assembler.
15 #ifdef obstack_chunk_alloc
16 # undef obstack_chunk_alloc
17 # define obstack_chunk_alloc xmalloc
19 # define obstack_chunk_alloc xmalloc
20 # define obstack_chunk_free free
23 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
31 #include "ppc32_gen_decls.h"
33 extern pset *symbol_pset;
35 /************************************************************************/
38 * returns the highest bit value
40 static unsigned highest_bit(unsigned v)
44 if (v >= (1U << 16U)) {
48 if (v >= (1U << 8U)) {
52 if (v >= (1U << 4U)) {
56 if (v >= (1U << 2U)) {
60 if (v >= (1U << 1U)) {
71 * output the alignment
73 static void ppc32_dump_align(struct obstack *obst, int align)
75 int h = highest_bit(align);
82 obstack_printf(obst, "\t.align %d\n", align);
85 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
90 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
94 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
98 obstack_printf(obst, "0x%02x%02x%02x%02x",
99 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 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
104 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
105 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
113 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
119 * dump an arithmetic tarval
121 static void ppc32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
126 obstack_printf(obst, "\t.byte\t");
130 obstack_printf(obst, "\t.value\t");
134 obstack_printf(obst, "\t.long\t");
138 obstack_printf(obst, "\t.quad\t");
146 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
149 dump_arith_tarval(obst, tv, bytes);
154 * dump an atomic value
156 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
158 ir_mode *mode = get_irn_mode(init);
159 int bytes = get_mode_size_bytes(mode);
162 switch (get_irn_opcode(init)) {
165 do_dump_atomic_init(obst, get_Cast_op(init));
169 do_dump_atomic_init(obst, get_Conv_op(init));
173 tv = get_Const_tarval(init);
175 /* beware of old stuff */
176 assert(! mode_is_reference(mode));
178 /* it's a arithmetic value */
179 dump_arith_tarval(obst, tv, bytes);
183 switch (get_SymConst_kind(init)) {
184 case symconst_addr_name:
185 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
188 case symconst_addr_ent:
189 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
193 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
197 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
202 do_dump_atomic_init(obst, get_Add_left(init));
203 obstack_printf(obst, " + ");
204 do_dump_atomic_init(obst, get_Add_right(init));
208 do_dump_atomic_init(obst, get_Sub_left(init));
209 obstack_printf(obst, " - ");
210 do_dump_atomic_init(obst, get_Sub_right(init));
214 do_dump_atomic_init(obst, get_Mul_left(init));
215 obstack_printf(obst, " * ");
216 do_dump_atomic_init(obst, get_Mul_right(init));
220 assert(0 && "dump_atomic_init(): unknown IR-node");
225 * dump an atomic value
227 static void dump_atomic_init(struct obstack *obst, ir_node *init)
229 ir_mode *mode = get_irn_mode(init);
230 int bytes = get_mode_size_bytes(mode);
235 obstack_printf(obst, "\t.byte\t");
239 obstack_printf(obst, "\t.value\t");
243 obstack_printf(obst, "\t.long\t");
247 obstack_printf(obst, "\t.quad\t");
252 /* handled in arith */
256 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
260 do_dump_atomic_init(obst, init);
261 obstack_printf(obst, "\n");
264 /************************************************************************/
265 /* Routines to dump global variables */
266 /************************************************************************/
269 * Determine if an entity is a string constant
270 * @param ent The entity
271 * @return 1 if it is a string constant, 0 otherwise
273 static int ent_is_string_const(entity *ent)
278 ty = get_entity_type(ent);
280 /* if it's an array */
281 if (is_Array_type(ty)) {
282 ir_type *elm_ty = get_array_element_type(ty);
284 /* and the array's element type is primitive */
285 if (is_Primitive_type(elm_ty)) {
286 ir_mode *mode = get_type_mode(elm_ty);
289 * and the mode of the element type is an int of
290 * the same size as the byte mode
292 if (mode_is_int(mode)
293 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
297 n = get_compound_ent_n_values(ent);
298 for (i = 0; i < n; ++i) {
299 ir_node *irn = get_compound_ent_value(ent, i);
300 if(get_irn_opcode(irn) != iro_Const)
303 c = (int) get_tarval_long(get_Const_tarval(irn));
305 if((i < n - 1 && !(isgraph(c) || isspace(c)))
306 || (i == n - 1 && c != '\0'))
319 * Dump a atring constant.
320 * No checks are made!!
321 * @param obst The obst to dump on.
322 * @param ent The entity to dump.
324 static void dump_string_cst(struct obstack *obst, entity *ent)
328 obstack_printf(obst, "\t.asciz \"");
329 n = get_compound_ent_n_values(ent);
331 for (i = 0; i < n-1; ++i) {
335 irn = get_compound_ent_value(ent, i);
336 c = (int) get_tarval_long(get_Const_tarval(irn));
339 case '"' : obstack_printf(obst, "\\\""); break;
340 case '\n': obstack_printf(obst, "\\n"); break;
341 case '\r': obstack_printf(obst, "\\r"); break;
342 case '\t': obstack_printf(obst, "\\t"); break;
345 obstack_printf(obst, "%c", c);
347 obstack_printf(obst, "%O", c);
351 obstack_printf(obst, "\"\n");
361 * Dumps the initialization of global variables that are not
364 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
366 ir_type *ty = get_entity_type(ent);
367 const char *ld_name = get_entity_ld_name(ent);
369 struct obstack *obst = data_obstack;
372 * FIXME: did NOT work for partly constant values
374 if (! is_Method_type(ty)) {
375 ent_variability variability = get_entity_variability(ent);
376 visibility visibility = get_entity_visibility(ent);
378 if (variability == variability_constant) {
379 /* a constant entity, put it on the rdata */
380 obst = rdata_obstack;
383 /* check, whether it is initialized, if yes create data */
384 if (variability != variability_uninitialized) {
385 if (visibility == visibility_external_visible) {
386 obstack_printf(obst, ".globl\t%s\n", ld_name);
388 // obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
389 // obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
391 align = get_type_alignment_bytes(ty);
392 ppc32_dump_align(obst, align);
394 obstack_printf(obst, "%s:\n", ld_name);
396 if (is_atomic_type(ty)) {
397 if (get_entity_visibility(ent) != visibility_external_allocated)
398 dump_atomic_init(obst, get_atomic_ent_value(ent));
403 if (ent_is_string_const(ent)) {
404 dump_string_cst(obst, ent);
406 else if (is_Array_type(ty)) {
409 /* potential spare values should be already included! */
410 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
411 entity *step = get_compound_ent_value_member(ent, i);
412 ir_type *stype = get_entity_type(step);
414 if (get_type_mode(stype)) {
415 int align = (get_type_alignment_bits(stype) + 7) >> 3;
416 int n = size % align;
419 obstack_printf(obst, "\t.zero\t%d\n", align - n);
423 dump_atomic_init(obst, get_compound_ent_value(ent, i));
424 size += get_type_size_bytes(stype);
426 filler = get_type_size_bytes(ty) - size;
429 obstack_printf(obst, "\t.zero\t%d\n", filler);
431 else if (is_compound_type(ty)) {
435 /* Compound entities are NOT sorted.
436 * The sorting strategy used doesn't work for `value' compound fields nor
437 * for partially_constant entities.
441 * in the worst case, every entity allocates one byte, so the type
442 * size should be equal or bigger the number of fields
444 type_size = get_type_size_bytes(ty);
445 vals = xcalloc(type_size, sizeof(*vals));
447 /* collect the values and store them at the offsets */
448 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
449 int graph_length, aipos, offset;
452 compound_graph_path *path = get_compound_ent_value_path(ent, i);
454 /* get the access path to the costant value */
455 graph_length = get_compound_graph_path_length(path);
456 ai = xcalloc(graph_length, sizeof(struct arr_info));
458 /* We wanna know how many arrays are on the path to the entity. We also have to know how
459 * many elements each array holds to calculate the offset for the entity. */
460 for (j = 0; j < graph_length; j++) {
461 entity *step = get_compound_graph_path_node(path, j);
462 ir_type *step_type = get_entity_type(step);
463 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
466 if (is_Array_type(step_type))
467 for (k = 0; k < get_array_n_dimensions(step_type); k++)
468 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
470 ai[j].n_elems = n ? all_n + 1 : 0;
472 ai[j].size = ty_size;
475 aipos = graph_length - 1;
478 for (offset = j = 0; j < graph_length; j++) {
479 entity *step = get_compound_graph_path_node(path, j);
480 ir_type *step_type = get_entity_type(step);
481 int ent_ofs = get_entity_offset_bytes(step);
484 /* add all positive offsets (= offsets in structs) */
485 if (ent_ofs >= 0) offset += ent_ofs;
487 if (j == graph_length - 1) {
488 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
490 /* Search the next free position in vals depending on the information from above (ai). */
491 while (vals[offset]) {
492 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
494 ai[aipos].visit_cnt++;
497 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
498 stepsize = ai[aipos--].size;
503 assert(aipos >= 0 && "couldn't store entity");
504 vals[offset] = get_compound_ent_value(ent, i);
511 /* now write them sorted */
512 for(i = 0; i < type_size; ) {
514 dump_atomic_init(obst, vals[i]);
515 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
519 obstack_printf(obst, "\t.byte\t0\n");
526 assert(0 && "unsupported type");
529 obstack_printf(obst, "\n");
531 else if (visibility != visibility_external_allocated) {
532 if (visibility == visibility_local) {
533 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
536 /* calculate the alignment */
537 /* align = get_type_alignment_bytes(ty);
538 h = highest_bit(align);
540 if ((1 << h) < align)
547 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);*/
548 obstack_printf(comm_obstack, "\t.comm\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
554 * Dumps declarations of global variables and the initialization code.
556 void ppc32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
558 ir_type *gt = get_glob_type();
559 int i, n = get_class_n_members(gt);
561 for (i = 0; i < n; i++)
562 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
565 void ppc32_dump_indirect_symbols(struct obstack *isyms)
568 for(ent=pset_first(symbol_pset); ent; ent=pset_next(symbol_pset))
570 const char *ld_name = get_entity_ld_name(ent);
571 obstack_printf(isyms, ".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n",ld_name,ld_name);
575 /************************************************************************/
577 void ppc32_gen_decls(FILE *out) {
578 struct obstack rodata, data, comm, isyms;
582 obstack_init(&rodata);
586 ppc32_dump_globals(&rodata, &data, &comm);
588 size = obstack_object_size(&data);
589 cp = obstack_finish(&data);
591 fprintf(out, "\t.data\n");
592 fwrite(cp, 1, size, out);
595 size = obstack_object_size(&rodata);
596 cp = obstack_finish(&rodata);
598 fprintf(out, "\t.const_data\n");
599 fwrite(cp, 1, size, out);
602 size = obstack_object_size(&comm);
603 cp = obstack_finish(&comm);
605 // fprintf(out, "\t.common\n");
606 fwrite(cp, 1, size, out);
610 obstack_free(&rodata, NULL);
611 obstack_free(&data, NULL);
612 obstack_free(&comm, NULL);
614 obstack_init(&isyms);
616 ppc32_dump_indirect_symbols(&isyms);
618 size = obstack_object_size(&isyms);
619 cp = obstack_finish(&isyms);
621 fprintf(out, "\t.data\n");
622 fwrite(cp, 1, size, out);
625 obstack_free(&isyms,NULL);