blob: bfd8abe793ab130a1f751cb0cb1d83c7b5214eda [file] [log] [blame]
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001//===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/DataEncoder.h"
11#include "lldb/Core/Log.h"
12#include "lldb/Core/ValueObjectConstResult.h"
13#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callananf673e762012-02-15 01:40:39 +000014#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000015#include "lldb/Expression/IRForTarget.h"
16#include "lldb/Expression/IRInterpreter.h"
17
Chandler Carruth1e157582013-01-02 12:20:07 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Module.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000023#include "llvm/IR/DataLayout.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000024
25#include <map>
26
27using namespace llvm;
28
Sean Callanan3bfdaa22011-09-15 02:13:07 +000029static std::string
30PrintValue(const Value *value, bool truncate = false)
31{
32 std::string s;
33 raw_string_ostream rso(s);
34 value->print(rso);
35 rso.flush();
36 if (truncate)
37 s.resize(s.length() - 1);
38
39 size_t offset;
40 while ((offset = s.find('\n')) != s.npos)
41 s.erase(offset, 1);
42 while (s[0] == ' ' || s[0] == '\t')
43 s.erase(0, 1);
44
45 return s;
46}
47
48static std::string
49PrintType(const Type *type, bool truncate = false)
50{
51 std::string s;
52 raw_string_ostream rso(s);
53 type->print(rso);
54 rso.flush();
55 if (truncate)
56 s.resize(s.length() - 1);
57 return s;
58}
59
Sean Callanan3bfdaa22011-09-15 02:13:07 +000060class InterpreterStackFrame
61{
62public:
Sean Callanan08052af2013-04-17 07:50:58 +000063 typedef std::map <const Value*, lldb::addr_t> ValueMap;
64
65 struct PlacedValue
66 {
67 lldb_private::Value lldb_value;
68 lldb::addr_t process_address;
69 size_t size;
70
71 PlacedValue (lldb_private::Value &_lldb_value,
72 lldb::addr_t _process_address,
73 size_t _size) :
74 lldb_value(_lldb_value),
75 process_address(_process_address),
76 size(_size)
77 {
78 }
79 };
80
81 typedef std::vector <PlacedValue> PlacedValueVector;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000082
83 ValueMap m_values;
Sean Callanan08052af2013-04-17 07:50:58 +000084 PlacedValueVector m_placed_values;
Micah Villmow8468dbe2012-10-08 16:28:57 +000085 DataLayout &m_target_data;
Sean Callanan175187b2013-04-17 17:51:08 +000086 lldb_private::ClangExpressionDeclMap *m_decl_map;
Sean Callanan179b5482013-04-16 23:49:09 +000087 lldb_private::IRMemoryMap &m_memory_map;
Sean Callanan3bfdaa22011-09-15 02:13:07 +000088 const BasicBlock *m_bb;
89 BasicBlock::const_iterator m_ii;
90 BasicBlock::const_iterator m_ie;
91
Sean Callanan1582ee62013-04-18 22:06:33 +000092 lldb::addr_t m_frame_process_address;
93 size_t m_frame_size;
94 lldb::addr_t m_stack_pointer;
95
Sean Callanan3bfdaa22011-09-15 02:13:07 +000096 lldb::ByteOrder m_byte_order;
97 size_t m_addr_byte_size;
98
Micah Villmow8468dbe2012-10-08 16:28:57 +000099 InterpreterStackFrame (DataLayout &target_data,
Sean Callanan175187b2013-04-17 17:51:08 +0000100 lldb_private::ClangExpressionDeclMap *decl_map,
Sean Callanan179b5482013-04-16 23:49:09 +0000101 lldb_private::IRMemoryMap &memory_map) :
Daniel Dunbara08823f2011-10-31 22:50:49 +0000102 m_target_data (target_data),
Sean Callanan179b5482013-04-16 23:49:09 +0000103 m_decl_map (decl_map),
104 m_memory_map (memory_map)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000105 {
106 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +0000107 m_addr_byte_size = (target_data.getPointerSize(0));
Sean Callanan1582ee62013-04-18 22:06:33 +0000108
109 m_frame_size = 512 * 1024;
110
111 lldb_private::Error alloc_error;
112
113 m_frame_process_address = memory_map.Malloc(m_frame_size,
114 m_addr_byte_size,
115 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
116 lldb_private::IRMemoryMap::eAllocationPolicyMirror,
117 alloc_error);
118
119 if (alloc_error.Success())
120 {
121 m_stack_pointer = m_frame_process_address + m_frame_size;
122 }
123 else
124 {
125 m_frame_process_address = LLDB_INVALID_ADDRESS;
126 m_stack_pointer = LLDB_INVALID_ADDRESS;
127 }
128 }
129
130 ~InterpreterStackFrame ()
131 {
132 if (m_frame_process_address != LLDB_INVALID_ADDRESS)
133 {
134 lldb_private::Error free_error;
135 m_memory_map.Free(m_frame_process_address, free_error);
136 m_frame_process_address = LLDB_INVALID_ADDRESS;
137 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000138 }
139
140 void Jump (const BasicBlock *bb)
141 {
142 m_bb = bb;
143 m_ii = m_bb->begin();
144 m_ie = m_bb->end();
145 }
146
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000147 std::string SummarizeValue (const Value *value)
148 {
149 lldb_private::StreamString ss;
150
151 ss.Printf("%s", PrintValue(value).c_str());
152
153 ValueMap::iterator i = m_values.find(value);
154
155 if (i != m_values.end())
156 {
Sean Callanan08052af2013-04-17 07:50:58 +0000157 lldb::addr_t addr = i->second;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000158
Sean Callanan08052af2013-04-17 07:50:58 +0000159 ss.Printf(" 0x%llx", (unsigned long long)addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000160 }
161
162 return ss.GetString();
163 }
164
165 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
166 {
167 size_t type_size = m_target_data.getTypeStoreSize(type);
168
169 switch (type_size)
170 {
171 case 1:
172 scalar = (uint8_t)u64value;
173 break;
174 case 2:
175 scalar = (uint16_t)u64value;
176 break;
177 case 4:
178 scalar = (uint32_t)u64value;
179 break;
180 case 8:
181 scalar = (uint64_t)u64value;
182 break;
183 default:
184 return false;
185 }
186
187 return true;
188 }
189
190 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
191 {
192 const Constant *constant = dyn_cast<Constant>(value);
193
194 if (constant)
195 {
196 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
197 {
198 return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
199 }
200 }
201 else
202 {
Sean Callanan08052af2013-04-17 07:50:58 +0000203 lldb::addr_t process_address = ResolveValue(value, module);
204 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
205
206 lldb_private::DataExtractor value_extractor;
207 lldb_private::Error extract_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000208
Sean Callanan08052af2013-04-17 07:50:58 +0000209 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
210
211 if (!extract_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000212 return false;
213
Greg Claytonc7bece562013-01-25 18:06:21 +0000214 lldb::offset_t offset = 0;
Sean Callanan08052af2013-04-17 07:50:58 +0000215 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000216
217 return AssignToMatchType(scalar, u64value, value->getType());
218 }
219
220 return false;
221 }
222
223 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
224 {
Sean Callanan08052af2013-04-17 07:50:58 +0000225 lldb::addr_t process_address = ResolveValue (value, module);
226
227 if (process_address == LLDB_INVALID_ADDRESS)
228 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000229
230 lldb_private::Scalar cast_scalar;
231
232 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
233 return false;
Sean Callanan08052af2013-04-17 07:50:58 +0000234
235 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000236
Sean Callanan08052af2013-04-17 07:50:58 +0000237 lldb_private::DataBufferHeap buf(value_byte_size, 0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000238
Sean Callanan08052af2013-04-17 07:50:58 +0000239 lldb_private::Error get_data_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000240
Sean Callanan08052af2013-04-17 07:50:58 +0000241 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000242 return false;
243
Sean Callanan08052af2013-04-17 07:50:58 +0000244 lldb_private::Error write_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000245
Sean Callanan08052af2013-04-17 07:50:58 +0000246 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sean Callananc8675502013-02-15 23:07:52 +0000247
Sean Callanan08052af2013-04-17 07:50:58 +0000248 return write_error.Success();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000249 }
250
Sean Callanan94a9a392012-02-08 01:27:49 +0000251 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000252 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000253 switch (constant->getValueID())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000254 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000255 default:
256 break;
257 case Value::ConstantIntVal:
258 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan80c48c12011-10-21 05:18:02 +0000259 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000260 value = constant_int->getValue();
261 return true;
262 }
263 break;
264 case Value::ConstantFPVal:
265 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
266 {
267 value = constant_fp->getValueAPF().bitcastToAPInt();
268 return true;
269 }
270 break;
271 case Value::ConstantExprVal:
272 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
273 {
274 switch (constant_expr->getOpcode())
Sean Callanan94a9a392012-02-08 01:27:49 +0000275 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000276 default:
Sean Callanan94a9a392012-02-08 01:27:49 +0000277 return false;
Sean Callanan1582ee62013-04-18 22:06:33 +0000278 case Instruction::IntToPtr:
279 case Instruction::PtrToInt:
280 case Instruction::BitCast:
281 return ResolveConstantValue(value, constant_expr->getOperand(0));
282 case Instruction::GetElementPtr:
283 {
284 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
285 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
286
287 Constant *base = dyn_cast<Constant>(*op_cursor);
288
289 if (!base)
290 return false;
291
292 if (!ResolveConstantValue(value, base))
293 return false;
294
295 op_cursor++;
296
297 if (op_cursor == op_end)
298 return true; // no offset to apply!
299
300 SmallVector <Value *, 8> indices (op_cursor, op_end);
301
302 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
303
304 const bool is_signed = true;
305 value += APInt(value.getBitWidth(), offset, is_signed);
306
307 return true;
308 }
Sean Callanan94a9a392012-02-08 01:27:49 +0000309 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000310 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000311 break;
312 case Value::ConstantPointerNullVal:
313 if (isa<ConstantPointerNull>(constant))
314 {
315 value = APInt(m_target_data.getPointerSizeInBits(), 0);
316 return true;
317 }
318 break;
319 }
320 return false;
321 }
322
323 bool MakeArgument(const Argument *value, uint64_t address)
324 {
325 lldb::addr_t data_address = Malloc(value->getType());
326
327 if (data_address == LLDB_INVALID_ADDRESS)
328 return false;
329
330 lldb_private::Error write_error;
331
332 m_memory_map.WritePointerToMemory(data_address, address, write_error);
333
334 if (!write_error.Success())
335 {
336 lldb_private::Error free_error;
337 m_memory_map.Free(data_address, free_error);
338 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000339 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000340
Sean Callanan1582ee62013-04-18 22:06:33 +0000341 m_values[value] = data_address;
342
343 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
344
345 if (log)
346 {
347 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
348 log->Printf(" Data region : %llx", (unsigned long long)address);
349 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
350 }
351
352 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000353 }
354
Sean Callanan08052af2013-04-17 07:50:58 +0000355 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
Sean Callanan94a9a392012-02-08 01:27:49 +0000356 {
357 APInt resolved_value;
358
359 if (!ResolveConstantValue(resolved_value, constant))
360 return false;
361
362 const uint64_t *raw_data = resolved_value.getRawData();
363
364 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Sean Callanan94a9a392012-02-08 01:27:49 +0000365
Sean Callanan08052af2013-04-17 07:50:58 +0000366 lldb_private::Error write_error;
367
368 m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error);
369
370 return write_error.Success();
371 }
372
Sean Callanan1582ee62013-04-18 22:06:33 +0000373 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
374 {
375 lldb::addr_t ret = m_stack_pointer;
376
377 ret -= size;
378 ret -= (ret % byte_alignment);
379
380 if (ret < m_frame_process_address)
381 return LLDB_INVALID_ADDRESS;
382
383 m_stack_pointer = ret;
384 return ret;
385 }
386
Sean Callanan08052af2013-04-17 07:50:58 +0000387 lldb::addr_t MallocPointer ()
388 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000389 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000390 }
391
Sean Callanan1582ee62013-04-18 22:06:33 +0000392 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000393 {
394 lldb_private::Error alloc_error;
395
Sean Callanan1582ee62013-04-18 22:06:33 +0000396 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000397 }
398
399 lldb::addr_t PlaceLLDBValue (const llvm::Value *value, lldb_private::Value lldb_value)
400 {
Sean Callanan175187b2013-04-17 17:51:08 +0000401 if (!m_decl_map)
402 return false;
403
Sean Callanan08052af2013-04-17 07:50:58 +0000404 lldb_private::Error alloc_error;
405 lldb_private::RegisterInfo *reg_info = lldb_value.GetRegisterInfo();
406
407 lldb::addr_t ret;
408
409 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
Sean Callanan1582ee62013-04-18 22:06:33 +0000410 size_t value_align = m_target_data.getPrefTypeAlignment(value->getType());
Sean Callanan08052af2013-04-17 07:50:58 +0000411
412 if (reg_info && (reg_info->encoding == lldb::eEncodingVector))
413 value_size = reg_info->byte_size;
414
415 if (!reg_info && (lldb_value.GetValueType() == lldb_private::Value::eValueTypeLoadAddress))
416 return lldb_value.GetScalar().ULongLong();
417
Sean Callanan1582ee62013-04-18 22:06:33 +0000418 ret = Malloc(value_size, value_align);
Sean Callanan08052af2013-04-17 07:50:58 +0000419
420 if (ret == LLDB_INVALID_ADDRESS)
421 return LLDB_INVALID_ADDRESS;
422
423 lldb_private::DataBufferHeap buf(value_size, 0);
424
Sean Callanan175187b2013-04-17 17:51:08 +0000425 m_decl_map->ReadTarget(m_memory_map, buf.GetBytes(), lldb_value, value_size);
Sean Callanan08052af2013-04-17 07:50:58 +0000426
427 lldb_private::Error write_error;
428
429 m_memory_map.WriteMemory(ret, buf.GetBytes(), buf.GetByteSize(), write_error);
430
431 if (!write_error.Success())
432 {
433 lldb_private::Error free_error;
434 m_memory_map.Free(ret, free_error);
435 return LLDB_INVALID_ADDRESS;
436 }
437
438 m_placed_values.push_back(PlacedValue(lldb_value, ret, value_size));
439
440 return ret;
441 }
442
443 void RestoreLLDBValues ()
444 {
Sean Callanan175187b2013-04-17 17:51:08 +0000445 if (!m_decl_map)
446 return;
447
Sean Callanan08052af2013-04-17 07:50:58 +0000448 for (PlacedValue &placed_value : m_placed_values)
449 {
450 lldb_private::DataBufferHeap buf(placed_value.size, 0);
451
452 lldb_private::Error read_error;
453
454 m_memory_map.ReadMemory(buf.GetBytes(), placed_value.process_address, buf.GetByteSize(), read_error);
455
456 if (read_error.Success())
Sean Callanan175187b2013-04-17 17:51:08 +0000457 m_decl_map->WriteTarget(m_memory_map, placed_value.lldb_value, buf.GetBytes(), buf.GetByteSize());
Sean Callanan08052af2013-04-17 07:50:58 +0000458 }
459 }
460
461 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
462 {
463 size_t length = m_target_data.getTypeStoreSize(type);
464
465 lldb_private::DataBufferHeap buf(length, 0);
466
467 lldb_private::Error read_error;
468
469 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
470
471 if (!read_error.Success())
472 return std::string("<couldn't read data>");
473
474 lldb_private::StreamString ss;
475
476 for (size_t i = 0; i < length; i++)
477 {
478 if ((!(i & 0xf)) && i)
479 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
480 else
481 ss.Printf("%02hhx ", buf.GetBytes()[i]);
482 }
483
484 return ss.GetString();
485 }
486
487 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000488 {
489 ValueMap::iterator i = m_values.find(value);
490
491 if (i != m_values.end())
492 return i->second;
493
Sean Callanan1582ee62013-04-18 22:06:33 +0000494 // Fall back and allocate space [allocation type Alloca]
495
496 lldb::addr_t data_address = Malloc(value->getType());
497
498 if (const Constant *constant = dyn_cast<Constant>(value))
499 {
500 if (!ResolveConstant (data_address, constant))
501 {
502 lldb_private::Error free_error;
503 m_memory_map.Free(data_address, free_error);
504 return LLDB_INVALID_ADDRESS;
505 }
506 }
507
508 m_values[value] = data_address;
509 return data_address;
510
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000511 const GlobalValue *global_value = dyn_cast<GlobalValue>(value);
512
Sean Callanand2cb6262011-10-26 21:20:00 +0000513 // If the variable is indirected through the argument
514 // array then we need to build an extra level of indirection
515 // for it. This is the default; only magic arguments like
516 // "this", "self", and "_cmd" are direct.
Sean Callanan496970f2012-12-11 22:39:36 +0000517 bool variable_is_this = false;
Sean Callanand2cb6262011-10-26 21:20:00 +0000518
Sean Callanan9be9d172013-03-19 01:45:02 +0000519 // If the variable is a function pointer, we do not need to
520 // build an extra layer of indirection for it because it is
521 // accessed directly.
522 bool variable_is_function_address = false;
523
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000524 // Attempt to resolve the value using the program's data.
525 // If it is, the values to be created are:
526 //
527 // data_region - a region of memory in which the variable's data resides.
528 // ref_region - a region of memory in which its address (i.e., &var) resides.
529 // In the JIT case, this region would be a member of the struct passed in.
530 // pointer_region - a region of memory in which the address of the pointer
531 // resides. This is an IR-level variable.
532 do
533 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000534 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand2cb6262011-10-26 21:20:00 +0000535
536 lldb_private::Value resolved_value;
Greg Clayton23f59502012-07-17 03:23:13 +0000537 lldb_private::ClangExpressionVariable::FlagType flags = 0;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000538
Sean Callanand2cb6262011-10-26 21:20:00 +0000539 if (global_value)
540 {
541 clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module);
542
543 if (!decl)
544 break;
545
546 if (isa<clang::FunctionDecl>(decl))
Sean Callanan9be9d172013-03-19 01:45:02 +0000547 variable_is_function_address = true;
Sean Callanand2cb6262011-10-26 21:20:00 +0000548
Sean Callanan175187b2013-04-17 17:51:08 +0000549 resolved_value = m_decl_map->LookupDecl(decl, flags);
Sean Callanand2cb6262011-10-26 21:20:00 +0000550 }
551 else
552 {
553 // Special-case "this", "self", and "_cmd"
554
Sean Callanan7f27d602011-11-19 02:54:21 +0000555 std::string name_str = value->getName().str();
Sean Callanand2cb6262011-10-26 21:20:00 +0000556
557 if (name_str == "this" ||
558 name_str == "self" ||
559 name_str == "_cmd")
Sean Callananc8675502013-02-15 23:07:52 +0000560 {
Sean Callanan175187b2013-04-17 17:51:08 +0000561 resolved_value = m_decl_map->GetSpecialValue(lldb_private::ConstString(name_str.c_str()));
Sean Callananc8675502013-02-15 23:07:52 +0000562 variable_is_this = true;
563 }
Sean Callanand2cb6262011-10-26 21:20:00 +0000564 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000565
566 if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void)
567 {
568 if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
569 {
Sean Callanan496970f2012-12-11 22:39:36 +0000570 if (variable_is_this)
Sean Callanan08052af2013-04-17 07:50:58 +0000571 {
572 lldb_private::Error alloc_error;
573 lldb::addr_t ref_addr = Malloc(value->getType());
Sean Callanan496970f2012-12-11 22:39:36 +0000574
Sean Callanan08052af2013-04-17 07:50:58 +0000575 if (ref_addr == LLDB_INVALID_ADDRESS)
576 return LLDB_INVALID_ADDRESS;
Sean Callanan496970f2012-12-11 22:39:36 +0000577
Sean Callanan08052af2013-04-17 07:50:58 +0000578 lldb_private::Error write_error;
579 m_memory_map.WritePointerToMemory(ref_addr, resolved_value.GetScalar().ULongLong(), write_error);
Sean Callanan496970f2012-12-11 22:39:36 +0000580
Sean Callanan08052af2013-04-17 07:50:58 +0000581 if (!write_error.Success())
582 return LLDB_INVALID_ADDRESS;
Sean Callanan496970f2012-12-11 22:39:36 +0000583
584 if (log)
585 {
586 log->Printf("Made an allocation for \"this\" register variable %s", PrintValue(value).c_str());
Sean Callanan08052af2013-04-17 07:50:58 +0000587 log->Printf(" Data region : %llx", (unsigned long long)resolved_value.GetScalar().ULongLong());
588 log->Printf(" Ref region : %llx", (unsigned long long)ref_addr);
Sean Callanan496970f2012-12-11 22:39:36 +0000589 }
590
Sean Callanan08052af2013-04-17 07:50:58 +0000591 m_values[value] = ref_addr;
592 return ref_addr;
Sean Callanan496970f2012-12-11 22:39:36 +0000593 }
594 else if (flags & lldb_private::ClangExpressionVariable::EVBareRegister)
Sean Callanan08052af2013-04-17 07:50:58 +0000595 {
596 lldb::addr_t data_address = PlaceLLDBValue(value, resolved_value);
Sean Callanand2cb6262011-10-26 21:20:00 +0000597
Sean Callanan08052af2013-04-17 07:50:58 +0000598 if (!data_address)
599 return LLDB_INVALID_ADDRESS;
Sean Callanan496970f2012-12-11 22:39:36 +0000600
Sean Callanan08052af2013-04-17 07:50:58 +0000601 lldb::addr_t ref_address = MallocPointer();
Sean Callanan496970f2012-12-11 22:39:36 +0000602
Sean Callanan08052af2013-04-17 07:50:58 +0000603 if (ref_address == LLDB_INVALID_ADDRESS)
604 {
605 lldb_private::Error free_error;
606 m_memory_map.Free(data_address, free_error);
607 return LLDB_INVALID_ADDRESS;
608 }
Sean Callanan496970f2012-12-11 22:39:36 +0000609
Sean Callanan08052af2013-04-17 07:50:58 +0000610 lldb_private::Error write_error;
611
612 m_memory_map.WritePointerToMemory(ref_address, data_address, write_error);
Sean Callanan496970f2012-12-11 22:39:36 +0000613
Sean Callanan08052af2013-04-17 07:50:58 +0000614 if (!write_error.Success())
615 {
616 lldb_private::Error free_error;
617 m_memory_map.Free(data_address, free_error);
618 m_memory_map.Free(ref_address, free_error);
619 return LLDB_INVALID_ADDRESS;
620 }
Sean Callanan496970f2012-12-11 22:39:36 +0000621
622 if (log)
623 {
624 log->Printf("Made an allocation for bare register variable %s", PrintValue(value).c_str());
Sean Callanan08052af2013-04-17 07:50:58 +0000625 log->Printf(" Data contents : %s", PrintData(data_address, value->getType()).c_str());
626 log->Printf(" Data region : 0x%llx", (unsigned long long)data_address);
627 log->Printf(" Ref region : 0x%llx", (unsigned long long)ref_address);
Sean Callanan496970f2012-12-11 22:39:36 +0000628 }
629
Sean Callanan08052af2013-04-17 07:50:58 +0000630 m_values[value] = ref_address;
631 return ref_address;
Sean Callanan496970f2012-12-11 22:39:36 +0000632 }
633 else
Sean Callanan08052af2013-04-17 07:50:58 +0000634 {
635 lldb::addr_t data_address = PlaceLLDBValue(value, resolved_value);
Sean Callanan496970f2012-12-11 22:39:36 +0000636
Sean Callanan08052af2013-04-17 07:50:58 +0000637 if (data_address == LLDB_INVALID_ADDRESS)
638 return LLDB_INVALID_ADDRESS;
Sean Callanan496970f2012-12-11 22:39:36 +0000639
Sean Callanan08052af2013-04-17 07:50:58 +0000640 lldb::addr_t ref_address = MallocPointer();
Sean Callanan496970f2012-12-11 22:39:36 +0000641
Sean Callanan08052af2013-04-17 07:50:58 +0000642 if (ref_address == LLDB_INVALID_ADDRESS)
643 {
644 lldb_private::Error free_error;
645 m_memory_map.Free(data_address, free_error);
646 return LLDB_INVALID_ADDRESS;
647 }
Sean Callanan496970f2012-12-11 22:39:36 +0000648
Sean Callanan08052af2013-04-17 07:50:58 +0000649 lldb::addr_t pointer_address = MallocPointer();
Sean Callanan496970f2012-12-11 22:39:36 +0000650
Sean Callanan08052af2013-04-17 07:50:58 +0000651 if (pointer_address == LLDB_INVALID_ADDRESS)
652 {
653 lldb_private::Error free_error;
654 m_memory_map.Free(data_address, free_error);
655 m_memory_map.Free(ref_address, free_error);
656 return LLDB_INVALID_ADDRESS;
657 }
658
659 lldb_private::Error write_error;
Sean Callanan496970f2012-12-11 22:39:36 +0000660
Sean Callanan08052af2013-04-17 07:50:58 +0000661 m_memory_map.WritePointerToMemory(ref_address, data_address, write_error);
Sean Callanan496970f2012-12-11 22:39:36 +0000662
Sean Callanan08052af2013-04-17 07:50:58 +0000663 if (!write_error.Success())
664 {
665 lldb_private::Error free_error;
666 m_memory_map.Free(data_address, free_error);
667 m_memory_map.Free(ref_address, free_error);
668 m_memory_map.Free(pointer_address, free_error);
669 return LLDB_INVALID_ADDRESS;
670 }
671
672 write_error.Clear();
673
674 m_memory_map.WritePointerToMemory(pointer_address, ref_address, write_error);
675
676 if (!write_error.Success())
677 {
678 lldb_private::Error free_error;
679 m_memory_map.Free(data_address, free_error);
680 m_memory_map.Free(ref_address, free_error);
681 m_memory_map.Free(pointer_address, free_error);
682 return LLDB_INVALID_ADDRESS;
683 }
Sean Callanan496970f2012-12-11 22:39:36 +0000684
685 if (log)
686 {
687 log->Printf("Made an allocation for ordinary register variable %s", PrintValue(value).c_str());
Sean Callanan08052af2013-04-17 07:50:58 +0000688 log->Printf(" Data contents : %s", PrintData(data_address, value->getType()).c_str());
689 log->Printf(" Data region : 0x%llx", (unsigned long long)data_address);
690 log->Printf(" Ref region : 0x%llx", (unsigned long long)ref_address);
691 log->Printf(" Pointer region : 0x%llx", (unsigned long long)pointer_address);
Sean Callanan496970f2012-12-11 22:39:36 +0000692 }
693
Sean Callanan08052af2013-04-17 07:50:58 +0000694 m_values[value] = pointer_address;
695 return pointer_address;
Sean Callanand2cb6262011-10-26 21:20:00 +0000696 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000697 }
698 else
699 {
Sean Callanan9be9d172013-03-19 01:45:02 +0000700 bool no_extra_redirect = (variable_is_this || variable_is_function_address);
701
Sean Callanan08052af2013-04-17 07:50:58 +0000702 lldb::addr_t data_address = PlaceLLDBValue(value, resolved_value);
Sean Callanand2cb6262011-10-26 21:20:00 +0000703
Sean Callanan08052af2013-04-17 07:50:58 +0000704 if (data_address == LLDB_INVALID_ADDRESS)
705 return LLDB_INVALID_ADDRESS;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000706
Sean Callanan08052af2013-04-17 07:50:58 +0000707 lldb::addr_t ref_address = MallocPointer();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000708
Sean Callanan08052af2013-04-17 07:50:58 +0000709 if (ref_address == LLDB_INVALID_ADDRESS)
710 {
711 lldb_private::Error free_error;
712 m_memory_map.Free(data_address, free_error);
713 return LLDB_INVALID_ADDRESS;
714 }
715
716 lldb::addr_t pointer_address = LLDB_INVALID_ADDRESS;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000717
Sean Callanan9be9d172013-03-19 01:45:02 +0000718 if (!no_extra_redirect)
Sean Callanand2cb6262011-10-26 21:20:00 +0000719 {
Sean Callanan08052af2013-04-17 07:50:58 +0000720 pointer_address = MallocPointer();
Sean Callanand2cb6262011-10-26 21:20:00 +0000721
Sean Callanan08052af2013-04-17 07:50:58 +0000722 if (pointer_address == LLDB_INVALID_ADDRESS)
723 {
724 lldb_private::Error free_error;
725 m_memory_map.Free(data_address, free_error);
726 m_memory_map.Free(ref_address, free_error);
727 return LLDB_INVALID_ADDRESS;
728 }
729 }
730
731 lldb_private::Error write_error;
732
733 m_memory_map.WritePointerToMemory(ref_address, data_address, write_error);
734
735 if (!write_error.Success())
736 {
737 lldb_private::Error free_error;
738 m_memory_map.Free(data_address, free_error);
739 m_memory_map.Free(ref_address, free_error);
740 if (pointer_address != LLDB_INVALID_ADDRESS)
741 m_memory_map.Free(pointer_address, free_error);
742 return LLDB_INVALID_ADDRESS;
743 }
744
745 if (!no_extra_redirect)
746 {
747 write_error.Clear();
748
749 m_memory_map.WritePointerToMemory(pointer_address, ref_address, write_error);
750
751 if (!write_error.Success())
752 {
753 lldb_private::Error free_error;
754 m_memory_map.Free(data_address, free_error);
755 m_memory_map.Free(ref_address, free_error);
756 if (pointer_address != LLDB_INVALID_ADDRESS)
757 m_memory_map.Free(pointer_address, free_error);
758 return LLDB_INVALID_ADDRESS;
759 }
Sean Callanand2cb6262011-10-26 21:20:00 +0000760 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000761
762 if (log)
763 {
Sean Callanand2cb6262011-10-26 21:20:00 +0000764 log->Printf("Made an allocation for %s", PrintValue(value).c_str());
Sean Callanan08052af2013-04-17 07:50:58 +0000765 log->Printf(" Data contents : %s", PrintData(data_address, value->getType()).c_str());
766 log->Printf(" Data region : %llx", (unsigned long long)data_address);
767 log->Printf(" Ref region : %llx", (unsigned long long)ref_address);
Sean Callanan496970f2012-12-11 22:39:36 +0000768 if (!variable_is_this)
Sean Callanan08052af2013-04-17 07:50:58 +0000769 log->Printf(" Pointer region : %llx", (unsigned long long)pointer_address);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000770 }
771
Sean Callanan9be9d172013-03-19 01:45:02 +0000772 if (no_extra_redirect)
Sean Callanan08052af2013-04-17 07:50:58 +0000773 {
774 m_values[value] = ref_address;
775 return ref_address;
776 }
Sean Callanan496970f2012-12-11 22:39:36 +0000777 else
Sean Callanan08052af2013-04-17 07:50:58 +0000778 {
779 m_values[value] = pointer_address;
780 return pointer_address;
781 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000782 }
783 }
784 }
785 while(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000786 }
787
788 bool ConstructResult (lldb::ClangExpressionVariableSP &result,
789 const GlobalValue *result_value,
790 const lldb_private::ConstString &result_name,
791 lldb_private::TypeFromParser result_type,
792 Module &module)
793 {
Sean Callanan175187b2013-04-17 17:51:08 +0000794 if (!m_decl_map)
795 return false;
796
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000797 // The result_value resolves to P, a pointer to a region R containing the result data.
798 // If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
799
800 if (!result_value)
801 return true; // There was no slot for a result – the expression doesn't return one.
802
803 ValueMap::iterator i = m_values.find(result_value);
804
805 if (i == m_values.end())
806 return false; // There was a slot for the result, but we didn't write into it.
807
Sean Callanan08052af2013-04-17 07:50:58 +0000808 lldb::addr_t P = i->second;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000809
810 Type *pointer_ty = result_value->getType();
811 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
812 if (!pointer_ptr_ty)
813 return false;
814 Type *R_ty = pointer_ptr_ty->getElementType();
815
Sean Callanan08052af2013-04-17 07:50:58 +0000816 lldb_private::Error read_error;
817 lldb::addr_t R;
818 m_memory_map.ReadPointerFromMemory(&R, P, read_error);
819 if (!read_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000820 return false;
821
822 lldb_private::Value base;
823
Sean Callanan0886e562011-09-22 00:41:11 +0000824 bool transient = false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000825 bool maybe_make_load = false;
Sean Callanan0886e562011-09-22 00:41:11 +0000826
Sean Callanan175187b2013-04-17 17:51:08 +0000827 if (m_decl_map->ResultIsReference(result_name))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000828 {
829 PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
830 if (!R_ptr_ty)
831 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000832
Sean Callanan08052af2013-04-17 07:50:58 +0000833 read_error.Clear();
834 lldb::addr_t R_pointer;
835 m_memory_map.ReadPointerFromMemory(&R_pointer, R, read_error);
836 if (!read_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000837 return false;
838
Sean Callanan08052af2013-04-17 07:50:58 +0000839 // We got a bare pointer. We are going to treat it as a load address
840 // or a file address, letting decl_map make the choice based on whether
841 // or not a process exists.
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000842
Sean Callanan08052af2013-04-17 07:50:58 +0000843 bool was_placed = false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000844
Sean Callanan08052af2013-04-17 07:50:58 +0000845 for (PlacedValue &value : m_placed_values)
Sean Callanan80c48c12011-10-21 05:18:02 +0000846 {
Sean Callanan08052af2013-04-17 07:50:58 +0000847 if (value.process_address == R_pointer)
848 {
849 base = value.lldb_value;
850 was_placed = true;
851 break;
852 }
853 }
854
855 if (!was_placed)
856 {
Sean Callanan80c48c12011-10-21 05:18:02 +0000857 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
858 base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
859 base.GetScalar() = (unsigned long long)R_pointer;
860 maybe_make_load = true;
861 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000862 }
863 else
864 {
865 base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
Sean Callanan08052af2013-04-17 07:50:58 +0000866 base.SetValueType(lldb_private::Value::eValueTypeLoadAddress);
867 base.GetScalar() = (unsigned long long)R;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000868 }
869
Sean Callanan175187b2013-04-17 17:51:08 +0000870 return m_decl_map->CompleteResultVariable (result, m_memory_map, base, result_name, result_type, transient, maybe_make_load);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000871 }
872};
873
874bool
Sean Callanan182bd6c2013-04-17 18:07:40 +0000875IRInterpreter::maybeRunOnFunction (lldb_private::ClangExpressionDeclMap *decl_map,
876 lldb_private::IRMemoryMap &memory_map,
877 lldb_private::Stream *error_stream,
878 lldb::ClangExpressionVariableSP &result,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000879 const lldb_private::ConstString &result_name,
880 lldb_private::TypeFromParser result_type,
881 Function &llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +0000882 Module &llvm_module,
883 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000884{
Sean Callanan175a0d02012-01-24 22:06:48 +0000885 if (supportsFunction (llvm_function, err))
Sean Callanan182bd6c2013-04-17 18:07:40 +0000886 return runOnFunction(decl_map,
887 memory_map,
888 error_stream,
889 result,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000890 result_name,
891 result_type,
892 llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +0000893 llvm_module,
894 err);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000895 else
896 return false;
897}
898
Sean Callanan175a0d02012-01-24 22:06:48 +0000899static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Greg Claytone01e07b2013-04-18 18:10:51 +0000900//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000901static const char *interpreter_internal_error = "Interpreter encountered an internal error";
902static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
903static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
904static const char *memory_write_error = "Interpreter couldn't write to memory";
905static const char *memory_read_error = "Interpreter couldn't read from memory";
906static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan5b26f272012-02-04 08:49:35 +0000907static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000908
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000909bool
Sean Callanan175a0d02012-01-24 22:06:48 +0000910IRInterpreter::supportsFunction (Function &llvm_function,
911 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000912{
Greg Clayton5160ce52013-03-27 23:08:40 +0000913 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000914
915 for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
916 bbi != bbe;
917 ++bbi)
918 {
919 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
920 ii != ie;
921 ++ii)
922 {
923 switch (ii->getOpcode())
924 {
925 default:
926 {
927 if (log)
928 log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +0000929 err.SetErrorToGenericError();
930 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000931 return false;
932 }
933 case Instruction::Add:
934 case Instruction::Alloca:
935 case Instruction::BitCast:
936 case Instruction::Br:
937 case Instruction::GetElementPtr:
938 break;
939 case Instruction::ICmp:
940 {
941 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
942
943 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000944 {
945 err.SetErrorToGenericError();
946 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000947 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000948 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000949
950 switch (icmp_inst->getPredicate())
951 {
952 default:
953 {
954 if (log)
955 log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +0000956
957 err.SetErrorToGenericError();
958 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000959 return false;
960 }
961 case CmpInst::ICMP_EQ:
962 case CmpInst::ICMP_NE:
963 case CmpInst::ICMP_UGT:
964 case CmpInst::ICMP_UGE:
965 case CmpInst::ICMP_ULT:
966 case CmpInst::ICMP_ULE:
967 case CmpInst::ICMP_SGT:
968 case CmpInst::ICMP_SGE:
969 case CmpInst::ICMP_SLT:
970 case CmpInst::ICMP_SLE:
971 break;
972 }
973 }
974 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000975 case Instruction::And:
976 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000977 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000978 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000979 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000980 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000981 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000982 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000983 case Instruction::Ret:
984 case Instruction::SDiv:
Sean Callanan087f4372013-01-09 22:44:41 +0000985 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000986 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000987 case Instruction::Store:
988 case Instruction::Sub:
989 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000990 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000991 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000992 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000993 break;
994 }
995 }
996 }
997
998 return true;
999}
1000
1001bool
Sean Callanan182bd6c2013-04-17 18:07:40 +00001002IRInterpreter::runOnFunction (lldb_private::ClangExpressionDeclMap *decl_map,
1003 lldb_private::IRMemoryMap &memory_map,
1004 lldb_private::Stream *error_stream,
1005 lldb::ClangExpressionVariableSP &result,
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001006 const lldb_private::ConstString &result_name,
1007 lldb_private::TypeFromParser result_type,
1008 Function &llvm_function,
Sean Callanan175a0d02012-01-24 22:06:48 +00001009 Module &llvm_module,
1010 lldb_private::Error &err)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001011{
Greg Clayton5160ce52013-03-27 23:08:40 +00001012 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001013
Micah Villmow8468dbe2012-10-08 16:28:57 +00001014 DataLayout target_data(&llvm_module);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001015
Sean Callanan182bd6c2013-04-17 18:07:40 +00001016 InterpreterStackFrame frame(target_data, decl_map, memory_map);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001017
1018 uint32_t num_insts = 0;
1019
1020 frame.Jump(llvm_function.begin());
1021
1022 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1023 {
1024 const Instruction *inst = frame.m_ii;
1025
1026 if (log)
1027 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1028
1029 switch (inst->getOpcode())
1030 {
1031 default:
1032 break;
1033 case Instruction::Add:
1034 case Instruction::Sub:
1035 case Instruction::Mul:
1036 case Instruction::SDiv:
1037 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +00001038 case Instruction::SRem:
1039 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +00001040 case Instruction::Shl:
1041 case Instruction::LShr:
1042 case Instruction::AShr:
1043 case Instruction::And:
1044 case Instruction::Or:
1045 case Instruction::Xor:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001046 {
1047 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1048
1049 if (!bin_op)
1050 {
1051 if (log)
1052 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
Sean Callanan175a0d02012-01-24 22:06:48 +00001053 err.SetErrorToGenericError();
1054 err.SetErrorString(interpreter_internal_error);
1055 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001056 }
1057
1058 Value *lhs = inst->getOperand(0);
1059 Value *rhs = inst->getOperand(1);
1060
1061 lldb_private::Scalar L;
1062 lldb_private::Scalar R;
1063
1064 if (!frame.EvaluateValue(L, lhs, llvm_module))
1065 {
1066 if (log)
1067 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001068 err.SetErrorToGenericError();
1069 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001070 return false;
1071 }
1072
1073 if (!frame.EvaluateValue(R, rhs, llvm_module))
1074 {
1075 if (log)
1076 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001077 err.SetErrorToGenericError();
1078 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001079 return false;
1080 }
1081
1082 lldb_private::Scalar result;
1083
1084 switch (inst->getOpcode())
1085 {
1086 default:
1087 break;
1088 case Instruction::Add:
1089 result = L + R;
1090 break;
1091 case Instruction::Mul:
1092 result = L * R;
1093 break;
1094 case Instruction::Sub:
1095 result = L - R;
1096 break;
1097 case Instruction::SDiv:
1098 result = L / R;
1099 break;
1100 case Instruction::UDiv:
1101 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1102 break;
Sean Callananf466a6e2012-12-21 22:27:55 +00001103 case Instruction::SRem:
1104 result = L % R;
1105 break;
1106 case Instruction::URem:
1107 result = L.GetRawBits64(0) % R.GetRawBits64(1);
1108 break;
Sean Callanan087f4372013-01-09 22:44:41 +00001109 case Instruction::Shl:
1110 result = L << R;
1111 break;
1112 case Instruction::AShr:
1113 result = L >> R;
1114 break;
1115 case Instruction::LShr:
1116 result = L;
1117 result.ShiftRightLogical(R);
1118 break;
1119 case Instruction::And:
1120 result = L & R;
1121 break;
1122 case Instruction::Or:
1123 result = L | R;
1124 break;
1125 case Instruction::Xor:
1126 result = L ^ R;
1127 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001128 }
1129
1130 frame.AssignValue(inst, result, llvm_module);
1131
1132 if (log)
1133 {
1134 log->Printf("Interpreted a %s", inst->getOpcodeName());
1135 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1136 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1137 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1138 }
1139 }
1140 break;
1141 case Instruction::Alloca:
1142 {
1143 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1144
1145 if (!alloca_inst)
1146 {
1147 if (log)
1148 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001149 err.SetErrorToGenericError();
1150 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001151 return false;
1152 }
1153
1154 if (alloca_inst->isArrayAllocation())
1155 {
1156 if (log)
1157 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
Sean Callanan175a0d02012-01-24 22:06:48 +00001158 err.SetErrorToGenericError();
1159 err.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001160 return false;
1161 }
1162
1163 // The semantics of Alloca are:
1164 // Create a region R of virtual memory of type T, backed by a data buffer
1165 // Create a region P of virtual memory of type T*, backed by a data buffer
1166 // Write the virtual address of R into P
1167
1168 Type *T = alloca_inst->getAllocatedType();
1169 Type *Tptr = alloca_inst->getType();
1170
Sean Callanan08052af2013-04-17 07:50:58 +00001171 lldb::addr_t R = frame.Malloc(T);
1172
1173 if (R == LLDB_INVALID_ADDRESS)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001174 {
1175 if (log)
1176 log->Printf("Couldn't allocate memory for an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001177 err.SetErrorToGenericError();
1178 err.SetErrorString(memory_allocation_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001179 return false;
1180 }
1181
Sean Callanan08052af2013-04-17 07:50:58 +00001182 lldb::addr_t P = frame.Malloc(Tptr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001183
Sean Callanan08052af2013-04-17 07:50:58 +00001184 if (P == LLDB_INVALID_ADDRESS)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001185 {
1186 if (log)
1187 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001188 err.SetErrorToGenericError();
1189 err.SetErrorString(memory_allocation_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001190 return false;
1191 }
1192
Sean Callanan08052af2013-04-17 07:50:58 +00001193 lldb_private::Error write_error;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001194
Sean Callanan182bd6c2013-04-17 18:07:40 +00001195 memory_map.WritePointerToMemory(P, R, write_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001196
1197 if (!write_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001198 {
1199 if (log)
Sean Callanan175a0d02012-01-24 22:06:48 +00001200 log->Printf("Couldn't write the result pointer for an AllocaInst");
1201 err.SetErrorToGenericError();
1202 err.SetErrorString(memory_write_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001203 lldb_private::Error free_error;
Sean Callanan182bd6c2013-04-17 18:07:40 +00001204 memory_map.Free(P, free_error);
1205 memory_map.Free(R, free_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001206 return false;
1207 }
1208
1209 frame.m_values[alloca_inst] = P;
1210
1211 if (log)
1212 {
1213 log->Printf("Interpreted an AllocaInst");
Sean Callanan08052af2013-04-17 07:50:58 +00001214 log->Printf(" R : 0x%llx", R);
1215 log->Printf(" P : 0x%llx", P);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001216 }
1217 }
1218 break;
1219 case Instruction::BitCast:
Sean Callanan1ef77432012-04-23 17:25:38 +00001220 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001221 {
Sean Callanan1ef77432012-04-23 17:25:38 +00001222 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001223
Sean Callanan1ef77432012-04-23 17:25:38 +00001224 if (!cast_inst)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001225 {
1226 if (log)
Sean Callanan1ef77432012-04-23 17:25:38 +00001227 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
Sean Callanan175a0d02012-01-24 22:06:48 +00001228 err.SetErrorToGenericError();
1229 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001230 return false;
1231 }
1232
Sean Callanan1ef77432012-04-23 17:25:38 +00001233 Value *source = cast_inst->getOperand(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001234
1235 lldb_private::Scalar S;
1236
1237 if (!frame.EvaluateValue(S, source, llvm_module))
1238 {
1239 if (log)
1240 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001241 err.SetErrorToGenericError();
1242 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001243 return false;
1244 }
1245
1246 frame.AssignValue(inst, S, llvm_module);
1247 }
1248 break;
1249 case Instruction::Br:
1250 {
1251 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
1252
1253 if (!br_inst)
1254 {
1255 if (log)
1256 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001257 err.SetErrorToGenericError();
1258 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001259 return false;
1260 }
1261
1262 if (br_inst->isConditional())
1263 {
1264 Value *condition = br_inst->getCondition();
1265
1266 lldb_private::Scalar C;
1267
1268 if (!frame.EvaluateValue(C, condition, llvm_module))
1269 {
1270 if (log)
1271 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001272 err.SetErrorToGenericError();
1273 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001274 return false;
1275 }
1276
1277 if (C.GetRawBits64(0))
1278 frame.Jump(br_inst->getSuccessor(0));
1279 else
1280 frame.Jump(br_inst->getSuccessor(1));
1281
1282 if (log)
1283 {
1284 log->Printf("Interpreted a BrInst with a condition");
1285 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1286 }
1287 }
1288 else
1289 {
1290 frame.Jump(br_inst->getSuccessor(0));
1291
1292 if (log)
1293 {
1294 log->Printf("Interpreted a BrInst with no condition");
1295 }
1296 }
1297 }
1298 continue;
1299 case Instruction::GetElementPtr:
1300 {
1301 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
1302
1303 if (!gep_inst)
1304 {
1305 if (log)
1306 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001307 err.SetErrorToGenericError();
1308 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001309 return false;
1310 }
1311
1312 const Value *pointer_operand = gep_inst->getPointerOperand();
1313 Type *pointer_type = pointer_operand->getType();
1314
1315 lldb_private::Scalar P;
1316
1317 if (!frame.EvaluateValue(P, pointer_operand, llvm_module))
Sean Callanan175a0d02012-01-24 22:06:48 +00001318 {
1319 if (log)
1320 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1321 err.SetErrorToGenericError();
1322 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001323 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001324 }
1325
Sean Callanan3f548132012-02-29 17:57:18 +00001326 typedef SmallVector <Value *, 8> IndexVector;
1327 typedef IndexVector::iterator IndexIterator;
1328
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001329 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1330 gep_inst->idx_end());
1331
Sean Callanan3f548132012-02-29 17:57:18 +00001332 SmallVector <Value *, 8> const_indices;
1333
1334 for (IndexIterator ii = indices.begin(), ie = indices.end();
1335 ii != ie;
1336 ++ii)
1337 {
1338 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1339
1340 if (!constant_index)
1341 {
1342 lldb_private::Scalar I;
1343
1344 if (!frame.EvaluateValue(I, *ii, llvm_module))
1345 {
1346 if (log)
1347 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1348 err.SetErrorToGenericError();
1349 err.SetErrorString(bad_value_error);
1350 return false;
1351 }
1352
1353 if (log)
1354 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1355
1356 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1357 }
1358
1359 const_indices.push_back(constant_index);
1360 }
1361
1362 uint64_t offset = target_data.getIndexedOffset(pointer_type, const_indices);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001363
1364 lldb_private::Scalar Poffset = P + offset;
1365
1366 frame.AssignValue(inst, Poffset, llvm_module);
1367
1368 if (log)
1369 {
1370 log->Printf("Interpreted a GetElementPtrInst");
1371 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1372 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1373 }
1374 }
1375 break;
1376 case Instruction::ICmp:
1377 {
1378 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
1379
1380 if (!icmp_inst)
1381 {
1382 if (log)
1383 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001384 err.SetErrorToGenericError();
1385 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001386 return false;
1387 }
1388
1389 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1390
1391 Value *lhs = inst->getOperand(0);
1392 Value *rhs = inst->getOperand(1);
1393
1394 lldb_private::Scalar L;
1395 lldb_private::Scalar R;
1396
1397 if (!frame.EvaluateValue(L, lhs, llvm_module))
1398 {
1399 if (log)
1400 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001401 err.SetErrorToGenericError();
1402 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001403 return false;
1404 }
1405
1406 if (!frame.EvaluateValue(R, rhs, llvm_module))
1407 {
1408 if (log)
1409 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
Sean Callanan175a0d02012-01-24 22:06:48 +00001410 err.SetErrorToGenericError();
1411 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001412 return false;
1413 }
1414
1415 lldb_private::Scalar result;
1416
1417 switch (predicate)
1418 {
1419 default:
1420 return false;
1421 case CmpInst::ICMP_EQ:
1422 result = (L == R);
1423 break;
1424 case CmpInst::ICMP_NE:
1425 result = (L != R);
1426 break;
1427 case CmpInst::ICMP_UGT:
1428 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1429 break;
1430 case CmpInst::ICMP_UGE:
1431 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1432 break;
1433 case CmpInst::ICMP_ULT:
1434 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1435 break;
1436 case CmpInst::ICMP_ULE:
1437 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1438 break;
1439 case CmpInst::ICMP_SGT:
1440 result = (L > R);
1441 break;
1442 case CmpInst::ICMP_SGE:
1443 result = (L >= R);
1444 break;
1445 case CmpInst::ICMP_SLT:
1446 result = (L < R);
1447 break;
1448 case CmpInst::ICMP_SLE:
1449 result = (L <= R);
1450 break;
1451 }
1452
1453 frame.AssignValue(inst, result, llvm_module);
1454
1455 if (log)
1456 {
1457 log->Printf("Interpreted an ICmpInst");
1458 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1459 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1460 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1461 }
1462 }
1463 break;
Sean Callanan80c48c12011-10-21 05:18:02 +00001464 case Instruction::IntToPtr:
1465 {
1466 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
1467
1468 if (!int_to_ptr_inst)
1469 {
1470 if (log)
1471 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001472 err.SetErrorToGenericError();
1473 err.SetErrorString(interpreter_internal_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001474 return false;
1475 }
1476
1477 Value *src_operand = int_to_ptr_inst->getOperand(0);
1478
1479 lldb_private::Scalar I;
1480
1481 if (!frame.EvaluateValue(I, src_operand, llvm_module))
Sean Callanan175a0d02012-01-24 22:06:48 +00001482 {
1483 if (log)
1484 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1485 err.SetErrorToGenericError();
1486 err.SetErrorString(bad_value_error);
Sean Callanan80c48c12011-10-21 05:18:02 +00001487 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001488 }
Sean Callanan80c48c12011-10-21 05:18:02 +00001489
1490 frame.AssignValue(inst, I, llvm_module);
1491
1492 if (log)
1493 {
1494 log->Printf("Interpreted an IntToPtr");
1495 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1496 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1497 }
1498 }
1499 break;
Sean Callanan2abffe02012-12-01 00:09:34 +00001500 case Instruction::PtrToInt:
1501 {
1502 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
1503
1504 if (!ptr_to_int_inst)
1505 {
1506 if (log)
1507 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1508 err.SetErrorToGenericError();
1509 err.SetErrorString(interpreter_internal_error);
1510 return false;
1511 }
1512
1513 Value *src_operand = ptr_to_int_inst->getOperand(0);
1514
1515 lldb_private::Scalar I;
1516
1517 if (!frame.EvaluateValue(I, src_operand, llvm_module))
1518 {
1519 if (log)
1520 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1521 err.SetErrorToGenericError();
1522 err.SetErrorString(bad_value_error);
1523 return false;
1524 }
1525
1526 frame.AssignValue(inst, I, llvm_module);
1527
1528 if (log)
1529 {
1530 log->Printf("Interpreted a PtrToInt");
1531 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1532 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1533 }
1534 }
1535 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001536 case Instruction::Load:
1537 {
1538 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
1539
1540 if (!load_inst)
1541 {
1542 if (log)
1543 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001544 err.SetErrorToGenericError();
1545 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001546 return false;
1547 }
1548
1549 // The semantics of Load are:
1550 // Create a region D that will contain the loaded data
1551 // Resolve the region P containing a pointer
1552 // Dereference P to get the region R that the data should be loaded from
1553 // Transfer a unit of type type(D) from R to D
1554
1555 const Value *pointer_operand = load_inst->getPointerOperand();
1556
1557 Type *pointer_ty = pointer_operand->getType();
1558 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1559 if (!pointer_ptr_ty)
Sean Callanan175a0d02012-01-24 22:06:48 +00001560 {
1561 if (log)
1562 log->Printf("getPointerOperand()->getType() is not a PointerType");
1563 err.SetErrorToGenericError();
1564 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001565 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001566 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001567 Type *target_ty = pointer_ptr_ty->getElementType();
1568
Sean Callanan08052af2013-04-17 07:50:58 +00001569 lldb::addr_t D = frame.ResolveValue(load_inst, llvm_module);
1570 lldb::addr_t P = frame.ResolveValue(pointer_operand, llvm_module);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001571
Sean Callanan08052af2013-04-17 07:50:58 +00001572 if (D == LLDB_INVALID_ADDRESS)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001573 {
1574 if (log)
1575 log->Printf("LoadInst's value doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001576 err.SetErrorToGenericError();
1577 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001578 return false;
1579 }
1580
Sean Callanan08052af2013-04-17 07:50:58 +00001581 if (P == LLDB_INVALID_ADDRESS)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001582 {
1583 if (log)
1584 log->Printf("LoadInst's pointer doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001585 err.SetErrorToGenericError();
1586 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001587 return false;
1588 }
1589
Sean Callanan08052af2013-04-17 07:50:58 +00001590 lldb::addr_t R;
1591 lldb_private::Error read_error;
Sean Callanan182bd6c2013-04-17 18:07:40 +00001592 memory_map.ReadPointerFromMemory(&R, P, read_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001593
Sean Callanan08052af2013-04-17 07:50:58 +00001594 if (!read_error.Success())
Sean Callanan80c48c12011-10-21 05:18:02 +00001595 {
Sean Callanan08052af2013-04-17 07:50:58 +00001596 if (log)
1597 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1598 err.SetErrorToGenericError();
1599 err.SetErrorString(memory_read_error);
1600 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +00001601 }
Sean Callanan08052af2013-04-17 07:50:58 +00001602
1603 size_t target_size = target_data.getTypeStoreSize(target_ty);
1604 lldb_private::DataBufferHeap buffer(target_size, 0);
1605
1606 read_error.Clear();
Sean Callanan182bd6c2013-04-17 18:07:40 +00001607 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001608 if (!read_error.Success())
Sean Callanan80c48c12011-10-21 05:18:02 +00001609 {
Sean Callanan08052af2013-04-17 07:50:58 +00001610 if (log)
1611 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1612 err.SetErrorToGenericError();
1613 err.SetErrorString(memory_read_error);
1614 return false;
1615 }
1616
1617 lldb_private::Error write_error;
Sean Callanan182bd6c2013-04-17 18:07:40 +00001618 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001619 if (!write_error.Success())
1620 {
1621 if (log)
1622 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1623 err.SetErrorToGenericError();
1624 err.SetErrorString(memory_read_error);
1625 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +00001626 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001627
1628 if (log)
1629 {
1630 log->Printf("Interpreted a LoadInst");
Sean Callanan08052af2013-04-17 07:50:58 +00001631 log->Printf(" P : 0x%llx", P);
1632 log->Printf(" R : 0x%llx", R);
1633 log->Printf(" D : 0x%llx", D);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001634 }
1635 }
1636 break;
1637 case Instruction::Ret:
1638 {
Sean Callanan08052af2013-04-17 07:50:58 +00001639 frame.RestoreLLDBValues();
1640
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001641 if (result_name.IsEmpty())
1642 return true;
1643
1644 GlobalValue *result_value = llvm_module.getNamedValue(result_name.GetCString());
Sean Callanan5b26f272012-02-04 08:49:35 +00001645
1646 if (!frame.ConstructResult(result, result_value, result_name, result_type, llvm_module))
1647 {
1648 if (log)
1649 log->Printf("Couldn't construct the expression's result");
1650 err.SetErrorToGenericError();
1651 err.SetErrorString(bad_result_error);
1652 return false;
1653 }
1654
1655 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001656 }
1657 case Instruction::Store:
1658 {
1659 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
1660
1661 if (!store_inst)
1662 {
1663 if (log)
1664 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
Sean Callanan175a0d02012-01-24 22:06:48 +00001665 err.SetErrorToGenericError();
1666 err.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001667 return false;
1668 }
1669
1670 // The semantics of Store are:
1671 // Resolve the region D containing the data to be stored
1672 // Resolve the region P containing a pointer
1673 // Dereference P to get the region R that the data should be stored in
1674 // Transfer a unit of type type(D) from D to R
1675
1676 const Value *value_operand = store_inst->getValueOperand();
1677 const Value *pointer_operand = store_inst->getPointerOperand();
1678
1679 Type *pointer_ty = pointer_operand->getType();
1680 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1681 if (!pointer_ptr_ty)
1682 return false;
1683 Type *target_ty = pointer_ptr_ty->getElementType();
1684
Sean Callanan08052af2013-04-17 07:50:58 +00001685 lldb::addr_t D = frame.ResolveValue(value_operand, llvm_module);
1686 lldb::addr_t P = frame.ResolveValue(pointer_operand, llvm_module);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001687
Sean Callanan08052af2013-04-17 07:50:58 +00001688 if (D == LLDB_INVALID_ADDRESS)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001689 {
1690 if (log)
1691 log->Printf("StoreInst's value doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001692 err.SetErrorToGenericError();
1693 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001694 return false;
1695 }
1696
Sean Callanan08052af2013-04-17 07:50:58 +00001697 if (P == LLDB_INVALID_ADDRESS)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001698 {
1699 if (log)
1700 log->Printf("StoreInst's pointer doesn't resolve to anything");
Sean Callanan175a0d02012-01-24 22:06:48 +00001701 err.SetErrorToGenericError();
1702 err.SetErrorString(bad_value_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001703 return false;
1704 }
1705
Sean Callanan08052af2013-04-17 07:50:58 +00001706 lldb::addr_t R;
1707 lldb_private::Error read_error;
Sean Callanan182bd6c2013-04-17 18:07:40 +00001708 memory_map.ReadPointerFromMemory(&R, P, read_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001709
1710 if (!read_error.Success())
1711 {
1712 if (log)
1713 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1714 err.SetErrorToGenericError();
1715 err.SetErrorString(memory_read_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001716 return false;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001717 }
1718
Sean Callanan08052af2013-04-17 07:50:58 +00001719 size_t target_size = target_data.getTypeStoreSize(target_ty);
1720 lldb_private::DataBufferHeap buffer(target_size, 0);
1721
1722 read_error.Clear();
Sean Callanan182bd6c2013-04-17 18:07:40 +00001723 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001724 if (!read_error.Success())
1725 {
1726 if (log)
1727 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1728 err.SetErrorToGenericError();
1729 err.SetErrorString(memory_read_error);
1730 return false;
1731 }
1732
1733 lldb_private::Error write_error;
Sean Callanan182bd6c2013-04-17 18:07:40 +00001734 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
Sean Callanan08052af2013-04-17 07:50:58 +00001735 if (!write_error.Success())
1736 {
1737 if (log)
1738 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1739 err.SetErrorToGenericError();
1740 err.SetErrorString(memory_read_error);
1741 return false;
1742 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001743
1744 if (log)
1745 {
1746 log->Printf("Interpreted a StoreInst");
Sean Callanan08052af2013-04-17 07:50:58 +00001747 log->Printf(" D : 0x%llx", D);
1748 log->Printf(" P : 0x%llx", P);
1749 log->Printf(" R : 0x%llx", R);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001750 }
1751 }
1752 break;
1753 }
1754
1755 ++frame.m_ii;
1756 }
1757
1758 if (num_insts >= 4096)
Sean Callanan175a0d02012-01-24 22:06:48 +00001759 {
1760 err.SetErrorToGenericError();
1761 err.SetErrorString(infinite_loop_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001762 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +00001763 }
1764
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001765 return false;
Greg Claytond4e25522011-10-12 00:53:29 +00001766}
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001767
1768// new api
1769
1770bool
1771IRInterpreter::CanInterpret (llvm::Module &module,
1772 llvm::Function &function,
1773 lldb_private::Error &error)
1774{
1775 return supportsFunction(function, error);
1776}
1777
1778bool
1779IRInterpreter::Interpret (llvm::Module &module,
1780 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +00001781 llvm::ArrayRef<lldb::addr_t> args,
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001782 lldb_private::IRMemoryMap &memory_map,
1783 lldb_private::Error &error)
1784{
1785 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1786
Sean Callanan1582ee62013-04-18 22:06:33 +00001787 if (log)
1788 {
1789 std::string s;
1790 raw_string_ostream oss(s);
1791
1792 module.print(oss, NULL);
1793
1794 oss.flush();
1795
1796 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
1797 }
1798
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001799 DataLayout data_layout(&module);
1800
1801 InterpreterStackFrame frame(data_layout, NULL, memory_map);
1802
Sean Callanan1582ee62013-04-18 22:06:33 +00001803 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
1804 {
1805 error.SetErrorString("Couldn't allocate stack frame");
1806 }
1807
1808 int arg_index = 0;
1809
1810 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
1811 ai != ae;
1812 ++ai, ++arg_index)
1813 {
1814 if (args.size() < arg_index)
1815 {
1816 error.SetErrorString ("Not enough arguments passed in to function");
1817 return false;
1818 }
1819
1820 lldb::addr_t ptr = args[arg_index];
1821
1822 frame.MakeArgument(ai, ptr);
1823 }
1824
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001825 uint32_t num_insts = 0;
1826
1827 frame.Jump(function.begin());
1828
1829 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
1830 {
1831 const Instruction *inst = frame.m_ii;
1832
1833 if (log)
1834 log->Printf("Interpreting %s", PrintValue(inst).c_str());
1835
1836 switch (inst->getOpcode())
1837 {
1838 default:
1839 break;
1840 case Instruction::Add:
1841 case Instruction::Sub:
1842 case Instruction::Mul:
1843 case Instruction::SDiv:
1844 case Instruction::UDiv:
1845 case Instruction::SRem:
1846 case Instruction::URem:
1847 case Instruction::Shl:
1848 case Instruction::LShr:
1849 case Instruction::AShr:
1850 case Instruction::And:
1851 case Instruction::Or:
1852 case Instruction::Xor:
1853 {
1854 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
1855
1856 if (!bin_op)
1857 {
1858 if (log)
1859 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
1860 error.SetErrorToGenericError();
1861 error.SetErrorString(interpreter_internal_error);
1862 return false;
1863 }
1864
1865 Value *lhs = inst->getOperand(0);
1866 Value *rhs = inst->getOperand(1);
1867
1868 lldb_private::Scalar L;
1869 lldb_private::Scalar R;
1870
1871 if (!frame.EvaluateValue(L, lhs, module))
1872 {
1873 if (log)
1874 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1875 error.SetErrorToGenericError();
1876 error.SetErrorString(bad_value_error);
1877 return false;
1878 }
1879
1880 if (!frame.EvaluateValue(R, rhs, module))
1881 {
1882 if (log)
1883 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1884 error.SetErrorToGenericError();
1885 error.SetErrorString(bad_value_error);
1886 return false;
1887 }
1888
1889 lldb_private::Scalar result;
1890
1891 switch (inst->getOpcode())
1892 {
1893 default:
1894 break;
1895 case Instruction::Add:
1896 result = L + R;
1897 break;
1898 case Instruction::Mul:
1899 result = L * R;
1900 break;
1901 case Instruction::Sub:
1902 result = L - R;
1903 break;
1904 case Instruction::SDiv:
1905 result = L / R;
1906 break;
1907 case Instruction::UDiv:
1908 result = L.GetRawBits64(0) / R.GetRawBits64(1);
1909 break;
1910 case Instruction::SRem:
1911 result = L % R;
1912 break;
1913 case Instruction::URem:
1914 result = L.GetRawBits64(0) % R.GetRawBits64(1);
1915 break;
1916 case Instruction::Shl:
1917 result = L << R;
1918 break;
1919 case Instruction::AShr:
1920 result = L >> R;
1921 break;
1922 case Instruction::LShr:
1923 result = L;
1924 result.ShiftRightLogical(R);
1925 break;
1926 case Instruction::And:
1927 result = L & R;
1928 break;
1929 case Instruction::Or:
1930 result = L | R;
1931 break;
1932 case Instruction::Xor:
1933 result = L ^ R;
1934 break;
1935 }
1936
1937 frame.AssignValue(inst, result, module);
1938
1939 if (log)
1940 {
1941 log->Printf("Interpreted a %s", inst->getOpcodeName());
1942 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1943 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1944 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1945 }
1946 }
1947 break;
1948 case Instruction::Alloca:
1949 {
1950 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
1951
1952 if (!alloca_inst)
1953 {
1954 if (log)
1955 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
1956 error.SetErrorToGenericError();
1957 error.SetErrorString(interpreter_internal_error);
1958 return false;
1959 }
1960
1961 if (alloca_inst->isArrayAllocation())
1962 {
1963 if (log)
1964 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
1965 error.SetErrorToGenericError();
1966 error.SetErrorString(unsupported_opcode_error);
1967 return false;
1968 }
1969
1970 // The semantics of Alloca are:
1971 // Create a region R of virtual memory of type T, backed by a data buffer
1972 // Create a region P of virtual memory of type T*, backed by a data buffer
1973 // Write the virtual address of R into P
1974
1975 Type *T = alloca_inst->getAllocatedType();
1976 Type *Tptr = alloca_inst->getType();
1977
1978 lldb::addr_t R = frame.Malloc(T);
1979
1980 if (R == LLDB_INVALID_ADDRESS)
1981 {
1982 if (log)
1983 log->Printf("Couldn't allocate memory for an AllocaInst");
1984 error.SetErrorToGenericError();
1985 error.SetErrorString(memory_allocation_error);
1986 return false;
1987 }
1988
1989 lldb::addr_t P = frame.Malloc(Tptr);
1990
1991 if (P == LLDB_INVALID_ADDRESS)
1992 {
1993 if (log)
1994 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
1995 error.SetErrorToGenericError();
1996 error.SetErrorString(memory_allocation_error);
1997 return false;
1998 }
1999
2000 lldb_private::Error write_error;
2001
2002 memory_map.WritePointerToMemory(P, R, write_error);
2003
2004 if (!write_error.Success())
2005 {
2006 if (log)
2007 log->Printf("Couldn't write the result pointer for an AllocaInst");
2008 error.SetErrorToGenericError();
2009 error.SetErrorString(memory_write_error);
2010 lldb_private::Error free_error;
2011 memory_map.Free(P, free_error);
2012 memory_map.Free(R, free_error);
2013 return false;
2014 }
2015
2016 frame.m_values[alloca_inst] = P;
2017
2018 if (log)
2019 {
2020 log->Printf("Interpreted an AllocaInst");
2021 log->Printf(" R : 0x%llx", R);
2022 log->Printf(" P : 0x%llx", P);
2023 }
2024 }
2025 break;
2026 case Instruction::BitCast:
2027 case Instruction::ZExt:
2028 {
2029 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
2030
2031 if (!cast_inst)
2032 {
2033 if (log)
2034 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
2035 error.SetErrorToGenericError();
2036 error.SetErrorString(interpreter_internal_error);
2037 return false;
2038 }
2039
2040 Value *source = cast_inst->getOperand(0);
2041
2042 lldb_private::Scalar S;
2043
2044 if (!frame.EvaluateValue(S, source, module))
2045 {
2046 if (log)
2047 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
2048 error.SetErrorToGenericError();
2049 error.SetErrorString(bad_value_error);
2050 return false;
2051 }
2052
2053 frame.AssignValue(inst, S, module);
2054 }
2055 break;
2056 case Instruction::Br:
2057 {
2058 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
2059
2060 if (!br_inst)
2061 {
2062 if (log)
2063 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
2064 error.SetErrorToGenericError();
2065 error.SetErrorString(interpreter_internal_error);
2066 return false;
2067 }
2068
2069 if (br_inst->isConditional())
2070 {
2071 Value *condition = br_inst->getCondition();
2072
2073 lldb_private::Scalar C;
2074
2075 if (!frame.EvaluateValue(C, condition, module))
2076 {
2077 if (log)
2078 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
2079 error.SetErrorToGenericError();
2080 error.SetErrorString(bad_value_error);
2081 return false;
2082 }
2083
2084 if (C.GetRawBits64(0))
2085 frame.Jump(br_inst->getSuccessor(0));
2086 else
2087 frame.Jump(br_inst->getSuccessor(1));
2088
2089 if (log)
2090 {
2091 log->Printf("Interpreted a BrInst with a condition");
2092 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
2093 }
2094 }
2095 else
2096 {
2097 frame.Jump(br_inst->getSuccessor(0));
2098
2099 if (log)
2100 {
2101 log->Printf("Interpreted a BrInst with no condition");
2102 }
2103 }
2104 }
2105 continue;
2106 case Instruction::GetElementPtr:
2107 {
2108 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
2109
2110 if (!gep_inst)
2111 {
2112 if (log)
2113 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
2114 error.SetErrorToGenericError();
2115 error.SetErrorString(interpreter_internal_error);
2116 return false;
2117 }
2118
2119 const Value *pointer_operand = gep_inst->getPointerOperand();
2120 Type *pointer_type = pointer_operand->getType();
2121
2122 lldb_private::Scalar P;
2123
2124 if (!frame.EvaluateValue(P, pointer_operand, module))
2125 {
2126 if (log)
2127 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
2128 error.SetErrorToGenericError();
2129 error.SetErrorString(bad_value_error);
2130 return false;
2131 }
2132
2133 typedef SmallVector <Value *, 8> IndexVector;
2134 typedef IndexVector::iterator IndexIterator;
2135
2136 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
2137 gep_inst->idx_end());
2138
2139 SmallVector <Value *, 8> const_indices;
2140
2141 for (IndexIterator ii = indices.begin(), ie = indices.end();
2142 ii != ie;
2143 ++ii)
2144 {
2145 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
2146
2147 if (!constant_index)
2148 {
2149 lldb_private::Scalar I;
2150
2151 if (!frame.EvaluateValue(I, *ii, module))
2152 {
2153 if (log)
2154 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
2155 error.SetErrorToGenericError();
2156 error.SetErrorString(bad_value_error);
2157 return false;
2158 }
2159
2160 if (log)
2161 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
2162
2163 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
2164 }
2165
2166 const_indices.push_back(constant_index);
2167 }
2168
2169 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
2170
2171 lldb_private::Scalar Poffset = P + offset;
2172
2173 frame.AssignValue(inst, Poffset, module);
2174
2175 if (log)
2176 {
2177 log->Printf("Interpreted a GetElementPtrInst");
2178 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
2179 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
2180 }
2181 }
2182 break;
2183 case Instruction::ICmp:
2184 {
2185 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
2186
2187 if (!icmp_inst)
2188 {
2189 if (log)
2190 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
2191 error.SetErrorToGenericError();
2192 error.SetErrorString(interpreter_internal_error);
2193 return false;
2194 }
2195
2196 CmpInst::Predicate predicate = icmp_inst->getPredicate();
2197
2198 Value *lhs = inst->getOperand(0);
2199 Value *rhs = inst->getOperand(1);
2200
2201 lldb_private::Scalar L;
2202 lldb_private::Scalar R;
2203
2204 if (!frame.EvaluateValue(L, lhs, module))
2205 {
2206 if (log)
2207 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
2208 error.SetErrorToGenericError();
2209 error.SetErrorString(bad_value_error);
2210 return false;
2211 }
2212
2213 if (!frame.EvaluateValue(R, rhs, module))
2214 {
2215 if (log)
2216 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
2217 error.SetErrorToGenericError();
2218 error.SetErrorString(bad_value_error);
2219 return false;
2220 }
2221
2222 lldb_private::Scalar result;
2223
2224 switch (predicate)
2225 {
2226 default:
2227 return false;
2228 case CmpInst::ICMP_EQ:
2229 result = (L == R);
2230 break;
2231 case CmpInst::ICMP_NE:
2232 result = (L != R);
2233 break;
2234 case CmpInst::ICMP_UGT:
2235 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
2236 break;
2237 case CmpInst::ICMP_UGE:
2238 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
2239 break;
2240 case CmpInst::ICMP_ULT:
2241 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
2242 break;
2243 case CmpInst::ICMP_ULE:
2244 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
2245 break;
2246 case CmpInst::ICMP_SGT:
2247 result = (L > R);
2248 break;
2249 case CmpInst::ICMP_SGE:
2250 result = (L >= R);
2251 break;
2252 case CmpInst::ICMP_SLT:
2253 result = (L < R);
2254 break;
2255 case CmpInst::ICMP_SLE:
2256 result = (L <= R);
2257 break;
2258 }
2259
2260 frame.AssignValue(inst, result, module);
2261
2262 if (log)
2263 {
2264 log->Printf("Interpreted an ICmpInst");
2265 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
2266 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
2267 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
2268 }
2269 }
2270 break;
2271 case Instruction::IntToPtr:
2272 {
2273 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
2274
2275 if (!int_to_ptr_inst)
2276 {
2277 if (log)
2278 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
2279 error.SetErrorToGenericError();
2280 error.SetErrorString(interpreter_internal_error);
2281 return false;
2282 }
2283
2284 Value *src_operand = int_to_ptr_inst->getOperand(0);
2285
2286 lldb_private::Scalar I;
2287
2288 if (!frame.EvaluateValue(I, src_operand, module))
2289 {
2290 if (log)
2291 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
2292 error.SetErrorToGenericError();
2293 error.SetErrorString(bad_value_error);
2294 return false;
2295 }
2296
2297 frame.AssignValue(inst, I, module);
2298
2299 if (log)
2300 {
2301 log->Printf("Interpreted an IntToPtr");
2302 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
2303 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
2304 }
2305 }
2306 break;
2307 case Instruction::PtrToInt:
2308 {
2309 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
2310
2311 if (!ptr_to_int_inst)
2312 {
2313 if (log)
2314 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
2315 error.SetErrorToGenericError();
2316 error.SetErrorString(interpreter_internal_error);
2317 return false;
2318 }
2319
2320 Value *src_operand = ptr_to_int_inst->getOperand(0);
2321
2322 lldb_private::Scalar I;
2323
2324 if (!frame.EvaluateValue(I, src_operand, module))
2325 {
2326 if (log)
2327 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
2328 error.SetErrorToGenericError();
2329 error.SetErrorString(bad_value_error);
2330 return false;
2331 }
2332
2333 frame.AssignValue(inst, I, module);
2334
2335 if (log)
2336 {
2337 log->Printf("Interpreted a PtrToInt");
2338 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
2339 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
2340 }
2341 }
2342 break;
2343 case Instruction::Load:
2344 {
2345 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
2346
2347 if (!load_inst)
2348 {
2349 if (log)
2350 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
2351 error.SetErrorToGenericError();
2352 error.SetErrorString(interpreter_internal_error);
2353 return false;
2354 }
2355
2356 // The semantics of Load are:
2357 // Create a region D that will contain the loaded data
2358 // Resolve the region P containing a pointer
2359 // Dereference P to get the region R that the data should be loaded from
2360 // Transfer a unit of type type(D) from R to D
2361
2362 const Value *pointer_operand = load_inst->getPointerOperand();
2363
2364 Type *pointer_ty = pointer_operand->getType();
2365 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
2366 if (!pointer_ptr_ty)
2367 {
2368 if (log)
2369 log->Printf("getPointerOperand()->getType() is not a PointerType");
2370 error.SetErrorToGenericError();
2371 error.SetErrorString(interpreter_internal_error);
2372 return false;
2373 }
2374 Type *target_ty = pointer_ptr_ty->getElementType();
2375
2376 lldb::addr_t D = frame.ResolveValue(load_inst, module);
2377 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
2378
2379 if (D == LLDB_INVALID_ADDRESS)
2380 {
2381 if (log)
2382 log->Printf("LoadInst's value doesn't resolve to anything");
2383 error.SetErrorToGenericError();
2384 error.SetErrorString(bad_value_error);
2385 return false;
2386 }
2387
2388 if (P == LLDB_INVALID_ADDRESS)
2389 {
2390 if (log)
2391 log->Printf("LoadInst's pointer doesn't resolve to anything");
2392 error.SetErrorToGenericError();
2393 error.SetErrorString(bad_value_error);
2394 return false;
2395 }
2396
2397 lldb::addr_t R;
2398 lldb_private::Error read_error;
2399 memory_map.ReadPointerFromMemory(&R, P, read_error);
2400
2401 if (!read_error.Success())
2402 {
2403 if (log)
2404 log->Printf("Couldn't read the address to be loaded for a LoadInst");
2405 error.SetErrorToGenericError();
2406 error.SetErrorString(memory_read_error);
2407 return false;
2408 }
2409
2410 size_t target_size = data_layout.getTypeStoreSize(target_ty);
2411 lldb_private::DataBufferHeap buffer(target_size, 0);
2412
2413 read_error.Clear();
2414 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
2415 if (!read_error.Success())
2416 {
2417 if (log)
2418 log->Printf("Couldn't read from a region on behalf of a LoadInst");
2419 error.SetErrorToGenericError();
2420 error.SetErrorString(memory_read_error);
2421 return false;
2422 }
2423
2424 lldb_private::Error write_error;
2425 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
2426 if (!write_error.Success())
2427 {
2428 if (log)
2429 log->Printf("Couldn't write to a region on behalf of a LoadInst");
2430 error.SetErrorToGenericError();
2431 error.SetErrorString(memory_read_error);
2432 return false;
2433 }
2434
2435 if (log)
2436 {
2437 log->Printf("Interpreted a LoadInst");
2438 log->Printf(" P : 0x%llx", P);
2439 log->Printf(" R : 0x%llx", R);
2440 log->Printf(" D : 0x%llx", D);
2441 }
2442 }
2443 break;
2444 case Instruction::Ret:
2445 {
2446 return true;
2447 }
2448 case Instruction::Store:
2449 {
2450 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
2451
2452 if (!store_inst)
2453 {
2454 if (log)
2455 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
2456 error.SetErrorToGenericError();
2457 error.SetErrorString(interpreter_internal_error);
2458 return false;
2459 }
2460
2461 // The semantics of Store are:
2462 // Resolve the region D containing the data to be stored
2463 // Resolve the region P containing a pointer
2464 // Dereference P to get the region R that the data should be stored in
2465 // Transfer a unit of type type(D) from D to R
2466
2467 const Value *value_operand = store_inst->getValueOperand();
2468 const Value *pointer_operand = store_inst->getPointerOperand();
2469
2470 Type *pointer_ty = pointer_operand->getType();
2471 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
2472 if (!pointer_ptr_ty)
2473 return false;
2474 Type *target_ty = pointer_ptr_ty->getElementType();
2475
2476 lldb::addr_t D = frame.ResolveValue(value_operand, module);
2477 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
2478
2479 if (D == LLDB_INVALID_ADDRESS)
2480 {
2481 if (log)
2482 log->Printf("StoreInst's value doesn't resolve to anything");
2483 error.SetErrorToGenericError();
2484 error.SetErrorString(bad_value_error);
2485 return false;
2486 }
2487
2488 if (P == LLDB_INVALID_ADDRESS)
2489 {
2490 if (log)
2491 log->Printf("StoreInst's pointer doesn't resolve to anything");
2492 error.SetErrorToGenericError();
2493 error.SetErrorString(bad_value_error);
2494 return false;
2495 }
2496
2497 lldb::addr_t R;
2498 lldb_private::Error read_error;
2499 memory_map.ReadPointerFromMemory(&R, P, read_error);
2500
2501 if (!read_error.Success())
2502 {
2503 if (log)
2504 log->Printf("Couldn't read the address to be loaded for a LoadInst");
2505 error.SetErrorToGenericError();
2506 error.SetErrorString(memory_read_error);
2507 return false;
2508 }
2509
2510 size_t target_size = data_layout.getTypeStoreSize(target_ty);
2511 lldb_private::DataBufferHeap buffer(target_size, 0);
2512
2513 read_error.Clear();
2514 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
2515 if (!read_error.Success())
2516 {
2517 if (log)
2518 log->Printf("Couldn't read from a region on behalf of a StoreInst");
2519 error.SetErrorToGenericError();
2520 error.SetErrorString(memory_read_error);
2521 return false;
2522 }
2523
2524 lldb_private::Error write_error;
2525 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
2526 if (!write_error.Success())
2527 {
2528 if (log)
2529 log->Printf("Couldn't write to a region on behalf of a StoreInst");
2530 error.SetErrorToGenericError();
2531 error.SetErrorString(memory_read_error);
2532 return false;
2533 }
2534
2535 if (log)
2536 {
2537 log->Printf("Interpreted a StoreInst");
2538 log->Printf(" D : 0x%llx", D);
2539 log->Printf(" P : 0x%llx", P);
2540 log->Printf(" R : 0x%llx", R);
2541 }
2542 }
2543 break;
2544 }
2545
2546 ++frame.m_ii;
2547 }
2548
2549 if (num_insts >= 4096)
2550 {
2551 error.SetErrorToGenericError();
2552 error.SetErrorString(infinite_loop_error);
2553 return false;
2554 }
2555
2556 return false;
2557}