bedwarf.c 25.8 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
10
11
12
13
14
 */

/**
 * @file
 * @brief   DWARF debugging info support
 * @author  Matthias Braun
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

15
#include "bearch.h"
16
#include "bedwarf_t.h"
Matthias Braun's avatar
Matthias Braun committed
17
#include "panic.h"
18
19
20
21
22
23
24
25
#include "obst.h"
#include "irprog.h"
#include "tv.h"
#include "xmalloc.h"
#include "pmap.h"
#include "pset_new.h"
#include "util.h"
#include "obst.h"
26
#include "array.h"
27
28
29
#include "irtools.h"
#include "lc_opts.h"
#include "lc_opts_enum.h"
30
31
32
33
#include "bemodule.h"
#include "beemitter.h"
#include "dbginfo.h"
#include "begnuas.h"
34
#include "typerep.h"
35

36
37
38
39
40
41
42
43
enum {
	LEVEL_NONE,
	LEVEL_BASIC,
	LEVEL_LOCATIONS,
	LEVEL_FRAMEINFO
};
static int debug_level = LEVEL_NONE;

44
static bool has_cfi_sections = true;
45

46
47
48
49
50
51
/**
 * 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 {
Matthias Braun's avatar
Matthias Braun committed
52
	abbrev_void_subprogram = 1,
53
54
	abbrev_subprogram,
	abbrev_formal_parameter,
55
	abbrev_unnamed_formal_parameter,
56
	abbrev_formal_parameter_no_location,
57
58
59
60
61
62
63
64
65
66
67
	abbrev_variable,
	abbrev_compile_unit,
	abbrev_base_type,
	abbrev_pointer_type,
	abbrev_void_pointer_type,
	abbrev_array_type,
	abbrev_subrange_type,
	abbrev_structure_type,
	abbrev_union_type,
	abbrev_class_type,
	abbrev_member,
68
	abbrev_bitfield_member,
69
70
	abbrev_subroutine_type,
	abbrev_void_subroutine_type,
71
72
} custom_abbrevs;

73
74
75
76
/**
 * The dwarf handle.
 */
typedef struct dwarf_t {
77
	const ir_entity  *cur_ent;      /**< current function entity */
Matthias Braun's avatar
Matthias Braun committed
78
79
80
81
82
83
84
85
86
87
	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;
88
89
} dwarf_t;

Matthias Braun's avatar
Matthias Braun committed
90
static dwarf_t               env;
91
static dwarf_source_language language;
Christoph Mallon's avatar
Christoph Mallon committed
92
static char                 *comp_dir;
93

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

104
105
106
107
	be_emit_irprintf("\t.file %u ", num);
	be_gas_emit_string_literal(filename);
	be_emit_char('\n');
	be_emit_write_line();
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
	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
163
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
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);
}

216
217
static void begin_abbrev(custom_abbrevs code, dwarf_tag tag,
                         dw_children children)
218
219
220
221
222
223
224
225
226
227
228
229
{
	emit_uleb128(code);
	emit_uleb128(tag);
	emit_int8(children);
}

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

230
static void emit_line_info(void)
231
232
233
234
{
	be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);

	emit_label("line_section_begin");
235
	/* on some systems gas handles producing the line info for us, and we don't
Matthias Braun's avatar
Matthias Braun committed
236
	 * have to do anything */
237
	if (!be_gas_produces_dwarf_line_info()) {
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
		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_int8(0);

		/* file list */
Matthias Braun's avatar
Matthias Braun committed
264
		for (size_t i = 0; i < ARR_LEN(env.file_list); ++i) {
265
			be_gas_emit_cstring(env.file_list[i]);
266
267
268
269
270
271
272
273
274
275
276
277
278
279
			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");
	}
}

280
static void emit_pubnames(void)
281
282
283
284
285
286
287
288
289
290
{
	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");

Matthias Braun's avatar
Matthias Braun committed
291
	for (size_t i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
292
		const ir_entity *entity = env.pubnames_list[i];
293
294
295
		be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
		                 be_gas_get_private_prefix(),
		                 get_entity_nr(entity), be_gas_get_private_prefix());
296
		be_gas_emit_cstring(get_entity_name(entity));
297
298
299
300
301
302
	}
	emit_int32(0);

	emit_label("pubnames_end");
}

