Crude Dwarf global variable debugging.
[oota-llvm.git] / include / llvm / CodeGen / DwarfWriter.h
1 //===-- llvm/CodeGen/DwarfWriter.h - 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.  For
11 // Details on the Dwarf 3 specfication see DWARF Debugging Information Format
12 // V.3 reference manual http://dwarf.freestandards.org ,
13 //
14 // The role of the Dwarf Writer class is to extract debug information from the
15 // MachineDebugInfo object, organize it in Dwarf form and then emit it into asm
16 // the current asm file using data and high level Dwarf directives.
17 // 
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CODEGEN_DWARFWRITER_H
21 #define LLVM_CODEGEN_DWARFWRITER_H
22
23 #include "llvm/ADT/UniqueVector.h"
24
25 #include <iosfwd>
26 #include <string>
27
28
29 namespace llvm {
30
31   //===--------------------------------------------------------------------===//
32   // Dwarf constants as gleaned from the DWARF Debugging Information Format V.3
33   // reference manual http://dwarf.freestandards.org .
34   //
35   enum dwarf_constants {
36     DWARF_VERSION = 2,
37     
38    // Tags
39     DW_TAG_array_type = 0x01,
40     DW_TAG_class_type = 0x02,
41     DW_TAG_entry_point = 0x03,
42     DW_TAG_enumeration_type = 0x04,
43     DW_TAG_formal_parameter = 0x05,
44     DW_TAG_imported_declaration = 0x08,
45     DW_TAG_label = 0x0a,
46     DW_TAG_lexical_block = 0x0b,
47     DW_TAG_member = 0x0d,
48     DW_TAG_pointer_type = 0x0f,
49     DW_TAG_reference_type = 0x10,
50     DW_TAG_compile_unit = 0x11,
51     DW_TAG_string_type = 0x12,
52     DW_TAG_structure_type = 0x13,
53     DW_TAG_subroutine_type = 0x15,
54     DW_TAG_typedef = 0x16,
55     DW_TAG_union_type = 0x17,
56     DW_TAG_unspecified_parameters = 0x18,
57     DW_TAG_variant = 0x19,
58     DW_TAG_common_block = 0x1a,
59     DW_TAG_common_inclusion = 0x1b,
60     DW_TAG_inheritance = 0x1c,
61     DW_TAG_inlined_subroutine = 0x1d,
62     DW_TAG_module = 0x1e,
63     DW_TAG_ptr_to_member_type = 0x1f,
64     DW_TAG_set_type = 0x20,
65     DW_TAG_subrange_type = 0x21,
66     DW_TAG_with_stmt = 0x22,
67     DW_TAG_access_declaration = 0x23,
68     DW_TAG_base_type = 0x24,
69     DW_TAG_catch_block = 0x25,
70     DW_TAG_const_type = 0x26,
71     DW_TAG_constant = 0x27,
72     DW_TAG_enumerator = 0x28,
73     DW_TAG_file_type = 0x29,
74     DW_TAG_friend = 0x2a,
75     DW_TAG_namelist = 0x2b,
76     DW_TAG_namelist_item = 0x2c,
77     DW_TAG_packed_type = 0x2d,
78     DW_TAG_subprogram = 0x2e,
79     DW_TAG_template_type_parameter = 0x2f,
80     DW_TAG_template_value_parameter = 0x30,
81     DW_TAG_thrown_type = 0x31,
82     DW_TAG_try_block = 0x32,
83     DW_TAG_variant_part = 0x33,
84     DW_TAG_variable = 0x34,
85     DW_TAG_volatile_type = 0x35,
86     DW_TAG_dwarf_procedure = 0x36,
87     DW_TAG_restrict_type = 0x37,
88     DW_TAG_interface_type = 0x38,
89     DW_TAG_namespace = 0x39,
90     DW_TAG_imported_module = 0x3a,
91     DW_TAG_unspecified_type = 0x3b,
92     DW_TAG_partial_unit = 0x3c,
93     DW_TAG_imported_unit = 0x3d,
94     DW_TAG_condition = 0x3f,
95     DW_TAG_shared_type = 0x40,
96     DW_TAG_lo_user = 0x4080,
97     DW_TAG_hi_user = 0xffff,
98
99     // Children flag
100     DW_CHILDREN_no = 0x00,
101     DW_CHILDREN_yes = 0x01,
102
103     // Attributes
104     DW_AT_sibling = 0x01,
105     DW_AT_location = 0x02,
106     DW_AT_name = 0x03,
107     DW_AT_ordering = 0x09,
108     DW_AT_byte_size = 0x0b,
109     DW_AT_bit_offset = 0x0c,
110     DW_AT_bit_size = 0x0d,
111     DW_AT_stmt_list = 0x10,
112     DW_AT_low_pc = 0x11,
113     DW_AT_high_pc = 0x12,
114     DW_AT_language = 0x13,
115     DW_AT_discr = 0x15,
116     DW_AT_discr_value = 0x16,
117     DW_AT_visibility = 0x17,
118     DW_AT_import = 0x18,
119     DW_AT_string_length = 0x19,
120     DW_AT_common_reference = 0x1a,
121     DW_AT_comp_dir = 0x1b,
122     DW_AT_const_value = 0x1c,
123     DW_AT_containing_type = 0x1d,
124     DW_AT_default_value = 0x1e,
125     DW_AT_inline = 0x20,
126     DW_AT_is_optional = 0x21,
127     DW_AT_lower_bound = 0x22,
128     DW_AT_producer = 0x25,
129     DW_AT_prototyped = 0x27,
130     DW_AT_return_addr = 0x2a,
131     DW_AT_start_scope = 0x2c,
132     DW_AT_bit_stride = 0x2e,
133     DW_AT_upper_bound = 0x2f,
134     DW_AT_abstract_origin = 0x31,
135     DW_AT_accessibility = 0x32,
136     DW_AT_address_class = 0x33,
137     DW_AT_artificial = 0x34,
138     DW_AT_base_types = 0x35,
139     DW_AT_calling_convention = 0x36,
140     DW_AT_count = 0x37,
141     DW_AT_data_member_location = 0x38,
142     DW_AT_decl_column = 0x39,
143     DW_AT_decl_file = 0x3a,
144     DW_AT_decl_line = 0x3b,
145     DW_AT_declaration = 0x3c,
146     DW_AT_discr_list = 0x3d,
147     DW_AT_encoding = 0x3e,
148     DW_AT_external = 0x3f,
149     DW_AT_frame_base = 0x40,
150     DW_AT_friend = 0x41,
151     DW_AT_identifier_case = 0x42,
152     DW_AT_macro_info = 0x43,
153     DW_AT_namelist_item = 0x44,
154     DW_AT_priority = 0x45,
155     DW_AT_segment = 0x46,
156     DW_AT_specification = 0x47,
157     DW_AT_static_link = 0x48,
158     DW_AT_type = 0x49,
159     DW_AT_use_location = 0x4a,
160     DW_AT_variable_parameter = 0x4b,
161     DW_AT_virtuality = 0x4c,
162     DW_AT_vtable_elem_location = 0x4d,
163     DW_AT_allocated = 0x4e,
164     DW_AT_associated = 0x4f,
165     DW_AT_data_location = 0x50,
166     DW_AT_byte_stride = 0x51,
167     DW_AT_entry_pc = 0x52,
168     DW_AT_use_UTF8 = 0x53,
169     DW_AT_extension = 0x54,
170     DW_AT_ranges = 0x55,
171     DW_AT_trampoline = 0x56,
172     DW_AT_call_column = 0x57,
173     DW_AT_call_file = 0x58,
174     DW_AT_call_line = 0x59,
175     DW_AT_description = 0x5a,
176     DW_AT_binary_scale = 0x5b,
177     DW_AT_decimal_scale = 0x5c,
178     DW_AT_small = 0x5d,
179     DW_AT_decimal_sign = 0x5e,
180     DW_AT_digit_count = 0x5f,
181     DW_AT_picture_string = 0x60,
182     DW_AT_mutable = 0x61,
183     DW_AT_threads_scaled = 0x62,
184     DW_AT_explicit = 0x63,
185     DW_AT_object_pointer = 0x64,
186     DW_AT_endianity = 0x65,
187     DW_AT_elemental = 0x66,
188     DW_AT_pure = 0x67,
189     DW_AT_recursive = 0x68,
190     DW_AT_lo_user = 0x2000,
191     DW_AT_hi_user = 0x3fff,
192
193     // Attribute form encodings
194     DW_FORM_addr = 0x01,
195     DW_FORM_block2 = 0x03,
196     DW_FORM_block4 = 0x04,
197     DW_FORM_data2 = 0x05,
198     DW_FORM_data4 = 0x06,
199     DW_FORM_data8 = 0x07,
200     DW_FORM_string = 0x08,
201     DW_FORM_block = 0x09,
202     DW_FORM_block1 = 0x0a,
203     DW_FORM_data1 = 0x0b,
204     DW_FORM_flag = 0x0c,
205     DW_FORM_sdata = 0x0d,
206     DW_FORM_strp = 0x0e,
207     DW_FORM_udata = 0x0f,
208     DW_FORM_ref_addr = 0x10,
209     DW_FORM_ref1 = 0x11,
210     DW_FORM_ref2 = 0x12,
211     DW_FORM_ref4 = 0x13,
212     DW_FORM_ref8 = 0x14,
213     DW_FORM_ref_udata = 0x15,
214     DW_FORM_indirect = 0x16,
215
216     // Operation encodings
217     DW_OP_addr = 0x03,
218     DW_OP_deref = 0x06,
219     DW_OP_const1u = 0x08,
220     DW_OP_const1s = 0x09,
221     DW_OP_const2u = 0x0a,
222     DW_OP_const2s = 0x0b,
223     DW_OP_const4u = 0x0c,
224     DW_OP_const4s = 0x0d,
225     DW_OP_const8u = 0x0e,
226     DW_OP_const8s = 0x0f,
227     DW_OP_constu = 0x10,
228     DW_OP_consts = 0x11,
229     DW_OP_dup = 0x12,
230     DW_OP_drop = 0x13,
231     DW_OP_over = 0x14,
232     DW_OP_pick = 0x15,
233     DW_OP_swap = 0x16,
234     DW_OP_rot = 0x17,
235     DW_OP_xderef = 0x18,
236     DW_OP_abs = 0x19,
237     DW_OP_and = 0x1a,
238     DW_OP_div = 0x1b,
239     DW_OP_minus = 0x1c,
240     DW_OP_mod = 0x1d,
241     DW_OP_mul = 0x1e,
242     DW_OP_neg = 0x1f,
243     DW_OP_not = 0x20,
244     DW_OP_or = 0x21,
245     DW_OP_plus = 0x22,
246     DW_OP_plus_uconst = 0x23,
247     DW_OP_shl = 0x24,
248     DW_OP_shr = 0x25,
249     DW_OP_shra = 0x26,
250     DW_OP_xor = 0x27,
251     DW_OP_skip = 0x2f,
252     DW_OP_bra = 0x28,
253     DW_OP_eq = 0x29,
254     DW_OP_ge = 0x2a,
255     DW_OP_gt = 0x2b,
256     DW_OP_le = 0x2c,
257     DW_OP_lt = 0x2d,
258     DW_OP_ne = 0x2e,
259     DW_OP_lit0 = 0x30,
260     DW_OP_lit1 = 0x31,
261     DW_OP_lit31 = 0x4f,
262     DW_OP_reg0 = 0x50,
263     DW_OP_reg1 = 0x51,
264     DW_OP_reg31 = 0x6f,
265     DW_OP_breg0 = 0x70,
266     DW_OP_breg1 = 0x71,
267     DW_OP_breg31 = 0x8f,
268     DW_OP_regx = 0x90,
269     DW_OP_fbreg = 0x91,
270     DW_OP_bregx = 0x92,
271     DW_OP_piece = 0x93,
272     DW_OP_deref_size = 0x94,
273     DW_OP_xderef_size = 0x95,
274     DW_OP_nop = 0x96,
275     DW_OP_push_object_address = 0x97,
276     DW_OP_call2 = 0x98,
277     DW_OP_call4 = 0x99,
278     DW_OP_call_ref = 0x9a,
279     DW_OP_form_tls_address = 0x9b,
280     DW_OP_call_frame_cfa = 0x9c,
281     DW_OP_lo_user = 0xe0,
282     DW_OP_hi_user = 0xff,
283
284     // Encoding attribute values
285     DW_ATE_address = 0x01,
286     DW_ATE_boolean = 0x02,
287     DW_ATE_complex_float = 0x03,
288     DW_ATE_float = 0x04,
289     DW_ATE_signed = 0x05,
290     DW_ATE_signed_char = 0x06,
291     DW_ATE_unsigned = 0x07,
292     DW_ATE_unsigned_char = 0x08,
293     DW_ATE_imaginary_float = 0x09,
294     DW_ATE_packed_decimal = 0x0a,
295     DW_ATE_numeric_string = 0x0b,
296     DW_ATE_edited = 0x0c,
297     DW_ATE_signed_fixed = 0x0d,
298     DW_ATE_unsigned_fixed = 0x0e,
299     DW_ATE_decimal_float = 0x0f,
300     DW_ATE_lo_user = 0x80,
301     DW_ATE_hi_user = 0xff,
302
303     // Decimal sign attribute values
304     DW_DS_unsigned = 0x01,
305     DW_DS_leading_overpunch = 0x02,
306     DW_DS_trailing_overpunch = 0x03,
307     DW_DS_leading_separate = 0x04,
308     DW_DS_trailing_separate = 0x05,
309
310     // Endianity attribute values
311     DW_END_default = 0x00,
312     DW_END_big = 0x01,
313     DW_END_little = 0x02,
314     DW_END_lo_user = 0x40,
315     DW_END_hi_user = 0xff,
316
317     // Accessibility codes
318     DW_ACCESS_public = 0x01,
319     DW_ACCESS_protected = 0x02,
320     DW_ACCESS_private = 0x03,
321
322     // Visibility codes 
323     DW_VIS_local = 0x01,
324     DW_VIS_exported = 0x02,
325     DW_VIS_qualified = 0x03,
326
327     // Virtuality codes
328     DW_VIRTUALITY_none = 0x00,
329     DW_VIRTUALITY_virtual = 0x01,
330     DW_VIRTUALITY_pure_virtual = 0x02,
331     
332     // Language names
333     DW_LANG_C89 = 0x0001,
334     DW_LANG_C = 0x0002,
335     DW_LANG_Ada83 = 0x0003,
336     DW_LANG_C_plus_plus = 0x0004,
337     DW_LANG_Cobol74 = 0x0005,
338     DW_LANG_Cobol85 = 0x0006,
339     DW_LANG_Fortran77 = 0x0007,
340     DW_LANG_Fortran90 = 0x0008,
341     DW_LANG_Pascal83 = 0x0009,
342     DW_LANG_Modula2 = 0x000a,
343     DW_LANG_Java = 0x000b,
344     DW_LANG_C99 = 0x000c,
345     DW_LANG_Ada95 = 0x000d,
346     DW_LANG_Fortran95 = 0x000e,
347     DW_LANG_PLI = 0x000f,
348     DW_LANG_ObjC = 0x0010,
349     DW_LANG_ObjC_plus_plus = 0x0011,
350     DW_LANG_UPC = 0x0012,
351     DW_LANG_D = 0x0013,
352     DW_LANG_lo_user = 0x8000,
353     DW_LANG_hi_user = 0xffff,
354     
355     // Identifier case codes
356     DW_ID_case_sensitive = 0x00,
357     DW_ID_up_case = 0x01,
358     DW_ID_down_case = 0x02,
359     DW_ID_case_insensitive = 0x03,
360
361     // Calling convention codes
362     DW_CC_normal = 0x01,
363     DW_CC_program = 0x02,
364     DW_CC_nocall = 0x03,
365     DW_CC_lo_user = 0x40,
366     DW_CC_hi_user = 0xff,
367
368     // Inline codes
369     DW_INL_not_inlined = 0x00,
370     DW_INL_inlined = 0x01,
371     DW_INL_declared_not_inlined = 0x02,
372     DW_INL_declared_inlined = 0x03,
373
374     // Array ordering 
375     DW_ORD_row_major = 0x00,
376     DW_ORD_col_major = 0x01,
377
378     // Discriminant descriptor values
379     DW_DSC_label = 0x00,
380     DW_DSC_range = 0x01,
381
382     // Line Number Standard Opcode Encodings
383     DW_LNS_copy = 0x01,
384     DW_LNS_advance_pc = 0x02,
385     DW_LNS_advance_line = 0x03,
386     DW_LNS_set_file = 0x04,
387     DW_LNS_set_column = 0x05,
388     DW_LNS_negate_stmt = 0x06,
389     DW_LNS_set_basic_block = 0x07,
390     DW_LNS_const_add_pc = 0x08,
391     DW_LNS_fixed_advance_pc = 0x09,
392     DW_LNS_set_prologue_end = 0x0a,
393     DW_LNS_set_epilogue_begin = 0x0b,
394     DW_LNS_set_isa = 0x0c,
395
396     // Line Number Extended Opcode Encodings
397     DW_LNE_end_sequence = 0x01,
398     DW_LNE_set_address = 0x02,
399     DW_LNE_define_file = 0x03,
400     DW_LNE_lo_user = 0x80,
401     DW_LNE_hi_user = 0xff,
402
403     // Macinfo Type Encodings
404     DW_MACINFO_define = 0x01,
405     DW_MACINFO_undef = 0x02,
406     DW_MACINFO_start_file = 0x03,
407     DW_MACINFO_end_file = 0x04,
408     DW_MACINFO_vendor_ext = 0xff,
409
410     // Call frame instruction encodings
411     DW_CFA_advance_loc = 0x40,
412     DW_CFA_offset = 0x80,
413     DW_CFA_restore = 0xc0,
414     DW_CFA_set_loc = 0x01,
415     DW_CFA_advance_loc1 = 0x02,
416     DW_CFA_advance_loc2 = 0x03,
417     DW_CFA_advance_loc4 = 0x04,
418     DW_CFA_offset_extended = 0x05,
419     DW_CFA_restore_extended = 0x06,
420     DW_CFA_undefined = 0x07,
421     DW_CFA_same_value = 0x08,
422     DW_CFA_register = 0x09,
423     DW_CFA_remember_state = 0x0a,
424     DW_CFA_restore_state = 0x0b,
425     DW_CFA_def_cfa = 0x0c,
426     DW_CFA_def_cfa_register = 0x0d,
427     DW_CFA_def_cfa_offset = 0x0e,
428     DW_CFA_def_cfa_expression = 0x0f,
429     DW_CFA_expression = 0x10,
430     DW_CFA_offset_extended_sf = 0x11,
431     DW_CFA_def_cfa_sf = 0x12,
432     DW_CFA_def_cfa_offset_sf = 0x13,
433     DW_CFA_val_offset = 0x14,
434     DW_CFA_val_offset_sf = 0x15,
435     DW_CFA_val_expression = 0x16,
436     DW_CFA_lo_user = 0x1c,
437     DW_CFA_hi_user = 0x3f
438   };
439   
440   //===--------------------------------------------------------------------===//
441   // Forward declarations.
442   //
443   class AsmPrinter;
444   class DIE;
445   class DwarfWriter; 
446   class DWContext;
447   class MachineDebugInfo;
448   class MachineFunction;
449   class Module;
450   class Type;
451   
452   //===--------------------------------------------------------------------===//
453   // DWLabel - Labels are used to track locations in the assembler file.
454   // Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
455   // category of label (Ex. location) and number is a value unique in that
456   // category.
457   class DWLabel {
458   public:
459     const char *Tag;                    // Label category tag. Should always be
460                                         // a staticly declared C string.
461     unsigned    Number;                 // Unique number.
462
463     DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
464   };
465   
466   //===--------------------------------------------------------------------===//
467   // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
468   // Dwarf abbreviation.
469   class DIEAbbrevData {
470   private:
471     unsigned Attribute;                 // Dwarf attribute code.
472     unsigned Form;                      // Dwarf form code.
473     
474   public:
475     DIEAbbrevData(unsigned A, unsigned F)
476     : Attribute(A)
477     , Form(F)
478     {}
479     
480     // Accessors
481     unsigned getAttribute() const { return Attribute; }
482     unsigned getForm()      const { return Form; }
483     
484     /// operator== - Used by DIEAbbrev to locate entry.
485     ///
486     bool operator==(const DIEAbbrevData &DAD) const {
487       return Attribute == DAD.Attribute && Form == DAD.Form;
488     }
489
490     /// operator!= - Used by DIEAbbrev to locate entry.
491     ///
492     bool operator!=(const DIEAbbrevData &DAD) const {
493       return Attribute != DAD.Attribute || Form != DAD.Form;
494     }
495     
496     /// operator< - Used by DIEAbbrev to locate entry.
497     ///
498     bool operator<(const DIEAbbrevData &DAD) const {
499       return Attribute < DAD.Attribute ||
500             (Attribute == DAD.Attribute && Form < DAD.Form);
501     }
502   };
503   
504   //===--------------------------------------------------------------------===//
505   // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
506   // information object.
507   class DIEAbbrev {
508   private:
509     unsigned Tag;                       // Dwarf tag code.
510     unsigned ChildrenFlag;              // Dwarf children flag.
511     std::vector<DIEAbbrevData> Data;    // Raw data bytes for abbreviation.
512
513   public:
514   
515     DIEAbbrev(unsigned T, unsigned C)
516     : Tag(T)
517     , ChildrenFlag(C)
518     , Data()
519     {}
520     ~DIEAbbrev() {}
521     
522     // Accessors
523     unsigned getTag()                           const { return Tag; }
524     unsigned getChildrenFlag()                  const { return ChildrenFlag; }
525     const std::vector<DIEAbbrevData> &getData() const { return Data; }
526
527     /// operator== - Used by UniqueVector to locate entry.
528     ///
529     bool operator==(const DIEAbbrev &DA) const;
530
531     /// operator< - Used by UniqueVector to locate entry.
532     ///
533     bool operator<(const DIEAbbrev &DA) const;
534
535     /// AddAttribute - Adds another set of attribute information to the
536     /// abbreviation.
537     void AddAttribute(unsigned Attribute, unsigned Form) {
538       Data.push_back(DIEAbbrevData(Attribute, Form));
539     }
540     
541     /// Emit - Print the abbreviation using the specified Dwarf writer.
542     ///
543     void Emit(const DwarfWriter &DW) const; 
544         
545 #ifndef NDEBUG
546     void print(std::ostream &O);
547     void dump();
548 #endif
549   };
550
551   //===--------------------------------------------------------------------===//
552   // DIEValue - A debug information entry value.
553   //
554   class DIEValue {
555   public:
556     enum {
557       isInteger,
558       isString,
559       isLabel,
560       isAsIsLabel,
561       isDelta,
562       isEntry
563     };
564     
565     unsigned Type;                      // Type of the value
566     
567     DIEValue(unsigned T) : Type(T) {}
568     virtual ~DIEValue() {}
569     
570     // Implement isa/cast/dyncast.
571     static bool classof(const DIEValue *) { return true; }
572     
573     /// EmitValue - Emit value via the Dwarf writer.
574     ///
575     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
576     
577     /// SizeOf - Return the size of a value in bytes.
578     ///
579     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
580   };
581
582   //===--------------------------------------------------------------------===//
583   // DWInteger - An integer value DIE.
584   // 
585   class DIEInteger : public DIEValue {
586   private:
587     int Integer;
588     
589   public:
590     DIEInteger(int I) : DIEValue(isInteger), Integer(I) {}
591
592     // Implement isa/cast/dyncast.
593     static bool classof(const DIEInteger *) { return true; }
594     static bool classof(const DIEValue *I)  { return I->Type == isInteger; }
595     
596     /// EmitValue - Emit integer of appropriate size.
597     ///
598     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
599     
600     /// SizeOf - Determine size of integer value in bytes.
601     ///
602     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
603   };
604
605   //===--------------------------------------------------------------------===//
606   // DIEString - A string value DIE.
607   // 
608   struct DIEString : public DIEValue {
609     const std::string String;
610     
611     DIEString(const std::string &S) : DIEValue(isString), String(S) {}
612
613     // Implement isa/cast/dyncast.
614     static bool classof(const DIEString *) { return true; }
615     static bool classof(const DIEValue *S) { return S->Type == isString; }
616     
617     /// EmitValue - Emit string value.
618     ///
619     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
620     
621     /// SizeOf - Determine size of string value in bytes.
622     ///
623     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
624   };
625
626   //===--------------------------------------------------------------------===//
627   // DIEDwarfLabel - A Dwarf internal label expression DIE.
628   //
629   struct DIEDwarfLabel : public DIEValue {
630     const DWLabel Label;
631     
632     DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
633
634     // Implement isa/cast/dyncast.
635     static bool classof(const DIEDwarfLabel *)  { return true; }
636     static bool classof(const DIEValue *L) { return L->Type == isLabel; }
637     
638     /// EmitValue - Emit label value.
639     ///
640     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
641     
642     /// SizeOf - Determine size of label value in bytes.
643     ///
644     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
645   };
646
647
648   //===--------------------------------------------------------------------===//
649   // DIEObjectLabel - A label to an object in code or data.
650   //
651   struct DIEObjectLabel : public DIEValue {
652     const std::string Label;
653     
654     DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
655
656     // Implement isa/cast/dyncast.
657     static bool classof(const DIEObjectLabel *) { return true; }
658     static bool classof(const DIEValue *L)    { return L->Type == isAsIsLabel; }
659     
660     /// EmitValue - Emit label value.
661     ///
662     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
663     
664     /// SizeOf - Determine size of label value in bytes.
665     ///
666     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
667   };
668
669   //===--------------------------------------------------------------------===//
670   // DIEDelta - A simple label difference DIE.
671   // 
672   struct DIEDelta : public DIEValue {
673     const DWLabel LabelHi;
674     const DWLabel LabelLo;
675     
676     DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
677     : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
678
679     // Implement isa/cast/dyncast.
680     static bool classof(const DIEDelta *)  { return true; }
681     static bool classof(const DIEValue *D) { return D->Type == isDelta; }
682     
683     /// EmitValue - Emit delta value.
684     ///
685     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
686     
687     /// SizeOf - Determine size of delta value in bytes.
688     ///
689     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
690   };
691   
692   //===--------------------------------------------------------------------===//
693   // DIEntry - A pointer to a debug information entry.
694   // 
695   struct DIEntry : public DIEValue {
696     DIE *Entry;
697     
698     DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
699
700     // Implement isa/cast/dyncast.
701     static bool classof(const DIEntry *)   { return true; }
702     static bool classof(const DIEValue *E) { return E->Type == isEntry; }
703     
704     /// EmitValue - Emit delta value.
705     ///
706     virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
707     
708     /// SizeOf - Determine size of delta value in bytes.
709     ///
710     virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
711   };
712   
713   //===--------------------------------------------------------------------===//
714   // DIE - A structured debug information entry.  Has an abbreviation which
715   // describes it's organization.
716   class DIE {
717   private:
718     DIEAbbrev *Abbrev;                    // Temporary buffer for abbreviation.
719     unsigned AbbrevID;                    // Decribing abbreviation ID.
720     unsigned Offset;                      // Offset in debug info section.
721     unsigned Size;                        // Size of instance + children.
722     DWContext *Context;                   // Context for types and values.
723     std::vector<DIE *> Children;          // Children DIEs.
724     std::vector<DIEValue *> Values;       // Attributes values.
725     
726   public:
727     DIE(unsigned Tag, unsigned ChildrenFlag);
728     ~DIE();
729     
730     // Accessors
731     unsigned   getAbbrevID()                   const { return AbbrevID; }
732     unsigned   getOffset()                     const { return Offset; }
733     unsigned   getSize()                       const { return Size; }
734     DWContext *getContext()                    const { return Context; }
735     const std::vector<DIE *> &getChildren()    const { return Children; }
736     const std::vector<DIEValue *> &getValues() const { return Values; }
737     void setOffset(unsigned O)                 { Offset = O; }
738     void setSize(unsigned S)                   { Size = S; }
739     void setContext(DWContext *C)              { Context = C; }
740     
741     /// SiblingOffset - Return the offset of the debug information entry's
742     /// sibling.
743     unsigned SiblingOffset() const { return Offset + Size; }
744
745     /// AddInt - Add a simple integer attribute data and value.
746     ///
747     void AddInt(unsigned Attribute, unsigned Form,
748                 int Integer, bool IsSigned = false);
749         
750     /// AddString - Add a std::string attribute data and value.
751     ///
752     void AddString(unsigned Attribute, unsigned Form,
753                    const std::string &String);
754         
755     /// AddLabel - Add a Dwarf label attribute data and value.
756     ///
757     void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
758         
759     /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
760     ///
761     void AddObjectLabel(unsigned Attribute, unsigned Form,
762                         const std::string &Label);
763         
764     /// AddDelta - Add a label delta attribute data and value.
765     ///
766     void AddDelta(unsigned Attribute, unsigned Form,
767                   const DWLabel &Hi, const DWLabel &Lo);
768         
769     ///  AddDIEntry - Add a DIE attribute data and value.
770     ///
771     void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
772
773     /// Complete - Indicate that all attributes have been added and
774     /// ready to get an abbreviation ID.
775     ///
776     void Complete(DwarfWriter &DW);
777     
778     /// AddChild - Add a child to the DIE.
779     void AddChild(DIE *Child);
780   };
781   
782   //===--------------------------------------------------------------------===//
783   /// DWContext - Name context for types and values.
784   ///
785   class DWContext {
786   private:
787     DwarfWriter &DW;                    // DwarfWriter for global information.
788     DWContext *Parent;                  // Next context level searched.
789     DIE *Owner;                         // Owning debug information entry.
790     std::map<const Type *, DIE*> Types; // Named types in context.
791     std::map<std::string, DIE*> Variables;// Named variables in context.
792     
793   public:
794     DWContext(DwarfWriter &D, DWContext *P, DIE *O)
795     : DW(D)
796     , Parent(P)
797     , Owner(O)
798     , Types()
799     , Variables()
800     {
801       Owner->setContext(this);
802     }
803     ~DWContext() {}
804     
805     /// NewBasicType - Creates a new basic type, if necessary, then adds to the
806     /// context and owner.
807     DIE *NewBasicType(const Type *Ty, unsigned Size, unsigned Align);
808                                                
809     /// NewVariable - Creates a basic variable, if necessary, then adds to the
810     /// context and owner.
811     DIE *NewGlobalVariable(const std::string &Name,
812                            const std::string &MangledName,
813                            DIE *Type);
814   };
815
816   //===--------------------------------------------------------------------===//
817   // DwarfWriter - Emits Dwarf debug and exception handling directives.
818   //
819   class DwarfWriter {
820   protected:
821   
822     //===------------------------------------------------------------------===//
823     // Core attributes used by the Dwarf  writer.
824     //
825     
826     //
827     /// O - Stream to .s file.
828     ///
829     std::ostream &O;
830
831     /// Asm - Target of Dwarf emission.
832     ///
833     AsmPrinter *Asm;
834     
835     /// DebugInfo - Collected debug information.
836     ///
837     MachineDebugInfo *DebugInfo;
838     
839     /// didInitial - Flag to indicate if initial emission has been done.
840     ///
841     bool didInitial;
842     
843     //===------------------------------------------------------------------===//
844     // Attributes used to construct specific Dwarf sections.
845     //
846     
847     /// CompileUnits - All the compile units involved in this build.  The index
848     /// of each entry in this vector corresponds to the sources in DebugInfo.
849     std::vector<DIE *> CompileUnits;
850
851     /// Abbreviations - A UniqueVector of TAG structure abbreviations.
852     ///
853     UniqueVector<DIEAbbrev> Abbreviations;
854     
855     /// GlobalTypes - A map of globally visible named types.
856     ///
857     std::map<std::string, DIE *> GlobalTypes;
858     
859     /// GlobalEntities - A map of globally visible named entities.
860     ///
861     std::map<std::string, DIE *> GlobalEntities;
862      
863     /// StringPool - A UniqueVector of strings used by indirect references.
864     ///
865     UniqueVector<std::string> StringPool;
866     
867     //===------------------------------------------------------------------===//
868     // Properties to be set by the derived class ctor, used to configure the
869     // Dwarf writer.
870     //
871     
872     /// AddressSize - Size of addresses used in file.
873     ///
874     unsigned AddressSize;
875
876     /// hasLEB128 - True if target asm supports leb128 directives.
877     ///
878     bool hasLEB128; /// Defaults to false.
879     
880     /// hasDotLoc - True if target asm supports .loc directives.
881     ///
882     bool hasDotLoc; /// Defaults to false.
883     
884     /// hasDotFile - True if target asm supports .file directives.
885     ///
886     bool hasDotFile; /// Defaults to false.
887     
888     /// needsSet - True if target asm can't compute addresses on data
889     /// directives.
890     bool needsSet; /// Defaults to false.
891     
892     /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
893     ///
894     const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
895
896     /// DwarfInfoSection - Section directive for Dwarf info.
897     ///
898     const char *DwarfInfoSection; /// Defaults to ".debug_info".
899
900     /// DwarfLineSection - Section directive for Dwarf info.
901     ///
902     const char *DwarfLineSection; /// Defaults to ".debug_line".
903     
904     /// DwarfFrameSection - Section directive for Dwarf info.
905     ///
906     const char *DwarfFrameSection; /// Defaults to ".debug_frame".
907     
908     /// DwarfPubNamesSection - Section directive for Dwarf info.
909     ///
910     const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
911     
912     /// DwarfPubTypesSection - Section directive for Dwarf info.
913     ///
914     const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
915     
916     /// DwarfStrSection - Section directive for Dwarf info.
917     ///
918     const char *DwarfStrSection; /// Defaults to ".debug_str".
919
920     /// DwarfLocSection - Section directive for Dwarf info.
921     ///
922     const char *DwarfLocSection; /// Defaults to ".debug_loc".
923
924     /// DwarfARangesSection - Section directive for Dwarf info.
925     ///
926     const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
927
928     /// DwarfRangesSection - Section directive for Dwarf info.
929     ///
930     const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
931
932     /// DwarfMacInfoSection - Section directive for Dwarf info.
933     ///
934     const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
935
936     /// TextSection - Section directive for standard text.
937     ///
938     const char *TextSection; /// Defaults to ".text".
939     
940     /// DataSection - Section directive for standard data.
941     ///
942     const char *DataSection; /// Defaults to ".data".
943
944     //===------------------------------------------------------------------===//
945     // Emission and print routines
946     //
947
948 public:
949     /// getAddressSize - Return the size of a target address in bytes.
950     ///
951     unsigned getAddressSize() const { return AddressSize; }
952
953     /// PrintHex - Print a value as a hexidecimal value.
954     ///
955     void PrintHex(int Value) const;
956
957     /// EOL - Print a newline character to asm stream.  If a comment is present
958     /// then it will be printed first.  Comments should not contain '\n'.
959     void EOL(const std::string &Comment) const;
960                                           
961     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
962     /// unsigned leb128 value.
963     void EmitULEB128Bytes(unsigned Value) const;
964     
965     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
966     /// signed leb128 value.
967     void EmitSLEB128Bytes(int Value) const;
968     
969     /// PrintULEB128 - Print a series of hexidecimal values (separated by
970     /// commas) representing an unsigned leb128 value.
971     void PrintULEB128(unsigned Value) const;
972
973     /// SizeULEB128 - Compute the number of bytes required for an unsigned
974     /// leb128 value.
975     static unsigned SizeULEB128(unsigned Value);
976     
977     /// PrintSLEB128 - Print a series of hexidecimal values (separated by
978     /// commas) representing a signed leb128 value.
979     void PrintSLEB128(int Value) const;
980     
981     /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
982     /// value.
983     static unsigned SizeSLEB128(int Value);
984     
985     /// EmitByte - Emit a byte directive and value.
986     ///
987     void EmitByte(int Value) const;
988
989     /// EmitShort - Emit a short directive and value.
990     ///
991     void EmitShort(int Value) const;
992
993     /// EmitLong - Emit a long directive and value.
994     ///
995     void EmitLong(int Value) const;
996     
997     /// EmitString - Emit a string with quotes and a null terminator.
998     /// Special characters are emitted properly. (Eg. '\t')
999     void EmitString(const std::string &String) const;
1000
1001     /// PrintLabelName - Print label name in form used by Dwarf writer.
1002     ///
1003     void PrintLabelName(DWLabel Label) const {
1004       PrintLabelName(Label.Tag, Label.Number);
1005     }
1006     void PrintLabelName(const char *Tag, unsigned Number) const;
1007     
1008     /// EmitLabel - Emit location label for internal use by Dwarf.
1009     ///
1010     void EmitLabel(DWLabel Label) const {
1011       EmitLabel(Label.Tag, Label.Number);
1012     }
1013     void EmitLabel(const char *Tag, unsigned Number) const;
1014     
1015     /// EmitReference - Emit a reference to a label.
1016     ///
1017     void EmitReference(DWLabel Label) const {
1018       EmitReference(Label.Tag, Label.Number);
1019     }
1020     void EmitReference(const char *Tag, unsigned Number) const;
1021     void EmitReference(const std::string &Name) const;
1022
1023     /// EmitDifference - Emit the difference between two labels.  Some
1024     /// assemblers do not behave with absolute expressions with data directives,
1025     /// so there is an option (needsSet) to use an intermediary set expression.
1026     void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
1027       EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
1028     }
1029     void EmitDifference(const char *TagHi, unsigned NumberHi,
1030                         const char *TagLo, unsigned NumberLo) const;
1031                                    
1032     /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
1033     ///  
1034     unsigned NewAbbreviation(DIEAbbrev *Abbrev);
1035     
1036     /// NewString - Add a string to the constant pool and returns a label.
1037     ///
1038     DWLabel NewString(const std::string &String);
1039     
1040     /// NewGlobalType - Make the type visible globally using the given name.
1041     ///
1042     void NewGlobalType(const std::string &Name, DIE *Type);
1043     
1044     /// NewGlobalEntity - Make the entity visible globally using the given name.
1045     ///
1046     void NewGlobalEntity(const std::string &Name, DIE *Entity);
1047     
1048     /// NewGlobalVariable - Add a new global variable DIE to the context.
1049     ///
1050     void NewGlobalVariable(DWContext *Context,
1051                            const std::string &Name,
1052                            const std::string &MangledName,
1053                            const Type *Ty,
1054                            unsigned Size, unsigned Align);
1055
1056 private:
1057     /// NewCompileUnit - Create new compile unit information.
1058     ///
1059     DIE *NewCompileUnit(const std::string &Directory,
1060                         const std::string &SourceName);
1061
1062     /// EmitInitial - Emit initial Dwarf declarations.
1063     ///
1064     void EmitInitial() const;
1065     
1066     /// EmitDIE - Recusively Emits a debug information entry.
1067     ///
1068     void EmitDIE(DIE *Die) const;
1069     
1070     /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1071     ///
1072     unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset) const;
1073
1074     /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1075     ///
1076     void SizeAndOffsets();
1077     
1078     /// EmitDebugInfo - Emit the debug info section.
1079     ///
1080     void EmitDebugInfo() const;
1081     
1082     /// EmitAbbreviations - Emit the abbreviation section.
1083     ///
1084     void EmitAbbreviations() const;
1085     
1086     /// EmitDebugLines - Emit source line information.
1087     ///
1088     void EmitDebugLines() const;
1089
1090     /// EmitDebugFrame - Emit info into a debug frame section.
1091     ///
1092     void EmitDebugFrame();
1093     
1094     /// EmitDebugPubNames - Emit info into a debug pubnames section.
1095     ///
1096     void EmitDebugPubNames();
1097     
1098     /// EmitDebugPubTypes - Emit info into a debug pubtypes section.
1099     ///
1100     void EmitDebugPubTypes();
1101     
1102     /// EmitDebugStr - Emit info into a debug str section.
1103     ///
1104     void EmitDebugStr();
1105     
1106     /// EmitDebugLoc - Emit info into a debug loc section.
1107     ///
1108     void EmitDebugLoc();
1109     
1110     /// EmitDebugARanges - Emit info into a debug aranges section.
1111     ///
1112     void EmitDebugARanges();
1113     
1114     /// EmitDebugRanges - Emit info into a debug ranges section.
1115     ///
1116     void EmitDebugRanges();
1117     
1118     /// EmitDebugMacInfo - Emit info into a debug macinfo section.
1119     ///
1120     void EmitDebugMacInfo();
1121     
1122     /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1123     /// header file.
1124     void ConstructCompileUnitDIEs();
1125     
1126     /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1127     /// variables.
1128     void ConstructGlobalDIEs(Module &M);
1129
1130     /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1131     /// When called it also checks to see if debug info is newly available.  if
1132     /// so the initial Dwarf headers are emitted.
1133     bool ShouldEmitDwarf();
1134   
1135   public:
1136     
1137     DwarfWriter(std::ostream &OS, AsmPrinter *A);
1138     virtual ~DwarfWriter();
1139     
1140     /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
1141     /// created it.  Set by the target AsmPrinter.
1142     void SetDebugInfo(MachineDebugInfo *DI) { DebugInfo = DI; }
1143
1144     //===------------------------------------------------------------------===//
1145     // Main entry points.
1146     //
1147     
1148     /// BeginModule - Emit all Dwarf sections that should come prior to the
1149     /// content.
1150     void BeginModule(Module &M);
1151     
1152     /// EndModule - Emit all Dwarf sections that should come after the content.
1153     ///
1154     void EndModule(Module &M);
1155     
1156     /// BeginFunction - Gather pre-function debug information.
1157     ///
1158     void BeginFunction(MachineFunction &MF);
1159     
1160     /// EndFunction - Gather and emit post-function debug information.
1161     ///
1162     void EndFunction(MachineFunction &MF);
1163   };
1164
1165 } // end llvm namespace
1166
1167 #endif