2 * Dumps global variables and constants as arm assembler.
19 #include "arm_emitter.h"
21 #include "arm_gen_decls.h"
23 /************************************************************************/
26 * returns the highest bit value
28 static unsigned highest_bit(unsigned v)
32 if (v >= (1U << 16U)) {
36 if (v >= (1U << 8U)) {
40 if (v >= (1U << 4U)) {
44 if (v >= (1U << 2U)) {
48 if (v >= (1U << 1U)) {
59 * output the alignment
61 static void arm_dump_align(struct obstack *obst, int align)
63 int h = highest_bit(align);
70 obstack_printf(obst, "\t.align %d\n", align);
73 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
78 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
82 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
86 obstack_printf(obst, "0x%02x%02x%02x%02x",
87 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
91 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
92 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
93 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
101 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
108 * dump an arithmetic tarval
110 static void arm_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
115 obstack_printf(obst, "\t.byte\t");
119 obstack_printf(obst, "\t.value\t");
123 obstack_printf(obst, "\t.long\t");
127 obstack_printf(obst, "\t.quad\t");
135 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
138 dump_arith_tarval(obst, tv, bytes);
143 * dump an atomic value
145 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
147 ir_mode *mode = get_irn_mode(init);
148 int bytes = get_mode_size_bytes(mode);
151 switch (get_irn_opcode(init)) {
154 do_dump_atomic_init(obst, get_Cast_op(init));
158 do_dump_atomic_init(obst, get_Conv_op(init));
162 tv = get_Const_tarval(init);
164 /* beware of old stuff */
165 assert(! mode_is_reference(mode));
167 /* it's a arithmetic value */
168 dump_arith_tarval(obst, tv, bytes);
172 switch (get_SymConst_kind(init)) {
173 case symconst_addr_name:
174 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
177 case symconst_addr_ent:
178 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
181 case symconst_ofs_ent:
182 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
185 case symconst_type_size:
186 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
189 case symconst_type_align:
190 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
193 case symconst_enum_const:
194 tv = get_enumeration_value(get_SymConst_enum(init));
195 dump_arith_tarval(obst, tv, bytes);
199 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
204 do_dump_atomic_init(obst, get_Add_left(init));
205 obstack_printf(obst, " + ");
206 do_dump_atomic_init(obst, get_Add_right(init));
210 do_dump_atomic_init(obst, get_Sub_left(init));
211 obstack_printf(obst, " - ");
212 do_dump_atomic_init(obst, get_Sub_right(init));
216 do_dump_atomic_init(obst, get_Mul_left(init));
217 obstack_printf(obst, " * ");
218 do_dump_atomic_init(obst, get_Mul_right(init));
222 assert(0 && "dump_atomic_init(): unknown IR-node");
227 * dump an atomic value
229 static void dump_atomic_init(struct obstack *obst, ir_node *init)
231 ir_mode *mode = get_irn_mode(init);
232 int bytes = get_mode_size_bytes(mode);
237 obstack_printf(obst, "\t.byte\t");
241 obstack_printf(obst, "\t.value\t");
245 obstack_printf(obst, "\t.long\t");
249 obstack_printf(obst, "\t.quad\t");
254 /* handled in arith */
258 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
262 do_dump_atomic_init(obst, init);
263 obstack_printf(obst, "\n");
266 /************************************************************************/
267 /* Routines to dump global variables */
268 /************************************************************************/
271 * Determine if an entity is a string constant
272 * @param ent The entity
273 * @return 1 if it is a string constant, 0 otherwise
275 static int ent_is_string_const(ir_entity *ent)
280 ty = get_entity_type(ent);
282 /* if it's an array */
283 if (is_Array_type(ty)) {
284 ir_type *elm_ty = get_array_element_type(ty);
286 /* and the array's element type is primitive */
287 if (is_Primitive_type(elm_ty)) {
288 ir_mode *mode = get_type_mode(elm_ty);
291 * and the mode of the element type is an int of
292 * the same size as the byte mode
294 if (mode_is_int(mode)
295 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
299 n = get_compound_ent_n_values(ent);
300 for (i = 0; i < n; ++i) {
301 ir_node *irn = get_compound_ent_value(ent, i);
302 if(get_irn_opcode(irn) != iro_Const)
305 c = (int) get_tarval_long(get_Const_tarval(irn));
307 if((i < n - 1 && !(isgraph(c) || isspace(c)))
308 || (i == n - 1 && c != '\0'))
321 * Dump a atring constant.
322 * No checks are made!!
323 * @param obst The obst to dump on.
324 * @param ent The entity to dump.
326 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
330 obstack_printf(obst, "\t.string \"");
331 n = get_compound_ent_n_values(ent);
333 for (i = 0; i < n-1; ++i) {
337 irn = get_compound_ent_value(ent, i);
338 c = (int) get_tarval_long(get_Const_tarval(irn));
341 case '"' : obstack_printf(obst, "\\\""); break;
342 case '\n': obstack_printf(obst, "\\n"); break;
343 case '\r': obstack_printf(obst, "\\r"); break;
344 case '\t': obstack_printf(obst, "\\t"); break;
347 obstack_printf(obst, "%c", c);
349 obstack_printf(obst, "%O", c);
353 obstack_printf(obst, "\"\n");
363 * Dumps the initialization of global variables that are not
366 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
368 ir_type *ty = get_entity_type(ent);
369 const char *ld_name = get_entity_ld_name(ent);
371 struct obstack *obst = data_obstack;
374 * FIXME: did NOT work for partly constant values
376 if (! is_Method_type(ty)) {
377 ir_variability variability = get_entity_variability(ent);
378 ir_visibility visibility = get_entity_visibility(ent);
380 if (variability == variability_constant) {
381 /* a constant entity, put it on the rdata */
382 obst = rdata_obstack;
385 /* check, wether it is initialized, if yes create data */
386 if (variability != variability_uninitialized) {
387 if (visibility == visibility_external_visible) {
388 obstack_printf(obst, ".globl\t%s\n", ld_name);
390 obstack_printf(obst, "\t.type\t%s,%%object\n", ld_name);
391 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
393 align = get_type_alignment_bytes(ty);
394 arm_dump_align(obst, align);
396 obstack_printf(obst, "%s:\n", ld_name);
398 if (is_atomic_type(ty)) {
399 if (get_entity_visibility(ent) != visibility_external_allocated)
400 dump_atomic_init(obst, get_atomic_ent_value(ent));
405 if (ent_is_string_const(ent)) {
406 dump_string_cst(obst, ent);
408 else if (is_Array_type(ty)) {
411 /* potential spare values should be already included! */
412 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
413 ir_entity *step = get_compound_ent_value_member(ent, i);
414 ir_type *stype = get_entity_type(step);
416 if (get_type_mode(stype)) {
417 int align = (get_type_alignment_bits(stype) + 7) >> 3;
418 int n = size % align;
421 obstack_printf(obst, "\t.zero\t%d\n", align - n);
425 dump_atomic_init(obst, get_compound_ent_value(ent, i));
426 size += get_type_size_bytes(stype);
428 filler = get_type_size_bytes(ty) - size;
431 obstack_printf(obst, "\t.zero\t%d\n", filler);
433 else if (is_compound_type(ty)) {
437 /* Compound entities are NOT sorted.
438 * The sorting strategy used doesn't work for `value' compound fields nor
439 * for partially_constant entities.
443 * in the worst case, every entity allocates one byte, so the type
444 * size should be equal or bigger the number of fields
446 type_size = get_type_size_bytes(ty);
447 vals = xcalloc(type_size, sizeof(*vals));
449 /* collect the values and store them at the offsets */
450 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
451 int graph_length, aipos, offset;
454 compound_graph_path *path = get_compound_ent_value_path(ent, i);
456 /* get the access path to the costant value */
457 graph_length = get_compound_graph_path_length(path);
458 ai = xcalloc(graph_length, sizeof(struct arr_info));
460 /* We wanna know how many arrays are on the path to the entity. We also have to know how
461 * many elements each array holds to calculate the offset for the entity. */
462 for (j = 0; j < graph_length; j++) {
463 ir_entity *step = get_compound_graph_path_node(path, j);
464 ir_type *step_type = get_entity_type(step);
465 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
468 if (is_Array_type(step_type))
469 for (k = 0; k < get_array_n_dimensions(step_type); k++)
470 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
472 ai[j].n_elems = n ? all_n + 1 : 0;
474 ai[j].size = ty_size;
477 aipos = graph_length - 1;
480 for (offset = j = 0; j < graph_length; j++) {
481 ir_entity *step = get_compound_graph_path_node(path, j);
482 ir_type *step_type = get_entity_type(step);
483 int ent_ofs = get_entity_offset(step);
486 /* add all positive offsets (= offsets in structs) */
487 if (ent_ofs >= 0) offset += ent_ofs;
489 if (j == graph_length - 1) {
490 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
492 /* Search the next free position in vals depending on the information from above (ai). */
493 while (vals[offset]) {
494 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
496 ai[aipos].visit_cnt++;
499 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
500 stepsize = ai[aipos--].size;
505 assert(aipos >= 0 && "couldn't store entity");
506 vals[offset] = get_compound_ent_value(ent, i);
513 /* now write them sorted */
514 for(i = 0; i < type_size; ) {
516 dump_atomic_init(obst, vals[i]);
517 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
521 obstack_printf(obst, "\t.byte\t0\n");
528 assert(0 && "unsupported type");
531 obstack_printf(obst, "\n");
533 else if (visibility != visibility_external_allocated) {
534 if (visibility == visibility_local) {
535 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
538 /* calculate the alignment */
539 align = get_type_alignment_bytes(ty);
540 h = highest_bit(align);
542 if ((1 << h) < align)
549 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
555 * Dumps declarations of global variables and the initialization code.
557 void arm_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
559 ir_type *gt = get_glob_type();
560 int i, n = get_class_n_members(gt);
562 for (i = 0; i < n; i++)
563 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
566 /************************************************************************/
568 void arm_gen_decls(FILE *out) {
569 struct obstack rodata, data, comm;
573 obstack_init(&rodata);
577 arm_dump_globals(&rodata, &data, &comm);
579 size = obstack_object_size(&data);
580 cp = obstack_finish(&data);
582 arm_switch_section(out, SECTION_DATA);
583 fwrite(cp, 1, size, out);
586 size = obstack_object_size(&rodata);
587 cp = obstack_finish(&rodata);
589 arm_switch_section(out, SECTION_RODATA);
590 fwrite(cp, 1, size, out);
593 size = obstack_object_size(&comm);
594 cp = obstack_finish(&comm);
596 arm_switch_section(out, SECTION_COMMON);
597 fwrite(cp, 1, size, out);
600 obstack_free(&rodata, NULL);
601 obstack_free(&data, NULL);
602 obstack_free(&comm, NULL);