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
31 #include "bedwarf_t.h"
45 #include "lc_opts_enum.h"
48 #include "beemitter.h"
58 static int debug_level = LEVEL_NONE;
61 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
62 * the cases where we need multiple ids with the same DW_TAG we define new IDs
65 typedef enum custom_abbrevs {
66 abbrev_void_subprogram,
68 abbrev_formal_parameter,
69 abbrev_unnamed_formal_parameter,
70 abbrev_formal_parameter_no_location,
75 abbrev_void_pointer_type,
78 abbrev_structure_type,
82 abbrev_bitfield_member,
83 abbrev_subroutine_type,
84 abbrev_void_subroutine_type,
90 typedef struct dwarf_t {
91 const ir_entity *cur_ent; /**< current method entity */
92 unsigned next_type_nr; /**< next type number */
93 pmap *file_map; /**< a map from file names to number in file list */
94 const char **file_list;
95 const ir_entity **pubnames_list;
96 pset_new_t emitted_types;
97 const char *main_file; /**< name of the main source file */
98 const char *curr_file; /**< name of the current source file */
105 static dwarf_source_language language;
106 static char *comp_dir;
108 static unsigned insert_file(const char *filename)
111 void *entry = pmap_get(void, env.file_map, filename);
113 return PTR_TO_INT(entry);
115 ARR_APP1(const char*, env.file_list, filename);
116 num = (unsigned)ARR_LEN(env.file_list);
117 pmap_insert(env.file_map, filename, INT_TO_PTR(num));
119 /* TODO: quote chars in string */
120 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
124 static void emit_int32(uint32_t value)
126 be_emit_irprintf("\t.long %u\n", value);
127 be_emit_write_line();
130 static void emit_int16(uint16_t value)
132 be_emit_irprintf("\t.short %u\n", value);
133 be_emit_write_line();
136 static void emit_int8(uint8_t value)
138 be_emit_irprintf("\t.byte %u\n", value);
139 be_emit_write_line();
142 static void emit_uleb128(unsigned value)
144 be_emit_irprintf("\t.uleb128 0x%x\n", value);
145 be_emit_write_line();
148 static unsigned get_uleb128_size(unsigned value)
154 } while (value != 0);
158 static void emit_sleb128(long value)
160 be_emit_irprintf("\t.sleb128 %ld\n", value);
161 be_emit_write_line();
164 static unsigned get_sleb128_size(long value)
170 } while (value != 0 && value != -1);
174 static void emit_string(const char *string)
176 /* TODO: quote special chars */
177 be_emit_irprintf("\t.asciz \"%s\"\n", string);
178 be_emit_write_line();
181 static void emit_ref(const ir_entity *entity)
183 be_emit_cstring("\t.long ");
184 be_gas_emit_entity(entity);
186 be_emit_write_line();
189 static void emit_string_printf(const char *fmt, ...)
193 be_emit_cstring("\t.asciz \"");
194 be_emit_irvprintf(fmt, ap);
195 be_emit_cstring("\"\n");
198 be_emit_write_line();
201 static void emit_address(const char *name)
203 be_emit_cstring("\t.long ");
204 be_emit_string(be_gas_get_private_prefix());
205 be_emit_string(name);
207 be_emit_write_line();
210 static void emit_size(const char *from_label, const char *to_label)
212 be_emit_cstring("\t.long ");
213 be_emit_string(be_gas_get_private_prefix());
214 be_emit_string(to_label);
215 be_emit_cstring(" - ");
216 be_emit_string(be_gas_get_private_prefix());
217 be_emit_string(from_label);
219 be_emit_write_line();
222 static void emit_label(const char *name)
224 be_emit_string(be_gas_get_private_prefix());
225 be_emit_string(name);
226 be_emit_cstring(":\n");
227 be_emit_write_line();
230 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
232 emit_uleb128(attribute);
236 static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
237 dw_children children)
244 static void end_abbrev(void)
250 static void emit_line_info(void)
252 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
254 emit_label("line_section_begin");
255 /* on elf systems gas handles producing the line info for us, and we
256 * don't have to do anything */
257 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
259 emit_size("line_info_begin", "line_info_end");
261 emit_label("line_info_begin");
262 emit_int16(2); /* version */
263 emit_size("line_info_prolog_begin", "line_info_prolog_end");
264 emit_label("line_info_prolog_begin");
265 emit_int8(1); /* len of smallest instruction TODO: query from backend */
266 emit_int8(1); /* default is statement */
267 emit_int8(246); /* line base */
268 emit_int8(245); /* line range */
269 emit_int8(10); /* opcode base */
281 /* include directory list */
282 emit_string("/foo/bar");
286 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
287 emit_string(env.file_list[i]);
288 emit_uleb128(1); /* directory */
289 emit_uleb128(0); /* modification time */
290 emit_uleb128(0); /* file length */
294 emit_label("line_info_prolog_end");
296 /* TODO: put the line_info program here */
298 emit_label("line_info_end");
302 static void emit_pubnames(void)
306 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
308 emit_size("pubnames_begin", "pubnames_end");
309 emit_label("pubnames_begin");
311 emit_int16(2); /* version */
312 emit_size("info_section_begin", "info_begin");
313 emit_size("compile_unit_begin", "compile_unit_end");
315 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
316 const ir_entity *entity = env.pubnames_list[i];
317 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
318 be_gas_get_private_prefix(),
319 get_entity_nr(entity), be_gas_get_private_prefix());
320 emit_string(get_entity_name(entity));
324 emit_label("pubnames_end");
327 void be_dwarf_location(dbg_info *dbgi)
332 if (debug_level < LEVEL_LOCATIONS)
334 loc = ir_retrieve_dbg_info(dbgi);
338 filenum = insert_file(loc.file);
339 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
340 be_emit_write_line();
343 void be_dwarf_callframe_register(const arch_register_t *reg)
345 if (debug_level < LEVEL_FRAMEINFO)
347 be_emit_cstring("\t.cfi_def_cfa_register ");
348 be_emit_irprintf("%d\n", reg->dwarf_number);
349 be_emit_write_line();
352 void be_dwarf_callframe_offset(int offset)
354 if (debug_level < LEVEL_FRAMEINFO)
356 be_emit_cstring("\t.cfi_def_cfa_offset ");
357 be_emit_irprintf("%d\n", offset);
358 be_emit_write_line();
361 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
363 if (debug_level < LEVEL_FRAMEINFO)
365 be_emit_cstring("\t.cfi_offset ");
366 be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
367 be_emit_write_line();
370 static bool is_extern_entity(const ir_entity *entity)
372 ir_visited_t visibility = get_entity_visibility(entity);
373 return visibility == ir_visibility_external;
376 static void emit_entity_label(const ir_entity *entity)
378 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
379 get_entity_nr(entity));
380 be_emit_write_line();
383 static void register_dbginfo_attributes(void)
385 register_attribute(DW_AT_decl_file, DW_FORM_udata);
386 register_attribute(DW_AT_decl_line, DW_FORM_udata);
387 register_attribute(DW_AT_decl_column, DW_FORM_udata);
390 static void emit_dbginfo(const dbg_info *dbgi)
392 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
393 unsigned const file = loc.file ? insert_file(loc.file) : 0;
395 emit_uleb128(loc.line);
396 emit_uleb128(loc.column);
399 static void emit_type_address(const ir_type *type)
401 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
402 be_gas_get_private_prefix(),
403 get_type_nr(type), be_gas_get_private_prefix());
404 be_emit_write_line();
407 static void emit_subprogram_abbrev(void)
409 begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
410 register_attribute(DW_AT_name, DW_FORM_string);
411 register_dbginfo_attributes();
412 register_attribute(DW_AT_type, DW_FORM_ref4);
413 register_attribute(DW_AT_external, DW_FORM_flag);
414 register_attribute(DW_AT_low_pc, DW_FORM_addr);
415 register_attribute(DW_AT_high_pc, DW_FORM_addr);
416 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
417 if (debug_level >= LEVEL_FRAMEINFO)
418 register_attribute(DW_AT_frame_base, DW_FORM_block1);
421 begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
422 register_attribute(DW_AT_name, DW_FORM_string);
423 register_dbginfo_attributes();
424 register_attribute(DW_AT_external, DW_FORM_flag);
425 register_attribute(DW_AT_low_pc, DW_FORM_addr);
426 register_attribute(DW_AT_high_pc, DW_FORM_addr);
427 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
428 if (debug_level >= LEVEL_FRAMEINFO)
429 register_attribute(DW_AT_frame_base, DW_FORM_block1);
432 begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
434 register_attribute(DW_AT_name, DW_FORM_string);
435 register_dbginfo_attributes();
436 register_attribute(DW_AT_type, DW_FORM_ref4);
437 register_attribute(DW_AT_location, DW_FORM_block1);
440 begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
442 register_attribute(DW_AT_name, DW_FORM_string);
443 register_dbginfo_attributes();
444 register_attribute(DW_AT_type, DW_FORM_ref4);
448 static void emit_type(ir_type *type);
450 static void emit_stack_location(long offset)
452 unsigned size = 1 + get_sleb128_size(offset);
454 emit_int8(DW_OP_fbreg);
455 emit_sleb128(offset);
458 static void emit_function_parameters(const ir_entity *entity,
459 const parameter_dbg_info_t *infos)
461 ir_type *type = get_entity_type(entity);
462 size_t n_params = get_method_n_params(type);
463 dbg_info *dbgi = get_entity_dbg_info(entity);
465 for (i = 0; i < n_params; ++i) {
466 ir_type *param_type = get_method_param_type(type, i);
468 if (infos != NULL && infos[i].entity != NULL) {
469 long const offset = get_entity_offset(infos[i].entity);
470 emit_uleb128(abbrev_formal_parameter);
471 emit_string_printf("arg%u", (unsigned)i);
473 emit_type_address(param_type);
474 emit_stack_location(offset);
476 emit_uleb128(abbrev_formal_parameter_no_location);
477 emit_string_printf("arg%u", (unsigned)i);
479 emit_type_address(param_type);
484 void be_dwarf_method_before(const ir_entity *entity,
485 const parameter_dbg_info_t *parameter_infos)
487 if (debug_level < LEVEL_BASIC)
490 ir_type *type = get_entity_type(entity);
491 size_t n_ress = get_method_n_ress(type);
492 size_t n_params = get_method_n_params(type);
495 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
498 ir_type *res = get_method_res_type(type, 0);
501 for (i = 0; i < n_params; ++i) {
502 ir_type *param_type = get_method_param_type(type, i);
503 emit_type(param_type);
506 emit_entity_label(entity);
507 emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
508 emit_string(get_entity_ld_name(entity));
509 emit_dbginfo(get_entity_dbg_info(entity));
511 ir_type *res = get_method_res_type(type, 0);
512 emit_type_address(res);
514 emit_int8(is_extern_entity(entity));
516 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
517 get_entity_ld_name(entity));
518 /* frame_base prog */
520 emit_int8(DW_OP_call_frame_cfa);
522 emit_function_parameters(entity, parameter_infos);
525 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
527 env.cur_ent = entity;
531 void be_dwarf_method_begin(void)
533 if (debug_level < LEVEL_FRAMEINFO)
535 be_emit_cstring("\t.cfi_startproc\n");
536 be_emit_write_line();
539 void be_dwarf_method_end(void)
541 if (debug_level < LEVEL_BASIC)
543 const ir_entity *entity = env.cur_ent;
544 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
545 get_entity_ld_name(entity));
547 if (debug_level >= LEVEL_FRAMEINFO) {
548 be_emit_cstring("\t.cfi_endproc\n");
549 be_emit_write_line();
553 static void emit_base_type_abbrev(void)
555 begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
556 register_attribute(DW_AT_encoding, DW_FORM_data1);
557 register_attribute(DW_AT_byte_size, DW_FORM_data1);
558 register_attribute(DW_AT_name, DW_FORM_string);
562 static void emit_type_label(const ir_type *type)
564 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
565 be_emit_write_line();
568 static void emit_base_type(const ir_type *type)
571 ir_mode *mode = get_type_mode(type);
572 ir_print_type(buf, sizeof(buf), type);
574 emit_type_label(type);
575 emit_uleb128(abbrev_base_type);
576 if (mode_is_int(mode)) {
578 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
579 emit_int8(DW_ATE_boolean);
581 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
583 } else if (mode_is_reference(mode)) {
584 emit_int8(DW_ATE_address);
585 } else if (mode_is_float(mode)) {
586 emit_int8(DW_ATE_float);
588 panic("mode not implemented yet");
590 emit_int8(get_mode_size_bytes(mode));
594 static void emit_pointer_type_abbrev(void)
596 begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
597 register_attribute(DW_AT_type, DW_FORM_ref4);
598 register_attribute(DW_AT_byte_size, DW_FORM_data1);
601 /* for void* pointer s*/
602 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
603 register_attribute(DW_AT_byte_size, DW_FORM_data1);
607 static void emit_pointer_type(const ir_type *type)
609 ir_type *points_to = get_pointer_points_to_type(type);
610 unsigned size = get_type_size_bytes(type);
613 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
614 emit_type(points_to);
616 emit_type_label(type);
617 emit_uleb128(abbrev_pointer_type);
618 emit_type_address(points_to);
620 emit_type_label(type);
621 emit_uleb128(abbrev_void_pointer_type);
626 static void emit_array_type_abbrev(void)
628 begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
629 register_attribute(DW_AT_type, DW_FORM_ref4);
632 begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
633 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
637 static void emit_array_type(const ir_type *type)
639 ir_type *element_type = get_array_element_type(type);
641 if (get_array_n_dimensions(type) != 1)
642 panic("multidimensional arrays no supported yet");
644 emit_type(element_type);
646 emit_type_label(type);
647 emit_uleb128(abbrev_array_type);
648 emit_type_address(element_type);
650 if (has_array_upper_bound(type, 0)) {
651 int bound = get_array_upper_bound_int(type, 0);
652 emit_uleb128(abbrev_subrange_type);
659 static void emit_compound_type_abbrev(void)
661 begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
662 register_attribute(DW_AT_byte_size, DW_FORM_udata);
663 // TODO register_dbginfo_attributes();
666 begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
667 register_attribute(DW_AT_byte_size, DW_FORM_udata);
668 // TODO register_dbginfo_attributes();
671 begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
672 register_attribute(DW_AT_byte_size, DW_FORM_udata);
673 // TODO register_dbginfo_attributes();
676 begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
677 register_attribute(DW_AT_type, DW_FORM_ref4);
678 register_attribute(DW_AT_name, DW_FORM_string);
679 register_dbginfo_attributes();
680 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
683 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
684 register_attribute(DW_AT_byte_size, DW_FORM_udata);
685 register_attribute(DW_AT_bit_size, DW_FORM_udata);
686 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
687 register_attribute(DW_AT_type, DW_FORM_ref4);
688 register_attribute(DW_AT_name, DW_FORM_string);
689 register_dbginfo_attributes();
690 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
694 static void emit_op_plus_uconst(unsigned value)
696 emit_int8(DW_OP_plus_uconst);
700 static void emit_compound_type(const ir_type *type)
703 size_t n_members = get_compound_n_members(type);
705 for (i = 0; i < n_members; ++i) {
706 ir_entity *member = get_compound_member(type, i);
707 ir_type *member_type = get_entity_type(member);
708 if (is_Primitive_type(member_type)) {
709 ir_type *base = get_primitive_base_type(member_type);
713 emit_type(member_type);
716 emit_type_label(type);
717 if (is_Struct_type(type)) {
718 emit_uleb128(abbrev_structure_type);
719 } else if (is_Union_type(type)) {
720 emit_uleb128(abbrev_union_type);
722 assert(is_Class_type(type));
723 emit_uleb128(abbrev_class_type);
725 emit_uleb128(get_type_size_bytes(type));
726 for (i = 0; i < n_members; ++i) {
727 ir_entity *member = get_compound_member(type, i);
728 ir_type *member_type = get_entity_type(member);
729 int offset = get_entity_offset(member);
732 if (is_Primitive_type(member_type) &&
733 (base = get_primitive_base_type(member_type))) {
734 unsigned bit_offset = get_entity_offset_bits_remainder(member);
735 unsigned base_size = get_type_size_bytes(base);
736 ir_mode *mode = get_type_mode(member_type);
737 unsigned bit_size = get_mode_size_bits(mode);
739 bit_offset = base_size*8 - bit_offset - bit_size;
741 emit_uleb128(abbrev_bitfield_member);
742 emit_uleb128(base_size);
743 emit_uleb128(bit_size);
744 emit_uleb128(bit_offset);
747 emit_uleb128(abbrev_member);
750 emit_type_address(member_type);
751 emit_string(get_entity_name(member));
752 emit_dbginfo(get_entity_dbg_info(member));
754 emit_int8(1 + get_uleb128_size(offset));
755 emit_op_plus_uconst(offset);
761 static void emit_subroutine_type_abbrev(void)
763 begin_abbrev(abbrev_subroutine_type,
764 DW_TAG_subroutine_type, DW_CHILDREN_yes);
765 register_attribute(DW_AT_prototyped, DW_FORM_flag);
766 register_attribute(DW_AT_type, DW_FORM_ref4);
769 begin_abbrev(abbrev_void_subroutine_type,
770 DW_TAG_subroutine_type, DW_CHILDREN_yes);
771 register_attribute(DW_AT_prototyped, DW_FORM_flag);
774 begin_abbrev(abbrev_unnamed_formal_parameter,
775 DW_TAG_formal_parameter, DW_CHILDREN_no);
776 register_attribute(DW_AT_type, DW_FORM_ref4);
780 static void emit_subroutine_type(const ir_type *type)
782 size_t n_params = get_method_n_params(type);
783 size_t n_ress = get_method_n_ress(type);
785 for (i = 0; i < n_params; ++i) {
786 ir_type *param_type = get_method_param_type(type, i);
787 emit_type(param_type);
789 for (i = 0; i < n_ress; ++i) {
790 ir_type *res_type = get_method_res_type(type, i);
794 emit_type_label(type);
795 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
796 emit_int8(1); /* prototyped */
798 /* dwarf only supports 1 return type */
799 ir_type *res_type = get_method_res_type(type, 0);
800 emit_type_address(res_type);
803 for (i = 0; i < n_params; ++i) {
804 ir_type *param_type = get_method_param_type(type, i);
805 emit_uleb128(abbrev_unnamed_formal_parameter);
806 emit_type_address(param_type);
811 static void emit_type(ir_type *type)
813 if (!pset_new_insert(&env.emitted_types, type))
816 switch (get_type_tpop_code(type)) {
817 case tpo_primitive: emit_base_type(type); break;
818 case tpo_pointer: emit_pointer_type(type); break;
819 case tpo_array: emit_array_type(type); break;
822 case tpo_union: emit_compound_type(type); break;
823 case tpo_method: emit_subroutine_type(type); break;
825 panic("type %+F not implemented yet", type);
829 static void emit_op_addr(const ir_entity *entity)
831 emit_int8(DW_OP_addr);
832 be_emit_cstring("\t.long ");
833 be_gas_emit_entity(entity);
835 be_emit_write_line();
838 static void emit_variable_abbrev(void)
840 begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
841 register_attribute(DW_AT_name, DW_FORM_string);
842 register_attribute(DW_AT_type, DW_FORM_ref4);
843 register_attribute(DW_AT_external, DW_FORM_flag);
844 register_dbginfo_attributes();
845 register_attribute(DW_AT_location, DW_FORM_block1);
849 void be_dwarf_variable(const ir_entity *entity)
851 ir_type *type = get_entity_type(entity);
853 if (debug_level < LEVEL_BASIC)
855 if (get_entity_ld_name(entity)[0] == '\0')
857 if (!entity_has_definition(entity))
860 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
864 emit_entity_label(entity);
865 emit_uleb128(abbrev_variable);
866 emit_string(get_entity_ld_name(entity));
867 emit_type_address(type);
868 emit_int8(is_extern_entity(entity));
869 emit_dbginfo(get_entity_dbg_info(entity));
871 emit_int8(5); /* block length */
872 emit_op_addr(entity);
874 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
877 static void emit_compile_unit_abbrev(void)
879 begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
880 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
881 register_attribute(DW_AT_producer, DW_FORM_string);
882 register_attribute(DW_AT_name, DW_FORM_string);
884 register_attribute(DW_AT_language, DW_FORM_data2);
885 if (comp_dir != NULL)
886 register_attribute(DW_AT_comp_dir, DW_FORM_string);
890 static void emit_abbrev(void)
892 /* create abbreviation for compile_unit */
893 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
895 emit_label("abbrev_begin");
897 emit_compile_unit_abbrev();
898 emit_variable_abbrev();
899 emit_subprogram_abbrev();
900 emit_base_type_abbrev();
901 emit_pointer_type_abbrev();
902 emit_array_type_abbrev();
903 emit_compound_type_abbrev();
904 emit_subroutine_type_abbrev();
908 void be_dwarf_unit_begin(const char *filename)
910 if (debug_level < LEVEL_BASIC)
914 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
915 emit_label("info_section_begin");
916 emit_label("info_begin");
918 /* length of compilation unit info */
919 emit_size("compile_unit_begin", "compile_unit_end");
920 emit_label("compile_unit_begin");
921 emit_int16(3); /* dwarf version */
922 emit_address("abbrev_begin");
923 emit_int8(4); /* pointer size, TODO: query backend */
925 /* compile_unit die */
926 emit_uleb128(abbrev_compile_unit);
927 emit_address("line_section_begin");
928 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
929 ir_get_version_minor(),
930 ir_get_version_revision());
931 emit_string(filename);
933 emit_int16(language);
934 if (comp_dir != NULL)
935 emit_string(comp_dir);
937 /* tell gas to emit cfi in debug_frame
938 * TODO: if we produce exception handling code then this should be
939 * .eh_frame (I also wonder if bad things happen if simply always
941 be_emit_cstring("\t.cfi_sections .debug_frame\n");
942 be_emit_write_line();
945 void be_dwarf_unit_end(void)
947 if (debug_level < LEVEL_BASIC)
949 be_gas_emit_switch_section(GAS_SECTION_TEXT);
950 emit_label("section_end");
952 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
953 emit_uleb128(0); /* end of compile_unit DIE */
955 emit_label("compile_unit_end");
961 void be_dwarf_close(void)
963 if (debug_level < LEVEL_BASIC)
965 pmap_destroy(env.file_map);
966 DEL_ARR_F(env.file_list);
967 DEL_ARR_F(env.pubnames_list);
968 pset_new_destroy(&env.emitted_types);
971 /* Opens a dwarf handler */
972 void be_dwarf_open(void)
974 if (debug_level < LEVEL_BASIC)
976 env.file_map = pmap_create();
977 env.file_list = NEW_ARR_F(const char*, 0);
978 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
979 pset_new_init(&env.emitted_types);
982 void be_dwarf_set_source_language(dwarf_source_language new_language)
984 language = new_language;
987 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
990 comp_dir = xstrdup(new_comp_dir);
993 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
994 void be_init_dwarf(void)
996 static const lc_opt_enum_int_items_t level_items[] = {
997 { "none", LEVEL_NONE },
998 { "basic", LEVEL_BASIC },
999 { "locations", LEVEL_LOCATIONS },
1000 { "frameinfo", LEVEL_FRAMEINFO },
1003 static lc_opt_enum_int_var_t debug_level_opt = {
1004 &debug_level, level_items
1006 static lc_opt_table_entry_t be_main_options[] = {
1007 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
1011 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1012 lc_opt_add_table(be_grp, be_main_options);