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 /* Matze: TODO what the heck is this? changing it to non-extern for now so that firm compiles again... */
50 /************************************************************************/
53 * returns the highest bit value
55 static unsigned highest_bit(unsigned v)
59 if (v >= (1U << 16U)) {
63 if (v >= (1U << 8U)) {
67 if (v >= (1U << 4U)) {
71 if (v >= (1U << 2U)) {
75 if (v >= (1U << 1U)) {
86 * output the alignment
88 static void ppc32_dump_align(struct obstack *obst, int align)
90 int h = highest_bit(align);
97 obstack_printf(obst, "\t.align %d\n", align);
100 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
105 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
109 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
113 obstack_printf(obst, "0x%02x%02x%02x%02x",
114 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
118 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
119 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
120 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
128 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
135 * dump an arithmetic tarval
137 static void ppc32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
142 obstack_printf(obst, "\t.byte\t");
146 obstack_printf(obst, "\t.value\t");
150 obstack_printf(obst, "\t.long\t");
154 obstack_printf(obst, "\t.quad\t");
162 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
165 dump_arith_tarval(obst, tv, bytes);
170 * dump an atomic value
172 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
174 ir_mode *mode = get_irn_mode(init);
175 int bytes = get_mode_size_bytes(mode);
178 switch (get_irn_opcode(init)) {
181 do_dump_atomic_init(obst, get_Cast_op(init));
185 do_dump_atomic_init(obst, get_Conv_op(init));
189 tv = get_Const_tarval(init);
191 /* beware of old stuff */
192 assert(! mode_is_reference(mode));
194 /* it's a arithmetic value */
195 dump_arith_tarval(obst, tv, bytes);
199 switch (get_SymConst_kind(init)) {
200 case symconst_addr_name:
201 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
204 case symconst_addr_ent:
205 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
208 case symconst_ofs_ent:
209 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
212 case symconst_type_size:
213 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
216 case symconst_type_align:
217 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
220 case symconst_enum_const:
221 tv = get_enumeration_value(get_SymConst_enum(init));
222 dump_arith_tarval(obst, tv, bytes);
226 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
231 do_dump_atomic_init(obst, get_Add_left(init));
232 obstack_printf(obst, " + ");
233 do_dump_atomic_init(obst, get_Add_right(init));
237 do_dump_atomic_init(obst, get_Sub_left(init));
238 obstack_printf(obst, " - ");
239 do_dump_atomic_init(obst, get_Sub_right(init));
243 do_dump_atomic_init(obst, get_Mul_left(init));
244 obstack_printf(obst, " * ");
245 do_dump_atomic_init(obst, get_Mul_right(init));
249 assert(0 && "dump_atomic_init(): unknown IR-node");
254 * dump an atomic value
256 static void dump_atomic_init(struct obstack *obst, ir_node *init)
258 ir_mode *mode = get_irn_mode(init);
259 int bytes = get_mode_size_bytes(mode);
264 obstack_printf(obst, "\t.byte\t");
268 obstack_printf(obst, "\t.value\t");
272 obstack_printf(obst, "\t.long\t");
276 obstack_printf(obst, "\t.quad\t");
281 /* handled in arith */
285 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
289 do_dump_atomic_init(obst, init);
290 obstack_printf(obst, "\n");
293 /************************************************************************/
294 /* Routines to dump global variables */
295 /************************************************************************/
298 * Determine if an entity is a string constant
299 * @param ent The entity
300 * @return 1 if it is a string constant, 0 otherwise
302 static int ent_is_string_const(ir_entity *ent)
307 ty = get_entity_type(ent);
309 /* if it's an array */
310 if (is_Array_type(ty)) {
311 ir_type *elm_ty = get_array_element_type(ty);
313 /* and the array's element type is primitive */
314 if (is_Primitive_type(elm_ty)) {
315 ir_mode *mode = get_type_mode(elm_ty);
318 * and the mode of the element type is an int of
319 * the same size as the byte mode
321 if (mode_is_int(mode)
322 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
326 n = get_compound_ent_n_values(ent);
327 for (i = 0; i < n; ++i) {
328 ir_node *irn = get_compound_ent_value(ent, i);
329 if(get_irn_opcode(irn) != iro_Const)
332 c = (int) get_tarval_long(get_Const_tarval(irn));
334 if((i < n - 1 && !(isgraph(c) || isspace(c)))
335 || (i == n - 1 && c != '\0'))
348 * Dump a atring constant.
349 * No checks are made!!
350 * @param obst The obst to dump on.
351 * @param ent The entity to dump.
353 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
357 obstack_printf(obst, "\t.asciz \"");
358 n = get_compound_ent_n_values(ent);
360 for (i = 0; i < n-1; ++i) {
364 irn = get_compound_ent_value(ent, i);
365 c = (int) get_tarval_long(get_Const_tarval(irn));
368 case '"' : obstack_printf(obst, "\\\""); break;
369 case '\n': obstack_printf(obst, "\\n"); break;
370 case '\r': obstack_printf(obst, "\\r"); break;
371 case '\t': obstack_printf(obst, "\\t"); break;
374 obstack_printf(obst, "%c", c);
376 obstack_printf(obst, "\\%o", c);
380 obstack_printf(obst, "\"\n");
390 * Dumps the initialization of global variables that are not
393 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
395 ir_type *ty = get_entity_type(ent);
396 const char *ld_name = get_entity_ld_name(ent);
398 struct obstack *obst = data_obstack;
401 * FIXME: did NOT work for partly constant values
403 if (! is_Method_type(ty)) {
404 ir_variability variability = get_entity_variability(ent);
405 ir_visibility visibility = get_entity_visibility(ent);
407 if (variability == variability_constant) {
408 /* a constant entity, put it on the rdata */
409 obst = rdata_obstack;
412 /* check, whether it is initialized, if yes create data */
413 if (variability != variability_uninitialized) {
414 if (visibility == visibility_external_visible) {
415 obstack_printf(obst, ".globl\t%s\n", ld_name);
417 // obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
418 // obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
420 align = get_type_alignment_bytes(ty);
421 ppc32_dump_align(obst, align);
423 obstack_printf(obst, "%s:\n", ld_name);
425 if (is_atomic_type(ty)) {
426 if (get_entity_visibility(ent) != visibility_external_allocated)
427 dump_atomic_init(obst, get_atomic_ent_value(ent));
432 if (ent_is_string_const(ent)) {
433 dump_string_cst(obst, ent);
435 else if (is_Array_type(ty)) {
438 /* potential spare values should be already included! */
439 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
440 ir_entity *step = get_compound_ent_value_member(ent, i);
441 ir_type *stype = get_entity_type(step);
443 if (get_type_mode(stype)) {
444 int align = (get_type_alignment_bits(stype) + 7) >> 3;
445 int n = size % align;
448 obstack_printf(obst, "\t.zero\t%d\n", align - n);
452 dump_atomic_init(obst, get_compound_ent_value(ent, i));
453 size += get_type_size_bytes(stype);
455 filler = get_type_size_bytes(ty) - size;
458 obstack_printf(obst, "\t.zero\t%d\n", filler);
460 else if (is_compound_type(ty)) {
464 /* Compound entities are NOT sorted.
465 * The sorting strategy used doesn't work for `value' compound fields nor
466 * for partially_constant entities.
470 * in the worst case, every entity allocates one byte, so the type
471 * size should be equal or bigger the number of fields
473 type_size = get_type_size_bytes(ty);
474 vals = xcalloc(type_size, sizeof(*vals));
476 /* collect the values and store them at the offsets */
477 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
478 int graph_length, aipos, offset;
481 compound_graph_path *path = get_compound_ent_value_path(ent, i);
483 /* get the access path to the costant value */
484 graph_length = get_compound_graph_path_length(path);
485 ai = xcalloc(graph_length, sizeof(struct arr_info));
487 /* We wanna know how many arrays are on the path to the entity. We also have to know how
488 * many elements each array holds to calculate the offset for the entity. */
489 for (j = 0; j < graph_length; j++) {
490 ir_entity *step = get_compound_graph_path_node(path, j);
491 ir_type *step_type = get_entity_type(step);
492 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
495 if (is_Array_type(step_type))
496 for (k = 0; k < get_array_n_dimensions(step_type); k++)
497 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
499 ai[j].n_elems = n ? all_n + 1 : 0;
501 ai[j].size = ty_size;
504 aipos = graph_length - 1;
507 for (offset = j = 0; j < graph_length; j++) {
508 ir_entity *step = get_compound_graph_path_node(path, j);
509 ir_type *step_type = get_entity_type(step);
510 int ent_ofs = get_entity_offset(step);
513 /* add all positive offsets (= offsets in structs) */
514 if (ent_ofs >= 0) offset += ent_ofs;
516 if (j == graph_length - 1) {
517 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
519 /* Search the next free position in vals depending on the information from above (ai). */
520 while (vals[offset]) {
521 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
523 ai[aipos].visit_cnt++;
526 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
527 stepsize = ai[aipos--].size;
532 assert(aipos >= 0 && "couldn't store entity");
533 vals[offset] = get_compound_ent_value(ent, i);
540 /* now write them sorted */
541 for(i = 0; i < type_size; ) {
543 dump_atomic_init(obst, vals[i]);
544 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
548 obstack_printf(obst, "\t.byte\t0\n");
555 assert(0 && "unsupported type");
558 obstack_printf(obst, "\n");
560 else if (visibility != visibility_external_allocated) {
561 if (visibility == visibility_local) {
562 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
565 /* calculate the alignment */
566 /* align = get_type_alignment_bytes(ty);
567 h = highest_bit(align);
569 if ((1 << h) < align)
576 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);*/
577 obstack_printf(comm_obstack, "\t.comm\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
583 * Dumps declarations of global variables and the initialization code.
585 void ppc32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
587 ir_type *gt = get_glob_type();
588 int i, n = get_class_n_members(gt);
590 for (i = 0; i < n; i++)
591 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
594 void ppc32_dump_indirect_symbols(struct obstack *isyms)
598 foreach_pset(symbol_pset, ent) {
599 const char *ld_name = get_entity_ld_name(ent);
600 obstack_printf(isyms, ".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n",ld_name,ld_name);
604 /************************************************************************/
606 void ppc32_gen_decls(FILE *out) {
607 struct obstack rodata, data, comm, isyms;
611 obstack_init(&rodata);
615 ppc32_dump_globals(&rodata, &data, &comm);
617 size = obstack_object_size(&data);
618 cp = obstack_finish(&data);
620 fprintf(out, "\t.data\n");
621 fwrite(cp, 1, size, out);
624 size = obstack_object_size(&rodata);
625 cp = obstack_finish(&rodata);
627 fprintf(out, "\t.const_data\n");
628 fwrite(cp, 1, size, out);
631 size = obstack_object_size(&comm);
632 cp = obstack_finish(&comm);
634 // fprintf(out, "\t.common\n");
635 fwrite(cp, 1, size, out);
639 obstack_free(&rodata, NULL);
640 obstack_free(&data, NULL);
641 obstack_free(&comm, NULL);
643 obstack_init(&isyms);
645 ppc32_dump_indirect_symbols(&isyms);
647 size = obstack_object_size(&isyms);
648 cp = obstack_finish(&isyms);
650 fprintf(out, "\t.data\n");
651 fwrite(cp, 1, size, out);
654 obstack_free(&isyms,NULL);