Output source notes in extended DWARF DIEs
[ghc.git] / compiler / nativeGen / Dwarf / Constants.hs
1 -- | Constants describing the DWARF format. Most of this simply
2 -- mirrors /usr/include/dwarf.h.
3
4 module Dwarf.Constants where
5
6 import FastString
7 import Platform
8 import Outputable
9
10 import Reg
11 import X86.Regs
12
13 import Data.Word
14
15 -- | Language ID used for Haskell.
16 dW_LANG_Haskell :: Word
17 dW_LANG_Haskell = 0x18
18 -- Thanks to Nathan Howell for getting us our very own language ID!
19
20 -- * Dwarf tags
21 dW_TAG_compile_unit, dW_TAG_subroutine_type,
22 dW_TAG_file_type, dW_TAG_subprogram, dW_TAG_lexical_block,
23 dW_TAG_base_type, dW_TAG_structure_type, dW_TAG_pointer_type,
24 dW_TAG_array_type, dW_TAG_subrange_type, dW_TAG_typedef,
25 dW_TAG_variable, dW_TAG_arg_variable, dW_TAG_auto_variable,
26 dW_TAG_ghc_src_note :: Word
27 dW_TAG_array_type = 1
28 dW_TAG_lexical_block = 11
29 dW_TAG_pointer_type = 15
30 dW_TAG_compile_unit = 17
31 dW_TAG_structure_type = 19
32 dW_TAG_typedef = 22
33 dW_TAG_subroutine_type = 32
34 dW_TAG_subrange_type = 33
35 dW_TAG_base_type = 36
36 dW_TAG_file_type = 41
37 dW_TAG_subprogram = 46
38 dW_TAG_variable = 52
39 dW_TAG_auto_variable = 256
40 dW_TAG_arg_variable = 257
41
42 dW_TAG_ghc_src_note = 0x5b00
43
44 -- * Dwarf attributes
45 dW_AT_name, dW_AT_stmt_list, dW_AT_low_pc, dW_AT_high_pc, dW_AT_language,
46 dW_AT_comp_dir, dW_AT_producer, dW_AT_external, dW_AT_frame_base,
47 dW_AT_use_UTF8, dW_AT_MIPS_linkage_name :: Word
48 dW_AT_name = 0x03
49 dW_AT_stmt_list = 0x10
50 dW_AT_low_pc = 0x11
51 dW_AT_high_pc = 0x12
52 dW_AT_language = 0x13
53 dW_AT_comp_dir = 0x1b
54 dW_AT_producer = 0x25
55 dW_AT_external = 0x3f
56 dW_AT_frame_base = 0x40
57 dW_AT_use_UTF8 = 0x53
58 dW_AT_MIPS_linkage_name = 0x2007
59
60 -- * Custom DWARF attributes
61 -- Chosen a more or less random section of the vendor-extensible region
62
63 -- ** Describing C-- blocks
64 -- These appear in DW_TAG_lexical_scope DIEs corresponding to C-- blocks
65 dW_AT_ghc_tick_parent :: Word
66 dW_AT_ghc_tick_parent = 0x2b20
67
68 -- ** Describing source notes
69 -- These appear in DW_TAG_ghc_src_note DIEs
70 dW_AT_ghc_span_file, dW_AT_ghc_span_start_line,
71 dW_AT_ghc_span_start_col, dW_AT_ghc_span_end_line,
72 dW_AT_ghc_span_end_col :: Word
73 dW_AT_ghc_span_file = 0x2b00
74 dW_AT_ghc_span_start_line = 0x2b01
75 dW_AT_ghc_span_start_col = 0x2b02
76 dW_AT_ghc_span_end_line = 0x2b03
77 dW_AT_ghc_span_end_col = 0x2b04
78
79
80 -- * Abbrev declarations
81 dW_CHILDREN_no, dW_CHILDREN_yes :: Word8
82 dW_CHILDREN_no = 0
83 dW_CHILDREN_yes = 1
84
85 dW_FORM_addr, dW_FORM_data2, dW_FORM_data4, dW_FORM_string, dW_FORM_flag,
86 dW_FORM_block1, dW_FORM_ref4, dW_FORM_ref_addr, dW_FORM_flag_present :: Word
87 dW_FORM_addr = 0x01
88 dW_FORM_data2 = 0x05
89 dW_FORM_data4 = 0x06
90 dW_FORM_string = 0x08
91 dW_FORM_flag = 0x0c
92 dW_FORM_block1 = 0x0a
93 dW_FORM_ref_addr = 0x10
94 dW_FORM_ref4 = 0x13
95 dW_FORM_flag_present = 0x19
96
97 -- * Dwarf native types
98 dW_ATE_address, dW_ATE_boolean, dW_ATE_float, dW_ATE_signed,
99 dW_ATE_signed_char, dW_ATE_unsigned, dW_ATE_unsigned_char :: Word
100 dW_ATE_address = 1
101 dW_ATE_boolean = 2
102 dW_ATE_float = 4
103 dW_ATE_signed = 5
104 dW_ATE_signed_char = 6
105 dW_ATE_unsigned = 7
106 dW_ATE_unsigned_char = 8
107
108 -- * Call frame information
109 dW_CFA_set_loc, dW_CFA_undefined, dW_CFA_same_value,
110 dW_CFA_def_cfa, dW_CFA_def_cfa_offset, dW_CFA_def_cfa_expression,
111 dW_CFA_expression, dW_CFA_offset_extended_sf, dW_CFA_def_cfa_offset_sf,
112 dW_CFA_def_cfa_sf, dW_CFA_val_offset, dW_CFA_val_expression,
113 dW_CFA_offset :: Word8
114 dW_CFA_set_loc = 0x01
115 dW_CFA_undefined = 0x07
116 dW_CFA_same_value = 0x08
117 dW_CFA_def_cfa = 0x0c
118 dW_CFA_def_cfa_offset = 0x0e
119 dW_CFA_def_cfa_expression = 0x0f
120 dW_CFA_expression = 0x10
121 dW_CFA_offset_extended_sf = 0x11
122 dW_CFA_def_cfa_sf = 0x12
123 dW_CFA_def_cfa_offset_sf = 0x13
124 dW_CFA_val_offset = 0x14
125 dW_CFA_val_expression = 0x16
126 dW_CFA_offset = 0x80
127
128 -- * Operations
129 dW_OP_deref, dW_OP_consts,
130 dW_OP_minus, dW_OP_mul, dW_OP_plus,
131 dW_OP_lit0, dW_OP_breg0, dW_OP_call_frame_cfa :: Word8
132 dW_OP_deref = 0x06
133 dW_OP_consts = 0x11
134 dW_OP_minus = 0x1c
135 dW_OP_mul = 0x1e
136 dW_OP_plus = 0x22
137 dW_OP_lit0 = 0x30
138 dW_OP_breg0 = 0x70
139 dW_OP_call_frame_cfa = 0x9c
140
141 -- * Dwarf section declarations
142 dwarfInfoSection, dwarfAbbrevSection, dwarfLineSection,
143 dwarfFrameSection, dwarfGhcSection, dwarfARangesSection :: SDoc
144 dwarfInfoSection = dwarfSection "info"
145 dwarfAbbrevSection = dwarfSection "abbrev"
146 dwarfLineSection = dwarfSection "line"
147 dwarfFrameSection = dwarfSection "frame"
148 dwarfGhcSection = dwarfSection "ghc"
149 dwarfARangesSection = dwarfSection "aranges"
150
151 dwarfSection :: String -> SDoc
152 dwarfSection name = sdocWithPlatform $ \plat -> ftext $ mkFastString $
153 case platformOS plat of
154 os | osElfTarget os
155 -> "\t.section .debug_" ++ name ++ ",\"\",@progbits"
156 | osMachOTarget os
157 -> "\t.section __DWARF,__debug_" ++ name ++ ",regular,debug"
158 | otherwise
159 -> "\t.section .debug_" ++ name ++ ",\"dr\""
160
161 -- * Dwarf section labels
162 dwarfInfoLabel, dwarfAbbrevLabel, dwarfLineLabel, dwarfFrameLabel :: LitString
163 dwarfInfoLabel = sLit ".Lsection_info"
164 dwarfAbbrevLabel = sLit ".Lsection_abbrev"
165 dwarfLineLabel = sLit ".Lsection_line"
166 dwarfFrameLabel = sLit ".Lsection_frame"
167
168 -- | Mapping of registers to DWARF register numbers
169 dwarfRegNo :: Platform -> Reg -> Word8
170 dwarfRegNo p r = case platformArch p of
171 ArchX86
172 | r == eax -> 0
173 | r == ecx -> 1 -- yes, no typo
174 | r == edx -> 2
175 | r == ebx -> 3
176 | r == esp -> 4
177 | r == ebp -> 5
178 | r == esi -> 6
179 | r == edi -> 7
180 ArchX86_64
181 | r == rax -> 0
182 | r == rdx -> 1 -- this neither. The order GCC allocates registers in?
183 | r == rcx -> 2
184 | r == rbx -> 3
185 | r == rsi -> 4
186 | r == rdi -> 5
187 | r == rbp -> 6
188 | r == rsp -> 7
189 | r == r8 -> 8
190 | r == r9 -> 9
191 | r == r10 -> 10
192 | r == r11 -> 11
193 | r == r12 -> 12
194 | r == r13 -> 13
195 | r == r14 -> 14
196 | r == r15 -> 15
197 | r == xmm0 -> 17
198 | r == xmm1 -> 18
199 | r == xmm2 -> 19
200 | r == xmm3 -> 20
201 | r == xmm4 -> 21
202 | r == xmm5 -> 22
203 | r == xmm6 -> 23
204 | r == xmm7 -> 24
205 | r == xmm8 -> 25
206 | r == xmm9 -> 26
207 | r == xmm10 -> 27
208 | r == xmm11 -> 28
209 | r == xmm12 -> 29
210 | r == xmm13 -> 30
211 | r == xmm14 -> 31
212 | r == xmm15 -> 32
213 _other -> error "dwarfRegNo: Unsupported platform or unknown register!"
214
215 -- | Virtual register number to use for return address.
216 dwarfReturnRegNo :: Platform -> Word8
217 dwarfReturnRegNo p
218 -- We "overwrite" IP with our pseudo register - that makes sense, as
219 -- when using this mechanism gdb already knows the IP anyway. Clang
220 -- does this too, so it must be safe.
221 = case platformArch p of
222 ArchX86 -> 8 -- eip
223 ArchX86_64 -> 16 -- rip
224 _other -> error "dwarfReturnRegNo: Unsupported platform!"