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