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 atomic value
119 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
121 ir_mode *mode = get_irn_mode(init);
122 int bytes = get_mode_size_bytes(mode);
125 switch (get_irn_opcode(init)) {
128 do_dump_atomic_init(obst, get_Cast_op(init));
132 do_dump_atomic_init(obst, get_Conv_op(init));
136 tv = get_Const_tarval(init);
138 /* beware of old stuff */
139 assert(! mode_is_reference(mode));
141 /* it's a arithmetic value */
142 dump_arith_tarval(obst, tv, bytes);
146 switch (get_SymConst_kind(init)) {
147 case symconst_addr_name:
148 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
151 case symconst_addr_ent:
152 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
156 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
160 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
165 do_dump_atomic_init(obst, get_Add_left(init));
166 obstack_printf(obst, " + ");
167 do_dump_atomic_init(obst, get_Add_right(init));
171 do_dump_atomic_init(obst, get_Sub_left(init));
172 obstack_printf(obst, " - ");
173 do_dump_atomic_init(obst, get_Sub_right(init));
177 do_dump_atomic_init(obst, get_Mul_left(init));
178 obstack_printf(obst, " * ");
179 do_dump_atomic_init(obst, get_Mul_right(init));
183 assert(0 && "dump_atomic_init(): unknown IR-node");
188 * dump an atomic value
190 static void dump_atomic_init(struct obstack *obst, ir_node *init)
192 ir_mode *mode = get_irn_mode(init);
193 int bytes = get_mode_size_bytes(mode);
198 obstack_printf(obst, "\t.byte\t");
202 obstack_printf(obst, "\t.value\t");
206 obstack_printf(obst, "\t.long\t");
210 obstack_printf(obst, "\t.quad\t");
215 /* handled in arith */
219 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
223 do_dump_atomic_init(obst, init);
224 obstack_printf(obst, "\n");
227 /************************************************************************/
228 /* Routines to dump global variables */
229 /************************************************************************/
232 * Determine if an entity is a string constant
233 * @param ent The entity
234 * @return 1 if it is a string constant, 0 otherwise
236 static int ent_is_string_const(entity *ent)
241 ty = get_entity_type(ent);
243 /* if it's an array */
244 if (is_Array_type(ty)) {
245 ir_type *elm_ty = get_array_element_type(ty);
247 /* and the array's element type is primitive */
248 if (is_Primitive_type(elm_ty)) {
249 ir_mode *mode = get_type_mode(elm_ty);
252 * and the mode of the element type is an int of
253 * the same size as the byte mode
255 if (mode_is_int(mode)
256 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
260 n = get_compound_ent_n_values(ent);
261 for (i = 0; i < n; ++i) {
262 ir_node *irn = get_compound_ent_value(ent, i);
263 if(get_irn_opcode(irn) != iro_Const)
266 c = (int) get_tarval_long(get_Const_tarval(irn));
268 if((i < n - 1 && !(isgraph(c) || isspace(c)))
269 || (i == n - 1 && c != '\0'))
282 * Dump a atring constant.
283 * No checks are made!!
284 * @param obst The obst to dump on.
285 * @param ent The entity to dump.
287 static void dump_string_cst(struct obstack *obst, entity *ent)
291 obstack_printf(obst, "\t.string \"");
292 n = get_compound_ent_n_values(ent);
294 for (i = 0; i < n-1; ++i) {
298 irn = get_compound_ent_value(ent, i);
299 c = (int) get_tarval_long(get_Const_tarval(irn));
302 case '"' : obstack_printf(obst, "\\\""); break;
303 case '\n': obstack_printf(obst, "\\n"); break;
304 case '\r': obstack_printf(obst, "\\r"); break;
305 case '\t': obstack_printf(obst, "\\t"); break;
308 obstack_printf(obst, "%c", c);
310 obstack_printf(obst, "%O", c);
314 obstack_printf(obst, "\"\n");
324 * Dumps the initialization of global variables that are not
327 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
329 ir_type *ty = get_entity_type(ent);
330 const char *ld_name = get_entity_ld_name(ent);
332 struct obstack *obst = data_obstack;
335 * FIXME: did NOT work for partly constant values
337 if (! is_Method_type(ty)) {
338 ent_variability variability = get_entity_variability(ent);
339 visibility visibility = get_entity_visibility(ent);
341 if (variability == variability_constant) {
342 /* a constant entity, put it on the rdata */
343 obst = rdata_obstack;
346 /* check, wether it is initialized, if yes create data */
347 if (variability != variability_uninitialized) {
348 if (visibility == visibility_external_visible) {
349 obstack_printf(obst, ".globl\t%s\n", ld_name);
351 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
352 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
354 align = get_type_alignment_bytes(ty);
355 ia32_dump_align(obst, align);
357 obstack_printf(obst, "%s:\n", ld_name);
359 if (is_atomic_type(ty)) {
360 if (get_entity_visibility(ent) != visibility_external_allocated)
361 dump_atomic_init(obst, get_atomic_ent_value(ent));
366 if (ent_is_string_const(ent)) {
367 dump_string_cst(obst, ent);
369 else if (is_Array_type(ty)) {
372 /* potential spare values should be already included! */
373 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
374 entity *step = get_compound_ent_value_member(ent, i);
375 ir_type *stype = get_entity_type(step);
377 if (get_type_mode(stype)) {
378 int align = (get_type_alignment_bits(stype) + 7) >> 3;
379 int n = size % align;
382 obstack_printf(obst, "\t.zero\t%d\n", align - n);
386 dump_atomic_init(obst, get_compound_ent_value(ent, i));
387 size += get_type_size_bytes(stype);
389 filler = get_type_size_bytes(ty) - size;
392 obstack_printf(obst, "\t.zero\t%d\n", filler);
394 else if (is_compound_type(ty)) {
398 /* Compound entities are NOT sorted.
399 * The sorting strategy used doesn't work for `value' compound fields nor
400 * for partially_constant entities.
404 * in the worst case, every entity allocates one byte, so the type
405 * size should be equal or bigger the number of fields
407 type_size = get_type_size_bytes(ty);
408 vals = xcalloc(type_size, sizeof(*vals));
410 /* collect the values and store them at the offsets */
411 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
412 int graph_length, aipos, offset;
415 compound_graph_path *path = get_compound_ent_value_path(ent, i);
417 /* get the access path to the costant value */
418 graph_length = get_compound_graph_path_length(path);
419 ai = xcalloc(graph_length, sizeof(struct arr_info));
421 /* We wanna know how many arrays are on the path to the entity. We also have to know how
422 * many elements each array holds to calculate the offset for the entity. */
423 for (j = 0; j < graph_length; j++) {
424 entity *step = get_compound_graph_path_node(path, j);
425 ir_type *step_type = get_entity_type(step);
426 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
429 if (is_Array_type(step_type))
430 for (k = 0; k < get_array_n_dimensions(step_type); k++)
431 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
433 ai[j].n_elems = n ? all_n + 1 : 0;
435 ai[j].size = ty_size;
438 aipos = graph_length - 1;
441 for (offset = j = 0; j < graph_length; j++) {
442 entity *step = get_compound_graph_path_node(path, j);
443 ir_type *step_type = get_entity_type(step);
444 int ent_ofs = get_entity_offset_bytes(step);
447 /* add all positive offsets (= offsets in structs) */
448 if (ent_ofs >= 0) offset += ent_ofs;
450 if (j == graph_length - 1) {
451 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
453 /* Search the next free position in vals depending on the information from above (ai). */
454 while (vals[offset]) {
455 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
457 ai[aipos].visit_cnt++;
460 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
461 stepsize = ai[aipos--].size;
466 assert(aipos >= 0 && "couldn't store entity");
467 vals[offset] = get_compound_ent_value(ent, i);
474 /* now write them sorted */
475 for(i = 0; i < type_size; ) {
477 dump_atomic_init(obst, vals[i]);
478 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
482 obstack_printf(obst, "\t.byte\t0\n");
489 assert(0 && "unsupported type");
492 obstack_printf(obst, "\n");
494 else if (visibility != visibility_external_allocated) {
495 if (visibility == visibility_local) {
496 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
499 /* calculate the alignment */
500 align = get_type_alignment_bytes(ty);
501 h = highest_bit(align);
503 if ((1 << h) < align)
510 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
516 * Dumps declarations of global variables and the initialization code.
518 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
520 ir_type *gt = get_glob_type();
521 int i, n = get_class_n_members(gt);
523 for (i = 0; i < n; i++)
524 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
527 /************************************************************************/
529 void ia32_gen_decls(FILE *out) {
530 struct obstack rodata, data, comm;
534 obstack_init(&rodata);
538 fprintf(out, "\t.intel_syntax\n");
540 ia32_dump_globals(&rodata, &data, &comm);
542 size = obstack_object_size(&data);
543 cp = obstack_finish(&data);
545 fprintf(out, "\t.data\n");
546 fwrite(cp, 1, size, out);
549 size = obstack_object_size(&rodata);
550 cp = obstack_finish(&rodata);
552 fprintf(out, "\t.section\t.rodata\n");
553 fwrite(cp, 1, size, out);
556 size = obstack_object_size(&comm);
557 cp = obstack_finish(&comm);
559 fprintf(out, "\t.text\n");
560 fwrite(cp, 1, size, out);
563 obstack_free(&rodata, NULL);
564 obstack_free(&data, NULL);
565 obstack_free(&comm, NULL);