2 * Dumps global variables and constants as arm assembler.
22 #include "arm_emitter.h"
24 #include "arm_gen_decls.h"
26 /************************************************************************/
29 * returns the highest bit value
31 static unsigned highest_bit(unsigned v)
35 if (v >= (1U << 16U)) {
39 if (v >= (1U << 8U)) {
43 if (v >= (1U << 4U)) {
47 if (v >= (1U << 2U)) {
51 if (v >= (1U << 1U)) {
62 * output the alignment
64 static void arm_dump_align(struct obstack *obst, int align)
66 int h = highest_bit(align);
73 obstack_printf(obst, "\t.align %d\n", align);
76 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
81 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
85 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
89 obstack_printf(obst, "0x%02x%02x%02x%02x",
90 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
94 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
95 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
96 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
104 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
111 * dump an arithmetic tarval
113 static void arm_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
118 obstack_printf(obst, "\t.byte\t");
122 obstack_printf(obst, "\t.value\t");
126 obstack_printf(obst, "\t.long\t");
130 obstack_printf(obst, "\t.quad\t");
138 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
141 dump_arith_tarval(obst, tv, bytes);
146 * dump an atomic value
148 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
150 ir_mode *mode = get_irn_mode(init);
151 int bytes = get_mode_size_bytes(mode);
154 switch (get_irn_opcode(init)) {
157 do_dump_atomic_init(obst, get_Cast_op(init));
161 do_dump_atomic_init(obst, get_Conv_op(init));
165 tv = get_Const_tarval(init);
167 /* beware of old stuff */
168 assert(! mode_is_reference(mode));
170 /* it's a arithmetic value */
171 dump_arith_tarval(obst, tv, bytes);
175 switch (get_SymConst_kind(init)) {
176 case symconst_addr_name:
177 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
180 case symconst_addr_ent:
181 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
184 case symconst_ofs_ent:
185 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
188 case symconst_type_size:
189 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
192 case symconst_type_align:
193 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
196 case symconst_enum_const:
197 tv = get_enumeration_value(get_SymConst_enum(init));
198 dump_arith_tarval(obst, tv, bytes);
202 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
207 do_dump_atomic_init(obst, get_Add_left(init));
208 obstack_printf(obst, " + ");
209 do_dump_atomic_init(obst, get_Add_right(init));
213 do_dump_atomic_init(obst, get_Sub_left(init));
214 obstack_printf(obst, " - ");
215 do_dump_atomic_init(obst, get_Sub_right(init));
219 do_dump_atomic_init(obst, get_Mul_left(init));
220 obstack_printf(obst, " * ");
221 do_dump_atomic_init(obst, get_Mul_right(init));
225 assert(0 && "dump_atomic_init(): unknown IR-node");
230 * dump an atomic value
232 static void dump_atomic_init(struct obstack *obst, ir_node *init)
234 ir_mode *mode = get_irn_mode(init);
235 int bytes = get_mode_size_bytes(mode);
240 obstack_printf(obst, "\t.byte\t");
244 obstack_printf(obst, "\t.value\t");
248 obstack_printf(obst, "\t.long\t");
252 obstack_printf(obst, "\t.quad\t");
257 /* handled in arith */
261 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
265 do_dump_atomic_init(obst, init);
266 obstack_printf(obst, "\n");
269 /************************************************************************/
270 /* Routines to dump global variables */
271 /************************************************************************/
274 * Determine if an entity is a string constant
275 * @param ent The entity
276 * @return 1 if it is a string constant, 0 otherwise
278 static int ent_is_string_const(ir_entity *ent)
283 ty = get_entity_type(ent);
285 /* if it's an array */
286 if (is_Array_type(ty)) {
287 ir_type *elm_ty = get_array_element_type(ty);
289 /* and the array's element type is primitive */
290 if (is_Primitive_type(elm_ty)) {
291 ir_mode *mode = get_type_mode(elm_ty);
294 * and the mode of the element type is an int of
295 * the same size as the byte mode
297 if (mode_is_int(mode)
298 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
302 n = get_compound_ent_n_values(ent);
303 for (i = 0; i < n; ++i) {
304 ir_node *irn = get_compound_ent_value(ent, i);
305 if(get_irn_opcode(irn) != iro_Const)
308 c = (int) get_tarval_long(get_Const_tarval(irn));
310 if((i < n - 1 && !(isgraph(c) || isspace(c)))
311 || (i == n - 1 && c != '\0'))
324 * Dump a atring constant.
325 * No checks are made!!
326 * @param obst The obst to dump on.
327 * @param ent The entity to dump.
329 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
333 obstack_printf(obst, "\t.string \"");
334 n = get_compound_ent_n_values(ent);
336 for (i = 0; i < n-1; ++i) {
340 irn = get_compound_ent_value(ent, i);
341 c = (int) get_tarval_long(get_Const_tarval(irn));
344 case '"' : obstack_printf(obst, "\\\""); break;
345 case '\n': obstack_printf(obst, "\\n"); break;
346 case '\r': obstack_printf(obst, "\\r"); break;
347 case '\t': obstack_printf(obst, "\\t"); break;
350 obstack_printf(obst, "%c", c);
352 obstack_printf(obst, "\\%o", c);
356 obstack_printf(obst, "\"\n");
366 * Dumps the initialization of global variables that are not
369 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
371 ir_type *ty = get_entity_type(ent);
372 const char *ld_name = get_entity_ld_name(ent);
374 struct obstack *obst = data_obstack;
377 * FIXME: did NOT work for partly constant values
379 if (! is_Method_type(ty)) {
380 ir_variability variability = get_entity_variability(ent);
381 ir_visibility visibility = get_entity_visibility(ent);
383 if (variability == variability_constant) {
384 /* a constant entity, put it on the rdata */
385 obst = rdata_obstack;
388 /* check, wether it is initialized, if yes create data */
389 if (variability != variability_uninitialized) {
390 if (visibility == visibility_external_visible) {
391 obstack_printf(obst, ".globl\t%s\n", ld_name);
393 obstack_printf(obst, "\t.type\t%s,%%object\n", ld_name);
394 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
396 align = get_type_alignment_bytes(ty);
397 arm_dump_align(obst, align);
399 obstack_printf(obst, "%s:\n", ld_name);
401 if (is_atomic_type(ty)) {
402 if (get_entity_visibility(ent) != visibility_external_allocated)
403 dump_atomic_init(obst, get_atomic_ent_value(ent));
408 if (ent_is_string_const(ent)) {
409 dump_string_cst(obst, ent);
411 else if (is_Array_type(ty)) {
414 /* potential spare values should be already included! */
415 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
416 ir_entity *step = get_compound_ent_value_member(ent, i);
417 ir_type *stype = get_entity_type(step);
419 if (get_type_mode(stype)) {
420 int align = (get_type_alignment_bits(stype) + 7) >> 3;
421 int n = size % align;
424 obstack_printf(obst, "\t.zero\t%d\n", align - n);
428 dump_atomic_init(obst, get_compound_ent_value(ent, i));
429 size += get_type_size_bytes(stype);
431 filler = get_type_size_bytes(ty) - size;
434 obstack_printf(obst, "\t.zero\t%d\n", filler);
436 else if (is_compound_type(ty)) {
440 /* Compound entities are NOT sorted.
441 * The sorting strategy used doesn't work for `value' compound fields nor
442 * for partially_constant entities.
446 * in the worst case, every entity allocates one byte, so the type
447 * size should be equal or bigger the number of fields
449 type_size = get_type_size_bytes(ty);
450 vals = xcalloc(type_size, sizeof(*vals));
452 /* collect the values and store them at the offsets */
453 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
454 int graph_length, aipos, offset;
457 compound_graph_path *path = get_compound_ent_value_path(ent, i);
459 /* get the access path to the costant value */
460 graph_length = get_compound_graph_path_length(path);
461 ai = xcalloc(graph_length, sizeof(struct arr_info));
463 /* We wanna know how many arrays are on the path to the entity. We also have to know how
464 * many elements each array holds to calculate the offset for the entity. */
465 for (j = 0; j < graph_length; j++) {
466 ir_entity *step = get_compound_graph_path_node(path, j);
467 ir_type *step_type = get_entity_type(step);
468 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
471 if (is_Array_type(step_type))
472 for (k = 0; k < get_array_n_dimensions(step_type); k++)
473 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
475 ai[j].n_elems = n ? all_n + 1 : 0;
477 ai[j].size = ty_size;
480 aipos = graph_length - 1;
483 for (offset = j = 0; j < graph_length; j++) {
484 ir_entity *step = get_compound_graph_path_node(path, j);
485 ir_type *step_type = get_entity_type(step);
486 int ent_ofs = get_entity_offset(step);
489 /* add all positive offsets (= offsets in structs) */
490 if (ent_ofs >= 0) offset += ent_ofs;
492 if (j == graph_length - 1) {
493 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
495 /* Search the next free position in vals depending on the information from above (ai). */
496 while (vals[offset]) {
497 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
499 ai[aipos].visit_cnt++;
502 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
503 stepsize = ai[aipos--].size;
508 assert(aipos >= 0 && "couldn't store entity");
509 vals[offset] = get_compound_ent_value(ent, i);
516 /* now write them sorted */
517 for(i = 0; i < type_size; ) {
519 dump_atomic_init(obst, vals[i]);
520 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
524 obstack_printf(obst, "\t.byte\t0\n");
531 assert(0 && "unsupported type");
534 obstack_printf(obst, "\n");
536 else if (visibility != visibility_external_allocated) {
537 if (visibility == visibility_local) {
538 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
541 /* calculate the alignment */
542 align = get_type_alignment_bytes(ty);
543 h = highest_bit(align);
545 if ((1 << h) < align)
552 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
558 * Dumps declarations of global variables and the initialization code.
560 void arm_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
562 ir_type *gt = get_glob_type();
563 int i, n = get_class_n_members(gt);
565 for (i = 0; i < n; i++)
566 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
569 /************************************************************************/
571 void arm_gen_decls(FILE *out) {
572 struct obstack rodata, data, comm;
576 obstack_init(&rodata);
580 arm_dump_globals(&rodata, &data, &comm);
582 size = obstack_object_size(&data);
583 cp = obstack_finish(&data);
585 arm_switch_section(out, SECTION_DATA);
586 fwrite(cp, 1, size, out);
589 size = obstack_object_size(&rodata);
590 cp = obstack_finish(&rodata);
592 arm_switch_section(out, SECTION_RODATA);
593 fwrite(cp, 1, size, out);
596 size = obstack_object_size(&comm);
597 cp = obstack_finish(&comm);
599 arm_switch_section(out, SECTION_COMMON);
600 fwrite(cp, 1, size, out);
603 obstack_free(&rodata, NULL);
604 obstack_free(&data, NULL);
605 obstack_free(&comm, NULL);