Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 1 | //===-- 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 Callanan | fefe43c | 2013-04-25 18:55:45 +0000 | [diff] [blame^] | 10 | #include "lldb/Core/DataExtractor.h" |
| 11 | #include "lldb/Core/Error.h" |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Log.h" |
Sean Callanan | fefe43c | 2013-04-25 18:55:45 +0000 | [diff] [blame^] | 13 | #include "lldb/Core/Scalar.h" |
| 14 | #include "lldb/Core/StreamString.h" |
| 15 | #include "lldb/Expression/IRMemoryMap.h" |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 16 | #include "lldb/Expression/IRInterpreter.h" |
| 17 | |
Chandler Carruth | 1e15758 | 2013-01-02 12:20:07 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
Sean Callanan | fefe43c | 2013-04-25 18:55:45 +0000 | [diff] [blame^] | 19 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 1e15758 | 2013-01-02 12:20:07 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Module.h" |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 24 | |
| 25 | #include <map> |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 29 | static std::string |
| 30 | PrintValue(const Value *value, bool truncate = false) |
| 31 | { |
| 32 | std::string s; |
| 33 | raw_string_ostream rso(s); |
| 34 | value->print(rso); |
| 35 | rso.flush(); |
| 36 | if (truncate) |
| 37 | s.resize(s.length() - 1); |
| 38 | |
| 39 | size_t offset; |
| 40 | while ((offset = s.find('\n')) != s.npos) |
| 41 | s.erase(offset, 1); |
| 42 | while (s[0] == ' ' || s[0] == '\t') |
| 43 | s.erase(0, 1); |
| 44 | |
| 45 | return s; |
| 46 | } |
| 47 | |
| 48 | static std::string |
| 49 | PrintType(const Type *type, bool truncate = false) |
| 50 | { |
| 51 | std::string s; |
| 52 | raw_string_ostream rso(s); |
| 53 | type->print(rso); |
| 54 | rso.flush(); |
| 55 | if (truncate) |
| 56 | s.resize(s.length() - 1); |
| 57 | return s; |
| 58 | } |
| 59 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 60 | class InterpreterStackFrame |
| 61 | { |
| 62 | public: |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 63 | typedef std::map <const Value*, lldb::addr_t> ValueMap; |
| 64 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 65 | ValueMap m_values; |
Micah Villmow | 8468dbe | 2012-10-08 16:28:57 +0000 | [diff] [blame] | 66 | DataLayout &m_target_data; |
Sean Callanan | 179b548 | 2013-04-16 23:49:09 +0000 | [diff] [blame] | 67 | lldb_private::IRMemoryMap &m_memory_map; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 68 | const BasicBlock *m_bb; |
| 69 | BasicBlock::const_iterator m_ii; |
| 70 | BasicBlock::const_iterator m_ie; |
| 71 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 72 | lldb::addr_t m_frame_process_address; |
| 73 | size_t m_frame_size; |
| 74 | lldb::addr_t m_stack_pointer; |
| 75 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 76 | lldb::ByteOrder m_byte_order; |
| 77 | size_t m_addr_byte_size; |
| 78 | |
Micah Villmow | 8468dbe | 2012-10-08 16:28:57 +0000 | [diff] [blame] | 79 | InterpreterStackFrame (DataLayout &target_data, |
Sean Callanan | 179b548 | 2013-04-16 23:49:09 +0000 | [diff] [blame] | 80 | lldb_private::IRMemoryMap &memory_map) : |
Daniel Dunbar | a08823f | 2011-10-31 22:50:49 +0000 | [diff] [blame] | 81 | m_target_data (target_data), |
Sean Callanan | 179b548 | 2013-04-16 23:49:09 +0000 | [diff] [blame] | 82 | m_memory_map (memory_map) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 83 | { |
| 84 | m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig); |
Sean Callanan | 95769bf | 2012-10-11 22:00:52 +0000 | [diff] [blame] | 85 | m_addr_byte_size = (target_data.getPointerSize(0)); |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 86 | |
| 87 | m_frame_size = 512 * 1024; |
| 88 | |
| 89 | lldb_private::Error alloc_error; |
| 90 | |
| 91 | m_frame_process_address = memory_map.Malloc(m_frame_size, |
| 92 | m_addr_byte_size, |
| 93 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 94 | lldb_private::IRMemoryMap::eAllocationPolicyMirror, |
| 95 | alloc_error); |
| 96 | |
| 97 | if (alloc_error.Success()) |
| 98 | { |
| 99 | m_stack_pointer = m_frame_process_address + m_frame_size; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | m_frame_process_address = LLDB_INVALID_ADDRESS; |
| 104 | m_stack_pointer = LLDB_INVALID_ADDRESS; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | ~InterpreterStackFrame () |
| 109 | { |
| 110 | if (m_frame_process_address != LLDB_INVALID_ADDRESS) |
| 111 | { |
| 112 | lldb_private::Error free_error; |
| 113 | m_memory_map.Free(m_frame_process_address, free_error); |
| 114 | m_frame_process_address = LLDB_INVALID_ADDRESS; |
| 115 | } |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void Jump (const BasicBlock *bb) |
| 119 | { |
| 120 | m_bb = bb; |
| 121 | m_ii = m_bb->begin(); |
| 122 | m_ie = m_bb->end(); |
| 123 | } |
| 124 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 125 | std::string SummarizeValue (const Value *value) |
| 126 | { |
| 127 | lldb_private::StreamString ss; |
| 128 | |
| 129 | ss.Printf("%s", PrintValue(value).c_str()); |
| 130 | |
| 131 | ValueMap::iterator i = m_values.find(value); |
| 132 | |
| 133 | if (i != m_values.end()) |
| 134 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 135 | lldb::addr_t addr = i->second; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 136 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 137 | ss.Printf(" 0x%llx", (unsigned long long)addr); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return ss.GetString(); |
| 141 | } |
| 142 | |
| 143 | bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type) |
| 144 | { |
| 145 | size_t type_size = m_target_data.getTypeStoreSize(type); |
| 146 | |
| 147 | switch (type_size) |
| 148 | { |
| 149 | case 1: |
| 150 | scalar = (uint8_t)u64value; |
| 151 | break; |
| 152 | case 2: |
| 153 | scalar = (uint16_t)u64value; |
| 154 | break; |
| 155 | case 4: |
| 156 | scalar = (uint32_t)u64value; |
| 157 | break; |
| 158 | case 8: |
| 159 | scalar = (uint64_t)u64value; |
| 160 | break; |
| 161 | default: |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module) |
| 169 | { |
| 170 | const Constant *constant = dyn_cast<Constant>(value); |
| 171 | |
| 172 | if (constant) |
| 173 | { |
| 174 | if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) |
| 175 | { |
| 176 | return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType()); |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 181 | lldb::addr_t process_address = ResolveValue(value, module); |
| 182 | size_t value_size = m_target_data.getTypeStoreSize(value->getType()); |
| 183 | |
| 184 | lldb_private::DataExtractor value_extractor; |
| 185 | lldb_private::Error extract_error; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 186 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 187 | m_memory_map.GetMemoryData(value_extractor, process_address, value_size, extract_error); |
| 188 | |
| 189 | if (!extract_error.Success()) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 190 | return false; |
| 191 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 192 | lldb::offset_t offset = 0; |
Greg Clayton | 78e44bd | 2013-04-25 00:57:05 +0000 | [diff] [blame] | 193 | if (value_size <= 8) |
| 194 | { |
| 195 | uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size); |
| 196 | return AssignToMatchType(scalar, u64value, value->getType()); |
| 197 | } |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module) |
| 204 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 205 | lldb::addr_t process_address = ResolveValue (value, module); |
| 206 | |
| 207 | if (process_address == LLDB_INVALID_ADDRESS) |
| 208 | return false; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 209 | |
| 210 | lldb_private::Scalar cast_scalar; |
| 211 | |
| 212 | if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType())) |
| 213 | return false; |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 214 | |
| 215 | size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType()); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 216 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 217 | lldb_private::DataBufferHeap buf(value_byte_size, 0); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 218 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 219 | lldb_private::Error get_data_error; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 220 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 221 | if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, get_data_error)) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 222 | return false; |
| 223 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 224 | lldb_private::Error write_error; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 225 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 226 | m_memory_map.WriteMemory(process_address, buf.GetBytes(), buf.GetByteSize(), write_error); |
Sean Callanan | c867550 | 2013-02-15 23:07:52 +0000 | [diff] [blame] | 227 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 228 | return write_error.Success(); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Sean Callanan | 94a9a39 | 2012-02-08 01:27:49 +0000 | [diff] [blame] | 231 | bool ResolveConstantValue (APInt &value, const Constant *constant) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 232 | { |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 233 | switch (constant->getValueID()) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 234 | { |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 235 | default: |
| 236 | break; |
| 237 | case Value::ConstantIntVal: |
| 238 | if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) |
Sean Callanan | 80c48c1 | 2011-10-21 05:18:02 +0000 | [diff] [blame] | 239 | { |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 240 | value = constant_int->getValue(); |
| 241 | return true; |
| 242 | } |
| 243 | break; |
| 244 | case Value::ConstantFPVal: |
| 245 | if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) |
| 246 | { |
| 247 | value = constant_fp->getValueAPF().bitcastToAPInt(); |
| 248 | return true; |
| 249 | } |
| 250 | break; |
| 251 | case Value::ConstantExprVal: |
| 252 | if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) |
| 253 | { |
| 254 | switch (constant_expr->getOpcode()) |
Sean Callanan | 94a9a39 | 2012-02-08 01:27:49 +0000 | [diff] [blame] | 255 | { |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 256 | default: |
Sean Callanan | 94a9a39 | 2012-02-08 01:27:49 +0000 | [diff] [blame] | 257 | return false; |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 258 | case Instruction::IntToPtr: |
| 259 | case Instruction::PtrToInt: |
| 260 | case Instruction::BitCast: |
| 261 | return ResolveConstantValue(value, constant_expr->getOperand(0)); |
| 262 | case Instruction::GetElementPtr: |
| 263 | { |
| 264 | ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin(); |
| 265 | ConstantExpr::const_op_iterator op_end = constant_expr->op_end(); |
| 266 | |
| 267 | Constant *base = dyn_cast<Constant>(*op_cursor); |
| 268 | |
| 269 | if (!base) |
| 270 | return false; |
| 271 | |
| 272 | if (!ResolveConstantValue(value, base)) |
| 273 | return false; |
| 274 | |
| 275 | op_cursor++; |
| 276 | |
| 277 | if (op_cursor == op_end) |
| 278 | return true; // no offset to apply! |
| 279 | |
| 280 | SmallVector <Value *, 8> indices (op_cursor, op_end); |
| 281 | |
| 282 | uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices); |
| 283 | |
| 284 | const bool is_signed = true; |
| 285 | value += APInt(value.getBitWidth(), offset, is_signed); |
| 286 | |
| 287 | return true; |
| 288 | } |
Sean Callanan | 94a9a39 | 2012-02-08 01:27:49 +0000 | [diff] [blame] | 289 | } |
Sean Callanan | 80c48c1 | 2011-10-21 05:18:02 +0000 | [diff] [blame] | 290 | } |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 291 | break; |
| 292 | case Value::ConstantPointerNullVal: |
| 293 | if (isa<ConstantPointerNull>(constant)) |
| 294 | { |
| 295 | value = APInt(m_target_data.getPointerSizeInBits(), 0); |
| 296 | return true; |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | bool MakeArgument(const Argument *value, uint64_t address) |
| 304 | { |
| 305 | lldb::addr_t data_address = Malloc(value->getType()); |
| 306 | |
| 307 | if (data_address == LLDB_INVALID_ADDRESS) |
| 308 | return false; |
| 309 | |
| 310 | lldb_private::Error write_error; |
| 311 | |
| 312 | m_memory_map.WritePointerToMemory(data_address, address, write_error); |
| 313 | |
| 314 | if (!write_error.Success()) |
| 315 | { |
| 316 | lldb_private::Error free_error; |
| 317 | m_memory_map.Free(data_address, free_error); |
| 318 | return false; |
Sean Callanan | 80c48c1 | 2011-10-21 05:18:02 +0000 | [diff] [blame] | 319 | } |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 320 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 321 | m_values[value] = data_address; |
| 322 | |
| 323 | lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 324 | |
| 325 | if (log) |
| 326 | { |
| 327 | log->Printf("Made an allocation for argument %s", PrintValue(value).c_str()); |
| 328 | log->Printf(" Data region : %llx", (unsigned long long)address); |
| 329 | log->Printf(" Ref region : %llx", (unsigned long long)data_address); |
| 330 | } |
| 331 | |
| 332 | return true; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 335 | bool ResolveConstant (lldb::addr_t process_address, const Constant *constant) |
Sean Callanan | 94a9a39 | 2012-02-08 01:27:49 +0000 | [diff] [blame] | 336 | { |
| 337 | APInt resolved_value; |
| 338 | |
| 339 | if (!ResolveConstantValue(resolved_value, constant)) |
| 340 | return false; |
| 341 | |
| 342 | const uint64_t *raw_data = resolved_value.getRawData(); |
| 343 | |
| 344 | size_t constant_size = m_target_data.getTypeStoreSize(constant->getType()); |
Sean Callanan | 94a9a39 | 2012-02-08 01:27:49 +0000 | [diff] [blame] | 345 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 346 | lldb_private::Error write_error; |
| 347 | |
| 348 | m_memory_map.WriteMemory(process_address, (uint8_t*)raw_data, constant_size, write_error); |
| 349 | |
| 350 | return write_error.Success(); |
| 351 | } |
| 352 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 353 | lldb::addr_t Malloc (size_t size, uint8_t byte_alignment) |
| 354 | { |
| 355 | lldb::addr_t ret = m_stack_pointer; |
| 356 | |
| 357 | ret -= size; |
| 358 | ret -= (ret % byte_alignment); |
| 359 | |
| 360 | if (ret < m_frame_process_address) |
| 361 | return LLDB_INVALID_ADDRESS; |
| 362 | |
| 363 | m_stack_pointer = ret; |
| 364 | return ret; |
| 365 | } |
| 366 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 367 | lldb::addr_t MallocPointer () |
| 368 | { |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 369 | return Malloc(m_target_data.getPointerSize(), m_target_data.getPointerPrefAlignment()); |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 372 | lldb::addr_t Malloc (llvm::Type *type) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 373 | { |
| 374 | lldb_private::Error alloc_error; |
| 375 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 376 | return Malloc(m_target_data.getTypeAllocSize(type), m_target_data.getPrefTypeAlignment(type)); |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 379 | std::string PrintData (lldb::addr_t addr, llvm::Type *type) |
| 380 | { |
| 381 | size_t length = m_target_data.getTypeStoreSize(type); |
| 382 | |
| 383 | lldb_private::DataBufferHeap buf(length, 0); |
| 384 | |
| 385 | lldb_private::Error read_error; |
| 386 | |
| 387 | m_memory_map.ReadMemory(buf.GetBytes(), addr, length, read_error); |
| 388 | |
| 389 | if (!read_error.Success()) |
| 390 | return std::string("<couldn't read data>"); |
| 391 | |
| 392 | lldb_private::StreamString ss; |
| 393 | |
| 394 | for (size_t i = 0; i < length; i++) |
| 395 | { |
| 396 | if ((!(i & 0xf)) && i) |
| 397 | ss.Printf("%02hhx - ", buf.GetBytes()[i]); |
| 398 | else |
| 399 | ss.Printf("%02hhx ", buf.GetBytes()[i]); |
| 400 | } |
| 401 | |
| 402 | return ss.GetString(); |
| 403 | } |
| 404 | |
| 405 | lldb::addr_t ResolveValue (const Value *value, Module &module) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 406 | { |
| 407 | ValueMap::iterator i = m_values.find(value); |
| 408 | |
| 409 | if (i != m_values.end()) |
| 410 | return i->second; |
| 411 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 412 | // Fall back and allocate space [allocation type Alloca] |
| 413 | |
| 414 | lldb::addr_t data_address = Malloc(value->getType()); |
| 415 | |
| 416 | if (const Constant *constant = dyn_cast<Constant>(value)) |
| 417 | { |
| 418 | if (!ResolveConstant (data_address, constant)) |
| 419 | { |
| 420 | lldb_private::Error free_error; |
| 421 | m_memory_map.Free(data_address, free_error); |
| 422 | return LLDB_INVALID_ADDRESS; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | m_values[value] = data_address; |
| 427 | return data_address; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 428 | } |
| 429 | }; |
| 430 | |
Sean Callanan | 175a0d0 | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 431 | static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes"; |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 432 | //static const char *interpreter_initialization_error = "Interpreter couldn't be initialized"; |
Sean Callanan | 175a0d0 | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 433 | static const char *interpreter_internal_error = "Interpreter encountered an internal error"; |
| 434 | static const char *bad_value_error = "Interpreter couldn't resolve a value during execution"; |
| 435 | static const char *memory_allocation_error = "Interpreter couldn't allocate memory"; |
| 436 | static const char *memory_write_error = "Interpreter couldn't write to memory"; |
| 437 | static const char *memory_read_error = "Interpreter couldn't read from memory"; |
| 438 | static const char *infinite_loop_error = "Interpreter ran for too many cycles"; |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 439 | //static const char *bad_result_error = "Result of expression is in bad memory"; |
Sean Callanan | 175a0d0 | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 440 | |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 441 | bool |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 442 | IRInterpreter::CanInterpret (llvm::Module &module, |
| 443 | llvm::Function &function, |
| 444 | lldb_private::Error &error) |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 445 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 446 | lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 447 | |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 448 | for (Function::iterator bbi = function.begin(), bbe = function.end(); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 449 | bbi != bbe; |
| 450 | ++bbi) |
| 451 | { |
| 452 | for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end(); |
| 453 | ii != ie; |
| 454 | ++ii) |
| 455 | { |
| 456 | switch (ii->getOpcode()) |
| 457 | { |
| 458 | default: |
| 459 | { |
| 460 | if (log) |
| 461 | log->Printf("Unsupported instruction: %s", PrintValue(ii).c_str()); |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 462 | error.SetErrorToGenericError(); |
| 463 | error.SetErrorString(unsupported_opcode_error); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | case Instruction::Add: |
| 467 | case Instruction::Alloca: |
| 468 | case Instruction::BitCast: |
| 469 | case Instruction::Br: |
| 470 | case Instruction::GetElementPtr: |
| 471 | break; |
| 472 | case Instruction::ICmp: |
| 473 | { |
| 474 | ICmpInst *icmp_inst = dyn_cast<ICmpInst>(ii); |
| 475 | |
| 476 | if (!icmp_inst) |
Sean Callanan | 175a0d0 | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 477 | { |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 478 | error.SetErrorToGenericError(); |
| 479 | error.SetErrorString(interpreter_internal_error); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 480 | return false; |
Sean Callanan | 175a0d0 | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 481 | } |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 482 | |
| 483 | switch (icmp_inst->getPredicate()) |
| 484 | { |
| 485 | default: |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 486 | { |
| 487 | if (log) |
| 488 | log->Printf("Unsupported ICmp predicate: %s", PrintValue(ii).c_str()); |
| 489 | |
| 490 | error.SetErrorToGenericError(); |
| 491 | error.SetErrorString(unsupported_opcode_error); |
| 492 | return false; |
| 493 | } |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 494 | case CmpInst::ICMP_EQ: |
| 495 | case CmpInst::ICMP_NE: |
| 496 | case CmpInst::ICMP_UGT: |
| 497 | case CmpInst::ICMP_UGE: |
| 498 | case CmpInst::ICMP_ULT: |
| 499 | case CmpInst::ICMP_ULE: |
| 500 | case CmpInst::ICMP_SGT: |
| 501 | case CmpInst::ICMP_SGE: |
| 502 | case CmpInst::ICMP_SLT: |
| 503 | case CmpInst::ICMP_SLE: |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | break; |
Sean Callanan | 087f437 | 2013-01-09 22:44:41 +0000 | [diff] [blame] | 508 | case Instruction::And: |
| 509 | case Instruction::AShr: |
Sean Callanan | 80c48c1 | 2011-10-21 05:18:02 +0000 | [diff] [blame] | 510 | case Instruction::IntToPtr: |
Sean Callanan | 2abffe0 | 2012-12-01 00:09:34 +0000 | [diff] [blame] | 511 | case Instruction::PtrToInt: |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 512 | case Instruction::Load: |
Sean Callanan | 087f437 | 2013-01-09 22:44:41 +0000 | [diff] [blame] | 513 | case Instruction::LShr: |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 514 | case Instruction::Mul: |
Sean Callanan | 087f437 | 2013-01-09 22:44:41 +0000 | [diff] [blame] | 515 | case Instruction::Or: |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 516 | case Instruction::Ret: |
| 517 | case Instruction::SDiv: |
Sean Callanan | 087f437 | 2013-01-09 22:44:41 +0000 | [diff] [blame] | 518 | case Instruction::Shl: |
Sean Callanan | f466a6e | 2012-12-21 22:27:55 +0000 | [diff] [blame] | 519 | case Instruction::SRem: |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 520 | case Instruction::Store: |
| 521 | case Instruction::Sub: |
| 522 | case Instruction::UDiv: |
Sean Callanan | f466a6e | 2012-12-21 22:27:55 +0000 | [diff] [blame] | 523 | case Instruction::URem: |
Sean Callanan | 087f437 | 2013-01-09 22:44:41 +0000 | [diff] [blame] | 524 | case Instruction::Xor: |
Sean Callanan | 1ef7743 | 2012-04-23 17:25:38 +0000 | [diff] [blame] | 525 | case Instruction::ZExt: |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 531 | return true;} |
Sean Callanan | 14cb2aa | 2013-04-17 18:35:47 +0000 | [diff] [blame] | 532 | |
| 533 | bool |
| 534 | IRInterpreter::Interpret (llvm::Module &module, |
| 535 | llvm::Function &function, |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 536 | llvm::ArrayRef<lldb::addr_t> args, |
Sean Callanan | 14cb2aa | 2013-04-17 18:35:47 +0000 | [diff] [blame] | 537 | lldb_private::IRMemoryMap &memory_map, |
| 538 | lldb_private::Error &error) |
| 539 | { |
| 540 | lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 541 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 542 | if (log) |
| 543 | { |
| 544 | std::string s; |
| 545 | raw_string_ostream oss(s); |
| 546 | |
| 547 | module.print(oss, NULL); |
| 548 | |
| 549 | oss.flush(); |
| 550 | |
| 551 | log->Printf("Module as passed in to IRInterpreter::Interpret: \n\"%s\"", s.c_str()); |
| 552 | } |
| 553 | |
Sean Callanan | 14cb2aa | 2013-04-17 18:35:47 +0000 | [diff] [blame] | 554 | DataLayout data_layout(&module); |
| 555 | |
Sean Callanan | 4434273 | 2013-04-19 08:14:32 +0000 | [diff] [blame] | 556 | InterpreterStackFrame frame(data_layout, memory_map); |
Sean Callanan | 14cb2aa | 2013-04-17 18:35:47 +0000 | [diff] [blame] | 557 | |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 558 | if (frame.m_frame_process_address == LLDB_INVALID_ADDRESS) |
| 559 | { |
| 560 | error.SetErrorString("Couldn't allocate stack frame"); |
| 561 | } |
| 562 | |
| 563 | int arg_index = 0; |
| 564 | |
| 565 | for (llvm::Function::arg_iterator ai = function.arg_begin(), ae = function.arg_end(); |
| 566 | ai != ae; |
| 567 | ++ai, ++arg_index) |
| 568 | { |
| 569 | if (args.size() < arg_index) |
| 570 | { |
| 571 | error.SetErrorString ("Not enough arguments passed in to function"); |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | lldb::addr_t ptr = args[arg_index]; |
| 576 | |
| 577 | frame.MakeArgument(ai, ptr); |
| 578 | } |
| 579 | |
Sean Callanan | 14cb2aa | 2013-04-17 18:35:47 +0000 | [diff] [blame] | 580 | uint32_t num_insts = 0; |
| 581 | |
| 582 | frame.Jump(function.begin()); |
| 583 | |
| 584 | while (frame.m_ii != frame.m_ie && (++num_insts < 4096)) |
| 585 | { |
| 586 | const Instruction *inst = frame.m_ii; |
| 587 | |
| 588 | if (log) |
| 589 | log->Printf("Interpreting %s", PrintValue(inst).c_str()); |
| 590 | |
| 591 | switch (inst->getOpcode()) |
| 592 | { |
| 593 | default: |
| 594 | break; |
| 595 | case Instruction::Add: |
| 596 | case Instruction::Sub: |
| 597 | case Instruction::Mul: |
| 598 | case Instruction::SDiv: |
| 599 | case Instruction::UDiv: |
| 600 | case Instruction::SRem: |
| 601 | case Instruction::URem: |
| 602 | case Instruction::Shl: |
| 603 | case Instruction::LShr: |
| 604 | case Instruction::AShr: |
| 605 | case Instruction::And: |
| 606 | case Instruction::Or: |
| 607 | case Instruction::Xor: |
| 608 | { |
| 609 | const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst); |
| 610 | |
| 611 | if (!bin_op) |
| 612 | { |
| 613 | if (log) |
| 614 | log->Printf("getOpcode() returns %s, but instruction is not a BinaryOperator", inst->getOpcodeName()); |
| 615 | error.SetErrorToGenericError(); |
| 616 | error.SetErrorString(interpreter_internal_error); |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | Value *lhs = inst->getOperand(0); |
| 621 | Value *rhs = inst->getOperand(1); |
| 622 | |
| 623 | lldb_private::Scalar L; |
| 624 | lldb_private::Scalar R; |
| 625 | |
| 626 | if (!frame.EvaluateValue(L, lhs, module)) |
| 627 | { |
| 628 | if (log) |
| 629 | log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); |
| 630 | error.SetErrorToGenericError(); |
| 631 | error.SetErrorString(bad_value_error); |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | if (!frame.EvaluateValue(R, rhs, module)) |
| 636 | { |
| 637 | if (log) |
| 638 | log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); |
| 639 | error.SetErrorToGenericError(); |
| 640 | error.SetErrorString(bad_value_error); |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | lldb_private::Scalar result; |
| 645 | |
| 646 | switch (inst->getOpcode()) |
| 647 | { |
| 648 | default: |
| 649 | break; |
| 650 | case Instruction::Add: |
| 651 | result = L + R; |
| 652 | break; |
| 653 | case Instruction::Mul: |
| 654 | result = L * R; |
| 655 | break; |
| 656 | case Instruction::Sub: |
| 657 | result = L - R; |
| 658 | break; |
| 659 | case Instruction::SDiv: |
| 660 | result = L / R; |
| 661 | break; |
| 662 | case Instruction::UDiv: |
| 663 | result = L.GetRawBits64(0) / R.GetRawBits64(1); |
| 664 | break; |
| 665 | case Instruction::SRem: |
| 666 | result = L % R; |
| 667 | break; |
| 668 | case Instruction::URem: |
| 669 | result = L.GetRawBits64(0) % R.GetRawBits64(1); |
| 670 | break; |
| 671 | case Instruction::Shl: |
| 672 | result = L << R; |
| 673 | break; |
| 674 | case Instruction::AShr: |
| 675 | result = L >> R; |
| 676 | break; |
| 677 | case Instruction::LShr: |
| 678 | result = L; |
| 679 | result.ShiftRightLogical(R); |
| 680 | break; |
| 681 | case Instruction::And: |
| 682 | result = L & R; |
| 683 | break; |
| 684 | case Instruction::Or: |
| 685 | result = L | R; |
| 686 | break; |
| 687 | case Instruction::Xor: |
| 688 | result = L ^ R; |
| 689 | break; |
| 690 | } |
| 691 | |
| 692 | frame.AssignValue(inst, result, module); |
| 693 | |
| 694 | if (log) |
| 695 | { |
| 696 | log->Printf("Interpreted a %s", inst->getOpcodeName()); |
| 697 | log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); |
| 698 | log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); |
| 699 | log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); |
| 700 | } |
| 701 | } |
| 702 | break; |
| 703 | case Instruction::Alloca: |
| 704 | { |
| 705 | const AllocaInst *alloca_inst = dyn_cast<AllocaInst>(inst); |
| 706 | |
| 707 | if (!alloca_inst) |
| 708 | { |
| 709 | if (log) |
| 710 | log->Printf("getOpcode() returns Alloca, but instruction is not an AllocaInst"); |
| 711 | error.SetErrorToGenericError(); |
| 712 | error.SetErrorString(interpreter_internal_error); |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | if (alloca_inst->isArrayAllocation()) |
| 717 | { |
| 718 | if (log) |
| 719 | log->Printf("AllocaInsts are not handled if isArrayAllocation() is true"); |
| 720 | error.SetErrorToGenericError(); |
| 721 | error.SetErrorString(unsupported_opcode_error); |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | // The semantics of Alloca are: |
| 726 | // Create a region R of virtual memory of type T, backed by a data buffer |
| 727 | // Create a region P of virtual memory of type T*, backed by a data buffer |
| 728 | // Write the virtual address of R into P |
| 729 | |
| 730 | Type *T = alloca_inst->getAllocatedType(); |
| 731 | Type *Tptr = alloca_inst->getType(); |
| 732 | |
| 733 | lldb::addr_t R = frame.Malloc(T); |
| 734 | |
| 735 | if (R == LLDB_INVALID_ADDRESS) |
| 736 | { |
| 737 | if (log) |
| 738 | log->Printf("Couldn't allocate memory for an AllocaInst"); |
| 739 | error.SetErrorToGenericError(); |
| 740 | error.SetErrorString(memory_allocation_error); |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | lldb::addr_t P = frame.Malloc(Tptr); |
| 745 | |
| 746 | if (P == LLDB_INVALID_ADDRESS) |
| 747 | { |
| 748 | if (log) |
| 749 | log->Printf("Couldn't allocate the result pointer for an AllocaInst"); |
| 750 | error.SetErrorToGenericError(); |
| 751 | error.SetErrorString(memory_allocation_error); |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | lldb_private::Error write_error; |
| 756 | |
| 757 | memory_map.WritePointerToMemory(P, R, write_error); |
| 758 | |
| 759 | if (!write_error.Success()) |
| 760 | { |
| 761 | if (log) |
| 762 | log->Printf("Couldn't write the result pointer for an AllocaInst"); |
| 763 | error.SetErrorToGenericError(); |
| 764 | error.SetErrorString(memory_write_error); |
| 765 | lldb_private::Error free_error; |
| 766 | memory_map.Free(P, free_error); |
| 767 | memory_map.Free(R, free_error); |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | frame.m_values[alloca_inst] = P; |
| 772 | |
| 773 | if (log) |
| 774 | { |
| 775 | log->Printf("Interpreted an AllocaInst"); |
| 776 | log->Printf(" R : 0x%llx", R); |
| 777 | log->Printf(" P : 0x%llx", P); |
| 778 | } |
| 779 | } |
| 780 | break; |
| 781 | case Instruction::BitCast: |
| 782 | case Instruction::ZExt: |
| 783 | { |
| 784 | const CastInst *cast_inst = dyn_cast<CastInst>(inst); |
| 785 | |
| 786 | if (!cast_inst) |
| 787 | { |
| 788 | if (log) |
| 789 | log->Printf("getOpcode() returns %s, but instruction is not a BitCastInst", cast_inst->getOpcodeName()); |
| 790 | error.SetErrorToGenericError(); |
| 791 | error.SetErrorString(interpreter_internal_error); |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | Value *source = cast_inst->getOperand(0); |
| 796 | |
| 797 | lldb_private::Scalar S; |
| 798 | |
| 799 | if (!frame.EvaluateValue(S, source, module)) |
| 800 | { |
| 801 | if (log) |
| 802 | log->Printf("Couldn't evaluate %s", PrintValue(source).c_str()); |
| 803 | error.SetErrorToGenericError(); |
| 804 | error.SetErrorString(bad_value_error); |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | frame.AssignValue(inst, S, module); |
| 809 | } |
| 810 | break; |
| 811 | case Instruction::Br: |
| 812 | { |
| 813 | const BranchInst *br_inst = dyn_cast<BranchInst>(inst); |
| 814 | |
| 815 | if (!br_inst) |
| 816 | { |
| 817 | if (log) |
| 818 | log->Printf("getOpcode() returns Br, but instruction is not a BranchInst"); |
| 819 | error.SetErrorToGenericError(); |
| 820 | error.SetErrorString(interpreter_internal_error); |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | if (br_inst->isConditional()) |
| 825 | { |
| 826 | Value *condition = br_inst->getCondition(); |
| 827 | |
| 828 | lldb_private::Scalar C; |
| 829 | |
| 830 | if (!frame.EvaluateValue(C, condition, module)) |
| 831 | { |
| 832 | if (log) |
| 833 | log->Printf("Couldn't evaluate %s", PrintValue(condition).c_str()); |
| 834 | error.SetErrorToGenericError(); |
| 835 | error.SetErrorString(bad_value_error); |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | if (C.GetRawBits64(0)) |
| 840 | frame.Jump(br_inst->getSuccessor(0)); |
| 841 | else |
| 842 | frame.Jump(br_inst->getSuccessor(1)); |
| 843 | |
| 844 | if (log) |
| 845 | { |
| 846 | log->Printf("Interpreted a BrInst with a condition"); |
| 847 | log->Printf(" cond : %s", frame.SummarizeValue(condition).c_str()); |
| 848 | } |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | frame.Jump(br_inst->getSuccessor(0)); |
| 853 | |
| 854 | if (log) |
| 855 | { |
| 856 | log->Printf("Interpreted a BrInst with no condition"); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | continue; |
| 861 | case Instruction::GetElementPtr: |
| 862 | { |
| 863 | const GetElementPtrInst *gep_inst = dyn_cast<GetElementPtrInst>(inst); |
| 864 | |
| 865 | if (!gep_inst) |
| 866 | { |
| 867 | if (log) |
| 868 | log->Printf("getOpcode() returns GetElementPtr, but instruction is not a GetElementPtrInst"); |
| 869 | error.SetErrorToGenericError(); |
| 870 | error.SetErrorString(interpreter_internal_error); |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | const Value *pointer_operand = gep_inst->getPointerOperand(); |
| 875 | Type *pointer_type = pointer_operand->getType(); |
| 876 | |
| 877 | lldb_private::Scalar P; |
| 878 | |
| 879 | if (!frame.EvaluateValue(P, pointer_operand, module)) |
| 880 | { |
| 881 | if (log) |
| 882 | log->Printf("Couldn't evaluate %s", PrintValue(pointer_operand).c_str()); |
| 883 | error.SetErrorToGenericError(); |
| 884 | error.SetErrorString(bad_value_error); |
| 885 | return false; |
| 886 | } |
| 887 | |
| 888 | typedef SmallVector <Value *, 8> IndexVector; |
| 889 | typedef IndexVector::iterator IndexIterator; |
| 890 | |
| 891 | SmallVector <Value *, 8> indices (gep_inst->idx_begin(), |
| 892 | gep_inst->idx_end()); |
| 893 | |
| 894 | SmallVector <Value *, 8> const_indices; |
| 895 | |
| 896 | for (IndexIterator ii = indices.begin(), ie = indices.end(); |
| 897 | ii != ie; |
| 898 | ++ii) |
| 899 | { |
| 900 | ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii); |
| 901 | |
| 902 | if (!constant_index) |
| 903 | { |
| 904 | lldb_private::Scalar I; |
| 905 | |
| 906 | if (!frame.EvaluateValue(I, *ii, module)) |
| 907 | { |
| 908 | if (log) |
| 909 | log->Printf("Couldn't evaluate %s", PrintValue(*ii).c_str()); |
| 910 | error.SetErrorToGenericError(); |
| 911 | error.SetErrorString(bad_value_error); |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | if (log) |
| 916 | log->Printf("Evaluated constant index %s as %llu", PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS)); |
| 917 | |
| 918 | constant_index = cast<ConstantInt>(ConstantInt::get((*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS))); |
| 919 | } |
| 920 | |
| 921 | const_indices.push_back(constant_index); |
| 922 | } |
| 923 | |
| 924 | uint64_t offset = data_layout.getIndexedOffset(pointer_type, const_indices); |
| 925 | |
| 926 | lldb_private::Scalar Poffset = P + offset; |
| 927 | |
| 928 | frame.AssignValue(inst, Poffset, module); |
| 929 | |
| 930 | if (log) |
| 931 | { |
| 932 | log->Printf("Interpreted a GetElementPtrInst"); |
| 933 | log->Printf(" P : %s", frame.SummarizeValue(pointer_operand).c_str()); |
| 934 | log->Printf(" Poffset : %s", frame.SummarizeValue(inst).c_str()); |
| 935 | } |
| 936 | } |
| 937 | break; |
| 938 | case Instruction::ICmp: |
| 939 | { |
| 940 | const ICmpInst *icmp_inst = dyn_cast<ICmpInst>(inst); |
| 941 | |
| 942 | if (!icmp_inst) |
| 943 | { |
| 944 | if (log) |
| 945 | log->Printf("getOpcode() returns ICmp, but instruction is not an ICmpInst"); |
| 946 | error.SetErrorToGenericError(); |
| 947 | error.SetErrorString(interpreter_internal_error); |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | CmpInst::Predicate predicate = icmp_inst->getPredicate(); |
| 952 | |
| 953 | Value *lhs = inst->getOperand(0); |
| 954 | Value *rhs = inst->getOperand(1); |
| 955 | |
| 956 | lldb_private::Scalar L; |
| 957 | lldb_private::Scalar R; |
| 958 | |
| 959 | if (!frame.EvaluateValue(L, lhs, module)) |
| 960 | { |
| 961 | if (log) |
| 962 | log->Printf("Couldn't evaluate %s", PrintValue(lhs).c_str()); |
| 963 | error.SetErrorToGenericError(); |
| 964 | error.SetErrorString(bad_value_error); |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | if (!frame.EvaluateValue(R, rhs, module)) |
| 969 | { |
| 970 | if (log) |
| 971 | log->Printf("Couldn't evaluate %s", PrintValue(rhs).c_str()); |
| 972 | error.SetErrorToGenericError(); |
| 973 | error.SetErrorString(bad_value_error); |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | lldb_private::Scalar result; |
| 978 | |
| 979 | switch (predicate) |
| 980 | { |
| 981 | default: |
| 982 | return false; |
| 983 | case CmpInst::ICMP_EQ: |
| 984 | result = (L == R); |
| 985 | break; |
| 986 | case CmpInst::ICMP_NE: |
| 987 | result = (L != R); |
| 988 | break; |
| 989 | case CmpInst::ICMP_UGT: |
| 990 | result = (L.GetRawBits64(0) > R.GetRawBits64(0)); |
| 991 | break; |
| 992 | case CmpInst::ICMP_UGE: |
| 993 | result = (L.GetRawBits64(0) >= R.GetRawBits64(0)); |
| 994 | break; |
| 995 | case CmpInst::ICMP_ULT: |
| 996 | result = (L.GetRawBits64(0) < R.GetRawBits64(0)); |
| 997 | break; |
| 998 | case CmpInst::ICMP_ULE: |
| 999 | result = (L.GetRawBits64(0) <= R.GetRawBits64(0)); |
| 1000 | break; |
| 1001 | case CmpInst::ICMP_SGT: |
| 1002 | result = (L > R); |
| 1003 | break; |
| 1004 | case CmpInst::ICMP_SGE: |
| 1005 | result = (L >= R); |
| 1006 | break; |
| 1007 | case CmpInst::ICMP_SLT: |
| 1008 | result = (L < R); |
| 1009 | break; |
| 1010 | case CmpInst::ICMP_SLE: |
| 1011 | result = (L <= R); |
| 1012 | break; |
| 1013 | } |
| 1014 | |
| 1015 | frame.AssignValue(inst, result, module); |
| 1016 | |
| 1017 | if (log) |
| 1018 | { |
| 1019 | log->Printf("Interpreted an ICmpInst"); |
| 1020 | log->Printf(" L : %s", frame.SummarizeValue(lhs).c_str()); |
| 1021 | log->Printf(" R : %s", frame.SummarizeValue(rhs).c_str()); |
| 1022 | log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); |
| 1023 | } |
| 1024 | } |
| 1025 | break; |
| 1026 | case Instruction::IntToPtr: |
| 1027 | { |
| 1028 | const IntToPtrInst *int_to_ptr_inst = dyn_cast<IntToPtrInst>(inst); |
| 1029 | |
| 1030 | if (!int_to_ptr_inst) |
| 1031 | { |
| 1032 | if (log) |
| 1033 | log->Printf("getOpcode() returns IntToPtr, but instruction is not an IntToPtrInst"); |
| 1034 | error.SetErrorToGenericError(); |
| 1035 | error.SetErrorString(interpreter_internal_error); |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
| 1039 | Value *src_operand = int_to_ptr_inst->getOperand(0); |
| 1040 | |
| 1041 | lldb_private::Scalar I; |
| 1042 | |
| 1043 | if (!frame.EvaluateValue(I, src_operand, module)) |
| 1044 | { |
| 1045 | if (log) |
| 1046 | log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); |
| 1047 | error.SetErrorToGenericError(); |
| 1048 | error.SetErrorString(bad_value_error); |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | frame.AssignValue(inst, I, module); |
| 1053 | |
| 1054 | if (log) |
| 1055 | { |
| 1056 | log->Printf("Interpreted an IntToPtr"); |
| 1057 | log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); |
| 1058 | log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); |
| 1059 | } |
| 1060 | } |
| 1061 | break; |
| 1062 | case Instruction::PtrToInt: |
| 1063 | { |
| 1064 | const PtrToIntInst *ptr_to_int_inst = dyn_cast<PtrToIntInst>(inst); |
| 1065 | |
| 1066 | if (!ptr_to_int_inst) |
| 1067 | { |
| 1068 | if (log) |
| 1069 | log->Printf("getOpcode() returns PtrToInt, but instruction is not an PtrToIntInst"); |
| 1070 | error.SetErrorToGenericError(); |
| 1071 | error.SetErrorString(interpreter_internal_error); |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | Value *src_operand = ptr_to_int_inst->getOperand(0); |
| 1076 | |
| 1077 | lldb_private::Scalar I; |
| 1078 | |
| 1079 | if (!frame.EvaluateValue(I, src_operand, module)) |
| 1080 | { |
| 1081 | if (log) |
| 1082 | log->Printf("Couldn't evaluate %s", PrintValue(src_operand).c_str()); |
| 1083 | error.SetErrorToGenericError(); |
| 1084 | error.SetErrorString(bad_value_error); |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | frame.AssignValue(inst, I, module); |
| 1089 | |
| 1090 | if (log) |
| 1091 | { |
| 1092 | log->Printf("Interpreted a PtrToInt"); |
| 1093 | log->Printf(" Src : %s", frame.SummarizeValue(src_operand).c_str()); |
| 1094 | log->Printf(" = : %s", frame.SummarizeValue(inst).c_str()); |
| 1095 | } |
| 1096 | } |
| 1097 | break; |
| 1098 | case Instruction::Load: |
| 1099 | { |
| 1100 | const LoadInst *load_inst = dyn_cast<LoadInst>(inst); |
| 1101 | |
| 1102 | if (!load_inst) |
| 1103 | { |
| 1104 | if (log) |
| 1105 | log->Printf("getOpcode() returns Load, but instruction is not a LoadInst"); |
| 1106 | error.SetErrorToGenericError(); |
| 1107 | error.SetErrorString(interpreter_internal_error); |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | // The semantics of Load are: |
| 1112 | // Create a region D that will contain the loaded data |
| 1113 | // Resolve the region P containing a pointer |
| 1114 | // Dereference P to get the region R that the data should be loaded from |
| 1115 | // Transfer a unit of type type(D) from R to D |
| 1116 | |
| 1117 | const Value *pointer_operand = load_inst->getPointerOperand(); |
| 1118 | |
| 1119 | Type *pointer_ty = pointer_operand->getType(); |
| 1120 | PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); |
| 1121 | if (!pointer_ptr_ty) |
| 1122 | { |
| 1123 | if (log) |
| 1124 | log->Printf("getPointerOperand()->getType() is not a PointerType"); |
| 1125 | error.SetErrorToGenericError(); |
| 1126 | error.SetErrorString(interpreter_internal_error); |
| 1127 | return false; |
| 1128 | } |
| 1129 | Type *target_ty = pointer_ptr_ty->getElementType(); |
| 1130 | |
| 1131 | lldb::addr_t D = frame.ResolveValue(load_inst, module); |
| 1132 | lldb::addr_t P = frame.ResolveValue(pointer_operand, module); |
| 1133 | |
| 1134 | if (D == LLDB_INVALID_ADDRESS) |
| 1135 | { |
| 1136 | if (log) |
| 1137 | log->Printf("LoadInst's value doesn't resolve to anything"); |
| 1138 | error.SetErrorToGenericError(); |
| 1139 | error.SetErrorString(bad_value_error); |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | if (P == LLDB_INVALID_ADDRESS) |
| 1144 | { |
| 1145 | if (log) |
| 1146 | log->Printf("LoadInst's pointer doesn't resolve to anything"); |
| 1147 | error.SetErrorToGenericError(); |
| 1148 | error.SetErrorString(bad_value_error); |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | lldb::addr_t R; |
| 1153 | lldb_private::Error read_error; |
| 1154 | memory_map.ReadPointerFromMemory(&R, P, read_error); |
| 1155 | |
| 1156 | if (!read_error.Success()) |
| 1157 | { |
| 1158 | if (log) |
| 1159 | log->Printf("Couldn't read the address to be loaded for a LoadInst"); |
| 1160 | error.SetErrorToGenericError(); |
| 1161 | error.SetErrorString(memory_read_error); |
| 1162 | return false; |
| 1163 | } |
| 1164 | |
| 1165 | size_t target_size = data_layout.getTypeStoreSize(target_ty); |
| 1166 | lldb_private::DataBufferHeap buffer(target_size, 0); |
| 1167 | |
| 1168 | read_error.Clear(); |
| 1169 | memory_map.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(), read_error); |
| 1170 | if (!read_error.Success()) |
| 1171 | { |
| 1172 | if (log) |
| 1173 | log->Printf("Couldn't read from a region on behalf of a LoadInst"); |
| 1174 | error.SetErrorToGenericError(); |
| 1175 | error.SetErrorString(memory_read_error); |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
| 1179 | lldb_private::Error write_error; |
| 1180 | memory_map.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(), write_error); |
| 1181 | if (!write_error.Success()) |
| 1182 | { |
| 1183 | if (log) |
| 1184 | log->Printf("Couldn't write to a region on behalf of a LoadInst"); |
| 1185 | error.SetErrorToGenericError(); |
| 1186 | error.SetErrorString(memory_read_error); |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | if (log) |
| 1191 | { |
| 1192 | log->Printf("Interpreted a LoadInst"); |
| 1193 | log->Printf(" P : 0x%llx", P); |
| 1194 | log->Printf(" R : 0x%llx", R); |
| 1195 | log->Printf(" D : 0x%llx", D); |
| 1196 | } |
| 1197 | } |
| 1198 | break; |
| 1199 | case Instruction::Ret: |
| 1200 | { |
| 1201 | return true; |
| 1202 | } |
| 1203 | case Instruction::Store: |
| 1204 | { |
| 1205 | const StoreInst *store_inst = dyn_cast<StoreInst>(inst); |
| 1206 | |
| 1207 | if (!store_inst) |
| 1208 | { |
| 1209 | if (log) |
| 1210 | log->Printf("getOpcode() returns Store, but instruction is not a StoreInst"); |
| 1211 | error.SetErrorToGenericError(); |
| 1212 | error.SetErrorString(interpreter_internal_error); |
| 1213 | return false; |
| 1214 | } |
| 1215 | |
| 1216 | // The semantics of Store are: |
| 1217 | // Resolve the region D containing the data to be stored |
| 1218 | // Resolve the region P containing a pointer |
| 1219 | // Dereference P to get the region R that the data should be stored in |
| 1220 | // Transfer a unit of type type(D) from D to R |
| 1221 | |
| 1222 | const Value *value_operand = store_inst->getValueOperand(); |
| 1223 | const Value *pointer_operand = store_inst->getPointerOperand(); |
| 1224 | |
| 1225 | Type *pointer_ty = pointer_operand->getType(); |
| 1226 | PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty); |
| 1227 | if (!pointer_ptr_ty) |
| 1228 | return false; |
| 1229 | Type *target_ty = pointer_ptr_ty->getElementType(); |
| 1230 | |
| 1231 | lldb::addr_t D = frame.ResolveValue(value_operand, module); |
| 1232 | lldb::addr_t P = frame.ResolveValue(pointer_operand, module); |
| 1233 | |
| 1234 | if (D == LLDB_INVALID_ADDRESS) |
| 1235 | { |
| 1236 | if (log) |
| 1237 | log->Printf("StoreInst's value doesn't resolve to anything"); |
| 1238 | error.SetErrorToGenericError(); |
| 1239 | error.SetErrorString(bad_value_error); |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
| 1243 | if (P == LLDB_INVALID_ADDRESS) |
| 1244 | { |
| 1245 | if (log) |
| 1246 | log->Printf("StoreInst's pointer doesn't resolve to anything"); |
| 1247 | error.SetErrorToGenericError(); |
| 1248 | error.SetErrorString(bad_value_error); |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | lldb::addr_t R; |
| 1253 | lldb_private::Error read_error; |
| 1254 | memory_map.ReadPointerFromMemory(&R, P, read_error); |
| 1255 | |
| 1256 | if (!read_error.Success()) |
| 1257 | { |
| 1258 | if (log) |
| 1259 | log->Printf("Couldn't read the address to be loaded for a LoadInst"); |
| 1260 | error.SetErrorToGenericError(); |
| 1261 | error.SetErrorString(memory_read_error); |
| 1262 | return false; |
| 1263 | } |
| 1264 | |
| 1265 | size_t target_size = data_layout.getTypeStoreSize(target_ty); |
| 1266 | lldb_private::DataBufferHeap buffer(target_size, 0); |
| 1267 | |
| 1268 | read_error.Clear(); |
| 1269 | memory_map.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(), read_error); |
| 1270 | if (!read_error.Success()) |
| 1271 | { |
| 1272 | if (log) |
| 1273 | log->Printf("Couldn't read from a region on behalf of a StoreInst"); |
| 1274 | error.SetErrorToGenericError(); |
| 1275 | error.SetErrorString(memory_read_error); |
| 1276 | return false; |
| 1277 | } |
| 1278 | |
| 1279 | lldb_private::Error write_error; |
| 1280 | memory_map.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(), write_error); |
| 1281 | if (!write_error.Success()) |
| 1282 | { |
| 1283 | if (log) |
| 1284 | log->Printf("Couldn't write to a region on behalf of a StoreInst"); |
| 1285 | error.SetErrorToGenericError(); |
Sean Callanan | 49630e7 | 2013-04-20 02:39:24 +0000 | [diff] [blame] | 1286 | error.SetErrorString(memory_write_error); |
Sean Callanan | 14cb2aa | 2013-04-17 18:35:47 +0000 | [diff] [blame] | 1287 | return false; |
| 1288 | } |
| 1289 | |
| 1290 | if (log) |
| 1291 | { |
| 1292 | log->Printf("Interpreted a StoreInst"); |
| 1293 | log->Printf(" D : 0x%llx", D); |
| 1294 | log->Printf(" P : 0x%llx", P); |
| 1295 | log->Printf(" R : 0x%llx", R); |
| 1296 | } |
| 1297 | } |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | ++frame.m_ii; |
| 1302 | } |
| 1303 | |
| 1304 | if (num_insts >= 4096) |
| 1305 | { |
| 1306 | error.SetErrorToGenericError(); |
| 1307 | error.SetErrorString(infinite_loop_error); |
| 1308 | return false; |
| 1309 | } |
| 1310 | |
| 1311 | return false; |
| 1312 | } |