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