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
32 #include "bedwarf_t.h"
47 #include "lc_opts_enum.h"
50 #include "beemitter.h"
61 static int debug_level = LEVEL_NONE;
64 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
65 * the cases where we need multiple ids with the same DW_TAG we define new IDs
68 typedef enum custom_abbrevs {
69 abbrev_void_subprogram = 1,
71 abbrev_formal_parameter,
72 abbrev_unnamed_formal_parameter,
73 abbrev_formal_parameter_no_location,
78 abbrev_void_pointer_type,
81 abbrev_structure_type,
85 abbrev_bitfield_member,
86 abbrev_subroutine_type,
87 abbrev_void_subroutine_type,
93 typedef struct dwarf_t {
94 const ir_entity *cur_ent; /**< current method entity */
95 unsigned next_type_nr; /**< next type number */
96 pmap *file_map; /**< a map from file names to number in file list */
97 const char **file_list;
98 const ir_entity **pubnames_list;
99 pset_new_t emitted_types;
100 const char *main_file; /**< name of the main source file */
101 const char *curr_file; /**< name of the current source file */
108 static dwarf_source_language language;
109 static char *comp_dir;
111 static unsigned insert_file(const char *filename)
114 void *entry = pmap_get(void, env.file_map, filename);
116 return PTR_TO_INT(entry);
118 ARR_APP1(const char*, env.file_list, filename);
119 num = (unsigned)ARR_LEN(env.file_list);
120 pmap_insert(env.file_map, filename, INT_TO_PTR(num));
122 /* TODO: quote chars in string */
123 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
127 static void emit_int32(uint32_t value)
129 be_emit_irprintf("\t.long %u\n", value);
130 be_emit_write_line();
133 static void emit_int16(uint16_t value)
135 be_emit_irprintf("\t.short %u\n", value);
136 be_emit_write_line();
139 static void emit_int8(uint8_t value)
141 be_emit_irprintf("\t.byte %u\n", value);
142 be_emit_write_line();
145 static void emit_uleb128(unsigned value)
147 be_emit_irprintf("\t.uleb128 0x%x\n", value);
148 be_emit_write_line();
151 static unsigned get_uleb128_size(unsigned value)
157 } while (value != 0);
161 static void emit_sleb128(long value)
163 be_emit_irprintf("\t.sleb128 %ld\n", value);
164 be_emit_write_line();
167 static unsigned get_sleb128_size(long value)
173 } while (value != 0 && value != -1);
177 static void emit_ref(const ir_entity *entity)
179 be_emit_cstring("\t.long ");
180 be_gas_emit_entity(entity);
182 be_emit_write_line();
185 static void emit_string_printf(const char *fmt, ...)
189 be_emit_cstring("\t.asciz \"");
190 be_emit_irvprintf(fmt, ap);
191 be_emit_cstring("\"\n");
194 be_emit_write_line();
197 static void emit_address(const char *name)
199 be_emit_cstring("\t.long ");
200 be_emit_string(be_gas_get_private_prefix());
201 be_emit_string(name);
203 be_emit_write_line();
206 static void emit_size(const char *from_label, const char *to_label)
208 be_emit_cstring("\t.long ");
209 be_emit_string(be_gas_get_private_prefix());
210 be_emit_string(to_label);
211 be_emit_cstring(" - ");
212 be_emit_string(be_gas_get_private_prefix());
213 be_emit_string(from_label);
215 be_emit_write_line();
218 static void emit_label(const char *name)
220 be_emit_string(be_gas_get_private_prefix());
221 be_emit_string(name);
222 be_emit_cstring(":\n");
223 be_emit_write_line();
226 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
228 emit_uleb128(attribute);
232 static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
233 dw_children children)
240 static void end_abbrev(void)
246 static void emit_line_info(void)
248 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
250 emit_label("line_section_begin");
251 /* on elf systems gas handles producing the line info for us, and we
252 * don't have to do anything */
253 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
255 emit_size("line_info_begin", "line_info_end");
257 emit_label("line_info_begin");
258 emit_int16(2); /* version */
259 emit_size("line_info_prolog_begin", "line_info_prolog_end");
260 emit_label("line_info_prolog_begin");
261 emit_int8(1); /* len of smallest instruction TODO: query from backend */
262 emit_int8(1); /* default is statement */
263 emit_int8(246); /* line base */
264 emit_int8(245); /* line range */
265 emit_int8(10); /* opcode base */
277 /* include directory list */
278 be_gas_emit_cstring("/foo/bar");
282 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
283 be_gas_emit_cstring(env.file_list[i]);
284 emit_uleb128(1); /* directory */
285 emit_uleb128(0); /* modification time */
286 emit_uleb128(0); /* file length */
290 emit_label("line_info_prolog_end");
292 /* TODO: put the line_info program here */
294 emit_label("line_info_end");
298 static void emit_pubnames(void)
302 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
304 emit_size("pubnames_begin", "pubnames_end");
305 emit_label("pubnames_begin");
307 emit_int16(2); /* version */
308 emit_size("info_section_begin", "info_begin");
309 emit_size("compile_unit_begin", "compile_unit_end");
311 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
312 const ir_entity *entity = env.pubnames_list[i];
313 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
314 be_gas_get_private_prefix(),
315 get_entity_nr(entity), be_gas_get_private_prefix());
316 be_gas_emit_cstring(get_entity_name(entity));
320 emit_label("pubnames_end");
323 void be_dwarf_location(dbg_info *dbgi)
328 if (debug_level < LEVEL_LOCATIONS)
330 loc = ir_retrieve_dbg_info(dbgi);
334 filenum = insert_file(loc.file);
335 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
336 be_emit_write_line();
339 void be_dwarf_callframe_register(const arch_register_t *reg)
341 if (debug_level < LEVEL_FRAMEINFO)
343 be_emit_cstring("\t.cfi_def_cfa_register ");
344 be_emit_irprintf("%d\n", reg->dwarf_number);
345 be_emit_write_line();
348 void be_dwarf_callframe_offset(int offset)
350 if (debug_level < LEVEL_FRAMEINFO)
352 be_emit_cstring("\t.cfi_def_cfa_offset ");
353 be_emit_irprintf("%d\n", offset);
354 be_emit_write_line();
357 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
359 if (debug_level < LEVEL_FRAMEINFO)
361 be_emit_cstring("\t.cfi_offset ");
362 be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
363 be_emit_write_line();
366 static bool is_extern_entity(const ir_entity *entity)
368 ir_visited_t visibility = get_entity_visibility(entity);
369 return visibility == ir_visibility_external;
372 static void emit_entity_label(const ir_entity *entity)
374 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
375 get_entity_nr(entity));
376 be_emit_write_line();
379 static void register_dbginfo_attributes(void)
381 register_attribute(DW_AT_decl_file, DW_FORM_udata);
382 register_attribute(DW_AT_decl_line, DW_FORM_udata);
383 register_attribute(DW_AT_decl_column, DW_FORM_udata);
386 static void emit_dbginfo(const dbg_info *dbgi)
388 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
389 unsigned const file = loc.file ? insert_file(loc.file) : 0;
391 emit_uleb128(loc.line);
392 emit_uleb128(loc.column);
395 static void emit_type_address(const ir_type *type)
397 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
398 be_gas_get_private_prefix(),
399 get_type_nr(type), be_gas_get_private_prefix());
400 be_emit_write_line();
403 static void emit_subprogram_abbrev(void)
405 begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
406 register_attribute(DW_AT_name, DW_FORM_string);
407 register_dbginfo_attributes();
408 register_attribute(DW_AT_type, DW_FORM_ref4);
409 register_attribute(DW_AT_external, DW_FORM_flag);
410 register_attribute(DW_AT_low_pc, DW_FORM_addr);
411 register_attribute(DW_AT_high_pc, DW_FORM_addr);
412 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
413 if (debug_level >= LEVEL_FRAMEINFO)
414 register_attribute(DW_AT_frame_base, DW_FORM_block1);
417 begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
418 register_attribute(DW_AT_name, DW_FORM_string);
419 register_dbginfo_attributes();
420 register_attribute(DW_AT_external, DW_FORM_flag);
421 register_attribute(DW_AT_low_pc, DW_FORM_addr);
422 register_attribute(DW_AT_high_pc, DW_FORM_addr);
423 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
424 if (debug_level >= LEVEL_FRAMEINFO)
425 register_attribute(DW_AT_frame_base, DW_FORM_block1);
428 begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
430 register_attribute(DW_AT_name, DW_FORM_string);
431 register_dbginfo_attributes();
432 register_attribute(DW_AT_type, DW_FORM_ref4);
433 register_attribute(DW_AT_location, DW_FORM_block1);
436 begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
438 register_attribute(DW_AT_name, DW_FORM_string);
439 register_dbginfo_attributes();
440 register_attribute(DW_AT_type, DW_FORM_ref4);
444 static void emit_type(ir_type *type);
446 static void emit_stack_location(long offset)
448 unsigned size = 1 + get_sleb128_size(offset);
450 emit_int8(DW_OP_fbreg);
451 emit_sleb128(offset);
454 static void emit_function_parameters(const ir_entity *entity,
455 const parameter_dbg_info_t *infos)
457 ir_type *type = get_entity_type(entity);
458 size_t n_params = get_method_n_params(type);
459 dbg_info *dbgi = get_entity_dbg_info(entity);
461 for (i = 0; i < n_params; ++i) {
462 ir_type *param_type = get_method_param_type(type, i);
464 if (infos != NULL && infos[i].entity != NULL) {
465 long const offset = get_entity_offset(infos[i].entity);
466 emit_uleb128(abbrev_formal_parameter);
467 emit_string_printf("arg%u", (unsigned)i);
469 emit_type_address(param_type);
470 emit_stack_location(offset);
472 emit_uleb128(abbrev_formal_parameter_no_location);
473 emit_string_printf("arg%u", (unsigned)i);
475 emit_type_address(param_type);
480 void be_dwarf_method_before(const ir_entity *entity,
481 const parameter_dbg_info_t *parameter_infos)
483 if (debug_level < LEVEL_BASIC)
486 ir_type *type = get_entity_type(entity);
487 size_t n_ress = get_method_n_ress(type);
488 size_t n_params = get_method_n_params(type);
491 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
494 ir_type *res = get_method_res_type(type, 0);
497 for (i = 0; i < n_params; ++i) {
498 ir_type *param_type = get_method_param_type(type, i);
499 emit_type(param_type);
502 emit_entity_label(entity);
503 emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
504 be_gas_emit_cstring(get_entity_ld_name(entity));
505 emit_dbginfo(get_entity_dbg_info(entity));
507 ir_type *res = get_method_res_type(type, 0);
508 emit_type_address(res);
510 emit_int8(is_extern_entity(entity));
512 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
513 get_entity_ld_name(entity));
514 /* frame_base prog */
516 emit_int8(DW_OP_call_frame_cfa);
518 emit_function_parameters(entity, parameter_infos);
521 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
523 env.cur_ent = entity;
527 void be_dwarf_method_begin(void)
529 if (debug_level < LEVEL_FRAMEINFO)
531 be_emit_cstring("\t.cfi_startproc\n");
532 be_emit_write_line();
535 void be_dwarf_method_end(void)
537 if (debug_level < LEVEL_BASIC)
539 const ir_entity *entity = env.cur_ent;
540 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
541 get_entity_ld_name(entity));
543 if (debug_level >= LEVEL_FRAMEINFO) {
544 be_emit_cstring("\t.cfi_endproc\n");
545 be_emit_write_line();
549 static void emit_base_type_abbrev(void)
551 begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
552 register_attribute(DW_AT_encoding, DW_FORM_data1);
553 register_attribute(DW_AT_byte_size, DW_FORM_data1);
554 register_attribute(DW_AT_name, DW_FORM_string);
558 static void emit_type_label(const ir_type *type)
560 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
561 be_emit_write_line();
564 static void emit_base_type(const ir_type *type)
567 ir_mode *mode = get_type_mode(type);
568 ir_print_type(buf, sizeof(buf), type);
570 emit_type_label(type);
571 emit_uleb128(abbrev_base_type);
572 if (mode_is_int(mode)) {
574 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
575 emit_int8(DW_ATE_boolean);
577 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
579 } else if (mode_is_reference(mode)) {
580 emit_int8(DW_ATE_address);
581 } else if (mode_is_float(mode)) {
582 emit_int8(DW_ATE_float);
584 panic("mode not implemented yet");
586 emit_int8(get_mode_size_bytes(mode));
587 be_gas_emit_cstring(buf);
590 static void emit_pointer_type_abbrev(void)
592 begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
593 register_attribute(DW_AT_type, DW_FORM_ref4);
594 register_attribute(DW_AT_byte_size, DW_FORM_data1);
597 /* for void* pointer s*/
598 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
599 register_attribute(DW_AT_byte_size, DW_FORM_data1);
603 static void emit_pointer_type(const ir_type *type)
605 ir_type *points_to = get_pointer_points_to_type(type);
606 unsigned size = get_type_size_bytes(type);
609 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
610 emit_type(points_to);
612 emit_type_label(type);
613 emit_uleb128(abbrev_pointer_type);
614 emit_type_address(points_to);
616 emit_type_label(type);
617 emit_uleb128(abbrev_void_pointer_type);
622 static void emit_array_type_abbrev(void)
624 begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
625 register_attribute(DW_AT_type, DW_FORM_ref4);
628 begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
629 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
633 static void emit_array_type(const ir_type *type)
635 ir_type *element_type = get_array_element_type(type);
637 if (get_array_n_dimensions(type) != 1)
638 panic("multidimensional arrays no supported yet");
640 emit_type(element_type);
642 emit_type_label(type);
643 emit_uleb128(abbrev_array_type);
644 emit_type_address(element_type);
646 if (has_array_upper_bound(type, 0)) {
647 int bound = get_array_upper_bound_int(type, 0);
648 emit_uleb128(abbrev_subrange_type);
655 static void emit_compound_type_abbrev(void)
657 begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
658 register_attribute(DW_AT_byte_size, DW_FORM_udata);
659 // TODO register_dbginfo_attributes();
662 begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
663 register_attribute(DW_AT_byte_size, DW_FORM_udata);
664 // TODO register_dbginfo_attributes();
667 begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
668 register_attribute(DW_AT_byte_size, DW_FORM_udata);
669 // TODO register_dbginfo_attributes();
672 begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
673 register_attribute(DW_AT_type, DW_FORM_ref4);
674 register_attribute(DW_AT_name, DW_FORM_string);
675 register_dbginfo_attributes();
676 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
679 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
680 register_attribute(DW_AT_byte_size, DW_FORM_udata);
681 register_attribute(DW_AT_bit_size, DW_FORM_udata);
682 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
683 register_attribute(DW_AT_type, DW_FORM_ref4);
684 register_attribute(DW_AT_name, DW_FORM_string);
685 register_dbginfo_attributes();
686 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
690 static void emit_op_plus_uconst(unsigned value)
692 emit_int8(DW_OP_plus_uconst);
696 static void emit_compound_type(const ir_type *type)
699 size_t n_members = get_compound_n_members(type);
701 for (i = 0; i < n_members; ++i) {
702 ir_entity *member = get_compound_member(type, i);
703 ir_type *member_type = get_entity_type(member);
704 if (is_Primitive_type(member_type)) {
705 ir_type *base = get_primitive_base_type(member_type);
709 emit_type(member_type);
712 emit_type_label(type);
713 if (is_Struct_type(type)) {
714 emit_uleb128(abbrev_structure_type);
715 } else if (is_Union_type(type)) {
716 emit_uleb128(abbrev_union_type);
718 assert(is_Class_type(type));
719 emit_uleb128(abbrev_class_type);
721 emit_uleb128(get_type_size_bytes(type));
722 for (i = 0; i < n_members; ++i) {
723 ir_entity *member = get_compound_member(type, i);
724 ir_type *member_type = get_entity_type(member);
725 int offset = get_entity_offset(member);
728 if (is_Primitive_type(member_type) &&
729 (base = get_primitive_base_type(member_type))) {
730 unsigned bit_offset = get_entity_offset_bits_remainder(member);
731 unsigned base_size = get_type_size_bytes(base);
732 ir_mode *mode = get_type_mode(member_type);
733 unsigned bit_size = get_mode_size_bits(mode);
735 bit_offset = base_size*8 - bit_offset - bit_size;
737 emit_uleb128(abbrev_bitfield_member);
738 emit_uleb128(base_size);
739 emit_uleb128(bit_size);
740 emit_uleb128(bit_offset);
743 emit_uleb128(abbrev_member);
746 emit_type_address(member_type);
747 be_gas_emit_cstring(get_entity_name(member));
748 emit_dbginfo(get_entity_dbg_info(member));
750 emit_int8(1 + get_uleb128_size(offset));
751 emit_op_plus_uconst(offset);
757 static void emit_subroutine_type_abbrev(void)
759 begin_abbrev(abbrev_subroutine_type,
760 DW_TAG_subroutine_type, DW_CHILDREN_yes);
761 register_attribute(DW_AT_prototyped, DW_FORM_flag);
762 register_attribute(DW_AT_type, DW_FORM_ref4);
765 begin_abbrev(abbrev_void_subroutine_type,
766 DW_TAG_subroutine_type, DW_CHILDREN_yes);
767 register_attribute(DW_AT_prototyped, DW_FORM_flag);
770 begin_abbrev(abbrev_unnamed_formal_parameter,
771 DW_TAG_formal_parameter, DW_CHILDREN_no);
772 register_attribute(DW_AT_type, DW_FORM_ref4);
776 static void emit_subroutine_type(const ir_type *type)
778 size_t n_params = get_method_n_params(type);
779 size_t n_ress = get_method_n_ress(type);
781 for (i = 0; i < n_params; ++i) {
782 ir_type *param_type = get_method_param_type(type, i);
783 emit_type(param_type);
785 for (i = 0; i < n_ress; ++i) {
786 ir_type *res_type = get_method_res_type(type, i);
790 emit_type_label(type);
791 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
792 emit_int8(1); /* prototyped */
794 /* dwarf only supports 1 return type */
795 ir_type *res_type = get_method_res_type(type, 0);
796 emit_type_address(res_type);
799 for (i = 0; i < n_params; ++i) {
800 ir_type *param_type = get_method_param_type(type, i);
801 emit_uleb128(abbrev_unnamed_formal_parameter);
802 emit_type_address(param_type);
807 static void emit_type(ir_type *type)
809 if (!pset_new_insert(&env.emitted_types, type))
812 switch (get_type_tpop_code(type)) {
813 case tpo_primitive: emit_base_type(type); break;
814 case tpo_pointer: emit_pointer_type(type); break;
815 case tpo_array: emit_array_type(type); break;
818 case tpo_union: emit_compound_type(type); break;
819 case tpo_method: emit_subroutine_type(type); break;
821 panic("type %+F not implemented yet", type);
825 static void emit_op_addr(const ir_entity *entity)
827 emit_int8(DW_OP_addr);
828 be_emit_cstring("\t.long ");
829 be_gas_emit_entity(entity);
831 be_emit_write_line();
834 static void emit_variable_abbrev(void)
836 begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
837 register_attribute(DW_AT_name, DW_FORM_string);
838 register_attribute(DW_AT_type, DW_FORM_ref4);
839 register_attribute(DW_AT_external, DW_FORM_flag);
840 register_dbginfo_attributes();
841 register_attribute(DW_AT_location, DW_FORM_block1);
845 void be_dwarf_variable(const ir_entity *entity)
847 ir_type *type = get_entity_type(entity);
849 if (debug_level < LEVEL_BASIC)
851 if (get_entity_ld_name(entity)[0] == '\0')
853 if (!entity_has_definition(entity))
856 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
860 emit_entity_label(entity);
861 emit_uleb128(abbrev_variable);
862 be_gas_emit_cstring(get_entity_ld_name(entity));
863 emit_type_address(type);
864 emit_int8(is_extern_entity(entity));
865 emit_dbginfo(get_entity_dbg_info(entity));
867 emit_int8(5); /* block length */
868 emit_op_addr(entity);
870 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
873 static void emit_compile_unit_abbrev(void)
875 begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
876 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
877 register_attribute(DW_AT_producer, DW_FORM_string);
878 register_attribute(DW_AT_name, DW_FORM_string);
880 register_attribute(DW_AT_language, DW_FORM_data2);
881 if (comp_dir != NULL)
882 register_attribute(DW_AT_comp_dir, DW_FORM_string);
886 static void emit_abbrev(void)
888 /* create abbreviation for compile_unit */
889 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
891 emit_label("abbrev_begin");
893 emit_compile_unit_abbrev();
894 emit_variable_abbrev();
895 emit_subprogram_abbrev();
896 emit_base_type_abbrev();
897 emit_pointer_type_abbrev();
898 emit_array_type_abbrev();
899 emit_compound_type_abbrev();
900 emit_subroutine_type_abbrev();
904 void be_dwarf_unit_begin(const char *filename)
906 if (debug_level < LEVEL_BASIC)
910 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
911 emit_label("info_section_begin");
912 emit_label("info_begin");
914 const backend_params *be_params = be_get_backend_param();
916 /* length of compilation unit info */
917 emit_size("compile_unit_begin", "compile_unit_end");
918 emit_label("compile_unit_begin");
919 emit_int16(3); /* dwarf version */
920 emit_address("abbrev_begin");
921 emit_int8(be_params->machine_size / 8); /* pointer size */
923 /* compile_unit die */
924 emit_uleb128(abbrev_compile_unit);
925 emit_address("line_section_begin");
926 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
927 ir_get_version_minor(), ir_get_version_revision());
928 be_gas_emit_cstring(filename);
930 emit_int16(language);
931 if (comp_dir != NULL)
932 be_gas_emit_cstring(comp_dir);
934 /* tell gas to emit cfi in debug_frame
935 * TODO: if we produce exception handling code then this should be
936 * .eh_frame (I also wonder if bad things happen if simply always
938 be_emit_cstring("\t.cfi_sections .debug_frame\n");
939 be_emit_write_line();
942 void be_dwarf_unit_end(void)
944 if (debug_level < LEVEL_BASIC)
946 be_gas_emit_switch_section(GAS_SECTION_TEXT);
947 emit_label("section_end");
949 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
950 emit_uleb128(0); /* end of compile_unit DIE */
952 emit_label("compile_unit_end");
958 void be_dwarf_close(void)
960 if (debug_level < LEVEL_BASIC)
962 pmap_destroy(env.file_map);
963 DEL_ARR_F(env.file_list);
964 DEL_ARR_F(env.pubnames_list);
965 pset_new_destroy(&env.emitted_types);
968 /* Opens a dwarf handler */
969 void be_dwarf_open(void)
971 if (debug_level < LEVEL_BASIC)
973 env.file_map = pmap_create();
974 env.file_list = NEW_ARR_F(const char*, 0);
975 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
976 pset_new_init(&env.emitted_types);
979 void be_dwarf_set_source_language(dwarf_source_language new_language)
981 language = new_language;
984 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
987 comp_dir = xstrdup(new_comp_dir);
990 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
991 void be_init_dwarf(void)
993 static const lc_opt_enum_int_items_t level_items[] = {
994 { "none", LEVEL_NONE },
995 { "basic", LEVEL_BASIC },
996 { "locations", LEVEL_LOCATIONS },
997 { "frameinfo", LEVEL_FRAMEINFO },
1000 static lc_opt_enum_int_var_t debug_level_opt = {
1001 &debug_level, level_items
1003 static lc_opt_table_entry_t be_main_options[] = {
1004 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
1008 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1009 lc_opt_add_table(be_grp, be_main_options);