blob: fb00615ce0784b6105e3029c9cdd02d09a362419 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Expression/DWARFExpression.h"
11
12#include <vector>
13
14#include "lldb/Core/dwarf.h"
15#include "lldb/Core/Log.h"
Greg Clayton061b79d2011-05-09 20:18:18 +000016#include "lldb/Core/RegisterValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/StreamString.h"
18#include "lldb/Core/Scalar.h"
19#include "lldb/Core/Value.h"
Greg Clayton178710c2010-09-14 02:20:48 +000020#include "lldb/Core/VMRange.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22#include "lldb/Expression/ClangExpressionDeclMap.h"
23#include "lldb/Expression/ClangExpressionVariable.h"
24
Greg Claytoncd548032011-02-01 01:31:41 +000025#include "lldb/Host/Endian.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27#include "lldb/lldb-private-log.h"
28
Greg Clayton1674b122010-07-21 22:12:05 +000029#include "lldb/Symbol/ClangASTType.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Symbol/ClangASTContext.h"
31#include "lldb/Symbol/Type.h"
32
33#include "lldb/Target/ExecutionContext.h"
34#include "lldb/Target/Process.h"
35#include "lldb/Target/RegisterContext.h"
36#include "lldb/Target/StackFrame.h"
37
38using namespace lldb;
39using namespace lldb_private;
40
41const char *
42DW_OP_value_to_name (uint32_t val)
43{
44 static char invalid[100];
45 switch (val) {
46 case 0x03: return "DW_OP_addr";
47 case 0x06: return "DW_OP_deref";
48 case 0x08: return "DW_OP_const1u";
49 case 0x09: return "DW_OP_const1s";
50 case 0x0a: return "DW_OP_const2u";
51 case 0x0b: return "DW_OP_const2s";
52 case 0x0c: return "DW_OP_const4u";
53 case 0x0d: return "DW_OP_const4s";
54 case 0x0e: return "DW_OP_const8u";
55 case 0x0f: return "DW_OP_const8s";
56 case 0x10: return "DW_OP_constu";
57 case 0x11: return "DW_OP_consts";
58 case 0x12: return "DW_OP_dup";
59 case 0x13: return "DW_OP_drop";
60 case 0x14: return "DW_OP_over";
61 case 0x15: return "DW_OP_pick";
62 case 0x16: return "DW_OP_swap";
63 case 0x17: return "DW_OP_rot";
64 case 0x18: return "DW_OP_xderef";
65 case 0x19: return "DW_OP_abs";
66 case 0x1a: return "DW_OP_and";
67 case 0x1b: return "DW_OP_div";
68 case 0x1c: return "DW_OP_minus";
69 case 0x1d: return "DW_OP_mod";
70 case 0x1e: return "DW_OP_mul";
71 case 0x1f: return "DW_OP_neg";
72 case 0x20: return "DW_OP_not";
73 case 0x21: return "DW_OP_or";
74 case 0x22: return "DW_OP_plus";
75 case 0x23: return "DW_OP_plus_uconst";
76 case 0x24: return "DW_OP_shl";
77 case 0x25: return "DW_OP_shr";
78 case 0x26: return "DW_OP_shra";
79 case 0x27: return "DW_OP_xor";
80 case 0x2f: return "DW_OP_skip";
81 case 0x28: return "DW_OP_bra";
82 case 0x29: return "DW_OP_eq";
83 case 0x2a: return "DW_OP_ge";
84 case 0x2b: return "DW_OP_gt";
85 case 0x2c: return "DW_OP_le";
86 case 0x2d: return "DW_OP_lt";
87 case 0x2e: return "DW_OP_ne";
88 case 0x30: return "DW_OP_lit0";
89 case 0x31: return "DW_OP_lit1";
90 case 0x32: return "DW_OP_lit2";
91 case 0x33: return "DW_OP_lit3";
92 case 0x34: return "DW_OP_lit4";
93 case 0x35: return "DW_OP_lit5";
94 case 0x36: return "DW_OP_lit6";
95 case 0x37: return "DW_OP_lit7";
96 case 0x38: return "DW_OP_lit8";
97 case 0x39: return "DW_OP_lit9";
98 case 0x3a: return "DW_OP_lit10";
99 case 0x3b: return "DW_OP_lit11";
100 case 0x3c: return "DW_OP_lit12";
101 case 0x3d: return "DW_OP_lit13";
102 case 0x3e: return "DW_OP_lit14";
103 case 0x3f: return "DW_OP_lit15";
104 case 0x40: return "DW_OP_lit16";
105 case 0x41: return "DW_OP_lit17";
106 case 0x42: return "DW_OP_lit18";
107 case 0x43: return "DW_OP_lit19";
108 case 0x44: return "DW_OP_lit20";
109 case 0x45: return "DW_OP_lit21";
110 case 0x46: return "DW_OP_lit22";
111 case 0x47: return "DW_OP_lit23";
112 case 0x48: return "DW_OP_lit24";
113 case 0x49: return "DW_OP_lit25";
114 case 0x4a: return "DW_OP_lit26";
115 case 0x4b: return "DW_OP_lit27";
116 case 0x4c: return "DW_OP_lit28";
117 case 0x4d: return "DW_OP_lit29";
118 case 0x4e: return "DW_OP_lit30";
119 case 0x4f: return "DW_OP_lit31";
120 case 0x50: return "DW_OP_reg0";
121 case 0x51: return "DW_OP_reg1";
122 case 0x52: return "DW_OP_reg2";
123 case 0x53: return "DW_OP_reg3";
124 case 0x54: return "DW_OP_reg4";
125 case 0x55: return "DW_OP_reg5";
126 case 0x56: return "DW_OP_reg6";
127 case 0x57: return "DW_OP_reg7";
128 case 0x58: return "DW_OP_reg8";
129 case 0x59: return "DW_OP_reg9";
130 case 0x5a: return "DW_OP_reg10";
131 case 0x5b: return "DW_OP_reg11";
132 case 0x5c: return "DW_OP_reg12";
133 case 0x5d: return "DW_OP_reg13";
134 case 0x5e: return "DW_OP_reg14";
135 case 0x5f: return "DW_OP_reg15";
136 case 0x60: return "DW_OP_reg16";
137 case 0x61: return "DW_OP_reg17";
138 case 0x62: return "DW_OP_reg18";
139 case 0x63: return "DW_OP_reg19";
140 case 0x64: return "DW_OP_reg20";
141 case 0x65: return "DW_OP_reg21";
142 case 0x66: return "DW_OP_reg22";
143 case 0x67: return "DW_OP_reg23";
144 case 0x68: return "DW_OP_reg24";
145 case 0x69: return "DW_OP_reg25";
146 case 0x6a: return "DW_OP_reg26";
147 case 0x6b: return "DW_OP_reg27";
148 case 0x6c: return "DW_OP_reg28";
149 case 0x6d: return "DW_OP_reg29";
150 case 0x6e: return "DW_OP_reg30";
151 case 0x6f: return "DW_OP_reg31";
152 case 0x70: return "DW_OP_breg0";
153 case 0x71: return "DW_OP_breg1";
154 case 0x72: return "DW_OP_breg2";
155 case 0x73: return "DW_OP_breg3";
156 case 0x74: return "DW_OP_breg4";
157 case 0x75: return "DW_OP_breg5";
158 case 0x76: return "DW_OP_breg6";
159 case 0x77: return "DW_OP_breg7";
160 case 0x78: return "DW_OP_breg8";
161 case 0x79: return "DW_OP_breg9";
162 case 0x7a: return "DW_OP_breg10";
163 case 0x7b: return "DW_OP_breg11";
164 case 0x7c: return "DW_OP_breg12";
165 case 0x7d: return "DW_OP_breg13";
166 case 0x7e: return "DW_OP_breg14";
167 case 0x7f: return "DW_OP_breg15";
168 case 0x80: return "DW_OP_breg16";
169 case 0x81: return "DW_OP_breg17";
170 case 0x82: return "DW_OP_breg18";
171 case 0x83: return "DW_OP_breg19";
172 case 0x84: return "DW_OP_breg20";
173 case 0x85: return "DW_OP_breg21";
174 case 0x86: return "DW_OP_breg22";
175 case 0x87: return "DW_OP_breg23";
176 case 0x88: return "DW_OP_breg24";
177 case 0x89: return "DW_OP_breg25";
178 case 0x8a: return "DW_OP_breg26";
179 case 0x8b: return "DW_OP_breg27";
180 case 0x8c: return "DW_OP_breg28";
181 case 0x8d: return "DW_OP_breg29";
182 case 0x8e: return "DW_OP_breg30";
183 case 0x8f: return "DW_OP_breg31";
184 case 0x90: return "DW_OP_regx";
185 case 0x91: return "DW_OP_fbreg";
186 case 0x92: return "DW_OP_bregx";
187 case 0x93: return "DW_OP_piece";
188 case 0x94: return "DW_OP_deref_size";
189 case 0x95: return "DW_OP_xderef_size";
190 case 0x96: return "DW_OP_nop";
191 case 0x97: return "DW_OP_push_object_address";
192 case 0x98: return "DW_OP_call2";
193 case 0x99: return "DW_OP_call4";
194 case 0x9a: return "DW_OP_call_ref";
195 case DW_OP_APPLE_array_ref: return "DW_OP_APPLE_array_ref";
196 case DW_OP_APPLE_extern: return "DW_OP_APPLE_extern";
197 case DW_OP_APPLE_uninit: return "DW_OP_APPLE_uninit";
198 case DW_OP_APPLE_assign: return "DW_OP_APPLE_assign";
199 case DW_OP_APPLE_address_of: return "DW_OP_APPLE_address_of";
200 case DW_OP_APPLE_value_of: return "DW_OP_APPLE_value_of";
201 case DW_OP_APPLE_deref_type: return "DW_OP_APPLE_deref_type";
202 case DW_OP_APPLE_expr_local: return "DW_OP_APPLE_expr_local";
203 case DW_OP_APPLE_constf: return "DW_OP_APPLE_constf";
204 case DW_OP_APPLE_scalar_cast: return "DW_OP_APPLE_scalar_cast";
205 case DW_OP_APPLE_clang_cast: return "DW_OP_APPLE_clang_cast";
206 case DW_OP_APPLE_clear: return "DW_OP_APPLE_clear";
207 case DW_OP_APPLE_error: return "DW_OP_APPLE_error";
208 default:
209 snprintf (invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val);
210 return invalid;
211 }
212}
213
214
215//----------------------------------------------------------------------
216// DWARFExpression constructor
217//----------------------------------------------------------------------
218DWARFExpression::DWARFExpression() :
219 m_data(),
220 m_reg_kind (eRegisterKindDWARF),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000221 m_loclist_slide (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
223}
224
225DWARFExpression::DWARFExpression(const DWARFExpression& rhs) :
226 m_data(rhs.m_data),
227 m_reg_kind (rhs.m_reg_kind),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000228 m_loclist_slide(rhs.m_loclist_slide)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
230}
231
232
Greg Clayton178710c2010-09-14 02:20:48 +0000233DWARFExpression::DWARFExpression(const DataExtractor& data, uint32_t data_offset, uint32_t data_length) :
Chris Lattner24943d22010-06-08 16:52:24 +0000234 m_data(data, data_offset, data_length),
235 m_reg_kind (eRegisterKindDWARF),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000236 m_loclist_slide(LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000237{
Chris Lattner24943d22010-06-08 16:52:24 +0000238}
239
240//----------------------------------------------------------------------
241// Destructor
242//----------------------------------------------------------------------
243DWARFExpression::~DWARFExpression()
244{
245}
246
247
248bool
249DWARFExpression::IsValid() const
250{
251 return m_data.GetByteSize() > 0;
252}
253
Chris Lattner24943d22010-06-08 16:52:24 +0000254void
Greg Clayton178710c2010-09-14 02:20:48 +0000255DWARFExpression::SetOpcodeData (const DataExtractor& data)
Chris Lattner24943d22010-06-08 16:52:24 +0000256{
257 m_data = data;
Chris Lattner24943d22010-06-08 16:52:24 +0000258}
259
260void
Greg Clayton178710c2010-09-14 02:20:48 +0000261DWARFExpression::SetOpcodeData (const DataExtractor& data, uint32_t data_offset, uint32_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000262{
263 m_data.SetData(data, data_offset, data_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000264}
265
266void
267DWARFExpression::DumpLocation (Stream *s, uint32_t offset, uint32_t length, lldb::DescriptionLevel level) const
268{
269 if (!m_data.ValidOffsetForDataOfSize(offset, length))
270 return;
271 const uint32_t start_offset = offset;
272 const uint32_t end_offset = offset + length;
273 while (m_data.ValidOffset(offset) && offset < end_offset)
274 {
275 const uint32_t op_offset = offset;
276 const uint8_t op = m_data.GetU8(&offset);
277
278 switch (level)
279 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000280 default:
281 break;
282
Chris Lattner24943d22010-06-08 16:52:24 +0000283 case lldb::eDescriptionLevelBrief:
284 if (offset > start_offset)
285 s->PutChar(' ');
286 break;
287
288 case lldb::eDescriptionLevelFull:
289 case lldb::eDescriptionLevelVerbose:
290 if (offset > start_offset)
291 s->EOL();
292 s->Indent();
293 if (level == lldb::eDescriptionLevelFull)
294 break;
295 // Fall through for verbose and print offset and DW_OP prefix..
296 s->Printf("0x%8.8x: %s", op_offset, op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
297 break;
298 }
299
300 switch (op)
301 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000302 case DW_OP_addr: *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") "; break; // 0x03 1 address
303 case DW_OP_deref: *s << "DW_OP_deref"; break; // 0x06
304 case DW_OP_const1u: s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x08 1 1-byte constant
305 case DW_OP_const1s: s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x09 1 1-byte constant
306 case DW_OP_const2u: s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0a 1 2-byte constant
307 case DW_OP_const2s: s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0b 1 2-byte constant
308 case DW_OP_const4u: s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0c 1 4-byte constant
309 case DW_OP_const4s: s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0d 1 4-byte constant
310 case DW_OP_const8u: s->Printf("DW_OP_const8u(0x%16.16llx) ", m_data.GetU64(&offset)); break; // 0x0e 1 8-byte constant
311 case DW_OP_const8s: s->Printf("DW_OP_const8s(0x%16.16llx) ", m_data.GetU64(&offset)); break; // 0x0f 1 8-byte constant
312 case DW_OP_constu: s->Printf("DW_OP_constu(0x%x) ", m_data.GetULEB128(&offset)); break; // 0x10 1 ULEB128 constant
313 case DW_OP_consts: s->Printf("DW_OP_consts(0x%x) ", m_data.GetSLEB128(&offset)); break; // 0x11 1 SLEB128 constant
314 case DW_OP_dup: s->PutCString("DW_OP_dup"); break; // 0x12
315 case DW_OP_drop: s->PutCString("DW_OP_drop"); break; // 0x13
316 case DW_OP_over: s->PutCString("DW_OP_over"); break; // 0x14
317 case DW_OP_pick: s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x15 1 1-byte stack index
318 case DW_OP_swap: s->PutCString("DW_OP_swap"); break; // 0x16
319 case DW_OP_rot: s->PutCString("DW_OP_rot"); break; // 0x17
320 case DW_OP_xderef: s->PutCString("DW_OP_xderef"); break; // 0x18
321 case DW_OP_abs: s->PutCString("DW_OP_abs"); break; // 0x19
322 case DW_OP_and: s->PutCString("DW_OP_and"); break; // 0x1a
323 case DW_OP_div: s->PutCString("DW_OP_div"); break; // 0x1b
324 case DW_OP_minus: s->PutCString("DW_OP_minus"); break; // 0x1c
325 case DW_OP_mod: s->PutCString("DW_OP_mod"); break; // 0x1d
326 case DW_OP_mul: s->PutCString("DW_OP_mul"); break; // 0x1e
327 case DW_OP_neg: s->PutCString("DW_OP_neg"); break; // 0x1f
328 case DW_OP_not: s->PutCString("DW_OP_not"); break; // 0x20
329 case DW_OP_or: s->PutCString("DW_OP_or"); break; // 0x21
330 case DW_OP_plus: s->PutCString("DW_OP_plus"); break; // 0x22
Chris Lattner24943d22010-06-08 16:52:24 +0000331 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
Greg Clayton9b82f862011-07-11 05:12:02 +0000332 s->Printf("DW_OP_plus_uconst(0x%x) ", m_data.GetULEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000333 break;
334
Greg Clayton9b82f862011-07-11 05:12:02 +0000335 case DW_OP_shl: s->PutCString("DW_OP_shl"); break; // 0x24
336 case DW_OP_shr: s->PutCString("DW_OP_shr"); break; // 0x25
337 case DW_OP_shra: s->PutCString("DW_OP_shra"); break; // 0x26
338 case DW_OP_xor: s->PutCString("DW_OP_xor"); break; // 0x27
339 case DW_OP_skip: s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x2f 1 signed 2-byte constant
340 case DW_OP_bra: s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x28 1 signed 2-byte constant
341 case DW_OP_eq: s->PutCString("DW_OP_eq"); break; // 0x29
342 case DW_OP_ge: s->PutCString("DW_OP_ge"); break; // 0x2a
343 case DW_OP_gt: s->PutCString("DW_OP_gt"); break; // 0x2b
344 case DW_OP_le: s->PutCString("DW_OP_le"); break; // 0x2c
345 case DW_OP_lt: s->PutCString("DW_OP_lt"); break; // 0x2d
346 case DW_OP_ne: s->PutCString("DW_OP_ne"); break; // 0x2e
Chris Lattner24943d22010-06-08 16:52:24 +0000347
348 case DW_OP_lit0: // 0x30
349 case DW_OP_lit1: // 0x31
350 case DW_OP_lit2: // 0x32
351 case DW_OP_lit3: // 0x33
352 case DW_OP_lit4: // 0x34
353 case DW_OP_lit5: // 0x35
354 case DW_OP_lit6: // 0x36
355 case DW_OP_lit7: // 0x37
356 case DW_OP_lit8: // 0x38
357 case DW_OP_lit9: // 0x39
358 case DW_OP_lit10: // 0x3A
359 case DW_OP_lit11: // 0x3B
360 case DW_OP_lit12: // 0x3C
361 case DW_OP_lit13: // 0x3D
362 case DW_OP_lit14: // 0x3E
363 case DW_OP_lit15: // 0x3F
364 case DW_OP_lit16: // 0x40
365 case DW_OP_lit17: // 0x41
366 case DW_OP_lit18: // 0x42
367 case DW_OP_lit19: // 0x43
368 case DW_OP_lit20: // 0x44
369 case DW_OP_lit21: // 0x45
370 case DW_OP_lit22: // 0x46
371 case DW_OP_lit23: // 0x47
372 case DW_OP_lit24: // 0x48
373 case DW_OP_lit25: // 0x49
374 case DW_OP_lit26: // 0x4A
375 case DW_OP_lit27: // 0x4B
376 case DW_OP_lit28: // 0x4C
377 case DW_OP_lit29: // 0x4D
378 case DW_OP_lit30: // 0x4E
Greg Clayton9b82f862011-07-11 05:12:02 +0000379 case DW_OP_lit31: s->Printf("DW_OP_lit%i", op - DW_OP_lit0); break; // 0x4f
Chris Lattner24943d22010-06-08 16:52:24 +0000380
381 case DW_OP_reg0: // 0x50
382 case DW_OP_reg1: // 0x51
383 case DW_OP_reg2: // 0x52
384 case DW_OP_reg3: // 0x53
385 case DW_OP_reg4: // 0x54
386 case DW_OP_reg5: // 0x55
387 case DW_OP_reg6: // 0x56
388 case DW_OP_reg7: // 0x57
389 case DW_OP_reg8: // 0x58
390 case DW_OP_reg9: // 0x59
391 case DW_OP_reg10: // 0x5A
392 case DW_OP_reg11: // 0x5B
393 case DW_OP_reg12: // 0x5C
394 case DW_OP_reg13: // 0x5D
395 case DW_OP_reg14: // 0x5E
396 case DW_OP_reg15: // 0x5F
397 case DW_OP_reg16: // 0x60
398 case DW_OP_reg17: // 0x61
399 case DW_OP_reg18: // 0x62
400 case DW_OP_reg19: // 0x63
401 case DW_OP_reg20: // 0x64
402 case DW_OP_reg21: // 0x65
403 case DW_OP_reg22: // 0x66
404 case DW_OP_reg23: // 0x67
405 case DW_OP_reg24: // 0x68
406 case DW_OP_reg25: // 0x69
407 case DW_OP_reg26: // 0x6A
408 case DW_OP_reg27: // 0x6B
409 case DW_OP_reg28: // 0x6C
410 case DW_OP_reg29: // 0x6D
411 case DW_OP_reg30: // 0x6E
Greg Clayton9b82f862011-07-11 05:12:02 +0000412 case DW_OP_reg31: s->Printf("DW_OP_reg%i", op - DW_OP_reg0); break; // 0x6f
Chris Lattner24943d22010-06-08 16:52:24 +0000413
414 case DW_OP_breg0:
415 case DW_OP_breg1:
416 case DW_OP_breg2:
417 case DW_OP_breg3:
418 case DW_OP_breg4:
419 case DW_OP_breg5:
420 case DW_OP_breg6:
421 case DW_OP_breg7:
422 case DW_OP_breg8:
423 case DW_OP_breg9:
424 case DW_OP_breg10:
425 case DW_OP_breg11:
426 case DW_OP_breg12:
427 case DW_OP_breg13:
428 case DW_OP_breg14:
429 case DW_OP_breg15:
430 case DW_OP_breg16:
431 case DW_OP_breg17:
432 case DW_OP_breg18:
433 case DW_OP_breg19:
434 case DW_OP_breg20:
435 case DW_OP_breg21:
436 case DW_OP_breg22:
437 case DW_OP_breg23:
438 case DW_OP_breg24:
439 case DW_OP_breg25:
440 case DW_OP_breg26:
441 case DW_OP_breg27:
442 case DW_OP_breg28:
443 case DW_OP_breg29:
444 case DW_OP_breg30:
Greg Clayton9b82f862011-07-11 05:12:02 +0000445 case DW_OP_breg31: s->Printf("DW_OP_breg%i(0x%x)", op - DW_OP_breg0, m_data.GetULEB128(&offset)); break;
Chris Lattner24943d22010-06-08 16:52:24 +0000446
447 case DW_OP_regx: // 0x90 1 ULEB128 register
Greg Clayton9b82f862011-07-11 05:12:02 +0000448 s->Printf("DW_OP_regx(0x%x)", m_data.GetULEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000449 break;
450 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
Greg Clayton9b82f862011-07-11 05:12:02 +0000451 s->Printf("DW_OP_fbreg(0x%x)",m_data.GetSLEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000452 break;
453 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
Greg Clayton9b82f862011-07-11 05:12:02 +0000454 s->Printf("DW_OP_bregx(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetSLEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000455 break;
456 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
Greg Clayton9b82f862011-07-11 05:12:02 +0000457 s->Printf("DW_OP_piece(0x%x)", m_data.GetULEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000458 break;
459 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
Greg Clayton9b82f862011-07-11 05:12:02 +0000460 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000461 break;
462 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
Greg Clayton9b82f862011-07-11 05:12:02 +0000463 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000464 break;
Greg Clayton9b82f862011-07-11 05:12:02 +0000465 case DW_OP_nop: s->PutCString("DW_OP_nop"); break; // 0x96
466 case DW_OP_push_object_address: s->PutCString("DW_OP_push_object_address"); break; // 0x97 DWARF3
Chris Lattner24943d22010-06-08 16:52:24 +0000467 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
Greg Clayton9b82f862011-07-11 05:12:02 +0000468 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000469 break;
470 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
Greg Clayton9b82f862011-07-11 05:12:02 +0000471 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000472 break;
473 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
Greg Clayton9b82f862011-07-11 05:12:02 +0000474 s->Printf("DW_OP_call_ref(0x%8.8llx)", m_data.GetAddress(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000475 break;
476// case DW_OP_form_tls_address: s << "form_tls_address"; break; // 0x9b DWARF3
477// case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break; // 0x9c DWARF3
478// case DW_OP_bit_piece: // 0x9d DWARF3 2
Greg Clayton9b82f862011-07-11 05:12:02 +0000479// s->Printf("DW_OP_bit_piece(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000480// break;
Greg Clayton9b82f862011-07-11 05:12:02 +0000481// case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break; // 0xe0
482// case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break; // 0xff
Chris Lattner24943d22010-06-08 16:52:24 +0000483 case DW_OP_APPLE_extern:
Greg Clayton9b82f862011-07-11 05:12:02 +0000484 s->Printf("DW_OP_APPLE_extern(%u)", m_data.GetULEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000485 break;
486 case DW_OP_APPLE_array_ref:
Greg Clayton9b82f862011-07-11 05:12:02 +0000487 s->PutCString("DW_OP_APPLE_array_ref");
Chris Lattner24943d22010-06-08 16:52:24 +0000488 break;
489 case DW_OP_APPLE_uninit:
Greg Clayton9b82f862011-07-11 05:12:02 +0000490 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
Chris Lattner24943d22010-06-08 16:52:24 +0000491 break;
492 case DW_OP_APPLE_assign: // 0xF1 - pops value off and assigns it to second item on stack (2nd item must have assignable context)
Greg Clayton9b82f862011-07-11 05:12:02 +0000493 s->PutCString("DW_OP_APPLE_assign");
Chris Lattner24943d22010-06-08 16:52:24 +0000494 break;
495 case DW_OP_APPLE_address_of: // 0xF2 - gets the address of the top stack item (top item must be a variable, or have value_type that is an address already)
Greg Clayton9b82f862011-07-11 05:12:02 +0000496 s->PutCString("DW_OP_APPLE_address_of");
Chris Lattner24943d22010-06-08 16:52:24 +0000497 break;
498 case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the stack and pushes the value of that object (top item must be a variable, or expression local)
Greg Clayton9b82f862011-07-11 05:12:02 +0000499 s->PutCString("DW_OP_APPLE_value_of");
Chris Lattner24943d22010-06-08 16:52:24 +0000500 break;
501 case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of the top stack item (top item must be a variable, or a clang type)
Greg Clayton9b82f862011-07-11 05:12:02 +0000502 s->PutCString("DW_OP_APPLE_deref_type");
Chris Lattner24943d22010-06-08 16:52:24 +0000503 break;
504 case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression local index
Greg Clayton9b82f862011-07-11 05:12:02 +0000505 s->Printf("DW_OP_APPLE_expr_local(%u)", m_data.GetULEB128(&offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000506 break;
507 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
508 {
509 uint8_t float_length = m_data.GetU8(&offset);
Greg Clayton9b82f862011-07-11 05:12:02 +0000510 s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000511 m_data.Dump(s, offset, eFormatHex, float_length, 1, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
512 s->PutChar(')');
513 // Consume the float data
514 m_data.GetData(&offset, float_length);
515 }
516 break;
517 case DW_OP_APPLE_scalar_cast:
Greg Clayton9b82f862011-07-11 05:12:02 +0000518 s->Printf("DW_OP_APPLE_scalar_cast(%s)", Scalar::GetValueTypeAsCString ((Scalar::Type)m_data.GetU8(&offset)));
Chris Lattner24943d22010-06-08 16:52:24 +0000519 break;
520 case DW_OP_APPLE_clang_cast:
521 {
522 clang::Type *clang_type = (clang::Type *)m_data.GetMaxU64(&offset, sizeof(void*));
Greg Clayton9b82f862011-07-11 05:12:02 +0000523 s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000524 }
525 break;
526 case DW_OP_APPLE_clear:
Greg Clayton9b82f862011-07-11 05:12:02 +0000527 s->PutCString("DW_OP_APPLE_clear");
Chris Lattner24943d22010-06-08 16:52:24 +0000528 break;
529 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
Greg Clayton9b82f862011-07-11 05:12:02 +0000530 s->PutCString("DW_OP_APPLE_error");
Chris Lattner24943d22010-06-08 16:52:24 +0000531 break;
532 }
533 }
534}
535
536void
Greg Clayton178710c2010-09-14 02:20:48 +0000537DWARFExpression::SetLocationListSlide (addr_t slide)
Chris Lattner24943d22010-06-08 16:52:24 +0000538{
Greg Clayton178710c2010-09-14 02:20:48 +0000539 m_loclist_slide = slide;
Chris Lattner24943d22010-06-08 16:52:24 +0000540}
541
542int
543DWARFExpression::GetRegisterKind ()
544{
545 return m_reg_kind;
546}
547
548void
549DWARFExpression::SetRegisterKind (int reg_kind)
550{
551 m_reg_kind = reg_kind;
552}
553
554bool
555DWARFExpression::IsLocationList() const
556{
Greg Clayton178710c2010-09-14 02:20:48 +0000557 return m_loclist_slide != LLDB_INVALID_ADDRESS;
Chris Lattner24943d22010-06-08 16:52:24 +0000558}
559
560void
Greg Clayton178710c2010-09-14 02:20:48 +0000561DWARFExpression::GetDescription (Stream *s, lldb::DescriptionLevel level, addr_t location_list_base_addr) const
Chris Lattner24943d22010-06-08 16:52:24 +0000562{
563 if (IsLocationList())
564 {
565 // We have a location list
566 uint32_t offset = 0;
567 uint32_t count = 0;
Greg Clayton178710c2010-09-14 02:20:48 +0000568 addr_t curr_base_addr = location_list_base_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000569 while (m_data.ValidOffset(offset))
570 {
571 lldb::addr_t begin_addr_offset = m_data.GetAddress(&offset);
572 lldb::addr_t end_addr_offset = m_data.GetAddress(&offset);
573 if (begin_addr_offset < end_addr_offset)
574 {
575 if (count > 0)
576 s->PutCString(", ");
Greg Clayton178710c2010-09-14 02:20:48 +0000577 VMRange addr_range(curr_base_addr + begin_addr_offset, curr_base_addr + end_addr_offset);
578 addr_range.Dump(s, 0, 8);
Chris Lattner24943d22010-06-08 16:52:24 +0000579 s->PutChar('{');
580 uint32_t location_length = m_data.GetU16(&offset);
581 DumpLocation (s, offset, location_length, level);
582 s->PutChar('}');
583 offset += location_length;
584 }
585 else if (begin_addr_offset == 0 && end_addr_offset == 0)
586 {
587 // The end of the location list is marked by both the start and end offset being zero
588 break;
589 }
590 else
591 {
Greg Claytonb3448432011-03-24 21:19:54 +0000592 if ((m_data.GetAddressByteSize() == 4 && (begin_addr_offset == UINT32_MAX)) ||
593 (m_data.GetAddressByteSize() == 8 && (begin_addr_offset == UINT64_MAX)))
Chris Lattner24943d22010-06-08 16:52:24 +0000594 {
Greg Clayton178710c2010-09-14 02:20:48 +0000595 curr_base_addr = end_addr_offset + location_list_base_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000596 // We have a new base address
597 if (count > 0)
598 s->PutCString(", ");
599 *s << "base_addr = " << end_addr_offset;
600 }
601 }
602
603 count++;
604 }
605 }
606 else
607 {
608 // We have a normal location that contains DW_OP location opcodes
609 DumpLocation (s, 0, m_data.GetByteSize(), level);
610 }
611}
612
613static bool
614ReadRegisterValueAsScalar
615(
Greg Clayton061b79d2011-05-09 20:18:18 +0000616 RegisterContext *reg_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000617 uint32_t reg_kind,
618 uint32_t reg_num,
619 Error *error_ptr,
620 Value &value
621)
622{
Greg Clayton061b79d2011-05-09 20:18:18 +0000623 if (reg_ctx == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000624 {
Jason Molenda8e69de42010-11-20 01:28:30 +0000625 if (error_ptr)
626 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000627 }
628 else
629 {
Greg Clayton061b79d2011-05-09 20:18:18 +0000630 uint32_t native_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
Jason Molenda8e69de42010-11-20 01:28:30 +0000631 if (native_reg == LLDB_INVALID_REGNUM)
632 {
633 if (error_ptr)
634 error_ptr->SetErrorStringWithFormat("Unable to convert register kind=%u reg_num=%u to a native register number.\n", reg_kind, reg_num);
635 }
636 else
637 {
Greg Clayton061b79d2011-05-09 20:18:18 +0000638 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg);
639 RegisterValue reg_value;
640 if (reg_ctx->ReadRegister (reg_info, reg_value))
641 {
642 if (reg_value.GetScalarValue(value.GetScalar()))
643 {
644 value.SetValueType (Value::eValueTypeScalar);
Greg Clayton82f07462011-05-30 00:49:24 +0000645 value.SetContext (Value::eContextTypeRegisterInfo,
646 const_cast<RegisterInfo *>(reg_info));
Greg Clayton061b79d2011-05-09 20:18:18 +0000647 if (error_ptr)
648 error_ptr->Clear();
649 return true;
650 }
651 else
652 {
Greg Clayton82f07462011-05-30 00:49:24 +0000653 // If we get this error, then we need to implement a value
654 // buffer in the dwarf expression evaluation function...
Greg Clayton061b79d2011-05-09 20:18:18 +0000655 if (error_ptr)
Greg Clayton82f07462011-05-30 00:49:24 +0000656 error_ptr->SetErrorStringWithFormat ("register %s can't be converted to a scalar value",
657 reg_info->name);
Greg Clayton061b79d2011-05-09 20:18:18 +0000658 }
659 }
660 else
661 {
662 if (error_ptr)
Greg Clayton82f07462011-05-30 00:49:24 +0000663 error_ptr->SetErrorStringWithFormat("register %s is not available", reg_info->name);
Greg Clayton061b79d2011-05-09 20:18:18 +0000664 }
Jason Molenda8e69de42010-11-20 01:28:30 +0000665 }
Chris Lattner24943d22010-06-08 16:52:24 +0000666 }
667 return false;
668}
669
Greg Clayton178710c2010-09-14 02:20:48 +0000670//bool
671//DWARFExpression::LocationListContainsLoadAddress (Process* process, const Address &addr) const
672//{
673// return LocationListContainsLoadAddress(process, addr.GetLoadAddress(process));
674//}
675//
676//bool
677//DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t load_addr) const
678//{
679// if (load_addr == LLDB_INVALID_ADDRESS)
680// return false;
681//
682// if (IsLocationList())
683// {
684// uint32_t offset = 0;
685//
686// addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
687//
688// if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
689// return false;
690//
691// while (m_data.ValidOffset(offset))
692// {
693// // We need to figure out what the value is for the location.
694// addr_t lo_pc = m_data.GetAddress(&offset);
695// addr_t hi_pc = m_data.GetAddress(&offset);
696// if (lo_pc == 0 && hi_pc == 0)
697// break;
698// else
699// {
700// lo_pc += loc_list_base_addr;
701// hi_pc += loc_list_base_addr;
702//
703// if (lo_pc <= load_addr && load_addr < hi_pc)
704// return true;
705//
706// offset += m_data.GetU16(&offset);
707// }
708// }
709// }
710// return false;
711//}
Greg Claytonb04e7a82010-08-24 21:05:24 +0000712
713bool
Greg Clayton178710c2010-09-14 02:20:48 +0000714DWARFExpression::LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const
Greg Claytonb04e7a82010-08-24 21:05:24 +0000715{
Greg Clayton178710c2010-09-14 02:20:48 +0000716 if (addr == LLDB_INVALID_ADDRESS)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000717 return false;
718
Chris Lattner24943d22010-06-08 16:52:24 +0000719 if (IsLocationList())
720 {
721 uint32_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000722
Greg Clayton178710c2010-09-14 02:20:48 +0000723 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000724 return false;
725
726 while (m_data.ValidOffset(offset))
727 {
728 // We need to figure out what the value is for the location.
729 addr_t lo_pc = m_data.GetAddress(&offset);
730 addr_t hi_pc = m_data.GetAddress(&offset);
731 if (lo_pc == 0 && hi_pc == 0)
732 break;
733 else
734 {
Greg Clayton178710c2010-09-14 02:20:48 +0000735 lo_pc += loclist_base_addr - m_loclist_slide;
736 hi_pc += loclist_base_addr - m_loclist_slide;
Chris Lattner24943d22010-06-08 16:52:24 +0000737
Greg Clayton178710c2010-09-14 02:20:48 +0000738 if (lo_pc <= addr && addr < hi_pc)
Chris Lattner24943d22010-06-08 16:52:24 +0000739 return true;
740
741 offset += m_data.GetU16(&offset);
742 }
743 }
744 }
745 return false;
746}
Greg Claytonb04e7a82010-08-24 21:05:24 +0000747
Chris Lattner24943d22010-06-08 16:52:24 +0000748bool
Greg Clayton9b82f862011-07-11 05:12:02 +0000749DWARFExpression::GetLocation (addr_t base_addr, addr_t pc, uint32_t &offset, uint32_t &length)
750{
751 offset = 0;
752 if (!IsLocationList())
753 {
754 length = m_data.GetByteSize();
755 return true;
756 }
757
758 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS)
759 {
760 addr_t curr_base_addr = base_addr;
761
762 while (m_data.ValidOffset(offset))
763 {
764 // We need to figure out what the value is for the location.
765 addr_t lo_pc = m_data.GetAddress(&offset);
766 addr_t hi_pc = m_data.GetAddress(&offset);
767 if (lo_pc == 0 && hi_pc == 0)
768 {
769 break;
770 }
771 else
772 {
773 lo_pc += curr_base_addr - m_loclist_slide;
774 hi_pc += curr_base_addr - m_loclist_slide;
775
776 length = m_data.GetU16(&offset);
777
778 if (length > 0 && lo_pc <= pc && pc < hi_pc)
779 return true;
780
781 offset += length;
782 }
783 }
784 }
785 offset = UINT32_MAX;
786 length = 0;
787 return false;
788}
789
790bool
791DWARFExpression::DumpLocationForAddress (Stream *s,
792 lldb::DescriptionLevel level,
793 addr_t base_addr,
794 addr_t address)
795{
796 uint32_t offset = 0;
797 uint32_t length = 0;
798
799 if (GetLocation (base_addr, address, offset, length))
800 {
801 if (length > 0)
802 {
803 DumpLocation(s, offset, length, level);
804 return true;
805 }
806 }
807 return false;
808}
809
810bool
Chris Lattner24943d22010-06-08 16:52:24 +0000811DWARFExpression::Evaluate
812(
813 ExecutionContextScope *exe_scope,
814 clang::ASTContext *ast_context,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000815 ClangExpressionVariableList *expr_locals,
816 ClangExpressionDeclMap *decl_map,
Greg Clayton178710c2010-09-14 02:20:48 +0000817 lldb::addr_t loclist_base_load_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000818 const Value* initial_value_ptr,
819 Value& result,
820 Error *error_ptr
821) const
822{
823 ExecutionContext exe_ctx (exe_scope);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000824 return Evaluate(&exe_ctx, ast_context, expr_locals, decl_map, NULL, loclist_base_load_addr, initial_value_ptr, result, error_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000825}
826
827bool
828DWARFExpression::Evaluate
829(
830 ExecutionContext *exe_ctx,
831 clang::ASTContext *ast_context,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000832 ClangExpressionVariableList *expr_locals,
833 ClangExpressionDeclMap *decl_map,
Jason Molenda8e69de42010-11-20 01:28:30 +0000834 RegisterContext *reg_ctx,
Greg Clayton178710c2010-09-14 02:20:48 +0000835 lldb::addr_t loclist_base_load_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000836 const Value* initial_value_ptr,
837 Value& result,
838 Error *error_ptr
839) const
840{
841 if (IsLocationList())
842 {
843 uint32_t offset = 0;
Jason Molenda8e69de42010-11-20 01:28:30 +0000844 addr_t pc;
845 if (reg_ctx)
846 pc = reg_ctx->GetPC();
847 else
848 pc = exe_ctx->frame->GetRegisterContext()->GetPC();
Chris Lattner24943d22010-06-08 16:52:24 +0000849
Greg Clayton178710c2010-09-14 02:20:48 +0000850 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000851 {
Greg Clayton178710c2010-09-14 02:20:48 +0000852 if (pc == LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000853 {
Greg Clayton178710c2010-09-14 02:20:48 +0000854 if (error_ptr)
855 error_ptr->SetErrorString("Invalid PC in frame.");
856 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000857 }
Greg Clayton178710c2010-09-14 02:20:48 +0000858
859 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
860
861 while (m_data.ValidOffset(offset))
Chris Lattner24943d22010-06-08 16:52:24 +0000862 {
Greg Clayton178710c2010-09-14 02:20:48 +0000863 // We need to figure out what the value is for the location.
864 addr_t lo_pc = m_data.GetAddress(&offset);
865 addr_t hi_pc = m_data.GetAddress(&offset);
866 if (lo_pc == 0 && hi_pc == 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000867 {
Greg Clayton178710c2010-09-14 02:20:48 +0000868 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000869 }
Greg Clayton178710c2010-09-14 02:20:48 +0000870 else
871 {
872 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
873 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
874
875 uint16_t length = m_data.GetU16(&offset);
876
877 if (length > 0 && lo_pc <= pc && pc < hi_pc)
878 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000879 return DWARFExpression::Evaluate (exe_ctx, ast_context, expr_locals, decl_map, reg_ctx, m_data, offset, length, m_reg_kind, initial_value_ptr, result, error_ptr);
Greg Clayton178710c2010-09-14 02:20:48 +0000880 }
881 offset += length;
882 }
Chris Lattner24943d22010-06-08 16:52:24 +0000883 }
884 }
885 if (error_ptr)
Greg Clayton82f07462011-05-30 00:49:24 +0000886 error_ptr->SetErrorString ("variable not available");
Chris Lattner24943d22010-06-08 16:52:24 +0000887 return false;
888 }
889
890 // Not a location list, just a single expression.
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000891 return DWARFExpression::Evaluate (exe_ctx, ast_context, expr_locals, decl_map, reg_ctx, m_data, 0, m_data.GetByteSize(), m_reg_kind, initial_value_ptr, result, error_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000892}
893
894
895
896bool
897DWARFExpression::Evaluate
898(
899 ExecutionContext *exe_ctx,
900 clang::ASTContext *ast_context,
Chris Lattner24943d22010-06-08 16:52:24 +0000901 ClangExpressionVariableList *expr_locals,
902 ClangExpressionDeclMap *decl_map,
Jason Molenda8e69de42010-11-20 01:28:30 +0000903 RegisterContext *reg_ctx,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000904 const DataExtractor& opcodes,
Chris Lattner24943d22010-06-08 16:52:24 +0000905 const uint32_t opcodes_offset,
906 const uint32_t opcodes_length,
907 const uint32_t reg_kind,
908 const Value* initial_value_ptr,
909 Value& result,
910 Error *error_ptr
911)
912{
913 std::vector<Value> stack;
914
Jason Molenda8e69de42010-11-20 01:28:30 +0000915 if (reg_ctx == NULL && exe_ctx && exe_ctx->frame)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000916 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Jason Molenda8e69de42010-11-20 01:28:30 +0000917
Chris Lattner24943d22010-06-08 16:52:24 +0000918 if (initial_value_ptr)
919 stack.push_back(*initial_value_ptr);
920
921 uint32_t offset = opcodes_offset;
922 const uint32_t end_offset = opcodes_offset + opcodes_length;
923 Value tmp;
924 uint32_t reg_num;
925
926 // Make sure all of the data is available in opcodes.
927 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length))
928 {
929 if (error_ptr)
930 error_ptr->SetErrorString ("Invalid offset and/or length for opcodes buffer.");
931 return false;
932 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000933 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Chris Lattner24943d22010-06-08 16:52:24 +0000934
935
936 while (opcodes.ValidOffset(offset) && offset < end_offset)
937 {
938 const uint32_t op_offset = offset;
939 const uint8_t op = opcodes.GetU8(&offset);
940
941 if (log)
942 {
Chris Lattner24943d22010-06-08 16:52:24 +0000943 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +0000944 log->Printf("Stack before operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +0000945 for (size_t i=0; i<count; ++i)
946 {
947 StreamString new_value;
948 new_value.Printf("[%zu]", i);
949 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +0000950 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000951 }
952 log->Printf("0x%8.8x: %s", op_offset, DW_OP_value_to_name(op));
953 }
954 switch (op)
955 {
956 //----------------------------------------------------------------------
957 // The DW_OP_addr operation has a single operand that encodes a machine
958 // address and whose size is the size of an address on the target machine.
959 //----------------------------------------------------------------------
960 case DW_OP_addr:
Greg Clayton801417e2011-07-07 01:59:51 +0000961 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
Chris Lattner24943d22010-06-08 16:52:24 +0000962 stack.back().SetValueType (Value::eValueTypeFileAddress);
963 break;
964
965 //----------------------------------------------------------------------
966 // The DW_OP_addr_sect_offset4 is used for any location expressions in
967 // shared libraries that have a location like:
968 // DW_OP_addr(0x1000)
969 // If this address resides in a shared library, then this virtual
970 // address won't make sense when it is evaluated in the context of a
971 // running process where shared libraries have been slid. To account for
972 // this, this new address type where we can store the section pointer
973 // and a 4 byte offset.
974 //----------------------------------------------------------------------
975// case DW_OP_addr_sect_offset4:
976// {
977// result_type = eResultTypeFileAddress;
978// lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *));
979// lldb::addr_t sect_offset = opcodes.GetU32(&offset);
980//
981// Address so_addr (sect, sect_offset);
982// lldb::addr_t load_addr = so_addr.GetLoadAddress();
983// if (load_addr != LLDB_INVALID_ADDRESS)
984// {
985// // We successfully resolve a file address to a load
986// // address.
987// stack.push_back(load_addr);
988// break;
989// }
990// else
991// {
992// // We were able
993// if (error_ptr)
994// error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString());
995// return false;
996// }
997// }
998// break;
999
1000 //----------------------------------------------------------------------
1001 // OPCODE: DW_OP_deref
1002 // OPERANDS: none
1003 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1004 // The value retrieved from that address is pushed. The size of the
1005 // data retrieved from the dereferenced address is the size of an
1006 // address on the target machine.
1007 //----------------------------------------------------------------------
1008 case DW_OP_deref:
1009 {
1010 Value::ValueType value_type = stack.back().GetValueType();
1011 switch (value_type)
1012 {
1013 case Value::eValueTypeHostAddress:
1014 {
1015 void *src = (void *)stack.back().GetScalar().ULongLong();
1016 intptr_t ptr;
1017 ::memcpy (&ptr, src, sizeof(void *));
1018 stack.back().GetScalar() = ptr;
1019 stack.back().ClearContext();
1020 }
1021 break;
1022 case Value::eValueTypeLoadAddress:
1023 if (exe_ctx)
1024 {
1025 if (exe_ctx->process)
1026 {
1027 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1028 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1029 uint32_t addr_size = exe_ctx->process->GetAddressByteSize();
1030 Error error;
1031 if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, addr_size, error) == addr_size)
1032 {
1033 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), addr_size);
1034 uint32_t addr_data_offset = 0;
1035 stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1036 stack.back().ClearContext();
1037 }
1038 else
1039 {
1040 if (error_ptr)
1041 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
1042 pointer_addr,
1043 error.AsCString());
1044 return false;
1045 }
1046 }
1047 else
1048 {
1049 if (error_ptr)
1050 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1051 return false;
1052 }
1053 }
1054 else
1055 {
1056 if (error_ptr)
1057 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1058 return false;
1059 }
1060 break;
1061
1062 default:
1063 break;
1064 }
1065
1066 }
1067 break;
1068
1069 //----------------------------------------------------------------------
1070 // OPCODE: DW_OP_deref_size
1071 // OPERANDS: 1
1072 // 1 - uint8_t that specifies the size of the data to dereference.
1073 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1074 // stack entry and treats it as an address. The value retrieved from that
1075 // address is pushed. In the DW_OP_deref_size operation, however, the
1076 // size in bytes of the data retrieved from the dereferenced address is
1077 // specified by the single operand. This operand is a 1-byte unsigned
1078 // integral constant whose value may not be larger than the size of an
1079 // address on the target machine. The data retrieved is zero extended
1080 // to the size of an address on the target machine before being pushed
1081 // on the expression stack.
1082 //----------------------------------------------------------------------
1083 case DW_OP_deref_size:
Jason Molenda8e69de42010-11-20 01:28:30 +00001084 {
1085 uint8_t size = opcodes.GetU8(&offset);
1086 Value::ValueType value_type = stack.back().GetValueType();
1087 switch (value_type)
1088 {
1089 case Value::eValueTypeHostAddress:
1090 {
1091 void *src = (void *)stack.back().GetScalar().ULongLong();
1092 intptr_t ptr;
1093 ::memcpy (&ptr, src, sizeof(void *));
1094 // I can't decide whether the size operand should apply to the bytes in their
1095 // lldb-host endianness or the target endianness.. I doubt this'll ever come up
1096 // but I'll opt for assuming big endian regardless.
1097 switch (size)
1098 {
1099 case 1: ptr = ptr & 0xff; break;
1100 case 2: ptr = ptr & 0xffff; break;
1101 case 3: ptr = ptr & 0xffffff; break;
1102 case 4: ptr = ptr & 0xffffffff; break;
Jason Molendaa99bcaa2010-11-29 21:38:58 +00001103 // the casts are added to work around the case where intptr_t is a 32 bit quantity;
1104 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this program.
1105 case 5: ptr = (intptr_t) ptr & 0xffffffffffULL; break;
1106 case 6: ptr = (intptr_t) ptr & 0xffffffffffffULL; break;
1107 case 7: ptr = (intptr_t) ptr & 0xffffffffffffffULL; break;
Jason Molenda8e69de42010-11-20 01:28:30 +00001108 default: break;
1109 }
1110 stack.back().GetScalar() = ptr;
1111 stack.back().ClearContext();
1112 }
1113 break;
1114 case Value::eValueTypeLoadAddress:
1115 if (exe_ctx)
1116 {
1117 if (exe_ctx->process)
1118 {
1119 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1120 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1121 Error error;
1122 if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size)
1123 {
1124 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), size);
1125 uint32_t addr_data_offset = 0;
1126 switch (size)
1127 {
1128 case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break;
1129 case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break;
1130 case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break;
1131 case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break;
1132 default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1133 }
1134 stack.back().ClearContext();
1135 }
1136 else
1137 {
1138 if (error_ptr)
1139 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
1140 pointer_addr,
1141 error.AsCString());
1142 return false;
1143 }
1144 }
1145 else
1146 {
1147 if (error_ptr)
1148 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1149 return false;
1150 }
1151 }
1152 else
1153 {
1154 if (error_ptr)
1155 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1156 return false;
1157 }
1158 break;
1159
1160 default:
1161 break;
1162 }
1163
1164 }
1165 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001166
1167 //----------------------------------------------------------------------
1168 // OPCODE: DW_OP_xderef_size
1169 // OPERANDS: 1
1170 // 1 - uint8_t that specifies the size of the data to dereference.
1171 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1172 // the top of the stack is treated as an address. The second stack
Greg Clayton33ed1702010-08-24 00:45:41 +00001173 // entry is treated as an "address space identifier" for those
Chris Lattner24943d22010-06-08 16:52:24 +00001174 // architectures that support multiple address spaces. The top two
1175 // stack elements are popped, a data item is retrieved through an
1176 // implementation-defined address calculation and pushed as the new
1177 // stack top. In the DW_OP_xderef_size operation, however, the size in
1178 // bytes of the data retrieved from the dereferenced address is
1179 // specified by the single operand. This operand is a 1-byte unsigned
1180 // integral constant whose value may not be larger than the size of an
1181 // address on the target machine. The data retrieved is zero extended
1182 // to the size of an address on the target machine before being pushed
1183 // on the expression stack.
1184 //----------------------------------------------------------------------
1185 case DW_OP_xderef_size:
1186 if (error_ptr)
1187 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1188 return false;
1189 //----------------------------------------------------------------------
1190 // OPCODE: DW_OP_xderef
1191 // OPERANDS: none
1192 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1193 // the top of the stack is treated as an address. The second stack entry
1194 // is treated as an "address space identifier" for those architectures
1195 // that support multiple address spaces. The top two stack elements are
1196 // popped, a data item is retrieved through an implementation-defined
1197 // address calculation and pushed as the new stack top. The size of the
1198 // data retrieved from the dereferenced address is the size of an address
1199 // on the target machine.
1200 //----------------------------------------------------------------------
1201 case DW_OP_xderef:
1202 if (error_ptr)
1203 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1204 return false;
1205
1206 //----------------------------------------------------------------------
1207 // All DW_OP_constXXX opcodes have a single operand as noted below:
1208 //
1209 // Opcode Operand 1
1210 // --------------- ----------------------------------------------------
1211 // DW_OP_const1u 1-byte unsigned integer constant
1212 // DW_OP_const1s 1-byte signed integer constant
1213 // DW_OP_const2u 2-byte unsigned integer constant
1214 // DW_OP_const2s 2-byte signed integer constant
1215 // DW_OP_const4u 4-byte unsigned integer constant
1216 // DW_OP_const4s 4-byte signed integer constant
1217 // DW_OP_const8u 8-byte unsigned integer constant
1218 // DW_OP_const8s 8-byte signed integer constant
1219 // DW_OP_constu unsigned LEB128 integer constant
1220 // DW_OP_consts signed LEB128 integer constant
1221 //----------------------------------------------------------------------
Greg Clayton801417e2011-07-07 01:59:51 +00001222 case DW_OP_const1u : stack.push_back(Scalar(( uint8_t)opcodes.GetU8 (&offset))); break;
1223 case DW_OP_const1s : stack.push_back(Scalar(( int8_t)opcodes.GetU8 (&offset))); break;
1224 case DW_OP_const2u : stack.push_back(Scalar((uint16_t)opcodes.GetU16 (&offset))); break;
1225 case DW_OP_const2s : stack.push_back(Scalar(( int16_t)opcodes.GetU16 (&offset))); break;
1226 case DW_OP_const4u : stack.push_back(Scalar((uint32_t)opcodes.GetU32 (&offset))); break;
1227 case DW_OP_const4s : stack.push_back(Scalar(( int32_t)opcodes.GetU32 (&offset))); break;
1228 case DW_OP_const8u : stack.push_back(Scalar((uint64_t)opcodes.GetU64 (&offset))); break;
1229 case DW_OP_const8s : stack.push_back(Scalar(( int64_t)opcodes.GetU64 (&offset))); break;
1230 case DW_OP_constu : stack.push_back(Scalar(opcodes.GetULEB128 (&offset))); break;
1231 case DW_OP_consts : stack.push_back(Scalar(opcodes.GetSLEB128 (&offset))); break;
Chris Lattner24943d22010-06-08 16:52:24 +00001232
1233 //----------------------------------------------------------------------
1234 // OPCODE: DW_OP_dup
1235 // OPERANDS: none
1236 // DESCRIPTION: duplicates the value at the top of the stack
1237 //----------------------------------------------------------------------
1238 case DW_OP_dup:
1239 if (stack.empty())
1240 {
1241 if (error_ptr)
1242 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1243 return false;
1244 }
1245 else
1246 stack.push_back(stack.back());
1247 break;
1248
1249 //----------------------------------------------------------------------
1250 // OPCODE: DW_OP_drop
1251 // OPERANDS: none
1252 // DESCRIPTION: pops the value at the top of the stack
1253 //----------------------------------------------------------------------
1254 case DW_OP_drop:
1255 if (stack.empty())
1256 {
1257 if (error_ptr)
1258 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1259 return false;
1260 }
1261 else
1262 stack.pop_back();
1263 break;
1264
1265 //----------------------------------------------------------------------
1266 // OPCODE: DW_OP_over
1267 // OPERANDS: none
1268 // DESCRIPTION: Duplicates the entry currently second in the stack at
1269 // the top of the stack.
1270 //----------------------------------------------------------------------
1271 case DW_OP_over:
1272 if (stack.size() < 2)
1273 {
1274 if (error_ptr)
1275 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1276 return false;
1277 }
1278 else
1279 stack.push_back(stack[stack.size() - 2]);
1280 break;
1281
1282
1283 //----------------------------------------------------------------------
1284 // OPCODE: DW_OP_pick
1285 // OPERANDS: uint8_t index into the current stack
1286 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1287 // inclusive) is pushed on the stack
1288 //----------------------------------------------------------------------
1289 case DW_OP_pick:
1290 {
1291 uint8_t pick_idx = opcodes.GetU8(&offset);
1292 if (pick_idx < stack.size())
1293 stack.push_back(stack[pick_idx]);
1294 else
1295 {
1296 if (error_ptr)
1297 error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1298 return false;
1299 }
1300 }
1301 break;
1302
1303 //----------------------------------------------------------------------
1304 // OPCODE: DW_OP_swap
1305 // OPERANDS: none
1306 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1307 // of the stack becomes the second stack entry, and the second entry
1308 // becomes the top of the stack
1309 //----------------------------------------------------------------------
1310 case DW_OP_swap:
1311 if (stack.size() < 2)
1312 {
1313 if (error_ptr)
1314 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1315 return false;
1316 }
1317 else
1318 {
1319 tmp = stack.back();
1320 stack.back() = stack[stack.size() - 2];
1321 stack[stack.size() - 2] = tmp;
1322 }
1323 break;
1324
1325 //----------------------------------------------------------------------
1326 // OPCODE: DW_OP_rot
1327 // OPERANDS: none
1328 // DESCRIPTION: Rotates the first three stack entries. The entry at
1329 // the top of the stack becomes the third stack entry, the second
1330 // entry becomes the top of the stack, and the third entry becomes
1331 // the second entry.
1332 //----------------------------------------------------------------------
1333 case DW_OP_rot:
1334 if (stack.size() < 3)
1335 {
1336 if (error_ptr)
1337 error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1338 return false;
1339 }
1340 else
1341 {
1342 size_t last_idx = stack.size() - 1;
1343 Value old_top = stack[last_idx];
1344 stack[last_idx] = stack[last_idx - 1];
1345 stack[last_idx - 1] = stack[last_idx - 2];
1346 stack[last_idx - 2] = old_top;
1347 }
1348 break;
1349
1350 //----------------------------------------------------------------------
1351 // OPCODE: DW_OP_abs
1352 // OPERANDS: none
1353 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1354 // value and pushes its absolute value. If the absolute value can not be
1355 // represented, the result is undefined.
1356 //----------------------------------------------------------------------
1357 case DW_OP_abs:
1358 if (stack.empty())
1359 {
1360 if (error_ptr)
1361 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1362 return false;
1363 }
1364 else if (stack.back().ResolveValue(exe_ctx, ast_context).AbsoluteValue() == false)
1365 {
1366 if (error_ptr)
1367 error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1368 return false;
1369 }
1370 break;
1371
1372 //----------------------------------------------------------------------
1373 // OPCODE: DW_OP_and
1374 // OPERANDS: none
1375 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1376 // operation on the two, and pushes the result.
1377 //----------------------------------------------------------------------
1378 case DW_OP_and:
1379 if (stack.size() < 2)
1380 {
1381 if (error_ptr)
1382 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1383 return false;
1384 }
1385 else
1386 {
1387 tmp = stack.back();
1388 stack.pop_back();
1389 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) & tmp.ResolveValue(exe_ctx, ast_context);
1390 }
1391 break;
1392
1393 //----------------------------------------------------------------------
1394 // OPCODE: DW_OP_div
1395 // OPERANDS: none
1396 // DESCRIPTION: pops the top two stack values, divides the former second
1397 // entry by the former top of the stack using signed division, and
1398 // pushes the result.
1399 //----------------------------------------------------------------------
1400 case DW_OP_div:
1401 if (stack.size() < 2)
1402 {
1403 if (error_ptr)
1404 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1405 return false;
1406 }
1407 else
1408 {
1409 tmp = stack.back();
1410 if (tmp.ResolveValue(exe_ctx, ast_context).IsZero())
1411 {
1412 if (error_ptr)
1413 error_ptr->SetErrorString("Divide by zero.");
1414 return false;
1415 }
1416 else
1417 {
1418 stack.pop_back();
1419 stack.back() = stack.back().ResolveValue(exe_ctx, ast_context) / tmp.ResolveValue(exe_ctx, ast_context);
1420 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1421 {
1422 if (error_ptr)
1423 error_ptr->SetErrorString("Divide failed.");
1424 return false;
1425 }
1426 }
1427 }
1428 break;
1429
1430 //----------------------------------------------------------------------
1431 // OPCODE: DW_OP_minus
1432 // OPERANDS: none
1433 // DESCRIPTION: pops the top two stack values, subtracts the former top
1434 // of the stack from the former second entry, and pushes the result.
1435 //----------------------------------------------------------------------
1436 case DW_OP_minus:
1437 if (stack.size() < 2)
1438 {
1439 if (error_ptr)
1440 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1441 return false;
1442 }
1443 else
1444 {
1445 tmp = stack.back();
1446 stack.pop_back();
1447 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) - tmp.ResolveValue(exe_ctx, ast_context);
1448 }
1449 break;
1450
1451 //----------------------------------------------------------------------
1452 // OPCODE: DW_OP_mod
1453 // OPERANDS: none
1454 // DESCRIPTION: pops the top two stack values and pushes the result of
1455 // the calculation: former second stack entry modulo the former top of
1456 // the stack.
1457 //----------------------------------------------------------------------
1458 case DW_OP_mod:
1459 if (stack.size() < 2)
1460 {
1461 if (error_ptr)
1462 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1463 return false;
1464 }
1465 else
1466 {
1467 tmp = stack.back();
1468 stack.pop_back();
1469 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) % tmp.ResolveValue(exe_ctx, ast_context);
1470 }
1471 break;
1472
1473
1474 //----------------------------------------------------------------------
1475 // OPCODE: DW_OP_mul
1476 // OPERANDS: none
1477 // DESCRIPTION: pops the top two stack entries, multiplies them
1478 // together, and pushes the result.
1479 //----------------------------------------------------------------------
1480 case DW_OP_mul:
1481 if (stack.size() < 2)
1482 {
1483 if (error_ptr)
1484 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1485 return false;
1486 }
1487 else
1488 {
1489 tmp = stack.back();
1490 stack.pop_back();
1491 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) * tmp.ResolveValue(exe_ctx, ast_context);
1492 }
1493 break;
1494
1495 //----------------------------------------------------------------------
1496 // OPCODE: DW_OP_neg
1497 // OPERANDS: none
1498 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1499 //----------------------------------------------------------------------
1500 case DW_OP_neg:
1501 if (stack.empty())
1502 {
1503 if (error_ptr)
1504 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1505 return false;
1506 }
1507 else
1508 {
1509 if (stack.back().ResolveValue(exe_ctx, ast_context).UnaryNegate() == false)
1510 {
1511 if (error_ptr)
1512 error_ptr->SetErrorString("Unary negate failed.");
1513 return false;
1514 }
1515 }
1516 break;
1517
1518 //----------------------------------------------------------------------
1519 // OPCODE: DW_OP_not
1520 // OPERANDS: none
1521 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1522 // complement
1523 //----------------------------------------------------------------------
1524 case DW_OP_not:
1525 if (stack.empty())
1526 {
1527 if (error_ptr)
1528 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1529 return false;
1530 }
1531 else
1532 {
1533 if (stack.back().ResolveValue(exe_ctx, ast_context).OnesComplement() == false)
1534 {
1535 if (error_ptr)
1536 error_ptr->SetErrorString("Logical NOT failed.");
1537 return false;
1538 }
1539 }
1540 break;
1541
1542 //----------------------------------------------------------------------
1543 // OPCODE: DW_OP_or
1544 // OPERANDS: none
1545 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1546 // operation on the two, and pushes the result.
1547 //----------------------------------------------------------------------
1548 case DW_OP_or:
1549 if (stack.size() < 2)
1550 {
1551 if (error_ptr)
1552 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1553 return false;
1554 }
1555 else
1556 {
1557 tmp = stack.back();
1558 stack.pop_back();
1559 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) | tmp.ResolveValue(exe_ctx, ast_context);
1560 }
1561 break;
1562
1563 //----------------------------------------------------------------------
1564 // OPCODE: DW_OP_plus
1565 // OPERANDS: none
1566 // DESCRIPTION: pops the top two stack entries, adds them together, and
1567 // pushes the result.
1568 //----------------------------------------------------------------------
1569 case DW_OP_plus:
1570 if (stack.size() < 2)
1571 {
1572 if (error_ptr)
1573 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1574 return false;
1575 }
1576 else
1577 {
1578 tmp = stack.back();
1579 stack.pop_back();
1580 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) + tmp.ResolveValue(exe_ctx, ast_context);
1581 }
1582 break;
1583
1584 //----------------------------------------------------------------------
1585 // OPCODE: DW_OP_plus_uconst
1586 // OPERANDS: none
1587 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1588 // constant operand and pushes the result.
1589 //----------------------------------------------------------------------
1590 case DW_OP_plus_uconst:
1591 if (stack.empty())
1592 {
1593 if (error_ptr)
1594 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1595 return false;
1596 }
1597 else
1598 {
1599 uint32_t uconst_value = opcodes.GetULEB128(&offset);
1600 // Implicit conversion from a UINT to a Scalar...
1601 stack.back().ResolveValue(exe_ctx, ast_context) += uconst_value;
1602 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1603 {
1604 if (error_ptr)
1605 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1606 return false;
1607 }
1608 }
1609 break;
1610
1611 //----------------------------------------------------------------------
1612 // OPCODE: DW_OP_shl
1613 // OPERANDS: none
1614 // DESCRIPTION: pops the top two stack entries, shifts the former
1615 // second entry left by the number of bits specified by the former top
1616 // of the stack, and pushes the result.
1617 //----------------------------------------------------------------------
1618 case DW_OP_shl:
1619 if (stack.size() < 2)
1620 {
1621 if (error_ptr)
1622 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
1623 return false;
1624 }
1625 else
1626 {
1627 tmp = stack.back();
1628 stack.pop_back();
1629 stack.back().ResolveValue(exe_ctx, ast_context) <<= tmp.ResolveValue(exe_ctx, ast_context);
1630 }
1631 break;
1632
1633 //----------------------------------------------------------------------
1634 // OPCODE: DW_OP_shr
1635 // OPERANDS: none
1636 // DESCRIPTION: pops the top two stack entries, shifts the former second
1637 // entry right logically (filling with zero bits) by the number of bits
1638 // specified by the former top of the stack, and pushes the result.
1639 //----------------------------------------------------------------------
1640 case DW_OP_shr:
1641 if (stack.size() < 2)
1642 {
1643 if (error_ptr)
1644 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
1645 return false;
1646 }
1647 else
1648 {
1649 tmp = stack.back();
1650 stack.pop_back();
1651 if (stack.back().ResolveValue(exe_ctx, ast_context).ShiftRightLogical(tmp.ResolveValue(exe_ctx, ast_context)) == false)
1652 {
1653 if (error_ptr)
1654 error_ptr->SetErrorString("DW_OP_shr failed.");
1655 return false;
1656 }
1657 }
1658 break;
1659
1660 //----------------------------------------------------------------------
1661 // OPCODE: DW_OP_shra
1662 // OPERANDS: none
1663 // DESCRIPTION: pops the top two stack entries, shifts the former second
1664 // entry right arithmetically (divide the magnitude by 2, keep the same
1665 // sign for the result) by the number of bits specified by the former
1666 // top of the stack, and pushes the result.
1667 //----------------------------------------------------------------------
1668 case DW_OP_shra:
1669 if (stack.size() < 2)
1670 {
1671 if (error_ptr)
1672 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
1673 return false;
1674 }
1675 else
1676 {
1677 tmp = stack.back();
1678 stack.pop_back();
1679 stack.back().ResolveValue(exe_ctx, ast_context) >>= tmp.ResolveValue(exe_ctx, ast_context);
1680 }
1681 break;
1682
1683 //----------------------------------------------------------------------
1684 // OPCODE: DW_OP_xor
1685 // OPERANDS: none
1686 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1687 // exclusive-or operation on the two, and pushes the result.
1688 //----------------------------------------------------------------------
1689 case DW_OP_xor:
1690 if (stack.size() < 2)
1691 {
1692 if (error_ptr)
1693 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
1694 return false;
1695 }
1696 else
1697 {
1698 tmp = stack.back();
1699 stack.pop_back();
1700 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) ^ tmp.ResolveValue(exe_ctx, ast_context);
1701 }
1702 break;
1703
1704
1705 //----------------------------------------------------------------------
1706 // OPCODE: DW_OP_skip
1707 // OPERANDS: int16_t
1708 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1709 // signed integer constant. The 2-byte constant is the number of bytes
1710 // of the DWARF expression to skip forward or backward from the current
1711 // operation, beginning after the 2-byte constant.
1712 //----------------------------------------------------------------------
1713 case DW_OP_skip:
1714 {
1715 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1716 uint32_t new_offset = offset + skip_offset;
1717 if (new_offset >= opcodes_offset && new_offset < end_offset)
1718 offset = new_offset;
1719 else
1720 {
1721 if (error_ptr)
1722 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1723 return false;
1724 }
1725 }
1726 break;
1727
1728 //----------------------------------------------------------------------
1729 // OPCODE: DW_OP_bra
1730 // OPERANDS: int16_t
1731 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1732 // signed integer constant. This operation pops the top of stack. If
1733 // the value popped is not the constant 0, the 2-byte constant operand
1734 // is the number of bytes of the DWARF expression to skip forward or
1735 // backward from the current operation, beginning after the 2-byte
1736 // constant.
1737 //----------------------------------------------------------------------
1738 case DW_OP_bra:
1739 {
1740 tmp = stack.back();
1741 stack.pop_back();
1742 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1743 Scalar zero(0);
1744 if (tmp.ResolveValue(exe_ctx, ast_context) != zero)
1745 {
1746 uint32_t new_offset = offset + bra_offset;
1747 if (new_offset >= opcodes_offset && new_offset < end_offset)
1748 offset = new_offset;
1749 else
1750 {
1751 if (error_ptr)
1752 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1753 return false;
1754 }
1755 }
1756 }
1757 break;
1758
1759 //----------------------------------------------------------------------
1760 // OPCODE: DW_OP_eq
1761 // OPERANDS: none
1762 // DESCRIPTION: pops the top two stack values, compares using the
1763 // equals (==) operator.
1764 // STACK RESULT: push the constant value 1 onto the stack if the result
1765 // of the operation is true or the constant value 0 if the result of the
1766 // operation is false.
1767 //----------------------------------------------------------------------
1768 case DW_OP_eq:
1769 if (stack.size() < 2)
1770 {
1771 if (error_ptr)
1772 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
1773 return false;
1774 }
1775 else
1776 {
1777 tmp = stack.back();
1778 stack.pop_back();
1779 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) == tmp.ResolveValue(exe_ctx, ast_context);
1780 }
1781 break;
1782
1783 //----------------------------------------------------------------------
1784 // OPCODE: DW_OP_ge
1785 // OPERANDS: none
1786 // DESCRIPTION: pops the top two stack values, compares using the
1787 // greater than or equal to (>=) operator.
1788 // STACK RESULT: push the constant value 1 onto the stack if the result
1789 // of the operation is true or the constant value 0 if the result of the
1790 // operation is false.
1791 //----------------------------------------------------------------------
1792 case DW_OP_ge:
1793 if (stack.size() < 2)
1794 {
1795 if (error_ptr)
1796 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
1797 return false;
1798 }
1799 else
1800 {
1801 tmp = stack.back();
1802 stack.pop_back();
1803 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) >= tmp.ResolveValue(exe_ctx, ast_context);
1804 }
1805 break;
1806
1807 //----------------------------------------------------------------------
1808 // OPCODE: DW_OP_gt
1809 // OPERANDS: none
1810 // DESCRIPTION: pops the top two stack values, compares using the
1811 // greater than (>) operator.
1812 // STACK RESULT: push the constant value 1 onto the stack if the result
1813 // of the operation is true or the constant value 0 if the result of the
1814 // operation is false.
1815 //----------------------------------------------------------------------
1816 case DW_OP_gt:
1817 if (stack.size() < 2)
1818 {
1819 if (error_ptr)
1820 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
1821 return false;
1822 }
1823 else
1824 {
1825 tmp = stack.back();
1826 stack.pop_back();
1827 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) > tmp.ResolveValue(exe_ctx, ast_context);
1828 }
1829 break;
1830
1831 //----------------------------------------------------------------------
1832 // OPCODE: DW_OP_le
1833 // OPERANDS: none
1834 // DESCRIPTION: pops the top two stack values, compares using the
1835 // less than or equal to (<=) operator.
1836 // STACK RESULT: push the constant value 1 onto the stack if the result
1837 // of the operation is true or the constant value 0 if the result of the
1838 // operation is false.
1839 //----------------------------------------------------------------------
1840 case DW_OP_le:
1841 if (stack.size() < 2)
1842 {
1843 if (error_ptr)
1844 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
1845 return false;
1846 }
1847 else
1848 {
1849 tmp = stack.back();
1850 stack.pop_back();
1851 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) <= tmp.ResolveValue(exe_ctx, ast_context);
1852 }
1853 break;
1854
1855 //----------------------------------------------------------------------
1856 // OPCODE: DW_OP_lt
1857 // OPERANDS: none
1858 // DESCRIPTION: pops the top two stack values, compares using the
1859 // less than (<) operator.
1860 // STACK RESULT: push the constant value 1 onto the stack if the result
1861 // of the operation is true or the constant value 0 if the result of the
1862 // operation is false.
1863 //----------------------------------------------------------------------
1864 case DW_OP_lt:
1865 if (stack.size() < 2)
1866 {
1867 if (error_ptr)
1868 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
1869 return false;
1870 }
1871 else
1872 {
1873 tmp = stack.back();
1874 stack.pop_back();
1875 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) < tmp.ResolveValue(exe_ctx, ast_context);
1876 }
1877 break;
1878
1879 //----------------------------------------------------------------------
1880 // OPCODE: DW_OP_ne
1881 // OPERANDS: none
1882 // DESCRIPTION: pops the top two stack values, compares using the
1883 // not equal (!=) operator.
1884 // STACK RESULT: push the constant value 1 onto the stack if the result
1885 // of the operation is true or the constant value 0 if the result of the
1886 // operation is false.
1887 //----------------------------------------------------------------------
1888 case DW_OP_ne:
1889 if (stack.size() < 2)
1890 {
1891 if (error_ptr)
1892 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
1893 return false;
1894 }
1895 else
1896 {
1897 tmp = stack.back();
1898 stack.pop_back();
1899 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) != tmp.ResolveValue(exe_ctx, ast_context);
1900 }
1901 break;
1902
1903 //----------------------------------------------------------------------
1904 // OPCODE: DW_OP_litn
1905 // OPERANDS: none
1906 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1907 // STACK RESULT: push the unsigned literal constant value onto the top
1908 // of the stack.
1909 //----------------------------------------------------------------------
1910 case DW_OP_lit0:
1911 case DW_OP_lit1:
1912 case DW_OP_lit2:
1913 case DW_OP_lit3:
1914 case DW_OP_lit4:
1915 case DW_OP_lit5:
1916 case DW_OP_lit6:
1917 case DW_OP_lit7:
1918 case DW_OP_lit8:
1919 case DW_OP_lit9:
1920 case DW_OP_lit10:
1921 case DW_OP_lit11:
1922 case DW_OP_lit12:
1923 case DW_OP_lit13:
1924 case DW_OP_lit14:
1925 case DW_OP_lit15:
1926 case DW_OP_lit16:
1927 case DW_OP_lit17:
1928 case DW_OP_lit18:
1929 case DW_OP_lit19:
1930 case DW_OP_lit20:
1931 case DW_OP_lit21:
1932 case DW_OP_lit22:
1933 case DW_OP_lit23:
1934 case DW_OP_lit24:
1935 case DW_OP_lit25:
1936 case DW_OP_lit26:
1937 case DW_OP_lit27:
1938 case DW_OP_lit28:
1939 case DW_OP_lit29:
1940 case DW_OP_lit30:
1941 case DW_OP_lit31:
Greg Clayton801417e2011-07-07 01:59:51 +00001942 stack.push_back(Scalar(op - DW_OP_lit0));
Chris Lattner24943d22010-06-08 16:52:24 +00001943 break;
1944
1945 //----------------------------------------------------------------------
1946 // OPCODE: DW_OP_regN
1947 // OPERANDS: none
1948 // DESCRIPTION: Push the value in register n on the top of the stack.
1949 //----------------------------------------------------------------------
1950 case DW_OP_reg0:
1951 case DW_OP_reg1:
1952 case DW_OP_reg2:
1953 case DW_OP_reg3:
1954 case DW_OP_reg4:
1955 case DW_OP_reg5:
1956 case DW_OP_reg6:
1957 case DW_OP_reg7:
1958 case DW_OP_reg8:
1959 case DW_OP_reg9:
1960 case DW_OP_reg10:
1961 case DW_OP_reg11:
1962 case DW_OP_reg12:
1963 case DW_OP_reg13:
1964 case DW_OP_reg14:
1965 case DW_OP_reg15:
1966 case DW_OP_reg16:
1967 case DW_OP_reg17:
1968 case DW_OP_reg18:
1969 case DW_OP_reg19:
1970 case DW_OP_reg20:
1971 case DW_OP_reg21:
1972 case DW_OP_reg22:
1973 case DW_OP_reg23:
1974 case DW_OP_reg24:
1975 case DW_OP_reg25:
1976 case DW_OP_reg26:
1977 case DW_OP_reg27:
1978 case DW_OP_reg28:
1979 case DW_OP_reg29:
1980 case DW_OP_reg30:
1981 case DW_OP_reg31:
1982 {
1983 reg_num = op - DW_OP_reg0;
1984
Jason Molenda8e69de42010-11-20 01:28:30 +00001985 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001986 stack.push_back(tmp);
1987 else
1988 return false;
1989 }
1990 break;
1991 //----------------------------------------------------------------------
1992 // OPCODE: DW_OP_regx
1993 // OPERANDS:
1994 // ULEB128 literal operand that encodes the register.
1995 // DESCRIPTION: Push the value in register on the top of the stack.
1996 //----------------------------------------------------------------------
1997 case DW_OP_regx:
1998 {
1999 reg_num = opcodes.GetULEB128(&offset);
Jason Molenda8e69de42010-11-20 01:28:30 +00002000 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00002001 stack.push_back(tmp);
2002 else
2003 return false;
2004 }
2005 break;
2006
2007 //----------------------------------------------------------------------
2008 // OPCODE: DW_OP_bregN
2009 // OPERANDS:
2010 // SLEB128 offset from register N
2011 // DESCRIPTION: Value is in memory at the address specified by register
2012 // N plus an offset.
2013 //----------------------------------------------------------------------
2014 case DW_OP_breg0:
2015 case DW_OP_breg1:
2016 case DW_OP_breg2:
2017 case DW_OP_breg3:
2018 case DW_OP_breg4:
2019 case DW_OP_breg5:
2020 case DW_OP_breg6:
2021 case DW_OP_breg7:
2022 case DW_OP_breg8:
2023 case DW_OP_breg9:
2024 case DW_OP_breg10:
2025 case DW_OP_breg11:
2026 case DW_OP_breg12:
2027 case DW_OP_breg13:
2028 case DW_OP_breg14:
2029 case DW_OP_breg15:
2030 case DW_OP_breg16:
2031 case DW_OP_breg17:
2032 case DW_OP_breg18:
2033 case DW_OP_breg19:
2034 case DW_OP_breg20:
2035 case DW_OP_breg21:
2036 case DW_OP_breg22:
2037 case DW_OP_breg23:
2038 case DW_OP_breg24:
2039 case DW_OP_breg25:
2040 case DW_OP_breg26:
2041 case DW_OP_breg27:
2042 case DW_OP_breg28:
2043 case DW_OP_breg29:
2044 case DW_OP_breg30:
2045 case DW_OP_breg31:
2046 {
2047 reg_num = op - DW_OP_breg0;
2048
Jason Molenda8e69de42010-11-20 01:28:30 +00002049 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00002050 {
2051 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2052 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
2053 stack.push_back(tmp);
2054 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2055 }
2056 else
2057 return false;
2058 }
2059 break;
2060 //----------------------------------------------------------------------
2061 // OPCODE: DW_OP_bregx
2062 // OPERANDS: 2
2063 // ULEB128 literal operand that encodes the register.
2064 // SLEB128 offset from register N
2065 // DESCRIPTION: Value is in memory at the address specified by register
2066 // N plus an offset.
2067 //----------------------------------------------------------------------
2068 case DW_OP_bregx:
2069 {
2070 reg_num = opcodes.GetULEB128(&offset);
2071
Jason Molenda8e69de42010-11-20 01:28:30 +00002072 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00002073 {
2074 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2075 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
2076 stack.push_back(tmp);
2077 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2078 }
2079 else
2080 return false;
2081 }
2082 break;
2083
2084 case DW_OP_fbreg:
2085 if (exe_ctx && exe_ctx->frame)
2086 {
2087 Scalar value;
2088 if (exe_ctx->frame->GetFrameBaseValue(value, error_ptr))
2089 {
2090 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2091 value += fbreg_offset;
2092 stack.push_back(value);
2093 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2094 }
2095 else
2096 return false;
2097 }
2098 else
2099 {
2100 if (error_ptr)
2101 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
2102 return false;
2103 }
2104 break;
2105
2106 //----------------------------------------------------------------------
2107 // OPCODE: DW_OP_nop
2108 // OPERANDS: none
2109 // DESCRIPTION: A place holder. It has no effect on the location stack
2110 // or any of its values.
2111 //----------------------------------------------------------------------
2112 case DW_OP_nop:
2113 break;
2114
2115 //----------------------------------------------------------------------
2116 // OPCODE: DW_OP_piece
2117 // OPERANDS: 1
2118 // ULEB128: byte size of the piece
2119 // DESCRIPTION: The operand describes the size in bytes of the piece of
2120 // the object referenced by the DWARF expression whose result is at the
2121 // top of the stack. If the piece is located in a register, but does not
2122 // occupy the entire register, the placement of the piece within that
2123 // register is defined by the ABI.
2124 //
2125 // Many compilers store a single variable in sets of registers, or store
2126 // a variable partially in memory and partially in registers.
2127 // DW_OP_piece provides a way of describing how large a part of a
2128 // variable a particular DWARF expression refers to.
2129 //----------------------------------------------------------------------
2130 case DW_OP_piece:
2131 if (error_ptr)
2132 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_piece.");
2133 return false;
2134
2135 //----------------------------------------------------------------------
2136 // OPCODE: DW_OP_push_object_address
2137 // OPERANDS: none
2138 // DESCRIPTION: Pushes the address of the object currently being
2139 // evaluated as part of evaluation of a user presented expression.
2140 // This object may correspond to an independent variable described by
2141 // its own DIE or it may be a component of an array, structure, or class
2142 // whose address has been dynamically determined by an earlier step
2143 // during user expression evaluation.
2144 //----------------------------------------------------------------------
2145 case DW_OP_push_object_address:
2146 if (error_ptr)
2147 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address.");
2148 return false;
2149
2150 //----------------------------------------------------------------------
2151 // OPCODE: DW_OP_call2
2152 // OPERANDS:
2153 // uint16_t compile unit relative offset of a DIE
2154 // DESCRIPTION: Performs subroutine calls during evaluation
2155 // of a DWARF expression. The operand is the 2-byte unsigned offset
2156 // of a debugging information entry in the current compilation unit.
2157 //
2158 // Operand interpretation is exactly like that for DW_FORM_ref2.
2159 //
2160 // This operation transfers control of DWARF expression evaluation
2161 // to the DW_AT_location attribute of the referenced DIE. If there is
2162 // no such attribute, then there is no effect. Execution of the DWARF
2163 // expression of a DW_AT_location attribute may add to and/or remove from
2164 // values on the stack. Execution returns to the point following the call
2165 // when the end of the attribute is reached. Values on the stack at the
2166 // time of the call may be used as parameters by the called expression
2167 // and values left on the stack by the called expression may be used as
2168 // return values by prior agreement between the calling and called
2169 // expressions.
2170 //----------------------------------------------------------------------
2171 case DW_OP_call2:
2172 if (error_ptr)
2173 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2174 return false;
2175 //----------------------------------------------------------------------
2176 // OPCODE: DW_OP_call4
2177 // OPERANDS: 1
2178 // uint32_t compile unit relative offset of a DIE
2179 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2180 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2181 // of a debugging information entry in the current compilation unit.
2182 //
2183 // Operand interpretation DW_OP_call4 is exactly like that for
2184 // DW_FORM_ref4.
2185 //
2186 // This operation transfers control of DWARF expression evaluation
2187 // to the DW_AT_location attribute of the referenced DIE. If there is
2188 // no such attribute, then there is no effect. Execution of the DWARF
2189 // expression of a DW_AT_location attribute may add to and/or remove from
2190 // values on the stack. Execution returns to the point following the call
2191 // when the end of the attribute is reached. Values on the stack at the
2192 // time of the call may be used as parameters by the called expression
2193 // and values left on the stack by the called expression may be used as
2194 // return values by prior agreement between the calling and called
2195 // expressions.
2196 //----------------------------------------------------------------------
2197 case DW_OP_call4:
2198 if (error_ptr)
2199 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2200 return false;
2201
2202
2203 //----------------------------------------------------------------------
2204 // OPCODE: DW_OP_call_ref
2205 // OPERANDS:
2206 // uint32_t absolute DIE offset for 32-bit DWARF or a uint64_t
2207 // absolute DIE offset for 64 bit DWARF.
2208 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2209 // expression. Takes a single operand. In the 32-bit DWARF format, the
2210 // operand is a 4-byte unsigned value; in the 64-bit DWARF format, it
2211 // is an 8-byte unsigned value. The operand is used as the offset of a
2212 // debugging information entry in a .debug_info section which may be
2213 // contained in a shared object for executable other than that
2214 // containing the operator. For references from one shared object or
2215 // executable to another, the relocation must be performed by the
2216 // consumer.
2217 //
2218 // Operand interpretation of DW_OP_call_ref is exactly like that for
2219 // DW_FORM_ref_addr.
2220 //
2221 // This operation transfers control of DWARF expression evaluation
2222 // to the DW_AT_location attribute of the referenced DIE. If there is
2223 // no such attribute, then there is no effect. Execution of the DWARF
2224 // expression of a DW_AT_location attribute may add to and/or remove from
2225 // values on the stack. Execution returns to the point following the call
2226 // when the end of the attribute is reached. Values on the stack at the
2227 // time of the call may be used as parameters by the called expression
2228 // and values left on the stack by the called expression may be used as
2229 // return values by prior agreement between the calling and called
2230 // expressions.
2231 //----------------------------------------------------------------------
2232 case DW_OP_call_ref:
2233 if (error_ptr)
2234 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call_ref.");
2235 return false;
2236
2237 //----------------------------------------------------------------------
2238 // OPCODE: DW_OP_APPLE_array_ref
2239 // OPERANDS: none
2240 // DESCRIPTION: Pops a value off the stack and uses it as the array
2241 // index. Pops a second value off the stack and uses it as the array
2242 // itself. Pushes a value onto the stack representing the element of
2243 // the array specified by the index.
2244 //----------------------------------------------------------------------
2245 case DW_OP_APPLE_array_ref:
2246 {
2247 if (stack.size() < 2)
2248 {
2249 if (error_ptr)
2250 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_array_ref.");
2251 return false;
2252 }
2253
2254 Value index_val = stack.back();
2255 stack.pop_back();
2256 Value array_val = stack.back();
2257 stack.pop_back();
2258
2259 Scalar &index_scalar = index_val.ResolveValue(exe_ctx, ast_context);
Greg Clayton381f9682011-04-01 18:14:08 +00002260 int64_t index = index_scalar.SLongLong(LLONG_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00002261
Greg Clayton381f9682011-04-01 18:14:08 +00002262 if (index == LLONG_MAX)
Chris Lattner24943d22010-06-08 16:52:24 +00002263 {
2264 if (error_ptr)
2265 error_ptr->SetErrorString("Invalid array index.");
2266 return false;
2267 }
2268
Greg Clayton6916e352010-11-13 03:52:47 +00002269 if (array_val.GetContextType() != Value::eContextTypeClangType)
Chris Lattner24943d22010-06-08 16:52:24 +00002270 {
2271 if (error_ptr)
2272 error_ptr->SetErrorString("Arrays without Clang types are unhandled at this time.");
2273 return false;
2274 }
2275
2276 if (array_val.GetValueType() != Value::eValueTypeLoadAddress &&
2277 array_val.GetValueType() != Value::eValueTypeHostAddress)
2278 {
2279 if (error_ptr)
2280 error_ptr->SetErrorString("Array must be stored in memory.");
2281 return false;
2282 }
2283
Greg Clayton462d4142010-09-29 01:12:09 +00002284 void *array_type = array_val.GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002285
2286 void *member_type;
2287 uint64_t size = 0;
2288
2289 if ((!ClangASTContext::IsPointerType(array_type, &member_type)) &&
2290 (!ClangASTContext::IsArrayType(array_type, &member_type, &size)))
2291 {
2292 if (error_ptr)
2293 error_ptr->SetErrorString("Array reference from something that is neither a pointer nor an array.");
2294 return false;
2295 }
2296
2297 if (size && (index >= size || index < 0))
2298 {
2299 if (error_ptr)
2300 error_ptr->SetErrorStringWithFormat("Out of bounds array access. %lld is not in [0, %llu]", index, size);
2301 return false;
2302 }
2303
Greg Clayton960d6a42010-08-03 00:35:52 +00002304 uint64_t member_bit_size = ClangASTType::GetClangTypeBitWidth(ast_context, member_type);
2305 uint64_t member_bit_align = ClangASTType::GetTypeBitAlign(ast_context, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002306 uint64_t member_bit_incr = ((member_bit_size + member_bit_align - 1) / member_bit_align) * member_bit_align;
2307 if (member_bit_incr % 8)
2308 {
2309 if (error_ptr)
2310 error_ptr->SetErrorStringWithFormat("Array increment is not byte aligned", index, size);
2311 return false;
2312 }
2313 int64_t member_offset = (int64_t)(member_bit_incr / 8) * index;
2314
2315 Value member;
2316
Greg Clayton6916e352010-11-13 03:52:47 +00002317 member.SetContext(Value::eContextTypeClangType, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002318 member.SetValueType(array_val.GetValueType());
2319
2320 addr_t array_base = (addr_t)array_val.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2321 addr_t member_loc = array_base + member_offset;
2322 member.GetScalar() = (uint64_t)member_loc;
2323
2324 stack.push_back(member);
2325 }
2326 break;
2327
2328 //----------------------------------------------------------------------
2329 // OPCODE: DW_OP_APPLE_uninit
2330 // OPERANDS: none
2331 // DESCRIPTION: Lets us know that the value is currently not initialized
2332 //----------------------------------------------------------------------
2333 case DW_OP_APPLE_uninit:
2334 //return eResultTypeErrorUninitialized;
2335 break; // Ignore this as we have seen cases where this value is incorrectly added
2336
2337 //----------------------------------------------------------------------
2338 // OPCODE: DW_OP_APPLE_assign
2339 // OPERANDS: none
2340 // DESCRIPTION: Pops a value off of the stack and assigns it to the next
2341 // item on the stack which must be something assignable (inferior
2342 // Variable, inferior Type with address, inferior register, or
2343 // expression local variable.
2344 //----------------------------------------------------------------------
2345 case DW_OP_APPLE_assign:
2346 if (stack.size() < 2)
2347 {
2348 if (error_ptr)
2349 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_assign.");
2350 return false;
2351 }
2352 else
2353 {
2354 tmp = stack.back();
2355 stack.pop_back();
2356 Value::ContextType context_type = stack.back().GetContextType();
Greg Claytoncd548032011-02-01 01:31:41 +00002357 StreamString new_value(Stream::eBinary, 4, lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00002358 switch (context_type)
2359 {
Greg Clayton6916e352010-11-13 03:52:47 +00002360 case Value::eContextTypeClangType:
Chris Lattner24943d22010-06-08 16:52:24 +00002361 {
Greg Clayton462d4142010-09-29 01:12:09 +00002362 void *clang_type = stack.back().GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002363
2364 if (ClangASTContext::IsAggregateType (clang_type))
2365 {
2366 Value::ValueType source_value_type = tmp.GetValueType();
2367 Value::ValueType target_value_type = stack.back().GetValueType();
2368
2369 addr_t source_addr = (addr_t)tmp.GetScalar().ULongLong();
2370 addr_t target_addr = (addr_t)stack.back().GetScalar().ULongLong();
2371
Greg Clayton960d6a42010-08-03 00:35:52 +00002372 size_t byte_size = (ClangASTType::GetClangTypeBitWidth(ast_context, clang_type) + 7) / 8;
Chris Lattner24943d22010-06-08 16:52:24 +00002373
2374 switch (source_value_type)
2375 {
Greg Clayton4fdf7602011-03-20 04:57:14 +00002376 case Value::eValueTypeScalar:
2377 case Value::eValueTypeFileAddress:
2378 break;
2379
Chris Lattner24943d22010-06-08 16:52:24 +00002380 case Value::eValueTypeLoadAddress:
2381 switch (target_value_type)
2382 {
2383 case Value::eValueTypeLoadAddress:
2384 {
2385 DataBufferHeap data;
2386 data.SetByteSize(byte_size);
2387
2388 Error error;
2389 if (exe_ctx->process->ReadMemory (source_addr, data.GetBytes(), byte_size, error) != byte_size)
2390 {
2391 if (error_ptr)
2392 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2393 return false;
2394 }
2395
2396 if (exe_ctx->process->WriteMemory (target_addr, data.GetBytes(), byte_size, error) != byte_size)
2397 {
2398 if (error_ptr)
2399 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2400 return false;
2401 }
2402 }
2403 break;
2404 case Value::eValueTypeHostAddress:
Greg Claytoncd548032011-02-01 01:31:41 +00002405 if (exe_ctx->process->GetByteOrder() != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +00002406 {
2407 if (error_ptr)
2408 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2409 return false;
2410 }
2411 else
2412 {
2413 Error error;
2414 if (exe_ctx->process->ReadMemory (source_addr, (uint8_t*)target_addr, byte_size, error) != byte_size)
2415 {
2416 if (error_ptr)
2417 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2418 return false;
2419 }
2420 }
2421 break;
2422 default:
2423 return false;
2424 }
2425 break;
2426 case Value::eValueTypeHostAddress:
2427 switch (target_value_type)
2428 {
2429 case Value::eValueTypeLoadAddress:
Greg Claytoncd548032011-02-01 01:31:41 +00002430 if (exe_ctx->process->GetByteOrder() != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +00002431 {
2432 if (error_ptr)
2433 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2434 return false;
2435 }
2436 else
2437 {
2438 Error error;
2439 if (exe_ctx->process->WriteMemory (target_addr, (uint8_t*)source_addr, byte_size, error) != byte_size)
2440 {
2441 if (error_ptr)
2442 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2443 return false;
2444 }
2445 }
2446 case Value::eValueTypeHostAddress:
2447 memcpy ((uint8_t*)target_addr, (uint8_t*)source_addr, byte_size);
2448 break;
2449 default:
2450 return false;
2451 }
2452 }
2453 }
2454 else
2455 {
Greg Clayton1674b122010-07-21 22:12:05 +00002456 if (!ClangASTType::SetValueFromScalar (ast_context,
2457 clang_type,
2458 tmp.ResolveValue(exe_ctx, ast_context),
2459 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002460 {
2461 if (error_ptr)
2462 error_ptr->SetErrorStringWithFormat ("Couldn't extract a value from an integral type.\n");
2463 return false;
2464 }
2465
2466 Value::ValueType value_type = stack.back().GetValueType();
2467
2468 switch (value_type)
2469 {
2470 case Value::eValueTypeLoadAddress:
2471 case Value::eValueTypeHostAddress:
2472 {
Greg Claytonb3448432011-03-24 21:19:54 +00002473 AddressType address_type = (value_type == Value::eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost);
Chris Lattner24943d22010-06-08 16:52:24 +00002474 lldb::addr_t addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton1674b122010-07-21 22:12:05 +00002475 if (!ClangASTType::WriteToMemory (ast_context,
2476 clang_type,
2477 exe_ctx,
2478 addr,
2479 address_type,
2480 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002481 {
2482 if (error_ptr)
2483 error_ptr->SetErrorStringWithFormat ("Failed to write value to memory at 0x%llx.\n", addr);
2484 return false;
2485 }
2486 }
2487 break;
2488
2489 default:
2490 break;
2491 }
2492 }
2493 }
2494 break;
2495
2496 default:
2497 if (error_ptr)
2498 error_ptr->SetErrorString ("Assign failed.");
2499 return false;
2500 }
2501 }
2502 break;
2503
2504 //----------------------------------------------------------------------
2505 // OPCODE: DW_OP_APPLE_address_of
2506 // OPERANDS: none
2507 // DESCRIPTION: Pops a value off of the stack and pushed its address.
2508 // The top item on the stack must be a variable, or already be a memory
2509 // location.
2510 //----------------------------------------------------------------------
2511 case DW_OP_APPLE_address_of:
2512 if (stack.empty())
2513 {
2514 if (error_ptr)
2515 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_address_of.");
2516 return false;
2517 }
2518 else
2519 {
2520 Value::ValueType value_type = stack.back().GetValueType();
2521 switch (value_type)
2522 {
2523 default:
2524 case Value::eValueTypeScalar: // raw scalar value
2525 if (error_ptr)
2526 error_ptr->SetErrorString("Top stack item isn't a memory based object.");
2527 return false;
2528
2529 case Value::eValueTypeLoadAddress: // load address value
2530 case Value::eValueTypeFileAddress: // file address value
2531 case Value::eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
2532 // Taking the address of an object reduces it to the address
2533 // of the value and removes any extra context it had.
2534 //stack.back().SetValueType(Value::eValueTypeScalar);
2535 stack.back().ClearContext();
2536 break;
2537 }
2538 }
2539 break;
2540
2541 //----------------------------------------------------------------------
2542 // OPCODE: DW_OP_APPLE_value_of
2543 // OPERANDS: none
2544 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2545 // The top item on the stack must be a variable, expression variable.
2546 //----------------------------------------------------------------------
2547 case DW_OP_APPLE_value_of:
2548 if (stack.empty())
2549 {
2550 if (error_ptr)
2551 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_value_of.");
2552 return false;
2553 }
2554 else if (!stack.back().ValueOf(exe_ctx, ast_context))
2555 {
2556 if (error_ptr)
2557 error_ptr->SetErrorString ("Top stack item isn't a valid candidate for DW_OP_APPLE_value_of.");
2558 return false;
2559 }
2560 break;
2561
2562 //----------------------------------------------------------------------
2563 // OPCODE: DW_OP_APPLE_deref_type
2564 // OPERANDS: none
2565 // DESCRIPTION: gets the value pointed to by the top stack item
2566 //----------------------------------------------------------------------
2567 case DW_OP_APPLE_deref_type:
2568 {
2569 if (stack.empty())
2570 {
2571 if (error_ptr)
2572 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_deref_type.");
2573 return false;
2574 }
2575
2576 tmp = stack.back();
2577 stack.pop_back();
2578
Greg Clayton6916e352010-11-13 03:52:47 +00002579 if (tmp.GetContextType() != Value::eContextTypeClangType)
Chris Lattner24943d22010-06-08 16:52:24 +00002580 {
2581 if (error_ptr)
2582 error_ptr->SetErrorString("Item at top of expression stack must have a Clang type");
2583 return false;
2584 }
2585
Greg Clayton462d4142010-09-29 01:12:09 +00002586 void *ptr_type = tmp.GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002587 void *target_type;
2588
2589 if (!ClangASTContext::IsPointerType(ptr_type, &target_type))
2590 {
2591 if (error_ptr)
2592 error_ptr->SetErrorString("Dereferencing a non-pointer type");
2593 return false;
2594 }
2595
2596 // TODO do we want all pointers to be dereferenced as load addresses?
2597 Value::ValueType value_type = tmp.GetValueType();
2598
2599 tmp.ResolveValue(exe_ctx, ast_context);
2600
2601 tmp.SetValueType(value_type);
Greg Clayton6916e352010-11-13 03:52:47 +00002602 tmp.SetContext(Value::eContextTypeClangType, target_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002603
2604 stack.push_back(tmp);
2605 }
2606 break;
2607
2608 //----------------------------------------------------------------------
2609 // OPCODE: DW_OP_APPLE_expr_local
2610 // OPERANDS: ULEB128
2611 // DESCRIPTION: pushes the expression local variable index onto the
2612 // stack and set the appropriate context so we know the stack item is
2613 // an expression local variable index.
2614 //----------------------------------------------------------------------
2615 case DW_OP_APPLE_expr_local:
2616 {
Sean Callanana6223432010-08-20 01:02:30 +00002617 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002618 uint32_t idx = opcodes.GetULEB128(&offset);
2619 if (expr_locals == NULL)
2620 {
2621 if (error_ptr)
2622 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) opcode encountered with no local variable list.\n", idx);
2623 return false;
2624 }
2625 Value *expr_local_variable = expr_locals->GetVariableAtIndex(idx);
2626 if (expr_local_variable == NULL)
2627 {
2628 if (error_ptr)
2629 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) with invalid index %u.\n", idx, idx);
2630 return false;
2631 }
Greg Clayton801417e2011-07-07 01:59:51 +00002632 // The proxy code has been removed. If it is ever re-added, please
2633 // use shared pointers or return by value to avoid possible memory
2634 // leak (there is no leak here, but in general, no returning pointers
2635 // that must be manually freed please.
Chris Lattner24943d22010-06-08 16:52:24 +00002636 Value *proxy = expr_local_variable->CreateProxy();
2637 stack.push_back(*proxy);
2638 delete proxy;
Greg Clayton6916e352010-11-13 03:52:47 +00002639 //stack.back().SetContext (Value::eContextTypeClangType, expr_local_variable->GetClangType());
Sean Callanana6223432010-08-20 01:02:30 +00002640 */
Chris Lattner24943d22010-06-08 16:52:24 +00002641 }
2642 break;
2643
2644 //----------------------------------------------------------------------
2645 // OPCODE: DW_OP_APPLE_extern
2646 // OPERANDS: ULEB128
2647 // DESCRIPTION: pushes a proxy for the extern object index onto the
2648 // stack.
2649 //----------------------------------------------------------------------
2650 case DW_OP_APPLE_extern:
2651 {
Sean Callanan8c127202010-08-23 23:09:38 +00002652 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002653 uint32_t idx = opcodes.GetULEB128(&offset);
2654 if (!decl_map)
2655 {
2656 if (error_ptr)
2657 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) opcode encountered with no decl map.\n", idx);
2658 return false;
2659 }
2660 Value *extern_var = decl_map->GetValueForIndex(idx);
2661 if (!extern_var)
2662 {
2663 if (error_ptr)
2664 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) with invalid index %u.\n", idx, idx);
2665 return false;
2666 }
Greg Clayton801417e2011-07-07 01:59:51 +00002667 // The proxy code has been removed. If it is ever re-added, please
2668 // use shared pointers or return by value to avoid possible memory
2669 // leak (there is no leak here, but in general, no returning pointers
2670 // that must be manually freed please.
Chris Lattner24943d22010-06-08 16:52:24 +00002671 Value *proxy = extern_var->CreateProxy();
2672 stack.push_back(*proxy);
2673 delete proxy;
Sean Callanan8c127202010-08-23 23:09:38 +00002674 */
Chris Lattner24943d22010-06-08 16:52:24 +00002675 }
2676 break;
2677
2678 case DW_OP_APPLE_scalar_cast:
2679 if (stack.empty())
2680 {
2681 if (error_ptr)
2682 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_scalar_cast.");
2683 return false;
2684 }
2685 else
2686 {
2687 // Simple scalar cast
2688 if (!stack.back().ResolveValue(exe_ctx, ast_context).Cast((Scalar::Type)opcodes.GetU8(&offset)))
2689 {
2690 if (error_ptr)
2691 error_ptr->SetErrorString("Cast failed.");
2692 return false;
2693 }
2694 }
2695 break;
2696
2697
2698 case DW_OP_APPLE_clang_cast:
2699 if (stack.empty())
2700 {
2701 if (error_ptr)
2702 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_clang_cast.");
2703 return false;
2704 }
2705 else
2706 {
2707 void *clang_type = (void *)opcodes.GetMaxU64(&offset, sizeof(void*));
Greg Clayton6916e352010-11-13 03:52:47 +00002708 stack.back().SetContext (Value::eContextTypeClangType, clang_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002709 }
2710 break;
2711 //----------------------------------------------------------------------
2712 // OPCODE: DW_OP_APPLE_constf
2713 // OPERANDS: 1 byte float length, followed by that many bytes containing
2714 // the constant float data.
2715 // DESCRIPTION: Push a float value onto the expression stack.
2716 //----------------------------------------------------------------------
2717 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
2718 {
2719 uint8_t float_length = opcodes.GetU8(&offset);
2720 if (sizeof(float) == float_length)
2721 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetFloat (&offset);
2722 else if (sizeof(double) == float_length)
2723 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetDouble (&offset);
2724 else if (sizeof(long double) == float_length)
2725 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetLongDouble (&offset);
2726 else
2727 {
2728 StreamString new_value;
2729 opcodes.Dump(&new_value, offset, eFormatBytes, 1, float_length, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
2730
2731 if (error_ptr)
2732 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_constf(<%u> %s) unsupported float size.\n", float_length, new_value.GetData());
2733 return false;
2734 }
2735 tmp.SetValueType(Value::eValueTypeScalar);
2736 tmp.ClearContext();
2737 stack.push_back(tmp);
2738 }
2739 break;
2740 //----------------------------------------------------------------------
2741 // OPCODE: DW_OP_APPLE_clear
2742 // OPERANDS: none
2743 // DESCRIPTION: Clears the expression stack.
2744 //----------------------------------------------------------------------
2745 case DW_OP_APPLE_clear:
2746 stack.clear();
2747 break;
2748
2749 //----------------------------------------------------------------------
2750 // OPCODE: DW_OP_APPLE_error
2751 // OPERANDS: none
2752 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2753 // The top item on the stack must be a variable, expression variable.
2754 //----------------------------------------------------------------------
2755 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
2756 if (error_ptr)
2757 error_ptr->SetErrorString ("Generic error.");
2758 return false;
2759 }
2760 }
2761
2762 if (stack.empty())
2763 {
2764 if (error_ptr)
2765 error_ptr->SetErrorString ("Stack empty after evaluation.");
2766 return false;
2767 }
2768 else if (log)
2769 {
Chris Lattner24943d22010-06-08 16:52:24 +00002770 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +00002771 log->Printf("Stack after operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +00002772 for (size_t i=0; i<count; ++i)
2773 {
2774 StreamString new_value;
2775 new_value.Printf("[%zu]", i);
2776 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +00002777 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +00002778 }
2779 }
2780
2781 result = stack.back();
2782 return true; // Return true on success
2783}
2784