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