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%x) ", m_data.GetULEB128(&offset)); break;      // 0x10 1 ULEB128 constant
314         case DW_OP_consts:  s->Printf("DW_OP_consts(0x%x) ", 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%x) ", 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%x)", 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(%u)", 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(%u)", 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         if (reg_ctx)
934             pc = reg_ctx->GetPC();
935         else
936             pc = exe_ctx->frame->GetRegisterContext()->GetPC();
937 
938         if (loclist_base_load_addr != LLDB_INVALID_ADDRESS)
939         {
940             if (pc == LLDB_INVALID_ADDRESS)
941             {
942                 if (error_ptr)
943                     error_ptr->SetErrorString("Invalid PC in frame.");
944                 return false;
945             }
946 
947             addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
948 
949             while (m_data.ValidOffset(offset))
950             {
951                 // We need to figure out what the value is for the location.
952                 addr_t lo_pc = m_data.GetAddress(&offset);
953                 addr_t hi_pc = m_data.GetAddress(&offset);
954                 if (lo_pc == 0 && hi_pc == 0)
955                 {
956                     break;
957                 }
958                 else
959                 {
960                     lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
961                     hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
962 
963                     uint16_t length = m_data.GetU16(&offset);
964 
965                     if (length > 0 && lo_pc <= pc && pc < hi_pc)
966                     {
967                         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);
968                     }
969                     offset += length;
970                 }
971             }
972         }
973         if (error_ptr)
974             error_ptr->SetErrorString ("variable not available");
975         return false;
976     }
977 
978     // Not a location list, just a single expression.
979     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);
980 }
981 
982 
983 
984 bool
985 DWARFExpression::Evaluate
986 (
987     ExecutionContext *exe_ctx,
988     clang::ASTContext *ast_context,
989     ClangExpressionVariableList *expr_locals,
990     ClangExpressionDeclMap *decl_map,
991     RegisterContext *reg_ctx,
992     const DataExtractor& opcodes,
993     const uint32_t opcodes_offset,
994     const uint32_t opcodes_length,
995     const uint32_t reg_kind,
996     const Value* initial_value_ptr,
997     Value& result,
998     Error *error_ptr
999 )
1000 {
1001     std::vector<Value> stack;
1002 
1003     if (reg_ctx == NULL && exe_ctx && exe_ctx->frame)
1004         reg_ctx = exe_ctx->frame->GetRegisterContext().get();
1005 
1006     if (initial_value_ptr)
1007         stack.push_back(*initial_value_ptr);
1008 
1009     uint32_t offset = opcodes_offset;
1010     const uint32_t end_offset = opcodes_offset + opcodes_length;
1011     Value tmp;
1012     uint32_t reg_num;
1013 
1014     // Make sure all of the data is available in opcodes.
1015     if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length))
1016     {
1017         if (error_ptr)
1018             error_ptr->SetErrorString ("Invalid offset and/or length for opcodes buffer.");
1019         return false;
1020     }
1021     LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1022 
1023 
1024     while (opcodes.ValidOffset(offset) && offset < end_offset)
1025     {
1026         const uint32_t op_offset = offset;
1027         const uint8_t op = opcodes.GetU8(&offset);
1028 
1029         if (log)
1030         {
1031             size_t count = stack.size();
1032             log->Printf("Stack before operation has %d values:", count);
1033             for (size_t i=0; i<count; ++i)
1034             {
1035                 StreamString new_value;
1036                 new_value.Printf("[%zu]", i);
1037                 stack[i].Dump(&new_value);
1038                 log->Printf("  %s", new_value.GetData());
1039             }
1040             log->Printf("0x%8.8x: %s", op_offset, DW_OP_value_to_name(op));
1041         }
1042         switch (op)
1043         {
1044         //----------------------------------------------------------------------
1045         // The DW_OP_addr operation has a single operand that encodes a machine
1046         // address and whose size is the size of an address on the target machine.
1047         //----------------------------------------------------------------------
1048         case DW_OP_addr:
1049             stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1050             stack.back().SetValueType (Value::eValueTypeFileAddress);
1051             break;
1052 
1053         //----------------------------------------------------------------------
1054         // The DW_OP_addr_sect_offset4 is used for any location expressions in
1055         // shared libraries that have a location like:
1056         //  DW_OP_addr(0x1000)
1057         // If this address resides in a shared library, then this virtual
1058         // address won't make sense when it is evaluated in the context of a
1059         // running process where shared libraries have been slid. To account for
1060         // this, this new address type where we can store the section pointer
1061         // and a 4 byte offset.
1062         //----------------------------------------------------------------------
1063 //      case DW_OP_addr_sect_offset4:
1064 //          {
1065 //              result_type = eResultTypeFileAddress;
1066 //              lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *));
1067 //              lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1068 //
1069 //              Address so_addr (sect, sect_offset);
1070 //              lldb::addr_t load_addr = so_addr.GetLoadAddress();
1071 //              if (load_addr != LLDB_INVALID_ADDRESS)
1072 //              {
1073 //                  // We successfully resolve a file address to a load
1074 //                  // address.
1075 //                  stack.push_back(load_addr);
1076 //                  break;
1077 //              }
1078 //              else
1079 //              {
1080 //                  // We were able
1081 //                  if (error_ptr)
1082 //                      error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1083 //                  return false;
1084 //              }
1085 //          }
1086 //          break;
1087 
1088         //----------------------------------------------------------------------
1089         // OPCODE: DW_OP_deref
1090         // OPERANDS: none
1091         // DESCRIPTION: Pops the top stack entry and treats it as an address.
1092         // The value retrieved from that address is pushed. The size of the
1093         // data retrieved from the dereferenced address is the size of an
1094         // address on the target machine.
1095         //----------------------------------------------------------------------
1096         case DW_OP_deref:
1097             {
1098                 Value::ValueType value_type = stack.back().GetValueType();
1099                 switch (value_type)
1100                 {
1101                 case Value::eValueTypeHostAddress:
1102                     {
1103                         void *src = (void *)stack.back().GetScalar().ULongLong();
1104                         intptr_t ptr;
1105                         ::memcpy (&ptr, src, sizeof(void *));
1106                         stack.back().GetScalar() = ptr;
1107                         stack.back().ClearContext();
1108                     }
1109                     break;
1110                 case Value::eValueTypeLoadAddress:
1111                     if (exe_ctx)
1112                     {
1113                         if (exe_ctx->process)
1114                         {
1115                             lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1116                             uint8_t addr_bytes[sizeof(lldb::addr_t)];
1117                             uint32_t addr_size = exe_ctx->process->GetAddressByteSize();
1118                             Error error;
1119                             if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, addr_size, error) == addr_size)
1120                             {
1121                                 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), addr_size);
1122                                 uint32_t addr_data_offset = 0;
1123                                 stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1124                                 stack.back().ClearContext();
1125                             }
1126                             else
1127                             {
1128                                 if (error_ptr)
1129                                     error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
1130                                                                          pointer_addr,
1131                                                                          error.AsCString());
1132                                 return false;
1133                             }
1134                         }
1135                         else
1136                         {
1137                             if (error_ptr)
1138                                 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1139                             return false;
1140                         }
1141                     }
1142                     else
1143                     {
1144                         if (error_ptr)
1145                             error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1146                         return false;
1147                     }
1148                     break;
1149 
1150                 default:
1151                     break;
1152                 }
1153 
1154             }
1155             break;
1156 
1157         //----------------------------------------------------------------------
1158         // OPCODE: DW_OP_deref_size
1159         // OPERANDS: 1
1160         //  1 - uint8_t that specifies the size of the data to dereference.
1161         // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1162         // stack entry and treats it as an address. The value retrieved from that
1163         // address is pushed. In the DW_OP_deref_size operation, however, the
1164         // size in bytes of the data retrieved from the dereferenced address is
1165         // specified by the single operand. This operand is a 1-byte unsigned
1166         // integral constant whose value may not be larger than the size of an
1167         // address on the target machine. The data retrieved is zero extended
1168         // to the size of an address on the target machine before being pushed
1169         // on the expression stack.
1170         //----------------------------------------------------------------------
1171         case DW_OP_deref_size:
1172             {
1173                 uint8_t size = opcodes.GetU8(&offset);
1174                 Value::ValueType value_type = stack.back().GetValueType();
1175                 switch (value_type)
1176                 {
1177                 case Value::eValueTypeHostAddress:
1178                     {
1179                         void *src = (void *)stack.back().GetScalar().ULongLong();
1180                         intptr_t ptr;
1181                         ::memcpy (&ptr, src, sizeof(void *));
1182                         // I can't decide whether the size operand should apply to the bytes in their
1183                         // lldb-host endianness or the target endianness.. I doubt this'll ever come up
1184                         // but I'll opt for assuming big endian regardless.
1185                         switch (size)
1186                         {
1187                             case 1: ptr = ptr & 0xff; break;
1188                             case 2: ptr = ptr & 0xffff; break;
1189                             case 3: ptr = ptr & 0xffffff; break;
1190                             case 4: ptr = ptr & 0xffffffff; break;
1191                             // the casts are added to work around the case where intptr_t is a 32 bit quantity;
1192                             // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this program.
1193                             case 5: ptr = (intptr_t) ptr & 0xffffffffffULL; break;
1194                             case 6: ptr = (intptr_t) ptr & 0xffffffffffffULL; break;
1195                             case 7: ptr = (intptr_t) ptr & 0xffffffffffffffULL; break;
1196                             default: break;
1197                         }
1198                         stack.back().GetScalar() = ptr;
1199                         stack.back().ClearContext();
1200                     }
1201                     break;
1202                 case Value::eValueTypeLoadAddress:
1203                     if (exe_ctx)
1204                     {
1205                         if (exe_ctx->process)
1206                         {
1207                             lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1208                             uint8_t addr_bytes[sizeof(lldb::addr_t)];
1209                             Error error;
1210                             if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size)
1211                             {
1212                                 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), size);
1213                                 uint32_t addr_data_offset = 0;
1214                                 switch (size)
1215                                 {
1216                                     case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break;
1217                                     case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break;
1218                                     case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break;
1219                                     case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break;
1220                                     default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1221                                 }
1222                                 stack.back().ClearContext();
1223                             }
1224                             else
1225                             {
1226                                 if (error_ptr)
1227                                     error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
1228                                                                          pointer_addr,
1229                                                                          error.AsCString());
1230                                 return false;
1231                             }
1232                         }
1233                         else
1234                         {
1235                             if (error_ptr)
1236                                 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1237                             return false;
1238                         }
1239                     }
1240                     else
1241                     {
1242                         if (error_ptr)
1243                             error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1244                         return false;
1245                     }
1246                     break;
1247 
1248                 default:
1249                     break;
1250                 }
1251 
1252             }
1253             break;
1254 
1255         //----------------------------------------------------------------------
1256         // OPCODE: DW_OP_xderef_size
1257         // OPERANDS: 1
1258         //  1 - uint8_t that specifies the size of the data to dereference.
1259         // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1260         // the top of the stack is treated as an address. The second stack
1261         // entry is treated as an "address space identifier" for those
1262         // architectures that support multiple address spaces. The top two
1263         // stack elements are popped, a data item is retrieved through an
1264         // implementation-defined address calculation and pushed as the new
1265         // stack top. In the DW_OP_xderef_size operation, however, the size in
1266         // bytes of the data retrieved from the dereferenced address is
1267         // specified by the single operand. This operand is a 1-byte unsigned
1268         // integral constant whose value may not be larger than the size of an
1269         // address on the target machine. The data retrieved is zero extended
1270         // to the size of an address on the target machine before being pushed
1271         // on the expression stack.
1272         //----------------------------------------------------------------------
1273         case DW_OP_xderef_size:
1274             if (error_ptr)
1275                 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1276             return false;
1277         //----------------------------------------------------------------------
1278         // OPCODE: DW_OP_xderef
1279         // OPERANDS: none
1280         // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1281         // the top of the stack is treated as an address. The second stack entry
1282         // is treated as an "address space identifier" for those architectures
1283         // that support multiple address spaces. The top two stack elements are
1284         // popped, a data item is retrieved through an implementation-defined
1285         // address calculation and pushed as the new stack top. The size of the
1286         // data retrieved from the dereferenced address is the size of an address
1287         // on the target machine.
1288         //----------------------------------------------------------------------
1289         case DW_OP_xderef:
1290             if (error_ptr)
1291                 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1292             return false;
1293 
1294         //----------------------------------------------------------------------
1295         // All DW_OP_constXXX opcodes have a single operand as noted below:
1296         //
1297         // Opcode           Operand 1
1298         // ---------------  ----------------------------------------------------
1299         // DW_OP_const1u    1-byte unsigned integer constant
1300         // DW_OP_const1s    1-byte signed integer constant
1301         // DW_OP_const2u    2-byte unsigned integer constant
1302         // DW_OP_const2s    2-byte signed integer constant
1303         // DW_OP_const4u    4-byte unsigned integer constant
1304         // DW_OP_const4s    4-byte signed integer constant
1305         // DW_OP_const8u    8-byte unsigned integer constant
1306         // DW_OP_const8s    8-byte signed integer constant
1307         // DW_OP_constu     unsigned LEB128 integer constant
1308         // DW_OP_consts     signed LEB128 integer constant
1309         //----------------------------------------------------------------------
1310         case DW_OP_const1u             :    stack.push_back(Scalar(( uint8_t)opcodes.GetU8 (&offset))); break;
1311         case DW_OP_const1s             :    stack.push_back(Scalar((  int8_t)opcodes.GetU8 (&offset))); break;
1312         case DW_OP_const2u             :    stack.push_back(Scalar((uint16_t)opcodes.GetU16 (&offset))); break;
1313         case DW_OP_const2s             :    stack.push_back(Scalar(( int16_t)opcodes.GetU16 (&offset))); break;
1314         case DW_OP_const4u             :    stack.push_back(Scalar((uint32_t)opcodes.GetU32 (&offset))); break;
1315         case DW_OP_const4s             :    stack.push_back(Scalar(( int32_t)opcodes.GetU32 (&offset))); break;
1316         case DW_OP_const8u             :    stack.push_back(Scalar((uint64_t)opcodes.GetU64 (&offset))); break;
1317         case DW_OP_const8s             :    stack.push_back(Scalar(( int64_t)opcodes.GetU64 (&offset))); break;
1318         case DW_OP_constu              :    stack.push_back(Scalar(opcodes.GetULEB128 (&offset))); break;
1319         case DW_OP_consts              :    stack.push_back(Scalar(opcodes.GetSLEB128 (&offset))); break;
1320 
1321         //----------------------------------------------------------------------
1322         // OPCODE: DW_OP_dup
1323         // OPERANDS: none
1324         // DESCRIPTION: duplicates the value at the top of the stack
1325         //----------------------------------------------------------------------
1326         case DW_OP_dup:
1327             if (stack.empty())
1328             {
1329                 if (error_ptr)
1330                     error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1331                 return false;
1332             }
1333             else
1334                 stack.push_back(stack.back());
1335             break;
1336 
1337         //----------------------------------------------------------------------
1338         // OPCODE: DW_OP_drop
1339         // OPERANDS: none
1340         // DESCRIPTION: pops the value at the top of the stack
1341         //----------------------------------------------------------------------
1342         case DW_OP_drop:
1343             if (stack.empty())
1344             {
1345                 if (error_ptr)
1346                     error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1347                 return false;
1348             }
1349             else
1350                 stack.pop_back();
1351             break;
1352 
1353         //----------------------------------------------------------------------
1354         // OPCODE: DW_OP_over
1355         // OPERANDS: none
1356         // DESCRIPTION: Duplicates the entry currently second in the stack at
1357         // the top of the stack.
1358         //----------------------------------------------------------------------
1359         case DW_OP_over:
1360             if (stack.size() < 2)
1361             {
1362                 if (error_ptr)
1363                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1364                 return false;
1365             }
1366             else
1367                 stack.push_back(stack[stack.size() - 2]);
1368             break;
1369 
1370 
1371         //----------------------------------------------------------------------
1372         // OPCODE: DW_OP_pick
1373         // OPERANDS: uint8_t index into the current stack
1374         // DESCRIPTION: The stack entry with the specified index (0 through 255,
1375         // inclusive) is pushed on the stack
1376         //----------------------------------------------------------------------
1377         case DW_OP_pick:
1378             {
1379                 uint8_t pick_idx = opcodes.GetU8(&offset);
1380                 if (pick_idx < stack.size())
1381                     stack.push_back(stack[pick_idx]);
1382                 else
1383                 {
1384                     if (error_ptr)
1385                         error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1386                     return false;
1387                 }
1388             }
1389             break;
1390 
1391         //----------------------------------------------------------------------
1392         // OPCODE: DW_OP_swap
1393         // OPERANDS: none
1394         // DESCRIPTION: swaps the top two stack entries. The entry at the top
1395         // of the stack becomes the second stack entry, and the second entry
1396         // becomes the top of the stack
1397         //----------------------------------------------------------------------
1398         case DW_OP_swap:
1399             if (stack.size() < 2)
1400             {
1401                 if (error_ptr)
1402                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1403                 return false;
1404             }
1405             else
1406             {
1407                 tmp = stack.back();
1408                 stack.back() = stack[stack.size() - 2];
1409                 stack[stack.size() - 2] = tmp;
1410             }
1411             break;
1412 
1413         //----------------------------------------------------------------------
1414         // OPCODE: DW_OP_rot
1415         // OPERANDS: none
1416         // DESCRIPTION: Rotates the first three stack entries. The entry at
1417         // the top of the stack becomes the third stack entry, the second
1418         // entry becomes the top of the stack, and the third entry becomes
1419         // the second entry.
1420         //----------------------------------------------------------------------
1421         case DW_OP_rot:
1422             if (stack.size() < 3)
1423             {
1424                 if (error_ptr)
1425                     error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1426                 return false;
1427             }
1428             else
1429             {
1430                 size_t last_idx = stack.size() - 1;
1431                 Value old_top = stack[last_idx];
1432                 stack[last_idx] = stack[last_idx - 1];
1433                 stack[last_idx - 1] = stack[last_idx - 2];
1434                 stack[last_idx - 2] = old_top;
1435             }
1436             break;
1437 
1438         //----------------------------------------------------------------------
1439         // OPCODE: DW_OP_abs
1440         // OPERANDS: none
1441         // DESCRIPTION: pops the top stack entry, interprets it as a signed
1442         // value and pushes its absolute value. If the absolute value can not be
1443         // represented, the result is undefined.
1444         //----------------------------------------------------------------------
1445         case DW_OP_abs:
1446             if (stack.empty())
1447             {
1448                 if (error_ptr)
1449                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1450                 return false;
1451             }
1452             else if (stack.back().ResolveValue(exe_ctx, ast_context).AbsoluteValue() == false)
1453             {
1454                 if (error_ptr)
1455                     error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1456                 return false;
1457             }
1458             break;
1459 
1460         //----------------------------------------------------------------------
1461         // OPCODE: DW_OP_and
1462         // OPERANDS: none
1463         // DESCRIPTION: pops the top two stack values, performs a bitwise and
1464         // operation on the two, and pushes the result.
1465         //----------------------------------------------------------------------
1466         case DW_OP_and:
1467             if (stack.size() < 2)
1468             {
1469                 if (error_ptr)
1470                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1471                 return false;
1472             }
1473             else
1474             {
1475                 tmp = stack.back();
1476                 stack.pop_back();
1477                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) & tmp.ResolveValue(exe_ctx, ast_context);
1478             }
1479             break;
1480 
1481         //----------------------------------------------------------------------
1482         // OPCODE: DW_OP_div
1483         // OPERANDS: none
1484         // DESCRIPTION: pops the top two stack values, divides the former second
1485         // entry by the former top of the stack using signed division, and
1486         // pushes the result.
1487         //----------------------------------------------------------------------
1488         case DW_OP_div:
1489             if (stack.size() < 2)
1490             {
1491                 if (error_ptr)
1492                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1493                 return false;
1494             }
1495             else
1496             {
1497                 tmp = stack.back();
1498                 if (tmp.ResolveValue(exe_ctx, ast_context).IsZero())
1499                 {
1500                     if (error_ptr)
1501                         error_ptr->SetErrorString("Divide by zero.");
1502                     return false;
1503                 }
1504                 else
1505                 {
1506                     stack.pop_back();
1507                     stack.back() = stack.back().ResolveValue(exe_ctx, ast_context) / tmp.ResolveValue(exe_ctx, ast_context);
1508                     if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1509                     {
1510                         if (error_ptr)
1511                             error_ptr->SetErrorString("Divide failed.");
1512                         return false;
1513                     }
1514                 }
1515             }
1516             break;
1517 
1518         //----------------------------------------------------------------------
1519         // OPCODE: DW_OP_minus
1520         // OPERANDS: none
1521         // DESCRIPTION: pops the top two stack values, subtracts the former top
1522         // of the stack from the former second entry, and pushes the result.
1523         //----------------------------------------------------------------------
1524         case DW_OP_minus:
1525             if (stack.size() < 2)
1526             {
1527                 if (error_ptr)
1528                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1529                 return false;
1530             }
1531             else
1532             {
1533                 tmp = stack.back();
1534                 stack.pop_back();
1535                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) - tmp.ResolveValue(exe_ctx, ast_context);
1536             }
1537             break;
1538 
1539         //----------------------------------------------------------------------
1540         // OPCODE: DW_OP_mod
1541         // OPERANDS: none
1542         // DESCRIPTION: pops the top two stack values and pushes the result of
1543         // the calculation: former second stack entry modulo the former top of
1544         // the stack.
1545         //----------------------------------------------------------------------
1546         case DW_OP_mod:
1547             if (stack.size() < 2)
1548             {
1549                 if (error_ptr)
1550                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1551                 return false;
1552             }
1553             else
1554             {
1555                 tmp = stack.back();
1556                 stack.pop_back();
1557                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) % tmp.ResolveValue(exe_ctx, ast_context);
1558             }
1559             break;
1560 
1561 
1562         //----------------------------------------------------------------------
1563         // OPCODE: DW_OP_mul
1564         // OPERANDS: none
1565         // DESCRIPTION: pops the top two stack entries, multiplies them
1566         // together, and pushes the result.
1567         //----------------------------------------------------------------------
1568         case DW_OP_mul:
1569             if (stack.size() < 2)
1570             {
1571                 if (error_ptr)
1572                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1573                 return false;
1574             }
1575             else
1576             {
1577                 tmp = stack.back();
1578                 stack.pop_back();
1579                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) * tmp.ResolveValue(exe_ctx, ast_context);
1580             }
1581             break;
1582 
1583         //----------------------------------------------------------------------
1584         // OPCODE: DW_OP_neg
1585         // OPERANDS: none
1586         // DESCRIPTION: pops the top stack entry, and pushes its negation.
1587         //----------------------------------------------------------------------
1588         case DW_OP_neg:
1589             if (stack.empty())
1590             {
1591                 if (error_ptr)
1592                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1593                 return false;
1594             }
1595             else
1596             {
1597                 if (stack.back().ResolveValue(exe_ctx, ast_context).UnaryNegate() == false)
1598                 {
1599                     if (error_ptr)
1600                         error_ptr->SetErrorString("Unary negate failed.");
1601                     return false;
1602                 }
1603             }
1604             break;
1605 
1606         //----------------------------------------------------------------------
1607         // OPCODE: DW_OP_not
1608         // OPERANDS: none
1609         // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1610         // complement
1611         //----------------------------------------------------------------------
1612         case DW_OP_not:
1613             if (stack.empty())
1614             {
1615                 if (error_ptr)
1616                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1617                 return false;
1618             }
1619             else
1620             {
1621                 if (stack.back().ResolveValue(exe_ctx, ast_context).OnesComplement() == false)
1622                 {
1623                     if (error_ptr)
1624                         error_ptr->SetErrorString("Logical NOT failed.");
1625                     return false;
1626                 }
1627             }
1628             break;
1629 
1630         //----------------------------------------------------------------------
1631         // OPCODE: DW_OP_or
1632         // OPERANDS: none
1633         // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1634         // operation on the two, and pushes the result.
1635         //----------------------------------------------------------------------
1636         case DW_OP_or:
1637             if (stack.size() < 2)
1638             {
1639                 if (error_ptr)
1640                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1641                 return false;
1642             }
1643             else
1644             {
1645                 tmp = stack.back();
1646                 stack.pop_back();
1647                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) | tmp.ResolveValue(exe_ctx, ast_context);
1648             }
1649             break;
1650 
1651         //----------------------------------------------------------------------
1652         // OPCODE: DW_OP_plus
1653         // OPERANDS: none
1654         // DESCRIPTION: pops the top two stack entries, adds them together, and
1655         // pushes the result.
1656         //----------------------------------------------------------------------
1657         case DW_OP_plus:
1658             if (stack.size() < 2)
1659             {
1660                 if (error_ptr)
1661                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1662                 return false;
1663             }
1664             else
1665             {
1666                 tmp = stack.back();
1667                 stack.pop_back();
1668                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) + tmp.ResolveValue(exe_ctx, ast_context);
1669             }
1670             break;
1671 
1672         //----------------------------------------------------------------------
1673         // OPCODE: DW_OP_plus_uconst
1674         // OPERANDS: none
1675         // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1676         // constant operand and pushes the result.
1677         //----------------------------------------------------------------------
1678         case DW_OP_plus_uconst:
1679             if (stack.empty())
1680             {
1681                 if (error_ptr)
1682                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1683                 return false;
1684             }
1685             else
1686             {
1687                 uint32_t uconst_value = opcodes.GetULEB128(&offset);
1688                 // Implicit conversion from a UINT to a Scalar...
1689                 stack.back().ResolveValue(exe_ctx, ast_context) += uconst_value;
1690                 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1691                 {
1692                     if (error_ptr)
1693                         error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1694                     return false;
1695                 }
1696             }
1697             break;
1698 
1699         //----------------------------------------------------------------------
1700         // OPCODE: DW_OP_shl
1701         // OPERANDS: none
1702         // DESCRIPTION:  pops the top two stack entries, shifts the former
1703         // second entry left by the number of bits specified by the former top
1704         // of the stack, and pushes the result.
1705         //----------------------------------------------------------------------
1706         case DW_OP_shl:
1707             if (stack.size() < 2)
1708             {
1709                 if (error_ptr)
1710                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
1711                 return false;
1712             }
1713             else
1714             {
1715                 tmp = stack.back();
1716                 stack.pop_back();
1717                 stack.back().ResolveValue(exe_ctx, ast_context) <<= tmp.ResolveValue(exe_ctx, ast_context);
1718             }
1719             break;
1720 
1721         //----------------------------------------------------------------------
1722         // OPCODE: DW_OP_shr
1723         // OPERANDS: none
1724         // DESCRIPTION: pops the top two stack entries, shifts the former second
1725         // entry right logically (filling with zero bits) by the number of bits
1726         // specified by the former top of the stack, and pushes the result.
1727         //----------------------------------------------------------------------
1728         case DW_OP_shr:
1729             if (stack.size() < 2)
1730             {
1731                 if (error_ptr)
1732                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
1733                 return false;
1734             }
1735             else
1736             {
1737                 tmp = stack.back();
1738                 stack.pop_back();
1739                 if (stack.back().ResolveValue(exe_ctx, ast_context).ShiftRightLogical(tmp.ResolveValue(exe_ctx, ast_context)) == false)
1740                 {
1741                     if (error_ptr)
1742                         error_ptr->SetErrorString("DW_OP_shr failed.");
1743                     return false;
1744                 }
1745             }
1746             break;
1747 
1748         //----------------------------------------------------------------------
1749         // OPCODE: DW_OP_shra
1750         // OPERANDS: none
1751         // DESCRIPTION: pops the top two stack entries, shifts the former second
1752         // entry right arithmetically (divide the magnitude by 2, keep the same
1753         // sign for the result) by the number of bits specified by the former
1754         // top of the stack, and pushes the result.
1755         //----------------------------------------------------------------------
1756         case DW_OP_shra:
1757             if (stack.size() < 2)
1758             {
1759                 if (error_ptr)
1760                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
1761                 return false;
1762             }
1763             else
1764             {
1765                 tmp = stack.back();
1766                 stack.pop_back();
1767                 stack.back().ResolveValue(exe_ctx, ast_context) >>= tmp.ResolveValue(exe_ctx, ast_context);
1768             }
1769             break;
1770 
1771         //----------------------------------------------------------------------
1772         // OPCODE: DW_OP_xor
1773         // OPERANDS: none
1774         // DESCRIPTION: pops the top two stack entries, performs the bitwise
1775         // exclusive-or operation on the two, and pushes the result.
1776         //----------------------------------------------------------------------
1777         case DW_OP_xor:
1778             if (stack.size() < 2)
1779             {
1780                 if (error_ptr)
1781                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
1782                 return false;
1783             }
1784             else
1785             {
1786                 tmp = stack.back();
1787                 stack.pop_back();
1788                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) ^ tmp.ResolveValue(exe_ctx, ast_context);
1789             }
1790             break;
1791 
1792 
1793         //----------------------------------------------------------------------
1794         // OPCODE: DW_OP_skip
1795         // OPERANDS: int16_t
1796         // DESCRIPTION:  An unconditional branch. Its single operand is a 2-byte
1797         // signed integer constant. The 2-byte constant is the number of bytes
1798         // of the DWARF expression to skip forward or backward from the current
1799         // operation, beginning after the 2-byte constant.
1800         //----------------------------------------------------------------------
1801         case DW_OP_skip:
1802             {
1803                 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1804                 uint32_t new_offset = offset + skip_offset;
1805                 if (new_offset >= opcodes_offset && new_offset < end_offset)
1806                     offset = new_offset;
1807                 else
1808                 {
1809                     if (error_ptr)
1810                         error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1811                     return false;
1812                 }
1813             }
1814             break;
1815 
1816         //----------------------------------------------------------------------
1817         // OPCODE: DW_OP_bra
1818         // OPERANDS: int16_t
1819         // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1820         // signed integer constant. This operation pops the top of stack. If
1821         // the value popped is not the constant 0, the 2-byte constant operand
1822         // is the number of bytes of the DWARF expression to skip forward or
1823         // backward from the current operation, beginning after the 2-byte
1824         // constant.
1825         //----------------------------------------------------------------------
1826         case DW_OP_bra:
1827             {
1828                 tmp = stack.back();
1829                 stack.pop_back();
1830                 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1831                 Scalar zero(0);
1832                 if (tmp.ResolveValue(exe_ctx, ast_context) != zero)
1833                 {
1834                     uint32_t new_offset = offset + bra_offset;
1835                     if (new_offset >= opcodes_offset && new_offset < end_offset)
1836                         offset = new_offset;
1837                     else
1838                     {
1839                         if (error_ptr)
1840                             error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1841                         return false;
1842                     }
1843                 }
1844             }
1845             break;
1846 
1847         //----------------------------------------------------------------------
1848         // OPCODE: DW_OP_eq
1849         // OPERANDS: none
1850         // DESCRIPTION: pops the top two stack values, compares using the
1851         // equals (==) operator.
1852         // STACK RESULT: push the constant value 1 onto the stack if the result
1853         // of the operation is true or the constant value 0 if the result of the
1854         // operation is false.
1855         //----------------------------------------------------------------------
1856         case DW_OP_eq:
1857             if (stack.size() < 2)
1858             {
1859                 if (error_ptr)
1860                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
1861                 return false;
1862             }
1863             else
1864             {
1865                 tmp = stack.back();
1866                 stack.pop_back();
1867                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) == tmp.ResolveValue(exe_ctx, ast_context);
1868             }
1869             break;
1870 
1871         //----------------------------------------------------------------------
1872         // OPCODE: DW_OP_ge
1873         // OPERANDS: none
1874         // DESCRIPTION: pops the top two stack values, compares using the
1875         // greater than or equal to (>=) operator.
1876         // STACK RESULT: push the constant value 1 onto the stack if the result
1877         // of the operation is true or the constant value 0 if the result of the
1878         // operation is false.
1879         //----------------------------------------------------------------------
1880         case DW_OP_ge:
1881             if (stack.size() < 2)
1882             {
1883                 if (error_ptr)
1884                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
1885                 return false;
1886             }
1887             else
1888             {
1889                 tmp = stack.back();
1890                 stack.pop_back();
1891                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) >= tmp.ResolveValue(exe_ctx, ast_context);
1892             }
1893             break;
1894 
1895         //----------------------------------------------------------------------
1896         // OPCODE: DW_OP_gt
1897         // OPERANDS: none
1898         // DESCRIPTION: pops the top two stack values, compares using the
1899         // greater than (>) operator.
1900         // STACK RESULT: push the constant value 1 onto the stack if the result
1901         // of the operation is true or the constant value 0 if the result of the
1902         // operation is false.
1903         //----------------------------------------------------------------------
1904         case DW_OP_gt:
1905             if (stack.size() < 2)
1906             {
1907                 if (error_ptr)
1908                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
1909                 return false;
1910             }
1911             else
1912             {
1913                 tmp = stack.back();
1914                 stack.pop_back();
1915                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) > tmp.ResolveValue(exe_ctx, ast_context);
1916             }
1917             break;
1918 
1919         //----------------------------------------------------------------------
1920         // OPCODE: DW_OP_le
1921         // OPERANDS: none
1922         // DESCRIPTION: pops the top two stack values, compares using the
1923         // less than or equal to (<=) operator.
1924         // STACK RESULT: push the constant value 1 onto the stack if the result
1925         // of the operation is true or the constant value 0 if the result of the
1926         // operation is false.
1927         //----------------------------------------------------------------------
1928         case DW_OP_le:
1929             if (stack.size() < 2)
1930             {
1931                 if (error_ptr)
1932                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
1933                 return false;
1934             }
1935             else
1936             {
1937                 tmp = stack.back();
1938                 stack.pop_back();
1939                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) <= tmp.ResolveValue(exe_ctx, ast_context);
1940             }
1941             break;
1942 
1943         //----------------------------------------------------------------------
1944         // OPCODE: DW_OP_lt
1945         // OPERANDS: none
1946         // DESCRIPTION: pops the top two stack values, compares using the
1947         // less than (<) operator.
1948         // STACK RESULT: push the constant value 1 onto the stack if the result
1949         // of the operation is true or the constant value 0 if the result of the
1950         // operation is false.
1951         //----------------------------------------------------------------------
1952         case DW_OP_lt:
1953             if (stack.size() < 2)
1954             {
1955                 if (error_ptr)
1956                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
1957                 return false;
1958             }
1959             else
1960             {
1961                 tmp = stack.back();
1962                 stack.pop_back();
1963                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) < tmp.ResolveValue(exe_ctx, ast_context);
1964             }
1965             break;
1966 
1967         //----------------------------------------------------------------------
1968         // OPCODE: DW_OP_ne
1969         // OPERANDS: none
1970         // DESCRIPTION: pops the top two stack values, compares using the
1971         // not equal (!=) operator.
1972         // STACK RESULT: push the constant value 1 onto the stack if the result
1973         // of the operation is true or the constant value 0 if the result of the
1974         // operation is false.
1975         //----------------------------------------------------------------------
1976         case DW_OP_ne:
1977             if (stack.size() < 2)
1978             {
1979                 if (error_ptr)
1980                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
1981                 return false;
1982             }
1983             else
1984             {
1985                 tmp = stack.back();
1986                 stack.pop_back();
1987                 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) != tmp.ResolveValue(exe_ctx, ast_context);
1988             }
1989             break;
1990 
1991         //----------------------------------------------------------------------
1992         // OPCODE: DW_OP_litn
1993         // OPERANDS: none
1994         // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1995         // STACK RESULT: push the unsigned literal constant value onto the top
1996         // of the stack.
1997         //----------------------------------------------------------------------
1998         case DW_OP_lit0:
1999         case DW_OP_lit1:
2000         case DW_OP_lit2:
2001         case DW_OP_lit3:
2002         case DW_OP_lit4:
2003         case DW_OP_lit5:
2004         case DW_OP_lit6:
2005         case DW_OP_lit7:
2006         case DW_OP_lit8:
2007         case DW_OP_lit9:
2008         case DW_OP_lit10:
2009         case DW_OP_lit11:
2010         case DW_OP_lit12:
2011         case DW_OP_lit13:
2012         case DW_OP_lit14:
2013         case DW_OP_lit15:
2014         case DW_OP_lit16:
2015         case DW_OP_lit17:
2016         case DW_OP_lit18:
2017         case DW_OP_lit19:
2018         case DW_OP_lit20:
2019         case DW_OP_lit21:
2020         case DW_OP_lit22:
2021         case DW_OP_lit23:
2022         case DW_OP_lit24:
2023         case DW_OP_lit25:
2024         case DW_OP_lit26:
2025         case DW_OP_lit27:
2026         case DW_OP_lit28:
2027         case DW_OP_lit29:
2028         case DW_OP_lit30:
2029         case DW_OP_lit31:
2030             stack.push_back(Scalar(op - DW_OP_lit0));
2031             break;
2032 
2033         //----------------------------------------------------------------------
2034         // OPCODE: DW_OP_regN
2035         // OPERANDS: none
2036         // DESCRIPTION: Push the value in register n on the top of the stack.
2037         //----------------------------------------------------------------------
2038         case DW_OP_reg0:
2039         case DW_OP_reg1:
2040         case DW_OP_reg2:
2041         case DW_OP_reg3:
2042         case DW_OP_reg4:
2043         case DW_OP_reg5:
2044         case DW_OP_reg6:
2045         case DW_OP_reg7:
2046         case DW_OP_reg8:
2047         case DW_OP_reg9:
2048         case DW_OP_reg10:
2049         case DW_OP_reg11:
2050         case DW_OP_reg12:
2051         case DW_OP_reg13:
2052         case DW_OP_reg14:
2053         case DW_OP_reg15:
2054         case DW_OP_reg16:
2055         case DW_OP_reg17:
2056         case DW_OP_reg18:
2057         case DW_OP_reg19:
2058         case DW_OP_reg20:
2059         case DW_OP_reg21:
2060         case DW_OP_reg22:
2061         case DW_OP_reg23:
2062         case DW_OP_reg24:
2063         case DW_OP_reg25:
2064         case DW_OP_reg26:
2065         case DW_OP_reg27:
2066         case DW_OP_reg28:
2067         case DW_OP_reg29:
2068         case DW_OP_reg30:
2069         case DW_OP_reg31:
2070             {
2071                 reg_num = op - DW_OP_reg0;
2072 
2073                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2074                     stack.push_back(tmp);
2075                 else
2076                     return false;
2077             }
2078             break;
2079         //----------------------------------------------------------------------
2080         // OPCODE: DW_OP_regx
2081         // OPERANDS:
2082         //      ULEB128 literal operand that encodes the register.
2083         // DESCRIPTION: Push the value in register on the top of the stack.
2084         //----------------------------------------------------------------------
2085         case DW_OP_regx:
2086             {
2087                 reg_num = opcodes.GetULEB128(&offset);
2088                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2089                     stack.push_back(tmp);
2090                 else
2091                     return false;
2092             }
2093             break;
2094 
2095         //----------------------------------------------------------------------
2096         // OPCODE: DW_OP_bregN
2097         // OPERANDS:
2098         //      SLEB128 offset from register N
2099         // DESCRIPTION: Value is in memory at the address specified by register
2100         // N plus an offset.
2101         //----------------------------------------------------------------------
2102         case DW_OP_breg0:
2103         case DW_OP_breg1:
2104         case DW_OP_breg2:
2105         case DW_OP_breg3:
2106         case DW_OP_breg4:
2107         case DW_OP_breg5:
2108         case DW_OP_breg6:
2109         case DW_OP_breg7:
2110         case DW_OP_breg8:
2111         case DW_OP_breg9:
2112         case DW_OP_breg10:
2113         case DW_OP_breg11:
2114         case DW_OP_breg12:
2115         case DW_OP_breg13:
2116         case DW_OP_breg14:
2117         case DW_OP_breg15:
2118         case DW_OP_breg16:
2119         case DW_OP_breg17:
2120         case DW_OP_breg18:
2121         case DW_OP_breg19:
2122         case DW_OP_breg20:
2123         case DW_OP_breg21:
2124         case DW_OP_breg22:
2125         case DW_OP_breg23:
2126         case DW_OP_breg24:
2127         case DW_OP_breg25:
2128         case DW_OP_breg26:
2129         case DW_OP_breg27:
2130         case DW_OP_breg28:
2131         case DW_OP_breg29:
2132         case DW_OP_breg30:
2133         case DW_OP_breg31:
2134             {
2135                 reg_num = op - DW_OP_breg0;
2136 
2137                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2138                 {
2139                     int64_t breg_offset = opcodes.GetSLEB128(&offset);
2140                     tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
2141                     stack.push_back(tmp);
2142                     stack.back().SetValueType (Value::eValueTypeLoadAddress);
2143                 }
2144                 else
2145                     return false;
2146             }
2147             break;
2148         //----------------------------------------------------------------------
2149         // OPCODE: DW_OP_bregx
2150         // OPERANDS: 2
2151         //      ULEB128 literal operand that encodes the register.
2152         //      SLEB128 offset from register N
2153         // DESCRIPTION: Value is in memory at the address specified by register
2154         // N plus an offset.
2155         //----------------------------------------------------------------------
2156         case DW_OP_bregx:
2157             {
2158                 reg_num = opcodes.GetULEB128(&offset);
2159 
2160                 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2161                 {
2162                     int64_t breg_offset = opcodes.GetSLEB128(&offset);
2163                     tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
2164                     stack.push_back(tmp);
2165                     stack.back().SetValueType (Value::eValueTypeLoadAddress);
2166                 }
2167                 else
2168                     return false;
2169             }
2170             break;
2171 
2172         case DW_OP_fbreg:
2173             if (exe_ctx && exe_ctx->frame)
2174             {
2175                 Scalar value;
2176                 if (exe_ctx->frame->GetFrameBaseValue(value, error_ptr))
2177                 {
2178                     int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2179                     value += fbreg_offset;
2180                     stack.push_back(value);
2181                     stack.back().SetValueType (Value::eValueTypeLoadAddress);
2182                 }
2183                 else
2184                     return false;
2185             }
2186             else
2187             {
2188                 if (error_ptr)
2189                     error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
2190                 return false;
2191             }
2192             break;
2193 
2194         //----------------------------------------------------------------------
2195         // OPCODE: DW_OP_nop
2196         // OPERANDS: none
2197         // DESCRIPTION: A place holder. It has no effect on the location stack
2198         // or any of its values.
2199         //----------------------------------------------------------------------
2200         case DW_OP_nop:
2201             break;
2202 
2203         //----------------------------------------------------------------------
2204         // OPCODE: DW_OP_piece
2205         // OPERANDS: 1
2206         //      ULEB128: byte size of the piece
2207         // DESCRIPTION: The operand describes the size in bytes of the piece of
2208         // the object referenced by the DWARF expression whose result is at the
2209         // top of the stack. If the piece is located in a register, but does not
2210         // occupy the entire register, the placement of the piece within that
2211         // register is defined by the ABI.
2212         //
2213         // Many compilers store a single variable in sets of registers, or store
2214         // a variable partially in memory and partially in registers.
2215         // DW_OP_piece provides a way of describing how large a part of a
2216         // variable a particular DWARF expression refers to.
2217         //----------------------------------------------------------------------
2218         case DW_OP_piece:
2219             if (error_ptr)
2220                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_piece.");
2221             return false;
2222 
2223         //----------------------------------------------------------------------
2224         // OPCODE: DW_OP_push_object_address
2225         // OPERANDS: none
2226         // DESCRIPTION: Pushes the address of the object currently being
2227         // evaluated as part of evaluation of a user presented expression.
2228         // This object may correspond to an independent variable described by
2229         // its own DIE or it may be a component of an array, structure, or class
2230         // whose address has been dynamically determined by an earlier step
2231         // during user expression evaluation.
2232         //----------------------------------------------------------------------
2233         case DW_OP_push_object_address:
2234             if (error_ptr)
2235                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address.");
2236             return false;
2237 
2238         //----------------------------------------------------------------------
2239         // OPCODE: DW_OP_call2
2240         // OPERANDS:
2241         //      uint16_t compile unit relative offset of a DIE
2242         // DESCRIPTION: Performs subroutine calls during evaluation
2243         // of a DWARF expression. The operand is the 2-byte unsigned offset
2244         // of a debugging information entry in the current compilation unit.
2245         //
2246         // Operand interpretation is exactly like that for DW_FORM_ref2.
2247         //
2248         // This operation transfers control of DWARF expression evaluation
2249         // to the DW_AT_location attribute of the referenced DIE. If there is
2250         // no such attribute, then there is no effect. Execution of the DWARF
2251         // expression of a DW_AT_location attribute may add to and/or remove from
2252         // values on the stack. Execution returns to the point following the call
2253         // when the end of the attribute is reached. Values on the stack at the
2254         // time of the call may be used as parameters by the called expression
2255         // and values left on the stack by the called expression may be used as
2256         // return values by prior agreement between the calling and called
2257         // expressions.
2258         //----------------------------------------------------------------------
2259         case DW_OP_call2:
2260             if (error_ptr)
2261                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2262             return false;
2263         //----------------------------------------------------------------------
2264         // OPCODE: DW_OP_call4
2265         // OPERANDS: 1
2266         //      uint32_t compile unit relative offset of a DIE
2267         // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2268         // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2269         // of a debugging information entry in  the current compilation unit.
2270         //
2271         // Operand interpretation DW_OP_call4 is exactly like that for
2272         // DW_FORM_ref4.
2273         //
2274         // This operation transfers control of DWARF expression evaluation
2275         // to the DW_AT_location attribute of the referenced DIE. If there is
2276         // no such attribute, then there is no effect. Execution of the DWARF
2277         // expression of a DW_AT_location attribute may add to and/or remove from
2278         // values on the stack. Execution returns to the point following the call
2279         // when the end of the attribute is reached. Values on the stack at the
2280         // time of the call may be used as parameters by the called expression
2281         // and values left on the stack by the called expression may be used as
2282         // return values by prior agreement between the calling and called
2283         // expressions.
2284         //----------------------------------------------------------------------
2285         case DW_OP_call4:
2286             if (error_ptr)
2287                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2288             return false;
2289 
2290 
2291         //----------------------------------------------------------------------
2292         // OPCODE: DW_OP_call_ref
2293         // OPERANDS:
2294         //      uint32_t absolute DIE offset for 32-bit DWARF or a uint64_t
2295         //               absolute DIE offset for 64 bit DWARF.
2296         // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2297         // expression. Takes a single operand. In the 32-bit DWARF format, the
2298         // operand is a 4-byte unsigned value; in the 64-bit DWARF format, it
2299         // is an 8-byte unsigned value. The operand is used as the offset of a
2300         // debugging information entry in a .debug_info section which may be
2301         // contained in a shared object for executable other than that
2302         // containing the operator. For references from one shared object or
2303         // executable to another, the relocation must be performed by the
2304         // consumer.
2305         //
2306         // Operand interpretation of DW_OP_call_ref is exactly like that for
2307         // DW_FORM_ref_addr.
2308         //
2309         // This operation transfers control of DWARF expression evaluation
2310         // to the DW_AT_location attribute of the referenced DIE. If there is
2311         // no such attribute, then there is no effect. Execution of the DWARF
2312         // expression of a DW_AT_location attribute may add to and/or remove from
2313         // values on the stack. Execution returns to the point following the call
2314         // when the end of the attribute is reached. Values on the stack at the
2315         // time of the call may be used as parameters by the called expression
2316         // and values left on the stack by the called expression may be used as
2317         // return values by prior agreement between the calling and called
2318         // expressions.
2319         //----------------------------------------------------------------------
2320         case DW_OP_call_ref:
2321             if (error_ptr)
2322                 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call_ref.");
2323             return false;
2324 
2325         //----------------------------------------------------------------------
2326         // OPCODE: DW_OP_APPLE_array_ref
2327         // OPERANDS: none
2328         // DESCRIPTION: Pops a value off the stack and uses it as the array
2329         // index.  Pops a second value off the stack and uses it as the array
2330         // itself.  Pushes a value onto the stack representing the element of
2331         // the array specified by the index.
2332         //----------------------------------------------------------------------
2333         case DW_OP_APPLE_array_ref:
2334             {
2335                 if (stack.size() < 2)
2336                 {
2337                     if (error_ptr)
2338                         error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_array_ref.");
2339                     return false;
2340                 }
2341 
2342                 Value index_val = stack.back();
2343                 stack.pop_back();
2344                 Value array_val = stack.back();
2345                 stack.pop_back();
2346 
2347                 Scalar &index_scalar = index_val.ResolveValue(exe_ctx, ast_context);
2348                 int64_t index = index_scalar.SLongLong(LLONG_MAX);
2349 
2350                 if (index == LLONG_MAX)
2351                 {
2352                     if (error_ptr)
2353                         error_ptr->SetErrorString("Invalid array index.");
2354                     return false;
2355                 }
2356 
2357                 if (array_val.GetContextType() != Value::eContextTypeClangType)
2358                 {
2359                     if (error_ptr)
2360                         error_ptr->SetErrorString("Arrays without Clang types are unhandled at this time.");
2361                     return false;
2362                 }
2363 
2364                 if (array_val.GetValueType() != Value::eValueTypeLoadAddress &&
2365                     array_val.GetValueType() != Value::eValueTypeHostAddress)
2366                 {
2367                     if (error_ptr)
2368                         error_ptr->SetErrorString("Array must be stored in memory.");
2369                     return false;
2370                 }
2371 
2372                 void *array_type = array_val.GetClangType();
2373 
2374                 void *member_type;
2375                 uint64_t size = 0;
2376 
2377                 if ((!ClangASTContext::IsPointerType(array_type, &member_type)) &&
2378                     (!ClangASTContext::IsArrayType(array_type, &member_type, &size)))
2379                 {
2380                     if (error_ptr)
2381                         error_ptr->SetErrorString("Array reference from something that is neither a pointer nor an array.");
2382                     return false;
2383                 }
2384 
2385                 if (size && (index >= size || index < 0))
2386                 {
2387                     if (error_ptr)
2388                         error_ptr->SetErrorStringWithFormat("Out of bounds array access.  %lld is not in [0, %llu]", index, size);
2389                     return false;
2390                 }
2391 
2392                 uint64_t member_bit_size = ClangASTType::GetClangTypeBitWidth(ast_context, member_type);
2393                 uint64_t member_bit_align = ClangASTType::GetTypeBitAlign(ast_context, member_type);
2394                 uint64_t member_bit_incr = ((member_bit_size + member_bit_align - 1) / member_bit_align) * member_bit_align;
2395                 if (member_bit_incr % 8)
2396                 {
2397                     if (error_ptr)
2398                         error_ptr->SetErrorStringWithFormat("Array increment is not byte aligned", index, size);
2399                     return false;
2400                 }
2401                 int64_t member_offset = (int64_t)(member_bit_incr / 8) * index;
2402 
2403                 Value member;
2404 
2405                 member.SetContext(Value::eContextTypeClangType, member_type);
2406                 member.SetValueType(array_val.GetValueType());
2407 
2408                 addr_t array_base = (addr_t)array_val.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2409                 addr_t member_loc = array_base + member_offset;
2410                 member.GetScalar() = (uint64_t)member_loc;
2411 
2412                 stack.push_back(member);
2413             }
2414             break;
2415 
2416         //----------------------------------------------------------------------
2417         // OPCODE: DW_OP_APPLE_uninit
2418         // OPERANDS: none
2419         // DESCRIPTION: Lets us know that the value is currently not initialized
2420         //----------------------------------------------------------------------
2421         case DW_OP_APPLE_uninit:
2422             //return eResultTypeErrorUninitialized;
2423             break;  // Ignore this as we have seen cases where this value is incorrectly added
2424 
2425         //----------------------------------------------------------------------
2426         // OPCODE: DW_OP_APPLE_assign
2427         // OPERANDS: none
2428         // DESCRIPTION: Pops a value off of the stack and assigns it to the next
2429         // item on the stack which must be something assignable (inferior
2430         // Variable, inferior Type with address, inferior register, or
2431         // expression local variable.
2432         //----------------------------------------------------------------------
2433         case DW_OP_APPLE_assign:
2434             if (stack.size() < 2)
2435             {
2436                 if (error_ptr)
2437                     error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_assign.");
2438                 return false;
2439             }
2440             else
2441             {
2442                 tmp = stack.back();
2443                 stack.pop_back();
2444                 Value::ContextType context_type = stack.back().GetContextType();
2445                 StreamString new_value(Stream::eBinary, 4, lldb::endian::InlHostByteOrder());
2446                 switch (context_type)
2447                 {
2448                 case Value::eContextTypeClangType:
2449                     {
2450                         void *clang_type = stack.back().GetClangType();
2451 
2452                         if (ClangASTContext::IsAggregateType (clang_type))
2453                         {
2454                             Value::ValueType source_value_type = tmp.GetValueType();
2455                             Value::ValueType target_value_type = stack.back().GetValueType();
2456 
2457                             addr_t source_addr = (addr_t)tmp.GetScalar().ULongLong();
2458                             addr_t target_addr = (addr_t)stack.back().GetScalar().ULongLong();
2459 
2460                             size_t byte_size = (ClangASTType::GetClangTypeBitWidth(ast_context, clang_type) + 7) / 8;
2461 
2462                             switch (source_value_type)
2463                             {
2464                             case Value::eValueTypeScalar:
2465                             case Value::eValueTypeFileAddress:
2466                                 break;
2467 
2468                             case Value::eValueTypeLoadAddress:
2469                                 switch (target_value_type)
2470                                 {
2471                                 case Value::eValueTypeLoadAddress:
2472                                     {
2473                                         DataBufferHeap data;
2474                                         data.SetByteSize(byte_size);
2475 
2476                                         Error error;
2477                                         if (exe_ctx->process->ReadMemory (source_addr, data.GetBytes(), byte_size, error) != byte_size)
2478                                         {
2479                                             if (error_ptr)
2480                                                 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2481                                             return false;
2482                                         }
2483 
2484                                         if (exe_ctx->process->WriteMemory (target_addr, data.GetBytes(), byte_size, error) != byte_size)
2485                                         {
2486                                             if (error_ptr)
2487                                                 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2488                                             return false;
2489                                         }
2490                                     }
2491                                     break;
2492                                 case Value::eValueTypeHostAddress:
2493                                     if (exe_ctx->process->GetByteOrder() != lldb::endian::InlHostByteOrder())
2494                                     {
2495                                         if (error_ptr)
2496                                             error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2497                                         return false;
2498                                     }
2499                                     else
2500                                     {
2501                                         Error error;
2502                                         if (exe_ctx->process->ReadMemory (source_addr, (uint8_t*)target_addr, byte_size, error) != byte_size)
2503                                         {
2504                                             if (error_ptr)
2505                                                 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2506                                             return false;
2507                                         }
2508                                     }
2509                                     break;
2510                                 default:
2511                                     return false;
2512                                 }
2513                                 break;
2514                             case Value::eValueTypeHostAddress:
2515                                 switch (target_value_type)
2516                                 {
2517                                 case Value::eValueTypeLoadAddress:
2518                                     if (exe_ctx->process->GetByteOrder() != lldb::endian::InlHostByteOrder())
2519                                     {
2520                                         if (error_ptr)
2521                                             error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2522                                         return false;
2523                                     }
2524                                     else
2525                                     {
2526                                         Error error;
2527                                         if (exe_ctx->process->WriteMemory (target_addr, (uint8_t*)source_addr, byte_size, error) != byte_size)
2528                                         {
2529                                             if (error_ptr)
2530                                                 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2531                                             return false;
2532                                         }
2533                                     }
2534                                 case Value::eValueTypeHostAddress:
2535                                     memcpy ((uint8_t*)target_addr, (uint8_t*)source_addr, byte_size);
2536                                     break;
2537                                 default:
2538                                     return false;
2539                                 }
2540                             }
2541                         }
2542                         else
2543                         {
2544                             if (!ClangASTType::SetValueFromScalar (ast_context,
2545                                                                   clang_type,
2546                                                                   tmp.ResolveValue(exe_ctx, ast_context),
2547                                                                   new_value))
2548                             {
2549                                 if (error_ptr)
2550                                     error_ptr->SetErrorStringWithFormat ("Couldn't extract a value from an integral type.\n");
2551                                 return false;
2552                             }
2553 
2554                             Value::ValueType value_type = stack.back().GetValueType();
2555 
2556                             switch (value_type)
2557                             {
2558                             case Value::eValueTypeLoadAddress:
2559                             case Value::eValueTypeHostAddress:
2560                                 {
2561                                     AddressType address_type = (value_type == Value::eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost);
2562                                     lldb::addr_t addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2563                                     if (!ClangASTType::WriteToMemory (ast_context,
2564                                                                           clang_type,
2565                                                                           exe_ctx,
2566                                                                           addr,
2567                                                                           address_type,
2568                                                                           new_value))
2569                                     {
2570                                         if (error_ptr)
2571                                             error_ptr->SetErrorStringWithFormat ("Failed to write value to memory at 0x%llx.\n", addr);
2572                                         return false;
2573                                     }
2574                                 }
2575                                 break;
2576 
2577                             default:
2578                                 break;
2579                             }
2580                         }
2581                     }
2582                     break;
2583 
2584                 default:
2585                     if (error_ptr)
2586                         error_ptr->SetErrorString ("Assign failed.");
2587                     return false;
2588                 }
2589             }
2590             break;
2591 
2592         //----------------------------------------------------------------------
2593         // OPCODE: DW_OP_APPLE_address_of
2594         // OPERANDS: none
2595         // DESCRIPTION: Pops a value off of the stack and pushed its address.
2596         // The top item on the stack must be a variable, or already be a memory
2597         // location.
2598         //----------------------------------------------------------------------
2599         case DW_OP_APPLE_address_of:
2600             if (stack.empty())
2601             {
2602                 if (error_ptr)
2603                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_address_of.");
2604                 return false;
2605             }
2606             else
2607             {
2608                 Value::ValueType value_type = stack.back().GetValueType();
2609                 switch (value_type)
2610                 {
2611                 default:
2612                 case Value::eValueTypeScalar:      // raw scalar value
2613                     if (error_ptr)
2614                         error_ptr->SetErrorString("Top stack item isn't a memory based object.");
2615                     return false;
2616 
2617                 case Value::eValueTypeLoadAddress: // load address value
2618                 case Value::eValueTypeFileAddress: // file address value
2619                 case Value::eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
2620                     // Taking the address of an object reduces it to the address
2621                     // of the value and removes any extra context it had.
2622                     //stack.back().SetValueType(Value::eValueTypeScalar);
2623                     stack.back().ClearContext();
2624                     break;
2625                 }
2626             }
2627             break;
2628 
2629         //----------------------------------------------------------------------
2630         // OPCODE: DW_OP_APPLE_value_of
2631         // OPERANDS: none
2632         // DESCRIPTION: Pops a value off of the stack and pushed its value.
2633         // The top item on the stack must be a variable, expression variable.
2634         //----------------------------------------------------------------------
2635         case DW_OP_APPLE_value_of:
2636             if (stack.empty())
2637             {
2638                 if (error_ptr)
2639                     error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_value_of.");
2640                 return false;
2641             }
2642             else if (!stack.back().ValueOf(exe_ctx, ast_context))
2643             {
2644                 if (error_ptr)
2645                     error_ptr->SetErrorString ("Top stack item isn't a valid candidate for DW_OP_APPLE_value_of.");
2646                 return false;
2647             }
2648             break;
2649 
2650         //----------------------------------------------------------------------
2651         // OPCODE: DW_OP_APPLE_deref_type
2652         // OPERANDS: none
2653         // DESCRIPTION: gets the value pointed to by the top stack item
2654         //----------------------------------------------------------------------
2655         case DW_OP_APPLE_deref_type:
2656             {
2657                 if (stack.empty())
2658                 {
2659                     if (error_ptr)
2660                         error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_deref_type.");
2661                     return false;
2662                 }
2663 
2664                 tmp = stack.back();
2665                 stack.pop_back();
2666 
2667                 if (tmp.GetContextType() != Value::eContextTypeClangType)
2668                 {
2669                     if (error_ptr)
2670                         error_ptr->SetErrorString("Item at top of expression stack must have a Clang type");
2671                     return false;
2672                 }
2673 
2674                 void *ptr_type = tmp.GetClangType();
2675                 void *target_type;
2676 
2677                 if (!ClangASTContext::IsPointerType(ptr_type, &target_type))
2678                 {
2679                     if (error_ptr)
2680                         error_ptr->SetErrorString("Dereferencing a non-pointer type");
2681                     return false;
2682                 }
2683 
2684                 // TODO do we want all pointers to be dereferenced as load addresses?
2685                 Value::ValueType value_type = tmp.GetValueType();
2686 
2687                 tmp.ResolveValue(exe_ctx, ast_context);
2688 
2689                 tmp.SetValueType(value_type);
2690                 tmp.SetContext(Value::eContextTypeClangType, target_type);
2691 
2692                 stack.push_back(tmp);
2693             }
2694             break;
2695 
2696         //----------------------------------------------------------------------
2697         // OPCODE: DW_OP_APPLE_expr_local
2698         // OPERANDS: ULEB128
2699         // DESCRIPTION: pushes the expression local variable index onto the
2700         // stack and set the appropriate context so we know the stack item is
2701         // an expression local variable index.
2702         //----------------------------------------------------------------------
2703         case DW_OP_APPLE_expr_local:
2704             {
2705                 /*
2706                 uint32_t idx = opcodes.GetULEB128(&offset);
2707                 if (expr_locals == NULL)
2708                 {
2709                     if (error_ptr)
2710                         error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) opcode encountered with no local variable list.\n", idx);
2711                     return false;
2712                 }
2713                 Value *expr_local_variable = expr_locals->GetVariableAtIndex(idx);
2714                 if (expr_local_variable == NULL)
2715                 {
2716                     if (error_ptr)
2717                         error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) with invalid index %u.\n", idx, idx);
2718                     return false;
2719                 }
2720                  // The proxy code has been removed. If it is ever re-added, please
2721                  // use shared pointers or return by value to avoid possible memory
2722                  // leak (there is no leak here, but in general, no returning pointers
2723                  // that must be manually freed please.
2724                 Value *proxy = expr_local_variable->CreateProxy();
2725                 stack.push_back(*proxy);
2726                 delete proxy;
2727                 //stack.back().SetContext (Value::eContextTypeClangType, expr_local_variable->GetClangType());
2728                 */
2729             }
2730             break;
2731 
2732         //----------------------------------------------------------------------
2733         // OPCODE: DW_OP_APPLE_extern
2734         // OPERANDS: ULEB128
2735         // DESCRIPTION: pushes a proxy for the extern object index onto the
2736         // stack.
2737         //----------------------------------------------------------------------
2738         case DW_OP_APPLE_extern:
2739             {
2740                 /*
2741                 uint32_t idx = opcodes.GetULEB128(&offset);
2742                 if (!decl_map)
2743                 {
2744                     if (error_ptr)
2745                         error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) opcode encountered with no decl map.\n", idx);
2746                     return false;
2747                 }
2748                 Value *extern_var = decl_map->GetValueForIndex(idx);
2749                 if (!extern_var)
2750                 {
2751                     if (error_ptr)
2752                         error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) with invalid index %u.\n", idx, idx);
2753                     return false;
2754                 }
2755                 // The proxy code has been removed. If it is ever re-added, please
2756                 // use shared pointers or return by value to avoid possible memory
2757                 // leak (there is no leak here, but in general, no returning pointers
2758                 // that must be manually freed please.
2759                 Value *proxy = extern_var->CreateProxy();
2760                 stack.push_back(*proxy);
2761                 delete proxy;
2762                 */
2763             }
2764             break;
2765 
2766         case DW_OP_APPLE_scalar_cast:
2767             if (stack.empty())
2768             {
2769                 if (error_ptr)
2770                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_scalar_cast.");
2771                 return false;
2772             }
2773             else
2774             {
2775                 // Simple scalar cast
2776                 if (!stack.back().ResolveValue(exe_ctx, ast_context).Cast((Scalar::Type)opcodes.GetU8(&offset)))
2777                 {
2778                     if (error_ptr)
2779                         error_ptr->SetErrorString("Cast failed.");
2780                     return false;
2781                 }
2782             }
2783             break;
2784 
2785 
2786         case DW_OP_APPLE_clang_cast:
2787             if (stack.empty())
2788             {
2789                 if (error_ptr)
2790                     error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_clang_cast.");
2791                 return false;
2792             }
2793             else
2794             {
2795                 void *clang_type = (void *)opcodes.GetMaxU64(&offset, sizeof(void*));
2796                 stack.back().SetContext (Value::eContextTypeClangType, clang_type);
2797             }
2798             break;
2799         //----------------------------------------------------------------------
2800         // OPCODE: DW_OP_APPLE_constf
2801         // OPERANDS: 1 byte float length, followed by that many bytes containing
2802         // the constant float data.
2803         // DESCRIPTION: Push a float value onto the expression stack.
2804         //----------------------------------------------------------------------
2805         case DW_OP_APPLE_constf:        // 0xF6 - 1 byte float size, followed by constant float data
2806             {
2807                 uint8_t float_length = opcodes.GetU8(&offset);
2808                 if (sizeof(float) == float_length)
2809                     tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetFloat (&offset);
2810                 else if (sizeof(double) == float_length)
2811                     tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetDouble (&offset);
2812                 else if (sizeof(long double) == float_length)
2813                     tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetLongDouble (&offset);
2814                 else
2815                 {
2816                     StreamString new_value;
2817                     opcodes.Dump(&new_value, offset, eFormatBytes, 1, float_length, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
2818 
2819                      if (error_ptr)
2820                         error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_constf(<%u> %s) unsupported float size.\n", float_length, new_value.GetData());
2821                     return false;
2822                }
2823                tmp.SetValueType(Value::eValueTypeScalar);
2824                tmp.ClearContext();
2825                stack.push_back(tmp);
2826             }
2827             break;
2828         //----------------------------------------------------------------------
2829         // OPCODE: DW_OP_APPLE_clear
2830         // OPERANDS: none
2831         // DESCRIPTION: Clears the expression stack.
2832         //----------------------------------------------------------------------
2833         case DW_OP_APPLE_clear:
2834             stack.clear();
2835             break;
2836 
2837         //----------------------------------------------------------------------
2838         // OPCODE: DW_OP_APPLE_error
2839         // OPERANDS: none
2840         // DESCRIPTION: Pops a value off of the stack and pushed its value.
2841         // The top item on the stack must be a variable, expression variable.
2842         //----------------------------------------------------------------------
2843         case DW_OP_APPLE_error:         // 0xFF - Stops expression evaluation and returns an error (no args)
2844             if (error_ptr)
2845                 error_ptr->SetErrorString ("Generic error.");
2846             return false;
2847         }
2848     }
2849 
2850     if (stack.empty())
2851     {
2852         if (error_ptr)
2853             error_ptr->SetErrorString ("Stack empty after evaluation.");
2854         return false;
2855     }
2856     else if (log)
2857     {
2858         size_t count = stack.size();
2859         log->Printf("Stack after operation has %d values:", count);
2860         for (size_t i=0; i<count; ++i)
2861         {
2862             StreamString new_value;
2863             new_value.Printf("[%zu]", i);
2864             stack[i].Dump(&new_value);
2865             log->Printf("  %s", new_value.GetData());
2866         }
2867     }
2868 
2869     result = stack.back();
2870     return true;    // Return true on success
2871 }
2872 
2873