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_type_size:
181 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
184 case symconst_type_align:
185 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
189 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
194 do_dump_atomic_init(obst, get_Add_left(init));
195 obstack_printf(obst, " + ");
196 do_dump_atomic_init(obst, get_Add_right(init));
200 do_dump_atomic_init(obst, get_Sub_left(init));
201 obstack_printf(obst, " - ");
202 do_dump_atomic_init(obst, get_Sub_right(init));
206 do_dump_atomic_init(obst, get_Mul_left(init));
207 obstack_printf(obst, " * ");
208 do_dump_atomic_init(obst, get_Mul_right(init));
212 assert(0 && "dump_atomic_init(): unknown IR-node");
217 * dump an atomic value
219 static void dump_atomic_init(struct obstack *obst, ir_node *init)
221 ir_mode *mode = get_irn_mode(init);
222 int bytes = get_mode_size_bytes(mode);
227 obstack_printf(obst, "\t.byte\t");
231 obstack_printf(obst, "\t.value\t");
235 obstack_printf(obst, "\t.long\t");
239 obstack_printf(obst, "\t.quad\t");
244 /* handled in arith */
248 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
252 do_dump_atomic_init(obst, init);
253 obstack_printf(obst, "\n");
256 /************************************************************************/
257 /* Routines to dump global variables */
258 /************************************************************************/
261 * Determine if an entity is a string constant
262 * @param ent The entity
263 * @return 1 if it is a string constant, 0 otherwise
265 static int ent_is_string_const(entity *ent)
270 ty = get_entity_type(ent);
272 /* if it's an array */
273 if (is_Array_type(ty)) {
274 ir_type *elm_ty = get_array_element_type(ty);
276 /* and the array's element type is primitive */
277 if (is_Primitive_type(elm_ty)) {
278 ir_mode *mode = get_type_mode(elm_ty);
281 * and the mode of the element type is an int of
282 * the same size as the byte mode
284 if (mode_is_int(mode)
285 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
289 n = get_compound_ent_n_values(ent);
290 for (i = 0; i < n; ++i) {
291 ir_node *irn = get_compound_ent_value(ent, i);
292 if(get_irn_opcode(irn) != iro_Const)
295 c = (int) get_tarval_long(get_Const_tarval(irn));
297 if((i < n - 1 && !(isgraph(c) || isspace(c)))
298 || (i == n - 1 && c != '\0'))
311 * Dump a atring constant.
312 * No checks are made!!
313 * @param obst The obst to dump on.
314 * @param ent The entity to dump.
316 static void dump_string_cst(struct obstack *obst, entity *ent)
320 obstack_printf(obst, "\t.string \"");
321 n = get_compound_ent_n_values(ent);
323 for (i = 0; i < n-1; ++i) {
327 irn = get_compound_ent_value(ent, i);
328 c = (int) get_tarval_long(get_Const_tarval(irn));
331 case '"' : obstack_printf(obst, "\\\""); break;
332 case '\n': obstack_printf(obst, "\\n"); break;
333 case '\r': obstack_printf(obst, "\\r"); break;
334 case '\t': obstack_printf(obst, "\\t"); break;
337 obstack_printf(obst, "%c", c);
339 obstack_printf(obst, "%O", c);
343 obstack_printf(obst, "\"\n");
353 * Dumps the initialization of global variables that are not
356 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
358 ir_type *ty = get_entity_type(ent);
359 const char *ld_name = get_entity_ld_name(ent);
361 struct obstack *obst = data_obstack;
364 * FIXME: did NOT work for partly constant values
366 if (! is_Method_type(ty)) {
367 ent_variability variability = get_entity_variability(ent);
368 visibility visibility = get_entity_visibility(ent);
370 if (variability == variability_constant) {
371 /* a constant entity, put it on the rdata */
372 obst = rdata_obstack;
375 /* check, wether it is initialized, if yes create data */
376 if (variability != variability_uninitialized) {
377 if (visibility == visibility_external_visible) {
378 obstack_printf(obst, ".globl\t%s\n", ld_name);
380 obstack_printf(obst, "\t.type\t%s,%%object\n", ld_name);
381 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
383 align = get_type_alignment_bytes(ty);
384 arm_dump_align(obst, align);
386 obstack_printf(obst, "%s:\n", ld_name);
388 if (is_atomic_type(ty)) {
389 if (get_entity_visibility(ent) != visibility_external_allocated)
390 dump_atomic_init(obst, get_atomic_ent_value(ent));
395 if (ent_is_string_const(ent)) {
396 dump_string_cst(obst, ent);
398 else if (is_Array_type(ty)) {
401 /* potential spare values should be already included! */
402 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
403 entity *step = get_compound_ent_value_member(ent, i);
404 ir_type *stype = get_entity_type(step);
406 if (get_type_mode(stype)) {
407 int align = (get_type_alignment_bits(stype) + 7) >> 3;
408 int n = size % align;
411 obstack_printf(obst, "\t.zero\t%d\n", align - n);
415 dump_atomic_init(obst, get_compound_ent_value(ent, i));
416 size += get_type_size_bytes(stype);
418 filler = get_type_size_bytes(ty) - size;
421 obstack_printf(obst, "\t.zero\t%d\n", filler);
423 else if (is_compound_type(ty)) {
427 /* Compound entities are NOT sorted.
428 * The sorting strategy used doesn't work for `value' compound fields nor
429 * for partially_constant entities.
433 * in the worst case, every entity allocates one byte, so the type
434 * size should be equal or bigger the number of fields
436 type_size = get_type_size_bytes(ty);
437 vals = xcalloc(type_size, sizeof(*vals));
439 /* collect the values and store them at the offsets */
440 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
441 int graph_length, aipos, offset;
444 compound_graph_path *path = get_compound_ent_value_path(ent, i);
446 /* get the access path to the costant value */
447 graph_length = get_compound_graph_path_length(path);
448 ai = xcalloc(graph_length, sizeof(struct arr_info));
450 /* We wanna know how many arrays are on the path to the entity. We also have to know how
451 * many elements each array holds to calculate the offset for the entity. */
452 for (j = 0; j < graph_length; j++) {
453 entity *step = get_compound_graph_path_node(path, j);
454 ir_type *step_type = get_entity_type(step);
455 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
458 if (is_Array_type(step_type))
459 for (k = 0; k < get_array_n_dimensions(step_type); k++)
460 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
462 ai[j].n_elems = n ? all_n + 1 : 0;
464 ai[j].size = ty_size;
467 aipos = graph_length - 1;
470 for (offset = j = 0; j < graph_length; j++) {
471 entity *step = get_compound_graph_path_node(path, j);
472 ir_type *step_type = get_entity_type(step);
473 int ent_ofs = get_entity_offset_bytes(step);
476 /* add all positive offsets (= offsets in structs) */
477 if (ent_ofs >= 0) offset += ent_ofs;
479 if (j == graph_length - 1) {
480 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
482 /* Search the next free position in vals depending on the information from above (ai). */
483 while (vals[offset]) {
484 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
486 ai[aipos].visit_cnt++;
489 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
490 stepsize = ai[aipos--].size;
495 assert(aipos >= 0 && "couldn't store entity");
496 vals[offset] = get_compound_ent_value(ent, i);
503 /* now write them sorted */
504 for(i = 0; i < type_size; ) {
506 dump_atomic_init(obst, vals[i]);
507 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
511 obstack_printf(obst, "\t.byte\t0\n");
518 assert(0 && "unsupported type");
521 obstack_printf(obst, "\n");
523 else if (visibility != visibility_external_allocated) {
524 if (visibility == visibility_local) {
525 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
528 /* calculate the alignment */
529 align = get_type_alignment_bytes(ty);
530 h = highest_bit(align);
532 if ((1 << h) < align)
539 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
545 * Dumps declarations of global variables and the initialization code.
547 void arm_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
549 ir_type *gt = get_glob_type();
550 int i, n = get_class_n_members(gt);
552 for (i = 0; i < n; i++)
553 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
556 /************************************************************************/
558 void arm_gen_decls(FILE *out) {
559 struct obstack rodata, data, comm;
563 obstack_init(&rodata);
567 arm_dump_globals(&rodata, &data, &comm);
569 size = obstack_object_size(&data);
570 cp = obstack_finish(&data);
572 arm_switch_section(out, SECTION_DATA);
573 fwrite(cp, 1, size, out);
576 size = obstack_object_size(&rodata);
577 cp = obstack_finish(&rodata);
579 arm_switch_section(out, SECTION_RODATA);
580 fwrite(cp, 1, size, out);
583 size = obstack_object_size(&comm);
584 cp = obstack_finish(&comm);
586 arm_switch_section(out, SECTION_COMMON);
587 fwrite(cp, 1, size, out);
590 obstack_free(&rodata, NULL);
591 obstack_free(&data, NULL);
592 obstack_free(&comm, NULL);