SBInstruction.cpp revision 0fe5a535b87841a5c422f4a79d55c21bf07b50ca
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
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"
17
18#include "lldb/Core/ArchSpec.h"
19#include "lldb/Core/Disassembler.h"
20#include "lldb/Core/EmulateInstruction.h"
21#include "lldb/Core/StreamFile.h"
22#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/StackFrame.h"
24#include "lldb/Target/Target.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29SBInstruction::SBInstruction ()
30{
31}
32
33SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) :
34    m_opaque_sp (inst_sp)
35{
36}
37
38SBInstruction::SBInstruction(const SBInstruction &rhs) :
39    m_opaque_sp (rhs.m_opaque_sp)
40{
41}
42
43const SBInstruction &
44SBInstruction::operator = (const SBInstruction &rhs)
45{
46    if (this != &rhs)
47        m_opaque_sp = rhs.m_opaque_sp;
48    return *this;
49}
50
51SBInstruction::~SBInstruction ()
52{
53}
54
55bool
56SBInstruction::IsValid()
57{
58    return (m_opaque_sp.get() != NULL);
59}
60
61SBAddress
62SBInstruction::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}
69
70size_t
71SBInstruction::GetByteSize ()
72{
73    if (m_opaque_sp)
74        return m_opaque_sp->GetOpcode().GetByteSize();
75    return 0;
76}
77
78bool
79SBInstruction::DoesBranch ()
80{
81    if (m_opaque_sp)
82        return m_opaque_sp->DoesBranch ();
83    return false;
84}
85
86void
87SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp)
88{
89    m_opaque_sp = inst_sp;
90}
91
92bool
93SBInstruction::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...
99        m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
100        return true;
101    }
102    return false;
103}
104
105void
106SBInstruction::Print (FILE *out)
107{
108    if (out == NULL)
109        return;
110
111    if (m_opaque_sp)
112    {
113        StreamFile out_stream (out, false);
114        m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false);
115    }
116}
117
118bool
119SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, bool auto_advance_pc)
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,
129                                     auto_advance_pc,
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
139bool
140SBInstruction::DumpEmulation (const char *triple)
141{
142    if (m_opaque_sp && triple)
143    {
144        lldb_private::ArchSpec arch (triple, NULL);
145
146        return m_opaque_sp->DumpEmulation (arch);
147
148    }
149    return false;
150}
151
152