blob: 77e1581ceaff51750e85fe86572f9250205cc671 [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"
16#include "lldb/Core/StreamString.h"
17#include "lldb/Core/Scalar.h"
18#include "lldb/Core/Value.h"
19
20#include "lldb/Expression/ClangExpressionDeclMap.h"
21#include "lldb/Expression/ClangExpressionVariable.h"
22
23#include "lldb/Host/Host.h"
24
25#include "lldb/lldb-private-log.h"
26
Greg Clayton1674b122010-07-21 22:12:05 +000027#include "lldb/Symbol/ClangASTType.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Symbol/ClangASTContext.h"
29#include "lldb/Symbol/Type.h"
30
31#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
34#include "lldb/Target/StackFrame.h"
35
36using namespace lldb;
37using namespace lldb_private;
38
39const char *
40DW_OP_value_to_name (uint32_t val)
41{
42 static char invalid[100];
43 switch (val) {
44 case 0x03: return "DW_OP_addr";
45 case 0x06: return "DW_OP_deref";
46 case 0x08: return "DW_OP_const1u";
47 case 0x09: return "DW_OP_const1s";
48 case 0x0a: return "DW_OP_const2u";
49 case 0x0b: return "DW_OP_const2s";
50 case 0x0c: return "DW_OP_const4u";
51 case 0x0d: return "DW_OP_const4s";
52 case 0x0e: return "DW_OP_const8u";
53 case 0x0f: return "DW_OP_const8s";
54 case 0x10: return "DW_OP_constu";
55 case 0x11: return "DW_OP_consts";
56 case 0x12: return "DW_OP_dup";
57 case 0x13: return "DW_OP_drop";
58 case 0x14: return "DW_OP_over";
59 case 0x15: return "DW_OP_pick";
60 case 0x16: return "DW_OP_swap";
61 case 0x17: return "DW_OP_rot";
62 case 0x18: return "DW_OP_xderef";
63 case 0x19: return "DW_OP_abs";
64 case 0x1a: return "DW_OP_and";
65 case 0x1b: return "DW_OP_div";
66 case 0x1c: return "DW_OP_minus";
67 case 0x1d: return "DW_OP_mod";
68 case 0x1e: return "DW_OP_mul";
69 case 0x1f: return "DW_OP_neg";
70 case 0x20: return "DW_OP_not";
71 case 0x21: return "DW_OP_or";
72 case 0x22: return "DW_OP_plus";
73 case 0x23: return "DW_OP_plus_uconst";
74 case 0x24: return "DW_OP_shl";
75 case 0x25: return "DW_OP_shr";
76 case 0x26: return "DW_OP_shra";
77 case 0x27: return "DW_OP_xor";
78 case 0x2f: return "DW_OP_skip";
79 case 0x28: return "DW_OP_bra";
80 case 0x29: return "DW_OP_eq";
81 case 0x2a: return "DW_OP_ge";
82 case 0x2b: return "DW_OP_gt";
83 case 0x2c: return "DW_OP_le";
84 case 0x2d: return "DW_OP_lt";
85 case 0x2e: return "DW_OP_ne";
86 case 0x30: return "DW_OP_lit0";
87 case 0x31: return "DW_OP_lit1";
88 case 0x32: return "DW_OP_lit2";
89 case 0x33: return "DW_OP_lit3";
90 case 0x34: return "DW_OP_lit4";
91 case 0x35: return "DW_OP_lit5";
92 case 0x36: return "DW_OP_lit6";
93 case 0x37: return "DW_OP_lit7";
94 case 0x38: return "DW_OP_lit8";
95 case 0x39: return "DW_OP_lit9";
96 case 0x3a: return "DW_OP_lit10";
97 case 0x3b: return "DW_OP_lit11";
98 case 0x3c: return "DW_OP_lit12";
99 case 0x3d: return "DW_OP_lit13";
100 case 0x3e: return "DW_OP_lit14";
101 case 0x3f: return "DW_OP_lit15";
102 case 0x40: return "DW_OP_lit16";
103 case 0x41: return "DW_OP_lit17";
104 case 0x42: return "DW_OP_lit18";
105 case 0x43: return "DW_OP_lit19";
106 case 0x44: return "DW_OP_lit20";
107 case 0x45: return "DW_OP_lit21";
108 case 0x46: return "DW_OP_lit22";
109 case 0x47: return "DW_OP_lit23";
110 case 0x48: return "DW_OP_lit24";
111 case 0x49: return "DW_OP_lit25";
112 case 0x4a: return "DW_OP_lit26";
113 case 0x4b: return "DW_OP_lit27";
114 case 0x4c: return "DW_OP_lit28";
115 case 0x4d: return "DW_OP_lit29";
116 case 0x4e: return "DW_OP_lit30";
117 case 0x4f: return "DW_OP_lit31";
118 case 0x50: return "DW_OP_reg0";
119 case 0x51: return "DW_OP_reg1";
120 case 0x52: return "DW_OP_reg2";
121 case 0x53: return "DW_OP_reg3";
122 case 0x54: return "DW_OP_reg4";
123 case 0x55: return "DW_OP_reg5";
124 case 0x56: return "DW_OP_reg6";
125 case 0x57: return "DW_OP_reg7";
126 case 0x58: return "DW_OP_reg8";
127 case 0x59: return "DW_OP_reg9";
128 case 0x5a: return "DW_OP_reg10";
129 case 0x5b: return "DW_OP_reg11";
130 case 0x5c: return "DW_OP_reg12";
131 case 0x5d: return "DW_OP_reg13";
132 case 0x5e: return "DW_OP_reg14";
133 case 0x5f: return "DW_OP_reg15";
134 case 0x60: return "DW_OP_reg16";
135 case 0x61: return "DW_OP_reg17";
136 case 0x62: return "DW_OP_reg18";
137 case 0x63: return "DW_OP_reg19";
138 case 0x64: return "DW_OP_reg20";
139 case 0x65: return "DW_OP_reg21";
140 case 0x66: return "DW_OP_reg22";
141 case 0x67: return "DW_OP_reg23";
142 case 0x68: return "DW_OP_reg24";
143 case 0x69: return "DW_OP_reg25";
144 case 0x6a: return "DW_OP_reg26";
145 case 0x6b: return "DW_OP_reg27";
146 case 0x6c: return "DW_OP_reg28";
147 case 0x6d: return "DW_OP_reg29";
148 case 0x6e: return "DW_OP_reg30";
149 case 0x6f: return "DW_OP_reg31";
150 case 0x70: return "DW_OP_breg0";
151 case 0x71: return "DW_OP_breg1";
152 case 0x72: return "DW_OP_breg2";
153 case 0x73: return "DW_OP_breg3";
154 case 0x74: return "DW_OP_breg4";
155 case 0x75: return "DW_OP_breg5";
156 case 0x76: return "DW_OP_breg6";
157 case 0x77: return "DW_OP_breg7";
158 case 0x78: return "DW_OP_breg8";
159 case 0x79: return "DW_OP_breg9";
160 case 0x7a: return "DW_OP_breg10";
161 case 0x7b: return "DW_OP_breg11";
162 case 0x7c: return "DW_OP_breg12";
163 case 0x7d: return "DW_OP_breg13";
164 case 0x7e: return "DW_OP_breg14";
165 case 0x7f: return "DW_OP_breg15";
166 case 0x80: return "DW_OP_breg16";
167 case 0x81: return "DW_OP_breg17";
168 case 0x82: return "DW_OP_breg18";
169 case 0x83: return "DW_OP_breg19";
170 case 0x84: return "DW_OP_breg20";
171 case 0x85: return "DW_OP_breg21";
172 case 0x86: return "DW_OP_breg22";
173 case 0x87: return "DW_OP_breg23";
174 case 0x88: return "DW_OP_breg24";
175 case 0x89: return "DW_OP_breg25";
176 case 0x8a: return "DW_OP_breg26";
177 case 0x8b: return "DW_OP_breg27";
178 case 0x8c: return "DW_OP_breg28";
179 case 0x8d: return "DW_OP_breg29";
180 case 0x8e: return "DW_OP_breg30";
181 case 0x8f: return "DW_OP_breg31";
182 case 0x90: return "DW_OP_regx";
183 case 0x91: return "DW_OP_fbreg";
184 case 0x92: return "DW_OP_bregx";
185 case 0x93: return "DW_OP_piece";
186 case 0x94: return "DW_OP_deref_size";
187 case 0x95: return "DW_OP_xderef_size";
188 case 0x96: return "DW_OP_nop";
189 case 0x97: return "DW_OP_push_object_address";
190 case 0x98: return "DW_OP_call2";
191 case 0x99: return "DW_OP_call4";
192 case 0x9a: return "DW_OP_call_ref";
193 case DW_OP_APPLE_array_ref: return "DW_OP_APPLE_array_ref";
194 case DW_OP_APPLE_extern: return "DW_OP_APPLE_extern";
195 case DW_OP_APPLE_uninit: return "DW_OP_APPLE_uninit";
196 case DW_OP_APPLE_assign: return "DW_OP_APPLE_assign";
197 case DW_OP_APPLE_address_of: return "DW_OP_APPLE_address_of";
198 case DW_OP_APPLE_value_of: return "DW_OP_APPLE_value_of";
199 case DW_OP_APPLE_deref_type: return "DW_OP_APPLE_deref_type";
200 case DW_OP_APPLE_expr_local: return "DW_OP_APPLE_expr_local";
201 case DW_OP_APPLE_constf: return "DW_OP_APPLE_constf";
202 case DW_OP_APPLE_scalar_cast: return "DW_OP_APPLE_scalar_cast";
203 case DW_OP_APPLE_clang_cast: return "DW_OP_APPLE_clang_cast";
204 case DW_OP_APPLE_clear: return "DW_OP_APPLE_clear";
205 case DW_OP_APPLE_error: return "DW_OP_APPLE_error";
206 default:
207 snprintf (invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val);
208 return invalid;
209 }
210}
211
212
213//----------------------------------------------------------------------
214// DWARFExpression constructor
215//----------------------------------------------------------------------
216DWARFExpression::DWARFExpression() :
217 m_data(),
218 m_reg_kind (eRegisterKindDWARF),
219 m_loclist_base_addr(),
220 m_expr_locals (NULL),
221 m_decl_map (NULL)
222{
223}
224
225DWARFExpression::DWARFExpression(const DWARFExpression& rhs) :
226 m_data(rhs.m_data),
227 m_reg_kind (rhs.m_reg_kind),
228 m_loclist_base_addr(rhs.m_loclist_base_addr),
229 m_expr_locals (rhs.m_expr_locals),
230 m_decl_map (rhs.m_decl_map)
231{
232}
233
234
235DWARFExpression::DWARFExpression(const DataExtractor& data, uint32_t data_offset, uint32_t data_length, const Address* loclist_base_addr_ptr) :
236 m_data(data, data_offset, data_length),
237 m_reg_kind (eRegisterKindDWARF),
238 m_loclist_base_addr(),
239 m_expr_locals (NULL),
240 m_decl_map (NULL)
241{
242 if (loclist_base_addr_ptr)
243 m_loclist_base_addr = *loclist_base_addr_ptr;
244}
245
246//----------------------------------------------------------------------
247// Destructor
248//----------------------------------------------------------------------
249DWARFExpression::~DWARFExpression()
250{
251}
252
253
254bool
255DWARFExpression::IsValid() const
256{
257 return m_data.GetByteSize() > 0;
258}
259
260
261void
262DWARFExpression::SetExpressionLocalVariableList (ClangExpressionVariableList *locals)
263{
264 m_expr_locals = locals;
265}
266
267void
268DWARFExpression::SetExpressionDeclMap (ClangExpressionDeclMap *decl_map)
269{
270 m_decl_map = decl_map;
271}
272
273void
274DWARFExpression::SetOpcodeData (const DataExtractor& data, const Address* loclist_base_addr_ptr)
275{
276 m_data = data;
277 if (loclist_base_addr_ptr != NULL)
278 m_loclist_base_addr = *loclist_base_addr_ptr;
279 else
280 m_loclist_base_addr.Clear();
281}
282
283void
284DWARFExpression::SetOpcodeData (const DataExtractor& data, uint32_t data_offset, uint32_t data_length, const Address* loclist_base_addr_ptr)
285{
286 m_data.SetData(data, data_offset, data_length);
287 if (loclist_base_addr_ptr != NULL)
288 m_loclist_base_addr = *loclist_base_addr_ptr;
289 else
290 m_loclist_base_addr.Clear();
291}
292
293void
294DWARFExpression::DumpLocation (Stream *s, uint32_t offset, uint32_t length, lldb::DescriptionLevel level) const
295{
296 if (!m_data.ValidOffsetForDataOfSize(offset, length))
297 return;
298 const uint32_t start_offset = offset;
299 const uint32_t end_offset = offset + length;
300 while (m_data.ValidOffset(offset) && offset < end_offset)
301 {
302 const uint32_t op_offset = offset;
303 const uint8_t op = m_data.GetU8(&offset);
304
305 switch (level)
306 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000307 default:
308 break;
309
Chris Lattner24943d22010-06-08 16:52:24 +0000310 case lldb::eDescriptionLevelBrief:
311 if (offset > start_offset)
312 s->PutChar(' ');
313 break;
314
315 case lldb::eDescriptionLevelFull:
316 case lldb::eDescriptionLevelVerbose:
317 if (offset > start_offset)
318 s->EOL();
319 s->Indent();
320 if (level == lldb::eDescriptionLevelFull)
321 break;
322 // Fall through for verbose and print offset and DW_OP prefix..
323 s->Printf("0x%8.8x: %s", op_offset, op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
324 break;
325 }
326
327 switch (op)
328 {
329 case DW_OP_addr: *s << "addr(" << m_data.GetAddress(&offset) << ") "; break; // 0x03 1 address
330 case DW_OP_deref: *s << "deref"; break; // 0x06
331 case DW_OP_const1u: s->Printf("const1u(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x08 1 1-byte constant
332 case DW_OP_const1s: s->Printf("const1s(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x09 1 1-byte constant
333 case DW_OP_const2u: s->Printf("const2u(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0a 1 2-byte constant
334 case DW_OP_const2s: s->Printf("const2s(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0b 1 2-byte constant
335 case DW_OP_const4u: s->Printf("const4u(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0c 1 4-byte constant
336 case DW_OP_const4s: s->Printf("const4s(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0d 1 4-byte constant
337 case DW_OP_const8u: s->Printf("const8u(0x%16.16llx) ", m_data.GetU64(&offset)); break; // 0x0e 1 8-byte constant
338 case DW_OP_const8s: s->Printf("const8s(0x%16.16llx) ", m_data.GetU64(&offset)); break; // 0x0f 1 8-byte constant
339 case DW_OP_constu: s->Printf("constu(0x%x) ", m_data.GetULEB128(&offset)); break; // 0x10 1 ULEB128 constant
340 case DW_OP_consts: s->Printf("consts(0x%x) ", m_data.GetSLEB128(&offset)); break; // 0x11 1 SLEB128 constant
341 case DW_OP_dup: s->PutCString("dup"); break; // 0x12
342 case DW_OP_drop: s->PutCString("drop"); break; // 0x13
343 case DW_OP_over: s->PutCString("over"); break; // 0x14
344 case DW_OP_pick: s->Printf("pick(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x15 1 1-byte stack index
345 case DW_OP_swap: s->PutCString("swap"); break; // 0x16
346 case DW_OP_rot: s->PutCString("rot"); break; // 0x17
347 case DW_OP_xderef: s->PutCString("xderef"); break; // 0x18
348 case DW_OP_abs: s->PutCString("abs"); break; // 0x19
349 case DW_OP_and: s->PutCString("and"); break; // 0x1a
350 case DW_OP_div: s->PutCString("div"); break; // 0x1b
351 case DW_OP_minus: s->PutCString("minus"); break; // 0x1c
352 case DW_OP_mod: s->PutCString("mod"); break; // 0x1d
353 case DW_OP_mul: s->PutCString("mul"); break; // 0x1e
354 case DW_OP_neg: s->PutCString("neg"); break; // 0x1f
355 case DW_OP_not: s->PutCString("not"); break; // 0x20
356 case DW_OP_or: s->PutCString("or"); break; // 0x21
357 case DW_OP_plus: s->PutCString("plus"); break; // 0x22
358 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
359 s->Printf("plus_uconst(0x%x) ", m_data.GetULEB128(&offset));
360 break;
361
362 case DW_OP_shl: s->PutCString("shl"); break; // 0x24
363 case DW_OP_shr: s->PutCString("shr"); break; // 0x25
364 case DW_OP_shra: s->PutCString("shra"); break; // 0x26
365 case DW_OP_xor: s->PutCString("xor"); break; // 0x27
366 case DW_OP_skip: s->Printf("skip(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x2f 1 signed 2-byte constant
367 case DW_OP_bra: s->Printf("bra(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x28 1 signed 2-byte constant
368 case DW_OP_eq: s->PutCString("eq"); break; // 0x29
369 case DW_OP_ge: s->PutCString("ge"); break; // 0x2a
370 case DW_OP_gt: s->PutCString("gt"); break; // 0x2b
371 case DW_OP_le: s->PutCString("le"); break; // 0x2c
372 case DW_OP_lt: s->PutCString("lt"); break; // 0x2d
373 case DW_OP_ne: s->PutCString("ne"); break; // 0x2e
374
375 case DW_OP_lit0: // 0x30
376 case DW_OP_lit1: // 0x31
377 case DW_OP_lit2: // 0x32
378 case DW_OP_lit3: // 0x33
379 case DW_OP_lit4: // 0x34
380 case DW_OP_lit5: // 0x35
381 case DW_OP_lit6: // 0x36
382 case DW_OP_lit7: // 0x37
383 case DW_OP_lit8: // 0x38
384 case DW_OP_lit9: // 0x39
385 case DW_OP_lit10: // 0x3A
386 case DW_OP_lit11: // 0x3B
387 case DW_OP_lit12: // 0x3C
388 case DW_OP_lit13: // 0x3D
389 case DW_OP_lit14: // 0x3E
390 case DW_OP_lit15: // 0x3F
391 case DW_OP_lit16: // 0x40
392 case DW_OP_lit17: // 0x41
393 case DW_OP_lit18: // 0x42
394 case DW_OP_lit19: // 0x43
395 case DW_OP_lit20: // 0x44
396 case DW_OP_lit21: // 0x45
397 case DW_OP_lit22: // 0x46
398 case DW_OP_lit23: // 0x47
399 case DW_OP_lit24: // 0x48
400 case DW_OP_lit25: // 0x49
401 case DW_OP_lit26: // 0x4A
402 case DW_OP_lit27: // 0x4B
403 case DW_OP_lit28: // 0x4C
404 case DW_OP_lit29: // 0x4D
405 case DW_OP_lit30: // 0x4E
406 case DW_OP_lit31: s->Printf("lit%i", op - DW_OP_lit0); break; // 0x4f
407
408 case DW_OP_reg0: // 0x50
409 case DW_OP_reg1: // 0x51
410 case DW_OP_reg2: // 0x52
411 case DW_OP_reg3: // 0x53
412 case DW_OP_reg4: // 0x54
413 case DW_OP_reg5: // 0x55
414 case DW_OP_reg6: // 0x56
415 case DW_OP_reg7: // 0x57
416 case DW_OP_reg8: // 0x58
417 case DW_OP_reg9: // 0x59
418 case DW_OP_reg10: // 0x5A
419 case DW_OP_reg11: // 0x5B
420 case DW_OP_reg12: // 0x5C
421 case DW_OP_reg13: // 0x5D
422 case DW_OP_reg14: // 0x5E
423 case DW_OP_reg15: // 0x5F
424 case DW_OP_reg16: // 0x60
425 case DW_OP_reg17: // 0x61
426 case DW_OP_reg18: // 0x62
427 case DW_OP_reg19: // 0x63
428 case DW_OP_reg20: // 0x64
429 case DW_OP_reg21: // 0x65
430 case DW_OP_reg22: // 0x66
431 case DW_OP_reg23: // 0x67
432 case DW_OP_reg24: // 0x68
433 case DW_OP_reg25: // 0x69
434 case DW_OP_reg26: // 0x6A
435 case DW_OP_reg27: // 0x6B
436 case DW_OP_reg28: // 0x6C
437 case DW_OP_reg29: // 0x6D
438 case DW_OP_reg30: // 0x6E
439 case DW_OP_reg31: s->Printf("reg%i", op - DW_OP_reg0); break; // 0x6f
440
441 case DW_OP_breg0:
442 case DW_OP_breg1:
443 case DW_OP_breg2:
444 case DW_OP_breg3:
445 case DW_OP_breg4:
446 case DW_OP_breg5:
447 case DW_OP_breg6:
448 case DW_OP_breg7:
449 case DW_OP_breg8:
450 case DW_OP_breg9:
451 case DW_OP_breg10:
452 case DW_OP_breg11:
453 case DW_OP_breg12:
454 case DW_OP_breg13:
455 case DW_OP_breg14:
456 case DW_OP_breg15:
457 case DW_OP_breg16:
458 case DW_OP_breg17:
459 case DW_OP_breg18:
460 case DW_OP_breg19:
461 case DW_OP_breg20:
462 case DW_OP_breg21:
463 case DW_OP_breg22:
464 case DW_OP_breg23:
465 case DW_OP_breg24:
466 case DW_OP_breg25:
467 case DW_OP_breg26:
468 case DW_OP_breg27:
469 case DW_OP_breg28:
470 case DW_OP_breg29:
471 case DW_OP_breg30:
472 case DW_OP_breg31: s->Printf("breg%i(0x%x)", op - DW_OP_breg0, m_data.GetULEB128(&offset)); break;
473
474 case DW_OP_regx: // 0x90 1 ULEB128 register
475 s->Printf("regx(0x%x)", m_data.GetULEB128(&offset));
476 break;
477 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
478 s->Printf("fbreg(0x%x)",m_data.GetSLEB128(&offset));
479 break;
480 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
481 s->Printf("bregx(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetSLEB128(&offset));
482 break;
483 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
484 s->Printf("piece(0x%x)", m_data.GetULEB128(&offset));
485 break;
486 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
487 s->Printf("deref_size(0x%2.2x)", m_data.GetU8(&offset));
488 break;
489 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
490 s->Printf("xderef_size(0x%2.2x)", m_data.GetU8(&offset));
491 break;
492 case DW_OP_nop: s->PutCString("nop"); break; // 0x96
493 case DW_OP_push_object_address: s->PutCString("push_object_address"); break; // 0x97 DWARF3
494 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
495 s->Printf("call2(0x%4.4x)", m_data.GetU16(&offset));
496 break;
497 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
498 s->Printf("call4(0x%8.8x)", m_data.GetU32(&offset));
499 break;
500 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
501 s->Printf("call_ref(0x%8.8llx)", m_data.GetAddress(&offset));
502 break;
503// case DW_OP_form_tls_address: s << "form_tls_address"; break; // 0x9b DWARF3
504// case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break; // 0x9c DWARF3
505// case DW_OP_bit_piece: // 0x9d DWARF3 2
506// s->Printf("bit_piece(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
507// break;
508// case DW_OP_lo_user: s->PutCString("lo_user"); break; // 0xe0
509// case DW_OP_hi_user: s->PutCString("hi_user"); break; // 0xff
510 case DW_OP_APPLE_extern:
511 s->Printf("extern(%u)", m_data.GetULEB128(&offset));
512 break;
513 case DW_OP_APPLE_array_ref:
514 s->PutCString("array_ref");
515 break;
516 case DW_OP_APPLE_uninit:
517 s->PutCString("uninit"); // 0xF0
518 break;
519 case DW_OP_APPLE_assign: // 0xF1 - pops value off and assigns it to second item on stack (2nd item must have assignable context)
520 s->PutCString("assign");
521 break;
522 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)
523 s->PutCString("address_of");
524 break;
525 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)
526 s->PutCString("value_of");
527 break;
528 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)
529 s->PutCString("deref_type");
530 break;
531 case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression local index
532 s->Printf("expr_local(%u)", m_data.GetULEB128(&offset));
533 break;
534 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
535 {
536 uint8_t float_length = m_data.GetU8(&offset);
537 s->Printf("constf(<%u> ", float_length);
538 m_data.Dump(s, offset, eFormatHex, float_length, 1, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
539 s->PutChar(')');
540 // Consume the float data
541 m_data.GetData(&offset, float_length);
542 }
543 break;
544 case DW_OP_APPLE_scalar_cast:
545 s->Printf("scalar_cast(%s)", Scalar::GetValueTypeAsCString ((Scalar::Type)m_data.GetU8(&offset)));
546 break;
547 case DW_OP_APPLE_clang_cast:
548 {
549 clang::Type *clang_type = (clang::Type *)m_data.GetMaxU64(&offset, sizeof(void*));
550 s->Printf("clang_cast(%p)", clang_type);
551 }
552 break;
553 case DW_OP_APPLE_clear:
554 s->PutCString("clear");
555 break;
556 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
557 s->PutCString("error");
558 break;
559 }
560 }
561}
562
563void
564DWARFExpression::SetLocationListBaseAddress(Address& base_addr)
565{
566 m_loclist_base_addr = base_addr;
567}
568
569int
570DWARFExpression::GetRegisterKind ()
571{
572 return m_reg_kind;
573}
574
575void
576DWARFExpression::SetRegisterKind (int reg_kind)
577{
578 m_reg_kind = reg_kind;
579}
580
581bool
582DWARFExpression::IsLocationList() const
583{
584 return m_loclist_base_addr.IsSectionOffset();
585}
586
587void
588DWARFExpression::GetDescription (Stream *s, lldb::DescriptionLevel level) const
589{
590 if (IsLocationList())
591 {
592 // We have a location list
593 uint32_t offset = 0;
594 uint32_t count = 0;
595 Address base_addr(m_loclist_base_addr);
596 while (m_data.ValidOffset(offset))
597 {
598 lldb::addr_t begin_addr_offset = m_data.GetAddress(&offset);
599 lldb::addr_t end_addr_offset = m_data.GetAddress(&offset);
600 if (begin_addr_offset < end_addr_offset)
601 {
602 if (count > 0)
603 s->PutCString(", ");
604 AddressRange addr_range(base_addr, end_addr_offset - begin_addr_offset);
605 addr_range.GetBaseAddress().SetOffset(base_addr.GetOffset() + begin_addr_offset);
606 addr_range.Dump (s, NULL, Address::DumpStyleFileAddress);
607 s->PutChar('{');
608 uint32_t location_length = m_data.GetU16(&offset);
609 DumpLocation (s, offset, location_length, level);
610 s->PutChar('}');
611 offset += location_length;
612 }
613 else if (begin_addr_offset == 0 && end_addr_offset == 0)
614 {
615 // The end of the location list is marked by both the start and end offset being zero
616 break;
617 }
618 else
619 {
620 if (m_data.GetAddressByteSize() == 4 && begin_addr_offset == 0xFFFFFFFFull ||
621 m_data.GetAddressByteSize() == 8 && begin_addr_offset == 0xFFFFFFFFFFFFFFFFull)
622 {
623 // We have a new base address
624 if (count > 0)
625 s->PutCString(", ");
626 *s << "base_addr = " << end_addr_offset;
627 }
628 }
629
630 count++;
631 }
632 }
633 else
634 {
635 // We have a normal location that contains DW_OP location opcodes
636 DumpLocation (s, 0, m_data.GetByteSize(), level);
637 }
638}
639
640static bool
641ReadRegisterValueAsScalar
642(
643 ExecutionContext *exe_ctx,
644 uint32_t reg_kind,
645 uint32_t reg_num,
646 Error *error_ptr,
647 Value &value
648)
649{
650 if (exe_ctx && exe_ctx->frame)
651 {
652 RegisterContext *reg_context = exe_ctx->frame->GetRegisterContext();
653
654 if (reg_context == NULL)
655 {
656 if (error_ptr)
657 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
658 }
659 else
660 {
661 uint32_t native_reg = reg_context->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
662 if (native_reg == LLDB_INVALID_REGNUM)
663 {
664 if (error_ptr)
665 error_ptr->SetErrorStringWithFormat("Unable to convert register kind=%u reg_num=%u to a native register number.\n", reg_kind, reg_num);
666 }
667 else
668 {
669 value.SetValueType (Value::eValueTypeScalar);
670 value.SetContext (Value::eContextTypeDCRegisterInfo, const_cast<RegisterInfo *>(reg_context->GetRegisterInfoAtIndex(native_reg)));
671
672 if (reg_context->ReadRegisterValue (native_reg, value.GetScalar()))
673 return true;
674
675 if (error_ptr)
676 error_ptr->SetErrorStringWithFormat("Failed to read register %u.\n", native_reg);
677 }
678 }
679 }
680 else
681 {
682 if (error_ptr)
683 error_ptr->SetErrorStringWithFormat("Invalid frame in execution context.\n");
684 }
685 return false;
686}
687
688bool
689DWARFExpression::LocationListContainsLoadAddress (Process* process, const Address &addr) const
690{
Greg Claytonb04e7a82010-08-24 21:05:24 +0000691 return LocationListContainsLoadAddress(process, addr.GetLoadAddress(process));
692}
693
694bool
695DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t load_addr) const
696{
697 if (load_addr == LLDB_INVALID_ADDRESS)
698 return false;
699
Chris Lattner24943d22010-06-08 16:52:24 +0000700 if (IsLocationList())
701 {
702 uint32_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000703
704 addr_t loc_list_base_addr = m_loclist_base_addr.GetLoadAddress(process);
705
706 if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
707 return false;
708
709 while (m_data.ValidOffset(offset))
710 {
711 // We need to figure out what the value is for the location.
712 addr_t lo_pc = m_data.GetAddress(&offset);
713 addr_t hi_pc = m_data.GetAddress(&offset);
714 if (lo_pc == 0 && hi_pc == 0)
715 break;
716 else
717 {
718 lo_pc += loc_list_base_addr;
719 hi_pc += loc_list_base_addr;
720
721 if (lo_pc <= load_addr && load_addr < hi_pc)
722 return true;
723
724 offset += m_data.GetU16(&offset);
725 }
726 }
727 }
728 return false;
729}
Greg Claytonb04e7a82010-08-24 21:05:24 +0000730
Chris Lattner24943d22010-06-08 16:52:24 +0000731bool
732DWARFExpression::Evaluate
733(
734 ExecutionContextScope *exe_scope,
735 clang::ASTContext *ast_context,
736 const Value* initial_value_ptr,
737 Value& result,
738 Error *error_ptr
739) const
740{
741 ExecutionContext exe_ctx (exe_scope);
742 return Evaluate(&exe_ctx, ast_context, initial_value_ptr, result, error_ptr);
743}
744
745bool
746DWARFExpression::Evaluate
747(
748 ExecutionContext *exe_ctx,
749 clang::ASTContext *ast_context,
750 const Value* initial_value_ptr,
751 Value& result,
752 Error *error_ptr
753) const
754{
755 if (IsLocationList())
756 {
757 uint32_t offset = 0;
Greg Claytonb04e7a82010-08-24 21:05:24 +0000758 addr_t pc = exe_ctx->frame->GetRegisterContext()->GetPC();
Chris Lattner24943d22010-06-08 16:52:24 +0000759
760 if (pc == LLDB_INVALID_ADDRESS)
761 {
762 if (error_ptr)
763 error_ptr->SetErrorString("Invalid PC in frame.");
764 return false;
765 }
766
767 addr_t loc_list_base_addr = m_loclist_base_addr.GetLoadAddress(exe_ctx->process);
768
769 if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
770 {
771 if (error_ptr)
772 error_ptr->SetErrorString("Out of scope.");
773 return false;
774 }
775
776 while (m_data.ValidOffset(offset))
777 {
778 // We need to figure out what the value is for the location.
779 addr_t lo_pc = m_data.GetAddress(&offset);
780 addr_t hi_pc = m_data.GetAddress(&offset);
781 if (lo_pc == 0 && hi_pc == 0)
782 {
783 break;
784 }
785 else
786 {
787 lo_pc += loc_list_base_addr;
788 hi_pc += loc_list_base_addr;
789
790 uint16_t length = m_data.GetU16(&offset);
791
792 if (length > 0 && lo_pc <= pc && pc < hi_pc)
793 {
794 return DWARFExpression::Evaluate (exe_ctx, ast_context, m_data, m_expr_locals, m_decl_map, offset, length, m_reg_kind, initial_value_ptr, result, error_ptr);
795 }
796 offset += length;
797 }
798 }
799 if (error_ptr)
Greg Clayton33ed1702010-08-24 00:45:41 +0000800 error_ptr->SetErrorStringWithFormat("Out of scope.");
Chris Lattner24943d22010-06-08 16:52:24 +0000801 return false;
802 }
803
804 // Not a location list, just a single expression.
805 return DWARFExpression::Evaluate (exe_ctx, ast_context, m_data, m_expr_locals, m_decl_map, 0, m_data.GetByteSize(), m_reg_kind, initial_value_ptr, result, error_ptr);
806}
807
808
809
810bool
811DWARFExpression::Evaluate
812(
813 ExecutionContext *exe_ctx,
814 clang::ASTContext *ast_context,
815 const DataExtractor& opcodes,
816 ClangExpressionVariableList *expr_locals,
817 ClangExpressionDeclMap *decl_map,
818 const uint32_t opcodes_offset,
819 const uint32_t opcodes_length,
820 const uint32_t reg_kind,
821 const Value* initial_value_ptr,
822 Value& result,
823 Error *error_ptr
824)
825{
826 std::vector<Value> stack;
827
828 if (initial_value_ptr)
829 stack.push_back(*initial_value_ptr);
830
831 uint32_t offset = opcodes_offset;
832 const uint32_t end_offset = opcodes_offset + opcodes_length;
833 Value tmp;
834 uint32_t reg_num;
835
836 // Make sure all of the data is available in opcodes.
837 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length))
838 {
839 if (error_ptr)
840 error_ptr->SetErrorString ("Invalid offset and/or length for opcodes buffer.");
841 return false;
842 }
843 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
844
845
846 while (opcodes.ValidOffset(offset) && offset < end_offset)
847 {
848 const uint32_t op_offset = offset;
849 const uint8_t op = opcodes.GetU8(&offset);
850
851 if (log)
852 {
Chris Lattner24943d22010-06-08 16:52:24 +0000853 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +0000854 log->Printf("Stack before operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +0000855 for (size_t i=0; i<count; ++i)
856 {
857 StreamString new_value;
858 new_value.Printf("[%zu]", i);
859 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +0000860 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000861 }
862 log->Printf("0x%8.8x: %s", op_offset, DW_OP_value_to_name(op));
863 }
864 switch (op)
865 {
866 //----------------------------------------------------------------------
867 // The DW_OP_addr operation has a single operand that encodes a machine
868 // address and whose size is the size of an address on the target machine.
869 //----------------------------------------------------------------------
870 case DW_OP_addr:
871 stack.push_back(opcodes.GetAddress(&offset));
872 stack.back().SetValueType (Value::eValueTypeFileAddress);
873 break;
874
875 //----------------------------------------------------------------------
876 // The DW_OP_addr_sect_offset4 is used for any location expressions in
877 // shared libraries that have a location like:
878 // DW_OP_addr(0x1000)
879 // If this address resides in a shared library, then this virtual
880 // address won't make sense when it is evaluated in the context of a
881 // running process where shared libraries have been slid. To account for
882 // this, this new address type where we can store the section pointer
883 // and a 4 byte offset.
884 //----------------------------------------------------------------------
885// case DW_OP_addr_sect_offset4:
886// {
887// result_type = eResultTypeFileAddress;
888// lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *));
889// lldb::addr_t sect_offset = opcodes.GetU32(&offset);
890//
891// Address so_addr (sect, sect_offset);
892// lldb::addr_t load_addr = so_addr.GetLoadAddress();
893// if (load_addr != LLDB_INVALID_ADDRESS)
894// {
895// // We successfully resolve a file address to a load
896// // address.
897// stack.push_back(load_addr);
898// break;
899// }
900// else
901// {
902// // We were able
903// if (error_ptr)
904// error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString());
905// return false;
906// }
907// }
908// break;
909
910 //----------------------------------------------------------------------
911 // OPCODE: DW_OP_deref
912 // OPERANDS: none
913 // DESCRIPTION: Pops the top stack entry and treats it as an address.
914 // The value retrieved from that address is pushed. The size of the
915 // data retrieved from the dereferenced address is the size of an
916 // address on the target machine.
917 //----------------------------------------------------------------------
918 case DW_OP_deref:
919 {
920 Value::ValueType value_type = stack.back().GetValueType();
921 switch (value_type)
922 {
923 case Value::eValueTypeHostAddress:
924 {
925 void *src = (void *)stack.back().GetScalar().ULongLong();
926 intptr_t ptr;
927 ::memcpy (&ptr, src, sizeof(void *));
928 stack.back().GetScalar() = ptr;
929 stack.back().ClearContext();
930 }
931 break;
932 case Value::eValueTypeLoadAddress:
933 if (exe_ctx)
934 {
935 if (exe_ctx->process)
936 {
937 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
938 uint8_t addr_bytes[sizeof(lldb::addr_t)];
939 uint32_t addr_size = exe_ctx->process->GetAddressByteSize();
940 Error error;
941 if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, addr_size, error) == addr_size)
942 {
943 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), addr_size);
944 uint32_t addr_data_offset = 0;
945 stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
946 stack.back().ClearContext();
947 }
948 else
949 {
950 if (error_ptr)
951 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
952 pointer_addr,
953 error.AsCString());
954 return false;
955 }
956 }
957 else
958 {
959 if (error_ptr)
960 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
961 return false;
962 }
963 }
964 else
965 {
966 if (error_ptr)
967 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
968 return false;
969 }
970 break;
971
972 default:
973 break;
974 }
975
976 }
977 break;
978
979 //----------------------------------------------------------------------
980 // OPCODE: DW_OP_deref_size
981 // OPERANDS: 1
982 // 1 - uint8_t that specifies the size of the data to dereference.
983 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
984 // stack entry and treats it as an address. The value retrieved from that
985 // address is pushed. In the DW_OP_deref_size operation, however, the
986 // size in bytes of the data retrieved from the dereferenced address is
987 // specified by the single operand. This operand is a 1-byte unsigned
988 // integral constant whose value may not be larger than the size of an
989 // address on the target machine. The data retrieved is zero extended
990 // to the size of an address on the target machine before being pushed
991 // on the expression stack.
992 //----------------------------------------------------------------------
993 case DW_OP_deref_size:
994 if (error_ptr)
995 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_deref_size.");
996 return false;
997
998 //----------------------------------------------------------------------
999 // OPCODE: DW_OP_xderef_size
1000 // OPERANDS: 1
1001 // 1 - uint8_t that specifies the size of the data to dereference.
1002 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1003 // the top of the stack is treated as an address. The second stack
Greg Clayton33ed1702010-08-24 00:45:41 +00001004 // entry is treated as an "address space identifier" for those
Chris Lattner24943d22010-06-08 16:52:24 +00001005 // architectures that support multiple address spaces. The top two
1006 // stack elements are popped, a data item is retrieved through an
1007 // implementation-defined address calculation and pushed as the new
1008 // stack top. In the DW_OP_xderef_size operation, however, the size in
1009 // bytes of the data retrieved from the dereferenced address is
1010 // specified by the single operand. This operand is a 1-byte unsigned
1011 // integral constant whose value may not be larger than the size of an
1012 // address on the target machine. The data retrieved is zero extended
1013 // to the size of an address on the target machine before being pushed
1014 // on the expression stack.
1015 //----------------------------------------------------------------------
1016 case DW_OP_xderef_size:
1017 if (error_ptr)
1018 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1019 return false;
1020 //----------------------------------------------------------------------
1021 // OPCODE: DW_OP_xderef
1022 // OPERANDS: none
1023 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1024 // the top of the stack is treated as an address. The second stack entry
1025 // is treated as an "address space identifier" for those architectures
1026 // that support multiple address spaces. The top two stack elements are
1027 // popped, a data item is retrieved through an implementation-defined
1028 // address calculation and pushed as the new stack top. The size of the
1029 // data retrieved from the dereferenced address is the size of an address
1030 // on the target machine.
1031 //----------------------------------------------------------------------
1032 case DW_OP_xderef:
1033 if (error_ptr)
1034 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1035 return false;
1036
1037 //----------------------------------------------------------------------
1038 // All DW_OP_constXXX opcodes have a single operand as noted below:
1039 //
1040 // Opcode Operand 1
1041 // --------------- ----------------------------------------------------
1042 // DW_OP_const1u 1-byte unsigned integer constant
1043 // DW_OP_const1s 1-byte signed integer constant
1044 // DW_OP_const2u 2-byte unsigned integer constant
1045 // DW_OP_const2s 2-byte signed integer constant
1046 // DW_OP_const4u 4-byte unsigned integer constant
1047 // DW_OP_const4s 4-byte signed integer constant
1048 // DW_OP_const8u 8-byte unsigned integer constant
1049 // DW_OP_const8s 8-byte signed integer constant
1050 // DW_OP_constu unsigned LEB128 integer constant
1051 // DW_OP_consts signed LEB128 integer constant
1052 //----------------------------------------------------------------------
1053 case DW_OP_const1u : stack.push_back(( uint8_t)opcodes.GetU8(&offset)); break;
1054 case DW_OP_const1s : stack.push_back(( int8_t)opcodes.GetU8(&offset)); break;
1055 case DW_OP_const2u : stack.push_back((uint16_t)opcodes.GetU16(&offset)); break;
1056 case DW_OP_const2s : stack.push_back(( int16_t)opcodes.GetU16(&offset)); break;
1057 case DW_OP_const4u : stack.push_back((uint32_t)opcodes.GetU32(&offset)); break;
1058 case DW_OP_const4s : stack.push_back(( int32_t)opcodes.GetU32(&offset)); break;
1059 case DW_OP_const8u : stack.push_back((uint64_t)opcodes.GetU64(&offset)); break;
1060 case DW_OP_const8s : stack.push_back(( int64_t)opcodes.GetU64(&offset)); break;
1061 case DW_OP_constu : stack.push_back(opcodes.GetULEB128(&offset)); break;
1062 case DW_OP_consts : stack.push_back(opcodes.GetSLEB128(&offset)); break;
1063
1064 //----------------------------------------------------------------------
1065 // OPCODE: DW_OP_dup
1066 // OPERANDS: none
1067 // DESCRIPTION: duplicates the value at the top of the stack
1068 //----------------------------------------------------------------------
1069 case DW_OP_dup:
1070 if (stack.empty())
1071 {
1072 if (error_ptr)
1073 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1074 return false;
1075 }
1076 else
1077 stack.push_back(stack.back());
1078 break;
1079
1080 //----------------------------------------------------------------------
1081 // OPCODE: DW_OP_drop
1082 // OPERANDS: none
1083 // DESCRIPTION: pops the value at the top of the stack
1084 //----------------------------------------------------------------------
1085 case DW_OP_drop:
1086 if (stack.empty())
1087 {
1088 if (error_ptr)
1089 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1090 return false;
1091 }
1092 else
1093 stack.pop_back();
1094 break;
1095
1096 //----------------------------------------------------------------------
1097 // OPCODE: DW_OP_over
1098 // OPERANDS: none
1099 // DESCRIPTION: Duplicates the entry currently second in the stack at
1100 // the top of the stack.
1101 //----------------------------------------------------------------------
1102 case DW_OP_over:
1103 if (stack.size() < 2)
1104 {
1105 if (error_ptr)
1106 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1107 return false;
1108 }
1109 else
1110 stack.push_back(stack[stack.size() - 2]);
1111 break;
1112
1113
1114 //----------------------------------------------------------------------
1115 // OPCODE: DW_OP_pick
1116 // OPERANDS: uint8_t index into the current stack
1117 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1118 // inclusive) is pushed on the stack
1119 //----------------------------------------------------------------------
1120 case DW_OP_pick:
1121 {
1122 uint8_t pick_idx = opcodes.GetU8(&offset);
1123 if (pick_idx < stack.size())
1124 stack.push_back(stack[pick_idx]);
1125 else
1126 {
1127 if (error_ptr)
1128 error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1129 return false;
1130 }
1131 }
1132 break;
1133
1134 //----------------------------------------------------------------------
1135 // OPCODE: DW_OP_swap
1136 // OPERANDS: none
1137 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1138 // of the stack becomes the second stack entry, and the second entry
1139 // becomes the top of the stack
1140 //----------------------------------------------------------------------
1141 case DW_OP_swap:
1142 if (stack.size() < 2)
1143 {
1144 if (error_ptr)
1145 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1146 return false;
1147 }
1148 else
1149 {
1150 tmp = stack.back();
1151 stack.back() = stack[stack.size() - 2];
1152 stack[stack.size() - 2] = tmp;
1153 }
1154 break;
1155
1156 //----------------------------------------------------------------------
1157 // OPCODE: DW_OP_rot
1158 // OPERANDS: none
1159 // DESCRIPTION: Rotates the first three stack entries. The entry at
1160 // the top of the stack becomes the third stack entry, the second
1161 // entry becomes the top of the stack, and the third entry becomes
1162 // the second entry.
1163 //----------------------------------------------------------------------
1164 case DW_OP_rot:
1165 if (stack.size() < 3)
1166 {
1167 if (error_ptr)
1168 error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1169 return false;
1170 }
1171 else
1172 {
1173 size_t last_idx = stack.size() - 1;
1174 Value old_top = stack[last_idx];
1175 stack[last_idx] = stack[last_idx - 1];
1176 stack[last_idx - 1] = stack[last_idx - 2];
1177 stack[last_idx - 2] = old_top;
1178 }
1179 break;
1180
1181 //----------------------------------------------------------------------
1182 // OPCODE: DW_OP_abs
1183 // OPERANDS: none
1184 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1185 // value and pushes its absolute value. If the absolute value can not be
1186 // represented, the result is undefined.
1187 //----------------------------------------------------------------------
1188 case DW_OP_abs:
1189 if (stack.empty())
1190 {
1191 if (error_ptr)
1192 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1193 return false;
1194 }
1195 else if (stack.back().ResolveValue(exe_ctx, ast_context).AbsoluteValue() == false)
1196 {
1197 if (error_ptr)
1198 error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1199 return false;
1200 }
1201 break;
1202
1203 //----------------------------------------------------------------------
1204 // OPCODE: DW_OP_and
1205 // OPERANDS: none
1206 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1207 // operation on the two, and pushes the result.
1208 //----------------------------------------------------------------------
1209 case DW_OP_and:
1210 if (stack.size() < 2)
1211 {
1212 if (error_ptr)
1213 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1214 return false;
1215 }
1216 else
1217 {
1218 tmp = stack.back();
1219 stack.pop_back();
1220 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) & tmp.ResolveValue(exe_ctx, ast_context);
1221 }
1222 break;
1223
1224 //----------------------------------------------------------------------
1225 // OPCODE: DW_OP_div
1226 // OPERANDS: none
1227 // DESCRIPTION: pops the top two stack values, divides the former second
1228 // entry by the former top of the stack using signed division, and
1229 // pushes the result.
1230 //----------------------------------------------------------------------
1231 case DW_OP_div:
1232 if (stack.size() < 2)
1233 {
1234 if (error_ptr)
1235 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1236 return false;
1237 }
1238 else
1239 {
1240 tmp = stack.back();
1241 if (tmp.ResolveValue(exe_ctx, ast_context).IsZero())
1242 {
1243 if (error_ptr)
1244 error_ptr->SetErrorString("Divide by zero.");
1245 return false;
1246 }
1247 else
1248 {
1249 stack.pop_back();
1250 stack.back() = stack.back().ResolveValue(exe_ctx, ast_context) / tmp.ResolveValue(exe_ctx, ast_context);
1251 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1252 {
1253 if (error_ptr)
1254 error_ptr->SetErrorString("Divide failed.");
1255 return false;
1256 }
1257 }
1258 }
1259 break;
1260
1261 //----------------------------------------------------------------------
1262 // OPCODE: DW_OP_minus
1263 // OPERANDS: none
1264 // DESCRIPTION: pops the top two stack values, subtracts the former top
1265 // of the stack from the former second entry, and pushes the result.
1266 //----------------------------------------------------------------------
1267 case DW_OP_minus:
1268 if (stack.size() < 2)
1269 {
1270 if (error_ptr)
1271 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1272 return false;
1273 }
1274 else
1275 {
1276 tmp = stack.back();
1277 stack.pop_back();
1278 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) - tmp.ResolveValue(exe_ctx, ast_context);
1279 }
1280 break;
1281
1282 //----------------------------------------------------------------------
1283 // OPCODE: DW_OP_mod
1284 // OPERANDS: none
1285 // DESCRIPTION: pops the top two stack values and pushes the result of
1286 // the calculation: former second stack entry modulo the former top of
1287 // the stack.
1288 //----------------------------------------------------------------------
1289 case DW_OP_mod:
1290 if (stack.size() < 2)
1291 {
1292 if (error_ptr)
1293 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1294 return false;
1295 }
1296 else
1297 {
1298 tmp = stack.back();
1299 stack.pop_back();
1300 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) % tmp.ResolveValue(exe_ctx, ast_context);
1301 }
1302 break;
1303
1304
1305 //----------------------------------------------------------------------
1306 // OPCODE: DW_OP_mul
1307 // OPERANDS: none
1308 // DESCRIPTION: pops the top two stack entries, multiplies them
1309 // together, and pushes the result.
1310 //----------------------------------------------------------------------
1311 case DW_OP_mul:
1312 if (stack.size() < 2)
1313 {
1314 if (error_ptr)
1315 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1316 return false;
1317 }
1318 else
1319 {
1320 tmp = stack.back();
1321 stack.pop_back();
1322 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) * tmp.ResolveValue(exe_ctx, ast_context);
1323 }
1324 break;
1325
1326 //----------------------------------------------------------------------
1327 // OPCODE: DW_OP_neg
1328 // OPERANDS: none
1329 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1330 //----------------------------------------------------------------------
1331 case DW_OP_neg:
1332 if (stack.empty())
1333 {
1334 if (error_ptr)
1335 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1336 return false;
1337 }
1338 else
1339 {
1340 if (stack.back().ResolveValue(exe_ctx, ast_context).UnaryNegate() == false)
1341 {
1342 if (error_ptr)
1343 error_ptr->SetErrorString("Unary negate failed.");
1344 return false;
1345 }
1346 }
1347 break;
1348
1349 //----------------------------------------------------------------------
1350 // OPCODE: DW_OP_not
1351 // OPERANDS: none
1352 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1353 // complement
1354 //----------------------------------------------------------------------
1355 case DW_OP_not:
1356 if (stack.empty())
1357 {
1358 if (error_ptr)
1359 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1360 return false;
1361 }
1362 else
1363 {
1364 if (stack.back().ResolveValue(exe_ctx, ast_context).OnesComplement() == false)
1365 {
1366 if (error_ptr)
1367 error_ptr->SetErrorString("Logical NOT failed.");
1368 return false;
1369 }
1370 }
1371 break;
1372
1373 //----------------------------------------------------------------------
1374 // OPCODE: DW_OP_or
1375 // OPERANDS: none
1376 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1377 // operation on the two, and pushes the result.
1378 //----------------------------------------------------------------------
1379 case DW_OP_or:
1380 if (stack.size() < 2)
1381 {
1382 if (error_ptr)
1383 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1384 return false;
1385 }
1386 else
1387 {
1388 tmp = stack.back();
1389 stack.pop_back();
1390 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) | tmp.ResolveValue(exe_ctx, ast_context);
1391 }
1392 break;
1393
1394 //----------------------------------------------------------------------
1395 // OPCODE: DW_OP_plus
1396 // OPERANDS: none
1397 // DESCRIPTION: pops the top two stack entries, adds them together, and
1398 // pushes the result.
1399 //----------------------------------------------------------------------
1400 case DW_OP_plus:
1401 if (stack.size() < 2)
1402 {
1403 if (error_ptr)
1404 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1405 return false;
1406 }
1407 else
1408 {
1409 tmp = stack.back();
1410 stack.pop_back();
1411 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) + tmp.ResolveValue(exe_ctx, ast_context);
1412 }
1413 break;
1414
1415 //----------------------------------------------------------------------
1416 // OPCODE: DW_OP_plus_uconst
1417 // OPERANDS: none
1418 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1419 // constant operand and pushes the result.
1420 //----------------------------------------------------------------------
1421 case DW_OP_plus_uconst:
1422 if (stack.empty())
1423 {
1424 if (error_ptr)
1425 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1426 return false;
1427 }
1428 else
1429 {
1430 uint32_t uconst_value = opcodes.GetULEB128(&offset);
1431 // Implicit conversion from a UINT to a Scalar...
1432 stack.back().ResolveValue(exe_ctx, ast_context) += uconst_value;
1433 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1434 {
1435 if (error_ptr)
1436 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1437 return false;
1438 }
1439 }
1440 break;
1441
1442 //----------------------------------------------------------------------
1443 // OPCODE: DW_OP_shl
1444 // OPERANDS: none
1445 // DESCRIPTION: pops the top two stack entries, shifts the former
1446 // second entry left by the number of bits specified by the former top
1447 // of the stack, and pushes the result.
1448 //----------------------------------------------------------------------
1449 case DW_OP_shl:
1450 if (stack.size() < 2)
1451 {
1452 if (error_ptr)
1453 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
1454 return false;
1455 }
1456 else
1457 {
1458 tmp = stack.back();
1459 stack.pop_back();
1460 stack.back().ResolveValue(exe_ctx, ast_context) <<= tmp.ResolveValue(exe_ctx, ast_context);
1461 }
1462 break;
1463
1464 //----------------------------------------------------------------------
1465 // OPCODE: DW_OP_shr
1466 // OPERANDS: none
1467 // DESCRIPTION: pops the top two stack entries, shifts the former second
1468 // entry right logically (filling with zero bits) by the number of bits
1469 // specified by the former top of the stack, and pushes the result.
1470 //----------------------------------------------------------------------
1471 case DW_OP_shr:
1472 if (stack.size() < 2)
1473 {
1474 if (error_ptr)
1475 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
1476 return false;
1477 }
1478 else
1479 {
1480 tmp = stack.back();
1481 stack.pop_back();
1482 if (stack.back().ResolveValue(exe_ctx, ast_context).ShiftRightLogical(tmp.ResolveValue(exe_ctx, ast_context)) == false)
1483 {
1484 if (error_ptr)
1485 error_ptr->SetErrorString("DW_OP_shr failed.");
1486 return false;
1487 }
1488 }
1489 break;
1490
1491 //----------------------------------------------------------------------
1492 // OPCODE: DW_OP_shra
1493 // OPERANDS: none
1494 // DESCRIPTION: pops the top two stack entries, shifts the former second
1495 // entry right arithmetically (divide the magnitude by 2, keep the same
1496 // sign for the result) by the number of bits specified by the former
1497 // top of the stack, and pushes the result.
1498 //----------------------------------------------------------------------
1499 case DW_OP_shra:
1500 if (stack.size() < 2)
1501 {
1502 if (error_ptr)
1503 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
1504 return false;
1505 }
1506 else
1507 {
1508 tmp = stack.back();
1509 stack.pop_back();
1510 stack.back().ResolveValue(exe_ctx, ast_context) >>= tmp.ResolveValue(exe_ctx, ast_context);
1511 }
1512 break;
1513
1514 //----------------------------------------------------------------------
1515 // OPCODE: DW_OP_xor
1516 // OPERANDS: none
1517 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1518 // exclusive-or operation on the two, and pushes the result.
1519 //----------------------------------------------------------------------
1520 case DW_OP_xor:
1521 if (stack.size() < 2)
1522 {
1523 if (error_ptr)
1524 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
1525 return false;
1526 }
1527 else
1528 {
1529 tmp = stack.back();
1530 stack.pop_back();
1531 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) ^ tmp.ResolveValue(exe_ctx, ast_context);
1532 }
1533 break;
1534
1535
1536 //----------------------------------------------------------------------
1537 // OPCODE: DW_OP_skip
1538 // OPERANDS: int16_t
1539 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1540 // signed integer constant. The 2-byte constant is the number of bytes
1541 // of the DWARF expression to skip forward or backward from the current
1542 // operation, beginning after the 2-byte constant.
1543 //----------------------------------------------------------------------
1544 case DW_OP_skip:
1545 {
1546 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1547 uint32_t new_offset = offset + skip_offset;
1548 if (new_offset >= opcodes_offset && new_offset < end_offset)
1549 offset = new_offset;
1550 else
1551 {
1552 if (error_ptr)
1553 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1554 return false;
1555 }
1556 }
1557 break;
1558
1559 //----------------------------------------------------------------------
1560 // OPCODE: DW_OP_bra
1561 // OPERANDS: int16_t
1562 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1563 // signed integer constant. This operation pops the top of stack. If
1564 // the value popped is not the constant 0, the 2-byte constant operand
1565 // is the number of bytes of the DWARF expression to skip forward or
1566 // backward from the current operation, beginning after the 2-byte
1567 // constant.
1568 //----------------------------------------------------------------------
1569 case DW_OP_bra:
1570 {
1571 tmp = stack.back();
1572 stack.pop_back();
1573 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1574 Scalar zero(0);
1575 if (tmp.ResolveValue(exe_ctx, ast_context) != zero)
1576 {
1577 uint32_t new_offset = offset + bra_offset;
1578 if (new_offset >= opcodes_offset && new_offset < end_offset)
1579 offset = new_offset;
1580 else
1581 {
1582 if (error_ptr)
1583 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1584 return false;
1585 }
1586 }
1587 }
1588 break;
1589
1590 //----------------------------------------------------------------------
1591 // OPCODE: DW_OP_eq
1592 // OPERANDS: none
1593 // DESCRIPTION: pops the top two stack values, compares using the
1594 // equals (==) operator.
1595 // STACK RESULT: push the constant value 1 onto the stack if the result
1596 // of the operation is true or the constant value 0 if the result of the
1597 // operation is false.
1598 //----------------------------------------------------------------------
1599 case DW_OP_eq:
1600 if (stack.size() < 2)
1601 {
1602 if (error_ptr)
1603 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
1604 return false;
1605 }
1606 else
1607 {
1608 tmp = stack.back();
1609 stack.pop_back();
1610 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) == tmp.ResolveValue(exe_ctx, ast_context);
1611 }
1612 break;
1613
1614 //----------------------------------------------------------------------
1615 // OPCODE: DW_OP_ge
1616 // OPERANDS: none
1617 // DESCRIPTION: pops the top two stack values, compares using the
1618 // greater than or equal to (>=) operator.
1619 // STACK RESULT: push the constant value 1 onto the stack if the result
1620 // of the operation is true or the constant value 0 if the result of the
1621 // operation is false.
1622 //----------------------------------------------------------------------
1623 case DW_OP_ge:
1624 if (stack.size() < 2)
1625 {
1626 if (error_ptr)
1627 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
1628 return false;
1629 }
1630 else
1631 {
1632 tmp = stack.back();
1633 stack.pop_back();
1634 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) >= tmp.ResolveValue(exe_ctx, ast_context);
1635 }
1636 break;
1637
1638 //----------------------------------------------------------------------
1639 // OPCODE: DW_OP_gt
1640 // OPERANDS: none
1641 // DESCRIPTION: pops the top two stack values, compares using the
1642 // greater than (>) operator.
1643 // STACK RESULT: push the constant value 1 onto the stack if the result
1644 // of the operation is true or the constant value 0 if the result of the
1645 // operation is false.
1646 //----------------------------------------------------------------------
1647 case DW_OP_gt:
1648 if (stack.size() < 2)
1649 {
1650 if (error_ptr)
1651 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
1652 return false;
1653 }
1654 else
1655 {
1656 tmp = stack.back();
1657 stack.pop_back();
1658 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) > tmp.ResolveValue(exe_ctx, ast_context);
1659 }
1660 break;
1661
1662 //----------------------------------------------------------------------
1663 // OPCODE: DW_OP_le
1664 // OPERANDS: none
1665 // DESCRIPTION: pops the top two stack values, compares using the
1666 // less than or equal to (<=) operator.
1667 // STACK RESULT: push the constant value 1 onto the stack if the result
1668 // of the operation is true or the constant value 0 if the result of the
1669 // operation is false.
1670 //----------------------------------------------------------------------
1671 case DW_OP_le:
1672 if (stack.size() < 2)
1673 {
1674 if (error_ptr)
1675 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
1676 return false;
1677 }
1678 else
1679 {
1680 tmp = stack.back();
1681 stack.pop_back();
1682 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) <= tmp.ResolveValue(exe_ctx, ast_context);
1683 }
1684 break;
1685
1686 //----------------------------------------------------------------------
1687 // OPCODE: DW_OP_lt
1688 // OPERANDS: none
1689 // DESCRIPTION: pops the top two stack values, compares using the
1690 // less than (<) operator.
1691 // STACK RESULT: push the constant value 1 onto the stack if the result
1692 // of the operation is true or the constant value 0 if the result of the
1693 // operation is false.
1694 //----------------------------------------------------------------------
1695 case DW_OP_lt:
1696 if (stack.size() < 2)
1697 {
1698 if (error_ptr)
1699 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
1700 return false;
1701 }
1702 else
1703 {
1704 tmp = stack.back();
1705 stack.pop_back();
1706 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) < tmp.ResolveValue(exe_ctx, ast_context);
1707 }
1708 break;
1709
1710 //----------------------------------------------------------------------
1711 // OPCODE: DW_OP_ne
1712 // OPERANDS: none
1713 // DESCRIPTION: pops the top two stack values, compares using the
1714 // not equal (!=) operator.
1715 // STACK RESULT: push the constant value 1 onto the stack if the result
1716 // of the operation is true or the constant value 0 if the result of the
1717 // operation is false.
1718 //----------------------------------------------------------------------
1719 case DW_OP_ne:
1720 if (stack.size() < 2)
1721 {
1722 if (error_ptr)
1723 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
1724 return false;
1725 }
1726 else
1727 {
1728 tmp = stack.back();
1729 stack.pop_back();
1730 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) != tmp.ResolveValue(exe_ctx, ast_context);
1731 }
1732 break;
1733
1734 //----------------------------------------------------------------------
1735 // OPCODE: DW_OP_litn
1736 // OPERANDS: none
1737 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1738 // STACK RESULT: push the unsigned literal constant value onto the top
1739 // of the stack.
1740 //----------------------------------------------------------------------
1741 case DW_OP_lit0:
1742 case DW_OP_lit1:
1743 case DW_OP_lit2:
1744 case DW_OP_lit3:
1745 case DW_OP_lit4:
1746 case DW_OP_lit5:
1747 case DW_OP_lit6:
1748 case DW_OP_lit7:
1749 case DW_OP_lit8:
1750 case DW_OP_lit9:
1751 case DW_OP_lit10:
1752 case DW_OP_lit11:
1753 case DW_OP_lit12:
1754 case DW_OP_lit13:
1755 case DW_OP_lit14:
1756 case DW_OP_lit15:
1757 case DW_OP_lit16:
1758 case DW_OP_lit17:
1759 case DW_OP_lit18:
1760 case DW_OP_lit19:
1761 case DW_OP_lit20:
1762 case DW_OP_lit21:
1763 case DW_OP_lit22:
1764 case DW_OP_lit23:
1765 case DW_OP_lit24:
1766 case DW_OP_lit25:
1767 case DW_OP_lit26:
1768 case DW_OP_lit27:
1769 case DW_OP_lit28:
1770 case DW_OP_lit29:
1771 case DW_OP_lit30:
1772 case DW_OP_lit31:
1773 stack.push_back(op - DW_OP_lit0);
1774 break;
1775
1776 //----------------------------------------------------------------------
1777 // OPCODE: DW_OP_regN
1778 // OPERANDS: none
1779 // DESCRIPTION: Push the value in register n on the top of the stack.
1780 //----------------------------------------------------------------------
1781 case DW_OP_reg0:
1782 case DW_OP_reg1:
1783 case DW_OP_reg2:
1784 case DW_OP_reg3:
1785 case DW_OP_reg4:
1786 case DW_OP_reg5:
1787 case DW_OP_reg6:
1788 case DW_OP_reg7:
1789 case DW_OP_reg8:
1790 case DW_OP_reg9:
1791 case DW_OP_reg10:
1792 case DW_OP_reg11:
1793 case DW_OP_reg12:
1794 case DW_OP_reg13:
1795 case DW_OP_reg14:
1796 case DW_OP_reg15:
1797 case DW_OP_reg16:
1798 case DW_OP_reg17:
1799 case DW_OP_reg18:
1800 case DW_OP_reg19:
1801 case DW_OP_reg20:
1802 case DW_OP_reg21:
1803 case DW_OP_reg22:
1804 case DW_OP_reg23:
1805 case DW_OP_reg24:
1806 case DW_OP_reg25:
1807 case DW_OP_reg26:
1808 case DW_OP_reg27:
1809 case DW_OP_reg28:
1810 case DW_OP_reg29:
1811 case DW_OP_reg30:
1812 case DW_OP_reg31:
1813 {
1814 reg_num = op - DW_OP_reg0;
1815
1816 if (ReadRegisterValueAsScalar (exe_ctx, reg_kind, reg_num, error_ptr, tmp))
1817 stack.push_back(tmp);
1818 else
1819 return false;
1820 }
1821 break;
1822 //----------------------------------------------------------------------
1823 // OPCODE: DW_OP_regx
1824 // OPERANDS:
1825 // ULEB128 literal operand that encodes the register.
1826 // DESCRIPTION: Push the value in register on the top of the stack.
1827 //----------------------------------------------------------------------
1828 case DW_OP_regx:
1829 {
1830 reg_num = opcodes.GetULEB128(&offset);
1831 if (ReadRegisterValueAsScalar (exe_ctx, reg_kind, reg_num, error_ptr, tmp))
1832 stack.push_back(tmp);
1833 else
1834 return false;
1835 }
1836 break;
1837
1838 //----------------------------------------------------------------------
1839 // OPCODE: DW_OP_bregN
1840 // OPERANDS:
1841 // SLEB128 offset from register N
1842 // DESCRIPTION: Value is in memory at the address specified by register
1843 // N plus an offset.
1844 //----------------------------------------------------------------------
1845 case DW_OP_breg0:
1846 case DW_OP_breg1:
1847 case DW_OP_breg2:
1848 case DW_OP_breg3:
1849 case DW_OP_breg4:
1850 case DW_OP_breg5:
1851 case DW_OP_breg6:
1852 case DW_OP_breg7:
1853 case DW_OP_breg8:
1854 case DW_OP_breg9:
1855 case DW_OP_breg10:
1856 case DW_OP_breg11:
1857 case DW_OP_breg12:
1858 case DW_OP_breg13:
1859 case DW_OP_breg14:
1860 case DW_OP_breg15:
1861 case DW_OP_breg16:
1862 case DW_OP_breg17:
1863 case DW_OP_breg18:
1864 case DW_OP_breg19:
1865 case DW_OP_breg20:
1866 case DW_OP_breg21:
1867 case DW_OP_breg22:
1868 case DW_OP_breg23:
1869 case DW_OP_breg24:
1870 case DW_OP_breg25:
1871 case DW_OP_breg26:
1872 case DW_OP_breg27:
1873 case DW_OP_breg28:
1874 case DW_OP_breg29:
1875 case DW_OP_breg30:
1876 case DW_OP_breg31:
1877 {
1878 reg_num = op - DW_OP_breg0;
1879
1880 if (ReadRegisterValueAsScalar (exe_ctx, reg_kind, reg_num, error_ptr, tmp))
1881 {
1882 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1883 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
1884 stack.push_back(tmp);
1885 stack.back().SetValueType (Value::eValueTypeLoadAddress);
1886 }
1887 else
1888 return false;
1889 }
1890 break;
1891 //----------------------------------------------------------------------
1892 // OPCODE: DW_OP_bregx
1893 // OPERANDS: 2
1894 // ULEB128 literal operand that encodes the register.
1895 // SLEB128 offset from register N
1896 // DESCRIPTION: Value is in memory at the address specified by register
1897 // N plus an offset.
1898 //----------------------------------------------------------------------
1899 case DW_OP_bregx:
1900 {
1901 reg_num = opcodes.GetULEB128(&offset);
1902
1903 if (ReadRegisterValueAsScalar (exe_ctx, reg_kind, reg_num, error_ptr, tmp))
1904 {
1905 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1906 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
1907 stack.push_back(tmp);
1908 stack.back().SetValueType (Value::eValueTypeLoadAddress);
1909 }
1910 else
1911 return false;
1912 }
1913 break;
1914
1915 case DW_OP_fbreg:
1916 if (exe_ctx && exe_ctx->frame)
1917 {
1918 Scalar value;
1919 if (exe_ctx->frame->GetFrameBaseValue(value, error_ptr))
1920 {
1921 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
1922 value += fbreg_offset;
1923 stack.push_back(value);
1924 stack.back().SetValueType (Value::eValueTypeLoadAddress);
1925 }
1926 else
1927 return false;
1928 }
1929 else
1930 {
1931 if (error_ptr)
1932 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
1933 return false;
1934 }
1935 break;
1936
1937 //----------------------------------------------------------------------
1938 // OPCODE: DW_OP_nop
1939 // OPERANDS: none
1940 // DESCRIPTION: A place holder. It has no effect on the location stack
1941 // or any of its values.
1942 //----------------------------------------------------------------------
1943 case DW_OP_nop:
1944 break;
1945
1946 //----------------------------------------------------------------------
1947 // OPCODE: DW_OP_piece
1948 // OPERANDS: 1
1949 // ULEB128: byte size of the piece
1950 // DESCRIPTION: The operand describes the size in bytes of the piece of
1951 // the object referenced by the DWARF expression whose result is at the
1952 // top of the stack. If the piece is located in a register, but does not
1953 // occupy the entire register, the placement of the piece within that
1954 // register is defined by the ABI.
1955 //
1956 // Many compilers store a single variable in sets of registers, or store
1957 // a variable partially in memory and partially in registers.
1958 // DW_OP_piece provides a way of describing how large a part of a
1959 // variable a particular DWARF expression refers to.
1960 //----------------------------------------------------------------------
1961 case DW_OP_piece:
1962 if (error_ptr)
1963 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_piece.");
1964 return false;
1965
1966 //----------------------------------------------------------------------
1967 // OPCODE: DW_OP_push_object_address
1968 // OPERANDS: none
1969 // DESCRIPTION: Pushes the address of the object currently being
1970 // evaluated as part of evaluation of a user presented expression.
1971 // This object may correspond to an independent variable described by
1972 // its own DIE or it may be a component of an array, structure, or class
1973 // whose address has been dynamically determined by an earlier step
1974 // during user expression evaluation.
1975 //----------------------------------------------------------------------
1976 case DW_OP_push_object_address:
1977 if (error_ptr)
1978 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address.");
1979 return false;
1980
1981 //----------------------------------------------------------------------
1982 // OPCODE: DW_OP_call2
1983 // OPERANDS:
1984 // uint16_t compile unit relative offset of a DIE
1985 // DESCRIPTION: Performs subroutine calls during evaluation
1986 // of a DWARF expression. The operand is the 2-byte unsigned offset
1987 // of a debugging information entry in the current compilation unit.
1988 //
1989 // Operand interpretation is exactly like that for DW_FORM_ref2.
1990 //
1991 // This operation transfers control of DWARF expression evaluation
1992 // to the DW_AT_location attribute of the referenced DIE. If there is
1993 // no such attribute, then there is no effect. Execution of the DWARF
1994 // expression of a DW_AT_location attribute may add to and/or remove from
1995 // values on the stack. Execution returns to the point following the call
1996 // when the end of the attribute is reached. Values on the stack at the
1997 // time of the call may be used as parameters by the called expression
1998 // and values left on the stack by the called expression may be used as
1999 // return values by prior agreement between the calling and called
2000 // expressions.
2001 //----------------------------------------------------------------------
2002 case DW_OP_call2:
2003 if (error_ptr)
2004 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2005 return false;
2006 //----------------------------------------------------------------------
2007 // OPCODE: DW_OP_call4
2008 // OPERANDS: 1
2009 // uint32_t compile unit relative offset of a DIE
2010 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2011 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2012 // of a debugging information entry in the current compilation unit.
2013 //
2014 // Operand interpretation DW_OP_call4 is exactly like that for
2015 // DW_FORM_ref4.
2016 //
2017 // This operation transfers control of DWARF expression evaluation
2018 // to the DW_AT_location attribute of the referenced DIE. If there is
2019 // no such attribute, then there is no effect. Execution of the DWARF
2020 // expression of a DW_AT_location attribute may add to and/or remove from
2021 // values on the stack. Execution returns to the point following the call
2022 // when the end of the attribute is reached. Values on the stack at the
2023 // time of the call may be used as parameters by the called expression
2024 // and values left on the stack by the called expression may be used as
2025 // return values by prior agreement between the calling and called
2026 // expressions.
2027 //----------------------------------------------------------------------
2028 case DW_OP_call4:
2029 if (error_ptr)
2030 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2031 return false;
2032
2033
2034 //----------------------------------------------------------------------
2035 // OPCODE: DW_OP_call_ref
2036 // OPERANDS:
2037 // uint32_t absolute DIE offset for 32-bit DWARF or a uint64_t
2038 // absolute DIE offset for 64 bit DWARF.
2039 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2040 // expression. Takes a single operand. In the 32-bit DWARF format, the
2041 // operand is a 4-byte unsigned value; in the 64-bit DWARF format, it
2042 // is an 8-byte unsigned value. The operand is used as the offset of a
2043 // debugging information entry in a .debug_info section which may be
2044 // contained in a shared object for executable other than that
2045 // containing the operator. For references from one shared object or
2046 // executable to another, the relocation must be performed by the
2047 // consumer.
2048 //
2049 // Operand interpretation of DW_OP_call_ref is exactly like that for
2050 // DW_FORM_ref_addr.
2051 //
2052 // This operation transfers control of DWARF expression evaluation
2053 // to the DW_AT_location attribute of the referenced DIE. If there is
2054 // no such attribute, then there is no effect. Execution of the DWARF
2055 // expression of a DW_AT_location attribute may add to and/or remove from
2056 // values on the stack. Execution returns to the point following the call
2057 // when the end of the attribute is reached. Values on the stack at the
2058 // time of the call may be used as parameters by the called expression
2059 // and values left on the stack by the called expression may be used as
2060 // return values by prior agreement between the calling and called
2061 // expressions.
2062 //----------------------------------------------------------------------
2063 case DW_OP_call_ref:
2064 if (error_ptr)
2065 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call_ref.");
2066 return false;
2067
2068 //----------------------------------------------------------------------
2069 // OPCODE: DW_OP_APPLE_array_ref
2070 // OPERANDS: none
2071 // DESCRIPTION: Pops a value off the stack and uses it as the array
2072 // index. Pops a second value off the stack and uses it as the array
2073 // itself. Pushes a value onto the stack representing the element of
2074 // the array specified by the index.
2075 //----------------------------------------------------------------------
2076 case DW_OP_APPLE_array_ref:
2077 {
2078 if (stack.size() < 2)
2079 {
2080 if (error_ptr)
2081 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_array_ref.");
2082 return false;
2083 }
2084
2085 Value index_val = stack.back();
2086 stack.pop_back();
2087 Value array_val = stack.back();
2088 stack.pop_back();
2089
2090 Scalar &index_scalar = index_val.ResolveValue(exe_ctx, ast_context);
2091 int64_t index = index_scalar.SLongLong(LONG_LONG_MAX);
2092
2093 if (index == LONG_LONG_MAX)
2094 {
2095 if (error_ptr)
2096 error_ptr->SetErrorString("Invalid array index.");
2097 return false;
2098 }
2099
2100 if (array_val.GetContextType() != Value::eContextTypeOpaqueClangQualType)
2101 {
2102 if (error_ptr)
2103 error_ptr->SetErrorString("Arrays without Clang types are unhandled at this time.");
2104 return false;
2105 }
2106
2107 if (array_val.GetValueType() != Value::eValueTypeLoadAddress &&
2108 array_val.GetValueType() != Value::eValueTypeHostAddress)
2109 {
2110 if (error_ptr)
2111 error_ptr->SetErrorString("Array must be stored in memory.");
2112 return false;
2113 }
2114
2115 void *array_type = array_val.GetOpaqueClangQualType();
2116
2117 void *member_type;
2118 uint64_t size = 0;
2119
2120 if ((!ClangASTContext::IsPointerType(array_type, &member_type)) &&
2121 (!ClangASTContext::IsArrayType(array_type, &member_type, &size)))
2122 {
2123 if (error_ptr)
2124 error_ptr->SetErrorString("Array reference from something that is neither a pointer nor an array.");
2125 return false;
2126 }
2127
2128 if (size && (index >= size || index < 0))
2129 {
2130 if (error_ptr)
2131 error_ptr->SetErrorStringWithFormat("Out of bounds array access. %lld is not in [0, %llu]", index, size);
2132 return false;
2133 }
2134
Greg Clayton960d6a42010-08-03 00:35:52 +00002135 uint64_t member_bit_size = ClangASTType::GetClangTypeBitWidth(ast_context, member_type);
2136 uint64_t member_bit_align = ClangASTType::GetTypeBitAlign(ast_context, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002137 uint64_t member_bit_incr = ((member_bit_size + member_bit_align - 1) / member_bit_align) * member_bit_align;
2138 if (member_bit_incr % 8)
2139 {
2140 if (error_ptr)
2141 error_ptr->SetErrorStringWithFormat("Array increment is not byte aligned", index, size);
2142 return false;
2143 }
2144 int64_t member_offset = (int64_t)(member_bit_incr / 8) * index;
2145
2146 Value member;
2147
2148 member.SetContext(Value::eContextTypeOpaqueClangQualType, member_type);
2149 member.SetValueType(array_val.GetValueType());
2150
2151 addr_t array_base = (addr_t)array_val.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2152 addr_t member_loc = array_base + member_offset;
2153 member.GetScalar() = (uint64_t)member_loc;
2154
2155 stack.push_back(member);
2156 }
2157 break;
2158
2159 //----------------------------------------------------------------------
2160 // OPCODE: DW_OP_APPLE_uninit
2161 // OPERANDS: none
2162 // DESCRIPTION: Lets us know that the value is currently not initialized
2163 //----------------------------------------------------------------------
2164 case DW_OP_APPLE_uninit:
2165 //return eResultTypeErrorUninitialized;
2166 break; // Ignore this as we have seen cases where this value is incorrectly added
2167
2168 //----------------------------------------------------------------------
2169 // OPCODE: DW_OP_APPLE_assign
2170 // OPERANDS: none
2171 // DESCRIPTION: Pops a value off of the stack and assigns it to the next
2172 // item on the stack which must be something assignable (inferior
2173 // Variable, inferior Type with address, inferior register, or
2174 // expression local variable.
2175 //----------------------------------------------------------------------
2176 case DW_OP_APPLE_assign:
2177 if (stack.size() < 2)
2178 {
2179 if (error_ptr)
2180 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_assign.");
2181 return false;
2182 }
2183 else
2184 {
2185 tmp = stack.back();
2186 stack.pop_back();
2187 Value::ContextType context_type = stack.back().GetContextType();
2188 StreamString new_value(Stream::eBinary, 4, eByteOrderHost);
2189 switch (context_type)
2190 {
2191 case Value::eContextTypeOpaqueClangQualType:
2192 {
2193 void *clang_type = stack.back().GetOpaqueClangQualType();
2194
2195 if (ClangASTContext::IsAggregateType (clang_type))
2196 {
2197 Value::ValueType source_value_type = tmp.GetValueType();
2198 Value::ValueType target_value_type = stack.back().GetValueType();
2199
2200 addr_t source_addr = (addr_t)tmp.GetScalar().ULongLong();
2201 addr_t target_addr = (addr_t)stack.back().GetScalar().ULongLong();
2202
Greg Clayton960d6a42010-08-03 00:35:52 +00002203 size_t byte_size = (ClangASTType::GetClangTypeBitWidth(ast_context, clang_type) + 7) / 8;
Chris Lattner24943d22010-06-08 16:52:24 +00002204
2205 switch (source_value_type)
2206 {
2207 case Value::eValueTypeLoadAddress:
2208 switch (target_value_type)
2209 {
2210 case Value::eValueTypeLoadAddress:
2211 {
2212 DataBufferHeap data;
2213 data.SetByteSize(byte_size);
2214
2215 Error error;
2216 if (exe_ctx->process->ReadMemory (source_addr, data.GetBytes(), byte_size, error) != byte_size)
2217 {
2218 if (error_ptr)
2219 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2220 return false;
2221 }
2222
2223 if (exe_ctx->process->WriteMemory (target_addr, data.GetBytes(), byte_size, error) != byte_size)
2224 {
2225 if (error_ptr)
2226 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2227 return false;
2228 }
2229 }
2230 break;
2231 case Value::eValueTypeHostAddress:
2232 if (exe_ctx->process->GetByteOrder() != Host::GetByteOrder())
2233 {
2234 if (error_ptr)
2235 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2236 return false;
2237 }
2238 else
2239 {
2240 Error error;
2241 if (exe_ctx->process->ReadMemory (source_addr, (uint8_t*)target_addr, byte_size, error) != byte_size)
2242 {
2243 if (error_ptr)
2244 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2245 return false;
2246 }
2247 }
2248 break;
2249 default:
2250 return false;
2251 }
2252 break;
2253 case Value::eValueTypeHostAddress:
2254 switch (target_value_type)
2255 {
2256 case Value::eValueTypeLoadAddress:
2257 if (exe_ctx->process->GetByteOrder() != Host::GetByteOrder())
2258 {
2259 if (error_ptr)
2260 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2261 return false;
2262 }
2263 else
2264 {
2265 Error error;
2266 if (exe_ctx->process->WriteMemory (target_addr, (uint8_t*)source_addr, byte_size, error) != byte_size)
2267 {
2268 if (error_ptr)
2269 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2270 return false;
2271 }
2272 }
2273 case Value::eValueTypeHostAddress:
2274 memcpy ((uint8_t*)target_addr, (uint8_t*)source_addr, byte_size);
2275 break;
2276 default:
2277 return false;
2278 }
2279 }
2280 }
2281 else
2282 {
Greg Clayton1674b122010-07-21 22:12:05 +00002283 if (!ClangASTType::SetValueFromScalar (ast_context,
2284 clang_type,
2285 tmp.ResolveValue(exe_ctx, ast_context),
2286 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002287 {
2288 if (error_ptr)
2289 error_ptr->SetErrorStringWithFormat ("Couldn't extract a value from an integral type.\n");
2290 return false;
2291 }
2292
2293 Value::ValueType value_type = stack.back().GetValueType();
2294
2295 switch (value_type)
2296 {
2297 case Value::eValueTypeLoadAddress:
2298 case Value::eValueTypeHostAddress:
2299 {
2300 lldb::AddressType address_type = (value_type == Value::eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost);
2301 lldb::addr_t addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton1674b122010-07-21 22:12:05 +00002302 if (!ClangASTType::WriteToMemory (ast_context,
2303 clang_type,
2304 exe_ctx,
2305 addr,
2306 address_type,
2307 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002308 {
2309 if (error_ptr)
2310 error_ptr->SetErrorStringWithFormat ("Failed to write value to memory at 0x%llx.\n", addr);
2311 return false;
2312 }
2313 }
2314 break;
2315
2316 default:
2317 break;
2318 }
2319 }
2320 }
2321 break;
2322
2323 default:
2324 if (error_ptr)
2325 error_ptr->SetErrorString ("Assign failed.");
2326 return false;
2327 }
2328 }
2329 break;
2330
2331 //----------------------------------------------------------------------
2332 // OPCODE: DW_OP_APPLE_address_of
2333 // OPERANDS: none
2334 // DESCRIPTION: Pops a value off of the stack and pushed its address.
2335 // The top item on the stack must be a variable, or already be a memory
2336 // location.
2337 //----------------------------------------------------------------------
2338 case DW_OP_APPLE_address_of:
2339 if (stack.empty())
2340 {
2341 if (error_ptr)
2342 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_address_of.");
2343 return false;
2344 }
2345 else
2346 {
2347 Value::ValueType value_type = stack.back().GetValueType();
2348 switch (value_type)
2349 {
2350 default:
2351 case Value::eValueTypeScalar: // raw scalar value
2352 if (error_ptr)
2353 error_ptr->SetErrorString("Top stack item isn't a memory based object.");
2354 return false;
2355
2356 case Value::eValueTypeLoadAddress: // load address value
2357 case Value::eValueTypeFileAddress: // file address value
2358 case Value::eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
2359 // Taking the address of an object reduces it to the address
2360 // of the value and removes any extra context it had.
2361 //stack.back().SetValueType(Value::eValueTypeScalar);
2362 stack.back().ClearContext();
2363 break;
2364 }
2365 }
2366 break;
2367
2368 //----------------------------------------------------------------------
2369 // OPCODE: DW_OP_APPLE_value_of
2370 // OPERANDS: none
2371 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2372 // The top item on the stack must be a variable, expression variable.
2373 //----------------------------------------------------------------------
2374 case DW_OP_APPLE_value_of:
2375 if (stack.empty())
2376 {
2377 if (error_ptr)
2378 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_value_of.");
2379 return false;
2380 }
2381 else if (!stack.back().ValueOf(exe_ctx, ast_context))
2382 {
2383 if (error_ptr)
2384 error_ptr->SetErrorString ("Top stack item isn't a valid candidate for DW_OP_APPLE_value_of.");
2385 return false;
2386 }
2387 break;
2388
2389 //----------------------------------------------------------------------
2390 // OPCODE: DW_OP_APPLE_deref_type
2391 // OPERANDS: none
2392 // DESCRIPTION: gets the value pointed to by the top stack item
2393 //----------------------------------------------------------------------
2394 case DW_OP_APPLE_deref_type:
2395 {
2396 if (stack.empty())
2397 {
2398 if (error_ptr)
2399 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_deref_type.");
2400 return false;
2401 }
2402
2403 tmp = stack.back();
2404 stack.pop_back();
2405
2406 if (tmp.GetContextType() != Value::eContextTypeOpaqueClangQualType)
2407 {
2408 if (error_ptr)
2409 error_ptr->SetErrorString("Item at top of expression stack must have a Clang type");
2410 return false;
2411 }
2412
2413 void *ptr_type = tmp.GetOpaqueClangQualType();
2414 void *target_type;
2415
2416 if (!ClangASTContext::IsPointerType(ptr_type, &target_type))
2417 {
2418 if (error_ptr)
2419 error_ptr->SetErrorString("Dereferencing a non-pointer type");
2420 return false;
2421 }
2422
2423 // TODO do we want all pointers to be dereferenced as load addresses?
2424 Value::ValueType value_type = tmp.GetValueType();
2425
2426 tmp.ResolveValue(exe_ctx, ast_context);
2427
2428 tmp.SetValueType(value_type);
2429 tmp.SetContext(Value::eContextTypeOpaqueClangQualType, target_type);
2430
2431 stack.push_back(tmp);
2432 }
2433 break;
2434
2435 //----------------------------------------------------------------------
2436 // OPCODE: DW_OP_APPLE_expr_local
2437 // OPERANDS: ULEB128
2438 // DESCRIPTION: pushes the expression local variable index onto the
2439 // stack and set the appropriate context so we know the stack item is
2440 // an expression local variable index.
2441 //----------------------------------------------------------------------
2442 case DW_OP_APPLE_expr_local:
2443 {
Sean Callanana6223432010-08-20 01:02:30 +00002444 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002445 uint32_t idx = opcodes.GetULEB128(&offset);
2446 if (expr_locals == NULL)
2447 {
2448 if (error_ptr)
2449 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) opcode encountered with no local variable list.\n", idx);
2450 return false;
2451 }
2452 Value *expr_local_variable = expr_locals->GetVariableAtIndex(idx);
2453 if (expr_local_variable == NULL)
2454 {
2455 if (error_ptr)
2456 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) with invalid index %u.\n", idx, idx);
2457 return false;
2458 }
2459 Value *proxy = expr_local_variable->CreateProxy();
2460 stack.push_back(*proxy);
2461 delete proxy;
2462 //stack.back().SetContext (Value::eContextTypeOpaqueClangQualType, expr_local_variable->GetOpaqueClangQualType());
Sean Callanana6223432010-08-20 01:02:30 +00002463 */
Chris Lattner24943d22010-06-08 16:52:24 +00002464 }
2465 break;
2466
2467 //----------------------------------------------------------------------
2468 // OPCODE: DW_OP_APPLE_extern
2469 // OPERANDS: ULEB128
2470 // DESCRIPTION: pushes a proxy for the extern object index onto the
2471 // stack.
2472 //----------------------------------------------------------------------
2473 case DW_OP_APPLE_extern:
2474 {
Sean Callanan8c127202010-08-23 23:09:38 +00002475 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002476 uint32_t idx = opcodes.GetULEB128(&offset);
2477 if (!decl_map)
2478 {
2479 if (error_ptr)
2480 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) opcode encountered with no decl map.\n", idx);
2481 return false;
2482 }
2483 Value *extern_var = decl_map->GetValueForIndex(idx);
2484 if (!extern_var)
2485 {
2486 if (error_ptr)
2487 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) with invalid index %u.\n", idx, idx);
2488 return false;
2489 }
2490 Value *proxy = extern_var->CreateProxy();
2491 stack.push_back(*proxy);
2492 delete proxy;
Sean Callanan8c127202010-08-23 23:09:38 +00002493 */
Chris Lattner24943d22010-06-08 16:52:24 +00002494 }
2495 break;
2496
2497 case DW_OP_APPLE_scalar_cast:
2498 if (stack.empty())
2499 {
2500 if (error_ptr)
2501 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_scalar_cast.");
2502 return false;
2503 }
2504 else
2505 {
2506 // Simple scalar cast
2507 if (!stack.back().ResolveValue(exe_ctx, ast_context).Cast((Scalar::Type)opcodes.GetU8(&offset)))
2508 {
2509 if (error_ptr)
2510 error_ptr->SetErrorString("Cast failed.");
2511 return false;
2512 }
2513 }
2514 break;
2515
2516
2517 case DW_OP_APPLE_clang_cast:
2518 if (stack.empty())
2519 {
2520 if (error_ptr)
2521 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_clang_cast.");
2522 return false;
2523 }
2524 else
2525 {
2526 void *clang_type = (void *)opcodes.GetMaxU64(&offset, sizeof(void*));
2527 stack.back().SetContext (Value::eContextTypeOpaqueClangQualType, clang_type);
2528 }
2529 break;
2530 //----------------------------------------------------------------------
2531 // OPCODE: DW_OP_APPLE_constf
2532 // OPERANDS: 1 byte float length, followed by that many bytes containing
2533 // the constant float data.
2534 // DESCRIPTION: Push a float value onto the expression stack.
2535 //----------------------------------------------------------------------
2536 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
2537 {
2538 uint8_t float_length = opcodes.GetU8(&offset);
2539 if (sizeof(float) == float_length)
2540 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetFloat (&offset);
2541 else if (sizeof(double) == float_length)
2542 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetDouble (&offset);
2543 else if (sizeof(long double) == float_length)
2544 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetLongDouble (&offset);
2545 else
2546 {
2547 StreamString new_value;
2548 opcodes.Dump(&new_value, offset, eFormatBytes, 1, float_length, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
2549
2550 if (error_ptr)
2551 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_constf(<%u> %s) unsupported float size.\n", float_length, new_value.GetData());
2552 return false;
2553 }
2554 tmp.SetValueType(Value::eValueTypeScalar);
2555 tmp.ClearContext();
2556 stack.push_back(tmp);
2557 }
2558 break;
2559 //----------------------------------------------------------------------
2560 // OPCODE: DW_OP_APPLE_clear
2561 // OPERANDS: none
2562 // DESCRIPTION: Clears the expression stack.
2563 //----------------------------------------------------------------------
2564 case DW_OP_APPLE_clear:
2565 stack.clear();
2566 break;
2567
2568 //----------------------------------------------------------------------
2569 // OPCODE: DW_OP_APPLE_error
2570 // OPERANDS: none
2571 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2572 // The top item on the stack must be a variable, expression variable.
2573 //----------------------------------------------------------------------
2574 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
2575 if (error_ptr)
2576 error_ptr->SetErrorString ("Generic error.");
2577 return false;
2578 }
2579 }
2580
2581 if (stack.empty())
2582 {
2583 if (error_ptr)
2584 error_ptr->SetErrorString ("Stack empty after evaluation.");
2585 return false;
2586 }
2587 else if (log)
2588 {
Chris Lattner24943d22010-06-08 16:52:24 +00002589 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +00002590 log->Printf("Stack after operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +00002591 for (size_t i=0; i<count; ++i)
2592 {
2593 StreamString new_value;
2594 new_value.Printf("[%zu]", i);
2595 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +00002596 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +00002597 }
2598 }
2599
2600 result = stack.back();
2601 return true; // Return true on success
2602}
2603