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 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 SBInstruction::SBInstruction ()
30 {
31 }
32 
33 SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) :
34     m_opaque_sp (inst_sp)
35 {
36 }
37 
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 
51 SBInstruction::~SBInstruction ()
52 {
53 }
54 
55 bool
56 SBInstruction::IsValid()
57 {
58     return (m_opaque_sp.get() != NULL);
59 }
60 
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 }
69 
70 size_t
71 SBInstruction::GetByteSize ()
72 {
73     if (m_opaque_sp)
74         return m_opaque_sp->GetOpcode().GetByteSize();
75     return 0;
76 }
77 
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...
99         m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
100         return true;
101     }
102     return false;
103 }
104 
105 void
106 SBInstruction::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 
118 bool
119 SBInstruction::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 
139 bool
140 SBInstruction::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