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