blob: 65b83bfedf60866d2f941c9ee1be295a4427e1ad [file] [log] [blame]
Jim Laskeye5032892005-12-21 19:48:16 +00001//===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by James M. Laskey and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
Jim Laskeyb2efb852006-01-04 22:28:25 +000014#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskeye5032892005-12-21 19:48:16 +000015
Jim Laskey063e7652006-01-17 17:31:53 +000016#include "llvm/ADT/StringExtras.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000017#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000018#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000019#include "llvm/Support/CommandLine.h"
20
Jim Laskeyb2efb852006-01-04 22:28:25 +000021#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000022
Jim Laskeyb2efb852006-01-04 22:28:25 +000023using namespace llvm;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000024
25static cl::opt<bool>
26DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskey063e7652006-01-17 17:31:53 +000027 cl::desc("Add comments to Dwarf directives."));
28
29//===----------------------------------------------------------------------===//
30// Dwarf abbreviations used by this emitter.
31//
32
33static const unsigned char AbbrevTAG_compile_unit[] = {
34 DW_TAG_compile_unit, DW_CHILDREN_yes,
35 DW_AT_stmt_list, DW_FORM_data4,
36 DW_AT_high_pc, DW_FORM_addr,
37 DW_AT_low_pc, DW_FORM_addr,
38 DW_AT_producer, DW_FORM_string,
39 DW_AT_language, DW_FORM_data1,
40 DW_AT_name, DW_FORM_string,
41 DW_AT_comp_dir, DW_FORM_string,
42 0, 0
43};
44
45static const unsigned char AbbrevTAG_subprogram[] = {
46 DW_TAG_subprogram, DW_CHILDREN_yes,
47 DW_AT_sibling, DW_FORM_ref4,
48 DW_AT_external, DW_FORM_flag,
49 DW_AT_name, DW_FORM_string,
50 DW_AT_decl_file, DW_FORM_data1,
51 DW_AT_decl_line, DW_FORM_data1,
52 DW_AT_prototyped, DW_FORM_flag,
53 DW_AT_type, DW_FORM_ref4,
54 DW_AT_low_pc, DW_FORM_addr,
55 DW_AT_high_pc, DW_FORM_addr,
56 DW_AT_frame_base, DW_FORM_block1,
57 0, 0
58};
59
60static const unsigned char AbbrevTAG_formal_parameter[] = {
61 DW_TAG_formal_parameter, DW_CHILDREN_no,
62 DW_AT_name, DW_FORM_string,
63 DW_AT_decl_file, DW_FORM_data1,
64 DW_AT_decl_line, DW_FORM_data1,
65 DW_AT_type, DW_FORM_ref4,
66 DW_AT_location, DW_FORM_block1,
67 0, 0
68};
69
70static const unsigned char AbbrevTAG_base_type[] = {
71 DW_TAG_base_type, DW_CHILDREN_no,
72 DW_AT_name, DW_FORM_string,
73 DW_AT_byte_size, DW_FORM_data1,
74 DW_AT_encoding, DW_FORM_data1,
75 0, 0
76};
77
78static const unsigned char AbbrevTAG_pointer_type[] = {
79 DW_TAG_pointer_type, DW_CHILDREN_no,
80 DW_AT_byte_size, DW_FORM_data1,
81 DW_AT_type, DW_FORM_ref4,
82 0, 0
83};
84
85static const unsigned char AbbrevTAG_array_type[] = {
86 DW_TAG_array_type, DW_CHILDREN_yes,
87 DW_AT_sibling, DW_FORM_ref4,
88 DW_AT_type, DW_FORM_ref4,
89 0, 0
90};
91
92static const unsigned char AbbrevTAG_subrange_type[] = {
93 DW_TAG_subrange_type, DW_CHILDREN_no,
94 0, 0
95};
96
97static const unsigned char AbbrevTAG_variable[] = {
98 DW_TAG_variable, DW_CHILDREN_no,
99 DW_AT_name, DW_FORM_string,
100 DW_AT_type, DW_FORM_ref4,
101 DW_AT_external, DW_FORM_flag,
102 DW_AT_artificial, DW_FORM_flag,
103 DW_AT_declaration, DW_FORM_flag,
104 0, 0
105};
106
107//===----------------------------------------------------------------------===//
108
109/// TagString - Return the string for the specified tag.
110///
111static const char *TagString(unsigned Tag) {
112 switch(Tag) {
113 case DW_TAG_array_type: return "TAG_array_type";
114 case DW_TAG_class_type: return "TAG_class_type";
115 case DW_TAG_entry_point: return "TAG_entry_point";
116 case DW_TAG_enumeration_type: return "TAG_enumeration_type";
117 case DW_TAG_formal_parameter: return "TAG_formal_parameter";
118 case DW_TAG_imported_declaration: return "TAG_imported_declaration";
119 case DW_TAG_label: return "TAG_label";
120 case DW_TAG_lexical_block: return "TAG_lexical_block";
121 case DW_TAG_member: return "TAG_member";
122 case DW_TAG_pointer_type: return "TAG_pointer_type";
123 case DW_TAG_reference_type: return "TAG_reference_type";
124 case DW_TAG_compile_unit: return "TAG_compile_unit";
125 case DW_TAG_string_type: return "TAG_string_type";
126 case DW_TAG_structure_type: return "TAG_structure_type";
127 case DW_TAG_subroutine_type: return "TAG_subroutine_type";
128 case DW_TAG_typedef: return "TAG_typedef";
129 case DW_TAG_union_type: return "TAG_union_type";
130 case DW_TAG_unspecified_parameters: return "TAG_unspecified_parameters";
131 case DW_TAG_variant: return "TAG_variant";
132 case DW_TAG_common_block: return "TAG_common_block";
133 case DW_TAG_common_inclusion: return "TAG_common_inclusion";
134 case DW_TAG_inheritance: return "TAG_inheritance";
135 case DW_TAG_inlined_subroutine: return "TAG_inlined_subroutine";
136 case DW_TAG_module: return "TAG_module";
137 case DW_TAG_ptr_to_member_type: return "TAG_ptr_to_member_type";
138 case DW_TAG_set_type: return "TAG_set_type";
139 case DW_TAG_subrange_type: return "TAG_subrange_type";
140 case DW_TAG_with_stmt: return "TAG_with_stmt";
141 case DW_TAG_access_declaration: return "TAG_access_declaration";
142 case DW_TAG_base_type: return "TAG_base_type";
143 case DW_TAG_catch_block: return "TAG_catch_block";
144 case DW_TAG_const_type: return "TAG_const_type";
145 case DW_TAG_constant: return "TAG_constant";
146 case DW_TAG_enumerator: return "TAG_enumerator";
147 case DW_TAG_file_type: return "TAG_file_type";
148 case DW_TAG_friend: return "TAG_friend";
149 case DW_TAG_namelist: return "TAG_namelist";
150 case DW_TAG_namelist_item: return "TAG_namelist_item";
151 case DW_TAG_packed_type: return "TAG_packed_type";
152 case DW_TAG_subprogram: return "TAG_subprogram";
153 case DW_TAG_template_type_parameter: return "TAG_template_type_parameter";
154 case DW_TAG_template_value_parameter: return "TAG_template_value_parameter";
155 case DW_TAG_thrown_type: return "TAG_thrown_type";
156 case DW_TAG_try_block: return "TAG_try_block";
157 case DW_TAG_variant_part: return "TAG_variant_part";
158 case DW_TAG_variable: return "TAG_variable";
159 case DW_TAG_volatile_type: return "TAG_volatile_type";
160 case DW_TAG_dwarf_procedure: return "TAG_dwarf_procedure";
161 case DW_TAG_restrict_type: return "TAG_restrict_type";
162 case DW_TAG_interface_type: return "TAG_interface_type";
163 case DW_TAG_namespace: return "TAG_namespace";
164 case DW_TAG_imported_module: return "TAG_imported_module";
165 case DW_TAG_unspecified_type: return "TAG_unspecified_type";
166 case DW_TAG_partial_unit: return "TAG_partial_unit";
167 case DW_TAG_imported_unit: return "TAG_imported_unit";
168 case DW_TAG_condition: return "TAG_condition";
169 case DW_TAG_shared_type: return "TAG_shared_type";
170 case DW_TAG_lo_user: return "TAG_lo_user";
171 case DW_TAG_hi_user: return "TAG_hi_user";
172 }
173 assert(0 && "Unknown Dwarf Tag");
174 return "";
175}
176
177/// ChildrenString - Return the string for the specified children flag.
178///
179static const char *ChildrenString(unsigned Children) {
180 switch(Children) {
181 case DW_CHILDREN_no: return "CHILDREN_no";
182 case DW_CHILDREN_yes: return "CHILDREN_yes";
183 }
184 assert(0 && "Unknown Dwarf ChildrenFlag");
185 return "";
186}
187
188/// AttributeString - Return the string for the specified attribute.
189///
190static const char *AttributeString(unsigned Attribute) {
191 switch(Attribute) {
192 case DW_AT_sibling: return "AT_sibling";
193 case DW_AT_location: return "AT_location";
194 case DW_AT_name: return "AT_name";
195 case DW_AT_ordering: return "AT_ordering";
196 case DW_AT_byte_size: return "AT_byte_size";
197 case DW_AT_bit_offset: return "AT_bit_offset";
198 case DW_AT_bit_size: return "AT_bit_size";
199 case DW_AT_stmt_list: return "AT_stmt_list";
200 case DW_AT_low_pc: return "AT_low_pc";
201 case DW_AT_high_pc: return "AT_high_pc";
202 case DW_AT_language: return "AT_language";
203 case DW_AT_discr: return "AT_discr";
204 case DW_AT_discr_value: return "AT_discr_value";
205 case DW_AT_visibility: return "AT_visibility";
206 case DW_AT_import: return "AT_import";
207 case DW_AT_string_length: return "AT_string_length";
208 case DW_AT_common_reference: return "AT_common_reference";
209 case DW_AT_comp_dir: return "AT_comp_dir";
210 case DW_AT_const_value: return "AT_const_value";
211 case DW_AT_containing_type: return "AT_containing_type";
212 case DW_AT_default_value: return "AT_default_value";
213 case DW_AT_inline: return "AT_inline";
214 case DW_AT_is_optional: return "AT_is_optional";
215 case DW_AT_lower_bound: return "AT_lower_bound";
216 case DW_AT_producer: return "AT_producer";
217 case DW_AT_prototyped: return "AT_prototyped";
218 case DW_AT_return_addr: return "AT_return_addr";
219 case DW_AT_start_scope: return "AT_start_scope";
220 case DW_AT_bit_stride: return "AT_bit_stride";
221 case DW_AT_upper_bound: return "AT_upper_bound";
222 case DW_AT_abstract_origin: return "AT_abstract_origin";
223 case DW_AT_accessibility: return "AT_accessibility";
224 case DW_AT_address_class: return "AT_address_class";
225 case DW_AT_artificial: return "AT_artificial";
226 case DW_AT_base_types: return "AT_base_types";
227 case DW_AT_calling_convention: return "AT_calling_convention";
228 case DW_AT_count: return "AT_count";
229 case DW_AT_data_member_location: return "AT_data_member_location";
230 case DW_AT_decl_column: return "AT_decl_column";
231 case DW_AT_decl_file: return "AT_decl_file";
232 case DW_AT_decl_line: return "AT_decl_line";
233 case DW_AT_declaration: return "AT_declaration";
234 case DW_AT_discr_list: return "AT_discr_list";
235 case DW_AT_encoding: return "AT_encoding";
236 case DW_AT_external: return "AT_external";
237 case DW_AT_frame_base: return "AT_frame_base";
238 case DW_AT_friend: return "AT_friend";
239 case DW_AT_identifier_case: return "AT_identifier_case";
240 case DW_AT_macro_info: return "AT_macro_info";
241 case DW_AT_namelist_item: return "AT_namelist_item";
242 case DW_AT_priority: return "AT_priority";
243 case DW_AT_segment: return "AT_segment";
244 case DW_AT_specification: return "AT_specification";
245 case DW_AT_static_link: return "AT_static_link";
246 case DW_AT_type: return "AT_type";
247 case DW_AT_use_location: return "AT_use_location";
248 case DW_AT_variable_parameter: return "AT_variable_parameter";
249 case DW_AT_virtuality: return "AT_virtuality";
250 case DW_AT_vtable_elem_location: return "AT_vtable_elem_location";
251 case DW_AT_allocated: return "AT_allocated";
252 case DW_AT_associated: return "AT_associated";
253 case DW_AT_data_location: return "AT_data_location";
254 case DW_AT_byte_stride: return "AT_byte_stride";
255 case DW_AT_entry_pc: return "AT_entry_pc";
256 case DW_AT_use_UTF8: return "AT_use_UTF8";
257 case DW_AT_extension: return "AT_extension";
258 case DW_AT_ranges: return "AT_ranges";
259 case DW_AT_trampoline: return "AT_trampoline";
260 case DW_AT_call_column: return "AT_call_column";
261 case DW_AT_call_file: return "AT_call_file";
262 case DW_AT_call_line: return "AT_call_line";
263 case DW_AT_description: return "AT_description";
264 case DW_AT_binary_scale: return "AT_binary_scale";
265 case DW_AT_decimal_scale: return "AT_decimal_scale";
266 case DW_AT_small: return "AT_small";
267 case DW_AT_decimal_sign: return "AT_decimal_sign";
268 case DW_AT_digit_count: return "AT_digit_count";
269 case DW_AT_picture_string: return "AT_picture_string";
270 case DW_AT_mutable: return "AT_mutable";
271 case DW_AT_threads_scaled: return "AT_threads_scaled";
272 case DW_AT_explicit: return "AT_explicit";
273 case DW_AT_object_pointer: return "AT_object_pointer";
274 case DW_AT_endianity: return "AT_endianity";
275 case DW_AT_elemental: return "AT_elemental";
276 case DW_AT_pure: return "AT_pure";
277 case DW_AT_recursive: return "AT_recursive";
278 case DW_AT_lo_user: return "AT_lo_user";
279 case DW_AT_hi_user: return "AT_hi_user";
280 }
281 assert(0 && "Unknown Dwarf Attribute");
282 return "";
283}
284
285/// FormEncodingString - Return the string for the specified form encoding.
286///
287static const char *FormEncodingString(unsigned Encoding) {
288 switch(Encoding) {
289 case DW_FORM_addr: return "FORM_addr";
290 case DW_FORM_block2: return "FORM_block2";
291 case DW_FORM_block4: return "FORM_block4";
292 case DW_FORM_data2: return "FORM_data2";
293 case DW_FORM_data4: return "FORM_data4";
294 case DW_FORM_data8: return "FORM_data8";
295 case DW_FORM_string: return "FORM_string";
296 case DW_FORM_block: return "FORM_block";
297 case DW_FORM_block1: return "FORM_block1";
298 case DW_FORM_data1: return "FORM_data1";
299 case DW_FORM_flag: return "FORM_flag";
300 case DW_FORM_sdata: return "FORM_sdata";
301 case DW_FORM_strp: return "FORM_strp";
302 case DW_FORM_udata: return "FORM_udata";
303 case DW_FORM_ref_addr: return "FORM_ref_addr";
304 case DW_FORM_ref1: return "FORM_ref1";
305 case DW_FORM_ref2: return "FORM_ref2";
306 case DW_FORM_ref4: return "FORM_ref4";
307 case DW_FORM_ref8: return "FORM_ref8";
308 case DW_FORM_ref_udata: return "FORM_ref_udata";
309 case DW_FORM_indirect: return "FORM_indirect";
310 }
311 assert(0 && "Unknown Dwarf Form Encoding");
312 return "";
313}
314
315/// OperationEncodingString - Return the string for the specified operation
316/// encoding.
317static const char *OperationEncodingString(unsigned Encoding) {
318 switch(Encoding) {
319 case DW_OP_addr: return "OP_addr";
320 case DW_OP_deref: return "OP_deref";
321 case DW_OP_const1u: return "OP_const1u";
322 case DW_OP_const1s: return "OP_const1s";
323 case DW_OP_const2u: return "OP_const2u";
324 case DW_OP_const2s: return "OP_const2s";
325 case DW_OP_const4u: return "OP_const4u";
326 case DW_OP_const4s: return "OP_const4s";
327 case DW_OP_const8u: return "OP_const8u";
328 case DW_OP_const8s: return "OP_const8s";
329 case DW_OP_constu: return "OP_constu";
330 case DW_OP_consts: return "OP_consts";
331 case DW_OP_dup: return "OP_dup";
332 case DW_OP_drop: return "OP_drop";
333 case DW_OP_over: return "OP_over";
334 case DW_OP_pick: return "OP_pick";
335 case DW_OP_swap: return "OP_swap";
336 case DW_OP_rot: return "OP_rot";
337 case DW_OP_xderef: return "OP_xderef";
338 case DW_OP_abs: return "OP_abs";
339 case DW_OP_and: return "OP_and";
340 case DW_OP_div: return "OP_div";
341 case DW_OP_minus: return "OP_minus";
342 case DW_OP_mod: return "OP_mod";
343 case DW_OP_mul: return "OP_mul";
344 case DW_OP_neg: return "OP_neg";
345 case DW_OP_not: return "OP_not";
346 case DW_OP_or: return "OP_or";
347 case DW_OP_plus: return "OP_plus";
348 case DW_OP_plus_uconst: return "OP_plus_uconst";
349 case DW_OP_shl: return "OP_shl";
350 case DW_OP_shr: return "OP_shr";
351 case DW_OP_shra: return "OP_shra";
352 case DW_OP_xor: return "OP_xor";
353 case DW_OP_skip: return "OP_skip";
354 case DW_OP_bra: return "OP_bra";
355 case DW_OP_eq: return "OP_eq";
356 case DW_OP_ge: return "OP_ge";
357 case DW_OP_gt: return "OP_gt";
358 case DW_OP_le: return "OP_le";
359 case DW_OP_lt: return "OP_lt";
360 case DW_OP_ne: return "OP_ne";
361 case DW_OP_lit0: return "OP_lit0";
362 case DW_OP_lit1: return "OP_lit1";
363 case DW_OP_lit31: return "OP_lit31";
364 case DW_OP_reg0: return "OP_reg0";
365 case DW_OP_reg1: return "OP_reg1";
366 case DW_OP_reg31: return "OP_reg31";
367 case DW_OP_breg0: return "OP_breg0";
368 case DW_OP_breg1: return "OP_breg1";
369 case DW_OP_breg31: return "OP_breg31";
370 case DW_OP_regx: return "OP_regx";
371 case DW_OP_fbreg: return "OP_fbreg";
372 case DW_OP_bregx: return "OP_bregx";
373 case DW_OP_piece: return "OP_piece";
374 case DW_OP_deref_size: return "OP_deref_size";
375 case DW_OP_xderef_size: return "OP_xderef_size";
376 case DW_OP_nop: return "OP_nop";
377 case DW_OP_push_object_address: return "OP_push_object_address";
378 case DW_OP_call2: return "OP_call2";
379 case DW_OP_call4: return "OP_call4";
380 case DW_OP_call_ref: return "OP_call_ref";
381 case DW_OP_form_tls_address: return "OP_form_tls_address";
382 case DW_OP_call_frame_cfa: return "OP_call_frame_cfa";
383 case DW_OP_lo_user: return "OP_lo_user";
384 case DW_OP_hi_user: return "OP_hi_user";
385 }
386 assert(0 && "Unknown Dwarf Operation Encoding");
387 return "";
388}
389
390/// AttributeEncodingString - Return the string for the specified attribute
391/// encoding.
392static const char *AttributeEncodingString(unsigned Encoding) {
393 switch(Encoding) {
394 case DW_ATE_address: return "ATE_address";
395 case DW_ATE_boolean: return "ATE_boolean";
396 case DW_ATE_complex_float: return "ATE_complex_float";
397 case DW_ATE_float: return "ATE_float";
398 case DW_ATE_signed: return "ATE_signed";
399 case DW_ATE_signed_char: return "ATE_signed_char";
400 case DW_ATE_unsigned: return "ATE_unsigned";
401 case DW_ATE_unsigned_char: return "ATE_unsigned_char";
402 case DW_ATE_imaginary_float: return "ATE_imaginary_float";
403 case DW_ATE_packed_decimal: return "ATE_packed_decimal";
404 case DW_ATE_numeric_string: return "ATE_numeric_string";
405 case DW_ATE_edited: return "ATE_edited";
406 case DW_ATE_signed_fixed: return "ATE_signed_fixed";
407 case DW_ATE_unsigned_fixed: return "ATE_unsigned_fixed";
408 case DW_ATE_decimal_float: return "ATE_decimal_float";
409 case DW_ATE_lo_user: return "ATE_lo_user";
410 case DW_ATE_hi_user: return "ATE_hi_user";
411 }
412 assert(0 && "Unknown Dwarf Attribute Encoding");
413 return "";
414}
415
416/// DecimalSignString - Return the string for the specified decimal sign
417/// attribute.
418static const char *DecimalSignString(unsigned Sign) {
419 switch(Sign) {
420 case DW_DS_unsigned: return "DS_unsigned";
421 case DW_DS_leading_overpunch: return "DS_leading_overpunch";
422 case DW_DS_trailing_overpunch: return "DS_trailing_overpunch";
423 case DW_DS_leading_separate: return "DS_leading_separate";
424 case DW_DS_trailing_separate: return "DS_trailing_separate";
425 }
426 assert(0 && "Unknown Dwarf Decimal Sign Attribute");
427 return "";
428}
429
430/// EndianityString - Return the string for the specified endianity.
431///
432static const char *EndianityString(unsigned Endian) {
433 switch(Endian) {
434 case DW_END_default: return "END_default";
435 case DW_END_big: return "END_big";
436 case DW_END_little: return "END_little";
437 case DW_END_lo_user: return "END_lo_user";
438 case DW_END_hi_user: return "END_hi_user";
439 }
440 assert(0 && "Unknown Dwarf Endianity");
441 return "";
442}
443
444/// AccessibilityString - Return the string for the specified accessibility.
445///
446static const char *AccessibilityString(unsigned Access) {
447 switch(Access) {
448 // Accessibility codes
449 case DW_ACCESS_public: return "ACCESS_public";
450 case DW_ACCESS_protected: return "ACCESS_protected";
451 case DW_ACCESS_private: return "ACCESS_private";
452 }
453 assert(0 && "Unknown Dwarf Accessibility");
454 return "";
455}
456
457/// VisibilityString - Return the string for the specified visibility.
458///
459static const char *VisibilityString(unsigned Visibility) {
460 switch(Visibility) {
461 case DW_VIS_local: return "VIS_local";
462 case DW_VIS_exported: return "VIS_exported";
463 case DW_VIS_qualified: return "VIS_qualified";
464 }
465 assert(0 && "Unknown Dwarf Visibility");
466 return "";
467}
468
469/// VirtualityString - Return the string for the specified virtuality.
470///
471static const char *VirtualityString(unsigned Virtuality) {
472 switch(Virtuality) {
473 case DW_VIRTUALITY_none: return "VIRTUALITY_none";
474 case DW_VIRTUALITY_virtual: return "VIRTUALITY_virtual";
475 case DW_VIRTUALITY_pure_virtual: return "VIRTUALITY_pure_virtual";
476 }
477 assert(0 && "Unknown Dwarf Virtuality");
478 return "";
479}
480
481/// LanguageString - Return the string for the specified language.
482///
483static const char *LanguageString(unsigned Language) {
484 switch(Language) {
485 case DW_LANG_C89: return "LANG_C89";
486 case DW_LANG_C: return "LANG_C";
487 case DW_LANG_Ada83: return "LANG_Ada83";
488 case DW_LANG_C_plus_plus: return "LANG_C_plus_plus";
489 case DW_LANG_Cobol74: return "LANG_Cobol74";
490 case DW_LANG_Cobol85: return "LANG_Cobol85";
491 case DW_LANG_Fortran77: return "LANG_Fortran77";
492 case DW_LANG_Fortran90: return "LANG_Fortran90";
493 case DW_LANG_Pascal83: return "LANG_Pascal83";
494 case DW_LANG_Modula2: return "LANG_Modula2";
495 case DW_LANG_Java: return "LANG_Java";
496 case DW_LANG_C99: return "LANG_C99";
497 case DW_LANG_Ada95: return "LANG_Ada95";
498 case DW_LANG_Fortran95: return "LANG_Fortran95";
499 case DW_LANG_PLI: return "LANG_PLI";
500 case DW_LANG_ObjC: return "LANG_ObjC";
501 case DW_LANG_ObjC_plus_plus: return "LANG_ObjC_plus_plus";
502 case DW_LANG_UPC: return "LANG_UPC";
503 case DW_LANG_D: return "LANG_D";
504 case DW_LANG_lo_user: return "LANG_lo_user";
505 case DW_LANG_hi_user: return "LANG_hi_user";
506 }
507 assert(0 && "Unknown Dwarf Language");
508 return "";
509}
510
511/// CaseString - Return the string for the specified identifier case.
512///
513static const char *CaseString(unsigned Case) {
514 switch(Case) {
515 case DW_ID_case_sensitive: return "ID_case_sensitive";
516 case DW_ID_up_case: return "ID_up_case";
517 case DW_ID_down_case: return "ID_down_case";
518 case DW_ID_case_insensitive: return "ID_case_insensitive";
519 }
520 assert(0 && "Unknown Dwarf Identifier Case");
521 return "";
522}
523
524/// ConventionString - Return the string for the specified calling convention.
525///
526static const char *ConventionString(unsigned Convention) {
527 switch(Convention) {
528 case DW_CC_normal: return "CC_normal";
529 case DW_CC_program: return "CC_program";
530 case DW_CC_nocall: return "CC_nocall";
531 case DW_CC_lo_user: return "CC_lo_user";
532 case DW_CC_hi_user: return "CC_hi_user";
533 }
534 assert(0 && "Unknown Dwarf Calling Convention");
535 return "";
536}
537
538/// InlineCodeString - Return the string for the specified inline code.
539///
540static const char *InlineCodeString(unsigned Code) {
541 switch(Code) {
542 case DW_INL_not_inlined: return "INL_not_inlined";
543 case DW_INL_inlined: return "INL_inlined";
544 case DW_INL_declared_not_inlined: return "INL_declared_not_inlined";
545 case DW_INL_declared_inlined: return "INL_declared_inlined";
546 }
547 assert(0 && "Unknown Dwarf Inline Code");
548 return "";
549}
550
551/// ArrayOrderString - Return the string for the specified array order.
552///
553static const char *ArrayOrderString(unsigned Order) {
554 switch(Order) {
555 case DW_ORD_row_major: return "ORD_row_major";
556 case DW_ORD_col_major: return "ORD_col_major";
557 }
558 assert(0 && "Unknown Dwarf Array Order");
559 return "";
560}
561
562/// DiscriminantString - Return the string for the specified discriminant
563/// descriptor.
564static const char *DiscriminantString(unsigned Discriminant) {
565 switch(Discriminant) {
566 case DW_DSC_label: return "DSC_label";
567 case DW_DSC_range: return "DSC_range";
568 }
569 assert(0 && "Unknown Dwarf Discriminant Descriptor");
570 return "";
571}
572
573/// LNStandardString - Return the string for the specified line number standard.
574///
575static const char *LNStandardString(unsigned Standard) {
576 switch(Standard) {
577 case DW_LNS_copy: return "LNS_copy";
578 case DW_LNS_advance_pc: return "LNS_advance_pc";
579 case DW_LNS_advance_line: return "LNS_advance_line";
580 case DW_LNS_set_file: return "LNS_set_file";
581 case DW_LNS_set_column: return "LNS_set_column";
582 case DW_LNS_negate_stmt: return "LNS_negate_stmt";
583 case DW_LNS_set_basic_block: return "LNS_set_basic_block";
584 case DW_LNS_const_add_pc: return "LNS_const_add_pc";
585 case DW_LNS_fixed_advance_pc: return "LNS_fixed_advance_pc";
586 case DW_LNS_set_prologue_end: return "LNS_set_prologue_end";
587 case DW_LNS_set_epilogue_begin: return "LNS_set_epilogue_begin";
588 case DW_LNS_set_isa: return "LNS_set_isa";
589 }
590 assert(0 && "Unknown Dwarf Line Number Standard");
591 return "";
592}
593
594/// LNExtendedString - Return the string for the specified line number extended
595/// opcode encodings.
596static const char *LNExtendedString(unsigned Encoding) {
597 switch(Encoding) {
598 // Line Number Extended Opcode Encodings
599 case DW_LNE_end_sequence: return "LNE_end_sequence";
600 case DW_LNE_set_address: return "LNE_set_address";
601 case DW_LNE_define_file: return "LNE_define_file";
602 case DW_LNE_lo_user: return "LNE_lo_user";
603 case DW_LNE_hi_user: return "LNE_hi_user";
604 }
605 assert(0 && "Unknown Dwarf Line Number Extended Opcode Encoding");
606 return "";
607}
608
609/// MacinfoString - Return the string for the specified macinfo type encodings.
610///
611static const char *MacinfoString(unsigned Encoding) {
612 switch(Encoding) {
613 // Macinfo Type Encodings
614 case DW_MACINFO_define: return "MACINFO_define";
615 case DW_MACINFO_undef: return "MACINFO_undef";
616 case DW_MACINFO_start_file: return "MACINFO_start_file";
617 case DW_MACINFO_end_file: return "MACINFO_end_file";
618 case DW_MACINFO_vendor_ext: return "MACINFO_vendor_ext";
619 }
620 assert(0 && "Unknown Dwarf Macinfo Type Encodings");
621 return "";
622}
623
624/// CallFrameString - Return the string for the specified call frame instruction
625/// encodings.
626static const char *CallFrameString(unsigned Encoding) {
627 switch(Encoding) {
628 case DW_CFA_advance_loc: return "CFA_advance_loc";
629 case DW_CFA_offset: return "CFA_offset";
630 case DW_CFA_restore: return "CFA_restore";
631 case DW_CFA_set_loc: return "CFA_set_loc";
632 case DW_CFA_advance_loc1: return "CFA_advance_loc1";
633 case DW_CFA_advance_loc2: return "CFA_advance_loc2";
634 case DW_CFA_advance_loc4: return "CFA_advance_loc4";
635 case DW_CFA_offset_extended: return "CFA_offset_extended";
636 case DW_CFA_restore_extended: return "CFA_restore_extended";
637 case DW_CFA_undefined: return "CFA_undefined";
638 case DW_CFA_same_value: return "CFA_same_value";
639 case DW_CFA_register: return "CFA_register";
640 case DW_CFA_remember_state: return "CFA_remember_state";
641 case DW_CFA_restore_state: return "CFA_restore_state";
642 case DW_CFA_def_cfa: return "CFA_def_cfa";
643 case DW_CFA_def_cfa_register: return "CFA_def_cfa_register";
644 case DW_CFA_def_cfa_offset: return "CFA_def_cfa_offset";
645 case DW_CFA_def_cfa_expression: return "CFA_def_cfa_expression";
646 case DW_CFA_expression: return "CFA_expression";
647 case DW_CFA_offset_extended_sf: return "CFA_offset_extended_sf";
648 case DW_CFA_def_cfa_sf: return "CFA_def_cfa_sf";
649 case DW_CFA_def_cfa_offset_sf: return "CFA_def_cfa_offset_sf";
650 case DW_CFA_val_offset: return "CFA_val_offset";
651 case DW_CFA_val_offset_sf: return "CFA_val_offset_sf";
652 case DW_CFA_val_expression: return "CFA_val_expression";
653 case DW_CFA_lo_user: return "CFA_lo_user";
654 case DW_CFA_hi_user: return "CFA_hi_user";
655 }
656 assert(0 && "Unknown Dwarf Call Frame Instruction Encodings");
657 return "";
658}
659
660//===----------------------------------------------------------------------===//
661
662/// EmitValue - Emit integer of appropriate size.
663///
664void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
665 switch (Form) {
666 case DW_FORM_data1: DW.EmitByte(Value); break;
667 case DW_FORM_data2: DW.EmitShort(Value); break;
668 case DW_FORM_data4: DW.EmitLong(Value); break;
669 default: assert(0 && "DIE Value form not supported yet"); break;
670 }
671}
672
673/// SizeOf - Determine size of integer value in bytes.
674///
675unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
676 switch (Form) {
677 case DW_FORM_data1: return sizeof(int8_t);
678 case DW_FORM_data2: return sizeof(int16_t);
679 case DW_FORM_data4: return sizeof(int32_t);
680 default: assert(0 && "DIE Value form not supported yet"); break;
681 }
682 return 0;
683}
684
685//===----------------------------------------------------------------------===//
686
687/// EmitValue - Emit string value.
688///
689void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
690 DW.EmitString(Value);
691}
692
693/// SizeOf - Determine size of string value in bytes.
694///
695unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
696 return Value.size() + sizeof(int8_t);
697}
698
699//===----------------------------------------------------------------------===//
700
701/// EmitValue - Emit label value.
702///
703void DIELabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
704 DW.EmitLabelReference(Value);
705}
706
707/// SizeOf - Determine size of label value in bytes.
708///
709unsigned DIELabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
710 return DW.getAddressSize();
711}
712
713//===----------------------------------------------------------------------===//
714
715/// EmitValue - Emit delta value.
716///
717void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
718 DW.EmitDifference(Value1, Value2);
719}
720
721/// SizeOf - Determine size of delta value in bytes.
722///
723unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
724 return DW.getAddressSize();
725}
726
727//===----------------------------------------------------------------------===//
728
729/// PrintHex - Print a value as a hexidecimal value.
730///
731void DwarfWriter::PrintHex(int Value) const {
732 O << "0x" << std::hex << Value << std::dec;
733}
734
735/// EOL - Print a newline character to asm stream. If a comment is present
736/// then it will be printed first. Comments should not contain '\n'.
737void DwarfWriter::EOL(const std::string &Comment) const {
738 if (DwarfVerbose) {
739 O << "\t"
740 << Asm->CommentString
741 << " "
742 << Comment;
743 }
744 O << "\n";
745}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000746
747/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000748/// unsigned leb128 value.
749void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000750 if (hasLEB128) {
751 O << "\t.uleb128\t"
752 << Value;
753 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000754 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000755 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000756 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000757}
758
759/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000760/// signed leb128 value.
761void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000762 if (hasLEB128) {
763 O << "\t.sleb128\t"
764 << Value;
765 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000766 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000767 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000768 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000769}
770
Jim Laskey063e7652006-01-17 17:31:53 +0000771/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000772/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000773void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000774 do {
775 unsigned Byte = Value & 0x7f;
776 Value >>= 7;
777 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000778 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000779 if (Value) O << ", ";
780 } while (Value);
781}
782
Jim Laskey063e7652006-01-17 17:31:53 +0000783/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
784/// value.
785unsigned DwarfWriter::SizeULEB128(unsigned Value) {
786 unsigned Size = 0;
787 do {
788 Value >>= 7;
789 Size += sizeof(int8_t);
790 } while (Value);
791 return Size;
792}
793
794/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000795/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000796void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000797 int Sign = Value >> (8 * sizeof(Value) - 1);
798 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000799
Jim Laskeyb2efb852006-01-04 22:28:25 +0000800 do {
801 unsigned Byte = Value & 0x7f;
802 Value >>= 7;
803 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
804 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000805 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000806 if (IsMore) O << ", ";
807 } while (IsMore);
808}
809
Jim Laskey063e7652006-01-17 17:31:53 +0000810/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
811/// value.
812unsigned DwarfWriter::SizeSLEB128(int Value) {
813 unsigned Size = 0;
814 int Sign = Value >> (8 * sizeof(Value) - 1);
815 bool IsMore;
816
817 do {
818 unsigned Byte = Value & 0x7f;
819 Value >>= 7;
820 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
821 Size += sizeof(int8_t);
822 } while (IsMore);
823 return Size;
824}
825
826/// EmitByte - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000827///
Jim Laskey063e7652006-01-17 17:31:53 +0000828void DwarfWriter::EmitByte(int Value) const {
829 O << Asm->Data8bitsDirective;
830 PrintHex(Value);
831}
832
833/// EmitShort - Emit a short directive and value.
834///
835void DwarfWriter::EmitShort(int Value) const {
836 O << Asm->Data16bitsDirective;
837 PrintHex(Value);
838}
839
840/// EmitLong - Emit a long directive and value.
841///
842void DwarfWriter::EmitLong(int Value) const {
843 O << Asm->Data32bitsDirective;
844 PrintHex(Value);
845}
846
847/// EmitString - Emit a string with quotes and a null terminator.
848/// Special characters are emitted properly. (Eg. '\t')
849void DwarfWriter::EmitString(const std::string &String) const {
850 O << Asm->AsciiDirective
851 << "\"";
852 for (unsigned i = 0, N = String.size(); i < N; i++) {
853 unsigned char C = String[i];
854
855 if (!isascii(C) || iscntrl(C)) {
856 switch(C) {
857 case '\b': O << "\\b"; break;
858 case '\f': O << "\\f"; break;
859 case '\n': O << "\\n"; break;
860 case '\r': O << "\\r"; break;
861 case '\t': O << "\\t"; break;
862 default:
863 O << '\\';
864 O << char('0' + (C >> 6));
865 O << char('0' + (C >> 3));
866 O << char('0' + (C >> 0));
867 break;
868 }
869 } else if (C == '\"') {
870 O << "\\\"";
871 } else if (C == '\'') {
872 O << "\\\'";
873 } else {
874 O << C;
875 }
876 }
877 O << "\\0\"";
878}
879
880/// PrintLabelName - Print label name in form used by Dwarf writer.
881///
882void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000883 O << Asm->PrivateGlobalPrefix
884 << "debug_"
885 << Tag
Jim Laskey063e7652006-01-17 17:31:53 +0000886 << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000887}
888
Jim Laskey063e7652006-01-17 17:31:53 +0000889/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000890///
Jim Laskey063e7652006-01-17 17:31:53 +0000891void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
892 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000893 O << ":\n";
894}
895
Jim Laskey063e7652006-01-17 17:31:53 +0000896/// EmitLabelReference - Emit a reference to a label.
897///
898void DwarfWriter::EmitLabelReference(const char *Tag, unsigned Number) const {
899 if (AddressSize == 4)
900 O << Asm->Data32bitsDirective;
901 else
902 O << Asm->Data64bitsDirective;
903
904 PrintLabelName(Tag, Number);
905}
906
907/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
908/// assemblers do not accept absolute expressions with data directives, so there
909/// is an option (needsSet) to use an intermediary 'set' expression.
910void DwarfWriter::EmitDifference(const char *Tag1, unsigned Number1,
911 const char *Tag2, unsigned Number2) const {
912 if (needsSet) {
913 static unsigned SetCounter = 0;
914 O << "\t.set\t";
915 PrintLabelName("set", SetCounter);
916 O << ",";
917 PrintLabelName(Tag1, Number1);
918 O << "-";
919 PrintLabelName(Tag2, Number2);
920 O << "\n";
921
922 if (AddressSize == sizeof(int32_t))
923 O << Asm->Data32bitsDirective;
924 else
925 O << Asm->Data64bitsDirective;
926
927 PrintLabelName("set", SetCounter);
928
929 SetCounter++;
930 } else {
931 if (AddressSize == sizeof(int32_t))
932 O << Asm->Data32bitsDirective;
933 else
934 O << Asm->Data64bitsDirective;
935
936 PrintLabelName(Tag1, Number1);
937 O << "-";
938 PrintLabelName(Tag2, Number2);
939 }
940}
941
942/// NewDIE - Construct a new structured debug information entry.
943///
944DIE *DwarfWriter::NewDIE(const unsigned char *AbbrevData) {
945 // Get the abbreviation ID.
946 unsigned AbbrevID = Abbreviations.insert(DIEAbbrev(AbbrevData));
947 // Allocate new new structured DIE.
948 DIE *Die = new DIE(AbbrevID);
949 // Return structured DIE.
950 return Die;
951}
952
953/// NewCompileUnit - Create new compile unit information.
954///
955DIE *DwarfWriter::NewCompileUnit(const std::string &Directory,
956 const std::string &SourceName) {
957 DIE *Die = NewDIE(AbbrevTAG_compile_unit);
958 // FIXME - use the correct line set.
959 Die->AddValue(DWLabel("line", 0));
960 Die->AddValue(DWLabel("text_end", 0));
961 Die->AddValue(DWLabel("text_begin", 0));
962 // FIXME - The producer needs to be in this form, but should come from
963 // an appropriate source.
964 Die->AddValue("llvm 3.4.x (LLVM Research Group)");
965 Die->AddValue(DW_LANG_C89);
966 Die->AddValue(SourceName);
967 Die->AddValue(Directory);
968
969 return Die;
970}
971
972/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
973/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000974///
975void DwarfWriter::EmitInitial() const {
Jim Laskey063e7652006-01-17 17:31:53 +0000976 // Dwarf sections base addresses.
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000977 Asm->SwitchSection(DwarfAbbrevSection, 0);
978 EmitLabel("abbrev", 0);
979 Asm->SwitchSection(DwarfInfoSection, 0);
980 EmitLabel("info", 0);
981 Asm->SwitchSection(DwarfLineSection, 0);
982 EmitLabel("line", 0);
Jim Laskey063e7652006-01-17 17:31:53 +0000983
984 // Standard sections base addresses.
985 Asm->SwitchSection(TextSection, 0);
986 EmitLabel("text_begin", 0);
987 Asm->SwitchSection(DataSection, 0);
988 EmitLabel("data_begin", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000989}
990
Jim Laskey063e7652006-01-17 17:31:53 +0000991/// EmitDIE - Recusively Emits a debug information entry.
992///
993void DwarfWriter::EmitDIE(DIE *Die) const {
994 // Get the abbreviation for this DIE.
995 unsigned AbbrevID = Die->getAbbrevID();
996 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
997
998 // Emit the code (index) for the abbreviation.
999 EmitULEB128Bytes(AbbrevID);
1000 EOL(std::string("Abbrev [" +
1001 utostr(AbbrevID) +
1002 TagString(Abbrev.getTag())) +
1003 " ");
1004
1005 const std::vector<DIEValue *> &Values = Die->getValues();
1006
1007 // Emit the DIE attribute values.
1008 for (unsigned i = 0, N = Values.size(); i < N; i++) {
1009 unsigned Attr = Abbrev.getAttribute(i);
1010 unsigned Form = Abbrev.getForm(i);
1011 assert(Form && "Too many attributes for DIE (check abbreviation)");
1012
1013 switch (Attr) {
1014 case DW_AT_sibling: {
1015 EmitLong(Die->SiblingOffset());
1016 break;
1017 }
1018 default: {
1019 // Emit an attribute using the defined form.
1020 Values[i]->EmitValue(*this, Form);
1021 break;
1022 }
1023 }
1024
1025 EOL(AttributeString(Attr));
1026 }
1027
1028 // Emit the DIE children if any.
1029 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1030 const std::vector<DIE *> &Children = Die->getChildren();
1031
1032 for (unsigned j = 0, M = Children.size(); j < M; j++) {
1033 // FIXME - handle sibling offsets.
1034 // FIXME - handle all DIE types.
1035 EmitDIE(Children[j]);
1036 }
1037
1038 EmitByte(0); EOL("End Of Children Mark");
1039 }
1040}
1041
1042/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1043///
1044unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) const {
1045 // Get the abbreviation for this DIE.
1046 unsigned AbbrevID = Die->getAbbrevID();
1047 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1048
1049 // Set DIE offset
1050 Die->setOffset(Offset);
1051
1052 // Start the size with the size of abbreviation code.
1053 Offset += SizeULEB128(AbbrevID);
1054
1055 const std::vector<DIEValue *> &Values = Die->getValues();
1056
1057 // Emit the DIE attribute values.
1058 for (unsigned i = 0, N = Values.size(); i < N; i++) {
1059 // Size attribute value.
1060 Offset += Values[i]->SizeOf(*this, Abbrev.getForm(i));
1061 }
1062
1063 // Emit the DIE children if any.
1064 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1065 const std::vector<DIE *> &Children = Die->getChildren();
1066
1067 for (unsigned j = 0, M = Children.size(); j < M; j++) {
1068 // FIXME - handle sibling offsets.
1069 // FIXME - handle all DIE types.
1070 Offset = SizeAndOffsetDie(Children[j], Offset);
1071 }
1072
1073 // End of children marker.
1074 Offset += sizeof(int8_t);
1075 }
1076
1077 Die->setSize(Offset - Die->getOffset());
1078 return Offset;
1079}
1080
1081/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1082///
1083void DwarfWriter::SizeAndOffsets() {
1084 // Compute size of debug unit header
1085 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1086 sizeof(int16_t) + // DWARF version number
1087 sizeof(int32_t) + // Offset Into Abbrev. Section
1088 sizeof(int8_t); // Pointer Size (in bytes)
1089
1090 // Process each compile unit.
1091 for (unsigned i = 0, N = CompileUnits.size(); i < N; i++) {
1092 Offset = SizeAndOffsetDie(CompileUnits[i], Offset);
1093 }
1094}
1095
1096/// EmitDebugInfo - Emit the debug info section.
1097///
1098void DwarfWriter::EmitDebugInfo() const {
1099 // Start debug info section.
1100 Asm->SwitchSection(DwarfInfoSection, 0);
1101
1102 // Get the number of compile units.
1103 unsigned N = CompileUnits.size();
1104
1105 // If there are any compile units.
1106 if (N) {
1107 // Emit the compile units header.
1108
1109 // Emit size of content not including length itself
1110 unsigned ContentSize = CompileUnits[N - 1]->SiblingOffset();
1111 EmitLong(ContentSize - sizeof(int32_t));
1112 EOL("Length of Compilation Unit Info");
1113
1114 EmitShort(DWARF_VERSION); EOL("DWARF version number");
1115
1116 EmitLabelReference("abbrev", 0); EOL("Offset Into Abbrev. Section");
1117
1118 EmitByte(AddressSize); EOL("Address Size (in bytes)");
1119
1120 // Process each compile unit.
1121 for (unsigned i = 0; i < N; i++) {
1122 EmitDIE(CompileUnits[i]);
1123 }
1124 }
1125}
1126
1127/// EmitAbbreviations - Emit the abbreviation section.
1128///
1129void DwarfWriter::EmitAbbreviations() const {
1130 // Start the debug abbrev section.
1131 Asm->SwitchSection(DwarfAbbrevSection, 0);
1132
1133 // For each abbrevation.
1134 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1135 AbbrevID <= NAID; AbbrevID++) {
1136 // Get abbreviation data
1137 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1138
1139 // Emit the abbrevations code (base 1 index.)
1140 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1141
1142 // Emit its Dwarf tag type.
1143 EmitULEB128Bytes(Abbrev.getTag());
1144 EOL(TagString(Abbrev.getTag()));
1145
1146 // Emit whether it has children DIEs.
1147 EmitULEB128Bytes(Abbrev.getChildrenFlag());
1148 EOL(ChildrenString(Abbrev.getChildrenFlag()));
1149
1150 // For each attribute description.
1151 for (unsigned i = 0; ; i++) {
1152 unsigned Attr = Abbrev.getAttribute(i);
1153 unsigned Form = Abbrev.getForm(i);
1154
1155 // Attributes are null terminated.
1156 if (!Attr) break;
1157
1158 // Emit attribute type.
1159 EmitULEB128Bytes(Attr);
1160 EOL(AttributeString(Attr));
1161
1162 // Emit form type.
1163 EmitULEB128Bytes(Form);
1164 EOL(FormEncodingString(Form));
1165 }
1166
1167 // Mark end of abbreviation.
1168 EmitULEB128Bytes(0); EOL("EOM(1)");
1169 EmitULEB128Bytes(0); EOL("EOM(2)");
1170 }
1171}
1172
1173/// EmitDebugLines - Emit source line information.
1174///
1175void DwarfWriter::EmitDebugLines() const {
1176 // Minimum line delta, thus ranging from -10..(255-10).
1177 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1178 // Maximum line delta, thus ranging from -10..(255-10).
1179 const int MaxLineDelta = 255 + MinLineDelta;
1180
1181 // Start the dwarf line section.
1182 Asm->SwitchSection(DwarfLineSection, 0);
1183
1184 // Construct the section header.
1185
1186 EmitDifference("line_end", 0, "line_begin", 0);
1187 EOL("Length of Source Line Info");
1188 EmitLabel("line_begin", 0);
1189
1190 EmitShort(DWARF_VERSION); EOL("DWARF version number");
1191
1192 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1193 EOL("Prolog Length");
1194 EmitLabel("line_prolog_begin", 0);
1195
1196 EmitByte(1); EOL("Minimum Instruction Length");
1197
1198 EmitByte(1); EOL("Default is_stmt_start flag");
1199
1200 EmitByte(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1201
1202 EmitByte(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1203
1204 EmitByte(-MinLineDelta); EOL("Special Opcode Base");
1205
1206 // Line number standard opcode encodings argument count
1207 EmitByte(0); EOL("DW_LNS_copy arg count");
1208 EmitByte(1); EOL("DW_LNS_advance_pc arg count");
1209 EmitByte(1); EOL("DW_LNS_advance_line arg count");
1210 EmitByte(1); EOL("DW_LNS_set_file arg count");
1211 EmitByte(1); EOL("DW_LNS_set_column arg count");
1212 EmitByte(0); EOL("DW_LNS_negate_stmt arg count");
1213 EmitByte(0); EOL("DW_LNS_set_basic_block arg count");
1214 EmitByte(0); EOL("DW_LNS_const_add_pc arg count");
1215 EmitByte(1); EOL("DW_LNS_fixed_advance_pc arg count");
1216
1217 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1218 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1219
1220 // Emit directories.
1221 for (unsigned DirectoryID = 1, NDID = Directories.size();
1222 DirectoryID <= NDID; DirectoryID++) {
1223 EmitString(Directories[DirectoryID]); EOL("Directory");
1224 }
1225 EmitByte(0); EOL("End of directories");
1226
1227 // Emit files.
1228 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1229 SourceID <= NSID; SourceID++) {
1230 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1231 EmitString(SourceFile.getName()); EOL("Source");
1232 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1233 EmitULEB128Bytes(0); EOL("Mod date");
1234 EmitULEB128Bytes(0); EOL("File size");
1235 }
1236 EmitByte(0); EOL("End of files");
1237
1238 EmitLabel("line_prolog_end", 0);
1239
1240 // Emit line information
1241 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1242
1243 // Dwarf assumes we start with first line of first source file.
1244 unsigned Source = 1;
1245 unsigned Line = 1;
1246
1247 // Construct rows of the address, source, line, column matrix.
1248 for (unsigned i = 0, N = LineInfos.size(); i < N; i++) {
1249 SourceLineInfo *LineInfo = LineInfos[i];
1250
1251 // Define the line address.
1252 EmitByte(0); EOL("Extended Op");
1253 EmitByte(4 + 1); EOL("Op size");
1254 EmitByte(DW_LNE_set_address); EOL("DW_LNE_set_address");
1255 EmitLabelReference("loc", i + 1); EOL("Location label");
1256
1257 // If change of source, then switch to the new source.
1258 if (Source != LineInfo->getSourceID()) {
1259 Source = LineInfo->getSourceID();
1260 EmitByte(DW_LNS_set_file); EOL("DW_LNS_set_file");
1261 EmitULEB128Bytes(0); EOL("New Source");
1262 }
1263
1264 // If change of line.
1265 if (Line != LineInfo->getLine()) {
1266 // Determine offset.
1267 int Offset = LineInfo->getLine() - Line;
1268 int Delta = Offset - MinLineDelta;
1269
1270 // Update line.
1271 Line = LineInfo->getLine();
1272
1273 // If delta is small enough and in range...
1274 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1275 // ... then use fast opcode.
1276 EmitByte(Delta - MinLineDelta); EOL("Line Delta");
1277 } else {
1278 // ... otherwise use long hand.
1279 EmitByte(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1280 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1281 EmitByte(DW_LNS_copy); EOL("DW_LNS_copy");
1282 }
1283 } else {
1284 // Copy the previous row (different address or source)
1285 EmitByte(DW_LNS_copy); EOL("DW_LNS_copy");
1286 }
1287 }
1288
1289 // Mark end of matrix.
1290 EmitByte(0); EOL("DW_LNE_end_sequence");
1291 EmitULEB128Bytes(1); O << "\n";
1292 EmitByte(1); O << "\n";
1293
1294 EmitLabel("line_end", 0);
1295}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001296
1297/// EmitDebugFrame - Emit visible names into a debug frame section.
1298///
1299void DwarfWriter::EmitDebugFrame() {
1300}
1301
1302/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1303///
1304void DwarfWriter::EmitDebugPubNames() {
1305}
1306
1307/// EmitDebugPubTypes - Emit visible names into a debug pubtypes section.
1308///
1309void DwarfWriter::EmitDebugPubTypes() {
1310}
1311
1312/// EmitDebugStr - Emit visible names into a debug str section.
1313///
1314void DwarfWriter::EmitDebugStr() {
1315}
1316
1317/// EmitDebugLoc - Emit visible names into a debug loc section.
1318///
1319void DwarfWriter::EmitDebugLoc() {
1320}
1321
1322/// EmitDebugARanges - Emit visible names into a debug aranges section.
1323///
1324void DwarfWriter::EmitDebugARanges() {
1325}
1326
1327/// EmitDebugRanges - Emit visible names into a debug ranges section.
1328///
1329void DwarfWriter::EmitDebugRanges() {
1330}
1331
1332/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1333///
1334void DwarfWriter::EmitDebugMacInfo() {
1335}
Jim Laskey063e7652006-01-17 17:31:53 +00001336
1337/// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001338///
1339bool DwarfWriter::ShouldEmitDwarf() {
1340 // Check if debug info is present.
1341 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1342
1343 // Make sure initial declarations are made.
1344 if (!didInitial) {
1345 EmitInitial();
1346 didInitial = true;
1347 }
1348
1349 // Okay to emit.
1350 return true;
1351}
1352
Jim Laskey063e7652006-01-17 17:31:53 +00001353//===----------------------------------------------------------------------===//
1354// Main enties.
1355//
1356
1357 DwarfWriter::DwarfWriter(std::ostream &o, AsmPrinter *ap)
1358 : O(o)
1359 , Asm(ap)
1360 , DebugInfo(NULL)
1361 , didInitial(false)
1362 , CompileUnits()
1363 , Abbreviations()
1364 , AddressSize(sizeof(int32_t))
1365 , hasLEB128(false)
1366 , hasDotLoc(false)
1367 , hasDotFile(false)
1368 , needsSet(false)
1369 , DwarfAbbrevSection(".debug_abbrev")
1370 , DwarfInfoSection(".debug_info")
1371 , DwarfLineSection(".debug_line")
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001372 , DwarfFrameSection(".debug_frame")
1373 , DwarfPubNamesSection(".debug_pubnames")
1374 , DwarfPubTypesSection(".debug_pubtypes")
1375 , DwarfStrSection(".debug_str")
1376 , DwarfLocSection(".debug_loc")
1377 , DwarfARangesSection(".debug_aranges")
1378 , DwarfRangesSection(".debug_ranges")
1379 , DwarfMacInfoSection(".debug_macinfo")
Jim Laskey063e7652006-01-17 17:31:53 +00001380 , TextSection(".text")
1381 , DataSection(".data")
1382 {}
1383 DwarfWriter::~DwarfWriter() {
1384 }
1385
1386/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001387///
1388void DwarfWriter::BeginModule() {
1389 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00001390 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00001391}
1392
Jim Laskey063e7652006-01-17 17:31:53 +00001393/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001394///
1395void DwarfWriter::EndModule() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001396 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00001397 EOL("Dwarf End Module");
1398
1399 // Standard sections final addresses.
1400 EmitLabel("text_end", 0);
1401 EmitLabel("data_end", 0);
1402
1403 // Get directory and source information.
1404 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1405 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1406
1407 // Construct compile unit DIEs for each source.
1408 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1409 SourceID <= NSID; SourceID++) {
1410 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1411 const std::string &Directory = Directories[SourceFile.getDirectoryID()];
1412 const std::string &SourceName = SourceFile.getName();
1413 DIE *CompileUnit = NewCompileUnit(Directory, SourceName);
1414 CompileUnits.push_back(CompileUnit);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001415 }
Jim Laskey063e7652006-01-17 17:31:53 +00001416
1417 // Compute DIE offsets and sizes.
1418 SizeAndOffsets();
1419
1420 // Emit all the DIEs into a debug info section
1421 EmitDebugInfo();
1422
1423 // Corresponding abbreviations into a abbrev section.
1424 EmitAbbreviations();
1425
1426 // Emit source line correspondence into a debug line section.
1427 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001428
1429 // Emit info into a debug frame section.
1430 EmitDebugFrame();
1431
1432 // Emit info into a debug pubnames section.
1433 EmitDebugPubNames();
1434
1435 // Emit info into a debug pubtypes section.
1436 EmitDebugPubTypes();
1437
1438 // Emit info into a debug str section.
1439 EmitDebugStr();
1440
1441 // Emit info into a debug loc section.
1442 EmitDebugLoc();
1443
1444 // Emit info into a debug aranges section.
1445 EmitDebugARanges();
1446
1447 // Emit info into a debug ranges section.
1448 EmitDebugRanges();
1449
1450 // Emit info into a debug macinfo section.
1451 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001452}
1453
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001454/// BeginFunction - Emit pre-function debug information.
1455///
1456void DwarfWriter::BeginFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001457 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00001458 EOL("Dwarf Begin Function");
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001459}
1460
1461/// EndFunction - Emit post-function debug information.
1462///
1463void DwarfWriter::EndFunction() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001464 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00001465 EOL("Dwarf End Function");
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001466}