blob: 741f27ea3292e396d0865b1c8ef153cd51ff44b8 [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"
14#include "lldb/Expression/IRForTarget.h"
15#include "lldb/Expression/IRInterpreter.h"
16
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Module.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Target/TargetData.h"
23
24#include <map>
25
26using namespace llvm;
27
28IRInterpreter::IRInterpreter(lldb_private::ClangExpressionDeclMap &decl_map,
29 lldb_private::Stream *error_stream) :
30 m_decl_map(decl_map),
31 m_error_stream(error_stream)
32{
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 Clayton13d24fb2012-01-29 20:56:30 +000072typedef SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP;
73typedef 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 Clayton13d24fb2012-01-29 20:56:30 +0000130 typedef 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 {
161 return m_allocation != NULL;
162 }
163
164 bool IsInvalid ()
165 {
166 return m_allocation == NULL;
167 }
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;
178 TargetData &m_target_data;
179
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:
197 Memory (TargetData &target_data,
198 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);
207 m_addr_byte_size = (target_data.getPointerSize());
208 }
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
391 ss.Printf("%llx [%s - %s %llx]",
392 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;
410 TargetData &m_target_data;
411 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
419 InterpreterStackFrame (TargetData &target_data,
420 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);
427 m_addr_byte_size = (target_data.getPointerSize());
428 }
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
541 bool ResolveConstant (Memory::Region &region, const Constant *constant)
542 {
543 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
544
545 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
546 {
547 const uint64_t *raw_data = constant_int->getValue().getRawData();
548 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size);
549 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000550 else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
Sean Callanan47dc4572011-09-15 02:13:07 +0000551 {
552 const uint64_t *raw_data = constant_fp->getValueAPF().bitcastToAPInt().getRawData();
553 return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size);
554 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000555 else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
556 {
557 switch (constant_expr->getOpcode())
558 {
559 default:
560 return false;
561 case Instruction::IntToPtr:
562 case Instruction::BitCast:
563 return ResolveConstant(region, constant_expr->getOperand(0));
564 }
565 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000566
567 return false;
568 }
569
570 Memory::Region ResolveValue (const Value *value, Module &module)
571 {
572 ValueMap::iterator i = m_values.find(value);
573
574 if (i != m_values.end())
575 return i->second;
576
577 const GlobalValue *global_value = dyn_cast<GlobalValue>(value);
578
Sean Callanan4b3cef02011-10-26 21:20:00 +0000579 // If the variable is indirected through the argument
580 // array then we need to build an extra level of indirection
581 // for it. This is the default; only magic arguments like
582 // "this", "self", and "_cmd" are direct.
583 bool indirect_variable = true;
584
Sean Callanan47dc4572011-09-15 02:13:07 +0000585 // Attempt to resolve the value using the program's data.
586 // If it is, the values to be created are:
587 //
588 // data_region - a region of memory in which the variable's data resides.
589 // ref_region - a region of memory in which its address (i.e., &var) resides.
590 // In the JIT case, this region would be a member of the struct passed in.
591 // pointer_region - a region of memory in which the address of the pointer
592 // resides. This is an IR-level variable.
593 do
594 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000595 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan4b3cef02011-10-26 21:20:00 +0000596
597 lldb_private::Value resolved_value;
Sean Callanan47dc4572011-09-15 02:13:07 +0000598
Sean Callanan4b3cef02011-10-26 21:20:00 +0000599 if (global_value)
600 {
601 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module);
602
603 if (!decl)
604 break;
605
606 if (isa<clang::FunctionDecl>(decl))
607 {
608 if (log)
609 log->Printf("The interpreter does not handle function pointers at the moment");
610
611 return Memory::Region();
612 }
613
614 resolved_value = m_decl_map.LookupDecl(decl);
615 }
616 else
617 {
618 // Special-case "this", "self", and "_cmd"
619
Sean Callananfecc09c2011-11-19 02:54:21 +0000620 std::string name_str = value->getName().str();
Sean Callanan4b3cef02011-10-26 21:20:00 +0000621
622 if (name_str == "this" ||
623 name_str == "self" ||
624 name_str == "_cmd")
625 resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str()));
626
627 indirect_variable = false;
628 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000629
630 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void)
631 {
632 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
633 {
634 Memory::Region data_region = m_memory.Malloc(value->getType());
635 data_region.m_allocation->m_origin = resolved_value;
636 Memory::Region ref_region = m_memory.Malloc(value->getType());
Sean Callanan4b3cef02011-10-26 21:20:00 +0000637 Memory::Region pointer_region;
638
639 if (indirect_variable)
640 pointer_region = m_memory.Malloc(value->getType());
Sean Callanan47dc4572011-09-15 02:13:07 +0000641
642 if (!Cache(data_region.m_allocation, value->getType()))
643 return Memory::Region();
644
645 if (ref_region.IsInvalid())
646 return Memory::Region();
647
Sean Callanan4b3cef02011-10-26 21:20:00 +0000648 if (pointer_region.IsInvalid() && indirect_variable)
Sean Callanan47dc4572011-09-15 02:13:07 +0000649 return Memory::Region();
650
651 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
652
653 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
654 return Memory::Region();
655
Sean Callanan4237e1e2012-01-04 21:42:46 +0000656 if (log)
657 {
658 log->Printf("Made an allocation for register variable %s", PrintValue(value).c_str());
659 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
660 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
661 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
662 if (indirect_variable)
663 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
664 }
665
Sean Callanan4b3cef02011-10-26 21:20:00 +0000666 if (indirect_variable)
667 {
668 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
669
670 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
671 return Memory::Region();
672
673 m_values[value] = pointer_region;
674 return pointer_region;
675 }
676 else
677 {
678 m_values[value] = ref_region;
679 return ref_region;
680 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000681 }
682 else
683 {
684 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
685 Memory::Region ref_region = m_memory.Malloc(value->getType());
Sean Callanan4b3cef02011-10-26 21:20:00 +0000686 Memory::Region pointer_region;
687
688 if (indirect_variable)
689 pointer_region = m_memory.Malloc(value->getType());
Sean Callanan47dc4572011-09-15 02:13:07 +0000690
691 if (ref_region.IsInvalid())
692 return Memory::Region();
693
Sean Callanan4b3cef02011-10-26 21:20:00 +0000694 if (pointer_region.IsInvalid() && indirect_variable)
Sean Callanan47dc4572011-09-15 02:13:07 +0000695 return Memory::Region();
696
697 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
698
699 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
700 return Memory::Region();
701
Sean Callanan4b3cef02011-10-26 21:20:00 +0000702 if (indirect_variable)
703 {
704 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
Sean Callanan47dc4572011-09-15 02:13:07 +0000705
Sean Callanan4b3cef02011-10-26 21:20:00 +0000706 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
707 return Memory::Region();
708
709 m_values[value] = pointer_region;
710 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000711
712 if (log)
713 {
Sean Callanan4b3cef02011-10-26 21:20:00 +0000714 log->Printf("Made an allocation for %s", PrintValue(value).c_str());
Sean Callanan47dc4572011-09-15 02:13:07 +0000715 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
716 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
717 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
Sean Callanan4b3cef02011-10-26 21:20:00 +0000718 if (indirect_variable)
719 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
Sean Callanan47dc4572011-09-15 02:13:07 +0000720 }
721
Sean Callanan4b3cef02011-10-26 21:20:00 +0000722 if (indirect_variable)
723 return pointer_region;
724 else
725 return ref_region;
Sean Callanan47dc4572011-09-15 02:13:07 +0000726 }
727 }
728 }
729 while(0);
730
731 // Fall back and allocate space [allocation type Alloca]
732
733 Type *type = value->getType();
734
735 lldb::ValueSP backing_value(new lldb_private::Value);
736
737 Memory::Region data_region = m_memory.Malloc(type);
738 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes();
739 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
740 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
741
742 const Constant *constant = dyn_cast<Constant>(value);
743
744 do
745 {
746 if (!constant)
747 break;
748
749 if (!ResolveConstant (data_region, constant))
750 return Memory::Region();
751 }
752 while(0);
753
754 m_values[value] = data_region;
755 return data_region;
756 }
757
758 bool ConstructResult (lldb::ClangExpressionVariableSP &result,
759 const GlobalValue *result_value,
760 const lldb_private::ConstString &result_name,
761 lldb_private::TypeFromParser result_type,
762 Module &module)
763 {
764 // The result_value resolves to P, a pointer to a region R containing the result data.
765 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
766
767 if (!result_value)
768 return true; // There was no slot for a result – the expression doesn't return one.
769
770 ValueMap::iterator i = m_values.find(result_value);
771
772 if (i == m_values.end())
773 return false; // There was a slot for the result, but we didn't write into it.
774
775 Memory::Region P = i->second;
776 DataExtractorSP P_extractor = m_memory.GetExtractor(P);
777
778 if (!P_extractor)
779 return false;
780
781 Type *pointer_ty = result_value->getType();
782 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
783 if (!pointer_ptr_ty)
784 return false;
785 Type *R_ty = pointer_ptr_ty->getElementType();
786
787 uint32_t offset = 0;
788 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
789
790 Memory::Region R = m_memory.Lookup(pointer, R_ty);
791
792 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress ||
793 !R.m_allocation->m_data)
794 return false;
795
796 lldb_private::Value base;
797
Sean Callanana8428a42011-09-22 00:41:11 +0000798 bool transient = false;
Sean Callanan557ccd62011-10-21 05:18:02 +0000799 bool maybe_make_load = false;
Sean Callanana8428a42011-09-22 00:41:11 +0000800
Sean Callanan47dc4572011-09-15 02:13:07 +0000801 if (m_decl_map.ResultIsReference(result_name))
802 {
803 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
804 if (!R_ptr_ty)
805 return false;
806 Type *R_final_ty = R_ptr_ty->getElementType();
807
808 DataExtractorSP R_extractor = m_memory.GetExtractor(R);
809
810 if (!R_extractor)
811 return false;
812
813 offset = 0;
814 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset);
815
816 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty);
817
Sean Callanan557ccd62011-10-21 05:18:02 +0000818 if (R_final.m_allocation)
819 {
820 if (R_final.m_allocation->m_data)
821 transient = true; // this is a stack allocation
Sean Callanan47dc4572011-09-15 02:13:07 +0000822
Sean Callanan557ccd62011-10-21 05:18:02 +0000823 base = R_final.m_allocation->m_origin;
824 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address);
825 }
826 else
827 {
828 // We got a bare pointer. We are going to treat it as a load address
829 // or a file address, letting decl_map make the choice based on whether
830 // or not a process exists.
831
832 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
833 base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
834 base.GetScalar() = (unsigned long long)R_pointer;
835 maybe_make_load = true;
836 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000837 }
838 else
839 {
840 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
841 base.SetValueType(lldb_private::Value::eValueTypeHostAddress);
842 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address);
843 }
844
Sean Callanan557ccd62011-10-21 05:18:02 +0000845 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load);
Sean Callanan47dc4572011-09-15 02:13:07 +0000846 }
847};
848
849bool
850IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result,
851 const lldb_private::ConstString &result_name,
852 lldb_private::TypeFromParser result_type,
853 Function &llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000854 Module &llvm_module,
855 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000856{
Sean Callananddf110d2012-01-24 22:06:48 +0000857 if (supportsFunction (llvm_function, err))
Sean Callanan47dc4572011-09-15 02:13:07 +0000858 return runOnFunction(result,
859 result_name,
860 result_type,
861 llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000862 llvm_module,
863 err);
Sean Callanan47dc4572011-09-15 02:13:07 +0000864 else
865 return false;
866}
867
Sean Callananddf110d2012-01-24 22:06:48 +0000868static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
869static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
870static const char *interpreter_internal_error = "Interpreter encountered an internal error";
871static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
872static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
873static const char *memory_write_error = "Interpreter couldn't write to memory";
874static const char *memory_read_error = "Interpreter couldn't read from memory";
875static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan8f2e3922012-02-04 08:49:35 +0000876static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callananddf110d2012-01-24 22:06:48 +0000877
Sean Callanan47dc4572011-09-15 02:13:07 +0000878bool
Sean Callananddf110d2012-01-24 22:06:48 +0000879IRInterpreter::supportsFunction (Function &llvm_function,
880 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000881{
882 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
883
884 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
885 bbi != bbe;
886 ++bbi)
887 {
888 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
889 ii != ie;
890 ++ii)
891 {
892 switch (ii->getOpcode())
893 {
894 default:
895 {
896 if (log)
897 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +0000898 err.SetErrorToGenericError();
899 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000900 return false;
901 }
902 case Instruction::Add:
903 case Instruction::Alloca:
904 case Instruction::BitCast:
905 case Instruction::Br:
906 case Instruction::GetElementPtr:
907 break;
908 case Instruction::ICmp:
909 {
910 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
911
912 if (!icmp_inst)
Sean Callananddf110d2012-01-24 22:06:48 +0000913 {
914 err.SetErrorToGenericError();
915 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000916 return false;
Sean Callananddf110d2012-01-24 22:06:48 +0000917 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000918
919 switch (icmp_inst->getPredicate())
920 {
921 default:
922 {
923 if (log)
924 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +0000925
926 err.SetErrorToGenericError();
927 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000928 return false;
929 }
930 case CmpInst::ICMP_EQ:
931 case CmpInst::ICMP_NE:
932 case CmpInst::ICMP_UGT:
933 case CmpInst::ICMP_UGE:
934 case CmpInst::ICMP_ULT:
935 case CmpInst::ICMP_ULE:
936 case CmpInst::ICMP_SGT:
937 case CmpInst::ICMP_SGE:
938 case CmpInst::ICMP_SLT:
939 case CmpInst::ICMP_SLE:
940 break;
941 }
942 }
943 break;
Sean Callanan557ccd62011-10-21 05:18:02 +0000944 case Instruction::IntToPtr:
Sean Callanan47dc4572011-09-15 02:13:07 +0000945 case Instruction::Load:
946 case Instruction::Mul:
947 case Instruction::Ret:
948 case Instruction::SDiv:
949 case Instruction::Store:
950 case Instruction::Sub:
951 case Instruction::UDiv:
952 break;
953 }
954 }
955 }
956
957 return true;
958}
959
960bool
961IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result,
962 const lldb_private::ConstString &result_name,
963 lldb_private::TypeFromParser result_type,
964 Function &llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000965 Module &llvm_module,
966 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000967{
968 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
969
970 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo();
971
972 if (!target_info.IsValid())
Sean Callananddf110d2012-01-24 22:06:48 +0000973 {
974 err.SetErrorToGenericError();
975 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000976 return false;
Sean Callananddf110d2012-01-24 22:06:48 +0000977 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000978
979 lldb::addr_t alloc_min;
980 lldb::addr_t alloc_max;
981
982 switch (target_info.address_byte_size)
983 {
984 default:
Sean Callananddf110d2012-01-24 22:06:48 +0000985 err.SetErrorToGenericError();
986 err.SetErrorString(interpreter_initialization_error);
987 return false;
Sean Callanan47dc4572011-09-15 02:13:07 +0000988 case 4:
989 alloc_min = 0x00001000llu;
990 alloc_max = 0x0000ffffllu;
991 break;
992 case 8:
993 alloc_min = 0x0000000000001000llu;
994 alloc_max = 0x000000000000ffffllu;
995 break;
996 }
997
998 TargetData target_data(&llvm_module);
999 if (target_data.getPointerSize() != target_info.address_byte_size)
Sean Callananddf110d2012-01-24 22:06:48 +00001000 {
1001 err.SetErrorToGenericError();
1002 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001003 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001004 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001005 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle))
Sean Callananddf110d2012-01-24 22:06:48 +00001006 {
1007 err.SetErrorToGenericError();
1008 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001009 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001010 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001011
1012 Memory memory(target_data, m_decl_map, alloc_min, alloc_max);
1013 InterpreterStackFrame frame(target_data, memory, m_decl_map);
1014
1015 uint32_t num_insts = 0;
1016
1017 frame.Jump(llvm_function.begin());
1018
1019 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1020 {
1021 const Instruction *inst = frame.m_ii;
1022
1023 if (log)
1024 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1025
1026 switch (inst->getOpcode())
1027 {
1028 default:
1029 break;
1030 case Instruction::Add:
1031 case Instruction::Sub:
1032 case Instruction::Mul:
1033 case Instruction::SDiv:
1034 case Instruction::UDiv:
1035 {
1036 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1037
1038 if (!bin_op)
1039 {
1040 if (log)
1041 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
Sean Callananddf110d2012-01-24 22:06:48 +00001042 err.SetErrorToGenericError();
1043 err.SetErrorString(interpreter_internal_error);
1044 return false;
Sean Callanan47dc4572011-09-15 02:13:07 +00001045 }
1046
1047 Value *lhs = inst->getOperand(0);
1048 Value *rhs = inst->getOperand(1);
1049
1050 lldb_private::Scalar L;
1051 lldb_private::Scalar R;
1052
1053 if (!frame.EvaluateValue(L, lhs, llvm_module))
1054 {
1055 if (log)
1056 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001057 err.SetErrorToGenericError();
1058 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001059 return false;
1060 }
1061
1062 if (!frame.EvaluateValue(R, rhs, llvm_module))
1063 {
1064 if (log)
1065 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001066 err.SetErrorToGenericError();
1067 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001068 return false;
1069 }
1070
1071 lldb_private::Scalar result;
1072
1073 switch (inst->getOpcode())
1074 {
1075 default:
1076 break;
1077 case Instruction::Add:
1078 result = L + R;
1079 break;
1080 case Instruction::Mul:
1081 result = L * R;
1082 break;
1083 case Instruction::Sub:
1084 result = L - R;
1085 break;
1086 case Instruction::SDiv:
1087 result = L / R;
1088 break;
1089 case Instruction::UDiv:
1090 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1091 break;
1092 }
1093
1094 frame.AssignValue(inst, result, llvm_module);
1095
1096 if (log)
1097 {
1098 log->Printf("Interpreted a %s", inst->getOpcodeName());
1099 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1100 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1101 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1102 }
1103 }
1104 break;
1105 case Instruction::Alloca:
1106 {
1107 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1108
1109 if (!alloca_inst)
1110 {
1111 if (log)
1112 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001113 err.SetErrorToGenericError();
1114 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001115 return false;
1116 }
1117
1118 if (alloca_inst->isArrayAllocation())
1119 {
1120 if (log)
1121 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
Sean Callananddf110d2012-01-24 22:06:48 +00001122 err.SetErrorToGenericError();
1123 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001124 return false;
1125 }
1126
1127 // The semantics of Alloca are:
1128 // Create a region R of virtual memory of type T, backed by a data buffer
1129 // Create a region P of virtual memory of type T*, backed by a data buffer
1130 // Write the virtual address of R into P
1131
1132 Type *T = alloca_inst->getAllocatedType();
1133 Type *Tptr = alloca_inst->getType();
1134
1135 Memory::Region R = memory.Malloc(T);
1136
1137 if (R.IsInvalid())
1138 {
1139 if (log)
1140 log->Printf("Couldn't allocate memory for an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001141 err.SetErrorToGenericError();
1142 err.SetErrorString(memory_allocation_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001143 return false;
1144 }
1145
1146 Memory::Region P = memory.Malloc(Tptr);
1147
1148 if (P.IsInvalid())
1149 {
1150 if (log)
1151 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001152 err.SetErrorToGenericError();
1153 err.SetErrorString(memory_allocation_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001154 return false;
1155 }
1156
1157 DataEncoderSP P_encoder = memory.GetEncoder(P);
1158
1159 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX)
1160 {
1161 if (log)
Sean Callananddf110d2012-01-24 22:06:48 +00001162 log->Printf("Couldn't write the result pointer for an AllocaInst");
1163 err.SetErrorToGenericError();
1164 err.SetErrorString(memory_write_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001165 return false;
1166 }
1167
1168 frame.m_values[alloca_inst] = P;
1169
1170 if (log)
1171 {
1172 log->Printf("Interpreted an AllocaInst");
1173 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1174 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str());
1175 }
1176 }
1177 break;
1178 case Instruction::BitCast:
1179 {
1180 const BitCastInst *bit_cast_inst = dyn_cast<BitCastInst>(inst);
1181
1182 if (!bit_cast_inst)
1183 {
1184 if (log)
1185 log->Printf("getOpcode() returns BitCast, but instruction is not a BitCastInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001186 err.SetErrorToGenericError();
1187 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001188 return false;
1189 }
1190
1191 Value *source = bit_cast_inst->getOperand(0);
1192
1193 lldb_private::Scalar S;
1194
1195 if (!frame.EvaluateValue(S, source, llvm_module))
1196 {
1197 if (log)
1198 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001199 err.SetErrorToGenericError();
1200 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001201 return false;
1202 }
1203
1204 frame.AssignValue(inst, S, llvm_module);
1205 }
1206 break;
1207 case Instruction::Br:
1208 {
1209 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
1210
1211 if (!br_inst)
1212 {
1213 if (log)
1214 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001215 err.SetErrorToGenericError();
1216 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001217 return false;
1218 }
1219
1220 if (br_inst->isConditional())
1221 {
1222 Value *condition = br_inst->getCondition();
1223
1224 lldb_private::Scalar C;
1225
1226 if (!frame.EvaluateValue(C, condition, llvm_module))
1227 {
1228 if (log)
1229 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001230 err.SetErrorToGenericError();
1231 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001232 return false;
1233 }
1234
1235 if (C.GetRawBits64(0))
1236 frame.Jump(br_inst->getSuccessor(0));
1237 else
1238 frame.Jump(br_inst->getSuccessor(1));
1239
1240 if (log)
1241 {
1242 log->Printf("Interpreted a BrInst with a condition");
1243 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1244 }
1245 }
1246 else
1247 {
1248 frame.Jump(br_inst->getSuccessor(0));
1249
1250 if (log)
1251 {
1252 log->Printf("Interpreted a BrInst with no condition");
1253 }
1254 }
1255 }
1256 continue;
1257 case Instruction::GetElementPtr:
1258 {
1259 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1260
1261 if (!gep_inst)
1262 {
1263 if (log)
1264 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001265 err.SetErrorToGenericError();
1266 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001267 return false;
1268 }
1269
1270 const Value *pointer_operand = gep_inst->getPointerOperand();
1271 Type *pointer_type = pointer_operand->getType();
1272
1273 lldb_private::Scalar P;
1274
1275 if (!frame.EvaluateValue(P, pointer_operand, llvm_module))
Sean Callananddf110d2012-01-24 22:06:48 +00001276 {
1277 if (log)
1278 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1279 err.SetErrorToGenericError();
1280 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001281 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001282 }
1283
Sean Callanan47dc4572011-09-15 02:13:07 +00001284 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1285 gep_inst->idx_end());
1286
1287 uint64_t offset = target_data.getIndexedOffset(pointer_type, indices);
1288
1289 lldb_private::Scalar Poffset = P + offset;
1290
1291 frame.AssignValue(inst, Poffset, llvm_module);
1292
1293 if (log)
1294 {
1295 log->Printf("Interpreted a GetElementPtrInst");
1296 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1297 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1298 }
1299 }
1300 break;
1301 case Instruction::ICmp:
1302 {
1303 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1304
1305 if (!icmp_inst)
1306 {
1307 if (log)
1308 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001309 err.SetErrorToGenericError();
1310 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001311 return false;
1312 }
1313
1314 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1315
1316 Value *lhs = inst->getOperand(0);
1317 Value *rhs = inst->getOperand(1);
1318
1319 lldb_private::Scalar L;
1320 lldb_private::Scalar R;
1321
1322 if (!frame.EvaluateValue(L, lhs, llvm_module))
1323 {
1324 if (log)
1325 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001326 err.SetErrorToGenericError();
1327 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001328 return false;
1329 }
1330
1331 if (!frame.EvaluateValue(R, rhs, llvm_module))
1332 {
1333 if (log)
1334 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001335 err.SetErrorToGenericError();
1336 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001337 return false;
1338 }
1339
1340 lldb_private::Scalar result;
1341
1342 switch (predicate)
1343 {
1344 default:
1345 return false;
1346 case CmpInst::ICMP_EQ:
1347 result = (L == R);
1348 break;
1349 case CmpInst::ICMP_NE:
1350 result = (L != R);
1351 break;
1352 case CmpInst::ICMP_UGT:
1353 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1354 break;
1355 case CmpInst::ICMP_UGE:
1356 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1357 break;
1358 case CmpInst::ICMP_ULT:
1359 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1360 break;
1361 case CmpInst::ICMP_ULE:
1362 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1363 break;
1364 case CmpInst::ICMP_SGT:
1365 result = (L > R);
1366 break;
1367 case CmpInst::ICMP_SGE:
1368 result = (L >= R);
1369 break;
1370 case CmpInst::ICMP_SLT:
1371 result = (L < R);
1372 break;
1373 case CmpInst::ICMP_SLE:
1374 result = (L <= R);
1375 break;
1376 }
1377
1378 frame.AssignValue(inst, result, llvm_module);
1379
1380 if (log)
1381 {
1382 log->Printf("Interpreted an ICmpInst");
1383 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1384 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1385 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1386 }
1387 }
1388 break;
Sean Callanan557ccd62011-10-21 05:18:02 +00001389 case Instruction::IntToPtr:
1390 {
1391 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1392
1393 if (!int_to_ptr_inst)
1394 {
1395 if (log)
1396 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001397 err.SetErrorToGenericError();
1398 err.SetErrorString(interpreter_internal_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001399 return false;
1400 }
1401
1402 Value *src_operand = int_to_ptr_inst->getOperand(0);
1403
1404 lldb_private::Scalar I;
1405
1406 if (!frame.EvaluateValue(I, src_operand, llvm_module))
Sean Callananddf110d2012-01-24 22:06:48 +00001407 {
1408 if (log)
1409 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1410 err.SetErrorToGenericError();
1411 err.SetErrorString(bad_value_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001412 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001413 }
Sean Callanan557ccd62011-10-21 05:18:02 +00001414
1415 frame.AssignValue(inst, I, llvm_module);
1416
1417 if (log)
1418 {
1419 log->Printf("Interpreted an IntToPtr");
1420 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1421 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1422 }
1423 }
1424 break;
Sean Callanan47dc4572011-09-15 02:13:07 +00001425 case Instruction::Load:
1426 {
1427 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1428
1429 if (!load_inst)
1430 {
1431 if (log)
1432 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001433 err.SetErrorToGenericError();
1434 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001435 return false;
1436 }
1437
1438 // The semantics of Load are:
1439 // Create a region D that will contain the loaded data
1440 // Resolve the region P containing a pointer
1441 // Dereference P to get the region R that the data should be loaded from
1442 // Transfer a unit of type type(D) from R to D
1443
1444 const Value *pointer_operand = load_inst->getPointerOperand();
1445
1446 Type *pointer_ty = pointer_operand->getType();
1447 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1448 if (!pointer_ptr_ty)
Sean Callananddf110d2012-01-24 22:06:48 +00001449 {
1450 if (log)
1451 log->Printf("getPointerOperand()->getType() is not a PointerType");
1452 err.SetErrorToGenericError();
1453 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001454 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001455 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001456 Type *target_ty = pointer_ptr_ty->getElementType();
1457
1458 Memory::Region D = frame.ResolveValue(load_inst, llvm_module);
1459 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1460
1461 if (D.IsInvalid())
1462 {
1463 if (log)
1464 log->Printf("LoadInst's value doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001465 err.SetErrorToGenericError();
1466 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001467 return false;
1468 }
1469
1470 if (P.IsInvalid())
1471 {
1472 if (log)
1473 log->Printf("LoadInst's pointer doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001474 err.SetErrorToGenericError();
1475 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001476 return false;
1477 }
1478
1479 DataExtractorSP P_extractor(memory.GetExtractor(P));
1480 DataEncoderSP D_encoder(memory.GetEncoder(D));
1481
1482 uint32_t offset = 0;
1483 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1484
1485 Memory::Region R = memory.Lookup(pointer, target_ty);
1486
Sean Callanan557ccd62011-10-21 05:18:02 +00001487 if (R.IsValid())
1488 {
1489 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)))
1490 {
1491 if (log)
1492 log->Printf("Couldn't read from a region on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001493 err.SetErrorToGenericError();
1494 err.SetErrorString(memory_read_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001495 return false;
1496 }
1497 }
1498 else
1499 {
1500 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty)))
1501 {
1502 if (log)
1503 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001504 err.SetErrorToGenericError();
1505 err.SetErrorString(memory_read_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001506 return false;
1507 }
1508 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001509
1510 if (log)
1511 {
1512 log->Printf("Interpreted a LoadInst");
1513 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
Sean Callanan557ccd62011-10-21 05:18:02 +00001514 if (R.IsValid())
1515 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1516 else
1517 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer);
Sean Callanan47dc4572011-09-15 02:13:07 +00001518 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str());
1519 }
1520 }
1521 break;
1522 case Instruction::Ret:
1523 {
1524 if (result_name.IsEmpty())
1525 return true;
1526
1527 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString());
Sean Callanan8f2e3922012-02-04 08:49:35 +00001528
1529 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module))
1530 {
1531 if (log)
1532 log->Printf("Couldn't construct the expression's result");
1533 err.SetErrorToGenericError();
1534 err.SetErrorString(bad_result_error);
1535 return false;
1536 }
1537
1538 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +00001539 }
1540 case Instruction::Store:
1541 {
1542 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1543
1544 if (!store_inst)
1545 {
1546 if (log)
1547 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001548 err.SetErrorToGenericError();
1549 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001550 return false;
1551 }
1552
1553 // The semantics of Store are:
1554 // Resolve the region D containing the data to be stored
1555 // Resolve the region P containing a pointer
1556 // Dereference P to get the region R that the data should be stored in
1557 // Transfer a unit of type type(D) from D to R
1558
1559 const Value *value_operand = store_inst->getValueOperand();
1560 const Value *pointer_operand = store_inst->getPointerOperand();
1561
1562 Type *pointer_ty = pointer_operand->getType();
1563 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1564 if (!pointer_ptr_ty)
1565 return false;
1566 Type *target_ty = pointer_ptr_ty->getElementType();
1567
1568 Memory::Region D = frame.ResolveValue(value_operand, llvm_module);
1569 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1570
1571 if (D.IsInvalid())
1572 {
1573 if (log)
1574 log->Printf("StoreInst's value doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001575 err.SetErrorToGenericError();
1576 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001577 return false;
1578 }
1579
1580 if (P.IsInvalid())
1581 {
1582 if (log)
1583 log->Printf("StoreInst's pointer doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001584 err.SetErrorToGenericError();
1585 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001586 return false;
1587 }
1588
1589 DataExtractorSP P_extractor(memory.GetExtractor(P));
1590 DataExtractorSP D_extractor(memory.GetExtractor(D));
1591
1592 if (!P_extractor || !D_extractor)
1593 return false;
1594
1595 uint32_t offset = 0;
1596 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1597
1598 Memory::Region R = memory.Lookup(pointer, target_ty);
1599
Sean Callanan557ccd62011-10-21 05:18:02 +00001600 if (R.IsValid())
Sean Callanan47dc4572011-09-15 02:13:07 +00001601 {
Sean Callanan557ccd62011-10-21 05:18:02 +00001602 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1603 {
1604 if (log)
1605 log->Printf("Couldn't write to a region on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001606 err.SetErrorToGenericError();
1607 err.SetErrorString(memory_write_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001608 return false;
1609 }
1610 }
1611 else
1612 {
1613 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1614 {
1615 if (log)
1616 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001617 err.SetErrorToGenericError();
1618 err.SetErrorString(memory_write_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001619 return false;
1620 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001621 }
1622
Sean Callanan47dc4572011-09-15 02:13:07 +00001623
1624 if (log)
1625 {
1626 log->Printf("Interpreted a StoreInst");
1627 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str());
1628 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1629 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1630 }
1631 }
1632 break;
1633 }
1634
1635 ++frame.m_ii;
1636 }
1637
1638 if (num_insts >= 4096)
Sean Callananddf110d2012-01-24 22:06:48 +00001639 {
1640 err.SetErrorToGenericError();
1641 err.SetErrorString(infinite_loop_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001642 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001643 }
1644
Sean Callanan47dc4572011-09-15 02:13:07 +00001645 return false;
Greg Clayton141f8d92011-10-12 00:53:29 +00001646}