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