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