DWARF: Factorise debug information format registration.
[libfirm] / ir / be / bedwarf.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   DWARF debugging info support
23  * @author  Matthias Braun
24  */
25 #include "config.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30
31 #include "obst.h"
32 #include "irprog.h"
33 #include "irgraph.h"
34 #include "tv.h"
35 #include "xmalloc.h"
36 #include "pmap.h"
37 #include "pdeq.h"
38 #include "pset_new.h"
39 #include "util.h"
40 #include "obst.h"
41 #include "array_t.h"
42 #include "be_dbgout_t.h"
43 #include "beabi.h"
44 #include "bemodule.h"
45 #include "beemitter.h"
46 #include "dbginfo.h"
47 #include "begnuas.h"
48
49 /* Tag names and codes.  */
50 typedef enum dwarf_tag {
51         DW_TAG_padding = 0x00,
52         DW_TAG_array_type = 0x01,
53         DW_TAG_class_type = 0x02,
54         DW_TAG_entry_point = 0x03,
55         DW_TAG_enumeration_type = 0x04,
56         DW_TAG_formal_parameter = 0x05,
57         DW_TAG_imported_declaration = 0x08,
58         DW_TAG_label = 0x0a,
59         DW_TAG_lexical_block = 0x0b,
60         DW_TAG_member = 0x0d,
61         DW_TAG_pointer_type = 0x0f,
62         DW_TAG_reference_type = 0x10,
63         DW_TAG_compile_unit = 0x11,
64         DW_TAG_string_type = 0x12,
65         DW_TAG_structure_type = 0x13,
66         DW_TAG_subroutine_type = 0x15,
67         DW_TAG_typedef = 0x16,
68         DW_TAG_union_type = 0x17,
69         DW_TAG_unspecified_parameters = 0x18,
70         DW_TAG_variant = 0x19,
71         DW_TAG_common_block = 0x1a,
72         DW_TAG_common_inclusion = 0x1b,
73         DW_TAG_inheritance = 0x1c,
74         DW_TAG_inlined_subroutine = 0x1d,
75         DW_TAG_module = 0x1e,
76         DW_TAG_ptr_to_member_type = 0x1f,
77         DW_TAG_set_type = 0x20,
78         DW_TAG_subrange_type = 0x21,
79         DW_TAG_with_stmt = 0x22,
80         DW_TAG_access_declaration = 0x23,
81         DW_TAG_base_type = 0x24,
82         DW_TAG_catch_block = 0x25,
83         DW_TAG_const_type = 0x26,
84         DW_TAG_constant = 0x27,
85         DW_TAG_enumerator = 0x28,
86         DW_TAG_file_type = 0x29,
87         DW_TAG_friend = 0x2a,
88         DW_TAG_namelist = 0x2b,
89         DW_TAG_namelist_item = 0x2c,
90         DW_TAG_packed_type = 0x2d,
91         DW_TAG_subprogram = 0x2e,
92         DW_TAG_template_type_param = 0x2f,
93         DW_TAG_template_value_param = 0x30,
94         DW_TAG_thrown_type = 0x31,
95         DW_TAG_try_block = 0x32,
96         DW_TAG_variant_part = 0x33,
97         DW_TAG_variable = 0x34,
98         DW_TAG_volatile_type = 0x35,
99         /* DWARF 3.  */
100         DW_TAG_dwarf_procedure = 0x36,
101         DW_TAG_restrict_type = 0x37,
102         DW_TAG_interface_type = 0x38,
103         DW_TAG_namespace = 0x39,
104         DW_TAG_imported_module = 0x3a,
105         DW_TAG_unspecified_type = 0x3b,
106         DW_TAG_partial_unit = 0x3c,
107         DW_TAG_imported_unit = 0x3d,
108         DW_TAG_condition = 0x3f,
109         DW_TAG_shared_type = 0x40,
110 } dwarf_tag;
111
112 typedef enum custom_abbrevs {
113         abbrev_void_pointer_type = 100,
114         abbrev_unnamed_formal_parameter,
115         abbrev_void_subroutine_type,
116         abbrev_bitfield_member,
117 } custom_abbrevs;
118
119 typedef enum dw_children {
120         DW_CHILDREN_no  = 0x00,
121         DW_CHILDREN_yes = 0x01
122 } dw_children;
123
124 typedef enum dwarf_form {
125         DW_FORM_addr = 0x01,
126         DW_FORM_block2 = 0x03,
127         DW_FORM_block4 = 0x04,
128         DW_FORM_data2 = 0x05,
129         DW_FORM_data4 = 0x06,
130         DW_FORM_data8 = 0x07,
131         DW_FORM_string = 0x08,
132         DW_FORM_block = 0x09,
133         DW_FORM_block1 = 0x0a,
134         DW_FORM_data1 = 0x0b,
135         DW_FORM_flag = 0x0c,
136         DW_FORM_sdata = 0x0d,
137         DW_FORM_strp = 0x0e,
138         DW_FORM_udata = 0x0f,
139         DW_FORM_ref_addr = 0x10,
140         DW_FORM_ref1 = 0x11,
141         DW_FORM_ref2 = 0x12,
142         DW_FORM_ref4 = 0x13,
143         DW_FORM_ref8 = 0x14,
144         DW_FORM_ref_udata = 0x15,
145         DW_FORM_indirect = 0x16
146 } dwarf_form;
147
148 typedef enum dwarf_attribute {
149         DW_AT_sibling = 0x01,
150         DW_AT_location = 0x02,
151         DW_AT_name = 0x03,
152         DW_AT_ordering = 0x09,
153         DW_AT_subscr_data = 0x0a,
154         DW_AT_byte_size = 0x0b,
155         DW_AT_bit_offset = 0x0c,
156         DW_AT_bit_size = 0x0d,
157         DW_AT_element_list = 0x0f,
158         DW_AT_stmt_list = 0x10,
159         DW_AT_low_pc = 0x11,
160         DW_AT_high_pc = 0x12,
161         DW_AT_language = 0x13,
162         DW_AT_member = 0x14,
163         DW_AT_discr = 0x15,
164         DW_AT_discr_value = 0x16,
165         DW_AT_visibility = 0x17,
166         DW_AT_import = 0x18,
167         DW_AT_string_length = 0x19,
168         DW_AT_common_reference = 0x1a,
169         DW_AT_comp_dir = 0x1b,
170         DW_AT_const_value = 0x1c,
171         DW_AT_containing_type = 0x1d,
172         DW_AT_default_value = 0x1e,
173         DW_AT_inline = 0x20,
174         DW_AT_is_optional = 0x21,
175         DW_AT_lower_bound = 0x22,
176         DW_AT_producer = 0x25,
177         DW_AT_prototyped = 0x27,
178         DW_AT_return_addr = 0x2a,
179         DW_AT_start_scope = 0x2c,
180         DW_AT_bit_stride = 0x2e,
181         DW_AT_stride_size = DW_AT_bit_stride,
182         DW_AT_upper_bound = 0x2f,
183         DW_AT_abstract_origin = 0x31,
184         DW_AT_accessibility = 0x32,
185         DW_AT_address_class = 0x33,
186         DW_AT_artificial = 0x34,
187         DW_AT_base_types = 0x35,
188         DW_AT_calling_convention = 0x36,
189         DW_AT_count = 0x37,
190         DW_AT_data_member_location = 0x38,
191         DW_AT_decl_column = 0x39,
192         DW_AT_decl_file = 0x3a,
193         DW_AT_decl_line = 0x3b,
194         DW_AT_declaration = 0x3c,
195         DW_AT_discr_list = 0x3d,
196         DW_AT_encoding = 0x3e,
197         DW_AT_external = 0x3f,
198         DW_AT_frame_base = 0x40,
199         DW_AT_friend = 0x41,
200         DW_AT_identifier_case = 0x42,
201         DW_AT_macro_info = 0x43,
202         DW_AT_namelist_items = 0x44,
203         DW_AT_priority = 0x45,
204         DW_AT_segment = 0x46,
205         DW_AT_specification = 0x47,
206         DW_AT_static_link = 0x48,
207         DW_AT_type = 0x49,
208         DW_AT_use_location = 0x4a,
209         DW_AT_variable_parameter = 0x4b,
210         DW_AT_virtuality = 0x4c,
211         DW_AT_vtable_elem_location = 0x4d,
212         /* DWARF 3 values.  */
213         DW_AT_allocated     = 0x4e,
214         DW_AT_associated    = 0x4f,
215         DW_AT_data_location = 0x50,
216         DW_AT_byte_stride   = 0x51,
217         DW_AT_stride        = DW_AT_byte_stride,
218         DW_AT_entry_pc      = 0x52,
219         DW_AT_use_UTF8      = 0x53,
220         DW_AT_extension     = 0x54,
221         DW_AT_ranges        = 0x55,
222         DW_AT_trampoline    = 0x56,
223         DW_AT_call_column   = 0x57,
224         DW_AT_call_file     = 0x58,
225         DW_AT_call_line     = 0x59,
226         DW_AT_description   = 0x5a,
227         DW_AT_binary_scale  = 0x5b,
228         DW_AT_decimal_scale = 0x5c,
229         DW_AT_small         = 0x5d,
230         DW_AT_decimal_sign  = 0x5e,
231         DW_AT_digit_count   = 0x5f,
232         DW_AT_picture_string = 0x60,
233         DW_AT_mutable       = 0x61,
234         DW_AT_threads_scaled = 0x62,
235         DW_AT_explicit      = 0x63,
236         DW_AT_object_pointer = 0x64,
237         DW_AT_endianity     = 0x65,
238         DW_AT_elemental     = 0x66,
239         DW_AT_pure          = 0x67,
240         DW_AT_recursive     = 0x68,
241 } dwarf_attribute;
242
243 enum dwarf_type {
244         DW_ATE_void = 0x0,
245         DW_ATE_address = 0x1,
246         DW_ATE_boolean = 0x2,
247         DW_ATE_complex_float = 0x3,
248         DW_ATE_float = 0x4,
249         DW_ATE_signed = 0x5,
250         DW_ATE_signed_char = 0x6,
251         DW_ATE_unsigned = 0x7,
252         DW_ATE_unsigned_char = 0x8,
253         /* DWARF 3.  */
254         DW_ATE_imaginary_float = 0x9,
255         DW_ATE_packed_decimal = 0xa,
256         DW_ATE_numeric_string = 0xb,
257         DW_ATE_edited = 0xc,
258         DW_ATE_signed_fixed = 0xd,
259         DW_ATE_unsigned_fixed = 0xe,
260         DW_ATE_decimal_float = 0xf,
261 };
262
263 typedef enum dwarf_line_number_x_ops {
264         DW_LNE_end_sequence = 1,
265         DW_LNE_set_address = 2,
266         DW_LNE_define_file = 3,
267 } dwarf_line_number_x_ops;
268
269 typedef enum dwarf_location_op {
270         DW_OP_addr = 0x03,
271         DW_OP_deref = 0x06,
272         DW_OP_const1u = 0x08,
273         DW_OP_const1s = 0x09,
274         DW_OP_const2u = 0x0a,
275         DW_OP_const2s = 0x0b,
276         DW_OP_const4u = 0x0c,
277         DW_OP_const4s = 0x0d,
278         DW_OP_const8u = 0x0e,
279         DW_OP_const8s = 0x0f,
280         DW_OP_constu = 0x10,
281         DW_OP_consts = 0x11,
282         DW_OP_dup = 0x12,
283         DW_OP_drop = 0x13,
284         DW_OP_over = 0x14,
285         DW_OP_pick = 0x15,
286         DW_OP_swap = 0x16,
287         DW_OP_rot = 0x17,
288         DW_OP_xderef = 0x18,
289         DW_OP_abs = 0x19,
290         DW_OP_and = 0x1a,
291         DW_OP_div = 0x1b,
292         DW_OP_minus = 0x1c,
293         DW_OP_mod = 0x1d,
294         DW_OP_mul = 0x1e,
295         DW_OP_neg = 0x1f,
296         DW_OP_not = 0x20,
297         DW_OP_or = 0x21,
298         DW_OP_plus = 0x22,
299         DW_OP_plus_uconst = 0x23,
300         DW_OP_shl = 0x24,
301         DW_OP_shr = 0x25,
302         DW_OP_shra = 0x26,
303         DW_OP_xor = 0x27,
304         DW_OP_bra = 0x28,
305         DW_OP_eq = 0x29,
306         DW_OP_ge = 0x2a,
307         DW_OP_gt = 0x2b,
308         DW_OP_le = 0x2c,
309         DW_OP_lt = 0x2d,
310         DW_OP_ne = 0x2e,
311         DW_OP_skip = 0x2f,
312         DW_OP_lit0 = 0x30,
313         DW_OP_lit1 = 0x31,
314         DW_OP_lit2 = 0x32,
315         DW_OP_lit3 = 0x33,
316         DW_OP_lit4 = 0x34,
317         DW_OP_lit5 = 0x35,
318         DW_OP_lit6 = 0x36,
319         DW_OP_lit7 = 0x37,
320         DW_OP_lit8 = 0x38,
321         DW_OP_lit9 = 0x39,
322         DW_OP_lit10 = 0x3a,
323         DW_OP_lit11 = 0x3b,
324         DW_OP_lit12 = 0x3c,
325         DW_OP_lit13 = 0x3d,
326         DW_OP_lit14 = 0x3e,
327         DW_OP_lit15 = 0x3f,
328         DW_OP_lit16 = 0x40,
329         DW_OP_lit17 = 0x41,
330         DW_OP_lit18 = 0x42,
331         DW_OP_lit19 = 0x43,
332         DW_OP_lit20 = 0x44,
333         DW_OP_lit21 = 0x45,
334         DW_OP_lit22 = 0x46,
335         DW_OP_lit23 = 0x47,
336         DW_OP_lit24 = 0x48,
337         DW_OP_lit25 = 0x49,
338         DW_OP_lit26 = 0x4a,
339         DW_OP_lit27 = 0x4b,
340         DW_OP_lit28 = 0x4c,
341         DW_OP_lit29 = 0x4d,
342         DW_OP_lit30 = 0x4e,
343         DW_OP_lit31 = 0x4f,
344         DW_OP_reg0 = 0x50,
345         DW_OP_reg1 = 0x51,
346         DW_OP_reg2 = 0x52,
347         DW_OP_reg3 = 0x53,
348         DW_OP_reg4 = 0x54,
349         DW_OP_reg5 = 0x55,
350         DW_OP_reg6 = 0x56,
351         DW_OP_reg7 = 0x57,
352         DW_OP_reg8 = 0x58,
353         DW_OP_reg9 = 0x59,
354         DW_OP_reg10 = 0x5a,
355         DW_OP_reg11 = 0x5b,
356         DW_OP_reg12 = 0x5c,
357         DW_OP_reg13 = 0x5d,
358         DW_OP_reg14 = 0x5e,
359         DW_OP_reg15 = 0x5f,
360         DW_OP_reg16 = 0x60,
361         DW_OP_reg17 = 0x61,
362         DW_OP_reg18 = 0x62,
363         DW_OP_reg19 = 0x63,
364         DW_OP_reg20 = 0x64,
365         DW_OP_reg21 = 0x65,
366         DW_OP_reg22 = 0x66,
367         DW_OP_reg23 = 0x67,
368         DW_OP_reg24 = 0x68,
369         DW_OP_reg25 = 0x69,
370         DW_OP_reg26 = 0x6a,
371         DW_OP_reg27 = 0x6b,
372         DW_OP_reg28 = 0x6c,
373         DW_OP_reg29 = 0x6d,
374         DW_OP_reg30 = 0x6e,
375         DW_OP_reg31 = 0x6f,
376         DW_OP_breg0 = 0x70,
377         DW_OP_breg1 = 0x71,
378         DW_OP_breg2 = 0x72,
379         DW_OP_breg3 = 0x73,
380         DW_OP_breg4 = 0x74,
381         DW_OP_breg5 = 0x75,
382         DW_OP_breg6 = 0x76,
383         DW_OP_breg7 = 0x77,
384         DW_OP_breg8 = 0x78,
385         DW_OP_breg9 = 0x79,
386         DW_OP_breg10 = 0x7a,
387         DW_OP_breg11 = 0x7b,
388         DW_OP_breg12 = 0x7c,
389         DW_OP_breg13 = 0x7d,
390         DW_OP_breg14 = 0x7e,
391         DW_OP_breg15 = 0x7f,
392         DW_OP_breg16 = 0x80,
393         DW_OP_breg17 = 0x81,
394         DW_OP_breg18 = 0x82,
395         DW_OP_breg19 = 0x83,
396         DW_OP_breg20 = 0x84,
397         DW_OP_breg21 = 0x85,
398         DW_OP_breg22 = 0x86,
399         DW_OP_breg23 = 0x87,
400         DW_OP_breg24 = 0x88,
401         DW_OP_breg25 = 0x89,
402         DW_OP_breg26 = 0x8a,
403         DW_OP_breg27 = 0x8b,
404         DW_OP_breg28 = 0x8c,
405         DW_OP_breg29 = 0x8d,
406         DW_OP_breg30 = 0x8e,
407         DW_OP_breg31 = 0x8f,
408         DW_OP_regx = 0x90,
409         DW_OP_fbreg = 0x91,
410         DW_OP_bregx = 0x92,
411         DW_OP_piece = 0x93,
412         DW_OP_deref_size = 0x94,
413         DW_OP_xderef_size = 0x95,
414         DW_OP_nop = 0x96,
415 } dwarf_location_op;
416
417 /**
418  * The dwarf handle.
419  */
420 typedef struct dwarf_t {
421         dbg_handle               base;         /**< the base class */
422         const ir_entity         *cur_ent;     /**< current method entity */
423         const be_stack_layout_t *layout;      /**< current stack layout */
424         unsigned                 next_type_nr; /**< next type number */
425         pmap                    *file_map;    /**< a map from file names to number in file list */
426         const char             **file_list;
427         const ir_entity        **pubnames_list;
428         pset_new_t               emitted_types;
429         const char              *main_file;   /**< name of the main source file */
430         const char              *curr_file;   /**< name of the current source file */
431         unsigned                 label_num;
432         unsigned                 last_line;
433 } dwarf_t;
434
435 static dwarf_source_language language;
436 static const char           *comp_dir;
437
438 static unsigned insert_file(dwarf_t *env, const char *filename)
439 {
440         unsigned num;
441         void    *entry = pmap_get(env->file_map, filename);
442         if (entry != NULL) {
443                 return PTR_TO_INT(entry);
444         }
445         ARR_APP1(const char*, env->file_list, filename);
446         num = (unsigned)ARR_LEN(env->file_list);
447         pmap_insert(env->file_map, filename, INT_TO_PTR(num));
448         /* TODO: quote chars in string */
449         be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
450         return num;
451 }
452
453 static void emit_int32(uint32_t value)
454 {
455         be_emit_irprintf("\t.long %u\n", value);
456         be_emit_write_line();
457 }
458
459 static void emit_int16(uint16_t value)
460 {
461         be_emit_irprintf("\t.short %u\n", value);
462         be_emit_write_line();
463 }
464
465 static void emit_int8(uint8_t value)
466 {
467         be_emit_irprintf("\t.byte %u\n", value);
468         be_emit_write_line();
469 }
470
471 static void emit_uleb128(unsigned value)
472 {
473         be_emit_irprintf("\t.uleb128 0x%x\n", value);
474         be_emit_write_line();
475 }
476
477 static unsigned get_uleb128_size(unsigned value)
478 {
479         unsigned size = 0;
480         do {
481                 value >>= 7;
482                 size += 1;
483         } while (value != 0);
484         return size;
485 }
486
487 static void emit_string(const char *string)
488 {
489         be_emit_irprintf("\t.asciz \"%s\"\n", string);
490         be_emit_write_line();
491 }
492
493 static void emit_ref(const ir_entity *entity)
494 {
495         be_emit_cstring("\t.long ");
496         be_gas_emit_entity(entity);
497         be_emit_char('\n');
498         be_emit_write_line();
499 }
500
501 static void emit_string_printf(const char *fmt, ...)
502 {
503         va_list ap;
504         va_start(ap, fmt);
505         be_emit_cstring("\t.asciz \"");
506         be_emit_irvprintf(fmt, ap);
507         be_emit_cstring("\"\n");
508         va_end(ap);
509
510         be_emit_write_line();
511 }
512
513 static void emit_address(const char *name)
514 {
515         be_emit_cstring("\t.long ");
516         be_emit_string(be_gas_get_private_prefix());
517         be_emit_string(name);
518         be_emit_char('\n');
519         be_emit_write_line();
520 }
521
522 static void emit_size(const char *from_label, const char *to_label)
523 {
524         be_emit_cstring("\t.long ");
525         be_emit_string(be_gas_get_private_prefix());
526         be_emit_string(to_label);
527         be_emit_cstring(" - ");
528         be_emit_string(be_gas_get_private_prefix());
529         be_emit_string(from_label);
530         be_emit_char('\n');
531         be_emit_write_line();
532 }
533
534 static void emit_label(const char *name)
535 {
536         be_emit_string(be_gas_get_private_prefix());
537         be_emit_string(name);
538         be_emit_cstring(":\n");
539         be_emit_write_line();
540 }
541
542 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
543 {
544         emit_uleb128(attribute);
545         emit_uleb128(form);
546 }
547
548 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
549 {
550         emit_uleb128(code);
551         emit_uleb128(tag);
552         emit_int8(children);
553 }
554
555 static void end_abbrev(void)
556 {
557         emit_uleb128(0);
558         emit_uleb128(0);
559 }
560
561 static void emit_line_info(dwarf_t *env)
562 {
563         be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
564
565         emit_label("line_section_begin");
566         /* on elf systems gas handles producing the line info for us, and we
567          * don't have to do anything */
568         if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
569                 size_t i;
570                 emit_size("line_info_begin", "line_info_end");
571
572                 emit_label("line_info_begin");
573                 emit_int16(2); /* version */
574                 emit_size("line_info_prolog_begin", "line_info_prolog_end");
575                 emit_label("line_info_prolog_begin");
576                 emit_int8(1); /* len of smallest instruction TODO: query from backend */
577                 emit_int8(1); /* default is statement */
578                 emit_int8(246); /* line base */
579                 emit_int8(245); /* line range */
580                 emit_int8(10); /* opcode base */
581
582                 emit_uleb128(0);
583                 emit_uleb128(1);
584                 emit_uleb128(1);
585                 emit_uleb128(1);
586                 emit_uleb128(1);
587                 emit_uleb128(0);
588                 emit_uleb128(0);
589                 emit_uleb128(0);
590                 emit_uleb128(1);
591
592                 /* include directory list */
593                 emit_string("/foo/bar");
594                 emit_int8(0);
595
596                 /* file list */
597                 for (i = 0; i < ARR_LEN(env->file_list); ++i) {
598                         emit_string(env->file_list[0]);
599                         emit_uleb128(1); /* directory */
600                         emit_uleb128(0); /* modification time */
601                         emit_uleb128(0); /* file length */
602                 }
603                 emit_int8(0);
604
605                 emit_label("line_info_prolog_end");
606
607                 /* TODO: put the line_info program here */
608
609                 emit_label("line_info_end");
610         }
611 }
612
613 static void emit_pubnames(dwarf_t *env)
614 {
615         size_t i;
616
617         be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
618
619         emit_size("pubnames_begin", "pubnames_end");
620         emit_label("pubnames_begin");
621
622         emit_int16(2); /* version */
623         emit_size("info_section_begin", "info_begin");
624         emit_size("compile_unit_begin", "compile_unit_end");
625
626         for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
627                 const ir_entity *entity = env->pubnames_list[i];
628                 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
629                                  be_gas_get_private_prefix(),
630                                  get_entity_nr(entity), be_gas_get_private_prefix());
631                 emit_string(get_entity_name(entity));
632         }
633         emit_int32(0);
634
635         emit_label("pubnames_end");
636 }
637
638 static void dwarf_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
639 {
640         dwarf_t  *const env = (dwarf_t*)h;
641         src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
642         unsigned        filenum;
643
644         if (!loc.file)
645                 return;
646
647         filenum = insert_file(env, loc.file);
648         be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
649         be_emit_write_line();
650 }
651
652 static bool is_extern_entity(const ir_entity *entity)
653 {
654         ir_visited_t visibility = get_entity_visibility(entity);
655         return visibility == ir_visibility_default
656             || visibility == ir_visibility_external;
657 }
658
659 static void emit_entity_label(const ir_entity *entity)
660 {
661         be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
662                          get_entity_nr(entity));
663         be_emit_write_line();
664 }
665
666 static void register_dbginfo_attributes(void)
667 {
668         register_attribute(DW_AT_decl_file, DW_FORM_udata);
669         register_attribute(DW_AT_decl_line, DW_FORM_udata);
670 }
671
672 /**
673  * emits values for DW_AT_decl_file then DW_AT_decl_line
674  */
675 static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
676 {
677         src_loc_t const loc  = ir_retrieve_dbg_info(dbgi);
678         unsigned  const file = loc.file ? insert_file(env, loc.file) : 0;
679         emit_uleb128(file);
680         emit_uleb128(loc.line);
681 }
682
683 static void emit_subprogram_abbrev(void)
684 {
685         begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
686         register_attribute(DW_AT_name,      DW_FORM_string);
687         register_dbginfo_attributes();
688         //register_attribute(DW_AT_prototyped, DW_FORM_flag);
689         //register_attribute(DW_AT_type,       DW_FORM_ref4);
690         register_attribute(DW_AT_external,  DW_FORM_flag);
691         register_attribute(DW_AT_low_pc,    DW_FORM_addr);
692         register_attribute(DW_AT_high_pc,   DW_FORM_addr);
693         //register_attribute(DW_AT_frame_base, DW_FORM_block1);
694         end_abbrev();
695 }
696
697 /**
698  * dump the drwarf for a method begin
699  */
700 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
701 {
702         dwarf_t *env = (dwarf_t*)handle;
703
704         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
705
706         emit_entity_label(entity);
707         emit_uleb128(DW_TAG_subprogram);
708         emit_string(get_entity_ld_name(entity));
709         emit_dbginfo(env, get_entity_dbg_info(entity));
710         emit_int8(is_extern_entity(entity));
711         emit_ref(entity);
712         be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
713                          get_entity_ld_name(entity));
714
715         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
716
717         env->cur_ent = entity;
718 }
719
720 /**
721  * dump the drwarf for a method end
722  */
723 static void dwarf_method_end(dbg_handle *handle)
724 {
725         dwarf_t *env = (dwarf_t*)handle;
726         const ir_entity *entity = env->cur_ent;
727
728         be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
729                          get_entity_ld_name(entity));
730 }
731
732 static void dwarf_types(dbg_handle *handle)
733 {
734         (void)handle;
735 }
736
737 static void emit_type(dwarf_t *env, ir_type *type);
738
739 static void emit_base_type_abbrev(void)
740 {
741         begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
742         register_attribute(DW_AT_encoding,  DW_FORM_data1);
743         register_attribute(DW_AT_byte_size, DW_FORM_data1);
744         register_attribute(DW_AT_name,      DW_FORM_string);
745         end_abbrev();
746 }
747
748 static void emit_type_label(const ir_type *type)
749 {
750         be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
751         be_emit_write_line();
752 }
753
754 static void emit_type_address(const ir_type *type)
755 {
756         be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
757                          be_gas_get_private_prefix(),
758                          get_type_nr(type), be_gas_get_private_prefix());
759         be_emit_write_line();
760 }
761
762 static void emit_base_type(const ir_type *type)
763 {
764         char buf[128];
765         ir_mode *mode = get_type_mode(type);
766         ir_print_type(buf, sizeof(buf), type);
767
768         emit_type_label(type);
769         emit_uleb128(DW_TAG_base_type);
770         if (mode_is_int(mode)) {
771                 /* bool hack */
772                 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
773                         emit_int8(DW_ATE_boolean);
774                 } else {
775                         emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
776                 }
777         } else if (mode_is_reference(mode)) {
778                 emit_int8(DW_ATE_address);
779         } else if (mode_is_float(mode)) {
780                 emit_int8(DW_ATE_float);
781         } else {
782                 panic("mode not implemented yet");
783         }
784         emit_int8(get_mode_size_bytes(mode));
785         emit_string(buf);
786 }
787
788 static void emit_pointer_type_abbrev(void)
789 {
790         begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
791         register_attribute(DW_AT_type,      DW_FORM_ref4);
792         register_attribute(DW_AT_byte_size, DW_FORM_data1);
793         end_abbrev();
794
795         /* for void* pointer s*/
796         begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
797         register_attribute(DW_AT_byte_size, DW_FORM_data1);
798         end_abbrev();
799 }
800
801 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
802 {
803         ir_type *points_to = get_pointer_points_to_type(type);
804         unsigned size      = get_type_size_bytes(type);
805         assert(size < 256);
806
807         if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
808                 emit_type(env, points_to);
809
810                 emit_type_label(type);
811                 emit_uleb128(DW_TAG_pointer_type);
812                 emit_type_address(points_to);
813         } else {
814                 emit_type_label(type);
815                 emit_uleb128(abbrev_void_pointer_type);
816         }
817         emit_int8(size);
818 }
819
820 static void emit_array_type_abbrev(void)
821 {
822         begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
823         register_attribute(DW_AT_type, DW_FORM_ref4);
824         end_abbrev();
825
826         begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
827         register_attribute(DW_AT_upper_bound, DW_FORM_udata);
828         end_abbrev();
829 }
830
831 static void emit_array_type(dwarf_t *env, const ir_type *type)
832 {
833         ir_type *element_type = get_array_element_type(type);
834
835         if (get_array_n_dimensions(type) != 1)
836                 panic("dwarf: multidimensional arrays no supported yet");
837
838         emit_type(env, element_type);
839
840         emit_type_label(type);
841         emit_uleb128(DW_TAG_array_type);
842         emit_type_address(element_type);
843
844         if (has_array_upper_bound(type, 0)) {
845                 int bound = get_array_upper_bound_int(type, 0);
846                 emit_uleb128(DW_TAG_subrange_type);
847                 emit_uleb128(bound);
848         }
849
850         emit_uleb128(0);
851 }
852
853 static void emit_compound_type_abbrev(void)
854 {
855         begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
856         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
857         // TODO register_dbginfo_attributes();
858         end_abbrev();
859
860         begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
861         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
862         // TODO register_dbginfo_attributes();
863         end_abbrev();
864
865         begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
866         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
867         // TODO register_dbginfo_attributes();
868         end_abbrev();
869
870         begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
871         register_attribute(DW_AT_type,                 DW_FORM_ref4);
872         register_attribute(DW_AT_name,                 DW_FORM_string);
873         register_dbginfo_attributes();
874         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
875         end_abbrev();
876
877         begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
878         register_attribute(DW_AT_byte_size,            DW_FORM_udata);
879         register_attribute(DW_AT_bit_size,             DW_FORM_udata);
880         register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
881         register_attribute(DW_AT_type,                 DW_FORM_ref4);
882         register_attribute(DW_AT_name,                 DW_FORM_string);
883         register_dbginfo_attributes();
884         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
885         end_abbrev();
886 }
887
888 static void emit_op_plus_uconst(unsigned value)
889 {
890         emit_int8(DW_OP_plus_uconst);
891         emit_uleb128(value);
892 }
893
894 static void emit_compound_type(dwarf_t *env, const ir_type *type)
895 {
896         size_t i;
897         size_t n_members = get_compound_n_members(type);
898
899         for (i = 0; i < n_members; ++i) {
900                 ir_entity *member      = get_compound_member(type, i);
901                 ir_type   *member_type = get_entity_type(member);
902                 if (is_Primitive_type(member_type)) {
903                         ir_type *base = get_primitive_base_type(member_type);
904                         if (base != NULL)
905                                 member_type = base;
906                 }
907                 emit_type(env, member_type);
908         }
909
910         emit_type_label(type);
911         if (is_Struct_type(type)) {
912                 emit_uleb128(DW_TAG_structure_type);
913         } else if (is_Union_type(type)) {
914                 emit_uleb128(DW_TAG_union_type);
915         } else {
916                 assert(is_Class_type(type));
917                 emit_uleb128(DW_TAG_class_type);
918         }
919         emit_uleb128(get_type_size_bytes(type));
920         for (i = 0; i < n_members; ++i) {
921                 ir_entity *member      = get_compound_member(type, i);
922                 ir_type   *member_type = get_entity_type(member);
923                 int        offset      = get_entity_offset(member);
924                 ir_type   *base;
925
926                 if (is_Primitive_type(member_type) &&
927                     (base = get_primitive_base_type(member_type))) {
928                     unsigned bit_offset = get_entity_offset_bits_remainder(member);
929                     unsigned base_size  = get_type_size_bytes(base);
930                     ir_mode *mode       = get_type_mode(member_type);
931                     unsigned bit_size   = get_mode_size_bits(mode);
932
933                         bit_offset = base_size*8 - bit_offset - bit_size;
934
935                         emit_uleb128(abbrev_bitfield_member);
936                         emit_uleb128(base_size);
937                         emit_uleb128(bit_size);
938                         emit_uleb128(bit_offset);
939                         member_type = base;
940                 } else {
941                         emit_uleb128(DW_TAG_member);
942                 }
943
944                 emit_type_address(member_type);
945                 emit_string(get_entity_name(member));
946                 emit_dbginfo(env, get_entity_dbg_info(member));
947                 assert(offset >= 0);
948                 emit_int8(1 + get_uleb128_size(offset));
949                 emit_op_plus_uconst(offset);
950         }
951
952         emit_int8(0);
953 }
954
955 static void emit_subroutine_type_abbrev(void)
956 {
957         begin_abbrev(DW_TAG_subroutine_type,
958                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
959         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
960         register_attribute(DW_AT_type,         DW_FORM_ref4);
961         end_abbrev();
962
963         begin_abbrev(abbrev_void_subroutine_type,
964                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
965         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
966         end_abbrev();
967
968         begin_abbrev(abbrev_unnamed_formal_parameter,
969                      DW_TAG_formal_parameter, DW_CHILDREN_no);
970         register_attribute(DW_AT_type,  DW_FORM_ref4);
971         end_abbrev();
972 }
973
974 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
975 {
976         size_t n_params = get_method_n_params(type);
977         size_t n_ress   = get_method_n_ress(type);
978         size_t i;
979         for (i = 0; i < n_params; ++i) {
980                 ir_type *param_type = get_method_param_type(type, i);
981                 emit_type(env, param_type);
982         }
983         for (i = 0; i < n_ress; ++i) {
984                 ir_type *res_type = get_method_res_type(type, i);
985                 emit_type(env, res_type);
986         }
987
988         emit_type_label(type);
989         emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
990         emit_int8(1); /* prototyped */
991         if (n_ress > 0) {
992                 /* dwarf only supports 1 return type */
993                 ir_type *res_type = get_method_res_type(type, 0);
994                 emit_type_address(res_type);
995         }
996
997         for (i = 0; i < n_params; ++i) {
998                 ir_type *param_type = get_method_param_type(type, i);
999                 emit_uleb128(abbrev_unnamed_formal_parameter);
1000                 emit_type_address(param_type);
1001         }
1002         emit_int8(0);
1003 }
1004
1005 static void emit_type(dwarf_t *env, ir_type *type)
1006 {
1007         if (pset_new_insert(&env->emitted_types, type))
1008                 return;
1009
1010         switch (get_type_tpop_code(type)) {
1011         case tpo_primitive: emit_base_type(type);            break;
1012         case tpo_pointer:   emit_pointer_type(env, type);    break;
1013         case tpo_array:     emit_array_type(env, type);      break;
1014         case tpo_class:
1015         case tpo_struct:
1016         case tpo_union:     emit_compound_type(env, type);   break;
1017         case tpo_method:    emit_subroutine_type(env, type); break;
1018         default:
1019                 panic("bedwarf: type %+F not implemented yet", type);
1020         }
1021 }
1022
1023 static void emit_op_addr(const ir_entity *entity)
1024 {
1025         emit_int8(DW_OP_addr);
1026         be_emit_cstring("\t.long ");
1027         be_gas_emit_entity(entity);
1028         be_emit_char('\n');
1029         be_emit_write_line();
1030 }
1031
1032 static void emit_variable_abbrev(void)
1033 {
1034         begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
1035         register_attribute(DW_AT_name,      DW_FORM_string);
1036         register_attribute(DW_AT_type,      DW_FORM_ref4);
1037         register_attribute(DW_AT_external,  DW_FORM_flag);
1038         register_dbginfo_attributes();
1039         register_attribute(DW_AT_location,  DW_FORM_block1);
1040         end_abbrev();
1041 }
1042
1043 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
1044 {
1045         dwarf_t  *env  = (dwarf_t*) handle;
1046         ir_type  *type = get_entity_type(entity);
1047
1048         if (get_entity_ld_name(entity)[0] == '\0')
1049                 return;
1050
1051         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1052
1053         emit_type(env, type);
1054
1055         emit_entity_label(entity);
1056         emit_uleb128(DW_TAG_variable);
1057         emit_string(get_entity_ld_name(entity));
1058         emit_type_address(type);
1059         emit_int8(is_extern_entity(entity));
1060         emit_dbginfo(env, get_entity_dbg_info(entity));
1061         /* DW_AT_location */
1062         emit_int8(5); /* block length */
1063         emit_op_addr(entity);
1064
1065         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
1066 }
1067
1068 static void emit_compile_unit_abbrev(void)
1069 {
1070         begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
1071         register_attribute(DW_AT_stmt_list, DW_FORM_data4);
1072         register_attribute(DW_AT_producer,  DW_FORM_string);
1073         register_attribute(DW_AT_name,      DW_FORM_string);
1074         if (language != 0)
1075                 register_attribute(DW_AT_language,  DW_FORM_data2);
1076         if (comp_dir != NULL)
1077                 register_attribute(DW_AT_comp_dir,  DW_FORM_string);
1078         end_abbrev();
1079 }
1080
1081 static void emit_abbrev(void)
1082 {
1083         /* create abbreviation for compile_unit */
1084         be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
1085
1086         emit_label("abbrev_begin");
1087
1088         emit_compile_unit_abbrev();
1089         emit_variable_abbrev();
1090         emit_subprogram_abbrev();
1091         emit_base_type_abbrev();
1092         emit_pointer_type_abbrev();
1093         emit_array_type_abbrev();
1094         emit_compound_type_abbrev();
1095         emit_subroutine_type_abbrev();
1096         emit_uleb128(0);
1097 }
1098
1099 /**
1100  * start a new source object (compilation unit)
1101  */
1102 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
1103 {
1104         (void) handle;
1105
1106         emit_abbrev();
1107
1108         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1109         emit_label("info_section_begin");
1110         emit_label("info_begin");
1111
1112         /* length of compilation unit info */
1113         emit_size("compile_unit_begin", "compile_unit_end");
1114         emit_label("compile_unit_begin");
1115         emit_int16(2);   /* dwarf version */
1116         emit_address("abbrev_begin");
1117         emit_int8(4);    /* pointer size */
1118
1119         /* compile_unit die */
1120         emit_uleb128(DW_TAG_compile_unit);
1121         emit_address("line_section_begin");
1122         emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
1123                            ir_get_version_minor(),
1124                            ir_get_version_revision());
1125         emit_string(filename);
1126         if (language != 0)
1127                 emit_int16(DW_LANG_C_plus_plus);
1128         if (comp_dir != NULL)
1129                 emit_string(comp_dir);
1130 }
1131
1132 static void dwarf_unit_end(dbg_handle *handle)
1133 {
1134         dwarf_t *env = (dwarf_t*)handle;
1135
1136         be_gas_emit_switch_section(GAS_SECTION_TEXT);
1137         emit_label("section_end");
1138
1139         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1140         emit_uleb128(0); /* end of compile_unit DIE */
1141
1142         emit_label("compile_unit_end");
1143
1144         emit_line_info(env);
1145         emit_pubnames(env);
1146 }
1147
1148 /**
1149  * Close the drwarf handler.
1150  */
1151 static void dwarf_close(dbg_handle *handle)
1152 {
1153         dwarf_t *h = (dwarf_t *)handle;
1154         pmap_destroy(h->file_map);
1155         DEL_ARR_F(h->file_list);
1156         DEL_ARR_F(h->pubnames_list);
1157         pset_new_destroy(&h->emitted_types);
1158         free(h);
1159 }
1160
1161 /** The drwarf operations. */
1162 static const debug_ops dwarf_ops = {
1163         dwarf_close,
1164         dwarf_unit_begin,
1165         dwarf_unit_end,
1166         dwarf_method_begin,
1167         dwarf_method_end,
1168         dwarf_types,
1169         dwarf_variable,
1170         dwarf_set_dbg_info
1171 };
1172
1173 /* Opens a drwarf handler */
1174 static dbg_handle *be_dwarf_open(void)
1175 {
1176         dwarf_t *h = XMALLOCZ(dwarf_t);
1177
1178         h->base.ops      = &dwarf_ops;
1179         h->file_map      = pmap_create();
1180         h->file_list     = NEW_ARR_F(const char*, 0);
1181         h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
1182         pset_new_init(&h->emitted_types);
1183
1184         return &h->base;
1185 }
1186
1187 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
1188 void be_init_dwarf(void)
1189 {
1190         be_register_dbgout_module("dwarf", be_dwarf_open);
1191 }
1192
1193 void be_dwarf_set_source_language(dwarf_source_language new_language)
1194 {
1195         language = new_language;
1196 }
1197
1198 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
1199 {
1200         comp_dir = new_comp_dir;
1201 }