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"
46 #include "lc_opts_enum.h"
49 #include "beemitter.h"
60 static int debug_level = LEVEL_NONE;
63 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
64 * the cases where we need multiple ids with the same DW_TAG we define new IDs
67 typedef enum custom_abbrevs {
68 abbrev_void_subprogram = 1,
70 abbrev_formal_parameter,
71 abbrev_unnamed_formal_parameter,
72 abbrev_formal_parameter_no_location,
77 abbrev_void_pointer_type,
80 abbrev_structure_type,
84 abbrev_bitfield_member,
85 abbrev_subroutine_type,
86 abbrev_void_subroutine_type,
92 typedef struct dwarf_t {
93 const ir_entity *cur_ent; /**< current method entity */
94 unsigned next_type_nr; /**< next type number */
95 pmap *file_map; /**< a map from file names to number in file list */
96 const char **file_list;
97 const ir_entity **pubnames_list;
98 pset_new_t emitted_types;
99 const char *main_file; /**< name of the main source file */
100 const char *curr_file; /**< name of the current source file */
107 static dwarf_source_language language;
108 static char *comp_dir;
110 static unsigned insert_file(const char *filename)
113 void *entry = pmap_get(void, env.file_map, filename);
115 return PTR_TO_INT(entry);
117 ARR_APP1(const char*, env.file_list, filename);
118 num = (unsigned)ARR_LEN(env.file_list);
119 pmap_insert(env.file_map, filename, INT_TO_PTR(num));
121 /* TODO: quote chars in string */
122 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
126 static void emit_int32(uint32_t value)
128 be_emit_irprintf("\t.long %u\n", value);
129 be_emit_write_line();
132 static void emit_int16(uint16_t value)
134 be_emit_irprintf("\t.short %u\n", value);
135 be_emit_write_line();
138 static void emit_int8(uint8_t value)
140 be_emit_irprintf("\t.byte %u\n", value);
141 be_emit_write_line();
144 static void emit_uleb128(unsigned value)
146 be_emit_irprintf("\t.uleb128 0x%x\n", value);
147 be_emit_write_line();
150 static unsigned get_uleb128_size(unsigned value)
156 } while (value != 0);
160 static void emit_sleb128(long value)
162 be_emit_irprintf("\t.sleb128 %ld\n", value);
163 be_emit_write_line();
166 static unsigned get_sleb128_size(long value)
172 } while (value != 0 && value != -1);
176 static void emit_ref(const ir_entity *entity)
178 be_emit_cstring("\t.long ");
179 be_gas_emit_entity(entity);
181 be_emit_write_line();
184 static void emit_string_printf(const char *fmt, ...)
188 be_emit_cstring("\t.asciz \"");
189 be_emit_irvprintf(fmt, ap);
190 be_emit_cstring("\"\n");
193 be_emit_write_line();
196 static void emit_address(const char *name)
198 be_emit_cstring("\t.long ");
199 be_emit_string(be_gas_get_private_prefix());
200 be_emit_string(name);
202 be_emit_write_line();
205 static void emit_size(const char *from_label, const char *to_label)
207 be_emit_cstring("\t.long ");
208 be_emit_string(be_gas_get_private_prefix());
209 be_emit_string(to_label);
210 be_emit_cstring(" - ");
211 be_emit_string(be_gas_get_private_prefix());
212 be_emit_string(from_label);
214 be_emit_write_line();
217 static void emit_label(const char *name)
219 be_emit_string(be_gas_get_private_prefix());
220 be_emit_string(name);
221 be_emit_cstring(":\n");
222 be_emit_write_line();
225 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
227 emit_uleb128(attribute);
231 static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
232 dw_children children)
239 static void end_abbrev(void)
245 static void emit_line_info(void)
247 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
249 emit_label("line_section_begin");
250 /* on elf systems gas handles producing the line info for us, and we
251 * don't have to do anything */
252 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
254 emit_size("line_info_begin", "line_info_end");
256 emit_label("line_info_begin");
257 emit_int16(2); /* version */
258 emit_size("line_info_prolog_begin", "line_info_prolog_end");
259 emit_label("line_info_prolog_begin");
260 emit_int8(1); /* len of smallest instruction TODO: query from backend */
261 emit_int8(1); /* default is statement */
262 emit_int8(246); /* line base */
263 emit_int8(245); /* line range */
264 emit_int8(10); /* opcode base */
276 /* include directory list */
277 be_gas_emit_cstring("/foo/bar");
281 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
282 be_gas_emit_cstring(env.file_list[i]);
283 emit_uleb128(1); /* directory */
284 emit_uleb128(0); /* modification time */
285 emit_uleb128(0); /* file length */
289 emit_label("line_info_prolog_end");
291 /* TODO: put the line_info program here */
293 emit_label("line_info_end");
297 static void emit_pubnames(void)
301 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
303 emit_size("pubnames_begin", "pubnames_end");
304 emit_label("pubnames_begin");
306 emit_int16(2); /* version */
307 emit_size("info_section_begin", "info_begin");
308 emit_size("compile_unit_begin", "compile_unit_end");
310 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
311 const ir_entity *entity = env.pubnames_list[i];
312 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
313 be_gas_get_private_prefix(),
314 get_entity_nr(entity), be_gas_get_private_prefix());
315 be_gas_emit_cstring(get_entity_name(entity));
319 emit_label("pubnames_end");
322 void be_dwarf_location(dbg_info *dbgi)
327 if (debug_level < LEVEL_LOCATIONS)
329 loc = ir_retrieve_dbg_info(dbgi);
333 filenum = insert_file(loc.file);
334 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
335 be_emit_write_line();
338 void be_dwarf_callframe_register(const arch_register_t *reg)
340 if (debug_level < LEVEL_FRAMEINFO)
342 be_emit_cstring("\t.cfi_def_cfa_register ");
343 be_emit_irprintf("%d\n", reg->dwarf_number);
344 be_emit_write_line();
347 void be_dwarf_callframe_offset(int offset)
349 if (debug_level < LEVEL_FRAMEINFO)
351 be_emit_cstring("\t.cfi_def_cfa_offset ");
352 be_emit_irprintf("%d\n", offset);
353 be_emit_write_line();
356 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
358 if (debug_level < LEVEL_FRAMEINFO)
360 be_emit_cstring("\t.cfi_offset ");
361 be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
362 be_emit_write_line();
365 static bool is_extern_entity(const ir_entity *entity)
367 ir_visited_t visibility = get_entity_visibility(entity);
368 return visibility == ir_visibility_external;
371 static void emit_entity_label(const ir_entity *entity)
373 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
374 get_entity_nr(entity));
375 be_emit_write_line();
378 static void register_dbginfo_attributes(void)
380 register_attribute(DW_AT_decl_file, DW_FORM_udata);
381 register_attribute(DW_AT_decl_line, DW_FORM_udata);
382 register_attribute(DW_AT_decl_column, DW_FORM_udata);
385 static void emit_dbginfo(const dbg_info *dbgi)
387 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
388 unsigned const file = loc.file ? insert_file(loc.file) : 0;
390 emit_uleb128(loc.line);
391 emit_uleb128(loc.column);
394 static void emit_type_address(const ir_type *type)
396 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
397 be_gas_get_private_prefix(),
398 get_type_nr(type), be_gas_get_private_prefix());
399 be_emit_write_line();
402 static void emit_subprogram_abbrev(void)
404 begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
405 register_attribute(DW_AT_name, DW_FORM_string);
406 register_dbginfo_attributes();
407 register_attribute(DW_AT_type, DW_FORM_ref4);
408 register_attribute(DW_AT_external, DW_FORM_flag);
409 register_attribute(DW_AT_low_pc, DW_FORM_addr);
410 register_attribute(DW_AT_high_pc, DW_FORM_addr);
411 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
412 if (debug_level >= LEVEL_FRAMEINFO)
413 register_attribute(DW_AT_frame_base, DW_FORM_block1);
416 begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
417 register_attribute(DW_AT_name, DW_FORM_string);
418 register_dbginfo_attributes();
419 register_attribute(DW_AT_external, DW_FORM_flag);
420 register_attribute(DW_AT_low_pc, DW_FORM_addr);
421 register_attribute(DW_AT_high_pc, DW_FORM_addr);
422 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
423 if (debug_level >= LEVEL_FRAMEINFO)
424 register_attribute(DW_AT_frame_base, DW_FORM_block1);
427 begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
429 register_attribute(DW_AT_name, DW_FORM_string);
430 register_dbginfo_attributes();
431 register_attribute(DW_AT_type, DW_FORM_ref4);
432 register_attribute(DW_AT_location, DW_FORM_block1);
435 begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
437 register_attribute(DW_AT_name, DW_FORM_string);
438 register_dbginfo_attributes();
439 register_attribute(DW_AT_type, DW_FORM_ref4);
443 static void emit_type(ir_type *type);
445 static void emit_stack_location(long offset)
447 unsigned size = 1 + get_sleb128_size(offset);
449 emit_int8(DW_OP_fbreg);
450 emit_sleb128(offset);
453 static void emit_function_parameters(const ir_entity *entity,
454 const parameter_dbg_info_t *infos)
456 ir_type *type = get_entity_type(entity);
457 size_t n_params = get_method_n_params(type);
458 dbg_info *dbgi = get_entity_dbg_info(entity);
460 for (i = 0; i < n_params; ++i) {
461 ir_type *param_type = get_method_param_type(type, i);
463 if (infos != NULL && infos[i].entity != NULL) {
464 long const offset = get_entity_offset(infos[i].entity);
465 emit_uleb128(abbrev_formal_parameter);
466 emit_string_printf("arg%u", (unsigned)i);
468 emit_type_address(param_type);
469 emit_stack_location(offset);
471 emit_uleb128(abbrev_formal_parameter_no_location);
472 emit_string_printf("arg%u", (unsigned)i);
474 emit_type_address(param_type);
479 void be_dwarf_method_before(const ir_entity *entity,
480 const parameter_dbg_info_t *parameter_infos)
482 if (debug_level < LEVEL_BASIC)
485 ir_type *type = get_entity_type(entity);
486 size_t n_ress = get_method_n_ress(type);
487 size_t n_params = get_method_n_params(type);
490 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
493 ir_type *res = get_method_res_type(type, 0);
496 for (i = 0; i < n_params; ++i) {
497 ir_type *param_type = get_method_param_type(type, i);
498 emit_type(param_type);
501 emit_entity_label(entity);
502 emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
503 be_gas_emit_cstring(get_entity_ld_name(entity));
504 emit_dbginfo(get_entity_dbg_info(entity));
506 ir_type *res = get_method_res_type(type, 0);
507 emit_type_address(res);
509 emit_int8(is_extern_entity(entity));
511 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
512 get_entity_ld_name(entity));
513 /* frame_base prog */
515 emit_int8(DW_OP_call_frame_cfa);
517 emit_function_parameters(entity, parameter_infos);
520 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
522 env.cur_ent = entity;
526 void be_dwarf_method_begin(void)
528 if (debug_level < LEVEL_FRAMEINFO)
530 be_emit_cstring("\t.cfi_startproc\n");
531 be_emit_write_line();
534 void be_dwarf_method_end(void)
536 if (debug_level < LEVEL_BASIC)
538 const ir_entity *entity = env.cur_ent;
539 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
540 get_entity_ld_name(entity));
542 if (debug_level >= LEVEL_FRAMEINFO) {
543 be_emit_cstring("\t.cfi_endproc\n");
544 be_emit_write_line();
548 static void emit_base_type_abbrev(void)
550 begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
551 register_attribute(DW_AT_encoding, DW_FORM_data1);
552 register_attribute(DW_AT_byte_size, DW_FORM_data1);
553 register_attribute(DW_AT_name, DW_FORM_string);
557 static void emit_type_label(const ir_type *type)
559 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
560 be_emit_write_line();
563 static void emit_base_type(const ir_type *type)
566 ir_mode *mode = get_type_mode(type);
567 ir_print_type(buf, sizeof(buf), type);
569 emit_type_label(type);
570 emit_uleb128(abbrev_base_type);
571 if (mode_is_int(mode)) {
573 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
574 emit_int8(DW_ATE_boolean);
576 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
578 } else if (mode_is_reference(mode)) {
579 emit_int8(DW_ATE_address);
580 } else if (mode_is_float(mode)) {
581 emit_int8(DW_ATE_float);
583 panic("mode not implemented yet");
585 emit_int8(get_mode_size_bytes(mode));
586 be_gas_emit_cstring(buf);
589 static void emit_pointer_type_abbrev(void)
591 begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
592 register_attribute(DW_AT_type, DW_FORM_ref4);
593 register_attribute(DW_AT_byte_size, DW_FORM_data1);
596 /* for void* pointer s*/
597 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
598 register_attribute(DW_AT_byte_size, DW_FORM_data1);
602 static void emit_pointer_type(const ir_type *type)
604 ir_type *points_to = get_pointer_points_to_type(type);
605 unsigned size = get_type_size_bytes(type);
608 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
609 emit_type(points_to);
611 emit_type_label(type);
612 emit_uleb128(abbrev_pointer_type);
613 emit_type_address(points_to);
615 emit_type_label(type);
616 emit_uleb128(abbrev_void_pointer_type);
621 static void emit_array_type_abbrev(void)
623 begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
624 register_attribute(DW_AT_type, DW_FORM_ref4);
627 begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
628 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
632 static void emit_array_type(const ir_type *type)
634 ir_type *element_type = get_array_element_type(type);
636 if (get_array_n_dimensions(type) != 1)
637 panic("multidimensional arrays no supported yet");
639 emit_type(element_type);
641 emit_type_label(type);
642 emit_uleb128(abbrev_array_type);
643 emit_type_address(element_type);
645 if (has_array_upper_bound(type, 0)) {
646 int bound = get_array_upper_bound_int(type, 0);
647 emit_uleb128(abbrev_subrange_type);
654 static void emit_compound_type_abbrev(void)
656 begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
657 register_attribute(DW_AT_byte_size, DW_FORM_udata);
658 // TODO register_dbginfo_attributes();
661 begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
662 register_attribute(DW_AT_byte_size, DW_FORM_udata);
663 // TODO register_dbginfo_attributes();
666 begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
667 register_attribute(DW_AT_byte_size, DW_FORM_udata);
668 // TODO register_dbginfo_attributes();
671 begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
672 register_attribute(DW_AT_type, DW_FORM_ref4);
673 register_attribute(DW_AT_name, DW_FORM_string);
674 register_dbginfo_attributes();
675 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
678 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
679 register_attribute(DW_AT_byte_size, DW_FORM_udata);
680 register_attribute(DW_AT_bit_size, DW_FORM_udata);
681 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
682 register_attribute(DW_AT_type, DW_FORM_ref4);
683 register_attribute(DW_AT_name, DW_FORM_string);
684 register_dbginfo_attributes();
685 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
689 static void emit_op_plus_uconst(unsigned value)
691 emit_int8(DW_OP_plus_uconst);
695 static void emit_compound_type(const ir_type *type)
698 size_t n_members = get_compound_n_members(type);
700 for (i = 0; i < n_members; ++i) {
701 ir_entity *member = get_compound_member(type, i);
702 ir_type *member_type = get_entity_type(member);
703 if (is_Primitive_type(member_type)) {
704 ir_type *base = get_primitive_base_type(member_type);
708 emit_type(member_type);
711 emit_type_label(type);
712 if (is_Struct_type(type)) {
713 emit_uleb128(abbrev_structure_type);
714 } else if (is_Union_type(type)) {
715 emit_uleb128(abbrev_union_type);
717 assert(is_Class_type(type));
718 emit_uleb128(abbrev_class_type);
720 emit_uleb128(get_type_size_bytes(type));
721 for (i = 0; i < n_members; ++i) {
722 ir_entity *member = get_compound_member(type, i);
723 ir_type *member_type = get_entity_type(member);
724 int offset = get_entity_offset(member);
727 if (is_Primitive_type(member_type) &&
728 (base = get_primitive_base_type(member_type))) {
729 unsigned bit_offset = get_entity_offset_bits_remainder(member);
730 unsigned base_size = get_type_size_bytes(base);
731 ir_mode *mode = get_type_mode(member_type);
732 unsigned bit_size = get_mode_size_bits(mode);
734 bit_offset = base_size*8 - bit_offset - bit_size;
736 emit_uleb128(abbrev_bitfield_member);
737 emit_uleb128(base_size);
738 emit_uleb128(bit_size);
739 emit_uleb128(bit_offset);
742 emit_uleb128(abbrev_member);
745 emit_type_address(member_type);
746 be_gas_emit_cstring(get_entity_name(member));
747 emit_dbginfo(get_entity_dbg_info(member));
749 emit_int8(1 + get_uleb128_size(offset));
750 emit_op_plus_uconst(offset);
756 static void emit_subroutine_type_abbrev(void)
758 begin_abbrev(abbrev_subroutine_type,
759 DW_TAG_subroutine_type, DW_CHILDREN_yes);
760 register_attribute(DW_AT_prototyped, DW_FORM_flag);
761 register_attribute(DW_AT_type, DW_FORM_ref4);
764 begin_abbrev(abbrev_void_subroutine_type,
765 DW_TAG_subroutine_type, DW_CHILDREN_yes);
766 register_attribute(DW_AT_prototyped, DW_FORM_flag);
769 begin_abbrev(abbrev_unnamed_formal_parameter,
770 DW_TAG_formal_parameter, DW_CHILDREN_no);
771 register_attribute(DW_AT_type, DW_FORM_ref4);
775 static void emit_subroutine_type(const ir_type *type)
777 size_t n_params = get_method_n_params(type);
778 size_t n_ress = get_method_n_ress(type);
780 for (i = 0; i < n_params; ++i) {
781 ir_type *param_type = get_method_param_type(type, i);
782 emit_type(param_type);
784 for (i = 0; i < n_ress; ++i) {
785 ir_type *res_type = get_method_res_type(type, i);
789 emit_type_label(type);
790 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
791 emit_int8(1); /* prototyped */
793 /* dwarf only supports 1 return type */
794 ir_type *res_type = get_method_res_type(type, 0);
795 emit_type_address(res_type);
798 for (i = 0; i < n_params; ++i) {
799 ir_type *param_type = get_method_param_type(type, i);
800 emit_uleb128(abbrev_unnamed_formal_parameter);
801 emit_type_address(param_type);
806 static void emit_type(ir_type *type)
808 if (!pset_new_insert(&env.emitted_types, type))
811 switch (get_type_tpop_code(type)) {
812 case tpo_primitive: emit_base_type(type); break;
813 case tpo_pointer: emit_pointer_type(type); break;
814 case tpo_array: emit_array_type(type); break;
817 case tpo_union: emit_compound_type(type); break;
818 case tpo_method: emit_subroutine_type(type); break;
820 panic("type %+F not implemented yet", type);
824 static void emit_op_addr(const ir_entity *entity)
826 emit_int8(DW_OP_addr);
827 be_emit_cstring("\t.long ");
828 be_gas_emit_entity(entity);
830 be_emit_write_line();
833 static void emit_variable_abbrev(void)
835 begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
836 register_attribute(DW_AT_name, DW_FORM_string);
837 register_attribute(DW_AT_type, DW_FORM_ref4);
838 register_attribute(DW_AT_external, DW_FORM_flag);
839 register_dbginfo_attributes();
840 register_attribute(DW_AT_location, DW_FORM_block1);
844 void be_dwarf_variable(const ir_entity *entity)
846 ir_type *type = get_entity_type(entity);
848 if (debug_level < LEVEL_BASIC)
850 if (get_entity_ld_name(entity)[0] == '\0')
852 if (!entity_has_definition(entity))
855 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
859 emit_entity_label(entity);
860 emit_uleb128(abbrev_variable);
861 be_gas_emit_cstring(get_entity_ld_name(entity));
862 emit_type_address(type);
863 emit_int8(is_extern_entity(entity));
864 emit_dbginfo(get_entity_dbg_info(entity));
866 emit_int8(5); /* block length */
867 emit_op_addr(entity);
869 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
872 static void emit_compile_unit_abbrev(void)
874 begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
875 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
876 register_attribute(DW_AT_producer, DW_FORM_string);
877 register_attribute(DW_AT_name, DW_FORM_string);
879 register_attribute(DW_AT_language, DW_FORM_data2);
880 if (comp_dir != NULL)
881 register_attribute(DW_AT_comp_dir, DW_FORM_string);
885 static void emit_abbrev(void)
887 /* create abbreviation for compile_unit */
888 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
890 emit_label("abbrev_begin");
892 emit_compile_unit_abbrev();
893 emit_variable_abbrev();
894 emit_subprogram_abbrev();
895 emit_base_type_abbrev();
896 emit_pointer_type_abbrev();
897 emit_array_type_abbrev();
898 emit_compound_type_abbrev();
899 emit_subroutine_type_abbrev();
903 void be_dwarf_unit_begin(const char *filename)
905 if (debug_level < LEVEL_BASIC)
909 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
910 emit_label("info_section_begin");
911 emit_label("info_begin");
913 const backend_params *be_params = be_get_backend_param();
915 /* length of compilation unit info */
916 emit_size("compile_unit_begin", "compile_unit_end");
917 emit_label("compile_unit_begin");
918 emit_int16(3); /* dwarf version */
919 emit_address("abbrev_begin");
920 emit_int8(be_params->machine_size / 8); /* pointer size */
922 /* compile_unit die */
923 emit_uleb128(abbrev_compile_unit);
924 emit_address("line_section_begin");
925 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
926 ir_get_version_minor(), ir_get_version_revision());
927 be_gas_emit_cstring(filename);
929 emit_int16(language);
930 if (comp_dir != NULL)
931 be_gas_emit_cstring(comp_dir);
933 /* tell gas to emit cfi in debug_frame
934 * TODO: if we produce exception handling code then this should be
935 * .eh_frame (I also wonder if bad things happen if simply always
937 be_emit_cstring("\t.cfi_sections .debug_frame\n");
938 be_emit_write_line();
941 void be_dwarf_unit_end(void)
943 if (debug_level < LEVEL_BASIC)
945 be_gas_emit_switch_section(GAS_SECTION_TEXT);
946 emit_label("section_end");
948 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
949 emit_uleb128(0); /* end of compile_unit DIE */
951 emit_label("compile_unit_end");
957 void be_dwarf_close(void)
959 if (debug_level < LEVEL_BASIC)
961 pmap_destroy(env.file_map);
962 DEL_ARR_F(env.file_list);
963 DEL_ARR_F(env.pubnames_list);
964 pset_new_destroy(&env.emitted_types);
967 /* Opens a dwarf handler */
968 void be_dwarf_open(void)
970 if (debug_level < LEVEL_BASIC)
972 env.file_map = pmap_create();
973 env.file_list = NEW_ARR_F(const char*, 0);
974 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
975 pset_new_init(&env.emitted_types);
978 void be_dwarf_set_source_language(dwarf_source_language new_language)
980 language = new_language;
983 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
986 comp_dir = xstrdup(new_comp_dir);
989 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
990 void be_init_dwarf(void)
992 static const lc_opt_enum_int_items_t level_items[] = {
993 { "none", LEVEL_NONE },
994 { "basic", LEVEL_BASIC },
995 { "locations", LEVEL_LOCATIONS },
996 { "frameinfo", LEVEL_FRAMEINFO },
999 static lc_opt_enum_int_var_t debug_level_opt = {
1000 &debug_level, level_items
1002 static lc_opt_table_entry_t be_main_options[] = {
1003 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
1007 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1008 lc_opt_add_table(be_grp, be_main_options);