blob: 3c7c76653fbe43a5aefd716da4ff21c36814c1ae [file] [log] [blame]
Sean Callanan3bfdaa22011-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 Callananf673e762012-02-15 01:40:39 +000014#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000015#include "lldb/Expression/IRForTarget.h"
16#include "lldb/Expression/IRInterpreter.h"
17
Chandler Carruth1e157582013-01-02 12:20:07 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Module.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000023#include "llvm/IR/DataLayout.h"
Sean Callanan3bfdaa22011-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 Maleaa85e6b62012-12-07 22:21:08 +000031 m_decl_map(decl_map)
Sean Callanan3bfdaa22011-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 Claytond64afba2012-03-14 03:07:05 +000072typedef STD_SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP;
73typedef STD_SHARED_PTR(lldb_private::DataExtractor) DataExtractorSP;
Sean Callanan3bfdaa22011-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 Claytond64afba2012-03-14 03:07:05 +0000130 typedef STD_SHARED_PTR(Allocation) AllocationSP;
Sean Callanan3bfdaa22011-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 Inghamf94e1792012-08-11 00:35:26 +0000161 return (bool) m_allocation;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000162 }
163
164 bool IsInvalid ()
165 {
Sean Callanan9a028512012-08-09 00:50:26 +0000166 return !m_allocation;
Sean Callanan3bfdaa22011-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 Villmow8468dbe2012-10-08 16:28:57 +0000178 DataLayout &m_target_data;
Sean Callanan3bfdaa22011-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 Villmow8468dbe2012-10-08 16:28:57 +0000197 Memory (DataLayout &target_data,
Sean Callanan3bfdaa22011-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 Villmow08318972012-10-11 17:21:41 +0000207 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan3bfdaa22011-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 Callananee458a72012-01-11 02:23:25 +0000277 if (i == m_memory.end() || !type->isSized())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000278 return Region();
Sean Callananee458a72012-01-11 02:23:25 +0000279
280 size_t size = m_target_data.getTypeStoreSize(type);
Sean Callanan3bfdaa22011-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 Callanan80c48c12011-10-21 05:18:02 +0000344 lldb_private::Value source = GetAccessTarget(addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000345
Sean Callanan80c48c12011-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 Callanan3bfdaa22011-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 Malead01b2952012-11-29 21:49:15 +0000391 ss.Printf("%" PRIx64 " [%s - %s %llx]",
Sean Callanan3bfdaa22011-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 Villmow8468dbe2012-10-08 16:28:57 +0000410 DataLayout &m_target_data;
Sean Callanan3bfdaa22011-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 Villmow8468dbe2012-10-08 16:28:57 +0000419 InterpreterStackFrame (DataLayout &target_data,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000420 Memory &memory,
421 lldb_private::ClangExpressionDeclMap &decl_map) :
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000422 m_memory (memory),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000423 m_target_data (target_data),
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000424 m_decl_map (decl_map)
425 {
426 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +0000427 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan3bfdaa22011-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
Greg Claytonc7bece562013-01-25 18:06:21 +0000509 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000510 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
Sean Callananc8675502013-02-15 23:07:52 +0000527 lldb_private::DataBufferHeap buf(region.m_extent, 0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000528
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
Greg Clayton5c9737a2013-02-07 03:41:30 +0000536 if (buf.GetByteSize() > region_encoder->GetByteSize())
Sean Callananc8675502013-02-15 23:07:52 +0000537 return false; // This should not happen
538
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000539 memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize());
540
541 return true;
542 }
543
Sean Callanan94a9a392012-02-08 01:27:49 +0000544 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000545 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000546 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
547 {
Sean Callanan94a9a392012-02-08 01:27:49 +0000548 value = constant_int->getValue();
549 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000550 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000551 else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000552 {
Sean Callanan94a9a392012-02-08 01:27:49 +0000553 value = constant_fp->getValueAPF().bitcastToAPInt();
554 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000555 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000556 else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
557 {
558 switch (constant_expr->getOpcode())
559 {
Sean Callanan94a9a392012-02-08 01:27:49 +0000560 default:
561 return false;
562 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000563 case Instruction::PtrToInt:
Sean Callanan94a9a392012-02-08 01:27:49 +0000564 case Instruction::BitCast:
565 return ResolveConstantValue(value, constant_expr->getOperand(0));
566 case Instruction::GetElementPtr:
567 {
568 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
569 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
570
571 Constant *base = dyn_cast<Constant>(*op_cursor);
572
573 if (!base)
574 return false;
575
576 if (!ResolveConstantValue(value, base))
577 return false;
578
579 op_cursor++;
580
581 if (op_cursor == op_end)
582 return true; // no offset to apply!
583
584 SmallVector <Value *, 8> indices (op_cursor, op_end);
585
586 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
587
588 const bool is_signed = true;
589 value += APInt(value.getBitWidth(), offset, is_signed);
590
591 return true;
592 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000593 }
594 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000595
596 return false;
597 }
598
Sean Callanan94a9a392012-02-08 01:27:49 +0000599 bool ResolveConstant (Memory::Region &region, const Constant *constant)
600 {
601 APInt resolved_value;
602
603 if (!ResolveConstantValue(resolved_value, constant))
604 return false;
605
606 const uint64_t *raw_data = resolved_value.getRawData();
607
608 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
609 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size);
610 }
611
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000612 Memory::Region ResolveValue (const Value *value, Module &module)
613 {
614 ValueMap::iterator i = m_values.find(value);
615
616 if (i != m_values.end())
617 return i->second;
618
619 const GlobalValue *global_value = dyn_cast<GlobalValue>(value);
620
Sean Callanand2cb6262011-10-26 21:20:00 +0000621 // If the variable is indirected through the argument
622 // array then we need to build an extra level of indirection
623 // for it. This is the default; only magic arguments like
624 // "this", "self", and "_cmd" are direct.
Sean Callanan496970f2012-12-11 22:39:36 +0000625 bool variable_is_this = false;
Sean Callanand2cb6262011-10-26 21:20:00 +0000626
Sean Callanan9be9d172013-03-19 01:45:02 +0000627 // If the variable is a function pointer, we do not need to
628 // build an extra layer of indirection for it because it is
629 // accessed directly.
630 bool variable_is_function_address = false;
631
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000632 // Attempt to resolve the value using the program's data.
633 // If it is, the values to be created are:
634 //
635 // data_region - a region of memory in which the variable's data resides.
636 // ref_region - a region of memory in which its address (i.e., &var) resides.
637 // In the JIT case, this region would be a member of the struct passed in.
638 // pointer_region - a region of memory in which the address of the pointer
639 // resides. This is an IR-level variable.
640 do
641 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000642 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand2cb6262011-10-26 21:20:00 +0000643
644 lldb_private::Value resolved_value;
Greg Clayton23f59502012-07-17 03:23:13 +0000645 lldb_private::ClangExpressionVariable::FlagType flags = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000646
Sean Callanand2cb6262011-10-26 21:20:00 +0000647 if (global_value)
648 {
649 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module);
650
651 if (!decl)
652 break;
653
654 if (isa<clang::FunctionDecl>(decl))
Sean Callanan9be9d172013-03-19 01:45:02 +0000655 variable_is_function_address = true;
Sean Callanand2cb6262011-10-26 21:20:00 +0000656
Sean Callananf673e762012-02-15 01:40:39 +0000657 resolved_value = m_decl_map.LookupDecl(decl, flags);
Sean Callanand2cb6262011-10-26 21:20:00 +0000658 }
659 else
660 {
661 // Special-case "this", "self", and "_cmd"
662
Sean Callanan7f27d602011-11-19 02:54:21 +0000663 std::string name_str = value->getName().str();
Sean Callanand2cb6262011-10-26 21:20:00 +0000664
665 if (name_str == "this" ||
666 name_str == "self" ||
667 name_str == "_cmd")
Sean Callananc8675502013-02-15 23:07:52 +0000668 {
Sean Callanand2cb6262011-10-26 21:20:00 +0000669 resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str()));
Sean Callananc8675502013-02-15 23:07:52 +0000670 variable_is_this = true;
671 }
Sean Callanand2cb6262011-10-26 21:20:00 +0000672 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000673
674 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void)
675 {
676 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
677 {
Sean Callanan496970f2012-12-11 22:39:36 +0000678 if (variable_is_this)
679 {
680 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
681
682 lldb_private::Value origin;
683
684 origin.SetValueType(lldb_private::Value::eValueTypeLoadAddress);
685 origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
686 origin.GetScalar() = resolved_value.GetScalar();
687
688 data_region.m_allocation->m_origin = origin;
689
690 Memory::Region ref_region = m_memory.Malloc(value->getType());
691
692 if (ref_region.IsInvalid())
693 return Memory::Region();
694
695 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
696
697 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
698 return Memory::Region();
699
700 if (log)
701 {
702 log->Printf("Made an allocation for \"this\" register variable %s", PrintValue(value).c_str());
703 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
704 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
705 }
706
707 m_values[value] = ref_region;
708 return ref_region;
709 }
710 else if (flags & lldb_private::ClangExpressionVariable::EVBareRegister)
711 {
712 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
713 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
Greg Clayton0665a0f2012-10-30 18:18:43 +0000714 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
715 m_memory.Malloc(value->getType());
Sean Callanand2cb6262011-10-26 21:20:00 +0000716
Sean Callanan496970f2012-12-11 22:39:36 +0000717 data_region.m_allocation->m_origin = resolved_value;
718 Memory::Region ref_region = m_memory.Malloc(value->getType());
719
720 if (!Cache(data_region.m_allocation, value->getType()))
721 return Memory::Region();
722
723 if (ref_region.IsInvalid())
724 return Memory::Region();
725
726 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
727
728 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
729 return Memory::Region();
730
731 if (log)
732 {
733 log->Printf("Made an allocation for bare register variable %s", PrintValue(value).c_str());
734 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
735 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
736 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
737 }
738
739 m_values[value] = ref_region;
740 return ref_region;
741 }
742 else
743 {
744 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
745 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
746 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
747 m_memory.Malloc(value->getType());
748
749 data_region.m_allocation->m_origin = resolved_value;
750 Memory::Region ref_region = m_memory.Malloc(value->getType());
751 Memory::Region pointer_region;
752
753 pointer_region = m_memory.Malloc(value->getType());
754
755 if (!Cache(data_region.m_allocation, value->getType()))
756 return Memory::Region();
757
758 if (ref_region.IsInvalid())
759 return Memory::Region();
760
761 if (pointer_region.IsInvalid())
762 return Memory::Region();
763
764 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
765
766 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
767 return Memory::Region();
768
769 if (log)
770 {
771 log->Printf("Made an allocation for ordinary register variable %s", PrintValue(value).c_str());
772 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
773 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
774 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
775 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
776 }
777
778 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
779
Sean Callanand2cb6262011-10-26 21:20:00 +0000780 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
781 return Memory::Region();
782
783 m_values[value] = pointer_region;
784 return pointer_region;
785 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000786 }
787 else
788 {
Sean Callanan9be9d172013-03-19 01:45:02 +0000789 bool no_extra_redirect = (variable_is_this || variable_is_function_address);
790
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000791 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
792 Memory::Region ref_region = m_memory.Malloc(value->getType());
Sean Callanand2cb6262011-10-26 21:20:00 +0000793 Memory::Region pointer_region;
794
Sean Callanan9be9d172013-03-19 01:45:02 +0000795 if (!no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000796 pointer_region = m_memory.Malloc(value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000797
798 if (ref_region.IsInvalid())
799 return Memory::Region();
800
Sean Callanan9be9d172013-03-19 01:45:02 +0000801 if (pointer_region.IsInvalid() && !no_extra_redirect)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000802 return Memory::Region();
803
804 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
805
806 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
807 return Memory::Region();
808
Sean Callanan9be9d172013-03-19 01:45:02 +0000809 if (!no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000810 {
811 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000812
Sean Callanand2cb6262011-10-26 21:20:00 +0000813 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
814 return Memory::Region();
815
816 m_values[value] = pointer_region;
817 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000818
819 if (log)
820 {
Sean Callanand2cb6262011-10-26 21:20:00 +0000821 log->Printf("Made an allocation for %s", PrintValue(value).c_str());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000822 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
823 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
824 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
Sean Callanan496970f2012-12-11 22:39:36 +0000825 if (!variable_is_this)
Sean Callanand2cb6262011-10-26 21:20:00 +0000826 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000827 }
828
Sean Callanan9be9d172013-03-19 01:45:02 +0000829 if (no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000830 return ref_region;
Sean Callanan496970f2012-12-11 22:39:36 +0000831 else
832 return pointer_region;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000833 }
834 }
835 }
836 while(0);
837
838 // Fall back and allocate space [allocation type Alloca]
839
840 Type *type = value->getType();
Sean Callananc8675502013-02-15 23:07:52 +0000841
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000842 Memory::Region data_region = m_memory.Malloc(type);
843 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes();
844 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
845 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
846
847 const Constant *constant = dyn_cast<Constant>(value);
848
849 do
850 {
851 if (!constant)
852 break;
853
854 if (!ResolveConstant (data_region, constant))
855 return Memory::Region();
856 }
857 while(0);
858
859 m_values[value] = data_region;
860 return data_region;
861 }
862
863 bool ConstructResult (lldb::ClangExpressionVariableSP &result,
864 const GlobalValue *result_value,
865 const lldb_private::ConstString &result_name,
866 lldb_private::TypeFromParser result_type,
867 Module &module)
868 {
869 // The result_value resolves to P, a pointer to a region R containing the result data.
870 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
871
872 if (!result_value)
873 return true; // There was no slot for a result – the expression doesn't return one.
874
875 ValueMap::iterator i = m_values.find(result_value);
876
877 if (i == m_values.end())
878 return false; // There was a slot for the result, but we didn't write into it.
879
880 Memory::Region P = i->second;
881 DataExtractorSP P_extractor = m_memory.GetExtractor(P);
882
883 if (!P_extractor)
884 return false;
885
886 Type *pointer_ty = result_value->getType();
887 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
888 if (!pointer_ptr_ty)
889 return false;
890 Type *R_ty = pointer_ptr_ty->getElementType();
891
Greg Claytonc7bece562013-01-25 18:06:21 +0000892 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000893 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
894
895 Memory::Region R = m_memory.Lookup(pointer, R_ty);
896
897 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress ||
898 !R.m_allocation->m_data)
899 return false;
900
901 lldb_private::Value base;
902
Sean Callanan0886e562011-09-22 00:41:11 +0000903 bool transient = false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000904 bool maybe_make_load = false;
Sean Callanan0886e562011-09-22 00:41:11 +0000905
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000906 if (m_decl_map.ResultIsReference(result_name))
907 {
908 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
909 if (!R_ptr_ty)
910 return false;
911 Type *R_final_ty = R_ptr_ty->getElementType();
912
913 DataExtractorSP R_extractor = m_memory.GetExtractor(R);
914
915 if (!R_extractor)
916 return false;
917
918 offset = 0;
919 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset);
920
921 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty);
922
Sean Callanan80c48c12011-10-21 05:18:02 +0000923 if (R_final.m_allocation)
924 {
925 if (R_final.m_allocation->m_data)
926 transient = true; // this is a stack allocation
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000927
Sean Callanan80c48c12011-10-21 05:18:02 +0000928 base = R_final.m_allocation->m_origin;
929 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address);
930 }
931 else
932 {
933 // We got a bare pointer. We are going to treat it as a load address
934 // or a file address, letting decl_map make the choice based on whether
935 // or not a process exists.
936
937 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
938 base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
939 base.GetScalar() = (unsigned long long)R_pointer;
940 maybe_make_load = true;
941 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000942 }
943 else
944 {
945 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
946 base.SetValueType(lldb_private::Value::eValueTypeHostAddress);
947 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address);
948 }
949
Sean Callanan80c48c12011-10-21 05:18:02 +0000950 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000951 }
952};
953
954bool
955IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result,
956 const lldb_private::ConstString &result_name,
957 lldb_private::TypeFromParser result_type,
958 Function &llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +0000959 Module &llvm_module,
960 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000961{
Sean Callanan175a0d02012-01-24 22:06:48 +0000962 if (supportsFunction (llvm_function, err))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000963 return runOnFunction(result,
964 result_name,
965 result_type,
966 llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +0000967 llvm_module,
968 err);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000969 else
970 return false;
971}
972
Sean Callanan175a0d02012-01-24 22:06:48 +0000973static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
974static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
975static const char *interpreter_internal_error = "Interpreter encountered an internal error";
976static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
977static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
978static const char *memory_write_error = "Interpreter couldn't write to memory";
979static const char *memory_read_error = "Interpreter couldn't read from memory";
980static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan5b26f272012-02-04 08:49:35 +0000981static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000982
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000983bool
Sean Callanan175a0d02012-01-24 22:06:48 +0000984IRInterpreter::supportsFunction (Function &llvm_function,
985 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000986{
987 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
988
989 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
990 bbi != bbe;
991 ++bbi)
992 {
993 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
994 ii != ie;
995 ++ii)
996 {
997 switch (ii->getOpcode())
998 {
999 default:
1000 {
1001 if (log)
1002 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001003 err.SetErrorToGenericError();
1004 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001005 return false;
1006 }
1007 case Instruction::Add:
1008 case Instruction::Alloca:
1009 case Instruction::BitCast:
1010 case Instruction::Br:
1011 case Instruction::GetElementPtr:
1012 break;
1013 case Instruction::ICmp:
1014 {
1015 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
1016
1017 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +00001018 {
1019 err.SetErrorToGenericError();
1020 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001021 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001022 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001023
1024 switch (icmp_inst->getPredicate())
1025 {
1026 default:
1027 {
1028 if (log)
1029 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001030
1031 err.SetErrorToGenericError();
1032 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001033 return false;
1034 }
1035 case CmpInst::ICMP_EQ:
1036 case CmpInst::ICMP_NE:
1037 case CmpInst::ICMP_UGT:
1038 case CmpInst::ICMP_UGE:
1039 case CmpInst::ICMP_ULT:
1040 case CmpInst::ICMP_ULE:
1041 case CmpInst::ICMP_SGT:
1042 case CmpInst::ICMP_SGE:
1043 case CmpInst::ICMP_SLT:
1044 case CmpInst::ICMP_SLE:
1045 break;
1046 }
1047 }
1048 break;
Sean Callanan087f4372013-01-09 22:44:41 +00001049 case Instruction::And:
1050 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +00001051 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +00001052 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001053 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +00001054 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001055 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +00001056 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001057 case Instruction::Ret:
1058 case Instruction::SDiv:
Sean Callanan087f4372013-01-09 22:44:41 +00001059 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +00001060 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001061 case Instruction::Store:
1062 case Instruction::Sub:
1063 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +00001064 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +00001065 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +00001066 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001067 break;
1068 }
1069 }
1070 }
1071
1072 return true;
1073}
1074
1075bool
1076IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result,
1077 const lldb_private::ConstString &result_name,
1078 lldb_private::TypeFromParser result_type,
1079 Function &llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +00001080 Module &llvm_module,
1081 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001082{
1083 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1084
1085 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo();
1086
1087 if (!target_info.IsValid())
Sean Callanan175a0d02012-01-24 22:06:48 +00001088 {
1089 err.SetErrorToGenericError();
1090 err.SetErrorString(interpreter_initialization_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001091 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001092 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001093
1094 lldb::addr_t alloc_min;
1095 lldb::addr_t alloc_max;
1096
1097 switch (target_info.address_byte_size)
1098 {
1099 default:
Sean Callanan175a0d02012-01-24 22:06:48 +00001100 err.SetErrorToGenericError();
1101 err.SetErrorString(interpreter_initialization_error);
1102 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001103 case 4:
1104 alloc_min = 0x00001000llu;
1105 alloc_max = 0x0000ffffllu;
1106 break;
1107 case 8:
1108 alloc_min = 0x0000000000001000llu;
1109 alloc_max = 0x000000000000ffffllu;
1110 break;
1111 }
1112
Micah Villmow8468dbe2012-10-08 16:28:57 +00001113 DataLayout target_data(&llvm_module);
Sean Callanan95769bf2012-10-11 22:00:52 +00001114 if (target_data.getPointerSize(0) != target_info.address_byte_size)
Sean Callanan175a0d02012-01-24 22:06:48 +00001115 {
1116 err.SetErrorToGenericError();
1117 err.SetErrorString(interpreter_initialization_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001118 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001119 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001120 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle))
Sean Callanan175a0d02012-01-24 22:06:48 +00001121 {
1122 err.SetErrorToGenericError();
1123 err.SetErrorString(interpreter_initialization_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001124 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001125 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001126
1127 Memory memory(target_data, m_decl_map, alloc_min, alloc_max);
1128 InterpreterStackFrame frame(target_data, memory, m_decl_map);
1129
1130 uint32_t num_insts = 0;
1131
1132 frame.Jump(llvm_function.begin());
1133
1134 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1135 {
1136 const Instruction *inst = frame.m_ii;
1137
1138 if (log)
1139 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1140
1141 switch (inst->getOpcode())
1142 {
1143 default:
1144 break;
1145 case Instruction::Add:
1146 case Instruction::Sub:
1147 case Instruction::Mul:
1148 case Instruction::SDiv:
1149 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +00001150 case Instruction::SRem:
1151 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +00001152 case Instruction::Shl:
1153 case Instruction::LShr:
1154 case Instruction::AShr:
1155 case Instruction::And:
1156 case Instruction::Or:
1157 case Instruction::Xor:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001158 {
1159 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1160
1161 if (!bin_op)
1162 {
1163 if (log)
1164 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
Sean Callanan175a0d02012-01-24 22:06:48 +00001165 err.SetErrorToGenericError();
1166 err.SetErrorString(interpreter_internal_error);
1167 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001168 }
1169
1170 Value *lhs = inst->getOperand(0);
1171 Value *rhs = inst->getOperand(1);
1172
1173 lldb_private::Scalar L;
1174 lldb_private::Scalar R;
1175
1176 if (!frame.EvaluateValue(L, lhs, llvm_module))
1177 {
1178 if (log)
1179 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001180 err.SetErrorToGenericError();
1181 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001182 return false;
1183 }
1184
1185 if (!frame.EvaluateValue(R, rhs, llvm_module))
1186 {
1187 if (log)
1188 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001189 err.SetErrorToGenericError();
1190 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001191 return false;
1192 }
1193
1194 lldb_private::Scalar result;
1195
1196 switch (inst->getOpcode())
1197 {
1198 default:
1199 break;
1200 case Instruction::Add:
1201 result = L + R;
1202 break;
1203 case Instruction::Mul:
1204 result = L * R;
1205 break;
1206 case Instruction::Sub:
1207 result = L - R;
1208 break;
1209 case Instruction::SDiv:
1210 result = L / R;
1211 break;
1212 case Instruction::UDiv:
1213 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1214 break;
Sean Callananf466a6e2012-12-21 22:27:55 +00001215 case Instruction::SRem:
1216 result = L % R;
1217 break;
1218 case Instruction::URem:
1219 result = L.GetRawBits64(0) % R.GetRawBits64(1);
1220 break;
Sean Callanan087f4372013-01-09 22:44:41 +00001221 case Instruction::Shl:
1222 result = L << R;
1223 break;
1224 case Instruction::AShr:
1225 result = L >> R;
1226 break;
1227 case Instruction::LShr:
1228 result = L;
1229 result.ShiftRightLogical(R);
1230 break;
1231 case Instruction::And:
1232 result = L & R;
1233 break;
1234 case Instruction::Or:
1235 result = L | R;
1236 break;
1237 case Instruction::Xor:
1238 result = L ^ R;
1239 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001240 }
1241
1242 frame.AssignValue(inst, result, llvm_module);
1243
1244 if (log)
1245 {
1246 log->Printf("Interpreted a %s", inst->getOpcodeName());
1247 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1248 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1249 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1250 }
1251 }
1252 break;
1253 case Instruction::Alloca:
1254 {
1255 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1256
1257 if (!alloca_inst)
1258 {
1259 if (log)
1260 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001261 err.SetErrorToGenericError();
1262 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001263 return false;
1264 }
1265
1266 if (alloca_inst->isArrayAllocation())
1267 {
1268 if (log)
1269 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
Sean Callanan175a0d02012-01-24 22:06:48 +00001270 err.SetErrorToGenericError();
1271 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001272 return false;
1273 }
1274
1275 // The semantics of Alloca are:
1276 // Create a region R of virtual memory of type T, backed by a data buffer
1277 // Create a region P of virtual memory of type T*, backed by a data buffer
1278 // Write the virtual address of R into P
1279
1280 Type *T = alloca_inst->getAllocatedType();
1281 Type *Tptr = alloca_inst->getType();
1282
1283 Memory::Region R = memory.Malloc(T);
1284
1285 if (R.IsInvalid())
1286 {
1287 if (log)
1288 log->Printf("Couldn't allocate memory for an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001289 err.SetErrorToGenericError();
1290 err.SetErrorString(memory_allocation_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001291 return false;
1292 }
1293
1294 Memory::Region P = memory.Malloc(Tptr);
1295
1296 if (P.IsInvalid())
1297 {
1298 if (log)
1299 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001300 err.SetErrorToGenericError();
1301 err.SetErrorString(memory_allocation_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001302 return false;
1303 }
1304
1305 DataEncoderSP P_encoder = memory.GetEncoder(P);
1306
1307 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX)
1308 {
1309 if (log)
Sean Callanan175a0d02012-01-24 22:06:48 +00001310 log->Printf("Couldn't write the result pointer for an AllocaInst");
1311 err.SetErrorToGenericError();
1312 err.SetErrorString(memory_write_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001313 return false;
1314 }
1315
1316 frame.m_values[alloca_inst] = P;
1317
1318 if (log)
1319 {
1320 log->Printf("Interpreted an AllocaInst");
1321 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1322 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str());
1323 }
1324 }
1325 break;
1326 case Instruction::BitCast:
Sean Callanan1ef77432012-04-23 17:25:38 +00001327 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001328 {
Sean Callanan1ef77432012-04-23 17:25:38 +00001329 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001330
Sean Callanan1ef77432012-04-23 17:25:38 +00001331 if (!cast_inst)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001332 {
1333 if (log)
Sean Callanan1ef77432012-04-23 17:25:38 +00001334 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
Sean Callanan175a0d02012-01-24 22:06:48 +00001335 err.SetErrorToGenericError();
1336 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001337 return false;
1338 }
1339
Sean Callanan1ef77432012-04-23 17:25:38 +00001340 Value *source = cast_inst->getOperand(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001341
1342 lldb_private::Scalar S;
1343
1344 if (!frame.EvaluateValue(S, source, llvm_module))
1345 {
1346 if (log)
1347 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001348 err.SetErrorToGenericError();
1349 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001350 return false;
1351 }
1352
1353 frame.AssignValue(inst, S, llvm_module);
1354 }
1355 break;
1356 case Instruction::Br:
1357 {
1358 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
1359
1360 if (!br_inst)
1361 {
1362 if (log)
1363 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001364 err.SetErrorToGenericError();
1365 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001366 return false;
1367 }
1368
1369 if (br_inst->isConditional())
1370 {
1371 Value *condition = br_inst->getCondition();
1372
1373 lldb_private::Scalar C;
1374
1375 if (!frame.EvaluateValue(C, condition, llvm_module))
1376 {
1377 if (log)
1378 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001379 err.SetErrorToGenericError();
1380 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001381 return false;
1382 }
1383
1384 if (C.GetRawBits64(0))
1385 frame.Jump(br_inst->getSuccessor(0));
1386 else
1387 frame.Jump(br_inst->getSuccessor(1));
1388
1389 if (log)
1390 {
1391 log->Printf("Interpreted a BrInst with a condition");
1392 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1393 }
1394 }
1395 else
1396 {
1397 frame.Jump(br_inst->getSuccessor(0));
1398
1399 if (log)
1400 {
1401 log->Printf("Interpreted a BrInst with no condition");
1402 }
1403 }
1404 }
1405 continue;
1406 case Instruction::GetElementPtr:
1407 {
1408 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1409
1410 if (!gep_inst)
1411 {
1412 if (log)
1413 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001414 err.SetErrorToGenericError();
1415 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001416 return false;
1417 }
1418
1419 const Value *pointer_operand = gep_inst->getPointerOperand();
1420 Type *pointer_type = pointer_operand->getType();
1421
1422 lldb_private::Scalar P;
1423
1424 if (!frame.EvaluateValue(P, pointer_operand, llvm_module))
Sean Callanan175a0d02012-01-24 22:06:48 +00001425 {
1426 if (log)
1427 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1428 err.SetErrorToGenericError();
1429 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001430 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001431 }
1432
Sean Callanan3f548132012-02-29 17:57:18 +00001433 typedef SmallVector <Value *, 8> IndexVector;
1434 typedef IndexVector::iterator IndexIterator;
1435
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001436 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1437 gep_inst->idx_end());
1438
Sean Callanan3f548132012-02-29 17:57:18 +00001439 SmallVector <Value *, 8> const_indices;
1440
1441 for (IndexIterator ii = indices.begin(), ie = indices.end();
1442 ii != ie;
1443 ++ii)
1444 {
1445 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1446
1447 if (!constant_index)
1448 {
1449 lldb_private::Scalar I;
1450
1451 if (!frame.EvaluateValue(I, *ii, llvm_module))
1452 {
1453 if (log)
1454 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1455 err.SetErrorToGenericError();
1456 err.SetErrorString(bad_value_error);
1457 return false;
1458 }
1459
1460 if (log)
1461 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1462
1463 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1464 }
1465
1466 const_indices.push_back(constant_index);
1467 }
1468
1469 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001470
1471 lldb_private::Scalar Poffset = P + offset;
1472
1473 frame.AssignValue(inst, Poffset, llvm_module);
1474
1475 if (log)
1476 {
1477 log->Printf("Interpreted a GetElementPtrInst");
1478 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1479 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1480 }
1481 }
1482 break;
1483 case Instruction::ICmp:
1484 {
1485 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1486
1487 if (!icmp_inst)
1488 {
1489 if (log)
1490 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001491 err.SetErrorToGenericError();
1492 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001493 return false;
1494 }
1495
1496 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1497
1498 Value *lhs = inst->getOperand(0);
1499 Value *rhs = inst->getOperand(1);
1500
1501 lldb_private::Scalar L;
1502 lldb_private::Scalar R;
1503
1504 if (!frame.EvaluateValue(L, lhs, llvm_module))
1505 {
1506 if (log)
1507 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001508 err.SetErrorToGenericError();
1509 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001510 return false;
1511 }
1512
1513 if (!frame.EvaluateValue(R, rhs, llvm_module))
1514 {
1515 if (log)
1516 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001517 err.SetErrorToGenericError();
1518 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001519 return false;
1520 }
1521
1522 lldb_private::Scalar result;
1523
1524 switch (predicate)
1525 {
1526 default:
1527 return false;
1528 case CmpInst::ICMP_EQ:
1529 result = (L == R);
1530 break;
1531 case CmpInst::ICMP_NE:
1532 result = (L != R);
1533 break;
1534 case CmpInst::ICMP_UGT:
1535 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1536 break;
1537 case CmpInst::ICMP_UGE:
1538 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1539 break;
1540 case CmpInst::ICMP_ULT:
1541 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1542 break;
1543 case CmpInst::ICMP_ULE:
1544 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1545 break;
1546 case CmpInst::ICMP_SGT:
1547 result = (L > R);
1548 break;
1549 case CmpInst::ICMP_SGE:
1550 result = (L >= R);
1551 break;
1552 case CmpInst::ICMP_SLT:
1553 result = (L < R);
1554 break;
1555 case CmpInst::ICMP_SLE:
1556 result = (L <= R);
1557 break;
1558 }
1559
1560 frame.AssignValue(inst, result, llvm_module);
1561
1562 if (log)
1563 {
1564 log->Printf("Interpreted an ICmpInst");
1565 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1566 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1567 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1568 }
1569 }
1570 break;
Sean Callanan80c48c12011-10-21 05:18:02 +00001571 case Instruction::IntToPtr:
1572 {
1573 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1574
1575 if (!int_to_ptr_inst)
1576 {
1577 if (log)
1578 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001579 err.SetErrorToGenericError();
1580 err.SetErrorString(interpreter_internal_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001581 return false;
1582 }
1583
1584 Value *src_operand = int_to_ptr_inst->getOperand(0);
1585
1586 lldb_private::Scalar I;
1587
1588 if (!frame.EvaluateValue(I, src_operand, llvm_module))
Sean Callanan175a0d02012-01-24 22:06:48 +00001589 {
1590 if (log)
1591 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1592 err.SetErrorToGenericError();
1593 err.SetErrorString(bad_value_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001594 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001595 }
Sean Callanan80c48c12011-10-21 05:18:02 +00001596
1597 frame.AssignValue(inst, I, llvm_module);
1598
1599 if (log)
1600 {
1601 log->Printf("Interpreted an IntToPtr");
1602 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1603 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1604 }
1605 }
1606 break;
Sean Callanan2abffe02012-12-01 00:09:34 +00001607 case Instruction::PtrToInt:
1608 {
1609 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1610
1611 if (!ptr_to_int_inst)
1612 {
1613 if (log)
1614 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1615 err.SetErrorToGenericError();
1616 err.SetErrorString(interpreter_internal_error);
1617 return false;
1618 }
1619
1620 Value *src_operand = ptr_to_int_inst->getOperand(0);
1621
1622 lldb_private::Scalar I;
1623
1624 if (!frame.EvaluateValue(I, src_operand, llvm_module))
1625 {
1626 if (log)
1627 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1628 err.SetErrorToGenericError();
1629 err.SetErrorString(bad_value_error);
1630 return false;
1631 }
1632
1633 frame.AssignValue(inst, I, llvm_module);
1634
1635 if (log)
1636 {
1637 log->Printf("Interpreted a PtrToInt");
1638 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1639 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1640 }
1641 }
1642 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001643 case Instruction::Load:
1644 {
1645 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1646
1647 if (!load_inst)
1648 {
1649 if (log)
1650 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001651 err.SetErrorToGenericError();
1652 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001653 return false;
1654 }
1655
1656 // The semantics of Load are:
1657 // Create a region D that will contain the loaded data
1658 // Resolve the region P containing a pointer
1659 // Dereference P to get the region R that the data should be loaded from
1660 // Transfer a unit of type type(D) from R to D
1661
1662 const Value *pointer_operand = load_inst->getPointerOperand();
1663
1664 Type *pointer_ty = pointer_operand->getType();
1665 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1666 if (!pointer_ptr_ty)
Sean Callanan175a0d02012-01-24 22:06:48 +00001667 {
1668 if (log)
1669 log->Printf("getPointerOperand()->getType() is not a PointerType");
1670 err.SetErrorToGenericError();
1671 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001672 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001673 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001674 Type *target_ty = pointer_ptr_ty->getElementType();
1675
1676 Memory::Region D = frame.ResolveValue(load_inst, llvm_module);
1677 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1678
1679 if (D.IsInvalid())
1680 {
1681 if (log)
1682 log->Printf("LoadInst's value doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001683 err.SetErrorToGenericError();
1684 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001685 return false;
1686 }
1687
1688 if (P.IsInvalid())
1689 {
1690 if (log)
1691 log->Printf("LoadInst's pointer doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001692 err.SetErrorToGenericError();
1693 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001694 return false;
1695 }
1696
1697 DataExtractorSP P_extractor(memory.GetExtractor(P));
1698 DataEncoderSP D_encoder(memory.GetEncoder(D));
1699
Greg Claytonc7bece562013-01-25 18:06:21 +00001700 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001701 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1702
1703 Memory::Region R = memory.Lookup(pointer, target_ty);
1704
Sean Callanan80c48c12011-10-21 05:18:02 +00001705 if (R.IsValid())
1706 {
1707 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)))
1708 {
1709 if (log)
1710 log->Printf("Couldn't read from a region on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001711 err.SetErrorToGenericError();
1712 err.SetErrorString(memory_read_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001713 return false;
1714 }
1715 }
1716 else
1717 {
1718 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty)))
1719 {
1720 if (log)
1721 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001722 err.SetErrorToGenericError();
1723 err.SetErrorString(memory_read_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001724 return false;
1725 }
1726 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001727
1728 if (log)
1729 {
1730 log->Printf("Interpreted a LoadInst");
1731 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
Sean Callanan80c48c12011-10-21 05:18:02 +00001732 if (R.IsValid())
1733 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1734 else
1735 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001736 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str());
1737 }
1738 }
1739 break;
1740 case Instruction::Ret:
1741 {
1742 if (result_name.IsEmpty())
1743 return true;
1744
1745 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString());
Sean Callanan5b26f272012-02-04 08:49:35 +00001746
1747 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module))
1748 {
1749 if (log)
1750 log->Printf("Couldn't construct the expression's result");
1751 err.SetErrorToGenericError();
1752 err.SetErrorString(bad_result_error);
1753 return false;
1754 }
1755
1756 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001757 }
1758 case Instruction::Store:
1759 {
1760 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1761
1762 if (!store_inst)
1763 {
1764 if (log)
1765 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001766 err.SetErrorToGenericError();
1767 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001768 return false;
1769 }
1770
1771 // The semantics of Store are:
1772 // Resolve the region D containing the data to be stored
1773 // Resolve the region P containing a pointer
1774 // Dereference P to get the region R that the data should be stored in
1775 // Transfer a unit of type type(D) from D to R
1776
1777 const Value *value_operand = store_inst->getValueOperand();
1778 const Value *pointer_operand = store_inst->getPointerOperand();
1779
1780 Type *pointer_ty = pointer_operand->getType();
1781 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1782 if (!pointer_ptr_ty)
1783 return false;
1784 Type *target_ty = pointer_ptr_ty->getElementType();
1785
1786 Memory::Region D = frame.ResolveValue(value_operand, llvm_module);
1787 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1788
1789 if (D.IsInvalid())
1790 {
1791 if (log)
1792 log->Printf("StoreInst's value doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001793 err.SetErrorToGenericError();
1794 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001795 return false;
1796 }
1797
1798 if (P.IsInvalid())
1799 {
1800 if (log)
1801 log->Printf("StoreInst's pointer doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001802 err.SetErrorToGenericError();
1803 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001804 return false;
1805 }
1806
1807 DataExtractorSP P_extractor(memory.GetExtractor(P));
1808 DataExtractorSP D_extractor(memory.GetExtractor(D));
1809
1810 if (!P_extractor || !D_extractor)
1811 return false;
1812
Greg Claytonc7bece562013-01-25 18:06:21 +00001813 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001814 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1815
1816 Memory::Region R = memory.Lookup(pointer, target_ty);
1817
Sean Callanan80c48c12011-10-21 05:18:02 +00001818 if (R.IsValid())
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001819 {
Sean Callanan80c48c12011-10-21 05:18:02 +00001820 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1821 {
1822 if (log)
1823 log->Printf("Couldn't write to a region on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001824 err.SetErrorToGenericError();
1825 err.SetErrorString(memory_write_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001826 return false;
1827 }
1828 }
1829 else
1830 {
1831 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1832 {
1833 if (log)
1834 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001835 err.SetErrorToGenericError();
1836 err.SetErrorString(memory_write_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001837 return false;
1838 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001839 }
1840
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001841
1842 if (log)
1843 {
1844 log->Printf("Interpreted a StoreInst");
1845 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str());
1846 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1847 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1848 }
1849 }
1850 break;
1851 }
1852
1853 ++frame.m_ii;
1854 }
1855
1856 if (num_insts >= 4096)
Sean Callanan175a0d02012-01-24 22:06:48 +00001857 {
1858 err.SetErrorToGenericError();
1859 err.SetErrorString(infinite_loop_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001860 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001861 }
1862
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001863 return false;
Greg Claytond4e25522011-10-12 00:53:29 +00001864}