bedwarf.c 26.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief   DWARF debugging info support
 * @author  Matthias Braun
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

31
#include "bedwarf_t.h"
32
33
34
35
36
37
38
39
40
41
42
#include "obst.h"
#include "irprog.h"
#include "irgraph.h"
#include "tv.h"
#include "xmalloc.h"
#include "pmap.h"
#include "pdeq.h"
#include "pset_new.h"
#include "util.h"
#include "obst.h"
#include "array_t.h"
43
44
45
#include "irtools.h"
#include "lc_opts.h"
#include "lc_opts_enum.h"
46
47
48
49
50
51
#include "beabi.h"
#include "bemodule.h"
#include "beemitter.h"
#include "dbginfo.h"
#include "begnuas.h"

52
53
54
55
56
57
58
59
enum {
	LEVEL_NONE,
	LEVEL_BASIC,
	LEVEL_LOCATIONS,
	LEVEL_FRAMEINFO
};
static int debug_level = LEVEL_NONE;

60
61
62
63
64
65
66
67
/**
 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
 * the cases where we need multiple ids with the same DW_TAG we define new IDs
 * here
 */
typedef enum custom_abbrevs {
	abbrev_void_pointer_type = 100,
	abbrev_unnamed_formal_parameter,
68
	abbrev_formal_parameter_no_location,
69
	abbrev_void_subroutine_type,
70
	abbrev_void_subprogram,
71
72
73
	abbrev_bitfield_member,
} custom_abbrevs;

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * The dwarf handle.
 */
typedef struct dwarf_t {
	const ir_entity         *cur_ent;     /**< current method entity */
	unsigned                 next_type_nr; /**< next type number */
	pmap                    *file_map;    /**< a map from file names to number in file list */
	const char             **file_list;
	const ir_entity        **pubnames_list;
	pset_new_t               emitted_types;
	const char              *main_file;   /**< name of the main source file */
	const char              *curr_file;   /**< name of the current source file */
	unsigned                 label_num;
	unsigned                 last_line;
} dwarf_t;

90
91
static dwarf_t env;

92
93
94
static dwarf_source_language language;
static const char           *comp_dir;

95
static unsigned insert_file(const char *filename)
96
97
{
	unsigned num;
98
	void    *entry = pmap_get(env.file_map, filename);
99
100
101
	if (entry != NULL) {
		return PTR_TO_INT(entry);
	}
102
103
104
	ARR_APP1(const char*, env.file_list, filename);
	num = (unsigned)ARR_LEN(env.file_list);
	pmap_insert(env.file_map, filename, INT_TO_PTR(num));
105

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	/* TODO: quote chars in string */
	be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
	return num;
}

static void emit_int32(uint32_t value)
{
	be_emit_irprintf("\t.long %u\n", value);
	be_emit_write_line();
}

static void emit_int16(uint16_t value)
{
	be_emit_irprintf("\t.short %u\n", value);
	be_emit_write_line();
}

static void emit_int8(uint8_t value)
{
	be_emit_irprintf("\t.byte %u\n", value);
	be_emit_write_line();
}

static void emit_uleb128(unsigned value)
{
	be_emit_irprintf("\t.uleb128 0x%x\n", value);
	be_emit_write_line();
}

static unsigned get_uleb128_size(unsigned value)
{
	unsigned size = 0;
	do {
		value >>= 7;
		size += 1;
	} while (value != 0);
	return size;
}

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
static void emit_sleb128(long value)
{
	be_emit_irprintf("\t.sleb128 %ld\n", value);
	be_emit_write_line();
}

static unsigned get_sleb128_size(long value)
{
	unsigned size = 0;
	do {
		value >>= 7;
		size += 1;
	} while (value != 0 && value != -1);
	return size;
}

161
162
static void emit_string(const char *string)
{
163
	/* TODO: quote special chars */
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	be_emit_irprintf("\t.asciz \"%s\"\n", string);
	be_emit_write_line();
}

