becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[libfirm] / ir / be / bedwarf.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   DWARF debugging info support
9  * @author  Matthias Braun
10  */
11 #include "config.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16
17 #include "bearch.h"
18 #include "bedwarf_t.h"
19 #include "error.h"
20 #include "obst.h"
21 #include "irprog.h"
22 #include "irgraph.h"
23 #include "tv.h"
24 #include "xmalloc.h"
25 #include "pmap.h"
26 #include "pdeq.h"
27 #include "pset_new.h"
28 #include "util.h"
29 #include "obst.h"
30 #include "array_t.h"
31 #include "irtools.h"
32 #include "lc_opts.h"
33 #include "lc_opts_enum.h"
34 #include "beabi.h"
35 #include "bemodule.h"
36 #include "beemitter.h"
37 #include "dbginfo.h"
38 #include "begnuas.h"
39 #include "typerep.h"
40
41 enum {
42         LEVEL_NONE,
43         LEVEL_BASIC,
44         LEVEL_LOCATIONS,
45         LEVEL_FRAMEINFO
46 };
47 static int debug_level = LEVEL_NONE;
48
49 /**
50  * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
51  * the cases where we need multiple ids with the same DW_TAG we define new IDs
52  * here
53  */
54 typedef enum custom_abbrevs {
55         abbrev_void_subprogram = 1,
56         abbrev_subprogram,
57         abbrev_formal_parameter,
58         abbrev_unnamed_formal_parameter,
59         abbrev_formal_parameter_no_location,
60         abbrev_variable,
61         abbrev_compile_unit,
62         abbrev_base_type,
63         abbrev_pointer_type,
64         abbrev_void_pointer_type,
65         abbrev_array_type,
66         abbrev_subrange_type,
67         abbrev_structure_type,
68         abbrev_union_type,
69         abbrev_class_type,
70         abbrev_member,
71         abbrev_bitfield_member,
72         abbrev_subroutine_type,
73         abbrev_void_subroutine_type,
74 } custom_abbrevs;
75
76 /**
77  * The dwarf handle.
78  */
79 typedef struct dwarf_t {
80         const ir_entity         *cur_ent;     /**< current method entity */
81         unsigned                 next_type_nr; /**< next type number */
82         pmap                    *file_map;    /**< a map from file names to number in file list */
83         const char             **file_list;
84         const ir_entity        **pubnames_list;
85         pset_new_t               emitted_types;
86         const char              *main_file;   /**< name of the main source file */
87         const char              *curr_file;   /**< name of the current source file */
88         unsigned                 label_num;
89         unsigned                 last_line;
90 } dwarf_t;
91
92 static dwarf_t env;
93
94 static dwarf_source_language language;
95 static char                 *comp_dir;
96
97 static unsigned insert_file(const char *filename)
98 {
99         unsigned num;
100         void    *entry = pmap_get(void, env.file_map, filename);
101         if (entry != NULL) {
102                 return PTR_TO_INT(entry);
103         }
104         ARR_APP1(const char*, env.file_list, filename);
105         num = (unsigned)ARR_LEN(env.file_list);
106         pmap_insert(env.file_map, filename, INT_TO_PTR(num));
107
108         /* TODO: quote chars in string */
109         be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
110         return num;
111 }
112
113 static void emit_int32(uint32_t value)
114 {
115         be_emit_irprintf("\t.long %u\n", value);
116         be_emit_write_line();
117 }
118
119 static void emit_int16(uint16_t value)
120 {
121         be_emit_irprintf("\t.short %u\n", value);
122         be_emit_write_line();
123 }
124
125 static void emit_int8(uint8_t value)
126 {
127         be_emit_irprintf("\t.byte %u\n", value);
128         be_emit_write_line();
129 }
130
131 static void emit_uleb128(unsigned value)
132 {
133         be_emit_irprintf("\t.uleb128 0x%x\n", value);
134         be_emit_write_line();
135 }
136
137 static unsigned get_uleb128_size(unsigned value)
138 {
139         unsigned size = 0;
140         do {
141                 value >>= 7;
142                 size += 1;
143         } while (value != 0);
144         return size;
145 }
146
147 static void emit_sleb128(long value)
148 {
149         be_emit_irprintf("\t.sleb128 %ld\n", value);
150         be_emit_write_line();
151 }
152
153 static unsigned get_sleb128_size(long value)
154 {
155         unsigned size = 0;
156         do {
157                 value >>= 7;
158                 size += 1;
159         } while (value != 0 && value != -1);
160         return size;
161 }
162
163 static void emit_ref(const ir_entity *entity)
164 {
165         be_emit_cstring("\t.long ");
166         be_gas_emit_entity(entity);
167         be_emit_char('\n');
168         be_emit_write_line();
169 }
170
171 static void emit_string_printf(const char *fmt, ...)
172 {
173         va_list ap;
174         va_start(ap, fmt);
175         be_emit_cstring("\t.asciz \"");
176         be_emit_irvprintf(fmt, ap);
177         be_emit_cstring("\"\n");
178         va_end(ap);
179
180         be_emit_write_line();
181 }
182
183 static void emit_address(const char *name)
184 {
185         be_emit_cstring("\t.long ");
186         be_emit_string(be_gas_get_private_prefix());
187         be_emit_string(name);
188         be_emit_char('\n');
189         be_emit_write_line();
190 }
191
192 static void emit_size(const char *from_label, const char *to_label)
193 {
194         be_emit_cstring("\t.long ");
195         be_emit_string(be_gas_get_private_prefix());
196         be_emit_string(to_label);
197         be_emit_cstring(" - ");
198         be_emit_string(be_gas_get_private_prefix());
199         be_emit_string(from_label);
200         be_emit_char('\n');
201         be_emit_write_line();
202 }
203
204 static void emit_label(const char *name)
205 {
206         be_emit_string(be_gas_get_private_prefix());
207         be_emit_string(name);
208         be_emit_cstring(":\n");
209         be_emit_write_line();
210 }
211
212 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
213 {
214         emit_uleb128(attribute);
215         emit_uleb128(form);
216 }
217
218 static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
219                          dw_children children)
220 {
221         emit_uleb128(code);
222         emit_uleb128(tag);
223         emit_int8(children);
224 }
225
226 static void end_abbrev(void)
227 {
228         emit_uleb128(0);
229         emit_uleb128(0);
230 }
231
232 static void emit_line_info(void)
233 {
234         be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
235
236         emit_label("line_section_begin");
237         /* on elf systems gas handles producing the line info for us, and we
238          * don't have to do anything */
239         if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
240                 size_t i;
241                 emit_size("line_info_begin", "line_info_end");
242
243                 emit_label("line_info_begin");
244                 emit_int16(2); /* version */
245                 emit_size("line_info_prolog_begin", "line_info_prolog_end");
246                 emit_label("line_info_prolog_begin");
247                 emit_int8(1); /* len of smallest instruction TODO: query from backend */
248                 emit_int8(1); /* default is statement */
249                 emit_int8(246); /* line base */
250                 emit_int8(245); /* line range */
251                 emit_int8(10); /* opcode base */
252
253                 emit_uleb128(0);
254                 emit_uleb128(1);
255                 emit_uleb128(1);
256                 emit_uleb128(1);
257                 emit_uleb128(1);
258                 emit_uleb128(0);
259                 emit_uleb128(0);
260                 emit_uleb128(0);
261                 emit_uleb128(1);
262
263                 /* include directory list */
264                 be_gas_emit_cstring("/foo/bar");
265                 emit_int8(0);
266
267                 /* file list */
268                 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
269                         be_gas_emit_cstring(env.file_list[i]);
270                         emit_uleb128(1); /* directory */
271                         emit_uleb128(0); /* modification time */
272                         emit_uleb128(0); /* file length */
273                 }
274                 emit_int8(0);
275
276                 emit_label("line_info_prolog_end");
277
278                 /* TODO: put the line_info program here */
279
280                 emit_label("line_info_end");
281         }
282 }
283
284 static void emit_pubnames(void)
285 {
286         size_t i;
287
288         be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
289
290         emit_size("pubnames_begin", "pubnames_end");
291         emit_label("pubnames_begin");
292
293         emit_int16(2); /* version */
294         emit_size("info_section_begin", "info_begin");
295         emit_size("compile_unit_begin", "compile_unit_end");
296
297         for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
298                 const ir_entity *entity = env.pubnames_list[i];
299                 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
300                                  be_gas_get_private_prefix(),
301                                  get_entity_nr(entity), be_gas_get_private_prefix());
302                 be_gas_emit_cstring(get_entity_name(entity));
303         }
304         emit_int32(0);
305
306         emit_label("pubnames_end");
307 }
308
309 void be_dwarf_location(dbg_info *dbgi)
310 {
311         src_loc_t loc;
312         unsigned  filenum;
313
314         if (debug_level < LEVEL_LOCATIONS)
315                 return;
316         loc = ir_retrieve_dbg_info(dbgi);
317         if (!loc.file)
318                 return;
319
320         filenum = insert_file(loc.file);
321         be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
322         be_emit_write_line();
323 }
324
325 void be_dwarf_callframe_register(const arch_register_t *reg)
326 {
327         if (debug_level < LEVEL_FRAMEINFO)
328                 return;
329         be_emit_cstring("\t.cfi_def_cfa_register ");
330         be_emit_irprintf("%d\n", reg->dwarf_number);
331         be_emit_write_line();
332 }
333
334 void be_dwarf_callframe_offset(int offset)
335 {
336         if (debug_level < LEVEL_FRAMEINFO)
337                 return;
338         be_emit_cstring("\t.cfi_def_cfa_offset ");
339         be_emit_irprintf("%d\n", offset);
340         be_emit_write_line();
341 }
342
343 void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
344 {
345         if (debug_level < LEVEL_FRAMEINFO)
346                 return;
347         be_emit_cstring("\t.cfi_offset ");
348         be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
349         be_emit_write_line();
350 }
351
352 static bool is_extern_entity(const ir_entity *entity)
353 {
354         ir_visited_t visibility = get_entity_visibility(entity);
355         return visibility == ir_visibility_external;
356 }
357
358 static void emit_entity_label(const ir_entity *entity)
359 {
360         be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
361                          get_entity_nr(entity));
362         be_emit_write_line();
363 }
364
365 static void register_dbginfo_attributes(void)
366 {
367         register_attribute(DW_AT_decl_file,   DW_FORM_udata);
368         register_attribute(DW_AT_decl_line,   DW_FORM_udata);
369         register_attribute(DW_AT_decl_column, DW_FORM_udata);
370 }
371
372 static void emit_dbginfo(const dbg_info *dbgi)
373 {
374         src_loc_t const loc  = ir_retrieve_dbg_info(dbgi);
375         unsigned  const file = loc.file ? insert_file(loc.file) : 0;
376         emit_uleb128(file);
377         emit_uleb128(loc.line);
378         emit_uleb128(loc.column);
379 }
380
381 static void emit_type_address(const ir_type *type)
382 {
383         be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
384                          be_gas_get_private_prefix(),
385                          get_type_nr(type), be_gas_get_private_prefix());
386         be_emit_write_line();
387 }
388
389 static void emit_subprogram_abbrev(void)
390 {
391         begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
392         register_attribute(DW_AT_name,      DW_FORM_string);
393         register_dbginfo_attributes();
394         register_attribute(DW_AT_type,       DW_FORM_ref4);
395         register_attribute(DW_AT_external,   DW_FORM_flag);
396         register_attribute(DW_AT_low_pc,     DW_FORM_addr);
397         register_attribute(DW_AT_high_pc,    DW_FORM_addr);
398         //register_attribute(DW_AT_prototyped, DW_FORM_flag);
399         if (debug_level >= LEVEL_FRAMEINFO)
400                 register_attribute(DW_AT_frame_base, DW_FORM_block1);
401         end_abbrev();
402
403         begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
404         register_attribute(DW_AT_name,       DW_FORM_string);
405         register_dbginfo_attributes();
406         register_attribute(DW_AT_external,   DW_FORM_flag);
407         register_attribute(DW_AT_low_pc,     DW_FORM_addr);
408         register_attribute(DW_AT_high_pc,    DW_FORM_addr);
409         //register_attribute(DW_AT_prototyped, DW_FORM_flag);
410         if (debug_level >= LEVEL_FRAMEINFO)
411                 register_attribute(DW_AT_frame_base, DW_FORM_block1);
412         end_abbrev();
413
414         begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
415                      DW_CHILDREN_no);
416         register_attribute(DW_AT_name,      DW_FORM_string);
417         register_dbginfo_attributes();
418         register_attribute(DW_AT_type,      DW_FORM_ref4);
419         register_attribute(DW_AT_location,  DW_FORM_block1);
420         end_abbrev();
421
422         begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
423                      DW_CHILDREN_no);
424         register_attribute(DW_AT_name,      DW_FORM_string);
425         register_dbginfo_attributes();
426         register_attribute(DW_AT_type,      DW_FORM_ref4);
427         end_abbrev();
428 }
429
430 static void emit_type(ir_type *type);
431
432 static void emit_stack_location(long offset)
433 {
434         unsigned size = 1 + get_sleb128_size(offset);
435         emit_int8(size);
436         emit_int8(DW_OP_fbreg);
437         emit_sleb128(offset);
438 }
439
440 static void emit_function_parameters(const ir_entity *entity,
441                                      const parameter_dbg_info_t *infos)
442 {
443         ir_type  *type     = get_entity_type(entity);
444         size_t    n_params = get_method_n_params(type);
445         dbg_info *dbgi     = get_entity_dbg_info(entity);
446         size_t    i;
447         for (i = 0; i < n_params; ++i) {
448                 ir_type *param_type = get_method_param_type(type, i);
449
450                 if (infos != NULL && infos[i].entity != NULL) {
451                         long const offset = get_entity_offset(infos[i].entity);
452                         emit_uleb128(abbrev_formal_parameter);
453                         emit_string_printf("arg%u", (unsigned)i);
454                         emit_dbginfo(dbgi);
455                         emit_type_address(param_type);
456                         emit_stack_location(offset);
457                 } else {
458                         emit_uleb128(abbrev_formal_parameter_no_location);
459                         emit_string_printf("arg%u", (unsigned)i);
460                         emit_dbginfo(dbgi);
461                         emit_type_address(param_type);
462                 }
463         }
464 }
465
466 void be_dwarf_method_before(const ir_entity *entity,
467                             const parameter_dbg_info_t *parameter_infos)
468 {
469         if (debug_level < LEVEL_BASIC)
470                 return;
471         {
472         ir_type *type     = get_entity_type(entity);
473         size_t   n_ress   = get_method_n_ress(type);
474         size_t   n_params = get_method_n_params(type);
475         size_t   i;
476
477         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
478
479         if (n_ress > 0) {
480                 ir_type *res = get_method_res_type(type, 0);
481                 emit_type(res);
482         }
483         for (i = 0; i < n_params; ++i) {
484                 ir_type *param_type = get_method_param_type(type, i);
485                 emit_type(param_type);
486         }
487
488         emit_entity_label(entity);
489         emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
490         be_gas_emit_cstring(get_entity_ld_name(entity));
491         emit_dbginfo(get_entity_dbg_info(entity));
492         if (n_ress > 0) {
493                 ir_type *res = get_method_res_type(type, 0);
494                 emit_type_address(res);
495         }
496         emit_int8(is_extern_entity(entity));
497         emit_ref(entity);
498         be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
499                          get_entity_ld_name(entity));
500         /* frame_base prog */
501         emit_int8(1);
502         emit_int8(DW_OP_call_frame_cfa);
503
504         emit_function_parameters(entity, parameter_infos);
505         emit_int8(0);
506
507         ARR_APP1(const ir_entity*, env.pubnames_list, entity);
508
509         env.cur_ent = entity;
510         }
511 }
512
513 void be_dwarf_method_begin(void)
514 {
515         if (debug_level < LEVEL_FRAMEINFO)
516                 return;
517         be_emit_cstring("\t.cfi_startproc\n");
518         be_emit_write_line();
519 }
520
521 void be_dwarf_method_end(void)
522 {
523         if (debug_level < LEVEL_BASIC)
524                 return;
525         const ir_entity *entity = env.cur_ent;
526         be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
527                          get_entity_ld_name(entity));
528
529         if (debug_level >= LEVEL_FRAMEINFO) {
530                 be_emit_cstring("\t.cfi_endproc\n");
531                 be_emit_write_line();
532         }
533 }
534
535 static void emit_base_type_abbrev(void)
536 {
537         begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
538         register_attribute(DW_AT_encoding,  DW_FORM_data1);
539         register_attribute(DW_AT_byte_size, DW_FORM_data1);
540         register_attribute(DW_AT_name,      DW_FORM_string);
541         end_abbrev();
542 }
543
544 static void emit_type_label(const ir_type *type)
545 {
546         be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
547         be_emit_write_line();
548 }
549
550 static void emit_base_type(const ir_type *type)
551 {
552         char buf[128];
553         ir_mode *mode = get_type_mode(type);
554         ir_print_type(buf, sizeof(buf), type);
555
556         emit_type_label(type);
557         emit_uleb128(abbrev_base_type);
558         if (mode_is_int(mode)) {
559                 /* bool hack */
560                 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
561                         emit_int8(DW_ATE_boolean);
562                 } else {
563                         emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
564                 }
565         } else if (mode_is_reference(mode)) {
566                 emit_int8(DW_ATE_address);
567         } else if (mode_is_float(mode)) {
568                 emit_int8(DW_ATE_float);
569         } else {
570                 panic("mode not implemented yet");
571         }
572         emit_int8(get_mode_size_bytes(mode));
573         be_gas_emit_cstring(buf);
574 }
575
576 static void emit_pointer_type_abbrev(void)
577 {
578         begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
579         register_attribute(DW_AT_type,      DW_FORM_ref4);
580         register_attribute(DW_AT_byte_size, DW_FORM_data1);
581         end_abbrev();
582
583         /* for void* pointer s*/
584         begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
585         register_attribute(DW_AT_byte_size, DW_FORM_data1);
586         end_abbrev();
587 }
588
589 static void emit_pointer_type(const ir_type *type)
590 {
591         ir_type *points_to = get_pointer_points_to_type(type);
592         unsigned size      = get_type_size_bytes(type);
593         assert(size < 256);
594
595         if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
596                 emit_type(points_to);
597
598                 emit_type_label(type);
599                 emit_uleb128(abbrev_pointer_type);
600                 emit_type_address(points_to);
601         } else {
602                 emit_type_label(type);
603                 emit_uleb128(abbrev_void_pointer_type);
604         }
605         emit_int8(size);
606 }
607
608 static void emit_array_type_abbrev(void)
609 {
610         begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
611         register_attribute(DW_AT_type, DW_FORM_ref4);
612         end_abbrev();
613
614         begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
615         register_attribute(DW_AT_upper_bound, DW_FORM_udata);
616         end_abbrev();
617 }
618
619 static void emit_array_type(const ir_type *type)
620 {
621         ir_type *element_type = get_array_element_type(type);
622
623         if (get_array_n_dimensions(type) != 1)
624                 panic("multidimensional arrays no supported yet");
625
626         emit_type(element_type);
627
628         emit_type_label(type);
629         emit_uleb128(abbrev_array_type);
630         emit_type_address(element_type);
631
632         if (has_array_upper_bound(type, 0)) {
633                 int bound = get_array_upper_bound_int(type, 0);
634                 emit_uleb128(abbrev_subrange_type);
635                 emit_uleb128(bound);
636         }
637
638         emit_uleb128(0);
639 }
640
641 static void emit_compound_type_abbrev(void)
642 {
643         begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
644         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
645         // TODO register_dbginfo_attributes();
646         end_abbrev();
647
648         begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
649         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
650         // TODO register_dbginfo_attributes();
651         end_abbrev();
652
653         begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
654         register_attribute(DW_AT_byte_size,  DW_FORM_udata);
655         // TODO register_dbginfo_attributes();
656         end_abbrev();
657
658         begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
659         register_attribute(DW_AT_type,                 DW_FORM_ref4);
660         register_attribute(DW_AT_name,                 DW_FORM_string);
661         register_dbginfo_attributes();
662         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
663         end_abbrev();
664
665         begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
666         register_attribute(DW_AT_byte_size,            DW_FORM_udata);
667         register_attribute(DW_AT_bit_size,             DW_FORM_udata);
668         register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
669         register_attribute(DW_AT_type,                 DW_FORM_ref4);
670         register_attribute(DW_AT_name,                 DW_FORM_string);
671         register_dbginfo_attributes();
672         register_attribute(DW_AT_data_member_location, DW_FORM_block1);
673         end_abbrev();
674 }
675
676 static void emit_op_plus_uconst(unsigned value)
677 {
678         emit_int8(DW_OP_plus_uconst);
679         emit_uleb128(value);
680 }
681
682 static void emit_compound_type(const ir_type *type)
683 {
684         size_t i;
685         size_t n_members = get_compound_n_members(type);
686
687         for (i = 0; i < n_members; ++i) {
688                 ir_entity *member      = get_compound_member(type, i);
689                 ir_type   *member_type = get_entity_type(member);
690                 if (is_Primitive_type(member_type)) {
691                         ir_type *base = get_primitive_base_type(member_type);
692                         if (base != NULL)
693                                 member_type = base;
694                 }
695                 emit_type(member_type);
696         }
697
698         emit_type_label(type);
699         if (is_Struct_type(type)) {
700                 emit_uleb128(abbrev_structure_type);
701         } else if (is_Union_type(type)) {
702                 emit_uleb128(abbrev_union_type);
703         } else {
704                 assert(is_Class_type(type));
705                 emit_uleb128(abbrev_class_type);
706         }
707         emit_uleb128(get_type_size_bytes(type));
708         for (i = 0; i < n_members; ++i) {
709                 ir_entity *member      = get_compound_member(type, i);
710                 ir_type   *member_type = get_entity_type(member);
711                 int        offset      = get_entity_offset(member);
712                 ir_type   *base;
713
714                 if (is_Primitive_type(member_type) &&
715                     (base = get_primitive_base_type(member_type))) {
716                     unsigned bit_offset = get_entity_offset_bits_remainder(member);
717                     unsigned base_size  = get_type_size_bytes(base);
718                     ir_mode *mode       = get_type_mode(member_type);
719                     unsigned bit_size   = get_mode_size_bits(mode);
720
721                         bit_offset = base_size*8 - bit_offset - bit_size;
722
723                         emit_uleb128(abbrev_bitfield_member);
724                         emit_uleb128(base_size);
725                         emit_uleb128(bit_size);
726                         emit_uleb128(bit_offset);
727                         member_type = base;
728                 } else {
729                         emit_uleb128(abbrev_member);
730                 }
731
732                 emit_type_address(member_type);
733                 be_gas_emit_cstring(get_entity_name(member));
734                 emit_dbginfo(get_entity_dbg_info(member));
735                 assert(offset >= 0);
736                 emit_int8(1 + get_uleb128_size(offset));
737                 emit_op_plus_uconst(offset);
738         }
739
740         emit_int8(0);
741 }
742
743 static void emit_subroutine_type_abbrev(void)
744 {
745         begin_abbrev(abbrev_subroutine_type,
746                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
747         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
748         register_attribute(DW_AT_type,         DW_FORM_ref4);
749         end_abbrev();
750
751         begin_abbrev(abbrev_void_subroutine_type,
752                      DW_TAG_subroutine_type, DW_CHILDREN_yes);
753         register_attribute(DW_AT_prototyped,   DW_FORM_flag);
754         end_abbrev();
755
756         begin_abbrev(abbrev_unnamed_formal_parameter,
757                      DW_TAG_formal_parameter, DW_CHILDREN_no);
758         register_attribute(DW_AT_type,  DW_FORM_ref4);
759         end_abbrev();
760 }
761
762 static void emit_subroutine_type(const ir_type *type)
763 {
764         size_t n_params = get_method_n_params(type);
765         size_t n_ress   = get_method_n_ress(type);
766         size_t i;
767         for (i = 0; i < n_params; ++i) {
768                 ir_type *param_type = get_method_param_type(type, i);
769                 emit_type(param_type);
770         }
771         for (i = 0; i < n_ress; ++i) {
772                 ir_type *res_type = get_method_res_type(type, i);
773                 emit_type(res_type);
774         }
775
776         emit_type_label(type);
777         emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : abbrev_subroutine_type);
778         emit_int8(1); /* prototyped */
779         if (n_ress > 0) {
780                 /* dwarf only supports 1 return type */
781                 ir_type *res_type = get_method_res_type(type, 0);
782                 emit_type_address(res_type);
783         }
784
785         for (i = 0; i < n_params; ++i) {
786                 ir_type *param_type = get_method_param_type(type, i);
787                 emit_uleb128(abbrev_unnamed_formal_parameter);
788                 emit_type_address(param_type);
789         }
790         emit_int8(0);
791 }
792
793 static void emit_type(ir_type *type)
794 {
795         if (!pset_new_insert(&env.emitted_types, type))
796                 return;
797
798         switch (get_type_tpop_code(type)) {
799         case tpo_primitive: emit_base_type(type);       break;
800         case tpo_pointer:   emit_pointer_type(type);    break;
801         case tpo_array:     emit_array_type(type);      break;
802         case tpo_class:
803         case tpo_struct:
804         case tpo_union:     emit_compound_type(type);   break;
805         case tpo_method:    emit_subroutine_type(type); break;
806         default:
807                 panic("type %+F not implemented yet", type);
808         }
809 }
810
811 static void emit_op_addr(const ir_entity *entity)
812 {
813         emit_int8(DW_OP_addr);
814         be_emit_cstring("\t.long ");
815         be_gas_emit_entity(entity);
816         be_emit_char('\n');
817         be_emit_write_line();
818 }
819
820 static void emit_variable_abbrev(void)
821 {
822         begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
823         register_attribute(DW_AT_name,      DW_FORM_string);
824         register_attribute(DW_AT_type,      DW_FORM_ref4);
825         register_attribute(DW_AT_external,  DW_FORM_flag);
826         register_dbginfo_attributes();
827         register_attribute(DW_AT_location,  DW_FORM_block1);
828         end_abbrev();
829 }
830
831 void be_dwarf_variable(const ir_entity *entity)
832 {
833         ir_type *type = get_entity_type(entity);
834
835         if (debug_level < LEVEL_BASIC)
836                 return;
837         if (get_entity_ld_name(entity)[0] == '\0')
838                 return;
839         if (!entity_has_definition(entity))
840                 return;
841
842         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
843
844         emit_type(type);
845
846         emit_entity_label(entity);
847         emit_uleb128(abbrev_variable);
848         be_gas_emit_cstring(get_entity_ld_name(entity));
849         emit_type_address(type);
850         emit_int8(is_extern_entity(entity));
851         emit_dbginfo(get_entity_dbg_info(entity));
852         /* DW_AT_location */
853         emit_int8(5); /* block length */
854         emit_op_addr(entity);
855
856         ARR_APP1(const ir_entity*, env.pubnames_list, entity);
857 }
858
859 static void emit_compile_unit_abbrev(void)
860 {
861         begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
862         register_attribute(DW_AT_stmt_list, DW_FORM_data4);
863         register_attribute(DW_AT_producer,  DW_FORM_string);
864         register_attribute(DW_AT_name,      DW_FORM_string);
865         if (language != 0)
866                 register_attribute(DW_AT_language,  DW_FORM_data2);
867         if (comp_dir != NULL)
868                 register_attribute(DW_AT_comp_dir,  DW_FORM_string);
869         end_abbrev();
870 }
871
872 static void emit_abbrev(void)
873 {
874         /* create abbreviation for compile_unit */
875         be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
876
877         emit_label("abbrev_begin");
878
879         emit_compile_unit_abbrev();
880         emit_variable_abbrev();
881         emit_subprogram_abbrev();
882         emit_base_type_abbrev();
883         emit_pointer_type_abbrev();
884         emit_array_type_abbrev();
885         emit_compound_type_abbrev();
886         emit_subroutine_type_abbrev();
887         emit_uleb128(0);
888 }
889
890 void be_dwarf_unit_begin(const char *filename)
891 {
892         if (debug_level < LEVEL_BASIC)
893                 return;
894         emit_abbrev();
895
896         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
897         emit_label("info_section_begin");
898         emit_label("info_begin");
899
900         const backend_params *be_params = be_get_backend_param();
901
902         /* length of compilation unit info */
903         emit_size("compile_unit_begin", "compile_unit_end");
904         emit_label("compile_unit_begin");
905         emit_int16(3);   /* dwarf version */
906         emit_address("abbrev_begin");
907         emit_int8(be_params->machine_size / 8); /* pointer size */
908
909         /* compile_unit die */
910         emit_uleb128(abbrev_compile_unit);
911         emit_address("line_section_begin");
912         emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
913                            ir_get_version_minor(), ir_get_version_revision());
914         be_gas_emit_cstring(filename);
915         if (language != 0)
916                 emit_int16(language);
917         if (comp_dir != NULL)
918                 be_gas_emit_cstring(comp_dir);
919
920         /* tell gas to emit cfi in debug_frame
921          * TODO: if we produce exception handling code then this should be
922          *       .eh_frame (I also wonder if bad things happen if simply always
923          *       use eh_frame) */
924         be_emit_cstring("\t.cfi_sections .debug_frame\n");
925         be_emit_write_line();
926 }
927
928 void be_dwarf_unit_end(void)
929 {
930         if (debug_level < LEVEL_BASIC)
931                 return;
932         be_gas_emit_switch_section(GAS_SECTION_TEXT);
933         emit_label("section_end");
934
935         be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
936         emit_uleb128(0); /* end of compile_unit DIE */
937
938         emit_label("compile_unit_end");
939
940         emit_line_info();
941         emit_pubnames();
942 }
943
944 void be_dwarf_close(void)
945 {
946         if (debug_level < LEVEL_BASIC)
947                 return;
948         pmap_destroy(env.file_map);
949         DEL_ARR_F(env.file_list);
950         DEL_ARR_F(env.pubnames_list);
951         pset_new_destroy(&env.emitted_types);
952 }
953
954 /* Opens a dwarf handler */
955 void be_dwarf_open(void)
956 {
957         if (debug_level < LEVEL_BASIC)
958                 return;
959         env.file_map      = pmap_create();
960         env.file_list     = NEW_ARR_F(const char*, 0);
961         env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
962         pset_new_init(&env.emitted_types);
963 }
964
965 void be_dwarf_set_source_language(dwarf_source_language new_language)
966 {
967         language = new_language;
968 }
969
970 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
971 {
972         xfree(comp_dir);
973         comp_dir = xstrdup(new_comp_dir);
974 }
975
976 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
977 void be_init_dwarf(void)
978 {
979         static const lc_opt_enum_int_items_t level_items[] = {
980                 { "none",      LEVEL_NONE },
981                 { "basic",     LEVEL_BASIC },
982                 { "locations", LEVEL_LOCATIONS },
983                 { "frameinfo", LEVEL_FRAMEINFO },
984                 { NULL,        0 }
985         };
986         static lc_opt_enum_int_var_t debug_level_opt = {
987                 &debug_level, level_items
988         };
989         static lc_opt_table_entry_t be_main_options[] = {
990                 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
991                                     &debug_level_opt),
992                 LC_OPT_LAST
993         };
994         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
995         lc_opt_add_table(be_grp, be_main_options);
996 }