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