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_type_size:
188 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
191 case symconst_type_align:
192 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
196 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
201 do_dump_atomic_init(obst, get_Add_left(init));
202 obstack_printf(obst, " + ");
203 do_dump_atomic_init(obst, get_Add_right(init));
207 do_dump_atomic_init(obst, get_Sub_left(init));
208 obstack_printf(obst, " - ");
209 do_dump_atomic_init(obst, get_Sub_right(init));
213 do_dump_atomic_init(obst, get_Mul_left(init));
214 obstack_printf(obst, " * ");
215 do_dump_atomic_init(obst, get_Mul_right(init));
219 assert(0 && "dump_atomic_init(): unknown IR-node");
224 * dump an atomic value
226 static void dump_atomic_init(struct obstack *obst, ir_node *init)
228 ir_mode *mode = get_irn_mode(init);
229 int bytes = get_mode_size_bytes(mode);
234 obstack_printf(obst, "\t.byte\t");
238 obstack_printf(obst, "\t.value\t");
242 obstack_printf(obst, "\t.long\t");
246 obstack_printf(obst, "\t.quad\t");
251 /* handled in arith */
255 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
259 do_dump_atomic_init(obst, init);
260 obstack_printf(obst, "\n");
263 /************************************************************************/
264 /* Routines to dump global variables */
265 /************************************************************************/
268 * Determine if an entity is a string constant
269 * @param ent The entity
270 * @return 1 if it is a string constant, 0 otherwise
272 static int ent_is_string_const(entity *ent)
277 ty = get_entity_type(ent);
279 /* if it's an array */
280 if (is_Array_type(ty)) {
281 ir_type *elm_ty = get_array_element_type(ty);
283 /* and the array's element type is primitive */
284 if (is_Primitive_type(elm_ty)) {
285 ir_mode *mode = get_type_mode(elm_ty);
288 * and the mode of the element type is an int of
289 * the same size as the byte mode
291 if (mode_is_int(mode)
292 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
296 n = get_compound_ent_n_values(ent);
297 for (i = 0; i < n; ++i) {
298 ir_node *irn = get_compound_ent_value(ent, i);
299 if(get_irn_opcode(irn) != iro_Const)
302 c = (int) get_tarval_long(get_Const_tarval(irn));
304 if((i < n - 1 && !(isgraph(c) || isspace(c)))
305 || (i == n - 1 && c != '\0'))
318 * Dump a atring constant.
319 * No checks are made!!
320 * @param obst The obst to dump on.
321 * @param ent The entity to dump.
323 static void dump_string_cst(struct obstack *obst, entity *ent)
327 obstack_printf(obst, "\t.string \"");
328 n = get_compound_ent_n_values(ent);
330 for (i = 0; i < n-1; ++i) {
334 irn = get_compound_ent_value(ent, i);
335 c = (int) get_tarval_long(get_Const_tarval(irn));
338 case '"' : obstack_printf(obst, "\\\""); break;
339 case '\n': obstack_printf(obst, "\\n"); break;
340 case '\r': obstack_printf(obst, "\\r"); break;
341 case '\t': obstack_printf(obst, "\\t"); break;
344 obstack_printf(obst, "%c", c);
346 obstack_printf(obst, "%O", c);
350 obstack_printf(obst, "\"\n");
360 * Dumps the initialization of global variables that are not
363 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
365 ir_type *ty = get_entity_type(ent);
366 const char *ld_name = get_entity_ld_name(ent);
368 struct obstack *obst = data_obstack;
371 * FIXME: did NOT work for partly constant values
373 if (! is_Method_type(ty)) {
374 ir_variability variability = get_entity_variability(ent);
375 ir_visibility visibility = get_entity_visibility(ent);
377 if (variability == variability_constant) {
378 /* a constant entity, put it on the rdata */
379 obst = rdata_obstack;
382 /* check, wether it is initialized, if yes create data */
383 if (variability != variability_uninitialized) {
384 if (visibility == visibility_external_visible) {
385 obstack_printf(obst, ".globl\t%s\n", ld_name);
387 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
388 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
390 align = get_type_alignment_bytes(ty);
391 TEMPLATE_dump_align(obst, align);
393 obstack_printf(obst, "%s:\n", ld_name);
395 if (is_atomic_type(ty)) {
396 if (get_entity_visibility(ent) != visibility_external_allocated)
397 dump_atomic_init(obst, get_atomic_ent_value(ent));
402 if (ent_is_string_const(ent)) {
403 dump_string_cst(obst, ent);
405 else if (is_Array_type(ty)) {
408 /* potential spare values should be already included! */
409 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
410 entity *step = get_compound_ent_value_member(ent, i);
411 ir_type *stype = get_entity_type(step);
413 if (get_type_mode(stype)) {
414 int align = (get_type_alignment_bits(stype) + 7) >> 3;
415 int n = size % align;
418 obstack_printf(obst, "\t.zero\t%d\n", align - n);
422 dump_atomic_init(obst, get_compound_ent_value(ent, i));
423 size += get_type_size_bytes(stype);
425 filler = get_type_size_bytes(ty) - size;
428 obstack_printf(obst, "\t.zero\t%d\n", filler);
430 else if (is_compound_type(ty)) {
434 /* Compound entities are NOT sorted.
435 * The sorting strategy used doesn't work for `value' compound fields nor
436 * for partially_constant entities.
440 * in the worst case, every entity allocates one byte, so the type
441 * size should be equal or bigger the number of fields
443 type_size = get_type_size_bytes(ty);
444 vals = xcalloc(type_size, sizeof(*vals));
446 /* collect the values and store them at the offsets */
447 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
448 int graph_length, aipos, offset;
451 compound_graph_path *path = get_compound_ent_value_path(ent, i);
453 /* get the access path to the costant value */
454 graph_length = get_compound_graph_path_length(path);
455 ai = xcalloc(graph_length, sizeof(struct arr_info));
457 /* We wanna know how many arrays are on the path to the entity. We also have to know how
458 * many elements each array holds to calculate the offset for the entity. */
459 for (j = 0; j < graph_length; j++) {
460 entity *step = get_compound_graph_path_node(path, j);
461 ir_type *step_type = get_entity_type(step);
462 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
465 if (is_Array_type(step_type))
466 for (k = 0; k < get_array_n_dimensions(step_type); k++)
467 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
469 ai[j].n_elems = n ? all_n + 1 : 0;
471 ai[j].size = ty_size;
474 aipos = graph_length - 1;
477 for (offset = j = 0; j < graph_length; j++) {
478 entity *step = get_compound_graph_path_node(path, j);
479 ir_type *step_type = get_entity_type(step);
480 int ent_ofs = get_entity_offset_bytes(step);
483 /* add all positive offsets (= offsets in structs) */
484 if (ent_ofs >= 0) offset += ent_ofs;
486 if (j == graph_length - 1) {
487 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
489 /* Search the next free position in vals depending on the information from above (ai). */
490 while (vals[offset]) {
491 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
493 ai[aipos].visit_cnt++;
496 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
497 stepsize = ai[aipos--].size;
502 assert(aipos >= 0 && "couldn't store entity");
503 vals[offset] = get_compound_ent_value(ent, i);
510 /* now write them sorted */
511 for(i = 0; i < type_size; ) {
513 dump_atomic_init(obst, vals[i]);
514 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
518 obstack_printf(obst, "\t.byte\t0\n");
525 assert(0 && "unsupported type");
528 obstack_printf(obst, "\n");
530 else if (visibility != visibility_external_allocated) {
531 if (visibility == visibility_local) {
532 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
535 /* calculate the alignment */
536 align = get_type_alignment_bytes(ty);
537 h = highest_bit(align);
539 if ((1 << h) < align)
546 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
552 * Dumps declarations of global variables and the initialization code.
554 void TEMPLATE_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
556 ir_type *gt = get_glob_type();
557 int i, n = get_class_n_members(gt);
559 for (i = 0; i < n; i++)
560 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
563 /************************************************************************/
565 void TEMPLATE_gen_decls(FILE *out) {
566 struct obstack rodata, data, comm;
570 obstack_init(&rodata);
574 TEMPLATE_dump_globals(&rodata, &data, &comm);
576 size = obstack_object_size(&data);
577 cp = obstack_finish(&data);
579 fprintf(out, "\t.data\n");
580 fwrite(cp, 1, size, out);
583 size = obstack_object_size(&rodata);
584 cp = obstack_finish(&rodata);
586 fprintf(out, "\t.section\t.rodata\n");
587 fwrite(cp, 1, size, out);
590 size = obstack_object_size(&comm);
591 cp = obstack_finish(&comm);
593 fprintf(out, "\t.common\n");
594 fwrite(cp, 1, size, out);
597 obstack_free(&rodata, NULL);
598 obstack_free(&data, NULL);
599 obstack_free(&comm, NULL);