bedwarf: query backend for pointer size
[libfirm] / ir / be / bedwarf.c
index 62d7cfc..49f4a8f 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <assert.h>
 
+#include "bedwarf_t.h"
 #include "obst.h"
 #include "irprog.h"
 #include "irgraph.h"
 #include "util.h"
 #include "obst.h"
 #include "array_t.h"
-#include "be_dbgout_t.h"
+#include "irtools.h"
+#include "lc_opts.h"
+#include "lc_opts_enum.h"
 #include "beabi.h"
 #include "bemodule.h"
 #include "beemitter.h"
 #include "dbginfo.h"
 #include "begnuas.h"
 
-/* Tag names and codes.  */
-typedef enum dwarf_tag {
-       DW_TAG_padding = 0x00,
-       DW_TAG_array_type = 0x01,
-       DW_TAG_class_type = 0x02,
-       DW_TAG_entry_point = 0x03,
-       DW_TAG_enumeration_type = 0x04,
-       DW_TAG_formal_parameter = 0x05,
-       DW_TAG_imported_declaration = 0x08,
-       DW_TAG_label = 0x0a,
-       DW_TAG_lexical_block = 0x0b,
-       DW_TAG_member = 0x0d,
-       DW_TAG_pointer_type = 0x0f,
-       DW_TAG_reference_type = 0x10,
-       DW_TAG_compile_unit = 0x11,
-       DW_TAG_string_type = 0x12,
-       DW_TAG_structure_type = 0x13,
-       DW_TAG_subroutine_type = 0x15,
-       DW_TAG_typedef = 0x16,
-       DW_TAG_union_type = 0x17,
-       DW_TAG_unspecified_parameters = 0x18,
-       DW_TAG_variant = 0x19,
-       DW_TAG_common_block = 0x1a,
-       DW_TAG_common_inclusion = 0x1b,
-       DW_TAG_inheritance = 0x1c,
-       DW_TAG_inlined_subroutine = 0x1d,
-       DW_TAG_module = 0x1e,
-       DW_TAG_ptr_to_member_type = 0x1f,
-       DW_TAG_set_type = 0x20,
-       DW_TAG_subrange_type = 0x21,
-       DW_TAG_with_stmt = 0x22,
-       DW_TAG_access_declaration = 0x23,
-       DW_TAG_base_type = 0x24,
-       DW_TAG_catch_block = 0x25,
-       DW_TAG_const_type = 0x26,
-       DW_TAG_constant = 0x27,
-       DW_TAG_enumerator = 0x28,
-       DW_TAG_file_type = 0x29,
-       DW_TAG_friend = 0x2a,
-       DW_TAG_namelist = 0x2b,
-       DW_TAG_namelist_item = 0x2c,
-       DW_TAG_packed_type = 0x2d,
-       DW_TAG_subprogram = 0x2e,
-       DW_TAG_template_type_param = 0x2f,
-       DW_TAG_template_value_param = 0x30,
-       DW_TAG_thrown_type = 0x31,
-       DW_TAG_try_block = 0x32,
-       DW_TAG_variant_part = 0x33,
-       DW_TAG_variable = 0x34,
-       DW_TAG_volatile_type = 0x35,
-       /* DWARF 3.  */
-       DW_TAG_dwarf_procedure = 0x36,
-       DW_TAG_restrict_type = 0x37,
-       DW_TAG_interface_type = 0x38,
-       DW_TAG_namespace = 0x39,
-       DW_TAG_imported_module = 0x3a,
-       DW_TAG_unspecified_type = 0x3b,
-       DW_TAG_partial_unit = 0x3c,
-       DW_TAG_imported_unit = 0x3d,
-       DW_TAG_condition = 0x3f,
-       DW_TAG_shared_type = 0x40,
-} dwarf_tag;
+enum {
+       LEVEL_NONE,
+       LEVEL_BASIC,
+       LEVEL_LOCATIONS,
+       LEVEL_FRAMEINFO
+};
+static int debug_level = LEVEL_NONE;
 
+/**
+ * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
+ * the cases where we need multiple ids with the same DW_TAG we define new IDs
+ * here
+ */
 typedef enum custom_abbrevs {
-       abbrev_void_pointer_type = 100,
+       abbrev_void_subprogram = 1,
+       abbrev_subprogram,
+       abbrev_formal_parameter,
        abbrev_unnamed_formal_parameter,
-       abbrev_void_subroutine_type,
+       abbrev_formal_parameter_no_location,
+       abbrev_variable,
+       abbrev_compile_unit,
+       abbrev_base_type,
+       abbrev_pointer_type,
+       abbrev_void_pointer_type,
+       abbrev_array_type,
+       abbrev_subrange_type,
+       abbrev_structure_type,
+       abbrev_union_type,
+       abbrev_class_type,
+       abbrev_member,
        abbrev_bitfield_member,
+       abbrev_subroutine_type,
+       abbrev_void_subroutine_type,
 } custom_abbrevs;
 
