2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief DWARF debugging info support
9 * @author Matthias Braun
18 #include "bedwarf_t.h"
33 #include "lc_opts_enum.h"
36 #include "beemitter.h"
47 static int debug_level = LEVEL_NONE;
50 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
51 * the cases where we need multiple ids with the same DW_TAG we define new IDs
54 typedef enum custom_abbrevs {
55 abbrev_void_subprogram = 1,
57 abbrev_formal_parameter,
58 abbrev_unnamed_formal_parameter,
59 abbrev_formal_parameter_no_location,
64 abbrev_void_pointer_type,
67 abbrev_structure_type,
71 abbrev_bitfield_member,
72 abbrev_subroutine_type,
73 abbrev_void_subroutine_type,
79 typedef struct dwarf_t {
80 const ir_entity *cur_ent; /**< current method entity */
81 unsigned next_type_nr; /**< next type number */
82 pmap *file_map; /**< a map from file names to number in file list */
83 const char **file_list;
84 const ir_entity **pubnames_list;
85 pset_new_t emitted_types;
86 const char *main_file; /**< name of the main source file */
87 const char *curr_file; /**< name of the current source file */
94 static dwarf_source_language language;
95 static char *comp_dir;
97 static unsigned insert_file(const char *filename)
100 void *entry = pmap_get(void, env.file_map, filename);
102 return PTR_TO_INT(entry);
104 ARR_APP1(const char*, env.file_list, filename);
105 num = (unsigned)ARR_LEN(env.file_list);
106 pmap_insert(env.file_map, filename, INT_TO_PTR(num));
108 /* TODO: quote chars in string */
109 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
113 static void emit_int32(uint32_t value)
115 be_emit_irprintf("\t.long %u\n", value);
116 be_emit_write_line();
119 static void emit_int16(uint16_t value)
121 be_emit_irprintf("\t.short %u\n", value);
122 be_emit_write_line();
125 static void emit_int8(uint8_t value)
127 be_emit_irprintf("\t.byte %u\n", value);
128 be_emit_write_line();
131 static void emit_uleb128(unsigned value)
133 be_emit_irprintf("\t.uleb128 0x%x\n", value);
134 be_emit_write_line();
137 static unsigned get_uleb128_size(unsigned value)
143 } while (value != 0);
147 static void emit_sleb128(long value)
149 be_emit_irprintf("\t.sleb128 %ld\n", value);
150 be_emit_write_line();
153 static unsigned get_sleb128_size(long value)
159 } while (value != 0 && value != -1);
163 static void emit_ref(const ir_entity *entity)
165 be_emit_cstring("\t.long ");
166 be_gas_emit_entity(entity);
168 be_emit_write_line();
171 static void emit_string_printf(const char *fmt, ...)
175 be_emit_cstring("\t.asciz \"");
176 be_emit_irvprintf(fmt, ap);
177 be_emit_cstring("\"\n");
180 be_emit_write_line();
183 static void emit_address(const char *name)
185 be_emit_cstring("\t.long ");
186 be_emit_string(be_gas_get_private_prefix());
187 be_emit_string(name);
189 be_emit_write_line();
192 static void emit_size(const char *from_label, const char *to_label)
194 be_emit_cstring("\t.long ");
195 be_emit_string(be_gas_get_private_prefix());
196 be_emit_string(to_label);
197 be_emit_cstring(" - ");
198 be_emit_string(be_gas_get_private_prefix());
199 be_emit_string(from_label);
201 be_emit_write_line();
204 static void emit_label(const char *name)
206 be_emit_string(be_gas_get_private_prefix());
207 be_emit_string(name);
208 be_emit_cstring(":\n");
209 be_emit_write_line();
212 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
214 emit_uleb128(attribute);
218 static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
219 dw_children children)
226 static void end_abbrev(void)
232 static void emit_line_info(void)
234 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
236 emit_label("line_section_begin");
237 /* on elf systems gas handles producing the line info for us, and we
238 * don't have to do anything */
239 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
241 emit_size("line_info_begin", "line_info_end");
243 emit_label("line_info_begin");
244 emit_int16(2); /* version */
245 emit_size("line_info_prolog_begin", "line_info_prolog_end");
246 emit_label("line_info_prolog_begin");
247 emit_int8(1); /* len of smallest instruction TODO: query from backend */
248 emit_int8(1); /* default is statement */
249 emit_int8(246); /* line base */
250 emit_int8(245); /* line range */
251 emit_int8(10); /* opcode base */
263 /* include directory list */
264 be_gas_emit_cstring("/foo/bar");
268 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
269 be_gas_emit_cstring(env.file_list[i]);
270 emit_uleb128(1); /* directory */
271 emit_uleb128(0); /* modification time */
272 emit_uleb128(0); /* file length */
276 emit_label("line_info_prolog_end");
278 /* TODO: put the line_info program here */
280 emit_label("line_info_end");
284 static void emit_pubnames(void)
288 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
290 emit_size("pubnames_begin", "pubnames_end");
291 emit_label("pubnames_begin");
293 emit_int16(2); /* version */
294 emit_size("info_section_begin", "info_begin");
295 emit_size("compile_unit_begin", "compile_unit_end");
297 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
298 const ir_entity *entity = env.pubnames_list[i];
299 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
300 be_gas_get_private_prefix(),
301 get_entity_nr(entity), be_gas_get_private_prefix());
302 be_gas_emit_cstring(get_entity_name(entity));
306 emit_label("pubnames_end");
309 void be_dwarf_location(dbg_info *dbgi)
314 if (debug_level < LEVEL_LOCATIONS)
316 loc = ir_retrieve_dbg_info(dbgi);
320 filenum = insert_file(loc.file);
321 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
322 be_emit_write_line();
325 void be_dwarf_callframe_register(const arch_register_t *reg)
327 if (debug_level < LEVEL_FRAMEINFO)
329 be_emit_cstring("\t.cfi_def_cfa_register ");
330 be_emit_irprintf("%d\n", reg->dwarf_number);
331 be_emit_write_line();
334 void be_dwarf_callframe_offset(int offset)
336 if (debug_level < LEVEL_FRAMEINFO)
338 be_emit_cstring("\t.cfi_def_cfa_offset ");
339 be_emit_irprintf("%d\n", offset);
340 be_emit_write_line();
343 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
345 if (debug_level < LEVEL_FRAMEINFO)
347 be_emit_cstring("\t.cfi_offset ");
348 be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
349 be_emit_write_line();
352 static bool is_extern_entity(const ir_entity *entity)
354 ir_visited_t visibility = get_entity_visibility(entity);
355 return visibility == ir_visibility_external;
358 static void emit_entity_label(const ir_entity *entity)
360 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
361 get_entity_nr(entity));
362 be_emit_write_line();
365 static void register_dbginfo_attributes(void)
367 register_attribute(DW_AT_decl_file, DW_FORM_udata);
368 register_attribute(DW_AT_decl_line, DW_FORM_udata);
369 register_attribute(DW_AT_decl_column, DW_FORM_udata);
372 static void emit_dbginfo(const dbg_info *dbgi)
374 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
375 unsigned const file = loc.file ? insert_file(loc.file) : 0;
377 emit_uleb128(loc.line);
378 emit_uleb128(loc.column);
381 static void emit_type_address(const ir_type *type)
383 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
384 be_gas_get_private_prefix(),
385 get_type_nr(type), be_gas_get_private_prefix());
386 be_emit_write_line();
389 static void emit_subprogram_abbrev(void)
391 begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
392 register_attribute(DW_AT_name, DW_FORM_string);
393 register_dbginfo_attributes();
394 register_attribute(DW_AT_type, DW_FORM_ref4);
395 register_attribute(DW_AT_external, DW_FORM_flag);
396 register_attribute(DW_AT_low_pc, DW_FORM_addr);
397 register_attribute(DW_AT_high_pc, DW_FORM_addr);
398 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
399 if (debug_level >= LEVEL_FRAMEINFO)
400 register_attribute(DW_AT_frame_base, DW_FORM_block1);
403 begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
404 register_attribute(DW_AT_name, DW_FORM_string);
405 register_dbginfo_attributes();
406 register_attribute(DW_AT_external, DW_FORM_flag);
407 register_attribute(DW_AT_low_pc, DW_FORM_addr);
408 register_attribute(DW_AT_high_pc, DW_FORM_addr);
409 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
410 if (debug_level >= LEVEL_FRAMEINFO)
411 register_attribute(DW_AT_frame_base, DW_FORM_block1);
414 begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
416 register_attribute(DW_AT_name, DW_FORM_string);
417 register_dbginfo_attributes();
418 register_attribute(DW_AT_type, DW_FORM_ref4);
419 register_attribute(DW_AT_location, DW_FORM_block1);
422 begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
424 register_attribute(DW_AT_name, DW_FORM_string);
425 register_dbginfo_attributes();
426 register_attribute(DW_AT_type, DW_FORM_ref4);
430 static void emit_type(ir_type *type);
432 static void emit_stack_location(long offset)
434 unsigned size = 1 + get_sleb128_size(offset);
436 emit_int8(DW_OP_fbreg);
437 emit_sleb128(offset);
440 static void emit_function_parameters(const ir_entity *entity,
441 const parameter_dbg_info_t *infos)
443 ir_type *type = get_entity_type(entity);
444 size_t n_params = get_method_n_params(type);
445 dbg_info *dbgi = get_entity_dbg_info(entity);
447 for (i = 0; i < n_params; ++i) {
448 ir_type *param_type = get_method_param_type(type, i);
450 if (infos != NULL && infos[i].entity != NULL) {
451 long const offset = get_entity_offset(infos[i].entity);
452 emit_uleb128(abbrev_formal_parameter);
453 emit_string_printf("arg%u", (unsigned)i);
455 emit_type_address(param_type);
456 emit_stack_location(offset);
458 emit_uleb128(abbrev_formal_parameter_no_location);
459 emit_string_printf("arg%u", (unsigned)i);
461 emit_type_address(param_type);
466 void be_dwarf_method_before(const ir_entity *entity,
467 const parameter_dbg_info_t *parameter_infos)
469 if (debug_level < LEVEL_BASIC)
472 ir_type *type = get_entity_type(entity);
473 size_t n_ress = get_method_n_ress(type);
474 size_t n_params = get_method_n_params(type);
477 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
480 ir_type *res = get_method_res_type(type, 0);
483 for (i = 0; i < n_params; ++i) {
484 ir_type *param_type = get_method_param_type(type, i);
485 emit_type(param_type);
488 emit_entity_label(entity);
489 emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
490 be_gas_emit_cstring(get_entity_ld_name(entity));
491 emit_dbginfo(get_entity_dbg_info(entity));
493 ir_type *res = get_method_res_type(type, 0);
494 emit_type_address(res);
496 emit_int8(is_extern_entity(entity));
498 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
499 get_entity_ld_name(entity));
500 /* frame_base prog */
502 emit_int8(DW_OP_call_frame_cfa);
504 emit_function_parameters(entity, parameter_infos);
507 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
509 env.cur_ent = entity;
513 void be_dwarf_method_begin(void)
515 if (debug_level < LEVEL_FRAMEINFO)
517 be_emit_cstring("\t.cfi_startproc\n");
518 be_emit_write_line();
521 void be_dwarf_method_end(void)
523 if (debug_level < LEVEL_BASIC)
525 const ir_entity *entity = env.cur_ent;
526 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
527 get_entity_ld_name(entity));
529 if (debug_level >= LEVEL_FRAMEINFO) {
530 be_emit_cstring("\t.cfi_endproc\n");
531 be_emit_write_line();
535 static void emit_base_type_abbrev(void)
537 begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
538 register_attribute(DW_AT_encoding, DW_FORM_data1);
539 register_attribute(DW_AT_byte_size, DW_FORM_data1);
540 register_attribute(DW_AT_name, DW_FORM_string);
544 static void emit_type_label(const ir_type *type)
546 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
547 be_emit_write_line();
550 static void emit_base_type(const ir_type *type)
553 ir_mode *mode = get_type_mode(type);
554 ir_print_type(buf, sizeof(buf), type);
556 emit_type_label(type);
557 emit_uleb128(abbrev_base_type);
558 if (mode_is_int(mode)) {
560 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
561 emit_int8(DW_ATE_boolean);
563 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
565 } else if (mode_is_reference(mode)) {
566 emit_int8(DW_ATE_address);
567 } else if (mode_is_float(mode)) {
568 emit_int8(DW_ATE_float);
570 panic("mode not implemented yet");
572 emit_int8(get_mode_size_bytes(mode));
573 be_gas_emit_cstring(buf);
576 static void emit_pointer_type_abbrev(void)
578 begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
579 register_attribute(DW_AT_type, DW_FORM_ref4);
580 register_attribute(DW_AT_byte_size, DW_FORM_data1);
583 /* for void* pointer s*/
584 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
585 register_attribute(DW_AT_byte_size, DW_FORM_data1);
589 static void emit_pointer_type(const ir_type *type)
591 ir_type *points_to = get_pointer_points_to_type(type);
592 unsigned size = get_type_size_bytes(type);
595 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
596 emit_type(points_to);
598 emit_type_label(type);
599 emit_uleb128(abbrev_pointer_type);
600 emit_type_address(points_to);
602 emit_type_label(type);
603 emit_uleb128(abbrev_void_pointer_type);
608 static void emit_array_type_abbrev(void)
610 begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
611 register_attribute(DW_AT_type, DW_FORM_ref4);
614 begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
615 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
619 static void emit_array_type(const ir_type *type)
621 ir_type *element_type = get_array_element_type(type);
623 if (get_array_n_dimensions(type) != 1)
624 panic("multidimensional arrays no supported yet");
626 emit_type(element_type);
628 emit_type_label(type);
629 emit_uleb128(abbrev_array_type);
630 emit_type_address(element_type);
632 if (has_array_upper_bound(type, 0)) {
633 int bound = get_array_upper_bound_int(type, 0);
634 emit_uleb128(abbrev_subrange_type);
641 static void emit_compound_type_abbrev(void)
643 begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
644 register_attribute(DW_AT_byte_size, DW_FORM_udata);
645 // TODO register_dbginfo_attributes();
648 begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
649 register_attribute(DW_AT_byte_size, DW_FORM_udata);
650 // TODO register_dbginfo_attributes();
653 begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
654 register_attribute(DW_AT_byte_size, DW_FORM_udata);
655 // TODO register_dbginfo_attributes();
658 begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
659 register_attribute(DW_AT_type, DW_FORM_ref4);
660 register_attribute(DW_AT_name, DW_FORM_string);
661 register_dbginfo_attributes();
662 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
665 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
666 register_attribute(DW_AT_byte_size, DW_FORM_udata);
667 register_attribute(DW_AT_bit_size, DW_FORM_udata);
668 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
669 register_attribute(DW_AT_type, DW_FORM_ref4);
670 register_attribute(DW_AT_name, DW_FORM_string);
671 register_dbginfo_attributes();
672 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
676 static void emit_op_plus_uconst(unsigned value)
678 emit_int8(DW_OP_plus_uconst);
682 static void emit_compound_type(const ir_type *type)
685 size_t n_members = get_compound_n_members(type);
687 for (i = 0; i < n_members; ++i) {
688 ir_entity *member = get_compound_member(type, i);
689 ir_type *member_type = get_entity_type(member);
690 if (is_Primitive_type(member_type)) {
691 ir_type *base = get_primitive_base_type(member_type);
695 emit_type(member_type);
698 emit_type_label(type);
699 if (is_Struct_type(type)) {
700 emit_uleb128(abbrev_structure_type);
701 } else if (is_Union_type(type)) {
702 emit_uleb128(abbrev_union_type);
704 assert(is_Class_type(type));
705 emit_uleb128(abbrev_class_type);
707 emit_uleb128(get_type_size_bytes(type));
708 for (i = 0; i < n_members; ++i) {
709 ir_entity *member = get_compound_member(type, i);
710 ir_type *member_type = get_entity_type(member);
711 int offset = get_entity_offset(member);
714 if (is_Primitive_type(member_type) &&
715 (base = get_primitive_base_type(member_type))) {
716 unsigned bit_offset = get_entity_offset_bits_remainder(member);
717 unsigned base_size = get_type_size_bytes(base);
718 ir_mode *mode = get_type_mode(member_type);
719 unsigned bit_size = get_mode_size_bits(mode);
721 bit_offset = base_size*8 - bit_offset - bit_size;
723 emit_uleb128(abbrev_bitfield_member);
724 emit_uleb128(base_size);
725 emit_uleb128(bit_size);
726 emit_uleb128(bit_offset);
729 emit_uleb128(abbrev_member);
732 emit_type_address(member_type);
733 be_gas_emit_cstring(get_entity_name(member));
734 emit_dbginfo(get_entity_dbg_info(member));
736 emit_int8(1 + get_uleb128_size(offset));
737 emit_op_plus_uconst(offset);
743 static void emit_subroutine_type_abbrev(void)
745 begin_abbrev(abbrev_subroutine_type,
746 DW_TAG_subroutine_type, DW_CHILDREN_yes);
747 register_attribute(DW_AT_prototyped, DW_FORM_flag);
748 register_attribute(DW_AT_type, DW_FORM_ref4);
751 begin_abbrev(abbrev_void_subroutine_type,
752 DW_TAG_subroutine_type, DW_CHILDREN_yes);
753 register_attribute(DW_AT_prototyped, DW_FORM_flag);
756 begin_abbrev(abbrev_unnamed_formal_parameter,
757 DW_TAG_formal_parameter, DW_CHILDREN_no);
758 register_attribute(DW_AT_type, DW_FORM_ref4);
762 static void emit_subroutine_type(const ir_type *type)
764 size_t n_params = get_method_n_params(type);
765 size_t n_ress = get_method_n_ress(type);
767 for (i = 0; i < n_params; ++i) {
768 ir_type *param_type = get_method_param_type(type, i);
769 emit_type(param_type);
771 for (i = 0; i < n_ress; ++i) {
772 ir_type *res_type = get_method_res_type(type, i);
776 emit_type_label(type);
777 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
778 emit_int8(1); /* prototyped */
780 /* dwarf only supports 1 return type */
781 ir_type *res_type = get_method_res_type(type, 0);
782 emit_type_address(res_type);
785 for (i = 0; i < n_params; ++i) {
786 ir_type *param_type = get_method_param_type(type, i);
787 emit_uleb128(abbrev_unnamed_formal_parameter);
788 emit_type_address(param_type);
793 static void emit_type(ir_type *type)
795 if (!pset_new_insert(&env.emitted_types, type))
798 switch (get_type_tpop_code(type)) {
799 case tpo_primitive: emit_base_type(type); break;
800 case tpo_pointer: emit_pointer_type(type); break;
801 case tpo_array: emit_array_type(type); break;
804 case tpo_union: emit_compound_type(type); break;
805 case tpo_method: emit_subroutine_type(type); break;
807 panic("type %+F not implemented yet", type);
811 static void emit_op_addr(const ir_entity *entity)
813 emit_int8(DW_OP_addr);
814 be_emit_cstring("\t.long ");
815 be_gas_emit_entity(entity);
817 be_emit_write_line();
820 static void emit_variable_abbrev(void)
822 begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
823 register_attribute(DW_AT_name, DW_FORM_string);
824 register_attribute(DW_AT_type, DW_FORM_ref4);
825 register_attribute(DW_AT_external, DW_FORM_flag);
826 register_dbginfo_attributes();
827 register_attribute(DW_AT_location, DW_FORM_block1);
831 void be_dwarf_variable(const ir_entity *entity)
833 ir_type *type = get_entity_type(entity);
835 if (debug_level < LEVEL_BASIC)
837 if (get_entity_ld_name(entity)[0] == '\0')
839 if (!entity_has_definition(entity))
842 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
846 emit_entity_label(entity);
847 emit_uleb128(abbrev_variable);
848 be_gas_emit_cstring(get_entity_ld_name(entity));
849 emit_type_address(type);
850 emit_int8(is_extern_entity(entity));
851 emit_dbginfo(get_entity_dbg_info(entity));
853 emit_int8(5); /* block length */
854 emit_op_addr(entity);
856 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
859 static void emit_compile_unit_abbrev(void)
861 begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
862 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
863 register_attribute(DW_AT_producer, DW_FORM_string);
864 register_attribute(DW_AT_name, DW_FORM_string);
866 register_attribute(DW_AT_language, DW_FORM_data2);
867 if (comp_dir != NULL)
868 register_attribute(DW_AT_comp_dir, DW_FORM_string);
872 static void emit_abbrev(void)
874 /* create abbreviation for compile_unit */
875 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
877 emit_label("abbrev_begin");
879 emit_compile_unit_abbrev();
880 emit_variable_abbrev();
881 emit_subprogram_abbrev();
882 emit_base_type_abbrev();
883 emit_pointer_type_abbrev();
884 emit_array_type_abbrev();
885 emit_compound_type_abbrev();
886 emit_subroutine_type_abbrev();
890 void be_dwarf_unit_begin(const char *filename)
892 if (debug_level < LEVEL_BASIC)
896 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
897 emit_label("info_section_begin");
898 emit_label("info_begin");
900 const backend_params *be_params = be_get_backend_param();
902 /* length of compilation unit info */
903 emit_size("compile_unit_begin", "compile_unit_end");
904 emit_label("compile_unit_begin");
905 emit_int16(3); /* dwarf version */
906 emit_address("abbrev_begin");
907 emit_int8(be_params->machine_size / 8); /* pointer size */
909 /* compile_unit die */
910 emit_uleb128(abbrev_compile_unit);
911 emit_address("line_section_begin");
912 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
913 ir_get_version_minor(), ir_get_version_revision());
914 be_gas_emit_cstring(filename);
916 emit_int16(language);
917 if (comp_dir != NULL)
918 be_gas_emit_cstring(comp_dir);
920 /* tell gas to emit cfi in debug_frame
921 * TODO: if we produce exception handling code then this should be
922 * .eh_frame (I also wonder if bad things happen if simply always
924 be_emit_cstring("\t.cfi_sections .debug_frame\n");
925 be_emit_write_line();
928 void be_dwarf_unit_end(void)
930 if (debug_level < LEVEL_BASIC)
932 be_gas_emit_switch_section(GAS_SECTION_TEXT);
933 emit_label("section_end");
935 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
936 emit_uleb128(0); /* end of compile_unit DIE */
938 emit_label("compile_unit_end");
944 void be_dwarf_close(void)
946 if (debug_level < LEVEL_BASIC)
948 pmap_destroy(env.file_map);
949 DEL_ARR_F(env.file_list);
950 DEL_ARR_F(env.pubnames_list);
951 pset_new_destroy(&env.emitted_types);
954 /* Opens a dwarf handler */
955 void be_dwarf_open(void)
957 if (debug_level < LEVEL_BASIC)
959 env.file_map = pmap_create();
960 env.file_list = NEW_ARR_F(const char*, 0);
961 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
962 pset_new_init(&env.emitted_types);
965 void be_dwarf_set_source_language(dwarf_source_language new_language)
967 language = new_language;
970 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
973 comp_dir = xstrdup(new_comp_dir);
976 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
977 void be_init_dwarf(void)
979 static const lc_opt_enum_int_items_t level_items[] = {
980 { "none", LEVEL_NONE },
981 { "basic", LEVEL_BASIC },
982 { "locations", LEVEL_LOCATIONS },
983 { "frameinfo", LEVEL_FRAMEINFO },
986 static lc_opt_enum_int_var_t debug_level_opt = {
987 &debug_level, level_items
989 static lc_opt_table_entry_t be_main_options[] = {
990 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
994 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
995 lc_opt_add_table(be_grp, be_main_options);