static void emit_ref(const ir_entity *entity)
{
	be_emit_cstring("\t.long ");
	be_gas_emit_entity(entity);
	be_emit_char('\n');
	be_emit_write_line();
}

static void emit_string_printf(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	be_emit_cstring("\t.asciz \"");
	be_emit_irvprintf(fmt, ap);
	be_emit_cstring("\"\n");
	va_end(ap);

	be_emit_write_line();
}

static void emit_address(const char *name)
{
	be_emit_cstring("\t.long ");
	be_emit_string(be_gas_get_private_prefix());
	be_emit_string(name);
	be_emit_char('\n');
	be_emit_write_line();
}

static void emit_size(const char *from_label, const char *to_label)
{
	be_emit_cstring("\t.long ");
	be_emit_string(be_gas_get_private_prefix());
	be_emit_string(to_label);
	be_emit_cstring(" - ");
	be_emit_string(be_gas_get_private_prefix());
	be_emit_string(from_label);
	be_emit_char('\n');
	be_emit_write_line();
}

static void emit_label(const char *name)
{
	be_emit_string(be_gas_get_private_prefix());
	be_emit_string(name);
	be_emit_cstring(":\n");
	be_emit_write_line();
}

static void register_attribute(dwarf_attribute attribute, dwarf_form form)
{
	emit_uleb128(attribute);
	emit_uleb128(form);
}

static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
{
	emit_uleb128(code);
	emit_uleb128(tag);
	emit_int8(children);
}

static void end_abbrev(void)
{
	emit_uleb128(0);
	emit_uleb128(0);
}

236
static void emit_line_info(void)
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
{
	be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);

	emit_label("line_section_begin");
	/* on elf systems gas handles producing the line info for us, and we
	 * don't have to do anything */
	if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
		size_t i;
		emit_size("line_info_begin", "line_info_end");

		emit_label("line_info_begin");
		emit_int16(2); /* version */
		emit_size("line_info_prolog_begin", "line_info_prolog_end");
		emit_label("line_info_prolog_begin");
		emit_int8(1); /* len of smallest instruction TODO: query from backend */
		emit_int8(1); /* default is statement */
		emit_int8(246); /* line base */
		emit_int8(245); /* line range */
		emit_int8(10); /* opcode base */

		emit_uleb128(0);
		emit_uleb128(1);
		emit_uleb128(1);
		emit_uleb128(1);
		emit_uleb128(1);
		emit_uleb128(0);
		emit_uleb128(0);
		emit_uleb128(0);
		emit_uleb128(1);

		/* include directory list */
		emit_string("/foo/bar");
		emit_int8(0);

		/* file list */
272
273
		for (i = 0; i < ARR_LEN(env.file_list); ++i) {
			emit_string(env.file_list[i]);
274
275
276
277
278
279
280
281
282
283
284
285
286
287
			emit_uleb128(1); /* directory */
			emit_uleb128(0); /* modification time */
			emit_uleb128(0); /* file length */
		}
		emit_int8(0);

		emit_label("line_info_prolog_end");

		/* TODO: put the line_info program here */

		emit_label("line_info_end");
	}
}

288
static void emit_pubnames(void)
289
290
291
292
293
294
295
296
297
298
299
300
{
	size_t i;

	be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);

	emit_size("pubnames_begin", "pubnames_end");
	emit_label("pubnames_begin");

	emit_int16(2); /* version */
	emit_size("info_section_begin", "info_begin");
	emit_size("compile_unit_begin", "compile_unit_end");

301
302
	for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
		const ir_entity *entity = env.pubnames_list[i];
303
304
305
306
307
308
309
310
311
312
		be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
		                 be_gas_get_private_prefix(),
		                 get_entity_nr(entity), be_gas_get_private_prefix());
		emit_string(get_entity_name(entity));
	}
	emit_int32(0);

	emit_label("pubnames_end");
}

