2 * Dumps global variables and constants as TEMPLATE assembler.
18 #ifdef obstack_chunk_alloc
19 # undef obstack_chunk_alloc
20 # define obstack_chunk_alloc xmalloc
22 # define obstack_chunk_alloc xmalloc
23 # define obstack_chunk_free free
31 #include "TEMPLATE_gen_decls.h"
33 /************************************************************************/
36 * returns the highest bit value
38 static unsigned highest_bit(unsigned v)
42 if (v >= (1U << 16U)) {
46 if (v >= (1U << 8U)) {
50 if (v >= (1U << 4U)) {
54 if (v >= (1U << 2U)) {
58 if (v >= (1U << 1U)) {
69 * output the alignment
71 static void TEMPLATE_dump_align(struct obstack *obst, int align)
73 int h = highest_bit(align);
80 obstack_printf(obst, "\t.align %d\n", align);
83 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
88 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
92 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
96 obstack_printf(obst, "0x%02x%02x%02x%02x",
97 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 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
102 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
103 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
111 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
117 * dump an arithmetic tarval
119 static void TEMPLATE_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
124 obstack_printf(obst, "\t.byte\t");
128 obstack_printf(obst, "\t.value\t");
132 obstack_printf(obst, "\t.long\t");
136 obstack_printf(obst, "\t.quad\t");
144 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
147 dump_arith_tarval(obst, tv, bytes);
152 * dump an atomic value
154 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
156 ir_mode *mode = get_irn_mode(init);
157 int bytes = get_mode_size_bytes(mode);
160 switch (get_irn_opcode(init)) {
163 do_dump_atomic_init(obst, get_Cast_op(init));
167 do_dump_atomic_init(obst, get_Conv_op(init));
171 tv = get_Const_tarval(init);
173 /* beware of old stuff */
174 assert(! mode_is_reference(mode));
176 /* it's a arithmetic value */
177 dump_arith_tarval(obst, tv, bytes);
181 switch (get_SymConst_kind(init)) {
182 case symconst_addr_name:
183 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
186 case symconst_addr_ent:
187 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
190 case symconst_ofs_ent:
191 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
194 case symconst_type_size:
195 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
198 case symconst_type_align:
199 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
203 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
208 do_dump_atomic_init(obst, get_Add_left(init));
209 obstack_printf(obst, " + ");
210 do_dump_atomic_init(obst, get_Add_right(init));
214 do_dump_atomic_init(obst, get_Sub_left(init));
215 obstack_printf(obst, " - ");
216 do_dump_atomic_init(obst, get_Sub_right(init));
220 do_dump_atomic_init(obst, get_Mul_left(init));
221 obstack_printf(obst, " * ");
222 do_dump_atomic_init(obst, get_Mul_right(init));
226 assert(0 && "dump_atomic_init(): unknown IR-node");
231 * dump an atomic value
233 static void dump_atomic_init(struct obstack *obst, ir_node *init)
235 ir_mode *mode = get_irn_mode(init);
236 int bytes = get_mode_size_bytes(mode);
241 obstack_printf(obst, "\t.byte\t");
245 obstack_printf(obst, "\t.value\t");
249 obstack_printf(obst, "\t.long\t");
253 obstack_printf(obst, "\t.quad\t");
258 /* handled in arith */
262 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
266 do_dump_atomic_init(obst, init);
267 obstack_printf(obst, "\n");
270 /************************************************************************/
271 /* Routines to dump global variables */
272 /************************************************************************/
275 * Determine if an ir_entity is a string constant
276 * @param ent The ir_entity
277 * @return 1 if it is a string constant, 0 otherwise
279 static int ent_is_string_const(ir_entity *ent)
284 ty = get_entity_type(ent);
286 /* if it's an array */
287 if (is_Array_type(ty)) {
288 ir_type *elm_ty = get_array_element_type(ty);
290 /* and the array's element type is primitive */
291 if (is_Primitive_type(elm_ty)) {
292 ir_mode *mode = get_type_mode(elm_ty);
295 * and the mode of the element type is an int of
296 * the same size as the byte mode
298 if (mode_is_int(mode)
299 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
303 n = get_compound_ent_n_values(ent);
304 for (i = 0; i < n; ++i) {
305 ir_node *irn = get_compound_ent_value(ent, i);
306 if(get_irn_opcode(irn) != iro_Const)
309 c = (int) get_tarval_long(get_Const_tarval(irn));
311 if((i < n - 1 && !(isgraph(c) || isspace(c)))
312 || (i == n - 1 && c != '\0'))
325 * Dump a atring constant.
326 * No checks are made!!
327 * @param obst The obst to dump on.
328 * @param ent The ir_entity to dump.
330 static void dump_string_cst(struct obstack *obst, ir_entity *ent)
334 obstack_printf(obst, "\t.string \"");
335 n = get_compound_ent_n_values(ent);
337 for (i = 0; i < n-1; ++i) {
341 irn = get_compound_ent_value(ent, i);
342 c = (int) get_tarval_long(get_Const_tarval(irn));
345 case '"' : obstack_printf(obst, "\\\""); break;
346 case '\n': obstack_printf(obst, "\\n"); break;
347 case '\r': obstack_printf(obst, "\\r"); break;
348 case '\t': obstack_printf(obst, "\\t"); break;
351 obstack_printf(obst, "%c", c);
353 obstack_printf(obst, "\\%o", c);
357 obstack_printf(obst, "\"\n");
367 * Dumps the initialization of global variables that are not
370 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, ir_entity *ent)
372 ir_type *ty = get_entity_type(ent);
373 const char *ld_name = get_entity_ld_name(ent);
375 struct obstack *obst = data_obstack;
378 * FIXME: did NOT work for partly constant values
380 if (! is_Method_type(ty)) {
381 ir_variability variability = get_entity_variability(ent);
382 ir_visibility visibility = get_entity_visibility(ent);
384 if (variability == variability_constant) {
385 /* a constant ir_entity, put it on the rdata */
386 obst = rdata_obstack;
389 /* check, wether it is initialized, if yes create data */
390 if (variability != variability_uninitialized) {
391 if (visibility == visibility_external_visible) {
392 obstack_printf(obst, ".globl\t%s\n", ld_name);
394 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
395 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
397 align = get_type_alignment_bytes(ty);
398 TEMPLATE_dump_align(obst, align);
400 obstack_printf(obst, "%s:\n", ld_name);
402 if (is_atomic_type(ty)) {
403 if (get_entity_visibility(ent) != visibility_external_allocated)
404 dump_atomic_init(obst, get_atomic_ent_value(ent));
409 if (ent_is_string_const(ent)) {
410 dump_string_cst(obst, ent);
412 else if (is_Array_type(ty)) {
415 /* potential spare values should be already included! */
416 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
417 ir_entity *step = get_compound_ent_value_member(ent, i);
418 ir_type *stype = get_entity_type(step);
420 if (get_type_mode(stype)) {
421 int align = (get_type_alignment_bits(stype) + 7) >> 3;
422 int n = size % align;
425 obstack_printf(obst, "\t.zero\t%d\n", align - n);
429 dump_atomic_init(obst, get_compound_ent_value(ent, i));
430 size += get_type_size_bytes(stype);
432 filler = get_type_size_bytes(ty) - size;
435 obstack_printf(obst, "\t.zero\t%d\n", filler);
437 else if (is_compound_type(ty)) {
441 /* Compound entities are NOT sorted.
442 * The sorting strategy used doesn't work for `value' compound fields nor
443 * for partially_constant entities.
447 * in the worst case, every ir_entity allocates one byte, so the type
448 * size should be equal or bigger the number of fields
450 type_size = get_type_size_bytes(ty);
451 vals = xcalloc(type_size, sizeof(*vals));
453 /* collect the values and store them at the offsets */
454 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
455 int graph_length, aipos, offset;
458 compound_graph_path *path = get_compound_ent_value_path(ent, i);
460 /* get the access path to the costant value */
461 graph_length = get_compound_graph_path_length(path);
462 ai = xcalloc(graph_length, sizeof(struct arr_info));
464 /* We wanna know how many arrays are on the path to the ir_entity. We also have to know how
465 * many elements each array holds to calculate the offset for the ir_entity. */
466 for (j = 0; j < graph_length; j++) {
467 ir_entity *step = get_compound_graph_path_node(path, j);
468 ir_type *step_type = get_entity_type(step);
469 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
472 if (is_Array_type(step_type))
473 for (k = 0; k < get_array_n_dimensions(step_type); k++)
474 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
476 ai[j].n_elems = n ? all_n + 1 : 0;
478 ai[j].size = ty_size;
481 aipos = graph_length - 1;
484 for (offset = j = 0; j < graph_length; j++) {
485 ir_entity *step = get_compound_graph_path_node(path, j);
486 ir_type *step_type = get_entity_type(step);
487 int ent_ofs = get_entity_offset_bytes(step);
490 /* add all positive offsets (= offsets in structs) */
491 if (ent_ofs >= 0) offset += ent_ofs;
493 if (j == graph_length - 1) {
494 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
496 /* Search the next free position in vals depending on the information from above (ai). */
497 while (vals[offset]) {
498 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
500 ai[aipos].visit_cnt++;
503 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
504 stepsize = ai[aipos--].size;
509 assert(aipos >= 0 && "couldn't store ir_entity");
510 vals[offset] = get_compound_ent_value(ent, i);
517 /* now write them sorted */
518 for(i = 0; i < type_size; ) {
520 dump_atomic_init(obst, vals[i]);
521 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
525 obstack_printf(obst, "\t.byte\t0\n");
532 assert(0 && "unsupported type");
535 obstack_printf(obst, "\n");
537 else if (visibility != visibility_external_allocated) {
538 if (visibility == visibility_local) {
539 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
542 /* calculate the alignment */
543 align = get_type_alignment_bytes(ty);
544 h = highest_bit(align);
546 if ((1 << h) < align)
553 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
559 * Dumps declarations of global variables and the initialization code.
561 void TEMPLATE_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
563 ir_type *gt = get_glob_type();
564 int i, n = get_class_n_members(gt);
566 for (i = 0; i < n; i++)
567 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
570 /************************************************************************/
572 void TEMPLATE_gen_decls(FILE *out) {
573 struct obstack rodata, data, comm;
577 obstack_init(&rodata);
581 TEMPLATE_dump_globals(&rodata, &data, &comm);
583 size = obstack_object_size(&data);
584 cp = obstack_finish(&data);
586 fprintf(out, "\t.data\n");
587 fwrite(cp, 1, size, out);
590 size = obstack_object_size(&rodata);
591 cp = obstack_finish(&rodata);
593 fprintf(out, "\t.section\t.rodata\n");
594 fwrite(cp, 1, size, out);
597 size = obstack_object_size(&comm);
598 cp = obstack_finish(&comm);
600 fprintf(out, "\t.common\n");
601 fwrite(cp, 1, size, out);
604 obstack_free(&rodata, NULL);
605 obstack_free(&data, NULL);
606 obstack_free(&comm, NULL);