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
21 * Dumps global variables and constants as ppc assembler.
43 #include "ppc32_gen_decls.h"
45 extern pset *symbol_pset;
47 /************************************************************************/
50 * returns the highest bit value
52 static unsigned highest_bit(unsigned v)
56 if (v >= (1U << 16U)) {
60 if (v >= (1U << 8U)) {
64 if (v >= (1U << 4U)) {
68 if (v >= (1U << 2U)) {
72 if (v >= (1U << 1U)) {
83 * output the alignment
85 static void ppc32_dump_align(struct obstack *obst, int align)
87 int h = highest_bit(align);
94 obstack_printf(obst, "\t.align %d\n", align);
97 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
102 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
106 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
110 obstack_printf(obst, "0x%02x%02x%02x%02x",
111 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
115 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
116 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
117 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
125 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
132 * dump an arithmetic tarval
134 static void ppc32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
139 obstack_printf(obst, "\t.byte\t");
143 obstack_printf(obst, "\t.value\t");
147 obstack_printf(obst, "\t.long\t");
151 obstack_printf(obst, "\t.quad\t");
159 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
162 dump_arith_tarval(obst, tv, bytes);
167 * dump an atomic value
169 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
171 ir_mode *mode = get_irn_mode(init);
172 int bytes = get_mode_size_bytes(mode);
175 switch (get_irn_opcode(init)) {
178 do_dump_atomic_init(obst, get_Cast_op(init));
182 do_dump_atomic_init(obst, get_Conv_op(init));
186 tv = get_Const_tarval(init);
188 /* beware of old stuff */
189 assert(! mode_is_reference(mode));
191 /* it's a arithmetic value */
192 dump_arith_tarval(obst, tv, bytes);
196 switch (get_SymConst_kind(init)) {
197 case symconst_addr_name:
198 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
201 case symconst_addr_ent:
202 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
205 case symconst_ofs_ent:
206 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
209 case symconst_type_size:
210 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
213 case symconst_type_align:
214 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
217 case symconst_enum_const:
218 tv = get_enumeration_value(get_SymConst_enum(init));
219 dump_arith_tarval(obst, tv, bytes);
223 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
228 do_dump_atomic_init(obst, get_Add_left(init));
229 obstack_printf(obst, " + ");
230 do_dump_atomic_init(obst, get_Add_right(init));
234 do_dump_atomic_init(obst, get_Sub_left(init));
235 obstack_printf(obst, " - ");
236 do_dump_atomic_init(obst, get_Sub_right(init));
240 do_dump_atomic_init(obst, get_Mul_left(init));
241 obstack_printf(obst, " * ");
242 do_dump_atomic_init(obst, get_Mul_right(init));
246 assert(0 && "dump_atomic_init(): unknown IR-node");
251 * dump an atomic value
253 static void dump_atomic_init(struct obstack *obst, ir_node *init)
255 ir_mode *mode = get_irn_mode(init);
256 int bytes = get_mode_size_bytes(mode);
261 obstack_printf(obst, "\t.byte\t");
265 obstack_printf(obst, "\t.value\t");
269 obstack_printf(obst, "\t.long\t");
273 obstack_printf(obst, "\t.quad\t");
278 /* handled in arith */
282 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
286 do_dump_atomic_init(obst, init);
287 obstack_printf(obst, "\n");
290 /************************************************************************/
291 /* Routines to dump global variables */
292 /************************************************************************/
295 * Determine if an entity is a string constant
296 * @param ent The entity
297 * @return 1 if it is a string constant, 0 otherwise
299 static int ent_is_string_const(ir_entity *ent)
304 ty = get_entity_type(ent);
306 /* if it's an array */
307 if (is_Array_type(ty)) {
308 ir_type *elm_ty = get_array_element_type(ty);
310 /* and the array's element type is primitive */
311 if (is_Primitive_type(elm_ty)) {
312 ir_mode *mode = get_type_mode(elm_ty);
315 * and the mode of the element type is an int of
316 * the same size as the byte mode
318 if (mode_is_int(mode)
319 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
323 n = get_compound_ent_n_values(ent);
324 for (i = 0; i < n; ++i) {
325 ir_node *irn = get_compound_ent_value(ent, i);
326 if(get_irn_opcode(irn) != iro_Const)
329 c = (int) get_tarval_long(get_Const_tarval(irn));
331 if((i < n - 1 && !(isgraph(c) || isspace(c)))
332 || (i == n - 1 && c != '\0'))
345 * Dump a atring constant.
346 * No checks are made!!
347 * @param obst The obst to dump on.
348 * @param ent The entity to dump.
350 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
354 obstack_printf(obst, "\t.asciz \"");
355 n = get_compound_ent_n_values(ent);
357 for (i = 0; i < n-1; ++i) {
361 irn = get_compound_ent_value(ent, i);
362 c = (int) get_tarval_long(get_Const_tarval(irn));
365 case '"' : obstack_printf(obst, "\\\""); break;
366 case '\n': obstack_printf(obst, "\\n"); break;
367 case '\r': obstack_printf(obst, "\\r"); break;
368 case '\t': obstack_printf(obst, "\\t"); break;
371 obstack_printf(obst, "%c", c);
373 obstack_printf(obst, "\\%o", c);
377 obstack_printf(obst, "\"\n");
387 * Dumps the initialization of global variables that are not
390 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
392 ir_type *ty = get_entity_type(ent);
393 const char *ld_name = get_entity_ld_name(ent);
395 struct obstack *obst = data_obstack;
398 * FIXME: did NOT work for partly constant values
400 if (! is_Method_type(ty)) {
401 ir_variability variability = get_entity_variability(ent);
402 ir_visibility visibility = get_entity_visibility(ent);
404 if (variability == variability_constant) {
405 /* a constant entity, put it on the rdata */
406 obst = rdata_obstack;
409 /* check, whether it is initialized, if yes create data */
410 if (variability != variability_uninitialized) {
411 if (visibility == visibility_external_visible) {
412 obstack_printf(obst, ".globl\t%s\n", ld_name);
414 // obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
415 // obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
417 align = get_type_alignment_bytes(ty);
418 ppc32_dump_align(obst, align);
420 obstack_printf(obst, "%s:\n", ld_name);
422 if (is_atomic_type(ty)) {
423 if (get_entity_visibility(ent) != visibility_external_allocated)
424 dump_atomic_init(obst, get_atomic_ent_value(ent));
429 if (ent_is_string_const(ent)) {
430 dump_string_cst(obst, ent);
432 else if (is_Array_type(ty)) {
435 /* potential spare values should be already included! */
436 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
437 ir_entity *step = get_compound_ent_value_member(ent, i);
438 ir_type *stype = get_entity_type(step);
440 if (get_type_mode(stype)) {
441 int align = (get_type_alignment_bits(stype) + 7) >> 3;
442 int n = size % align;
445 obstack_printf(obst, "\t.zero\t%d\n", align - n);
449 dump_atomic_init(obst, get_compound_ent_value(ent, i));
450 size += get_type_size_bytes(stype);
452 filler = get_type_size_bytes(ty) - size;
455 obstack_printf(obst, "\t.zero\t%d\n", filler);
457 else if (is_compound_type(ty)) {
461 /* Compound entities are NOT sorted.
462 * The sorting strategy used doesn't work for `value' compound fields nor
463 * for partially_constant entities.
467 * in the worst case, every entity allocates one byte, so the type
468 * size should be equal or bigger the number of fields
470 type_size = get_type_size_bytes(ty);
471 vals = xcalloc(type_size, sizeof(*vals));
473 /* collect the values and store them at the offsets */
474 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
475 int graph_length, aipos, offset;
478 compound_graph_path *path = get_compound_ent_value_path(ent, i);
480 /* get the access path to the costant value */
481 graph_length = get_compound_graph_path_length(path);
482 ai = xcalloc(graph_length, sizeof(struct arr_info));
484 /* We wanna know how many arrays are on the path to the entity. We also have to know how
485 * many elements each array holds to calculate the offset for the entity. */
486 for (j = 0; j < graph_length; j++) {
487 ir_entity *step = get_compound_graph_path_node(path, j);
488 ir_type *step_type = get_entity_type(step);
489 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
492 if (is_Array_type(step_type))
493 for (k = 0; k < get_array_n_dimensions(step_type); k++)
494 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
496 ai[j].n_elems = n ? all_n + 1 : 0;
498 ai[j].size = ty_size;
501 aipos = graph_length - 1;
504 for (offset = j = 0; j < graph_length; j++) {
505 ir_entity *step = get_compound_graph_path_node(path, j);
506 ir_type *step_type = get_entity_type(step);
507 int ent_ofs = get_entity_offset(step);
510 /* add all positive offsets (= offsets in structs) */
511 if (ent_ofs >= 0) offset += ent_ofs;
513 if (j == graph_length - 1) {
514 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
516 /* Search the next free position in vals depending on the information from above (ai). */
517 while (vals[offset]) {
518 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
520 ai[aipos].visit_cnt++;
523 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
524 stepsize = ai[aipos--].size;
529 assert(aipos >= 0 && "couldn't store entity");
530 vals[offset] = get_compound_ent_value(ent, i);
537 /* now write them sorted */
538 for(i = 0; i < type_size; ) {
540 dump_atomic_init(obst, vals[i]);
541 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
545 obstack_printf(obst, "\t.byte\t0\n");
552 assert(0 && "unsupported type");
555 obstack_printf(obst, "\n");
557 else if (visibility != visibility_external_allocated) {
558 if (visibility == visibility_local) {
559 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
562 /* calculate the alignment */
563 /* align = get_type_alignment_bytes(ty);
564 h = highest_bit(align);
566 if ((1 << h) < align)
573 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);*/
574 obstack_printf(comm_obstack, "\t.comm\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
580 * Dumps declarations of global variables and the initialization code.
582 void ppc32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
584 ir_type *gt = get_glob_type();
585 int i, n = get_class_n_members(gt);
587 for (i = 0; i < n; i++)
588 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
591 void ppc32_dump_indirect_symbols(struct obstack *isyms)
595 foreach_pset(symbol_pset, ent) {
596 const char *ld_name = get_entity_ld_name(ent);
597 obstack_printf(isyms, ".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n",ld_name,ld_name);
601 /************************************************************************/
603 void ppc32_gen_decls(FILE *out) {
604 struct obstack rodata, data, comm, isyms;
608 obstack_init(&rodata);
612 ppc32_dump_globals(&rodata, &data, &comm);
614 size = obstack_object_size(&data);
615 cp = obstack_finish(&data);
617 fprintf(out, "\t.data\n");
618 fwrite(cp, 1, size, out);
621 size = obstack_object_size(&rodata);
622 cp = obstack_finish(&rodata);
624 fprintf(out, "\t.const_data\n");
625 fwrite(cp, 1, size, out);
628 size = obstack_object_size(&comm);
629 cp = obstack_finish(&comm);
631 // fprintf(out, "\t.common\n");
632 fwrite(cp, 1, size, out);
636 obstack_free(&rodata, NULL);
637 obstack_free(&data, NULL);
638 obstack_free(&comm, NULL);
640 obstack_init(&isyms);
642 ppc32_dump_indirect_symbols(&isyms);
644 size = obstack_object_size(&isyms);
645 cp = obstack_finish(&isyms);
647 fprintf(out, "\t.data\n");
648 fwrite(cp, 1, size, out);
651 obstack_free(&isyms,NULL);