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