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