303
void be_dwarf_location(dbg_info *dbgi)
304
{
305
306
	if (debug_level < LEVEL_LOCATIONS)
		return;
Matthias Braun's avatar
Matthias Braun committed
307
	src_loc_t loc = ir_retrieve_dbg_info(dbgi);
308
	if (!loc.file)
309
310
		return;

Matthias Braun's avatar
Matthias Braun committed
311
	unsigned filenum = insert_file(loc.file);
312
	be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
313
314
315
	be_emit_write_line();
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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();
}

343
344
345
static bool is_extern_entity(const ir_entity *entity)
{
	ir_visited_t visibility = get_entity_visibility(entity);
Matthias Braun's avatar
Matthias Braun committed
346
	return visibility == ir_visibility_external;
347
348
349
350
351
352
353
354
355
}

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();
}

356
357
static void register_dbginfo_attributes(void)
{
358
359
360
	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);
361
362
}

363
static void emit_dbginfo(const dbg_info *dbgi)
364
{
365
	src_loc_t const loc  = ir_retrieve_dbg_info(dbgi);
366
	unsigned  const file = loc.file ? insert_file(loc.file) : 0;
367
	emit_uleb128(file);
368
	emit_uleb128(loc.line);
369
	emit_uleb128(loc.column);
370
371
}

372
373
374
375
376
377
378
379
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();
}

380
381
static void emit_subprogram_abbrev(void)
{
382
	begin_abbrev(abbrev_subprogram, DW_TAG_subprogram, DW_CHILDREN_yes);
383
	register_attribute(DW_AT_name,      DW_FORM_string);
384
	register_dbginfo_attributes();
385
386
387
388
	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);
389
	//register_attribute(DW_AT_prototyped, DW_FORM_flag);
390
391
	if (debug_level >= LEVEL_FRAMEINFO)
		register_attribute(DW_AT_frame_base, DW_FORM_block1);
392
	end_abbrev();
393
394
395
396
397
398
399
400
401
402
403
404

	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();

405
	begin_abbrev(abbrev_formal_parameter, DW_TAG_formal_parameter,
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
	             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);
429
430
}

431
432
433
static void emit_function_parameters(const ir_entity *entity,
                                     const parameter_dbg_info_t *infos)
{
Matthias Braun's avatar
Matthias Braun committed
434
435
	const ir_type *type = get_entity_type(entity);
	dbg_info      *dbgi = get_entity_dbg_info(entity);
Matthias Braun's avatar
Matthias Braun committed
436
437
	for (size_t i = 0, n_params = get_method_n_params(type);
	     i < n_params; ++i) {
438
439
440
		ir_type *param_type = get_method_param_type(type, i);

		if (infos != NULL && infos[i].entity != NULL) {
Christoph Mallon's avatar
Christoph Mallon committed
441
			long const offset = get_entity_offset(infos[i].entity);
442
			emit_uleb128(abbrev_formal_parameter);
443
444
445
446
447
448
449
450
451
452
453
454
455
			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);
		}
	}
}

456
457
void be_dwarf_function_before(const ir_entity *entity,
                              const parameter_dbg_info_t *parameter_infos)
458
{
459
460
	if (debug_level < LEVEL_BASIC)
		return;
461

462
463
	be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);

Matthias Braun's avatar
Matthias Braun committed
464
465
	ir_type *type   = get_entity_type(entity);
	size_t   n_ress = get_method_n_ress(type);
466
467
468
469
	if (n_ress > 0) {
		ir_type *res = get_method_res_type(type, 0);
		emit_type(res);
	}
Matthias Braun's avatar
Matthias Braun committed
470
471
	for (size_t i = 0, n_params = get_method_n_params(type);
	     i < n_params; ++i) {
472
473
474
475
		ir_type *param_type = get_method_param_type(type, i);
		emit_type(param_type);
	}

476
	emit_entity_label(entity);
477
	emit_uleb128(n_ress == 0 ? abbrev_void_subprogram : abbrev_subprogram);
478
	be_gas_emit_cstring(get_entity_ld_name(entity));
479
	emit_dbginfo(get_entity_dbg_info(entity));
480
481
482
483
	if (n_ress > 0) {
		ir_type *res = get_method_res_type(type, 0);
		emit_type_address(res);
	}
484
485
	emit_int8(is_extern_entity(entity));
	emit_ref(entity);
