2 * Dumps global variables and constants as ia32 assembler.
3 * @author Christian Wuerdig
19 #include "ia32_gen_decls.h"
21 /************************************************************************/
24 * returns the highest bit value
26 static unsigned highest_bit(unsigned v)
30 if (v >= (1U << 16U)) {
34 if (v >= (1U << 8U)) {
38 if (v >= (1U << 4U)) {
42 if (v >= (1U << 2U)) {
46 if (v >= (1U << 1U)) {
57 * output the alignment
59 static void ia32_dump_align(struct obstack *obst, int align)
61 int h = highest_bit(align);
68 obstack_printf(obst, "\t.align %d\n", align);
71 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
76 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
80 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
84 obstack_printf(obst, "0x%02x%02x%02x%02x",
85 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
89 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
90 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
91 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
99 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
105 * dump an atomic value
107 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
109 ir_mode *mode = get_irn_mode(init);
110 int bytes = get_mode_size_bytes(mode);
113 switch (get_irn_opcode(init)) {
116 do_dump_atomic_init(obst, get_Cast_op(init));
120 do_dump_atomic_init(obst, get_Conv_op(init));
124 tv = get_Const_tarval(init);
126 /* beware of old stuff */
127 assert(! mode_is_reference(mode));
129 /* it's a arithmetic value */
130 dump_arith_tarval(obst, tv, bytes);
134 switch (get_SymConst_kind(init)) {
135 case symconst_addr_name:
136 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
139 case symconst_addr_ent:
140 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
144 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
148 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
153 do_dump_atomic_init(obst, get_Add_left(init));
154 obstack_printf(obst, " + ");
155 do_dump_atomic_init(obst, get_Add_right(init));
159 do_dump_atomic_init(obst, get_Sub_left(init));
160 obstack_printf(obst, " - ");
161 do_dump_atomic_init(obst, get_Sub_right(init));
165 do_dump_atomic_init(obst, get_Mul_left(init));
166 obstack_printf(obst, " * ");
167 do_dump_atomic_init(obst, get_Mul_right(init));
171 assert(0 && "dump_atomic_init(): unknown IR-node");
176 * dump an atomic value
178 static void dump_atomic_init(struct obstack *obst, ir_node *init)
180 ir_mode *mode = get_irn_mode(init);
181 int bytes = get_mode_size_bytes(mode);
186 obstack_printf(obst, "\t.byte\t");
190 obstack_printf(obst, "\t.value\t");
194 obstack_printf(obst, "\t.long\t");
198 obstack_printf(obst, "\t.quad\t");
203 /* handled in arith */
207 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
211 do_dump_atomic_init(obst, init);
212 obstack_printf(obst, "\n");
215 /************************************************************************/
216 /* Routines to dump global variables */
217 /************************************************************************/
220 * Determine if an entity is a string constant
221 * @param ent The entity
222 * @return 1 if it is a string constant, 0 otherwise
224 static int ent_is_string_const(entity *ent)
229 ty = get_entity_type(ent);
231 /* if it's an array */
232 if (is_Array_type(ty)) {
233 ir_type *elm_ty = get_array_element_type(ty);
235 /* and the array's element type is primitive */
236 if (is_Primitive_type(elm_ty)) {
237 ir_mode *mode = get_type_mode(elm_ty);
240 * and the mode of the element type is an int of
241 * the same size as the byte mode
243 if (mode_is_int(mode)
244 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
248 n = get_compound_ent_n_values(ent);
249 for (i = 0; i < n; ++i) {
250 ir_node *irn = get_compound_ent_value(ent, i);
251 if(get_irn_opcode(irn) != iro_Const)
254 c = (int) get_tarval_long(get_Const_tarval(irn));
256 if((i < n - 1 && !(isgraph(c) || isspace(c)))
257 || (i == n - 1 && c != '\0'))
270 * Dump a atring constant.
271 * No checks are made!!
272 * @param obst The obst to dump on.
273 * @param ent The entity to dump.
275 static void dump_string_cst(struct obstack *obst, entity *ent)
279 obstack_printf(obst, "\t.string \"");
280 n = get_compound_ent_n_values(ent);
282 for (i = 0; i < n-1; ++i) {
286 irn = get_compound_ent_value(ent, i);
287 c = (int) get_tarval_long(get_Const_tarval(irn));
290 case '"' : obstack_printf(obst, "\\\""); break;
291 case '\n': obstack_printf(obst, "\\n"); break;
292 case '\r': obstack_printf(obst, "\\r"); break;
293 case '\t': obstack_printf(obst, "\\t"); break;
296 obstack_printf(obst, "%c", c);
298 obstack_printf(obst, "%O", c);
302 obstack_printf(obst, "\"\n");
312 * Dumps the initialization of global variables that are not
315 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
317 ir_type *ty = get_entity_type(ent);
318 const char *ld_name = get_entity_ld_name(ent);
320 struct obstack *obst = data_obstack;
323 * FIXME: did NOT work for partly constant values
325 if (! is_Method_type(ty)) {
326 ent_variability variability = get_entity_variability(ent);
327 visibility visibility = get_entity_visibility(ent);
329 if (variability == variability_constant) {
330 /* a constant entity, put it on the rdata */
331 obst = rdata_obstack;
334 /* check, wether it is initialized, if yes create data */
335 if (variability != variability_uninitialized) {
336 if (visibility == visibility_external_visible) {
337 obstack_printf(obst, ".globl\t%s\n", ld_name);
339 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
340 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
342 align = get_type_alignment_bytes(ty);
343 ia32_dump_align(obst, align);
345 obstack_printf(obst, "%s:\n", ld_name);
347 if (is_atomic_type(ty)) {
348 if (get_entity_visibility(ent) != visibility_external_allocated)
349 dump_atomic_init(obst, get_atomic_ent_value(ent));
354 if (ent_is_string_const(ent)) {
355 dump_string_cst(obst, ent);
357 else if (is_Array_type(ty)) {
360 /* potential spare values should be already included! */
361 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
362 entity *step = get_compound_ent_value_member(ent, i);
363 ir_type *stype = get_entity_type(step);
365 if (get_type_mode(stype)) {
366 int align = (get_type_alignment_bits(stype) + 7) >> 3;
367 int n = size % align;
370 obstack_printf(obst, "\t.zero\t%d\n", align - n);
374 dump_atomic_init(obst, get_compound_ent_value(ent, i));
375 size += get_type_size_bytes(stype);
377 filler = get_type_size_bytes(ty) - size;
380 obstack_printf(obst, "\t.zero\t%d\n", filler);
382 else if (is_compound_type(ty)) {
386 /* Compound entities are NOT sorted.
387 * The sorting strategy used doesn't work for `value' compound fields nor
388 * for partially_constant entities.
392 * in the worst case, every entity allocates one byte, so the type
393 * size should be equal or bigger the number of fields
395 type_size = get_type_size_bytes(ty);
396 vals = xcalloc(type_size, sizeof(*vals));
398 /* collect the values and store them at the offsets */
399 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
400 int graph_length, aipos, offset;
403 compound_graph_path *path = get_compound_ent_value_path(ent, i);
405 /* get the access path to the costant value */
406 graph_length = get_compound_graph_path_length(path);
407 ai = xcalloc(graph_length, sizeof(struct arr_info));
409 /* We wanna know how many arrays are on the path to the entity. We also have to know how
410 * many elements each array holds to calculate the offset for the entity. */
411 for (j = 0; j < graph_length; j++) {
412 entity *step = get_compound_graph_path_node(path, j);
413 ir_type *step_type = get_entity_type(step);
414 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
417 if (is_Array_type(step_type))
418 for (k = 0; k < get_array_n_dimensions(step_type); k++)
419 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
421 ai[j].n_elems = n ? all_n + 1 : 0;
423 ai[j].size = ty_size;
426 aipos = graph_length - 1;
429 for (offset = j = 0; j < graph_length; j++) {
430 entity *step = get_compound_graph_path_node(path, j);
431 ir_type *step_type = get_entity_type(step);
432 int ent_ofs = get_entity_offset_bytes(step);
435 /* add all positive offsets (= offsets in structs) */
436 if (ent_ofs >= 0) offset += ent_ofs;
438 if (j == graph_length - 1) {
439 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
441 /* Search the next free position in vals depending on the information from above (ai). */
442 while (vals[offset]) {
443 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
445 ai[aipos].visit_cnt++;
448 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
449 stepsize = ai[aipos--].size;
454 assert(aipos >= 0 && "couldn't store entity");
455 vals[offset] = get_compound_ent_value(ent, i);
462 /* now write them sorted */
463 for(i = 0; i < type_size; ) {
465 dump_atomic_init(obst, vals[i]);
466 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
470 obstack_printf(obst, "\t.byte\t0\n");
477 assert(0 && "unsupported type");
480 obstack_printf(obst, "\n");
482 else if (visibility != visibility_external_allocated) {
483 if (visibility == visibility_local) {
484 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
487 /* calculate the alignment */
488 align = get_type_alignment_bytes(ty);
489 h = highest_bit(align);
491 if ((1 << h) < align)
498 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
504 * Dumps declarations of global variables and the initialization code.
506 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
508 ir_type *gt = get_glob_type();
509 int i, n = get_class_n_members(gt);
511 for (i = 0; i < n; i++)
512 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
515 /************************************************************************/
517 void ia32_gen_decls(FILE *out) {
518 struct obstack rodata, data, comm;
522 obstack_init(&rodata);
526 fprintf(out, "\t.intel_syntax\n");
528 ia32_dump_globals(&rodata, &data, &comm);
530 size = obstack_object_size(&data);
531 cp = obstack_finish(&data);
533 fprintf(out, "\t.data\n");
534 fwrite(cp, 1, size, out);
537 size = obstack_object_size(&rodata);
538 cp = obstack_finish(&rodata);
540 fprintf(out, "\t.section\t.rodata\n");
541 fwrite(cp, 1, size, out);
544 size = obstack_object_size(&comm);
545 cp = obstack_finish(&comm);
547 fprintf(out, "\t.text\n");
548 fwrite(cp, 1, size, out);
551 obstack_free(&rodata, NULL);
552 obstack_free(&data, NULL);
553 obstack_free(&comm, NULL);