62d7cfc8d688a126ad730f2771a569ba92388322
[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 *env = (dwarf_t*) h;
641         const char *filename;
642         unsigned    lineno;
643         unsigned    column = 0;
644         unsigned    filenum;
645
646         if (dbgi == NULL)
647                 return;
648
649         filename = ir_retrieve_dbg_info(dbgi, &lineno);
650         if (filename == NULL)
651                 return;
652         filenum = insert_file(env, filename);
653
654         be_emit_irprintf("\t.loc %u %u %u\n", filenum, lineno, column);
655         be_emit_write_line();
656 }
657
658 static bool is_extern_entity(const ir_entity *entity)
659 {
660         ir_visited_t visibility = get_entity_visibility(entity);
661         return visibility == ir_visibility_default
662             || visibility == ir_visibility_external;
663 }
664
665 static void emit_entity_label(const ir_entity *entity)
666 {
667         be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
668                          get_entity_nr(entity));
669         be_emit_write_line();
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         const char *filename;
678         unsigned line;
679         unsigned file;
680
681         if (dbgi == NULL) {
682                 emit_uleb128(0);
683                 emit_uleb128(0);
684                 return;
685         }
686         filename = ir_retrieve_dbg_info(dbgi, &line);
687         if (filename == NULL) {
688                 emit_uleb128(0);
689                 emit_uleb128(0);
690                 return;
691         }
692
693         file = insert_file(env, filename);
694         emit_uleb128(file);
695         emit_uleb128(line);
696 }
697
698 static void emit_subprogram_abbrev(void)
699 {
700         begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
701         register_attribute(DW_AT_name,      DW_FORM_string);
702         register_attribute(DW_AT_decl_file, DW_FORM_udata);
703         register_attribute(DW_AT_decl_line, DW_FORM_udata);
704         //register_attribute(DW_AT_prototyped, DW_FORM_flag);
705         //register_attribute(DW_AT_type,       DW_FORM_ref4);
706         register_attribute(DW_AT_external,  DW_FORM_flag);
707         register_attribute(DW_AT_low_pc,    DW_FORM_addr);
708         register_attribute(DW_AT_high_pc,   DW_FORM_addr);
709         //register_attribute(DW_AT_frame_base, DW_FORM_block1);
710         end_abbrev();
711 }
712
713 /**
714  * dump the drwarf for a method begin
715  */
716 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
717 {
718         dwarf_t *env = (dwarf_t*)handle;
719
720         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
721
722         emit_entity_label(entity);
723         emit_uleb128(DW_TAG_subprogram);
724         emit_string(get_entity_ld_name(entity));
725         emit_dbginfo(env, get_entity_dbg_info(entity));
726         emit_int8(is_extern_entity(entity));
727         emit_ref(entity);
728         be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
729                          get_entity_ld_name(entity));
730
731         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
732
733         env->cur_ent = entity;
734 }
735
736 /**
737  * dump the drwarf for a method end
738  */
739 static void dwarf_method_end(dbg_handle *handle)
740 {
741         dwarf_t *env = (dwarf_t*)handle;
742         const ir_entity *entity = env->cur_ent;
743
744         be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
745                          get_entity_ld_name(entity));
746 }
747
748 static void dwarf_types(dbg_handle *handle)
749 {
750         (void)handle;
751 }
752
753 static void emit_type(dwarf_t *env, ir_type *type);
754
755 static void emit_base_type_abbrev(void)
756 {
757         begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
758         register_attribute(DW_AT_encoding,  DW_FORM_data1);
759         register_attribute(DW_AT_byte_size, DW_FORM_data1);
760         register_attribute(DW_AT_name,      DW_FORM_string);
761         end_abbrev();
762 }
763
764 static void emit_type_label(const ir_type *type)
765 {
766         be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
767         be_emit_write_line();
768 }
769
770 static void emit_type_address(const ir_type *type)
771 {
772         be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
773                          be_gas_get_private_prefix(),
774                          get_type_nr(type), be_gas_get_private_prefix());
775         be_emit_write_line();
776 }
777
778 static void emit_base_type(const ir_type *type)
779 {
780         char buf[128];
781         ir_mode *mode = get_type_mode(type);
782         ir_print_type(buf, sizeof(buf), type);
783
784         emit_type_label(type);
785         emit_uleb128(DW_TAG_base_type);
786         if (mode_is_int(mode)) {
787                 /* bool hack */
788                 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
789                         emit_int8(DW_ATE_boolean);
790                 } else {
791                         emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
792                 }
793         } else if (mode_is_reference(mode)) {
794                 emit_int8(DW_ATE_address);
795         } else if (mode_is_float(mode)) {
796                 emit_int8(DW_ATE_float);
797         } else {
798                 panic("mode not implemented yet");
799         }
800         emit_int8(get_mode_size_bytes(mode));
801         emit_string(buf);
802 }
803
804 static void emit_pointer_type_abbrev(void)
805 {
806         begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
807         register_attribute(DW_AT_type,      DW_FORM_ref4);
808         register_attribute(DW_AT_byte_size, DW_FORM_data1);
809         end_abbrev();
810
811         /* for void* pointer s*/
812         begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
813         register_attribute(DW_AT_byte_size, DW_FORM_data1);
814         end_abbrev();
815 }
816
817 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
818 {
819         ir_type *points_to = get_pointer_points_to_type(type);
820         unsigned size      = get_type_size_bytes(type);
821         assert(size < 256);
822
823         if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
824                 emit_type(env, points_to);
825
826                 emit_type_label(type);
827                 emit_uleb128(DW_TAG_pointer_type);
828                 emit_type_address(points_to);
829         } else {
830                 emit_type_label(type);
831                 emit_uleb128(abbrev_void_pointer_type);
832         }
833         emit_int8(size);
834 }
835
836 static void emit_array_type_abbrev(void)
837 {
838         begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
839         register_attribute(DW_AT_type, DW_FORM_ref4);
840         end_abbrev();
841
842         begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
843         register_attribute(DW_AT_upper_bound, DW_FORM_udata);
844         end_abbrev();
845 }
846
847 static void emit_array_type(dwarf_t *env, const ir_type *type)
848 {
849         ir_type *element_type = get_array_element_type(type);
850
851         if (get_array_n_dimensions(type) != 1)
852                 panic("dwarf: multidimensional arrays no supported yet");
853
854         emit_type(env, element_type);
855
856         emit_type_label(type);
857         emit_uleb128(DW_TAG_array_type);
858         emit_type_address(element_type);
859
860         if (has_array_upper_bound(type, 0)) {
861                 int bound = get_array_upper_bound_int(type, 0);
862                 emit_uleb128(DW_TAG_subrange_type);
863                 emit_uleb128(bound);
864         }
865
866         emit_uleb128(0);
867 }
868
869 static void emit_compound_type_abbrev(void)
870 {
871         begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
872         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
873         //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
874         //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
875         end_abbrev();
876
877         begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
878         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
879         //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
880         //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
881         end_abbrev();
882
883         begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
884         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
885         //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
886         //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
887         end_abbrev();
888
889         begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
890         register_attribute(DW_AT_type,                 DW_FORM_ref4);
891         register_attribute(DW_AT_name,                 DW_FORM_string);
892         register_attribute(DW_AT_decl_file,            DW_FORM_udata);
893         register_attribute(DW_AT_decl_line,            DW_FORM_udata);
894         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
895         end_abbrev();
896
897         begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
898         register_attribute(DW_AT_byte_size,            DW_FORM_udata);
899         register_attribute(DW_AT_bit_size,             DW_FORM_udata);
900         register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
901         register_attribute(DW_AT_type,                 DW_FORM_ref4);
902         register_attribute(DW_AT_name,                 DW_FORM_string);
903         register_attribute(DW_AT_decl_file,            DW_FORM_udata);
904         register_attribute(DW_AT_decl_line,            DW_FORM_udata);
905         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
906         end_abbrev();
907 }
908
909 static void emit_op_plus_uconst(unsigned value)
910 {
911         emit_int8(DW_OP_plus_uconst);
912         emit_uleb128(value);
913 }
914
915 static void emit_compound_type(dwarf_t *env, const ir_type *type)
916 {
917         size_t i;
918         size_t n_members = get_compound_n_members(type);
919
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                 if (is_Primitive_type(member_type)) {
924                         ir_type *base = get_primitive_base_type(member_type);
925                         if (base != NULL)
926                                 member_type = base;
927                 }
928                 emit_type(env, member_type);
929         }
930
931         emit_type_label(type);
932         if (is_Struct_type(type)) {
933                 emit_uleb128(DW_TAG_structure_type);
934         } else if (is_Union_type(type)) {
935                 emit_uleb128(DW_TAG_union_type);
936         } else {
937                 assert(is_Class_type(type));
938                 emit_uleb128(DW_TAG_class_type);
939         }
940         emit_uleb128(get_type_size_bytes(type));
941         for (i = 0; i < n_members; ++i) {
942                 ir_entity *member      = get_compound_member(type, i);
943                 ir_type   *member_type = get_entity_type(member);
944                 int        offset      = get_entity_offset(member);
945                 ir_type   *base;
946
947                 if (is_Primitive_type(member_type) &&
948                     (base = get_primitive_base_type(member_type))) {
949                     unsigned bit_offset = get_entity_offset_bits_remainder(member);
950                     unsigned base_size  = get_type_size_bytes(base);
951                     ir_mode *mode       = get_type_mode(member_type);
952                     unsigned bit_size   = get_mode_size_bits(mode);
953
954                         bit_offset = base_size*8 - bit_offset - bit_size;
955
956                         emit_uleb128(abbrev_bitfield_member);
957                         emit_uleb128(base_size);
958                         emit_uleb128(bit_size);
959                         emit_uleb128(bit_offset);
960                         member_type = base;
961                 } else {
962                         emit_uleb128(DW_TAG_member);
963                 }
964
965                 emit_type_address(member_type);
966                 emit_string(get_entity_name(member));
967                 emit_dbginfo(env, get_entity_dbg_info(member));
968                 assert(offset >= 0);
969                 emit_int8(1 + get_uleb128_size(offset));
970                 emit_op_plus_uconst(offset);
971         }
972
973         emit_int8(0);
974 }
975
976 static void emit_subroutine_type_abbrev(void)
977 {
978         begin_abbrev(DW_TAG_subroutine_type,
979                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
980         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
981         register_attribute(DW_AT_type,         DW_FORM_ref4);
982         end_abbrev();
983
984         begin_abbrev(abbrev_void_subroutine_type,
985                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
986         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
987         end_abbrev();
988
989         begin_abbrev(abbrev_unnamed_formal_parameter,
990                      DW_TAG_formal_parameter, DW_CHILDREN_no);
991         register_attribute(DW_AT_type,  DW_FORM_ref4);
992         end_abbrev();
993 }
994
995 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
996 {
997         size_t n_params = get_method_n_params(type);
998         size_t n_ress   = get_method_n_ress(type);
999         size_t i;
1000         for (i = 0; i < n_params; ++i) {
1001                 ir_type *param_type = get_method_param_type(type, i);
1002                 emit_type(env, param_type);
1003         }
1004         for (i = 0; i < n_ress; ++i) {
1005                 ir_type *res_type = get_method_res_type(type, i);
1006                 emit_type(env, res_type);
1007         }
1008
1009         emit_type_label(type);
1010         emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
1011         emit_int8(1); /* prototyped */
1012         if (n_ress > 0) {
1013                 /* dwarf only supports 1 return type */
1014                 ir_type *res_type = get_method_res_type(type, 0);
1015                 emit_type_address(res_type);
1016         }
1017
1018         for (i = 0; i < n_params; ++i) {
1019                 ir_type *param_type = get_method_param_type(type, i);
1020                 emit_uleb128(abbrev_unnamed_formal_parameter);
1021                 emit_type_address(param_type);
1022         }
1023         emit_int8(0);
1024 }
1025
1026 static void emit_type(dwarf_t *env, ir_type *type)
1027 {
1028         if (pset_new_insert(&env->emitted_types, type))
1029                 return;
1030
1031         switch (get_type_tpop_code(type)) {
1032         case tpo_primitive: emit_base_type(type);            break;
1033         case tpo_pointer:   emit_pointer_type(env, type);    break;
1034         case tpo_array:     emit_array_type(env, type);      break;
1035         case tpo_class:
1036         case tpo_struct:
1037         case tpo_union:     emit_compound_type(env, type);   break;
1038         case tpo_method:    emit_subroutine_type(env, type); break;
1039         default:
1040                 panic("bedwarf: type %+F not implemented yet", type);
1041         }
1042 }
1043
1044 static void emit_op_addr(const ir_entity *entity)
1045 {
1046         emit_int8(DW_OP_addr);
1047         be_emit_cstring("\t.long ");
1048         be_gas_emit_entity(entity);
1049         be_emit_char('\n');
1050         be_emit_write_line();
1051 }
1052
1053 static void emit_variable_abbrev(void)
1054 {
1055         begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
1056         register_attribute(DW_AT_name,      DW_FORM_string);
1057         register_attribute(DW_AT_type,      DW_FORM_ref4);
1058         register_attribute(DW_AT_external,  DW_FORM_flag);
1059         register_attribute(DW_AT_decl_file, DW_FORM_udata);
1060         register_attribute(DW_AT_decl_line, DW_FORM_udata);
1061         register_attribute(DW_AT_location,  DW_FORM_block1);
1062         end_abbrev();
1063 }
1064
1065 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
1066 {
1067         dwarf_t  *env  = (dwarf_t*) handle;
1068         ir_type  *type = get_entity_type(entity);
1069
1070         if (get_entity_ld_name(entity)[0] == '\0')
1071                 return;
1072
1073         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1074
1075         emit_type(env, type);
1076
1077         emit_entity_label(entity);
1078         emit_uleb128(DW_TAG_variable);
1079         emit_string(get_entity_ld_name(entity));
1080         emit_type_address(type);
1081         emit_int8(is_extern_entity(entity));
1082         emit_dbginfo(env, get_entity_dbg_info(entity));
1083         /* DW_AT_location */
1084         emit_int8(5); /* block length */
1085         emit_op_addr(entity);
1086
1087         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
1088 }
1089
1090 static void emit_compile_unit_abbrev(void)
1091 {
1092         begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
1093         register_attribute(DW_AT_stmt_list, DW_FORM_data4);
1094         register_attribute(DW_AT_producer,  DW_FORM_string);
1095         register_attribute(DW_AT_name,      DW_FORM_string);
1096         if (language != 0)
1097                 register_attribute(DW_AT_language,  DW_FORM_data2);
1098         if (comp_dir != NULL)
1099                 register_attribute(DW_AT_comp_dir,  DW_FORM_string);
1100         end_abbrev();
1101 }
1102
1103 static void emit_abbrev(void)
1104 {
1105         /* create abbreviation for compile_unit */
1106         be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
1107
1108         emit_label("abbrev_begin");
1109
1110         emit_compile_unit_abbrev();
1111         emit_variable_abbrev();
1112         emit_subprogram_abbrev();
1113         emit_base_type_abbrev();
1114         emit_pointer_type_abbrev();
1115         emit_array_type_abbrev();
1116         emit_compound_type_abbrev();
1117         emit_subroutine_type_abbrev();
1118         emit_uleb128(0);
1119 }
1120
1121 /**
1122  * start a new source object (compilation unit)
1123  */
1124 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
1125 {
1126         (void) handle;
1127
1128         emit_abbrev();
1129
1130         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1131         emit_label("info_section_begin");
1132         emit_label("info_begin");
1133
1134         /* length of compilation unit info */
1135         emit_size("compile_unit_begin", "compile_unit_end");
1136         emit_label("compile_unit_begin");
1137         emit_int16(2);   /* dwarf version */
1138         emit_address("abbrev_begin");
1139         emit_int8(4);    /* pointer size */
1140
1141         /* compile_unit die */
1142         emit_uleb128(DW_TAG_compile_unit);
1143         emit_address("line_section_begin");
1144         emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
1145                            ir_get_version_minor(),
1146                            ir_get_version_revision());
1147         emit_string(filename);
1148         if (language != 0)
1149                 emit_int16(DW_LANG_C_plus_plus);
1150         if (comp_dir != NULL)
1151                 emit_string(comp_dir);
1152 }
1153
1154 static void dwarf_unit_end(dbg_handle *handle)
1155 {
1156         dwarf_t *env = (dwarf_t*)handle;
1157
1158         be_gas_emit_switch_section(GAS_SECTION_TEXT);
1159         emit_label("section_end");
1160
1161         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1162         emit_uleb128(0); /* end of compile_unit DIE */
1163
1164         emit_label("compile_unit_end");
1165
1166         emit_line_info(env);
1167         emit_pubnames(env);
1168 }
1169
1170 /**
1171  * Close the drwarf handler.
1172  */
1173 static void dwarf_close(dbg_handle *handle)
1174 {
1175         dwarf_t *h = (dwarf_t *)handle;
1176         pmap_destroy(h->file_map);
1177         DEL_ARR_F(h->file_list);
1178         DEL_ARR_F(h->pubnames_list);
1179         pset_new_destroy(&h->emitted_types);
1180         free(h);
1181 }
1182
1183 /** The drwarf operations. */
1184 static const debug_ops dwarf_ops = {
1185         dwarf_close,
1186         dwarf_unit_begin,
1187         dwarf_unit_end,
1188         dwarf_method_begin,
1189         dwarf_method_end,
1190         dwarf_types,
1191         dwarf_variable,
1192         dwarf_set_dbg_info
1193 };
1194
1195 /* Opens a drwarf handler */
1196 static dbg_handle *be_dwarf_open(void)
1197 {
1198         dwarf_t *h = XMALLOCZ(dwarf_t);
1199
1200         h->base.ops      = &dwarf_ops;
1201         h->file_map      = pmap_create();
1202         h->file_list     = NEW_ARR_F(const char*, 0);
1203         h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
1204         pset_new_init(&h->emitted_types);
1205
1206         return &h->base;
1207 }
1208
1209 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
1210 void be_init_dwarf(void)
1211 {
1212         be_register_dbgout_module("dwarf", be_dwarf_open);
1213 }
1214
1215 void be_dwarf_set_source_language(dwarf_source_language new_language)
1216 {
1217         language = new_language;
1218 }
1219
1220 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
1221 {
1222         comp_dir = new_comp_dir;
1223 }