2 * Dumps global variables and constants as TEMPLATE assembler.
19 #ifdef obstack_chunk_alloc
20 # undef obstack_chunk_alloc
21 # define obstack_chunk_alloc xmalloc
23 # define obstack_chunk_alloc xmalloc
24 # define obstack_chunk_free free
32 #include "TEMPLATE_gen_decls.h"
34 /************************************************************************/
37 * returns the highest bit value
39 static unsigned highest_bit(unsigned v)
43 if (v >= (1U << 16U)) {
47 if (v >= (1U << 8U)) {
51 if (v >= (1U << 4U)) {
55 if (v >= (1U << 2U)) {
59 if (v >= (1U << 1U)) {
70 * output the alignment
72 static void TEMPLATE_dump_align(struct obstack *obst, int align)
74 int h = highest_bit(align);
81 obstack_printf(obst, "\t.align %d\n", align);
84 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
89 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
93 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
97 obstack_printf(obst, "0x%02x%02x%02x%02x",
98 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
102 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
103 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
104 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
112 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
118 * dump an arithmetic tarval
120 static void TEMPLATE_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
125 obstack_printf(obst, "\t.byte\t");
129 obstack_printf(obst, "\t.value\t");
133 obstack_printf(obst, "\t.long\t");
137 obstack_printf(obst, "\t.quad\t");
145 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
148 dump_arith_tarval(obst, tv, bytes);
153 * dump an atomic value
155 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
157 ir_mode *mode = get_irn_mode(init);
158 int bytes = get_mode_size_bytes(mode);
161 switch (get_irn_opcode(init)) {
164 do_dump_atomic_init(obst, get_Cast_op(init));
168 do_dump_atomic_init(obst, get_Conv_op(init));
172 tv = get_Const_tarval(init);
174 /* beware of old stuff */
175 assert(! mode_is_reference(mode));
177 /* it's a arithmetic value */
178 dump_arith_tarval(obst, tv, bytes);
182 switch (get_SymConst_kind(init)) {
183 case symconst_addr_name:
184 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
187 case symconst_addr_ent:
188 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
191 case symconst_ofs_ent:
192 obstack_printf(obst, "%d", get_entity_offset_bytes(get_SymConst_entity(init)));
195 case symconst_type_size:
196 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
199 case symconst_type_align:
200 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
204 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
209 do_dump_atomic_init(obst, get_Add_left(init));
210 obstack_printf(obst, " + ");
211 do_dump_atomic_init(obst, get_Add_right(init));
215 do_dump_atomic_init(obst, get_Sub_left(init));
216 obstack_printf(obst, " - ");
217 do_dump_atomic_init(obst, get_Sub_right(init));
221 do_dump_atomic_init(obst, get_Mul_left(init));
222 obstack_printf(obst, " * ");
223 do_dump_atomic_init(obst, get_Mul_right(init));
227 assert(0 && "dump_atomic_init(): unknown IR-node");
232 * dump an atomic value
234 static void dump_atomic_init(struct obstack *obst, ir_node *init)
236 ir_mode *mode = get_irn_mode(init);
237 int bytes = get_mode_size_bytes(mode);
242 obstack_printf(obst, "\t.byte\t");
246 obstack_printf(obst, "\t.value\t");
250 obstack_printf(obst, "\t.long\t");
254 obstack_printf(obst, "\t.quad\t");
259 /* handled in arith */
263 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
267 do_dump_atomic_init(obst, init);
268 obstack_printf(obst, "\n");
271 /************************************************************************/
272 /* Routines to dump global variables */
273 /************************************************************************/
276 * Determine if an entity is a string constant
277 * @param ent The entity
278 * @return 1 if it is a string constant, 0 otherwise
280 static int ent_is_string_const(entity *ent)
285 ty = get_entity_type(ent);
287 /* if it's an array */
288 if (is_Array_type(ty)) {
289 ir_type *elm_ty = get_array_element_type(ty);
291 /* and the array's element type is primitive */
292 if (is_Primitive_type(elm_ty)) {
293 ir_mode *mode = get_type_mode(elm_ty);
296 * and the mode of the element type is an int of
297 * the same size as the byte mode
299 if (mode_is_int(mode)
300 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
304 n = get_compound_ent_n_values(ent);
305 for (i = 0; i < n; ++i) {
306 ir_node *irn = get_compound_ent_value(ent, i);
307 if(get_irn_opcode(irn) != iro_Const)
310 c = (int) get_tarval_long(get_Const_tarval(irn));
312 if((i < n - 1 && !(isgraph(c) || isspace(c)))
313 || (i == n - 1 && c != '\0'))
326 * Dump a atring constant.
327 * No checks are made!!
328 * @param obst The obst to dump on.
329 * @param ent The entity to dump.
331 static void dump_string_cst(struct obstack *obst, entity *ent)
335 obstack_printf(obst, "\t.string \"");
336 n = get_compound_ent_n_values(ent);
338 for (i = 0; i < n-1; ++i) {
342 irn = get_compound_ent_value(ent, i);
343 c = (int) get_tarval_long(get_Const_tarval(irn));
346 case '"' : obstack_printf(obst, "\\\""); break;
347 case '\n': obstack_printf(obst, "\\n"); break;
348 case '\r': obstack_printf(obst, "\\r"); break;
349 case '\t': obstack_printf(obst, "\\t"); break;
352 obstack_printf(obst, "%c", c);
354 obstack_printf(obst, "\\%o", c);
358 obstack_printf(obst, "\"\n");
368 * Dumps the initialization of global variables that are not
371 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
373 ir_type *ty = get_entity_type(ent);
374 const char *ld_name = get_entity_ld_name(ent);
376 struct obstack *obst = data_obstack;
379 * FIXME: did NOT work for partly constant values
381 if (! is_Method_type(ty)) {
382 ir_variability variability = get_entity_variability(ent);
383 ir_visibility visibility = get_entity_visibility(ent);
385 if (variability == variability_constant) {
386 /* a constant entity, put it on the rdata */
387 obst = rdata_obstack;
390 /* check, wether it is initialized, if yes create data */
391 if (variability != variability_uninitialized) {
392 if (visibility == visibility_external_visible) {
393 obstack_printf(obst, ".globl\t%s\n", ld_name);
395 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
396 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
398 align = get_type_alignment_bytes(ty);
399 TEMPLATE_dump_align(obst, align);
401 obstack_printf(obst, "%s:\n", ld_name);
403 if (is_atomic_type(ty)) {
404 if (get_entity_visibility(ent) != visibility_external_allocated)
405 dump_atomic_init(obst, get_atomic_ent_value(ent));
410 if (ent_is_string_const(ent)) {
411 dump_string_cst(obst, ent);
413 else if (is_Array_type(ty)) {
416 /* potential spare values should be already included! */
417 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
418 entity *step = get_compound_ent_value_member(ent, i);
419 ir_type *stype = get_entity_type(step);
421 if (get_type_mode(stype)) {
422 int align = (get_type_alignment_bits(stype) + 7) >> 3;
423 int n = size % align;
426 obstack_printf(obst, "\t.zero\t%d\n", align - n);
430 dump_atomic_init(obst, get_compound_ent_value(ent, i));
431 size += get_type_size_bytes(stype);
433 filler = get_type_size_bytes(ty) - size;
436 obstack_printf(obst, "\t.zero\t%d\n", filler);
438 else if (is_compound_type(ty)) {
442 /* Compound entities are NOT sorted.
443 * The sorting strategy used doesn't work for `value' compound fields nor
444 * for partially_constant entities.
448 * in the worst case, every entity allocates one byte, so the type
449 * size should be equal or bigger the number of fields
451 type_size = get_type_size_bytes(ty);
452 vals = xcalloc(type_size, sizeof(*vals));
454 /* collect the values and store them at the offsets */
455 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
456 int graph_length, aipos, offset;
459 compound_graph_path *path = get_compound_ent_value_path(ent, i);
461 /* get the access path to the costant value */
462 graph_length = get_compound_graph_path_length(path);
463 ai = xcalloc(graph_length, sizeof(struct arr_info));
465 /* We wanna know how many arrays are on the path to the entity. We also have to know how
466 * many elements each array holds to calculate the offset for the entity. */
467 for (j = 0; j < graph_length; j++) {
468 entity *step = get_compound_graph_path_node(path, j);
469 ir_type *step_type = get_entity_type(step);
470 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
473 if (is_Array_type(step_type))
474 for (k = 0; k < get_array_n_dimensions(step_type); k++)
475 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
477 ai[j].n_elems = n ? all_n + 1 : 0;
479 ai[j].size = ty_size;
482 aipos = graph_length - 1;
485 for (offset = j = 0; j < graph_length; j++) {
486 entity *step = get_compound_graph_path_node(path, j);
487 ir_type *step_type = get_entity_type(step);
488 int ent_ofs = get_entity_offset_bytes(step);
491 /* add all positive offsets (= offsets in structs) */
492 if (ent_ofs >= 0) offset += ent_ofs;
494 if (j == graph_length - 1) {
495 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
497 /* Search the next free position in vals depending on the information from above (ai). */
498 while (vals[offset]) {
499 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
501 ai[aipos].visit_cnt++;
504 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
505 stepsize = ai[aipos--].size;
510 assert(aipos >= 0 && "couldn't store entity");
511 vals[offset] = get_compound_ent_value(ent, i);
518 /* now write them sorted */
519 for(i = 0; i < type_size; ) {
521 dump_atomic_init(obst, vals[i]);
522 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
526 obstack_printf(obst, "\t.byte\t0\n");
533 assert(0 && "unsupported type");
536 obstack_printf(obst, "\n");
538 else if (visibility != visibility_external_allocated) {
539 if (visibility == visibility_local) {
540 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
543 /* calculate the alignment */
544 align = get_type_alignment_bytes(ty);
545 h = highest_bit(align);
547 if ((1 << h) < align)
554 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
560 * Dumps declarations of global variables and the initialization code.
562 void TEMPLATE_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
564 ir_type *gt = get_glob_type();
565 int i, n = get_class_n_members(gt);
567 for (i = 0; i < n; i++)
568 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
571 /************************************************************************/
573 void TEMPLATE_gen_decls(FILE *out) {
574 struct obstack rodata, data, comm;
578 obstack_init(&rodata);
582 TEMPLATE_dump_globals(&rodata, &data, &comm);
584 size = obstack_object_size(&data);
585 cp = obstack_finish(&data);
587 fprintf(out, "\t.data\n");
588 fwrite(cp, 1, size, out);
591 size = obstack_object_size(&rodata);
592 cp = obstack_finish(&rodata);
594 fprintf(out, "\t.section\t.rodata\n");
595 fwrite(cp, 1, size, out);
598 size = obstack_object_size(&comm);
599 cp = obstack_finish(&comm);
601 fprintf(out, "\t.common\n");
602 fwrite(cp, 1, size, out);
605 obstack_free(&rodata, NULL);
606 obstack_free(&data, NULL);
607 obstack_free(&comm, NULL);