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);
107 * dump an arithmetic tarval
109 static void arm_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
114 obstack_printf(obst, "\t.byte\t");
118 obstack_printf(obst, "\t.value\t");
122 obstack_printf(obst, "\t.long\t");
126 obstack_printf(obst, "\t.quad\t");
134 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
137 dump_arith_tarval(obst, tv, bytes);
142 * dump an atomic value
144 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
146 ir_mode *mode = get_irn_mode(init);
147 int bytes = get_mode_size_bytes(mode);
150 switch (get_irn_opcode(init)) {
153 do_dump_atomic_init(obst, get_Cast_op(init));
157 do_dump_atomic_init(obst, get_Conv_op(init));
161 tv = get_Const_tarval(init);
163 /* beware of old stuff */
164 assert(! mode_is_reference(mode));
166 /* it's a arithmetic value */
167 dump_arith_tarval(obst, tv, bytes);
171 switch (get_SymConst_kind(init)) {
172 case symconst_addr_name:
173 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
176 case symconst_addr_ent:
177 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
180 case symconst_ofs_ent:
181 obstack_printf(obst, "%d", get_entity_offset_bytes(get_SymConst_entity(init)));
184 case symconst_type_size:
185 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
188 case symconst_type_align:
189 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
192 case symconst_enum_const:
193 tv = get_enumeration_value(get_SymConst_enum(init));
194 dump_arith_tarval(obst, tv, bytes);
198 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
203 do_dump_atomic_init(obst, get_Add_left(init));
204 obstack_printf(obst, " + ");
205 do_dump_atomic_init(obst, get_Add_right(init));
209 do_dump_atomic_init(obst, get_Sub_left(init));
210 obstack_printf(obst, " - ");
211 do_dump_atomic_init(obst, get_Sub_right(init));
215 do_dump_atomic_init(obst, get_Mul_left(init));
216 obstack_printf(obst, " * ");
217 do_dump_atomic_init(obst, get_Mul_right(init));
221 assert(0 && "dump_atomic_init(): unknown IR-node");
226 * dump an atomic value
228 static void dump_atomic_init(struct obstack *obst, ir_node *init)
230 ir_mode *mode = get_irn_mode(init);
231 int bytes = get_mode_size_bytes(mode);
236 obstack_printf(obst, "\t.byte\t");
240 obstack_printf(obst, "\t.value\t");
244 obstack_printf(obst, "\t.long\t");
248 obstack_printf(obst, "\t.quad\t");
253 /* handled in arith */
257 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
261 do_dump_atomic_init(obst, init);
262 obstack_printf(obst, "\n");
265 /************************************************************************/
266 /* Routines to dump global variables */
267 /************************************************************************/
270 * Determine if an entity is a string constant
271 * @param ent The entity
272 * @return 1 if it is a string constant, 0 otherwise
274 static int ent_is_string_const(entity *ent)
279 ty = get_entity_type(ent);
281 /* if it's an array */
282 if (is_Array_type(ty)) {
283 ir_type *elm_ty = get_array_element_type(ty);
285 /* and the array's element type is primitive */
286 if (is_Primitive_type(elm_ty)) {
287 ir_mode *mode = get_type_mode(elm_ty);
290 * and the mode of the element type is an int of
291 * the same size as the byte mode
293 if (mode_is_int(mode)
294 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
298 n = get_compound_ent_n_values(ent);
299 for (i = 0; i < n; ++i) {
300 ir_node *irn = get_compound_ent_value(ent, i);
301 if(get_irn_opcode(irn) != iro_Const)
304 c = (int) get_tarval_long(get_Const_tarval(irn));
306 if((i < n - 1 && !(isgraph(c) || isspace(c)))
307 || (i == n - 1 && c != '\0'))
320 * Dump a atring constant.
321 * No checks are made!!
322 * @param obst The obst to dump on.
323 * @param ent The entity to dump.
325 static void dump_string_cst(struct obstack *obst, entity *ent)
329 obstack_printf(obst, "\t.string \"");
330 n = get_compound_ent_n_values(ent);
332 for (i = 0; i < n-1; ++i) {
336 irn = get_compound_ent_value(ent, i);
337 c = (int) get_tarval_long(get_Const_tarval(irn));
340 case '"' : obstack_printf(obst, "\\\""); break;
341 case '\n': obstack_printf(obst, "\\n"); break;
342 case '\r': obstack_printf(obst, "\\r"); break;
343 case '\t': obstack_printf(obst, "\\t"); break;
346 obstack_printf(obst, "%c", c);
348 obstack_printf(obst, "%O", c);
352 obstack_printf(obst, "\"\n");
362 * Dumps the initialization of global variables that are not
365 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
367 ir_type *ty = get_entity_type(ent);
368 const char *ld_name = get_entity_ld_name(ent);
370 struct obstack *obst = data_obstack;
373 * FIXME: did NOT work for partly constant values
375 if (! is_Method_type(ty)) {
376 ir_variability variability = get_entity_variability(ent);
377 ir_visibility visibility = get_entity_visibility(ent);
379 if (variability == variability_constant) {
380 /* a constant entity, put it on the rdata */
381 obst = rdata_obstack;
384 /* check, wether it is initialized, if yes create data */
385 if (variability != variability_uninitialized) {
386 if (visibility == visibility_external_visible) {
387 obstack_printf(obst, ".globl\t%s\n", ld_name);
389 obstack_printf(obst, "\t.type\t%s,%%object\n", ld_name);
390 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
392 align = get_type_alignment_bytes(ty);
393 arm_dump_align(obst, align);
395 obstack_printf(obst, "%s:\n", ld_name);
397 if (is_atomic_type(ty)) {
398 if (get_entity_visibility(ent) != visibility_external_allocated)
399 dump_atomic_init(obst, get_atomic_ent_value(ent));
404 if (ent_is_string_const(ent)) {
405 dump_string_cst(obst, ent);
407 else if (is_Array_type(ty)) {
410 /* potential spare values should be already included! */
411 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
412 entity *step = get_compound_ent_value_member(ent, i);
413 ir_type *stype = get_entity_type(step);
415 if (get_type_mode(stype)) {
416 int align = (get_type_alignment_bits(stype) + 7) >> 3;
417 int n = size % align;
420 obstack_printf(obst, "\t.zero\t%d\n", align - n);
424 dump_atomic_init(obst, get_compound_ent_value(ent, i));
425 size += get_type_size_bytes(stype);
427 filler = get_type_size_bytes(ty) - size;
430 obstack_printf(obst, "\t.zero\t%d\n", filler);
432 else if (is_compound_type(ty)) {
436 /* Compound entities are NOT sorted.
437 * The sorting strategy used doesn't work for `value' compound fields nor
438 * for partially_constant entities.
442 * in the worst case, every entity allocates one byte, so the type
443 * size should be equal or bigger the number of fields
445 type_size = get_type_size_bytes(ty);
446 vals = xcalloc(type_size, sizeof(*vals));
448 /* collect the values and store them at the offsets */
449 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
450 int graph_length, aipos, offset;
453 compound_graph_path *path = get_compound_ent_value_path(ent, i);
455 /* get the access path to the costant value */
456 graph_length = get_compound_graph_path_length(path);
457 ai = xcalloc(graph_length, sizeof(struct arr_info));
459 /* We wanna know how many arrays are on the path to the entity. We also have to know how
460 * many elements each array holds to calculate the offset for the entity. */
461 for (j = 0; j < graph_length; j++) {
462 entity *step = get_compound_graph_path_node(path, j);
463 ir_type *step_type = get_entity_type(step);
464 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
467 if (is_Array_type(step_type))
468 for (k = 0; k < get_array_n_dimensions(step_type); k++)
469 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
471 ai[j].n_elems = n ? all_n + 1 : 0;
473 ai[j].size = ty_size;
476 aipos = graph_length - 1;
479 for (offset = j = 0; j < graph_length; j++) {
480 entity *step = get_compound_graph_path_node(path, j);
481 ir_type *step_type = get_entity_type(step);
482 int ent_ofs = get_entity_offset_bytes(step);
485 /* add all positive offsets (= offsets in structs) */
486 if (ent_ofs >= 0) offset += ent_ofs;
488 if (j == graph_length - 1) {
489 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
491 /* Search the next free position in vals depending on the information from above (ai). */
492 while (vals[offset]) {
493 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
495 ai[aipos].visit_cnt++;
498 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
499 stepsize = ai[aipos--].size;
504 assert(aipos >= 0 && "couldn't store entity");
505 vals[offset] = get_compound_ent_value(ent, i);
512 /* now write them sorted */
513 for(i = 0; i < type_size; ) {
515 dump_atomic_init(obst, vals[i]);
516 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
520 obstack_printf(obst, "\t.byte\t0\n");
527 assert(0 && "unsupported type");
530 obstack_printf(obst, "\n");
532 else if (visibility != visibility_external_allocated) {
533 if (visibility == visibility_local) {
534 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
537 /* calculate the alignment */
538 align = get_type_alignment_bytes(ty);
539 h = highest_bit(align);
541 if ((1 << h) < align)
548 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
554 * Dumps declarations of global variables and the initialization code.
556 void arm_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 /************************************************************************/
567 void arm_gen_decls(FILE *out) {
568 struct obstack rodata, data, comm;
572 obstack_init(&rodata);
576 arm_dump_globals(&rodata, &data, &comm);
578 size = obstack_object_size(&data);
579 cp = obstack_finish(&data);
581 arm_switch_section(out, SECTION_DATA);
582 fwrite(cp, 1, size, out);
585 size = obstack_object_size(&rodata);
586 cp = obstack_finish(&rodata);
588 arm_switch_section(out, SECTION_RODATA);
589 fwrite(cp, 1, size, out);
592 size = obstack_object_size(&comm);
593 cp = obstack_finish(&comm);
595 arm_switch_section(out, SECTION_COMMON);
596 fwrite(cp, 1, size, out);
599 obstack_free(&rodata, NULL);
600 obstack_free(&data, NULL);
601 obstack_free(&comm, NULL);