ROSE 0.11.145.147
Disassembler/Powerpc.h
1/* Disassembly specific to the PowerPC architecture. */
2#ifndef ROSE_BinaryAnalysis_Disassembler_Powerpc_H
3#define ROSE_BinaryAnalysis_Disassembler_Powerpc_H
4#include <featureTests.h>
5#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#include <Rose/BinaryAnalysis/Disassembler/Base.h>
7
8#include <Rose/BinaryAnalysis/Architecture/BasicTypes.h>
9#include <Rose/BinaryAnalysis/InstructionEnumsPowerpc.h>
10
11#include <SgAsmIntegerValueExpression.h>
12
13#include "integerOps.h"
14#include "SageBuilderAsm.h"
15
16#include <Sawyer/BitFlags.h>
17
18namespace Rose {
19namespace BinaryAnalysis {
20namespace Disassembler {
21
23class Powerpc: public Base {
24public:
26 using Ptr = PowerpcPtr;
27
28private:
29 // Per-instruction state
30 struct State {
31 uint64_t ip; // Instruction pointer
32 uint32_t insn; // 4-byte instruction word
33 State(): ip(0), insn(0) {}
34 };
35
36 PowerpcWordSize wordSize_;
39 bool strictReserved_ = false; // if true, then reserved instruction bits must be zero
40
41protected:
42 explicit Powerpc(const Architecture::BaseConstPtr&);
43
44public:
47
54 bool strictReserved() const;
55 void strictReserved(bool);
58 // Overrides documented in a super class
59 virtual ~Powerpc() {}
60 virtual Base::Ptr clone() const;
61 virtual SgAsmInstruction *disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL);
62 virtual void assembleOne(SgAsmInstruction*, SgUnsignedCharList&) {abort();}
64
75private:
76 // Same as Exception except with a different constructor for ease of use in Powerpc. This constructor should be used when
77 // an exception occurs during disassembly of an instruction; it is not suitable for errors that occur before or after (use
78 // superclass constructors for that case).
79 class ExceptionPowerpc: public Exception {
80 public:
81 ExceptionPowerpc(const std::string &mesg, const State &state, size_t bit=0);
82 };
83
85 makeRegister(State&, PowerpcRegisterClass reg_class, int reg_number,
86 PowerpcConditionRegisterAccessGranularity reg_grainularity = powerpc_condreggranularity_whole,
87 SgAsmType *type = NULL) const;
88
93 bool is64bitInsn(PowerpcInstructionKind);
94
96 SgAsmPowerpcInstruction* makeInstructionWithoutOperands(uint64_t address, PowerpcInstructionKind, uint32_t insnBytes,
98
99 // Helper function to use field definitions (counted with bits from left and inclusive on both sides) from manual.
100 template <size_t First, size_t Last> uint64_t fld(State&) const;
101
102 // Helper function to test whether reserved fields have appropriate values
103 template<size_t First, size_t Last> bool reservedOk(State&) const;
104
105 // Decoded fields from section 1.7.16 of the v2.01 UISA.
106 bool AA(State &state) const;
107 SgAsmRegisterReferenceExpression* BA(State &state) const;
108 SgAsmRegisterReferenceExpression* BB(State &state) const;
109 uint64_t BD(State &state) const;
110 SgAsmRegisterReferenceExpression* BF_cr(State &state) const;
111 SgAsmRegisterReferenceExpression* BF_fpscr(State &state) const;
112 SgAsmRegisterReferenceExpression* BFA_cr(State &state) const;
113 SgAsmRegisterReferenceExpression* BFA_fpscr(State &state) const;
114 SgAsmValueExpression* BH(State &state) const;
115 SgAsmRegisterReferenceExpression* BI(State &state) const;
116 SgAsmValueExpression* BO(State &state) const;
117 SgAsmRegisterReferenceExpression* BT(State &state) const;
118 SgAsmValueExpression* D(State &state) const;
119 SgAsmValueExpression* DS(State &state) const;
120 SgAsmValueExpression* FLM(State &state) const;
121 SgAsmRegisterReferenceExpression* FRA(State &state) const;
122 SgAsmRegisterReferenceExpression* FRB(State &state) const;
123 SgAsmRegisterReferenceExpression* FRC(State &state) const;
124 SgAsmRegisterReferenceExpression* FRS(State &state) const;
125 SgAsmRegisterReferenceExpression* FRT(State &state) const;
126 SgAsmValueExpression* FXM(State &state) const;
127 SgAsmValueExpression* L_10(State &state) const;
128 SgAsmValueExpression* L_15(State &state) const;
129 uint8_t L_sync(State &state) const;
130 SgAsmValueExpression* LEV(State &state) const;
131 uint64_t LI(State &state) const;
132 bool LK(State &state) const;
133 SgAsmValueExpression* MB_32bit(State &state) const;
134 SgAsmValueExpression* ME_32bit(State &state) const;
135 SgAsmValueExpression* MB_64bit(State &state) const;
136 SgAsmValueExpression* ME_64bit(State &state) const;
137 SgAsmValueExpression* NB(State &state) const;
138 bool OE(State &state) const;
139 SgAsmRegisterReferenceExpression* RA(State &state) const;
140 SgAsmExpression* RA_or_zero(State &state) const;
141 SgAsmRegisterReferenceExpression* RB(State &state) const;
142 bool Rc(State &state) const;
143 SgAsmRegisterReferenceExpression* RS(State &state) const;
144 SgAsmRegisterReferenceExpression* RT(State &state) const;
145 SgAsmValueExpression* SH_32bit(State &state) const;
146 SgAsmValueExpression* SH_64bit(State &state) const;
147 SgAsmValueExpression* SI(State &state) const;
148 SgAsmRegisterReferenceExpression* SPR(State &state) const;
149 SgAsmRegisterReferenceExpression* SR(State &state) const;
150 SgAsmRegisterReferenceExpression* TBR(State &state) const;
151 SgAsmValueExpression* TH(State &state) const;
152 SgAsmValueExpression* TO(State &state) const;
153 SgAsmValueExpression* U(State &state) const;
154 SgAsmValueExpression* UI(State &state) const;
155 SgAsmMemoryReferenceExpression* memref(State &state, SgAsmType* t) const;
156 SgAsmMemoryReferenceExpression* memrefds(State &state, SgAsmType *t) const;
157 SgAsmMemoryReferenceExpression* memrefra(State &state, SgAsmType *t) const;
158 SgAsmMemoryReferenceExpression* memrefx(State &state, SgAsmType* t) const;
159 SgAsmMemoryReferenceExpression* memrefu(State &state, SgAsmType* t) const;
160 SgAsmMemoryReferenceExpression* memrefux(State &state, SgAsmType* t) const;
161
162 // There are 15 different forms of PowerPC instructions, but all are 32-bit (fixed length instruction set).
163 SgAsmPowerpcInstruction* decode_I_formInstruction(State&);
164 SgAsmPowerpcInstruction* decode_B_formInstruction(State&);
165 SgAsmPowerpcInstruction* decode_SC_formInstruction(State&);
166 SgAsmPowerpcInstruction* decode_DS_formInstruction(State&);
167 SgAsmPowerpcInstruction* decode_X_formInstruction_00(State&);
168 SgAsmPowerpcInstruction* decode_X_formInstruction_1F(State&);
169 SgAsmPowerpcInstruction* decode_X_formInstruction_3F(State&);
170 SgAsmPowerpcInstruction* decode_XL_formInstruction(State&);
171 SgAsmPowerpcInstruction* decode_XS_formInstruction(State&);
172 SgAsmPowerpcInstruction* decode_A_formInstruction_00(State&);
173 SgAsmPowerpcInstruction* decode_A_formInstruction_04(State&);
174 SgAsmPowerpcInstruction* decode_A_formInstruction_3B(State&);
175 SgAsmPowerpcInstruction* decode_A_formInstruction_3F(State&);
176 SgAsmPowerpcInstruction* decode_MD_formInstruction(State&);
177 SgAsmPowerpcInstruction* decode_MDS_formInstruction(State&);
178
179 SgAsmIntegerValueExpression* makeBranchTarget( uint64_t targetAddr ) const;
180
181 SgAsmPowerpcInstruction* disassemble(State&);
182
183 // Initialize instances of this class. Called by constructor.
184 void init();
185
186 // Resets disassembler state to beginning of an instruction.
187 void startInstruction(State &state, rose_addr_t start_va, uint32_t c) const;
188};
189
190} // namespace
191} // namespace
192} // namespace
193
194#endif
195#endif
Virtual base class for instruction disassemblers.
size_t bit
Bit offset in instruction byte sequence where disassembly failed (bit/8 is the index into the "bytes"...
Disassembler for the PowerPC architecture.
virtual Base::Ptr clone() const
Creates a new copy of a disassembler.
const Sawyer::BitFlags< PowerpcCapability > & capabilities() const
Capabilities describe what instructions can be decoded.
Sawyer::BitFlags< PowerpcCapability > & capabilities()
Capabilities describe what instructions can be decoded.
bool strictReserved() const
Property: Whether to enforce reserved instruction fields.
virtual SgAsmInstruction * disassembleOne(const MemoryMap::Ptr &map, rose_addr_t start_va, AddressSet *successors=NULL)
This is the lowest level disassembly function and is implemented in the architecture-specific subclas...
void strictReserved(bool)
Property: Whether to enforce reserved instruction fields.
static Ptr instance(const Architecture::BaseConstPtr &)
Allocating constructor for 32- or 64-bit disassembler.
virtual SgAsmInstruction * makeUnknownInstruction(const Exception &)
Makes an unknown instruction from an exception.
Stores a vector of enum bit flags.
Reference-counting intrusive smart pointer.
Base class for expressions.
Base class for machine instructions.
Base class for integer values.
Reference to memory locations.
Represents one PowerPC machine instruction.
Base class for references to a machine register.
Base class for binary types.
Base class for values.
std::shared_ptr< const Base > BaseConstPtr
Reference counted pointer for Architecture::Base.
PowerpcCapability
Subsets for the PowerPC instruction set.
The ROSE library.