-typedef enum dw_children {
-       DW_CHILDREN_no  = 0x00,
-       DW_CHILDREN_yes = 0x01
-} dw_children;
-
-typedef enum dwarf_form {
-       DW_FORM_addr = 0x01,
-       DW_FORM_block2 = 0x03,
-       DW_FORM_block4 = 0x04,
-       DW_FORM_data2 = 0x05,
-       DW_FORM_data4 = 0x06,
-       DW_FORM_data8 = 0x07,
-       DW_FORM_string = 0x08,
-       DW_FORM_block = 0x09,
-       DW_FORM_block1 = 0x0a,
-       DW_FORM_data1 = 0x0b,
-       DW_FORM_flag = 0x0c,
-       DW_FORM_sdata = 0x0d,
-       DW_FORM_strp = 0x0e,
-       DW_FORM_udata = 0x0f,
-       DW_FORM_ref_addr = 0x10,
-       DW_FORM_ref1 = 0x11,
-       DW_FORM_ref2 = 0x12,
-       DW_FORM_ref4 = 0x13,
-       DW_FORM_ref8 = 0x14,
-       DW_FORM_ref_udata = 0x15,
-       DW_FORM_indirect = 0x16
-} dwarf_form;
-
-typedef enum dwarf_attribute {
-       DW_AT_sibling = 0x01,
-       DW_AT_location = 0x02,
-       DW_AT_name = 0x03,
-       DW_AT_ordering = 0x09,
-       DW_AT_subscr_data = 0x0a,
-       DW_AT_byte_size = 0x0b,
-       DW_AT_bit_offset = 0x0c,
-       DW_AT_bit_size = 0x0d,
-       DW_AT_element_list = 0x0f,
-       DW_AT_stmt_list = 0x10,
-       DW_AT_low_pc = 0x11,
-       DW_AT_high_pc = 0x12,
-       DW_AT_language = 0x13,
-       DW_AT_member = 0x14,
-       DW_AT_discr = 0x15,
-       DW_AT_discr_value = 0x16,
-       DW_AT_visibility = 0x17,
-       DW_AT_import = 0x18,
-       DW_AT_string_length = 0x19,
-       DW_AT_common_reference = 0x1a,
-       DW_AT_comp_dir = 0x1b,
-       DW_AT_const_value = 0x1c,
-       DW_AT_containing_type = 0x1d,
-       DW_AT_default_value = 0x1e,
-       DW_AT_inline = 0x20,
-       DW_AT_is_optional = 0x21,
-       DW_AT_lower_bound = 0x22,
-       DW_AT_producer = 0x25,
-       DW_AT_prototyped = 0x27,
-       DW_AT_return_addr = 0x2a,
-       DW_AT_start_scope = 0x2c,
-       DW_AT_bit_stride = 0x2e,
-       DW_AT_stride_size = DW_AT_bit_stride,
-       DW_AT_upper_bound = 0x2f,
-       DW_AT_abstract_origin = 0x31,
-       DW_AT_accessibility = 0x32,
-       DW_AT_address_class = 0x33,
-       DW_AT_artificial = 0x34,
-       DW_AT_base_types = 0x35,
-       DW_AT_calling_convention = 0x36,
-       DW_AT_count = 0x37,
-       DW_AT_data_member_location = 0x38,
-       DW_AT_decl_column = 0x39,
-       DW_AT_decl_file = 0x3a,
-       DW_AT_decl_line = 0x3b,
-       DW_AT_declaration = 0x3c,
-       DW_AT_discr_list = 0x3d,
-       DW_AT_encoding = 0x3e,
-       DW_AT_external = 0x3f,
-       DW_AT_frame_base = 0x40,
-       DW_AT_friend = 0x41,
-       DW_AT_identifier_case = 0x42,
-       DW_AT_macro_info = 0x43,
-       DW_AT_namelist_items = 0x44,
-       DW_AT_priority = 0x45,
-       DW_AT_segment = 0x46,
-       DW_AT_specification = 0x47,
-       DW_AT_static_link = 0x48,
-       DW_AT_type = 0x49,
-       DW_AT_use_location = 0x4a,
-       DW_AT_variable_parameter = 0x4b,
-       DW_AT_virtuality = 0x4c,
-       DW_AT_vtable_elem_location = 0x4d,
-       /* DWARF 3 values.  */
-       DW_AT_allocated     = 0x4e,
-       DW_AT_associated    = 0x4f,
-       DW_AT_data_location = 0x50,
-       DW_AT_byte_stride   = 0x51,
-       DW_AT_stride        = DW_AT_byte_stride,
-       DW_AT_entry_pc      = 0x52,
-       DW_AT_use_UTF8      = 0x53,
-       DW_AT_extension     = 0x54,
-       DW_AT_ranges        = 0x55,
-       DW_AT_trampoline    = 0x56,
-       DW_AT_call_column   = 0x57,
-       DW_AT_call_file     = 0x58,
-       DW_AT_call_line     = 0x59,
-       DW_AT_description   = 0x5a,
-       DW_AT_binary_scale  = 0x5b,
-       DW_AT_decimal_scale = 0x5c,
-       DW_AT_small         = 0x5d,
-       DW_AT_decimal_sign  = 0x5e,
-       DW_AT_digit_count   = 0x5f,
-       DW_AT_picture_string = 0x60,
-       DW_AT_mutable       = 0x61,
-       DW_AT_threads_scaled = 0x62,
-       DW_AT_explicit      = 0x63,
-       DW_AT_object_pointer = 0x64,
-       DW_AT_endianity     = 0x65,
-       DW_AT_elemental     = 0x66,
-       DW_AT_pure          = 0x67,
-       DW_AT_recursive     = 0x68,
-} dwarf_attribute;
-
-enum dwarf_type {
-       DW_ATE_void = 0x0,
-       DW_ATE_address = 0x1,
-       DW_ATE_boolean = 0x2,
-       DW_ATE_complex_float = 0x3,
-       DW_ATE_float = 0x4,
-       DW_ATE_signed = 0x5,
-       DW_ATE_signed_char = 0x6,
-       DW_ATE_unsigned = 0x7,
-       DW_ATE_unsigned_char = 0x8,
-       /* DWARF 3.  */
-       DW_ATE_imaginary_float = 0x9,
-       DW_ATE_packed_decimal = 0xa,
-       DW_ATE_numeric_string = 0xb,
-       DW_ATE_edited = 0xc,
-       DW_ATE_signed_fixed = 0xd,
-       DW_ATE_unsigned_fixed = 0xe,
-       DW_ATE_decimal_float = 0xf,
-};
-
-typedef enum dwarf_line_number_x_ops {
-       DW_LNE_end_sequence = 1,
-       DW_LNE_set_address = 2,
-       DW_LNE_define_file = 3,
-} dwarf_line_number_x_ops;
-
-typedef enum dwarf_location_op {
-       DW_OP_addr = 0x03,
-       DW_OP_deref = 0x06,
-       DW_OP_const1u = 0x08,
-       DW_OP_const1s = 0x09,
-       DW_OP_const2u = 0x0a,
-       DW_OP_const2s = 0x0b,
-       DW_OP_const4u = 0x0c,
-       DW_OP_const4s = 0x0d,
-       DW_OP_const8u = 0x0e,
-       DW_OP_const8s = 0x0f,
-       DW_OP_constu = 0x10,
-       DW_OP_consts = 0x11,
-       DW_OP_dup = 0x12,
-       DW_OP_drop = 0x13,
-       DW_OP_over = 0x14,
-       DW_OP_pick = 0x15,
-       DW_OP_swap = 0x16,
-       DW_OP_rot = 0x17,
-       DW_OP_xderef = 0x18,
-       DW_OP_abs = 0x19,
-       DW_OP_and = 0x1a,
-       DW_OP_div = 0x1b,
-       DW_OP_minus = 0x1c,
-       DW_OP_mod = 0x1d,
-       DW_OP_mul = 0x1e,
-       DW_OP_neg = 0x1f,
-       DW_OP_not = 0x20,
-       DW_OP_or = 0x21,
-       DW_OP_plus = 0x22,
-       DW_OP_plus_uconst = 0x23,
-       DW_OP_shl = 0x24,
-       DW_OP_shr = 0x25,
-       DW_OP_shra = 0x26,
-       DW_OP_xor = 0x27,
-       DW_OP_bra = 0x28,
-       DW_OP_eq = 0x29,
-       DW_OP_ge = 0x2a,
-       DW_OP_gt = 0x2b,
-       DW_OP_le = 0x2c,
-       DW_OP_lt = 0x2d,
-       DW_OP_ne = 0x2e,
-       DW_OP_skip = 0x2f,
-       DW_OP_lit0 = 0x30,
-       DW_OP_lit1 = 0x31,
-       DW_OP_lit2 = 0x32,
-       DW_OP_lit3 = 0x33,
-       DW_OP_lit4 = 0x34,
-       DW_OP_lit5 = 0x35,
-       DW_OP_lit6 = 0x36,
-       DW_OP_lit7 = 0x37,
-       DW_OP_lit8 = 0x38,
-       DW_OP_lit9 = 0x39,
-       DW_OP_lit10 = 0x3a,
-       DW_OP_lit11 = 0x3b,
-       DW_OP_lit12 = 0x3c,
-       DW_OP_lit13 = 0x3d,
-       DW_OP_lit14 = 0x3e,
-       DW_OP_lit15 = 0x3f,
-       DW_OP_lit16 = 0x40,
-       DW_OP_lit17 = 0x41,
-       DW_OP_lit18 = 0x42,
-       DW_OP_lit19 = 0x43,
-       DW_OP_lit20 = 0x44,
-       DW_OP_lit21 = 0x45,
-       DW_OP_lit22 = 0x46,
-       DW_OP_lit23 = 0x47,
-       DW_OP_lit24 = 0x48,
-       DW_OP_lit25 = 0x49,
-       DW_OP_lit26 = 0x4a,
-       DW_OP_lit27 = 0x4b,
-       DW_OP_lit28 = 0x4c,
-       DW_OP_lit29 = 0x4d,
-       DW_OP_lit30 = 0x4e,
-       DW_OP_lit31 = 0x4f,
-       DW_OP_reg0 = 0x50,
-       DW_OP_reg1 = 0x51,
-       DW_OP_reg2 = 0x52,
-       DW_OP_reg3 = 0x53,
-       DW_OP_reg4 = 0x54,
-       DW_OP_reg5 = 0x55,
-       DW_OP_reg6 = 0x56,
-       DW_OP_reg7 = 0x57,
-       DW_OP_reg8 = 0x58,
-       DW_OP_reg9 = 0x59,
-       DW_OP_reg10 = 0x5a,
-       DW_OP_reg11 = 0x5b,
-       DW_OP_reg12 = 0x5c,
-       DW_OP_reg13 = 0x5d,
-       DW_OP_reg14 = 0x5e,
-       DW_OP_reg15 = 0x5f,
-       DW_OP_reg16 = 0x60,
-       DW_OP_reg17 = 0x61,
-       DW_OP_reg18 = 0x62,
-       DW_OP_reg19 = 0x63,
-       DW_OP_reg20 = 0x64,
-       DW_OP_reg21 = 0x65,
-       DW_OP_reg22 = 0x66,
-       DW_OP_reg23 = 0x67,
-       DW_OP_reg24 = 0x68,
-       DW_OP_reg25 = 0x69,
-       DW_OP_reg26 = 0x6a,
-       DW_OP_reg27 = 0x6b,
-       DW_OP_reg28 = 0x6c,
-       DW_OP_reg29 = 0x6d,
-       DW_OP_reg30 = 0x6e,
-       DW_OP_reg31 = 0x6f,
-       DW_OP_breg0 = 0x70,
-       DW_OP_breg1 = 0x71,
-       DW_OP_breg2 = 0x72,
-       DW_OP_breg3 = 0x73,
-       DW_OP_breg4 = 0x74,
-       DW_OP_breg5 = 0x75,
-       DW_OP_breg6 = 0x76,
-       DW_OP_breg7 = 0x77,
-       DW_OP_breg8 = 0x78,
-       DW_OP_breg9 = 0x79,
-       DW_OP_breg10 = 0x7a,
-       DW_OP_breg11 = 0x7b,
-       DW_OP_breg12 = 0x7c,
-       DW_OP_breg13 = 0x7d,
-       DW_OP_breg14 = 0x7e,
-       DW_OP_breg15 = 0x7f,
-       DW_OP_breg16 = 0x80,
-       DW_OP_breg17 = 0x81,
-       DW_OP_breg18 = 0x82,
-       DW_OP_breg19 = 0x83,
-       DW_OP_breg20 = 0x84,
-       DW_OP_breg21 = 0x85,
-       DW_OP_breg22 = 0x86,
-       DW_OP_breg23 = 0x87,
-       DW_OP_breg24 = 0x88,
-       DW_OP_breg25 = 0x89,
-       DW_OP_breg26 = 0x8a,
-       DW_OP_breg27 = 0x8b,
-       DW_OP_breg28 = 0x8c,
-       DW_OP_breg29 = 0x8d,
-       DW_OP_breg30 = 0x8e,
-       DW_OP_breg31 = 0x8f,
-       DW_OP_regx = 0x90,
-       DW_OP_fbreg = 0x91,
-       DW_OP_bregx = 0x92,
-       DW_OP_piece = 0x93,
-       DW_OP_deref_size = 0x94,
-       DW_OP_xderef_size = 0x95,
-       DW_OP_nop = 0x96,
-} dwarf_location_op;
-
 /**
  * The dwarf handle.
  */
 typedef struct dwarf_t {
-       dbg_handle               base;         /**< the base class */
        const ir_entity         *cur_ent;     /**< current method entity */
-       const be_stack_layout_t *layout;      /**< current stack layout */
        unsigned                 next_type_nr; /**< next type number */
        pmap                    *file_map;    /**< a map from file names to number in file list */
        const char             **file_list;
@@ -432,19 +100,22 @@ typedef struct dwarf_t {
        unsigned                 last_line;
 } dwarf_t;
 
+static dwarf_t env;
+
 static dwarf_source_language language;
-static const char           *comp_dir;
+static char                 *comp_dir;
 
-static unsigned insert_file(dwarf_t *env, const char *filename)
+static unsigned insert_file(const char *filename)
 {
        unsigned num;
-       void    *entry = pmap_get(env->file_map, filename);
+       void    *entry = pmap_get(void, env.file_map, filename);
        if (entry != NULL) {
                return PTR_TO_INT(entry);
        }
-       ARR_APP1(const char*, env->file_list, filename);
-       num = (unsigned)ARR_LEN(env->file_list);
-       pmap_insert(env->file_map, filename, INT_TO_PTR(num));
+       ARR_APP1(const char*, env.file_list, filename);
+       num = (unsigned)ARR_LEN(env.file_list);
+       pmap_insert(env.file_map, filename, INT_TO_PTR(num));
+
        /* TODO: quote chars in string */
        be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
        return num;
@@ -484,8 +155,25 @@ static unsigned get_uleb128_size(unsigned value)
        return size;
 }
 
+static void emit_sleb128(long value)
+{
+       be_emit_irprintf("\t.sleb128 %ld\n", value);
+       be_emit_write_line();
+}
+
+static unsigned get_sleb128_size(long value)
+{
+       unsigned size = 0;
+       do {
+               value >>= 7;
+               size += 1;
+       } while (value != 0 && value != -1);
+       return size;
+}
+
 static void emit_string(const char *string)
 {
+       /* TODO: quote special chars */
        be_emit_irprintf("\t.asciz \"%s\"\n", string);
        be_emit_write_line();
 }
@@ -545,7 +233,8 @@ static void register_attribute(dwarf_attribute attribute, dwarf_form form)
        emit_uleb128(form);
 }
 
