ROSE 0.11.145.247
DispatcherAarch32.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_DispatcherAarch32_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_DispatcherAarch32_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_ASM_AARCH32
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Rose/BinaryAnalysis/InstructionEnumsAarch32.h>
8#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
9
10#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
11#include <boost/serialization/access.hpp>
12#include <boost/serialization/base_object.hpp>
13#include <boost/serialization/export.hpp>
14#include <boost/serialization/split_member.hpp>
15#endif
16
17namespace Rose {
18namespace BinaryAnalysis {
19namespace InstructionSemantics {
20
22using DispatcherAarch32Ptr = boost::shared_ptr<class DispatcherAarch32>;
23
24class DispatcherAarch32: public BaseSemantics::Dispatcher {
25public:
27 using Super = BaseSemantics::Dispatcher;
28
30 using Ptr = DispatcherAarch32Ptr;
31
32public:
39 RegisterDescriptor REG_PC, REG_SP, REG_LR;
40 RegisterDescriptor REG_PSTATE_N, REG_PSTATE_Z, REG_PSTATE_C, REG_PSTATE_V, REG_PSTATE_T; // parts of CPSR
41 RegisterDescriptor REG_PSTATE_E, REG_PSTATE_Q, REG_PSTATE_GE;
42 RegisterDescriptor REG_PSTATE_NZCV; // the CPSR N, Z, C, and V bits
43 RegisterDescriptor REG_SPSR, REG_CPSR;
44 RegisterDescriptor REG_DTRTX; // debug registers
45 RegisterDescriptor REG_UNKNOWN; // special ROSE register
48#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
49private:
50 friend class boost::serialization::access;
51
52 template<class S>
53 void save(S &s, const unsigned /*version*/) const {
54 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
55 }
56
57 template<class S>
58 void load(S &s, const unsigned /*version*/) {
59 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
60 initializeRegisterDescriptors();
61 initializeInsnDispatchTable();
62 initializeMemory();
63 }
64
65 BOOST_SERIALIZATION_SPLIT_MEMBER();
66#endif
67
68private:
69 DispatcherAarch32(); // used only by boost::serialization
70
71protected:
72 // prototypical constructor
73 DispatcherAarch32(const Architecture::BaseConstPtr&);
74
75 DispatcherAarch32(const Architecture::BaseConstPtr&, const BaseSemantics::RiscOperatorsPtr&);
76
77public:
78 ~DispatcherAarch32();
79
83 static DispatcherAarch32Ptr instance(const Architecture::BaseConstPtr&);
84
86 static DispatcherAarch32Ptr instance(const Architecture::BaseConstPtr&, const BaseSemantics::RiscOperatorsPtr&);
87
89 virtual BaseSemantics::DispatcherPtr create(const BaseSemantics::RiscOperatorsPtr&) const override;
90
92 static DispatcherAarch32Ptr promote(const BaseSemantics::DispatcherPtr&);
93
94protected:
96 void initializeRegisterDescriptors();
97
101 void initializeInsnDispatchTable();
102
104 void initializeMemory();
105
106protected:
107 int iprocKey(SgAsmInstruction*) const override;
108 RegisterDescriptor instructionPointerRegister() const override;
109 RegisterDescriptor stackPointerRegister() const override;
110 RegisterDescriptor callReturnRegister() const override;
111
112public:
113 BaseSemantics::SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0) override;
114 void write(SgAsmExpression*, const BaseSemantics::SValuePtr&, size_t addr_nbits=0) override;
115
116 // Instruction condition
117 BaseSemantics::SValuePtr conditionHolds(Aarch32InstructionCondition);
118
119 // True if the expression is the program counter register
120 bool isIpRegister(SgAsmExpression*);
121
122 // Read the instruction pointer (PC) register, which is handled in a special way.
124
125 // Is the processor in T32 mode? This is based just on the instruction being executed.
126 BaseSemantics::SValuePtr isT32Mode();
127
128 // Is the processor in A32 mode? This is based just on the instruction being executed.
129 BaseSemantics::SValuePtr isA32Mode();
130
131 // Set T32 mode on or off.
132 void setThumbMode(SgAsmAarch32Instruction*); // on if instructions is 2 bytes; off otherwise
133 void setThumbMode(const BaseSemantics::SValuePtr &state);
134 void setThumbMode(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &state);
135
136 // Conditionally write a value to a register
137 void maybeWriteRegister(const BaseSemantics::SValuePtr &enabled, RegisterDescriptor, const BaseSemantics::SValuePtr &value);
138
139 // Conditionally write a value to memory
140 void maybeWriteMemory(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &address,
141 const BaseSemantics::SValuePtr &value);
142
143 // Conditionally write the the destination described the by ROSE expression, which must also be readable.
144 void maybeWrite(const BaseSemantics::SValuePtr &enabled, SgAsmExpression *destination, const BaseSemantics::SValuePtr &value);
145
146 // Returns the most significant bit.
148
149 // Return the register that's being directly read or written.
150 RegisterDescriptor accessedRegister(SgAsmExpression*);
151
152 // Returns true if the specified value is a constant true, false in all other cases
153 bool mustBeSet(const BaseSemantics::SValuePtr&);
154
155 //----------------------------------------------------------------------------------------------------------------
156 // The following functions are more or less from ARM documentation and named similarly. They are generally not
157 // further documented here or by ARM.
158 //----------------------------------------------------------------------------------------------------------------
159 using TwoValues = std::tuple<BaseSemantics::SValuePtr, BaseSemantics::SValuePtr>;
160
161 enum class SrType { LSL, LSR, ASR, ROR, RRX }; // SRType
162 enum class BranchType { // BranchType
163 DIRCALL, // direct branch with link
164 DIR, // undocumented but used by B instruction (maybe the same as DIRCALL?)
165 INDCALL, // indirect branch with link
166 ERET, // exception return (indirect)
167 DBGEXIT, // exit from debug state
168 RET, // indirect branch with function return hint
169 INDIR, // indicrect branch
170 EXCEPTION, // exception entry
171 RESET, // reset
172 UNKNOWN // other
173 };
174
175 BaseSemantics::SValuePtr part(const BaseSemantics::SValuePtr&, size_t maxBit, size_t minBit); // X<m,n>
176 BaseSemantics::SValuePtr part(const BaseSemantics::SValuePtr&, size_t bitNumber); // X<n>
178 BaseSemantics::SValuePtr zeroExtend(const BaseSemantics::SValuePtr&, size_t); // ZeroExtend
179 BaseSemantics::SValuePtr makeZeros(size_t); // Zeros
180 TwoValues a32ExpandImmC(const BaseSemantics::SValuePtr&); // A32ExpandImm_C
181 TwoValues shiftC(const BaseSemantics::SValuePtr&, SrType, int amount, const BaseSemantics::SValuePtr &carry); // Shift_C
182 TwoValues lslC(const BaseSemantics::SValuePtr&, size_t shift); // LSL_C
183 TwoValues lsrC(const BaseSemantics::SValuePtr&, size_t shift); // LSR_C
184 TwoValues asrC(const BaseSemantics::SValuePtr&, size_t shift); // ASR_C
185 TwoValues rorC(const BaseSemantics::SValuePtr&, int shift); // ROR_C
186 TwoValues rrxC(const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr &carry); // RRX_C
187 BaseSemantics::SValuePtr lsr(const BaseSemantics::SValuePtr&, size_t shift); // LSR
188 BaseSemantics::SValuePtr lsl(const BaseSemantics::SValuePtr&, size_t shift); // LSL
190 void aluExceptionReturn(const BaseSemantics::SValuePtr &enabled,
191 const BaseSemantics::SValuePtr &address); // ALUExceptionReturn
192 void aluWritePc(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &address); // ALUWritePC
193 void bxWritePc(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &address, BranchType); // BXWritePC
194 void branchWritePc(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &address,
195 BranchType branchType); // BranchWritePC
196 void branchTo(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &alignedAddress,
197 BranchType); // BranchTo
198 BaseSemantics::SValuePtr align(const BaseSemantics::SValuePtr&, unsigned); // Align
199 BaseSemantics::SValuePtr pc(); // PC. Returns address of current instruction plus eight (not sure why).
200 BaseSemantics::SValuePtr pcStoreValue(); // PCStoreValue (which doesn't store anything at all)
201 void loadWritePc(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &address); // LoadWritePC
202 TwoValues addWithCarry(const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
203 const BaseSemantics::SValuePtr&); // AddWithCarry
204 BaseSemantics::SValuePtr spsr(); // SPSR
205 void aarch32ExceptionReturn(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &address,
206 const BaseSemantics::SValuePtr &spsr); // AArch32.ExceptionReturn
207 void dbgdtrEl0(const BaseSemantics::SValuePtr &enabled, const BaseSemantics::SValuePtr &value); // DBGDTR_EL0
208 BaseSemantics::SValuePtr dbgdtrEl0(); // DBGDTR_EL0
209 BaseSemantics::SValuePtr bigEndian(); // BigEndian
210 BaseSemantics::SValuePtr signedSat(const BaseSemantics::SValuePtr&, size_t); // SignedSat
211 TwoValues signedSatQ(const BaseSemantics::SValuePtr&, size_t); // SignedSatQ
212 BaseSemantics::SValuePtr unsignedSat(const BaseSemantics::SValuePtr&, size_t); // UnsignedSat
213 TwoValues unsignedSatQ(const BaseSemantics::SValuePtr&, size_t); // UnsignedSatQ
215 BaseSemantics::SValuePtr countLeadingZeroBits(const BaseSemantics::SValuePtr&); // CountLeadingZeroBits
216 void aarch32CallHypervisor(const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&); // AArch32.CallHypervisor
217};
218
219} // namespace
220} // namespace
221} // namespace
222
223#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
224BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::InstructionSemantics::DispatcherAarch32);
225#endif
226
227#endif
228#endif
Dispatches instructions through the RISC layer.
Definition Dispatcher.h:46
Base class for expressions.
Base class for machine instructions.
bool signBit(T value)
Returns true if the sign bit is set, false if clear.
Definition integerOps.h:72
ROSE_DLL_API void load(SgProject *project, std::list< std::string > const &filepaths)
Load ASTs that have been saved to files.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
Unsigned signExtend(Unsigned src, size_t n)
Sign extend part of a value to the full width of the src type.
Definition BitOps.h:324
std::string join(const std::string &separator, const Container &container)
Join individual items to form a single string.
Definition SplitJoin.h:62
The ROSE library.
const char * SrType(int64_t)
Convert Rose::BinaryAnalysis::InstructionSemantics::DispatcherAarch32::SrType enum constant to a stri...
const char * BranchType(int64_t)
Convert Rose::BinaryAnalysis::InstructionSemantics::DispatcherAarch32::BranchType enum constant to a ...