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