blob: 0a5594a5a97960650c3d1fbc8feb2566062e1664 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Clayton5c4c7462010-10-06 03:09:58 +000012#include "lldb/API/SBAddress.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000013#include "lldb/API/SBFrame.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +000014#include "lldb/API/SBInstruction.h"
15#include "lldb/API/SBStream.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000016#include "lldb/API/SBTarget.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +000017
Caroline Ticeaf591802011-04-05 23:22:54 +000018#include "lldb/Core/ArchSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Disassembler.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000020#include "lldb/Core/EmulateInstruction.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +000021#include "lldb/Core/StreamFile.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000022#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/StackFrame.h"
24#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
Chris Lattner24943d22010-06-08 16:52:24 +000029SBInstruction::SBInstruction ()
30{
31}
32
Greg Clayton5c4c7462010-10-06 03:09:58 +000033SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) :
34 m_opaque_sp (inst_sp)
35{
36}
37
Greg Clayton538eb822010-11-05 23:17:00 +000038SBInstruction::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
Chris Lattner24943d22010-06-08 16:52:24 +000051SBInstruction::~SBInstruction ()
52{
53}
54
Greg Clayton5c4c7462010-10-06 03:09:58 +000055bool
56SBInstruction::IsValid()
57{
58 return (m_opaque_sp.get() != NULL);
59}
Chris Lattner24943d22010-06-08 16:52:24 +000060
Greg Clayton5c4c7462010-10-06 03:09:58 +000061SBAddress
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}
Chris Lattner24943d22010-06-08 16:52:24 +000069
Greg Clayton5c4c7462010-10-06 03:09:58 +000070size_t
71SBInstruction::GetByteSize ()
72{
73 if (m_opaque_sp)
Greg Clayton149731c2011-03-25 18:03:16 +000074 return m_opaque_sp->GetOpcode().GetByteSize();
Greg Clayton5c4c7462010-10-06 03:09:58 +000075 return 0;
76}
Chris Lattner24943d22010-06-08 16:52:24 +000077
Greg Clayton5c4c7462010-10-06 03:09:58 +000078bool
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...
Greg Clayton889fbd02011-03-26 19:14:58 +000099 m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000100 return true;
101 }
102 return false;
103}
Chris Lattner24943d22010-06-08 16:52:24 +0000104
105void
106SBInstruction::Print (FILE *out)
107{
108 if (out == NULL)
109 return;
110
Greg Clayton5c4c7462010-10-06 03:09:58 +0000111 if (m_opaque_sp)
112 {
Greg Clayton58928562011-02-09 01:08:52 +0000113 StreamFile out_stream (out, false);
Greg Clayton889fbd02011-03-26 19:14:58 +0000114 m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000115 }
Chris Lattner24943d22010-06-08 16:52:24 +0000116}
Caroline Ticeaf591802011-04-05 23:22:54 +0000117
118bool
Greg Clayton888a7332011-04-26 04:39:08 +0000119SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options)
Caroline Ticeaf591802011-04-05 23:22:54 +0000120{
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 Clayton888a7332011-04-26 04:39:08 +0000129 evaluate_options,
Caroline Ticeaf591802011-04-05 23:22:54 +0000130 (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 {
Greg Claytonf15996e2011-04-07 22:46:35 +0000144 lldb_private::ArchSpec arch (triple, NULL);
Caroline Ticeaf591802011-04-05 23:22:54 +0000145
Caroline Tice0fe5a532011-04-08 23:33:06 +0000146 return m_opaque_sp->DumpEmulation (arch);
147
Caroline Ticeaf591802011-04-05 23:22:54 +0000148 }
149 return false;
150}
151
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000152bool
153SBInstruction::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 Chen09008d02011-04-21 20:27:45 +0000158 return m_opaque_sp->TestEmulation (output_stream.get(), test_file);
159}