313
void be_dwarf_location(dbg_info *dbgi)
314
{
315
316
	src_loc_t loc;
	unsigned  filenum;
317

318
319
320
	if (debug_level < LEVEL_LOCATIONS)
		return;
	loc = ir_retrieve_dbg_info(dbgi);
321
	if (!loc.file)
322
323
		return;

324
	filenum = insert_file(loc.file);
325
	be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
326
327
328
	be_emit_write_line();
}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
void be_dwarf_callframe_register(const arch_register_t *reg)
{
	if (debug_level < LEVEL_FRAMEINFO)
		return;
	be_emit_cstring("\t.cfi_def_cfa_register ");
	be_emit_irprintf("%d\n", reg->dwarf_number);
	be_emit_write_line();
}

void be_dwarf_callframe_offset(int offset)
{
	if (debug_level < LEVEL_FRAMEINFO)
		return;
	be_emit_cstring("\t.cfi_def_cfa_offset ");
	be_emit_irprintf("%d\n", offset);
	be_emit_write_line();
}

void be_dwarf_callframe_spilloffset(const arch_register_t *reg, int offset)
{
	if (debug_level < LEVEL_FRAMEINFO)
		return;
	be_emit_cstring("\t.cfi_offset ");
	be_emit_irprintf("%d, %d\n", reg->dwarf_number, offset);
	be_emit_write_line();
}

356
357
358
359
360
361
362
363
364
365
366
367
368
369
static bool is_extern_entity(const ir_entity *entity)
{
	ir_visited_t visibility = get_entity_visibility(entity);
	return visibility == ir_visibility_default
	    || visibility == ir_visibility_external;
}

static void emit_entity_label(const ir_entity *entity)
{
	be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
	                 get_entity_nr(entity));
	be_emit_write_line();
}

370
371
static void register_dbginfo_attributes(void)
{
372
373
374
	register_attribute(DW_AT_decl_file,   DW_FORM_udata);
	register_attribute(DW_AT_decl_line,   DW_FORM_udata);
	register_attribute(DW_AT_decl_column, DW_FORM_udata);
375
376
}

377
static void emit_dbginfo(const dbg_info *dbgi)
378
{
379
	src_loc_t const loc  = ir_retrieve_dbg_info(dbgi);
380
	unsigned  const file = loc.file ? insert_file(loc.file) : 0;
381
	emit_uleb128(file);
382
	emit_uleb128(loc.line);
383
	emit_uleb128(loc.column);
384
385
}

386
387
388
389
390
391
392
393
static void emit_type_address(const ir_type *type)
{
	be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
	                 be_gas_get_private_prefix(),
	                 get_type_nr(type), be_gas_get_private_prefix());
	be_emit_write_line();
}