-static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
+static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
+                         dw_children children)
 {
        emit_uleb128(code);
        emit_uleb128(tag);
@@ -558,7 +247,7 @@ static void end_abbrev(void)
        emit_uleb128(0);
 }
 
-static void emit_line_info(dwarf_t *env)
+static void emit_line_info(void)
 {
        be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
 
@@ -594,8 +283,8 @@ static void emit_line_info(dwarf_t *env)
                emit_int8(0);
 
                /* file list */
-               for (i = 0; i < ARR_LEN(env->file_list); ++i) {
-                       emit_string(env->file_list[0]);
+               for (i = 0; i < ARR_LEN(env.file_list); ++i) {
+                       emit_string(env.file_list[i]);
                        emit_uleb128(1); /* directory */
                        emit_uleb128(0); /* modification time */
                        emit_uleb128(0); /* file length */
@@ -610,7 +299,7 @@ static void emit_line_info(dwarf_t *env)
        }
 }
 
-static void emit_pubnames(dwarf_t *env)
+static void emit_pubnames(void)
 {
        size_t i;
 
@@ -623,8 +312,8 @@ static void emit_pubnames(dwarf_t *env)
        emit_size("info_section_begin", "info_begin");
        emit_size("compile_unit_begin", "compile_unit_end");
 
-       for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
-               const ir_entity *entity = env->pubnames_list[i];
+       for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
+               const ir_entity *entity = env.pubnames_list[i];
                be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
                                 be_gas_get_private_prefix(),
                                 get_entity_nr(entity), be_gas_get_private_prefix());
@@ -635,31 +324,53 @@ static void emit_pubnames(dwarf_t *env)
        emit_label("pubnames_end");
 }
 