486
	be_emit_irprintf("\t.long %sfunction_end_%s\n", be_gas_get_private_prefix(),
487
	                 get_entity_ld_name(entity));
488
489
490
491
492
493
	/* frame_base prog */
	emit_int8(1);
	emit_int8(DW_OP_call_frame_cfa);

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

495
	ARR_APP1(const ir_entity*, env.pubnames_list, entity);
496

497
	env.cur_ent = entity;
498
499
}

500
void be_dwarf_function_begin(void)
501
502
503
504
505
{
	if (debug_level < LEVEL_FRAMEINFO)
		return;
	be_emit_cstring("\t.cfi_startproc\n");
	be_emit_write_line();
506
507
}

508
void be_dwarf_function_end(void)
509
{
510
511
512
	if (debug_level < LEVEL_BASIC)
		return;
	const ir_entity *entity = env.cur_ent;
513
	be_emit_irprintf("%sfunction_end_%s:\n", be_gas_get_private_prefix(),
514
515
	                 get_entity_ld_name(entity));

516
517
518
519
520
	if (debug_level >= LEVEL_FRAMEINFO) {
		be_emit_cstring("\t.cfi_endproc\n");
		be_emit_write_line();
	}
}
521
522
523

static void emit_base_type_abbrev(void)
{
524
	begin_abbrev(abbrev_base_type, DW_TAG_base_type, DW_CHILDREN_no);
525
526
527
528
529
530
531
532
	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)
{
Matthias Braun's avatar
Matthias Braun committed
533
534
	be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(),
	                 get_type_nr(type));
535
536
537
538
539
540
541
542
543
	be_emit_write_line();
}

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

	emit_type_label(type);
544
	emit_uleb128(abbrev_base_type);
Matthias Braun's avatar
Matthias Braun committed
545
	ir_mode *mode = get_type_mode(type);
546
547
	if (mode_is_int(mode)) {
		/* bool hack */
Christoph Mallon's avatar
Christoph Mallon committed
548
		if (streq(buf, "_Bool") || streq(buf, "bool")) {
549
550
551
552
553
554
555
556
557
558
559
560
			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));
561
	be_gas_emit_cstring(buf);
562
563
564
565
}

