2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Dumps global variables and constants as ppc assembler.
23 * @author Moritz Kroll, Jens Mueller
45 #include "ppc32_gen_decls.h"
47 extern pset *symbol_pset;
49 /************************************************************************/
52 * returns the highest bit value
54 static unsigned highest_bit(unsigned v)
58 if (v >= (1U << 16U)) {
62 if (v >= (1U << 8U)) {
66 if (v >= (1U << 4U)) {
70 if (v >= (1U << 2U)) {
74 if (v >= (1U << 1U)) {
85 * output the alignment
87 static void ppc32_dump_align(struct obstack *obst, int align)
89 int h = highest_bit(align);
96 obstack_printf(obst, "\t.align %d\n", align);
99 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
104 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
108 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
112 obstack_printf(obst, "0x%02x%02x%02x%02x",
113 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
117 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
118 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
119 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
127 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
134 * dump an arithmetic tarval
136 static void ppc32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
141 obstack_printf(obst, "\t.byte\t");
145 obstack_printf(obst, "\t.value\t");
149 obstack_printf(obst, "\t.long\t");
153 obstack_printf(obst, "\t.quad\t");
161 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
164 dump_arith_tarval(obst, tv, bytes);
169 * dump an atomic value
171 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
173 ir_mode *mode = get_irn_mode(init);
174 int bytes = get_mode_size_bytes(mode);
177 switch (get_irn_opcode(init)) {
180 do_dump_atomic_init(obst, get_Cast_op(init));
184 do_dump_atomic_init(obst, get_Conv_op(init));
188 tv = get_Const_tarval(init);
190 /* beware of old stuff */
191 assert(! mode_is_reference(mode));
193 /* it's a arithmetic value */
194 dump_arith_tarval(obst, tv, bytes);
198 switch (get_SymConst_kind(init)) {
199 case symconst_addr_name:
200 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
203 case symconst_addr_ent:
204 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
207 case symconst_ofs_ent:
208 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
211 case symconst_type_size:
212 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
215 case symconst_type_align:
216 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
219 case symconst_enum_const:
220 tv = get_enumeration_value(get_SymConst_enum(init));
221 dump_arith_tarval(obst, tv, bytes);
225 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
230 do_dump_atomic_init(obst, get_Add_left(init));
231 obstack_printf(obst, " + ");
232 do_dump_atomic_init(obst, get_Add_right(init));
236 do_dump_atomic_init(obst, get_Sub_left(init));
237 obstack_printf(obst, " - ");
238 do_dump_atomic_init(obst, get_Sub_right(init));
242 do_dump_atomic_init(obst, get_Mul_left(init));
243 obstack_printf(obst, " * ");
244 do_dump_atomic_init(obst, get_Mul_right(init));
248 assert(0 && "dump_atomic_init(): unknown IR-node");
253 * dump an atomic value
255 static void dump_atomic_init(struct obstack *obst, ir_node *init)
257 ir_mode *mode = get_irn_mode(init);
258 int bytes = get_mode_size_bytes(mode);
263 obstack_printf(obst, "\t.byte\t");
267 obstack_printf(obst, "\t.value\t");
271 obstack_printf(obst, "\t.long\t");
275 obstack_printf(obst, "\t.quad\t");
280 /* handled in arith */
284 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
288 do_dump_atomic_init(obst, init);
289 obstack_printf(obst, "\n");
292 /************************************************************************/
293 /* Routines to dump global variables */
294 /************************************************************************/
297 * Determine if an entity is a string constant
298 * @param ent The entity
299 * @return 1 if it is a string constant, 0 otherwise
301 static int ent_is_string_const(ir_entity *ent)
306 ty = get_entity_type(ent);
308 /* if it's an array */
309 if (is_Array_type(ty)) {
310 ir_type *elm_ty = get_array_element_type(ty);
312 /* and the array's element type is primitive */
313 if (is_Primitive_type(elm_ty)) {
314 ir_mode *mode = get_type_mode(elm_ty);
317 * and the mode of the element type is an int of
318 * the same size as the byte mode
320 if (mode_is_int(mode)
321 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
325 n = get_compound_ent_n_values(ent);
326 for (i = 0; i < n; ++i) {
327 ir_node *irn = get_compound_ent_value(ent, i);
328 if(get_irn_opcode(irn) != iro_Const)
331 c = (int) get_tarval_long(get_Const_tarval(irn));
333 if((i < n - 1 && !(isgraph(c) || isspace(c)))
334 || (i == n - 1 && c != '\0'))
347 * Dump a atring constant.
348 * No checks are made!!
349 * @param obst The obst to dump on.
350 * @param ent The entity to dump.
352 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
356 obstack_printf(obst, "\t.asciz \"");
357 n = get_compound_ent_n_values(ent);
359 for (i = 0; i < n-1; ++i) {
363 irn = get_compound_ent_value(ent, i);
364 c = (int) get_tarval_long(get_Const_tarval(irn));
367 case '"' : obstack_printf(obst, "\\\""); break;
368 case '\n': obstack_printf(obst, "\\n"); break;
369 case '\r': obstack_printf(obst, "\\r"); break;
370 case '\t': obstack_printf(obst, "\\t"); break;
373 obstack_printf(obst, "%c", c);
375 obstack_printf(obst, "\\%o", c);
379 obstack_printf(obst, "\"\n");
389 * Dumps the initialization of global variables that are not
392 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
394 ir_type *ty = get_entity_type(ent);
395 const char *ld_name = get_entity_ld_name(ent);
397 struct obstack *obst = data_obstack;
400 * FIXME: did NOT work for partly constant values
402 if (! is_Method_type(ty)) {
403 ir_variability variability = get_entity_variability(ent);
404 ir_visibility visibility = get_entity_visibility(ent);
406 if (variability == variability_constant) {
407 /* a constant entity, put it on the rdata */
408 obst = rdata_obstack;
411 /* check, whether it is initialized, if yes create data */
412 if (variability != variability_uninitialized) {
413 if (visibility == visibility_external_visible) {
414 obstack_printf(obst, ".globl\t%s\n", ld_name);
416 // obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
417 // obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
419 align = get_type_alignment_bytes(ty);
420 ppc32_dump_align(obst, align);
422 obstack_printf(obst, "%s:\n", ld_name);
424 if (is_atomic_type(ty)) {
425 if (get_entity_visibility(ent) != visibility_external_allocated)
426 dump_atomic_init(obst, get_atomic_ent_value(ent));
431 if (ent_is_string_const(ent)) {
432 dump_string_cst(obst, ent);
434 else if (is_Array_type(ty)) {
437 /* potential spare values should be already included! */
438 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
439 ir_entity *step = get_compound_ent_value_member(ent, i);
440 ir_type *stype = get_entity_type(step);
442 if (get_type_mode(stype)) {
443 int align = (get_type_alignment_bits(stype) + 7) >> 3;
444 int n = size % align;
447 obstack_printf(obst, "\t.zero\t%d\n", align - n);
451 dump_atomic_init(obst, get_compound_ent_value(ent, i));
452 size += get_type_size_bytes(stype);
454 filler = get_type_size_bytes(ty) - size;
457 obstack_printf(obst, "\t.zero\t%d\n", filler);
459 else if (is_compound_type(ty)) {
463 /* Compound entities are NOT sorted.
464 * The sorting strategy used doesn't work for `value' compound fields nor
465 * for partially_constant entities.
469 * in the worst case, every entity allocates one byte, so the type
470 * size should be equal or bigger the number of fields
472 type_size = get_type_size_bytes(ty);
473 vals = xcalloc(type_size, sizeof(*vals));
475 /* collect the values and store them at the offsets */
476 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
477 int graph_length, aipos, offset;
480 compound_graph_path *path = get_compound_ent_value_path(ent, i);
482 /* get the access path to the costant value */
483 graph_length = get_compound_graph_path_length(path);
484 ai = xcalloc(graph_length, sizeof(struct arr_info));
486 /* We wanna know how many arrays are on the path to the entity. We also have to know how
487 * many elements each array holds to calculate the offset for the entity. */
488 for (j = 0; j < graph_length; j++) {
489 ir_entity *step = get_compound_graph_path_node(path, j);
490 ir_type *step_type = get_entity_type(step);
491 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
494 if (is_Array_type(step_type))
495 for (k = 0; k < get_array_n_dimensions(step_type); k++)
496 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
498 ai[j].n_elems = n ? all_n + 1 : 0;
500 ai[j].size = ty_size;
503 aipos = graph_length - 1;
506 for (offset = j = 0; j < graph_length; j++) {
507 ir_entity *step = get_compound_graph_path_node(path, j);
508 ir_type *step_type = get_entity_type(step);
509 int ent_ofs = get_entity_offset(step);
512 /* add all positive offsets (= offsets in structs) */
513 if (ent_ofs >= 0) offset += ent_ofs;
515 if (j == graph_length - 1) {
516 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
518 /* Search the next free position in vals depending on the information from above (ai). */
519 while (vals[offset]) {
520 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
522 ai[aipos].visit_cnt++;
525 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
526 stepsize = ai[aipos--].size;
531 assert(aipos >= 0 && "couldn't store entity");
532 vals[offset] = get_compound_ent_value(ent, i);
539 /* now write them sorted */
540 for(i = 0; i < type_size; ) {
542 dump_atomic_init(obst, vals[i]);
543 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
547 obstack_printf(obst, "\t.byte\t0\n");
554 assert(0 && "unsupported type");
557 obstack_printf(obst, "\n");
559 else if (visibility != visibility_external_allocated) {
560 if (visibility == visibility_local) {
561 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
564 /* calculate the alignment */
565 /* align = get_type_alignment_bytes(ty);
566 h = highest_bit(align);
568 if ((1 << h) < align)
575 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);*/
576 obstack_printf(comm_obstack, "\t.comm\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
582 * Dumps declarations of global variables and the initialization code.
584 void ppc32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
586 ir_type *gt = get_glob_type();
587 int i, n = get_class_n_members(gt);
589 for (i = 0; i < n; i++)
590 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
593 void ppc32_dump_indirect_symbols(struct obstack *isyms)
597 foreach_pset(symbol_pset, ent) {
598 const char *ld_name = get_entity_ld_name(ent);
599 obstack_printf(isyms, ".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n",ld_name,ld_name);
603 /************************************************************************/
605 void ppc32_gen_decls(FILE *out) {
606 struct obstack rodata, data, comm, isyms;
610 obstack_init(&rodata);
614 ppc32_dump_globals(&rodata, &data, &comm);
616 size = obstack_object_size(&data);
617 cp = obstack_finish(&data);
619 fprintf(out, "\t.data\n");
620 fwrite(cp, 1, size, out);
623 size = obstack_object_size(&rodata);
624 cp = obstack_finish(&rodata);
626 fprintf(out, "\t.const_data\n");
627 fwrite(cp, 1, size, out);
630 size = obstack_object_size(&comm);
631 cp = obstack_finish(&comm);
633 // fprintf(out, "\t.common\n");
634 fwrite(cp, 1, size, out);
638 obstack_free(&rodata, NULL);
639 obstack_free(&data, NULL);
640 obstack_free(&comm, NULL);
642 obstack_init(&isyms);
644 ppc32_dump_indirect_symbols(&isyms);
646 size = obstack_object_size(&isyms);
647 cp = obstack_finish(&isyms);
649 fprintf(out, "\t.data\n");
650 fwrite(cp, 1, size, out);
653 obstack_free(&isyms,NULL);