-static void dwarf_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
+void be_dwarf_location(dbg_info *dbgi)
 {
-       dwarf_t *env = (dwarf_t*) h;
-       const char *filename;
-       unsigned    lineno;
-       unsigned    column = 0;
-       unsigned    filenum;
+       src_loc_t loc;
+       unsigned  filenum;
+
+       if (debug_level < LEVEL_LOCATIONS)
+               return;
+       loc = ir_retrieve_dbg_info(dbgi);
+       if (!loc.file)
+               return;
+
+       filenum = insert_file(loc.file);
+       be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
+       be_emit_write_line();
+}
 
-       if (dbgi == NULL)
+void be_dwarf_callframe_register(const arch_register_t *reg)
+{
+       if (debug_level < LEVEL_FRAMEINFO)
                return;
+       be_emit_cstring("\t.cfi_def_cfa_register ");
+       be_emit_irprintf("%d\n", reg->dwarf_number);
+       be_emit_write_line();
+}
 
-       filename = ir_retrieve_dbg_info(dbgi, &lineno);
-       if (filename == NULL)
+void be_dwarf_callframe_offset(int offset)
+{
+       if (debug_level < LEVEL_FRAMEINFO)
                return;
-       filenum = insert_file(env, filename);
+       be_emit_cstring("\t.cfi_def_cfa_offset ");
+       be_emit_irprintf("%d\n", offset);
+       be_emit_write_line();
+}
 
-       be_emit_irprintf("\t.loc %u %u %u\n", filenum, lineno, column);
+void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
+{
+       if (debug_level < LEVEL_FRAMEINFO)
+               return;
+       be_emit_cstring("\t.cfi_offset ");
+       be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
        be_emit_write_line();
 }
 
 static bool is_extern_entity(const ir_entity *entity)
 {
        ir_visited_t visibility = get_entity_visibility(entity);
-       return visibility == ir_visibility_default
-           || visibility == ir_visibility_external;
+       return visibility == ir_visibility_external;
 }
 
 static void emit_entity_label(const ir_entity *entity)
@@ -669,92 +380,179 @@ static void emit_entity_label(const ir_entity *entity)
        be_emit_write_line();
 }
 