394
395
static void emit_subprogram_abbrev(void)
{
396
	begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
397
	register_attribute(DW_AT_name,      DW_FORM_string);
398
	register_dbginfo_attributes();
399
400
401
402
	register_attribute(DW_AT_type,       DW_FORM_ref4);
	register_attribute(DW_AT_external,   DW_FORM_flag);
	register_attribute(DW_AT_low_pc,     DW_FORM_addr);
	register_attribute(DW_AT_high_pc,    DW_FORM_addr);
403
	//register_attribute(DW_AT_prototyped, DW_FORM_flag);
404
405
	if (debug_level >= LEVEL_FRAMEINFO)
		register_attribute(DW_AT_frame_base, DW_FORM_block1);
406
	end_abbrev();
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

	begin_abbrev(abbrev_void_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
	register_attribute(DW_AT_name,       DW_FORM_string);
	register_dbginfo_attributes();
	register_attribute(DW_AT_external,   DW_FORM_flag);
	register_attribute(DW_AT_low_pc,     DW_FORM_addr);
	register_attribute(DW_AT_high_pc,    DW_FORM_addr);
	//register_attribute(DW_AT_prototyped, DW_FORM_flag);
	if (debug_level >= LEVEL_FRAMEINFO)
		register_attribute(DW_AT_frame_base, DW_FORM_block1);
	end_abbrev();

	begin_abbrev(DW_TAG_formal_parameter, DW_TAG_formal_parameter,
	             DW_CHILDREN_no);
	register_attribute(DW_AT_name,      DW_FORM_string);
	register_dbginfo_attributes();
	register_attribute(DW_AT_type,      DW_FORM_ref4);
	register_attribute(DW_AT_location,  DW_FORM_block1);
	end_abbrev();

	begin_abbrev(abbrev_formal_parameter_no_location, DW_TAG_formal_parameter,
	             DW_CHILDREN_no);
	register_attribute(DW_AT_name,      DW_FORM_string);
	register_dbginfo_attributes();
	register_attribute(DW_AT_type,      DW_FORM_ref4);
	end_abbrev();
}

static void emit_type(ir_type *type);

static void emit_stack_location(long offset)
{
	unsigned size = 1 + get_sleb128_size(offset);
	emit_int8(size);
	emit_int8(DW_OP_fbreg);
	emit_sleb128(offset);
443
444
}

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
static void emit_function_parameters(const ir_entity *entity,
                                     const parameter_dbg_info_t *infos)
{
	ir_type  *type     = get_entity_type(entity);
	size_t    n_params = get_method_n_params(type);
	dbg_info *dbgi     = get_entity_dbg_info(entity);
	size_t    i;
	for (i = 0; i < n_params; ++i) {
		ir_type *param_type = get_method_param_type(type, i);

		if (infos != NULL && infos[i].entity != NULL) {
			const ir_entity *entity = infos[i].entity;
			long             offset = get_entity_offset(entity);
			emit_uleb128(DW_TAG_formal_parameter);
			emit_string_printf("arg%u", (unsigned)i);
			emit_dbginfo(dbgi);
			emit_type_address(param_type);
			emit_stack_location(offset);
		} else {
			emit_uleb128(abbrev_formal_parameter_no_location);
			emit_string_printf("arg%u", (unsigned)i);
			emit_dbginfo(dbgi);
			emit_type_address(param_type);
		}
	}
}

void be_dwarf_method_before(const ir_entity *entity,
                            const parameter_dbg_info_t *parameter_infos)
474
{
475
476
	if (debug_level < LEVEL_BASIC)
		return;
477
478
479
480
481
482
483
484
	{
	ir_type *type     = get_entity_type(entity);
	size_t   n_ress   = get_method_n_ress(type);
	size_t   n_params = get_method_n_params(type);
	size_t   i;

	(void)parameter_infos;

485
486
	be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);

487
488
489
490
491
492
493
494
495
	if (n_ress > 0) {
		ir_type *res = get_method_res_type(type, 0);
		emit_type(res);
	}
	for (i = 0; i < n_params; ++i) {
		ir_type *param_type = get_method_param_type(type, i);
		emit_type(param_type);
	}

496
	emit_entity_label(entity);
497
	emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : DW_TAG_subprogram);
498
	emit_string(get_entity_ld_name(entity));
499
	emit_dbginfo(get_entity_dbg_info(entity));
500
501
502
503
	if (n_ress > 0) {
		ir_type *res = get_method_res_type(type, 0);
		emit_type_address(res);
	}
504
505
506
507
	emit_int8(is_extern_entity(entity));
	emit_ref(entity);
	be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
	                 get_entity_ld_name(entity));
508
509
510
511
512
513
	/* frame_base prog */
	emit_int8(1);
	emit_int8(DW_OP_call_frame_cfa);

	emit_function_parameters(entity, parameter_infos);
	emit_int8(0);
514

515
	ARR_APP1(const ir_entity*, env.pubnames_list, entity);
516

517
	env.cur_ent = entity;
518
519
520
521
522
523
524
525
526
	}
}

void be_dwarf_method_begin(void)
{
	if (debug_level < LEVEL_FRAMEINFO)
		return;
	be_emit_cstring("\t.cfi_startproc\n");
	be_emit_write_line();
527
528
}

