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