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