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_pointer_type = 100,
67 abbrev_unnamed_formal_parameter,
68 abbrev_formal_parameter_no_location,
69 abbrev_void_subroutine_type,
70 abbrev_void_subprogram,
71 abbrev_bitfield_member,
77 typedef struct dwarf_t {
78 const ir_entity *cur_ent; /**< current method entity */
79 unsigned next_type_nr; /**< next type number */
80 pmap *file_map; /**< a map from file names to number in file list */
81 const char **file_list;
82 const ir_entity **pubnames_list;
83 pset_new_t emitted_types;
84 const char *main_file; /**< name of the main source file */
85 const char *curr_file; /**< name of the current source file */
92 static dwarf_source_language language;
93 static const char *comp_dir;
95 static unsigned insert_file(const char *filename)
98 void *entry = pmap_get(env.file_map, filename);
100 return PTR_TO_INT(entry);
102 ARR_APP1(const char*, env.file_list, filename);
103 num = (unsigned)ARR_LEN(env.file_list);
104 pmap_insert(env.file_map, filename, INT_TO_PTR(num));
106 /* TODO: quote chars in string */
107 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
111 static void emit_int32(uint32_t value)
113 be_emit_irprintf("\t.long %u\n", value);
114 be_emit_write_line();
117 static void emit_int16(uint16_t value)
119 be_emit_irprintf("\t.short %u\n", value);
120 be_emit_write_line();
123 static void emit_int8(uint8_t value)
125 be_emit_irprintf("\t.byte %u\n", value);
126 be_emit_write_line();
129 static void emit_uleb128(unsigned value)
131 be_emit_irprintf("\t.uleb128 0x%x\n", value);
132 be_emit_write_line();
135 static unsigned get_uleb128_size(unsigned value)
141 } while (value != 0);
145 static void emit_sleb128(long value)
147 be_emit_irprintf("\t.sleb128 %ld\n", value);
148 be_emit_write_line();
151 static unsigned get_sleb128_size(long value)
157 } while (value != 0 && value != -1);
161 static void emit_string(const char *string)
163 /* TODO: quote special chars */
164 be_emit_irprintf("\t.asciz \"%s\"\n", string);
165 be_emit_write_line();
168 static void emit_ref(const ir_entity *entity)
170 be_emit_cstring("\t.long ");
171 be_gas_emit_entity(entity);
173 be_emit_write_line();
176 static void emit_string_printf(const char *fmt, ...)
180 be_emit_cstring("\t.asciz \"");
181 be_emit_irvprintf(fmt, ap);
182 be_emit_cstring("\"\n");
185 be_emit_write_line();
188 static void emit_address(const char *name)
190 be_emit_cstring("\t.long ");
191 be_emit_string(be_gas_get_private_prefix());
192 be_emit_string(name);
194 be_emit_write_line();
197 static void emit_size(const char *from_label, const char *to_label)
199 be_emit_cstring("\t.long ");
200 be_emit_string(be_gas_get_private_prefix());
201 be_emit_string(to_label);
202 be_emit_cstring(" - ");
203 be_emit_string(be_gas_get_private_prefix());
204 be_emit_string(from_label);
206 be_emit_write_line();
209 static void emit_label(const char *name)
211 be_emit_string(be_gas_get_private_prefix());
212 be_emit_string(name);
213 be_emit_cstring(":\n");
214 be_emit_write_line();
217 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
219 emit_uleb128(attribute);
223 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
230 static void end_abbrev(void)
236 static void emit_line_info(void)
238 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
240 emit_label("line_section_begin");
241 /* on elf systems gas handles producing the line info for us, and we
242 * don't have to do anything */
243 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
245 emit_size("line_info_begin", "line_info_end");
247 emit_label("line_info_begin");
248 emit_int16(2); /* version */
249 emit_size("line_info_prolog_begin", "line_info_prolog_end");
250 emit_label("line_info_prolog_begin");
251 emit_int8(1); /* len of smallest instruction TODO: query from backend */
252 emit_int8(1); /* default is statement */
253 emit_int8(246); /* line base */
254 emit_int8(245); /* line range */
255 emit_int8(10); /* opcode base */
267 /* include directory list */
268 emit_string("/foo/bar");
272 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
273 emit_string(env.file_list[i]);
274 emit_uleb128(1); /* directory */
275 emit_uleb128(0); /* modification time */
276 emit_uleb128(0); /* file length */
280 emit_label("line_info_prolog_end");
282 /* TODO: put the line_info program here */
284 emit_label("line_info_end");
288 static void emit_pubnames(void)
292 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
294 emit_size("pubnames_begin", "pubnames_end");
295 emit_label("pubnames_begin");
297 emit_int16(2); /* version */
298 emit_size("info_section_begin", "info_begin");
299 emit_size("compile_unit_begin", "compile_unit_end");
301 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
302 const ir_entity *entity = env.pubnames_list[i];
303 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
304 be_gas_get_private_prefix(),
305 get_entity_nr(entity), be_gas_get_private_prefix());
306 emit_string(get_entity_name(entity));
310 emit_label("pubnames_end");
313 void be_dwarf_location(dbg_info *dbgi)
318 if (debug_level < LEVEL_LOCATIONS)
320 loc = ir_retrieve_dbg_info(dbgi);
324 filenum = insert_file(loc.file);
325 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
326 be_emit_write_line();
329 void be_dwarf_callframe_register(const arch_register_t *reg)
331 if (debug_level < LEVEL_FRAMEINFO)
333 be_emit_cstring("\t.cfi_def_cfa_register ");
334 be_emit_irprintf("%d\n", reg->dwarf_number);
335 be_emit_write_line();
338 void be_dwarf_callframe_offset(int offset)
340 if (debug_level < LEVEL_FRAMEINFO)
342 be_emit_cstring("\t.cfi_def_cfa_offset ");
343 be_emit_irprintf("%d\n", offset);
344 be_emit_write_line();
347 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
349 if (debug_level < LEVEL_FRAMEINFO)
351 be_emit_cstring("\t.cfi_offset ");
352 be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
353 be_emit_write_line();
356 static bool is_extern_entity(const ir_entity *entity)
358 ir_visited_t visibility = get_entity_visibility(entity);
359 return visibility == ir_visibility_external;
362 static void emit_entity_label(const ir_entity *entity)
364 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
365 get_entity_nr(entity));
366 be_emit_write_line();
369 static void register_dbginfo_attributes(void)
371 register_attribute(DW_AT_decl_file, DW_FORM_udata);
372 register_attribute(DW_AT_decl_line, DW_FORM_udata);
373 register_attribute(DW_AT_decl_column, DW_FORM_udata);
376 static void emit_dbginfo(const dbg_info *dbgi)
378 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
379 unsigned const file = loc.file ? insert_file(loc.file) : 0;
381 emit_uleb128(loc.line);
382 emit_uleb128(loc.column);
385 static void emit_type_address(const ir_type *type)
387 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
388 be_gas_get_private_prefix(),
389 get_type_nr(type), be_gas_get_private_prefix());
390 be_emit_write_line();
393 static void emit_subprogram_abbrev(void)
395 begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
396 register_attribute(DW_AT_name, DW_FORM_string);
397 register_dbginfo_attributes();
398 register_attribute(DW_AT_type, DW_FORM_ref4);
399 register_attribute(DW_AT_external, DW_FORM_flag);
400 register_attribute(DW_AT_low_pc, DW_FORM_addr);
401 register_attribute(DW_AT_high_pc, DW_FORM_addr);
402 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
403 if (debug_level >= LEVEL_FRAMEINFO)
404 register_attribute(DW_AT_frame_base, DW_FORM_block1);
407 begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
408 register_attribute(DW_AT_name, DW_FORM_string);
409 register_dbginfo_attributes();
410 register_attribute(DW_AT_external, DW_FORM_flag);
411 register_attribute(DW_AT_low_pc, DW_FORM_addr);
412 register_attribute(DW_AT_high_pc, DW_FORM_addr);
413 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
414 if (debug_level >= LEVEL_FRAMEINFO)
415 register_attribute(DW_AT_frame_base, DW_FORM_block1);
418 begin_abbrev(DW_TAG_formal_parameter, DW_TAG_formal_parameter,
420 register_attribute(DW_AT_name, DW_FORM_string);
421 register_dbginfo_attributes();
422 register_attribute(DW_AT_type, DW_FORM_ref4);
423 register_attribute(DW_AT_location, DW_FORM_block1);
426 begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
428 register_attribute(DW_AT_name, DW_FORM_string);
429 register_dbginfo_attributes();
430 register_attribute(DW_AT_type, DW_FORM_ref4);
434 static void emit_type(ir_type *type);
436 static void emit_stack_location(long offset)
438 unsigned size = 1 + get_sleb128_size(offset);
440 emit_int8(DW_OP_fbreg);
441 emit_sleb128(offset);
444 static void emit_function_parameters(const ir_entity *entity,
445 const parameter_dbg_info_t *infos)
447 ir_type *type = get_entity_type(entity);
448 size_t n_params = get_method_n_params(type);
449 dbg_info *dbgi = get_entity_dbg_info(entity);
451 for (i = 0; i < n_params; ++i) {
452 ir_type *param_type = get_method_param_type(type, i);
454 if (infos != NULL && infos[i].entity != NULL) {
455 const ir_entity *entity = infos[i].entity;
456 long offset = get_entity_offset(entity);
457 emit_uleb128(DW_TAG_formal_parameter);
458 emit_string_printf("arg%u", (unsigned)i);
460 emit_type_address(param_type);
461 emit_stack_location(offset);
463 emit_uleb128(abbrev_formal_parameter_no_location);
464 emit_string_printf("arg%u", (unsigned)i);
466 emit_type_address(param_type);
471 void be_dwarf_method_before(const ir_entity *entity,
472 const parameter_dbg_info_t *parameter_infos)
474 if (debug_level < LEVEL_BASIC)
477 ir_type *type = get_entity_type(entity);
478 size_t n_ress = get_method_n_ress(type);
479 size_t n_params = get_method_n_params(type);
482 (void)parameter_infos;
484 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
487 ir_type *res = get_method_res_type(type, 0);
490 for (i = 0; i < n_params; ++i) {
491 ir_type *param_type = get_method_param_type(type, i);
492 emit_type(param_type);
495 emit_entity_label(entity);
496 emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : DW_TAG_subprogram);
497 emit_string(get_entity_ld_name(entity));
498 emit_dbginfo(get_entity_dbg_info(entity));
500 ir_type *res = get_method_res_type(type, 0);
501 emit_type_address(res);
503 emit_int8(is_extern_entity(entity));
505 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
506 get_entity_ld_name(entity));
507 /* frame_base prog */
509 emit_int8(DW_OP_call_frame_cfa);
511 emit_function_parameters(entity, parameter_infos);
514 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
516 env.cur_ent = entity;
520 void be_dwarf_method_begin(void)
522 if (debug_level < LEVEL_FRAMEINFO)
524 be_emit_cstring("\t.cfi_startproc\n");
525 be_emit_write_line();
528 void be_dwarf_method_end(void)
530 if (debug_level < LEVEL_BASIC)
532 const ir_entity *entity = env.cur_ent;
533 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
534 get_entity_ld_name(entity));
536 if (debug_level >= LEVEL_FRAMEINFO) {
537 be_emit_cstring("\t.cfi_endproc\n");
538 be_emit_write_line();
542 static void emit_base_type_abbrev(void)
544 begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
545 register_attribute(DW_AT_encoding, DW_FORM_data1);
546 register_attribute(DW_AT_byte_size, DW_FORM_data1);
547 register_attribute(DW_AT_name, DW_FORM_string);
551 static void emit_type_label(const ir_type *type)
553 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
554 be_emit_write_line();
557 static void emit_base_type(const ir_type *type)
560 ir_mode *mode = get_type_mode(type);
561 ir_print_type(buf, sizeof(buf), type);
563 emit_type_label(type);
564 emit_uleb128(DW_TAG_base_type);
565 if (mode_is_int(mode)) {
567 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
568 emit_int8(DW_ATE_boolean);
570 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
572 } else if (mode_is_reference(mode)) {
573 emit_int8(DW_ATE_address);
574 } else if (mode_is_float(mode)) {
575 emit_int8(DW_ATE_float);
577 panic("mode not implemented yet");
579 emit_int8(get_mode_size_bytes(mode));
583 static void emit_pointer_type_abbrev(void)
585 begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
586 register_attribute(DW_AT_type, DW_FORM_ref4);
587 register_attribute(DW_AT_byte_size, DW_FORM_data1);
590 /* for void* pointer s*/
591 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
592 register_attribute(DW_AT_byte_size, DW_FORM_data1);
596 static void emit_pointer_type(const ir_type *type)
598 ir_type *points_to = get_pointer_points_to_type(type);
599 unsigned size = get_type_size_bytes(type);
602 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
603 emit_type(points_to);
605 emit_type_label(type);
606 emit_uleb128(DW_TAG_pointer_type);
607 emit_type_address(points_to);
609 emit_type_label(type);
610 emit_uleb128(abbrev_void_pointer_type);
615 static void emit_array_type_abbrev(void)
617 begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
618 register_attribute(DW_AT_type, DW_FORM_ref4);
621 begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
622 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
626 static void emit_array_type(const ir_type *type)
628 ir_type *element_type = get_array_element_type(type);
630 if (get_array_n_dimensions(type) != 1)
631 panic("dwarf: multidimensional arrays no supported yet");
633 emit_type(element_type);
635 emit_type_label(type);
636 emit_uleb128(DW_TAG_array_type);
637 emit_type_address(element_type);
639 if (has_array_upper_bound(type, 0)) {
640 int bound = get_array_upper_bound_int(type, 0);
641 emit_uleb128(DW_TAG_subrange_type);
648 static void emit_compound_type_abbrev(void)
650 begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
651 register_attribute(DW_AT_byte_size, DW_FORM_udata);
652 // TODO register_dbginfo_attributes();
655 begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
656 register_attribute(DW_AT_byte_size, DW_FORM_udata);
657 // TODO register_dbginfo_attributes();
660 begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
661 register_attribute(DW_AT_byte_size, DW_FORM_udata);
662 // TODO register_dbginfo_attributes();
665 begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
666 register_attribute(DW_AT_type, DW_FORM_ref4);
667 register_attribute(DW_AT_name, DW_FORM_string);
668 register_dbginfo_attributes();
669 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
672 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
673 register_attribute(DW_AT_byte_size, DW_FORM_udata);
674 register_attribute(DW_AT_bit_size, DW_FORM_udata);
675 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
676 register_attribute(DW_AT_type, DW_FORM_ref4);
677 register_attribute(DW_AT_name, DW_FORM_string);
678 register_dbginfo_attributes();
679 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
683 static void emit_op_plus_uconst(unsigned value)
685 emit_int8(DW_OP_plus_uconst);
689 static void emit_compound_type(const ir_type *type)
692 size_t n_members = get_compound_n_members(type);
694 for (i = 0; i < n_members; ++i) {
695 ir_entity *member = get_compound_member(type, i);
696 ir_type *member_type = get_entity_type(member);
697 if (is_Primitive_type(member_type)) {
698 ir_type *base = get_primitive_base_type(member_type);
702 emit_type(member_type);
705 emit_type_label(type);
706 if (is_Struct_type(type)) {
707 emit_uleb128(DW_TAG_structure_type);
708 } else if (is_Union_type(type)) {
709 emit_uleb128(DW_TAG_union_type);
711 assert(is_Class_type(type));
712 emit_uleb128(DW_TAG_class_type);
714 emit_uleb128(get_type_size_bytes(type));
715 for (i = 0; i < n_members; ++i) {
716 ir_entity *member = get_compound_member(type, i);
717 ir_type *member_type = get_entity_type(member);
718 int offset = get_entity_offset(member);
721 if (is_Primitive_type(member_type) &&
722 (base = get_primitive_base_type(member_type))) {
723 unsigned bit_offset = get_entity_offset_bits_remainder(member);
724 unsigned base_size = get_type_size_bytes(base);
725 ir_mode *mode = get_type_mode(member_type);
726 unsigned bit_size = get_mode_size_bits(mode);
728 bit_offset = base_size*8 - bit_offset - bit_size;
730 emit_uleb128(abbrev_bitfield_member);
731 emit_uleb128(base_size);
732 emit_uleb128(bit_size);
733 emit_uleb128(bit_offset);
736 emit_uleb128(DW_TAG_member);
739 emit_type_address(member_type);
740 emit_string(get_entity_name(member));
741 emit_dbginfo(get_entity_dbg_info(member));
743 emit_int8(1 + get_uleb128_size(offset));
744 emit_op_plus_uconst(offset);
750 static void emit_subroutine_type_abbrev(void)
752 begin_abbrev(DW_TAG_subroutine_type,
753 DW_TAG_subroutine_type, DW_CHILDREN_yes);
754 register_attribute(DW_AT_prototyped, DW_FORM_flag);
755 register_attribute(DW_AT_type, DW_FORM_ref4);
758 begin_abbrev(abbrev_void_subroutine_type,
759 DW_TAG_subroutine_type, DW_CHILDREN_yes);
760 register_attribute(DW_AT_prototyped, DW_FORM_flag);
763 begin_abbrev(abbrev_unnamed_formal_parameter,
764 DW_TAG_formal_parameter, DW_CHILDREN_no);
765 register_attribute(DW_AT_type, DW_FORM_ref4);
769 static void emit_subroutine_type(const ir_type *type)
771 size_t n_params = get_method_n_params(type);
772 size_t n_ress = get_method_n_ress(type);
774 for (i = 0; i < n_params; ++i) {
775 ir_type *param_type = get_method_param_type(type, i);
776 emit_type(param_type);
778 for (i = 0; i < n_ress; ++i) {
779 ir_type *res_type = get_method_res_type(type, i);
783 emit_type_label(type);
784 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
785 emit_int8(1); /* prototyped */
787 /* dwarf only supports 1 return type */
788 ir_type *res_type = get_method_res_type(type, 0);
789 emit_type_address(res_type);
792 for (i = 0; i < n_params; ++i) {
793 ir_type *param_type = get_method_param_type(type, i);
794 emit_uleb128(abbrev_unnamed_formal_parameter);
795 emit_type_address(param_type);
800 static void emit_type(ir_type *type)
802 if (pset_new_insert(&env.emitted_types, type))
805 switch (get_type_tpop_code(type)) {
806 case tpo_primitive: emit_base_type(type); break;
807 case tpo_pointer: emit_pointer_type(type); break;
808 case tpo_array: emit_array_type(type); break;
811 case tpo_union: emit_compound_type(type); break;
812 case tpo_method: emit_subroutine_type(type); break;
814 panic("bedwarf: type %+F not implemented yet", type);
818 static void emit_op_addr(const ir_entity *entity)
820 emit_int8(DW_OP_addr);
821 be_emit_cstring("\t.long ");
822 be_gas_emit_entity(entity);
824 be_emit_write_line();
827 static void emit_variable_abbrev(void)
829 begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
830 register_attribute(DW_AT_name, DW_FORM_string);
831 register_attribute(DW_AT_type, DW_FORM_ref4);
832 register_attribute(DW_AT_external, DW_FORM_flag);
833 register_dbginfo_attributes();
834 register_attribute(DW_AT_location, DW_FORM_block1);
838 void be_dwarf_variable(const ir_entity *entity)
840 ir_type *type = get_entity_type(entity);
842 if (debug_level < LEVEL_BASIC)
844 if (get_entity_ld_name(entity)[0] == '\0')
846 if (!entity_has_definition(entity))
849 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
853 emit_entity_label(entity);
854 emit_uleb128(DW_TAG_variable);
855 emit_string(get_entity_ld_name(entity));
856 emit_type_address(type);
857 emit_int8(is_extern_entity(entity));
858 emit_dbginfo(get_entity_dbg_info(entity));
860 emit_int8(5); /* block length */
861 emit_op_addr(entity);
863 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
866 static void emit_compile_unit_abbrev(void)
868 begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
869 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
870 register_attribute(DW_AT_producer, DW_FORM_string);
871 register_attribute(DW_AT_name, DW_FORM_string);
873 register_attribute(DW_AT_language, DW_FORM_data2);
874 if (comp_dir != NULL)
875 register_attribute(DW_AT_comp_dir, DW_FORM_string);
879 static void emit_abbrev(void)
881 /* create abbreviation for compile_unit */
882 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
884 emit_label("abbrev_begin");
886 emit_compile_unit_abbrev();
887 emit_variable_abbrev();
888 emit_subprogram_abbrev();
889 emit_base_type_abbrev();
890 emit_pointer_type_abbrev();
891 emit_array_type_abbrev();
892 emit_compound_type_abbrev();
893 emit_subroutine_type_abbrev();
897 void be_dwarf_unit_begin(const char *filename)
899 if (debug_level < LEVEL_BASIC)
903 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
904 emit_label("info_section_begin");
905 emit_label("info_begin");
907 /* length of compilation unit info */
908 emit_size("compile_unit_begin", "compile_unit_end");
909 emit_label("compile_unit_begin");
910 emit_int16(3); /* dwarf version */
911 emit_address("abbrev_begin");
912 emit_int8(4); /* pointer size, TODO: query backend */
914 /* compile_unit die */
915 emit_uleb128(DW_TAG_compile_unit);
916 emit_address("line_section_begin");
917 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
918 ir_get_version_minor(),
919 ir_get_version_revision());
920 emit_string(filename);
922 emit_int16(language);
923 if (comp_dir != NULL)
924 emit_string(comp_dir);
926 /* tell gas to emit cfi in debug_frame
927 * TODO: if we produce exception handling code then this should be
928 * .eh_frame (I also wonder if bad things happen if simply always
930 be_emit_cstring("\t.cfi_sections .debug_frame\n");
931 be_emit_write_line();
934 void be_dwarf_unit_end(void)
936 if (debug_level < LEVEL_BASIC)
938 be_gas_emit_switch_section(GAS_SECTION_TEXT);
939 emit_label("section_end");
941 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
942 emit_uleb128(0); /* end of compile_unit DIE */
944 emit_label("compile_unit_end");
950 void be_dwarf_close(void)
952 if (debug_level < LEVEL_BASIC)
954 pmap_destroy(env.file_map);
955 DEL_ARR_F(env.file_list);
956 DEL_ARR_F(env.pubnames_list);
957 pset_new_destroy(&env.emitted_types);
960 /* Opens a dwarf handler */
961 void be_dwarf_open(void)
963 if (debug_level < LEVEL_BASIC)
965 env.file_map = pmap_create();
966 env.file_list = NEW_ARR_F(const char*, 0);
967 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
968 pset_new_init(&env.emitted_types);
971 void be_dwarf_set_source_language(dwarf_source_language new_language)
973 language = new_language;
976 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
978 comp_dir = new_comp_dir;
981 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
982 void be_init_dwarf(void)
984 static const lc_opt_enum_int_items_t level_items[] = {
985 { "none", LEVEL_NONE },
986 { "basic", LEVEL_BASIC },
987 { "locations", LEVEL_LOCATIONS },
988 { "frameinfo", LEVEL_FRAMEINFO },
991 static lc_opt_enum_int_var_t debug_level_opt = {
992 &debug_level, level_items
994 static lc_opt_table_entry_t be_main_options[] = {
995 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
999 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1000 lc_opt_add_table(be_grp, be_main_options);