blob: 4211c876bc66256e7cf7cf42f6b9a7e4734246c3 [file] [log] [blame]
Sean Callanan47dc4572011-09-15 02:13:07 +00001//===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/DataEncoder.h"
11#include "lldb/Core/Log.h"
12#include "lldb/Core/ValueObjectConstResult.h"
13#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan52d0d022012-02-15 01:40:39 +000014#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan47dc4572011-09-15 02:13:07 +000015#include "lldb/Expression/IRForTarget.h"
16#include "lldb/Expression/IRInterpreter.h"
17
18#include "llvm/Constants.h"
19#include "llvm/Function.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/Support/raw_ostream.h"
Micah Villmow3051ed72012-10-08 16:28:57 +000023#include "llvm/DataLayout.h"
Sean Callanan47dc4572011-09-15 02:13:07 +000024
25#include <map>
26
27using namespace llvm;
28
29IRInterpreter::IRInterpreter(lldb_private::ClangExpressionDeclMap &decl_map,
30 lldb_private::Stream *error_stream) :
31 m_decl_map(decl_map),
32 m_error_stream(error_stream)
33{
34
35}
36
37IRInterpreter::~IRInterpreter()
38{
39
40}
41
42static std::string
43PrintValue(const Value *value, bool truncate = false)
44{
45 std::string s;
46 raw_string_ostream rso(s);
47 value->print(rso);
48 rso.flush();
49 if (truncate)
50 s.resize(s.length() - 1);
51
52 size_t offset;
53 while ((offset = s.find('\n')) != s.npos)
54 s.erase(offset, 1);
55 while (s[0] == ' ' || s[0] == '\t')
56 s.erase(0, 1);
57
58 return s;
59}
60
61static std::string
62PrintType(const Type *type, bool truncate = false)
63{
64 std::string s;
65 raw_string_ostream rso(s);
66 type->print(rso);
67 rso.flush();
68 if (truncate)
69 s.resize(s.length() - 1);
70 return s;
71}
72
Greg Clayton598df882012-03-14 03:07:05 +000073typedef STD_SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP;
74typedef STD_SHARED_PTR(lldb_private::DataExtractor) DataExtractorSP;
Sean Callanan47dc4572011-09-15 02:13:07 +000075
76class Memory
77{
78public:
79 typedef uint32_t index_t;
80
81 struct Allocation
82 {
83 // m_virtual_address is always the address of the variable in the virtual memory
84 // space provided by Memory.
85 //
86 // m_origin is always non-NULL and describes the source of the data (possibly
87 // m_data if this allocation is the authoritative source).
88 //
89 // Possible value configurations:
90 //
91 // Allocation type getValueType() getContextType() m_origin->GetScalar() m_data
92 // =========================================================================================================================
93 // FileAddress eValueTypeFileAddress eContextTypeInvalid A location in a binary NULL
94 // image
95 //
96 // LoadAddress eValueTypeLoadAddress eContextTypeInvalid A location in the target's NULL
97 // virtual memory
98 //
99 // Alloca eValueTypeHostAddress eContextTypeInvalid == m_data->GetBytes() Deleted at end of
100 // execution
101 //
102 // PersistentVar eValueTypeHostAddress eContextTypeClangType A persistent variable's NULL
103 // location in LLDB's memory
104 //
105 // Register [ignored] eContextTypeRegister [ignored] Flushed to the register
106 // at the end of execution
107
108 lldb::addr_t m_virtual_address;
109 size_t m_extent;
110 lldb_private::Value m_origin;
111 lldb::DataBufferSP m_data;
112
113 Allocation (lldb::addr_t virtual_address,
114 size_t extent,
115 lldb::DataBufferSP data) :
116 m_virtual_address(virtual_address),
117 m_extent(extent),
118 m_data(data)
119 {
120 }
121
122 Allocation (const Allocation &allocation) :
123 m_virtual_address(allocation.m_virtual_address),
124 m_extent(allocation.m_extent),
125 m_origin(allocation.m_origin),
126 m_data(allocation.m_data)
127 {
128 }
129 };
130
Greg Clayton598df882012-03-14 03:07:05 +0000131 typedef STD_SHARED_PTR(Allocation) AllocationSP;
Sean Callanan47dc4572011-09-15 02:13:07 +0000132
133 struct Region
134 {
135 AllocationSP m_allocation;
136 uint64_t m_base;
137 uint64_t m_extent;
138
139 Region () :
140 m_allocation(),
141 m_base(0),
142 m_extent(0)
143 {
144 }
145
146 Region (AllocationSP allocation, uint64_t base, uint64_t extent) :
147 m_allocation(allocation),
148 m_base(base),
149 m_extent(extent)
150 {
151 }
152
153 Region (const Region &region) :
154 m_allocation(region.m_allocation),
155 m_base(region.m_base),
156 m_extent(region.m_extent)
157 {
158 }
159
160 bool IsValid ()
161 {
Jim Ingham9880efa2012-08-11 00:35:26 +0000162 return (bool) m_allocation;
Sean Callanan47dc4572011-09-15 02:13:07 +0000163 }
164
165 bool IsInvalid ()
166 {
Sean Callananb386d822012-08-09 00:50:26 +0000167 return !m_allocation;
Sean Callanan47dc4572011-09-15 02:13:07 +0000168 }
169 };
170
171 typedef std::vector <AllocationSP> MemoryMap;
172
173private:
174 lldb::addr_t m_addr_base;
175 lldb::addr_t m_addr_max;
176 MemoryMap m_memory;
177 lldb::ByteOrder m_byte_order;
178 lldb::addr_t m_addr_byte_size;
Micah Villmow3051ed72012-10-08 16:28:57 +0000179 DataLayout &m_target_data;
Sean Callanan47dc4572011-09-15 02:13:07 +0000180
181 lldb_private::ClangExpressionDeclMap &m_decl_map;
182
183 MemoryMap::iterator LookupInternal (lldb::addr_t addr)
184 {
185 for (MemoryMap::iterator i = m_memory.begin(), e = m_memory.end();
186 i != e;
187 ++i)
188 {
189 if ((*i)->m_virtual_address <= addr &&
190 (*i)->m_virtual_address + (*i)->m_extent > addr)
191 return i;
192 }
193
194 return m_memory.end();
195 }
196
197public:
Micah Villmow3051ed72012-10-08 16:28:57 +0000198 Memory (DataLayout &target_data,
Sean Callanan47dc4572011-09-15 02:13:07 +0000199 lldb_private::ClangExpressionDeclMap &decl_map,
200 lldb::addr_t alloc_start,
201 lldb::addr_t alloc_max) :
202 m_addr_base(alloc_start),
203 m_addr_max(alloc_max),
204 m_target_data(target_data),
205 m_decl_map(decl_map)
206 {
207 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Micah Villmowce633582012-10-11 17:21:41 +0000208 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan47dc4572011-09-15 02:13:07 +0000209 }
210
211 Region Malloc (size_t size, size_t align)
212 {
213 lldb::DataBufferSP data(new lldb_private::DataBufferHeap(size, 0));
214
215 if (data)
216 {
217 index_t index = m_memory.size();
218
219 const size_t mask = (align - 1);
220
221 m_addr_base += mask;
222 m_addr_base &= ~mask;
223
224 if (m_addr_base + size < m_addr_base ||
225 m_addr_base + size > m_addr_max)
226 return Region();
227
228 uint64_t base = m_addr_base;
229
230 m_memory.push_back(AllocationSP(new Allocation(base, size, data)));
231
232 m_addr_base += size;
233
234 AllocationSP alloc = m_memory[index];
235
236 alloc->m_origin.GetScalar() = (unsigned long long)data->GetBytes();
237 alloc->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
238 alloc->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
239
240 return Region(alloc, base, size);
241 }
242
243 return Region();
244 }
245
246 Region Malloc (Type *type)
247 {
248 return Malloc (m_target_data.getTypeAllocSize(type),
249 m_target_data.getPrefTypeAlignment(type));
250 }
251
252 Region Place (Type *type, lldb::addr_t base, lldb_private::Value &value)
253 {
254 index_t index = m_memory.size();
255 size_t size = m_target_data.getTypeAllocSize(type);
256
257 m_memory.push_back(AllocationSP(new Allocation(base, size, lldb::DataBufferSP())));
258
259 AllocationSP alloc = m_memory[index];
260
261 alloc->m_origin = value;
262
263 return Region(alloc, base, size);
264 }
265
266 void Free (lldb::addr_t addr)
267 {
268 MemoryMap::iterator i = LookupInternal (addr);
269
270 if (i != m_memory.end())
271 m_memory.erase(i);
272 }
273
274 Region Lookup (lldb::addr_t addr, Type *type)
275 {
276 MemoryMap::iterator i = LookupInternal(addr);
277
Sean Callanan740b3b72012-01-11 02:23:25 +0000278 if (i == m_memory.end() || !type->isSized())
Sean Callanan47dc4572011-09-15 02:13:07 +0000279 return Region();
Sean Callanan740b3b72012-01-11 02:23:25 +0000280
281 size_t size = m_target_data.getTypeStoreSize(type);
Sean Callanan47dc4572011-09-15 02:13:07 +0000282
283 return Region(*i, addr, size);
284 }
285
286 DataEncoderSP GetEncoder (Region region)
287 {
288 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
289 return DataEncoderSP();
290
291 lldb::DataBufferSP buffer = region.m_allocation->m_data;
292
293 if (!buffer)
294 return DataEncoderSP();
295
296 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
297
298 return DataEncoderSP(new lldb_private::DataEncoder(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
299 }
300
301 DataExtractorSP GetExtractor (Region region)
302 {
303 if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
304 return DataExtractorSP();
305
306 lldb::DataBufferSP buffer = region.m_allocation->m_data;
307 size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
308
309 if (buffer)
310 return DataExtractorSP(new lldb_private::DataExtractor(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
311 else
312 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));
313 }
314
315 lldb_private::Value GetAccessTarget(lldb::addr_t addr)
316 {
317 MemoryMap::iterator i = LookupInternal(addr);
318
319 if (i == m_memory.end())
320 return lldb_private::Value();
321
322 lldb_private::Value target = (*i)->m_origin;
323
324 if (target.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
325 {
326 target.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
327 target.SetValueType(lldb_private::Value::eValueTypeHostAddress);
328 target.GetScalar() = (unsigned long long)(*i)->m_data->GetBytes();
329 }
330
331 target.GetScalar() += (addr - (*i)->m_virtual_address);
332
333 return target;
334 }
335
336 bool Write (lldb::addr_t addr, const uint8_t *data, size_t length)
337 {
338 lldb_private::Value target = GetAccessTarget(addr);
339
340 return m_decl_map.WriteTarget(target, data, length);
341 }
342
343 bool Read (uint8_t *data, lldb::addr_t addr, size_t length)
344 {
Sean Callanan557ccd62011-10-21 05:18:02 +0000345 lldb_private::Value source = GetAccessTarget(addr);
Sean Callanan47dc4572011-09-15 02:13:07 +0000346
Sean Callanan557ccd62011-10-21 05:18:02 +0000347 return m_decl_map.ReadTarget(data, source, length);
348 }
349
350 bool WriteToRawPtr (lldb::addr_t addr, const uint8_t *data, size_t length)
351 {
352 lldb_private::Value target = m_decl_map.WrapBareAddress(addr);
353
354 return m_decl_map.WriteTarget(target, data, length);
355 }
356
357 bool ReadFromRawPtr (uint8_t *data, lldb::addr_t addr, size_t length)
358 {
359 lldb_private::Value source = m_decl_map.WrapBareAddress(addr);
360
361 return m_decl_map.ReadTarget(data, source, length);
Sean Callanan47dc4572011-09-15 02:13:07 +0000362 }
363
364 std::string PrintData (lldb::addr_t addr, size_t length)
365 {
366 lldb_private::Value target = GetAccessTarget(addr);
367
368 lldb_private::DataBufferHeap buf(length, 0);
369
370 if (!m_decl_map.ReadTarget(buf.GetBytes(), target, length))
371 return std::string("<couldn't read data>");
372
373 lldb_private::StreamString ss;
374
375 for (size_t i = 0; i < length; i++)
376 {
377 if ((!(i & 0xf)) && i)
378 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
379 else
380 ss.Printf("%02hhx ", buf.GetBytes()[i]);
381 }
382
383 return ss.GetString();
384 }
385
386 std::string SummarizeRegion (Region &region)
387 {
388 lldb_private::StreamString ss;
389
390 lldb_private::Value base = GetAccessTarget(region.m_base);
391
392 ss.Printf("%llx [%s - %s %llx]",
393 region.m_base,
394 lldb_private::Value::GetValueTypeAsCString(base.GetValueType()),
395 lldb_private::Value::GetContextTypeAsCString(base.GetContextType()),
396 base.GetScalar().ULongLong());
397
398 ss.Printf(" %s", PrintData(region.m_base, region.m_extent).c_str());
399
400 return ss.GetString();
401 }
402};
403
404class InterpreterStackFrame
405{
406public:
407 typedef std::map <const Value*, Memory::Region> ValueMap;
408
409 ValueMap m_values;
410 Memory &m_memory;
Micah Villmow3051ed72012-10-08 16:28:57 +0000411 DataLayout &m_target_data;
Sean Callanan47dc4572011-09-15 02:13:07 +0000412 lldb_private::ClangExpressionDeclMap &m_decl_map;
413 const BasicBlock *m_bb;
414 BasicBlock::const_iterator m_ii;
415 BasicBlock::const_iterator m_ie;
416
417 lldb::ByteOrder m_byte_order;
418 size_t m_addr_byte_size;
419
Micah Villmow3051ed72012-10-08 16:28:57 +0000420 InterpreterStackFrame (DataLayout &target_data,
Sean Callanan47dc4572011-09-15 02:13:07 +0000421 Memory &memory,
422 lldb_private::ClangExpressionDeclMap &decl_map) :
Sean Callanan47dc4572011-09-15 02:13:07 +0000423 m_memory (memory),
Daniel Dunbar97c89572011-10-31 22:50:49 +0000424 m_target_data (target_data),
Sean Callanan47dc4572011-09-15 02:13:07 +0000425 m_decl_map (decl_map)
426 {
427 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan4fbe61b2012-10-11 22:00:52 +0000428 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan47dc4572011-09-15 02:13:07 +0000429 }
430
431 void Jump (const BasicBlock *bb)
432 {
433 m_bb = bb;
434 m_ii = m_bb->begin();
435 m_ie = m_bb->end();
436 }
437
438 bool Cache (Memory::AllocationSP allocation, Type *type)
439 {
440 if (allocation->m_origin.GetContextType() != lldb_private::Value::eContextTypeRegisterInfo)
441 return false;
442
443 return m_decl_map.ReadTarget(allocation->m_data->GetBytes(), allocation->m_origin, allocation->m_data->GetByteSize());
444 }
445
446 std::string SummarizeValue (const Value *value)
447 {
448 lldb_private::StreamString ss;
449
450 ss.Printf("%s", PrintValue(value).c_str());
451
452 ValueMap::iterator i = m_values.find(value);
453
454 if (i != m_values.end())
455 {
456 Memory::Region region = i->second;
457
458 ss.Printf(" %s", m_memory.SummarizeRegion(region).c_str());
459 }
460
461 return ss.GetString();
462 }
463
464 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
465 {
466 size_t type_size = m_target_data.getTypeStoreSize(type);
467
468 switch (type_size)
469 {
470 case 1:
471 scalar = (uint8_t)u64value;
472 break;
473 case 2:
474 scalar = (uint16_t)u64value;
475 break;
476 case 4:
477 scalar = (uint32_t)u64value;
478 break;
479 case 8:
480 scalar = (uint64_t)u64value;
481 break;
482 default:
483 return false;
484 }
485
486 return true;
487 }
488
489 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
490 {
491 const Constant *constant = dyn_cast<Constant>(value);
492
493 if (constant)
494 {
495 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
496 {
497 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
498 }
499 }
500 else
501 {
502 Memory::Region region = ResolveValue(value, module);
503 DataExtractorSP value_extractor = m_memory.GetExtractor(region);
504
505 if (!value_extractor)
506 return false;
507
508 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
509
510 uint32_t offset = 0;
511 uint64_t u64value = value_extractor->GetMaxU64(&offset, value_size);
512
513 return AssignToMatchType(scalar, u64value, value->getType());
514 }
515
516 return false;
517 }
518
519 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
520 {
521 Memory::Region region = ResolveValue (value, module);
522
523 lldb_private::Scalar cast_scalar;
524
525 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
526 return false;
527
528 lldb_private::DataBufferHeap buf(cast_scalar.GetByteSize(), 0);
529
530 lldb_private::Error err;
531
532 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, err))
533 return false;
534
535 DataEncoderSP region_encoder = m_memory.GetEncoder(region);
536
537 memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize());
538
539 return true;
540 }
541
Sean Callanan8eac77d2012-02-08 01:27:49 +0000542 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan47dc4572011-09-15 02:13:07 +0000543 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000544 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
545 {
Sean Callanan8eac77d2012-02-08 01:27:49 +0000546 value = constant_int->getValue();
547 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +0000548 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000549 else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
Sean Callanan47dc4572011-09-15 02:13:07 +0000550 {
Sean Callanan8eac77d2012-02-08 01:27:49 +0000551 value = constant_fp->getValueAPF().bitcastToAPInt();
552 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +0000553 }
Sean Callanan557ccd62011-10-21 05:18:02 +0000554 else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
555 {
556 switch (constant_expr->getOpcode())
557 {
Sean Callanan8eac77d2012-02-08 01:27:49 +0000558 default:
559 return false;
560 case Instruction::IntToPtr:
561 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.
622 bool indirect_variable = true;
623
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
667 indirect_variable = false;
668 }
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 Callanan52d0d022012-02-15 01:40:39 +0000674 bool bare_register = (flags & lldb_private::ClangExpressionVariable::EVBareRegister);
675
676 if (bare_register)
677 indirect_variable = false;
678
Greg Clayton7c5e22f2012-10-30 18:18:43 +0000679 lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
680 Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
681 m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
682 m_memory.Malloc(value->getType());
683
Sean Callanan47dc4572011-09-15 02:13:07 +0000684 data_region.m_allocation->m_origin = 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 (!Cache(data_region.m_allocation, value->getType()))
692 return Memory::Region();
693
694 if (ref_region.IsInvalid())
695 return Memory::Region();
696
Sean Callanan4b3cef02011-10-26 21:20:00 +0000697 if (pointer_region.IsInvalid() && indirect_variable)
Sean Callanan47dc4572011-09-15 02:13:07 +0000698 return Memory::Region();
699
700 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
701
702 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
703 return Memory::Region();
704
Sean Callanan4237e1e2012-01-04 21:42:46 +0000705 if (log)
706 {
707 log->Printf("Made an allocation for register variable %s", PrintValue(value).c_str());
708 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
709 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
710 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
711 if (indirect_variable)
712 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
713 }
714
Sean Callanan4b3cef02011-10-26 21:20:00 +0000715 if (indirect_variable)
716 {
717 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
718
719 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
720 return Memory::Region();
721
722 m_values[value] = pointer_region;
723 return pointer_region;
724 }
725 else
726 {
727 m_values[value] = ref_region;
728 return ref_region;
729 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000730 }
731 else
732 {
733 Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
734 Memory::Region ref_region = m_memory.Malloc(value->getType());
Sean Callanan4b3cef02011-10-26 21:20:00 +0000735 Memory::Region pointer_region;
736
737 if (indirect_variable)
738 pointer_region = m_memory.Malloc(value->getType());
Sean Callanan47dc4572011-09-15 02:13:07 +0000739
740 if (ref_region.IsInvalid())
741 return Memory::Region();
742
Sean Callanan4b3cef02011-10-26 21:20:00 +0000743 if (pointer_region.IsInvalid() && indirect_variable)
Sean Callanan47dc4572011-09-15 02:13:07 +0000744 return Memory::Region();
745
746 DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
747
748 if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
749 return Memory::Region();
750
Sean Callanan4b3cef02011-10-26 21:20:00 +0000751 if (indirect_variable)
752 {
753 DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
Sean Callanan47dc4572011-09-15 02:13:07 +0000754
Sean Callanan4b3cef02011-10-26 21:20:00 +0000755 if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
756 return Memory::Region();
757
758 m_values[value] = pointer_region;
759 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000760
761 if (log)
762 {
Sean Callanan4b3cef02011-10-26 21:20:00 +0000763 log->Printf("Made an allocation for %s", PrintValue(value).c_str());
Sean Callanan47dc4572011-09-15 02:13:07 +0000764 log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
765 log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
766 log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
Sean Callanan4b3cef02011-10-26 21:20:00 +0000767 if (indirect_variable)
768 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
Sean Callanan47dc4572011-09-15 02:13:07 +0000769 }
770
Sean Callanan4b3cef02011-10-26 21:20:00 +0000771 if (indirect_variable)
772 return pointer_region;
773 else
774 return ref_region;
Sean Callanan47dc4572011-09-15 02:13:07 +0000775 }
776 }
777 }
778 while(0);
779
780 // Fall back and allocate space [allocation type Alloca]
781
782 Type *type = value->getType();
783
784 lldb::ValueSP backing_value(new lldb_private::Value);
785
786 Memory::Region data_region = m_memory.Malloc(type);
787 data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes();
788 data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
789 data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
790
791 const Constant *constant = dyn_cast<Constant>(value);
792
793 do
794 {
795 if (!constant)
796 break;
797
798 if (!ResolveConstant (data_region, constant))
799 return Memory::Region();
800 }
801 while(0);
802
803 m_values[value] = data_region;
804 return data_region;
805 }
806
807 bool ConstructResult (lldb::ClangExpressionVariableSP &result,
808 const GlobalValue *result_value,
809 const lldb_private::ConstString &result_name,
810 lldb_private::TypeFromParser result_type,
811 Module &module)
812 {
813 // The result_value resolves to P, a pointer to a region R containing the result data.
814 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
815
816 if (!result_value)
817 return true; // There was no slot for a result – the expression doesn't return one.
818
819 ValueMap::iterator i = m_values.find(result_value);
820
821 if (i == m_values.end())
822 return false; // There was a slot for the result, but we didn't write into it.
823
824 Memory::Region P = i->second;
825 DataExtractorSP P_extractor = m_memory.GetExtractor(P);
826
827 if (!P_extractor)
828 return false;
829
830 Type *pointer_ty = result_value->getType();
831 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
832 if (!pointer_ptr_ty)
833 return false;
834 Type *R_ty = pointer_ptr_ty->getElementType();
835
836 uint32_t offset = 0;
837 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
838
839 Memory::Region R = m_memory.Lookup(pointer, R_ty);
840
841 if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress ||
842 !R.m_allocation->m_data)
843 return false;
844
845 lldb_private::Value base;
846
Sean Callanana8428a42011-09-22 00:41:11 +0000847 bool transient = false;
Sean Callanan557ccd62011-10-21 05:18:02 +0000848 bool maybe_make_load = false;
Sean Callanana8428a42011-09-22 00:41:11 +0000849
Sean Callanan47dc4572011-09-15 02:13:07 +0000850 if (m_decl_map.ResultIsReference(result_name))
851 {
852 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
853 if (!R_ptr_ty)
854 return false;
855 Type *R_final_ty = R_ptr_ty->getElementType();
856
857 DataExtractorSP R_extractor = m_memory.GetExtractor(R);
858
859 if (!R_extractor)
860 return false;
861
862 offset = 0;
863 lldb::addr_t R_pointer = R_extractor->GetAddress(&offset);
864
865 Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty);
866
Sean Callanan557ccd62011-10-21 05:18:02 +0000867 if (R_final.m_allocation)
868 {
869 if (R_final.m_allocation->m_data)
870 transient = true; // this is a stack allocation
Sean Callanan47dc4572011-09-15 02:13:07 +0000871
Sean Callanan557ccd62011-10-21 05:18:02 +0000872 base = R_final.m_allocation->m_origin;
873 base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address);
874 }
875 else
876 {
877 // We got a bare pointer. We are going to treat it as a load address
878 // or a file address, letting decl_map make the choice based on whether
879 // or not a process exists.
880
881 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
882 base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
883 base.GetScalar() = (unsigned long long)R_pointer;
884 maybe_make_load = true;
885 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000886 }
887 else
888 {
889 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
890 base.SetValueType(lldb_private::Value::eValueTypeHostAddress);
891 base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address);
892 }
893
Sean Callanan557ccd62011-10-21 05:18:02 +0000894 return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load);
Sean Callanan47dc4572011-09-15 02:13:07 +0000895 }
896};
897
898bool
899IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result,
900 const lldb_private::ConstString &result_name,
901 lldb_private::TypeFromParser result_type,
902 Function &llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000903 Module &llvm_module,
904 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000905{
Sean Callananddf110d2012-01-24 22:06:48 +0000906 if (supportsFunction (llvm_function, err))
Sean Callanan47dc4572011-09-15 02:13:07 +0000907 return runOnFunction(result,
908 result_name,
909 result_type,
910 llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +0000911 llvm_module,
912 err);
Sean Callanan47dc4572011-09-15 02:13:07 +0000913 else
914 return false;
915}
916
Sean Callananddf110d2012-01-24 22:06:48 +0000917static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
918static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
919static const char *interpreter_internal_error = "Interpreter encountered an internal error";
920static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
921static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
922static const char *memory_write_error = "Interpreter couldn't write to memory";
923static const char *memory_read_error = "Interpreter couldn't read from memory";
924static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan8f2e3922012-02-04 08:49:35 +0000925static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callananddf110d2012-01-24 22:06:48 +0000926
Sean Callanan47dc4572011-09-15 02:13:07 +0000927bool
Sean Callananddf110d2012-01-24 22:06:48 +0000928IRInterpreter::supportsFunction (Function &llvm_function,
929 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +0000930{
931 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
932
933 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
934 bbi != bbe;
935 ++bbi)
936 {
937 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
938 ii != ie;
939 ++ii)
940 {
941 switch (ii->getOpcode())
942 {
943 default:
944 {
945 if (log)
946 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +0000947 err.SetErrorToGenericError();
948 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000949 return false;
950 }
951 case Instruction::Add:
952 case Instruction::Alloca:
953 case Instruction::BitCast:
954 case Instruction::Br:
955 case Instruction::GetElementPtr:
956 break;
957 case Instruction::ICmp:
958 {
959 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
960
961 if (!icmp_inst)
Sean Callananddf110d2012-01-24 22:06:48 +0000962 {
963 err.SetErrorToGenericError();
964 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000965 return false;
Sean Callananddf110d2012-01-24 22:06:48 +0000966 }
Sean Callanan47dc4572011-09-15 02:13:07 +0000967
968 switch (icmp_inst->getPredicate())
969 {
970 default:
971 {
972 if (log)
973 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +0000974
975 err.SetErrorToGenericError();
976 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +0000977 return false;
978 }
979 case CmpInst::ICMP_EQ:
980 case CmpInst::ICMP_NE:
981 case CmpInst::ICMP_UGT:
982 case CmpInst::ICMP_UGE:
983 case CmpInst::ICMP_ULT:
984 case CmpInst::ICMP_ULE:
985 case CmpInst::ICMP_SGT:
986 case CmpInst::ICMP_SGE:
987 case CmpInst::ICMP_SLT:
988 case CmpInst::ICMP_SLE:
989 break;
990 }
991 }
992 break;
Sean Callanan557ccd62011-10-21 05:18:02 +0000993 case Instruction::IntToPtr:
Sean Callanan47dc4572011-09-15 02:13:07 +0000994 case Instruction::Load:
995 case Instruction::Mul:
996 case Instruction::Ret:
997 case Instruction::SDiv:
998 case Instruction::Store:
999 case Instruction::Sub:
1000 case Instruction::UDiv:
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001001 case Instruction::ZExt:
Sean Callanan47dc4572011-09-15 02:13:07 +00001002 break;
1003 }
1004 }
1005 }
1006
1007 return true;
1008}
1009
1010bool
1011IRInterpreter::runOnFunction (lldb::ClangExpressionVariableSP &result,
1012 const lldb_private::ConstString &result_name,
1013 lldb_private::TypeFromParser result_type,
1014 Function &llvm_function,
Sean Callananddf110d2012-01-24 22:06:48 +00001015 Module &llvm_module,
1016 lldb_private::Error &err)
Sean Callanan47dc4572011-09-15 02:13:07 +00001017{
1018 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1019
1020 lldb_private::ClangExpressionDeclMap::TargetInfo target_info = m_decl_map.GetTargetInfo();
1021
1022 if (!target_info.IsValid())
Sean Callananddf110d2012-01-24 22:06:48 +00001023 {
1024 err.SetErrorToGenericError();
1025 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001026 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001027 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001028
1029 lldb::addr_t alloc_min;
1030 lldb::addr_t alloc_max;
1031
1032 switch (target_info.address_byte_size)
1033 {
1034 default:
Sean Callananddf110d2012-01-24 22:06:48 +00001035 err.SetErrorToGenericError();
1036 err.SetErrorString(interpreter_initialization_error);
1037 return false;
Sean Callanan47dc4572011-09-15 02:13:07 +00001038 case 4:
1039 alloc_min = 0x00001000llu;
1040 alloc_max = 0x0000ffffllu;
1041 break;
1042 case 8:
1043 alloc_min = 0x0000000000001000llu;
1044 alloc_max = 0x000000000000ffffllu;
1045 break;
1046 }
1047
Micah Villmow3051ed72012-10-08 16:28:57 +00001048 DataLayout target_data(&llvm_module);
Sean Callanan4fbe61b2012-10-11 22:00:52 +00001049 if (target_data.getPointerSize(0) != target_info.address_byte_size)
Sean Callananddf110d2012-01-24 22:06:48 +00001050 {
1051 err.SetErrorToGenericError();
1052 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001053 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001054 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001055 if (target_data.isLittleEndian() != (target_info.byte_order == lldb::eByteOrderLittle))
Sean Callananddf110d2012-01-24 22:06:48 +00001056 {
1057 err.SetErrorToGenericError();
1058 err.SetErrorString(interpreter_initialization_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001059 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001060 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001061
1062 Memory memory(target_data, m_decl_map, alloc_min, alloc_max);
1063 InterpreterStackFrame frame(target_data, memory, m_decl_map);
1064
1065 uint32_t num_insts = 0;
1066
1067 frame.Jump(llvm_function.begin());
1068
1069 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1070 {
1071 const Instruction *inst = frame.m_ii;
1072
1073 if (log)
1074 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1075
1076 switch (inst->getOpcode())
1077 {
1078 default:
1079 break;
1080 case Instruction::Add:
1081 case Instruction::Sub:
1082 case Instruction::Mul:
1083 case Instruction::SDiv:
1084 case Instruction::UDiv:
1085 {
1086 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1087
1088 if (!bin_op)
1089 {
1090 if (log)
1091 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
Sean Callananddf110d2012-01-24 22:06:48 +00001092 err.SetErrorToGenericError();
1093 err.SetErrorString(interpreter_internal_error);
1094 return false;
Sean Callanan47dc4572011-09-15 02:13:07 +00001095 }
1096
1097 Value *lhs = inst->getOperand(0);
1098 Value *rhs = inst->getOperand(1);
1099
1100 lldb_private::Scalar L;
1101 lldb_private::Scalar R;
1102
1103 if (!frame.EvaluateValue(L, lhs, llvm_module))
1104 {
1105 if (log)
1106 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001107 err.SetErrorToGenericError();
1108 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001109 return false;
1110 }
1111
1112 if (!frame.EvaluateValue(R, rhs, llvm_module))
1113 {
1114 if (log)
1115 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001116 err.SetErrorToGenericError();
1117 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001118 return false;
1119 }
1120
1121 lldb_private::Scalar result;
1122
1123 switch (inst->getOpcode())
1124 {
1125 default:
1126 break;
1127 case Instruction::Add:
1128 result = L + R;
1129 break;
1130 case Instruction::Mul:
1131 result = L * R;
1132 break;
1133 case Instruction::Sub:
1134 result = L - R;
1135 break;
1136 case Instruction::SDiv:
1137 result = L / R;
1138 break;
1139 case Instruction::UDiv:
1140 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1141 break;
1142 }
1143
1144 frame.AssignValue(inst, result, llvm_module);
1145
1146 if (log)
1147 {
1148 log->Printf("Interpreted a %s", inst->getOpcodeName());
1149 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1150 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1151 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1152 }
1153 }
1154 break;
1155 case Instruction::Alloca:
1156 {
1157 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1158
1159 if (!alloca_inst)
1160 {
1161 if (log)
1162 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001163 err.SetErrorToGenericError();
1164 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001165 return false;
1166 }
1167
1168 if (alloca_inst->isArrayAllocation())
1169 {
1170 if (log)
1171 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
Sean Callananddf110d2012-01-24 22:06:48 +00001172 err.SetErrorToGenericError();
1173 err.SetErrorString(unsupported_opcode_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001174 return false;
1175 }
1176
1177 // The semantics of Alloca are:
1178 // Create a region R of virtual memory of type T, backed by a data buffer
1179 // Create a region P of virtual memory of type T*, backed by a data buffer
1180 // Write the virtual address of R into P
1181
1182 Type *T = alloca_inst->getAllocatedType();
1183 Type *Tptr = alloca_inst->getType();
1184
1185 Memory::Region R = memory.Malloc(T);
1186
1187 if (R.IsInvalid())
1188 {
1189 if (log)
1190 log->Printf("Couldn't allocate memory for an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001191 err.SetErrorToGenericError();
1192 err.SetErrorString(memory_allocation_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001193 return false;
1194 }
1195
1196 Memory::Region P = memory.Malloc(Tptr);
1197
1198 if (P.IsInvalid())
1199 {
1200 if (log)
1201 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001202 err.SetErrorToGenericError();
1203 err.SetErrorString(memory_allocation_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001204 return false;
1205 }
1206
1207 DataEncoderSP P_encoder = memory.GetEncoder(P);
1208
1209 if (P_encoder->PutAddress(0, R.m_base) == UINT32_MAX)
1210 {
1211 if (log)
Sean Callananddf110d2012-01-24 22:06:48 +00001212 log->Printf("Couldn't write the result pointer for an AllocaInst");
1213 err.SetErrorToGenericError();
1214 err.SetErrorString(memory_write_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001215 return false;
1216 }
1217
1218 frame.m_values[alloca_inst] = P;
1219
1220 if (log)
1221 {
1222 log->Printf("Interpreted an AllocaInst");
1223 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1224 log->Printf(" P : %s", frame.SummarizeValue(alloca_inst).c_str());
1225 }
1226 }
1227 break;
1228 case Instruction::BitCast:
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001229 case Instruction::ZExt:
Sean Callanan47dc4572011-09-15 02:13:07 +00001230 {
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001231 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sean Callanan47dc4572011-09-15 02:13:07 +00001232
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001233 if (!cast_inst)
Sean Callanan47dc4572011-09-15 02:13:07 +00001234 {
1235 if (log)
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001236 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
Sean Callananddf110d2012-01-24 22:06:48 +00001237 err.SetErrorToGenericError();
1238 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001239 return false;
1240 }
1241
Sean Callanan6a3f9af2012-04-23 17:25:38 +00001242 Value *source = cast_inst->getOperand(0);
Sean Callanan47dc4572011-09-15 02:13:07 +00001243
1244 lldb_private::Scalar S;
1245
1246 if (!frame.EvaluateValue(S, source, llvm_module))
1247 {
1248 if (log)
1249 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001250 err.SetErrorToGenericError();
1251 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001252 return false;
1253 }
1254
1255 frame.AssignValue(inst, S, llvm_module);
1256 }
1257 break;
1258 case Instruction::Br:
1259 {
1260 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
1261
1262 if (!br_inst)
1263 {
1264 if (log)
1265 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001266 err.SetErrorToGenericError();
1267 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001268 return false;
1269 }
1270
1271 if (br_inst->isConditional())
1272 {
1273 Value *condition = br_inst->getCondition();
1274
1275 lldb_private::Scalar C;
1276
1277 if (!frame.EvaluateValue(C, condition, llvm_module))
1278 {
1279 if (log)
1280 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001281 err.SetErrorToGenericError();
1282 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001283 return false;
1284 }
1285
1286 if (C.GetRawBits64(0))
1287 frame.Jump(br_inst->getSuccessor(0));
1288 else
1289 frame.Jump(br_inst->getSuccessor(1));
1290
1291 if (log)
1292 {
1293 log->Printf("Interpreted a BrInst with a condition");
1294 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1295 }
1296 }
1297 else
1298 {
1299 frame.Jump(br_inst->getSuccessor(0));
1300
1301 if (log)
1302 {
1303 log->Printf("Interpreted a BrInst with no condition");
1304 }
1305 }
1306 }
1307 continue;
1308 case Instruction::GetElementPtr:
1309 {
1310 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1311
1312 if (!gep_inst)
1313 {
1314 if (log)
1315 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001316 err.SetErrorToGenericError();
1317 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001318 return false;
1319 }
1320
1321 const Value *pointer_operand = gep_inst->getPointerOperand();
1322 Type *pointer_type = pointer_operand->getType();
1323
1324 lldb_private::Scalar P;
1325
1326 if (!frame.EvaluateValue(P, pointer_operand, llvm_module))
Sean Callananddf110d2012-01-24 22:06:48 +00001327 {
1328 if (log)
1329 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1330 err.SetErrorToGenericError();
1331 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001332 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001333 }
1334
Sean Callanan7347ef82012-02-29 17:57:18 +00001335 typedef SmallVector <Value *, 8> IndexVector;
1336 typedef IndexVector::iterator IndexIterator;
1337
Sean Callanan47dc4572011-09-15 02:13:07 +00001338 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1339 gep_inst->idx_end());
1340
Sean Callanan7347ef82012-02-29 17:57:18 +00001341 SmallVector <Value *, 8> const_indices;
1342
1343 for (IndexIterator ii = indices.begin(), ie = indices.end();
1344 ii != ie;
1345 ++ii)
1346 {
1347 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1348
1349 if (!constant_index)
1350 {
1351 lldb_private::Scalar I;
1352
1353 if (!frame.EvaluateValue(I, *ii, llvm_module))
1354 {
1355 if (log)
1356 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1357 err.SetErrorToGenericError();
1358 err.SetErrorString(bad_value_error);
1359 return false;
1360 }
1361
1362 if (log)
1363 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1364
1365 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1366 }
1367
1368 const_indices.push_back(constant_index);
1369 }
1370
1371 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices);
Sean Callanan47dc4572011-09-15 02:13:07 +00001372
1373 lldb_private::Scalar Poffset = P + offset;
1374
1375 frame.AssignValue(inst, Poffset, llvm_module);
1376
1377 if (log)
1378 {
1379 log->Printf("Interpreted a GetElementPtrInst");
1380 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1381 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1382 }
1383 }
1384 break;
1385 case Instruction::ICmp:
1386 {
1387 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1388
1389 if (!icmp_inst)
1390 {
1391 if (log)
1392 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001393 err.SetErrorToGenericError();
1394 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001395 return false;
1396 }
1397
1398 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1399
1400 Value *lhs = inst->getOperand(0);
1401 Value *rhs = inst->getOperand(1);
1402
1403 lldb_private::Scalar L;
1404 lldb_private::Scalar R;
1405
1406 if (!frame.EvaluateValue(L, lhs, llvm_module))
1407 {
1408 if (log)
1409 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001410 err.SetErrorToGenericError();
1411 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001412 return false;
1413 }
1414
1415 if (!frame.EvaluateValue(R, rhs, llvm_module))
1416 {
1417 if (log)
1418 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callananddf110d2012-01-24 22:06:48 +00001419 err.SetErrorToGenericError();
1420 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001421 return false;
1422 }
1423
1424 lldb_private::Scalar result;
1425
1426 switch (predicate)
1427 {
1428 default:
1429 return false;
1430 case CmpInst::ICMP_EQ:
1431 result = (L == R);
1432 break;
1433 case CmpInst::ICMP_NE:
1434 result = (L != R);
1435 break;
1436 case CmpInst::ICMP_UGT:
1437 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1438 break;
1439 case CmpInst::ICMP_UGE:
1440 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1441 break;
1442 case CmpInst::ICMP_ULT:
1443 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1444 break;
1445 case CmpInst::ICMP_ULE:
1446 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1447 break;
1448 case CmpInst::ICMP_SGT:
1449 result = (L > R);
1450 break;
1451 case CmpInst::ICMP_SGE:
1452 result = (L >= R);
1453 break;
1454 case CmpInst::ICMP_SLT:
1455 result = (L < R);
1456 break;
1457 case CmpInst::ICMP_SLE:
1458 result = (L <= R);
1459 break;
1460 }
1461
1462 frame.AssignValue(inst, result, llvm_module);
1463
1464 if (log)
1465 {
1466 log->Printf("Interpreted an ICmpInst");
1467 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1468 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1469 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1470 }
1471 }
1472 break;
Sean Callanan557ccd62011-10-21 05:18:02 +00001473 case Instruction::IntToPtr:
1474 {
1475 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1476
1477 if (!int_to_ptr_inst)
1478 {
1479 if (log)
1480 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001481 err.SetErrorToGenericError();
1482 err.SetErrorString(interpreter_internal_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001483 return false;
1484 }
1485
1486 Value *src_operand = int_to_ptr_inst->getOperand(0);
1487
1488 lldb_private::Scalar I;
1489
1490 if (!frame.EvaluateValue(I, src_operand, llvm_module))
Sean Callananddf110d2012-01-24 22:06:48 +00001491 {
1492 if (log)
1493 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1494 err.SetErrorToGenericError();
1495 err.SetErrorString(bad_value_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001496 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001497 }
Sean Callanan557ccd62011-10-21 05:18:02 +00001498
1499 frame.AssignValue(inst, I, llvm_module);
1500
1501 if (log)
1502 {
1503 log->Printf("Interpreted an IntToPtr");
1504 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1505 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1506 }
1507 }
1508 break;
Sean Callanan47dc4572011-09-15 02:13:07 +00001509 case Instruction::Load:
1510 {
1511 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1512
1513 if (!load_inst)
1514 {
1515 if (log)
1516 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001517 err.SetErrorToGenericError();
1518 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001519 return false;
1520 }
1521
1522 // The semantics of Load are:
1523 // Create a region D that will contain the loaded data
1524 // Resolve the region P containing a pointer
1525 // Dereference P to get the region R that the data should be loaded from
1526 // Transfer a unit of type type(D) from R to D
1527
1528 const Value *pointer_operand = load_inst->getPointerOperand();
1529
1530 Type *pointer_ty = pointer_operand->getType();
1531 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1532 if (!pointer_ptr_ty)
Sean Callananddf110d2012-01-24 22:06:48 +00001533 {
1534 if (log)
1535 log->Printf("getPointerOperand()->getType() is not a PointerType");
1536 err.SetErrorToGenericError();
1537 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001538 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001539 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001540 Type *target_ty = pointer_ptr_ty->getElementType();
1541
1542 Memory::Region D = frame.ResolveValue(load_inst, llvm_module);
1543 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1544
1545 if (D.IsInvalid())
1546 {
1547 if (log)
1548 log->Printf("LoadInst's value doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001549 err.SetErrorToGenericError();
1550 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001551 return false;
1552 }
1553
1554 if (P.IsInvalid())
1555 {
1556 if (log)
1557 log->Printf("LoadInst's pointer doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001558 err.SetErrorToGenericError();
1559 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001560 return false;
1561 }
1562
1563 DataExtractorSP P_extractor(memory.GetExtractor(P));
1564 DataEncoderSP D_encoder(memory.GetEncoder(D));
1565
1566 uint32_t offset = 0;
1567 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1568
1569 Memory::Region R = memory.Lookup(pointer, target_ty);
1570
Sean Callanan557ccd62011-10-21 05:18:02 +00001571 if (R.IsValid())
1572 {
1573 if (!memory.Read(D_encoder->GetDataStart(), R.m_base, target_data.getTypeStoreSize(target_ty)))
1574 {
1575 if (log)
1576 log->Printf("Couldn't read from a region on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001577 err.SetErrorToGenericError();
1578 err.SetErrorString(memory_read_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001579 return false;
1580 }
1581 }
1582 else
1583 {
1584 if (!memory.ReadFromRawPtr(D_encoder->GetDataStart(), pointer, target_data.getTypeStoreSize(target_ty)))
1585 {
1586 if (log)
1587 log->Printf("Couldn't read from a raw pointer on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001588 err.SetErrorToGenericError();
1589 err.SetErrorString(memory_read_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001590 return false;
1591 }
1592 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001593
1594 if (log)
1595 {
1596 log->Printf("Interpreted a LoadInst");
1597 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
Sean Callanan557ccd62011-10-21 05:18:02 +00001598 if (R.IsValid())
1599 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1600 else
1601 log->Printf(" R : raw pointer 0x%llx", (unsigned long long)pointer);
Sean Callanan47dc4572011-09-15 02:13:07 +00001602 log->Printf(" D : %s", frame.SummarizeValue(load_inst).c_str());
1603 }
1604 }
1605 break;
1606 case Instruction::Ret:
1607 {
1608 if (result_name.IsEmpty())
1609 return true;
1610
1611 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString());
Sean Callanan8f2e3922012-02-04 08:49:35 +00001612
1613 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module))
1614 {
1615 if (log)
1616 log->Printf("Couldn't construct the expression's result");
1617 err.SetErrorToGenericError();
1618 err.SetErrorString(bad_result_error);
1619 return false;
1620 }
1621
1622 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +00001623 }
1624 case Instruction::Store:
1625 {
1626 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1627
1628 if (!store_inst)
1629 {
1630 if (log)
1631 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001632 err.SetErrorToGenericError();
1633 err.SetErrorString(interpreter_internal_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001634 return false;
1635 }
1636
1637 // The semantics of Store are:
1638 // Resolve the region D containing the data to be stored
1639 // Resolve the region P containing a pointer
1640 // Dereference P to get the region R that the data should be stored in
1641 // Transfer a unit of type type(D) from D to R
1642
1643 const Value *value_operand = store_inst->getValueOperand();
1644 const Value *pointer_operand = store_inst->getPointerOperand();
1645
1646 Type *pointer_ty = pointer_operand->getType();
1647 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1648 if (!pointer_ptr_ty)
1649 return false;
1650 Type *target_ty = pointer_ptr_ty->getElementType();
1651
1652 Memory::Region D = frame.ResolveValue(value_operand, llvm_module);
1653 Memory::Region P = frame.ResolveValue(pointer_operand, llvm_module);
1654
1655 if (D.IsInvalid())
1656 {
1657 if (log)
1658 log->Printf("StoreInst's value doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001659 err.SetErrorToGenericError();
1660 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001661 return false;
1662 }
1663
1664 if (P.IsInvalid())
1665 {
1666 if (log)
1667 log->Printf("StoreInst's pointer doesn't resolve to anything");
Sean Callananddf110d2012-01-24 22:06:48 +00001668 err.SetErrorToGenericError();
1669 err.SetErrorString(bad_value_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001670 return false;
1671 }
1672
1673 DataExtractorSP P_extractor(memory.GetExtractor(P));
1674 DataExtractorSP D_extractor(memory.GetExtractor(D));
1675
1676 if (!P_extractor || !D_extractor)
1677 return false;
1678
1679 uint32_t offset = 0;
1680 lldb::addr_t pointer = P_extractor->GetAddress(&offset);
1681
1682 Memory::Region R = memory.Lookup(pointer, target_ty);
1683
Sean Callanan557ccd62011-10-21 05:18:02 +00001684 if (R.IsValid())
Sean Callanan47dc4572011-09-15 02:13:07 +00001685 {
Sean Callanan557ccd62011-10-21 05:18:02 +00001686 if (!memory.Write(R.m_base, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1687 {
1688 if (log)
1689 log->Printf("Couldn't write to a region on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001690 err.SetErrorToGenericError();
1691 err.SetErrorString(memory_write_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001692 return false;
1693 }
1694 }
1695 else
1696 {
1697 if (!memory.WriteToRawPtr(pointer, D_extractor->GetDataStart(), target_data.getTypeStoreSize(target_ty)))
1698 {
1699 if (log)
1700 log->Printf("Couldn't write to a raw pointer on behalf of a LoadInst");
Sean Callananddf110d2012-01-24 22:06:48 +00001701 err.SetErrorToGenericError();
1702 err.SetErrorString(memory_write_error);
Sean Callanan557ccd62011-10-21 05:18:02 +00001703 return false;
1704 }
Sean Callanan47dc4572011-09-15 02:13:07 +00001705 }
1706
Sean Callanan47dc4572011-09-15 02:13:07 +00001707
1708 if (log)
1709 {
1710 log->Printf("Interpreted a StoreInst");
1711 log->Printf(" D : %s", frame.SummarizeValue(value_operand).c_str());
1712 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1713 log->Printf(" R : %s", memory.SummarizeRegion(R).c_str());
1714 }
1715 }
1716 break;
1717 }
1718
1719 ++frame.m_ii;
1720 }
1721
1722 if (num_insts >= 4096)
Sean Callananddf110d2012-01-24 22:06:48 +00001723 {
1724 err.SetErrorToGenericError();
1725 err.SetErrorString(infinite_loop_error);
Sean Callanan47dc4572011-09-15 02:13:07 +00001726 return false;
Sean Callananddf110d2012-01-24 22:06:48 +00001727 }
1728
Sean Callanan47dc4572011-09-15 02:13:07 +00001729 return false;
Greg Clayton141f8d92011-10-12 00:53:29 +00001730}