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)));
187 case symconst_ofs_ent:
188 obstack_printf(obst, "%d", get_entity_offset_bytes(get_SymConst_entity(init)));
191 case symconst_type_size:
192 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
195 case symconst_type_align:
196 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
200 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
205 do_dump_atomic_init(obst, get_Add_left(init));
206 obstack_printf(obst, " + ");
207 do_dump_atomic_init(obst, get_Add_right(init));
211 do_dump_atomic_init(obst, get_Sub_left(init));
212 obstack_printf(obst, " - ");
213 do_dump_atomic_init(obst, get_Sub_right(init));
217 do_dump_atomic_init(obst, get_Mul_left(init));
218 obstack_printf(obst, " * ");
219 do_dump_atomic_init(obst, get_Mul_right(init));
223 assert(0 && "dump_atomic_init(): unknown IR-node");
228 * dump an atomic value
230 static void dump_atomic_init(struct obstack *obst, ir_node *init)
232 ir_mode *mode = get_irn_mode(init);
233 int bytes = get_mode_size_bytes(mode);
238 obstack_printf(obst, "\t.byte\t");
242 obstack_printf(obst, "\t.value\t");
246 obstack_printf(obst, "\t.long\t");
250 obstack_printf(obst, "\t.quad\t");
255 /* handled in arith */
259 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
263 do_dump_atomic_init(obst, init);
264 obstack_printf(obst, "\n");
267 /************************************************************************/
268 /* Routines to dump global variables */
269 /************************************************************************/
272 * Determine if an entity is a string constant
273 * @param ent The entity
274 * @return 1 if it is a string constant, 0 otherwise
276 static int ent_is_string_const(entity *ent)
281 ty = get_entity_type(ent);
283 /* if it's an array */
284 if (is_Array_type(ty)) {
285 ir_type *elm_ty = get_array_element_type(ty);
287 /* and the array's element type is primitive */
288 if (is_Primitive_type(elm_ty)) {
289 ir_mode *mode = get_type_mode(elm_ty);
292 * and the mode of the element type is an int of
293 * the same size as the byte mode
295 if (mode_is_int(mode)
296 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
300 n = get_compound_ent_n_values(ent);
301 for (i = 0; i < n; ++i) {
302 ir_node *irn = get_compound_ent_value(ent, i);
303 if(get_irn_opcode(irn) != iro_Const)
306 c = (int) get_tarval_long(get_Const_tarval(irn));
308 if((i < n - 1 && !(isgraph(c) || isspace(c)))
309 || (i == n - 1 && c != '\0'))
322 * Dump a atring constant.
323 * No checks are made!!
324 * @param obst The obst to dump on.
325 * @param ent The entity to dump.
327 static void dump_string_cst(struct obstack *obst, entity *ent)
331 obstack_printf(obst, "\t.string \"");
332 n = get_compound_ent_n_values(ent);
334 for (i = 0; i < n-1; ++i) {
338 irn = get_compound_ent_value(ent, i);
339 c = (int) get_tarval_long(get_Const_tarval(irn));
342 case '"' : obstack_printf(obst, "\\\""); break;
343 case '\n': obstack_printf(obst, "\\n"); break;
344 case '\r': obstack_printf(obst, "\\r"); break;
345 case '\t': obstack_printf(obst, "\\t"); break;
348 obstack_printf(obst, "%c", c);
350 obstack_printf(obst, "%O", c);
354 obstack_printf(obst, "\"\n");
364 * Dumps the initialization of global variables that are not
367 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
369 ir_type *ty = get_entity_type(ent);
370 const char *ld_name = get_entity_ld_name(ent);
372 struct obstack *obst = data_obstack;
375 * FIXME: did NOT work for partly constant values
377 if (! is_Method_type(ty)) {
378 ir_variability variability = get_entity_variability(ent);
379 ir_visibility visibility = get_entity_visibility(ent);
381 if (variability == variability_constant) {
382 /* a constant entity, put it on the rdata */
383 obst = rdata_obstack;
386 /* check, wether it is initialized, if yes create data */
387 if (variability != variability_uninitialized) {
388 if (visibility == visibility_external_visible) {
389 obstack_printf(obst, ".globl\t%s\n", ld_name);
391 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
392 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
394 align = get_type_alignment_bytes(ty);
395 TEMPLATE_dump_align(obst, align);
397 obstack_printf(obst, "%s:\n", ld_name);
399 if (is_atomic_type(ty)) {
400 if (get_entity_visibility(ent) != visibility_external_allocated)
401 dump_atomic_init(obst, get_atomic_ent_value(ent));
406 if (ent_is_string_const(ent)) {
407 dump_string_cst(obst, ent);
409 else if (is_Array_type(ty)) {
412 /* potential spare values should be already included! */
413 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
414 entity *step = get_compound_ent_value_member(ent, i);
415 ir_type *stype = get_entity_type(step);
417 if (get_type_mode(stype)) {
418 int align = (get_type_alignment_bits(stype) + 7) >> 3;
419 int n = size % align;
422 obstack_printf(obst, "\t.zero\t%d\n", align - n);
426 dump_atomic_init(obst, get_compound_ent_value(ent, i));
427 size += get_type_size_bytes(stype);
429 filler = get_type_size_bytes(ty) - size;
432 obstack_printf(obst, "\t.zero\t%d\n", filler);
434 else if (is_compound_type(ty)) {
438 /* Compound entities are NOT sorted.
439 * The sorting strategy used doesn't work for `value' compound fields nor
440 * for partially_constant entities.
444 * in the worst case, every entity allocates one byte, so the type
445 * size should be equal or bigger the number of fields
447 type_size = get_type_size_bytes(ty);
448 vals = xcalloc(type_size, sizeof(*vals));
450 /* collect the values and store them at the offsets */
451 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
452 int graph_length, aipos, offset;
455 compound_graph_path *path = get_compound_ent_value_path(ent, i);
457 /* get the access path to the costant value */
458 graph_length = get_compound_graph_path_length(path);
459 ai = xcalloc(graph_length, sizeof(struct arr_info));
461 /* We wanna know how many arrays are on the path to the entity. We also have to know how
462 * many elements each array holds to calculate the offset for the entity. */
463 for (j = 0; j < graph_length; j++) {
464 entity *step = get_compound_graph_path_node(path, j);
465 ir_type *step_type = get_entity_type(step);
466 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
469 if (is_Array_type(step_type))
470 for (k = 0; k < get_array_n_dimensions(step_type); k++)
471 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
473 ai[j].n_elems = n ? all_n + 1 : 0;
475 ai[j].size = ty_size;
478 aipos = graph_length - 1;
481 for (offset = j = 0; j < graph_length; j++) {
482 entity *step = get_compound_graph_path_node(path, j);
483 ir_type *step_type = get_entity_type(step);
484 int ent_ofs = get_entity_offset_bytes(step);
487 /* add all positive offsets (= offsets in structs) */
488 if (ent_ofs >= 0) offset += ent_ofs;
490 if (j == graph_length - 1) {
491 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
493 /* Search the next free position in vals depending on the information from above (ai). */
494 while (vals[offset]) {
495 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
497 ai[aipos].visit_cnt++;
500 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
501 stepsize = ai[aipos--].size;
506 assert(aipos >= 0 && "couldn't store entity");
507 vals[offset] = get_compound_ent_value(ent, i);
514 /* now write them sorted */
515 for(i = 0; i < type_size; ) {
517 dump_atomic_init(obst, vals[i]);
518 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
522 obstack_printf(obst, "\t.byte\t0\n");
529 assert(0 && "unsupported type");
532 obstack_printf(obst, "\n");
534 else if (visibility != visibility_external_allocated) {
535 if (visibility == visibility_local) {
536 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
539 /* calculate the alignment */
540 align = get_type_alignment_bytes(ty);
541 h = highest_bit(align);
543 if ((1 << h) < align)
550 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
556 * Dumps declarations of global variables and the initialization code.
558 void TEMPLATE_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
560 ir_type *gt = get_glob_type();
561 int i, n = get_class_n_members(gt);
563 for (i = 0; i < n; i++)
564 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
567 /************************************************************************/
569 void TEMPLATE_gen_decls(FILE *out) {
570 struct obstack rodata, data, comm;
574 obstack_init(&rodata);
578 TEMPLATE_dump_globals(&rodata, &data, &comm);
580 size = obstack_object_size(&data);
581 cp = obstack_finish(&data);
583 fprintf(out, "\t.data\n");
584 fwrite(cp, 1, size, out);
587 size = obstack_object_size(&rodata);
588 cp = obstack_finish(&rodata);
590 fprintf(out, "\t.section\t.rodata\n");
591 fwrite(cp, 1, size, out);
594 size = obstack_object_size(&comm);
595 cp = obstack_finish(&comm);
597 fprintf(out, "\t.common\n");
598 fwrite(cp, 1, size, out);
601 obstack_free(&rodata, NULL);
602 obstack_free(&data, NULL);
603 obstack_free(&comm, NULL);