1 //===-- SBInstruction.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBInstruction.h"
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBFrame.h"
14 #include "lldb/API/SBInstruction.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBTarget.h"
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/DataExtractor.h"
21 #include "lldb/Core/Disassembler.h"
22 #include "lldb/Core/EmulateInstruction.h"
23 #include "lldb/Core/StreamFile.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/StackFrame.h"
26 #include "lldb/Target/Target.h"
29 using namespace lldb_private;
31 SBInstruction::SBInstruction ()
35 SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) :
40 SBInstruction::SBInstruction(const SBInstruction &rhs) :
41 m_opaque_sp (rhs.m_opaque_sp)
46 SBInstruction::operator = (const SBInstruction &rhs)
49 m_opaque_sp = rhs.m_opaque_sp;
53 SBInstruction::~SBInstruction ()
58 SBInstruction::IsValid()
60 return (m_opaque_sp.get() != NULL);
64 SBInstruction::GetAddress()
67 if (m_opaque_sp && m_opaque_sp->GetAddress().IsValid())
68 sb_addr.SetAddress(&m_opaque_sp->GetAddress());
73 SBInstruction::GetMnemonic(SBTarget target)
77 Mutex::Locker api_locker;
78 ExecutionContext exe_ctx;
79 TargetSP target_sp (target.GetSP());
82 api_locker.Lock (target_sp->GetAPIMutex());
83 target_sp->CalculateExecutionContext (exe_ctx);
84 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
86 return m_opaque_sp->GetMnemonic(&exe_ctx);
92 SBInstruction::GetOperands(SBTarget target)
96 Mutex::Locker api_locker;
97 ExecutionContext exe_ctx;
98 TargetSP target_sp (target.GetSP());
101 api_locker.Lock (target_sp->GetAPIMutex());
102 target_sp->CalculateExecutionContext (exe_ctx);
103 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
105 return m_opaque_sp->GetOperands(&exe_ctx);
111 SBInstruction::GetComment(SBTarget target)
115 Mutex::Locker api_locker;
116 ExecutionContext exe_ctx;
117 TargetSP target_sp (target.GetSP());
120 api_locker.Lock (target_sp->GetAPIMutex());
121 target_sp->CalculateExecutionContext (exe_ctx);
122 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
124 return m_opaque_sp->GetComment(&exe_ctx);
130 SBInstruction::GetByteSize ()
133 return m_opaque_sp->GetOpcode().GetByteSize();
138 SBInstruction::GetData (SBTarget target)
140 lldb::SBData sb_data;
143 DataExtractorSP data_extractor_sp (new DataExtractor());
144 if (m_opaque_sp->GetData (*data_extractor_sp))
146 sb_data.SetOpaque (data_extractor_sp);
155 SBInstruction::DoesBranch ()
158 return m_opaque_sp->DoesBranch ();
163 SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp)
165 m_opaque_sp = inst_sp;
169 SBInstruction::GetDescription (lldb::SBStream &s)
173 // Use the "ref()" instead of the "get()" accessor in case the SBStream
174 // didn't have a stream already created, one will get created...
175 m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL);
182 SBInstruction::Print (FILE *out)
189 StreamFile out_stream (out, false);
190 m_opaque_sp->Dump (&out_stream, 0, true, false, NULL);
195 SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options)
199 lldb::StackFrameSP frame_sp (frame.GetFrameSP());
203 lldb_private::ExecutionContext exe_ctx;
204 frame_sp->CalculateExecutionContext (exe_ctx);
205 lldb_private::Target *target = exe_ctx.GetTargetPtr();
206 lldb_private::ArchSpec arch = target->GetArchitecture();
208 return m_opaque_sp->Emulate (arch,
210 (void *) frame_sp.get(),
211 &lldb_private::EmulateInstruction::ReadMemoryFrame,
212 &lldb_private::EmulateInstruction::WriteMemoryFrame,
213 &lldb_private::EmulateInstruction::ReadRegisterFrame,
214 &lldb_private::EmulateInstruction::WriteRegisterFrame);
221 SBInstruction::DumpEmulation (const char *triple)
223 if (m_opaque_sp && triple)
225 lldb_private::ArchSpec arch (triple, NULL);
227 return m_opaque_sp->DumpEmulation (arch);
234 SBInstruction::TestEmulation (lldb::SBStream &output_stream, const char *test_file)
236 if (!m_opaque_sp.get())
237 m_opaque_sp.reset (new PseudoInstruction());
239 return m_opaque_sp->TestEmulation (output_stream.get(), test_file);
243 SBInstruction::GetAddressClass ()
245 if (m_opaque_sp.get())
246 return m_opaque_sp->GetAddressClass();
247 return eAddressClassInvalid;