Jim Laskey | e503289 | 2005-12-21 19:48:16 +0000 | [diff] [blame] | 1 | //===-- 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 Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/DwarfWriter.h" |
Jim Laskey | e503289 | 2005-12-21 19:48:16 +0000 | [diff] [blame] | 15 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/AsmPrinter.h" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
| 20 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 21 | #include <iostream> |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 22 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 24 | |
| 25 | static cl::opt<bool> |
| 26 | DwarfVerbose("dwarf-verbose", cl::Hidden, |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 27 | cl::desc("Add comments to Dwarf directives.")); |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 30 | |
| 31 | /// TagString - Return the string for the specified tag. |
| 32 | /// |
| 33 | static const char *TagString(unsigned Tag) { |
| 34 | switch(Tag) { |
| 35 | case DW_TAG_array_type: return "TAG_array_type"; |
| 36 | case DW_TAG_class_type: return "TAG_class_type"; |
| 37 | case DW_TAG_entry_point: return "TAG_entry_point"; |
| 38 | case DW_TAG_enumeration_type: return "TAG_enumeration_type"; |
| 39 | case DW_TAG_formal_parameter: return "TAG_formal_parameter"; |
| 40 | case DW_TAG_imported_declaration: return "TAG_imported_declaration"; |
| 41 | case DW_TAG_label: return "TAG_label"; |
| 42 | case DW_TAG_lexical_block: return "TAG_lexical_block"; |
| 43 | case DW_TAG_member: return "TAG_member"; |
| 44 | case DW_TAG_pointer_type: return "TAG_pointer_type"; |
| 45 | case DW_TAG_reference_type: return "TAG_reference_type"; |
| 46 | case DW_TAG_compile_unit: return "TAG_compile_unit"; |
| 47 | case DW_TAG_string_type: return "TAG_string_type"; |
| 48 | case DW_TAG_structure_type: return "TAG_structure_type"; |
| 49 | case DW_TAG_subroutine_type: return "TAG_subroutine_type"; |
| 50 | case DW_TAG_typedef: return "TAG_typedef"; |
| 51 | case DW_TAG_union_type: return "TAG_union_type"; |
| 52 | case DW_TAG_unspecified_parameters: return "TAG_unspecified_parameters"; |
| 53 | case DW_TAG_variant: return "TAG_variant"; |
| 54 | case DW_TAG_common_block: return "TAG_common_block"; |
| 55 | case DW_TAG_common_inclusion: return "TAG_common_inclusion"; |
| 56 | case DW_TAG_inheritance: return "TAG_inheritance"; |
| 57 | case DW_TAG_inlined_subroutine: return "TAG_inlined_subroutine"; |
| 58 | case DW_TAG_module: return "TAG_module"; |
| 59 | case DW_TAG_ptr_to_member_type: return "TAG_ptr_to_member_type"; |
| 60 | case DW_TAG_set_type: return "TAG_set_type"; |
| 61 | case DW_TAG_subrange_type: return "TAG_subrange_type"; |
| 62 | case DW_TAG_with_stmt: return "TAG_with_stmt"; |
| 63 | case DW_TAG_access_declaration: return "TAG_access_declaration"; |
| 64 | case DW_TAG_base_type: return "TAG_base_type"; |
| 65 | case DW_TAG_catch_block: return "TAG_catch_block"; |
| 66 | case DW_TAG_const_type: return "TAG_const_type"; |
| 67 | case DW_TAG_constant: return "TAG_constant"; |
| 68 | case DW_TAG_enumerator: return "TAG_enumerator"; |
| 69 | case DW_TAG_file_type: return "TAG_file_type"; |
| 70 | case DW_TAG_friend: return "TAG_friend"; |
| 71 | case DW_TAG_namelist: return "TAG_namelist"; |
| 72 | case DW_TAG_namelist_item: return "TAG_namelist_item"; |
| 73 | case DW_TAG_packed_type: return "TAG_packed_type"; |
| 74 | case DW_TAG_subprogram: return "TAG_subprogram"; |
| 75 | case DW_TAG_template_type_parameter: return "TAG_template_type_parameter"; |
| 76 | case DW_TAG_template_value_parameter: return "TAG_template_value_parameter"; |
| 77 | case DW_TAG_thrown_type: return "TAG_thrown_type"; |
| 78 | case DW_TAG_try_block: return "TAG_try_block"; |
| 79 | case DW_TAG_variant_part: return "TAG_variant_part"; |
| 80 | case DW_TAG_variable: return "TAG_variable"; |
| 81 | case DW_TAG_volatile_type: return "TAG_volatile_type"; |
| 82 | case DW_TAG_dwarf_procedure: return "TAG_dwarf_procedure"; |
| 83 | case DW_TAG_restrict_type: return "TAG_restrict_type"; |
| 84 | case DW_TAG_interface_type: return "TAG_interface_type"; |
| 85 | case DW_TAG_namespace: return "TAG_namespace"; |
| 86 | case DW_TAG_imported_module: return "TAG_imported_module"; |
| 87 | case DW_TAG_unspecified_type: return "TAG_unspecified_type"; |
| 88 | case DW_TAG_partial_unit: return "TAG_partial_unit"; |
| 89 | case DW_TAG_imported_unit: return "TAG_imported_unit"; |
| 90 | case DW_TAG_condition: return "TAG_condition"; |
| 91 | case DW_TAG_shared_type: return "TAG_shared_type"; |
| 92 | case DW_TAG_lo_user: return "TAG_lo_user"; |
| 93 | case DW_TAG_hi_user: return "TAG_hi_user"; |
| 94 | } |
| 95 | assert(0 && "Unknown Dwarf Tag"); |
| 96 | return ""; |
| 97 | } |
| 98 | |
| 99 | /// ChildrenString - Return the string for the specified children flag. |
| 100 | /// |
| 101 | static const char *ChildrenString(unsigned Children) { |
| 102 | switch(Children) { |
| 103 | case DW_CHILDREN_no: return "CHILDREN_no"; |
| 104 | case DW_CHILDREN_yes: return "CHILDREN_yes"; |
| 105 | } |
| 106 | assert(0 && "Unknown Dwarf ChildrenFlag"); |
| 107 | return ""; |
| 108 | } |
| 109 | |
| 110 | /// AttributeString - Return the string for the specified attribute. |
| 111 | /// |
| 112 | static const char *AttributeString(unsigned Attribute) { |
| 113 | switch(Attribute) { |
| 114 | case DW_AT_sibling: return "AT_sibling"; |
| 115 | case DW_AT_location: return "AT_location"; |
| 116 | case DW_AT_name: return "AT_name"; |
| 117 | case DW_AT_ordering: return "AT_ordering"; |
| 118 | case DW_AT_byte_size: return "AT_byte_size"; |
| 119 | case DW_AT_bit_offset: return "AT_bit_offset"; |
| 120 | case DW_AT_bit_size: return "AT_bit_size"; |
| 121 | case DW_AT_stmt_list: return "AT_stmt_list"; |
| 122 | case DW_AT_low_pc: return "AT_low_pc"; |
| 123 | case DW_AT_high_pc: return "AT_high_pc"; |
| 124 | case DW_AT_language: return "AT_language"; |
| 125 | case DW_AT_discr: return "AT_discr"; |
| 126 | case DW_AT_discr_value: return "AT_discr_value"; |
| 127 | case DW_AT_visibility: return "AT_visibility"; |
| 128 | case DW_AT_import: return "AT_import"; |
| 129 | case DW_AT_string_length: return "AT_string_length"; |
| 130 | case DW_AT_common_reference: return "AT_common_reference"; |
| 131 | case DW_AT_comp_dir: return "AT_comp_dir"; |
| 132 | case DW_AT_const_value: return "AT_const_value"; |
| 133 | case DW_AT_containing_type: return "AT_containing_type"; |
| 134 | case DW_AT_default_value: return "AT_default_value"; |
| 135 | case DW_AT_inline: return "AT_inline"; |
| 136 | case DW_AT_is_optional: return "AT_is_optional"; |
| 137 | case DW_AT_lower_bound: return "AT_lower_bound"; |
| 138 | case DW_AT_producer: return "AT_producer"; |
| 139 | case DW_AT_prototyped: return "AT_prototyped"; |
| 140 | case DW_AT_return_addr: return "AT_return_addr"; |
| 141 | case DW_AT_start_scope: return "AT_start_scope"; |
| 142 | case DW_AT_bit_stride: return "AT_bit_stride"; |
| 143 | case DW_AT_upper_bound: return "AT_upper_bound"; |
| 144 | case DW_AT_abstract_origin: return "AT_abstract_origin"; |
| 145 | case DW_AT_accessibility: return "AT_accessibility"; |
| 146 | case DW_AT_address_class: return "AT_address_class"; |
| 147 | case DW_AT_artificial: return "AT_artificial"; |
| 148 | case DW_AT_base_types: return "AT_base_types"; |
| 149 | case DW_AT_calling_convention: return "AT_calling_convention"; |
| 150 | case DW_AT_count: return "AT_count"; |
| 151 | case DW_AT_data_member_location: return "AT_data_member_location"; |
| 152 | case DW_AT_decl_column: return "AT_decl_column"; |
| 153 | case DW_AT_decl_file: return "AT_decl_file"; |
| 154 | case DW_AT_decl_line: return "AT_decl_line"; |
| 155 | case DW_AT_declaration: return "AT_declaration"; |
| 156 | case DW_AT_discr_list: return "AT_discr_list"; |
| 157 | case DW_AT_encoding: return "AT_encoding"; |
| 158 | case DW_AT_external: return "AT_external"; |
| 159 | case DW_AT_frame_base: return "AT_frame_base"; |
| 160 | case DW_AT_friend: return "AT_friend"; |
| 161 | case DW_AT_identifier_case: return "AT_identifier_case"; |
| 162 | case DW_AT_macro_info: return "AT_macro_info"; |
| 163 | case DW_AT_namelist_item: return "AT_namelist_item"; |
| 164 | case DW_AT_priority: return "AT_priority"; |
| 165 | case DW_AT_segment: return "AT_segment"; |
| 166 | case DW_AT_specification: return "AT_specification"; |
| 167 | case DW_AT_static_link: return "AT_static_link"; |
| 168 | case DW_AT_type: return "AT_type"; |
| 169 | case DW_AT_use_location: return "AT_use_location"; |
| 170 | case DW_AT_variable_parameter: return "AT_variable_parameter"; |
| 171 | case DW_AT_virtuality: return "AT_virtuality"; |
| 172 | case DW_AT_vtable_elem_location: return "AT_vtable_elem_location"; |
| 173 | case DW_AT_allocated: return "AT_allocated"; |
| 174 | case DW_AT_associated: return "AT_associated"; |
| 175 | case DW_AT_data_location: return "AT_data_location"; |
| 176 | case DW_AT_byte_stride: return "AT_byte_stride"; |
| 177 | case DW_AT_entry_pc: return "AT_entry_pc"; |
| 178 | case DW_AT_use_UTF8: return "AT_use_UTF8"; |
| 179 | case DW_AT_extension: return "AT_extension"; |
| 180 | case DW_AT_ranges: return "AT_ranges"; |
| 181 | case DW_AT_trampoline: return "AT_trampoline"; |
| 182 | case DW_AT_call_column: return "AT_call_column"; |
| 183 | case DW_AT_call_file: return "AT_call_file"; |
| 184 | case DW_AT_call_line: return "AT_call_line"; |
| 185 | case DW_AT_description: return "AT_description"; |
| 186 | case DW_AT_binary_scale: return "AT_binary_scale"; |
| 187 | case DW_AT_decimal_scale: return "AT_decimal_scale"; |
| 188 | case DW_AT_small: return "AT_small"; |
| 189 | case DW_AT_decimal_sign: return "AT_decimal_sign"; |
| 190 | case DW_AT_digit_count: return "AT_digit_count"; |
| 191 | case DW_AT_picture_string: return "AT_picture_string"; |
| 192 | case DW_AT_mutable: return "AT_mutable"; |
| 193 | case DW_AT_threads_scaled: return "AT_threads_scaled"; |
| 194 | case DW_AT_explicit: return "AT_explicit"; |
| 195 | case DW_AT_object_pointer: return "AT_object_pointer"; |
| 196 | case DW_AT_endianity: return "AT_endianity"; |
| 197 | case DW_AT_elemental: return "AT_elemental"; |
| 198 | case DW_AT_pure: return "AT_pure"; |
| 199 | case DW_AT_recursive: return "AT_recursive"; |
| 200 | case DW_AT_lo_user: return "AT_lo_user"; |
| 201 | case DW_AT_hi_user: return "AT_hi_user"; |
| 202 | } |
| 203 | assert(0 && "Unknown Dwarf Attribute"); |
| 204 | return ""; |
| 205 | } |
| 206 | |
| 207 | /// FormEncodingString - Return the string for the specified form encoding. |
| 208 | /// |
| 209 | static const char *FormEncodingString(unsigned Encoding) { |
| 210 | switch(Encoding) { |
| 211 | case DW_FORM_addr: return "FORM_addr"; |
| 212 | case DW_FORM_block2: return "FORM_block2"; |
| 213 | case DW_FORM_block4: return "FORM_block4"; |
| 214 | case DW_FORM_data2: return "FORM_data2"; |
| 215 | case DW_FORM_data4: return "FORM_data4"; |
| 216 | case DW_FORM_data8: return "FORM_data8"; |
| 217 | case DW_FORM_string: return "FORM_string"; |
| 218 | case DW_FORM_block: return "FORM_block"; |
| 219 | case DW_FORM_block1: return "FORM_block1"; |
| 220 | case DW_FORM_data1: return "FORM_data1"; |
| 221 | case DW_FORM_flag: return "FORM_flag"; |
| 222 | case DW_FORM_sdata: return "FORM_sdata"; |
| 223 | case DW_FORM_strp: return "FORM_strp"; |
| 224 | case DW_FORM_udata: return "FORM_udata"; |
| 225 | case DW_FORM_ref_addr: return "FORM_ref_addr"; |
| 226 | case DW_FORM_ref1: return "FORM_ref1"; |
| 227 | case DW_FORM_ref2: return "FORM_ref2"; |
| 228 | case DW_FORM_ref4: return "FORM_ref4"; |
| 229 | case DW_FORM_ref8: return "FORM_ref8"; |
| 230 | case DW_FORM_ref_udata: return "FORM_ref_udata"; |
| 231 | case DW_FORM_indirect: return "FORM_indirect"; |
| 232 | } |
| 233 | assert(0 && "Unknown Dwarf Form Encoding"); |
| 234 | return ""; |
| 235 | } |
| 236 | |
| 237 | /// OperationEncodingString - Return the string for the specified operation |
| 238 | /// encoding. |
| 239 | static const char *OperationEncodingString(unsigned Encoding) { |
| 240 | switch(Encoding) { |
| 241 | case DW_OP_addr: return "OP_addr"; |
| 242 | case DW_OP_deref: return "OP_deref"; |
| 243 | case DW_OP_const1u: return "OP_const1u"; |
| 244 | case DW_OP_const1s: return "OP_const1s"; |
| 245 | case DW_OP_const2u: return "OP_const2u"; |
| 246 | case DW_OP_const2s: return "OP_const2s"; |
| 247 | case DW_OP_const4u: return "OP_const4u"; |
| 248 | case DW_OP_const4s: return "OP_const4s"; |
| 249 | case DW_OP_const8u: return "OP_const8u"; |
| 250 | case DW_OP_const8s: return "OP_const8s"; |
| 251 | case DW_OP_constu: return "OP_constu"; |
| 252 | case DW_OP_consts: return "OP_consts"; |
| 253 | case DW_OP_dup: return "OP_dup"; |
| 254 | case DW_OP_drop: return "OP_drop"; |
| 255 | case DW_OP_over: return "OP_over"; |
| 256 | case DW_OP_pick: return "OP_pick"; |
| 257 | case DW_OP_swap: return "OP_swap"; |
| 258 | case DW_OP_rot: return "OP_rot"; |
| 259 | case DW_OP_xderef: return "OP_xderef"; |
| 260 | case DW_OP_abs: return "OP_abs"; |
| 261 | case DW_OP_and: return "OP_and"; |
| 262 | case DW_OP_div: return "OP_div"; |
| 263 | case DW_OP_minus: return "OP_minus"; |
| 264 | case DW_OP_mod: return "OP_mod"; |
| 265 | case DW_OP_mul: return "OP_mul"; |
| 266 | case DW_OP_neg: return "OP_neg"; |
| 267 | case DW_OP_not: return "OP_not"; |
| 268 | case DW_OP_or: return "OP_or"; |
| 269 | case DW_OP_plus: return "OP_plus"; |
| 270 | case DW_OP_plus_uconst: return "OP_plus_uconst"; |
| 271 | case DW_OP_shl: return "OP_shl"; |
| 272 | case DW_OP_shr: return "OP_shr"; |
| 273 | case DW_OP_shra: return "OP_shra"; |
| 274 | case DW_OP_xor: return "OP_xor"; |
| 275 | case DW_OP_skip: return "OP_skip"; |
| 276 | case DW_OP_bra: return "OP_bra"; |
| 277 | case DW_OP_eq: return "OP_eq"; |
| 278 | case DW_OP_ge: return "OP_ge"; |
| 279 | case DW_OP_gt: return "OP_gt"; |
| 280 | case DW_OP_le: return "OP_le"; |
| 281 | case DW_OP_lt: return "OP_lt"; |
| 282 | case DW_OP_ne: return "OP_ne"; |
| 283 | case DW_OP_lit0: return "OP_lit0"; |
| 284 | case DW_OP_lit1: return "OP_lit1"; |
| 285 | case DW_OP_lit31: return "OP_lit31"; |
| 286 | case DW_OP_reg0: return "OP_reg0"; |
| 287 | case DW_OP_reg1: return "OP_reg1"; |
| 288 | case DW_OP_reg31: return "OP_reg31"; |
| 289 | case DW_OP_breg0: return "OP_breg0"; |
| 290 | case DW_OP_breg1: return "OP_breg1"; |
| 291 | case DW_OP_breg31: return "OP_breg31"; |
| 292 | case DW_OP_regx: return "OP_regx"; |
| 293 | case DW_OP_fbreg: return "OP_fbreg"; |
| 294 | case DW_OP_bregx: return "OP_bregx"; |
| 295 | case DW_OP_piece: return "OP_piece"; |
| 296 | case DW_OP_deref_size: return "OP_deref_size"; |
| 297 | case DW_OP_xderef_size: return "OP_xderef_size"; |
| 298 | case DW_OP_nop: return "OP_nop"; |
| 299 | case DW_OP_push_object_address: return "OP_push_object_address"; |
| 300 | case DW_OP_call2: return "OP_call2"; |
| 301 | case DW_OP_call4: return "OP_call4"; |
| 302 | case DW_OP_call_ref: return "OP_call_ref"; |
| 303 | case DW_OP_form_tls_address: return "OP_form_tls_address"; |
| 304 | case DW_OP_call_frame_cfa: return "OP_call_frame_cfa"; |
| 305 | case DW_OP_lo_user: return "OP_lo_user"; |
| 306 | case DW_OP_hi_user: return "OP_hi_user"; |
| 307 | } |
| 308 | assert(0 && "Unknown Dwarf Operation Encoding"); |
| 309 | return ""; |
| 310 | } |
| 311 | |
| 312 | /// AttributeEncodingString - Return the string for the specified attribute |
| 313 | /// encoding. |
| 314 | static const char *AttributeEncodingString(unsigned Encoding) { |
| 315 | switch(Encoding) { |
| 316 | case DW_ATE_address: return "ATE_address"; |
| 317 | case DW_ATE_boolean: return "ATE_boolean"; |
| 318 | case DW_ATE_complex_float: return "ATE_complex_float"; |
| 319 | case DW_ATE_float: return "ATE_float"; |
| 320 | case DW_ATE_signed: return "ATE_signed"; |
| 321 | case DW_ATE_signed_char: return "ATE_signed_char"; |
| 322 | case DW_ATE_unsigned: return "ATE_unsigned"; |
| 323 | case DW_ATE_unsigned_char: return "ATE_unsigned_char"; |
| 324 | case DW_ATE_imaginary_float: return "ATE_imaginary_float"; |
| 325 | case DW_ATE_packed_decimal: return "ATE_packed_decimal"; |
| 326 | case DW_ATE_numeric_string: return "ATE_numeric_string"; |
| 327 | case DW_ATE_edited: return "ATE_edited"; |
| 328 | case DW_ATE_signed_fixed: return "ATE_signed_fixed"; |
| 329 | case DW_ATE_unsigned_fixed: return "ATE_unsigned_fixed"; |
| 330 | case DW_ATE_decimal_float: return "ATE_decimal_float"; |
| 331 | case DW_ATE_lo_user: return "ATE_lo_user"; |
| 332 | case DW_ATE_hi_user: return "ATE_hi_user"; |
| 333 | } |
| 334 | assert(0 && "Unknown Dwarf Attribute Encoding"); |
| 335 | return ""; |
| 336 | } |
| 337 | |
| 338 | /// DecimalSignString - Return the string for the specified decimal sign |
| 339 | /// attribute. |
| 340 | static const char *DecimalSignString(unsigned Sign) { |
| 341 | switch(Sign) { |
| 342 | case DW_DS_unsigned: return "DS_unsigned"; |
| 343 | case DW_DS_leading_overpunch: return "DS_leading_overpunch"; |
| 344 | case DW_DS_trailing_overpunch: return "DS_trailing_overpunch"; |
| 345 | case DW_DS_leading_separate: return "DS_leading_separate"; |
| 346 | case DW_DS_trailing_separate: return "DS_trailing_separate"; |
| 347 | } |
| 348 | assert(0 && "Unknown Dwarf Decimal Sign Attribute"); |
| 349 | return ""; |
| 350 | } |
| 351 | |
| 352 | /// EndianityString - Return the string for the specified endianity. |
| 353 | /// |
| 354 | static const char *EndianityString(unsigned Endian) { |
| 355 | switch(Endian) { |
| 356 | case DW_END_default: return "END_default"; |
| 357 | case DW_END_big: return "END_big"; |
| 358 | case DW_END_little: return "END_little"; |
| 359 | case DW_END_lo_user: return "END_lo_user"; |
| 360 | case DW_END_hi_user: return "END_hi_user"; |
| 361 | } |
| 362 | assert(0 && "Unknown Dwarf Endianity"); |
| 363 | return ""; |
| 364 | } |
| 365 | |
| 366 | /// AccessibilityString - Return the string for the specified accessibility. |
| 367 | /// |
| 368 | static const char *AccessibilityString(unsigned Access) { |
| 369 | switch(Access) { |
| 370 | // Accessibility codes |
| 371 | case DW_ACCESS_public: return "ACCESS_public"; |
| 372 | case DW_ACCESS_protected: return "ACCESS_protected"; |
| 373 | case DW_ACCESS_private: return "ACCESS_private"; |
| 374 | } |
| 375 | assert(0 && "Unknown Dwarf Accessibility"); |
| 376 | return ""; |
| 377 | } |
| 378 | |
| 379 | /// VisibilityString - Return the string for the specified visibility. |
| 380 | /// |
| 381 | static const char *VisibilityString(unsigned Visibility) { |
| 382 | switch(Visibility) { |
| 383 | case DW_VIS_local: return "VIS_local"; |
| 384 | case DW_VIS_exported: return "VIS_exported"; |
| 385 | case DW_VIS_qualified: return "VIS_qualified"; |
| 386 | } |
| 387 | assert(0 && "Unknown Dwarf Visibility"); |
| 388 | return ""; |
| 389 | } |
| 390 | |
| 391 | /// VirtualityString - Return the string for the specified virtuality. |
| 392 | /// |
| 393 | static const char *VirtualityString(unsigned Virtuality) { |
| 394 | switch(Virtuality) { |
| 395 | case DW_VIRTUALITY_none: return "VIRTUALITY_none"; |
| 396 | case DW_VIRTUALITY_virtual: return "VIRTUALITY_virtual"; |
| 397 | case DW_VIRTUALITY_pure_virtual: return "VIRTUALITY_pure_virtual"; |
| 398 | } |
| 399 | assert(0 && "Unknown Dwarf Virtuality"); |
| 400 | return ""; |
| 401 | } |
| 402 | |
| 403 | /// LanguageString - Return the string for the specified language. |
| 404 | /// |
| 405 | static const char *LanguageString(unsigned Language) { |
| 406 | switch(Language) { |
| 407 | case DW_LANG_C89: return "LANG_C89"; |
| 408 | case DW_LANG_C: return "LANG_C"; |
| 409 | case DW_LANG_Ada83: return "LANG_Ada83"; |
| 410 | case DW_LANG_C_plus_plus: return "LANG_C_plus_plus"; |
| 411 | case DW_LANG_Cobol74: return "LANG_Cobol74"; |
| 412 | case DW_LANG_Cobol85: return "LANG_Cobol85"; |
| 413 | case DW_LANG_Fortran77: return "LANG_Fortran77"; |
| 414 | case DW_LANG_Fortran90: return "LANG_Fortran90"; |
| 415 | case DW_LANG_Pascal83: return "LANG_Pascal83"; |
| 416 | case DW_LANG_Modula2: return "LANG_Modula2"; |
| 417 | case DW_LANG_Java: return "LANG_Java"; |
| 418 | case DW_LANG_C99: return "LANG_C99"; |
| 419 | case DW_LANG_Ada95: return "LANG_Ada95"; |
| 420 | case DW_LANG_Fortran95: return "LANG_Fortran95"; |
| 421 | case DW_LANG_PLI: return "LANG_PLI"; |
| 422 | case DW_LANG_ObjC: return "LANG_ObjC"; |
| 423 | case DW_LANG_ObjC_plus_plus: return "LANG_ObjC_plus_plus"; |
| 424 | case DW_LANG_UPC: return "LANG_UPC"; |
| 425 | case DW_LANG_D: return "LANG_D"; |
| 426 | case DW_LANG_lo_user: return "LANG_lo_user"; |
| 427 | case DW_LANG_hi_user: return "LANG_hi_user"; |
| 428 | } |
| 429 | assert(0 && "Unknown Dwarf Language"); |
| 430 | return ""; |
| 431 | } |
| 432 | |
| 433 | /// CaseString - Return the string for the specified identifier case. |
| 434 | /// |
| 435 | static const char *CaseString(unsigned Case) { |
| 436 | switch(Case) { |
| 437 | case DW_ID_case_sensitive: return "ID_case_sensitive"; |
| 438 | case DW_ID_up_case: return "ID_up_case"; |
| 439 | case DW_ID_down_case: return "ID_down_case"; |
| 440 | case DW_ID_case_insensitive: return "ID_case_insensitive"; |
| 441 | } |
| 442 | assert(0 && "Unknown Dwarf Identifier Case"); |
| 443 | return ""; |
| 444 | } |
| 445 | |
| 446 | /// ConventionString - Return the string for the specified calling convention. |
| 447 | /// |
| 448 | static const char *ConventionString(unsigned Convention) { |
| 449 | switch(Convention) { |
| 450 | case DW_CC_normal: return "CC_normal"; |
| 451 | case DW_CC_program: return "CC_program"; |
| 452 | case DW_CC_nocall: return "CC_nocall"; |
| 453 | case DW_CC_lo_user: return "CC_lo_user"; |
| 454 | case DW_CC_hi_user: return "CC_hi_user"; |
| 455 | } |
| 456 | assert(0 && "Unknown Dwarf Calling Convention"); |
| 457 | return ""; |
| 458 | } |
| 459 | |
| 460 | /// InlineCodeString - Return the string for the specified inline code. |
| 461 | /// |
| 462 | static const char *InlineCodeString(unsigned Code) { |
| 463 | switch(Code) { |
| 464 | case DW_INL_not_inlined: return "INL_not_inlined"; |
| 465 | case DW_INL_inlined: return "INL_inlined"; |
| 466 | case DW_INL_declared_not_inlined: return "INL_declared_not_inlined"; |
| 467 | case DW_INL_declared_inlined: return "INL_declared_inlined"; |
| 468 | } |
| 469 | assert(0 && "Unknown Dwarf Inline Code"); |
| 470 | return ""; |
| 471 | } |
| 472 | |
| 473 | /// ArrayOrderString - Return the string for the specified array order. |
| 474 | /// |
| 475 | static const char *ArrayOrderString(unsigned Order) { |
| 476 | switch(Order) { |
| 477 | case DW_ORD_row_major: return "ORD_row_major"; |
| 478 | case DW_ORD_col_major: return "ORD_col_major"; |
| 479 | } |
| 480 | assert(0 && "Unknown Dwarf Array Order"); |
| 481 | return ""; |
| 482 | } |
| 483 | |
| 484 | /// DiscriminantString - Return the string for the specified discriminant |
| 485 | /// descriptor. |
| 486 | static const char *DiscriminantString(unsigned Discriminant) { |
| 487 | switch(Discriminant) { |
| 488 | case DW_DSC_label: return "DSC_label"; |
| 489 | case DW_DSC_range: return "DSC_range"; |
| 490 | } |
| 491 | assert(0 && "Unknown Dwarf Discriminant Descriptor"); |
| 492 | return ""; |
| 493 | } |
| 494 | |
| 495 | /// LNStandardString - Return the string for the specified line number standard. |
| 496 | /// |
| 497 | static const char *LNStandardString(unsigned Standard) { |
| 498 | switch(Standard) { |
| 499 | case DW_LNS_copy: return "LNS_copy"; |
| 500 | case DW_LNS_advance_pc: return "LNS_advance_pc"; |
| 501 | case DW_LNS_advance_line: return "LNS_advance_line"; |
| 502 | case DW_LNS_set_file: return "LNS_set_file"; |
| 503 | case DW_LNS_set_column: return "LNS_set_column"; |
| 504 | case DW_LNS_negate_stmt: return "LNS_negate_stmt"; |
| 505 | case DW_LNS_set_basic_block: return "LNS_set_basic_block"; |
| 506 | case DW_LNS_const_add_pc: return "LNS_const_add_pc"; |
| 507 | case DW_LNS_fixed_advance_pc: return "LNS_fixed_advance_pc"; |
| 508 | case DW_LNS_set_prologue_end: return "LNS_set_prologue_end"; |
| 509 | case DW_LNS_set_epilogue_begin: return "LNS_set_epilogue_begin"; |
| 510 | case DW_LNS_set_isa: return "LNS_set_isa"; |
| 511 | } |
| 512 | assert(0 && "Unknown Dwarf Line Number Standard"); |
| 513 | return ""; |
| 514 | } |
| 515 | |
| 516 | /// LNExtendedString - Return the string for the specified line number extended |
| 517 | /// opcode encodings. |
| 518 | static const char *LNExtendedString(unsigned Encoding) { |
| 519 | switch(Encoding) { |
| 520 | // Line Number Extended Opcode Encodings |
| 521 | case DW_LNE_end_sequence: return "LNE_end_sequence"; |
| 522 | case DW_LNE_set_address: return "LNE_set_address"; |
| 523 | case DW_LNE_define_file: return "LNE_define_file"; |
| 524 | case DW_LNE_lo_user: return "LNE_lo_user"; |
| 525 | case DW_LNE_hi_user: return "LNE_hi_user"; |
| 526 | } |
| 527 | assert(0 && "Unknown Dwarf Line Number Extended Opcode Encoding"); |
| 528 | return ""; |
| 529 | } |
| 530 | |
| 531 | /// MacinfoString - Return the string for the specified macinfo type encodings. |
| 532 | /// |
| 533 | static const char *MacinfoString(unsigned Encoding) { |
| 534 | switch(Encoding) { |
| 535 | // Macinfo Type Encodings |
| 536 | case DW_MACINFO_define: return "MACINFO_define"; |
| 537 | case DW_MACINFO_undef: return "MACINFO_undef"; |
| 538 | case DW_MACINFO_start_file: return "MACINFO_start_file"; |
| 539 | case DW_MACINFO_end_file: return "MACINFO_end_file"; |
| 540 | case DW_MACINFO_vendor_ext: return "MACINFO_vendor_ext"; |
| 541 | } |
| 542 | assert(0 && "Unknown Dwarf Macinfo Type Encodings"); |
| 543 | return ""; |
| 544 | } |
| 545 | |
| 546 | /// CallFrameString - Return the string for the specified call frame instruction |
| 547 | /// encodings. |
| 548 | static const char *CallFrameString(unsigned Encoding) { |
| 549 | switch(Encoding) { |
| 550 | case DW_CFA_advance_loc: return "CFA_advance_loc"; |
| 551 | case DW_CFA_offset: return "CFA_offset"; |
| 552 | case DW_CFA_restore: return "CFA_restore"; |
| 553 | case DW_CFA_set_loc: return "CFA_set_loc"; |
| 554 | case DW_CFA_advance_loc1: return "CFA_advance_loc1"; |
| 555 | case DW_CFA_advance_loc2: return "CFA_advance_loc2"; |
| 556 | case DW_CFA_advance_loc4: return "CFA_advance_loc4"; |
| 557 | case DW_CFA_offset_extended: return "CFA_offset_extended"; |
| 558 | case DW_CFA_restore_extended: return "CFA_restore_extended"; |
| 559 | case DW_CFA_undefined: return "CFA_undefined"; |
| 560 | case DW_CFA_same_value: return "CFA_same_value"; |
| 561 | case DW_CFA_register: return "CFA_register"; |
| 562 | case DW_CFA_remember_state: return "CFA_remember_state"; |
| 563 | case DW_CFA_restore_state: return "CFA_restore_state"; |
| 564 | case DW_CFA_def_cfa: return "CFA_def_cfa"; |
| 565 | case DW_CFA_def_cfa_register: return "CFA_def_cfa_register"; |
| 566 | case DW_CFA_def_cfa_offset: return "CFA_def_cfa_offset"; |
| 567 | case DW_CFA_def_cfa_expression: return "CFA_def_cfa_expression"; |
| 568 | case DW_CFA_expression: return "CFA_expression"; |
| 569 | case DW_CFA_offset_extended_sf: return "CFA_offset_extended_sf"; |
| 570 | case DW_CFA_def_cfa_sf: return "CFA_def_cfa_sf"; |
| 571 | case DW_CFA_def_cfa_offset_sf: return "CFA_def_cfa_offset_sf"; |
| 572 | case DW_CFA_val_offset: return "CFA_val_offset"; |
| 573 | case DW_CFA_val_offset_sf: return "CFA_val_offset_sf"; |
| 574 | case DW_CFA_val_expression: return "CFA_val_expression"; |
| 575 | case DW_CFA_lo_user: return "CFA_lo_user"; |
| 576 | case DW_CFA_hi_user: return "CFA_hi_user"; |
| 577 | } |
| 578 | assert(0 && "Unknown Dwarf Call Frame Instruction Encodings"); |
| 579 | return ""; |
| 580 | } |
| 581 | |
| 582 | //===----------------------------------------------------------------------===// |
| 583 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 584 | /// operator== - Used by UniqueVector to locate entry. |
| 585 | /// |
| 586 | bool DIEAbbrev::operator==(const DIEAbbrev &DA) const { |
| 587 | if (Tag != DA.Tag) return false; |
| 588 | if (ChildrenFlag != DA.ChildrenFlag) return false; |
| 589 | if (Data.size() != DA.Data.size()) return false; |
| 590 | |
| 591 | for (unsigned i = 0, N = Data.size(); i < N; i++) { |
| 592 | const DIEAbbrevData &AttrData = Data[i]; |
Jim Laskey | 7368321 | 2006-01-21 00:59:54 +0000 | [diff] [blame^] | 593 | const DIEAbbrevData &DAAttrData = DA.Data[i]; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 594 | if (AttrData.getAttribute() != DAAttrData.getAttribute()) return false; |
| 595 | if (AttrData.getForm() != DAAttrData.getForm()) return false; |
| 596 | } |
| 597 | |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | /// operator< - Used by UniqueVector to locate entry. |
| 602 | /// |
| 603 | bool DIEAbbrev::operator<(const DIEAbbrev &DA) const { |
| 604 | if (Tag != DA.Tag) return Tag < DA.Tag; |
| 605 | if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag; |
| 606 | if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size(); |
| 607 | |
| 608 | for (unsigned i = 0, N = Data.size(); i < N; i++) { |
| 609 | const DIEAbbrevData &AttrData = Data[i]; |
| 610 | const DIEAbbrevData &DAAttrData = DA.Data[i]; |
| 611 | if (AttrData.getAttribute() != DAAttrData.getAttribute()) |
| 612 | return AttrData.getAttribute() < DAAttrData.getAttribute(); |
| 613 | if (AttrData.getForm() != DAAttrData.getForm()) |
| 614 | return AttrData.getForm() < DAAttrData.getForm(); |
| 615 | } |
| 616 | |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | /// Emit - Print the abbreviation using the specified Dwarf writer. |
| 621 | /// |
| 622 | void DIEAbbrev::Emit(const DwarfWriter &DW) const { |
| 623 | // Emit its Dwarf tag type. |
| 624 | DW.EmitULEB128Bytes(Tag); |
| 625 | DW.EOL(TagString(Tag)); |
| 626 | |
| 627 | // Emit whether it has children DIEs. |
| 628 | DW.EmitULEB128Bytes(ChildrenFlag); |
| 629 | DW.EOL(ChildrenString(ChildrenFlag)); |
| 630 | |
| 631 | // For each attribute description. |
| 632 | for (unsigned i = 0, N = Data.size(); i < N; i++) { |
| 633 | const DIEAbbrevData &AttrData = Data[i]; |
| 634 | |
| 635 | // Emit attribute type. |
| 636 | DW.EmitULEB128Bytes(AttrData.getAttribute()); |
| 637 | DW.EOL(AttributeString(AttrData.getAttribute())); |
| 638 | |
| 639 | // Emit form type. |
| 640 | DW.EmitULEB128Bytes(AttrData.getForm()); |
| 641 | DW.EOL(FormEncodingString(AttrData.getForm())); |
| 642 | } |
| 643 | |
| 644 | // Mark end of abbreviation. |
| 645 | DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)"); |
| 646 | DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)"); |
| 647 | } |
| 648 | |
| 649 | #ifndef NDEBUG |
| 650 | void DIEAbbrev::print(std::ostream &O) { |
| 651 | O << "Abbreviation @" |
| 652 | << std::hex << (unsigned)this << std::dec |
| 653 | << " " |
| 654 | << TagString(Tag) |
| 655 | << " " |
| 656 | << ChildrenString(ChildrenFlag) |
| 657 | << "\n"; |
| 658 | |
| 659 | for (unsigned i = 0, N = Data.size(); i < N; i++) { |
| 660 | O << " " |
| 661 | << AttributeString(Data[i].getAttribute()) |
| 662 | << " " |
| 663 | << FormEncodingString(Data[i].getForm()) |
| 664 | << "\n"; |
| 665 | } |
| 666 | } |
| 667 | void DIEAbbrev::dump() { print(std::cerr); } |
| 668 | #endif |
| 669 | |
| 670 | //===----------------------------------------------------------------------===// |
| 671 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 672 | /// EmitValue - Emit integer of appropriate size. |
| 673 | /// |
| 674 | void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
| 675 | switch (Form) { |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 676 | case DW_FORM_flag: // Fall thru |
| 677 | case DW_FORM_data1: DW.EmitByte(Integer); break; |
| 678 | case DW_FORM_data2: DW.EmitShort(Integer); break; |
| 679 | case DW_FORM_data4: DW.EmitLong(Integer); break; |
| 680 | case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break; |
| 681 | case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 682 | default: assert(0 && "DIE Value form not supported yet"); break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /// SizeOf - Determine size of integer value in bytes. |
| 687 | /// |
| 688 | unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 689 | switch (Form) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 690 | case DW_FORM_flag: // Fall thru |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 691 | case DW_FORM_data1: return sizeof(int8_t); |
| 692 | case DW_FORM_data2: return sizeof(int16_t); |
| 693 | case DW_FORM_data4: return sizeof(int32_t); |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 694 | case DW_FORM_udata: return DW.SizeULEB128(Integer); |
| 695 | case DW_FORM_sdata: return DW.SizeSLEB128(Integer); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 696 | default: assert(0 && "DIE Value form not supported yet"); break; |
| 697 | } |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | //===----------------------------------------------------------------------===// |
| 702 | |
| 703 | /// EmitValue - Emit string value. |
| 704 | /// |
| 705 | void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 706 | DW.EmitString(String); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /// SizeOf - Determine size of string value in bytes. |
| 710 | /// |
| 711 | unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 712 | return String.size() + sizeof(char); // sizeof('\0'); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | //===----------------------------------------------------------------------===// |
| 716 | |
| 717 | /// EmitValue - Emit label value. |
| 718 | /// |
| 719 | void DIELabel::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 720 | DW.EmitReference(Label); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /// SizeOf - Determine size of label value in bytes. |
| 724 | /// |
| 725 | unsigned DIELabel::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 726 | return DW.getAddressSize(); |
| 727 | } |
| 728 | |
| 729 | //===----------------------------------------------------------------------===// |
| 730 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 731 | /// EmitValue - Emit label value. |
| 732 | /// |
| 733 | void DIEAsIsLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
| 734 | DW.EmitReference(Label); |
| 735 | } |
| 736 | |
| 737 | /// SizeOf - Determine size of label value in bytes. |
| 738 | /// |
| 739 | unsigned DIEAsIsLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 740 | return DW.getAddressSize(); |
| 741 | } |
| 742 | |
| 743 | //===----------------------------------------------------------------------===// |
| 744 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 745 | /// EmitValue - Emit delta value. |
| 746 | /// |
| 747 | void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 748 | DW.EmitDifference(LabelHi, LabelLo); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /// SizeOf - Determine size of delta value in bytes. |
| 752 | /// |
| 753 | unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 754 | return DW.getAddressSize(); |
| 755 | } |
| 756 | |
| 757 | //===----------------------------------------------------------------------===// |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 758 | /// EmitValue - Emit extry offset. |
| 759 | /// |
| 760 | void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
| 761 | DW.EmitLong(Entry->getOffset()); |
| 762 | } |
| 763 | |
| 764 | /// SizeOf - Determine size of label value in bytes. |
| 765 | /// |
| 766 | unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 767 | return sizeof(int32_t); |
| 768 | } |
| 769 | |
| 770 | //===----------------------------------------------------------------------===// |
| 771 | |
| 772 | DIE::DIE(unsigned Tag, unsigned ChildrenFlag) |
| 773 | : Abbrev(new DIEAbbrev(Tag, ChildrenFlag)) |
| 774 | , AbbrevID(0) |
| 775 | , Offset(0) |
| 776 | , Size(0) |
| 777 | , Context(NULL) |
| 778 | , Children() |
| 779 | , Values() |
| 780 | {} |
| 781 | |
| 782 | DIE::~DIE() { |
| 783 | if (Abbrev) delete Abbrev; |
| 784 | |
| 785 | for (unsigned i = 0, N = Children.size(); i < N; i++) { |
| 786 | delete Children[i]; |
| 787 | } |
| 788 | |
| 789 | for (unsigned j = 0, M = Values.size(); j < M; j++) { |
| 790 | delete Values[j]; |
| 791 | } |
| 792 | |
| 793 | if (Context) delete Context; |
| 794 | } |
| 795 | |
| 796 | /// AddInt - Add a simple integer attribute data and value. |
| 797 | /// |
| 798 | void DIE::AddInt(unsigned Attribute, unsigned Form, |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 799 | int Integer, bool IsSigned) { |
| 800 | if (Form == 0) { |
| 801 | if (IsSigned) { |
| 802 | if ((char)Integer == Integer) Form = DW_FORM_data1; |
| 803 | else if ((short)Integer == Integer) Form = DW_FORM_data2; |
| 804 | else Form = DW_FORM_data4; |
| 805 | } else { |
| 806 | if ((unsigned char)Integer == Integer) Form = DW_FORM_data1; |
| 807 | else if ((unsigned short)Integer == Integer) Form = DW_FORM_data2; |
| 808 | else Form = DW_FORM_data4; |
| 809 | } |
| 810 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 811 | Abbrev->AddAttribute(Attribute, Form); |
| 812 | Values.push_back(new DIEInteger(Integer)); |
| 813 | } |
| 814 | |
| 815 | /// AddString - Add a std::string attribute data and value. |
| 816 | /// |
| 817 | void DIE::AddString(unsigned Attribute, unsigned Form, |
| 818 | const std::string &String) { |
| 819 | Abbrev->AddAttribute(Attribute, Form); |
| 820 | Values.push_back(new DIEString(String)); |
| 821 | } |
| 822 | |
| 823 | /// AddLabel - Add a Dwarf label attribute data and value. |
| 824 | /// |
| 825 | void DIE::AddLabel(unsigned Attribute, unsigned Form, |
| 826 | const DWLabel &Label) { |
| 827 | Abbrev->AddAttribute(Attribute, Form); |
| 828 | Values.push_back(new DIELabel(Label)); |
| 829 | } |
| 830 | |
| 831 | /// AddAsIsLabel - Add an non-Dwarf label attribute data and value. |
| 832 | /// |
| 833 | void DIE::AddAsIsLabel(unsigned Attribute, unsigned Form, |
| 834 | const std::string &Label) { |
| 835 | Abbrev->AddAttribute(Attribute, Form); |
| 836 | Values.push_back(new DIEAsIsLabel(Label)); |
| 837 | } |
| 838 | |
| 839 | /// AddDelta - Add a label delta attribute data and value. |
| 840 | /// |
| 841 | void DIE::AddDelta(unsigned Attribute, unsigned Form, |
| 842 | const DWLabel &Hi, const DWLabel &Lo) { |
| 843 | Abbrev->AddAttribute(Attribute, Form); |
| 844 | Values.push_back(new DIEDelta(Hi, Lo)); |
| 845 | } |
| 846 | |
| 847 | /// AddDIEntry - Add a DIE attribute data and value. |
| 848 | /// |
| 849 | void DIE::AddDIEntry(unsigned Attribute, |
| 850 | unsigned Form, DIE *Entry) { |
| 851 | Abbrev->AddAttribute(Attribute, Form); |
| 852 | Values.push_back(new DIEntry(Entry)); |
| 853 | } |
| 854 | |
| 855 | /// Complete - Indicate that all attributes have been added and ready to get an |
| 856 | /// abbreviation ID. |
| 857 | void DIE::Complete(DwarfWriter &DW) { |
| 858 | AbbrevID = DW.NewAbbreviation(Abbrev); |
| 859 | delete Abbrev; |
| 860 | Abbrev = NULL; |
| 861 | } |
| 862 | |
| 863 | /// AddChild - Add a child to the DIE. |
| 864 | /// |
| 865 | void DIE::AddChild(DIE *Child) { |
| 866 | Children.push_back(Child); |
| 867 | } |
| 868 | |
| 869 | //===----------------------------------------------------------------------===// |
| 870 | |
| 871 | /// NewBasicType - Creates a new basic type if necessary, then adds in the |
| 872 | /// context and owner. |
| 873 | DIE *DWContext::NewBasicType(const std::string &Name, unsigned Size, |
| 874 | unsigned Encoding) { |
| 875 | // FIXME - Just a prototype. |
| 876 | DIE *Type = Types[Name]; |
| 877 | |
| 878 | // If first occurance of type. |
| 879 | if (!Type) { |
| 880 | // construct the type DIE. |
| 881 | Type = new DIE(DW_TAG_base_type, DW_CHILDREN_no); |
| 882 | Type->AddString(DW_AT_name, DW_FORM_string, Name); |
| 883 | Type->AddInt (DW_AT_byte_size, DW_FORM_data1, Size); |
| 884 | Type->AddInt (DW_AT_encoding, DW_FORM_data1, Encoding); |
| 885 | Type->Complete(DW); |
| 886 | |
| 887 | // Add to context owner. |
| 888 | Owner->AddChild(Type); |
| 889 | |
| 890 | // Add to map. |
| 891 | Types[Name] = Type; |
| 892 | } |
| 893 | |
| 894 | return Type; |
| 895 | } |
| 896 | |
| 897 | /// NewVariable - Creates a basic variable, if necessary, then adds in the |
| 898 | /// context and owner. |
| 899 | DIE *DWContext::NewVariable(const std::string &Name, |
| 900 | unsigned SourceFileID, unsigned Line, |
| 901 | DIE *Type, bool IsExternal) { |
| 902 | // FIXME - Just a prototype. |
| 903 | DIE *Variable = Variables[Name]; |
| 904 | |
| 905 | // If first occurance of variable. |
| 906 | if (!Variable) { |
| 907 | assert(IsExternal && "Internal variables not handled yet"); |
| 908 | Variable = new DIE(DW_TAG_variable, DW_CHILDREN_no); |
| 909 | Variable->AddString (DW_AT_name, DW_FORM_string, Name); |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 910 | Variable->AddInt (DW_AT_decl_file, 0, SourceFileID); |
| 911 | Variable->AddInt (DW_AT_decl_line, 0, Line); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 912 | Variable->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type); |
| 913 | Variable->AddInt (DW_AT_external, DW_FORM_flag, (int)IsExternal); |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 914 | // FIXME - needs to be an expression. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 915 | Variable->AddAsIsLabel(DW_AT_location, DW_FORM_block1, |
| 916 | std::string("_")+Name+".b"); |
| 917 | Variable->Complete(DW); |
| 918 | |
| 919 | // Add to context owner. |
| 920 | Owner->AddChild(Variable); |
| 921 | |
| 922 | // Add to map. |
| 923 | Variables[Name] = Variable; |
| 924 | |
| 925 | // If external add to visible names. |
| 926 | if (IsExternal) { |
| 927 | DW.NewGlobalEntity(Name, Variable); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | return Variable; |
| 932 | } |
| 933 | |
| 934 | //===----------------------------------------------------------------------===// |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 935 | |
| 936 | /// PrintHex - Print a value as a hexidecimal value. |
| 937 | /// |
| 938 | void DwarfWriter::PrintHex(int Value) const { |
| 939 | O << "0x" << std::hex << Value << std::dec; |
| 940 | } |
| 941 | |
| 942 | /// EOL - Print a newline character to asm stream. If a comment is present |
| 943 | /// then it will be printed first. Comments should not contain '\n'. |
| 944 | void DwarfWriter::EOL(const std::string &Comment) const { |
| 945 | if (DwarfVerbose) { |
| 946 | O << "\t" |
| 947 | << Asm->CommentString |
| 948 | << " " |
| 949 | << Comment; |
| 950 | } |
| 951 | O << "\n"; |
| 952 | } |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 953 | |
| 954 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 955 | /// unsigned leb128 value. |
| 956 | void DwarfWriter::EmitULEB128Bytes(unsigned Value) const { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 957 | if (hasLEB128) { |
| 958 | O << "\t.uleb128\t" |
| 959 | << Value; |
| 960 | } else { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 961 | O << Asm->Data8bitsDirective; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 962 | PrintULEB128(Value); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 963 | } |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 967 | /// signed leb128 value. |
| 968 | void DwarfWriter::EmitSLEB128Bytes(int Value) const { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 969 | if (hasLEB128) { |
| 970 | O << "\t.sleb128\t" |
| 971 | << Value; |
| 972 | } else { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 973 | O << Asm->Data8bitsDirective; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 974 | PrintSLEB128(Value); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 975 | } |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 978 | /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 979 | /// representing an unsigned leb128 value. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 980 | void DwarfWriter::PrintULEB128(unsigned Value) const { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 981 | do { |
| 982 | unsigned Byte = Value & 0x7f; |
| 983 | Value >>= 7; |
| 984 | if (Value) Byte |= 0x80; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 985 | PrintHex(Byte); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 986 | if (Value) O << ", "; |
| 987 | } while (Value); |
| 988 | } |
| 989 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 990 | /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128 |
| 991 | /// value. |
| 992 | unsigned DwarfWriter::SizeULEB128(unsigned Value) { |
| 993 | unsigned Size = 0; |
| 994 | do { |
| 995 | Value >>= 7; |
| 996 | Size += sizeof(int8_t); |
| 997 | } while (Value); |
| 998 | return Size; |
| 999 | } |
| 1000 | |
| 1001 | /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1002 | /// representing a signed leb128 value. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1003 | void DwarfWriter::PrintSLEB128(int Value) const { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1004 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 1005 | bool IsMore; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1006 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1007 | do { |
| 1008 | unsigned Byte = Value & 0x7f; |
| 1009 | Value >>= 7; |
| 1010 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 1011 | if (IsMore) Byte |= 0x80; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1012 | PrintHex(Byte); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1013 | if (IsMore) O << ", "; |
| 1014 | } while (IsMore); |
| 1015 | } |
| 1016 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1017 | /// SizeSLEB128 - Compute the number of bytes required for a signed leb128 |
| 1018 | /// value. |
| 1019 | unsigned DwarfWriter::SizeSLEB128(int Value) { |
| 1020 | unsigned Size = 0; |
| 1021 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 1022 | bool IsMore; |
| 1023 | |
| 1024 | do { |
| 1025 | unsigned Byte = Value & 0x7f; |
| 1026 | Value >>= 7; |
| 1027 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 1028 | Size += sizeof(int8_t); |
| 1029 | } while (IsMore); |
| 1030 | return Size; |
| 1031 | } |
| 1032 | |
| 1033 | /// EmitByte - Emit a byte directive and value. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1034 | /// |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1035 | void DwarfWriter::EmitByte(int Value) const { |
| 1036 | O << Asm->Data8bitsDirective; |
| 1037 | PrintHex(Value); |
| 1038 | } |
| 1039 | |
| 1040 | /// EmitShort - Emit a short directive and value. |
| 1041 | /// |
| 1042 | void DwarfWriter::EmitShort(int Value) const { |
| 1043 | O << Asm->Data16bitsDirective; |
| 1044 | PrintHex(Value); |
| 1045 | } |
| 1046 | |
| 1047 | /// EmitLong - Emit a long directive and value. |
| 1048 | /// |
| 1049 | void DwarfWriter::EmitLong(int Value) const { |
| 1050 | O << Asm->Data32bitsDirective; |
| 1051 | PrintHex(Value); |
| 1052 | } |
| 1053 | |
| 1054 | /// EmitString - Emit a string with quotes and a null terminator. |
| 1055 | /// Special characters are emitted properly. (Eg. '\t') |
| 1056 | void DwarfWriter::EmitString(const std::string &String) const { |
| 1057 | O << Asm->AsciiDirective |
| 1058 | << "\""; |
| 1059 | for (unsigned i = 0, N = String.size(); i < N; i++) { |
| 1060 | unsigned char C = String[i]; |
| 1061 | |
| 1062 | if (!isascii(C) || iscntrl(C)) { |
| 1063 | switch(C) { |
| 1064 | case '\b': O << "\\b"; break; |
| 1065 | case '\f': O << "\\f"; break; |
| 1066 | case '\n': O << "\\n"; break; |
| 1067 | case '\r': O << "\\r"; break; |
| 1068 | case '\t': O << "\\t"; break; |
| 1069 | default: |
| 1070 | O << '\\'; |
| 1071 | O << char('0' + (C >> 6)); |
| 1072 | O << char('0' + (C >> 3)); |
| 1073 | O << char('0' + (C >> 0)); |
| 1074 | break; |
| 1075 | } |
| 1076 | } else if (C == '\"') { |
| 1077 | O << "\\\""; |
| 1078 | } else if (C == '\'') { |
| 1079 | O << "\\\'"; |
| 1080 | } else { |
| 1081 | O << C; |
| 1082 | } |
| 1083 | } |
| 1084 | O << "\\0\""; |
| 1085 | } |
| 1086 | |
| 1087 | /// PrintLabelName - Print label name in form used by Dwarf writer. |
| 1088 | /// |
| 1089 | void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1090 | O << Asm->PrivateGlobalPrefix |
| 1091 | << "debug_" |
| 1092 | << Tag |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1093 | << Number; |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1096 | /// EmitLabel - Emit location label for internal use by Dwarf. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1097 | /// |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1098 | void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const { |
| 1099 | PrintLabelName(Tag, Number); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1100 | O << ":\n"; |
| 1101 | } |
| 1102 | |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1103 | /// EmitReference - Emit a reference to a label. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1104 | /// |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1105 | void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1106 | if (AddressSize == 4) |
| 1107 | O << Asm->Data32bitsDirective; |
| 1108 | else |
| 1109 | O << Asm->Data64bitsDirective; |
| 1110 | |
| 1111 | PrintLabelName(Tag, Number); |
| 1112 | } |
Jim Laskey | 7368321 | 2006-01-21 00:59:54 +0000 | [diff] [blame^] | 1113 | void DwarfWriter::EmitReference(const std::string &Name) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1114 | if (AddressSize == 4) |
| 1115 | O << Asm->Data32bitsDirective; |
| 1116 | else |
| 1117 | O << Asm->Data64bitsDirective; |
| 1118 | |
| 1119 | O << Name; |
| 1120 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1121 | |
| 1122 | /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some |
| 1123 | /// assemblers do not accept absolute expressions with data directives, so there |
| 1124 | /// is an option (needsSet) to use an intermediary 'set' expression. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1125 | void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi, |
| 1126 | const char *TagLo, unsigned NumberLo) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1127 | if (needsSet) { |
| 1128 | static unsigned SetCounter = 0; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1129 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1130 | O << "\t.set\t"; |
| 1131 | PrintLabelName("set", SetCounter); |
| 1132 | O << ","; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1133 | PrintLabelName(TagHi, NumberHi); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1134 | O << "-"; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1135 | PrintLabelName(TagLo, NumberLo); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1136 | O << "\n"; |
| 1137 | |
| 1138 | if (AddressSize == sizeof(int32_t)) |
| 1139 | O << Asm->Data32bitsDirective; |
| 1140 | else |
| 1141 | O << Asm->Data64bitsDirective; |
| 1142 | |
| 1143 | PrintLabelName("set", SetCounter); |
| 1144 | |
| 1145 | SetCounter++; |
| 1146 | } else { |
| 1147 | if (AddressSize == sizeof(int32_t)) |
| 1148 | O << Asm->Data32bitsDirective; |
| 1149 | else |
| 1150 | O << Asm->Data64bitsDirective; |
| 1151 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1152 | PrintLabelName(TagHi, NumberHi); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1153 | O << "-"; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1154 | PrintLabelName(TagLo, NumberLo); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1158 | /// NewAbbreviation - Add the abbreviation to the Abbreviation vector. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1159 | /// |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1160 | unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) { |
| 1161 | return Abbreviations.insert(*Abbrev); |
| 1162 | } |
| 1163 | |
| 1164 | /// NewString - Add a string to the constant pool and returns a label. |
| 1165 | /// |
| 1166 | DWLabel DwarfWriter::NewString(const std::string &String) { |
| 1167 | unsigned StringID = StringPool.insert(String); |
| 1168 | return DWLabel("string", StringID); |
| 1169 | } |
| 1170 | |
| 1171 | /// NewGlobalType - Make the type visible globally using the given name. |
| 1172 | /// |
| 1173 | void DwarfWriter::NewGlobalType(const std::string &Name, DIE *Type) { |
| 1174 | // FIXME - check for duplication. |
| 1175 | GlobalTypes[Name] = Type; |
| 1176 | } |
| 1177 | |
| 1178 | /// NewGlobalEntity - Make the entity visible globally using the given name. |
| 1179 | /// |
| 1180 | void DwarfWriter::NewGlobalEntity(const std::string &Name, DIE *Entity) { |
| 1181 | // FIXME - check for duplication. |
| 1182 | GlobalEntities[Name] = Entity; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /// NewCompileUnit - Create new compile unit information. |
| 1186 | /// |
| 1187 | DIE *DwarfWriter::NewCompileUnit(const std::string &Directory, |
| 1188 | const std::string &SourceName) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1189 | DIE *Unit = new DIE(DW_TAG_compile_unit, DW_CHILDREN_yes); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1190 | // FIXME - use the correct line set. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1191 | Unit->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0)); |
| 1192 | Unit->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0)); |
| 1193 | Unit->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0)); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1194 | // FIXME - The producer needs to be in this form, but should come from |
| 1195 | // an appropriate source. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1196 | Unit->AddString(DW_AT_producer, DW_FORM_string, |
| 1197 | "llvm 3.4.x (LLVM Research Group)"); |
| 1198 | Unit->AddInt (DW_AT_language, DW_FORM_data1, DW_LANG_C89); |
| 1199 | Unit->AddString(DW_AT_name, DW_FORM_string, SourceName); |
| 1200 | Unit->AddString(DW_AT_comp_dir, DW_FORM_string, Directory); |
| 1201 | Unit->Complete(*this); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1202 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1203 | return Unit; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc |
| 1207 | /// tools to recognize the object file contains Dwarf information. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1208 | /// |
| 1209 | void DwarfWriter::EmitInitial() const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1210 | // Dwarf sections base addresses. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1211 | Asm->SwitchSection(DwarfAbbrevSection, 0); |
| 1212 | EmitLabel("abbrev", 0); |
| 1213 | Asm->SwitchSection(DwarfInfoSection, 0); |
| 1214 | EmitLabel("info", 0); |
| 1215 | Asm->SwitchSection(DwarfLineSection, 0); |
| 1216 | EmitLabel("line", 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Standard sections base addresses. |
| 1219 | Asm->SwitchSection(TextSection, 0); |
| 1220 | EmitLabel("text_begin", 0); |
| 1221 | Asm->SwitchSection(DataSection, 0); |
| 1222 | EmitLabel("data_begin", 0); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1225 | /// EmitDIE - Recusively Emits a debug information entry. |
| 1226 | /// |
| 1227 | void DwarfWriter::EmitDIE(DIE *Die) const { |
| 1228 | // Get the abbreviation for this DIE. |
| 1229 | unsigned AbbrevID = Die->getAbbrevID(); |
| 1230 | const DIEAbbrev &Abbrev = Abbreviations[AbbrevID]; |
| 1231 | |
| 1232 | // Emit the code (index) for the abbreviation. |
| 1233 | EmitULEB128Bytes(AbbrevID); |
| 1234 | EOL(std::string("Abbrev [" + |
| 1235 | utostr(AbbrevID) + |
| 1236 | TagString(Abbrev.getTag())) + |
| 1237 | " "); |
| 1238 | |
| 1239 | const std::vector<DIEValue *> &Values = Die->getValues(); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1240 | const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Emit the DIE attribute values. |
| 1243 | for (unsigned i = 0, N = Values.size(); i < N; i++) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1244 | unsigned Attr = AbbrevData[i].getAttribute(); |
| 1245 | unsigned Form = AbbrevData[i].getForm(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1246 | assert(Form && "Too many attributes for DIE (check abbreviation)"); |
| 1247 | |
| 1248 | switch (Attr) { |
| 1249 | case DW_AT_sibling: { |
| 1250 | EmitLong(Die->SiblingOffset()); |
| 1251 | break; |
| 1252 | } |
| 1253 | default: { |
| 1254 | // Emit an attribute using the defined form. |
| 1255 | Values[i]->EmitValue(*this, Form); |
| 1256 | break; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | EOL(AttributeString(Attr)); |
| 1261 | } |
| 1262 | |
| 1263 | // Emit the DIE children if any. |
| 1264 | if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) { |
| 1265 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1266 | |
| 1267 | for (unsigned j = 0, M = Children.size(); j < M; j++) { |
| 1268 | // FIXME - handle sibling offsets. |
| 1269 | // FIXME - handle all DIE types. |
| 1270 | EmitDIE(Children[j]); |
| 1271 | } |
| 1272 | |
| 1273 | EmitByte(0); EOL("End Of Children Mark"); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /// SizeAndOffsetDie - Compute the size and offset of a DIE. |
| 1278 | /// |
| 1279 | unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) const { |
| 1280 | // Get the abbreviation for this DIE. |
| 1281 | unsigned AbbrevID = Die->getAbbrevID(); |
| 1282 | const DIEAbbrev &Abbrev = Abbreviations[AbbrevID]; |
| 1283 | |
| 1284 | // Set DIE offset |
| 1285 | Die->setOffset(Offset); |
| 1286 | |
| 1287 | // Start the size with the size of abbreviation code. |
| 1288 | Offset += SizeULEB128(AbbrevID); |
| 1289 | |
| 1290 | const std::vector<DIEValue *> &Values = Die->getValues(); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1291 | const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Emit the DIE attribute values. |
| 1294 | for (unsigned i = 0, N = Values.size(); i < N; i++) { |
| 1295 | // Size attribute value. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1296 | Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm()); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // Emit the DIE children if any. |
| 1300 | if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) { |
| 1301 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1302 | |
| 1303 | for (unsigned j = 0, M = Children.size(); j < M; j++) { |
| 1304 | // FIXME - handle sibling offsets. |
| 1305 | // FIXME - handle all DIE types. |
| 1306 | Offset = SizeAndOffsetDie(Children[j], Offset); |
| 1307 | } |
| 1308 | |
| 1309 | // End of children marker. |
| 1310 | Offset += sizeof(int8_t); |
| 1311 | } |
| 1312 | |
| 1313 | Die->setSize(Offset - Die->getOffset()); |
| 1314 | return Offset; |
| 1315 | } |
| 1316 | |
| 1317 | /// SizeAndOffsets - Compute the size and offset of all the DIEs. |
| 1318 | /// |
| 1319 | void DwarfWriter::SizeAndOffsets() { |
| 1320 | // Compute size of debug unit header |
| 1321 | unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info |
| 1322 | sizeof(int16_t) + // DWARF version number |
| 1323 | sizeof(int32_t) + // Offset Into Abbrev. Section |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1324 | sizeof(int8_t); // Pointer Size (in bytes) |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Process each compile unit. |
| 1327 | for (unsigned i = 0, N = CompileUnits.size(); i < N; i++) { |
| 1328 | Offset = SizeAndOffsetDie(CompileUnits[i], Offset); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | /// EmitDebugInfo - Emit the debug info section. |
| 1333 | /// |
| 1334 | void DwarfWriter::EmitDebugInfo() const { |
| 1335 | // Start debug info section. |
| 1336 | Asm->SwitchSection(DwarfInfoSection, 0); |
| 1337 | |
| 1338 | // Get the number of compile units. |
| 1339 | unsigned N = CompileUnits.size(); |
| 1340 | |
| 1341 | // If there are any compile units. |
| 1342 | if (N) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1343 | EmitLabel("info_begin", 0); |
| 1344 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1345 | // Emit the compile units header. |
| 1346 | |
| 1347 | // Emit size of content not including length itself |
| 1348 | unsigned ContentSize = CompileUnits[N - 1]->SiblingOffset(); |
| 1349 | EmitLong(ContentSize - sizeof(int32_t)); |
| 1350 | EOL("Length of Compilation Unit Info"); |
| 1351 | |
| 1352 | EmitShort(DWARF_VERSION); EOL("DWARF version number"); |
| 1353 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1354 | EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1355 | |
| 1356 | EmitByte(AddressSize); EOL("Address Size (in bytes)"); |
| 1357 | |
| 1358 | // Process each compile unit. |
| 1359 | for (unsigned i = 0; i < N; i++) { |
| 1360 | EmitDIE(CompileUnits[i]); |
| 1361 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1362 | |
| 1363 | EmitLabel("info_end", 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /// EmitAbbreviations - Emit the abbreviation section. |
| 1368 | /// |
| 1369 | void DwarfWriter::EmitAbbreviations() const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1370 | // Check to see if it is worth the effort. |
| 1371 | if (!Abbreviations.empty()) { |
| 1372 | // Start the debug abbrev section. |
| 1373 | Asm->SwitchSection(DwarfAbbrevSection, 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1374 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1375 | EmitLabel("abbrev_begin", 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1376 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1377 | // For each abbrevation. |
| 1378 | for (unsigned AbbrevID = 1, NAID = Abbreviations.size(); |
| 1379 | AbbrevID <= NAID; AbbrevID++) { |
| 1380 | // Get abbreviation data |
| 1381 | const DIEAbbrev &Abbrev = Abbreviations[AbbrevID]; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1382 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1383 | // Emit the abbrevations code (base 1 index.) |
| 1384 | EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1385 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1386 | // Emit the abbreviations data. |
| 1387 | Abbrev.Emit(*this); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1388 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1389 | |
| 1390 | EmitLabel("abbrev_end", 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | /// EmitDebugLines - Emit source line information. |
| 1395 | /// |
| 1396 | void DwarfWriter::EmitDebugLines() const { |
| 1397 | // Minimum line delta, thus ranging from -10..(255-10). |
| 1398 | const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1); |
| 1399 | // Maximum line delta, thus ranging from -10..(255-10). |
| 1400 | const int MaxLineDelta = 255 + MinLineDelta; |
| 1401 | |
| 1402 | // Start the dwarf line section. |
| 1403 | Asm->SwitchSection(DwarfLineSection, 0); |
| 1404 | |
| 1405 | // Construct the section header. |
| 1406 | |
| 1407 | EmitDifference("line_end", 0, "line_begin", 0); |
| 1408 | EOL("Length of Source Line Info"); |
| 1409 | EmitLabel("line_begin", 0); |
| 1410 | |
| 1411 | EmitShort(DWARF_VERSION); EOL("DWARF version number"); |
| 1412 | |
| 1413 | EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0); |
| 1414 | EOL("Prolog Length"); |
| 1415 | EmitLabel("line_prolog_begin", 0); |
| 1416 | |
| 1417 | EmitByte(1); EOL("Minimum Instruction Length"); |
| 1418 | |
| 1419 | EmitByte(1); EOL("Default is_stmt_start flag"); |
| 1420 | |
| 1421 | EmitByte(MinLineDelta); EOL("Line Base Value (Special Opcodes)"); |
| 1422 | |
| 1423 | EmitByte(MaxLineDelta); EOL("Line Range Value (Special Opcodes)"); |
| 1424 | |
| 1425 | EmitByte(-MinLineDelta); EOL("Special Opcode Base"); |
| 1426 | |
| 1427 | // Line number standard opcode encodings argument count |
| 1428 | EmitByte(0); EOL("DW_LNS_copy arg count"); |
| 1429 | EmitByte(1); EOL("DW_LNS_advance_pc arg count"); |
| 1430 | EmitByte(1); EOL("DW_LNS_advance_line arg count"); |
| 1431 | EmitByte(1); EOL("DW_LNS_set_file arg count"); |
| 1432 | EmitByte(1); EOL("DW_LNS_set_column arg count"); |
| 1433 | EmitByte(0); EOL("DW_LNS_negate_stmt arg count"); |
| 1434 | EmitByte(0); EOL("DW_LNS_set_basic_block arg count"); |
| 1435 | EmitByte(0); EOL("DW_LNS_const_add_pc arg count"); |
| 1436 | EmitByte(1); EOL("DW_LNS_fixed_advance_pc arg count"); |
| 1437 | |
| 1438 | const UniqueVector<std::string> &Directories = DebugInfo->getDirectories(); |
| 1439 | const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles(); |
| 1440 | |
| 1441 | // Emit directories. |
| 1442 | for (unsigned DirectoryID = 1, NDID = Directories.size(); |
| 1443 | DirectoryID <= NDID; DirectoryID++) { |
| 1444 | EmitString(Directories[DirectoryID]); EOL("Directory"); |
| 1445 | } |
| 1446 | EmitByte(0); EOL("End of directories"); |
| 1447 | |
| 1448 | // Emit files. |
| 1449 | for (unsigned SourceID = 1, NSID = SourceFiles.size(); |
| 1450 | SourceID <= NSID; SourceID++) { |
| 1451 | const SourceFileInfo &SourceFile = SourceFiles[SourceID]; |
| 1452 | EmitString(SourceFile.getName()); EOL("Source"); |
| 1453 | EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #"); |
| 1454 | EmitULEB128Bytes(0); EOL("Mod date"); |
| 1455 | EmitULEB128Bytes(0); EOL("File size"); |
| 1456 | } |
| 1457 | EmitByte(0); EOL("End of files"); |
| 1458 | |
| 1459 | EmitLabel("line_prolog_end", 0); |
| 1460 | |
| 1461 | // Emit line information |
| 1462 | const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines(); |
| 1463 | |
| 1464 | // Dwarf assumes we start with first line of first source file. |
| 1465 | unsigned Source = 1; |
| 1466 | unsigned Line = 1; |
| 1467 | |
| 1468 | // Construct rows of the address, source, line, column matrix. |
| 1469 | for (unsigned i = 0, N = LineInfos.size(); i < N; i++) { |
| 1470 | SourceLineInfo *LineInfo = LineInfos[i]; |
| 1471 | |
| 1472 | // Define the line address. |
| 1473 | EmitByte(0); EOL("Extended Op"); |
| 1474 | EmitByte(4 + 1); EOL("Op size"); |
| 1475 | EmitByte(DW_LNE_set_address); EOL("DW_LNE_set_address"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1476 | EmitReference("loc", i + 1); EOL("Location label"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1477 | |
| 1478 | // If change of source, then switch to the new source. |
| 1479 | if (Source != LineInfo->getSourceID()) { |
| 1480 | Source = LineInfo->getSourceID(); |
| 1481 | EmitByte(DW_LNS_set_file); EOL("DW_LNS_set_file"); |
| 1482 | EmitULEB128Bytes(0); EOL("New Source"); |
| 1483 | } |
| 1484 | |
| 1485 | // If change of line. |
| 1486 | if (Line != LineInfo->getLine()) { |
| 1487 | // Determine offset. |
| 1488 | int Offset = LineInfo->getLine() - Line; |
| 1489 | int Delta = Offset - MinLineDelta; |
| 1490 | |
| 1491 | // Update line. |
| 1492 | Line = LineInfo->getLine(); |
| 1493 | |
| 1494 | // If delta is small enough and in range... |
| 1495 | if (Delta >= 0 && Delta < (MaxLineDelta - 1)) { |
| 1496 | // ... then use fast opcode. |
| 1497 | EmitByte(Delta - MinLineDelta); EOL("Line Delta"); |
| 1498 | } else { |
| 1499 | // ... otherwise use long hand. |
| 1500 | EmitByte(DW_LNS_advance_line); EOL("DW_LNS_advance_line"); |
| 1501 | EmitSLEB128Bytes(Offset); EOL("Line Offset"); |
| 1502 | EmitByte(DW_LNS_copy); EOL("DW_LNS_copy"); |
| 1503 | } |
| 1504 | } else { |
| 1505 | // Copy the previous row (different address or source) |
| 1506 | EmitByte(DW_LNS_copy); EOL("DW_LNS_copy"); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | // Mark end of matrix. |
| 1511 | EmitByte(0); EOL("DW_LNE_end_sequence"); |
| 1512 | EmitULEB128Bytes(1); O << "\n"; |
| 1513 | EmitByte(1); O << "\n"; |
| 1514 | |
| 1515 | EmitLabel("line_end", 0); |
| 1516 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1517 | |
| 1518 | /// EmitDebugFrame - Emit visible names into a debug frame section. |
| 1519 | /// |
| 1520 | void DwarfWriter::EmitDebugFrame() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1521 | // FIXME - Should be per frame |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /// EmitDebugPubNames - Emit visible names into a debug pubnames section. |
| 1525 | /// |
| 1526 | void DwarfWriter::EmitDebugPubNames() { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1527 | // Check to see if it is worth the effort. |
| 1528 | if (!GlobalEntities.empty()) { |
| 1529 | // Start the dwarf pubnames section. |
| 1530 | Asm->SwitchSection(DwarfPubNamesSection, 0); |
| 1531 | |
| 1532 | EmitDifference("pubnames_end", 0, "pubnames_begin", 0); |
| 1533 | EOL("Length of Public Names Info"); |
| 1534 | |
| 1535 | EmitLabel("pubnames_begin", 0); |
| 1536 | |
| 1537 | EmitShort(DWARF_VERSION); EOL("DWARF Version"); |
| 1538 | |
| 1539 | EmitReference("info_begin", 0); EOL("Offset of Compilation Unit Info"); |
| 1540 | |
| 1541 | EmitDifference("info_end", 0, "info_begin", 0); |
| 1542 | EOL("Compilation Unit Length"); |
| 1543 | |
| 1544 | for (std::map<std::string, DIE *>::iterator G = GlobalTypes.begin(), |
| 1545 | GE = GlobalTypes.begin(); |
| 1546 | G != GE; G++) { |
| 1547 | const std::string &Name = (*G).first; |
| 1548 | DIE * Entity = (*G).second; |
| 1549 | |
| 1550 | EmitLong(Entity->getOffset()); EOL("DIE offset"); |
| 1551 | EmitString(Name); EOL("External Name"); |
| 1552 | |
| 1553 | } |
| 1554 | |
| 1555 | EmitLong(0); EOL("End Mark"); |
| 1556 | EmitLabel("pubnames_end", 0); |
| 1557 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /// EmitDebugPubTypes - Emit visible names into a debug pubtypes section. |
| 1561 | /// |
| 1562 | void DwarfWriter::EmitDebugPubTypes() { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1563 | // Check to see if it is worth the effort. |
| 1564 | if (!GlobalTypes.empty()) { |
| 1565 | // Start the dwarf pubtypes section. |
| 1566 | Asm->SwitchSection(DwarfPubTypesSection, 0); |
| 1567 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /// EmitDebugStr - Emit visible names into a debug str section. |
| 1571 | /// |
| 1572 | void DwarfWriter::EmitDebugStr() { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1573 | // Check to see if it is worth the effort. |
| 1574 | if (!StringPool.empty()) { |
| 1575 | // Start the dwarf str section. |
| 1576 | Asm->SwitchSection(DwarfStrSection, 0); |
| 1577 | |
| 1578 | // For each of strings in teh string pool. |
| 1579 | for (unsigned StringID = 1, N = StringPool.size(); |
| 1580 | StringID <= N; StringID++) { |
| 1581 | // Emit a label for reference from debug information entries. |
| 1582 | EmitLabel("string", StringID); |
| 1583 | // Emit the string itself. |
| 1584 | const std::string &String = StringPool[StringID]; |
| 1585 | EmitString(String); O << "\n"; |
| 1586 | } |
| 1587 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /// EmitDebugLoc - Emit visible names into a debug loc section. |
| 1591 | /// |
| 1592 | void DwarfWriter::EmitDebugLoc() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1593 | // Start the dwarf loc section. |
| 1594 | Asm->SwitchSection(DwarfLocSection, 0); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /// EmitDebugARanges - Emit visible names into a debug aranges section. |
| 1598 | /// |
| 1599 | void DwarfWriter::EmitDebugARanges() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1600 | // Start the dwarf aranges section. |
| 1601 | Asm->SwitchSection(DwarfARangesSection, 0); |
| 1602 | |
| 1603 | // FIXME - Mock up |
| 1604 | |
| 1605 | // Don't include size of length |
| 1606 | EmitLong(0x1c); EOL("Length of Address Ranges Info"); |
| 1607 | |
| 1608 | EmitShort(DWARF_VERSION); EOL("Dwarf Version"); |
| 1609 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1610 | EmitReference("info_begin", 0); EOL("Offset of Compilation Unit Info"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1611 | |
| 1612 | EmitByte(AddressSize); EOL("Size of Address"); |
| 1613 | |
| 1614 | EmitByte(0); EOL("Size of Segment Descriptor"); |
| 1615 | |
| 1616 | EmitShort(0); EOL("Pad (1)"); |
| 1617 | EmitShort(0); EOL("Pad (2)"); |
| 1618 | |
| 1619 | // Range 1 |
| 1620 | EmitReference("text_begin", 0); EOL("Address"); |
| 1621 | EmitDifference("text_end", 0, "text_begin", 0); EOL("Length"); |
| 1622 | |
| 1623 | EmitLong(0); EOL("EOM (1)"); |
| 1624 | EmitLong(0); EOL("EOM (2)"); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /// EmitDebugRanges - Emit visible names into a debug ranges section. |
| 1628 | /// |
| 1629 | void DwarfWriter::EmitDebugRanges() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1630 | // Start the dwarf ranges section. |
| 1631 | Asm->SwitchSection(DwarfRangesSection, 0); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /// EmitDebugMacInfo - Emit visible names into a debug macinfo section. |
| 1635 | /// |
| 1636 | void DwarfWriter::EmitDebugMacInfo() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1637 | // Start the dwarf macinfo section. |
| 1638 | Asm->SwitchSection(DwarfMacInfoSection, 0); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1639 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1640 | |
| 1641 | /// ShouldEmitDwarf - Determine if Dwarf declarations should be made. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1642 | /// |
| 1643 | bool DwarfWriter::ShouldEmitDwarf() { |
| 1644 | // Check if debug info is present. |
| 1645 | if (!DebugInfo || !DebugInfo->hasInfo()) return false; |
| 1646 | |
| 1647 | // Make sure initial declarations are made. |
| 1648 | if (!didInitial) { |
| 1649 | EmitInitial(); |
| 1650 | didInitial = true; |
| 1651 | } |
| 1652 | |
| 1653 | // Okay to emit. |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1657 | //===----------------------------------------------------------------------===// |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1658 | // Main entry points. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1659 | // |
| 1660 | |
| 1661 | DwarfWriter::DwarfWriter(std::ostream &o, AsmPrinter *ap) |
| 1662 | : O(o) |
| 1663 | , Asm(ap) |
| 1664 | , DebugInfo(NULL) |
| 1665 | , didInitial(false) |
| 1666 | , CompileUnits() |
| 1667 | , Abbreviations() |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1668 | , GlobalTypes() |
| 1669 | , GlobalEntities() |
| 1670 | , StringPool() |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1671 | , AddressSize(sizeof(int32_t)) |
| 1672 | , hasLEB128(false) |
| 1673 | , hasDotLoc(false) |
| 1674 | , hasDotFile(false) |
| 1675 | , needsSet(false) |
| 1676 | , DwarfAbbrevSection(".debug_abbrev") |
| 1677 | , DwarfInfoSection(".debug_info") |
| 1678 | , DwarfLineSection(".debug_line") |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1679 | , DwarfFrameSection(".debug_frame") |
| 1680 | , DwarfPubNamesSection(".debug_pubnames") |
| 1681 | , DwarfPubTypesSection(".debug_pubtypes") |
| 1682 | , DwarfStrSection(".debug_str") |
| 1683 | , DwarfLocSection(".debug_loc") |
| 1684 | , DwarfARangesSection(".debug_aranges") |
| 1685 | , DwarfRangesSection(".debug_ranges") |
| 1686 | , DwarfMacInfoSection(".debug_macinfo") |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1687 | , TextSection(".text") |
| 1688 | , DataSection(".data") |
| 1689 | {} |
| 1690 | DwarfWriter::~DwarfWriter() { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1691 | for (unsigned i = 0, N = CompileUnits.size(); i < N; i++) { |
| 1692 | delete CompileUnits[i]; |
| 1693 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | /// BeginModule - Emit all Dwarf sections that should come prior to the content. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1697 | /// |
| 1698 | void DwarfWriter::BeginModule() { |
| 1699 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1700 | EOL("Dwarf Begin Module"); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1703 | /// EndModule - Emit all Dwarf sections that should come after the content. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1704 | /// |
| 1705 | void DwarfWriter::EndModule() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1706 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1707 | EOL("Dwarf End Module"); |
| 1708 | |
| 1709 | // Standard sections final addresses. |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1710 | Asm->SwitchSection(TextSection, 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1711 | EmitLabel("text_end", 0); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1712 | Asm->SwitchSection(DataSection, 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1713 | EmitLabel("data_end", 0); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1714 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1715 | // Get directory and source information. |
| 1716 | const UniqueVector<std::string> &Directories = DebugInfo->getDirectories(); |
| 1717 | const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles(); |
| 1718 | |
| 1719 | // Construct compile unit DIEs for each source. |
| 1720 | for (unsigned SourceID = 1, NSID = SourceFiles.size(); |
| 1721 | SourceID <= NSID; SourceID++) { |
| 1722 | const SourceFileInfo &SourceFile = SourceFiles[SourceID]; |
| 1723 | const std::string &Directory = Directories[SourceFile.getDirectoryID()]; |
| 1724 | const std::string &SourceName = SourceFile.getName(); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1725 | DIE *Unit = NewCompileUnit(Directory, SourceName); |
| 1726 | |
| 1727 | #if 0 |
| 1728 | // FIXME - just testing. |
| 1729 | DWContext *Context = new DWContext(*this, Unit); |
| 1730 | DIE *TypeInt = Context->NewBasicType("int", sizeof(int32_t), DW_ATE_signed); |
| 1731 | Context->NewVariable("MyGlobal", SourceID, 1, TypeInt, true); |
| 1732 | #endif |
| 1733 | |
| 1734 | CompileUnits.push_back(Unit); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1735 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Compute DIE offsets and sizes. |
| 1738 | SizeAndOffsets(); |
| 1739 | |
| 1740 | // Emit all the DIEs into a debug info section |
| 1741 | EmitDebugInfo(); |
| 1742 | |
| 1743 | // Corresponding abbreviations into a abbrev section. |
| 1744 | EmitAbbreviations(); |
| 1745 | |
| 1746 | // Emit source line correspondence into a debug line section. |
| 1747 | EmitDebugLines(); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Emit info into a debug frame section. |
| 1750 | EmitDebugFrame(); |
| 1751 | |
| 1752 | // Emit info into a debug pubnames section. |
| 1753 | EmitDebugPubNames(); |
| 1754 | |
| 1755 | // Emit info into a debug pubtypes section. |
| 1756 | EmitDebugPubTypes(); |
| 1757 | |
| 1758 | // Emit info into a debug str section. |
| 1759 | EmitDebugStr(); |
| 1760 | |
| 1761 | // Emit info into a debug loc section. |
| 1762 | EmitDebugLoc(); |
| 1763 | |
| 1764 | // Emit info into a debug aranges section. |
| 1765 | EmitDebugARanges(); |
| 1766 | |
| 1767 | // Emit info into a debug ranges section. |
| 1768 | EmitDebugRanges(); |
| 1769 | |
| 1770 | // Emit info into a debug macinfo section. |
| 1771 | EmitDebugMacInfo(); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1774 | /// BeginFunction - Gather pre-function debug information. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1775 | /// |
| 1776 | void DwarfWriter::BeginFunction() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1777 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1778 | EOL("Dwarf Begin Function"); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1781 | /// EndFunction - Gather and emit post-function debug information. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1782 | /// |
| 1783 | void DwarfWriter::EndFunction() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1784 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1785 | EOL("Dwarf End Function"); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1786 | } |