blob: 626e270ab3e538592d6c2dd52f78a0f3de8323db [file] [log] [blame]
Sean Callanan47dc4572011-09-15 02:13:07 +00001//===-- IRInterpreter.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/Core/DataEncoder.h"
11#include "lldb/Core/Log.h"
12#include "lldb/Core/ValueObjectConstResult.h"
13#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan52d0d022012-02-15 01:40:39 +000014#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan47dc4572011-09-15 02:13:07 +000015#include "lldb/Expression/IRForTarget.h"
16#include "lldb/Expression/IRInterpreter.h"
17
18#include "llvm/Constants.h"
19#include "llvm/Function.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Support/raw_ostream.h"
Micah Villmow3051ed72012-10-08 16:28:57 +000023#include "llvm/DataLayout.h"
Sean Callanan47dc4572011-09-15 02:13:07 +000024
25#include <map>
26
27using namespace llvm;
28
29IRInterpreter::IRInterpreter(lldb_private::ClangExpressionDeclMap &decl_map,
30 lldb_private::Stream *error_stream) :
Daniel Maleab9db9d52012-12-07 22:21:08 +000031 m_decl_map(decl_map)
Sean Callanan47dc4572011-09-15 02:13:07 +000032{
33
34}
35
36IRInterpreter::~IRInterpreter()
37{
38
39}
40
41static std::string
42PrintValue(const Value *value, bool truncate = false)
43{
44 std::string s;
45 raw_string_ostream rso(s);
46 value->print(rso);
47 rso.flush();
48 if (truncate)
49 s.resize(s.length() - 1);
50
51 size_t offset;
52 while ((offset = s.find('\n')) != s.npos)
53 s.erase(offset, 1);
54 while (s[0] == ' ' || s[0] == '\t')
55 s.erase(0, 1);
56
57 return s;
58}
59
60static std::string
61PrintType(const Type *type, bool truncate = false)
62{
63 std::string s;
64 raw_string_ostream rso(s);
65 type->print(rso);
66 rso.flush();
67 if (truncate)
68 s.resize(s.length() - 1);
69 return s;
70}
71
Greg Clayton598df882012-03-14 03:07:05 +000072typedef STD_SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP;
73typedef STD_SHARED_PTR(lldb_private::DataExtractor) DataExtractorSP;
Sean Callanan47dc4572011-09-15 02:13:07 +000074
75class Memory
76{
77public:
78 typedef uint32_t index_t;
79
80 struct Allocation
81 {
82 // m_virtual_address is always the address of the variable in the virtual memory
83 // space provided by Memory.
84 //
85 // m_origin is always non-NULL and describes the source of the data (possibly
86 // m_data if this allocation is the authoritative source).
87 //
88 // Possible value configurations:
89 //
90 // Allocation type getValueType() getContextType() m_origin->GetScalar() m_data
91 // =========================================================================================================================
92 // FileAddress eValueTypeFileAddress eContextTypeInvalid A location in a binary NULL
93 // image
94 //
95 // LoadAddress eValueTypeLoadAddress eContextTypeInvalid A location in the target's NULL
96 // virtual memory
97 //
98 // Alloca eValueTypeHostAddress eContextTypeInvalid == m_data->GetBytes() Deleted at end of
99 // execution
100 //
101 // PersistentVar eValueTypeHostAddress eContextTypeClangType A persistent variable's NULL
102 // location in LLDB's memory
103 //
104 // Register [ignored] eContextTypeRegister [ignored] Flushed to the register
105 // at the end of execution
106
107 lldb::addr_t m_virtual_address;
108 size_t m_extent;
109 lldb_private::Value m_origin;
110 lldb::DataBufferSP m_data;
111
112 Allocation (lldb::addr_t virtual_address,
113 size_t extent,
114 lldb::DataBufferSP data) :
115 m_virtual_address(virtual_address),
116 m_extent(extent),
117 m_data(data)
118 {
119 }
120
121 Allocation (const Allocation &allocation) :
122 m_virtual_address(allocation.m_virtual_address),
123 m_extent(allocation.m_extent),
124 m_origin(allocation.m_origin),
125 m_data(allocation.m_data)
126 {
127 }
128 };
129
Greg Clayton598df882012-03-14 03:07:05 +0000130 typedef STD_SHARED_PTR(Allocation) AllocationSP;
Sean Callanan47dc4572011-09-15 02:13:07 +0000131
132 struct Region
133 {
134 AllocationSP m_allocation;
135 uint64_t m_base;
136 uint64_t m_extent;
137
138 Region () :
139 m_allocation(),
140 m_base(0),
141 m_extent(0)
142 {
143 }
144
145 Region (AllocationSP allocation, uint64_t base, uint64_t extent) :
146 m_allocation(allocation),
147 m_base(base),
148 m_extent(extent)
149 {
150 }
151
152 Region (const Region &region) :
153 m_allocation(region.m_allocation),
154 m_base(region.m_base),
155 m_extent(region.m_extent)
156 {
157 }
158
159 bool IsValid ()
160 {
Jim Ingham9880efa2012-08-11 00:35:26 +0000161 return (bool) m_allocation;
Sean Callanan47dc4572011-09-15 02:13:07 +0000162 }
163
164 bool IsInvalid ()
165 {
Sean Callananb386d822012-08-09 00:50:26 +0000166 return !m_allocation;
Sean Callanan47dc4572011-09-15 02:13:07 +0000167 }
168 };
169
170 typedef std::vector <AllocationSP> MemoryMap;
171
172private:
173 lldb::addr_t m_addr_base;
174 lldb::addr_t m_addr_max;
175 MemoryMap m_memory;
176 lldb::ByteOrder m_byte_order;
177 lldb::addr_t m_addr_byte_size;
Micah Villmow3051ed72012-10-08 16:28:57 +0000178 DataLayout &m_target_data;
Sean Callanan47dc4572011-09-15 02:13:07 +0000179
180 lldb_private::ClangExpressionDeclMap &m_decl_map;
181
182 MemoryMap::iterator LookupInternal (lldb::addr_t addr)
183 {
184 for (MemoryMap::iterator i = m_memory.begin(), e = m_memory.end();
185 i != e;
186 ++i)
187 {
188 if ((*i)->m_virtual_address <= addr &&
189 (*i)->m_virtual_address + (*i)->m_extent > addr)
190 return i;
191 }
192
193 return m_memory.end();
194 }
195
196public:
Micah Villmow3051ed72012-10-08 16:28:57 +0000197 Memory (DataLayout &target_data,
Sean Callanan47dc4572011-09-15 02:13:07 +0000198 lldb_private::ClangExpressionDeclMap &decl_map,
199 lldb::addr_t alloc_start,
200 lldb::addr_t alloc_max) :
201 m_addr_base(alloc_start),
202 m_addr_max(alloc_max),
203 m_target_data(target_data),
204 m_decl_map(decl_map)
205 {
206 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Micah Villmowce633582012-10-11 17:21:41 +0000207 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan47dc4572011-09-15 02:13:07 +0000208 }
209
210 Region Malloc (size_t size, size_t align)
211 {
212 lldb::DataBufferSP data(new lldb_private::DataBufferHeap(size, 0));
213
214 if (data)
215 {
216 index_t index = m_memory.size();
217
218 const size_t mask = (align - 1);
219
220 m_addr_base += mask;
221 m_addr_base &= ~mask;
222
223 if (m_addr_base + size < m_addr_base ||
224 m_addr_base + size > m_addr_max)
225 return Region();
226
227 uint64_t base = m_addr_base;
228
229 m_memory.push_back(AllocationSP(new Allocation(base, size, data)));
230
231 m_addr_base += size;
232
233 AllocationSP alloc = m_memory[index];
234
235 alloc->m_origin.GetScalar() = (unsigned long long)data->GetBytes();
236 alloc->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
237 alloc->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
238
239 return Region(alloc, base, size);
240 }
241
242 return Region();
243 }
244
245 Region Malloc (Type *type)
246 {
247 return Malloc (m_target_data.getTypeAllocSize(type),
248 m_target_data.getPrefTypeAlignment(type));
249 }
250
251 Region Place (Type *type, lldb::addr_t base, lldb_private::Value &value)
252 {
253 index_t index = m_memory.size();
254 size_t size = m_target_data.getTypeAllocSize(type);
255
256 m_memory.push_back(AllocationSP(new Allocation(base, size, lldb::DataBufferSP())));
257
258 AllocationSP alloc = m_memory[index];
259
260 alloc->m_origin = value;
261
262 return Region(alloc, base, size);
263 }
264
265 void Free (lldb::addr_t addr)
266 {
267 MemoryMap::iterator i = LookupInternal (addr);
268
269 if (i != m_memory.end())
270 m_memory.erase(i);
271 }
272
273 Region Lookup (lldb::addr_t addr, Type *type)
274 {
275 MemoryMap::iterator i = LookupInternal(addr);
276
Sean Callanan740b3b72012-01-11 02:23:25 +0000277 if (i == m_memory.end() || !type->isSized())
Sean Callanan47dc4572011-09-15 02:13:07 +0000278 return Region();
Sean Callanan740b3b72012-01-11 02:23:25 +0000279
280 size_t size = m_target_data.getTypeStoreSize(type);
Sean Callanan47dc4572011-09-15 02:13:07 +0000281
282 return Region(*i, addr, size);
283 }
284
285 DataEncoderSP GetEncoder (Region region)
286 {
287 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
288 return DataEncoderSP();
289
290 lldb::DataBufferSP buffer = region.m_allocation->m_data;
291
292 if (!buffer)
293 return DataEncoderSP();
294
295 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
296
297 return DataEncoderSP(new lldb_private::DataEncoder(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
298 }
299
300 DataExtractorSP GetExtractor (Region region)
301 {
302 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
303 return DataExtractorSP();
304
305 lldb::DataBufferSP buffer = region.m_allocation->m_data;
306 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
307
308 if (buffer)
309 return DataExtractorSP(new lldb_private::DataExtractor(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
310 else
311 return DataExtractorSP(new lldb_private::DataExtractor((uint8_t*)region.m_allocation->m_origin.GetScalar().ULongLong() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
312 }
313
314 lldb_private::Value GetAccessTarget(lldb::addr_t addr)
315 {
316 MemoryMap::iterator i = LookupInternal(addr);
317
318 if (i == m_memory.end())
319 return lldb_private::Value();
320
321 lldb_private::Value target = (*i)->m_origin;
322
323 if (target.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
324 {
325 target.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
326 target.SetValueType(lldb_private::Value::eValueTypeHostAddress);
327 target.GetScalar() = (unsigned long long)(*i)->m_data->GetBytes();
328 }
329
330 target.GetScalar() += (addr - (*i)->m_virtual_address);
331
332 return target;
333 }
334
335 bool Write (lldb::addr_t addr, const uint8_t *data, size_t length)
336 {
337 lldb_private::Value target = GetAccessTarget(addr);
338
339 return m_decl_map.WriteTarget(target, data, length);
340 }
341
342 bool Read (uint8_t *data, lldb::addr_t addr, size_t length)
343 {
Sean Callanan557ccd62011-10-21 05:18:02 +0000344 lldb_private::Value source = GetAccessTarget(addr);
Sean Callanan47dc4572011-09-15 02:13:07 +0000345
Sean Callanan557ccd62011-10-21 05:18:02 +0000346 return m_decl_map.ReadTarget(data, source, length);
347 }
348
349 bool WriteToRawPtr (lldb::addr_t addr, const uint8_t *data, size_t length)
350 {
351 lldb_private::Value target = m_decl_map.WrapBareAddress(addr);
352
353 return m_decl_map.WriteTarget(target, data, length);
354 }
355
356 bool ReadFromRawPtr (uint8_t *data, lldb::addr_t addr, size_t length)
357 {
358 lldb_private::Value source = m_decl_map.WrapBareAddress(addr);
359
360 return m_decl_map.ReadTarget(data, source, length);
Sean Callanan47dc4572011-09-15 02:13:07 +0000361 }
362
363 std::string PrintData (lldb::addr_t addr, size_t length)
364 {
365 lldb_private::Value target = GetAccessTarget(addr);
366
367 lldb_private::DataBufferHeap buf(length, 0);
368
369 if (!m_decl_map.ReadTarget(buf.GetBytes(), target, length))
370 return std::string("<couldn't read data>");
371
372 lldb_private::StreamString ss;
373
374 for (size_t i = 0; i < length; i++)
375 {
376 if ((!(i & 0xf)) && i)
377 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
378 else
379 ss.Printf("%02hhx ", buf.GetBytes()[i]);
380 }
381
382 return ss.GetString();
383 }
384
385 std::string SummarizeRegion (Region &region)
386 {
387 lldb_private::StreamString ss;
388
389 lldb_private::Value base = GetAccessTarget(region.m_base);
390
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000391 ss.Printf("%" PRIx64 " [%s - %s %llx]",
Sean Callanan47dc4572011-09-15 02:13:07 +0000392 region.m_base,
393 lldb_private::Value::GetValueTypeAsCString(base.GetValueType()),
394 lldb_private::Value::GetContextTypeAsCString(base.GetContextType()),
395 base.GetScalar().ULongLong());
396
397 ss.Printf(" %s", PrintData(region.m_base, region.m_extent).c_str());
398
399 return ss.GetString();
400 }
401};
402
403class InterpreterStackFrame
404{
405public:
406 typedef std::map <const Value*, Memory::Region> ValueMap;
407
408 ValueMap m_values;
409 Memory &m_memory;
Micah Villmow3051ed72012-10-08 16:28:57 +0000410 DataLayout &m_target_data;
Sean Callanan47dc4572011-09-15 02:13:07 +0000411 lldb_private::ClangExpressionDeclMap &m_decl_map;
412 const BasicBlock *m_bb;
413 BasicBlock::const_iterator m_ii;
414 BasicBlock::const_iterator m_ie;
415
416 lldb::ByteOrder m_byte_order;
417 size_t m_addr_byte_size;
418
Micah Villmow3051ed72012-10-08 16:28:57 +0000419 InterpreterStackFrame (DataLayout &target_data,
Sean Callanan47dc4572011-09-15 02:13:07 +0000420 Memory &memory,
421 lldb_private::ClangExpressionDeclMap &decl_map) :
Sean Callanan47dc4572011-09-15 02:13:07 +0000422 m_memory (memory),
Daniel Dunbar97c89572011-10-31 22:50:49 +0000423 m_target_data (target_data),
Sean Callanan47dc4572011-09-15 02:13:07 +0000424 m_decl_map (decl_map)
425 {
426 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan4fbe61b2012-10-11 22:00:52 +0000427 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan47dc4572011-09-15 02:13:07 +0000428 }
429
430 void Jump (const BasicBlock *bb)
431 {
432 m_bb = bb;
433 m_ii = m_bb->begin();
434 m_ie = m_bb->end();
435 }
436
437 bool Cache (Memory::AllocationSP allocation, Type *type)
438 {
439 if (allocation->m_origin.GetContextType() != lldb_private::Value::eContextTypeRegisterInfo)
440 return false;
441
442 return m_decl_map.ReadTarget(allocation->m_data->GetBytes(), allocation->m_origin, allocation->m_data->GetByteSize());
443 }
444
445 std::string SummarizeValue (const Value *value)
446 {
447 lldb_private::StreamString ss;
448
449 ss.Printf("%s", PrintValue(value).c_str());
450
451 ValueMap::iterator i = m_values.find(value);
452
453 if (i != m_values.end())
454 {
455 Memory::Region region = i->second;
456
457 ss.Printf(" %s", m_memory.SummarizeRegion(region).c_str());
458 }
459
460 return ss.GetString();
461 }
462
463 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
464 {
465 size_t type_size = m_target_data.getTypeStoreSize(type);
466
467 switch (type_size)
468 {
469 case 1:
470 scalar = (uint8_t)u64value;
471 break;
472 case 2:
473 scalar = (uint16_t)u64value;
474 break;
475 case 4:
476 scalar = (uint32_t)u64value;
477 break;
478 case 8:
479 scalar = (uint64_t)u64value;
480 break;
481 default:
482 return false;
483 }
484
485 return true;
486 }
487
488 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
489 {
490 const Constant *constant = dyn_cast<Constant>(value);
491
492 if (constant)
493 {
494 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
495 {
496 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
497 }
498 }
499 else
500 {
501 Memory::Region region = ResolveValue(value, module);
502 DataExtractorSP value_extractor = m_memory.GetExtractor(region);
503
504 if (!value_extractor)
505 return false;
506
507 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
508
509 uint32_t offset = 0;
510 uint64_t u64value = value_extractor->GetMaxU64(&offset, value_size);
511
512 return AssignToMatchType(scalar, u64value, value->getType());
513 }
514
515 return false;
516 }
517
518 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
519 {
520 Memory::Region region = ResolveValue (value, module);
521
522 lldb_private::Scalar cast_scalar;
523
524 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
525 return false;
526
527 lldb_private::DataBufferHeap buf(cast_scalar.GetByteSize(), 0);
528
529 lldb_private::Error err;
530
531 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, err))
532 return false;
533
534 DataEncoderSP region_encoder = m_memory.GetEncoder(region);
535
536 memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize());
537
538 return true;
539 }
540
Sean Callanan8eac77d2012-02-08 01:27:49 +0000541 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan47dc4572011-09-15 02:13:07 +0000542 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000543 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
544 {
Sean Callanan8eac77d2012-02-08 01:27:49 +0000545 value = constant_int->getValue();
546 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +0000547 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000548 else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
Sean Callanan47dc4572011-09-15 02:13:07 +0000549 {
Sean Callanan8eac77d2012-02-08 01:27:49 +0000550 value = constant_fp->getValueAPF().bitcastToAPInt();
551 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +0000552 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000553 else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
554 {
555 switch (constant_expr->getOpcode())
556 {
Sean Callanan8eac77d2012-02-08 01:27:49 +0000557 default:
558 return false;
559 case Instruction::IntToPtr:
Sean Callanan6b21a9b2012-12-01 00:09:34 +0000560 case Instruction::PtrToInt:
Sean Callanan8eac77d2012-02-08 01:27:49 +0000561 case Instruction::BitCast:
562 return ResolveConstantValue(value, constant_expr->getOperand(0));
563 case Instruction::GetElementPtr:
564 {
565 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
566 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
567
568 Constant *base = dyn_cast<Constant>(*op_cursor);
569
570 if (!base)
571 return false;
572
573 if (!ResolveConstantValue(value, base))
574 return false;
575
576 op_cursor++;
577
578 if (op_cursor == op_end)
579 return true; // no offset to apply!
580
581 SmallVector <Value *, 8> indices (op_cursor, op_end);
582
583 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
584
585 const bool is_signed = true;
586 value += APInt(value.getBitWidth(), offset, is_signed);
587
588 return true;
589 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000590 }
591 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000592
593 return false;
594 }
595
Sean Callanan8eac77d2012-02-08 01:27:49 +0000596 bool ResolveConstant (Memory::Region &region, const Constant *constant)
597 {
598 APInt resolved_value;
599
600 if (!ResolveConstantValue(resolved_value, constant))
601 return false;
602
603 const uint64_t *raw_data = resolved_value.getRawData();
604
605 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
606 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size);
607 }
608
Sean Callanan47dc4572011-09-15 02:13:07 +0000609 Memory::Region ResolveValue (const Value *value, Module &module)
610 {
611 ValueMap::iterator i = m_values.find(value);
612
613 if (i != m_values.end())
614 return i->second;
615
616 const GlobalValue *global_value = dyn_cast<GlobalValue>(value);
617
Sean Callanan4b3cef02011-10-26 21:20:00 +0000618 // If the variable is indirected through the argument
619 // array then we need to build an extra level of indirection
620 // for it. This is the default; only magic arguments like
621 // "this", "self", and "_cmd" are direct.
Sean Callananb3191182012-12-11 22:39:36 +0000622 bool variable_is_this = false;
Sean Callanan4b3cef02011-10-26 21:20:00 +0000623
Sean Callanan47dc4572011-09-15 02:13:07 +0000624 // Attempt to resolve the value using the program's data.
625 // If it is, the values to be created are:
626 //
627 // data_region - a region of memory in which the variable's data resides.
628 // ref_region - a region of memory in which its address (i.e., &var) resides.
629 // In the JIT case, this region would be a member of the struct passed in.
630 // pointer_region - a region of memory in which the address of the pointer
631 // resides. This is an IR-level variable.
632 do
633 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000634 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan4b3cef02011-10-26 21:20:00 +0000635
636 lldb_private::Value resolved_value;
Greg Clayton4a379b12012-07-17 03:23:13 +0000637 lldb_private::ClangExpressionVariable::FlagType flags = 0;
Sean Callanan47dc4572011-09-15 02:13:07 +0000638
Sean Callanan4b3cef02011-10-26 21:20:00 +0000639 if (global_value)
640 {
641 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module);
642
643 if (!decl)
644 break;
645
646 if (isa<clang::FunctionDecl>(decl))
647 {
648 if (log)
649 log->Printf("The interpreter does not handle function pointers at the moment");
650
651 return Memory::Region();
652 }
653
Sean Callanan52d0d022012-02-15 01:40:39 +0000654 resolved_value = m_decl_map.LookupDecl(decl, flags);
Sean Callanan4b3cef02011-10-26 21:20:00 +0000655 }
656 else
657 {
658 // Special-case "this", "self", and "_cmd"
659
Sean Callananfecc09c2011-11-19 02:54:21 +0000660 std::string name_str = value->getName().str();
Sean Callanan4b3cef02011-10-26 21:20:00 +0000661
662 if (name_str == "this" ||
663 name_str == "self" ||
664 name_str == "_cmd")
665 resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str()));
666
Sean Callananb3191182012-12-11 22:39:36 +0000667 variable_is_this = true;
Sean Callanan4b3cef02011-10-26 21:20:00 +0000668 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000669
670 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void)
671 {
672 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
673 {
Sean Callananb3191182012-12-11 22:39:36 +0000674 if (variable_is_this)
675 {
676 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
677
678 lldb_private::Value origin;
679
680 origin.SetValueType(lldb_private::Value::eValueTypeLoadAddress);
681 origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
682 origin.GetScalar() = resolved_value.GetScalar();
683
684 data_region.m_allocation->m_origin = origin;
685
686 Memory::Region ref_region = m_memory.Malloc(value->getType());
687
688 if (ref_region.IsInvalid())
689 return Memory::Region();
690
691 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
692
693 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
694 return Memory::Region();
695
696 if (log)
697 {
698 log->Printf("Made an allocation for \"this\" register variable %s", PrintValue(value).c_str());
699 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
700 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
701 }
702
703 m_values[value] = ref_region;
704 return ref_region;
705 }
706 else if (flags & lldb_private::ClangExpressionVariable::EVBareRegister)
707 {
708 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
709 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
Greg Clayton7c5e22f2012-10-30 18:18:43 +0000710 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
711 m_memory.Malloc(value->getType());
Sean Callanan4b3cef02011-10-26 21:20:00 +0000712
Sean Callananb3191182012-12-11 22:39:36 +0000713 data_region.m_allocation->m_origin = resolved_value;
714 Memory::Region ref_region = m_memory.Malloc(value->getType());
715
716 if (!Cache(data_region.m_allocation, value->getType()))
717 return Memory::Region();
718
719 if (ref_region.IsInvalid())
720 return Memory::Region();
721
722 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
723
724 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
725 return Memory::Region();
726
727 if (log)
728 {
729 log->Printf("Made an allocation for bare register variable %s", PrintValue(value).c_str());
730 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
731 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
732 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
733 }
734
735 m_values[value] = ref_region;
736 return ref_region;
737 }
738 else
739 {
740 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
741 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
742 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
743 m_memory.Malloc(value->getType());
744
745 data_region.m_allocation->m_origin = resolved_value;
746 Memory::Region ref_region = m_memory.Malloc(value->getType());
747 Memory::Region pointer_region;
748
749 pointer_region = m_memory.Malloc(value->getType());
750
751 if (!Cache(data_region.m_allocation, value->getType()))
752 return Memory::Region();
753
754 if (ref_region.IsInvalid())
755 return Memory::Region();
756
757 if (pointer_region.IsInvalid())
758 return Memory::Region();
759
760 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
761
762 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
763 return Memory::Region();
764
765 if (log)
766 {
767 log->Printf("Made an allocation for ordinary register variable %s", PrintValue(value).c_str());
768 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
769 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
770 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
771 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
772 }
773
774 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
775
Sean Callanan4b3cef02011-10-26 21:20:00 +0000776 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
777 return Memory::Region();
778
779 m_values[value] = pointer_region;
780 return pointer_region;
781 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000782 }
783 else
784 {
785 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
786 Memory::Region ref_region = m_memory.Malloc(value->getType());
Sean Callanan4b3cef02011-10-26 21:20:00 +0000787 Memory::Region pointer_region;
788
Sean Callananb3191182012-12-11 22:39:36 +0000789 if (!variable_is_this)
Sean Callanan4b3cef02011-10-26 21:20:00 +0000790 pointer_region = m_memory.Malloc(value->getType());
Sean Callanan47dc4572011-09-15 02:13:07 +0000791
792 if (ref_region.IsInvalid())
793 return Memory::Region();
794
Sean Callananb3191182012-12-11 22:39:36 +0000795 if (pointer_region.IsInvalid() && !variable_is_this)
Sean Callanan47dc4572011-09-15 02:13:07 +0000796 return Memory::Region();
797
798 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
799
800 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
801 return Memory::Region();
802
Sean Callananb3191182012-12-11 22:39:36 +0000803 if (!variable_is_this)
Sean Callanan4b3cef02011-10-26 21:20:00 +0000804 {
805 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
Sean Callanan47dc4572011-09-15 02:13:07 +0000806
Sean Callanan4b3cef02011-10-26 21:20:00 +0000807 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
808 return Memory::Region();
809
810 m_values[value] = pointer_region;
811 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000812
813 if (log)
814 {
Sean Callanan4b3cef02011-10-26 21:20:00 +0000815 log->Printf("Made an allocation for %s", PrintValue(value).c_str());
Sean Callanan47dc4572011-09-15 02:13:07 +0000816 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
817 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
818 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
Sean Callananb3191182012-12-11 22:39:36 +0000819 if (!variable_is_this)
Sean Callanan4b3cef02011-10-26 21:20:00 +0000820 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
Sean Callanan47dc4572011-09-15 02:13:07 +0000821 }
822
Sean Callananb3191182012-12-11 22:39:36 +0000823 if (variable_is_this)
Sean Callanan4b3cef02011-10-26 21:20:00 +0000824 return ref_region;
Sean Callananb3191182012-12-11 22:39:36 +0000825 else
826 return pointer_region;
Sean Callanan47dc4572011-09-15 02:13:07 +0000827 }
828 }
829 }
830 while(0);
831
832 // Fall back and allocate space [allocation type Alloca]
833
834 Type *type = value->getType();
835
836 lldb::ValueSP backing_value(new lldb_private::Value);
837
838 Memory::Region data_region = m_memory.Malloc(type);
839 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes();
840 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
841 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
842
843 const Constant *constant = dyn_cast<Constant>(value);
844
845 do
846 {
847 if (!constant)
848 break;
849
850 if (!ResolveConstant (data_region, constant))
851 return Memory::Region();
852 }
853 while(0);
854
855 m_values[value] = data_region;
856 return data_region;
857 }
858
859 bool ConstructResult (lldb::ClangExpressionVariableSP &result,
860 const GlobalValue *result_value,
861 const lldb_private::ConstString &result_name,
862 lldb_private::TypeFromParser result_type,
863 Module &module)
864 {
865 // The result_value resolves to P, a pointer to a region R containing the result data.
866 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
867
868 if (!result_value)
869 return true; // There was no slot for a result – the expression doesn't return one.
870
871 ValueMap::iterator i = m_values.find(result_value);
872
873 if (i == m_values.end())
874 return false; // There was a slot for the result, but we didn't write into it.
875
876 Memory::Region P = i->second;
877 DataExtractorSP P_extractor = m_memory.GetExtractor(P);
878
879 if (!P_extractor)
880 return false;
881
882 Type *pointer_ty = result_value->getType();
883 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
884 if (!pointer_ptr_ty)
885 return false;
886 Type *R_ty = pointer_ptr_ty->getElementType();
887
888 uint32_t offset = 0;
889 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
890
891 Memory::Region R = m_memory.Lookup(pointer, R_ty);
892
893 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress ||
894 !R.m_allocation->m_data)
895 return false;
896
897 lldb_private::Value base;
898
Sean Callanana8428a42011-09-22 00:41:11 +0000899 bool transient = false;
Sean Callanan557ccd62011-10-21 05:18:02 +0000900 bool maybe_make_load = false;
Sean Callanana8428a42011-09-22 00:41:11 +0000901
Sean Callanan47dc4572011-09-15 02:13:07 +0000902 if (m_decl_map.ResultIsReference(result_name))
903 {
904 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
905 if (!R_ptr_ty)
906 return false;
907 Type *R_final_ty = R_ptr_ty->getElementType();
908
909 DataExtractorSP R_extractor = m_memory.GetExtractor(R);
910
911 if (!R_extractor)
912 return false;
913
914 offset = 0;
915 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset);
916
917 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty);
918
Sean Callanan557ccd62011-10-21 05:18:02 +0000919 if (R_final.m_allocation)
920 {
921 if (R_final.m_allocation->m_data)
922 transient = true; // this is a stack allocation
Sean Callanan47dc4572011-09-15 02:13:07 +0000923
Sean Callanan557ccd62011-10-21 05:18:02 +0000924 base = R_final.m_allocation->m_origin;
925 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address);
926 }
927 else
928 {
929 // We got a bare pointer. We are going to treat it as a load address
930 // or a file address, letting decl_map make the choice based on whether
931 // or not a process exists.
932
933 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
934 base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
935 base.GetScalar() = (unsigned long long)R_pointer;
936 maybe_make_load = true;
937 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000938 }
939 else
940 {
941 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
942 base.SetValueType(lldb_private::Value::eValueTypeHostAddress);
943 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address);
944 }
945
Sean Callanan557ccd62011-10-21 05:18:02 +0000946 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load);
Sean Callanan47dc4572011-09-15 02:13:07 +0000947 }
948};
949
950bool
951IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result,
952 const lldb_private::ConstString &result_name,
953 lldb_private::TypeFromParser result_type,
954 Function &llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000955 Module &llvm_module,
956 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000957{
Sean Callananddf110d2012-01-24 22:06:48 +0000958 if (supportsFunction (llvm_function, err))
Sean Callanan47dc4572011-09-15 02:13:07 +0000959 return runOnFunction(result,
960 result_name,
961 result_type,
962 llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000963 llvm_module,
964 err);
Sean Callanan47dc4572011-09-15 02:13:07 +0000965 else
966 return false;
967}
968
Sean Callananddf110d2012-01-24 22:06:48 +0000969static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
970static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
971static const char *interpreter_internal_error = "Interpreter encountered an internal error";
972static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
973static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
974static const char *memory_write_error = "Interpreter couldn't write to memory";
975static const char *memory_read_error = "Interpreter couldn't read from memory";
976static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan8f2e3922012-02-04 08:49:35 +0000977static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callananddf110d2012-01-24 22:06:48 +0000978
Sean Callanan47dc4572011-09-15 02:13:07 +0000979bool
Sean Callananddf110d2012-01-24 22:06:48 +0000980IRInterpreter::supportsFunction (Function &llvm_function,
981 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000982{
983 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
984
985 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
986 bbi != bbe;
987 ++bbi)
988 {
989 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
990 ii != ie;
991 ++ii)
992 {
993 switch (ii->getOpcode())
994 {
995 default:
996 {
997 if (log)
998 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +0000999 err.SetErrorToGenericError();
1000 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001001 return false;
1002 }
1003 case Instruction::Add:
1004 case Instruction::Alloca:
1005 case Instruction::BitCast:
1006 case Instruction::Br:
1007 case Instruction::GetElementPtr:
1008 break;
1009 case Instruction::ICmp:
1010 {
1011 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
1012
1013 if (!icmp_inst)
Sean Callananddf110d2012-01-24 22:06:48 +00001014 {
1015 err.SetErrorToGenericError();
1016 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001017 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001018 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001019
1020 switch (icmp_inst->getPredicate())
1021 {
1022 default:
1023 {
1024 if (log)
1025 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001026
1027 err.SetErrorToGenericError();
1028 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001029 return false;
1030 }
1031 case CmpInst::ICMP_EQ:
1032 case CmpInst::ICMP_NE:
1033 case CmpInst::ICMP_UGT:
1034 case CmpInst::ICMP_UGE:
1035 case CmpInst::ICMP_ULT:
1036 case CmpInst::ICMP_ULE:
1037 case CmpInst::ICMP_SGT:
1038 case CmpInst::ICMP_SGE:
1039 case CmpInst::ICMP_SLT:
1040 case CmpInst::ICMP_SLE:
1041 break;
1042 }
1043 }
1044 break;
Sean Callanan557ccd62011-10-21 05:18:02 +00001045 case Instruction::IntToPtr:
Sean Callanan6b21a9b2012-12-01 00:09:34 +00001046 case Instruction::PtrToInt:
Sean Callanan47dc4572011-09-15 02:13:07 +00001047 case Instruction::Load:
1048 case Instruction::Mul:
1049 case Instruction::Ret:
1050 case Instruction::SDiv:
1051 case Instruction::Store:
1052 case Instruction::Sub:
1053 case Instruction::UDiv:
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001054 case Instruction::ZExt:
Sean Callanan47dc4572011-09-15 02:13:07 +00001055 break;
1056 }
1057 }
1058 }
1059
1060 return true;
1061}
1062
1063bool
1064IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result,
1065 const lldb_private::ConstString &result_name,
1066 lldb_private::TypeFromParser result_type,
1067 Function &llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +00001068 Module &llvm_module,
1069 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +00001070{
1071 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1072
1073 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo();
1074
1075 if (!target_info.IsValid())
Sean Callananddf110d2012-01-24 22:06:48 +00001076 {
1077 err.SetErrorToGenericError();
1078 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001079 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001080 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001081
1082 lldb::addr_t alloc_min;
1083 lldb::addr_t alloc_max;
1084
1085 switch (target_info.address_byte_size)
1086 {
1087 default:
Sean Callananddf110d2012-01-24 22:06:48 +00001088 err.SetErrorToGenericError();
1089 err.SetErrorString(interpreter_initialization_error);
1090 return false;
Sean Callanan47dc4572011-09-15 02:13:07 +00001091 case 4:
1092 alloc_min = 0x00001000llu;
1093 alloc_max = 0x0000ffffllu;
1094 break;
1095 case 8:
1096 alloc_min = 0x0000000000001000llu;
1097 alloc_max = 0x000000000000ffffllu;
1098 break;
1099 }
1100
Micah Villmow3051ed72012-10-08 16:28:57 +00001101 DataLayout target_data(&llvm_module);
Sean Callanan4fbe61b2012-10-11 22:00:52 +00001102 if (target_data.getPointerSize(0) != target_info.address_byte_size)
Sean Callananddf110d2012-01-24 22:06:48 +00001103 {
1104 err.SetErrorToGenericError();
1105 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001106 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001107 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001108 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle))
Sean Callananddf110d2012-01-24 22:06:48 +00001109 {
1110 err.SetErrorToGenericError();
1111 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001112 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001113 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001114
1115 Memory memory(target_data, m_decl_map, alloc_min, alloc_max);
1116 InterpreterStackFrame frame(target_data, memory, m_decl_map);
1117
1118 uint32_t num_insts = 0;
1119
1120 frame.Jump(llvm_function.begin());
1121
1122 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1123 {
1124 const Instruction *inst = frame.m_ii;
1125
1126 if (log)
1127 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1128
1129 switch (inst->getOpcode())
1130 {
1131 default:
1132 break;
1133 case Instruction::Add:
1134 case Instruction::Sub:
1135 case Instruction::Mul:
1136 case Instruction::SDiv:
1137 case Instruction::UDiv:
1138 {
1139 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1140
1141 if (!bin_op)
1142 {
1143 if (log)
1144 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
Sean Callananddf110d2012-01-24 22:06:48 +00001145 err.SetErrorToGenericError();
1146 err.SetErrorString(interpreter_internal_error);
1147 return false;
Sean Callanan47dc4572011-09-15 02:13:07 +00001148 }
1149
1150 Value *lhs = inst->getOperand(0);
1151 Value *rhs = inst->getOperand(1);
1152
1153 lldb_private::Scalar L;
1154 lldb_private::Scalar R;
1155
1156 if (!frame.EvaluateValue(L, lhs, llvm_module))
1157 {
1158 if (log)
1159 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001160 err.SetErrorToGenericError();
1161 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001162 return false;
1163 }
1164
1165 if (!frame.EvaluateValue(R, rhs, llvm_module))
1166 {
1167 if (log)
1168 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001169 err.SetErrorToGenericError();
1170 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001171 return false;
1172 }
1173
1174 lldb_private::Scalar result;
1175
1176 switch (inst->getOpcode())
1177 {
1178 default:
1179 break;
1180 case Instruction::Add:
1181 result = L + R;
1182 break;
1183 case Instruction::Mul:
1184 result = L * R;
1185 break;
1186 case Instruction::Sub:
1187 result = L - R;
1188 break;
1189 case Instruction::SDiv:
1190 result = L / R;
1191 break;
1192 case Instruction::UDiv:
1193 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1194 break;
1195 }
1196
1197 frame.AssignValue(inst, result, llvm_module);
1198
1199 if (log)
1200 {
1201 log->Printf("Interpreted a %s", inst->getOpcodeName());
1202 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1203 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1204 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1205 }
1206 }
1207 break;
1208 case Instruction::Alloca:
1209 {
1210 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1211
1212 if (!alloca_inst)
1213 {
1214 if (log)
1215 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001216 err.SetErrorToGenericError();
1217 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001218 return false;
1219 }
1220
1221 if (alloca_inst->isArrayAllocation())
1222 {
1223 if (log)
1224 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
Sean Callananddf110d2012-01-24 22:06:48 +00001225 err.SetErrorToGenericError();
1226 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001227 return false;
1228 }
1229
1230 // The semantics of Alloca are:
1231 // Create a region R of virtual memory of type T, backed by a data buffer
1232 // Create a region P of virtual memory of type T*, backed by a data buffer
1233 // Write the virtual address of R into P
1234
1235 Type *T = alloca_inst->getAllocatedType();
1236 Type *Tptr = alloca_inst->getType();
1237
1238 Memory::Region R = memory.Malloc(T);
1239
1240 if (R.IsInvalid())
1241 {
1242 if (log)
1243 log->Printf("Couldn't allocate memory for an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001244 err.SetErrorToGenericError();
1245 err.SetErrorString(memory_allocation_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001246 return false;
1247 }
1248
1249 Memory::Region P = memory.Malloc(Tptr);
1250
1251 if (P.IsInvalid())
1252 {
1253 if (log)
1254 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001255 err.SetErrorToGenericError();
1256 err.SetErrorString(memory_allocation_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001257 return false;
1258 }
1259
1260 DataEncoderSP P_encoder = memory.GetEncoder(P);
1261
1262 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX)
1263 {
1264 if (log)
Sean Callananddf110d2012-01-24 22:06:48 +00001265 log->Printf("Couldn't write the result pointer for an AllocaInst");
1266 err.SetErrorToGenericError();
1267 err.SetErrorString(memory_write_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001268 return false;
1269 }
1270
1271 frame.m_values[alloca_inst] = P;
1272
1273 if (log)
1274 {
1275 log->Printf("Interpreted an AllocaInst");
1276 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1277 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str());
1278 }
1279 }
1280 break;
1281 case Instruction::BitCast:
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001282 case Instruction::ZExt:
Sean Callanan47dc4572011-09-15 02:13:07 +00001283 {
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001284 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sean Callanan47dc4572011-09-15 02:13:07 +00001285
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001286 if (!cast_inst)
Sean Callanan47dc4572011-09-15 02:13:07 +00001287 {
1288 if (log)
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001289 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
Sean Callananddf110d2012-01-24 22:06:48 +00001290 err.SetErrorToGenericError();
1291 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001292 return false;
1293 }
1294
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001295 Value *source = cast_inst->getOperand(0);
Sean Callanan47dc4572011-09-15 02:13:07 +00001296
1297 lldb_private::Scalar S;
1298
1299 if (!frame.EvaluateValue(S, source, llvm_module))
1300 {
1301 if (log)
1302 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001303 err.SetErrorToGenericError();
1304 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001305 return false;
1306 }
1307
1308 frame.AssignValue(inst, S, llvm_module);
1309 }
1310 break;
1311 case Instruction::Br:
1312 {
1313 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
1314
1315 if (!br_inst)
1316 {
1317 if (log)
1318 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001319 err.SetErrorToGenericError();
1320 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001321 return false;
1322 }
1323
1324 if (br_inst->isConditional())
1325 {
1326 Value *condition = br_inst->getCondition();
1327
1328 lldb_private::Scalar C;
1329
1330 if (!frame.EvaluateValue(C, condition, llvm_module))
1331 {
1332 if (log)
1333 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001334 err.SetErrorToGenericError();
1335 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001336 return false;
1337 }
1338
1339 if (C.GetRawBits64(0))
1340 frame.Jump(br_inst->getSuccessor(0));
1341 else
1342 frame.Jump(br_inst->getSuccessor(1));
1343
1344 if (log)
1345 {
1346 log->Printf("Interpreted a BrInst with a condition");
1347 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1348 }
1349 }
1350 else
1351 {
1352 frame.Jump(br_inst->getSuccessor(0));
1353
1354 if (log)
1355 {
1356 log->Printf("Interpreted a BrInst with no condition");
1357 }
1358 }
1359 }
1360 continue;
1361 case Instruction::GetElementPtr:
1362 {
1363 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1364
1365 if (!gep_inst)
1366 {
1367 if (log)
1368 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001369 err.SetErrorToGenericError();
1370 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001371 return false;
1372 }
1373
1374 const Value *pointer_operand = gep_inst->getPointerOperand();
1375 Type *pointer_type = pointer_operand->getType();
1376
1377 lldb_private::Scalar P;
1378
1379 if (!frame.EvaluateValue(P, pointer_operand, llvm_module))
Sean Callananddf110d2012-01-24 22:06:48 +00001380 {
1381 if (log)
1382 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1383 err.SetErrorToGenericError();
1384 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001385 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001386 }
1387
Sean Callanan7347ef82012-02-29 17:57:18 +00001388 typedef SmallVector <Value *, 8> IndexVector;
1389 typedef IndexVector::iterator IndexIterator;
1390
Sean Callanan47dc4572011-09-15 02:13:07 +00001391 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1392 gep_inst->idx_end());
1393
Sean Callanan7347ef82012-02-29 17:57:18 +00001394 SmallVector <Value *, 8> const_indices;
1395
1396 for (IndexIterator ii = indices.begin(), ie = indices.end();
1397 ii != ie;
1398 ++ii)
1399 {
1400 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1401
1402 if (!constant_index)
1403 {
1404 lldb_private::Scalar I;
1405
1406 if (!frame.EvaluateValue(I, *ii, llvm_module))
1407 {
1408 if (log)
1409 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1410 err.SetErrorToGenericError();
1411 err.SetErrorString(bad_value_error);
1412 return false;
1413 }
1414
1415 if (log)
1416 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1417
1418 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1419 }
1420
1421 const_indices.push_back(constant_index);
1422 }
1423
1424 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices);
Sean Callanan47dc4572011-09-15 02:13:07 +00001425
1426 lldb_private::Scalar Poffset = P + offset;
1427
1428 frame.AssignValue(inst, Poffset, llvm_module);
1429
1430 if (log)
1431 {
1432 log->Printf("Interpreted a GetElementPtrInst");
1433 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1434 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1435 }
1436 }
1437 break;
1438 case Instruction::ICmp:
1439 {
1440 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1441
1442 if (!icmp_inst)
1443 {
1444 if (log)
1445 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001446 err.SetErrorToGenericError();
1447 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001448 return false;
1449 }
1450
1451 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1452
1453 Value *lhs = inst->getOperand(0);
1454 Value *rhs = inst->getOperand(1);
1455
1456 lldb_private::Scalar L;
1457 lldb_private::Scalar R;
1458
1459 if (!frame.EvaluateValue(L, lhs, llvm_module))
1460 {
1461 if (log)
1462 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001463 err.SetErrorToGenericError();
1464 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001465 return false;
1466 }
1467
1468 if (!frame.EvaluateValue(R, rhs, llvm_module))
1469 {
1470 if (log)
1471 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001472 err.SetErrorToGenericError();
1473 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001474 return false;
1475 }
1476
1477 lldb_private::Scalar result;
1478
1479 switch (predicate)
1480 {
1481 default:
1482 return false;
1483 case CmpInst::ICMP_EQ:
1484 result = (L == R);
1485 break;
1486 case CmpInst::ICMP_NE:
1487 result = (L != R);
1488 break;
1489 case CmpInst::ICMP_UGT:
1490 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1491 break;
1492 case CmpInst::ICMP_UGE:
1493 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1494 break;
1495 case CmpInst::ICMP_ULT:
1496 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1497 break;
1498 case CmpInst::ICMP_ULE:
1499 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1500 break;
1501 case CmpInst::ICMP_SGT:
1502 result = (L > R);
1503 break;
1504 case CmpInst::ICMP_SGE:
1505 result = (L >= R);
1506 break;
1507 case CmpInst::ICMP_SLT:
1508 result = (L < R);
1509 break;
1510 case CmpInst::ICMP_SLE:
1511 result = (L <= R);
1512 break;
1513 }
1514
1515 frame.AssignValue(inst, result, llvm_module);
1516
1517 if (log)
1518 {
1519 log->Printf("Interpreted an ICmpInst");
1520 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1521 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1522 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1523 }
1524 }
1525 break;
Sean Callanan557ccd62011-10-21 05:18:02 +00001526 case Instruction::IntToPtr:
1527 {
1528 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1529
1530 if (!int_to_ptr_inst)
1531 {
1532 if (log)
1533 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001534 err.SetErrorToGenericError();
1535 err.SetErrorString(interpreter_internal_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001536 return false;
1537 }
1538
1539 Value *src_operand = int_to_ptr_inst->getOperand(0);
1540
1541 lldb_private::Scalar I;
1542
1543 if (!frame.EvaluateValue(I, src_operand, llvm_module))
Sean Callananddf110d2012-01-24 22:06:48 +00001544 {
1545 if (log)
1546 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1547 err.SetErrorToGenericError();
1548 err.SetErrorString(bad_value_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001549 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001550 }
Sean Callanan557ccd62011-10-21 05:18:02 +00001551
1552 frame.AssignValue(inst, I, llvm_module);
1553
1554 if (log)
1555 {
1556 log->Printf("Interpreted an IntToPtr");
1557 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1558 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1559 }
1560 }
1561 break;
Sean Callanan6b21a9b2012-12-01 00:09:34 +00001562 case Instruction::PtrToInt:
1563 {
1564 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1565
1566 if (!ptr_to_int_inst)
1567 {
1568 if (log)
1569 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1570 err.SetErrorToGenericError();
1571 err.SetErrorString(interpreter_internal_error);
1572 return false;
1573 }
1574
1575 Value *src_operand = ptr_to_int_inst->getOperand(0);
1576
1577 lldb_private::Scalar I;
1578
1579 if (!frame.EvaluateValue(I, src_operand, llvm_module))
1580 {
1581 if (log)
1582 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1583 err.SetErrorToGenericError();
1584 err.SetErrorString(bad_value_error);
1585 return false;
1586 }
1587
1588 frame.AssignValue(inst, I, llvm_module);
1589
1590 if (log)
1591 {
1592 log->Printf("Interpreted a PtrToInt");
1593 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1594 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1595 }
1596 }
1597 break;
Sean Callanan47dc4572011-09-15 02:13:07 +00001598 case Instruction::Load:
1599 {
1600 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1601
1602 if (!load_inst)
1603 {
1604 if (log)
1605 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001606 err.SetErrorToGenericError();
1607 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001608 return false;
1609 }
1610
1611 // The semantics of Load are:
1612 // Create a region D that will contain the loaded data
1613 // Resolve the region P containing a pointer
1614 // Dereference P to get the region R that the data should be loaded from
1615 // Transfer a unit of type type(D) from R to D
1616
1617 const Value *pointer_operand = load_inst->getPointerOperand();
1618
1619 Type *pointer_ty = pointer_operand->getType();
1620 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1621 if (!pointer_ptr_ty)
Sean Callananddf110d2012-01-24 22:06:48 +00001622 {
1623 if (log)
1624 log->Printf("getPointerOperand()->getType() is not a PointerType");
1625 err.SetErrorToGenericError();
1626 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001627 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001628 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001629 Type *target_ty = pointer_ptr_ty->getElementType();
1630
1631 Memory::Region D = frame.ResolveValue(load_inst, llvm_module);
1632 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1633
1634 if (D.IsInvalid())
1635 {
1636 if (log)
1637 log->Printf("LoadInst's value doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001638 err.SetErrorToGenericError();
1639 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001640 return false;
1641 }
1642
1643 if (P.IsInvalid())
1644 {
1645 if (log)
1646 log->Printf("LoadInst's pointer doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001647 err.SetErrorToGenericError();
1648 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001649 return false;
1650 }
1651
1652 DataExtractorSP P_extractor(memory.GetExtractor(P));
1653 DataEncoderSP D_encoder(memory.GetEncoder(D));
1654
1655 uint32_t offset = 0;
1656 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1657
1658 Memory::Region R = memory.Lookup(pointer, target_ty);
1659
Sean Callanan557ccd62011-10-21 05:18:02 +00001660 if (R.IsValid())
1661 {
1662 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)))
1663 {
1664 if (log)
1665 log->Printf("Couldn't read from a region on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001666 err.SetErrorToGenericError();
1667 err.SetErrorString(memory_read_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001668 return false;
1669 }
1670 }
1671 else
1672 {
1673 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty)))
1674 {
1675 if (log)
1676 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001677 err.SetErrorToGenericError();
1678 err.SetErrorString(memory_read_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001679 return false;
1680 }
1681 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001682
1683 if (log)
1684 {
1685 log->Printf("Interpreted a LoadInst");
1686 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
Sean Callanan557ccd62011-10-21 05:18:02 +00001687 if (R.IsValid())
1688 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1689 else
1690 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer);
Sean Callanan47dc4572011-09-15 02:13:07 +00001691 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str());
1692 }
1693 }
1694 break;
1695 case Instruction::Ret:
1696 {
1697 if (result_name.IsEmpty())
1698 return true;
1699
1700 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString());
Sean Callanan8f2e3922012-02-04 08:49:35 +00001701
1702 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module))
1703 {
1704 if (log)
1705 log->Printf("Couldn't construct the expression's result");
1706 err.SetErrorToGenericError();
1707 err.SetErrorString(bad_result_error);
1708 return false;
1709 }
1710
1711 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +00001712 }
1713 case Instruction::Store:
1714 {
1715 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1716
1717 if (!store_inst)
1718 {
1719 if (log)
1720 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001721 err.SetErrorToGenericError();
1722 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001723 return false;
1724 }
1725
1726 // The semantics of Store are:
1727 // Resolve the region D containing the data to be stored
1728 // Resolve the region P containing a pointer
1729 // Dereference P to get the region R that the data should be stored in
1730 // Transfer a unit of type type(D) from D to R
1731
1732 const Value *value_operand = store_inst->getValueOperand();
1733 const Value *pointer_operand = store_inst->getPointerOperand();
1734
1735 Type *pointer_ty = pointer_operand->getType();
1736 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1737 if (!pointer_ptr_ty)
1738 return false;
1739 Type *target_ty = pointer_ptr_ty->getElementType();
1740
1741 Memory::Region D = frame.ResolveValue(value_operand, llvm_module);
1742 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1743
1744 if (D.IsInvalid())
1745 {
1746 if (log)
1747 log->Printf("StoreInst's value doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001748 err.SetErrorToGenericError();
1749 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001750 return false;
1751 }
1752
1753 if (P.IsInvalid())
1754 {
1755 if (log)
1756 log->Printf("StoreInst's pointer doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001757 err.SetErrorToGenericError();
1758 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001759 return false;
1760 }
1761
1762 DataExtractorSP P_extractor(memory.GetExtractor(P));
1763 DataExtractorSP D_extractor(memory.GetExtractor(D));
1764
1765 if (!P_extractor || !D_extractor)
1766 return false;
1767
1768 uint32_t offset = 0;
1769 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1770
1771 Memory::Region R = memory.Lookup(pointer, target_ty);
1772
Sean Callanan557ccd62011-10-21 05:18:02 +00001773 if (R.IsValid())
Sean Callanan47dc4572011-09-15 02:13:07 +00001774 {
Sean Callanan557ccd62011-10-21 05:18:02 +00001775 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1776 {
1777 if (log)
1778 log->Printf("Couldn't write to a region on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001779 err.SetErrorToGenericError();
1780 err.SetErrorString(memory_write_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001781 return false;
1782 }
1783 }
1784 else
1785 {
1786 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1787 {
1788 if (log)
1789 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001790 err.SetErrorToGenericError();
1791 err.SetErrorString(memory_write_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001792 return false;
1793 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001794 }
1795
Sean Callanan47dc4572011-09-15 02:13:07 +00001796
1797 if (log)
1798 {
1799 log->Printf("Interpreted a StoreInst");
1800 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str());
1801 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1802 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1803 }
1804 }
1805 break;
1806 }
1807
1808 ++frame.m_ii;
1809 }
1810
1811 if (num_insts >= 4096)
Sean Callananddf110d2012-01-24 22:06:48 +00001812 {
1813 err.SetErrorToGenericError();
1814 err.SetErrorString(infinite_loop_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001815 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001816 }
1817
Sean Callanan47dc4572011-09-15 02:13:07 +00001818 return false;
Greg Clayton141f8d92011-10-12 00:53:29 +00001819}