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