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