529
void be_dwarf_method_end(void)
530
{
531
532
533
	if (debug_level < LEVEL_BASIC)
		return;
	const ir_entity *entity = env.cur_ent;
534
535
536
	be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
	                 get_entity_ld_name(entity));

537
538
539
540
541
	if (debug_level >= LEVEL_FRAMEINFO) {
		be_emit_cstring("\t.cfi_endproc\n");
		be_emit_write_line();
	}
}
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596

static void emit_base_type_abbrev(void)
{
	begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
	register_attribute(DW_AT_encoding,  DW_FORM_data1);
	register_attribute(DW_AT_byte_size, DW_FORM_data1);
	register_attribute(DW_AT_name,      DW_FORM_string);
	end_abbrev();
}

static void emit_type_label(const ir_type *type)
{
	be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
	be_emit_write_line();
}

static void emit_base_type(const ir_type *type)
{
	char buf[128];
	ir_mode *mode = get_type_mode(type);
	ir_print_type(buf, sizeof(buf), type);

	emit_type_label(type);
	emit_uleb128(DW_TAG_base_type);
	if (mode_is_int(mode)) {
		/* bool hack */
		if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
			emit_int8(DW_ATE_boolean);
		} else {
			emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
		}
	} else if (mode_is_reference(mode)) {
		emit_int8(DW_ATE_address);
	} else if (mode_is_float(mode)) {
		emit_int8(DW_ATE_float);
	} else {
		panic("mode not implemented yet");
	}
	emit_int8(get_mode_size_bytes(mode));
	emit_string(buf);
}

static void emit_pointer_type_abbrev(void)
{
	begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
	register_attribute(DW_AT_type,      DW_FORM_ref4);
	register_attribute(DW_AT_byte_size, DW_FORM_data1);
	end_abbrev();

	/* for void* pointer s*/
	begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
	register_attribute(DW_AT_byte_size, DW_FORM_data1);
	end_abbrev();
}

597
static void emit_pointer_type(const ir_type *type)
598
599
600
601
602
603
{
	ir_type *points_to = get_pointer_points_to_type(type);
	unsigned size      = get_type_size_bytes(type);
	assert(size < 256);

	if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
604
		emit_type(points_to);
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626

		emit_type_label(type);
		emit_uleb128(DW_TAG_pointer_type);
		emit_type_address(points_to);
	} else {
		emit_type_label(type);
		emit_uleb128(abbrev_void_pointer_type);
	}
	emit_int8(size);
}

static void emit_array_type_abbrev(void)
{
	begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
	register_attribute(DW_AT_type, DW_FORM_ref4);
	end_abbrev();

	begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
	register_attribute(DW_AT_upper_bound, DW_FORM_udata);
	end_abbrev();
}

627
static void emit_array_type(const ir_type *type)
628
629
630
631
632
633
{
	ir_type *element_type = get_array_element_type(type);

	if (get_array_n_dimensions(type) != 1)
		panic("dwarf: multidimensional arrays no supported yet");

634
	emit_type(element_type);
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

	emit_type_label(type);
	emit_uleb128(DW_TAG_array_type);
	emit_type_address(element_type);

	if (has_array_upper_bound(type, 0)) {
		int bound = get_array_upper_bound_int(type, 0);
		emit_uleb128(DW_TAG_subrange_type);
		emit_uleb128(bound);
	}

	emit_uleb128(0);
}

