blob: 9512f54873ee6c7defe9e4ba4ef4c6d51013a4e3 [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//===----------------------------------------------------------------------===//
Jim Laskey3ea0e0e2006-01-27 18:32:41 +000013
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 Laskey52060a02006-01-24 00:49:18 +000017#include "llvm/Module.h"
18#include "llvm/Type.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000019#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000020#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000021#include "llvm/Support/Dwarf.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000022#include "llvm/Support/CommandLine.h"
Jim Laskey52060a02006-01-24 00:49:18 +000023#include "llvm/Support/Mangler.h"
24#include "llvm/Target/TargetMachine.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000025
Jim Laskeyb2efb852006-01-04 22:28:25 +000026#include <iostream>
Jim Laskeya7cea6f2006-01-04 13:52:30 +000027
Jim Laskeyb2efb852006-01-04 22:28:25 +000028using namespace llvm;
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029
30static cl::opt<bool>
31DwarfVerbose("dwarf-verbose", cl::Hidden,
Jim Laskey063e7652006-01-17 17:31:53 +000032 cl::desc("Add comments to Dwarf directives."));
33
34//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +000035
36/// TagString - Return the string for the specified tag.
37///
38static const char *TagString(unsigned Tag) {
39 switch(Tag) {
40 case DW_TAG_array_type: return "TAG_array_type";
41 case DW_TAG_class_type: return "TAG_class_type";
42 case DW_TAG_entry_point: return "TAG_entry_point";
43 case DW_TAG_enumeration_type: return "TAG_enumeration_type";
44 case DW_TAG_formal_parameter: return "TAG_formal_parameter";
45 case DW_TAG_imported_declaration: return "TAG_imported_declaration";
46 case DW_TAG_label: return "TAG_label";
47 case DW_TAG_lexical_block: return "TAG_lexical_block";
48 case DW_TAG_member: return "TAG_member";
49 case DW_TAG_pointer_type: return "TAG_pointer_type";
50 case DW_TAG_reference_type: return "TAG_reference_type";
51 case DW_TAG_compile_unit: return "TAG_compile_unit";
52 case DW_TAG_string_type: return "TAG_string_type";
53 case DW_TAG_structure_type: return "TAG_structure_type";
54 case DW_TAG_subroutine_type: return "TAG_subroutine_type";
55 case DW_TAG_typedef: return "TAG_typedef";
56 case DW_TAG_union_type: return "TAG_union_type";
57 case DW_TAG_unspecified_parameters: return "TAG_unspecified_parameters";
58 case DW_TAG_variant: return "TAG_variant";
59 case DW_TAG_common_block: return "TAG_common_block";
60 case DW_TAG_common_inclusion: return "TAG_common_inclusion";
61 case DW_TAG_inheritance: return "TAG_inheritance";
62 case DW_TAG_inlined_subroutine: return "TAG_inlined_subroutine";
63 case DW_TAG_module: return "TAG_module";
64 case DW_TAG_ptr_to_member_type: return "TAG_ptr_to_member_type";
65 case DW_TAG_set_type: return "TAG_set_type";
66 case DW_TAG_subrange_type: return "TAG_subrange_type";
67 case DW_TAG_with_stmt: return "TAG_with_stmt";
68 case DW_TAG_access_declaration: return "TAG_access_declaration";
69 case DW_TAG_base_type: return "TAG_base_type";
70 case DW_TAG_catch_block: return "TAG_catch_block";
71 case DW_TAG_const_type: return "TAG_const_type";
72 case DW_TAG_constant: return "TAG_constant";
73 case DW_TAG_enumerator: return "TAG_enumerator";
74 case DW_TAG_file_type: return "TAG_file_type";
75 case DW_TAG_friend: return "TAG_friend";
76 case DW_TAG_namelist: return "TAG_namelist";
77 case DW_TAG_namelist_item: return "TAG_namelist_item";
78 case DW_TAG_packed_type: return "TAG_packed_type";
79 case DW_TAG_subprogram: return "TAG_subprogram";
80 case DW_TAG_template_type_parameter: return "TAG_template_type_parameter";
81 case DW_TAG_template_value_parameter: return "TAG_template_value_parameter";
82 case DW_TAG_thrown_type: return "TAG_thrown_type";
83 case DW_TAG_try_block: return "TAG_try_block";
84 case DW_TAG_variant_part: return "TAG_variant_part";
85 case DW_TAG_variable: return "TAG_variable";
86 case DW_TAG_volatile_type: return "TAG_volatile_type";
87 case DW_TAG_dwarf_procedure: return "TAG_dwarf_procedure";
88 case DW_TAG_restrict_type: return "TAG_restrict_type";
89 case DW_TAG_interface_type: return "TAG_interface_type";
90 case DW_TAG_namespace: return "TAG_namespace";
91 case DW_TAG_imported_module: return "TAG_imported_module";
92 case DW_TAG_unspecified_type: return "TAG_unspecified_type";
93 case DW_TAG_partial_unit: return "TAG_partial_unit";
94 case DW_TAG_imported_unit: return "TAG_imported_unit";
95 case DW_TAG_condition: return "TAG_condition";
96 case DW_TAG_shared_type: return "TAG_shared_type";
97 case DW_TAG_lo_user: return "TAG_lo_user";
98 case DW_TAG_hi_user: return "TAG_hi_user";
99 }
100 assert(0 && "Unknown Dwarf Tag");
101 return "";
102}
103
104/// ChildrenString - Return the string for the specified children flag.
105///
106static const char *ChildrenString(unsigned Children) {
107 switch(Children) {
108 case DW_CHILDREN_no: return "CHILDREN_no";
109 case DW_CHILDREN_yes: return "CHILDREN_yes";
110 }
111 assert(0 && "Unknown Dwarf ChildrenFlag");
112 return "";
113}
114
115/// AttributeString - Return the string for the specified attribute.
116///
117static const char *AttributeString(unsigned Attribute) {
118 switch(Attribute) {
119 case DW_AT_sibling: return "AT_sibling";
120 case DW_AT_location: return "AT_location";
121 case DW_AT_name: return "AT_name";
122 case DW_AT_ordering: return "AT_ordering";
123 case DW_AT_byte_size: return "AT_byte_size";
124 case DW_AT_bit_offset: return "AT_bit_offset";
125 case DW_AT_bit_size: return "AT_bit_size";
126 case DW_AT_stmt_list: return "AT_stmt_list";
127 case DW_AT_low_pc: return "AT_low_pc";
128 case DW_AT_high_pc: return "AT_high_pc";
129 case DW_AT_language: return "AT_language";
130 case DW_AT_discr: return "AT_discr";
131 case DW_AT_discr_value: return "AT_discr_value";
132 case DW_AT_visibility: return "AT_visibility";
133 case DW_AT_import: return "AT_import";
134 case DW_AT_string_length: return "AT_string_length";
135 case DW_AT_common_reference: return "AT_common_reference";
136 case DW_AT_comp_dir: return "AT_comp_dir";
137 case DW_AT_const_value: return "AT_const_value";
138 case DW_AT_containing_type: return "AT_containing_type";
139 case DW_AT_default_value: return "AT_default_value";
140 case DW_AT_inline: return "AT_inline";
141 case DW_AT_is_optional: return "AT_is_optional";
142 case DW_AT_lower_bound: return "AT_lower_bound";
143 case DW_AT_producer: return "AT_producer";
144 case DW_AT_prototyped: return "AT_prototyped";
145 case DW_AT_return_addr: return "AT_return_addr";
146 case DW_AT_start_scope: return "AT_start_scope";
147 case DW_AT_bit_stride: return "AT_bit_stride";
148 case DW_AT_upper_bound: return "AT_upper_bound";
149 case DW_AT_abstract_origin: return "AT_abstract_origin";
150 case DW_AT_accessibility: return "AT_accessibility";
151 case DW_AT_address_class: return "AT_address_class";
152 case DW_AT_artificial: return "AT_artificial";
153 case DW_AT_base_types: return "AT_base_types";
154 case DW_AT_calling_convention: return "AT_calling_convention";
155 case DW_AT_count: return "AT_count";
156 case DW_AT_data_member_location: return "AT_data_member_location";
157 case DW_AT_decl_column: return "AT_decl_column";
158 case DW_AT_decl_file: return "AT_decl_file";
159 case DW_AT_decl_line: return "AT_decl_line";
160 case DW_AT_declaration: return "AT_declaration";
161 case DW_AT_discr_list: return "AT_discr_list";
162 case DW_AT_encoding: return "AT_encoding";
163 case DW_AT_external: return "AT_external";
164 case DW_AT_frame_base: return "AT_frame_base";
165 case DW_AT_friend: return "AT_friend";
166 case DW_AT_identifier_case: return "AT_identifier_case";
167 case DW_AT_macro_info: return "AT_macro_info";
168 case DW_AT_namelist_item: return "AT_namelist_item";
169 case DW_AT_priority: return "AT_priority";
170 case DW_AT_segment: return "AT_segment";
171 case DW_AT_specification: return "AT_specification";
172 case DW_AT_static_link: return "AT_static_link";
173 case DW_AT_type: return "AT_type";
174 case DW_AT_use_location: return "AT_use_location";
175 case DW_AT_variable_parameter: return "AT_variable_parameter";
176 case DW_AT_virtuality: return "AT_virtuality";
177 case DW_AT_vtable_elem_location: return "AT_vtable_elem_location";
178 case DW_AT_allocated: return "AT_allocated";
179 case DW_AT_associated: return "AT_associated";
180 case DW_AT_data_location: return "AT_data_location";
181 case DW_AT_byte_stride: return "AT_byte_stride";
182 case DW_AT_entry_pc: return "AT_entry_pc";
183 case DW_AT_use_UTF8: return "AT_use_UTF8";
184 case DW_AT_extension: return "AT_extension";
185 case DW_AT_ranges: return "AT_ranges";
186 case DW_AT_trampoline: return "AT_trampoline";
187 case DW_AT_call_column: return "AT_call_column";
188 case DW_AT_call_file: return "AT_call_file";
189 case DW_AT_call_line: return "AT_call_line";
190 case DW_AT_description: return "AT_description";
191 case DW_AT_binary_scale: return "AT_binary_scale";
192 case DW_AT_decimal_scale: return "AT_decimal_scale";
193 case DW_AT_small: return "AT_small";
194 case DW_AT_decimal_sign: return "AT_decimal_sign";
195 case DW_AT_digit_count: return "AT_digit_count";
196 case DW_AT_picture_string: return "AT_picture_string";
197 case DW_AT_mutable: return "AT_mutable";
198 case DW_AT_threads_scaled: return "AT_threads_scaled";
199 case DW_AT_explicit: return "AT_explicit";
200 case DW_AT_object_pointer: return "AT_object_pointer";
201 case DW_AT_endianity: return "AT_endianity";
202 case DW_AT_elemental: return "AT_elemental";
203 case DW_AT_pure: return "AT_pure";
204 case DW_AT_recursive: return "AT_recursive";
205 case DW_AT_lo_user: return "AT_lo_user";
206 case DW_AT_hi_user: return "AT_hi_user";
207 }
208 assert(0 && "Unknown Dwarf Attribute");
209 return "";
210}
211
212/// FormEncodingString - Return the string for the specified form encoding.
213///
214static const char *FormEncodingString(unsigned Encoding) {
215 switch(Encoding) {
216 case DW_FORM_addr: return "FORM_addr";
217 case DW_FORM_block2: return "FORM_block2";
218 case DW_FORM_block4: return "FORM_block4";
219 case DW_FORM_data2: return "FORM_data2";
220 case DW_FORM_data4: return "FORM_data4";
221 case DW_FORM_data8: return "FORM_data8";
222 case DW_FORM_string: return "FORM_string";
223 case DW_FORM_block: return "FORM_block";
224 case DW_FORM_block1: return "FORM_block1";
225 case DW_FORM_data1: return "FORM_data1";
226 case DW_FORM_flag: return "FORM_flag";
227 case DW_FORM_sdata: return "FORM_sdata";
228 case DW_FORM_strp: return "FORM_strp";
229 case DW_FORM_udata: return "FORM_udata";
230 case DW_FORM_ref_addr: return "FORM_ref_addr";
231 case DW_FORM_ref1: return "FORM_ref1";
232 case DW_FORM_ref2: return "FORM_ref2";
233 case DW_FORM_ref4: return "FORM_ref4";
234 case DW_FORM_ref8: return "FORM_ref8";
235 case DW_FORM_ref_udata: return "FORM_ref_udata";
236 case DW_FORM_indirect: return "FORM_indirect";
237 }
238 assert(0 && "Unknown Dwarf Form Encoding");
239 return "";
240}
241
242/// OperationEncodingString - Return the string for the specified operation
243/// encoding.
244static const char *OperationEncodingString(unsigned Encoding) {
245 switch(Encoding) {
246 case DW_OP_addr: return "OP_addr";
247 case DW_OP_deref: return "OP_deref";
248 case DW_OP_const1u: return "OP_const1u";
249 case DW_OP_const1s: return "OP_const1s";
250 case DW_OP_const2u: return "OP_const2u";
251 case DW_OP_const2s: return "OP_const2s";
252 case DW_OP_const4u: return "OP_const4u";
253 case DW_OP_const4s: return "OP_const4s";
254 case DW_OP_const8u: return "OP_const8u";
255 case DW_OP_const8s: return "OP_const8s";
256 case DW_OP_constu: return "OP_constu";
257 case DW_OP_consts: return "OP_consts";
258 case DW_OP_dup: return "OP_dup";
259 case DW_OP_drop: return "OP_drop";
260 case DW_OP_over: return "OP_over";
261 case DW_OP_pick: return "OP_pick";
262 case DW_OP_swap: return "OP_swap";
263 case DW_OP_rot: return "OP_rot";
264 case DW_OP_xderef: return "OP_xderef";
265 case DW_OP_abs: return "OP_abs";
266 case DW_OP_and: return "OP_and";
267 case DW_OP_div: return "OP_div";
268 case DW_OP_minus: return "OP_minus";
269 case DW_OP_mod: return "OP_mod";
270 case DW_OP_mul: return "OP_mul";
271 case DW_OP_neg: return "OP_neg";
272 case DW_OP_not: return "OP_not";
273 case DW_OP_or: return "OP_or";
274 case DW_OP_plus: return "OP_plus";
275 case DW_OP_plus_uconst: return "OP_plus_uconst";
276 case DW_OP_shl: return "OP_shl";
277 case DW_OP_shr: return "OP_shr";
278 case DW_OP_shra: return "OP_shra";
279 case DW_OP_xor: return "OP_xor";
280 case DW_OP_skip: return "OP_skip";
281 case DW_OP_bra: return "OP_bra";
282 case DW_OP_eq: return "OP_eq";
283 case DW_OP_ge: return "OP_ge";
284 case DW_OP_gt: return "OP_gt";
285 case DW_OP_le: return "OP_le";
286 case DW_OP_lt: return "OP_lt";
287 case DW_OP_ne: return "OP_ne";
288 case DW_OP_lit0: return "OP_lit0";
289 case DW_OP_lit1: return "OP_lit1";
290 case DW_OP_lit31: return "OP_lit31";
291 case DW_OP_reg0: return "OP_reg0";
292 case DW_OP_reg1: return "OP_reg1";
293 case DW_OP_reg31: return "OP_reg31";
294 case DW_OP_breg0: return "OP_breg0";
295 case DW_OP_breg1: return "OP_breg1";
296 case DW_OP_breg31: return "OP_breg31";
297 case DW_OP_regx: return "OP_regx";
298 case DW_OP_fbreg: return "OP_fbreg";
299 case DW_OP_bregx: return "OP_bregx";
300 case DW_OP_piece: return "OP_piece";
301 case DW_OP_deref_size: return "OP_deref_size";
302 case DW_OP_xderef_size: return "OP_xderef_size";
303 case DW_OP_nop: return "OP_nop";
304 case DW_OP_push_object_address: return "OP_push_object_address";
305 case DW_OP_call2: return "OP_call2";
306 case DW_OP_call4: return "OP_call4";
307 case DW_OP_call_ref: return "OP_call_ref";
308 case DW_OP_form_tls_address: return "OP_form_tls_address";
309 case DW_OP_call_frame_cfa: return "OP_call_frame_cfa";
310 case DW_OP_lo_user: return "OP_lo_user";
311 case DW_OP_hi_user: return "OP_hi_user";
312 }
313 assert(0 && "Unknown Dwarf Operation Encoding");
314 return "";
315}
316
317/// AttributeEncodingString - Return the string for the specified attribute
318/// encoding.
319static const char *AttributeEncodingString(unsigned Encoding) {
320 switch(Encoding) {
321 case DW_ATE_address: return "ATE_address";
322 case DW_ATE_boolean: return "ATE_boolean";
323 case DW_ATE_complex_float: return "ATE_complex_float";
324 case DW_ATE_float: return "ATE_float";
325 case DW_ATE_signed: return "ATE_signed";
326 case DW_ATE_signed_char: return "ATE_signed_char";
327 case DW_ATE_unsigned: return "ATE_unsigned";
328 case DW_ATE_unsigned_char: return "ATE_unsigned_char";
329 case DW_ATE_imaginary_float: return "ATE_imaginary_float";
330 case DW_ATE_packed_decimal: return "ATE_packed_decimal";
331 case DW_ATE_numeric_string: return "ATE_numeric_string";
332 case DW_ATE_edited: return "ATE_edited";
333 case DW_ATE_signed_fixed: return "ATE_signed_fixed";
334 case DW_ATE_unsigned_fixed: return "ATE_unsigned_fixed";
335 case DW_ATE_decimal_float: return "ATE_decimal_float";
336 case DW_ATE_lo_user: return "ATE_lo_user";
337 case DW_ATE_hi_user: return "ATE_hi_user";
338 }
339 assert(0 && "Unknown Dwarf Attribute Encoding");
340 return "";
341}
342
343/// DecimalSignString - Return the string for the specified decimal sign
344/// attribute.
345static const char *DecimalSignString(unsigned Sign) {
346 switch(Sign) {
347 case DW_DS_unsigned: return "DS_unsigned";
348 case DW_DS_leading_overpunch: return "DS_leading_overpunch";
349 case DW_DS_trailing_overpunch: return "DS_trailing_overpunch";
350 case DW_DS_leading_separate: return "DS_leading_separate";
351 case DW_DS_trailing_separate: return "DS_trailing_separate";
352 }
353 assert(0 && "Unknown Dwarf Decimal Sign Attribute");
354 return "";
355}
356
357/// EndianityString - Return the string for the specified endianity.
358///
359static const char *EndianityString(unsigned Endian) {
360 switch(Endian) {
361 case DW_END_default: return "END_default";
362 case DW_END_big: return "END_big";
363 case DW_END_little: return "END_little";
364 case DW_END_lo_user: return "END_lo_user";
365 case DW_END_hi_user: return "END_hi_user";
366 }
367 assert(0 && "Unknown Dwarf Endianity");
368 return "";
369}
370
371/// AccessibilityString - Return the string for the specified accessibility.
372///
373static const char *AccessibilityString(unsigned Access) {
374 switch(Access) {
375 // Accessibility codes
376 case DW_ACCESS_public: return "ACCESS_public";
377 case DW_ACCESS_protected: return "ACCESS_protected";
378 case DW_ACCESS_private: return "ACCESS_private";
379 }
380 assert(0 && "Unknown Dwarf Accessibility");
381 return "";
382}
383
384/// VisibilityString - Return the string for the specified visibility.
385///
386static const char *VisibilityString(unsigned Visibility) {
387 switch(Visibility) {
388 case DW_VIS_local: return "VIS_local";
389 case DW_VIS_exported: return "VIS_exported";
390 case DW_VIS_qualified: return "VIS_qualified";
391 }
392 assert(0 && "Unknown Dwarf Visibility");
393 return "";
394}
395
396/// VirtualityString - Return the string for the specified virtuality.
397///
398static const char *VirtualityString(unsigned Virtuality) {
399 switch(Virtuality) {
400 case DW_VIRTUALITY_none: return "VIRTUALITY_none";
401 case DW_VIRTUALITY_virtual: return "VIRTUALITY_virtual";
402 case DW_VIRTUALITY_pure_virtual: return "VIRTUALITY_pure_virtual";
403 }
404 assert(0 && "Unknown Dwarf Virtuality");
405 return "";
406}
407
408/// LanguageString - Return the string for the specified language.
409///
410static const char *LanguageString(unsigned Language) {
411 switch(Language) {
412 case DW_LANG_C89: return "LANG_C89";
413 case DW_LANG_C: return "LANG_C";
414 case DW_LANG_Ada83: return "LANG_Ada83";
415 case DW_LANG_C_plus_plus: return "LANG_C_plus_plus";
416 case DW_LANG_Cobol74: return "LANG_Cobol74";
417 case DW_LANG_Cobol85: return "LANG_Cobol85";
418 case DW_LANG_Fortran77: return "LANG_Fortran77";
419 case DW_LANG_Fortran90: return "LANG_Fortran90";
420 case DW_LANG_Pascal83: return "LANG_Pascal83";
421 case DW_LANG_Modula2: return "LANG_Modula2";
422 case DW_LANG_Java: return "LANG_Java";
423 case DW_LANG_C99: return "LANG_C99";
424 case DW_LANG_Ada95: return "LANG_Ada95";
425 case DW_LANG_Fortran95: return "LANG_Fortran95";
426 case DW_LANG_PLI: return "LANG_PLI";
427 case DW_LANG_ObjC: return "LANG_ObjC";
428 case DW_LANG_ObjC_plus_plus: return "LANG_ObjC_plus_plus";
429 case DW_LANG_UPC: return "LANG_UPC";
430 case DW_LANG_D: return "LANG_D";
431 case DW_LANG_lo_user: return "LANG_lo_user";
432 case DW_LANG_hi_user: return "LANG_hi_user";
433 }
434 assert(0 && "Unknown Dwarf Language");
435 return "";
436}
437
438/// CaseString - Return the string for the specified identifier case.
439///
440static const char *CaseString(unsigned Case) {
441 switch(Case) {
442 case DW_ID_case_sensitive: return "ID_case_sensitive";
443 case DW_ID_up_case: return "ID_up_case";
444 case DW_ID_down_case: return "ID_down_case";
445 case DW_ID_case_insensitive: return "ID_case_insensitive";
446 }
447 assert(0 && "Unknown Dwarf Identifier Case");
448 return "";
449}
450
451/// ConventionString - Return the string for the specified calling convention.
452///
453static const char *ConventionString(unsigned Convention) {
454 switch(Convention) {
455 case DW_CC_normal: return "CC_normal";
456 case DW_CC_program: return "CC_program";
457 case DW_CC_nocall: return "CC_nocall";
458 case DW_CC_lo_user: return "CC_lo_user";
459 case DW_CC_hi_user: return "CC_hi_user";
460 }
461 assert(0 && "Unknown Dwarf Calling Convention");
462 return "";
463}
464
465/// InlineCodeString - Return the string for the specified inline code.
466///
467static const char *InlineCodeString(unsigned Code) {
468 switch(Code) {
469 case DW_INL_not_inlined: return "INL_not_inlined";
470 case DW_INL_inlined: return "INL_inlined";
471 case DW_INL_declared_not_inlined: return "INL_declared_not_inlined";
472 case DW_INL_declared_inlined: return "INL_declared_inlined";
473 }
474 assert(0 && "Unknown Dwarf Inline Code");
475 return "";
476}
477
478/// ArrayOrderString - Return the string for the specified array order.
479///
480static const char *ArrayOrderString(unsigned Order) {
481 switch(Order) {
482 case DW_ORD_row_major: return "ORD_row_major";
483 case DW_ORD_col_major: return "ORD_col_major";
484 }
485 assert(0 && "Unknown Dwarf Array Order");
486 return "";
487}
488
489/// DiscriminantString - Return the string for the specified discriminant
490/// descriptor.
491static const char *DiscriminantString(unsigned Discriminant) {
492 switch(Discriminant) {
493 case DW_DSC_label: return "DSC_label";
494 case DW_DSC_range: return "DSC_range";
495 }
496 assert(0 && "Unknown Dwarf Discriminant Descriptor");
497 return "";
498}
499
500/// LNStandardString - Return the string for the specified line number standard.
501///
502static const char *LNStandardString(unsigned Standard) {
503 switch(Standard) {
504 case DW_LNS_copy: return "LNS_copy";
505 case DW_LNS_advance_pc: return "LNS_advance_pc";
506 case DW_LNS_advance_line: return "LNS_advance_line";
507 case DW_LNS_set_file: return "LNS_set_file";
508 case DW_LNS_set_column: return "LNS_set_column";
509 case DW_LNS_negate_stmt: return "LNS_negate_stmt";
510 case DW_LNS_set_basic_block: return "LNS_set_basic_block";
511 case DW_LNS_const_add_pc: return "LNS_const_add_pc";
512 case DW_LNS_fixed_advance_pc: return "LNS_fixed_advance_pc";
513 case DW_LNS_set_prologue_end: return "LNS_set_prologue_end";
514 case DW_LNS_set_epilogue_begin: return "LNS_set_epilogue_begin";
515 case DW_LNS_set_isa: return "LNS_set_isa";
516 }
517 assert(0 && "Unknown Dwarf Line Number Standard");
518 return "";
519}
520
521/// LNExtendedString - Return the string for the specified line number extended
522/// opcode encodings.
523static const char *LNExtendedString(unsigned Encoding) {
524 switch(Encoding) {
525 // Line Number Extended Opcode Encodings
526 case DW_LNE_end_sequence: return "LNE_end_sequence";
527 case DW_LNE_set_address: return "LNE_set_address";
528 case DW_LNE_define_file: return "LNE_define_file";
529 case DW_LNE_lo_user: return "LNE_lo_user";
530 case DW_LNE_hi_user: return "LNE_hi_user";
531 }
532 assert(0 && "Unknown Dwarf Line Number Extended Opcode Encoding");
533 return "";
534}
535
536/// MacinfoString - Return the string for the specified macinfo type encodings.
537///
538static const char *MacinfoString(unsigned Encoding) {
539 switch(Encoding) {
540 // Macinfo Type Encodings
541 case DW_MACINFO_define: return "MACINFO_define";
542 case DW_MACINFO_undef: return "MACINFO_undef";
543 case DW_MACINFO_start_file: return "MACINFO_start_file";
544 case DW_MACINFO_end_file: return "MACINFO_end_file";
545 case DW_MACINFO_vendor_ext: return "MACINFO_vendor_ext";
546 }
547 assert(0 && "Unknown Dwarf Macinfo Type Encodings");
548 return "";
549}
550
551/// CallFrameString - Return the string for the specified call frame instruction
552/// encodings.
553static const char *CallFrameString(unsigned Encoding) {
554 switch(Encoding) {
555 case DW_CFA_advance_loc: return "CFA_advance_loc";
556 case DW_CFA_offset: return "CFA_offset";
557 case DW_CFA_restore: return "CFA_restore";
558 case DW_CFA_set_loc: return "CFA_set_loc";
559 case DW_CFA_advance_loc1: return "CFA_advance_loc1";
560 case DW_CFA_advance_loc2: return "CFA_advance_loc2";
561 case DW_CFA_advance_loc4: return "CFA_advance_loc4";
562 case DW_CFA_offset_extended: return "CFA_offset_extended";
563 case DW_CFA_restore_extended: return "CFA_restore_extended";
564 case DW_CFA_undefined: return "CFA_undefined";
565 case DW_CFA_same_value: return "CFA_same_value";
566 case DW_CFA_register: return "CFA_register";
567 case DW_CFA_remember_state: return "CFA_remember_state";
568 case DW_CFA_restore_state: return "CFA_restore_state";
569 case DW_CFA_def_cfa: return "CFA_def_cfa";
570 case DW_CFA_def_cfa_register: return "CFA_def_cfa_register";
571 case DW_CFA_def_cfa_offset: return "CFA_def_cfa_offset";
572 case DW_CFA_def_cfa_expression: return "CFA_def_cfa_expression";
573 case DW_CFA_expression: return "CFA_expression";
574 case DW_CFA_offset_extended_sf: return "CFA_offset_extended_sf";
575 case DW_CFA_def_cfa_sf: return "CFA_def_cfa_sf";
576 case DW_CFA_def_cfa_offset_sf: return "CFA_def_cfa_offset_sf";
577 case DW_CFA_val_offset: return "CFA_val_offset";
578 case DW_CFA_val_offset_sf: return "CFA_val_offset_sf";
579 case DW_CFA_val_expression: return "CFA_val_expression";
580 case DW_CFA_lo_user: return "CFA_lo_user";
581 case DW_CFA_hi_user: return "CFA_hi_user";
582 }
583 assert(0 && "Unknown Dwarf Call Frame Instruction Encodings");
584 return "";
585}
586
587//===----------------------------------------------------------------------===//
588
Jim Laskeyd18e2892006-01-20 20:34:06 +0000589/// operator== - Used by UniqueVector to locate entry.
590///
591bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
592 if (Tag != DA.Tag) return false;
593 if (ChildrenFlag != DA.ChildrenFlag) return false;
594 if (Data.size() != DA.Data.size()) return false;
595
Jim Laskey52060a02006-01-24 00:49:18 +0000596 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000597 if (Data[i] != DA.Data[i]) return false;
Jim Laskeyd18e2892006-01-20 20:34:06 +0000598 }
599
600 return true;
601}
602
603/// operator< - Used by UniqueVector to locate entry.
604///
605bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
606 if (Tag != DA.Tag) return Tag < DA.Tag;
607 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
608 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
609
Jim Laskey52060a02006-01-24 00:49:18 +0000610 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskey63ae85f2006-01-21 01:13:18 +0000611 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
Jim Laskeyd18e2892006-01-20 20:34:06 +0000612 }
613
614 return false;
615}
616
617/// Emit - Print the abbreviation using the specified Dwarf writer.
618///
619void DIEAbbrev::Emit(const DwarfWriter &DW) const {
620 // Emit its Dwarf tag type.
621 DW.EmitULEB128Bytes(Tag);
622 DW.EOL(TagString(Tag));
623
624 // Emit whether it has children DIEs.
625 DW.EmitULEB128Bytes(ChildrenFlag);
626 DW.EOL(ChildrenString(ChildrenFlag));
627
628 // For each attribute description.
Jim Laskey52060a02006-01-24 00:49:18 +0000629 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000630 const DIEAbbrevData &AttrData = Data[i];
631
632 // Emit attribute type.
633 DW.EmitULEB128Bytes(AttrData.getAttribute());
634 DW.EOL(AttributeString(AttrData.getAttribute()));
635
636 // Emit form type.
637 DW.EmitULEB128Bytes(AttrData.getForm());
638 DW.EOL(FormEncodingString(AttrData.getForm()));
639 }
640
641 // Mark end of abbreviation.
642 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
643 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
644}
645
646#ifndef NDEBUG
647 void DIEAbbrev::print(std::ostream &O) {
648 O << "Abbreviation @"
Jeff Cohen05ebc8d2006-01-25 17:18:50 +0000649 << std::hex << (intptr_t)this << std::dec
Jim Laskeyd18e2892006-01-20 20:34:06 +0000650 << " "
651 << TagString(Tag)
652 << " "
653 << ChildrenString(ChildrenFlag)
654 << "\n";
655
Jim Laskey52060a02006-01-24 00:49:18 +0000656 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000657 O << " "
658 << AttributeString(Data[i].getAttribute())
659 << " "
660 << FormEncodingString(Data[i].getForm())
661 << "\n";
662 }
663 }
664 void DIEAbbrev::dump() { print(std::cerr); }
665#endif
666
667//===----------------------------------------------------------------------===//
668
Jim Laskey063e7652006-01-17 17:31:53 +0000669/// EmitValue - Emit integer of appropriate size.
670///
671void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
672 switch (Form) {
Jim Laskey40020172006-01-20 21:02:36 +0000673 case DW_FORM_flag: // Fall thru
Jim Laskeyda427fa2006-01-27 20:31:25 +0000674 case DW_FORM_data1: DW.EmitInt8(Integer); break;
675 case DW_FORM_data2: DW.EmitInt16(Integer); break;
676 case DW_FORM_data4: DW.EmitInt32(Integer); break;
677 case DW_FORM_data8: DW.EmitInt64(Integer); break;
Jim Laskey40020172006-01-20 21:02:36 +0000678 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
679 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
Jim Laskey063e7652006-01-17 17:31:53 +0000680 default: assert(0 && "DIE Value form not supported yet"); break;
681 }
682}
683
684/// SizeOf - Determine size of integer value in bytes.
685///
686unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
687 switch (Form) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000688 case DW_FORM_flag: // Fall thru
Jim Laskey063e7652006-01-17 17:31:53 +0000689 case DW_FORM_data1: return sizeof(int8_t);
690 case DW_FORM_data2: return sizeof(int16_t);
691 case DW_FORM_data4: return sizeof(int32_t);
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000692 case DW_FORM_data8: return sizeof(int64_t);
Jim Laskey40020172006-01-20 21:02:36 +0000693 case DW_FORM_udata: return DW.SizeULEB128(Integer);
694 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
Jim Laskey063e7652006-01-17 17:31:53 +0000695 default: assert(0 && "DIE Value form not supported yet"); break;
696 }
697 return 0;
698}
699
700//===----------------------------------------------------------------------===//
701
702/// EmitValue - Emit string value.
703///
704void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000705 DW.EmitString(String);
Jim Laskey063e7652006-01-17 17:31:53 +0000706}
707
708/// SizeOf - Determine size of string value in bytes.
709///
710unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey40020172006-01-20 21:02:36 +0000711 return String.size() + sizeof(char); // sizeof('\0');
Jim Laskey063e7652006-01-17 17:31:53 +0000712}
713
714//===----------------------------------------------------------------------===//
715
716/// EmitValue - Emit label value.
717///
Jim Laskey52060a02006-01-24 00:49:18 +0000718void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000719 DW.EmitReference(Label);
Jim Laskey063e7652006-01-17 17:31:53 +0000720}
721
722/// SizeOf - Determine size of label value in bytes.
723///
Jim Laskey52060a02006-01-24 00:49:18 +0000724unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000725 return DW.getAddressSize();
726}
727
728//===----------------------------------------------------------------------===//
729
Jim Laskeyd18e2892006-01-20 20:34:06 +0000730/// EmitValue - Emit label value.
731///
Jim Laskey52060a02006-01-24 00:49:18 +0000732void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000733 DW.EmitInt8(sizeof(int8_t) + DW.getAddressSize());
Jim Laskey52060a02006-01-24 00:49:18 +0000734 DW.EOL("DW_FORM_block1 length");
735
Jim Laskeyda427fa2006-01-27 20:31:25 +0000736 DW.EmitInt8(DW_OP_addr);
Jim Laskey52060a02006-01-24 00:49:18 +0000737 DW.EOL("DW_OP_addr");
738
Jim Laskeyd18e2892006-01-20 20:34:06 +0000739 DW.EmitReference(Label);
740}
741
742/// SizeOf - Determine size of label value in bytes.
743///
Jim Laskey52060a02006-01-24 00:49:18 +0000744unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
745 return sizeof(int8_t) + sizeof(int8_t) + DW.getAddressSize();
Jim Laskeyd18e2892006-01-20 20:34:06 +0000746}
747
748//===----------------------------------------------------------------------===//
749
Jim Laskey063e7652006-01-17 17:31:53 +0000750/// EmitValue - Emit delta value.
751///
752void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000753 DW.EmitDifference(LabelHi, LabelLo);
Jim Laskey063e7652006-01-17 17:31:53 +0000754}
755
756/// SizeOf - Determine size of delta value in bytes.
757///
758unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
759 return DW.getAddressSize();
760}
761
762//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +0000763/// EmitValue - Emit extry offset.
764///
765void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
Jim Laskeyda427fa2006-01-27 20:31:25 +0000766 DW.EmitInt32(Entry->getOffset());
Jim Laskeyd18e2892006-01-20 20:34:06 +0000767}
768
769/// SizeOf - Determine size of label value in bytes.
770///
771unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
772 return sizeof(int32_t);
773}
774
775//===----------------------------------------------------------------------===//
776
Jim Laskey0420f2a2006-02-22 19:02:11 +0000777DIE::DIE(unsigned Tag)
778: Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
Jim Laskeyd18e2892006-01-20 20:34:06 +0000779, AbbrevID(0)
780, Offset(0)
781, Size(0)
Jim Laskeyd18e2892006-01-20 20:34:06 +0000782, Children()
783, Values()
784{}
785
786DIE::~DIE() {
787 if (Abbrev) delete Abbrev;
788
Jim Laskey52060a02006-01-24 00:49:18 +0000789 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000790 delete Children[i];
791 }
792
Jim Laskey52060a02006-01-24 00:49:18 +0000793 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000794 delete Values[j];
795 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000796}
797
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000798/// AddUInt - Add an unsigned integer attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000799///
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000800void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
Jim Laskey40020172006-01-20 21:02:36 +0000801 if (Form == 0) {
Jim Laskey40020172006-01-20 21:02:36 +0000802 if ((unsigned char)Integer == Integer) Form = DW_FORM_data1;
803 else if ((unsigned short)Integer == Integer) Form = DW_FORM_data2;
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000804 else if ((unsigned int)Integer == Integer) Form = DW_FORM_data4;
805 else Form = DW_FORM_data8;
806 }
807 Abbrev->AddAttribute(Attribute, Form);
808 Values.push_back(new DIEInteger(Integer));
809}
810
811/// AddSInt - Add an signed integer attribute data and value.
812///
813void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
814 if (Form == 0) {
815 if ((char)Integer == Integer) Form = DW_FORM_data1;
816 else if ((short)Integer == Integer) Form = DW_FORM_data2;
817 else if ((int)Integer == Integer) Form = DW_FORM_data4;
818 else Form = DW_FORM_data8;
Jim Laskey40020172006-01-20 21:02:36 +0000819 }
Jim Laskeyd18e2892006-01-20 20:34:06 +0000820 Abbrev->AddAttribute(Attribute, Form);
821 Values.push_back(new DIEInteger(Integer));
822}
823
824/// AddString - Add a std::string attribute data and value.
825///
826void DIE::AddString(unsigned Attribute, unsigned Form,
827 const std::string &String) {
828 Abbrev->AddAttribute(Attribute, Form);
829 Values.push_back(new DIEString(String));
830}
831
832/// AddLabel - Add a Dwarf label attribute data and value.
833///
834void DIE::AddLabel(unsigned Attribute, unsigned Form,
835 const DWLabel &Label) {
836 Abbrev->AddAttribute(Attribute, Form);
Jim Laskey52060a02006-01-24 00:49:18 +0000837 Values.push_back(new DIEDwarfLabel(Label));
Jim Laskeyd18e2892006-01-20 20:34:06 +0000838}
839
Jim Laskey52060a02006-01-24 00:49:18 +0000840/// AddObjectLabel - Add an non-Dwarf label attribute data and value.
Jim Laskeyd18e2892006-01-20 20:34:06 +0000841///
Jim Laskey52060a02006-01-24 00:49:18 +0000842void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
843 const std::string &Label) {
Jim Laskeyd18e2892006-01-20 20:34:06 +0000844 Abbrev->AddAttribute(Attribute, Form);
Jim Laskey52060a02006-01-24 00:49:18 +0000845 Values.push_back(new DIEObjectLabel(Label));
Jim Laskeyd18e2892006-01-20 20:34:06 +0000846}
847
848/// AddDelta - Add a label delta attribute data and value.
849///
850void DIE::AddDelta(unsigned Attribute, unsigned Form,
851 const DWLabel &Hi, const DWLabel &Lo) {
852 Abbrev->AddAttribute(Attribute, Form);
853 Values.push_back(new DIEDelta(Hi, Lo));
854}
855
856/// AddDIEntry - Add a DIE attribute data and value.
857///
858void DIE::AddDIEntry(unsigned Attribute,
859 unsigned Form, DIE *Entry) {
860 Abbrev->AddAttribute(Attribute, Form);
861 Values.push_back(new DIEntry(Entry));
862}
863
864/// Complete - Indicate that all attributes have been added and ready to get an
865/// abbreviation ID.
866void DIE::Complete(DwarfWriter &DW) {
867 AbbrevID = DW.NewAbbreviation(Abbrev);
868 delete Abbrev;
869 Abbrev = NULL;
870}
871
872/// AddChild - Add a child to the DIE.
873///
874void DIE::AddChild(DIE *Child) {
Jim Laskey0420f2a2006-02-22 19:02:11 +0000875 assert(Abbrev && "Adding children without an abbreviation");
876 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
Jim Laskeyd18e2892006-01-20 20:34:06 +0000877 Children.push_back(Child);
878}
879
880//===----------------------------------------------------------------------===//
881
Jim Laskey0420f2a2006-02-22 19:02:11 +0000882/// DWContext
Jim Laskeyd18e2892006-01-20 20:34:06 +0000883
884//===----------------------------------------------------------------------===//
Jim Laskey063e7652006-01-17 17:31:53 +0000885
886/// PrintHex - Print a value as a hexidecimal value.
887///
888void DwarfWriter::PrintHex(int Value) const {
889 O << "0x" << std::hex << Value << std::dec;
890}
891
892/// EOL - Print a newline character to asm stream. If a comment is present
893/// then it will be printed first. Comments should not contain '\n'.
894void DwarfWriter::EOL(const std::string &Comment) const {
895 if (DwarfVerbose) {
896 O << "\t"
897 << Asm->CommentString
898 << " "
899 << Comment;
900 }
901 O << "\n";
902}
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000903
904/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
Jim Laskey063e7652006-01-17 17:31:53 +0000905/// unsigned leb128 value.
906void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000907 if (hasLEB128) {
908 O << "\t.uleb128\t"
909 << Value;
910 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000911 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000912 PrintULEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000913 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000914}
915
916/// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
Jim Laskey063e7652006-01-17 17:31:53 +0000917/// signed leb128 value.
918void DwarfWriter::EmitSLEB128Bytes(int Value) const {
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000919 if (hasLEB128) {
920 O << "\t.sleb128\t"
921 << Value;
922 } else {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000923 O << Asm->Data8bitsDirective;
Jim Laskey063e7652006-01-17 17:31:53 +0000924 PrintSLEB128(Value);
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000925 }
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000926}
927
Jim Laskey063e7652006-01-17 17:31:53 +0000928/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000929/// representing an unsigned leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000930void DwarfWriter::PrintULEB128(unsigned Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000931 do {
932 unsigned Byte = Value & 0x7f;
933 Value >>= 7;
934 if (Value) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000935 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000936 if (Value) O << ", ";
937 } while (Value);
938}
939
Jim Laskey063e7652006-01-17 17:31:53 +0000940/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
941/// value.
942unsigned DwarfWriter::SizeULEB128(unsigned Value) {
943 unsigned Size = 0;
944 do {
945 Value >>= 7;
946 Size += sizeof(int8_t);
947 } while (Value);
948 return Size;
949}
950
951/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
Jim Laskeyb2efb852006-01-04 22:28:25 +0000952/// representing a signed leb128 value.
Jim Laskey063e7652006-01-17 17:31:53 +0000953void DwarfWriter::PrintSLEB128(int Value) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000954 int Sign = Value >> (8 * sizeof(Value) - 1);
955 bool IsMore;
Jim Laskeya7cea6f2006-01-04 13:52:30 +0000956
Jim Laskeyb2efb852006-01-04 22:28:25 +0000957 do {
958 unsigned Byte = Value & 0x7f;
959 Value >>= 7;
960 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
961 if (IsMore) Byte |= 0x80;
Jim Laskey063e7652006-01-17 17:31:53 +0000962 PrintHex(Byte);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000963 if (IsMore) O << ", ";
964 } while (IsMore);
965}
966
Jim Laskey063e7652006-01-17 17:31:53 +0000967/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
968/// value.
969unsigned DwarfWriter::SizeSLEB128(int Value) {
970 unsigned Size = 0;
971 int Sign = Value >> (8 * sizeof(Value) - 1);
972 bool IsMore;
973
974 do {
975 unsigned Byte = Value & 0x7f;
976 Value >>= 7;
977 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
978 Size += sizeof(int8_t);
979 } while (IsMore);
980 return Size;
981}
982
Jim Laskeyda427fa2006-01-27 20:31:25 +0000983/// EmitInt8 - Emit a byte directive and value.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000984///
Jim Laskeyda427fa2006-01-27 20:31:25 +0000985void DwarfWriter::EmitInt8(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000986 O << Asm->Data8bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +0000987 PrintHex(Value & 0xFF);
Jim Laskey063e7652006-01-17 17:31:53 +0000988}
989
Jim Laskeyda427fa2006-01-27 20:31:25 +0000990/// EmitInt16 - Emit a short directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +0000991///
Jim Laskeyda427fa2006-01-27 20:31:25 +0000992void DwarfWriter::EmitInt16(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +0000993 O << Asm->Data16bitsDirective;
Jim Laskey0420f2a2006-02-22 19:02:11 +0000994 PrintHex(Value & 0xFFFF);
Jim Laskey063e7652006-01-17 17:31:53 +0000995}
996
Jim Laskeyda427fa2006-01-27 20:31:25 +0000997/// EmitInt32 - Emit a long directive and value.
Jim Laskey063e7652006-01-17 17:31:53 +0000998///
Jim Laskeyda427fa2006-01-27 20:31:25 +0000999void DwarfWriter::EmitInt32(int Value) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001000 O << Asm->Data32bitsDirective;
1001 PrintHex(Value);
1002}
1003
Jim Laskeyda427fa2006-01-27 20:31:25 +00001004/// EmitInt64 - Emit a long long directive and value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001005///
Jim Laskeyda427fa2006-01-27 20:31:25 +00001006void DwarfWriter::EmitInt64(uint64_t Value) const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001007 if (Asm->Data64bitsDirective) {
1008 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1009 } else {
1010 const TargetData &TD = Asm->TM.getTargetData();
1011
1012 if (TD.isBigEndian()) {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001013 EmitInt32(unsigned(Value >> 32)); O << "\n";
1014 EmitInt32(unsigned(Value));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001015 } else {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001016 EmitInt32(unsigned(Value)); O << "\n";
1017 EmitInt32(unsigned(Value >> 32));
Jim Laskeyd8f77ba2006-01-27 15:20:54 +00001018 }
1019 }
1020}
1021
Jim Laskey063e7652006-01-17 17:31:53 +00001022/// EmitString - Emit a string with quotes and a null terminator.
1023/// Special characters are emitted properly. (Eg. '\t')
1024void DwarfWriter::EmitString(const std::string &String) const {
1025 O << Asm->AsciiDirective
1026 << "\"";
Jim Laskey52060a02006-01-24 00:49:18 +00001027 for (unsigned i = 0, N = String.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001028 unsigned char C = String[i];
1029
1030 if (!isascii(C) || iscntrl(C)) {
1031 switch(C) {
1032 case '\b': O << "\\b"; break;
1033 case '\f': O << "\\f"; break;
1034 case '\n': O << "\\n"; break;
1035 case '\r': O << "\\r"; break;
1036 case '\t': O << "\\t"; break;
1037 default:
1038 O << '\\';
1039 O << char('0' + (C >> 6));
1040 O << char('0' + (C >> 3));
1041 O << char('0' + (C >> 0));
1042 break;
1043 }
1044 } else if (C == '\"') {
1045 O << "\\\"";
1046 } else if (C == '\'') {
1047 O << "\\\'";
1048 } else {
1049 O << C;
1050 }
1051 }
1052 O << "\\0\"";
1053}
1054
1055/// PrintLabelName - Print label name in form used by Dwarf writer.
1056///
1057void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001058 O << Asm->PrivateGlobalPrefix
1059 << "debug_"
Jim Laskey0420f2a2006-02-22 19:02:11 +00001060 << Tag;
1061 if (Number) O << Number;
Jim Laskeyb2efb852006-01-04 22:28:25 +00001062}
1063
Jim Laskey063e7652006-01-17 17:31:53 +00001064/// EmitLabel - Emit location label for internal use by Dwarf.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001065///
Jim Laskey063e7652006-01-17 17:31:53 +00001066void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1067 PrintLabelName(Tag, Number);
Jim Laskeyb2efb852006-01-04 22:28:25 +00001068 O << ":\n";
1069}
1070
Jim Laskeye719a7c2006-01-18 16:54:26 +00001071/// EmitReference - Emit a reference to a label.
Jim Laskey063e7652006-01-17 17:31:53 +00001072///
Jim Laskeye719a7c2006-01-18 16:54:26 +00001073void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001074 if (AddressSize == 4)
1075 O << Asm->Data32bitsDirective;
1076 else
1077 O << Asm->Data64bitsDirective;
1078
1079 PrintLabelName(Tag, Number);
1080}
Jim Laskey73683212006-01-21 00:59:54 +00001081void DwarfWriter::EmitReference(const std::string &Name) const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001082 if (AddressSize == 4)
1083 O << Asm->Data32bitsDirective;
1084 else
1085 O << Asm->Data64bitsDirective;
1086
1087 O << Name;
1088}
Jim Laskey063e7652006-01-17 17:31:53 +00001089
1090/// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1091/// assemblers do not accept absolute expressions with data directives, so there
1092/// is an option (needsSet) to use an intermediary 'set' expression.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001093void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1094 const char *TagLo, unsigned NumberLo) const {
Jim Laskey063e7652006-01-17 17:31:53 +00001095 if (needsSet) {
1096 static unsigned SetCounter = 0;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001097
Jim Laskey063e7652006-01-17 17:31:53 +00001098 O << "\t.set\t";
1099 PrintLabelName("set", SetCounter);
1100 O << ",";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001101 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001102 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001103 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001104 O << "\n";
1105
1106 if (AddressSize == sizeof(int32_t))
1107 O << Asm->Data32bitsDirective;
1108 else
1109 O << Asm->Data64bitsDirective;
1110
1111 PrintLabelName("set", SetCounter);
1112
Jim Laskey52060a02006-01-24 00:49:18 +00001113 ++SetCounter;
Jim Laskey063e7652006-01-17 17:31:53 +00001114 } else {
1115 if (AddressSize == sizeof(int32_t))
1116 O << Asm->Data32bitsDirective;
1117 else
1118 O << Asm->Data64bitsDirective;
1119
Jim Laskeyd18e2892006-01-20 20:34:06 +00001120 PrintLabelName(TagHi, NumberHi);
Jim Laskey063e7652006-01-17 17:31:53 +00001121 O << "-";
Jim Laskeyd18e2892006-01-20 20:34:06 +00001122 PrintLabelName(TagLo, NumberLo);
Jim Laskey063e7652006-01-17 17:31:53 +00001123 }
1124}
1125
Jim Laskeyd18e2892006-01-20 20:34:06 +00001126/// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
Jim Laskey063e7652006-01-17 17:31:53 +00001127///
Jim Laskeyd18e2892006-01-20 20:34:06 +00001128unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1129 return Abbreviations.insert(*Abbrev);
1130}
1131
1132/// NewString - Add a string to the constant pool and returns a label.
1133///
1134DWLabel DwarfWriter::NewString(const std::string &String) {
1135 unsigned StringID = StringPool.insert(String);
1136 return DWLabel("string", StringID);
1137}
1138
Jim Laskey0420f2a2006-02-22 19:02:11 +00001139/// NewBasicType - Creates a new basic type if necessary, then adds to the
1140/// owner.
1141/// FIXME - Should never be needed.
1142DIE *DwarfWriter::NewBasicType(DIE *Owner, Type *Ty) {
1143 DIE *&Slot = TypeToDieMap[Ty];
1144 if (Slot) return Slot;
1145
1146 const char *Name;
1147 unsigned Size;
1148 unsigned Encoding = 0;
1149
1150 switch (Ty->getTypeID()) {
1151 case Type::UByteTyID:
1152 Name = "unsigned char";
1153 Size = 1;
1154 Encoding = DW_ATE_unsigned_char;
1155 break;
1156 case Type::SByteTyID:
1157 Name = "char";
1158 Size = 1;
1159 Encoding = DW_ATE_signed_char;
1160 break;
1161 case Type::UShortTyID:
1162 Name = "unsigned short";
1163 Size = 2;
1164 Encoding = DW_ATE_unsigned;
1165 break;
1166 case Type::ShortTyID:
1167 Name = "short";
1168 Size = 2;
1169 Encoding = DW_ATE_signed;
1170 break;
1171 case Type::UIntTyID:
1172 Name = "unsigned int";
1173 Size = 4;
1174 Encoding = DW_ATE_unsigned;
1175 break;
1176 case Type::IntTyID:
1177 Name = "int";
1178 Size = 4;
1179 Encoding = DW_ATE_signed;
1180 break;
1181 case Type::ULongTyID:
1182 Name = "unsigned long long";
1183 Size = 7;
1184 Encoding = DW_ATE_unsigned;
1185 break;
1186 case Type::LongTyID:
1187 Name = "long long";
1188 Size = 7;
1189 Encoding = DW_ATE_signed;
1190 break;
1191 case Type::FloatTyID:
1192 Name = "float";
1193 Size = 4;
1194 Encoding = DW_ATE_float;
1195 break;
1196 case Type::DoubleTyID:
1197 Name = "double";
1198 Size = 8;
1199 Encoding = DW_ATE_float;
1200 break;
1201 default:
1202 // FIXME - handle more complex types.
1203 Name = "unknown";
1204 Size = 1;
1205 Encoding = DW_ATE_address;
1206 break;
1207 }
1208
1209 // construct the type DIE.
1210 Slot = new DIE(DW_TAG_base_type);
1211 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
1212 Slot->AddUInt (DW_AT_byte_size, 0, Size);
1213 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1214
1215 // Add to context owner.
1216 Owner->AddChild(Slot);
1217
1218 return Slot;
1219}
1220
Jim Laskeyd18e2892006-01-20 20:34:06 +00001221/// NewGlobalType - Make the type visible globally using the given name.
1222///
1223void DwarfWriter::NewGlobalType(const std::string &Name, DIE *Type) {
Jim Laskey52060a02006-01-24 00:49:18 +00001224 assert(!GlobalTypes[Name] && "Duplicate global type");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001225 GlobalTypes[Name] = Type;
1226}
1227
1228/// NewGlobalEntity - Make the entity visible globally using the given name.
1229///
1230void DwarfWriter::NewGlobalEntity(const std::string &Name, DIE *Entity) {
Jim Laskey52060a02006-01-24 00:49:18 +00001231 assert(!GlobalEntities[Name] && "Duplicate global variable or function");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001232 GlobalEntities[Name] = Entity;
Jim Laskey063e7652006-01-17 17:31:53 +00001233}
1234
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001235/// NewType - Create a new type DIE.
1236///
1237DIE *DwarfWriter::NewType(DIE *Unit, TypeDesc *TyDesc) {
1238 // Check for pre-existence.
1239 DIE *&Slot = DescToDieMap[TyDesc];
1240 if (Slot) return Slot;
1241
1242 // Get core information.
1243 const std::string &Name = TyDesc->getName();
1244 // FIXME - handle larger sizes.
1245 unsigned Size = TyDesc->getSize() >> 3;
1246
Jim Laskey434b40b2006-02-23 22:37:30 +00001247 DIE *Ty = NULL;
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001248
Jim Laskey434b40b2006-02-23 22:37:30 +00001249 // Determine how to handle.
1250 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1251 Slot = Ty = new DIE(DW_TAG_base_type);
1252 unsigned Encoding = BasicTy->getEncoding();
1253 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1254 } else if (TypedefDesc *TypedefTy = dyn_cast<TypedefDesc>(TyDesc)) {
1255 Slot = Ty = new DIE(DW_TAG_typedef);
1256 TypeDesc *FromTy = TypedefTy->getFromType();
1257 DIE *FromTyDie = NewType(Unit, FromTy);
1258 CompileUnitDesc *File = TypedefTy->getFile();
1259 unsigned FileID = DebugInfo->RecordSource(File);
1260 int Line = TypedefTy->getLine();
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001261
Jim Laskey434b40b2006-02-23 22:37:30 +00001262 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, FromTyDie);
1263 Ty->AddUInt (DW_AT_decl_file, 0, FileID);
1264 Ty->AddUInt (DW_AT_decl_line, 0, Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001265 }
Jim Laskey434b40b2006-02-23 22:37:30 +00001266
1267 assert(Ty && "Type not supported yet");
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001268
Jim Laskey434b40b2006-02-23 22:37:30 +00001269 // Add common information.
1270 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1271 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1272
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001273 // Add to context owner.
Jim Laskey434b40b2006-02-23 22:37:30 +00001274 Unit->AddChild(Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001275
1276 return Slot;
1277}
1278
1279/// NewCompileUnit - Create new compile unit DIE.
Jim Laskey063e7652006-01-17 17:31:53 +00001280///
Jim Laskey0420f2a2006-02-22 19:02:11 +00001281DIE *DwarfWriter::NewCompileUnit(CompileUnitDesc *CompileUnit) {
1282 // Check for pre-existence.
1283 DIE *&Slot = DescToDieMap[CompileUnit];
1284 if (Slot) return Slot;
1285
1286 DIE *Unit = new DIE(DW_TAG_compile_unit);
Jim Laskey063e7652006-01-17 17:31:53 +00001287 // FIXME - use the correct line set.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001288 Unit->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0));
Jim Laskeyd18e2892006-01-20 20:34:06 +00001289 Unit->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1290 Unit->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001291 Unit->AddString(DW_AT_producer, DW_FORM_string, CompileUnit->getProducer());
1292 Unit->AddUInt (DW_AT_language, DW_FORM_data1, CompileUnit->getLanguage());
1293 Unit->AddString(DW_AT_name, DW_FORM_string, CompileUnit->getFileName());
1294 Unit->AddString(DW_AT_comp_dir, DW_FORM_string, CompileUnit->getDirectory());
Jim Laskey0420f2a2006-02-22 19:02:11 +00001295
1296 Slot = Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001297
Jim Laskeyd18e2892006-01-20 20:34:06 +00001298 return Unit;
Jim Laskey063e7652006-01-17 17:31:53 +00001299}
1300
Jim Laskey0420f2a2006-02-22 19:02:11 +00001301/// NewGlobalVariable - Add a new global variable DIE.
1302///
1303DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1304 // Check for pre-existence.
1305 DIE *&Slot = DescToDieMap[GVD];
1306 if (Slot) return Slot;
1307
1308 // Get the compile unit context.
1309 CompileUnitDesc *CompileUnit =
1310 static_cast<CompileUnitDesc *>(GVD->getContext());
1311 DIE *Unit = NewCompileUnit(CompileUnit);
1312 // Get the global variable itself.
1313 GlobalVariable *GV = GVD->getGlobalVariable();
1314 // Generate the mangled name.
1315 std::string MangledName = Asm->Mang->getValueName(GV);
1316
1317 // Gather the details (simplify add attribute code.)
1318 const std::string &Name = GVD->getName();
1319 unsigned FileID = DebugInfo->RecordSource(CompileUnit);
1320 unsigned Line = GVD->getLine();
1321
Jim Laskeyf4afdd92006-02-23 16:58:18 +00001322 // Get the global's type.
1323 DIE *Type = NewType(Unit, GVD->getTypeDesc());
1324
1325 // Create the globale variable DIE.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001326 DIE *VariableDie = new DIE(DW_TAG_variable);
1327 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1328 VariableDie->AddUInt (DW_AT_decl_file, 0, FileID);
1329 VariableDie->AddUInt (DW_AT_decl_line, 0, Line);
1330 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1331 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1332 // FIXME - needs to be a proper expression.
1333 VariableDie->AddObjectLabel(DW_AT_location, DW_FORM_block1, MangledName);
1334
1335 // Add to map.
1336 Slot = VariableDie;
1337
1338 // Add to context owner.
1339 Unit->AddChild(VariableDie);
1340
1341 // Expose as global.
1342 NewGlobalEntity(Name, VariableDie);
1343
1344 return VariableDie;
1345}
1346
1347/// NewSubprogram - Add a new subprogram DIE.
1348///
1349DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1350 // Check for pre-existence.
1351 DIE *&Slot = DescToDieMap[SPD];
1352 if (Slot) return Slot;
1353
1354 // Get the compile unit context.
1355 CompileUnitDesc *CompileUnit =
1356 static_cast<CompileUnitDesc *>(SPD->getContext());
1357 DIE *Unit = NewCompileUnit(CompileUnit);
1358
1359 // Gather the details (simplify add attribute code.)
1360 const std::string &Name = SPD->getName();
1361 unsigned FileID = DebugInfo->RecordSource(CompileUnit);
1362 // FIXME - faking the line for the time being.
1363 unsigned Line = 1;
1364
1365 // FIXME - faking the type for the time being.
1366 DIE *Type = NewBasicType(Unit, Type::IntTy);
1367
1368 DIE *SubprogramDie = new DIE(DW_TAG_variable);
1369 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1370 SubprogramDie->AddUInt (DW_AT_decl_file, 0, FileID);
1371 SubprogramDie->AddUInt (DW_AT_decl_line, 0, Line);
1372 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1373 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1374
1375 // Add to map.
1376 Slot = SubprogramDie;
1377
1378 // Add to context owner.
1379 Unit->AddChild(SubprogramDie);
1380
1381 // Expose as global.
1382 NewGlobalEntity(Name, SubprogramDie);
1383
1384 return SubprogramDie;
1385}
1386
Jim Laskey063e7652006-01-17 17:31:53 +00001387/// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1388/// tools to recognize the object file contains Dwarf information.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001389///
1390void DwarfWriter::EmitInitial() const {
Jim Laskey063e7652006-01-17 17:31:53 +00001391 // Dwarf sections base addresses.
Jim Laskey0420f2a2006-02-22 19:02:11 +00001392 Asm->SwitchSection(DwarfFrameSection, 0);
1393 EmitLabel("section_frame", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001394 Asm->SwitchSection(DwarfInfoSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001395 EmitLabel("section_info", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001396 EmitLabel("info", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001397 Asm->SwitchSection(DwarfAbbrevSection, 0);
1398 EmitLabel("section_abbrev", 0);
1399 EmitLabel("abbrev", 0);
1400 Asm->SwitchSection(DwarfARangesSection, 0);
1401 EmitLabel("section_aranges", 0);
1402 Asm->SwitchSection(DwarfMacInfoSection, 0);
1403 EmitLabel("section_macinfo", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001404 Asm->SwitchSection(DwarfLineSection, 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001405 EmitLabel("section_line", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001406 EmitLabel("line", 0);
Jim Laskey0420f2a2006-02-22 19:02:11 +00001407 Asm->SwitchSection(DwarfLocSection, 0);
1408 EmitLabel("section_loc", 0);
1409 Asm->SwitchSection(DwarfPubNamesSection, 0);
1410 EmitLabel("section_pubnames", 0);
1411 Asm->SwitchSection(DwarfStrSection, 0);
1412 EmitLabel("section_str", 0);
1413 Asm->SwitchSection(DwarfRangesSection, 0);
1414 EmitLabel("section_ranges", 0);
1415
Jim Laskey063e7652006-01-17 17:31:53 +00001416 Asm->SwitchSection(TextSection, 0);
1417 EmitLabel("text_begin", 0);
1418 Asm->SwitchSection(DataSection, 0);
1419 EmitLabel("data_begin", 0);
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001420}
1421
Jim Laskey063e7652006-01-17 17:31:53 +00001422/// EmitDIE - Recusively Emits a debug information entry.
1423///
1424void DwarfWriter::EmitDIE(DIE *Die) const {
1425 // Get the abbreviation for this DIE.
1426 unsigned AbbrevID = Die->getAbbrevID();
1427 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1428
1429 // Emit the code (index) for the abbreviation.
1430 EmitULEB128Bytes(AbbrevID);
1431 EOL(std::string("Abbrev [" +
1432 utostr(AbbrevID) +
Jim Laskey52060a02006-01-24 00:49:18 +00001433 "] " +
Jim Laskey063e7652006-01-17 17:31:53 +00001434 TagString(Abbrev.getTag())) +
1435 " ");
1436
1437 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001438 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001439
1440 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001441 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001442 unsigned Attr = AbbrevData[i].getAttribute();
1443 unsigned Form = AbbrevData[i].getForm();
Jim Laskey063e7652006-01-17 17:31:53 +00001444 assert(Form && "Too many attributes for DIE (check abbreviation)");
1445
1446 switch (Attr) {
1447 case DW_AT_sibling: {
Jim Laskeyda427fa2006-01-27 20:31:25 +00001448 EmitInt32(Die->SiblingOffset());
Jim Laskey063e7652006-01-17 17:31:53 +00001449 break;
1450 }
1451 default: {
1452 // Emit an attribute using the defined form.
1453 Values[i]->EmitValue(*this, Form);
1454 break;
1455 }
1456 }
1457
1458 EOL(AttributeString(Attr));
1459 }
1460
1461 // Emit the DIE children if any.
1462 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1463 const std::vector<DIE *> &Children = Die->getChildren();
1464
Jim Laskey52060a02006-01-24 00:49:18 +00001465 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001466 // FIXME - handle sibling offsets.
1467 // FIXME - handle all DIE types.
1468 EmitDIE(Children[j]);
1469 }
1470
Jim Laskeyda427fa2006-01-27 20:31:25 +00001471 EmitInt8(0); EOL("End Of Children Mark");
Jim Laskey063e7652006-01-17 17:31:53 +00001472 }
1473}
1474
1475/// SizeAndOffsetDie - Compute the size and offset of a DIE.
1476///
Jim Laskey0420f2a2006-02-22 19:02:11 +00001477unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1478 // Record the abbreviation.
1479 Die->Complete(*this);
1480
Jim Laskey063e7652006-01-17 17:31:53 +00001481 // Get the abbreviation for this DIE.
1482 unsigned AbbrevID = Die->getAbbrevID();
1483 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1484
1485 // Set DIE offset
1486 Die->setOffset(Offset);
1487
1488 // Start the size with the size of abbreviation code.
1489 Offset += SizeULEB128(AbbrevID);
1490
1491 const std::vector<DIEValue *> &Values = Die->getValues();
Jim Laskeyd18e2892006-01-20 20:34:06 +00001492 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Jim Laskey063e7652006-01-17 17:31:53 +00001493
1494 // Emit the DIE attribute values.
Jim Laskey52060a02006-01-24 00:49:18 +00001495 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001496 // Size attribute value.
Jim Laskeyd18e2892006-01-20 20:34:06 +00001497 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
Jim Laskey063e7652006-01-17 17:31:53 +00001498 }
1499
1500 // Emit the DIE children if any.
1501 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1502 const std::vector<DIE *> &Children = Die->getChildren();
1503
Jim Laskey52060a02006-01-24 00:49:18 +00001504 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Jim Laskey063e7652006-01-17 17:31:53 +00001505 // FIXME - handle sibling offsets.
1506 // FIXME - handle all DIE types.
1507 Offset = SizeAndOffsetDie(Children[j], Offset);
1508 }
1509
1510 // End of children marker.
1511 Offset += sizeof(int8_t);
1512 }
1513
1514 Die->setSize(Offset - Die->getOffset());
1515 return Offset;
1516}
1517
1518/// SizeAndOffsets - Compute the size and offset of all the DIEs.
1519///
1520void DwarfWriter::SizeAndOffsets() {
1521 // Compute size of debug unit header
1522 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1523 sizeof(int16_t) + // DWARF version number
1524 sizeof(int32_t) + // Offset Into Abbrev. Section
Jim Laskeyd18e2892006-01-20 20:34:06 +00001525 sizeof(int8_t); // Pointer Size (in bytes)
Jim Laskey063e7652006-01-17 17:31:53 +00001526
1527 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001528 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001529 Offset = SizeAndOffsetDie(CompileUnits[i], Offset);
1530 }
1531}
1532
1533/// EmitDebugInfo - Emit the debug info section.
1534///
1535void DwarfWriter::EmitDebugInfo() const {
1536 // Start debug info section.
1537 Asm->SwitchSection(DwarfInfoSection, 0);
1538
1539 // Get the number of compile units.
1540 unsigned N = CompileUnits.size();
1541
1542 // If there are any compile units.
1543 if (N) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001544 EmitLabel("info_begin", 0);
1545
Jim Laskey063e7652006-01-17 17:31:53 +00001546 // Emit the compile units header.
1547
1548 // Emit size of content not including length itself
1549 unsigned ContentSize = CompileUnits[N - 1]->SiblingOffset();
Jim Laskeyda427fa2006-01-27 20:31:25 +00001550 EmitInt32(ContentSize - sizeof(int32_t));
Jim Laskey063e7652006-01-17 17:31:53 +00001551 EOL("Length of Compilation Unit Info");
1552
Jim Laskeyda427fa2006-01-27 20:31:25 +00001553 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001554
Jim Laskeyd18e2892006-01-20 20:34:06 +00001555 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
Jim Laskey063e7652006-01-17 17:31:53 +00001556
Jim Laskeyda427fa2006-01-27 20:31:25 +00001557 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001558
1559 // Process each compile unit.
Jim Laskey52060a02006-01-24 00:49:18 +00001560 for (unsigned i = 0; i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001561 EmitDIE(CompileUnits[i]);
1562 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001563
1564 EmitLabel("info_end", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001565 }
1566}
1567
1568/// EmitAbbreviations - Emit the abbreviation section.
1569///
1570void DwarfWriter::EmitAbbreviations() const {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001571 // Check to see if it is worth the effort.
1572 if (!Abbreviations.empty()) {
1573 // Start the debug abbrev section.
1574 Asm->SwitchSection(DwarfAbbrevSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001575
Jim Laskeyd18e2892006-01-20 20:34:06 +00001576 EmitLabel("abbrev_begin", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001577
Jim Laskeyd18e2892006-01-20 20:34:06 +00001578 // For each abbrevation.
1579 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001580 AbbrevID <= NAID; ++AbbrevID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001581 // Get abbreviation data
1582 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
Jim Laskey063e7652006-01-17 17:31:53 +00001583
Jim Laskeyd18e2892006-01-20 20:34:06 +00001584 // Emit the abbrevations code (base 1 index.)
1585 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
Jim Laskey063e7652006-01-17 17:31:53 +00001586
Jim Laskeyd18e2892006-01-20 20:34:06 +00001587 // Emit the abbreviations data.
1588 Abbrev.Emit(*this);
Jim Laskey063e7652006-01-17 17:31:53 +00001589 }
Jim Laskeyd18e2892006-01-20 20:34:06 +00001590
1591 EmitLabel("abbrev_end", 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001592 }
1593}
1594
1595/// EmitDebugLines - Emit source line information.
1596///
1597void DwarfWriter::EmitDebugLines() const {
1598 // Minimum line delta, thus ranging from -10..(255-10).
1599 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1600 // Maximum line delta, thus ranging from -10..(255-10).
1601 const int MaxLineDelta = 255 + MinLineDelta;
1602
1603 // Start the dwarf line section.
1604 Asm->SwitchSection(DwarfLineSection, 0);
1605
1606 // Construct the section header.
1607
1608 EmitDifference("line_end", 0, "line_begin", 0);
1609 EOL("Length of Source Line Info");
1610 EmitLabel("line_begin", 0);
1611
Jim Laskeyda427fa2006-01-27 20:31:25 +00001612 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
Jim Laskey063e7652006-01-17 17:31:53 +00001613
1614 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1615 EOL("Prolog Length");
1616 EmitLabel("line_prolog_begin", 0);
1617
Jim Laskeyda427fa2006-01-27 20:31:25 +00001618 EmitInt8(1); EOL("Minimum Instruction Length");
Jim Laskey063e7652006-01-17 17:31:53 +00001619
Jim Laskeyda427fa2006-01-27 20:31:25 +00001620 EmitInt8(1); EOL("Default is_stmt_start flag");
Jim Laskey063e7652006-01-17 17:31:53 +00001621
Jim Laskeyda427fa2006-01-27 20:31:25 +00001622 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001623
Jim Laskeyda427fa2006-01-27 20:31:25 +00001624 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
Jim Laskey063e7652006-01-17 17:31:53 +00001625
Jim Laskeyda427fa2006-01-27 20:31:25 +00001626 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Jim Laskey063e7652006-01-17 17:31:53 +00001627
1628 // Line number standard opcode encodings argument count
Jim Laskeyda427fa2006-01-27 20:31:25 +00001629 EmitInt8(0); EOL("DW_LNS_copy arg count");
1630 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1631 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1632 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1633 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1634 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1635 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1636 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1637 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Jim Laskey063e7652006-01-17 17:31:53 +00001638
1639 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1640 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1641
1642 // Emit directories.
1643 for (unsigned DirectoryID = 1, NDID = Directories.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001644 DirectoryID <= NDID; ++DirectoryID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001645 EmitString(Directories[DirectoryID]); EOL("Directory");
1646 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001647 EmitInt8(0); EOL("End of directories");
Jim Laskey063e7652006-01-17 17:31:53 +00001648
1649 // Emit files.
1650 for (unsigned SourceID = 1, NSID = SourceFiles.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001651 SourceID <= NSID; ++SourceID) {
Jim Laskey063e7652006-01-17 17:31:53 +00001652 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1653 EmitString(SourceFile.getName()); EOL("Source");
1654 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1655 EmitULEB128Bytes(0); EOL("Mod date");
1656 EmitULEB128Bytes(0); EOL("File size");
1657 }
Jim Laskeyda427fa2006-01-27 20:31:25 +00001658 EmitInt8(0); EOL("End of files");
Jim Laskey063e7652006-01-17 17:31:53 +00001659
1660 EmitLabel("line_prolog_end", 0);
1661
1662 // Emit line information
1663 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1664
1665 // Dwarf assumes we start with first line of first source file.
1666 unsigned Source = 1;
1667 unsigned Line = 1;
1668
1669 // Construct rows of the address, source, line, column matrix.
Jim Laskey52060a02006-01-24 00:49:18 +00001670 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
Jim Laskey063e7652006-01-17 17:31:53 +00001671 SourceLineInfo *LineInfo = LineInfos[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001672
1673 if (DwarfVerbose) {
1674 unsigned SourceID = LineInfo->getSourceID();
1675 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1676 unsigned DirectoryID = SourceFile.getDirectoryID();
1677 O << "\t"
1678 << Asm->CommentString << " "
1679 << Directories[DirectoryID]
1680 << SourceFile.getName() << ":"
1681 << LineInfo->getLine() << "\n";
1682 }
Jim Laskey063e7652006-01-17 17:31:53 +00001683
1684 // Define the line address.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001685 EmitInt8(0); EOL("Extended Op");
1686 EmitInt8(4 + 1); EOL("Op size");
1687 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001688 EmitReference("loc", i + 1); EOL("Location label");
Jim Laskey063e7652006-01-17 17:31:53 +00001689
1690 // If change of source, then switch to the new source.
1691 if (Source != LineInfo->getSourceID()) {
1692 Source = LineInfo->getSourceID();
Jim Laskeyda427fa2006-01-27 20:31:25 +00001693 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
Jim Laskey063e7652006-01-17 17:31:53 +00001694 EmitULEB128Bytes(0); EOL("New Source");
1695 }
1696
1697 // If change of line.
1698 if (Line != LineInfo->getLine()) {
1699 // Determine offset.
1700 int Offset = LineInfo->getLine() - Line;
1701 int Delta = Offset - MinLineDelta;
1702
1703 // Update line.
1704 Line = LineInfo->getLine();
1705
1706 // If delta is small enough and in range...
1707 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1708 // ... then use fast opcode.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001709 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Jim Laskey063e7652006-01-17 17:31:53 +00001710 } else {
1711 // ... otherwise use long hand.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001712 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
Jim Laskey063e7652006-01-17 17:31:53 +00001713 EmitSLEB128Bytes(Offset); EOL("Line Offset");
Jim Laskeyda427fa2006-01-27 20:31:25 +00001714 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00001715 }
1716 } else {
1717 // Copy the previous row (different address or source)
Jim Laskeyda427fa2006-01-27 20:31:25 +00001718 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
Jim Laskey063e7652006-01-17 17:31:53 +00001719 }
1720 }
1721
Jim Laskey0420f2a2006-02-22 19:02:11 +00001722 // Define last address.
1723 EmitInt8(0); EOL("Extended Op");
1724 EmitInt8(4 + 1); EOL("Op size");
1725 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1726 EmitReference("text_end", 0); EOL("Location label");
1727
Jim Laskey063e7652006-01-17 17:31:53 +00001728 // Mark end of matrix.
Jim Laskeyda427fa2006-01-27 20:31:25 +00001729 EmitInt8(0); EOL("DW_LNE_end_sequence");
Jim Laskey063e7652006-01-17 17:31:53 +00001730 EmitULEB128Bytes(1); O << "\n";
Jim Laskeyda427fa2006-01-27 20:31:25 +00001731 EmitInt8(1); O << "\n";
Jim Laskey063e7652006-01-17 17:31:53 +00001732
1733 EmitLabel("line_end", 0);
1734}
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001735
1736/// EmitDebugFrame - Emit visible names into a debug frame section.
1737///
1738void DwarfWriter::EmitDebugFrame() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001739 // FIXME - Should be per frame
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001740}
1741
1742/// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1743///
1744void DwarfWriter::EmitDebugPubNames() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001745 // Check to see if it is worth the effort.
1746 if (!GlobalEntities.empty()) {
1747 // Start the dwarf pubnames section.
1748 Asm->SwitchSection(DwarfPubNamesSection, 0);
1749
1750 EmitDifference("pubnames_end", 0, "pubnames_begin", 0);
1751 EOL("Length of Public Names Info");
1752
1753 EmitLabel("pubnames_begin", 0);
1754
Jim Laskeyda427fa2006-01-27 20:31:25 +00001755 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001756
1757 EmitReference("info_begin", 0); EOL("Offset of Compilation Unit Info");
1758
1759 EmitDifference("info_end", 0, "info_begin", 0);
1760 EOL("Compilation Unit Length");
1761
Jim Laskey52060a02006-01-24 00:49:18 +00001762 for (std::map<std::string, DIE *>::iterator GI = GlobalEntities.begin(),
1763 GE = GlobalEntities.end();
1764 GI != GE; ++GI) {
1765 const std::string &Name = GI->first;
1766 DIE * Entity = GI->second;
Jim Laskeyd18e2892006-01-20 20:34:06 +00001767
Jim Laskeyda427fa2006-01-27 20:31:25 +00001768 EmitInt32(Entity->getOffset()); EOL("DIE offset");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001769 EmitString(Name); EOL("External Name");
1770
1771 }
1772
Jim Laskeyda427fa2006-01-27 20:31:25 +00001773 EmitInt32(0); EOL("End Mark");
Jim Laskeyd18e2892006-01-20 20:34:06 +00001774 EmitLabel("pubnames_end", 0);
1775 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001776}
1777
1778/// EmitDebugPubTypes - Emit visible names into a debug pubtypes section.
1779///
1780void DwarfWriter::EmitDebugPubTypes() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001781 // Check to see if it is worth the effort.
1782 if (!GlobalTypes.empty()) {
1783 // Start the dwarf pubtypes section.
1784 Asm->SwitchSection(DwarfPubTypesSection, 0);
1785 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001786}
1787
1788/// EmitDebugStr - Emit visible names into a debug str section.
1789///
1790void DwarfWriter::EmitDebugStr() {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001791 // Check to see if it is worth the effort.
1792 if (!StringPool.empty()) {
1793 // Start the dwarf str section.
1794 Asm->SwitchSection(DwarfStrSection, 0);
1795
1796 // For each of strings in teh string pool.
1797 for (unsigned StringID = 1, N = StringPool.size();
Jim Laskey52060a02006-01-24 00:49:18 +00001798 StringID <= N; ++StringID) {
Jim Laskeyd18e2892006-01-20 20:34:06 +00001799 // Emit a label for reference from debug information entries.
1800 EmitLabel("string", StringID);
1801 // Emit the string itself.
1802 const std::string &String = StringPool[StringID];
1803 EmitString(String); O << "\n";
1804 }
1805 }
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001806}
1807
1808/// EmitDebugLoc - Emit visible names into a debug loc section.
1809///
1810void DwarfWriter::EmitDebugLoc() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001811 // Start the dwarf loc section.
1812 Asm->SwitchSection(DwarfLocSection, 0);
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001813}
1814
1815/// EmitDebugARanges - Emit visible names into a debug aranges section.
1816///
1817void DwarfWriter::EmitDebugARanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001818 // Start the dwarf aranges section.
1819 Asm->SwitchSection(DwarfARangesSection, 0);
1820
1821 // FIXME - Mock up
1822
1823 // Don't include size of length
Jim Laskeyda427fa2006-01-27 20:31:25 +00001824 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001825
Jim Laskeyda427fa2006-01-27 20:31:25 +00001826 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001827
Jim Laskeyd18e2892006-01-20 20:34:06 +00001828 EmitReference("info_begin", 0); EOL("Offset of Compilation Unit Info");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001829
Jim Laskeyda427fa2006-01-27 20:31:25 +00001830 EmitInt8(AddressSize); EOL("Size of Address");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001831
Jim Laskeyda427fa2006-01-27 20:31:25 +00001832 EmitInt8(0); EOL("Size of Segment Descriptor");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001833
Jim Laskeyda427fa2006-01-27 20:31:25 +00001834 EmitInt16(0); EOL("Pad (1)");
1835 EmitInt16(0); EOL("Pad (2)");
Jim Laskeye719a7c2006-01-18 16:54:26 +00001836
1837 // Range 1
1838 EmitReference("text_begin", 0); EOL("Address");
1839 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
1840
Jim Laskeyda427fa2006-01-27 20:31:25 +00001841 EmitInt32(0); EOL("EOM (1)");
1842 EmitInt32(0); EOL("EOM (2)");
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001843}
1844
1845/// EmitDebugRanges - Emit visible names into a debug ranges section.
1846///
1847void DwarfWriter::EmitDebugRanges() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001848 // Start the dwarf ranges section.
1849 Asm->SwitchSection(DwarfRangesSection, 0);
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001850}
1851
1852/// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1853///
1854void DwarfWriter::EmitDebugMacInfo() {
Jim Laskeye719a7c2006-01-18 16:54:26 +00001855 // Start the dwarf macinfo section.
1856 Asm->SwitchSection(DwarfMacInfoSection, 0);
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001857}
Jim Laskey063e7652006-01-17 17:31:53 +00001858
Jim Laskey52060a02006-01-24 00:49:18 +00001859/// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1860/// header file.
1861void DwarfWriter::ConstructCompileUnitDIEs() {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001862 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001863
1864 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001865 DIE *Unit = NewCompileUnit(CUW[i]);
Jim Laskey52060a02006-01-24 00:49:18 +00001866 CompileUnits.push_back(Unit);
1867 }
1868}
1869
1870/// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1871/// variables.
1872void DwarfWriter::ConstructGlobalDIEs(Module &M) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001873 std::vector<GlobalVariableDesc *> GlobalVariables =
Jim Laskey0420f2a2006-02-22 19:02:11 +00001874 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001875
1876 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001877 GlobalVariableDesc *GVD = GlobalVariables[i];
Jim Laskey0420f2a2006-02-22 19:02:11 +00001878 NewGlobalVariable(GVD);
Jim Laskey52060a02006-01-24 00:49:18 +00001879 }
1880}
1881
Jim Laskey0420f2a2006-02-22 19:02:11 +00001882/// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
1883/// subprograms.
1884void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
1885 std::vector<SubprogramDesc *> Subprograms =
1886 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
1887
1888 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
1889 SubprogramDesc *SPD = Subprograms[i];
1890 NewSubprogram(SPD);
1891 }
1892}
Jim Laskey52060a02006-01-24 00:49:18 +00001893
Jim Laskey063e7652006-01-17 17:31:53 +00001894/// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001895///
1896bool DwarfWriter::ShouldEmitDwarf() {
1897 // Check if debug info is present.
1898 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1899
1900 // Make sure initial declarations are made.
1901 if (!didInitial) {
1902 EmitInitial();
1903 didInitial = true;
1904 }
1905
1906 // Okay to emit.
1907 return true;
1908}
1909
Jim Laskey063e7652006-01-17 17:31:53 +00001910//===----------------------------------------------------------------------===//
Jim Laskeyd18e2892006-01-20 20:34:06 +00001911// Main entry points.
Jim Laskey063e7652006-01-17 17:31:53 +00001912//
Jim Laskey52060a02006-01-24 00:49:18 +00001913
1914DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
1915: O(OS)
1916, Asm(A)
1917, DebugInfo(NULL)
1918, didInitial(false)
1919, CompileUnits()
1920, Abbreviations()
1921, GlobalTypes()
1922, GlobalEntities()
1923, StringPool()
Jim Laskey0420f2a2006-02-22 19:02:11 +00001924, DescToDieMap()
1925, TypeToDieMap()
Jim Laskey52060a02006-01-24 00:49:18 +00001926, AddressSize(sizeof(int32_t))
1927, hasLEB128(false)
1928, hasDotLoc(false)
1929, hasDotFile(false)
1930, needsSet(false)
1931, DwarfAbbrevSection(".debug_abbrev")
1932, DwarfInfoSection(".debug_info")
1933, DwarfLineSection(".debug_line")
1934, DwarfFrameSection(".debug_frame")
1935, DwarfPubNamesSection(".debug_pubnames")
1936, DwarfPubTypesSection(".debug_pubtypes")
1937, DwarfStrSection(".debug_str")
1938, DwarfLocSection(".debug_loc")
1939, DwarfARangesSection(".debug_aranges")
1940, DwarfRangesSection(".debug_ranges")
1941, DwarfMacInfoSection(".debug_macinfo")
1942, TextSection(".text")
1943, DataSection(".data")
1944{}
1945DwarfWriter::~DwarfWriter() {
1946 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1947 delete CompileUnits[i];
Jim Laskey063e7652006-01-17 17:31:53 +00001948 }
Jim Laskey52060a02006-01-24 00:49:18 +00001949}
Jim Laskey063e7652006-01-17 17:31:53 +00001950
1951/// BeginModule - Emit all Dwarf sections that should come prior to the content.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001952///
Jim Laskey52060a02006-01-24 00:49:18 +00001953void DwarfWriter::BeginModule(Module &M) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001954 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00001955 EOL("Dwarf Begin Module");
Jim Laskeyb2efb852006-01-04 22:28:25 +00001956}
1957
Jim Laskey063e7652006-01-17 17:31:53 +00001958/// EndModule - Emit all Dwarf sections that should come after the content.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00001959///
Jim Laskey52060a02006-01-24 00:49:18 +00001960void DwarfWriter::EndModule(Module &M) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001961 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00001962 EOL("Dwarf End Module");
1963
1964 // Standard sections final addresses.
Jim Laskeye719a7c2006-01-18 16:54:26 +00001965 Asm->SwitchSection(TextSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001966 EmitLabel("text_end", 0);
Jim Laskeye719a7c2006-01-18 16:54:26 +00001967 Asm->SwitchSection(DataSection, 0);
Jim Laskey063e7652006-01-17 17:31:53 +00001968 EmitLabel("data_end", 0);
Jim Laskeyd18e2892006-01-20 20:34:06 +00001969
Jim Laskey52060a02006-01-24 00:49:18 +00001970 // Create all the compile unit DIEs.
1971 ConstructCompileUnitDIEs();
1972
1973 // Create DIEs for each of the externally visible global variables.
1974 ConstructGlobalDIEs(M);
Jim Laskey063e7652006-01-17 17:31:53 +00001975
Jim Laskey0420f2a2006-02-22 19:02:11 +00001976 // Create DIEs for each of the externally visible subprograms.
1977 ConstructSubprogramDIEs(M);
1978
Jim Laskey063e7652006-01-17 17:31:53 +00001979 // Compute DIE offsets and sizes.
1980 SizeAndOffsets();
1981
1982 // Emit all the DIEs into a debug info section
1983 EmitDebugInfo();
1984
1985 // Corresponding abbreviations into a abbrev section.
1986 EmitAbbreviations();
1987
1988 // Emit source line correspondence into a debug line section.
1989 EmitDebugLines();
Jim Laskey19ef4ef2006-01-17 20:41:40 +00001990
1991 // Emit info into a debug frame section.
1992 EmitDebugFrame();
1993
1994 // Emit info into a debug pubnames section.
1995 EmitDebugPubNames();
1996
1997 // Emit info into a debug pubtypes section.
1998 EmitDebugPubTypes();
1999
2000 // Emit info into a debug str section.
2001 EmitDebugStr();
2002
2003 // Emit info into a debug loc section.
2004 EmitDebugLoc();
2005
2006 // Emit info into a debug aranges section.
2007 EmitDebugARanges();
2008
2009 // Emit info into a debug ranges section.
2010 EmitDebugRanges();
2011
2012 // Emit info into a debug macinfo section.
2013 EmitDebugMacInfo();
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002014}
2015
Jim Laskeye719a7c2006-01-18 16:54:26 +00002016/// BeginFunction - Gather pre-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002017///
Jim Laskey52060a02006-01-24 00:49:18 +00002018void DwarfWriter::BeginFunction(MachineFunction &MF) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002019 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002020 EOL("Dwarf Begin Function");
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002021}
2022
Jim Laskeye719a7c2006-01-18 16:54:26 +00002023/// EndFunction - Gather and emit post-function debug information.
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002024///
Jim Laskey52060a02006-01-24 00:49:18 +00002025void DwarfWriter::EndFunction(MachineFunction &MF) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002026 if (!ShouldEmitDwarf()) return;
Jim Laskey063e7652006-01-17 17:31:53 +00002027 EOL("Dwarf End Function");
Jim Laskeya7cea6f2006-01-04 13:52:30 +00002028}