2 * Dumps global variables and constants as TEMPLATE assembler.
3 * @author Christian Wuerdig
16 #ifdef obstack_chunk_alloc
17 # undef obstack_chunk_alloc
18 # define obstack_chunk_alloc xmalloc
20 # define obstack_chunk_alloc xmalloc
21 # define obstack_chunk_free free
29 #include "TEMPLATE_gen_decls.h"
31 /************************************************************************/
34 * returns the highest bit value
36 static unsigned highest_bit(unsigned v)
40 if (v >= (1U << 16U)) {
44 if (v >= (1U << 8U)) {
48 if (v >= (1U << 4U)) {
52 if (v >= (1U << 2U)) {
56 if (v >= (1U << 1U)) {
67 * output the alignment
69 static void TEMPLATE_dump_align(struct obstack *obst, int align)
71 int h = highest_bit(align);
78 obstack_printf(obst, "\t.align %d\n", align);
81 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
86 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
90 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
94 obstack_printf(obst, "0x%02x%02x%02x%02x",
95 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
99 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
100 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
101 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
109 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
115 * dump an arithmetic tarval
117 static void TEMPLATE_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
122 obstack_printf(obst, "\t.byte\t");
126 obstack_printf(obst, "\t.value\t");
130 obstack_printf(obst, "\t.long\t");
134 obstack_printf(obst, "\t.quad\t");
142 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
145 dump_arith_tarval(obst, tv, bytes);
150 * dump an atomic value
152 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
154 ir_mode *mode = get_irn_mode(init);
155 int bytes = get_mode_size_bytes(mode);
158 switch (get_irn_opcode(init)) {
161 do_dump_atomic_init(obst, get_Cast_op(init));
165 do_dump_atomic_init(obst, get_Conv_op(init));
169 tv = get_Const_tarval(init);
171 /* beware of old stuff */
172 assert(! mode_is_reference(mode));
174 /* it's a arithmetic value */
175 dump_arith_tarval(obst, tv, bytes);
179 switch (get_SymConst_kind(init)) {
180 case symconst_addr_name:
181 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
184 case symconst_addr_ent:
185 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
189 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
193 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
198 do_dump_atomic_init(obst, get_Add_left(init));
199 obstack_printf(obst, " + ");
200 do_dump_atomic_init(obst, get_Add_right(init));
204 do_dump_atomic_init(obst, get_Sub_left(init));
205 obstack_printf(obst, " - ");
206 do_dump_atomic_init(obst, get_Sub_right(init));
210 do_dump_atomic_init(obst, get_Mul_left(init));
211 obstack_printf(obst, " * ");
212 do_dump_atomic_init(obst, get_Mul_right(init));
216 assert(0 && "dump_atomic_init(): unknown IR-node");
221 * dump an atomic value
223 static void dump_atomic_init(struct obstack *obst, ir_node *init)
225 ir_mode *mode = get_irn_mode(init);
226 int bytes = get_mode_size_bytes(mode);
231 obstack_printf(obst, "\t.byte\t");
235 obstack_printf(obst, "\t.value\t");
239 obstack_printf(obst, "\t.long\t");
243 obstack_printf(obst, "\t.quad\t");
248 /* handled in arith */
252 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
256 do_dump_atomic_init(obst, init);
257 obstack_printf(obst, "\n");
260 /************************************************************************/
261 /* Routines to dump global variables */
262 /************************************************************************/
265 * Determine if an entity is a string constant
266 * @param ent The entity
267 * @return 1 if it is a string constant, 0 otherwise
269 static int ent_is_string_const(entity *ent)
274 ty = get_entity_type(ent);
276 /* if it's an array */
277 if (is_Array_type(ty)) {
278 ir_type *elm_ty = get_array_element_type(ty);
280 /* and the array's element type is primitive */
281 if (is_Primitive_type(elm_ty)) {
282 ir_mode *mode = get_type_mode(elm_ty);
285 * and the mode of the element type is an int of
286 * the same size as the byte mode
288 if (mode_is_int(mode)
289 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
293 n = get_compound_ent_n_values(ent);
294 for (i = 0; i < n; ++i) {
295 ir_node *irn = get_compound_ent_value(ent, i);
296 if(get_irn_opcode(irn) != iro_Const)
299 c = (int) get_tarval_long(get_Const_tarval(irn));
301 if((i < n - 1 && !(isgraph(c) || isspace(c)))
302 || (i == n - 1 && c != '\0'))
315 * Dump a atring constant.
316 * No checks are made!!
317 * @param obst The obst to dump on.
318 * @param ent The entity to dump.
320 static void dump_string_cst(struct obstack *obst, entity *ent)
324 obstack_printf(obst, "\t.string \"");
325 n = get_compound_ent_n_values(ent);
327 for (i = 0; i < n-1; ++i) {
331 irn = get_compound_ent_value(ent, i);
332 c = (int) get_tarval_long(get_Const_tarval(irn));
335 case '"' : obstack_printf(obst, "\\\""); break;
336 case '\n': obstack_printf(obst, "\\n"); break;
337 case '\r': obstack_printf(obst, "\\r"); break;
338 case '\t': obstack_printf(obst, "\\t"); break;
341 obstack_printf(obst, "%c", c);
343 obstack_printf(obst, "%O", c);
347 obstack_printf(obst, "\"\n");
357 * Dumps the initialization of global variables that are not
360 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
362 ir_type *ty = get_entity_type(ent);
363 const char *ld_name = get_entity_ld_name(ent);
365 struct obstack *obst = data_obstack;
368 * FIXME: did NOT work for partly constant values
370 if (! is_Method_type(ty)) {
371 ent_variability variability = get_entity_variability(ent);
372 visibility visibility = get_entity_visibility(ent);
374 if (variability == variability_constant) {
375 /* a constant entity, put it on the rdata */
376 obst = rdata_obstack;
379 /* check, wether it is initialized, if yes create data */
380 if (variability != variability_uninitialized) {
381 if (visibility == visibility_external_visible) {
382 obstack_printf(obst, ".globl\t%s\n", ld_name);
384 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
385 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
387 align = get_type_alignment_bytes(ty);
388 TEMPLATE_dump_align(obst, align);
390 obstack_printf(obst, "%s:\n", ld_name);
392 if (is_atomic_type(ty)) {
393 if (get_entity_visibility(ent) != visibility_external_allocated)
394 dump_atomic_init(obst, get_atomic_ent_value(ent));
399 if (ent_is_string_const(ent)) {
400 dump_string_cst(obst, ent);
402 else if (is_Array_type(ty)) {
405 /* potential spare values should be already included! */
406 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
407 entity *step = get_compound_ent_value_member(ent, i);
408 ir_type *stype = get_entity_type(step);
410 if (get_type_mode(stype)) {
411 int align = (get_type_alignment_bits(stype) + 7) >> 3;
412 int n = size % align;
415 obstack_printf(obst, "\t.zero\t%d\n", align - n);
419 dump_atomic_init(obst, get_compound_ent_value(ent, i));
420 size += get_type_size_bytes(stype);
422 filler = get_type_size_bytes(ty) - size;
425 obstack_printf(obst, "\t.zero\t%d\n", filler);
427 else if (is_compound_type(ty)) {
431 /* Compound entities are NOT sorted.
432 * The sorting strategy used doesn't work for `value' compound fields nor
433 * for partially_constant entities.
437 * in the worst case, every entity allocates one byte, so the type
438 * size should be equal or bigger the number of fields
440 type_size = get_type_size_bytes(ty);
441 vals = xcalloc(type_size, sizeof(*vals));
443 /* collect the values and store them at the offsets */
444 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
445 int graph_length, aipos, offset;
448 compound_graph_path *path = get_compound_ent_value_path(ent, i);
450 /* get the access path to the costant value */
451 graph_length = get_compound_graph_path_length(path);
452 ai = xcalloc(graph_length, sizeof(struct arr_info));
454 /* We wanna know how many arrays are on the path to the entity. We also have to know how
455 * many elements each array holds to calculate the offset for the entity. */
456 for (j = 0; j < graph_length; j++) {
457 entity *step = get_compound_graph_path_node(path, j);
458 ir_type *step_type = get_entity_type(step);
459 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
462 if (is_Array_type(step_type))
463 for (k = 0; k < get_array_n_dimensions(step_type); k++)
464 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
466 ai[j].n_elems = n ? all_n + 1 : 0;
468 ai[j].size = ty_size;
471 aipos = graph_length - 1;
474 for (offset = j = 0; j < graph_length; j++) {
475 entity *step = get_compound_graph_path_node(path, j);
476 ir_type *step_type = get_entity_type(step);
477 int ent_ofs = get_entity_offset_bytes(step);
480 /* add all positive offsets (= offsets in structs) */
481 if (ent_ofs >= 0) offset += ent_ofs;
483 if (j == graph_length - 1) {
484 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
486 /* Search the next free position in vals depending on the information from above (ai). */
487 while (vals[offset]) {
488 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
490 ai[aipos].visit_cnt++;
493 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
494 stepsize = ai[aipos--].size;
499 assert(aipos >= 0 && "couldn't store entity");
500 vals[offset] = get_compound_ent_value(ent, i);
507 /* now write them sorted */
508 for(i = 0; i < type_size; ) {
510 dump_atomic_init(obst, vals[i]);
511 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
515 obstack_printf(obst, "\t.byte\t0\n");
522 assert(0 && "unsupported type");
525 obstack_printf(obst, "\n");
527 else if (visibility != visibility_external_allocated) {
528 if (visibility == visibility_local) {
529 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
532 /* calculate the alignment */
533 align = get_type_alignment_bytes(ty);
534 h = highest_bit(align);
536 if ((1 << h) < align)
543 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
549 * Dumps declarations of global variables and the initialization code.
551 void TEMPLATE_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
553 ir_type *gt = get_glob_type();
554 int i, n = get_class_n_members(gt);
556 for (i = 0; i < n; i++)
557 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
560 /************************************************************************/
562 void TEMPLATE_gen_decls(FILE *out) {
563 struct obstack rodata, data, comm;
567 obstack_init(&rodata);
571 TEMPLATE_dump_globals(&rodata, &data, &comm);
573 size = obstack_object_size(&data);
574 cp = obstack_finish(&data);
576 fprintf(out, "\t.data\n");
577 fwrite(cp, 1, size, out);
580 size = obstack_object_size(&rodata);
581 cp = obstack_finish(&rodata);
583 fprintf(out, "\t.section\t.rodata\n");
584 fwrite(cp, 1, size, out);
587 size = obstack_object_size(&comm);
588 cp = obstack_finish(&comm);
590 fprintf(out, "\t.common\n");
591 fwrite(cp, 1, size, out);
594 obstack_free(&rodata, NULL);
595 obstack_free(&data, NULL);
596 obstack_free(&comm, NULL);