blob: a0d7a163a92b6de4d1559ff6f5b7d834d59b55e5 [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 Callanan579e70c2016-03-19 00:03:59 +000010#include "lldb/Expression/IRInterpreter.h"
Ted Woodward7071c5fd2016-03-01 21:53:26 +000011#include "lldb/Core/ConstString.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000012#include "lldb/Core/DataExtractor.h"
13#include "lldb/Core/Error.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000014#include "lldb/Core/Log.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000015#include "lldb/Core/Module.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000016#include "lldb/Core/ModuleSpec.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000017#include "lldb/Core/Scalar.h"
18#include "lldb/Core/StreamString.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000019#include "lldb/Core/ValueObject.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000020#include "lldb/Expression/DiagnosticManager.h"
Ted Woodward7071c5fd2016-03-01 21:53:26 +000021#include "lldb/Expression/IRExecutionUnit.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000022#include "lldb/Expression/IRMemoryMap.h"
Sean Callanan46fc0062013-09-17 18:26:42 +000023#include "lldb/Host/Endian.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000024
Ewan Crawford90ff7912015-07-14 10:56:58 +000025#include "lldb/Target/ABI.h"
26#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
30#include "lldb/Target/ThreadPlanCallFunctionUsingABI.h"
31
Chandler Carruth1e157582013-01-02 12:20:07 +000032#include "llvm/IR/Constants.h"
Sean Callananfefe43c2013-04-25 18:55:45 +000033#include "llvm/IR/DataLayout.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000034#include "llvm/IR/Function.h"
35#include "llvm/IR/Instructions.h"
Sean Callanan576a4372014-03-25 19:33:15 +000036#include "llvm/IR/Intrinsics.h"
Ewan Crawford90ff7912015-07-14 10:56:58 +000037#include "llvm/IR/LLVMContext.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000038#include "llvm/IR/Module.h"
Eduard Burtescud05b8992016-01-22 03:43:23 +000039#include "llvm/IR/Operator.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000040#include "llvm/Support/raw_ostream.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000041
42#include <map>
43
44using namespace llvm;
45
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000046static std::string
Sean Callanan3bfdaa22011-09-15 02:13:07 +000047PrintValue(const Value *value, bool truncate = false)
48{
49 std::string s;
50 raw_string_ostream rso(s);
51 value->print(rso);
52 rso.flush();
53 if (truncate)
54 s.resize(s.length() - 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000055
Sean Callanan3bfdaa22011-09-15 02:13:07 +000056 size_t offset;
57 while ((offset = s.find('\n')) != s.npos)
58 s.erase(offset, 1);
59 while (s[0] == ' ' || s[0] == '\t')
60 s.erase(0, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000061
Sean Callanan3bfdaa22011-09-15 02:13:07 +000062 return s;
63}
64
65static std::string
66PrintType(const Type *type, bool truncate = false)
67{
68 std::string s;
69 raw_string_ostream rso(s);
70 type->print(rso);
71 rso.flush();
72 if (truncate)
73 s.resize(s.length() - 1);
74 return s;
75}
76
Sean Callanan576a4372014-03-25 19:33:15 +000077static bool
78CanIgnoreCall (const CallInst *call)
79{
80 const llvm::Function *called_function = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000081
Sean Callanan576a4372014-03-25 19:33:15 +000082 if (!called_function)
83 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000084
Sean Callanan576a4372014-03-25 19:33:15 +000085 if (called_function->isIntrinsic())
86 {
87 switch (called_function->getIntrinsicID())
88 {
89 default:
90 break;
91 case llvm::Intrinsic::dbg_declare:
92 case llvm::Intrinsic::dbg_value:
93 return true;
94 }
95 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000096
Sean Callanan576a4372014-03-25 19:33:15 +000097 return false;
98}
99
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000100class InterpreterStackFrame
101{
102public:
Sean Callanan08052af2013-04-17 07:50:58 +0000103 typedef std::map <const Value*, lldb::addr_t> ValueMap;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000104
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000105 ValueMap m_values;
Micah Villmow8468dbe2012-10-08 16:28:57 +0000106 DataLayout &m_target_data;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000107 lldb_private::IRExecutionUnit &m_execution_unit;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000108 const BasicBlock *m_bb;
109 BasicBlock::const_iterator m_ii;
110 BasicBlock::const_iterator m_ie;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000111
Sean Callanan1582ee62013-04-18 22:06:33 +0000112 lldb::addr_t m_frame_process_address;
113 size_t m_frame_size;
114 lldb::addr_t m_stack_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000115
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000116 lldb::ByteOrder m_byte_order;
117 size_t m_addr_byte_size;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000118
Micah Villmow8468dbe2012-10-08 16:28:57 +0000119 InterpreterStackFrame (DataLayout &target_data,
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000120 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanandf565402013-04-27 02:19:33 +0000121 lldb::addr_t stack_frame_bottom,
122 lldb::addr_t stack_frame_top) :
Daniel Dunbara08823f2011-10-31 22:50:49 +0000123 m_target_data (target_data),
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000124 m_execution_unit (execution_unit)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000125 {
126 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Sean Callanan95769bf2012-10-11 22:00:52 +0000127 m_addr_byte_size = (target_data.getPointerSize(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000128
Sean Callanandf565402013-04-27 02:19:33 +0000129 m_frame_process_address = stack_frame_bottom;
130 m_frame_size = stack_frame_top - stack_frame_bottom;
131 m_stack_pointer = stack_frame_top;
Sean Callanan1582ee62013-04-18 22:06:33 +0000132 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000133
Sean Callanan1582ee62013-04-18 22:06:33 +0000134 ~InterpreterStackFrame ()
135 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000136 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000137
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000138 void Jump (const BasicBlock *bb)
139 {
140 m_bb = bb;
141 m_ii = m_bb->begin();
142 m_ie = m_bb->end();
143 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000144
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000145 std::string SummarizeValue (const Value *value)
146 {
147 lldb_private::StreamString ss;
148
149 ss.Printf("%s", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000150
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000151 ValueMap::iterator i = m_values.find(value);
152
153 if (i != m_values.end())
154 {
Sean Callanan08052af2013-04-17 07:50:58 +0000155 lldb::addr_t addr = i->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callanan08052af2013-04-17 07:50:58 +0000157 ss.Printf(" 0x%llx", (unsigned long long)addr);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000158 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000159
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000160 return ss.GetString();
161 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000163 bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
164 {
165 size_t type_size = m_target_data.getTypeStoreSize(type);
166
167 switch (type_size)
168 {
169 case 1:
170 scalar = (uint8_t)u64value;
171 break;
172 case 2:
173 scalar = (uint16_t)u64value;
174 break;
175 case 4:
176 scalar = (uint32_t)u64value;
177 break;
178 case 8:
179 scalar = (uint64_t)u64value;
180 break;
181 default:
182 return false;
183 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000184
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000185 return true;
186 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000187
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000188 bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
189 {
190 const Constant *constant = dyn_cast<Constant>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000191
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000192 if (constant)
193 {
Sean Callanan415422c2013-06-05 22:07:06 +0000194 APInt value_apint;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000195
Sean Callanan415422c2013-06-05 22:07:06 +0000196 if (!ResolveConstantValue(value_apint, constant))
197 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000198
Sean Callanan415422c2013-06-05 22:07:06 +0000199 return AssignToMatchType(scalar, value_apint.getLimitedValue(), value->getType());
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000200 }
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());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000205
Sean Callanan08052af2013-04-17 07:50:58 +0000206 lldb_private::DataExtractor value_extractor;
207 lldb_private::Error extract_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000209 m_execution_unit.GetMemoryData(value_extractor, process_address, value_size, extract_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000210
Sean Callanan08052af2013-04-17 07:50:58 +0000211 if (!extract_error.Success())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000212 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000213
Greg Claytonc7bece562013-01-25 18:06:21 +0000214 lldb::offset_t offset = 0;
Sean Callanan544053e2013-06-06 21:14:35 +0000215 if (value_size == 1 || value_size == 2 || value_size == 4 || value_size == 8)
Greg Clayton78e44bd2013-04-25 00:57:05 +0000216 {
217 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
218 return AssignToMatchType(scalar, u64value, value->getType());
219 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000220 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000221
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000222 return false;
223 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000225 bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
226 {
Sean Callanan08052af2013-04-17 07:50:58 +0000227 lldb::addr_t process_address = ResolveValue (value, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000228
Sean Callanan08052af2013-04-17 07:50:58 +0000229 if (process_address == LLDB_INVALID_ADDRESS)
230 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000231
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000232 lldb_private::Scalar cast_scalar;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000233
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000234 if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
235 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000236
Sean Callanan08052af2013-04-17 07:50:58 +0000237 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Sean Callanan08052af2013-04-17 07:50:58 +0000239 lldb_private::DataBufferHeap buf(value_byte_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Sean Callanan08052af2013-04-17 07:50:58 +0000241 lldb_private::Error get_data_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000242
Sean Callanan08052af2013-04-17 07:50:58 +0000243 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000244 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000245
Sean Callanan08052af2013-04-17 07:50:58 +0000246 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000247
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000248 m_execution_unit.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callanan08052af2013-04-17 07:50:58 +0000250 return write_error.Success();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000251 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Sean Callanan94a9a392012-02-08 01:27:49 +0000253 bool ResolveConstantValue (APInt &value, const Constant *constant)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000254 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000255 switch (constant->getValueID())
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000256 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000257 default:
258 break;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000259 case Value::FunctionVal:
260 if (const Function *constant_func = dyn_cast<Function>(constant))
261 {
262 lldb_private::ConstString name(constant_func->getName());
263 lldb::addr_t addr = m_execution_unit.FindSymbol(name);
264 if (addr == LLDB_INVALID_ADDRESS)
265 return false;
266 value = APInt(m_target_data.getPointerSizeInBits(), addr);
267 return true;
268 }
269 break;
Sean Callanan1582ee62013-04-18 22:06:33 +0000270 case Value::ConstantIntVal:
271 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
Sean Callanan80c48c12011-10-21 05:18:02 +0000272 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000273 value = constant_int->getValue();
274 return true;
275 }
276 break;
277 case Value::ConstantFPVal:
278 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
279 {
280 value = constant_fp->getValueAPF().bitcastToAPInt();
281 return true;
282 }
283 break;
284 case Value::ConstantExprVal:
285 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
286 {
287 switch (constant_expr->getOpcode())
Sean Callanan94a9a392012-02-08 01:27:49 +0000288 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000289 default:
Sean Callanan94a9a392012-02-08 01:27:49 +0000290 return false;
Sean Callanan1582ee62013-04-18 22:06:33 +0000291 case Instruction::IntToPtr:
292 case Instruction::PtrToInt:
293 case Instruction::BitCast:
294 return ResolveConstantValue(value, constant_expr->getOperand(0));
295 case Instruction::GetElementPtr:
296 {
297 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
298 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000299
Sean Callanan1582ee62013-04-18 22:06:33 +0000300 Constant *base = dyn_cast<Constant>(*op_cursor);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000301
Sean Callanan1582ee62013-04-18 22:06:33 +0000302 if (!base)
303 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000304
Sean Callanan1582ee62013-04-18 22:06:33 +0000305 if (!ResolveConstantValue(value, base))
306 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000307
Sean Callanan1582ee62013-04-18 22:06:33 +0000308 op_cursor++;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000309
Sean Callanan1582ee62013-04-18 22:06:33 +0000310 if (op_cursor == op_end)
311 return true; // no offset to apply!
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Sean Callanan1582ee62013-04-18 22:06:33 +0000313 SmallVector <Value *, 8> indices (op_cursor, op_end);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000314
Eduard Burtescud05b8992016-01-22 03:43:23 +0000315 Type *src_elem_ty = cast<GEPOperator>(constant_expr)->getSourceElementType();
316 uint64_t offset = m_target_data.getIndexedOffsetInType(src_elem_ty, indices);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000317
Sean Callanan1582ee62013-04-18 22:06:33 +0000318 const bool is_signed = true;
319 value += APInt(value.getBitWidth(), offset, is_signed);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Sean Callanan1582ee62013-04-18 22:06:33 +0000321 return true;
322 }
Sean Callanan94a9a392012-02-08 01:27:49 +0000323 }
Sean Callanan80c48c12011-10-21 05:18:02 +0000324 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000325 break;
326 case Value::ConstantPointerNullVal:
327 if (isa<ConstantPointerNull>(constant))
328 {
329 value = APInt(m_target_data.getPointerSizeInBits(), 0);
330 return true;
331 }
332 break;
333 }
334 return false;
335 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Sean Callanan1582ee62013-04-18 22:06:33 +0000337 bool MakeArgument(const Argument *value, uint64_t address)
338 {
339 lldb::addr_t data_address = Malloc(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000340
Sean Callanan1582ee62013-04-18 22:06:33 +0000341 if (data_address == LLDB_INVALID_ADDRESS)
342 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000343
Sean Callanan1582ee62013-04-18 22:06:33 +0000344 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000345
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000346 m_execution_unit.WritePointerToMemory(data_address, address, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Sean Callanan1582ee62013-04-18 22:06:33 +0000348 if (!write_error.Success())
349 {
350 lldb_private::Error free_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000351 m_execution_unit.Free(data_address, free_error);
Sean Callanan1582ee62013-04-18 22:06:33 +0000352 return false;
Sean Callanan80c48c12011-10-21 05:18:02 +0000353 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000354
Sean Callanan1582ee62013-04-18 22:06:33 +0000355 m_values[value] = data_address;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000356
Sean Callanan1582ee62013-04-18 22:06:33 +0000357 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
358
359 if (log)
360 {
361 log->Printf("Made an allocation for argument %s", PrintValue(value).c_str());
362 log->Printf(" Data region : %llx", (unsigned long long)address);
363 log->Printf(" Ref region : %llx", (unsigned long long)data_address);
364 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000365
Sean Callanan1582ee62013-04-18 22:06:33 +0000366 return true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000367 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000368
Sean Callanan08052af2013-04-17 07:50:58 +0000369 bool ResolveConstant (lldb::addr_t process_address, const Constant *constant)
Sean Callanan94a9a392012-02-08 01:27:49 +0000370 {
371 APInt resolved_value;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000372
Sean Callanan94a9a392012-02-08 01:27:49 +0000373 if (!ResolveConstantValue(resolved_value, constant))
374 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000375
Sean Callanan46fc0062013-09-17 18:26:42 +0000376 lldb_private::StreamString buffer (lldb_private::Stream::eBinary,
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000377 m_execution_unit.GetAddressByteSize(),
378 m_execution_unit.GetByteOrder());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000379
Sean Callanan94a9a392012-02-08 01:27:49 +0000380 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000381
Sean Callanan46fc0062013-09-17 18:26:42 +0000382 const uint64_t *raw_data = resolved_value.getRawData();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000383
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000384 buffer.PutRawBytes(raw_data, constant_size, lldb_private::endian::InlHostByteOrder());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000385
Sean Callanan08052af2013-04-17 07:50:58 +0000386 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000387
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000388 m_execution_unit.WriteMemory(process_address, (const uint8_t*)buffer.GetData(), constant_size, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000389
Sean Callanan08052af2013-04-17 07:50:58 +0000390 return write_error.Success();
391 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000392
Sean Callanan1582ee62013-04-18 22:06:33 +0000393 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
394 {
395 lldb::addr_t ret = m_stack_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000396
Sean Callanan1582ee62013-04-18 22:06:33 +0000397 ret -= size;
398 ret -= (ret % byte_alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000399
Sean Callanan1582ee62013-04-18 22:06:33 +0000400 if (ret < m_frame_process_address)
401 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000402
Sean Callanan1582ee62013-04-18 22:06:33 +0000403 m_stack_pointer = ret;
404 return ret;
405 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000406
Sean Callanan08052af2013-04-17 07:50:58 +0000407 lldb::addr_t MallocPointer ()
408 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000409 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000410 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000411
Sean Callanan1582ee62013-04-18 22:06:33 +0000412 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000413 {
414 lldb_private::Error alloc_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000415
Sean Callanan1582ee62013-04-18 22:06:33 +0000416 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000417 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000418
Sean Callanan08052af2013-04-17 07:50:58 +0000419 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
420 {
421 size_t length = m_target_data.getTypeStoreSize(type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000422
Sean Callanan08052af2013-04-17 07:50:58 +0000423 lldb_private::DataBufferHeap buf(length, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000424
Sean Callanan08052af2013-04-17 07:50:58 +0000425 lldb_private::Error read_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000426
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000427 m_execution_unit.ReadMemory(buf.GetBytes(), addr, length, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000428
Sean Callanan08052af2013-04-17 07:50:58 +0000429 if (!read_error.Success())
430 return std::string("<couldn't read data>");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431
Sean Callanan08052af2013-04-17 07:50:58 +0000432 lldb_private::StreamString ss;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000433
Sean Callanan08052af2013-04-17 07:50:58 +0000434 for (size_t i = 0; i < length; i++)
435 {
436 if ((!(i & 0xf)) && i)
437 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
438 else
439 ss.Printf("%02hhx ", buf.GetBytes()[i]);
440 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000441
Sean Callanan08052af2013-04-17 07:50:58 +0000442 return ss.GetString();
443 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000444
Sean Callanan08052af2013-04-17 07:50:58 +0000445 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000446 {
447 ValueMap::iterator i = m_values.find(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000448
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000449 if (i != m_values.end())
450 return i->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000451
Sean Callanan1582ee62013-04-18 22:06:33 +0000452 // Fall back and allocate space [allocation type Alloca]
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000453
Sean Callanan1582ee62013-04-18 22:06:33 +0000454 lldb::addr_t data_address = Malloc(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000455
Sean Callanan1582ee62013-04-18 22:06:33 +0000456 if (const Constant *constant = dyn_cast<Constant>(value))
457 {
458 if (!ResolveConstant (data_address, constant))
459 {
460 lldb_private::Error free_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000461 m_execution_unit.Free(data_address, free_error);
Sean Callanan1582ee62013-04-18 22:06:33 +0000462 return LLDB_INVALID_ADDRESS;
463 }
464 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000465
Sean Callanan1582ee62013-04-18 22:06:33 +0000466 m_values[value] = data_address;
467 return data_address;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000468 }
469};
470
Sean Callanan175a0d02012-01-24 22:06:48 +0000471static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Sean Callanan3fa3e652013-05-02 00:33:44 +0000472static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
Greg Claytone01e07b2013-04-18 18:10:51 +0000473//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000474static const char *interpreter_internal_error = "Interpreter encountered an internal error";
475static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
476static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
477static const char *memory_write_error = "Interpreter couldn't write to memory";
478static const char *memory_read_error = "Interpreter couldn't read from memory";
479static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan44342732013-04-19 08:14:32 +0000480//static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000481
Sean Callanan8c62daf2016-02-12 21:16:58 +0000482static bool
483CanResolveConstant (llvm::Constant *constant)
484{
485 switch (constant->getValueID())
486 {
487 default:
488 return false;
489 case Value::ConstantIntVal:
490 case Value::ConstantFPVal:
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000491 case Value::FunctionVal:
Sean Callanan8c62daf2016-02-12 21:16:58 +0000492 return true;
493 case Value::ConstantExprVal:
494 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
495 {
496 switch (constant_expr->getOpcode())
497 {
498 default:
499 return false;
500 case Instruction::IntToPtr:
501 case Instruction::PtrToInt:
502 case Instruction::BitCast:
503 return CanResolveConstant(constant_expr->getOperand(0));
504 case Instruction::GetElementPtr:
505 {
506 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
507 Constant *base = dyn_cast<Constant>(*op_cursor);
508 if (!base)
509 return false;
510
511 return CanResolveConstant(base);
512 }
513 }
514 } else {
515 return false;
516 }
517 case Value::ConstantPointerNullVal:
518 return true;
519 }
520}
521
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000522bool
Sean Callanan44342732013-04-19 08:14:32 +0000523IRInterpreter::CanInterpret (llvm::Module &module,
524 llvm::Function &function,
Ewan Crawford90ff7912015-07-14 10:56:58 +0000525 lldb_private::Error &error,
526 const bool support_function_calls)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000527{
Greg Clayton5160ce52013-03-27 23:08:40 +0000528 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000529
Sean Callanan85fc8762013-06-27 01:59:51 +0000530 bool saw_function_with_body = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000531
Sean Callanan85fc8762013-06-27 01:59:51 +0000532 for (Module::iterator fi = module.begin(), fe = module.end();
533 fi != fe;
534 ++fi)
535 {
536 if (fi->begin() != fi->end())
537 {
538 if (saw_function_with_body)
539 return false;
540 saw_function_with_body = true;
541 }
542 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000543
Sean Callanan44342732013-04-19 08:14:32 +0000544 for (Function::iterator bbi = function.begin(), bbe = function.end();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000545 bbi != bbe;
546 ++bbi)
547 {
548 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
549 ii != ie;
550 ++ii)
551 {
552 switch (ii->getOpcode())
553 {
554 default:
555 {
556 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000557 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str());
Sean Callanan44342732013-04-19 08:14:32 +0000558 error.SetErrorToGenericError();
559 error.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000560 return false;
561 }
562 case Instruction::Add:
563 case Instruction::Alloca:
564 case Instruction::BitCast:
565 case Instruction::Br:
Sean Callanan576a4372014-03-25 19:33:15 +0000566 break;
567 case Instruction::Call:
568 {
569 CallInst *call_inst = dyn_cast<CallInst>(ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000570
Sean Callanan576a4372014-03-25 19:33:15 +0000571 if (!call_inst)
572 {
573 error.SetErrorToGenericError();
574 error.SetErrorString(interpreter_internal_error);
575 return false;
576 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000577
Ewan Crawford90ff7912015-07-14 10:56:58 +0000578 if (!CanIgnoreCall(call_inst) && !support_function_calls)
Sean Callanan576a4372014-03-25 19:33:15 +0000579 {
580 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000581 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str());
Sean Callanan576a4372014-03-25 19:33:15 +0000582 error.SetErrorToGenericError();
583 error.SetErrorString(unsupported_opcode_error);
584 return false;
585 }
586 }
Sean Callanan92872892014-03-25 19:47:07 +0000587 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000588 case Instruction::GetElementPtr:
589 break;
590 case Instruction::ICmp:
591 {
592 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000593
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000594 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000595 {
Sean Callanan44342732013-04-19 08:14:32 +0000596 error.SetErrorToGenericError();
597 error.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000598 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000599 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000600
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000601 switch (icmp_inst->getPredicate())
602 {
603 default:
Sean Callanan44342732013-04-19 08:14:32 +0000604 {
605 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000606 log->Printf("Unsupported ICmp predicate: %s", PrintValue(&*ii).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000607
Sean Callanan44342732013-04-19 08:14:32 +0000608 error.SetErrorToGenericError();
609 error.SetErrorString(unsupported_opcode_error);
610 return false;
611 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000612 case CmpInst::ICMP_EQ:
613 case CmpInst::ICMP_NE:
614 case CmpInst::ICMP_UGT:
615 case CmpInst::ICMP_UGE:
616 case CmpInst::ICMP_ULT:
617 case CmpInst::ICMP_ULE:
618 case CmpInst::ICMP_SGT:
619 case CmpInst::ICMP_SGE:
620 case CmpInst::ICMP_SLT:
621 case CmpInst::ICMP_SLE:
622 break;
623 }
624 }
625 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000626 case Instruction::And:
627 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000628 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000629 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000630 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000631 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000632 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000633 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000634 case Instruction::Ret:
635 case Instruction::SDiv:
Sean Callanan415422c2013-06-05 22:07:06 +0000636 case Instruction::SExt:
Sean Callanan087f4372013-01-09 22:44:41 +0000637 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000638 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000639 case Instruction::Store:
640 case Instruction::Sub:
Sean Callanan8c46bac2013-10-11 19:45:00 +0000641 case Instruction::Trunc:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000642 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000643 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000644 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000645 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000646 break;
647 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000648
Sean Callanan3fa3e652013-05-02 00:33:44 +0000649 for (int oi = 0, oe = ii->getNumOperands();
650 oi != oe;
651 ++oi)
652 {
653 Value *operand = ii->getOperand(oi);
654 Type *operand_type = operand->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000655
Sean Callanan3fa3e652013-05-02 00:33:44 +0000656 switch (operand_type->getTypeID())
657 {
658 default:
659 break;
660 case Type::VectorTyID:
661 {
662 if (log)
663 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
664 error.SetErrorString(unsupported_operand_error);
665 return false;
666 }
667 }
Sean Callanan8c62daf2016-02-12 21:16:58 +0000668
669 if (Constant *constant = llvm::dyn_cast<Constant>(operand))
670 {
671 if (!CanResolveConstant(constant))
672 {
673 if (log)
674 log->Printf("Unsupported constant: %s", PrintValue(constant).c_str());
675 error.SetErrorString(unsupported_operand_error);
676 return false;
677 }
678 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000679 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000680 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000681
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000682 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000683
Sean Callanan44342732013-04-19 08:14:32 +0000684 return true;}
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000685
686bool
687IRInterpreter::Interpret (llvm::Module &module,
688 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +0000689 llvm::ArrayRef<lldb::addr_t> args,
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000690 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanandf565402013-04-27 02:19:33 +0000691 lldb_private::Error &error,
692 lldb::addr_t stack_frame_bottom,
Ewan Crawford90ff7912015-07-14 10:56:58 +0000693 lldb::addr_t stack_frame_top,
694 lldb_private::ExecutionContext &exe_ctx)
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000695{
696 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000697
Sean Callanan1582ee62013-04-18 22:06:33 +0000698 if (log)
699 {
700 std::string s;
701 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000702
Sean Callanan1582ee62013-04-18 22:06:33 +0000703 module.print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000704
Sean Callanan1582ee62013-04-18 22:06:33 +0000705 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000706
Sean Callanan1582ee62013-04-18 22:06:33 +0000707 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
708 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000709
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000710 DataLayout data_layout(&module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000711
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000712 InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom, stack_frame_top);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000713
Sean Callanan1582ee62013-04-18 22:06:33 +0000714 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
715 {
716 error.SetErrorString("Couldn't allocate stack frame");
717 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000718
Sean Callanan1582ee62013-04-18 22:06:33 +0000719 int arg_index = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000720
Sean Callanan1582ee62013-04-18 22:06:33 +0000721 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
722 ai != ae;
723 ++ai, ++arg_index)
724 {
Chaoren Lin3ca7a3e2015-09-16 01:20:34 +0000725 if (args.size() <= static_cast<size_t>(arg_index))
Sean Callanan1582ee62013-04-18 22:06:33 +0000726 {
727 error.SetErrorString ("Not enough arguments passed in to function");
728 return false;
729 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000730
Sean Callanan1582ee62013-04-18 22:06:33 +0000731 lldb::addr_t ptr = args[arg_index];
732
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000733 frame.MakeArgument(&*ai, ptr);
Sean Callanan1582ee62013-04-18 22:06:33 +0000734 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000735
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000736 uint32_t num_insts = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000737
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000738 frame.Jump(&function.front());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000739
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000740 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
741 {
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000742 const Instruction *inst = &*frame.m_ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000743
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000744 if (log)
745 log->Printf("Interpreting %s", PrintValue(inst).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000746
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000747 switch (inst->getOpcode())
748 {
749 default:
750 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000751
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000752 case Instruction::Add:
753 case Instruction::Sub:
754 case Instruction::Mul:
755 case Instruction::SDiv:
756 case Instruction::UDiv:
757 case Instruction::SRem:
758 case Instruction::URem:
759 case Instruction::Shl:
760 case Instruction::LShr:
761 case Instruction::AShr:
762 case Instruction::And:
763 case Instruction::Or:
764 case Instruction::Xor:
765 {
766 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000767
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000768 if (!bin_op)
769 {
770 if (log)
771 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
772 error.SetErrorToGenericError();
773 error.SetErrorString(interpreter_internal_error);
774 return false;
775 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000776
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000777 Value *lhs = inst->getOperand(0);
778 Value *rhs = inst->getOperand(1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000779
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000780 lldb_private::Scalar L;
781 lldb_private::Scalar R;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000782
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000783 if (!frame.EvaluateValue(L, lhs, module))
784 {
785 if (log)
786 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
787 error.SetErrorToGenericError();
788 error.SetErrorString(bad_value_error);
789 return false;
790 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000791
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000792 if (!frame.EvaluateValue(R, rhs, module))
793 {
794 if (log)
795 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
796 error.SetErrorToGenericError();
797 error.SetErrorString(bad_value_error);
798 return false;
799 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000800
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000801 lldb_private::Scalar result;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000802
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000803 switch (inst->getOpcode())
804 {
805 default:
806 break;
807 case Instruction::Add:
808 result = L + R;
809 break;
810 case Instruction::Mul:
811 result = L * R;
812 break;
813 case Instruction::Sub:
814 result = L - R;
815 break;
816 case Instruction::SDiv:
Sean Callanan0b342b62013-05-24 20:36:56 +0000817 L.MakeSigned();
818 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000819 result = L / R;
820 break;
821 case Instruction::UDiv:
822 result = L.GetRawBits64(0) / R.GetRawBits64(1);
823 break;
824 case Instruction::SRem:
Sean Callanan0b342b62013-05-24 20:36:56 +0000825 L.MakeSigned();
826 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000827 result = L % R;
828 break;
829 case Instruction::URem:
830 result = L.GetRawBits64(0) % R.GetRawBits64(1);
831 break;
832 case Instruction::Shl:
833 result = L << R;
834 break;
835 case Instruction::AShr:
836 result = L >> R;
837 break;
838 case Instruction::LShr:
839 result = L;
840 result.ShiftRightLogical(R);
841 break;
842 case Instruction::And:
843 result = L & R;
844 break;
845 case Instruction::Or:
846 result = L | R;
847 break;
848 case Instruction::Xor:
849 result = L ^ R;
850 break;
851 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000853 frame.AssignValue(inst, result, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000854
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000855 if (log)
856 {
857 log->Printf("Interpreted a %s", inst->getOpcodeName());
858 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
859 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
860 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
861 }
862 }
863 break;
864 case Instruction::Alloca:
865 {
866 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000867
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000868 if (!alloca_inst)
869 {
870 if (log)
871 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
872 error.SetErrorToGenericError();
873 error.SetErrorString(interpreter_internal_error);
874 return false;
875 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000876
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000877 if (alloca_inst->isArrayAllocation())
878 {
879 if (log)
880 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
881 error.SetErrorToGenericError();
882 error.SetErrorString(unsupported_opcode_error);
883 return false;
884 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000885
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000886 // The semantics of Alloca are:
887 // Create a region R of virtual memory of type T, backed by a data buffer
888 // Create a region P of virtual memory of type T*, backed by a data buffer
889 // Write the virtual address of R into P
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000890
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000891 Type *T = alloca_inst->getAllocatedType();
892 Type *Tptr = alloca_inst->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000894 lldb::addr_t R = frame.Malloc(T);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000895
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000896 if (R == LLDB_INVALID_ADDRESS)
897 {
898 if (log)
899 log->Printf("Couldn't allocate memory for an AllocaInst");
900 error.SetErrorToGenericError();
901 error.SetErrorString(memory_allocation_error);
902 return false;
903 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000904
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000905 lldb::addr_t P = frame.Malloc(Tptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000906
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000907 if (P == LLDB_INVALID_ADDRESS)
908 {
909 if (log)
910 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
911 error.SetErrorToGenericError();
912 error.SetErrorString(memory_allocation_error);
913 return false;
914 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000915
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000916 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000917
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000918 execution_unit.WritePointerToMemory(P, R, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000919
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000920 if (!write_error.Success())
921 {
922 if (log)
923 log->Printf("Couldn't write the result pointer for an AllocaInst");
924 error.SetErrorToGenericError();
925 error.SetErrorString(memory_write_error);
926 lldb_private::Error free_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000927 execution_unit.Free(P, free_error);
928 execution_unit.Free(R, free_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000929 return false;
930 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000931
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000932 frame.m_values[alloca_inst] = P;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000933
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000934 if (log)
935 {
936 log->Printf("Interpreted an AllocaInst");
Matt Kopecef143712013-06-03 18:00:07 +0000937 log->Printf(" R : 0x%" PRIx64, R);
938 log->Printf(" P : 0x%" PRIx64, P);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000939 }
940 }
941 break;
942 case Instruction::BitCast:
943 case Instruction::ZExt:
944 {
945 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000946
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000947 if (!cast_inst)
948 {
949 if (log)
950 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
951 error.SetErrorToGenericError();
952 error.SetErrorString(interpreter_internal_error);
953 return false;
954 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000955
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000956 Value *source = cast_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000957
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000958 lldb_private::Scalar S;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000959
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000960 if (!frame.EvaluateValue(S, source, module))
961 {
962 if (log)
963 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
964 error.SetErrorToGenericError();
965 error.SetErrorString(bad_value_error);
966 return false;
967 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000968
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000969 frame.AssignValue(inst, S, module);
970 }
971 break;
Sean Callanan415422c2013-06-05 22:07:06 +0000972 case Instruction::SExt:
973 {
974 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000975
Sean Callanan415422c2013-06-05 22:07:06 +0000976 if (!cast_inst)
977 {
978 if (log)
979 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
980 error.SetErrorToGenericError();
981 error.SetErrorString(interpreter_internal_error);
982 return false;
983 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000984
Sean Callanan415422c2013-06-05 22:07:06 +0000985 Value *source = cast_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000986
Sean Callanan415422c2013-06-05 22:07:06 +0000987 lldb_private::Scalar S;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000988
Sean Callanan415422c2013-06-05 22:07:06 +0000989 if (!frame.EvaluateValue(S, source, module))
990 {
991 if (log)
992 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
993 error.SetErrorToGenericError();
994 error.SetErrorString(bad_value_error);
995 return false;
996 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000997
Sean Callanan415422c2013-06-05 22:07:06 +0000998 S.MakeSigned();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000999
Sean Callanan415422c2013-06-05 22:07:06 +00001000 lldb_private::Scalar S_signextend(S.SLongLong());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001001
Sean Callanan415422c2013-06-05 22:07:06 +00001002 frame.AssignValue(inst, S_signextend, module);
1003 }
1004 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001005 case Instruction::Br:
1006 {
1007 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001008
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001009 if (!br_inst)
1010 {
1011 if (log)
1012 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
1013 error.SetErrorToGenericError();
1014 error.SetErrorString(interpreter_internal_error);
1015 return false;
1016 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001017
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001018 if (br_inst->isConditional())
1019 {
1020 Value *condition = br_inst->getCondition();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001021
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001022 lldb_private::Scalar C;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001023
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001024 if (!frame.EvaluateValue(C, condition, module))
1025 {
1026 if (log)
1027 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
1028 error.SetErrorToGenericError();
1029 error.SetErrorString(bad_value_error);
1030 return false;
1031 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001032
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001033 if (C.GetRawBits64(0))
1034 frame.Jump(br_inst->getSuccessor(0));
1035 else
1036 frame.Jump(br_inst->getSuccessor(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001037
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001038 if (log)
1039 {
1040 log->Printf("Interpreted a BrInst with a condition");
1041 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1042 }
1043 }
1044 else
1045 {
1046 frame.Jump(br_inst->getSuccessor(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001047
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001048 if (log)
1049 {
1050 log->Printf("Interpreted a BrInst with no condition");
1051 }
1052 }
1053 }
1054 continue;
1055 case Instruction::GetElementPtr:
1056 {
1057 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001058
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001059 if (!gep_inst)
1060 {
1061 if (log)
1062 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
1063 error.SetErrorToGenericError();
1064 error.SetErrorString(interpreter_internal_error);
1065 return false;
1066 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001067
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001068 const Value *pointer_operand = gep_inst->getPointerOperand();
Eduard Burtescud05b8992016-01-22 03:43:23 +00001069 Type *src_elem_ty = gep_inst->getSourceElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001070
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001071 lldb_private::Scalar P;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001072
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001073 if (!frame.EvaluateValue(P, pointer_operand, module))
1074 {
1075 if (log)
1076 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1077 error.SetErrorToGenericError();
1078 error.SetErrorString(bad_value_error);
1079 return false;
1080 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001081
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001082 typedef SmallVector <Value *, 8> IndexVector;
1083 typedef IndexVector::iterator IndexIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001084
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001085 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1086 gep_inst->idx_end());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001087
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001088 SmallVector <Value *, 8> const_indices;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001089
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001090 for (IndexIterator ii = indices.begin(), ie = indices.end();
1091 ii != ie;
1092 ++ii)
1093 {
1094 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001095
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001096 if (!constant_index)
1097 {
1098 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001099
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001100 if (!frame.EvaluateValue(I, *ii, module))
1101 {
1102 if (log)
1103 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1104 error.SetErrorToGenericError();
1105 error.SetErrorString(bad_value_error);
1106 return false;
1107 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001108
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001109 if (log)
1110 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001111
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001112 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1113 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001114
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001115 const_indices.push_back(constant_index);
1116 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001117
Eduard Burtescud05b8992016-01-22 03:43:23 +00001118 uint64_t offset = data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001119
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001120 lldb_private::Scalar Poffset = P + offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001121
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001122 frame.AssignValue(inst, Poffset, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001123
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001124 if (log)
1125 {
1126 log->Printf("Interpreted a GetElementPtrInst");
1127 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1128 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1129 }
1130 }
1131 break;
1132 case Instruction::ICmp:
1133 {
1134 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001135
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001136 if (!icmp_inst)
1137 {
1138 if (log)
1139 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1140 error.SetErrorToGenericError();
1141 error.SetErrorString(interpreter_internal_error);
1142 return false;
1143 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001144
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001145 CmpInst::Predicate predicate = icmp_inst->getPredicate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001146
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001147 Value *lhs = inst->getOperand(0);
1148 Value *rhs = inst->getOperand(1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001149
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001150 lldb_private::Scalar L;
1151 lldb_private::Scalar R;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001152
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001153 if (!frame.EvaluateValue(L, lhs, module))
1154 {
1155 if (log)
1156 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1157 error.SetErrorToGenericError();
1158 error.SetErrorString(bad_value_error);
1159 return false;
1160 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001161
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001162 if (!frame.EvaluateValue(R, rhs, module))
1163 {
1164 if (log)
1165 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1166 error.SetErrorToGenericError();
1167 error.SetErrorString(bad_value_error);
1168 return false;
1169 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001170
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001171 lldb_private::Scalar result;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001172
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001173 switch (predicate)
1174 {
1175 default:
1176 return false;
1177 case CmpInst::ICMP_EQ:
1178 result = (L == R);
1179 break;
1180 case CmpInst::ICMP_NE:
1181 result = (L != R);
1182 break;
1183 case CmpInst::ICMP_UGT:
1184 result = (L.GetRawBits64(0) > R.GetRawBits64(0));
1185 break;
1186 case CmpInst::ICMP_UGE:
1187 result = (L.GetRawBits64(0) >= R.GetRawBits64(0));
1188 break;
1189 case CmpInst::ICMP_ULT:
1190 result = (L.GetRawBits64(0) < R.GetRawBits64(0));
1191 break;
1192 case CmpInst::ICMP_ULE:
1193 result = (L.GetRawBits64(0) <= R.GetRawBits64(0));
1194 break;
1195 case CmpInst::ICMP_SGT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001196 L.MakeSigned();
1197 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001198 result = (L > R);
1199 break;
1200 case CmpInst::ICMP_SGE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001201 L.MakeSigned();
1202 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001203 result = (L >= R);
1204 break;
1205 case CmpInst::ICMP_SLT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001206 L.MakeSigned();
1207 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001208 result = (L < R);
1209 break;
1210 case CmpInst::ICMP_SLE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001211 L.MakeSigned();
1212 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001213 result = (L <= R);
1214 break;
1215 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001216
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001217 frame.AssignValue(inst, result, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001218
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001219 if (log)
1220 {
1221 log->Printf("Interpreted an ICmpInst");
1222 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1223 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1224 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1225 }
1226 }
1227 break;
1228 case Instruction::IntToPtr:
1229 {
1230 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001232 if (!int_to_ptr_inst)
1233 {
1234 if (log)
1235 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1236 error.SetErrorToGenericError();
1237 error.SetErrorString(interpreter_internal_error);
1238 return false;
1239 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001240
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001241 Value *src_operand = int_to_ptr_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001242
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001243 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001244
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001245 if (!frame.EvaluateValue(I, src_operand, module))
1246 {
1247 if (log)
1248 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1249 error.SetErrorToGenericError();
1250 error.SetErrorString(bad_value_error);
1251 return false;
1252 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001253
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001254 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001255
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001256 if (log)
1257 {
1258 log->Printf("Interpreted an IntToPtr");
1259 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1260 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1261 }
1262 }
1263 break;
1264 case Instruction::PtrToInt:
1265 {
1266 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001267
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001268 if (!ptr_to_int_inst)
1269 {
1270 if (log)
1271 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1272 error.SetErrorToGenericError();
1273 error.SetErrorString(interpreter_internal_error);
1274 return false;
1275 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001276
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001277 Value *src_operand = ptr_to_int_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001278
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001279 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001280
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001281 if (!frame.EvaluateValue(I, src_operand, module))
1282 {
1283 if (log)
1284 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1285 error.SetErrorToGenericError();
1286 error.SetErrorString(bad_value_error);
1287 return false;
1288 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001289
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001290 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001291
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001292 if (log)
1293 {
1294 log->Printf("Interpreted a PtrToInt");
1295 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1296 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1297 }
1298 }
1299 break;
Sean Callanan8c46bac2013-10-11 19:45:00 +00001300 case Instruction::Trunc:
1301 {
1302 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001303
Sean Callanan8c46bac2013-10-11 19:45:00 +00001304 if (!trunc_inst)
1305 {
1306 if (log)
1307 log->Printf("getOpcode() returns Trunc, but instruction is not a TruncInst");
1308 error.SetErrorToGenericError();
1309 error.SetErrorString(interpreter_internal_error);
1310 return false;
1311 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001312
Sean Callanan8c46bac2013-10-11 19:45:00 +00001313 Value *src_operand = trunc_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001314
Sean Callanan8c46bac2013-10-11 19:45:00 +00001315 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001316
Sean Callanan8c46bac2013-10-11 19:45:00 +00001317 if (!frame.EvaluateValue(I, src_operand, module))
1318 {
1319 if (log)
1320 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1321 error.SetErrorToGenericError();
1322 error.SetErrorString(bad_value_error);
1323 return false;
1324 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001325
Sean Callanan8c46bac2013-10-11 19:45:00 +00001326 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001327
Sean Callanan8c46bac2013-10-11 19:45:00 +00001328 if (log)
1329 {
1330 log->Printf("Interpreted a Trunc");
1331 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1332 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1333 }
1334 }
1335 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001336 case Instruction::Load:
1337 {
1338 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001339
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001340 if (!load_inst)
1341 {
1342 if (log)
1343 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1344 error.SetErrorToGenericError();
1345 error.SetErrorString(interpreter_internal_error);
1346 return false;
1347 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001348
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001349 // The semantics of Load are:
1350 // Create a region D that will contain the loaded data
1351 // Resolve the region P containing a pointer
1352 // Dereference P to get the region R that the data should be loaded from
1353 // Transfer a unit of type type(D) from R to D
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001354
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001355 const Value *pointer_operand = load_inst->getPointerOperand();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001356
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001357 Type *pointer_ty = pointer_operand->getType();
1358 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1359 if (!pointer_ptr_ty)
1360 {
1361 if (log)
1362 log->Printf("getPointerOperand()->getType() is not a PointerType");
1363 error.SetErrorToGenericError();
1364 error.SetErrorString(interpreter_internal_error);
1365 return false;
1366 }
1367 Type *target_ty = pointer_ptr_ty->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001368
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001369 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1370 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001371
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001372 if (D == LLDB_INVALID_ADDRESS)
1373 {
1374 if (log)
1375 log->Printf("LoadInst's value doesn't resolve to anything");
1376 error.SetErrorToGenericError();
1377 error.SetErrorString(bad_value_error);
1378 return false;
1379 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001380
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001381 if (P == LLDB_INVALID_ADDRESS)
1382 {
1383 if (log)
1384 log->Printf("LoadInst's pointer doesn't resolve to anything");
1385 error.SetErrorToGenericError();
1386 error.SetErrorString(bad_value_error);
1387 return false;
1388 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001389
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001390 lldb::addr_t R;
1391 lldb_private::Error read_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001392 execution_unit.ReadPointerFromMemory(&R, P, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001393
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001394 if (!read_error.Success())
1395 {
1396 if (log)
1397 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1398 error.SetErrorToGenericError();
1399 error.SetErrorString(memory_read_error);
1400 return false;
1401 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001402
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001403 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1404 lldb_private::DataBufferHeap buffer(target_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001405
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001406 read_error.Clear();
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001407 execution_unit.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001408 if (!read_error.Success())
1409 {
1410 if (log)
1411 log->Printf("Couldn't read from a region on behalf of a LoadInst");
1412 error.SetErrorToGenericError();
1413 error.SetErrorString(memory_read_error);
1414 return false;
1415 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001416
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001417 lldb_private::Error write_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001418 execution_unit.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001419 if (!write_error.Success())
1420 {
1421 if (log)
1422 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1423 error.SetErrorToGenericError();
1424 error.SetErrorString(memory_read_error);
1425 return false;
1426 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001427
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001428 if (log)
1429 {
1430 log->Printf("Interpreted a LoadInst");
Matt Kopecef143712013-06-03 18:00:07 +00001431 log->Printf(" P : 0x%" PRIx64, P);
1432 log->Printf(" R : 0x%" PRIx64, R);
1433 log->Printf(" D : 0x%" PRIx64, D);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001434 }
1435 }
1436 break;
1437 case Instruction::Ret:
1438 {
1439 return true;
1440 }
1441 case Instruction::Store:
1442 {
1443 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001444
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001445 if (!store_inst)
1446 {
1447 if (log)
1448 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1449 error.SetErrorToGenericError();
1450 error.SetErrorString(interpreter_internal_error);
1451 return false;
1452 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001453
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001454 // The semantics of Store are:
1455 // Resolve the region D containing the data to be stored
1456 // Resolve the region P containing a pointer
1457 // Dereference P to get the region R that the data should be stored in
1458 // Transfer a unit of type type(D) from D to R
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001459
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001460 const Value *value_operand = store_inst->getValueOperand();
1461 const Value *pointer_operand = store_inst->getPointerOperand();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001462
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001463 Type *pointer_ty = pointer_operand->getType();
1464 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1465 if (!pointer_ptr_ty)
1466 return false;
1467 Type *target_ty = pointer_ptr_ty->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001468
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001469 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1470 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001471
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001472 if (D == LLDB_INVALID_ADDRESS)
1473 {
1474 if (log)
1475 log->Printf("StoreInst's value doesn't resolve to anything");
1476 error.SetErrorToGenericError();
1477 error.SetErrorString(bad_value_error);
1478 return false;
1479 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001480
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001481 if (P == LLDB_INVALID_ADDRESS)
1482 {
1483 if (log)
1484 log->Printf("StoreInst's pointer doesn't resolve to anything");
1485 error.SetErrorToGenericError();
1486 error.SetErrorString(bad_value_error);
1487 return false;
1488 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001489
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001490 lldb::addr_t R;
1491 lldb_private::Error read_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001492 execution_unit.ReadPointerFromMemory(&R, P, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001493
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001494 if (!read_error.Success())
1495 {
1496 if (log)
1497 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1498 error.SetErrorToGenericError();
1499 error.SetErrorString(memory_read_error);
1500 return false;
1501 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001502
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001503 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1504 lldb_private::DataBufferHeap buffer(target_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001505
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001506 read_error.Clear();
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001507 execution_unit.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001508 if (!read_error.Success())
1509 {
1510 if (log)
1511 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1512 error.SetErrorToGenericError();
1513 error.SetErrorString(memory_read_error);
1514 return false;
1515 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001516
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001517 lldb_private::Error write_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001518 execution_unit.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001519 if (!write_error.Success())
1520 {
1521 if (log)
1522 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1523 error.SetErrorToGenericError();
Sean Callanan49630e72013-04-20 02:39:24 +00001524 error.SetErrorString(memory_write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001525 return false;
1526 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001527
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001528 if (log)
1529 {
1530 log->Printf("Interpreted a StoreInst");
Matt Kopecef143712013-06-03 18:00:07 +00001531 log->Printf(" D : 0x%" PRIx64, D);
1532 log->Printf(" P : 0x%" PRIx64, P);
1533 log->Printf(" R : 0x%" PRIx64, R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001534 }
1535 }
1536 break;
Ewan Crawford90ff7912015-07-14 10:56:58 +00001537 case Instruction::Call:
1538 {
1539 const CallInst *call_inst = dyn_cast<CallInst>(inst);
1540
1541 if (!call_inst)
1542 {
1543 if (log)
1544 log->Printf("getOpcode() returns %s, but instruction is not a CallInst", inst->getOpcodeName());
1545 error.SetErrorToGenericError();
1546 error.SetErrorString(interpreter_internal_error);
1547 return false;
1548 }
1549
1550 if (CanIgnoreCall(call_inst))
1551 break;
1552
1553 // Get the return type
1554 llvm::Type *returnType = call_inst->getType();
1555 if (returnType == nullptr)
1556 {
1557 error.SetErrorToGenericError();
1558 error.SetErrorString("unable to access return type");
1559 return false;
1560 }
1561
1562 // Work with void, integer and pointer return types
1563 if (!returnType->isVoidTy() &&
1564 !returnType->isIntegerTy() &&
1565 !returnType->isPointerTy())
1566 {
1567 error.SetErrorToGenericError();
1568 error.SetErrorString("return type is not supported");
1569 return false;
1570 }
1571
1572 // Check we can actually get a thread
1573 if (exe_ctx.GetThreadPtr() == nullptr)
1574 {
1575 error.SetErrorToGenericError();
1576 error.SetErrorStringWithFormat("unable to acquire thread");
1577 return false;
1578 }
1579
1580 // Make sure we have a valid process
1581 if (!exe_ctx.GetProcessPtr())
1582 {
1583 error.SetErrorToGenericError();
1584 error.SetErrorStringWithFormat("unable to get the process");
1585 return false;
1586 }
1587
1588 // Find the address of the callee function
1589 lldb_private::Scalar I;
1590 const llvm::Value *val = call_inst->getCalledValue();
1591
1592 if (!frame.EvaluateValue(I, val, module))
1593 {
1594 error.SetErrorToGenericError();
1595 error.SetErrorString("unable to get address of function");
1596 return false;
1597 }
1598 lldb_private::Address funcAddr(I.ULongLong(LLDB_INVALID_ADDRESS));
1599
Sean Callanan579e70c2016-03-19 00:03:59 +00001600 lldb_private::DiagnosticManager diagnostics;
Ewan Crawford90ff7912015-07-14 10:56:58 +00001601 lldb_private::EvaluateExpressionOptions options;
1602
1603 // We generally receive a function pointer which we must dereference
1604 llvm::Type* prototype = val->getType();
1605 if (!prototype->isPointerTy())
1606 {
1607 error.SetErrorToGenericError();
1608 error.SetErrorString("call need function pointer");
1609 return false;
1610 }
1611
1612 // Dereference the function pointer
1613 prototype = prototype->getPointerElementType();
1614 if (!(prototype->isFunctionTy() || prototype->isFunctionVarArg()))
1615 {
1616 error.SetErrorToGenericError();
1617 error.SetErrorString("call need function pointer");
1618 return false;
1619 }
1620
1621 // Find number of arguments
1622 const int numArgs = call_inst->getNumArgOperands();
1623
1624 // We work with a fixed array of 16 arguments which is our upper limit
1625 static lldb_private::ABI::CallArgument rawArgs[16];
1626 if (numArgs >= 16)
1627 {
1628 error.SetErrorToGenericError();
1629 error.SetErrorStringWithFormat("function takes too many arguments");
1630 return false;
1631 }
1632
1633 // Push all function arguments to the argument list that will
1634 // be passed to the call function thread plan
1635 for (int i = 0; i < numArgs; i++)
1636 {
1637 // Get details of this argument
1638 llvm::Value *arg_op = call_inst->getArgOperand(i);
1639 llvm::Type *arg_ty = arg_op->getType();
1640
1641 // Ensure that this argument is an supported type
1642 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy())
1643 {
1644 error.SetErrorToGenericError();
1645 error.SetErrorStringWithFormat("argument %d must be integer type", i);
1646 return false;
1647 }
1648
1649 // Extract the arguments value
1650 lldb_private::Scalar tmp_op = 0;
1651 if (!frame.EvaluateValue(tmp_op, arg_op, module))
1652 {
1653 error.SetErrorToGenericError();
1654 error.SetErrorStringWithFormat("unable to evaluate argument %d", i);
1655 return false;
1656 }
1657
1658 // Check if this is a string literal or constant string pointer
1659 if (arg_ty->isPointerTy())
1660 {
1661 // Pointer to just one type
1662 assert(arg_ty->getNumContainedTypes() == 1);
1663
1664 lldb::addr_t addr = tmp_op.ULongLong();
1665 size_t dataSize = 0;
1666
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001667 if (execution_unit.GetAllocSize(addr, dataSize))
Ewan Crawford90ff7912015-07-14 10:56:58 +00001668 {
1669 // Create the required buffer
1670 rawArgs[i].size = dataSize;
1671 rawArgs[i].data_ap.reset(new uint8_t[dataSize + 1]);
1672
1673 // Read string from host memory
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001674 execution_unit.ReadMemory(rawArgs[i].data_ap.get(), addr, dataSize, error);
Ewan Crawford90ff7912015-07-14 10:56:58 +00001675 if (error.Fail())
1676 {
1677 assert(!"we have failed to read the string from memory");
1678 return false;
1679 }
1680 // Add null terminator
1681 rawArgs[i].data_ap[dataSize] = '\0';
1682 rawArgs[i].type = lldb_private::ABI::CallArgument::HostPointer;
1683 }
1684 else
1685 {
1686 assert(!"unable to locate host data for transfer to device");
1687 return false;
1688 }
1689 }
1690 else /* if ( arg_ty->isPointerTy() ) */
1691 {
1692 rawArgs[i].type = lldb_private::ABI::CallArgument::TargetValue;
1693 // Get argument size in bytes
1694 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8;
1695 // Push value into argument list for thread plan
1696 rawArgs[i].value = tmp_op.ULongLong();
1697 }
1698
1699 }
1700
1701 // Pack the arguments into an llvm::array
1702 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1703
1704 // Setup a thread plan to call the target function
Sean Callanan579e70c2016-03-19 00:03:59 +00001705 lldb::ThreadPlanSP call_plan_sp(new lldb_private::ThreadPlanCallFunctionUsingABI(
1706 exe_ctx.GetThreadRef(), funcAddr, *prototype, *returnType, args, options));
Ewan Crawford90ff7912015-07-14 10:56:58 +00001707
1708 // Check if the plan is valid
Sean Callanan579e70c2016-03-19 00:03:59 +00001709 lldb_private::StreamString ss;
1710 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss))
Ewan Crawford90ff7912015-07-14 10:56:58 +00001711 {
1712 error.SetErrorToGenericError();
Sean Callanan579e70c2016-03-19 00:03:59 +00001713 error.SetErrorStringWithFormat("unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1714 I.ULongLong());
Ewan Crawford90ff7912015-07-14 10:56:58 +00001715 return false;
1716 }
1717
1718 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
1719
1720 // Execute the actual function call thread plan
Sean Callanan579e70c2016-03-19 00:03:59 +00001721 lldb::ExpressionResults res =
1722 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
Ewan Crawford90ff7912015-07-14 10:56:58 +00001723
1724 // Check that the thread plan completed successfully
1725 if (res != lldb::ExpressionResults::eExpressionCompleted)
1726 {
1727 error.SetErrorToGenericError();
1728 error.SetErrorStringWithFormat("ThreadPlanCallFunctionUsingABI failed");
1729 return false;
1730 }
1731
1732 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
1733
1734 // Void return type
1735 if (returnType->isVoidTy())
1736 {
1737 // Cant assign to void types, so we leave the frame untouched
1738 }
1739 else
1740 // Integer or pointer return type
1741 if (returnType->isIntegerTy() || returnType->isPointerTy())
1742 {
1743 // Get the encapsulated return value
1744 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1745
1746 lldb_private::Scalar returnVal = -1;
1747 lldb_private::ValueObject *vobj = retVal.get();
1748
1749 // Check if the return value is valid
1750 if (vobj == nullptr || retVal.empty())
1751 {
1752 error.SetErrorToGenericError();
1753 error.SetErrorStringWithFormat("unable to get the return value");
1754 return false;
1755 }
1756
1757 // Extract the return value as a integer
1758 lldb_private::Value & value = vobj->GetValue();
1759 returnVal = value.GetScalar();
1760
1761 // Push the return value as the result
1762 frame.AssignValue(inst, returnVal, module);
1763 }
1764 }
1765 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001766 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001767
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001768 ++frame.m_ii;
1769 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001770
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001771 if (num_insts >= 4096)
1772 {
1773 error.SetErrorToGenericError();
1774 error.SetErrorString(infinite_loop_error);
1775 return false;
1776 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001777
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001778 return false;
1779}