-/**
- * emits values for DW_AT_decl_file then DW_AT_decl_line
- */
-static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
+static void register_dbginfo_attributes(void)
 {
-       const char *filename;
-       unsigned line;
-       unsigned file;
-
-       if (dbgi == NULL) {
-               emit_uleb128(0);
-               emit_uleb128(0);
-               return;
-       }
-       filename = ir_retrieve_dbg_info(dbgi, &line);
-       if (filename == NULL) {
-               emit_uleb128(0);
-               emit_uleb128(0);
-               return;
-       }
+       register_attribute(DW_AT_decl_file,   DW_FORM_udata);
+       register_attribute(DW_AT_decl_line,   DW_FORM_udata);
+       register_attribute(DW_AT_decl_column, DW_FORM_udata);
+}
 
-       file = insert_file(env, filename);
+static void emit_dbginfo(const dbg_info *dbgi)
+{
+       src_loc_t const loc  = ir_retrieve_dbg_info(dbgi);
+       unsigned  const file = loc.file ? insert_file(loc.file) : 0;
        emit_uleb128(file);
-       emit_uleb128(line);
+       emit_uleb128(loc.line);
+       emit_uleb128(loc.column);
+}
+
+static void emit_type_address(const ir_type *type)
+{
+       be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
+                        be_gas_get_private_prefix(),
+                        get_type_nr(type), be_gas_get_private_prefix());
+       be_emit_write_line();
 }
 
 static void emit_subprogram_abbrev(void)
 {
-       begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
+       begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
        register_attribute(DW_AT_name,      DW_FORM_string);
-       register_attribute(DW_AT_decl_file, DW_FORM_udata);
-       register_attribute(DW_AT_decl_line, DW_FORM_udata);
+       register_dbginfo_attributes();
+       register_attribute(DW_AT_type,       DW_FORM_ref4);
+       register_attribute(DW_AT_external,   DW_FORM_flag);
+       register_attribute(DW_AT_low_pc,     DW_FORM_addr);
+       register_attribute(DW_AT_high_pc,    DW_FORM_addr);
        //register_attribute(DW_AT_prototyped, DW_FORM_flag);
-       //register_attribute(DW_AT_type,       DW_FORM_ref4);
-       register_attribute(DW_AT_external,  DW_FORM_flag);
-       register_attribute(DW_AT_low_pc,    DW_FORM_addr);
-       register_attribute(DW_AT_high_pc,   DW_FORM_addr);
-       //register_attribute(DW_AT_frame_base, DW_FORM_block1);
+       if (debug_level >= LEVEL_FRAMEINFO)
+               register_attribute(DW_AT_frame_base, DW_FORM_block1);
+       end_abbrev();
+
+       begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
+       register_attribute(DW_AT_name,       DW_FORM_string);
+       register_dbginfo_attributes();
+       register_attribute(DW_AT_external,   DW_FORM_flag);
+       register_attribute(DW_AT_low_pc,     DW_FORM_addr);
+       register_attribute(DW_AT_high_pc,    DW_FORM_addr);
+       //register_attribute(DW_AT_prototyped, DW_FORM_flag);
+       if (debug_level >= LEVEL_FRAMEINFO)
+               register_attribute(DW_AT_frame_base, DW_FORM_block1);
+       end_abbrev();
+
+       begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
+                    DW_CHILDREN_no);
+       register_attribute(DW_AT_name,      DW_FORM_string);
+       register_dbginfo_attributes();
+       register_attribute(DW_AT_type,      DW_FORM_ref4);
+       register_attribute(DW_AT_location,  DW_FORM_block1);
+       end_abbrev();
+
+       begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
+                    DW_CHILDREN_no);
+       register_attribute(DW_AT_name,      DW_FORM_string);
+       register_dbginfo_attributes();
+       register_attribute(DW_AT_type,      DW_FORM_ref4);
        end_abbrev();
 }
 
-/**
- * dump the drwarf for a method begin
- */
-static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
+static void emit_type(ir_type *type);
+
+static void emit_stack_location(long offset)
 {
-       dwarf_t *env = (dwarf_t*)handle;
+       unsigned size = 1 + get_sleb128_size(offset);
+       emit_int8(size);
+       emit_int8(DW_OP_fbreg);
+       emit_sleb128(offset);
+}
+
+static void emit_function_parameters(const ir_entity *entity,
+                                     const parameter_dbg_info_t *infos)
+{
+       ir_type  *type     = get_entity_type(entity);
+       size_t    n_params = get_method_n_params(type);
+       dbg_info *dbgi     = get_entity_dbg_info(entity);
+       size_t    i;
+       for (i = 0; i < n_params; ++i) {
+               ir_type *param_type = get_method_param_type(type, i);
+
+               if (infos != NULL && infos[i].entity != NULL) {
+                       long const offset = get_entity_offset(infos[i].entity);
+                       emit_uleb128(abbrev_formal_parameter);
+                       emit_string_printf("arg%u", (unsigned)i);
+                       emit_dbginfo(dbgi);
+                       emit_type_address(param_type);
+                       emit_stack_location(offset);
+               } else {
+                       emit_uleb128(abbrev_formal_parameter_no_location);
+                       emit_string_printf("arg%u", (unsigned)i);
+                       emit_dbginfo(dbgi);
+                       emit_type_address(param_type);
+               }
+       }
+}
+
+void be_dwarf_method_before(const ir_entity *entity,
+                            const parameter_dbg_info_t *parameter_infos)
+{
+       if (debug_level < LEVEL_BASIC)
+               return;
+       {
+       ir_type *type     = get_entity_type(entity);
+       size_t   n_ress   = get_method_n_ress(type);
+       size_t   n_params = get_method_n_params(type);
+       size_t   i;
 
        be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
 
+       if (n_ress > 0) {
+               ir_type *res = get_method_res_type(type, 0);
+               emit_type(res);
+       }
+       for (i = 0; i < n_params; ++i) {
+               ir_type *param_type = get_method_param_type(type, i);
+               emit_type(param_type);
+       }
+
        emit_entity_label(entity);
-       emit_uleb128(DW_TAG_subprogram);
+       emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
        emit_string(get_entity_ld_name(entity));
-       emit_dbginfo(env, get_entity_dbg_info(entity));
+       emit_dbginfo(get_entity_dbg_info(entity));
+       if (n_ress > 0) {
+               ir_type *res = get_method_res_type(type, 0);
+               emit_type_address(res);
+       }
        emit_int8(is_extern_entity(entity));
        emit_ref(entity);
        be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
                         get_entity_ld_name(entity));
+       /* frame_base prog */
+       emit_int8(1);
+       emit_int8(DW_OP_call_frame_cfa);
+
+       emit_function_parameters(entity, parameter_infos);
+       emit_int8(0);
 
-       ARR_APP1(const ir_entity*, env->pubnames_list, entity);
+       ARR_APP1(const ir_entity*, env.pubnames_list, entity);
 
-       env->cur_ent = entity;
+       env.cur_ent = entity;
+       }
 }
 
