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