static void emit_compound_type_abbrev(void)
{
	begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
	register_attribute(DW_AT_byte_size,  DW_FORM_udata);
653
	// TODO register_dbginfo_attributes();
654
655
656
657
	end_abbrev();

	begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
	register_attribute(DW_AT_byte_size,  DW_FORM_udata);
658
	// TODO register_dbginfo_attributes();
659
660
	end_abbrev();

661
662
	begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
	register_attribute(DW_AT_byte_size,  DW_FORM_udata);
663
	// TODO register_dbginfo_attributes();
664
665
	end_abbrev();

666
667
668
	begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
	register_attribute(DW_AT_type,                 DW_FORM_ref4);
	register_attribute(DW_AT_name,                 DW_FORM_string);
669
	register_dbginfo_attributes();
670
671
672
673
674
675
676
677
678
	register_attribute(DW_AT_data_member_location, DW_FORM_block1);
	end_abbrev();

	begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
	register_attribute(DW_AT_byte_size,            DW_FORM_udata);
	register_attribute(DW_AT_bit_size,             DW_FORM_udata);
	register_attribute(DW_AT_bit_offset,           DW_FORM_udata);
	register_attribute(DW_AT_type,                 DW_FORM_ref4);
	register_attribute(DW_AT_name,                 DW_FORM_string);
679
	register_dbginfo_attributes();
680
681
682
683
684
685
686
687
688
689
	register_attribute(DW_AT_data_member_location, DW_FORM_block1);
	end_abbrev();
}

static void emit_op_plus_uconst(unsigned value)
{
	emit_int8(DW_OP_plus_uconst);
	emit_uleb128(value);
}

690
static void emit_compound_type(const ir_type *type)
691
692
693
694
695
696
697
698
699
700
701
702
{
	size_t i;
	size_t n_members = get_compound_n_members(type);

	for (i = 0; i < n_members; ++i) {
		ir_entity *member      = get_compound_member(type, i);
		ir_type   *member_type = get_entity_type(member);
		if (is_Primitive_type(member_type)) {
			ir_type *base = get_primitive_base_type(member_type);
			if (base != NULL)
				member_type = base;
		}
703
		emit_type(member_type);
704
705
706
	}

	emit_type_label(type);
707
708
709
	if (is_Struct_type(type)) {
		emit_uleb128(DW_TAG_structure_type);
	} else if (is_Union_type(type)) {
710
711
		emit_uleb128(DW_TAG_union_type);
	} else {
712
713
		assert(is_Class_type(type));
		emit_uleb128(DW_TAG_class_type);
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
	}
	emit_uleb128(get_type_size_bytes(type));
	for (i = 0; i < n_members; ++i) {
		ir_entity *member      = get_compound_member(type, i);
		ir_type   *member_type = get_entity_type(member);
		int        offset      = get_entity_offset(member);
		ir_type   *base;

		if (is_Primitive_type(member_type) &&
		    (base = get_primitive_base_type(member_type))) {
		    unsigned bit_offset = get_entity_offset_bits_remainder(member);
		    unsigned base_size  = get_type_size_bytes(base);
		    ir_mode *mode       = get_type_mode(member_type);
		    unsigned bit_size   = get_mode_size_bits(mode);

			bit_offset = base_size*8 - bit_offset - bit_size;

			emit_uleb128(abbrev_bitfield_member);
			emit_uleb128(base_size);
			emit_uleb128(bit_size);
			emit_uleb128(bit_offset);
			member_type = base;
		} else {
			emit_uleb128(DW_TAG_member);
		}

		emit_type_address(member_type);
		emit_string(get_entity_name(member));
742
		emit_dbginfo(get_entity_dbg_info(member));
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
		assert(offset >= 0);
		emit_int8(1 + get_uleb128_size(offset));
		emit_op_plus_uconst(offset);
	}

	emit_int8(0);
}

static void emit_subroutine_type_abbrev(void)
{
	begin_abbrev(DW_TAG_subroutine_type,
	             DW_TAG_subroutine_type, DW_CHILDREN_yes);
	register_attribute(DW_AT_prototyped,   DW_FORM_flag);
	register_attribute(DW_AT_type,         DW_FORM_ref4);
	end_abbrev();

	begin_abbrev(abbrev_void_subroutine_type,
	             DW_TAG_subroutine_type, DW_CHILDREN_yes);
	register_attribute(DW_AT_prototyped,   DW_FORM_flag);
	end_abbrev();

	begin_abbrev(abbrev_unnamed_formal_parameter,
	             DW_TAG_formal_parameter, DW_CHILDREN_no);
	register_attribute(DW_AT_type,  DW_FORM_ref4);
	end_abbrev();
}

