blob: 2871a066013a56035df99480bd14a0128d62429a [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
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000234 if (!AssignToMatchType(cast_scalar, scalar.ULongLong(), value->getType()))
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000235 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 Callanan94a9a392012-02-08 01:27:49 +0000376 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000377 lldb_private::DataBufferHeap buf(constant_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000378
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000379 lldb_private::Error get_data_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000381 lldb_private::Scalar resolved_scalar(resolved_value.zextOrTrunc(llvm::NextPowerOf2(constant_size) * 8));
382 if (!resolved_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error))
383 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000384
Sean Callanan08052af2013-04-17 07:50:58 +0000385 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000386
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000387 m_execution_unit.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000388
Sean Callanan08052af2013-04-17 07:50:58 +0000389 return write_error.Success();
390 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000391
Sean Callanan1582ee62013-04-18 22:06:33 +0000392 lldb::addr_t Malloc (size_t size, uint8_t byte_alignment)
393 {
394 lldb::addr_t ret = m_stack_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000395
Sean Callanan1582ee62013-04-18 22:06:33 +0000396 ret -= size;
397 ret -= (ret % byte_alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000398
Sean Callanan1582ee62013-04-18 22:06:33 +0000399 if (ret < m_frame_process_address)
400 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000401
Sean Callanan1582ee62013-04-18 22:06:33 +0000402 m_stack_pointer = ret;
403 return ret;
404 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000405
Sean Callanan08052af2013-04-17 07:50:58 +0000406 lldb::addr_t MallocPointer ()
407 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000408 return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment());
Sean Callanan08052af2013-04-17 07:50:58 +0000409 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000410
Sean Callanan1582ee62013-04-18 22:06:33 +0000411 lldb::addr_t Malloc (llvm::Type *type)
Sean Callanan08052af2013-04-17 07:50:58 +0000412 {
413 lldb_private::Error alloc_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000414
Sean Callanan1582ee62013-04-18 22:06:33 +0000415 return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type));
Sean Callanan08052af2013-04-17 07:50:58 +0000416 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000417
Sean Callanan08052af2013-04-17 07:50:58 +0000418 std::string PrintData (lldb::addr_t addr, llvm::Type *type)
419 {
420 size_t length = m_target_data.getTypeStoreSize(type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000421
Sean Callanan08052af2013-04-17 07:50:58 +0000422 lldb_private::DataBufferHeap buf(length, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000423
Sean Callanan08052af2013-04-17 07:50:58 +0000424 lldb_private::Error read_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000426 m_execution_unit.ReadMemory(buf.GetBytes(), addr, length, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000427
Sean Callanan08052af2013-04-17 07:50:58 +0000428 if (!read_error.Success())
429 return std::string("<couldn't read data>");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000430
Sean Callanan08052af2013-04-17 07:50:58 +0000431 lldb_private::StreamString ss;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432
Sean Callanan08052af2013-04-17 07:50:58 +0000433 for (size_t i = 0; i < length; i++)
434 {
435 if ((!(i & 0xf)) && i)
436 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
437 else
438 ss.Printf("%02hhx ", buf.GetBytes()[i]);
439 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000440
Sean Callanan08052af2013-04-17 07:50:58 +0000441 return ss.GetString();
442 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000443
Sean Callanan08052af2013-04-17 07:50:58 +0000444 lldb::addr_t ResolveValue (const Value *value, Module &module)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000445 {
446 ValueMap::iterator i = m_values.find(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000447
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000448 if (i != m_values.end())
449 return i->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000450
Sean Callanan1582ee62013-04-18 22:06:33 +0000451 // Fall back and allocate space [allocation type Alloca]
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000452
Sean Callanan1582ee62013-04-18 22:06:33 +0000453 lldb::addr_t data_address = Malloc(value->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000454
Sean Callanan1582ee62013-04-18 22:06:33 +0000455 if (const Constant *constant = dyn_cast<Constant>(value))
456 {
457 if (!ResolveConstant (data_address, constant))
458 {
459 lldb_private::Error free_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000460 m_execution_unit.Free(data_address, free_error);
Sean Callanan1582ee62013-04-18 22:06:33 +0000461 return LLDB_INVALID_ADDRESS;
462 }
463 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000464
Sean Callanan1582ee62013-04-18 22:06:33 +0000465 m_values[value] = data_address;
466 return data_address;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000467 }
468};
469
Sean Callanan175a0d02012-01-24 22:06:48 +0000470static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
Sean Callanan3fa3e652013-05-02 00:33:44 +0000471static const char *unsupported_operand_error = "Interpreter doesn't handle one of the expression's operands";
Greg Claytone01e07b2013-04-18 18:10:51 +0000472//static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
Sean Callanan175a0d02012-01-24 22:06:48 +0000473static const char *interpreter_internal_error = "Interpreter encountered an internal error";
474static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
475static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
476static const char *memory_write_error = "Interpreter couldn't write to memory";
477static const char *memory_read_error = "Interpreter couldn't read from memory";
478static const char *infinite_loop_error = "Interpreter ran for too many cycles";
Sean Callanan44342732013-04-19 08:14:32 +0000479//static const char *bad_result_error = "Result of expression is in bad memory";
Sean Callanan175a0d02012-01-24 22:06:48 +0000480
Sean Callanan8c62daf2016-02-12 21:16:58 +0000481static bool
482CanResolveConstant (llvm::Constant *constant)
483{
484 switch (constant->getValueID())
485 {
486 default:
487 return false;
488 case Value::ConstantIntVal:
489 case Value::ConstantFPVal:
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000490 case Value::FunctionVal:
Sean Callanan8c62daf2016-02-12 21:16:58 +0000491 return true;
492 case Value::ConstantExprVal:
493 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
494 {
495 switch (constant_expr->getOpcode())
496 {
497 default:
498 return false;
499 case Instruction::IntToPtr:
500 case Instruction::PtrToInt:
501 case Instruction::BitCast:
502 return CanResolveConstant(constant_expr->getOperand(0));
503 case Instruction::GetElementPtr:
504 {
505 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
506 Constant *base = dyn_cast<Constant>(*op_cursor);
507 if (!base)
508 return false;
509
510 return CanResolveConstant(base);
511 }
512 }
513 } else {
514 return false;
515 }
516 case Value::ConstantPointerNullVal:
517 return true;
518 }
519}
520
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000521bool
Sean Callanan44342732013-04-19 08:14:32 +0000522IRInterpreter::CanInterpret (llvm::Module &module,
523 llvm::Function &function,
Ewan Crawford90ff7912015-07-14 10:56:58 +0000524 lldb_private::Error &error,
525 const bool support_function_calls)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000526{
Greg Clayton5160ce52013-03-27 23:08:40 +0000527 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000528
Sean Callanan85fc8762013-06-27 01:59:51 +0000529 bool saw_function_with_body = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000530
Sean Callanan85fc8762013-06-27 01:59:51 +0000531 for (Module::iterator fi = module.begin(), fe = module.end();
532 fi != fe;
533 ++fi)
534 {
535 if (fi->begin() != fi->end())
536 {
537 if (saw_function_with_body)
538 return false;
539 saw_function_with_body = true;
540 }
541 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000542
Sean Callanan44342732013-04-19 08:14:32 +0000543 for (Function::iterator bbi = function.begin(), bbe = function.end();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000544 bbi != bbe;
545 ++bbi)
546 {
547 for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
548 ii != ie;
549 ++ii)
550 {
551 switch (ii->getOpcode())
552 {
553 default:
554 {
555 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000556 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str());
Sean Callanan44342732013-04-19 08:14:32 +0000557 error.SetErrorToGenericError();
558 error.SetErrorString(unsupported_opcode_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000559 return false;
560 }
561 case Instruction::Add:
562 case Instruction::Alloca:
563 case Instruction::BitCast:
564 case Instruction::Br:
Sean Callanan576a4372014-03-25 19:33:15 +0000565 break;
566 case Instruction::Call:
567 {
568 CallInst *call_inst = dyn_cast<CallInst>(ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000569
Sean Callanan576a4372014-03-25 19:33:15 +0000570 if (!call_inst)
571 {
572 error.SetErrorToGenericError();
573 error.SetErrorString(interpreter_internal_error);
574 return false;
575 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000576
Ewan Crawford90ff7912015-07-14 10:56:58 +0000577 if (!CanIgnoreCall(call_inst) && !support_function_calls)
Sean Callanan576a4372014-03-25 19:33:15 +0000578 {
579 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000580 log->Printf("Unsupported instruction: %s", PrintValue(&*ii).c_str());
Sean Callanan576a4372014-03-25 19:33:15 +0000581 error.SetErrorToGenericError();
582 error.SetErrorString(unsupported_opcode_error);
583 return false;
584 }
585 }
Sean Callanan92872892014-03-25 19:47:07 +0000586 break;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000587 case Instruction::GetElementPtr:
588 break;
589 case Instruction::ICmp:
590 {
591 ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000593 if (!icmp_inst)
Sean Callanan175a0d02012-01-24 22:06:48 +0000594 {
Sean Callanan44342732013-04-19 08:14:32 +0000595 error.SetErrorToGenericError();
596 error.SetErrorString(interpreter_internal_error);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000597 return false;
Sean Callanan175a0d02012-01-24 22:06:48 +0000598 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000599
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000600 switch (icmp_inst->getPredicate())
601 {
602 default:
Sean Callanan44342732013-04-19 08:14:32 +0000603 {
604 if (log)
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000605 log->Printf("Unsupported ICmp predicate: %s", PrintValue(&*ii).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000606
Sean Callanan44342732013-04-19 08:14:32 +0000607 error.SetErrorToGenericError();
608 error.SetErrorString(unsupported_opcode_error);
609 return false;
610 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000611 case CmpInst::ICMP_EQ:
612 case CmpInst::ICMP_NE:
613 case CmpInst::ICMP_UGT:
614 case CmpInst::ICMP_UGE:
615 case CmpInst::ICMP_ULT:
616 case CmpInst::ICMP_ULE:
617 case CmpInst::ICMP_SGT:
618 case CmpInst::ICMP_SGE:
619 case CmpInst::ICMP_SLT:
620 case CmpInst::ICMP_SLE:
621 break;
622 }
623 }
624 break;
Sean Callanan087f4372013-01-09 22:44:41 +0000625 case Instruction::And:
626 case Instruction::AShr:
Sean Callanan80c48c12011-10-21 05:18:02 +0000627 case Instruction::IntToPtr:
Sean Callanan2abffe02012-12-01 00:09:34 +0000628 case Instruction::PtrToInt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000629 case Instruction::Load:
Sean Callanan087f4372013-01-09 22:44:41 +0000630 case Instruction::LShr:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000631 case Instruction::Mul:
Sean Callanan087f4372013-01-09 22:44:41 +0000632 case Instruction::Or:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000633 case Instruction::Ret:
634 case Instruction::SDiv:
Sean Callanan415422c2013-06-05 22:07:06 +0000635 case Instruction::SExt:
Sean Callanan087f4372013-01-09 22:44:41 +0000636 case Instruction::Shl:
Sean Callananf466a6e2012-12-21 22:27:55 +0000637 case Instruction::SRem:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000638 case Instruction::Store:
639 case Instruction::Sub:
Sean Callanan8c46bac2013-10-11 19:45:00 +0000640 case Instruction::Trunc:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000641 case Instruction::UDiv:
Sean Callananf466a6e2012-12-21 22:27:55 +0000642 case Instruction::URem:
Sean Callanan087f4372013-01-09 22:44:41 +0000643 case Instruction::Xor:
Sean Callanan1ef77432012-04-23 17:25:38 +0000644 case Instruction::ZExt:
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000645 break;
646 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Sean Callanan3fa3e652013-05-02 00:33:44 +0000648 for (int oi = 0, oe = ii->getNumOperands();
649 oi != oe;
650 ++oi)
651 {
652 Value *operand = ii->getOperand(oi);
653 Type *operand_type = operand->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000654
Sean Callanan3fa3e652013-05-02 00:33:44 +0000655 switch (operand_type->getTypeID())
656 {
657 default:
658 break;
659 case Type::VectorTyID:
660 {
661 if (log)
662 log->Printf("Unsupported operand type: %s", PrintType(operand_type).c_str());
663 error.SetErrorString(unsupported_operand_error);
664 return false;
665 }
666 }
Sean Callanan8c62daf2016-02-12 21:16:58 +0000667
668 if (Constant *constant = llvm::dyn_cast<Constant>(operand))
669 {
670 if (!CanResolveConstant(constant))
671 {
672 if (log)
673 log->Printf("Unsupported constant: %s", PrintValue(constant).c_str());
674 error.SetErrorString(unsupported_operand_error);
675 return false;
676 }
677 }
Sean Callanan3fa3e652013-05-02 00:33:44 +0000678 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000679 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000680
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000681 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000682
Sean Callanan44342732013-04-19 08:14:32 +0000683 return true;}
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000684
685bool
686IRInterpreter::Interpret (llvm::Module &module,
687 llvm::Function &function,
Sean Callanan1582ee62013-04-18 22:06:33 +0000688 llvm::ArrayRef<lldb::addr_t> args,
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000689 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanandf565402013-04-27 02:19:33 +0000690 lldb_private::Error &error,
691 lldb::addr_t stack_frame_bottom,
Ewan Crawford90ff7912015-07-14 10:56:58 +0000692 lldb::addr_t stack_frame_top,
693 lldb_private::ExecutionContext &exe_ctx)
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000694{
695 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000696
Sean Callanan1582ee62013-04-18 22:06:33 +0000697 if (log)
698 {
699 std::string s;
700 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000701
Sean Callanan1582ee62013-04-18 22:06:33 +0000702 module.print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000703
Sean Callanan1582ee62013-04-18 22:06:33 +0000704 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000705
Sean Callanan1582ee62013-04-18 22:06:33 +0000706 log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str());
707 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000708
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000709 DataLayout data_layout(&module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000710
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000711 InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom, stack_frame_top);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000712
Sean Callanan1582ee62013-04-18 22:06:33 +0000713 if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS)
714 {
715 error.SetErrorString("Couldn't allocate stack frame");
716 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000717
Sean Callanan1582ee62013-04-18 22:06:33 +0000718 int arg_index = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000719
Sean Callanan1582ee62013-04-18 22:06:33 +0000720 for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end();
721 ai != ae;
722 ++ai, ++arg_index)
723 {
Chaoren Lin3ca7a3e2015-09-16 01:20:34 +0000724 if (args.size() <= static_cast<size_t>(arg_index))
Sean Callanan1582ee62013-04-18 22:06:33 +0000725 {
726 error.SetErrorString ("Not enough arguments passed in to function");
727 return false;
728 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000729
Sean Callanan1582ee62013-04-18 22:06:33 +0000730 lldb::addr_t ptr = args[arg_index];
731
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000732 frame.MakeArgument(&*ai, ptr);
Sean Callanan1582ee62013-04-18 22:06:33 +0000733 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000734
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000735 uint32_t num_insts = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000736
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000737 frame.Jump(&function.front());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000738
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000739 while (frame.m_ii != frame.m_ie && (++num_insts < 4096))
740 {
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +0000741 const Instruction *inst = &*frame.m_ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000742
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000743 if (log)
744 log->Printf("Interpreting %s", PrintValue(inst).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000745
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000746 switch (inst->getOpcode())
747 {
748 default:
749 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000750
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000751 case Instruction::Add:
752 case Instruction::Sub:
753 case Instruction::Mul:
754 case Instruction::SDiv:
755 case Instruction::UDiv:
756 case Instruction::SRem:
757 case Instruction::URem:
758 case Instruction::Shl:
759 case Instruction::LShr:
760 case Instruction::AShr:
761 case Instruction::And:
762 case Instruction::Or:
763 case Instruction::Xor:
764 {
765 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000766
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000767 if (!bin_op)
768 {
769 if (log)
770 log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName());
771 error.SetErrorToGenericError();
772 error.SetErrorString(interpreter_internal_error);
773 return false;
774 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000775
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000776 Value *lhs = inst->getOperand(0);
777 Value *rhs = inst->getOperand(1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000778
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000779 lldb_private::Scalar L;
780 lldb_private::Scalar R;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000781
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000782 if (!frame.EvaluateValue(L, lhs, module))
783 {
784 if (log)
785 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
786 error.SetErrorToGenericError();
787 error.SetErrorString(bad_value_error);
788 return false;
789 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000790
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000791 if (!frame.EvaluateValue(R, rhs, module))
792 {
793 if (log)
794 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
795 error.SetErrorToGenericError();
796 error.SetErrorString(bad_value_error);
797 return false;
798 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000799
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000800 lldb_private::Scalar result;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000801
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000802 switch (inst->getOpcode())
803 {
804 default:
805 break;
806 case Instruction::Add:
807 result = L + R;
808 break;
809 case Instruction::Mul:
810 result = L * R;
811 break;
812 case Instruction::Sub:
813 result = L - R;
814 break;
815 case Instruction::SDiv:
Sean Callanan0b342b62013-05-24 20:36:56 +0000816 L.MakeSigned();
817 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000818 result = L / R;
819 break;
820 case Instruction::UDiv:
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000821 L.MakeUnsigned();
822 R.MakeUnsigned();
823 result = L / R;
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000824 break;
825 case Instruction::SRem:
Sean Callanan0b342b62013-05-24 20:36:56 +0000826 L.MakeSigned();
827 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000828 result = L % R;
829 break;
830 case Instruction::URem:
Ulrich Weigand9521ad22016-04-15 09:55:52 +0000831 L.MakeUnsigned();
832 R.MakeUnsigned();
833 result = L % R;
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000834 break;
835 case Instruction::Shl:
836 result = L << R;
837 break;
838 case Instruction::AShr:
839 result = L >> R;
840 break;
841 case Instruction::LShr:
842 result = L;
843 result.ShiftRightLogical(R);
844 break;
845 case Instruction::And:
846 result = L & R;
847 break;
848 case Instruction::Or:
849 result = L | R;
850 break;
851 case Instruction::Xor:
852 result = L ^ R;
853 break;
854 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000855
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000856 frame.AssignValue(inst, result, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000857
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000858 if (log)
859 {
860 log->Printf("Interpreted a %s", inst->getOpcodeName());
861 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
862 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
863 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
864 }
865 }
866 break;
867 case Instruction::Alloca:
868 {
869 const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000870
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000871 if (!alloca_inst)
872 {
873 if (log)
874 log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst");
875 error.SetErrorToGenericError();
876 error.SetErrorString(interpreter_internal_error);
877 return false;
878 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000879
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000880 if (alloca_inst->isArrayAllocation())
881 {
882 if (log)
883 log->Printf("AllocaInsts are not handled if isArrayAllocation() is true");
884 error.SetErrorToGenericError();
885 error.SetErrorString(unsupported_opcode_error);
886 return false;
887 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000888
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000889 // The semantics of Alloca are:
890 // Create a region R of virtual memory of type T, backed by a data buffer
891 // Create a region P of virtual memory of type T*, backed by a data buffer
892 // Write the virtual address of R into P
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000894 Type *T = alloca_inst->getAllocatedType();
895 Type *Tptr = alloca_inst->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000896
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000897 lldb::addr_t R = frame.Malloc(T);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000898
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000899 if (R == LLDB_INVALID_ADDRESS)
900 {
901 if (log)
902 log->Printf("Couldn't allocate memory for an AllocaInst");
903 error.SetErrorToGenericError();
904 error.SetErrorString(memory_allocation_error);
905 return false;
906 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000907
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000908 lldb::addr_t P = frame.Malloc(Tptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000909
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000910 if (P == LLDB_INVALID_ADDRESS)
911 {
912 if (log)
913 log->Printf("Couldn't allocate the result pointer for an AllocaInst");
914 error.SetErrorToGenericError();
915 error.SetErrorString(memory_allocation_error);
916 return false;
917 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000918
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000919 lldb_private::Error write_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000920
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000921 execution_unit.WritePointerToMemory(P, R, write_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000922
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000923 if (!write_error.Success())
924 {
925 if (log)
926 log->Printf("Couldn't write the result pointer for an AllocaInst");
927 error.SetErrorToGenericError();
928 error.SetErrorString(memory_write_error);
929 lldb_private::Error free_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +0000930 execution_unit.Free(P, free_error);
931 execution_unit.Free(R, free_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000932 return false;
933 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000934
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000935 frame.m_values[alloca_inst] = P;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000936
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000937 if (log)
938 {
939 log->Printf("Interpreted an AllocaInst");
Matt Kopecef143712013-06-03 18:00:07 +0000940 log->Printf(" R : 0x%" PRIx64, R);
941 log->Printf(" P : 0x%" PRIx64, P);
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000942 }
943 }
944 break;
945 case Instruction::BitCast:
946 case Instruction::ZExt:
947 {
948 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000949
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000950 if (!cast_inst)
951 {
952 if (log)
953 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
954 error.SetErrorToGenericError();
955 error.SetErrorString(interpreter_internal_error);
956 return false;
957 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000958
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000959 Value *source = cast_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000960
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000961 lldb_private::Scalar S;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000962
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000963 if (!frame.EvaluateValue(S, source, module))
964 {
965 if (log)
966 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
967 error.SetErrorToGenericError();
968 error.SetErrorString(bad_value_error);
969 return false;
970 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000971
Sean Callanan14cb2aa2013-04-17 18:35:47 +0000972 frame.AssignValue(inst, S, module);
973 }
974 break;
Sean Callanan415422c2013-06-05 22:07:06 +0000975 case Instruction::SExt:
976 {
977 const CastInst *cast_inst = dyn_cast<CastInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000978
Sean Callanan415422c2013-06-05 22:07:06 +0000979 if (!cast_inst)
980 {
981 if (log)
982 log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName());
983 error.SetErrorToGenericError();
984 error.SetErrorString(interpreter_internal_error);
985 return false;
986 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000987
Sean Callanan415422c2013-06-05 22:07:06 +0000988 Value *source = cast_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000989
Sean Callanan415422c2013-06-05 22:07:06 +0000990 lldb_private::Scalar S;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000991
Sean Callanan415422c2013-06-05 22:07:06 +0000992 if (!frame.EvaluateValue(S, source, module))
993 {
994 if (log)
995 log->Printf("Couldn't evaluate %s", PrintValue(source).c_str());
996 error.SetErrorToGenericError();
997 error.SetErrorString(bad_value_error);
998 return false;
999 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001000
Sean Callanan415422c2013-06-05 22:07:06 +00001001 S.MakeSigned();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001002
Sean Callanan415422c2013-06-05 22:07:06 +00001003 lldb_private::Scalar S_signextend(S.SLongLong());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001004
Sean Callanan415422c2013-06-05 22:07:06 +00001005 frame.AssignValue(inst, S_signextend, module);
1006 }
1007 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001008 case Instruction::Br:
1009 {
1010 const BranchInst *br_inst = dyn_cast<BranchInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001011
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001012 if (!br_inst)
1013 {
1014 if (log)
1015 log->Printf("getOpcode() returns Br, but instruction is not a BranchInst");
1016 error.SetErrorToGenericError();
1017 error.SetErrorString(interpreter_internal_error);
1018 return false;
1019 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001020
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001021 if (br_inst->isConditional())
1022 {
1023 Value *condition = br_inst->getCondition();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001024
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001025 lldb_private::Scalar C;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001026
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001027 if (!frame.EvaluateValue(C, condition, module))
1028 {
1029 if (log)
1030 log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str());
1031 error.SetErrorToGenericError();
1032 error.SetErrorString(bad_value_error);
1033 return false;
1034 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001035
Ulrich Weigand9521ad22016-04-15 09:55:52 +00001036 if (!C.IsZero())
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001037 frame.Jump(br_inst->getSuccessor(0));
1038 else
1039 frame.Jump(br_inst->getSuccessor(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001040
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001041 if (log)
1042 {
1043 log->Printf("Interpreted a BrInst with a condition");
1044 log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str());
1045 }
1046 }
1047 else
1048 {
1049 frame.Jump(br_inst->getSuccessor(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001050
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001051 if (log)
1052 {
1053 log->Printf("Interpreted a BrInst with no condition");
1054 }
1055 }
1056 }
1057 continue;
1058 case Instruction::GetElementPtr:
1059 {
1060 const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001061
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001062 if (!gep_inst)
1063 {
1064 if (log)
1065 log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst");
1066 error.SetErrorToGenericError();
1067 error.SetErrorString(interpreter_internal_error);
1068 return false;
1069 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001070
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001071 const Value *pointer_operand = gep_inst->getPointerOperand();
Eduard Burtescud05b8992016-01-22 03:43:23 +00001072 Type *src_elem_ty = gep_inst->getSourceElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001073
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001074 lldb_private::Scalar P;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001075
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001076 if (!frame.EvaluateValue(P, pointer_operand, module))
1077 {
1078 if (log)
1079 log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str());
1080 error.SetErrorToGenericError();
1081 error.SetErrorString(bad_value_error);
1082 return false;
1083 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001084
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001085 typedef SmallVector <Value *, 8> IndexVector;
1086 typedef IndexVector::iterator IndexIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001087
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001088 SmallVector <Value *, 8> indices (gep_inst->idx_begin(),
1089 gep_inst->idx_end());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001090
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001091 SmallVector <Value *, 8> const_indices;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001092
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001093 for (IndexIterator ii = indices.begin(), ie = indices.end();
1094 ii != ie;
1095 ++ii)
1096 {
1097 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001098
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001099 if (!constant_index)
1100 {
1101 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001102
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001103 if (!frame.EvaluateValue(I, *ii, module))
1104 {
1105 if (log)
1106 log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str());
1107 error.SetErrorToGenericError();
1108 error.SetErrorString(bad_value_error);
1109 return false;
1110 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001111
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001112 if (log)
1113 log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001114
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001115 constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1116 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001117
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001118 const_indices.push_back(constant_index);
1119 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001120
Eduard Burtescud05b8992016-01-22 03:43:23 +00001121 uint64_t offset = data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001122
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001123 lldb_private::Scalar Poffset = P + offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001124
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001125 frame.AssignValue(inst, Poffset, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001126
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001127 if (log)
1128 {
1129 log->Printf("Interpreted a GetElementPtrInst");
1130 log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str());
1131 log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str());
1132 }
1133 }
1134 break;
1135 case Instruction::ICmp:
1136 {
1137 const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001138
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001139 if (!icmp_inst)
1140 {
1141 if (log)
1142 log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst");
1143 error.SetErrorToGenericError();
1144 error.SetErrorString(interpreter_internal_error);
1145 return false;
1146 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001147
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001148 CmpInst::Predicate predicate = icmp_inst->getPredicate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001149
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001150 Value *lhs = inst->getOperand(0);
1151 Value *rhs = inst->getOperand(1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001152
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001153 lldb_private::Scalar L;
1154 lldb_private::Scalar R;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001155
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001156 if (!frame.EvaluateValue(L, lhs, module))
1157 {
1158 if (log)
1159 log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str());
1160 error.SetErrorToGenericError();
1161 error.SetErrorString(bad_value_error);
1162 return false;
1163 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001164
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001165 if (!frame.EvaluateValue(R, rhs, module))
1166 {
1167 if (log)
1168 log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str());
1169 error.SetErrorToGenericError();
1170 error.SetErrorString(bad_value_error);
1171 return false;
1172 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001173
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001174 lldb_private::Scalar result;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001175
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001176 switch (predicate)
1177 {
1178 default:
1179 return false;
1180 case CmpInst::ICMP_EQ:
1181 result = (L == R);
1182 break;
1183 case CmpInst::ICMP_NE:
1184 result = (L != R);
1185 break;
1186 case CmpInst::ICMP_UGT:
Ulrich Weigand9521ad22016-04-15 09:55:52 +00001187 L.MakeUnsigned();
1188 R.MakeUnsigned();
1189 result = (L > R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001190 break;
1191 case CmpInst::ICMP_UGE:
Ulrich Weigand9521ad22016-04-15 09:55:52 +00001192 L.MakeUnsigned();
1193 R.MakeUnsigned();
1194 result = (L >= R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001195 break;
1196 case CmpInst::ICMP_ULT:
Ulrich Weigand9521ad22016-04-15 09:55:52 +00001197 L.MakeUnsigned();
1198 R.MakeUnsigned();
1199 result = (L < R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001200 break;
1201 case CmpInst::ICMP_ULE:
Ulrich Weigand9521ad22016-04-15 09:55:52 +00001202 L.MakeUnsigned();
1203 R.MakeUnsigned();
1204 result = (L <= R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001205 break;
1206 case CmpInst::ICMP_SGT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001207 L.MakeSigned();
1208 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001209 result = (L > R);
1210 break;
1211 case CmpInst::ICMP_SGE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001212 L.MakeSigned();
1213 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001214 result = (L >= R);
1215 break;
1216 case CmpInst::ICMP_SLT:
Sean Callanan0b342b62013-05-24 20:36:56 +00001217 L.MakeSigned();
1218 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001219 result = (L < R);
1220 break;
1221 case CmpInst::ICMP_SLE:
Sean Callanan0b342b62013-05-24 20:36:56 +00001222 L.MakeSigned();
1223 R.MakeSigned();
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001224 result = (L <= R);
1225 break;
1226 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001227
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001228 frame.AssignValue(inst, result, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001229
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001230 if (log)
1231 {
1232 log->Printf("Interpreted an ICmpInst");
1233 log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str());
1234 log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str());
1235 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1236 }
1237 }
1238 break;
1239 case Instruction::IntToPtr:
1240 {
1241 const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001242
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001243 if (!int_to_ptr_inst)
1244 {
1245 if (log)
1246 log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst");
1247 error.SetErrorToGenericError();
1248 error.SetErrorString(interpreter_internal_error);
1249 return false;
1250 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001251
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001252 Value *src_operand = int_to_ptr_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001253
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001254 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001255
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001256 if (!frame.EvaluateValue(I, src_operand, module))
1257 {
1258 if (log)
1259 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1260 error.SetErrorToGenericError();
1261 error.SetErrorString(bad_value_error);
1262 return false;
1263 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001264
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001265 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001266
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001267 if (log)
1268 {
1269 log->Printf("Interpreted an IntToPtr");
1270 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1271 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1272 }
1273 }
1274 break;
1275 case Instruction::PtrToInt:
1276 {
1277 const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001278
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001279 if (!ptr_to_int_inst)
1280 {
1281 if (log)
1282 log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst");
1283 error.SetErrorToGenericError();
1284 error.SetErrorString(interpreter_internal_error);
1285 return false;
1286 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001287
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001288 Value *src_operand = ptr_to_int_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001289
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001290 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001291
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001292 if (!frame.EvaluateValue(I, src_operand, module))
1293 {
1294 if (log)
1295 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1296 error.SetErrorToGenericError();
1297 error.SetErrorString(bad_value_error);
1298 return false;
1299 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001300
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001301 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001302
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001303 if (log)
1304 {
1305 log->Printf("Interpreted a PtrToInt");
1306 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1307 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1308 }
1309 }
1310 break;
Sean Callanan8c46bac2013-10-11 19:45:00 +00001311 case Instruction::Trunc:
1312 {
1313 const TruncInst *trunc_inst = dyn_cast<TruncInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001314
Sean Callanan8c46bac2013-10-11 19:45:00 +00001315 if (!trunc_inst)
1316 {
1317 if (log)
1318 log->Printf("getOpcode() returns Trunc, but instruction is not a TruncInst");
1319 error.SetErrorToGenericError();
1320 error.SetErrorString(interpreter_internal_error);
1321 return false;
1322 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001323
Sean Callanan8c46bac2013-10-11 19:45:00 +00001324 Value *src_operand = trunc_inst->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001325
Sean Callanan8c46bac2013-10-11 19:45:00 +00001326 lldb_private::Scalar I;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001327
Sean Callanan8c46bac2013-10-11 19:45:00 +00001328 if (!frame.EvaluateValue(I, src_operand, module))
1329 {
1330 if (log)
1331 log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str());
1332 error.SetErrorToGenericError();
1333 error.SetErrorString(bad_value_error);
1334 return false;
1335 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001336
Sean Callanan8c46bac2013-10-11 19:45:00 +00001337 frame.AssignValue(inst, I, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001338
Sean Callanan8c46bac2013-10-11 19:45:00 +00001339 if (log)
1340 {
1341 log->Printf("Interpreted a Trunc");
1342 log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str());
1343 log->Printf(" = : %s", frame.SummarizeValue(inst).c_str());
1344 }
1345 }
1346 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001347 case Instruction::Load:
1348 {
1349 const LoadInst *load_inst = dyn_cast<LoadInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001350
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001351 if (!load_inst)
1352 {
1353 if (log)
1354 log->Printf("getOpcode() returns Load, but instruction is not a LoadInst");
1355 error.SetErrorToGenericError();
1356 error.SetErrorString(interpreter_internal_error);
1357 return false;
1358 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001359
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001360 // The semantics of Load are:
1361 // Create a region D that will contain the loaded data
1362 // Resolve the region P containing a pointer
1363 // Dereference P to get the region R that the data should be loaded from
1364 // Transfer a unit of type type(D) from R to D
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001365
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001366 const Value *pointer_operand = load_inst->getPointerOperand();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001367
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001368 Type *pointer_ty = pointer_operand->getType();
1369 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1370 if (!pointer_ptr_ty)
1371 {
1372 if (log)
1373 log->Printf("getPointerOperand()->getType() is not a PointerType");
1374 error.SetErrorToGenericError();
1375 error.SetErrorString(interpreter_internal_error);
1376 return false;
1377 }
1378 Type *target_ty = pointer_ptr_ty->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001379
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001380 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1381 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001382
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001383 if (D == LLDB_INVALID_ADDRESS)
1384 {
1385 if (log)
1386 log->Printf("LoadInst's value doesn't resolve to anything");
1387 error.SetErrorToGenericError();
1388 error.SetErrorString(bad_value_error);
1389 return false;
1390 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001391
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001392 if (P == LLDB_INVALID_ADDRESS)
1393 {
1394 if (log)
1395 log->Printf("LoadInst's pointer doesn't resolve to anything");
1396 error.SetErrorToGenericError();
1397 error.SetErrorString(bad_value_error);
1398 return false;
1399 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001400
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001401 lldb::addr_t R;
1402 lldb_private::Error read_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001403 execution_unit.ReadPointerFromMemory(&R, P, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001404
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001405 if (!read_error.Success())
1406 {
1407 if (log)
1408 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1409 error.SetErrorToGenericError();
1410 error.SetErrorString(memory_read_error);
1411 return false;
1412 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001413
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001414 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1415 lldb_private::DataBufferHeap buffer(target_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001416
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001417 read_error.Clear();
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001418 execution_unit.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001419 if (!read_error.Success())
1420 {
1421 if (log)
1422 log->Printf("Couldn't read from 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 lldb_private::Error write_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001429 execution_unit.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001430 if (!write_error.Success())
1431 {
1432 if (log)
1433 log->Printf("Couldn't write to a region on behalf of a LoadInst");
1434 error.SetErrorToGenericError();
1435 error.SetErrorString(memory_read_error);
1436 return false;
1437 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001439 if (log)
1440 {
1441 log->Printf("Interpreted a LoadInst");
Matt Kopecef143712013-06-03 18:00:07 +00001442 log->Printf(" P : 0x%" PRIx64, P);
1443 log->Printf(" R : 0x%" PRIx64, R);
1444 log->Printf(" D : 0x%" PRIx64, D);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001445 }
1446 }
1447 break;
1448 case Instruction::Ret:
1449 {
1450 return true;
1451 }
1452 case Instruction::Store:
1453 {
1454 const StoreInst *store_inst = dyn_cast<StoreInst>(inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001455
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001456 if (!store_inst)
1457 {
1458 if (log)
1459 log->Printf("getOpcode() returns Store, but instruction is not a StoreInst");
1460 error.SetErrorToGenericError();
1461 error.SetErrorString(interpreter_internal_error);
1462 return false;
1463 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001464
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001465 // The semantics of Store are:
1466 // Resolve the region D containing the data to be stored
1467 // Resolve the region P containing a pointer
1468 // Dereference P to get the region R that the data should be stored in
1469 // Transfer a unit of type type(D) from D to R
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001470
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001471 const Value *value_operand = store_inst->getValueOperand();
1472 const Value *pointer_operand = store_inst->getPointerOperand();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001473
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001474 Type *pointer_ty = pointer_operand->getType();
1475 PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
1476 if (!pointer_ptr_ty)
1477 return false;
1478 Type *target_ty = pointer_ptr_ty->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001479
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001480 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1481 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001482
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001483 if (D == LLDB_INVALID_ADDRESS)
1484 {
1485 if (log)
1486 log->Printf("StoreInst's value doesn't resolve to anything");
1487 error.SetErrorToGenericError();
1488 error.SetErrorString(bad_value_error);
1489 return false;
1490 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001491
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001492 if (P == LLDB_INVALID_ADDRESS)
1493 {
1494 if (log)
1495 log->Printf("StoreInst's pointer doesn't resolve to anything");
1496 error.SetErrorToGenericError();
1497 error.SetErrorString(bad_value_error);
1498 return false;
1499 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001500
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001501 lldb::addr_t R;
1502 lldb_private::Error read_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001503 execution_unit.ReadPointerFromMemory(&R, P, read_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001504
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001505 if (!read_error.Success())
1506 {
1507 if (log)
1508 log->Printf("Couldn't read the address to be loaded for a LoadInst");
1509 error.SetErrorToGenericError();
1510 error.SetErrorString(memory_read_error);
1511 return false;
1512 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001513
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001514 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1515 lldb_private::DataBufferHeap buffer(target_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001516
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001517 read_error.Clear();
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001518 execution_unit.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001519 if (!read_error.Success())
1520 {
1521 if (log)
1522 log->Printf("Couldn't read from a region on behalf of a StoreInst");
1523 error.SetErrorToGenericError();
1524 error.SetErrorString(memory_read_error);
1525 return false;
1526 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001527
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001528 lldb_private::Error write_error;
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001529 execution_unit.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001530 if (!write_error.Success())
1531 {
1532 if (log)
1533 log->Printf("Couldn't write to a region on behalf of a StoreInst");
1534 error.SetErrorToGenericError();
Sean Callanan49630e72013-04-20 02:39:24 +00001535 error.SetErrorString(memory_write_error);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001536 return false;
1537 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001538
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001539 if (log)
1540 {
1541 log->Printf("Interpreted a StoreInst");
Matt Kopecef143712013-06-03 18:00:07 +00001542 log->Printf(" D : 0x%" PRIx64, D);
1543 log->Printf(" P : 0x%" PRIx64, P);
1544 log->Printf(" R : 0x%" PRIx64, R);
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001545 }
1546 }
1547 break;
Ewan Crawford90ff7912015-07-14 10:56:58 +00001548 case Instruction::Call:
1549 {
1550 const CallInst *call_inst = dyn_cast<CallInst>(inst);
1551
1552 if (!call_inst)
1553 {
1554 if (log)
1555 log->Printf("getOpcode() returns %s, but instruction is not a CallInst", inst->getOpcodeName());
1556 error.SetErrorToGenericError();
1557 error.SetErrorString(interpreter_internal_error);
1558 return false;
1559 }
1560
1561 if (CanIgnoreCall(call_inst))
1562 break;
1563
1564 // Get the return type
1565 llvm::Type *returnType = call_inst->getType();
1566 if (returnType == nullptr)
1567 {
1568 error.SetErrorToGenericError();
1569 error.SetErrorString("unable to access return type");
1570 return false;
1571 }
1572
1573 // Work with void, integer and pointer return types
1574 if (!returnType->isVoidTy() &&
1575 !returnType->isIntegerTy() &&
1576 !returnType->isPointerTy())
1577 {
1578 error.SetErrorToGenericError();
1579 error.SetErrorString("return type is not supported");
1580 return false;
1581 }
1582
1583 // Check we can actually get a thread
1584 if (exe_ctx.GetThreadPtr() == nullptr)
1585 {
1586 error.SetErrorToGenericError();
1587 error.SetErrorStringWithFormat("unable to acquire thread");
1588 return false;
1589 }
1590
1591 // Make sure we have a valid process
1592 if (!exe_ctx.GetProcessPtr())
1593 {
1594 error.SetErrorToGenericError();
1595 error.SetErrorStringWithFormat("unable to get the process");
1596 return false;
1597 }
1598
1599 // Find the address of the callee function
1600 lldb_private::Scalar I;
1601 const llvm::Value *val = call_inst->getCalledValue();
1602
1603 if (!frame.EvaluateValue(I, val, module))
1604 {
1605 error.SetErrorToGenericError();
1606 error.SetErrorString("unable to get address of function");
1607 return false;
1608 }
1609 lldb_private::Address funcAddr(I.ULongLong(LLDB_INVALID_ADDRESS));
1610
Sean Callanan579e70c2016-03-19 00:03:59 +00001611 lldb_private::DiagnosticManager diagnostics;
Ewan Crawford90ff7912015-07-14 10:56:58 +00001612 lldb_private::EvaluateExpressionOptions options;
1613
1614 // We generally receive a function pointer which we must dereference
1615 llvm::Type* prototype = val->getType();
1616 if (!prototype->isPointerTy())
1617 {
1618 error.SetErrorToGenericError();
1619 error.SetErrorString("call need function pointer");
1620 return false;
1621 }
1622
1623 // Dereference the function pointer
1624 prototype = prototype->getPointerElementType();
1625 if (!(prototype->isFunctionTy() || prototype->isFunctionVarArg()))
1626 {
1627 error.SetErrorToGenericError();
1628 error.SetErrorString("call need function pointer");
1629 return false;
1630 }
1631
1632 // Find number of arguments
1633 const int numArgs = call_inst->getNumArgOperands();
1634
1635 // We work with a fixed array of 16 arguments which is our upper limit
1636 static lldb_private::ABI::CallArgument rawArgs[16];
1637 if (numArgs >= 16)
1638 {
1639 error.SetErrorToGenericError();
1640 error.SetErrorStringWithFormat("function takes too many arguments");
1641 return false;
1642 }
1643
1644 // Push all function arguments to the argument list that will
1645 // be passed to the call function thread plan
1646 for (int i = 0; i < numArgs; i++)
1647 {
1648 // Get details of this argument
1649 llvm::Value *arg_op = call_inst->getArgOperand(i);
1650 llvm::Type *arg_ty = arg_op->getType();
1651
1652 // Ensure that this argument is an supported type
1653 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy())
1654 {
1655 error.SetErrorToGenericError();
1656 error.SetErrorStringWithFormat("argument %d must be integer type", i);
1657 return false;
1658 }
1659
1660 // Extract the arguments value
1661 lldb_private::Scalar tmp_op = 0;
1662 if (!frame.EvaluateValue(tmp_op, arg_op, module))
1663 {
1664 error.SetErrorToGenericError();
1665 error.SetErrorStringWithFormat("unable to evaluate argument %d", i);
1666 return false;
1667 }
1668
1669 // Check if this is a string literal or constant string pointer
1670 if (arg_ty->isPointerTy())
1671 {
1672 // Pointer to just one type
1673 assert(arg_ty->getNumContainedTypes() == 1);
1674
1675 lldb::addr_t addr = tmp_op.ULongLong();
1676 size_t dataSize = 0;
1677
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001678 if (execution_unit.GetAllocSize(addr, dataSize))
Ewan Crawford90ff7912015-07-14 10:56:58 +00001679 {
1680 // Create the required buffer
1681 rawArgs[i].size = dataSize;
1682 rawArgs[i].data_ap.reset(new uint8_t[dataSize + 1]);
1683
1684 // Read string from host memory
Ted Woodward7071c5fd2016-03-01 21:53:26 +00001685 execution_unit.ReadMemory(rawArgs[i].data_ap.get(), addr, dataSize, error);
Ewan Crawford90ff7912015-07-14 10:56:58 +00001686 if (error.Fail())
1687 {
1688 assert(!"we have failed to read the string from memory");
1689 return false;
1690 }
1691 // Add null terminator
1692 rawArgs[i].data_ap[dataSize] = '\0';
1693 rawArgs[i].type = lldb_private::ABI::CallArgument::HostPointer;
1694 }
1695 else
1696 {
1697 assert(!"unable to locate host data for transfer to device");
1698 return false;
1699 }
1700 }
1701 else /* if ( arg_ty->isPointerTy() ) */
1702 {
1703 rawArgs[i].type = lldb_private::ABI::CallArgument::TargetValue;
1704 // Get argument size in bytes
1705 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8;
1706 // Push value into argument list for thread plan
1707 rawArgs[i].value = tmp_op.ULongLong();
1708 }
1709
1710 }
1711
1712 // Pack the arguments into an llvm::array
1713 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1714
1715 // Setup a thread plan to call the target function
Sean Callanan579e70c2016-03-19 00:03:59 +00001716 lldb::ThreadPlanSP call_plan_sp(new lldb_private::ThreadPlanCallFunctionUsingABI(
1717 exe_ctx.GetThreadRef(), funcAddr, *prototype, *returnType, args, options));
Ewan Crawford90ff7912015-07-14 10:56:58 +00001718
1719 // Check if the plan is valid
Sean Callanan579e70c2016-03-19 00:03:59 +00001720 lldb_private::StreamString ss;
1721 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss))
Ewan Crawford90ff7912015-07-14 10:56:58 +00001722 {
1723 error.SetErrorToGenericError();
Sean Callanan579e70c2016-03-19 00:03:59 +00001724 error.SetErrorStringWithFormat("unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1725 I.ULongLong());
Ewan Crawford90ff7912015-07-14 10:56:58 +00001726 return false;
1727 }
1728
1729 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
1730
1731 // Execute the actual function call thread plan
Sean Callanan579e70c2016-03-19 00:03:59 +00001732 lldb::ExpressionResults res =
1733 exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
Ewan Crawford90ff7912015-07-14 10:56:58 +00001734
1735 // Check that the thread plan completed successfully
1736 if (res != lldb::ExpressionResults::eExpressionCompleted)
1737 {
1738 error.SetErrorToGenericError();
1739 error.SetErrorStringWithFormat("ThreadPlanCallFunctionUsingABI failed");
1740 return false;
1741 }
1742
1743 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
1744
1745 // Void return type
1746 if (returnType->isVoidTy())
1747 {
1748 // Cant assign to void types, so we leave the frame untouched
1749 }
1750 else
1751 // Integer or pointer return type
1752 if (returnType->isIntegerTy() || returnType->isPointerTy())
1753 {
1754 // Get the encapsulated return value
1755 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1756
1757 lldb_private::Scalar returnVal = -1;
1758 lldb_private::ValueObject *vobj = retVal.get();
1759
1760 // Check if the return value is valid
1761 if (vobj == nullptr || retVal.empty())
1762 {
1763 error.SetErrorToGenericError();
1764 error.SetErrorStringWithFormat("unable to get the return value");
1765 return false;
1766 }
1767
1768 // Extract the return value as a integer
1769 lldb_private::Value & value = vobj->GetValue();
1770 returnVal = value.GetScalar();
1771
1772 // Push the return value as the result
1773 frame.AssignValue(inst, returnVal, module);
1774 }
1775 }
1776 break;
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001777 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001778
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001779 ++frame.m_ii;
1780 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001781
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001782 if (num_insts >= 4096)
1783 {
1784 error.SetErrorToGenericError();
1785 error.SetErrorString(infinite_loop_error);
1786 return false;
1787 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001788
Sean Callanan14cb2aa2013-04-17 18:35:47 +00001789 return false;
1790}