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