Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBInstruction.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/API/SBInstruction.h" |
| 11 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBAddress.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 13 | #include "lldb/API/SBFrame.h" |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 14 | #include "lldb/API/SBInstruction.h" |
| 15 | #include "lldb/API/SBStream.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 16 | #include "lldb/API/SBTarget.h" |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 17 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ArchSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Disassembler.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 20 | #include "lldb/Core/EmulateInstruction.h" |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 21 | #include "lldb/Core/StreamFile.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 22 | #include "lldb/Target/ExecutionContext.h" |
| 23 | #include "lldb/Target/StackFrame.h" |
| 24 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | SBInstruction::SBInstruction () |
| 30 | { |
| 31 | } |
| 32 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 33 | SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) : |
| 34 | m_opaque_sp (inst_sp) |
| 35 | { |
| 36 | } |
| 37 | |
Greg Clayton | 538eb82 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 38 | SBInstruction::SBInstruction(const SBInstruction &rhs) : |
| 39 | m_opaque_sp (rhs.m_opaque_sp) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | const SBInstruction & |
| 44 | SBInstruction::operator = (const SBInstruction &rhs) |
| 45 | { |
| 46 | if (this != &rhs) |
| 47 | m_opaque_sp = rhs.m_opaque_sp; |
| 48 | return *this; |
| 49 | } |
| 50 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | SBInstruction::~SBInstruction () |
| 52 | { |
| 53 | } |
| 54 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 55 | bool |
| 56 | SBInstruction::IsValid() |
| 57 | { |
| 58 | return (m_opaque_sp.get() != NULL); |
| 59 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 61 | SBAddress |
| 62 | SBInstruction::GetAddress() |
| 63 | { |
| 64 | SBAddress sb_addr; |
| 65 | if (m_opaque_sp && m_opaque_sp->GetAddress().IsValid()) |
| 66 | sb_addr.SetAddress(&m_opaque_sp->GetAddress()); |
| 67 | return sb_addr; |
| 68 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 70 | size_t |
| 71 | SBInstruction::GetByteSize () |
| 72 | { |
| 73 | if (m_opaque_sp) |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 74 | return m_opaque_sp->GetOpcode().GetByteSize(); |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 78 | bool |
| 79 | SBInstruction::DoesBranch () |
| 80 | { |
| 81 | if (m_opaque_sp) |
| 82 | return m_opaque_sp->DoesBranch (); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp) |
| 88 | { |
| 89 | m_opaque_sp = inst_sp; |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | SBInstruction::GetDescription (lldb::SBStream &s) |
| 94 | { |
| 95 | if (m_opaque_sp) |
| 96 | { |
| 97 | // Use the "ref()" instead of the "get()" accessor in case the SBStream |
| 98 | // didn't have a stream already created, one will get created... |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 99 | m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false); |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | |
| 105 | void |
| 106 | SBInstruction::Print (FILE *out) |
| 107 | { |
| 108 | if (out == NULL) |
| 109 | return; |
| 110 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 111 | if (m_opaque_sp) |
| 112 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 113 | StreamFile out_stream (out, false); |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 114 | m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false); |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | } |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 117 | |
| 118 | bool |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 119 | SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options) |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 120 | { |
| 121 | if (m_opaque_sp && frame.get()) |
| 122 | { |
| 123 | lldb_private::ExecutionContext exe_ctx; |
| 124 | frame->CalculateExecutionContext (exe_ctx); |
| 125 | lldb_private::Target *target = exe_ctx.target; |
| 126 | lldb_private::ArchSpec arch = target->GetArchitecture(); |
| 127 | |
| 128 | return m_opaque_sp->Emulate (arch, |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 129 | evaluate_options, |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 130 | (void *) frame.get(), |
| 131 | &lldb_private::EmulateInstruction::ReadMemoryFrame, |
| 132 | &lldb_private::EmulateInstruction::WriteMemoryFrame, |
| 133 | &lldb_private::EmulateInstruction::ReadRegisterFrame, |
| 134 | &lldb_private::EmulateInstruction::WriteRegisterFrame); |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | bool |
| 140 | SBInstruction::DumpEmulation (const char *triple) |
| 141 | { |
| 142 | if (m_opaque_sp && triple) |
| 143 | { |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 144 | lldb_private::ArchSpec arch (triple, NULL); |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 145 | |
Caroline Tice | 0fe5a53 | 2011-04-08 23:33:06 +0000 | [diff] [blame] | 146 | return m_opaque_sp->DumpEmulation (arch); |
| 147 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
Caroline Tice | 6b8d3b5 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 152 | bool |
| 153 | SBInstruction::TestEmulation (lldb::SBStream &output_stream, const char *test_file) |
| 154 | { |
| 155 | if (!m_opaque_sp.get()) |
| 156 | m_opaque_sp.reset (new PseudoInstruction()); |
| 157 | |
Johnny Chen | 09008d0 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 158 | return m_opaque_sp->TestEmulation (output_stream.get(), test_file); |
| 159 | } |