rename dbg_set_dbg_info to dbg_location
[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 "bedwarf_t.h"
32 #include "obst.h"
33 #include "irprog.h"
34 #include "irgraph.h"
35 #include "tv.h"
36 #include "xmalloc.h"
37 #include "pmap.h"
38 #include "pdeq.h"
39 #include "pset_new.h"
40 #include "util.h"
41 #include "obst.h"
42 #include "array_t.h"
43 #include "be_dbgout_t.h"
44 #include "beabi.h"
45 #include "bemodule.h"
46 #include "beemitter.h"
47 #include "dbginfo.h"
48 #include "begnuas.h"
49
50 /**
51  * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
52  * the cases where we need multiple ids with the same DW_TAG we define new IDs
53  * here
54  */
55 typedef enum custom_abbrevs {
56         abbrev_void_pointer_type = 100,
57         abbrev_unnamed_formal_parameter,
58         abbrev_void_subroutine_type,
59         abbrev_bitfield_member,
60 } custom_abbrevs;
61
62 /**
63  * The dwarf handle.
64  */
65 typedef struct dwarf_t {
66         dbg_handle               base;         /**< the base class */
67         const ir_entity         *cur_ent;     /**< current method entity */
68         const be_stack_layout_t *layout;      /**< current stack layout */
69         unsigned                 next_type_nr; /**< next type number */
70         pmap                    *file_map;    /**< a map from file names to number in file list */
71         const char             **file_list;
72         const ir_entity        **pubnames_list;
73         pset_new_t               emitted_types;
74         const char              *main_file;   /**< name of the main source file */
75         const char              *curr_file;   /**< name of the current source file */
76         unsigned                 label_num;
77         unsigned                 last_line;
78 } dwarf_t;
79
80 static dwarf_source_language language;
81 static const char           *comp_dir;
82
83 static unsigned insert_file(dwarf_t *env, const char *filename)
84 {
85         unsigned num;
86         void    *entry = pmap_get(env->file_map, filename);
87         if (entry != NULL) {
88                 return PTR_TO_INT(entry);
89         }
90         ARR_APP1(const char*, env->file_list, filename);
91         num = (unsigned)ARR_LEN(env->file_list);
92         pmap_insert(env->file_map, filename, INT_TO_PTR(num));
93         /* TODO: quote chars in string */
94         be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
95         return num;
96 }
97
98 static void emit_int32(uint32_t value)
99 {
100         be_emit_irprintf("\t.long %u\n", value);
101         be_emit_write_line();
102 }
103
104 static void emit_int16(uint16_t value)
105 {
106         be_emit_irprintf("\t.short %u\n", value);
107         be_emit_write_line();
108 }
109
110 static void emit_int8(uint8_t value)
111 {
112         be_emit_irprintf("\t.byte %u\n", value);
113         be_emit_write_line();
114 }
115
116 static void emit_uleb128(unsigned value)
117 {
118         be_emit_irprintf("\t.uleb128 0x%x\n", value);
119         be_emit_write_line();
120 }
121
122 static unsigned get_uleb128_size(unsigned value)
123 {
124         unsigned size = 0;
125         do {
126                 value >>= 7;
127                 size += 1;
128         } while (value != 0);
129         return size;
130 }
131
132 static void emit_string(const char *string)
133 {
134         be_emit_irprintf("\t.asciz \"%s\"\n", string);
135         be_emit_write_line();
136 }
137
138 static void emit_ref(const ir_entity *entity)
139 {
140         be_emit_cstring("\t.long ");
141         be_gas_emit_entity(entity);
142         be_emit_char('\n');
143         be_emit_write_line();
144 }
145
146 static void emit_string_printf(const char *fmt, ...)
147 {
148         va_list ap;
149         va_start(ap, fmt);
150         be_emit_cstring("\t.asciz \"");
151         be_emit_irvprintf(fmt, ap);
152         be_emit_cstring("\"\n");
153         va_end(ap);
154
155         be_emit_write_line();
156 }
157
158 static void emit_address(const char *name)
159 {
160         be_emit_cstring("\t.long ");
161         be_emit_string(be_gas_get_private_prefix());
162         be_emit_string(name);
163         be_emit_char('\n');
164         be_emit_write_line();
165 }
166
167 static void emit_size(const char *from_label, const char *to_label)
168 {
169         be_emit_cstring("\t.long ");
170         be_emit_string(be_gas_get_private_prefix());
171         be_emit_string(to_label);
172         be_emit_cstring(" - ");
173         be_emit_string(be_gas_get_private_prefix());
174         be_emit_string(from_label);
175         be_emit_char('\n');
176         be_emit_write_line();
177 }
178
179 static void emit_label(const char *name)
180 {
181         be_emit_string(be_gas_get_private_prefix());
182         be_emit_string(name);
183         be_emit_cstring(":\n");
184         be_emit_write_line();
185 }
186
187 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
188 {
189         emit_uleb128(attribute);
190         emit_uleb128(form);
191 }
192
193 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
194 {
195         emit_uleb128(code);
196         emit_uleb128(tag);
197         emit_int8(children);
198 }
199
200 static void end_abbrev(void)
201 {
202         emit_uleb128(0);
203         emit_uleb128(0);
204 }
205
206 static void emit_line_info(dwarf_t *env)
207 {
208         be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
209
210         emit_label("line_section_begin");
211         /* on elf systems gas handles producing the line info for us, and we
212          * don't have to do anything */
213         if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
214                 size_t i;
215                 emit_size("line_info_begin", "line_info_end");
216
217                 emit_label("line_info_begin");
218                 emit_int16(2); /* version */
219                 emit_size("line_info_prolog_begin", "line_info_prolog_end");
220                 emit_label("line_info_prolog_begin");
221                 emit_int8(1); /* len of smallest instruction TODO: query from backend */
222                 emit_int8(1); /* default is statement */
223                 emit_int8(246); /* line base */
224                 emit_int8(245); /* line range */
225                 emit_int8(10); /* opcode base */
226
227                 emit_uleb128(0);
228                 emit_uleb128(1);
229                 emit_uleb128(1);
230                 emit_uleb128(1);
231                 emit_uleb128(1);
232                 emit_uleb128(0);
233                 emit_uleb128(0);
234                 emit_uleb128(0);
235                 emit_uleb128(1);
236
237                 /* include directory list */
238                 emit_string("/foo/bar");
239                 emit_int8(0);
240
241                 /* file list */
242                 for (i = 0; i < ARR_LEN(env->file_list); ++i) {
243                         emit_string(env->file_list[i]);
244                         emit_uleb128(1); /* directory */
245                         emit_uleb128(0); /* modification time */
246                         emit_uleb128(0); /* file length */
247                 }
248                 emit_int8(0);
249
250                 emit_label("line_info_prolog_end");
251
252                 /* TODO: put the line_info program here */
253
254                 emit_label("line_info_end");
255         }
256 }
257
258 static void emit_pubnames(dwarf_t *env)
259 {
260         size_t i;
261
262         be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
263
264         emit_size("pubnames_begin", "pubnames_end");
265         emit_label("pubnames_begin");
266
267         emit_int16(2); /* version */
268         emit_size("info_section_begin", "info_begin");
269         emit_size("compile_unit_begin", "compile_unit_end");
270
271         for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
272                 const ir_entity *entity = env->pubnames_list[i];
273                 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
274                                  be_gas_get_private_prefix(),
275                                  get_entity_nr(entity), be_gas_get_private_prefix());
276                 emit_string(get_entity_name(entity));
277         }
278         emit_int32(0);
279
280         emit_label("pubnames_end");
281 }
282
283 static void dwarf_location(dbg_handle *h, dbg_info *dbgi)
284 {
285         dwarf_t  *const env = (dwarf_t*)h;
286         src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
287         unsigned        filenum;
288
289         if (!loc.file)
290                 return;
291
292         filenum = insert_file(env, loc.file);
293         be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
294         be_emit_write_line();
295 }
296
297 static bool is_extern_entity(const ir_entity *entity)
298 {
299         ir_visited_t visibility = get_entity_visibility(entity);
300         return visibility == ir_visibility_default
301             || visibility == ir_visibility_external;
302 }
303
304 static void emit_entity_label(const ir_entity *entity)
305 {
306         be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
307                          get_entity_nr(entity));
308         be_emit_write_line();
309 }
310
311 static void register_dbginfo_attributes(void)
312 {
313         register_attribute(DW_AT_decl_file,   DW_FORM_udata);
314         register_attribute(DW_AT_decl_line,   DW_FORM_udata);
315         register_attribute(DW_AT_decl_column, DW_FORM_udata);
316 }
317
318 /**
319  * Emit values for DW_AT_decl_file, DW_AT_decl_line and DW_AT_decl_column.
320  */
321 static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
322 {
323         src_loc_t const loc  = ir_retrieve_dbg_info(dbgi);
324         unsigned  const file = loc.file ? insert_file(env, loc.file) : 0;
325         emit_uleb128(file);
326         emit_uleb128(loc.line);
327         emit_uleb128(loc.column);
328 }
329
330 static void emit_subprogram_abbrev(void)
331 {
332         begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
333         register_attribute(DW_AT_name,      DW_FORM_string);
334         register_dbginfo_attributes();
335         //register_attribute(DW_AT_prototyped, DW_FORM_flag);
336         //register_attribute(DW_AT_type,       DW_FORM_ref4);
337         register_attribute(DW_AT_external,  DW_FORM_flag);
338         register_attribute(DW_AT_low_pc,    DW_FORM_addr);
339         register_attribute(DW_AT_high_pc,   DW_FORM_addr);
340         //register_attribute(DW_AT_frame_base, DW_FORM_block1);
341         end_abbrev();
342 }
343
344 /**
345  * dump the dwarf for a method begin
346  */
347 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
348 {
349         dwarf_t *env = (dwarf_t*)handle;
350
351         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
352
353         emit_entity_label(entity);
354         emit_uleb128(DW_TAG_subprogram);
355         emit_string(get_entity_ld_name(entity));
356         emit_dbginfo(env, get_entity_dbg_info(entity));
357         emit_int8(is_extern_entity(entity));
358         emit_ref(entity);
359         be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
360                          get_entity_ld_name(entity));
361
362         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
363
364         env->cur_ent = entity;
365 }
366
367 /**
368  * dump the dwarf for a method end
369  */
370 static void dwarf_method_end(dbg_handle *handle)
371 {
372         dwarf_t *env = (dwarf_t*)handle;
373         const ir_entity *entity = env->cur_ent;
374
375         be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
376                          get_entity_ld_name(entity));
377 }
378
379 static void dwarf_types(dbg_handle *handle)
380 {
381         (void)handle;
382 }
383
384 static void emit_type(dwarf_t *env, ir_type *type);
385
386 static void emit_base_type_abbrev(void)
387 {
388         begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
389         register_attribute(DW_AT_encoding,  DW_FORM_data1);
390         register_attribute(DW_AT_byte_size, DW_FORM_data1);
391         register_attribute(DW_AT_name,      DW_FORM_string);
392         end_abbrev();
393 }
394
395 static void emit_type_label(const ir_type *type)
396 {
397         be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
398         be_emit_write_line();
399 }
400
401 static void emit_type_address(const ir_type *type)
402 {
403         be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
404                          be_gas_get_private_prefix(),
405                          get_type_nr(type), be_gas_get_private_prefix());
406         be_emit_write_line();
407 }
408
409 static void emit_base_type(const ir_type *type)
410 {
411         char buf[128];
412         ir_mode *mode = get_type_mode(type);
413         ir_print_type(buf, sizeof(buf), type);
414
415         emit_type_label(type);
416         emit_uleb128(DW_TAG_base_type);
417         if (mode_is_int(mode)) {
418                 /* bool hack */
419                 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
420                         emit_int8(DW_ATE_boolean);
421                 } else {
422                         emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
423                 }
424         } else if (mode_is_reference(mode)) {
425                 emit_int8(DW_ATE_address);
426         } else if (mode_is_float(mode)) {
427                 emit_int8(DW_ATE_float);
428         } else {
429                 panic("mode not implemented yet");
430         }
431         emit_int8(get_mode_size_bytes(mode));
432         emit_string(buf);
433 }
434
435 static void emit_pointer_type_abbrev(void)
436 {
437         begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
438         register_attribute(DW_AT_type,      DW_FORM_ref4);
439         register_attribute(DW_AT_byte_size, DW_FORM_data1);
440         end_abbrev();
441
442         /* for void* pointer s*/
443         begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
444         register_attribute(DW_AT_byte_size, DW_FORM_data1);
445         end_abbrev();
446 }
447
448 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
449 {
450         ir_type *points_to = get_pointer_points_to_type(type);
451         unsigned size      = get_type_size_bytes(type);
452         assert(size < 256);
453
454         if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
455                 emit_type(env, points_to);
456
457                 emit_type_label(type);
458                 emit_uleb128(DW_TAG_pointer_type);
459                 emit_type_address(points_to);
460         } else {
461                 emit_type_label(type);
462                 emit_uleb128(abbrev_void_pointer_type);
463         }
464         emit_int8(size);
465 }
466
467 static void emit_array_type_abbrev(void)
468 {
469         begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
470         register_attribute(DW_AT_type, DW_FORM_ref4);
471         end_abbrev();
472
473         begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
474         register_attribute(DW_AT_upper_bound, DW_FORM_udata);
475         end_abbrev();
476 }
477
478 static void emit_array_type(dwarf_t *env, const ir_type *type)
479 {
480         ir_type *element_type = get_array_element_type(type);
481
482         if (get_array_n_dimensions(type) != 1)
483                 panic("dwarf: multidimensional arrays no supported yet");
484
485         emit_type(env, element_type);
486
487         emit_type_label(type);
488         emit_uleb128(DW_TAG_array_type);
489         emit_type_address(element_type);
490
491         if (has_array_upper_bound(type, 0)) {
492                 int bound = get_array_upper_bound_int(type, 0);
493                 emit_uleb128(DW_TAG_subrange_type);
494                 emit_uleb128(bound);
495         }
496
497         emit_uleb128(0);
498 }
499
500 static void emit_compound_type_abbrev(void)
501 {
502         begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
503         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
504         // TODO register_dbginfo_attributes();
505         end_abbrev();
506
507         begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
508         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
509         // TODO register_dbginfo_attributes();
510         end_abbrev();
511
512         begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
513         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
514         // TODO register_dbginfo_attributes();
515         end_abbrev();
516
517         begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
518         register_attribute(DW_AT_type,                 DW_FORM_ref4);
519         register_attribute(DW_AT_name,                 DW_FORM_string);
520         register_dbginfo_attributes();
521         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
522         end_abbrev();
523
524         begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
525         register_attribute(DW_AT_byte_size,            DW_FORM_udata);
526         register_attribute(DW_AT_bit_size,             DW_FORM_udata);
527         register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
528         register_attribute(DW_AT_type,                 DW_FORM_ref4);
529         register_attribute(DW_AT_name,                 DW_FORM_string);
530         register_dbginfo_attributes();
531         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
532         end_abbrev();
533 }
534
535 static void emit_op_plus_uconst(unsigned value)
536 {
537         emit_int8(DW_OP_plus_uconst);
538         emit_uleb128(value);
539 }
540
541 static void emit_compound_type(dwarf_t *env, const ir_type *type)
542 {
543         size_t i;
544         size_t n_members = get_compound_n_members(type);
545
546         for (i = 0; i < n_members; ++i) {
547                 ir_entity *member      = get_compound_member(type, i);
548                 ir_type   *member_type = get_entity_type(member);
549                 if (is_Primitive_type(member_type)) {
550                         ir_type *base = get_primitive_base_type(member_type);
551                         if (base != NULL)
552                                 member_type = base;
553                 }
554                 emit_type(env, member_type);
555         }
556
557         emit_type_label(type);
558         if (is_Struct_type(type)) {
559                 emit_uleb128(DW_TAG_structure_type);
560         } else if (is_Union_type(type)) {
561                 emit_uleb128(DW_TAG_union_type);
562         } else {
563                 assert(is_Class_type(type));
564                 emit_uleb128(DW_TAG_class_type);
565         }
566         emit_uleb128(get_type_size_bytes(type));
567         for (i = 0; i < n_members; ++i) {
568                 ir_entity *member      = get_compound_member(type, i);
569                 ir_type   *member_type = get_entity_type(member);
570                 int        offset      = get_entity_offset(member);
571                 ir_type   *base;
572
573                 if (is_Primitive_type(member_type) &&
574                     (base = get_primitive_base_type(member_type))) {
575                     unsigned bit_offset = get_entity_offset_bits_remainder(member);
576                     unsigned base_size  = get_type_size_bytes(base);
577                     ir_mode *mode       = get_type_mode(member_type);
578                     unsigned bit_size   = get_mode_size_bits(mode);
579
580                         bit_offset = base_size*8 - bit_offset - bit_size;
581
582                         emit_uleb128(abbrev_bitfield_member);
583                         emit_uleb128(base_size);
584                         emit_uleb128(bit_size);
585                         emit_uleb128(bit_offset);
586                         member_type = base;
587                 } else {
588                         emit_uleb128(DW_TAG_member);
589                 }
590
591                 emit_type_address(member_type);
592                 emit_string(get_entity_name(member));
593                 emit_dbginfo(env, get_entity_dbg_info(member));
594                 assert(offset >= 0);
595                 emit_int8(1 + get_uleb128_size(offset));
596                 emit_op_plus_uconst(offset);
597         }
598
599         emit_int8(0);
600 }
601
602 static void emit_subroutine_type_abbrev(void)
603 {
604         begin_abbrev(DW_TAG_subroutine_type,
605                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
606         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
607         register_attribute(DW_AT_type,         DW_FORM_ref4);
608         end_abbrev();
609
610         begin_abbrev(abbrev_void_subroutine_type,
611                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
612         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
613         end_abbrev();
614
615         begin_abbrev(abbrev_unnamed_formal_parameter,
616                      DW_TAG_formal_parameter, DW_CHILDREN_no);
617         register_attribute(DW_AT_type,  DW_FORM_ref4);
618         end_abbrev();
619 }
620
621 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
622 {
623         size_t n_params = get_method_n_params(type);
624         size_t n_ress   = get_method_n_ress(type);
625         size_t i;
626         for (i = 0; i < n_params; ++i) {
627                 ir_type *param_type = get_method_param_type(type, i);
628                 emit_type(env, param_type);
629         }
630         for (i = 0; i < n_ress; ++i) {
631                 ir_type *res_type = get_method_res_type(type, i);
632                 emit_type(env, res_type);
633         }
634
635         emit_type_label(type);
636         emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
637         emit_int8(1); /* prototyped */
638         if (n_ress > 0) {
639                 /* dwarf only supports 1 return type */
640                 ir_type *res_type = get_method_res_type(type, 0);
641                 emit_type_address(res_type);
642         }
643
644         for (i = 0; i < n_params; ++i) {
645                 ir_type *param_type = get_method_param_type(type, i);
646                 emit_uleb128(abbrev_unnamed_formal_parameter);
647                 emit_type_address(param_type);
648         }
649         emit_int8(0);
650 }
651
652 static void emit_type(dwarf_t *env, ir_type *type)
653 {
654         if (pset_new_insert(&env->emitted_types, type))
655                 return;
656
657         switch (get_type_tpop_code(type)) {
658         case tpo_primitive: emit_base_type(type);            break;
659         case tpo_pointer:   emit_pointer_type(env, type);    break;
660         case tpo_array:     emit_array_type(env, type);      break;
661         case tpo_class:
662         case tpo_struct:
663         case tpo_union:     emit_compound_type(env, type);   break;
664         case tpo_method:    emit_subroutine_type(env, type); break;
665         default:
666                 panic("bedwarf: type %+F not implemented yet", type);
667         }
668 }
669
670 static void emit_op_addr(const ir_entity *entity)
671 {
672         emit_int8(DW_OP_addr);
673         be_emit_cstring("\t.long ");
674         be_gas_emit_entity(entity);
675         be_emit_char('\n');
676         be_emit_write_line();
677 }
678
679 static void emit_variable_abbrev(void)
680 {
681         begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
682         register_attribute(DW_AT_name,      DW_FORM_string);
683         register_attribute(DW_AT_type,      DW_FORM_ref4);
684         register_attribute(DW_AT_external,  DW_FORM_flag);
685         register_dbginfo_attributes();
686         register_attribute(DW_AT_location,  DW_FORM_block1);
687         end_abbrev();
688 }
689
690 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
691 {
692         dwarf_t  *env  = (dwarf_t*) handle;
693         ir_type  *type = get_entity_type(entity);
694
695         if (get_entity_ld_name(entity)[0] == '\0')
696                 return;
697
698         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
699
700         emit_type(env, type);
701
702         emit_entity_label(entity);
703         emit_uleb128(DW_TAG_variable);
704         emit_string(get_entity_ld_name(entity));
705         emit_type_address(type);
706         emit_int8(is_extern_entity(entity));
707         emit_dbginfo(env, get_entity_dbg_info(entity));
708         /* DW_AT_location */
709         emit_int8(5); /* block length */
710         emit_op_addr(entity);
711
712         ARR_APP1(const ir_entity*, env->pubnames_list, entity);
713 }
714
715 static void emit_compile_unit_abbrev(void)
716 {
717         begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
718         register_attribute(DW_AT_stmt_list, DW_FORM_data4);
719         register_attribute(DW_AT_producer,  DW_FORM_string);
720         register_attribute(DW_AT_name,      DW_FORM_string);
721         if (language != 0)
722                 register_attribute(DW_AT_language,  DW_FORM_data2);
723         if (comp_dir != NULL)
724                 register_attribute(DW_AT_comp_dir,  DW_FORM_string);
725         end_abbrev();
726 }
727
728 static void emit_abbrev(void)
729 {
730         /* create abbreviation for compile_unit */
731         be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
732
733         emit_label("abbrev_begin");
734
735         emit_compile_unit_abbrev();
736         emit_variable_abbrev();
737         emit_subprogram_abbrev();
738         emit_base_type_abbrev();
739         emit_pointer_type_abbrev();
740         emit_array_type_abbrev();
741         emit_compound_type_abbrev();
742         emit_subroutine_type_abbrev();
743         emit_uleb128(0);
744 }
745
746 /**
747  * start a new source object (compilation unit)
748  */
749 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
750 {
751         (void) handle;
752
753         emit_abbrev();
754
755         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
756         emit_label("info_section_begin");
757         emit_label("info_begin");
758
759         /* length of compilation unit info */
760         emit_size("compile_unit_begin", "compile_unit_end");
761         emit_label("compile_unit_begin");
762         emit_int16(2);   /* dwarf version */
763         emit_address("abbrev_begin");
764         emit_int8(4);    /* pointer size */
765
766         /* compile_unit die */
767         emit_uleb128(DW_TAG_compile_unit);
768         emit_address("line_section_begin");
769         emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
770                            ir_get_version_minor(),
771                            ir_get_version_revision());
772         emit_string(filename);
773         if (language != 0)
774                 emit_int16(language);
775         if (comp_dir != NULL)
776                 emit_string(comp_dir);
777 }
778
779 static void dwarf_unit_end(dbg_handle *handle)
780 {
781         dwarf_t *env = (dwarf_t*)handle;
782
783         be_gas_emit_switch_section(GAS_SECTION_TEXT);
784         emit_label("section_end");
785
786         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
787         emit_uleb128(0); /* end of compile_unit DIE */
788
789         emit_label("compile_unit_end");
790
791         emit_line_info(env);
792         emit_pubnames(env);
793 }
794
795 /**
796  * Close the dwarf handler.
797  */
798 static void dwarf_close(dbg_handle *handle)
799 {
800         dwarf_t *h = (dwarf_t *)handle;
801         pmap_destroy(h->file_map);
802         DEL_ARR_F(h->file_list);
803         DEL_ARR_F(h->pubnames_list);
804         pset_new_destroy(&h->emitted_types);
805         free(h);
806 }
807
808 /** The dwarf operations. */
809 static const debug_ops dwarf_ops = {
810         dwarf_close,
811         dwarf_unit_begin,
812         dwarf_unit_end,
813         dwarf_method_begin,
814         dwarf_method_end,
815         dwarf_types,
816         dwarf_variable,
817         dwarf_location,
818 };
819
820 /* Opens a dwarf handler */
821 static dbg_handle *be_dwarf_open(void)
822 {
823         dwarf_t *h = XMALLOCZ(dwarf_t);
824
825         h->base.ops      = &dwarf_ops;
826         h->file_map      = pmap_create();
827         h->file_list     = NEW_ARR_F(const char*, 0);
828         h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
829         pset_new_init(&h->emitted_types);
830
831         return &h->base;
832 }
833
834 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
835 void be_init_dwarf(void)
836 {
837         be_register_dbgout_module("dwarf", be_dwarf_open);
838 }
839
840 void be_dwarf_set_source_language(dwarf_source_language new_language)
841 {
842         language = new_language;
843 }
844
845 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
846 {
847         comp_dir = new_comp_dir;
848 }