blob: cc4ef8fde15b7812c57b87c126b69ea6de1491a4 [file] [log] [blame]
David Srbecky15c19752015-03-31 14:53:55 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
18#define ART_COMPILER_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_
19
20#include "dwarf.h"
21#include "register.h"
22#include "writer.h"
23
24namespace art {
25namespace dwarf {
26
27// Writer for .debug_frame opcodes (DWARF-3).
28// See the DWARF specification for the precise meaning of the opcodes.
29// The writer is very light-weight, however it will do the following for you:
30// * Choose the most compact encoding of a given opcode.
31// * Keep track of current state and convert absolute values to deltas.
32// * Divide by header-defined factors as appropriate.
33template<typename Allocator = std::allocator<uint8_t> >
34class DebugFrameOpCodeWriter : private Writer<Allocator> {
35 public:
36 // To save space, DWARF divides most offsets by header-defined factors.
37 // They are used in integer divisions, so we make them constants.
38 // We usually subtract from stack base pointer, so making the factor
39 // negative makes the encoded values positive and thus easier to encode.
40 static constexpr int kDataAlignmentFactor = -4;
41 static constexpr int kCodeAlignmentFactor = 1;
42
43 // Explicitely advance the program counter to given location.
44 void AdvancePC(int absolute_pc) {
45 DCHECK_GE(absolute_pc, current_pc_);
46 int delta = FactorCodeOffset(absolute_pc - current_pc_);
47 if (delta != 0) {
48 if (delta <= 0x3F) {
49 this->PushUint8(DW_CFA_advance_loc | delta);
50 } else if (delta <= UINT8_MAX) {
51 this->PushUint8(DW_CFA_advance_loc1);
52 this->PushUint8(delta);
53 } else if (delta <= UINT16_MAX) {
54 this->PushUint8(DW_CFA_advance_loc2);
55 this->PushUint16(delta);
56 } else {
57 this->PushUint8(DW_CFA_advance_loc4);
58 this->PushUint32(delta);
59 }
60 }
61 current_pc_ = absolute_pc;
62 }
63
64 // Override this method to automatically advance the PC before each opcode.
65 virtual void ImplicitlyAdvancePC() { }
66
67 // Common alias in assemblers - spill relative to current stack pointer.
68 void RelOffset(Reg reg, int offset) {
69 Offset(reg, offset - current_cfa_offset_);
70 }
71
72 // Common alias in assemblers - increase stack frame size.
73 void AdjustCFAOffset(int delta) {
74 DefCFAOffset(current_cfa_offset_ + delta);
75 }
76
77 // Custom alias - spill many registers based on bitmask.
78 void RelOffsetForMany(Reg reg_base, int offset, uint32_t reg_mask,
79 int reg_size) {
80 DCHECK(reg_size == 4 || reg_size == 8);
81 for (int i = 0; reg_mask != 0u; reg_mask >>= 1, i++) {
82 if ((reg_mask & 1) != 0u) {
83 RelOffset(Reg(reg_base.num() + i), offset);
84 offset += reg_size;
85 }
86 }
87 }
88
89 // Custom alias - unspill many registers based on bitmask.
90 void RestoreMany(Reg reg_base, uint32_t reg_mask) {
91 for (int i = 0; reg_mask != 0u; reg_mask >>= 1, i++) {
92 if ((reg_mask & 1) != 0u) {
93 Restore(Reg(reg_base.num() + i));
94 }
95 }
96 }
97
98 void Nop() {
99 this->PushUint8(DW_CFA_nop);
100 }
101
102 void Offset(Reg reg, int offset) {
103 ImplicitlyAdvancePC();
104 int factored_offset = FactorDataOffset(offset); // May change sign.
105 if (factored_offset >= 0) {
106 if (0 <= reg.num() && reg.num() <= 0x3F) {
107 this->PushUint8(DW_CFA_offset | reg.num());
108 this->PushUleb128(factored_offset);
109 } else {
110 this->PushUint8(DW_CFA_offset_extended);
111 this->PushUleb128(reg.num());
112 this->PushUleb128(factored_offset);
113 }
114 } else {
115 uses_dwarf3_features_ = true;
116 this->PushUint8(DW_CFA_offset_extended_sf);
117 this->PushUleb128(reg.num());
118 this->PushSleb128(factored_offset);
119 }
120 }
121
122 void Restore(Reg reg) {
123 ImplicitlyAdvancePC();
124 if (0 <= reg.num() && reg.num() <= 0x3F) {
125 this->PushUint8(DW_CFA_restore | reg.num());
126 } else {
127 this->PushUint8(DW_CFA_restore_extended);
128 this->PushUleb128(reg.num());
129 }
130 }
131
132 void Undefined(Reg reg) {
133 ImplicitlyAdvancePC();
134 this->PushUint8(DW_CFA_undefined);
135 this->PushUleb128(reg.num());
136 }
137
138 void SameValue(Reg reg) {
139 ImplicitlyAdvancePC();
140 this->PushUint8(DW_CFA_same_value);
141 this->PushUleb128(reg.num());
142 }
143
144 // The previous value of "reg" is stored in register "new_reg".
145 void Register(Reg reg, Reg new_reg) {
146 ImplicitlyAdvancePC();
147 this->PushUint8(DW_CFA_register);
148 this->PushUleb128(reg.num());
149 this->PushUleb128(new_reg.num());
150 }
151
152 void RememberState() {
153 // Note that we do not need to advance the PC.
154 this->PushUint8(DW_CFA_remember_state);
155 }
156
157 void RestoreState() {
158 ImplicitlyAdvancePC();
159 this->PushUint8(DW_CFA_restore_state);
160 }
161
162 void DefCFA(Reg reg, int offset) {
163 ImplicitlyAdvancePC();
164 if (offset >= 0) {
165 this->PushUint8(DW_CFA_def_cfa);
166 this->PushUleb128(reg.num());
167 this->PushUleb128(offset); // Non-factored.
168 } else {
169 uses_dwarf3_features_ = true;
170 this->PushUint8(DW_CFA_def_cfa_sf);
171 this->PushUleb128(reg.num());
172 this->PushSleb128(FactorDataOffset(offset));
173 }
174 current_cfa_offset_ = offset;
175 }
176
177 void DefCFARegister(Reg reg) {
178 ImplicitlyAdvancePC();
179 this->PushUint8(DW_CFA_def_cfa_register);
180 this->PushUleb128(reg.num());
181 }
182
183 void DefCFAOffset(int offset) {
184 if (current_cfa_offset_ != offset) {
185 ImplicitlyAdvancePC();
186 if (offset >= 0) {
187 this->PushUint8(DW_CFA_def_cfa_offset);
188 this->PushUleb128(offset); // Non-factored.
189 } else {
190 uses_dwarf3_features_ = true;
191 this->PushUint8(DW_CFA_def_cfa_offset_sf);
192 this->PushSleb128(FactorDataOffset(offset));
193 }
194 current_cfa_offset_ = offset;
195 }
196 }
197
198 void ValOffset(Reg reg, int offset) {
199 ImplicitlyAdvancePC();
200 uses_dwarf3_features_ = true;
201 int factored_offset = FactorDataOffset(offset); // May change sign.
202 if (factored_offset >= 0) {
203 this->PushUint8(DW_CFA_val_offset);
204 this->PushUleb128(reg.num());
205 this->PushUleb128(factored_offset);
206 } else {
207 this->PushUint8(DW_CFA_val_offset_sf);
208 this->PushUleb128(reg.num());
209 this->PushSleb128(factored_offset);
210 }
211 }
212
213 void DefCFAExpression(void* expr, int expr_size) {
214 ImplicitlyAdvancePC();
215 uses_dwarf3_features_ = true;
216 this->PushUint8(DW_CFA_def_cfa_expression);
217 this->PushUleb128(expr_size);
218 this->PushData(expr, expr_size);
219 }
220
221 void Expression(Reg reg, void* expr, int expr_size) {
222 ImplicitlyAdvancePC();
223 uses_dwarf3_features_ = true;
224 this->PushUint8(DW_CFA_expression);
225 this->PushUleb128(reg.num());
226 this->PushUleb128(expr_size);
227 this->PushData(expr, expr_size);
228 }
229
230 void ValExpression(Reg reg, void* expr, int expr_size) {
231 ImplicitlyAdvancePC();
232 uses_dwarf3_features_ = true;
233 this->PushUint8(DW_CFA_val_expression);
234 this->PushUleb128(reg.num());
235 this->PushUleb128(expr_size);
236 this->PushData(expr, expr_size);
237 }
238
239 int GetCurrentCFAOffset() const {
240 return current_cfa_offset_;
241 }
242
243 void SetCurrentCFAOffset(int offset) {
244 current_cfa_offset_ = offset;
245 }
246
247 using Writer<Allocator>::data;
248
249 DebugFrameOpCodeWriter(const Allocator& alloc = Allocator())
250 : Writer<Allocator>(&opcodes_),
251 opcodes_(alloc),
252 current_cfa_offset_(0),
253 current_pc_(0),
254 uses_dwarf3_features_(false) {
255 }
256
257 virtual ~DebugFrameOpCodeWriter() { }
258
259 protected:
260 int FactorDataOffset(int offset) const {
261 DCHECK_EQ(offset % kDataAlignmentFactor, 0);
262 return offset / kDataAlignmentFactor;
263 }
264
265 int FactorCodeOffset(int offset) const {
266 DCHECK_EQ(offset % kCodeAlignmentFactor, 0);
267 return offset / kCodeAlignmentFactor;
268 }
269
270 std::vector<uint8_t, Allocator> opcodes_;
271 int current_cfa_offset_;
272 int current_pc_;
273 bool uses_dwarf3_features_;
274
275 private:
276 DISALLOW_COPY_AND_ASSIGN(DebugFrameOpCodeWriter);
277};
278
279} // namespace dwarf
280} // namespace art
281
282#endif // ART_COMPILER_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_