2 * Dumps global variables and constants as ia32 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
24 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
31 #include "ia32_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 ia32_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 ia32_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)));
191 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
195 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
200 do_dump_atomic_init(obst, get_Add_left(init));
201 obstack_printf(obst, " + ");
202 do_dump_atomic_init(obst, get_Add_right(init));
206 do_dump_atomic_init(obst, get_Sub_left(init));
207 obstack_printf(obst, " - ");
208 do_dump_atomic_init(obst, get_Sub_right(init));
212 do_dump_atomic_init(obst, get_Mul_left(init));
213 obstack_printf(obst, " * ");
214 do_dump_atomic_init(obst, get_Mul_right(init));
218 assert(0 && "dump_atomic_init(): unknown IR-node");
223 * dump an atomic value
225 static void dump_atomic_init(struct obstack *obst, ir_node *init)
227 ir_mode *mode = get_irn_mode(init);
228 int bytes = get_mode_size_bytes(mode);
233 obstack_printf(obst, "\t.byte\t");
237 obstack_printf(obst, "\t.value\t");
241 obstack_printf(obst, "\t.long\t");
245 obstack_printf(obst, "\t.quad\t");
250 /* handled in arith */
254 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
258 do_dump_atomic_init(obst, init);
259 obstack_printf(obst, "\n");
262 /************************************************************************/
263 /* Routines to dump global variables */
264 /************************************************************************/
267 * Determine if an entity is a string constant
268 * @param ent The entity
269 * @return 1 if it is a string constant, 0 otherwise
271 static int ent_is_string_const(entity *ent)
276 ty = get_entity_type(ent);
278 /* if it's an array */
279 if (is_Array_type(ty)) {
280 ir_type *elm_ty = get_array_element_type(ty);
282 /* and the array's element type is primitive */
283 if (is_Primitive_type(elm_ty)) {
284 ir_mode *mode = get_type_mode(elm_ty);
287 * and the mode of the element type is an int of
288 * the same size as the byte mode
290 if (mode_is_int(mode)
291 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
295 n = get_compound_ent_n_values(ent);
296 for (i = 0; i < n; ++i) {
297 ir_node *irn = get_compound_ent_value(ent, i);
298 if(get_irn_opcode(irn) != iro_Const)
301 c = (int) get_tarval_long(get_Const_tarval(irn));
303 if((i < n - 1 && !(isgraph(c) || isspace(c)))
304 || (i == n - 1 && c != '\0'))
317 * Dump a atring constant.
318 * No checks are made!!
319 * @param obst The obst to dump on.
320 * @param ent The entity to dump.
322 static void dump_string_cst(struct obstack *obst, entity *ent)
326 obstack_printf(obst, "\t.string \"");
327 n = get_compound_ent_n_values(ent);
329 for (i = 0; i < n-1; ++i) {
333 irn = get_compound_ent_value(ent, i);
334 c = (int) get_tarval_long(get_Const_tarval(irn));
337 case '"' : obstack_printf(obst, "\\\""); break;
338 case '\n': obstack_printf(obst, "\\n"); break;
339 case '\r': obstack_printf(obst, "\\r"); break;
340 case '\t': obstack_printf(obst, "\\t"); break;
343 obstack_printf(obst, "%c", c);
345 obstack_printf(obst, "%O", c);
349 obstack_printf(obst, "\"\n");
359 * Dumps the initialization of global variables that are not
362 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
364 ir_type *ty = get_entity_type(ent);
365 const char *ld_name = get_entity_ld_name(ent);
367 struct obstack *obst = data_obstack;
370 * FIXME: did NOT work for partly constant values
372 if (! is_Method_type(ty)) {
373 ent_variability variability = get_entity_variability(ent);
374 visibility visibility = get_entity_visibility(ent);
376 if (variability == variability_constant) {
377 /* a constant entity, put it on the rdata */
378 obst = rdata_obstack;
381 /* check, wether it is initialized, if yes create data */
382 if (variability != variability_uninitialized) {
383 if (visibility == visibility_external_visible) {
384 obstack_printf(obst, ".globl\t%s\n", ld_name);
386 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
387 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
389 align = get_type_alignment_bytes(ty);
390 ia32_dump_align(obst, align);
392 obstack_printf(obst, "%s:\n", ld_name);
394 if (is_atomic_type(ty)) {
395 if (get_entity_visibility(ent) != visibility_external_allocated)
396 dump_atomic_init(obst, get_atomic_ent_value(ent));
401 if (ent_is_string_const(ent)) {
402 dump_string_cst(obst, ent);
404 else if (is_Array_type(ty)) {
407 /* potential spare values should be already included! */
408 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
409 entity *step = get_compound_ent_value_member(ent, i);
410 ir_type *stype = get_entity_type(step);
412 if (get_type_mode(stype)) {
413 int align = (get_type_alignment_bits(stype) + 7) >> 3;
414 int n = size % align;
417 obstack_printf(obst, "\t.zero\t%d\n", align - n);
421 dump_atomic_init(obst, get_compound_ent_value(ent, i));
422 size += get_type_size_bytes(stype);
424 filler = get_type_size_bytes(ty) - size;
427 obstack_printf(obst, "\t.zero\t%d\n", filler);
429 else if (is_compound_type(ty)) {
433 /* Compound entities are NOT sorted.
434 * The sorting strategy used doesn't work for `value' compound fields nor
435 * for partially_constant entities.
439 * in the worst case, every entity allocates one byte, so the type
440 * size should be equal or bigger the number of fields
442 type_size = get_type_size_bytes(ty);
443 vals = xcalloc(type_size, sizeof(*vals));
445 /* collect the values and store them at the offsets */
446 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
447 int graph_length, aipos, offset;
450 compound_graph_path *path = get_compound_ent_value_path(ent, i);
452 /* get the access path to the costant value */
453 graph_length = get_compound_graph_path_length(path);
454 ai = xcalloc(graph_length, sizeof(struct arr_info));
456 /* We wanna know how many arrays are on the path to the entity. We also have to know how
457 * many elements each array holds to calculate the offset for the entity. */
458 for (j = 0; j < graph_length; j++) {
459 entity *step = get_compound_graph_path_node(path, j);
460 ir_type *step_type = get_entity_type(step);
461 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
464 if (is_Array_type(step_type))
465 for (k = 0; k < get_array_n_dimensions(step_type); k++)
466 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
468 ai[j].n_elems = n ? all_n + 1 : 0;
470 ai[j].size = ty_size;
473 aipos = graph_length - 1;
476 for (offset = j = 0; j < graph_length; j++) {
477 entity *step = get_compound_graph_path_node(path, j);
478 ir_type *step_type = get_entity_type(step);
479 int ent_ofs = get_entity_offset_bytes(step);
482 /* add all positive offsets (= offsets in structs) */
483 if (ent_ofs >= 0) offset += ent_ofs;
485 if (j == graph_length - 1) {
486 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
488 /* Search the next free position in vals depending on the information from above (ai). */
489 while (vals[offset]) {
490 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
492 ai[aipos].visit_cnt++;
495 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
496 stepsize = ai[aipos--].size;
501 assert(aipos >= 0 && "couldn't store entity");
502 vals[offset] = get_compound_ent_value(ent, i);
509 /* now write them sorted */
510 for(i = 0; i < type_size; ) {
512 dump_atomic_init(obst, vals[i]);
513 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
517 obstack_printf(obst, "\t.byte\t0\n");
524 assert(0 && "unsupported type");
527 obstack_printf(obst, "\n");
529 else if (visibility != visibility_external_allocated) {
530 if (visibility == visibility_local) {
531 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
534 /* calculate the alignment */
535 align = get_type_alignment_bytes(ty);
536 h = highest_bit(align);
538 if ((1 << h) < align)
545 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
551 * Dumps declarations of global variables and the initialization code.
553 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
555 ir_type *gt = get_glob_type();
556 int i, n = get_class_n_members(gt);
558 for (i = 0; i < n; i++)
559 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
562 /************************************************************************/
564 void ia32_gen_decls(FILE *out) {
565 struct obstack rodata, data, comm;
569 obstack_init(&rodata);
573 fprintf(out, "\t.intel_syntax\n");
575 ia32_dump_globals(&rodata, &data, &comm);
577 size = obstack_object_size(&data);
578 cp = obstack_finish(&data);
580 fprintf(out, "\t.data\n");
581 fwrite(cp, 1, size, out);
584 size = obstack_object_size(&rodata);
585 cp = obstack_finish(&rodata);
587 fprintf(out, "\t.section\t.rodata\n");
588 fwrite(cp, 1, size, out);
591 size = obstack_object_size(&comm);
592 cp = obstack_finish(&comm);
594 fprintf(out, "\t.common\n");
595 fwrite(cp, 1, size, out);
598 obstack_free(&rodata, NULL);
599 obstack_free(&data, NULL);
600 obstack_free(&comm, NULL);