-/**
- * dump the drwarf for a method end
- */
-static void dwarf_method_end(dbg_handle *handle)
+void be_dwarf_method_begin(void)
 {
-       dwarf_t *env = (dwarf_t*)handle;
-       const ir_entity *entity = env->cur_ent;
+       if (debug_level < LEVEL_FRAMEINFO)
+               return;
+       be_emit_cstring("\t.cfi_startproc\n");
+       be_emit_write_line();
+}
 
+void be_dwarf_method_end(void)
+{
+       if (debug_level < LEVEL_BASIC)
+               return;
+       const ir_entity *entity = env.cur_ent;
        be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
                         get_entity_ld_name(entity));
-}
 
-static void dwarf_types(dbg_handle *handle)
-{
-       (void)handle;
+       if (debug_level >= LEVEL_FRAMEINFO) {
+               be_emit_cstring("\t.cfi_endproc\n");
+               be_emit_write_line();
+       }
 }
 
-static void emit_type(dwarf_t *env, ir_type *type);
-
 static void emit_base_type_abbrev(void)
 {
-       begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
+       begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
        register_attribute(DW_AT_encoding,  DW_FORM_data1);
        register_attribute(DW_AT_byte_size, DW_FORM_data1);
        register_attribute(DW_AT_name,      DW_FORM_string);
@@ -767,14 +565,6 @@ static void emit_type_label(const ir_type *type)
        be_emit_write_line();
 }
 
