2 * Dumps global variables and constants as TEMPLATE assembler.
15 #ifdef obstack_chunk_alloc
16 # undef obstack_chunk_alloc
17 # define obstack_chunk_alloc xmalloc
19 # define obstack_chunk_alloc xmalloc
20 # define obstack_chunk_free free
28 #include "TEMPLATE_gen_decls.h"
30 /************************************************************************/
33 * returns the highest bit value
35 static unsigned highest_bit(unsigned v)
39 if (v >= (1U << 16U)) {
43 if (v >= (1U << 8U)) {
47 if (v >= (1U << 4U)) {
51 if (v >= (1U << 2U)) {
55 if (v >= (1U << 1U)) {
66 * output the alignment
68 static void TEMPLATE_dump_align(struct obstack *obst, int align)
70 int h = highest_bit(align);
77 obstack_printf(obst, "\t.align %d\n", align);
80 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
85 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
89 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
93 obstack_printf(obst, "0x%02x%02x%02x%02x",
94 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
98 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
99 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
100 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
108 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
114 * dump an arithmetic tarval
116 static void TEMPLATE_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
121 obstack_printf(obst, "\t.byte\t");
125 obstack_printf(obst, "\t.value\t");
129 obstack_printf(obst, "\t.long\t");
133 obstack_printf(obst, "\t.quad\t");
141 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
144 dump_arith_tarval(obst, tv, bytes);
149 * dump an atomic value
151 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
153 ir_mode *mode = get_irn_mode(init);
154 int bytes = get_mode_size_bytes(mode);
157 switch (get_irn_opcode(init)) {
160 do_dump_atomic_init(obst, get_Cast_op(init));
164 do_dump_atomic_init(obst, get_Conv_op(init));
168 tv = get_Const_tarval(init);
170 /* beware of old stuff */
171 assert(! mode_is_reference(mode));
173 /* it's a arithmetic value */
174 dump_arith_tarval(obst, tv, bytes);
178 switch (get_SymConst_kind(init)) {
179 case symconst_addr_name:
180 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
183 case symconst_addr_ent:
184 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
188 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
192 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
197 do_dump_atomic_init(obst, get_Add_left(init));
198 obstack_printf(obst, " + ");
199 do_dump_atomic_init(obst, get_Add_right(init));
203 do_dump_atomic_init(obst, get_Sub_left(init));
204 obstack_printf(obst, " - ");
205 do_dump_atomic_init(obst, get_Sub_right(init));
209 do_dump_atomic_init(obst, get_Mul_left(init));
210 obstack_printf(obst, " * ");
211 do_dump_atomic_init(obst, get_Mul_right(init));
215 assert(0 && "dump_atomic_init(): unknown IR-node");
220 * dump an atomic value
222 static void dump_atomic_init(struct obstack *obst, ir_node *init)
224 ir_mode *mode = get_irn_mode(init);
225 int bytes = get_mode_size_bytes(mode);
230 obstack_printf(obst, "\t.byte\t");
234 obstack_printf(obst, "\t.value\t");
238 obstack_printf(obst, "\t.long\t");
242 obstack_printf(obst, "\t.quad\t");
247 /* handled in arith */
251 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
255 do_dump_atomic_init(obst, init);
256 obstack_printf(obst, "\n");
259 /************************************************************************/
260 /* Routines to dump global variables */
261 /************************************************************************/
264 * Determine if an entity is a string constant
265 * @param ent The entity
266 * @return 1 if it is a string constant, 0 otherwise
268 static int ent_is_string_const(entity *ent)
273 ty = get_entity_type(ent);
275 /* if it's an array */
276 if (is_Array_type(ty)) {
277 ir_type *elm_ty = get_array_element_type(ty);
279 /* and the array's element type is primitive */
280 if (is_Primitive_type(elm_ty)) {
281 ir_mode *mode = get_type_mode(elm_ty);
284 * and the mode of the element type is an int of
285 * the same size as the byte mode
287 if (mode_is_int(mode)
288 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
292 n = get_compound_ent_n_values(ent);
293 for (i = 0; i < n; ++i) {
294 ir_node *irn = get_compound_ent_value(ent, i);
295 if(get_irn_opcode(irn) != iro_Const)
298 c = (int) get_tarval_long(get_Const_tarval(irn));
300 if((i < n - 1 && !(isgraph(c) || isspace(c)))
301 || (i == n - 1 && c != '\0'))
314 * Dump a atring constant.
315 * No checks are made!!
316 * @param obst The obst to dump on.
317 * @param ent The entity to dump.
319 static void dump_string_cst(struct obstack *obst, entity *ent)
323 obstack_printf(obst, "\t.string \"");
324 n = get_compound_ent_n_values(ent);
326 for (i = 0; i < n-1; ++i) {
330 irn = get_compound_ent_value(ent, i);
331 c = (int) get_tarval_long(get_Const_tarval(irn));
334 case '"' : obstack_printf(obst, "\\\""); break;
335 case '\n': obstack_printf(obst, "\\n"); break;
336 case '\r': obstack_printf(obst, "\\r"); break;
337 case '\t': obstack_printf(obst, "\\t"); break;
340 obstack_printf(obst, "%c", c);
342 obstack_printf(obst, "%O", c);
346 obstack_printf(obst, "\"\n");
356 * Dumps the initialization of global variables that are not
359 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
361 ir_type *ty = get_entity_type(ent);
362 const char *ld_name = get_entity_ld_name(ent);
364 struct obstack *obst = data_obstack;
367 * FIXME: did NOT work for partly constant values
369 if (! is_Method_type(ty)) {
370 ent_variability variability = get_entity_variability(ent);
371 visibility visibility = get_entity_visibility(ent);
373 if (variability == variability_constant) {
374 /* a constant entity, put it on the rdata */
375 obst = rdata_obstack;
378 /* check, wether it is initialized, if yes create data */
379 if (variability != variability_uninitialized) {
380 if (visibility == visibility_external_visible) {
381 obstack_printf(obst, ".globl\t%s\n", ld_name);
383 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
384 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
386 align = get_type_alignment_bytes(ty);
387 TEMPLATE_dump_align(obst, align);
389 obstack_printf(obst, "%s:\n", ld_name);
391 if (is_atomic_type(ty)) {
392 if (get_entity_visibility(ent) != visibility_external_allocated)
393 dump_atomic_init(obst, get_atomic_ent_value(ent));
398 if (ent_is_string_const(ent)) {
399 dump_string_cst(obst, ent);
401 else if (is_Array_type(ty)) {
404 /* potential spare values should be already included! */
405 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
406 entity *step = get_compound_ent_value_member(ent, i);
407 ir_type *stype = get_entity_type(step);
409 if (get_type_mode(stype)) {
410 int align = (get_type_alignment_bits(stype) + 7) >> 3;
411 int n = size % align;
414 obstack_printf(obst, "\t.zero\t%d\n", align - n);
418 dump_atomic_init(obst, get_compound_ent_value(ent, i));
419 size += get_type_size_bytes(stype);
421 filler = get_type_size_bytes(ty) - size;
424 obstack_printf(obst, "\t.zero\t%d\n", filler);
426 else if (is_compound_type(ty)) {
430 /* Compound entities are NOT sorted.
431 * The sorting strategy used doesn't work for `value' compound fields nor
432 * for partially_constant entities.
436 * in the worst case, every entity allocates one byte, so the type
437 * size should be equal or bigger the number of fields
439 type_size = get_type_size_bytes(ty);
440 vals = xcalloc(type_size, sizeof(*vals));
442 /* collect the values and store them at the offsets */
443 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
444 int graph_length, aipos, offset;
447 compound_graph_path *path = get_compound_ent_value_path(ent, i);
449 /* get the access path to the costant value */
450 graph_length = get_compound_graph_path_length(path);
451 ai = xcalloc(graph_length, sizeof(struct arr_info));
453 /* We wanna know how many arrays are on the path to the entity. We also have to know how
454 * many elements each array holds to calculate the offset for the entity. */
455 for (j = 0; j < graph_length; j++) {
456 entity *step = get_compound_graph_path_node(path, j);
457 ir_type *step_type = get_entity_type(step);
458 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
461 if (is_Array_type(step_type))
462 for (k = 0; k < get_array_n_dimensions(step_type); k++)
463 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
465 ai[j].n_elems = n ? all_n + 1 : 0;
467 ai[j].size = ty_size;
470 aipos = graph_length - 1;
473 for (offset = j = 0; j < graph_length; j++) {
474 entity *step = get_compound_graph_path_node(path, j);
475 ir_type *step_type = get_entity_type(step);
476 int ent_ofs = get_entity_offset_bytes(step);
479 /* add all positive offsets (= offsets in structs) */
480 if (ent_ofs >= 0) offset += ent_ofs;
482 if (j == graph_length - 1) {
483 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
485 /* Search the next free position in vals depending on the information from above (ai). */
486 while (vals[offset]) {
487 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
489 ai[aipos].visit_cnt++;
492 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
493 stepsize = ai[aipos--].size;
498 assert(aipos >= 0 && "couldn't store entity");
499 vals[offset] = get_compound_ent_value(ent, i);
506 /* now write them sorted */
507 for(i = 0; i < type_size; ) {
509 dump_atomic_init(obst, vals[i]);
510 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
514 obstack_printf(obst, "\t.byte\t0\n");
521 assert(0 && "unsupported type");
524 obstack_printf(obst, "\n");
526 else if (visibility != visibility_external_allocated) {
527 if (visibility == visibility_local) {
528 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
531 /* calculate the alignment */
532 align = get_type_alignment_bytes(ty);
533 h = highest_bit(align);
535 if ((1 << h) < align)
542 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
548 * Dumps declarations of global variables and the initialization code.
550 void TEMPLATE_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
552 ir_type *gt = get_glob_type();
553 int i, n = get_class_n_members(gt);
555 for (i = 0; i < n; i++)
556 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
559 /************************************************************************/
561 void TEMPLATE_gen_decls(FILE *out) {
562 struct obstack rodata, data, comm;
566 obstack_init(&rodata);
570 TEMPLATE_dump_globals(&rodata, &data, &comm);
572 size = obstack_object_size(&data);
573 cp = obstack_finish(&data);
575 fprintf(out, "\t.data\n");
576 fwrite(cp, 1, size, out);
579 size = obstack_object_size(&rodata);
580 cp = obstack_finish(&rodata);
582 fprintf(out, "\t.section\t.rodata\n");
583 fwrite(cp, 1, size, out);
586 size = obstack_object_size(&comm);
587 cp = obstack_finish(&comm);
589 fprintf(out, "\t.common\n");
590 fwrite(cp, 1, size, out);
593 obstack_free(&rodata, NULL);
594 obstack_free(&data, NULL);
595 obstack_free(&comm, NULL);