770
static void emit_subroutine_type(const ir_type *type)
771
772
773
774
775
776
{
	size_t n_params = get_method_n_params(type);
	size_t n_ress   = get_method_n_ress(type);
	size_t i;
	for (i = 0; i < n_params; ++i) {
		ir_type *param_type = get_method_param_type(type, i);
777
		emit_type(param_type);
778
779
780
	}
	for (i = 0; i < n_ress; ++i) {
		ir_type *res_type = get_method_res_type(type, i);
781
		emit_type(res_type);
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
	}

	emit_type_label(type);
	emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
	emit_int8(1); /* prototyped */
	if (n_ress > 0) {
		/* dwarf only supports 1 return type */
		ir_type *res_type = get_method_res_type(type, 0);
		emit_type_address(res_type);
	}

	for (i = 0; i < n_params; ++i) {
		ir_type *param_type = get_method_param_type(type, i);
		emit_uleb128(abbrev_unnamed_formal_parameter);
		emit_type_address(param_type);
	}
	emit_int8(0);
}

801
static void emit_type(ir_type *type)
802
{
803
	if (pset_new_insert(&env.emitted_types, type))
804
805
806
		return;

	switch (get_type_tpop_code(type)) {
807
808
809
	case tpo_primitive: emit_base_type(type);       break;
	case tpo_pointer:   emit_pointer_type(type);    break;
	case tpo_array:     emit_array_type(type);      break;
810
	case tpo_class:
811
	case tpo_struct:
812
813
	case tpo_union:     emit_compound_type(type);   break;
	case tpo_method:    emit_subroutine_type(type); break;
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
	default:
		panic("bedwarf: type %+F not implemented yet", type);
	}
}

static void emit_op_addr(const ir_entity *entity)
{
	emit_int8(DW_OP_addr);
	be_emit_cstring("\t.long ");
	be_gas_emit_entity(entity);
	be_emit_char('\n');
	be_emit_write_line();
}

static void emit_variable_abbrev(void)
{
	begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
	register_attribute(DW_AT_name,      DW_FORM_string);
	register_attribute(DW_AT_type,      DW_FORM_ref4);
	register_attribute(DW_AT_external,  DW_FORM_flag);
834
	register_dbginfo_attributes();
835
836
837
838
	register_attribute(DW_AT_location,  DW_FORM_block1);
	end_abbrev();
}

839
void be_dwarf_variable(const ir_entity *entity)
840
{
841
	ir_type *type = get_entity_type(entity);
842

843
844
	if (debug_level < LEVEL_BASIC)
		return;
845
846
	if (get_entity_ld_name(entity)[0] == '\0')
		return;
847
848
849
	/* skip external variables */
	if (get_entity_visibility(entity) == ir_visibility_external)
		return;
850
851
852

	be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);

853
	emit_type(type);
854
855
856

	emit_entity_label(entity);
	emit_uleb128(DW_TAG_variable);
857
	emit_string(get_entity_ld_name(entity));
858
859
	emit_type_address(type);
	emit_int8(is_extern_entity(entity));
860
	emit_dbginfo(get_entity_dbg_info(entity));
861
862
863
864
	/* DW_AT_location */
	emit_int8(5); /* block length */
	emit_op_addr(entity);

865
	ARR_APP1(const ir_entity*, env.pubnames_list, entity);
866
867
868
869
870
871
872
873
}

static void emit_compile_unit_abbrev(void)
{
	begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
	register_attribute(DW_AT_stmt_list, DW_FORM_data4);
	register_attribute(DW_AT_producer,  DW_FORM_string);
	register_attribute(DW_AT_name,      DW_FORM_string);
874
875
876
877
	if (language != 0)
		register_attribute(DW_AT_language,  DW_FORM_data2);
	if (comp_dir != NULL)
		register_attribute(DW_AT_comp_dir,  DW_FORM_string);
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
	end_abbrev();
}

