Initial support for dwarf debug info
[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 unsigned insert_file(dwarf_t *env, const char *filename)
436 {
437         unsigned num;
438         void    *entry = pmap_get(env->file_map, filename);
439         if (entry != NULL) {
440                 return PTR_TO_INT(entry);
441         }
442         ARR_APP1(const char*, env->file_list, filename);
443         num = (unsigned)ARR_LEN(env->file_list);
444         pmap_insert(env->file_map, filename, INT_TO_PTR(num));
445         /* TODO: quote chars in string */
446         be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
447         return num;
448 }
449
450 static void emit_int32(uint32_t value)
451 {
452         be_emit_irprintf("\t.long %u\n", value);
453         be_emit_write_line();
454 }
455
456 static void emit_int16(uint16_t value)
457 {
458         be_emit_irprintf("\t.short %u\n", value);
459         be_emit_write_line();
460 }
461
462 static void emit_int8(uint8_t value)
463 {
464         be_emit_irprintf("\t.byte %u\n", value);
465         be_emit_write_line();
466 }
467
468 static void emit_uleb128(unsigned value)
469 {
470         be_emit_irprintf("\t.uleb128 0x%x\n", value);
471         be_emit_write_line();
472 }
473
474 static unsigned get_uleb128_size(unsigned value)
475 {
476         unsigned size = 0;
477         do {
478                 value >>= 7;
479                 size += 1;
480         } while (value != 0);
481         return size;
482 }
483
484 static void emit_string(const char *string)
485 {
486         be_emit_irprintf("\t.asciz \"%s\"\n", string);
487         be_emit_write_line();
488 }
489
490 static void emit_ref(const ir_entity *entity)
491 {
492         be_emit_cstring("\t.long ");
493         be_gas_emit_entity(entity);
494         be_emit_char('\n');
495         be_emit_write_line();
496 }
497
498 static void emit_string_printf(const char *fmt, ...)
499 {
500         va_list ap;
501         va_start(ap, fmt);
502         be_emit_cstring("\t.asciz \"");
503         be_emit_irvprintf(fmt, ap);
504         be_emit_cstring("\"\n");
505         va_end(ap);
506
507         be_emit_write_line();
508 }
509
510 static void emit_address(const char *name)
511 {
512         be_emit_cstring("\t.long ");
513         be_emit_string(be_gas_get_private_prefix());
514         be_emit_string(name);
515         be_emit_char('\n');
516         be_emit_write_line();
517 }
518
519 static void emit_size(const char *from_label, const char *to_label)
520 {
521         be_emit_cstring("\t.long ");
522         be_emit_string(be_gas_get_private_prefix());
523         be_emit_string(to_label);
524         be_emit_cstring(" - ");
525         be_emit_string(be_gas_get_private_prefix());
526         be_emit_string(from_label);
527         be_emit_char('\n');
528         be_emit_write_line();
529 }
530
531 static void emit_label(const char *name)
532 {
533         be_emit_string(be_gas_get_private_prefix());
534         be_emit_string(name);
535         be_emit_cstring(":\n");
536         be_emit_write_line();
537 }
538
539 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
540 {
541         emit_uleb128(attribute);
542         emit_uleb128(form);
543 }
544
545 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
546 {
547         emit_uleb128(code);
548         emit_uleb128(tag);
549         emit_int8(children);
550 }
551
552 static void end_abbrev(void)
553 {
554         emit_uleb128(0);
555         emit_uleb128(0);
556 }
557
558 static void emit_line_info(dwarf_t *env)
559 {
560         be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
561
562         emit_label("line_section_begin");
563         /* on elf systems gas handles producing the line info for us, and we
564          * don't have to do anything */
565         if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
566                 size_t i;
567                 emit_size("line_info_begin", "line_info_end");
568
569                 emit_label("line_info_begin");
570                 emit_int16(2); /* version */
571                 emit_size("line_info_prolog_begin", "line_info_prolog_end");
572                 emit_label("line_info_prolog_begin");
573                 emit_int8(1); /* len of smallest instruction TODO: query from backend */
574                 emit_int8(1); /* default is statement */
575                 emit_int8(246); /* line base */
576                 emit_int8(245); /* line range */
577                 emit_int8(10); /* opcode base */
578
579                 emit_uleb128(0);
580                 emit_uleb128(1);
581                 emit_uleb128(1);
582                 emit_uleb128(1);
583                 emit_uleb128(1);
584                 emit_uleb128(0);
585                 emit_uleb128(0);
586                 emit_uleb128(0);
587                 emit_uleb128(1);
588
589                 /* include directory list */
590                 emit_string("/foo/bar");
591                 emit_int8(0);
592
593                 /* file list */
594                 for (i = 0; i < ARR_LEN(env->file_list); ++i) {
595                         emit_string(env->file_list[0]);
596                         emit_uleb128(1); /* directory */
597                         emit_uleb128(0); /* modification time */
598                         emit_uleb128(0); /* file length */
599                 }
600                 emit_int8(0);
601
602                 emit_label("line_info_prolog_end");
603
604                 /* TODO: put the line_info program here */
605
606                 emit_label("line_info_end");
607         }
608 }
609
610 static void emit_pubnames(dwarf_t *env)
611 {
612         size_t i;
613
614         be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
615
616         emit_size("pubnames_begin", "pubnames_end");
617         emit_label("pubnames_begin");
618
619         emit_int16(2); /* version */
620         emit_size("info_section_begin", "info_begin");
621         emit_size("compile_unit_begin", "compile_unit_end");
622
623         for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
624                 const ir_entity *entity = env->pubnames_list[i];
625                 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
626                                  be_gas_get_private_prefix(),
627                                  get_entity_nr(entity), be_gas_get_private_prefix());
628                 emit_string(get_entity_name(entity));
629         }
630         emit_int32(0);
631
632         emit_label("pubnames_end");
633 }
634
635 static void dwarf_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
636 {
637         dwarf_t *env = (dwarf_t*) h;
638         const char *filename;
639         unsigned    lineno;
640         unsigned    column = 0;
641         unsigned    filenum;
642
643         if (dbgi == NULL)
644                 return;
645
646         filename = ir_retrieve_dbg_info(dbgi, &lineno);
647         if (filename == NULL)
648                 return;
649         filenum = insert_file(env, filename);
650
651         be_emit_irprintf("\t.loc %u %u %u\n", filenum, lineno, column);
652         be_emit_write_line();
653 }
654
655 static bool is_extern_entity(const ir_entity *entity)
656 {
657         ir_visited_t visibility = get_entity_visibility(entity);
658         return visibility == ir_visibility_default
659             || visibility == ir_visibility_external;
660 }
661
662 static void emit_entity_label(const ir_entity *entity)
663 {
664         be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
665                          get_entity_nr(entity));
666         be_emit_write_line();
667 }
668
669 /**
670  * emits values for DW_AT_decl_file then DW_AT_decl_line
671  */
672 static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
673 {
674         const char *filename;
675         unsigned line;
676         unsigned file;
677
678         if (dbgi == NULL) {
679                 emit_uleb128(0);
680                 emit_uleb128(0);
681                 return;
682         }
683         filename = ir_retrieve_dbg_info(dbgi, &line);
684         if (filename == NULL) {
685                 emit_uleb128(0);
686                 emit_uleb128(0);
687                 return;
688         }
689
690         file = insert_file(env, filename);
691         emit_uleb128(file);
692         emit_uleb128(line);
693 }
694
695 static void emit_subprogram_abbrev(void)
696 {
697         begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
698         register_attribute(DW_AT_name,      DW_FORM_string);
699         register_attribute(DW_AT_decl_file, DW_FORM_udata);
700         register_attribute(DW_AT_decl_line, DW_FORM_udata);
701         //register_attribute(DW_AT_prototyped, DW_FORM_flag);
702         //register_attribute(DW_AT_type,       DW_FORM_ref4);
703         register_attribute(DW_AT_external,  DW_FORM_flag);
704         register_attribute(DW_AT_low_pc,    DW_FORM_addr);
705         register_attribute(DW_AT_high_pc,   DW_FORM_addr);
706         //register_attribute(DW_AT_frame_base, DW_FORM_block1);
707         end_abbrev();
708 }
709
710 /**
711  * dump the drwarf for a method begin
712  */
713 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
714 {
715         dwarf_t *env = (dwarf_t*)handle;
716
717         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
718
719         emit_entity_label(entity);
720         emit_uleb128(DW_TAG_subprogram);
721         emit_string(get_entity_name(entity));
722         emit_dbginfo(env, get_entity_dbg_info(entity));
723         emit_int8(is_extern_entity(entity));
724         emit_ref(entity);
725         be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
726                          get_entity_ld_name(entity));
727
728         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
729
730         env->cur_ent = entity;
731 }
732
733 /**
734  * dump the drwarf for a method end
735  */
736 static void dwarf_method_end(dbg_handle *handle)
737 {
738         dwarf_t *env = (dwarf_t*)handle;
739         const ir_entity *entity = env->cur_ent;
740
741         be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
742                          get_entity_ld_name(entity));
743 }
744
745 static void dwarf_types(dbg_handle *handle)
746 {
747         (void)handle;
748 }
749
750 static void emit_type(dwarf_t *env, ir_type *type);
751
752 static void emit_base_type_abbrev(void)
753 {
754         begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
755         register_attribute(DW_AT_encoding,  DW_FORM_data1);
756         register_attribute(DW_AT_byte_size, DW_FORM_data1);
757         register_attribute(DW_AT_name,      DW_FORM_string);
758         end_abbrev();
759 }
760
761 static void emit_type_label(const ir_type *type)
762 {
763         be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
764         be_emit_write_line();
765 }
766
767 static void emit_type_address(const ir_type *type)
768 {
769         be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
770                          be_gas_get_private_prefix(),
771                          get_type_nr(type), be_gas_get_private_prefix());
772         be_emit_write_line();
773 }
774
775 static void emit_base_type(const ir_type *type)
776 {
777         char buf[128];
778         ir_mode *mode = get_type_mode(type);
779         ir_print_type(buf, sizeof(buf), type);
780
781         emit_type_label(type);
782         emit_uleb128(DW_TAG_base_type);
783         if (mode_is_int(mode)) {
784                 /* bool hack */
785                 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
786                         emit_int8(DW_ATE_boolean);
787                 } else {
788                         emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
789                 }
790         } else if (mode_is_reference(mode)) {
791                 emit_int8(DW_ATE_address);
792         } else if (mode_is_float(mode)) {
793                 emit_int8(DW_ATE_float);
794         } else {
795                 panic("mode not implemented yet");
796         }
797         emit_int8(get_mode_size_bytes(mode));
798         emit_string(buf);
799 }
800
801 static void emit_pointer_type_abbrev(void)
802 {
803         begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
804         register_attribute(DW_AT_type,      DW_FORM_ref4);
805         register_attribute(DW_AT_byte_size, DW_FORM_data1);
806         end_abbrev();
807
808         /* for void* pointer s*/
809         begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
810         register_attribute(DW_AT_byte_size, DW_FORM_data1);
811         end_abbrev();
812 }
813
814 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
815 {
816         ir_type *points_to = get_pointer_points_to_type(type);
817         unsigned size      = get_type_size_bytes(type);
818         assert(size < 256);
819
820         if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
821                 emit_type(env, points_to);
822
823                 emit_type_label(type);
824                 emit_uleb128(DW_TAG_pointer_type);
825                 emit_type_address(points_to);
826         } else {
827                 emit_type_label(type);
828                 emit_uleb128(abbrev_void_pointer_type);
829         }
830         emit_int8(size);
831 }
832
833 static void emit_array_type_abbrev(void)
834 {
835         begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
836         register_attribute(DW_AT_type, DW_FORM_ref4);
837         end_abbrev();
838
839         begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
840         register_attribute(DW_AT_upper_bound, DW_FORM_udata);
841         end_abbrev();
842 }
843
844 static void emit_array_type(dwarf_t *env, const ir_type *type)
845 {
846         ir_type *element_type = get_array_element_type(type);
847
848         if (get_array_n_dimensions(type) != 1)
849                 panic("dwarf: multidimensional arrays no supported yet");
850
851         emit_type(env, element_type);
852
853         emit_type_label(type);
854         emit_uleb128(DW_TAG_array_type);
855         emit_type_address(element_type);
856
857         if (has_array_upper_bound(type, 0)) {
858                 int bound = get_array_upper_bound_int(type, 0);
859                 emit_uleb128(DW_TAG_subrange_type);
860                 emit_uleb128(bound);
861         }
862
863         emit_uleb128(0);
864 }
865
866 static void emit_compound_type_abbrev(void)
867 {
868         begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
869         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
870         //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
871         //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
872         end_abbrev();
873
874         begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
875         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
876         //register_attribute(DW_AT_decl_file,  DW_FORM_udata);
877         //register_attribute(DW_AT_decl_line,  DW_FORM_udata);
878         end_abbrev();
879
880         begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
881         register_attribute(DW_AT_type,                 DW_FORM_ref4);
882         register_attribute(DW_AT_name,                 DW_FORM_string);
883         register_attribute(DW_AT_decl_file,            DW_FORM_udata);
884         register_attribute(DW_AT_decl_line,            DW_FORM_udata);
885         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
886         end_abbrev();
887
888         begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
889         register_attribute(DW_AT_byte_size,            DW_FORM_udata);
890         register_attribute(DW_AT_bit_size,             DW_FORM_udata);
891         register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
892         register_attribute(DW_AT_type,                 DW_FORM_ref4);
893         register_attribute(DW_AT_name,                 DW_FORM_string);
894         register_attribute(DW_AT_decl_file,            DW_FORM_udata);
895         register_attribute(DW_AT_decl_line,            DW_FORM_udata);
896         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
897         end_abbrev();
898 }
899
900 static void emit_op_plus_uconst(unsigned value)
901 {
902         emit_int8(DW_OP_plus_uconst);
903         emit_uleb128(value);
904 }
905
906 static void emit_compound_type(dwarf_t *env, const ir_type *type)
907 {
908         size_t i;
909         size_t n_members = get_compound_n_members(type);
910
911         for (i = 0; i < n_members; ++i) {
912                 ir_entity *member      = get_compound_member(type, i);
913                 ir_type   *member_type = get_entity_type(member);
914                 if (is_Primitive_type(member_type)) {
915                         ir_type *base = get_primitive_base_type(member_type);
916                         if (base != NULL)
917                                 member_type = base;
918                 }
919                 emit_type(env, member_type);
920         }
921
922         emit_type_label(type);
923         if (is_Union_type(type)) {
924                 emit_uleb128(DW_TAG_union_type);
925         } else {
926                 assert(is_Struct_type(type));
927                 emit_uleb128(DW_TAG_structure_type);
928         }
929         emit_uleb128(get_type_size_bytes(type));
930         for (i = 0; i < n_members; ++i) {
931                 ir_entity *member      = get_compound_member(type, i);
932                 ir_type   *member_type = get_entity_type(member);
933                 int        offset      = get_entity_offset(member);
934                 ir_type   *base;
935
936                 if (is_Primitive_type(member_type) &&
937                     (base = get_primitive_base_type(member_type))) {
938                     unsigned bit_offset = get_entity_offset_bits_remainder(member);
939                     unsigned base_size  = get_type_size_bytes(base);
940                     ir_mode *mode       = get_type_mode(member_type);
941                     unsigned bit_size   = get_mode_size_bits(mode);
942
943                         bit_offset = base_size*8 - bit_offset - bit_size;
944
945                         emit_uleb128(abbrev_bitfield_member);
946                         emit_uleb128(base_size);
947                         emit_uleb128(bit_size);
948                         emit_uleb128(bit_offset);
949                         member_type = base;
950                 } else {
951                         emit_uleb128(DW_TAG_member);
952                 }
953
954                 emit_type_address(member_type);
955                 emit_string(get_entity_name(member));
956                 emit_dbginfo(env, get_entity_dbg_info(member));
957                 assert(offset >= 0);
958                 emit_int8(1 + get_uleb128_size(offset));
959                 emit_op_plus_uconst(offset);
960         }
961
962         emit_int8(0);
963 }
964
965 static void emit_subroutine_type_abbrev(void)
966 {
967         begin_abbrev(DW_TAG_subroutine_type,
968                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
969         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
970         register_attribute(DW_AT_type,         DW_FORM_ref4);
971         end_abbrev();
972
973         begin_abbrev(abbrev_void_subroutine_type,
974                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
975         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
976         end_abbrev();
977
978         begin_abbrev(abbrev_unnamed_formal_parameter,
979                      DW_TAG_formal_parameter, DW_CHILDREN_no);
980         register_attribute(DW_AT_type,  DW_FORM_ref4);
981         end_abbrev();
982 }
983
984 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
985 {
986         size_t n_params = get_method_n_params(type);
987         size_t n_ress   = get_method_n_ress(type);
988         size_t i;
989         for (i = 0; i < n_params; ++i) {
990                 ir_type *param_type = get_method_param_type(type, i);
991                 emit_type(env, param_type);
992         }
993         for (i = 0; i < n_ress; ++i) {
994                 ir_type *res_type = get_method_res_type(type, i);
995                 emit_type(env, res_type);
996         }
997
998         emit_type_label(type);
999         emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
1000         emit_int8(1); /* prototyped */
1001         if (n_ress > 0) {
1002                 /* dwarf only supports 1 return type */
1003                 ir_type *res_type = get_method_res_type(type, 0);
1004                 emit_type_address(res_type);
1005         }
1006
1007         for (i = 0; i < n_params; ++i) {
1008                 ir_type *param_type = get_method_param_type(type, i);
1009                 emit_uleb128(abbrev_unnamed_formal_parameter);
1010                 emit_type_address(param_type);
1011         }
1012         emit_int8(0);
1013 }
1014
1015 static void emit_type(dwarf_t *env, ir_type *type)
1016 {
1017         if (pset_new_insert(&env->emitted_types, type))
1018                 return;
1019
1020         switch (get_type_tpop_code(type)) {
1021         case tpo_primitive: emit_base_type(type);            break;
1022         case tpo_pointer:   emit_pointer_type(env, type);    break;
1023         case tpo_array:     emit_array_type(env, type);      break;
1024         case tpo_struct:
1025         case tpo_union:     emit_compound_type(env, type);   break;
1026         case tpo_method:    emit_subroutine_type(env, type); break;
1027         default:
1028                 panic("bedwarf: type %+F not implemented yet", type);
1029         }
1030 }
1031
1032 static void emit_op_addr(const ir_entity *entity)
1033 {
1034         emit_int8(DW_OP_addr);
1035         be_emit_cstring("\t.long ");
1036         be_gas_emit_entity(entity);
1037         be_emit_char('\n');
1038         be_emit_write_line();
1039 }
1040
1041 static void emit_variable_abbrev(void)
1042 {
1043         begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
1044         register_attribute(DW_AT_name,      DW_FORM_string);
1045         register_attribute(DW_AT_type,      DW_FORM_ref4);
1046         register_attribute(DW_AT_external,  DW_FORM_flag);
1047         register_attribute(DW_AT_decl_file, DW_FORM_udata);
1048         register_attribute(DW_AT_decl_line, DW_FORM_udata);
1049         register_attribute(DW_AT_location,  DW_FORM_block1);
1050         end_abbrev();
1051 }
1052
1053 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
1054 {
1055         dwarf_t  *env  = (dwarf_t*) handle;
1056         ir_type  *type = get_entity_type(entity);
1057
1058         if (get_entity_ld_name(entity)[0] == '\0')
1059                 return;
1060
1061         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1062
1063         emit_type(env, type);
1064
1065         emit_entity_label(entity);
1066         emit_uleb128(DW_TAG_variable);
1067         emit_string(get_entity_name(entity));
1068         emit_type_address(type);
1069         emit_int8(is_extern_entity(entity));
1070         emit_dbginfo(env, get_entity_dbg_info(entity));
1071         /* DW_AT_location */
1072         emit_int8(5); /* block length */
1073         emit_op_addr(entity);
1074
1075         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
1076 }
1077
1078 static void emit_compile_unit_abbrev(void)
1079 {
1080         begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
1081         register_attribute(DW_AT_stmt_list, DW_FORM_data4);
1082         register_attribute(DW_AT_producer,  DW_FORM_string);
1083         register_attribute(DW_AT_name,      DW_FORM_string);
1084         register_attribute(DW_AT_comp_dir,  DW_FORM_string);
1085         end_abbrev();
1086 }
1087
1088 static void emit_abbrev(void)
1089 {
1090         /* create abbreviation for compile_unit */
1091         be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
1092
1093         emit_label("abbrev_begin");
1094
1095         emit_compile_unit_abbrev();
1096         emit_variable_abbrev();
1097         emit_subprogram_abbrev();
1098         emit_base_type_abbrev();
1099         emit_pointer_type_abbrev();
1100         emit_array_type_abbrev();
1101         emit_compound_type_abbrev();
1102         emit_subroutine_type_abbrev();
1103         emit_uleb128(0);
1104 }
1105
1106 /**
1107  * start a new source object (compilation unit)
1108  */
1109 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
1110 {
1111         (void) handle;
1112
1113         emit_abbrev();
1114
1115         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1116         emit_label("info_section_begin");
1117         emit_label("info_begin");
1118
1119         /* length of compilation unit info */
1120         emit_size("compile_unit_begin", "compile_unit_end");
1121         emit_label("compile_unit_begin");
1122         emit_int16(2);   /* dwarf version */
1123         emit_address("abbrev_begin");
1124         emit_int8(4);    /* pointer size */
1125
1126         /* compile_unit die */
1127         emit_uleb128(DW_TAG_compile_unit);
1128         emit_address("line_section_begin");
1129         emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
1130                            ir_get_version_minor(),
1131                            ir_get_version_revision());
1132         emit_string(filename);
1133         emit_string("/foo/bar/");
1134 }
1135
1136 static void dwarf_unit_end(dbg_handle *handle)
1137 {
1138         dwarf_t *env = (dwarf_t*)handle;
1139
1140         be_gas_emit_switch_section(GAS_SECTION_TEXT);
1141         emit_label("section_end");
1142
1143         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
1144         emit_uleb128(0); /* end of compile_unit DIE */
1145
1146         emit_label("compile_unit_end");
1147
1148         emit_line_info(env);
1149         emit_pubnames(env);
1150 }
1151
1152 /**
1153  * Close the drwarf handler.
1154  */
1155 static void dwarf_close(dbg_handle *handle)
1156 {
1157         dwarf_t *h = (dwarf_t *)handle;
1158         pmap_destroy(h->file_map);
1159         DEL_ARR_F(h->file_list);
1160         DEL_ARR_F(h->pubnames_list);
1161         pset_new_destroy(&h->emitted_types);
1162         free(h);
1163 }
1164
1165 /** The drwarf operations. */
1166 static const debug_ops dwarf_ops = {
1167         dwarf_close,
1168         dwarf_unit_begin,
1169         dwarf_unit_end,
1170         dwarf_method_begin,
1171         dwarf_method_end,
1172         dwarf_types,
1173         dwarf_variable,
1174         dwarf_set_dbg_info
1175 };
1176
1177 /* Opens a drwarf handler */
1178 static dbg_handle *be_dwarf_open(void)
1179 {
1180         dwarf_t *h = XMALLOCZ(dwarf_t);
1181
1182         h->base.ops      = &dwarf_ops;
1183         h->file_map      = pmap_create();
1184         h->file_list     = NEW_ARR_F(const char*, 0);
1185         h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
1186         pset_new_init(&h->emitted_types);
1187
1188         return &h->base;
1189 }
1190
1191 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
1192 void be_init_dwarf(void)
1193 {
1194         be_register_dbgout_module("dwarf", be_dwarf_open);
1195 }