static void emit_pointer_type_abbrev(void)
{
566
	begin_abbrev(abbrev_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
567
568
569
570
571
572
573
574
575
576
	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();
}

577
static void emit_pointer_type(const ir_type *type)
578
579
{
	ir_type *points_to = get_pointer_points_to_type(type);
580
	unsigned size      = get_type_size(type);
581
582
583
	assert(size < 256);

	if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
584
		emit_type(points_to);
585
586

		emit_type_label(type);
587
		emit_uleb128(abbrev_pointer_type);
588
589
590
591
592
593
594
595
596
597
		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)
{
598
	begin_abbrev(abbrev_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
599
600
601
	register_attribute(DW_AT_type, DW_FORM_ref4);
	end_abbrev();

602
	begin_abbrev(abbrev_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
603
604
605
606
	register_attribute(DW_AT_upper_bound, DW_FORM_udata);
	end_abbrev();
}

607
static void emit_array_type(const ir_type *type)
608
{
Matthias Braun's avatar
Matthias Braun committed
609
	ir_type *element_type = get_array_element_type(type);
610
	emit_type(element_type);
611
612

	emit_type_label(type);
613
	emit_uleb128(abbrev_array_type);
614
615
	emit_type_address(element_type);

Christoph Mallon's avatar
Christoph Mallon committed
616
617
	unsigned const bound = get_array_size(type);
	if (bound != 0) {
618
		emit_uleb128(abbrev_subrange_type);
619
620
621
622
623
624
625
626
		emit_uleb128(bound);
	}

	emit_uleb128(0);
}

static void emit_compound_type_abbrev(void)
{
627
	begin_abbrev(abbrev_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
628
	register_attribute(DW_AT_byte_size,  DW_FORM_udata);
629
	// TODO register_dbginfo_attributes();
630
631
	end_abbrev();

632
	begin_abbrev(abbrev_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
633
	register_attribute(DW_AT_byte_size,  DW_FORM_udata);
634
	// TODO register_dbginfo_attributes();
635
636
	end_abbrev();

637
	begin_abbrev(abbrev_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
638
	register_attribute(DW_AT_byte_size,  DW_FORM_udata);
639
	// TODO register_dbginfo_attributes();
640
641
	end_abbrev();

642
	begin_abbrev(abbrev_member, DW_TAG_member, DW_CHILDREN_no);
643
644
	register_attribute(DW_AT_type,                 DW_FORM_ref4);
	register_attribute(DW_AT_name,                 DW_FORM_string);
645
	register_dbginfo_attributes();
646
647
648
649
650
651
652
653
654
	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);
655
	register_dbginfo_attributes();
656
657
658
659
660
661
662
663
664
665
	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);
}

666
static void emit_compound_type(const ir_type *type)
667
668
{
	size_t n_members = get_compound_n_members(type);
Matthias Braun's avatar
Matthias Braun committed
669
	for (size_t i = 0; i < n_members; ++i) {
670
671
		ir_entity *member      = get_compound_member(type, i);
		ir_type   *member_type = get_entity_type(member);
672
		emit_type(member_type);
673
674
675
	}

	emit_type_label(type);
676
	if (is_Struct_type(type)) {
677
		emit_uleb128(abbrev_structure_type);
678
	} else if (is_Union_type(type)) {
679
		emit_uleb128(abbrev_union_type);
680
	} else {
681
		assert(is_Class_type(type));
682
		emit_uleb128(abbrev_class_type);
683
	}
684
	emit_uleb128(get_type_size(type));
Matthias Braun's avatar
Matthias Braun committed
685
	for (size_t i = 0; i < n_members; ++i) {
686
687
688
689
		ir_entity *member      = get_compound_member(type, i);
		ir_type   *member_type = get_entity_type(member);
		int        offset      = get_entity_offset(member);

690
		if (get_entity_bitfield_size(member) > 0) {
691
692
			unsigned bit_offset = get_entity_bitfield_offset(member);
			unsigned bit_size   = get_entity_bitfield_size(member);
693
			unsigned base_size  = get_type_size(member_type);
694
695
696
697
698
699
700
701

			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);
		} else {
702
			emit_uleb128(abbrev_member);
703
704
705
		}

		emit_type_address(member_type);
706
		be_gas_emit_cstring(get_entity_name(member));
707
		emit_dbginfo(get_entity_dbg_info(member));
708
709
710
711
712
713
714
715
716
717
		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)
{
718
	begin_abbrev(abbrev_subroutine_type,
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
	             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();
}

735
static void emit_subroutine_type(const ir_type *type)
736
737
738
{
	size_t n_params = get_method_n_params(type);
	size_t n_ress   = get_method_n_ress(type);
Matthias Braun's avatar
Matthias Braun committed
739
	for (size_t i = 0; i < n_params; ++i) {
740
		ir_type *param_type = get_method_param_type(type, i);
741
		emit_type(param_type);
742
	}
Matthias Braun's avatar
Matthias Braun committed
743
	for (size_t i = 0; i < n_ress; ++i) {
744
		ir_type *res_type = get_method_res_type(type, i);
745
		emit_type(res_type);
746
747
748
	}

	emit_type_label(type);
Matthias Braun's avatar
Matthias Braun committed
749
750
	emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type
	                         : abbrev_subroutine_type);
751
752
753
754
755
756
757
	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);
	}

Matthias Braun's avatar
Matthias Braun committed
758
	for (size_t i = 0; i < n_params; ++i) {
759
760
761
762
763
764
765
		ir_type *param_type = get_method_param_type(type, i);
		emit_uleb128(abbrev_unnamed_formal_parameter);
		emit_type_address(param_type);
	}
	emit_int8(0);
}

766
static void emit_type(ir_type *type)
767
{
768
	if (!pset_new_insert(&env.emitted_types, type))
769
770
		return;

771
772
773
774
	switch (get_type_opcode(type)) {
	case tpo_primitive: emit_base_type(type);       return;
	case tpo_pointer:   emit_pointer_type(type);    return;
	case tpo_array:     emit_array_type(type);      return;
775
	case tpo_class:
776
	case tpo_struct:
777
778
	case tpo_union:     emit_compound_type(type);   return;
	case tpo_method:    emit_subroutine_type(type); return;
779
	case tpo_segment:
780
781
782
783
	case tpo_code:
	case tpo_unknown:
	case tpo_uninitialized:
		panic("unexpected type %+F", type);
784
	}
785
	panic("invalid type");
786
787
788
789
790
791
792
793
794
795
796
797
798
}

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)
{
799
	begin_abbrev(abbrev_variable, DW_TAG_variable, DW_CHILDREN_no);
800
801
802
	register_attribute(DW_AT_name,      DW_FORM_string);
	register_attribute(DW_AT_type,      DW_FORM_ref4);
	register_attribute(DW_AT_external,  DW_FORM_flag);
803
	register_dbginfo_attributes();
804
805
806
807
	register_attribute(DW_AT_location,  DW_FORM_block1);
	end_abbrev();
}

808
void be_dwarf_variable(const ir_entity *entity)
809
{
810
811
	if (debug_level < LEVEL_BASIC)
		return;
812
813
	if (get_entity_ld_name(entity)[0] == '\0')
		return;
814
	if (!entity_has_definition(entity))
815
		return;
816
817
818

	be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);

Matthias Braun's avatar
Matthias Braun committed
819
	ir_type *type = get_entity_type(entity);
820
	emit_type(type);
821
822

	emit_entity_label(entity);
823
	emit_uleb128(abbrev_variable);
824
	be_gas_emit_cstring(get_entity_ld_name(entity));
825
826
	emit_type_address(type);
	emit_int8(is_extern_entity(entity));
827
	emit_dbginfo(get_entity_dbg_info(entity));
828
829
830
831
	/* DW_AT_location */
	emit_int8(5); /* block length */
	emit_op_addr(entity);

832
	ARR_APP1(const ir_entity*, env.pubnames_list, entity);
833
834
835
836
}