-static void emit_type_address(const ir_type *type)
-{
-       be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
-                        be_gas_get_private_prefix(),
-                        get_type_nr(type), be_gas_get_private_prefix());
-       be_emit_write_line();
-}
-
 static void emit_base_type(const ir_type *type)
 {
        char buf[128];
@@ -782,7 +572,7 @@ static void emit_base_type(const ir_type *type)
        ir_print_type(buf, sizeof(buf), type);
 
        emit_type_label(type);
-       emit_uleb128(DW_TAG_base_type);
+       emit_uleb128(abbrev_base_type);
        if (mode_is_int(mode)) {
                /* bool hack */
                if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
@@ -803,7 +593,7 @@ static void emit_base_type(const ir_type *type)
 
 static void emit_pointer_type_abbrev(void)
 {
-       begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
+       begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
        register_attribute(DW_AT_type,      DW_FORM_ref4);
        register_attribute(DW_AT_byte_size, DW_FORM_data1);
        end_abbrev();
@@ -814,17 +604,17 @@ static void emit_pointer_type_abbrev(void)
        end_abbrev();
 }
 
-static void emit_pointer_type(dwarf_t *env, const ir_type *type)
+static void emit_pointer_type(const ir_type *type)
 {
        ir_type *points_to = get_pointer_points_to_type(type);
        unsigned size      = get_type_size_bytes(type);
        assert(size < 256);
 
        if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
-               emit_type(env, points_to);
+               emit_type(points_to);
 
                emit_type_label(type);
-               emit_uleb128(DW_TAG_pointer_type);
+               emit_uleb128(abbrev_pointer_type);
                emit_type_address(points_to);
        } else {
                emit_type_label(type);
@@ -835,31 +625,31 @@ static void emit_pointer_type(dwarf_t *env, const ir_type *type)
 
 static void emit_array_type_abbrev(void)
 {
-       begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
+       begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
        register_attribute(DW_AT_type, DW_FORM_ref4);
        end_abbrev();
 
-       begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
+       begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
        register_attribute(DW_AT_upper_bound, DW_FORM_udata);
        end_abbrev();
 }
 
-static void emit_array_type(dwarf_t *env, const ir_type *type)
+static void emit_array_type(const ir_type *type)
 {
        ir_type *element_type = get_array_element_type(type);
 
        if (get_array_n_dimensions(type) != 1)
-               panic("dwarf: multidimensional arrays no supported yet");
+               panic("multidimensional arrays no supported yet");
 
-       emit_type(env, element_type);
+       emit_type(element_type);
 
        emit_type_label(type);
-       emit_uleb128(DW_TAG_array_type);
+       emit_uleb128(abbrev_array_type);
        emit_type_address(element_type);
 
        if (has_array_upper_bound(type, 0)) {
                int bound = get_array_upper_bound_int(type, 0);
-               emit_uleb128(DW_TAG_subrange_type);
+               emit_uleb128(abbrev_subrange_type);
                emit_uleb128(bound);
        }
 
@@ -868,29 +658,25 @@ static void emit_array_type(dwarf_t *env, const ir_type *type)
 
 static void emit_compound_type_abbrev(void)
 {
-       begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
+       begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
        register_attribute(DW_AT_byte_size,  DW_FORM_udata);
-       //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
-       //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
+       // TODO register_dbginfo_attributes();
        end_abbrev();
 
-       begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
+       begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
        register_attribute(DW_AT_byte_size,  DW_FORM_udata);
-       //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
-       //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
+       // TODO register_dbginfo_attributes();
        end_abbrev();
 
-       begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
+       begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
        register_attribute(DW_AT_byte_size,  DW_FORM_udata);
-       //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
-       //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
+       // TODO register_dbginfo_attributes();
        end_abbrev();
 
-       begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
+       begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
        register_attribute(DW_AT_type,                 DW_FORM_ref4);
        register_attribute(DW_AT_name,                 DW_FORM_string);
-       register_attribute(DW_AT_decl_file,            DW_FORM_udata);
-       register_attribute(DW_AT_decl_line,            DW_FORM_udata);
+       register_dbginfo_attributes();
        register_attribute(DW_AT_data_member_location, DW_FORM_block1);
        end_abbrev();
 
@@ -900,8 +686,7 @@ static void emit_compound_type_abbrev(void)
        register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
        register_attribute(DW_AT_type,                 DW_FORM_ref4);
        register_attribute(DW_AT_name,                 DW_FORM_string);
-       register_attribute(DW_AT_decl_file,            DW_FORM_udata);
-       register_attribute(DW_AT_decl_line,            DW_FORM_udata);
+       register_dbginfo_attributes();
        register_attribute(DW_AT_data_member_location, DW_FORM_block1);
        end_abbrev();
 }
@@ -912,7 +697,7 @@ static void emit_op_plus_uconst(unsigned value)
        emit_uleb128(value);
 }
 
-static void emit_compound_type(dwarf_t *env, const ir_type *type)
+static void emit_compound_type(const ir_type *type)
 {
        size_t i;
        size_t n_members = get_compound_n_members(type);
@@ -925,17 +710,17 @@ static void emit_compound_type(dwarf_t *env, const ir_type *type)
                        if (base != NULL)
                                member_type = base;
                }
-               emit_type(env, member_type);
+               emit_type(member_type);
        }
 
        emit_type_label(type);
        if (is_Struct_type(type)) {
-               emit_uleb128(DW_TAG_structure_type);
+               emit_uleb128(abbrev_structure_type);
        } else if (is_Union_type(type)) {
-               emit_uleb128(DW_TAG_union_type);
+               emit_uleb128(abbrev_union_type);
        } else {
                assert(is_Class_type(type));
-               emit_uleb128(DW_TAG_class_type);
+               emit_uleb128(abbrev_class_type);
        }
        emit_uleb128(get_type_size_bytes(type));
        for (i = 0; i < n_members; ++i) {
@@ -959,12 +744,12 @@ static void emit_compound_type(dwarf_t *env, const ir_type *type)
                        emit_uleb128(bit_offset);
                        member_type = base;
                } else {
-                       emit_uleb128(DW_TAG_member);
+                       emit_uleb128(abbrev_member);
                }
 
                emit_type_address(member_type);
                emit_string(get_entity_name(member));
-               emit_dbginfo(env, get_entity_dbg_info(member));
+               emit_dbginfo(get_entity_dbg_info(member));
                assert(offset >= 0);
                emit_int8(1 + get_uleb128_size(offset));
                emit_op_plus_uconst(offset);
@@ -975,7 +760,7 @@ static void emit_compound_type(dwarf_t *env, const ir_type *type)
 
 static void emit_subroutine_type_abbrev(void)
 {
-       begin_abbrev(DW_TAG_subroutine_type,
+       begin_abbrev(abbrev_subroutine_type,
                     DW_TAG_subroutine_type, DW_CHILDREN_yes);
        register_attribute(DW_AT_prototyped,   DW_FORM_flag);
        register_attribute(DW_AT_type,         DW_FORM_ref4);
@@ -992,22 +777,22 @@ static void emit_subroutine_type_abbrev(void)
        end_abbrev();
 }
 
-static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
+static void emit_subroutine_type(const ir_type *type)
 {
        size_t n_params = get_method_n_params(type);
        size_t n_ress   = get_method_n_ress(type);
        size_t i;
        for (i = 0; i < n_params; ++i) {
                ir_type *param_type = get_method_param_type(type, i);
-               emit_type(env, param_type);
+               emit_type(param_type);
        }
        for (i = 0; i < n_ress; ++i) {
                ir_type *res_type = get_method_res_type(type, i);
-               emit_type(env, res_type);
+               emit_type(res_type);
        }
 
        emit_type_label(type);
-       emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
+       emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
        emit_int8(1); /* prototyped */
        if (n_ress > 0) {
                /* dwarf only supports 1 return type */
@@ -1023,21 +808,21 @@ static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
        emit_int8(0);
 }
 
-static void emit_type(dwarf_t *env, ir_type *type)
+static void emit_type(ir_type *type)
 {
-       if (pset_new_insert(&env->emitted_types, type))
+       if (!pset_new_insert(&env.emitted_types, type))
                return;
 
        switch (get_type_tpop_code(type)) {
-       case tpo_primitive: emit_base_type(type);            break;
-       case tpo_pointer:   emit_pointer_type(env, type);    break;
-       case tpo_array:     emit_array_type(env, type);      break;
+       case tpo_primitive: emit_base_type(type);       break;
+       case tpo_pointer:   emit_pointer_type(type);    break;
+       case tpo_array:     emit_array_type(type);      break;
        case tpo_class:
        case tpo_struct:
-       case tpo_union:     emit_compound_type(env, type);   break;
-       case tpo_method:    emit_subroutine_type(env, type); break;
+       case tpo_union:     emit_compound_type(type);   break;
+       case tpo_method:    emit_subroutine_type(type); break;
        default:
-               panic("bedwarf: type %+F not implemented yet", type);
+               panic("type %+F not implemented yet", type);
        }
 }
 
@@ -1052,44 +837,46 @@ static void emit_op_addr(const ir_entity *entity)
 
 static void emit_variable_abbrev(void)
 {
-       begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
+       begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
        register_attribute(DW_AT_name,      DW_FORM_string);
        register_attribute(DW_AT_type,      DW_FORM_ref4);
        register_attribute(DW_AT_external,  DW_FORM_flag);
-       register_attribute(DW_AT_decl_file, DW_FORM_udata);
-       register_attribute(DW_AT_decl_line, DW_FORM_udata);
+       register_dbginfo_attributes();
        register_attribute(DW_AT_location,  DW_FORM_block1);
        end_abbrev();
 }
 
-static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
+void be_dwarf_variable(const ir_entity *entity)
 {
-       dwarf_t  *env  = (dwarf_t*) handle;
-       ir_type  *type = get_entity_type(entity);
+       ir_type *type = get_entity_type(entity);
 
+       if (debug_level < LEVEL_BASIC)
+               return;
        if (get_entity_ld_name(entity)[0] == '\0')
                return;
+       if (!entity_has_definition(entity))
+               return;
 
        be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
 
-       emit_type(env, type);
+       emit_type(type);
 
        emit_entity_label(entity);
-       emit_uleb128(DW_TAG_variable);
+       emit_uleb128(abbrev_variable);
        emit_string(get_entity_ld_name(entity));
        emit_type_address(type);
        emit_int8(is_extern_entity(entity));
-       emit_dbginfo(env, get_entity_dbg_info(entity));
+       emit_dbginfo(get_entity_dbg_info(entity));
        /* DW_AT_location */
        emit_int8(5); /* block length */
        emit_op_addr(entity);
 
-       ARR_APP1(const ir_entity*, env->pubnames_list, entity);
+       ARR_APP1(const ir_entity*, env.pubnames_list, entity);
 }
 
 static void emit_compile_unit_abbrev(void)
 {
-       begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
+       begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
        register_attribute(DW_AT_stmt_list, DW_FORM_data4);
        register_attribute(DW_AT_producer,  DW_FORM_string);
        register_attribute(DW_AT_name,      DW_FORM_string);
@@ -1118,43 +905,48 @@ static void emit_abbrev(void)
        emit_uleb128(0);
 }
 
-/**
- * start a new source object (compilation unit)
- */
-static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
+void be_dwarf_unit_begin(const char *filename)
 {
-       (void) handle;
-
+       if (debug_level < LEVEL_BASIC)
+               return;
        emit_abbrev();
 
        be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
        emit_label("info_section_begin");
        emit_label("info_begin");
 
+       const backend_params *be_params = be_get_backend_param();
+
        /* length of compilation unit info */
        emit_size("compile_unit_begin", "compile_unit_end");
        emit_label("compile_unit_begin");
-       emit_int16(2);   /* dwarf version */
+       emit_int16(3);   /* dwarf version */
        emit_address("abbrev_begin");
-       emit_int8(4);    /* pointer size */
+       emit_int8(be_params->machine_size / 8); /* pointer size */
 
        /* compile_unit die */
-       emit_uleb128(DW_TAG_compile_unit);
+       emit_uleb128(abbrev_compile_unit);
        emit_address("line_section_begin");
        emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
-                          ir_get_version_minor(),
-                          ir_get_version_revision());
+                          ir_get_version_minor(), ir_get_version_revision());
        emit_string(filename);
        if (language != 0)
-               emit_int16(DW_LANG_C_plus_plus);
+               emit_int16(language);
        if (comp_dir != NULL)
                emit_string(comp_dir);
+
+       /* tell gas to emit cfi in debug_frame
+        * TODO: if we produce exception handling code then this should be
+        *       .eh_frame (I also wonder if bad things happen if simply always
+        *       use eh_frame) */
+       be_emit_cstring("\t.cfi_sections .debug_frame\n");
+       be_emit_write_line();
 }
 
-static void dwarf_unit_end(dbg_handle *handle)
+void be_dwarf_unit_end(void)
 {
-       dwarf_t *env = (dwarf_t*)handle;
-
+       if (debug_level < LEVEL_BASIC)
+               return;
        be_gas_emit_switch_section(GAS_SECTION_TEXT);
        emit_label("section_end");
 
@@ -1163,53 +955,29 @@ static void dwarf_unit_end(dbg_handle *handle)
 
        emit_label("compile_unit_end");
 
-       emit_line_info(env);
-       emit_pubnames(env);
+       emit_line_info();
+       emit_pubnames();
 }
 
-/**
- * Close the drwarf handler.
- */
-static void dwarf_close(dbg_handle *handle)
+void be_dwarf_close(void)
 {
-       dwarf_t *h = (dwarf_t *)handle;
-       pmap_destroy(h->file_map);
-       DEL_ARR_F(h->file_list);
-       DEL_ARR_F(h->pubnames_list);
-       pset_new_destroy(&h->emitted_types);
-       free(h);
-}
-
-/** The drwarf operations. */
-static const debug_ops dwarf_ops = {
-       dwarf_close,
-       dwarf_unit_begin,
-       dwarf_unit_end,
-       dwarf_method_begin,
-       dwarf_method_end,
-       dwarf_types,
-       dwarf_variable,
-       dwarf_set_dbg_info
-};
-
-/* Opens a drwarf handler */
-static dbg_handle *be_dwarf_open(void)
-{
-       dwarf_t *h = XMALLOCZ(dwarf_t);
-
-       h->base.ops      = &dwarf_ops;
-       h->file_map      = pmap_create();
-       h->file_list     = NEW_ARR_F(const char*, 0);
-       h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
-       pset_new_init(&h->emitted_types);
-
-       return &h->base;
+       if (debug_level < LEVEL_BASIC)
+               return;
+       pmap_destroy(env.file_map);
+       DEL_ARR_F(env.file_list);
+       DEL_ARR_F(env.pubnames_list);
+       pset_new_destroy(&env.emitted_types);
 }
 
-BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
-void be_init_dwarf(void)
+/* Opens a dwarf handler */
+void be_dwarf_open(void)
 {
-       be_register_dbgout_module("dwarf", be_dwarf_open);
+       if (debug_level < LEVEL_BASIC)
+               return;
+       env.file_map      = pmap_create();
+       env.file_list     = NEW_ARR_F(const char*, 0);
+       env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
+       pset_new_init(&env.emitted_types);
 }
 
 void be_dwarf_set_source_language(dwarf_source_language new_language)
@@ -1219,5 +987,28 @@ void be_dwarf_set_source_language(dwarf_source_language new_language)
 
 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
 {
-       comp_dir = new_comp_dir;
+       xfree(comp_dir);
+       comp_dir = xstrdup(new_comp_dir);
+}
+
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
+void be_init_dwarf(void)
+{
+       static const lc_opt_enum_int_items_t level_items[] = {
+               { "none",      LEVEL_NONE },
+               { "basic",     LEVEL_BASIC },
+               { "locations", LEVEL_LOCATIONS },
+               { "frameinfo", LEVEL_FRAMEINFO },
+               { NULL,        0 }
+       };
+       static lc_opt_enum_int_var_t debug_level_opt = {
+               &debug_level, level_items
+       };
+       static lc_opt_table_entry_t be_main_options[] = {
+               LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
+                                   &debug_level_opt),
+               LC_OPT_LAST
+       };
+       lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
+       lc_opt_add_table(be_grp, be_main_options);
 }