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 = 1,
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_ref(const ir_entity *entity)
176 be_emit_cstring("\t.long ");
177 be_gas_emit_entity(entity);
179 be_emit_write_line();
182 static void emit_string_printf(const char *fmt, ...)
186 be_emit_cstring("\t.asciz \"");
187 be_emit_irvprintf(fmt, ap);
188 be_emit_cstring("\"\n");
191 be_emit_write_line();
194 static void emit_address(const char *name)
196 be_emit_cstring("\t.long ");
197 be_emit_string(be_gas_get_private_prefix());
198 be_emit_string(name);
200 be_emit_write_line();
203 static void emit_size(const char *from_label, const char *to_label)
205 be_emit_cstring("\t.long ");
206 be_emit_string(be_gas_get_private_prefix());
207 be_emit_string(to_label);
208 be_emit_cstring(" - ");
209 be_emit_string(be_gas_get_private_prefix());
210 be_emit_string(from_label);
212 be_emit_write_line();
215 static void emit_label(const char *name)
217 be_emit_string(be_gas_get_private_prefix());
218 be_emit_string(name);
219 be_emit_cstring(":\n");
220 be_emit_write_line();
223 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
225 emit_uleb128(attribute);
229 static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
230 dw_children children)
237 static void end_abbrev(void)
243 static void emit_line_info(void)
245 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
247 emit_label("line_section_begin");
248 /* on elf systems gas handles producing the line info for us, and we
249 * don't have to do anything */
250 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
252 emit_size("line_info_begin", "line_info_end");
254 emit_label("line_info_begin");
255 emit_int16(2); /* version */
256 emit_size("line_info_prolog_begin", "line_info_prolog_end");
257 emit_label("line_info_prolog_begin");
258 emit_int8(1); /* len of smallest instruction TODO: query from backend */
259 emit_int8(1); /* default is statement */
260 emit_int8(246); /* line base */
261 emit_int8(245); /* line range */
262 emit_int8(10); /* opcode base */
274 /* include directory list */
275 be_gas_emit_cstring("/foo/bar");
279 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
280 be_gas_emit_cstring(env.file_list[i]);
281 emit_uleb128(1); /* directory */
282 emit_uleb128(0); /* modification time */
283 emit_uleb128(0); /* file length */
287 emit_label("line_info_prolog_end");
289 /* TODO: put the line_info program here */
291 emit_label("line_info_end");
295 static void emit_pubnames(void)
299 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
301 emit_size("pubnames_begin", "pubnames_end");
302 emit_label("pubnames_begin");
304 emit_int16(2); /* version */
305 emit_size("info_section_begin", "info_begin");
306 emit_size("compile_unit_begin", "compile_unit_end");
308 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
309 const ir_entity *entity = env.pubnames_list[i];
310 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
311 be_gas_get_private_prefix(),
312 get_entity_nr(entity), be_gas_get_private_prefix());
313 be_gas_emit_cstring(get_entity_name(entity));
317 emit_label("pubnames_end");
320 void be_dwarf_location(dbg_info *dbgi)
325 if (debug_level < LEVEL_LOCATIONS)
327 loc = ir_retrieve_dbg_info(dbgi);
331 filenum = insert_file(loc.file);
332 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
333 be_emit_write_line();
336 void be_dwarf_callframe_register(const arch_register_t *reg)
338 if (debug_level < LEVEL_FRAMEINFO)
340 be_emit_cstring("\t.cfi_def_cfa_register ");
341 be_emit_irprintf("%d\n", reg->dwarf_number);
342 be_emit_write_line();
345 void be_dwarf_callframe_offset(int offset)
347 if (debug_level < LEVEL_FRAMEINFO)
349 be_emit_cstring("\t.cfi_def_cfa_offset ");
350 be_emit_irprintf("%d\n", offset);
351 be_emit_write_line();
354 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
356 if (debug_level < LEVEL_FRAMEINFO)
358 be_emit_cstring("\t.cfi_offset ");
359 be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
360 be_emit_write_line();
363 static bool is_extern_entity(const ir_entity *entity)
365 ir_visited_t visibility = get_entity_visibility(entity);
366 return visibility == ir_visibility_external;
369 static void emit_entity_label(const ir_entity *entity)
371 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
372 get_entity_nr(entity));
373 be_emit_write_line();
376 static void register_dbginfo_attributes(void)
378 register_attribute(DW_AT_decl_file, DW_FORM_udata);
379 register_attribute(DW_AT_decl_line, DW_FORM_udata);
380 register_attribute(DW_AT_decl_column, DW_FORM_udata);
383 static void emit_dbginfo(const dbg_info *dbgi)
385 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
386 unsigned const file = loc.file ? insert_file(loc.file) : 0;
388 emit_uleb128(loc.line);
389 emit_uleb128(loc.column);
392 static void emit_type_address(const ir_type *type)
394 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
395 be_gas_get_private_prefix(),
396 get_type_nr(type), be_gas_get_private_prefix());
397 be_emit_write_line();
400 static void emit_subprogram_abbrev(void)
402 begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
403 register_attribute(DW_AT_name, DW_FORM_string);
404 register_dbginfo_attributes();
405 register_attribute(DW_AT_type, DW_FORM_ref4);
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_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
415 register_attribute(DW_AT_name, DW_FORM_string);
416 register_dbginfo_attributes();
417 register_attribute(DW_AT_external, DW_FORM_flag);
418 register_attribute(DW_AT_low_pc, DW_FORM_addr);
419 register_attribute(DW_AT_high_pc, DW_FORM_addr);
420 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
421 if (debug_level >= LEVEL_FRAMEINFO)
422 register_attribute(DW_AT_frame_base, DW_FORM_block1);
425 begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
427 register_attribute(DW_AT_name, DW_FORM_string);
428 register_dbginfo_attributes();
429 register_attribute(DW_AT_type, DW_FORM_ref4);
430 register_attribute(DW_AT_location, DW_FORM_block1);
433 begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
435 register_attribute(DW_AT_name, DW_FORM_string);
436 register_dbginfo_attributes();
437 register_attribute(DW_AT_type, DW_FORM_ref4);
441 static void emit_type(ir_type *type);
443 static void emit_stack_location(long offset)
445 unsigned size = 1 + get_sleb128_size(offset);
447 emit_int8(DW_OP_fbreg);
448 emit_sleb128(offset);
451 static void emit_function_parameters(const ir_entity *entity,
452 const parameter_dbg_info_t *infos)
454 ir_type *type = get_entity_type(entity);
455 size_t n_params = get_method_n_params(type);
456 dbg_info *dbgi = get_entity_dbg_info(entity);
458 for (i = 0; i < n_params; ++i) {
459 ir_type *param_type = get_method_param_type(type, i);
461 if (infos != NULL && infos[i].entity != NULL) {
462 long const offset = get_entity_offset(infos[i].entity);
463 emit_uleb128(abbrev_formal_parameter);
464 emit_string_printf("arg%u", (unsigned)i);
466 emit_type_address(param_type);
467 emit_stack_location(offset);
469 emit_uleb128(abbrev_formal_parameter_no_location);
470 emit_string_printf("arg%u", (unsigned)i);
472 emit_type_address(param_type);
477 void be_dwarf_method_before(const ir_entity *entity,
478 const parameter_dbg_info_t *parameter_infos)
480 if (debug_level < LEVEL_BASIC)
483 ir_type *type = get_entity_type(entity);
484 size_t n_ress = get_method_n_ress(type);
485 size_t n_params = get_method_n_params(type);
488 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
491 ir_type *res = get_method_res_type(type, 0);
494 for (i = 0; i < n_params; ++i) {
495 ir_type *param_type = get_method_param_type(type, i);
496 emit_type(param_type);
499 emit_entity_label(entity);
500 emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
501 be_gas_emit_cstring(get_entity_ld_name(entity));
502 emit_dbginfo(get_entity_dbg_info(entity));
504 ir_type *res = get_method_res_type(type, 0);
505 emit_type_address(res);
507 emit_int8(is_extern_entity(entity));
509 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
510 get_entity_ld_name(entity));
511 /* frame_base prog */
513 emit_int8(DW_OP_call_frame_cfa);
515 emit_function_parameters(entity, parameter_infos);
518 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
520 env.cur_ent = entity;
524 void be_dwarf_method_begin(void)
526 if (debug_level < LEVEL_FRAMEINFO)
528 be_emit_cstring("\t.cfi_startproc\n");
529 be_emit_write_line();
532 void be_dwarf_method_end(void)
534 if (debug_level < LEVEL_BASIC)
536 const ir_entity *entity = env.cur_ent;
537 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
538 get_entity_ld_name(entity));
540 if (debug_level >= LEVEL_FRAMEINFO) {
541 be_emit_cstring("\t.cfi_endproc\n");
542 be_emit_write_line();
546 static void emit_base_type_abbrev(void)
548 begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
549 register_attribute(DW_AT_encoding, DW_FORM_data1);
550 register_attribute(DW_AT_byte_size, DW_FORM_data1);
551 register_attribute(DW_AT_name, DW_FORM_string);
555 static void emit_type_label(const ir_type *type)
557 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
558 be_emit_write_line();
561 static void emit_base_type(const ir_type *type)
564 ir_mode *mode = get_type_mode(type);
565 ir_print_type(buf, sizeof(buf), type);
567 emit_type_label(type);
568 emit_uleb128(abbrev_base_type);
569 if (mode_is_int(mode)) {
571 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
572 emit_int8(DW_ATE_boolean);
574 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
576 } else if (mode_is_reference(mode)) {
577 emit_int8(DW_ATE_address);
578 } else if (mode_is_float(mode)) {
579 emit_int8(DW_ATE_float);
581 panic("mode not implemented yet");
583 emit_int8(get_mode_size_bytes(mode));
584 be_gas_emit_cstring(buf);
587 static void emit_pointer_type_abbrev(void)
589 begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
590 register_attribute(DW_AT_type, DW_FORM_ref4);
591 register_attribute(DW_AT_byte_size, DW_FORM_data1);
594 /* for void* pointer s*/
595 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
596 register_attribute(DW_AT_byte_size, DW_FORM_data1);
600 static void emit_pointer_type(const ir_type *type)
602 ir_type *points_to = get_pointer_points_to_type(type);
603 unsigned size = get_type_size_bytes(type);
606 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
607 emit_type(points_to);
609 emit_type_label(type);
610 emit_uleb128(abbrev_pointer_type);
611 emit_type_address(points_to);
613 emit_type_label(type);
614 emit_uleb128(abbrev_void_pointer_type);
619 static void emit_array_type_abbrev(void)
621 begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
622 register_attribute(DW_AT_type, DW_FORM_ref4);
625 begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
626 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
630 static void emit_array_type(const ir_type *type)
632 ir_type *element_type = get_array_element_type(type);
634 if (get_array_n_dimensions(type) != 1)
635 panic("multidimensional arrays no supported yet");
637 emit_type(element_type);
639 emit_type_label(type);
640 emit_uleb128(abbrev_array_type);
641 emit_type_address(element_type);
643 if (has_array_upper_bound(type, 0)) {
644 int bound = get_array_upper_bound_int(type, 0);
645 emit_uleb128(abbrev_subrange_type);
652 static void emit_compound_type_abbrev(void)
654 begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
655 register_attribute(DW_AT_byte_size, DW_FORM_udata);
656 // TODO register_dbginfo_attributes();
659 begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
660 register_attribute(DW_AT_byte_size, DW_FORM_udata);
661 // TODO register_dbginfo_attributes();
664 begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
665 register_attribute(DW_AT_byte_size, DW_FORM_udata);
666 // TODO register_dbginfo_attributes();
669 begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
670 register_attribute(DW_AT_type, DW_FORM_ref4);
671 register_attribute(DW_AT_name, DW_FORM_string);
672 register_dbginfo_attributes();
673 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
676 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
677 register_attribute(DW_AT_byte_size, DW_FORM_udata);
678 register_attribute(DW_AT_bit_size, DW_FORM_udata);
679 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
680 register_attribute(DW_AT_type, DW_FORM_ref4);
681 register_attribute(DW_AT_name, DW_FORM_string);
682 register_dbginfo_attributes();
683 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
687 static void emit_op_plus_uconst(unsigned value)
689 emit_int8(DW_OP_plus_uconst);
693 static void emit_compound_type(const ir_type *type)
696 size_t n_members = get_compound_n_members(type);
698 for (i = 0; i < n_members; ++i) {
699 ir_entity *member = get_compound_member(type, i);
700 ir_type *member_type = get_entity_type(member);
701 if (is_Primitive_type(member_type)) {
702 ir_type *base = get_primitive_base_type(member_type);
706 emit_type(member_type);
709 emit_type_label(type);
710 if (is_Struct_type(type)) {
711 emit_uleb128(abbrev_structure_type);
712 } else if (is_Union_type(type)) {
713 emit_uleb128(abbrev_union_type);
715 assert(is_Class_type(type));
716 emit_uleb128(abbrev_class_type);
718 emit_uleb128(get_type_size_bytes(type));
719 for (i = 0; i < n_members; ++i) {
720 ir_entity *member = get_compound_member(type, i);
721 ir_type *member_type = get_entity_type(member);
722 int offset = get_entity_offset(member);
725 if (is_Primitive_type(member_type) &&
726 (base = get_primitive_base_type(member_type))) {
727 unsigned bit_offset = get_entity_offset_bits_remainder(member);
728 unsigned base_size = get_type_size_bytes(base);
729 ir_mode *mode = get_type_mode(member_type);
730 unsigned bit_size = get_mode_size_bits(mode);
732 bit_offset = base_size*8 - bit_offset - bit_size;
734 emit_uleb128(abbrev_bitfield_member);
735 emit_uleb128(base_size);
736 emit_uleb128(bit_size);
737 emit_uleb128(bit_offset);
740 emit_uleb128(abbrev_member);
743 emit_type_address(member_type);
744 be_gas_emit_cstring(get_entity_name(member));
745 emit_dbginfo(get_entity_dbg_info(member));
747 emit_int8(1 + get_uleb128_size(offset));
748 emit_op_plus_uconst(offset);
754 static void emit_subroutine_type_abbrev(void)
756 begin_abbrev(abbrev_subroutine_type,
757 DW_TAG_subroutine_type, DW_CHILDREN_yes);
758 register_attribute(DW_AT_prototyped, DW_FORM_flag);
759 register_attribute(DW_AT_type, DW_FORM_ref4);
762 begin_abbrev(abbrev_void_subroutine_type,
763 DW_TAG_subroutine_type, DW_CHILDREN_yes);
764 register_attribute(DW_AT_prototyped, DW_FORM_flag);
767 begin_abbrev(abbrev_unnamed_formal_parameter,
768 DW_TAG_formal_parameter, DW_CHILDREN_no);
769 register_attribute(DW_AT_type, DW_FORM_ref4);
773 static void emit_subroutine_type(const ir_type *type)
775 size_t n_params = get_method_n_params(type);
776 size_t n_ress = get_method_n_ress(type);
778 for (i = 0; i < n_params; ++i) {
779 ir_type *param_type = get_method_param_type(type, i);
780 emit_type(param_type);
782 for (i = 0; i < n_ress; ++i) {
783 ir_type *res_type = get_method_res_type(type, i);
787 emit_type_label(type);
788 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
789 emit_int8(1); /* prototyped */
791 /* dwarf only supports 1 return type */
792 ir_type *res_type = get_method_res_type(type, 0);
793 emit_type_address(res_type);
796 for (i = 0; i < n_params; ++i) {
797 ir_type *param_type = get_method_param_type(type, i);
798 emit_uleb128(abbrev_unnamed_formal_parameter);
799 emit_type_address(param_type);
804 static void emit_type(ir_type *type)
806 if (!pset_new_insert(&env.emitted_types, type))
809 switch (get_type_tpop_code(type)) {
810 case tpo_primitive: emit_base_type(type); break;
811 case tpo_pointer: emit_pointer_type(type); break;
812 case tpo_array: emit_array_type(type); break;
815 case tpo_union: emit_compound_type(type); break;
816 case tpo_method: emit_subroutine_type(type); break;
818 panic("type %+F not implemented yet", type);
822 static void emit_op_addr(const ir_entity *entity)
824 emit_int8(DW_OP_addr);
825 be_emit_cstring("\t.long ");
826 be_gas_emit_entity(entity);
828 be_emit_write_line();
831 static void emit_variable_abbrev(void)
833 begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
834 register_attribute(DW_AT_name, DW_FORM_string);
835 register_attribute(DW_AT_type, DW_FORM_ref4);
836 register_attribute(DW_AT_external, DW_FORM_flag);
837 register_dbginfo_attributes();
838 register_attribute(DW_AT_location, DW_FORM_block1);
842 void be_dwarf_variable(const ir_entity *entity)
844 ir_type *type = get_entity_type(entity);
846 if (debug_level < LEVEL_BASIC)
848 if (get_entity_ld_name(entity)[0] == '\0')
850 if (!entity_has_definition(entity))
853 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
857 emit_entity_label(entity);
858 emit_uleb128(abbrev_variable);
859 be_gas_emit_cstring(get_entity_ld_name(entity));
860 emit_type_address(type);
861 emit_int8(is_extern_entity(entity));
862 emit_dbginfo(get_entity_dbg_info(entity));
864 emit_int8(5); /* block length */
865 emit_op_addr(entity);
867 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
870 static void emit_compile_unit_abbrev(void)
872 begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
873 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
874 register_attribute(DW_AT_producer, DW_FORM_string);
875 register_attribute(DW_AT_name, DW_FORM_string);
877 register_attribute(DW_AT_language, DW_FORM_data2);
878 if (comp_dir != NULL)
879 register_attribute(DW_AT_comp_dir, DW_FORM_string);
883 static void emit_abbrev(void)
885 /* create abbreviation for compile_unit */
886 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
888 emit_label("abbrev_begin");
890 emit_compile_unit_abbrev();
891 emit_variable_abbrev();
892 emit_subprogram_abbrev();
893 emit_base_type_abbrev();
894 emit_pointer_type_abbrev();
895 emit_array_type_abbrev();
896 emit_compound_type_abbrev();
897 emit_subroutine_type_abbrev();
901 void be_dwarf_unit_begin(const char *filename)
903 if (debug_level < LEVEL_BASIC)
907 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
908 emit_label("info_section_begin");
909 emit_label("info_begin");
911 const backend_params *be_params = be_get_backend_param();
913 /* length of compilation unit info */
914 emit_size("compile_unit_begin", "compile_unit_end");
915 emit_label("compile_unit_begin");
916 emit_int16(3); /* dwarf version */
917 emit_address("abbrev_begin");
918 emit_int8(be_params->machine_size / 8); /* pointer size */
920 /* compile_unit die */
921 emit_uleb128(abbrev_compile_unit);
922 emit_address("line_section_begin");
923 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
924 ir_get_version_minor(), ir_get_version_revision());
925 be_gas_emit_cstring(filename);
927 emit_int16(language);
928 if (comp_dir != NULL)
929 be_gas_emit_cstring(comp_dir);
931 /* tell gas to emit cfi in debug_frame
932 * TODO: if we produce exception handling code then this should be
933 * .eh_frame (I also wonder if bad things happen if simply always
935 be_emit_cstring("\t.cfi_sections .debug_frame\n");
936 be_emit_write_line();
939 void be_dwarf_unit_end(void)
941 if (debug_level < LEVEL_BASIC)
943 be_gas_emit_switch_section(GAS_SECTION_TEXT);
944 emit_label("section_end");
946 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
947 emit_uleb128(0); /* end of compile_unit DIE */
949 emit_label("compile_unit_end");
955 void be_dwarf_close(void)
957 if (debug_level < LEVEL_BASIC)
959 pmap_destroy(env.file_map);
960 DEL_ARR_F(env.file_list);
961 DEL_ARR_F(env.pubnames_list);
962 pset_new_destroy(&env.emitted_types);
965 /* Opens a dwarf handler */
966 void be_dwarf_open(void)
968 if (debug_level < LEVEL_BASIC)
970 env.file_map = pmap_create();
971 env.file_list = NEW_ARR_F(const char*, 0);
972 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
973 pset_new_init(&env.emitted_types);
976 void be_dwarf_set_source_language(dwarf_source_language new_language)
978 language = new_language;
981 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
984 comp_dir = xstrdup(new_comp_dir);
987 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
988 void be_init_dwarf(void)
990 static const lc_opt_enum_int_items_t level_items[] = {
991 { "none", LEVEL_NONE },
992 { "basic", LEVEL_BASIC },
993 { "locations", LEVEL_LOCATIONS },
994 { "frameinfo", LEVEL_FRAMEINFO },
997 static lc_opt_enum_int_var_t debug_level_opt = {
998 &debug_level, level_items
1000 static lc_opt_table_entry_t be_main_options[] = {
1001 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
1005 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1006 lc_opt_add_table(be_grp, be_main_options);