2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief DWARF debugging info support
23 * @author Matthias Braun
42 #include "be_dbgout_t.h"
45 #include "beemitter.h"
49 /* Tag names and codes. */
50 typedef enum dwarf_tag {
51 DW_TAG_padding = 0x00,
52 DW_TAG_array_type = 0x01,
53 DW_TAG_class_type = 0x02,
54 DW_TAG_entry_point = 0x03,
55 DW_TAG_enumeration_type = 0x04,
56 DW_TAG_formal_parameter = 0x05,
57 DW_TAG_imported_declaration = 0x08,
59 DW_TAG_lexical_block = 0x0b,
61 DW_TAG_pointer_type = 0x0f,
62 DW_TAG_reference_type = 0x10,
63 DW_TAG_compile_unit = 0x11,
64 DW_TAG_string_type = 0x12,
65 DW_TAG_structure_type = 0x13,
66 DW_TAG_subroutine_type = 0x15,
67 DW_TAG_typedef = 0x16,
68 DW_TAG_union_type = 0x17,
69 DW_TAG_unspecified_parameters = 0x18,
70 DW_TAG_variant = 0x19,
71 DW_TAG_common_block = 0x1a,
72 DW_TAG_common_inclusion = 0x1b,
73 DW_TAG_inheritance = 0x1c,
74 DW_TAG_inlined_subroutine = 0x1d,
76 DW_TAG_ptr_to_member_type = 0x1f,
77 DW_TAG_set_type = 0x20,
78 DW_TAG_subrange_type = 0x21,
79 DW_TAG_with_stmt = 0x22,
80 DW_TAG_access_declaration = 0x23,
81 DW_TAG_base_type = 0x24,
82 DW_TAG_catch_block = 0x25,
83 DW_TAG_const_type = 0x26,
84 DW_TAG_constant = 0x27,
85 DW_TAG_enumerator = 0x28,
86 DW_TAG_file_type = 0x29,
88 DW_TAG_namelist = 0x2b,
89 DW_TAG_namelist_item = 0x2c,
90 DW_TAG_packed_type = 0x2d,
91 DW_TAG_subprogram = 0x2e,
92 DW_TAG_template_type_param = 0x2f,
93 DW_TAG_template_value_param = 0x30,
94 DW_TAG_thrown_type = 0x31,
95 DW_TAG_try_block = 0x32,
96 DW_TAG_variant_part = 0x33,
97 DW_TAG_variable = 0x34,
98 DW_TAG_volatile_type = 0x35,
100 DW_TAG_dwarf_procedure = 0x36,
101 DW_TAG_restrict_type = 0x37,
102 DW_TAG_interface_type = 0x38,
103 DW_TAG_namespace = 0x39,
104 DW_TAG_imported_module = 0x3a,
105 DW_TAG_unspecified_type = 0x3b,
106 DW_TAG_partial_unit = 0x3c,
107 DW_TAG_imported_unit = 0x3d,
108 DW_TAG_condition = 0x3f,
109 DW_TAG_shared_type = 0x40,
112 typedef enum custom_abbrevs {
113 abbrev_void_pointer_type = 100,
114 abbrev_unnamed_formal_parameter,
115 abbrev_void_subroutine_type,
116 abbrev_bitfield_member,
119 typedef enum dw_children {
120 DW_CHILDREN_no = 0x00,
121 DW_CHILDREN_yes = 0x01
124 typedef enum dwarf_form {
126 DW_FORM_block2 = 0x03,
127 DW_FORM_block4 = 0x04,
128 DW_FORM_data2 = 0x05,
129 DW_FORM_data4 = 0x06,
130 DW_FORM_data8 = 0x07,
131 DW_FORM_string = 0x08,
132 DW_FORM_block = 0x09,
133 DW_FORM_block1 = 0x0a,
134 DW_FORM_data1 = 0x0b,
136 DW_FORM_sdata = 0x0d,
138 DW_FORM_udata = 0x0f,
139 DW_FORM_ref_addr = 0x10,
144 DW_FORM_ref_udata = 0x15,
145 DW_FORM_indirect = 0x16
148 typedef enum dwarf_attribute {
149 DW_AT_sibling = 0x01,
150 DW_AT_location = 0x02,
152 DW_AT_ordering = 0x09,
153 DW_AT_subscr_data = 0x0a,
154 DW_AT_byte_size = 0x0b,
155 DW_AT_bit_offset = 0x0c,
156 DW_AT_bit_size = 0x0d,
157 DW_AT_element_list = 0x0f,
158 DW_AT_stmt_list = 0x10,
160 DW_AT_high_pc = 0x12,
161 DW_AT_language = 0x13,
164 DW_AT_discr_value = 0x16,
165 DW_AT_visibility = 0x17,
167 DW_AT_string_length = 0x19,
168 DW_AT_common_reference = 0x1a,
169 DW_AT_comp_dir = 0x1b,
170 DW_AT_const_value = 0x1c,
171 DW_AT_containing_type = 0x1d,
172 DW_AT_default_value = 0x1e,
174 DW_AT_is_optional = 0x21,
175 DW_AT_lower_bound = 0x22,
176 DW_AT_producer = 0x25,
177 DW_AT_prototyped = 0x27,
178 DW_AT_return_addr = 0x2a,
179 DW_AT_start_scope = 0x2c,
180 DW_AT_bit_stride = 0x2e,
181 DW_AT_stride_size = DW_AT_bit_stride,
182 DW_AT_upper_bound = 0x2f,
183 DW_AT_abstract_origin = 0x31,
184 DW_AT_accessibility = 0x32,
185 DW_AT_address_class = 0x33,
186 DW_AT_artificial = 0x34,
187 DW_AT_base_types = 0x35,
188 DW_AT_calling_convention = 0x36,
190 DW_AT_data_member_location = 0x38,
191 DW_AT_decl_column = 0x39,
192 DW_AT_decl_file = 0x3a,
193 DW_AT_decl_line = 0x3b,
194 DW_AT_declaration = 0x3c,
195 DW_AT_discr_list = 0x3d,
196 DW_AT_encoding = 0x3e,
197 DW_AT_external = 0x3f,
198 DW_AT_frame_base = 0x40,
200 DW_AT_identifier_case = 0x42,
201 DW_AT_macro_info = 0x43,
202 DW_AT_namelist_items = 0x44,
203 DW_AT_priority = 0x45,
204 DW_AT_segment = 0x46,
205 DW_AT_specification = 0x47,
206 DW_AT_static_link = 0x48,
208 DW_AT_use_location = 0x4a,
209 DW_AT_variable_parameter = 0x4b,
210 DW_AT_virtuality = 0x4c,
211 DW_AT_vtable_elem_location = 0x4d,
212 /* DWARF 3 values. */
213 DW_AT_allocated = 0x4e,
214 DW_AT_associated = 0x4f,
215 DW_AT_data_location = 0x50,
216 DW_AT_byte_stride = 0x51,
217 DW_AT_stride = DW_AT_byte_stride,
218 DW_AT_entry_pc = 0x52,
219 DW_AT_use_UTF8 = 0x53,
220 DW_AT_extension = 0x54,
222 DW_AT_trampoline = 0x56,
223 DW_AT_call_column = 0x57,
224 DW_AT_call_file = 0x58,
225 DW_AT_call_line = 0x59,
226 DW_AT_description = 0x5a,
227 DW_AT_binary_scale = 0x5b,
228 DW_AT_decimal_scale = 0x5c,
230 DW_AT_decimal_sign = 0x5e,
231 DW_AT_digit_count = 0x5f,
232 DW_AT_picture_string = 0x60,
233 DW_AT_mutable = 0x61,
234 DW_AT_threads_scaled = 0x62,
235 DW_AT_explicit = 0x63,
236 DW_AT_object_pointer = 0x64,
237 DW_AT_endianity = 0x65,
238 DW_AT_elemental = 0x66,
240 DW_AT_recursive = 0x68,
245 DW_ATE_address = 0x1,
246 DW_ATE_boolean = 0x2,
247 DW_ATE_complex_float = 0x3,
250 DW_ATE_signed_char = 0x6,
251 DW_ATE_unsigned = 0x7,
252 DW_ATE_unsigned_char = 0x8,
254 DW_ATE_imaginary_float = 0x9,
255 DW_ATE_packed_decimal = 0xa,
256 DW_ATE_numeric_string = 0xb,
258 DW_ATE_signed_fixed = 0xd,
259 DW_ATE_unsigned_fixed = 0xe,
260 DW_ATE_decimal_float = 0xf,
263 typedef enum dwarf_line_number_x_ops {
264 DW_LNE_end_sequence = 1,
265 DW_LNE_set_address = 2,
266 DW_LNE_define_file = 3,
267 } dwarf_line_number_x_ops;
269 typedef enum dwarf_location_op {
272 DW_OP_const1u = 0x08,
273 DW_OP_const1s = 0x09,
274 DW_OP_const2u = 0x0a,
275 DW_OP_const2s = 0x0b,
276 DW_OP_const4u = 0x0c,
277 DW_OP_const4s = 0x0d,
278 DW_OP_const8u = 0x0e,
279 DW_OP_const8s = 0x0f,
299 DW_OP_plus_uconst = 0x23,
412 DW_OP_deref_size = 0x94,
413 DW_OP_xderef_size = 0x95,
420 typedef struct dwarf_t {
421 dbg_handle base; /**< the base class */
422 const ir_entity *cur_ent; /**< current method entity */
423 const be_stack_layout_t *layout; /**< current stack layout */
424 unsigned next_type_nr; /**< next type number */
425 pmap *file_map; /**< a map from file names to number in file list */
426 const char **file_list;
427 const ir_entity **pubnames_list;
428 pset_new_t emitted_types;
429 const char *main_file; /**< name of the main source file */
430 const char *curr_file; /**< name of the current source file */
435 static dwarf_source_language language;
436 static const char *comp_dir;
438 static unsigned insert_file(dwarf_t *env, const char *filename)
441 void *entry = pmap_get(env->file_map, filename);
443 return PTR_TO_INT(entry);
445 ARR_APP1(const char*, env->file_list, filename);
446 num = (unsigned)ARR_LEN(env->file_list);
447 pmap_insert(env->file_map, filename, INT_TO_PTR(num));
448 /* TODO: quote chars in string */
449 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
453 static void emit_int32(uint32_t value)
455 be_emit_irprintf("\t.long %u\n", value);
456 be_emit_write_line();
459 static void emit_int16(uint16_t value)
461 be_emit_irprintf("\t.short %u\n", value);
462 be_emit_write_line();
465 static void emit_int8(uint8_t value)
467 be_emit_irprintf("\t.byte %u\n", value);
468 be_emit_write_line();
471 static void emit_uleb128(unsigned value)
473 be_emit_irprintf("\t.uleb128 0x%x\n", value);
474 be_emit_write_line();
477 static unsigned get_uleb128_size(unsigned value)
483 } while (value != 0);
487 static void emit_string(const char *string)
489 be_emit_irprintf("\t.asciz \"%s\"\n", string);
490 be_emit_write_line();
493 static void emit_ref(const ir_entity *entity)
495 be_emit_cstring("\t.long ");
496 be_gas_emit_entity(entity);
498 be_emit_write_line();
501 static void emit_string_printf(const char *fmt, ...)
505 be_emit_cstring("\t.asciz \"");
506 be_emit_irvprintf(fmt, ap);
507 be_emit_cstring("\"\n");
510 be_emit_write_line();
513 static void emit_address(const char *name)
515 be_emit_cstring("\t.long ");
516 be_emit_string(be_gas_get_private_prefix());
517 be_emit_string(name);
519 be_emit_write_line();
522 static void emit_size(const char *from_label, const char *to_label)
524 be_emit_cstring("\t.long ");
525 be_emit_string(be_gas_get_private_prefix());
526 be_emit_string(to_label);
527 be_emit_cstring(" - ");
528 be_emit_string(be_gas_get_private_prefix());
529 be_emit_string(from_label);
531 be_emit_write_line();
534 static void emit_label(const char *name)
536 be_emit_string(be_gas_get_private_prefix());
537 be_emit_string(name);
538 be_emit_cstring(":\n");
539 be_emit_write_line();
542 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
544 emit_uleb128(attribute);
548 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
555 static void end_abbrev(void)
561 static void emit_line_info(dwarf_t *env)
563 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
565 emit_label("line_section_begin");
566 /* on elf systems gas handles producing the line info for us, and we
567 * don't have to do anything */
568 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
570 emit_size("line_info_begin", "line_info_end");
572 emit_label("line_info_begin");
573 emit_int16(2); /* version */
574 emit_size("line_info_prolog_begin", "line_info_prolog_end");
575 emit_label("line_info_prolog_begin");
576 emit_int8(1); /* len of smallest instruction TODO: query from backend */
577 emit_int8(1); /* default is statement */
578 emit_int8(246); /* line base */
579 emit_int8(245); /* line range */
580 emit_int8(10); /* opcode base */
592 /* include directory list */
593 emit_string("/foo/bar");
597 for (i = 0; i < ARR_LEN(env->file_list); ++i) {
598 emit_string(env->file_list[0]);
599 emit_uleb128(1); /* directory */
600 emit_uleb128(0); /* modification time */
601 emit_uleb128(0); /* file length */
605 emit_label("line_info_prolog_end");
607 /* TODO: put the line_info program here */
609 emit_label("line_info_end");
613 static void emit_pubnames(dwarf_t *env)
617 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
619 emit_size("pubnames_begin", "pubnames_end");
620 emit_label("pubnames_begin");
622 emit_int16(2); /* version */
623 emit_size("info_section_begin", "info_begin");
624 emit_size("compile_unit_begin", "compile_unit_end");
626 for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
627 const ir_entity *entity = env->pubnames_list[i];
628 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
629 be_gas_get_private_prefix(),
630 get_entity_nr(entity), be_gas_get_private_prefix());
631 emit_string(get_entity_name(entity));
635 emit_label("pubnames_end");
638 static void dwarf_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
640 dwarf_t *const env = (dwarf_t*)h;
641 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
647 filenum = insert_file(env, loc.file);
648 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
649 be_emit_write_line();
652 static bool is_extern_entity(const ir_entity *entity)
654 ir_visited_t visibility = get_entity_visibility(entity);
655 return visibility == ir_visibility_default
656 || visibility == ir_visibility_external;
659 static void emit_entity_label(const ir_entity *entity)
661 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
662 get_entity_nr(entity));
663 be_emit_write_line();
666 static void register_dbginfo_attributes(void)
668 register_attribute(DW_AT_decl_file, DW_FORM_udata);
669 register_attribute(DW_AT_decl_line, DW_FORM_udata);
673 * emits values for DW_AT_decl_file then DW_AT_decl_line
675 static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
677 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
678 unsigned const file = loc.file ? insert_file(env, loc.file) : 0;
680 emit_uleb128(loc.line);
683 static void emit_subprogram_abbrev(void)
685 begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
686 register_attribute(DW_AT_name, DW_FORM_string);
687 register_dbginfo_attributes();
688 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
689 //register_attribute(DW_AT_type, DW_FORM_ref4);
690 register_attribute(DW_AT_external, DW_FORM_flag);
691 register_attribute(DW_AT_low_pc, DW_FORM_addr);
692 register_attribute(DW_AT_high_pc, DW_FORM_addr);
693 //register_attribute(DW_AT_frame_base, DW_FORM_block1);
698 * dump the drwarf for a method begin
700 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
702 dwarf_t *env = (dwarf_t*)handle;
704 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
706 emit_entity_label(entity);
707 emit_uleb128(DW_TAG_subprogram);
708 emit_string(get_entity_ld_name(entity));
709 emit_dbginfo(env, get_entity_dbg_info(entity));
710 emit_int8(is_extern_entity(entity));
712 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
713 get_entity_ld_name(entity));
715 ARR_APP1(const ir_entity*, env->pubnames_list, entity);
717 env->cur_ent = entity;
721 * dump the drwarf for a method end
723 static void dwarf_method_end(dbg_handle *handle)
725 dwarf_t *env = (dwarf_t*)handle;
726 const ir_entity *entity = env->cur_ent;
728 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
729 get_entity_ld_name(entity));
732 static void dwarf_types(dbg_handle *handle)
737 static void emit_type(dwarf_t *env, ir_type *type);
739 static void emit_base_type_abbrev(void)
741 begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
742 register_attribute(DW_AT_encoding, DW_FORM_data1);
743 register_attribute(DW_AT_byte_size, DW_FORM_data1);
744 register_attribute(DW_AT_name, DW_FORM_string);
748 static void emit_type_label(const ir_type *type)
750 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
751 be_emit_write_line();
754 static void emit_type_address(const ir_type *type)
756 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
757 be_gas_get_private_prefix(),
758 get_type_nr(type), be_gas_get_private_prefix());
759 be_emit_write_line();
762 static void emit_base_type(const ir_type *type)
765 ir_mode *mode = get_type_mode(type);
766 ir_print_type(buf, sizeof(buf), type);
768 emit_type_label(type);
769 emit_uleb128(DW_TAG_base_type);
770 if (mode_is_int(mode)) {
772 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
773 emit_int8(DW_ATE_boolean);
775 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
777 } else if (mode_is_reference(mode)) {
778 emit_int8(DW_ATE_address);
779 } else if (mode_is_float(mode)) {
780 emit_int8(DW_ATE_float);
782 panic("mode not implemented yet");
784 emit_int8(get_mode_size_bytes(mode));
788 static void emit_pointer_type_abbrev(void)
790 begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
791 register_attribute(DW_AT_type, DW_FORM_ref4);
792 register_attribute(DW_AT_byte_size, DW_FORM_data1);
795 /* for void* pointer s*/
796 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
797 register_attribute(DW_AT_byte_size, DW_FORM_data1);
801 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
803 ir_type *points_to = get_pointer_points_to_type(type);
804 unsigned size = get_type_size_bytes(type);
807 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
808 emit_type(env, points_to);
810 emit_type_label(type);
811 emit_uleb128(DW_TAG_pointer_type);
812 emit_type_address(points_to);
814 emit_type_label(type);
815 emit_uleb128(abbrev_void_pointer_type);
820 static void emit_array_type_abbrev(void)
822 begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
823 register_attribute(DW_AT_type, DW_FORM_ref4);
826 begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
827 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
831 static void emit_array_type(dwarf_t *env, const ir_type *type)
833 ir_type *element_type = get_array_element_type(type);
835 if (get_array_n_dimensions(type) != 1)
836 panic("dwarf: multidimensional arrays no supported yet");
838 emit_type(env, element_type);
840 emit_type_label(type);
841 emit_uleb128(DW_TAG_array_type);
842 emit_type_address(element_type);
844 if (has_array_upper_bound(type, 0)) {
845 int bound = get_array_upper_bound_int(type, 0);
846 emit_uleb128(DW_TAG_subrange_type);
853 static void emit_compound_type_abbrev(void)
855 begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
856 register_attribute(DW_AT_byte_size, DW_FORM_udata);
857 // TODO register_dbginfo_attributes();
860 begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
861 register_attribute(DW_AT_byte_size, DW_FORM_udata);
862 // TODO register_dbginfo_attributes();
865 begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
866 register_attribute(DW_AT_byte_size, DW_FORM_udata);
867 // TODO register_dbginfo_attributes();
870 begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
871 register_attribute(DW_AT_type, DW_FORM_ref4);
872 register_attribute(DW_AT_name, DW_FORM_string);
873 register_dbginfo_attributes();
874 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
877 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
878 register_attribute(DW_AT_byte_size, DW_FORM_udata);
879 register_attribute(DW_AT_bit_size, DW_FORM_udata);
880 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
881 register_attribute(DW_AT_type, DW_FORM_ref4);
882 register_attribute(DW_AT_name, DW_FORM_string);
883 register_dbginfo_attributes();
884 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
888 static void emit_op_plus_uconst(unsigned value)
890 emit_int8(DW_OP_plus_uconst);
894 static void emit_compound_type(dwarf_t *env, const ir_type *type)
897 size_t n_members = get_compound_n_members(type);
899 for (i = 0; i < n_members; ++i) {
900 ir_entity *member = get_compound_member(type, i);
901 ir_type *member_type = get_entity_type(member);
902 if (is_Primitive_type(member_type)) {
903 ir_type *base = get_primitive_base_type(member_type);
907 emit_type(env, member_type);
910 emit_type_label(type);
911 if (is_Struct_type(type)) {
912 emit_uleb128(DW_TAG_structure_type);
913 } else if (is_Union_type(type)) {
914 emit_uleb128(DW_TAG_union_type);
916 assert(is_Class_type(type));
917 emit_uleb128(DW_TAG_class_type);
919 emit_uleb128(get_type_size_bytes(type));
920 for (i = 0; i < n_members; ++i) {
921 ir_entity *member = get_compound_member(type, i);
922 ir_type *member_type = get_entity_type(member);
923 int offset = get_entity_offset(member);
926 if (is_Primitive_type(member_type) &&
927 (base = get_primitive_base_type(member_type))) {
928 unsigned bit_offset = get_entity_offset_bits_remainder(member);
929 unsigned base_size = get_type_size_bytes(base);
930 ir_mode *mode = get_type_mode(member_type);
931 unsigned bit_size = get_mode_size_bits(mode);
933 bit_offset = base_size*8 - bit_offset - bit_size;
935 emit_uleb128(abbrev_bitfield_member);
936 emit_uleb128(base_size);
937 emit_uleb128(bit_size);
938 emit_uleb128(bit_offset);
941 emit_uleb128(DW_TAG_member);
944 emit_type_address(member_type);
945 emit_string(get_entity_name(member));
946 emit_dbginfo(env, get_entity_dbg_info(member));
948 emit_int8(1 + get_uleb128_size(offset));
949 emit_op_plus_uconst(offset);
955 static void emit_subroutine_type_abbrev(void)
957 begin_abbrev(DW_TAG_subroutine_type,
958 DW_TAG_subroutine_type, DW_CHILDREN_yes);
959 register_attribute(DW_AT_prototyped, DW_FORM_flag);
960 register_attribute(DW_AT_type, DW_FORM_ref4);
963 begin_abbrev(abbrev_void_subroutine_type,
964 DW_TAG_subroutine_type, DW_CHILDREN_yes);
965 register_attribute(DW_AT_prototyped, DW_FORM_flag);
968 begin_abbrev(abbrev_unnamed_formal_parameter,
969 DW_TAG_formal_parameter, DW_CHILDREN_no);
970 register_attribute(DW_AT_type, DW_FORM_ref4);
974 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
976 size_t n_params = get_method_n_params(type);
977 size_t n_ress = get_method_n_ress(type);
979 for (i = 0; i < n_params; ++i) {
980 ir_type *param_type = get_method_param_type(type, i);
981 emit_type(env, param_type);
983 for (i = 0; i < n_ress; ++i) {
984 ir_type *res_type = get_method_res_type(type, i);
985 emit_type(env, res_type);
988 emit_type_label(type);
989 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
990 emit_int8(1); /* prototyped */
992 /* dwarf only supports 1 return type */
993 ir_type *res_type = get_method_res_type(type, 0);
994 emit_type_address(res_type);
997 for (i = 0; i < n_params; ++i) {
998 ir_type *param_type = get_method_param_type(type, i);
999 emit_uleb128(abbrev_unnamed_formal_parameter);
1000 emit_type_address(param_type);
1005 static void emit_type(dwarf_t *env, ir_type *type)
1007 if (pset_new_insert(&env->emitted_types, type))
1010 switch (get_type_tpop_code(type)) {
1011 case tpo_primitive: emit_base_type(type); break;
1012 case tpo_pointer: emit_pointer_type(env, type); break;
1013 case tpo_array: emit_array_type(env, type); break;
1016 case tpo_union: emit_compound_type(env, type); break;
1017 case tpo_method: emit_subroutine_type(env, type); break;
1019 panic("bedwarf: type %+F not implemented yet", type);
1023 static void emit_op_addr(const ir_entity *entity)
1025 emit_int8(DW_OP_addr);
1026 be_emit_cstring("\t.long ");
1027 be_gas_emit_entity(entity);
1029 be_emit_write_line();
1032 static void emit_variable_abbrev(void)
1034 begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
1035 register_attribute(DW_AT_name, DW_FORM_string);
1036 register_attribute(DW_AT_type, DW_FORM_ref4);
1037 register_attribute(DW_AT_external, DW_FORM_flag);
1038 register_dbginfo_attributes();
1039 register_attribute(DW_AT_location, DW_FORM_block1);
1043 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
1045 dwarf_t *env = (dwarf_t*) handle;
1046 ir_type *type = get_entity_type(entity);
1048 if (get_entity_ld_name(entity)[0] == '\0')
1051 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1053 emit_type(env, type);
1055 emit_entity_label(entity);
1056 emit_uleb128(DW_TAG_variable);
1057 emit_string(get_entity_ld_name(entity));
1058 emit_type_address(type);
1059 emit_int8(is_extern_entity(entity));
1060 emit_dbginfo(env, get_entity_dbg_info(entity));
1061 /* DW_AT_location */
1062 emit_int8(5); /* block length */
1063 emit_op_addr(entity);
1065 ARR_APP1(const ir_entity*, env->pubnames_list, entity);
1068 static void emit_compile_unit_abbrev(void)
1070 begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
1071 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
1072 register_attribute(DW_AT_producer, DW_FORM_string);
1073 register_attribute(DW_AT_name, DW_FORM_string);
1075 register_attribute(DW_AT_language, DW_FORM_data2);
1076 if (comp_dir != NULL)
1077 register_attribute(DW_AT_comp_dir, DW_FORM_string);
1081 static void emit_abbrev(void)
1083 /* create abbreviation for compile_unit */
1084 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
1086 emit_label("abbrev_begin");
1088 emit_compile_unit_abbrev();
1089 emit_variable_abbrev();
1090 emit_subprogram_abbrev();
1091 emit_base_type_abbrev();
1092 emit_pointer_type_abbrev();
1093 emit_array_type_abbrev();
1094 emit_compound_type_abbrev();
1095 emit_subroutine_type_abbrev();
1100 * start a new source object (compilation unit)
1102 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
1108 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1109 emit_label("info_section_begin");
1110 emit_label("info_begin");
1112 /* length of compilation unit info */
1113 emit_size("compile_unit_begin", "compile_unit_end");
1114 emit_label("compile_unit_begin");
1115 emit_int16(2); /* dwarf version */
1116 emit_address("abbrev_begin");
1117 emit_int8(4); /* pointer size */
1119 /* compile_unit die */
1120 emit_uleb128(DW_TAG_compile_unit);
1121 emit_address("line_section_begin");
1122 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
1123 ir_get_version_minor(),
1124 ir_get_version_revision());
1125 emit_string(filename);
1127 emit_int16(DW_LANG_C_plus_plus);
1128 if (comp_dir != NULL)
1129 emit_string(comp_dir);
1132 static void dwarf_unit_end(dbg_handle *handle)
1134 dwarf_t *env = (dwarf_t*)handle;
1136 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1137 emit_label("section_end");
1139 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1140 emit_uleb128(0); /* end of compile_unit DIE */
1142 emit_label("compile_unit_end");
1144 emit_line_info(env);
1149 * Close the drwarf handler.
1151 static void dwarf_close(dbg_handle *handle)
1153 dwarf_t *h = (dwarf_t *)handle;
1154 pmap_destroy(h->file_map);
1155 DEL_ARR_F(h->file_list);
1156 DEL_ARR_F(h->pubnames_list);
1157 pset_new_destroy(&h->emitted_types);
1161 /** The drwarf operations. */
1162 static const debug_ops dwarf_ops = {
1173 /* Opens a drwarf handler */
1174 static dbg_handle *be_dwarf_open(void)
1176 dwarf_t *h = XMALLOCZ(dwarf_t);
1178 h->base.ops = &dwarf_ops;
1179 h->file_map = pmap_create();
1180 h->file_list = NEW_ARR_F(const char*, 0);
1181 h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
1182 pset_new_init(&h->emitted_types);
1187 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
1188 void be_init_dwarf(void)
1190 be_register_dbgout_module("dwarf", be_dwarf_open);
1193 void be_dwarf_set_source_language(dwarf_source_language new_language)
1195 language = new_language;
1198 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
1200 comp_dir = new_comp_dir;