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