blob: a2b0c5b86851d657724dffd1e23825c1434fac51 [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
Sean Callananfefe43c2013-04-25 18:55:45 +000010#include "lldb/Core/DataExtractor.h"
11#include "lldb/Core/Error.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000012#include "lldb/Core/Log.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000013#include "lldb/Core/ModuleSpec.h"
14#include "lldb/Core/Module.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000015#include "lldb/Core/Scalar.h"
16#include "lldb/Core/StreamString.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000017#include "lldb/Core/ValueObject.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000018#include "lldb/Expression/IRMemoryMap.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000019#include "lldb/Expression/IRInterpreter.h"
Sean Callanan46fc0062013-09-17 18:26:42 +000020#include "lldb/Host/Endian.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000021
Ewan Crawford90ff7912015-07-14 10:56:58 +000022#include "lldb/Target/ABI.h"
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlan.h"
27#include "lldb/Target/ThreadPlanCallFunctionUsingABI.h"
28
Chandler Carruth1e157582013-01-02 12:20:07 +000029#include "llvm/IR/Constants.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000030#include "llvm/IR/DataLayout.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000031#include "llvm/IR/Function.h"
32#include "llvm/IR/Instructions.h"
Sean Callanan576a4372014-03-25 19:33:15 +000033#include "llvm/IR/Intrinsics.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000034#include "llvm/IR/LLVMContext.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000035#include "llvm/IR/Module.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000036#include "llvm/Support/raw_ostream.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000037
38#include <map>
39
40using namespace llvm;
41
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000042static std::string
Sean Callanan3bfdaa22011-09-15 02:13:07 +000043PrintValue(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);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000051
Sean Callanan3bfdaa22011-09-15 02:13:07 +000052 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);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000057
Sean Callanan3bfdaa22011-09-15 02:13:07 +000058 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
Sean Callanan576a4372014-03-25 19:33:15 +000073static bool
74CanIgnoreCall (const CallInst *call)
75{
76 const llvm::Function *called_function = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000077
Sean Callanan576a4372014-03-25 19:33:15 +000078 if (!called_function)
79 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Sean Callanan576a4372014-03-25 19:33:15 +000081 if (called_function->isIntrinsic())
82 {
83 switch (called_function->getIntrinsicID())
84 {
85 default:
86 break;
87 case llvm::Intrinsic::dbg_declare:
88 case llvm::Intrinsic::dbg_value:
89 return true;
90 }
91 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000092
Sean Callanan576a4372014-03-25 19:33:15 +000093 return false;
94}
95
Sean Callanan3bfdaa22011-09-15 02:13:07 +000096class InterpreterStackFrame
97{
98public:
Sean Callanan08052af2013-04-17 07:50:58 +000099 typedef std::map <const Value*, lldb::addr_t> ValueMap;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000100
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000101 ValueMap m_values;
Micah Villmow8468dbe2012-10-08 16:28:57 +0000102 DataLayout &m_target_data;
Sean Callanan179b5482013-04-16 23:49:09 +0000103 lldb_private::IRMemoryMap &m_memory_map;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000104 const BasicBlock *m_bb;
105 BasicBlock::const_iterator m_ii;
106 BasicBlock::const_iterator m_ie;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000107
Sean Callanan1582ee62013-04-18 22:06:33 +0000108 lldb::addr_t m_frame_process_address;
109 size_t m_frame_size;
110 lldb::addr_t m_stack_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000111
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000112 lldb::ByteOrder m_byte_order;
113 size_t m_addr_byte_size;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000114
Micah Villmow8468dbe2012-10-08 16:28:57 +0000115 InterpreterStackFrame (DataLayout &target_data,
Sean Callanandf565402013-04-27 02:19:33 +0000116 lldb_private::IRMemoryMap &memory_map,
117 lldb::addr_t stack_frame_bottom,
118 lldb::addr_t stack_frame_top) :
Daniel Dunbara08823f2011-10-31 22:50:49 +0000119 m_target_data (target_data),
Sean Callanan179b5482013-04-16 23:49:09 +0000120 m_memory_map (memory_map)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000121 {
122 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +0000123 m_addr_byte_size = (target_data.getPointerSize(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000124
Sean Callanandf565402013-04-27 02:19:33 +0000125 m_frame_process_address = stack_frame_bottom;
126 m_frame_size = stack_frame_top - stack_frame_bottom;
127 m_stack_pointer = stack_frame_top;
Sean Callanan1582ee62013-04-18 22:06:33 +0000128 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000129
Sean Callanan1582ee62013-04-18 22:06:33 +0000130 ~InterpreterStackFrame ()
131 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000132 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000133
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000134 void Jump (const BasicBlock *bb)
135 {
136 m_bb = bb;
137 m_ii = m_bb->begin();
138 m_ie = m_bb->end();
139 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000140
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000141 std::string SummarizeValue (const Value *value)
142 {
143 lldb_private::StreamString ss;
144
145 ss.Printf("%s", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000146
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000147 ValueMap::iterator i = m_values.find(value);
148
149 if (i != m_values.end())
150 {
Sean Callanan08052af2013-04-17 07:50:58 +0000151 lldb::addr_t addr = i->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000152
Sean Callanan08052af2013-04-17 07:50:58 +0000153 ss.Printf(" 0x%llx", (unsigned long long)addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000154 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000155
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000156 return ss.GetString();
157 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000158
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000159 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
160 {
161 size_t type_size = m_target_data.getTypeStoreSize(type);
162
163 switch (type_size)
164 {
165 case 1:
166 scalar = (uint8_t)u64value;
167 break;
168 case 2:
169 scalar = (uint16_t)u64value;
170 break;
171 case 4:
172 scalar = (uint32_t)u64value;
173 break;
174 case 8:
175 scalar = (uint64_t)u64value;
176 break;
177 default:
178 return false;
179 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000180
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000181 return true;
182 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000183
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000184 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
185 {
186 const Constant *constant = dyn_cast<Constant>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000187
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000188 if (constant)
189 {
Sean Callanan415422c2013-06-05 22:07:06 +0000190 APInt value_apint;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000191
Sean Callanan415422c2013-06-05 22:07:06 +0000192 if (!ResolveConstantValue(value_apint, constant))
193 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000194
Sean Callanan415422c2013-06-05 22:07:06 +0000195 return AssignToMatchType(scalar, value_apint.getLimitedValue(), value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000196 }
197 else
198 {
Sean Callanan08052af2013-04-17 07:50:58 +0000199 lldb::addr_t process_address = ResolveValue(value, module);
200 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000201
Sean Callanan08052af2013-04-17 07:50:58 +0000202 lldb_private::DataExtractor value_extractor;
203 lldb_private::Error extract_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000204
Sean Callanan08052af2013-04-17 07:50:58 +0000205 m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000206
Sean Callanan08052af2013-04-17 07:50:58 +0000207 if (!extract_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000208 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000209
Greg Claytonc7bece562013-01-25 18:06:21 +0000210 lldb::offset_t offset = 0;
Sean Callanan544053e2013-06-06 21:14:35 +0000211 if (value_size == 1 || value_size == 2 || value_size == 4 || value_size == 8)
Greg Clayton78e44bd2013-04-25 00:57:05 +0000212 {
213 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
214 return AssignToMatchType(scalar, u64value, value->getType());
215 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000216 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000217
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000218 return false;
219 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000220
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000221 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
222 {
Sean Callanan08052af2013-04-17 07:50:58 +0000223 lldb::addr_t process_address = ResolveValue (value, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan08052af2013-04-17 07:50:58 +0000225 if (process_address == LLDB_INVALID_ADDRESS)
226 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000227
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000228 lldb_private::Scalar cast_scalar;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000230 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
231 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000232
Sean Callanan08052af2013-04-17 07:50:58 +0000233 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Sean Callanan08052af2013-04-17 07:50:58 +0000235 lldb_private::DataBufferHeap buf(value_byte_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000236
Sean Callanan08052af2013-04-17 07:50:58 +0000237 lldb_private::Error get_data_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Sean Callanan08052af2013-04-17 07:50:58 +0000239 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000240 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000241
Sean Callanan08052af2013-04-17 07:50:58 +0000242 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000243
Sean Callanan08052af2013-04-17 07:50:58 +0000244 m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000245
Sean Callanan08052af2013-04-17 07:50:58 +0000246 return write_error.Success();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000247 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000248
Sean Callanan94a9a392012-02-08 01:27:49 +0000249 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000250 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000251 switch (constant->getValueID())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000252 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000253 default:
254 break;
255 case Value::ConstantIntVal:
256 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan80c48c12011-10-21 05:18:02 +0000257 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000258 value = constant_int->getValue();
259 return true;
260 }
261 break;
262 case Value::ConstantFPVal:
263 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
264 {
265 value = constant_fp->getValueAPF().bitcastToAPInt();
266 return true;
267 }
268 break;
269 case Value::ConstantExprVal:
270 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
271 {
272 switch (constant_expr->getOpcode())
Sean Callanan94a9a392012-02-08 01:27:49 +0000273 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000274 default:
Sean Callanan94a9a392012-02-08 01:27:49 +0000275 return false;
Sean Callanan1582ee62013-04-18 22:06:33 +0000276 case Instruction::IntToPtr:
277 case Instruction::PtrToInt:
278 case Instruction::BitCast:
279 return ResolveConstantValue(value, constant_expr->getOperand(0));
280 case Instruction::GetElementPtr:
281 {
282 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
283 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000284
Sean Callanan1582ee62013-04-18 22:06:33 +0000285 Constant *base = dyn_cast<Constant>(*op_cursor);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000286
Sean Callanan1582ee62013-04-18 22:06:33 +0000287 if (!base)
288 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000289
Sean Callanan1582ee62013-04-18 22:06:33 +0000290 if (!ResolveConstantValue(value, base))
291 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000292
Sean Callanan1582ee62013-04-18 22:06:33 +0000293 op_cursor++;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Sean Callanan1582ee62013-04-18 22:06:33 +0000295 if (op_cursor == op_end)
296 return true; // no offset to apply!
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Sean Callanan1582ee62013-04-18 22:06:33 +0000298 SmallVector <Value *, 8> indices (op_cursor, op_end);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000299
Sean Callanan1582ee62013-04-18 22:06:33 +0000300 uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000301
Sean Callanan1582ee62013-04-18 22:06:33 +0000302 const bool is_signed = true;
303 value += APInt(value.getBitWidth(), offset, is_signed);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000304
Sean Callanan1582ee62013-04-18 22:06:33 +0000305 return true;
306 }
Sean Callanan94a9a392012-02-08 01:27:49 +0000307 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000308 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000309 break;
310 case Value::ConstantPointerNullVal:
311 if (isa<ConstantPointerNull>(constant))
312 {
313 value = APInt(m_target_data.getPointerSizeInBits(), 0);
314 return true;
315 }
316 break;
317 }
318 return false;
319 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Sean Callanan1582ee62013-04-18 22:06:33 +0000321 bool MakeArgument(const Argument *value, uint64_t address)
322 {
323 lldb::addr_t data_address = Malloc(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000324
Sean Callanan1582ee62013-04-18 22:06:33 +0000325 if (data_address == LLDB_INVALID_ADDRESS)
326 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000327
Sean Callanan1582ee62013-04-18 22:06:33 +0000328 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Sean Callanan1582ee62013-04-18 22:06:33 +0000330 m_memory_map.WritePointerToMemory(data_address, address, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000331
Sean Callanan1582ee62013-04-18 22:06:33 +0000332 if (!write_error.Success())
333 {
334 lldb_private::Error free_error;
335 m_memory_map.Free(data_address, free_error);
336 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000337 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000338
Sean Callanan1582ee62013-04-18 22:06:33 +0000339 m_values[value] = data_address;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000340
Sean Callanan1582ee62013-04-18 22:06:33 +0000341 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
342
343 if (log)
344 {
345 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
346 log->Printf(" Data region : %llx", (unsigned long long)address);
347 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
348 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Sean Callanan1582ee62013-04-18 22:06:33 +0000350 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000351 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000352
Sean Callanan08052af2013-04-17 07:50:58 +0000353 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
Sean Callanan94a9a392012-02-08 01:27:49 +0000354 {
355 APInt resolved_value;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000356
Sean Callanan94a9a392012-02-08 01:27:49 +0000357 if (!ResolveConstantValue(resolved_value, constant))
358 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000359
Sean Callanan46fc0062013-09-17 18:26:42 +0000360 lldb_private::StreamString buffer (lldb_private::Stream::eBinary,
361 m_memory_map.GetAddressByteSize(),
362 m_memory_map.GetByteOrder());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Sean Callanan94a9a392012-02-08 01:27:49 +0000364 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000365
Sean Callanan46fc0062013-09-17 18:26:42 +0000366 const uint64_t *raw_data = resolved_value.getRawData();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000367
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000368 buffer.PutRawBytes(raw_data, constant_size, lldb_private::endian::InlHostByteOrder());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000369
Sean Callanan08052af2013-04-17 07:50:58 +0000370 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Sean Callanan46fc0062013-09-17 18:26:42 +0000372 m_memory_map.WriteMemory(process_address, (const uint8_t*)buffer.GetData(), constant_size, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000373
Sean Callanan08052af2013-04-17 07:50:58 +0000374 return write_error.Success();
375 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000376
Sean Callanan1582ee62013-04-18 22:06:33 +0000377 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
378 {
379 lldb::addr_t ret = m_stack_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Sean Callanan1582ee62013-04-18 22:06:33 +0000381 ret -= size;
382 ret -= (ret % byte_alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000383
Sean Callanan1582ee62013-04-18 22:06:33 +0000384 if (ret < m_frame_process_address)
385 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000386
Sean Callanan1582ee62013-04-18 22:06:33 +0000387 m_stack_pointer = ret;
388 return ret;
389 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000390
Sean Callanan08052af2013-04-17 07:50:58 +0000391 lldb::addr_t MallocPointer ()
392 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000393 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000394 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000395
Sean Callanan1582ee62013-04-18 22:06:33 +0000396 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000397 {
398 lldb_private::Error alloc_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000399
Sean Callanan1582ee62013-04-18 22:06:33 +0000400 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000401 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000402
Sean Callanan08052af2013-04-17 07:50:58 +0000403 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
404 {
405 size_t length = m_target_data.getTypeStoreSize(type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000406
Sean Callanan08052af2013-04-17 07:50:58 +0000407 lldb_private::DataBufferHeap buf(length, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan08052af2013-04-17 07:50:58 +0000409 lldb_private::Error read_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000410
Sean Callanan08052af2013-04-17 07:50:58 +0000411 m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000412
Sean Callanan08052af2013-04-17 07:50:58 +0000413 if (!read_error.Success())
414 return std::string("<couldn't read data>");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000415
Sean Callanan08052af2013-04-17 07:50:58 +0000416 lldb_private::StreamString ss;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000417
Sean Callanan08052af2013-04-17 07:50:58 +0000418 for (size_t i = 0; i < length; i++)
419 {
420 if ((!(i & 0xf)) && i)
421 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
422 else
423 ss.Printf("%02hhx ", buf.GetBytes()[i]);
424 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Sean Callanan08052af2013-04-17 07:50:58 +0000426 return ss.GetString();
427 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000428
Sean Callanan08052af2013-04-17 07:50:58 +0000429 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000430 {
431 ValueMap::iterator i = m_values.find(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000433 if (i != m_values.end())
434 return i->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000435
Sean Callanan1582ee62013-04-18 22:06:33 +0000436 // Fall back and allocate space [allocation type Alloca]
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000437
Sean Callanan1582ee62013-04-18 22:06:33 +0000438 lldb::addr_t data_address = Malloc(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000439
Sean Callanan1582ee62013-04-18 22:06:33 +0000440 if (const Constant *constant = dyn_cast<Constant>(value))
441 {
442 if (!ResolveConstant (data_address, constant))
443 {
444 lldb_private::Error free_error;
445 m_memory_map.Free(data_address, free_error);
446 return LLDB_INVALID_ADDRESS;
447 }
448 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Sean Callanan1582ee62013-04-18 22:06:33 +0000450 m_values[value] = data_address;
451 return data_address;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000452 }
453};
454
Sean Callanan175a0d02012-01-24 22:06:48 +0000455static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Sean Callanan3fa3e652013-05-02 00:33:44 +0000456static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
Greg Claytone01e07b2013-04-18 18:10:51 +0000457//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000458static const char *interpreter_internal_error = "Interpreter encountered an internal error";
459static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
460static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
461static const char *memory_write_error = "Interpreter couldn't write to memory";
462static const char *memory_read_error = "Interpreter couldn't read from memory";
463static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan44342732013-04-19 08:14:32 +0000464//static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000465
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000466bool
Sean Callanan44342732013-04-19 08:14:32 +0000467IRInterpreter::CanInterpret (llvm::Module &module,
468 llvm::Function &function,
Ewan Crawford90ff7912015-07-14 10:56:58 +0000469 lldb_private::Error &error,
470 const bool support_function_calls)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000471{
Greg Clayton5160ce52013-03-27 23:08:40 +0000472 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000473
Sean Callanan85fc8762013-06-27 01:59:51 +0000474 bool saw_function_with_body = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000475
Sean Callanan85fc8762013-06-27 01:59:51 +0000476 for (Module::iterator fi = module.begin(), fe = module.end();
477 fi != fe;
478 ++fi)
479 {
480 if (fi->begin() != fi->end())
481 {
482 if (saw_function_with_body)
483 return false;
484 saw_function_with_body = true;
485 }
486 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Sean Callanan44342732013-04-19 08:14:32 +0000488 for (Function::iterator bbi = function.begin(), bbe = function.end();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000489 bbi != bbe;
490 ++bbi)
491 {
492 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
493 ii != ie;
494 ++ii)
495 {
496 switch (ii->getOpcode())
497 {
498 default:
499 {
500 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000501 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str());
Sean Callanan44342732013-04-19 08:14:32 +0000502 error.SetErrorToGenericError();
503 error.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000504 return false;
505 }
506 case Instruction::Add:
507 case Instruction::Alloca:
508 case Instruction::BitCast:
509 case Instruction::Br:
Sean Callanan576a4372014-03-25 19:33:15 +0000510 break;
511 case Instruction::Call:
512 {
513 CallInst *call_inst = dyn_cast<CallInst>(ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000514
Sean Callanan576a4372014-03-25 19:33:15 +0000515 if (!call_inst)
516 {
517 error.SetErrorToGenericError();
518 error.SetErrorString(interpreter_internal_error);
519 return false;
520 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000521
Ewan Crawford90ff7912015-07-14 10:56:58 +0000522 if (!CanIgnoreCall(call_inst) && !support_function_calls)
Sean Callanan576a4372014-03-25 19:33:15 +0000523 {
524 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000525 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str());
Sean Callanan576a4372014-03-25 19:33:15 +0000526 error.SetErrorToGenericError();
527 error.SetErrorString(unsupported_opcode_error);
528 return false;
529 }
530 }
Sean Callanan92872892014-03-25 19:47:07 +0000531 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000532 case Instruction::GetElementPtr:
533 break;
534 case Instruction::ICmp:
535 {
536 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000537
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000538 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000539 {
Sean Callanan44342732013-04-19 08:14:32 +0000540 error.SetErrorToGenericError();
541 error.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000542 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000543 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000544
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000545 switch (icmp_inst->getPredicate())
546 {
547 default:
Sean Callanan44342732013-04-19 08:14:32 +0000548 {
549 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000550 log->Printf("Unsupported ICmp predicate: %s", PrintValue(&*ii).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000551
Sean Callanan44342732013-04-19 08:14:32 +0000552 error.SetErrorToGenericError();
553 error.SetErrorString(unsupported_opcode_error);
554 return false;
555 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000556 case CmpInst::ICMP_EQ:
557 case CmpInst::ICMP_NE:
558 case CmpInst::ICMP_UGT:
559 case CmpInst::ICMP_UGE:
560 case CmpInst::ICMP_ULT:
561 case CmpInst::ICMP_ULE:
562 case CmpInst::ICMP_SGT:
563 case CmpInst::ICMP_SGE:
564 case CmpInst::ICMP_SLT:
565 case CmpInst::ICMP_SLE:
566 break;
567 }
568 }
569 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000570 case Instruction::And:
571 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000572 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000573 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000574 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000575 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000576 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000577 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000578 case Instruction::Ret:
579 case Instruction::SDiv:
Sean Callanan415422c2013-06-05 22:07:06 +0000580 case Instruction::SExt:
Sean Callanan087f4372013-01-09 22:44:41 +0000581 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000582 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000583 case Instruction::Store:
584 case Instruction::Sub:
Sean Callanan8c46bac2013-10-11 19:45:00 +0000585 case Instruction::Trunc:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000586 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000587 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000588 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000589 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000590 break;
591 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Sean Callanan3fa3e652013-05-02 00:33:44 +0000593 for (int oi = 0, oe = ii->getNumOperands();
594 oi != oe;
595 ++oi)
596 {
597 Value *operand = ii->getOperand(oi);
598 Type *operand_type = operand->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000599
Sean Callanan3fa3e652013-05-02 00:33:44 +0000600 switch (operand_type->getTypeID())
601 {
602 default:
603 break;
604 case Type::VectorTyID:
605 {
606 if (log)
607 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
608 error.SetErrorString(unsupported_operand_error);
609 return false;
610 }
611 }
612 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000613 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000615 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000616
Sean Callanan44342732013-04-19 08:14:32 +0000617 return true;}
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000618
619bool
620IRInterpreter::Interpret (llvm::Module &module,
621 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +0000622 llvm::ArrayRef<lldb::addr_t> args,
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000623 lldb_private::IRMemoryMap &memory_map,
Sean Callanandf565402013-04-27 02:19:33 +0000624 lldb_private::Error &error,
625 lldb::addr_t stack_frame_bottom,
Ewan Crawford90ff7912015-07-14 10:56:58 +0000626 lldb::addr_t stack_frame_top,
627 lldb_private::ExecutionContext &exe_ctx)
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000628{
629 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Sean Callanan1582ee62013-04-18 22:06:33 +0000631 if (log)
632 {
633 std::string s;
634 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000635
Sean Callanan1582ee62013-04-18 22:06:33 +0000636 module.print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000637
Sean Callanan1582ee62013-04-18 22:06:33 +0000638 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000639
Sean Callanan1582ee62013-04-18 22:06:33 +0000640 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
641 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000642
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000643 DataLayout data_layout(&module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000644
Sean Callanandf565402013-04-27 02:19:33 +0000645 InterpreterStackFrame frame(data_layout, memory_map, stack_frame_bottom, stack_frame_top);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000646
Sean Callanan1582ee62013-04-18 22:06:33 +0000647 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
648 {
649 error.SetErrorString("Couldn't allocate stack frame");
650 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000651
Sean Callanan1582ee62013-04-18 22:06:33 +0000652 int arg_index = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000653
Sean Callanan1582ee62013-04-18 22:06:33 +0000654 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
655 ai != ae;
656 ++ai, ++arg_index)
657 {
Chaoren Lin3ca7a3e2015-09-16 01:20:34 +0000658 if (args.size() <= static_cast<size_t>(arg_index))
Sean Callanan1582ee62013-04-18 22:06:33 +0000659 {
660 error.SetErrorString ("Not enough arguments passed in to function");
661 return false;
662 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000663
Sean Callanan1582ee62013-04-18 22:06:33 +0000664 lldb::addr_t ptr = args[arg_index];
665
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000666 frame.MakeArgument(&*ai, ptr);
Sean Callanan1582ee62013-04-18 22:06:33 +0000667 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000668
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000669 uint32_t num_insts = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000670
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000671 frame.Jump(&function.front());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000672
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000673 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
674 {
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000675 const Instruction *inst = &*frame.m_ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000676
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000677 if (log)
678 log->Printf("Interpreting %s", PrintValue(inst).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000679
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000680 switch (inst->getOpcode())
681 {
682 default:
683 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000685 case Instruction::Add:
686 case Instruction::Sub:
687 case Instruction::Mul:
688 case Instruction::SDiv:
689 case Instruction::UDiv:
690 case Instruction::SRem:
691 case Instruction::URem:
692 case Instruction::Shl:
693 case Instruction::LShr:
694 case Instruction::AShr:
695 case Instruction::And:
696 case Instruction::Or:
697 case Instruction::Xor:
698 {
699 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000700
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000701 if (!bin_op)
702 {
703 if (log)
704 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
705 error.SetErrorToGenericError();
706 error.SetErrorString(interpreter_internal_error);
707 return false;
708 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000709
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000710 Value *lhs = inst->getOperand(0);
711 Value *rhs = inst->getOperand(1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000712
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000713 lldb_private::Scalar L;
714 lldb_private::Scalar R;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000715
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000716 if (!frame.EvaluateValue(L, lhs, module))
717 {
718 if (log)
719 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
720 error.SetErrorToGenericError();
721 error.SetErrorString(bad_value_error);
722 return false;
723 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000724
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000725 if (!frame.EvaluateValue(R, rhs, module))
726 {
727 if (log)
728 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
729 error.SetErrorToGenericError();
730 error.SetErrorString(bad_value_error);
731 return false;
732 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000733
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000734 lldb_private::Scalar result;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000735
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000736 switch (inst->getOpcode())
737 {
738 default:
739 break;
740 case Instruction::Add:
741 result = L + R;
742 break;
743 case Instruction::Mul:
744 result = L * R;
745 break;
746 case Instruction::Sub:
747 result = L - R;
748 break;
749 case Instruction::SDiv:
Sean Callanan0b342b62013-05-24 20:36:56 +0000750 L.MakeSigned();
751 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000752 result = L / R;
753 break;
754 case Instruction::UDiv:
755 result = L.GetRawBits64(0) / R.GetRawBits64(1);
756 break;
757 case Instruction::SRem:
Sean Callanan0b342b62013-05-24 20:36:56 +0000758 L.MakeSigned();
759 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000760 result = L % R;
761 break;
762 case Instruction::URem:
763 result = L.GetRawBits64(0) % R.GetRawBits64(1);
764 break;
765 case Instruction::Shl:
766 result = L << R;
767 break;
768 case Instruction::AShr:
769 result = L >> R;
770 break;
771 case Instruction::LShr:
772 result = L;
773 result.ShiftRightLogical(R);
774 break;
775 case Instruction::And:
776 result = L & R;
777 break;
778 case Instruction::Or:
779 result = L | R;
780 break;
781 case Instruction::Xor:
782 result = L ^ R;
783 break;
784 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000786 frame.AssignValue(inst, result, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000787
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000788 if (log)
789 {
790 log->Printf("Interpreted a %s", inst->getOpcodeName());
791 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
792 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
793 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
794 }
795 }
796 break;
797 case Instruction::Alloca:
798 {
799 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000800
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000801 if (!alloca_inst)
802 {
803 if (log)
804 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
805 error.SetErrorToGenericError();
806 error.SetErrorString(interpreter_internal_error);
807 return false;
808 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000809
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000810 if (alloca_inst->isArrayAllocation())
811 {
812 if (log)
813 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
814 error.SetErrorToGenericError();
815 error.SetErrorString(unsupported_opcode_error);
816 return false;
817 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000818
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000819 // The semantics of Alloca are:
820 // Create a region R of virtual memory of type T, backed by a data buffer
821 // Create a region P of virtual memory of type T*, backed by a data buffer
822 // Write the virtual address of R into P
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000823
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000824 Type *T = alloca_inst->getAllocatedType();
825 Type *Tptr = alloca_inst->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000826
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000827 lldb::addr_t R = frame.Malloc(T);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000828
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000829 if (R == LLDB_INVALID_ADDRESS)
830 {
831 if (log)
832 log->Printf("Couldn't allocate memory for an AllocaInst");
833 error.SetErrorToGenericError();
834 error.SetErrorString(memory_allocation_error);
835 return false;
836 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000837
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000838 lldb::addr_t P = frame.Malloc(Tptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000839
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000840 if (P == LLDB_INVALID_ADDRESS)
841 {
842 if (log)
843 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
844 error.SetErrorToGenericError();
845 error.SetErrorString(memory_allocation_error);
846 return false;
847 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000848
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000849 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000850
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000851 memory_map.WritePointerToMemory(P, R, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000853 if (!write_error.Success())
854 {
855 if (log)
856 log->Printf("Couldn't write the result pointer for an AllocaInst");
857 error.SetErrorToGenericError();
858 error.SetErrorString(memory_write_error);
859 lldb_private::Error free_error;
860 memory_map.Free(P, free_error);
861 memory_map.Free(R, free_error);
862 return false;
863 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000864
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000865 frame.m_values[alloca_inst] = P;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000866
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000867 if (log)
868 {
869 log->Printf("Interpreted an AllocaInst");
Matt Kopecef143712013-06-03 18:00:07 +0000870 log->Printf(" R : 0x%" PRIx64, R);
871 log->Printf(" P : 0x%" PRIx64, P);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000872 }
873 }
874 break;
875 case Instruction::BitCast:
876 case Instruction::ZExt:
877 {
878 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000879
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000880 if (!cast_inst)
881 {
882 if (log)
883 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
884 error.SetErrorToGenericError();
885 error.SetErrorString(interpreter_internal_error);
886 return false;
887 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000888
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000889 Value *source = cast_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000890
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000891 lldb_private::Scalar S;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000892
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000893 if (!frame.EvaluateValue(S, source, module))
894 {
895 if (log)
896 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
897 error.SetErrorToGenericError();
898 error.SetErrorString(bad_value_error);
899 return false;
900 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000901
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000902 frame.AssignValue(inst, S, module);
903 }
904 break;
Sean Callanan415422c2013-06-05 22:07:06 +0000905 case Instruction::SExt:
906 {
907 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000908
Sean Callanan415422c2013-06-05 22:07:06 +0000909 if (!cast_inst)
910 {
911 if (log)
912 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
913 error.SetErrorToGenericError();
914 error.SetErrorString(interpreter_internal_error);
915 return false;
916 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000917
Sean Callanan415422c2013-06-05 22:07:06 +0000918 Value *source = cast_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000919
Sean Callanan415422c2013-06-05 22:07:06 +0000920 lldb_private::Scalar S;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000921
Sean Callanan415422c2013-06-05 22:07:06 +0000922 if (!frame.EvaluateValue(S, source, module))
923 {
924 if (log)
925 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
926 error.SetErrorToGenericError();
927 error.SetErrorString(bad_value_error);
928 return false;
929 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000930
Sean Callanan415422c2013-06-05 22:07:06 +0000931 S.MakeSigned();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000932
Sean Callanan415422c2013-06-05 22:07:06 +0000933 lldb_private::Scalar S_signextend(S.SLongLong());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000934
Sean Callanan415422c2013-06-05 22:07:06 +0000935 frame.AssignValue(inst, S_signextend, module);
936 }
937 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000938 case Instruction::Br:
939 {
940 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000941
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000942 if (!br_inst)
943 {
944 if (log)
945 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
946 error.SetErrorToGenericError();
947 error.SetErrorString(interpreter_internal_error);
948 return false;
949 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000950
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000951 if (br_inst->isConditional())
952 {
953 Value *condition = br_inst->getCondition();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000954
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000955 lldb_private::Scalar C;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000956
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000957 if (!frame.EvaluateValue(C, condition, module))
958 {
959 if (log)
960 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
961 error.SetErrorToGenericError();
962 error.SetErrorString(bad_value_error);
963 return false;
964 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000965
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000966 if (C.GetRawBits64(0))
967 frame.Jump(br_inst->getSuccessor(0));
968 else
969 frame.Jump(br_inst->getSuccessor(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000970
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000971 if (log)
972 {
973 log->Printf("Interpreted a BrInst with a condition");
974 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
975 }
976 }
977 else
978 {
979 frame.Jump(br_inst->getSuccessor(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000980
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000981 if (log)
982 {
983 log->Printf("Interpreted a BrInst with no condition");
984 }
985 }
986 }
987 continue;
988 case Instruction::GetElementPtr:
989 {
990 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000991
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000992 if (!gep_inst)
993 {
994 if (log)
995 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
996 error.SetErrorToGenericError();
997 error.SetErrorString(interpreter_internal_error);
998 return false;
999 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001000
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001001 const Value *pointer_operand = gep_inst->getPointerOperand();
1002 Type *pointer_type = pointer_operand->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001003
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001004 lldb_private::Scalar P;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001005
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001006 if (!frame.EvaluateValue(P, pointer_operand, module))
1007 {
1008 if (log)
1009 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1010 error.SetErrorToGenericError();
1011 error.SetErrorString(bad_value_error);
1012 return false;
1013 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001014
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001015 typedef SmallVector <Value *, 8> IndexVector;
1016 typedef IndexVector::iterator IndexIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001017
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001018 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1019 gep_inst->idx_end());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001020
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001021 SmallVector <Value *, 8> const_indices;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001022
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001023 for (IndexIterator ii = indices.begin(), ie = indices.end();
1024 ii != ie;
1025 ++ii)
1026 {
1027 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001028
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001029 if (!constant_index)
1030 {
1031 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001032
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001033 if (!frame.EvaluateValue(I, *ii, module))
1034 {
1035 if (log)
1036 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1037 error.SetErrorToGenericError();
1038 error.SetErrorString(bad_value_error);
1039 return false;
1040 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001041
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001042 if (log)
1043 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001044
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001045 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1046 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001047
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001048 const_indices.push_back(constant_index);
1049 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001050
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001051 uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001052
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001053 lldb_private::Scalar Poffset = P + offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001054
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001055 frame.AssignValue(inst, Poffset, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001056
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001057 if (log)
1058 {
1059 log->Printf("Interpreted a GetElementPtrInst");
1060 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1061 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1062 }
1063 }
1064 break;
1065 case Instruction::ICmp:
1066 {
1067 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001068
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001069 if (!icmp_inst)
1070 {
1071 if (log)
1072 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1073 error.SetErrorToGenericError();
1074 error.SetErrorString(interpreter_internal_error);
1075 return false;
1076 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001077
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001078 CmpInst::Predicate predicate = icmp_inst->getPredicate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001079
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001080 Value *lhs = inst->getOperand(0);
1081 Value *rhs = inst->getOperand(1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001082
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001083 lldb_private::Scalar L;
1084 lldb_private::Scalar R;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001086 if (!frame.EvaluateValue(L, lhs, module))
1087 {
1088 if (log)
1089 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1090 error.SetErrorToGenericError();
1091 error.SetErrorString(bad_value_error);
1092 return false;
1093 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001094
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001095 if (!frame.EvaluateValue(R, rhs, module))
1096 {
1097 if (log)
1098 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1099 error.SetErrorToGenericError();
1100 error.SetErrorString(bad_value_error);
1101 return false;
1102 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001103
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001104 lldb_private::Scalar result;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001105
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001106 switch (predicate)
1107 {
1108 default:
1109 return false;
1110 case CmpInst::ICMP_EQ:
1111 result = (L == R);
1112 break;
1113 case CmpInst::ICMP_NE:
1114 result = (L != R);
1115 break;
1116 case CmpInst::ICMP_UGT:
1117 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1118 break;
1119 case CmpInst::ICMP_UGE:
1120 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1121 break;
1122 case CmpInst::ICMP_ULT:
1123 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1124 break;
1125 case CmpInst::ICMP_ULE:
1126 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1127 break;
1128 case CmpInst::ICMP_SGT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001129 L.MakeSigned();
1130 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001131 result = (L > R);
1132 break;
1133 case CmpInst::ICMP_SGE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001134 L.MakeSigned();
1135 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001136 result = (L >= R);
1137 break;
1138 case CmpInst::ICMP_SLT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001139 L.MakeSigned();
1140 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001141 result = (L < R);
1142 break;
1143 case CmpInst::ICMP_SLE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001144 L.MakeSigned();
1145 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001146 result = (L <= R);
1147 break;
1148 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001149
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001150 frame.AssignValue(inst, result, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001151
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001152 if (log)
1153 {
1154 log->Printf("Interpreted an ICmpInst");
1155 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1156 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1157 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1158 }
1159 }
1160 break;
1161 case Instruction::IntToPtr:
1162 {
1163 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001164
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001165 if (!int_to_ptr_inst)
1166 {
1167 if (log)
1168 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1169 error.SetErrorToGenericError();
1170 error.SetErrorString(interpreter_internal_error);
1171 return false;
1172 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001173
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001174 Value *src_operand = int_to_ptr_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001175
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001176 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001177
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001178 if (!frame.EvaluateValue(I, src_operand, module))
1179 {
1180 if (log)
1181 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1182 error.SetErrorToGenericError();
1183 error.SetErrorString(bad_value_error);
1184 return false;
1185 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001186
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001187 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001188
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001189 if (log)
1190 {
1191 log->Printf("Interpreted an IntToPtr");
1192 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1193 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1194 }
1195 }
1196 break;
1197 case Instruction::PtrToInt:
1198 {
1199 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001200
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001201 if (!ptr_to_int_inst)
1202 {
1203 if (log)
1204 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1205 error.SetErrorToGenericError();
1206 error.SetErrorString(interpreter_internal_error);
1207 return false;
1208 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001209
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001210 Value *src_operand = ptr_to_int_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001211
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001212 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001213
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001214 if (!frame.EvaluateValue(I, src_operand, module))
1215 {
1216 if (log)
1217 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1218 error.SetErrorToGenericError();
1219 error.SetErrorString(bad_value_error);
1220 return false;
1221 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001222
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001223 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001224
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001225 if (log)
1226 {
1227 log->Printf("Interpreted a PtrToInt");
1228 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1229 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1230 }
1231 }
1232 break;
Sean Callanan8c46bac2013-10-11 19:45:00 +00001233 case Instruction::Trunc:
1234 {
1235 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001236
Sean Callanan8c46bac2013-10-11 19:45:00 +00001237 if (!trunc_inst)
1238 {
1239 if (log)
1240 log->Printf("getOpcode() returns Trunc, but instruction is not a TruncInst");
1241 error.SetErrorToGenericError();
1242 error.SetErrorString(interpreter_internal_error);
1243 return false;
1244 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001245
Sean Callanan8c46bac2013-10-11 19:45:00 +00001246 Value *src_operand = trunc_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001247
Sean Callanan8c46bac2013-10-11 19:45:00 +00001248 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001249
Sean Callanan8c46bac2013-10-11 19:45:00 +00001250 if (!frame.EvaluateValue(I, src_operand, module))
1251 {
1252 if (log)
1253 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1254 error.SetErrorToGenericError();
1255 error.SetErrorString(bad_value_error);
1256 return false;
1257 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001258
Sean Callanan8c46bac2013-10-11 19:45:00 +00001259 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001260
Sean Callanan8c46bac2013-10-11 19:45:00 +00001261 if (log)
1262 {
1263 log->Printf("Interpreted a Trunc");
1264 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1265 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1266 }
1267 }
1268 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001269 case Instruction::Load:
1270 {
1271 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001272
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001273 if (!load_inst)
1274 {
1275 if (log)
1276 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1277 error.SetErrorToGenericError();
1278 error.SetErrorString(interpreter_internal_error);
1279 return false;
1280 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001281
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001282 // The semantics of Load are:
1283 // Create a region D that will contain the loaded data
1284 // Resolve the region P containing a pointer
1285 // Dereference P to get the region R that the data should be loaded from
1286 // Transfer a unit of type type(D) from R to D
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001287
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001288 const Value *pointer_operand = load_inst->getPointerOperand();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001289
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001290 Type *pointer_ty = pointer_operand->getType();
1291 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1292 if (!pointer_ptr_ty)
1293 {
1294 if (log)
1295 log->Printf("getPointerOperand()->getType() is not a PointerType");
1296 error.SetErrorToGenericError();
1297 error.SetErrorString(interpreter_internal_error);
1298 return false;
1299 }
1300 Type *target_ty = pointer_ptr_ty->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001301
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001302 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1303 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001304
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001305 if (D == LLDB_INVALID_ADDRESS)
1306 {
1307 if (log)
1308 log->Printf("LoadInst's value doesn't resolve to anything");
1309 error.SetErrorToGenericError();
1310 error.SetErrorString(bad_value_error);
1311 return false;
1312 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001313
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001314 if (P == LLDB_INVALID_ADDRESS)
1315 {
1316 if (log)
1317 log->Printf("LoadInst's pointer doesn't resolve to anything");
1318 error.SetErrorToGenericError();
1319 error.SetErrorString(bad_value_error);
1320 return false;
1321 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001322
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001323 lldb::addr_t R;
1324 lldb_private::Error read_error;
1325 memory_map.ReadPointerFromMemory(&R, P, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001326
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001327 if (!read_error.Success())
1328 {
1329 if (log)
1330 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1331 error.SetErrorToGenericError();
1332 error.SetErrorString(memory_read_error);
1333 return false;
1334 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001335
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001336 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1337 lldb_private::DataBufferHeap buffer(target_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001338
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001339 read_error.Clear();
1340 memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
1341 if (!read_error.Success())
1342 {
1343 if (log)
1344 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1345 error.SetErrorToGenericError();
1346 error.SetErrorString(memory_read_error);
1347 return false;
1348 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001349
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001350 lldb_private::Error write_error;
1351 memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1352 if (!write_error.Success())
1353 {
1354 if (log)
1355 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1356 error.SetErrorToGenericError();
1357 error.SetErrorString(memory_read_error);
1358 return false;
1359 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001360
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001361 if (log)
1362 {
1363 log->Printf("Interpreted a LoadInst");
Matt Kopecef143712013-06-03 18:00:07 +00001364 log->Printf(" P : 0x%" PRIx64, P);
1365 log->Printf(" R : 0x%" PRIx64, R);
1366 log->Printf(" D : 0x%" PRIx64, D);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001367 }
1368 }
1369 break;
1370 case Instruction::Ret:
1371 {
1372 return true;
1373 }
1374 case Instruction::Store:
1375 {
1376 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001377
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001378 if (!store_inst)
1379 {
1380 if (log)
1381 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1382 error.SetErrorToGenericError();
1383 error.SetErrorString(interpreter_internal_error);
1384 return false;
1385 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001386
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001387 // The semantics of Store are:
1388 // Resolve the region D containing the data to be stored
1389 // Resolve the region P containing a pointer
1390 // Dereference P to get the region R that the data should be stored in
1391 // Transfer a unit of type type(D) from D to R
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001392
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001393 const Value *value_operand = store_inst->getValueOperand();
1394 const Value *pointer_operand = store_inst->getPointerOperand();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001395
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001396 Type *pointer_ty = pointer_operand->getType();
1397 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1398 if (!pointer_ptr_ty)
1399 return false;
1400 Type *target_ty = pointer_ptr_ty->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001401
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001402 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1403 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001404
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001405 if (D == LLDB_INVALID_ADDRESS)
1406 {
1407 if (log)
1408 log->Printf("StoreInst's value doesn't resolve to anything");
1409 error.SetErrorToGenericError();
1410 error.SetErrorString(bad_value_error);
1411 return false;
1412 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001413
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001414 if (P == LLDB_INVALID_ADDRESS)
1415 {
1416 if (log)
1417 log->Printf("StoreInst's pointer doesn't resolve to anything");
1418 error.SetErrorToGenericError();
1419 error.SetErrorString(bad_value_error);
1420 return false;
1421 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001422
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001423 lldb::addr_t R;
1424 lldb_private::Error read_error;
1425 memory_map.ReadPointerFromMemory(&R, P, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001426
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001427 if (!read_error.Success())
1428 {
1429 if (log)
1430 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1431 error.SetErrorToGenericError();
1432 error.SetErrorString(memory_read_error);
1433 return false;
1434 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001435
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001436 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1437 lldb_private::DataBufferHeap buffer(target_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001439 read_error.Clear();
1440 memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
1441 if (!read_error.Success())
1442 {
1443 if (log)
1444 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1445 error.SetErrorToGenericError();
1446 error.SetErrorString(memory_read_error);
1447 return false;
1448 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001449
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001450 lldb_private::Error write_error;
1451 memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
1452 if (!write_error.Success())
1453 {
1454 if (log)
1455 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1456 error.SetErrorToGenericError();
Sean Callanan49630e72013-04-20 02:39:24 +00001457 error.SetErrorString(memory_write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001458 return false;
1459 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001460
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001461 if (log)
1462 {
1463 log->Printf("Interpreted a StoreInst");
Matt Kopecef143712013-06-03 18:00:07 +00001464 log->Printf(" D : 0x%" PRIx64, D);
1465 log->Printf(" P : 0x%" PRIx64, P);
1466 log->Printf(" R : 0x%" PRIx64, R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001467 }
1468 }
1469 break;
Ewan Crawford90ff7912015-07-14 10:56:58 +00001470 case Instruction::Call:
1471 {
1472 const CallInst *call_inst = dyn_cast<CallInst>(inst);
1473
1474 if (!call_inst)
1475 {
1476 if (log)
1477 log->Printf("getOpcode() returns %s, but instruction is not a CallInst", inst->getOpcodeName());
1478 error.SetErrorToGenericError();
1479 error.SetErrorString(interpreter_internal_error);
1480 return false;
1481 }
1482
1483 if (CanIgnoreCall(call_inst))
1484 break;
1485
1486 // Get the return type
1487 llvm::Type *returnType = call_inst->getType();
1488 if (returnType == nullptr)
1489 {
1490 error.SetErrorToGenericError();
1491 error.SetErrorString("unable to access return type");
1492 return false;
1493 }
1494
1495 // Work with void, integer and pointer return types
1496 if (!returnType->isVoidTy() &&
1497 !returnType->isIntegerTy() &&
1498 !returnType->isPointerTy())
1499 {
1500 error.SetErrorToGenericError();
1501 error.SetErrorString("return type is not supported");
1502 return false;
1503 }
1504
1505 // Check we can actually get a thread
1506 if (exe_ctx.GetThreadPtr() == nullptr)
1507 {
1508 error.SetErrorToGenericError();
1509 error.SetErrorStringWithFormat("unable to acquire thread");
1510 return false;
1511 }
1512
1513 // Make sure we have a valid process
1514 if (!exe_ctx.GetProcessPtr())
1515 {
1516 error.SetErrorToGenericError();
1517 error.SetErrorStringWithFormat("unable to get the process");
1518 return false;
1519 }
1520
1521 // Find the address of the callee function
1522 lldb_private::Scalar I;
1523 const llvm::Value *val = call_inst->getCalledValue();
1524
1525 if (!frame.EvaluateValue(I, val, module))
1526 {
1527 error.SetErrorToGenericError();
1528 error.SetErrorString("unable to get address of function");
1529 return false;
1530 }
1531 lldb_private::Address funcAddr(I.ULongLong(LLDB_INVALID_ADDRESS));
1532
1533 lldb_private::StreamString error_stream;
1534 lldb_private::EvaluateExpressionOptions options;
1535
1536 // We generally receive a function pointer which we must dereference
1537 llvm::Type* prototype = val->getType();
1538 if (!prototype->isPointerTy())
1539 {
1540 error.SetErrorToGenericError();
1541 error.SetErrorString("call need function pointer");
1542 return false;
1543 }
1544
1545 // Dereference the function pointer
1546 prototype = prototype->getPointerElementType();
1547 if (!(prototype->isFunctionTy() || prototype->isFunctionVarArg()))
1548 {
1549 error.SetErrorToGenericError();
1550 error.SetErrorString("call need function pointer");
1551 return false;
1552 }
1553
1554 // Find number of arguments
1555 const int numArgs = call_inst->getNumArgOperands();
1556
1557 // We work with a fixed array of 16 arguments which is our upper limit
1558 static lldb_private::ABI::CallArgument rawArgs[16];
1559 if (numArgs >= 16)
1560 {
1561 error.SetErrorToGenericError();
1562 error.SetErrorStringWithFormat("function takes too many arguments");
1563 return false;
1564 }
1565
1566 // Push all function arguments to the argument list that will
1567 // be passed to the call function thread plan
1568 for (int i = 0; i < numArgs; i++)
1569 {
1570 // Get details of this argument
1571 llvm::Value *arg_op = call_inst->getArgOperand(i);
1572 llvm::Type *arg_ty = arg_op->getType();
1573
1574 // Ensure that this argument is an supported type
1575 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy())
1576 {
1577 error.SetErrorToGenericError();
1578 error.SetErrorStringWithFormat("argument %d must be integer type", i);
1579 return false;
1580 }
1581
1582 // Extract the arguments value
1583 lldb_private::Scalar tmp_op = 0;
1584 if (!frame.EvaluateValue(tmp_op, arg_op, module))
1585 {
1586 error.SetErrorToGenericError();
1587 error.SetErrorStringWithFormat("unable to evaluate argument %d", i);
1588 return false;
1589 }
1590
1591 // Check if this is a string literal or constant string pointer
1592 if (arg_ty->isPointerTy())
1593 {
1594 // Pointer to just one type
1595 assert(arg_ty->getNumContainedTypes() == 1);
1596
1597 lldb::addr_t addr = tmp_op.ULongLong();
1598 size_t dataSize = 0;
1599
1600 if (memory_map.GetAllocSize(addr, dataSize))
1601 {
1602 // Create the required buffer
1603 rawArgs[i].size = dataSize;
1604 rawArgs[i].data_ap.reset(new uint8_t[dataSize + 1]);
1605
1606 // Read string from host memory
1607 memory_map.ReadMemory(rawArgs[i].data_ap.get(), addr, dataSize, error);
1608 if (error.Fail())
1609 {
1610 assert(!"we have failed to read the string from memory");
1611 return false;
1612 }
1613 // Add null terminator
1614 rawArgs[i].data_ap[dataSize] = '\0';
1615 rawArgs[i].type = lldb_private::ABI::CallArgument::HostPointer;
1616 }
1617 else
1618 {
1619 assert(!"unable to locate host data for transfer to device");
1620 return false;
1621 }
1622 }
1623 else /* if ( arg_ty->isPointerTy() ) */
1624 {
1625 rawArgs[i].type = lldb_private::ABI::CallArgument::TargetValue;
1626 // Get argument size in bytes
1627 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8;
1628 // Push value into argument list for thread plan
1629 rawArgs[i].value = tmp_op.ULongLong();
1630 }
1631
1632 }
1633
1634 // Pack the arguments into an llvm::array
1635 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1636
1637 // Setup a thread plan to call the target function
1638 lldb::ThreadPlanSP call_plan_sp
1639 (
1640 new lldb_private::ThreadPlanCallFunctionUsingABI
1641 (
1642 exe_ctx.GetThreadRef(),
1643 funcAddr,
1644 *prototype,
1645 *returnType,
1646 args,
1647 options
1648 )
1649 );
1650
1651 // Check if the plan is valid
1652 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&error_stream))
1653 {
1654 error.SetErrorToGenericError();
1655 error.SetErrorStringWithFormat("unable to make ThreadPlanCallFunctionUsingABI for 0x%llx", I.ULongLong());
1656 return false;
1657 }
1658
1659 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
1660
1661 // Execute the actual function call thread plan
1662 lldb::ExpressionResults res = exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, error_stream);
1663
1664 // Check that the thread plan completed successfully
1665 if (res != lldb::ExpressionResults::eExpressionCompleted)
1666 {
1667 error.SetErrorToGenericError();
1668 error.SetErrorStringWithFormat("ThreadPlanCallFunctionUsingABI failed");
1669 return false;
1670 }
1671
1672 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
1673
1674 // Void return type
1675 if (returnType->isVoidTy())
1676 {
1677 // Cant assign to void types, so we leave the frame untouched
1678 }
1679 else
1680 // Integer or pointer return type
1681 if (returnType->isIntegerTy() || returnType->isPointerTy())
1682 {
1683 // Get the encapsulated return value
1684 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1685
1686 lldb_private::Scalar returnVal = -1;
1687 lldb_private::ValueObject *vobj = retVal.get();
1688
1689 // Check if the return value is valid
1690 if (vobj == nullptr || retVal.empty())
1691 {
1692 error.SetErrorToGenericError();
1693 error.SetErrorStringWithFormat("unable to get the return value");
1694 return false;
1695 }
1696
1697 // Extract the return value as a integer
1698 lldb_private::Value & value = vobj->GetValue();
1699 returnVal = value.GetScalar();
1700
1701 // Push the return value as the result
1702 frame.AssignValue(inst, returnVal, module);
1703 }
1704 }
1705 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001706 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001707
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001708 ++frame.m_ii;
1709 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001710
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001711 if (num_insts >= 4096)
1712 {
1713 error.SetErrorToGenericError();
1714 error.SetErrorString(infinite_loop_error);
1715 return false;
1716 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001717
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001718 return false;
1719}