static void emit_compile_unit_abbrev(void)
{
837
	begin_abbrev(abbrev_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
838
839
840
	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);
841
842
843
844
	if (language != 0)
		register_attribute(DW_AT_language,  DW_FORM_data2);
	if (comp_dir != NULL)
		register_attribute(DW_AT_comp_dir,  DW_FORM_string);
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
	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);
}

866
void be_dwarf_unit_begin(const char *filename)
867
{
868
869
	if (debug_level < LEVEL_BASIC)
		return;
870
871
872
873
874
875
876
	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 */
Matthias Braun's avatar
Matthias Braun committed
877
	const backend_params *be_params = be_get_backend_param();
878
879
	emit_size("compile_unit_begin", "compile_unit_end");
	emit_label("compile_unit_begin");
880
	emit_int16(3);   /* dwarf version */
881
	emit_address("abbrev_begin");
882
	emit_int8(be_params->machine_size / 8); /* pointer size */
883
884

	/* compile_unit die */
885
	emit_uleb128(abbrev_compile_unit);
886
887
	emit_address("line_section_begin");
	emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
888
	                   ir_get_version_minor(), ir_get_version_revision());
889
	be_gas_emit_cstring(filename);
890
	if (language != 0)
891
		emit_int16(language);
892
	if (comp_dir != NULL)
893
		be_gas_emit_cstring(comp_dir);
894

895
896
897
898
899
900
901
902
	if (has_cfi_sections) {
		/* 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();
	}
903
904
}

905
void be_dwarf_unit_end(void)
906
{
907
908
	if (debug_level < LEVEL_BASIC)
		return;
909
910
911
912
913
914
915
916
	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");

917
918
	emit_line_info();
	emit_pubnames();
919
920
}

921
void be_dwarf_close(void)
922
{
923
924
925
926
927
928
	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);
929
930
}

931
932
/* Opens a dwarf handler */
void be_dwarf_open(void)
933
{
934
935
936
937
938
939
	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);
940
}
941
942
943
944
945
946
947
948

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)
{
949
	free(comp_dir);
Matthias Braun's avatar
Matthias Braun committed
950
	comp_dir = xstrdup(new_comp_dir);
951
}
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972

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);
973
974
975
976
977
978
979
980
981

	static lc_opt_table_entry_t dwarf_options[] = {
		LC_OPT_ENT_BOOL("has_cfi_sections",
		                "assembler supports .cfi_sections directive",
		                &has_cfi_sections),
		LC_OPT_LAST
	};
	lc_opt_entry_t *dwarf_grp = lc_opt_get_grp(be_grp, "dwarf");
	lc_opt_add_table(dwarf_grp, dwarf_options);
982
}