blob: cbd9e8df0f0772482ab57f5002ad8804ce1aa7dd [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,
Sean Callanan179b5482013-04-16 23:49:09 +000030 lldb_private::IRMemoryMap &memory_map,
31 lldb_private::Stream *error_stream) :
32 m_decl_map(decl_map),
33 m_memory_map(memory_map)
Sean Callanan3bfdaa22011-09-15 02:13:07 +000034{
35
36}
37
38IRInterpreter::~IRInterpreter()
39{
40
41}
42
43static std::string
44PrintValue(const Value *value, bool truncate = false)
45{
46 std::string s;
47 raw_string_ostream rso(s);
48 value->print(rso);
49 rso.flush();
50 if (truncate)
51 s.resize(s.length() - 1);
52
53 size_t offset;
54 while ((offset = s.find('\n')) != s.npos)
55 s.erase(offset, 1);
56 while (s[0] == ' ' || s[0] == '\t')
57 s.erase(0, 1);
58
59 return s;
60}
61
62static std::string
63PrintType(const Type *type, bool truncate = false)
64{
65 std::string s;
66 raw_string_ostream rso(s);
67 type->print(rso);
68 rso.flush();
69 if (truncate)
70 s.resize(s.length() - 1);
71 return s;
72}
73
Greg Claytond64afba2012-03-14 03:07:05 +000074typedef STD_SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP;
75typedef STD_SHARED_PTR(lldb_private::DataExtractor) DataExtractorSP;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000076
77class Memory
78{
79public:
80 typedef uint32_t index_t;
81
82 struct Allocation
83 {
84 // m_virtual_address is always the address of the variable in the virtual memory
85 // space provided by Memory.
86 //
87 // m_origin is always non-NULL and describes the source of the data (possibly
88 // m_data if this allocation is the authoritative source).
89 //
90 // Possible value configurations:
91 //
92 // Allocation type getValueType() getContextType() m_origin->GetScalar() m_data
93 // =========================================================================================================================
94 // FileAddress eValueTypeFileAddress eContextTypeInvalid A location in a binary NULL
95 // image
96 //
97 // LoadAddress eValueTypeLoadAddress eContextTypeInvalid A location in the target's NULL
98 // virtual memory
99 //
100 // Alloca eValueTypeHostAddress eContextTypeInvalid == m_data->GetBytes() Deleted at end of
101 // execution
102 //
103 // PersistentVar eValueTypeHostAddress eContextTypeClangType A persistent variable's NULL
104 // location in LLDB's memory
105 //
106 // Register [ignored] eContextTypeRegister [ignored] Flushed to the register
107 // at the end of execution
108
109 lldb::addr_t m_virtual_address;
110 size_t m_extent;
111 lldb_private::Value m_origin;
112 lldb::DataBufferSP m_data;
113
114 Allocation (lldb::addr_t virtual_address,
115 size_t extent,
116 lldb::DataBufferSP data) :
117 m_virtual_address(virtual_address),
118 m_extent(extent),
119 m_data(data)
120 {
121 }
122
123 Allocation (const Allocation &allocation) :
124 m_virtual_address(allocation.m_virtual_address),
125 m_extent(allocation.m_extent),
126 m_origin(allocation.m_origin),
127 m_data(allocation.m_data)
128 {
129 }
130 };
131
Greg Claytond64afba2012-03-14 03:07:05 +0000132 typedef STD_SHARED_PTR(Allocation) AllocationSP;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000133
134 struct Region
135 {
136 AllocationSP m_allocation;
137 uint64_t m_base;
138 uint64_t m_extent;
139
140 Region () :
141 m_allocation(),
142 m_base(0),
143 m_extent(0)
144 {
145 }
146
147 Region (AllocationSP allocation, uint64_t base, uint64_t extent) :
148 m_allocation(allocation),
149 m_base(base),
150 m_extent(extent)
151 {
152 }
153
154 Region (const Region &region) :
155 m_allocation(region.m_allocation),
156 m_base(region.m_base),
157 m_extent(region.m_extent)
158 {
159 }
160
161 bool IsValid ()
162 {
Jim Inghamf94e1792012-08-11 00:35:26 +0000163 return (bool) m_allocation;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000164 }
165
166 bool IsInvalid ()
167 {
Sean Callanan9a028512012-08-09 00:50:26 +0000168 return !m_allocation;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000169 }
170 };
171
172 typedef std::vector <AllocationSP> MemoryMap;
173
174private:
175 lldb::addr_t m_addr_base;
176 lldb::addr_t m_addr_max;
177 MemoryMap m_memory;
178 lldb::ByteOrder m_byte_order;
179 lldb::addr_t m_addr_byte_size;
Micah Villmow8468dbe2012-10-08 16:28:57 +0000180 DataLayout &m_target_data;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000181
182 lldb_private::ClangExpressionDeclMap &m_decl_map;
183
184 MemoryMap::iterator LookupInternal (lldb::addr_t addr)
185 {
186 for (MemoryMap::iterator i = m_memory.begin(), e = m_memory.end();
187 i != e;
188 ++i)
189 {
190 if ((*i)->m_virtual_address <= addr &&
191 (*i)->m_virtual_address + (*i)->m_extent > addr)
192 return i;
193 }
194
195 return m_memory.end();
196 }
197
198public:
Micah Villmow8468dbe2012-10-08 16:28:57 +0000199 Memory (DataLayout &target_data,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000200 lldb_private::ClangExpressionDeclMap &decl_map,
201 lldb::addr_t alloc_start,
202 lldb::addr_t alloc_max) :
203 m_addr_base(alloc_start),
204 m_addr_max(alloc_max),
205 m_target_data(target_data),
206 m_decl_map(decl_map)
207 {
208 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Micah Villmow08318972012-10-11 17:21:41 +0000209 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000210 }
211
212 Region Malloc (size_t size, size_t align)
213 {
214 lldb::DataBufferSP data(new lldb_private::DataBufferHeap(size, 0));
215
216 if (data)
217 {
218 index_t index = m_memory.size();
219
220 const size_t mask = (align - 1);
221
222 m_addr_base += mask;
223 m_addr_base &= ~mask;
224
225 if (m_addr_base + size < m_addr_base ||
226 m_addr_base + size > m_addr_max)
227 return Region();
228
229 uint64_t base = m_addr_base;
230
231 m_memory.push_back(AllocationSP(new Allocation(base, size, data)));
232
233 m_addr_base += size;
234
235 AllocationSP alloc = m_memory[index];
236
237 alloc->m_origin.GetScalar() = (unsigned long long)data->GetBytes();
238 alloc->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
239 alloc->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
240
241 return Region(alloc, base, size);
242 }
243
244 return Region();
245 }
246
247 Region Malloc (Type *type)
248 {
249 return Malloc (m_target_data.getTypeAllocSize(type),
250 m_target_data.getPrefTypeAlignment(type));
251 }
252
253 Region Place (Type *type, lldb::addr_t base, lldb_private::Value &value)
254 {
255 index_t index = m_memory.size();
256 size_t size = m_target_data.getTypeAllocSize(type);
257
258 m_memory.push_back(AllocationSP(new Allocation(base, size, lldb::DataBufferSP())));
259
260 AllocationSP alloc = m_memory[index];
261
262 alloc->m_origin = value;
263
264 return Region(alloc, base, size);
265 }
266
267 void Free (lldb::addr_t addr)
268 {
269 MemoryMap::iterator i = LookupInternal (addr);
270
271 if (i != m_memory.end())
272 m_memory.erase(i);
273 }
274
275 Region Lookup (lldb::addr_t addr, Type *type)
276 {
277 MemoryMap::iterator i = LookupInternal(addr);
278
Sean Callananee458a72012-01-11 02:23:25 +0000279 if (i == m_memory.end() || !type->isSized())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000280 return Region();
Sean Callananee458a72012-01-11 02:23:25 +0000281
282 size_t size = m_target_data.getTypeStoreSize(type);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000283
284 return Region(*i, addr, size);
285 }
286
287 DataEncoderSP GetEncoder (Region region)
288 {
289 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
290 return DataEncoderSP();
291
292 lldb::DataBufferSP buffer = region.m_allocation->m_data;
293
294 if (!buffer)
295 return DataEncoderSP();
296
297 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
298
299 return DataEncoderSP(new lldb_private::DataEncoder(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
300 }
301
302 DataExtractorSP GetExtractor (Region region)
303 {
304 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
305 return DataExtractorSP();
306
307 lldb::DataBufferSP buffer = region.m_allocation->m_data;
308 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
309
310 if (buffer)
311 return DataExtractorSP(new lldb_private::DataExtractor(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
312 else
313 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));
314 }
315
316 lldb_private::Value GetAccessTarget(lldb::addr_t addr)
317 {
318 MemoryMap::iterator i = LookupInternal(addr);
319
320 if (i == m_memory.end())
321 return lldb_private::Value();
322
323 lldb_private::Value target = (*i)->m_origin;
324
325 if (target.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
326 {
327 target.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
328 target.SetValueType(lldb_private::Value::eValueTypeHostAddress);
329 target.GetScalar() = (unsigned long long)(*i)->m_data->GetBytes();
330 }
331
332 target.GetScalar() += (addr - (*i)->m_virtual_address);
333
334 return target;
335 }
336
337 bool Write (lldb::addr_t addr, const uint8_t *data, size_t length)
338 {
339 lldb_private::Value target = GetAccessTarget(addr);
340
341 return m_decl_map.WriteTarget(target, data, length);
342 }
343
344 bool Read (uint8_t *data, lldb::addr_t addr, size_t length)
345 {
Sean Callanan80c48c12011-10-21 05:18:02 +0000346 lldb_private::Value source = GetAccessTarget(addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000347
Sean Callanan80c48c12011-10-21 05:18:02 +0000348 return m_decl_map.ReadTarget(data, source, length);
349 }
350
351 bool WriteToRawPtr (lldb::addr_t addr, const uint8_t *data, size_t length)
352 {
353 lldb_private::Value target = m_decl_map.WrapBareAddress(addr);
354
355 return m_decl_map.WriteTarget(target, data, length);
356 }
357
358 bool ReadFromRawPtr (uint8_t *data, lldb::addr_t addr, size_t length)
359 {
360 lldb_private::Value source = m_decl_map.WrapBareAddress(addr);
361
362 return m_decl_map.ReadTarget(data, source, length);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000363 }
364
365 std::string PrintData (lldb::addr_t addr, size_t length)
366 {
367 lldb_private::Value target = GetAccessTarget(addr);
368
369 lldb_private::DataBufferHeap buf(length, 0);
370
371 if (!m_decl_map.ReadTarget(buf.GetBytes(), target, length))
372 return std::string("<couldn't read data>");
373
374 lldb_private::StreamString ss;
375
376 for (size_t i = 0; i < length; i++)
377 {
378 if ((!(i & 0xf)) && i)
379 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
380 else
381 ss.Printf("%02hhx ", buf.GetBytes()[i]);
382 }
383
384 return ss.GetString();
385 }
386
387 std::string SummarizeRegion (Region &region)
388 {
389 lldb_private::StreamString ss;
390
391 lldb_private::Value base = GetAccessTarget(region.m_base);
392
Daniel Malead01b2952012-11-29 21:49:15 +0000393 ss.Printf("%" PRIx64 " [%s - %s %llx]",
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000394 region.m_base,
395 lldb_private::Value::GetValueTypeAsCString(base.GetValueType()),
396 lldb_private::Value::GetContextTypeAsCString(base.GetContextType()),
397 base.GetScalar().ULongLong());
398
399 ss.Printf(" %s", PrintData(region.m_base, region.m_extent).c_str());
400
401 return ss.GetString();
402 }
403};
404
405class InterpreterStackFrame
406{
407public:
408 typedef std::map <const Value*, Memory::Region> ValueMap;
409
410 ValueMap m_values;
411 Memory &m_memory;
Micah Villmow8468dbe2012-10-08 16:28:57 +0000412 DataLayout &m_target_data;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000413 lldb_private::ClangExpressionDeclMap &m_decl_map;
Sean Callanan179b5482013-04-16 23:49:09 +0000414 lldb_private::IRMemoryMap &m_memory_map;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000415 const BasicBlock *m_bb;
416 BasicBlock::const_iterator m_ii;
417 BasicBlock::const_iterator m_ie;
418
419 lldb::ByteOrder m_byte_order;
420 size_t m_addr_byte_size;
421
Micah Villmow8468dbe2012-10-08 16:28:57 +0000422 InterpreterStackFrame (DataLayout &target_data,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000423 Memory &memory,
Sean Callanan179b5482013-04-16 23:49:09 +0000424 lldb_private::ClangExpressionDeclMap &decl_map,
425 lldb_private::IRMemoryMap &memory_map) :
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000426 m_memory (memory),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000427 m_target_data (target_data),
Sean Callanan179b5482013-04-16 23:49:09 +0000428 m_decl_map (decl_map),
429 m_memory_map (memory_map)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000430 {
431 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +0000432 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000433 }
434
435 void Jump (const BasicBlock *bb)
436 {
437 m_bb = bb;
438 m_ii = m_bb->begin();
439 m_ie = m_bb->end();
440 }
441
442 bool Cache (Memory::AllocationSP allocation, Type *type)
443 {
444 if (allocation->m_origin.GetContextType() != lldb_private::Value::eContextTypeRegisterInfo)
445 return false;
446
447 return m_decl_map.ReadTarget(allocation->m_data->GetBytes(), allocation->m_origin, allocation->m_data->GetByteSize());
448 }
449
450 std::string SummarizeValue (const Value *value)
451 {
452 lldb_private::StreamString ss;
453
454 ss.Printf("%s", PrintValue(value).c_str());
455
456 ValueMap::iterator i = m_values.find(value);
457
458 if (i != m_values.end())
459 {
460 Memory::Region region = i->second;
461
462 ss.Printf(" %s", m_memory.SummarizeRegion(region).c_str());
463 }
464
465 return ss.GetString();
466 }
467
468 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
469 {
470 size_t type_size = m_target_data.getTypeStoreSize(type);
471
472 switch (type_size)
473 {
474 case 1:
475 scalar = (uint8_t)u64value;
476 break;
477 case 2:
478 scalar = (uint16_t)u64value;
479 break;
480 case 4:
481 scalar = (uint32_t)u64value;
482 break;
483 case 8:
484 scalar = (uint64_t)u64value;
485 break;
486 default:
487 return false;
488 }
489
490 return true;
491 }
492
493 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
494 {
495 const Constant *constant = dyn_cast<Constant>(value);
496
497 if (constant)
498 {
499 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
500 {
501 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
502 }
503 }
504 else
505 {
506 Memory::Region region = ResolveValue(value, module);
507 DataExtractorSP value_extractor = m_memory.GetExtractor(region);
508
509 if (!value_extractor)
510 return false;
511
512 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
513
Greg Claytonc7bece562013-01-25 18:06:21 +0000514 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000515 uint64_t u64value = value_extractor->GetMaxU64(&offset, value_size);
516
517 return AssignToMatchType(scalar, u64value, value->getType());
518 }
519
520 return false;
521 }
522
523 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
524 {
525 Memory::Region region = ResolveValue (value, module);
526
527 lldb_private::Scalar cast_scalar;
528
529 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
530 return false;
531
Sean Callananc8675502013-02-15 23:07:52 +0000532 lldb_private::DataBufferHeap buf(region.m_extent, 0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000533
534 lldb_private::Error err;
535
536 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, err))
537 return false;
538
539 DataEncoderSP region_encoder = m_memory.GetEncoder(region);
540
Greg Clayton5c9737a2013-02-07 03:41:30 +0000541 if (buf.GetByteSize() > region_encoder->GetByteSize())
Sean Callananc8675502013-02-15 23:07:52 +0000542 return false; // This should not happen
543
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000544 memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize());
545
546 return true;
547 }
548
Sean Callanan94a9a392012-02-08 01:27:49 +0000549 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000550 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000551 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
552 {
Sean Callanan94a9a392012-02-08 01:27:49 +0000553 value = constant_int->getValue();
554 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000555 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000556 else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000557 {
Sean Callanan94a9a392012-02-08 01:27:49 +0000558 value = constant_fp->getValueAPF().bitcastToAPInt();
559 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000560 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000561 else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
562 {
563 switch (constant_expr->getOpcode())
564 {
Sean Callanan94a9a392012-02-08 01:27:49 +0000565 default:
566 return false;
567 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000568 case Instruction::PtrToInt:
Sean Callanan94a9a392012-02-08 01:27:49 +0000569 case Instruction::BitCast:
570 return ResolveConstantValue(value, constant_expr->getOperand(0));
571 case Instruction::GetElementPtr:
572 {
573 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
574 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
575
576 Constant *base = dyn_cast<Constant>(*op_cursor);
577
578 if (!base)
579 return false;
580
581 if (!ResolveConstantValue(value, base))
582 return false;
583
584 op_cursor++;
585
586 if (op_cursor == op_end)
587 return true; // no offset to apply!
588
589 SmallVector <Value *, 8> indices (op_cursor, op_end);
590
591 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
592
593 const bool is_signed = true;
594 value += APInt(value.getBitWidth(), offset, is_signed);
595
596 return true;
597 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000598 }
599 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000600
601 return false;
602 }
603
Sean Callanan94a9a392012-02-08 01:27:49 +0000604 bool ResolveConstant (Memory::Region &region, const Constant *constant)
605 {
606 APInt resolved_value;
607
608 if (!ResolveConstantValue(resolved_value, constant))
609 return false;
610
611 const uint64_t *raw_data = resolved_value.getRawData();
612
613 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
614 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size);
615 }
616
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000617 Memory::Region ResolveValue (const Value *value, Module &module)
618 {
619 ValueMap::iterator i = m_values.find(value);
620
621 if (i != m_values.end())
622 return i->second;
623
624 const GlobalValue *global_value = dyn_cast<GlobalValue>(value);
625
Sean Callanand2cb6262011-10-26 21:20:00 +0000626 // If the variable is indirected through the argument
627 // array then we need to build an extra level of indirection
628 // for it. This is the default; only magic arguments like
629 // "this", "self", and "_cmd" are direct.
Sean Callanan496970f2012-12-11 22:39:36 +0000630 bool variable_is_this = false;
Sean Callanand2cb6262011-10-26 21:20:00 +0000631
Sean Callanan9be9d172013-03-19 01:45:02 +0000632 // If the variable is a function pointer, we do not need to
633 // build an extra layer of indirection for it because it is
634 // accessed directly.
635 bool variable_is_function_address = false;
636
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000637 // Attempt to resolve the value using the program's data.
638 // If it is, the values to be created are:
639 //
640 // data_region - a region of memory in which the variable's data resides.
641 // ref_region - a region of memory in which its address (i.e., &var) resides.
642 // In the JIT case, this region would be a member of the struct passed in.
643 // pointer_region - a region of memory in which the address of the pointer
644 // resides. This is an IR-level variable.
645 do
646 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000647 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand2cb6262011-10-26 21:20:00 +0000648
649 lldb_private::Value resolved_value;
Greg Clayton23f59502012-07-17 03:23:13 +0000650 lldb_private::ClangExpressionVariable::FlagType flags = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000651
Sean Callanand2cb6262011-10-26 21:20:00 +0000652 if (global_value)
653 {
654 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module);
655
656 if (!decl)
657 break;
658
659 if (isa<clang::FunctionDecl>(decl))
Sean Callanan9be9d172013-03-19 01:45:02 +0000660 variable_is_function_address = true;
Sean Callanand2cb6262011-10-26 21:20:00 +0000661
Sean Callananf673e762012-02-15 01:40:39 +0000662 resolved_value = m_decl_map.LookupDecl(decl, flags);
Sean Callanand2cb6262011-10-26 21:20:00 +0000663 }
664 else
665 {
666 // Special-case "this", "self", and "_cmd"
667
Sean Callanan7f27d602011-11-19 02:54:21 +0000668 std::string name_str = value->getName().str();
Sean Callanand2cb6262011-10-26 21:20:00 +0000669
670 if (name_str == "this" ||
671 name_str == "self" ||
672 name_str == "_cmd")
Sean Callananc8675502013-02-15 23:07:52 +0000673 {
Sean Callanand2cb6262011-10-26 21:20:00 +0000674 resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str()));
Sean Callananc8675502013-02-15 23:07:52 +0000675 variable_is_this = true;
676 }
Sean Callanand2cb6262011-10-26 21:20:00 +0000677 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000678
679 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void)
680 {
681 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
682 {
Sean Callanan496970f2012-12-11 22:39:36 +0000683 if (variable_is_this)
684 {
685 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
686
687 lldb_private::Value origin;
688
689 origin.SetValueType(lldb_private::Value::eValueTypeLoadAddress);
690 origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
691 origin.GetScalar() = resolved_value.GetScalar();
692
693 data_region.m_allocation->m_origin = origin;
694
695 Memory::Region ref_region = m_memory.Malloc(value->getType());
696
697 if (ref_region.IsInvalid())
698 return Memory::Region();
699
700 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
701
702 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
703 return Memory::Region();
704
705 if (log)
706 {
707 log->Printf("Made an allocation for \"this\" register variable %s", PrintValue(value).c_str());
708 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
709 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
710 }
711
712 m_values[value] = ref_region;
713 return ref_region;
714 }
715 else if (flags & lldb_private::ClangExpressionVariable::EVBareRegister)
716 {
717 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
718 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
Greg Clayton0665a0f2012-10-30 18:18:43 +0000719 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
720 m_memory.Malloc(value->getType());
Sean Callanand2cb6262011-10-26 21:20:00 +0000721
Sean Callanan496970f2012-12-11 22:39:36 +0000722 data_region.m_allocation->m_origin = resolved_value;
723 Memory::Region ref_region = m_memory.Malloc(value->getType());
724
725 if (!Cache(data_region.m_allocation, value->getType()))
726 return Memory::Region();
727
728 if (ref_region.IsInvalid())
729 return Memory::Region();
730
731 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
732
733 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
734 return Memory::Region();
735
736 if (log)
737 {
738 log->Printf("Made an allocation for bare register variable %s", PrintValue(value).c_str());
739 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
740 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
741 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
742 }
743
744 m_values[value] = ref_region;
745 return ref_region;
746 }
747 else
748 {
749 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
750 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
751 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
752 m_memory.Malloc(value->getType());
753
754 data_region.m_allocation->m_origin = resolved_value;
755 Memory::Region ref_region = m_memory.Malloc(value->getType());
756 Memory::Region pointer_region;
757
758 pointer_region = m_memory.Malloc(value->getType());
759
760 if (!Cache(data_region.m_allocation, value->getType()))
761 return Memory::Region();
762
763 if (ref_region.IsInvalid())
764 return Memory::Region();
765
766 if (pointer_region.IsInvalid())
767 return Memory::Region();
768
769 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
770
771 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
772 return Memory::Region();
773
774 if (log)
775 {
776 log->Printf("Made an allocation for ordinary register variable %s", PrintValue(value).c_str());
777 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
778 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
779 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
780 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
781 }
782
783 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
784
Sean Callanand2cb6262011-10-26 21:20:00 +0000785 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
786 return Memory::Region();
787
788 m_values[value] = pointer_region;
789 return pointer_region;
790 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000791 }
792 else
793 {
Sean Callanan9be9d172013-03-19 01:45:02 +0000794 bool no_extra_redirect = (variable_is_this || variable_is_function_address);
795
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000796 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
797 Memory::Region ref_region = m_memory.Malloc(value->getType());
Sean Callanand2cb6262011-10-26 21:20:00 +0000798 Memory::Region pointer_region;
799
Sean Callanan9be9d172013-03-19 01:45:02 +0000800 if (!no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000801 pointer_region = m_memory.Malloc(value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000802
803 if (ref_region.IsInvalid())
804 return Memory::Region();
805
Sean Callanan9be9d172013-03-19 01:45:02 +0000806 if (pointer_region.IsInvalid() && !no_extra_redirect)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000807 return Memory::Region();
808
809 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
810
811 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
812 return Memory::Region();
813
Sean Callanan9be9d172013-03-19 01:45:02 +0000814 if (!no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000815 {
816 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000817
Sean Callanand2cb6262011-10-26 21:20:00 +0000818 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
819 return Memory::Region();
820
821 m_values[value] = pointer_region;
822 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000823
824 if (log)
825 {
Sean Callanand2cb6262011-10-26 21:20:00 +0000826 log->Printf("Made an allocation for %s", PrintValue(value).c_str());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000827 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
828 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
829 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
Sean Callanan496970f2012-12-11 22:39:36 +0000830 if (!variable_is_this)
Sean Callanand2cb6262011-10-26 21:20:00 +0000831 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000832 }
833
Sean Callanan9be9d172013-03-19 01:45:02 +0000834 if (no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000835 return ref_region;
Sean Callanan496970f2012-12-11 22:39:36 +0000836 else
837 return pointer_region;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000838 }
839 }
840 }
841 while(0);
842
843 // Fall back and allocate space [allocation type Alloca]
844
845 Type *type = value->getType();
Sean Callananc8675502013-02-15 23:07:52 +0000846
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000847 Memory::Region data_region = m_memory.Malloc(type);
848 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes();
849 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
850 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
851
852 const Constant *constant = dyn_cast<Constant>(value);
853
854 do
855 {
856 if (!constant)
857 break;
858
859 if (!ResolveConstant (data_region, constant))
860 return Memory::Region();
861 }
862 while(0);
863
864 m_values[value] = data_region;
865 return data_region;
866 }
867
868 bool ConstructResult (lldb::ClangExpressionVariableSP &result,
869 const GlobalValue *result_value,
870 const lldb_private::ConstString &result_name,
871 lldb_private::TypeFromParser result_type,
872 Module &module)
873 {
874 // The result_value resolves to P, a pointer to a region R containing the result data.
875 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
876
877 if (!result_value)
878 return true; // There was no slot for a result – the expression doesn't return one.
879
880 ValueMap::iterator i = m_values.find(result_value);
881
882 if (i == m_values.end())
883 return false; // There was a slot for the result, but we didn't write into it.
884
885 Memory::Region P = i->second;
886 DataExtractorSP P_extractor = m_memory.GetExtractor(P);
887
888 if (!P_extractor)
889 return false;
890
891 Type *pointer_ty = result_value->getType();
892 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
893 if (!pointer_ptr_ty)
894 return false;
895 Type *R_ty = pointer_ptr_ty->getElementType();
896
Greg Claytonc7bece562013-01-25 18:06:21 +0000897 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000898 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
899
900 Memory::Region R = m_memory.Lookup(pointer, R_ty);
901
902 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress ||
903 !R.m_allocation->m_data)
904 return false;
905
906 lldb_private::Value base;
907
Sean Callanan0886e562011-09-22 00:41:11 +0000908 bool transient = false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000909 bool maybe_make_load = false;
Sean Callanan0886e562011-09-22 00:41:11 +0000910
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000911 if (m_decl_map.ResultIsReference(result_name))
912 {
913 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
914 if (!R_ptr_ty)
915 return false;
916 Type *R_final_ty = R_ptr_ty->getElementType();
917
918 DataExtractorSP R_extractor = m_memory.GetExtractor(R);
919
920 if (!R_extractor)
921 return false;
922
923 offset = 0;
924 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset);
925
926 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty);
927
Sean Callanan80c48c12011-10-21 05:18:02 +0000928 if (R_final.m_allocation)
929 {
930 if (R_final.m_allocation->m_data)
931 transient = true; // this is a stack allocation
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000932
Sean Callanan80c48c12011-10-21 05:18:02 +0000933 base = R_final.m_allocation->m_origin;
934 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address);
935 }
936 else
937 {
938 // We got a bare pointer. We are going to treat it as a load address
939 // or a file address, letting decl_map make the choice based on whether
940 // or not a process exists.
941
942 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
943 base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
944 base.GetScalar() = (unsigned long long)R_pointer;
945 maybe_make_load = true;
946 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000947 }
948 else
949 {
950 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
951 base.SetValueType(lldb_private::Value::eValueTypeHostAddress);
952 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address);
953 }
954
Sean Callanan80c48c12011-10-21 05:18:02 +0000955 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000956 }
957};
958
959bool
960IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result,
961 const lldb_private::ConstString &result_name,
962 lldb_private::TypeFromParser result_type,
963 Function &llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +0000964 Module &llvm_module,
965 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000966{
Sean Callanan175a0d02012-01-24 22:06:48 +0000967 if (supportsFunction (llvm_function, err))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000968 return runOnFunction(result,
969 result_name,
970 result_type,
971 llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +0000972 llvm_module,
973 err);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000974 else
975 return false;
976}
977
Sean Callanan175a0d02012-01-24 22:06:48 +0000978static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
979static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
980static const char *interpreter_internal_error = "Interpreter encountered an internal error";
981static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
982static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
983static const char *memory_write_error = "Interpreter couldn't write to memory";
984static const char *memory_read_error = "Interpreter couldn't read from memory";
985static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan5b26f272012-02-04 08:49:35 +0000986static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000987
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000988bool
Sean Callanan175a0d02012-01-24 22:06:48 +0000989IRInterpreter::supportsFunction (Function &llvm_function,
990 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000991{
Greg Clayton5160ce52013-03-27 23:08:40 +0000992 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000993
994 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
995 bbi != bbe;
996 ++bbi)
997 {
998 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
999 ii != ie;
1000 ++ii)
1001 {
1002 switch (ii->getOpcode())
1003 {
1004 default:
1005 {
1006 if (log)
1007 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001008 err.SetErrorToGenericError();
1009 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001010 return false;
1011 }
1012 case Instruction::Add:
1013 case Instruction::Alloca:
1014 case Instruction::BitCast:
1015 case Instruction::Br:
1016 case Instruction::GetElementPtr:
1017 break;
1018 case Instruction::ICmp:
1019 {
1020 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
1021
1022 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +00001023 {
1024 err.SetErrorToGenericError();
1025 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001026 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001027 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001028
1029 switch (icmp_inst->getPredicate())
1030 {
1031 default:
1032 {
1033 if (log)
1034 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001035
1036 err.SetErrorToGenericError();
1037 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001038 return false;
1039 }
1040 case CmpInst::ICMP_EQ:
1041 case CmpInst::ICMP_NE:
1042 case CmpInst::ICMP_UGT:
1043 case CmpInst::ICMP_UGE:
1044 case CmpInst::ICMP_ULT:
1045 case CmpInst::ICMP_ULE:
1046 case CmpInst::ICMP_SGT:
1047 case CmpInst::ICMP_SGE:
1048 case CmpInst::ICMP_SLT:
1049 case CmpInst::ICMP_SLE:
1050 break;
1051 }
1052 }
1053 break;
Sean Callanan087f4372013-01-09 22:44:41 +00001054 case Instruction::And:
1055 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +00001056 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +00001057 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001058 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +00001059 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001060 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +00001061 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001062 case Instruction::Ret:
1063 case Instruction::SDiv:
Sean Callanan087f4372013-01-09 22:44:41 +00001064 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +00001065 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001066 case Instruction::Store:
1067 case Instruction::Sub:
1068 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +00001069 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +00001070 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +00001071 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001072 break;
1073 }
1074 }
1075 }
1076
1077 return true;
1078}
1079
1080bool
1081IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result,
1082 const lldb_private::ConstString &result_name,
1083 lldb_private::TypeFromParser result_type,
1084 Function &llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +00001085 Module &llvm_module,
1086 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001087{
Greg Clayton5160ce52013-03-27 23:08:40 +00001088 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001089
1090 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo();
1091
1092 if (!target_info.IsValid())
Sean Callanan175a0d02012-01-24 22:06:48 +00001093 {
1094 err.SetErrorToGenericError();
1095 err.SetErrorString(interpreter_initialization_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001096 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001097 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001098
1099 lldb::addr_t alloc_min;
1100 lldb::addr_t alloc_max;
1101
1102 switch (target_info.address_byte_size)
1103 {
1104 default:
Sean Callanan175a0d02012-01-24 22:06:48 +00001105 err.SetErrorToGenericError();
1106 err.SetErrorString(interpreter_initialization_error);
1107 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001108 case 4:
1109 alloc_min = 0x00001000llu;
1110 alloc_max = 0x0000ffffllu;
1111 break;
1112 case 8:
1113 alloc_min = 0x0000000000001000llu;
1114 alloc_max = 0x000000000000ffffllu;
1115 break;
1116 }
1117
Micah Villmow8468dbe2012-10-08 16:28:57 +00001118 DataLayout target_data(&llvm_module);
Sean Callanan95769bf2012-10-11 22:00:52 +00001119 if (target_data.getPointerSize(0) != target_info.address_byte_size)
Sean Callanan175a0d02012-01-24 22:06:48 +00001120 {
1121 err.SetErrorToGenericError();
1122 err.SetErrorString(interpreter_initialization_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001123 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001124 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001125 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle))
Sean Callanan175a0d02012-01-24 22:06:48 +00001126 {
1127 err.SetErrorToGenericError();
1128 err.SetErrorString(interpreter_initialization_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001129 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001130 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001131
1132 Memory memory(target_data, m_decl_map, alloc_min, alloc_max);
Sean Callanan179b5482013-04-16 23:49:09 +00001133 InterpreterStackFrame frame(target_data, memory, m_decl_map, m_memory_map);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001134
1135 uint32_t num_insts = 0;
1136
1137 frame.Jump(llvm_function.begin());
1138
1139 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1140 {
1141 const Instruction *inst = frame.m_ii;
1142
1143 if (log)
1144 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1145
1146 switch (inst->getOpcode())
1147 {
1148 default:
1149 break;
1150 case Instruction::Add:
1151 case Instruction::Sub:
1152 case Instruction::Mul:
1153 case Instruction::SDiv:
1154 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +00001155 case Instruction::SRem:
1156 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +00001157 case Instruction::Shl:
1158 case Instruction::LShr:
1159 case Instruction::AShr:
1160 case Instruction::And:
1161 case Instruction::Or:
1162 case Instruction::Xor:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001163 {
1164 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1165
1166 if (!bin_op)
1167 {
1168 if (log)
1169 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
Sean Callanan175a0d02012-01-24 22:06:48 +00001170 err.SetErrorToGenericError();
1171 err.SetErrorString(interpreter_internal_error);
1172 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001173 }
1174
1175 Value *lhs = inst->getOperand(0);
1176 Value *rhs = inst->getOperand(1);
1177
1178 lldb_private::Scalar L;
1179 lldb_private::Scalar R;
1180
1181 if (!frame.EvaluateValue(L, lhs, llvm_module))
1182 {
1183 if (log)
1184 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001185 err.SetErrorToGenericError();
1186 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001187 return false;
1188 }
1189
1190 if (!frame.EvaluateValue(R, rhs, llvm_module))
1191 {
1192 if (log)
1193 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001194 err.SetErrorToGenericError();
1195 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001196 return false;
1197 }
1198
1199 lldb_private::Scalar result;
1200
1201 switch (inst->getOpcode())
1202 {
1203 default:
1204 break;
1205 case Instruction::Add:
1206 result = L + R;
1207 break;
1208 case Instruction::Mul:
1209 result = L * R;
1210 break;
1211 case Instruction::Sub:
1212 result = L - R;
1213 break;
1214 case Instruction::SDiv:
1215 result = L / R;
1216 break;
1217 case Instruction::UDiv:
1218 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1219 break;
Sean Callananf466a6e2012-12-21 22:27:55 +00001220 case Instruction::SRem:
1221 result = L % R;
1222 break;
1223 case Instruction::URem:
1224 result = L.GetRawBits64(0) % R.GetRawBits64(1);
1225 break;
Sean Callanan087f4372013-01-09 22:44:41 +00001226 case Instruction::Shl:
1227 result = L << R;
1228 break;
1229 case Instruction::AShr:
1230 result = L >> R;
1231 break;
1232 case Instruction::LShr:
1233 result = L;
1234 result.ShiftRightLogical(R);
1235 break;
1236 case Instruction::And:
1237 result = L & R;
1238 break;
1239 case Instruction::Or:
1240 result = L | R;
1241 break;
1242 case Instruction::Xor:
1243 result = L ^ R;
1244 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001245 }
1246
1247 frame.AssignValue(inst, result, llvm_module);
1248
1249 if (log)
1250 {
1251 log->Printf("Interpreted a %s", inst->getOpcodeName());
1252 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1253 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1254 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1255 }
1256 }
1257 break;
1258 case Instruction::Alloca:
1259 {
1260 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1261
1262 if (!alloca_inst)
1263 {
1264 if (log)
1265 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001266 err.SetErrorToGenericError();
1267 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001268 return false;
1269 }
1270
1271 if (alloca_inst->isArrayAllocation())
1272 {
1273 if (log)
1274 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
Sean Callanan175a0d02012-01-24 22:06:48 +00001275 err.SetErrorToGenericError();
1276 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001277 return false;
1278 }
1279
1280 // The semantics of Alloca are:
1281 // Create a region R of virtual memory of type T, backed by a data buffer
1282 // Create a region P of virtual memory of type T*, backed by a data buffer
1283 // Write the virtual address of R into P
1284
1285 Type *T = alloca_inst->getAllocatedType();
1286 Type *Tptr = alloca_inst->getType();
1287
1288 Memory::Region R = memory.Malloc(T);
1289
1290 if (R.IsInvalid())
1291 {
1292 if (log)
1293 log->Printf("Couldn't allocate memory for an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001294 err.SetErrorToGenericError();
1295 err.SetErrorString(memory_allocation_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001296 return false;
1297 }
1298
1299 Memory::Region P = memory.Malloc(Tptr);
1300
1301 if (P.IsInvalid())
1302 {
1303 if (log)
1304 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001305 err.SetErrorToGenericError();
1306 err.SetErrorString(memory_allocation_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001307 return false;
1308 }
1309
1310 DataEncoderSP P_encoder = memory.GetEncoder(P);
1311
1312 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX)
1313 {
1314 if (log)
Sean Callanan175a0d02012-01-24 22:06:48 +00001315 log->Printf("Couldn't write the result pointer for an AllocaInst");
1316 err.SetErrorToGenericError();
1317 err.SetErrorString(memory_write_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001318 return false;
1319 }
1320
1321 frame.m_values[alloca_inst] = P;
1322
1323 if (log)
1324 {
1325 log->Printf("Interpreted an AllocaInst");
1326 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1327 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str());
1328 }
1329 }
1330 break;
1331 case Instruction::BitCast:
Sean Callanan1ef77432012-04-23 17:25:38 +00001332 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001333 {
Sean Callanan1ef77432012-04-23 17:25:38 +00001334 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001335
Sean Callanan1ef77432012-04-23 17:25:38 +00001336 if (!cast_inst)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001337 {
1338 if (log)
Sean Callanan1ef77432012-04-23 17:25:38 +00001339 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
Sean Callanan175a0d02012-01-24 22:06:48 +00001340 err.SetErrorToGenericError();
1341 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001342 return false;
1343 }
1344
Sean Callanan1ef77432012-04-23 17:25:38 +00001345 Value *source = cast_inst->getOperand(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001346
1347 lldb_private::Scalar S;
1348
1349 if (!frame.EvaluateValue(S, source, llvm_module))
1350 {
1351 if (log)
1352 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001353 err.SetErrorToGenericError();
1354 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001355 return false;
1356 }
1357
1358 frame.AssignValue(inst, S, llvm_module);
1359 }
1360 break;
1361 case Instruction::Br:
1362 {
1363 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
1364
1365 if (!br_inst)
1366 {
1367 if (log)
1368 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001369 err.SetErrorToGenericError();
1370 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001371 return false;
1372 }
1373
1374 if (br_inst->isConditional())
1375 {
1376 Value *condition = br_inst->getCondition();
1377
1378 lldb_private::Scalar C;
1379
1380 if (!frame.EvaluateValue(C, condition, llvm_module))
1381 {
1382 if (log)
1383 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001384 err.SetErrorToGenericError();
1385 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001386 return false;
1387 }
1388
1389 if (C.GetRawBits64(0))
1390 frame.Jump(br_inst->getSuccessor(0));
1391 else
1392 frame.Jump(br_inst->getSuccessor(1));
1393
1394 if (log)
1395 {
1396 log->Printf("Interpreted a BrInst with a condition");
1397 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1398 }
1399 }
1400 else
1401 {
1402 frame.Jump(br_inst->getSuccessor(0));
1403
1404 if (log)
1405 {
1406 log->Printf("Interpreted a BrInst with no condition");
1407 }
1408 }
1409 }
1410 continue;
1411 case Instruction::GetElementPtr:
1412 {
1413 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1414
1415 if (!gep_inst)
1416 {
1417 if (log)
1418 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001419 err.SetErrorToGenericError();
1420 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001421 return false;
1422 }
1423
1424 const Value *pointer_operand = gep_inst->getPointerOperand();
1425 Type *pointer_type = pointer_operand->getType();
1426
1427 lldb_private::Scalar P;
1428
1429 if (!frame.EvaluateValue(P, pointer_operand, llvm_module))
Sean Callanan175a0d02012-01-24 22:06:48 +00001430 {
1431 if (log)
1432 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1433 err.SetErrorToGenericError();
1434 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001435 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001436 }
1437
Sean Callanan3f548132012-02-29 17:57:18 +00001438 typedef SmallVector <Value *, 8> IndexVector;
1439 typedef IndexVector::iterator IndexIterator;
1440
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001441 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1442 gep_inst->idx_end());
1443
Sean Callanan3f548132012-02-29 17:57:18 +00001444 SmallVector <Value *, 8> const_indices;
1445
1446 for (IndexIterator ii = indices.begin(), ie = indices.end();
1447 ii != ie;
1448 ++ii)
1449 {
1450 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1451
1452 if (!constant_index)
1453 {
1454 lldb_private::Scalar I;
1455
1456 if (!frame.EvaluateValue(I, *ii, llvm_module))
1457 {
1458 if (log)
1459 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1460 err.SetErrorToGenericError();
1461 err.SetErrorString(bad_value_error);
1462 return false;
1463 }
1464
1465 if (log)
1466 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1467
1468 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1469 }
1470
1471 const_indices.push_back(constant_index);
1472 }
1473
1474 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001475
1476 lldb_private::Scalar Poffset = P + offset;
1477
1478 frame.AssignValue(inst, Poffset, llvm_module);
1479
1480 if (log)
1481 {
1482 log->Printf("Interpreted a GetElementPtrInst");
1483 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1484 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1485 }
1486 }
1487 break;
1488 case Instruction::ICmp:
1489 {
1490 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1491
1492 if (!icmp_inst)
1493 {
1494 if (log)
1495 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001496 err.SetErrorToGenericError();
1497 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001498 return false;
1499 }
1500
1501 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1502
1503 Value *lhs = inst->getOperand(0);
1504 Value *rhs = inst->getOperand(1);
1505
1506 lldb_private::Scalar L;
1507 lldb_private::Scalar R;
1508
1509 if (!frame.EvaluateValue(L, lhs, llvm_module))
1510 {
1511 if (log)
1512 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001513 err.SetErrorToGenericError();
1514 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001515 return false;
1516 }
1517
1518 if (!frame.EvaluateValue(R, rhs, llvm_module))
1519 {
1520 if (log)
1521 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001522 err.SetErrorToGenericError();
1523 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001524 return false;
1525 }
1526
1527 lldb_private::Scalar result;
1528
1529 switch (predicate)
1530 {
1531 default:
1532 return false;
1533 case CmpInst::ICMP_EQ:
1534 result = (L == R);
1535 break;
1536 case CmpInst::ICMP_NE:
1537 result = (L != R);
1538 break;
1539 case CmpInst::ICMP_UGT:
1540 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1541 break;
1542 case CmpInst::ICMP_UGE:
1543 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1544 break;
1545 case CmpInst::ICMP_ULT:
1546 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1547 break;
1548 case CmpInst::ICMP_ULE:
1549 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1550 break;
1551 case CmpInst::ICMP_SGT:
1552 result = (L > R);
1553 break;
1554 case CmpInst::ICMP_SGE:
1555 result = (L >= R);
1556 break;
1557 case CmpInst::ICMP_SLT:
1558 result = (L < R);
1559 break;
1560 case CmpInst::ICMP_SLE:
1561 result = (L <= R);
1562 break;
1563 }
1564
1565 frame.AssignValue(inst, result, llvm_module);
1566
1567 if (log)
1568 {
1569 log->Printf("Interpreted an ICmpInst");
1570 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1571 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1572 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1573 }
1574 }
1575 break;
Sean Callanan80c48c12011-10-21 05:18:02 +00001576 case Instruction::IntToPtr:
1577 {
1578 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1579
1580 if (!int_to_ptr_inst)
1581 {
1582 if (log)
1583 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001584 err.SetErrorToGenericError();
1585 err.SetErrorString(interpreter_internal_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001586 return false;
1587 }
1588
1589 Value *src_operand = int_to_ptr_inst->getOperand(0);
1590
1591 lldb_private::Scalar I;
1592
1593 if (!frame.EvaluateValue(I, src_operand, llvm_module))
Sean Callanan175a0d02012-01-24 22:06:48 +00001594 {
1595 if (log)
1596 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1597 err.SetErrorToGenericError();
1598 err.SetErrorString(bad_value_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001599 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001600 }
Sean Callanan80c48c12011-10-21 05:18:02 +00001601
1602 frame.AssignValue(inst, I, llvm_module);
1603
1604 if (log)
1605 {
1606 log->Printf("Interpreted an IntToPtr");
1607 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1608 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1609 }
1610 }
1611 break;
Sean Callanan2abffe02012-12-01 00:09:34 +00001612 case Instruction::PtrToInt:
1613 {
1614 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1615
1616 if (!ptr_to_int_inst)
1617 {
1618 if (log)
1619 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1620 err.SetErrorToGenericError();
1621 err.SetErrorString(interpreter_internal_error);
1622 return false;
1623 }
1624
1625 Value *src_operand = ptr_to_int_inst->getOperand(0);
1626
1627 lldb_private::Scalar I;
1628
1629 if (!frame.EvaluateValue(I, src_operand, llvm_module))
1630 {
1631 if (log)
1632 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1633 err.SetErrorToGenericError();
1634 err.SetErrorString(bad_value_error);
1635 return false;
1636 }
1637
1638 frame.AssignValue(inst, I, llvm_module);
1639
1640 if (log)
1641 {
1642 log->Printf("Interpreted a PtrToInt");
1643 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1644 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1645 }
1646 }
1647 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001648 case Instruction::Load:
1649 {
1650 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1651
1652 if (!load_inst)
1653 {
1654 if (log)
1655 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001656 err.SetErrorToGenericError();
1657 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001658 return false;
1659 }
1660
1661 // The semantics of Load are:
1662 // Create a region D that will contain the loaded data
1663 // Resolve the region P containing a pointer
1664 // Dereference P to get the region R that the data should be loaded from
1665 // Transfer a unit of type type(D) from R to D
1666
1667 const Value *pointer_operand = load_inst->getPointerOperand();
1668
1669 Type *pointer_ty = pointer_operand->getType();
1670 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1671 if (!pointer_ptr_ty)
Sean Callanan175a0d02012-01-24 22:06:48 +00001672 {
1673 if (log)
1674 log->Printf("getPointerOperand()->getType() is not a PointerType");
1675 err.SetErrorToGenericError();
1676 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001677 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001678 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001679 Type *target_ty = pointer_ptr_ty->getElementType();
1680
1681 Memory::Region D = frame.ResolveValue(load_inst, llvm_module);
1682 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1683
1684 if (D.IsInvalid())
1685 {
1686 if (log)
1687 log->Printf("LoadInst's value doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001688 err.SetErrorToGenericError();
1689 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001690 return false;
1691 }
1692
1693 if (P.IsInvalid())
1694 {
1695 if (log)
1696 log->Printf("LoadInst's pointer doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001697 err.SetErrorToGenericError();
1698 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001699 return false;
1700 }
1701
1702 DataExtractorSP P_extractor(memory.GetExtractor(P));
1703 DataEncoderSP D_encoder(memory.GetEncoder(D));
1704
Greg Claytonc7bece562013-01-25 18:06:21 +00001705 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001706 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1707
1708 Memory::Region R = memory.Lookup(pointer, target_ty);
1709
Sean Callanan80c48c12011-10-21 05:18:02 +00001710 if (R.IsValid())
1711 {
1712 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)))
1713 {
1714 if (log)
1715 log->Printf("Couldn't read from a region on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001716 err.SetErrorToGenericError();
1717 err.SetErrorString(memory_read_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001718 return false;
1719 }
1720 }
1721 else
1722 {
1723 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty)))
1724 {
1725 if (log)
1726 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001727 err.SetErrorToGenericError();
1728 err.SetErrorString(memory_read_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001729 return false;
1730 }
1731 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001732
1733 if (log)
1734 {
1735 log->Printf("Interpreted a LoadInst");
1736 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
Sean Callanan80c48c12011-10-21 05:18:02 +00001737 if (R.IsValid())
1738 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1739 else
1740 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001741 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str());
1742 }
1743 }
1744 break;
1745 case Instruction::Ret:
1746 {
1747 if (result_name.IsEmpty())
1748 return true;
1749
1750 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString());
Sean Callanan5b26f272012-02-04 08:49:35 +00001751
1752 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module))
1753 {
1754 if (log)
1755 log->Printf("Couldn't construct the expression's result");
1756 err.SetErrorToGenericError();
1757 err.SetErrorString(bad_result_error);
1758 return false;
1759 }
1760
1761 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001762 }
1763 case Instruction::Store:
1764 {
1765 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1766
1767 if (!store_inst)
1768 {
1769 if (log)
1770 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001771 err.SetErrorToGenericError();
1772 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001773 return false;
1774 }
1775
1776 // The semantics of Store are:
1777 // Resolve the region D containing the data to be stored
1778 // Resolve the region P containing a pointer
1779 // Dereference P to get the region R that the data should be stored in
1780 // Transfer a unit of type type(D) from D to R
1781
1782 const Value *value_operand = store_inst->getValueOperand();
1783 const Value *pointer_operand = store_inst->getPointerOperand();
1784
1785 Type *pointer_ty = pointer_operand->getType();
1786 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1787 if (!pointer_ptr_ty)
1788 return false;
1789 Type *target_ty = pointer_ptr_ty->getElementType();
1790
1791 Memory::Region D = frame.ResolveValue(value_operand, llvm_module);
1792 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1793
1794 if (D.IsInvalid())
1795 {
1796 if (log)
1797 log->Printf("StoreInst's value doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001798 err.SetErrorToGenericError();
1799 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001800 return false;
1801 }
1802
1803 if (P.IsInvalid())
1804 {
1805 if (log)
1806 log->Printf("StoreInst's pointer doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001807 err.SetErrorToGenericError();
1808 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001809 return false;
1810 }
1811
1812 DataExtractorSP P_extractor(memory.GetExtractor(P));
1813 DataExtractorSP D_extractor(memory.GetExtractor(D));
1814
1815 if (!P_extractor || !D_extractor)
1816 return false;
1817
Greg Claytonc7bece562013-01-25 18:06:21 +00001818 lldb::offset_t offset = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001819 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1820
1821 Memory::Region R = memory.Lookup(pointer, target_ty);
1822
Sean Callanan80c48c12011-10-21 05:18:02 +00001823 if (R.IsValid())
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001824 {
Sean Callanan80c48c12011-10-21 05:18:02 +00001825 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1826 {
1827 if (log)
1828 log->Printf("Couldn't write to a region on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001829 err.SetErrorToGenericError();
1830 err.SetErrorString(memory_write_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001831 return false;
1832 }
1833 }
1834 else
1835 {
1836 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1837 {
1838 if (log)
1839 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001840 err.SetErrorToGenericError();
1841 err.SetErrorString(memory_write_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001842 return false;
1843 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001844 }
1845
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001846
1847 if (log)
1848 {
1849 log->Printf("Interpreted a StoreInst");
1850 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str());
1851 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1852 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1853 }
1854 }
1855 break;
1856 }
1857
1858 ++frame.m_ii;
1859 }
1860
1861 if (num_insts >= 4096)
Sean Callanan175a0d02012-01-24 22:06:48 +00001862 {
1863 err.SetErrorToGenericError();
1864 err.SetErrorString(infinite_loop_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001865 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001866 }
1867
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001868 return false;
Greg Claytond4e25522011-10-12 00:53:29 +00001869}