static void emit_abbrev(void)
{
	/* create abbreviation for compile_unit */
	be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);

	emit_label("abbrev_begin");

	emit_compile_unit_abbrev();
	emit_variable_abbrev();
	emit_subprogram_abbrev();
	emit_base_type_abbrev();
	emit_pointer_type_abbrev();
	emit_array_type_abbrev();
	emit_compound_type_abbrev();
	emit_subroutine_type_abbrev();
	emit_uleb128(0);
}

899
void be_dwarf_unit_begin(const char *filename)
900
{
901
902
	if (debug_level < LEVEL_BASIC)
		return;
903
904
905
906
907
908
909
910
911
	emit_abbrev();

	be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
	emit_label("info_section_begin");
	emit_label("info_begin");

	/* length of compilation unit info */
	emit_size("compile_unit_begin", "compile_unit_end");
	emit_label("compile_unit_begin");
912
	emit_int16(3);   /* dwarf version */
913
	emit_address("abbrev_begin");
914
	emit_int8(4);    /* pointer size, TODO: query backend */
915
916
917
918
919
920
921
922

	/* compile_unit die */
	emit_uleb128(DW_TAG_compile_unit);
	emit_address("line_section_begin");
	emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
	                   ir_get_version_minor(),
	                   ir_get_version_revision());
	emit_string(filename);
923
	if (language != 0)
924
		emit_int16(language);
925
926
	if (comp_dir != NULL)
		emit_string(comp_dir);
927
928
929
930
931
932
933

	/* tell gas to emit cfi in debug_frame
	 * TODO: if we produce exception handling code then this should be
	 *       .eh_frame (I also wonder if bad things happen if simply always
	 *       use eh_frame) */
	be_emit_cstring("\t.cfi_sections .debug_frame\n");
	be_emit_write_line();
934
935
}

936
void be_dwarf_unit_end(void)
937
{
938
939
	if (debug_level < LEVEL_BASIC)
		return;
940
941
942
943
944
945
946
947
	be_gas_emit_switch_section(GAS_SECTION_TEXT);
	emit_label("section_end");

	be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
	emit_uleb128(0); /* end of compile_unit DIE */

	emit_label("compile_unit_end");

948
949
	emit_line_info();
	emit_pubnames();
950
951
}

952
void be_dwarf_close(void)
953
{
954
955
956
957
958
959
	if (debug_level < LEVEL_BASIC)
		return;
	pmap_destroy(env.file_map);
	DEL_ARR_F(env.file_list);
	DEL_ARR_F(env.pubnames_list);
	pset_new_destroy(&env.emitted_types);
960
961
}

962
963
/* Opens a dwarf handler */
void be_dwarf_open(void)
964
{
965
966
967
968
969
970
	if (debug_level < LEVEL_BASIC)
		return;
	env.file_map      = pmap_create();
	env.file_list     = NEW_ARR_F(const char*, 0);
	env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
	pset_new_init(&env.emitted_types);
971
}
972
973
974
975
976
977
978
979
980
981

void be_dwarf_set_source_language(dwarf_source_language new_language)
{
	language = new_language;
}

void be_dwarf_set_compilation_directory(const char *new_comp_dir)
{
	comp_dir = new_comp_dir;
}
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
void be_init_dwarf(void)
{
	static const lc_opt_enum_int_items_t level_items[] = {
		{ "none",      LEVEL_NONE },
		{ "basic",     LEVEL_BASIC },
		{ "locations", LEVEL_LOCATIONS },
		{ "frameinfo", LEVEL_FRAMEINFO },
		{ NULL,        0 }
	};
	static lc_opt_enum_int_var_t debug_level_opt = {
		&debug_level, level_items
	};
	static lc_opt_table_entry_t be_main_options[] = {
		LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
		                    &debug_level_opt),
		LC_OPT_LAST
	};
	lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
	lc_opt_add_table(be_grp, be_main_options);
}