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