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