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