| 1 |
// Copyright 2014, VIXL authors |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are met: |
| 6 |
// |
| 7 |
// * Redistributions of source code must retain the above copyright notice, |
| 8 |
// this list of conditions and the following disclaimer. |
| 9 |
// * Redistributions in binary form must reproduce the above copyright notice, |
| 10 |
// this list of conditions and the following disclaimer in the documentation |
| 11 |
// and/or other materials provided with the distribution. |
| 12 |
// * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 |
// used to endorse or promote products derived from this software without |
| 14 |
// specific prior written permission. |
| 15 |
// |
| 16 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 |
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 |
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 |
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 |
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 |
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 |
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
|
| 27 |
#ifndef VIXL_A64_DECODER_A64_H_ |
| 28 |
#define VIXL_A64_DECODER_A64_H_ |
| 29 |
|
| 30 |
#include "mozilla/Vector.h" |
| 31 |
|
| 32 |
#include "jit/arm64/vixl/Globals-vixl.h" |
| 33 |
#include "jit/arm64/vixl/Instructions-vixl.h" |
| 34 |
#include "js/AllocPolicy.h" |
| 35 |
|
| 36 |
|
| 37 |
// List macro containing all visitors needed by the decoder class. |
| 38 |
|
| 39 |
#define VISITOR_LIST_THAT_RETURN(V) \ |
| 40 |
V(PCRelAddressing) \ |
| 41 |
V(AddSubImmediate) \ |
| 42 |
V(LogicalImmediate) \ |
| 43 |
V(MoveWideImmediate) \ |
| 44 |
V(AtomicMemory) \ |
| 45 |
V(Bitfield) \ |
| 46 |
V(Extract) \ |
| 47 |
V(UnconditionalBranch) \ |
| 48 |
V(UnconditionalBranchToRegister) \ |
| 49 |
V(CompareBranch) \ |
| 50 |
V(TestBranch) \ |
| 51 |
V(ConditionalBranch) \ |
| 52 |
V(System) \ |
| 53 |
V(Exception) \ |
| 54 |
V(LoadStorePairPostIndex) \ |
| 55 |
V(LoadStorePairOffset) \ |
| 56 |
V(LoadStorePairPreIndex) \ |
| 57 |
V(LoadStorePairNonTemporal) \ |
| 58 |
V(LoadLiteral) \ |
| 59 |
V(LoadStoreUnscaledOffset) \ |
| 60 |
V(LoadStorePostIndex) \ |
| 61 |
V(LoadStorePreIndex) \ |
| 62 |
V(LoadStoreRegisterOffset) \ |
| 63 |
V(LoadStoreUnsignedOffset) \ |
| 64 |
V(LoadStoreExclusive) \ |
| 65 |
V(LogicalShifted) \ |
| 66 |
V(AddSubShifted) \ |
| 67 |
V(AddSubExtended) \ |
| 68 |
V(AddSubWithCarry) \ |
| 69 |
V(ConditionalCompareRegister) \ |
| 70 |
V(ConditionalCompareImmediate) \ |
| 71 |
V(ConditionalSelect) \ |
| 72 |
V(DataProcessing1Source) \ |
| 73 |
V(DataProcessing2Source) \ |
| 74 |
V(DataProcessing3Source) \ |
| 75 |
V(FPCompare) \ |
| 76 |
V(FPConditionalCompare) \ |
| 77 |
V(FPConditionalSelect) \ |
| 78 |
V(FPImmediate) \ |
| 79 |
V(FPDataProcessing1Source) \ |
| 80 |
V(FPDataProcessing2Source) \ |
| 81 |
V(FPDataProcessing3Source) \ |
| 82 |
V(FPIntegerConvert) \ |
| 83 |
V(FPFixedPointConvert) \ |
| 84 |
V(Crypto2RegSHA) \ |
| 85 |
V(Crypto3RegSHA) \ |
| 86 |
V(CryptoAES) \ |
| 87 |
V(NEON2RegMisc) \ |
| 88 |
V(NEON3Different) \ |
| 89 |
V(NEON3Same) \ |
| 90 |
V(NEONAcrossLanes) \ |
| 91 |
V(NEONByIndexedElement) \ |
| 92 |
V(NEONCopy) \ |
| 93 |
V(NEONExtract) \ |
| 94 |
V(NEONLoadStoreMultiStruct) \ |
| 95 |
V(NEONLoadStoreMultiStructPostIndex) \ |
| 96 |
V(NEONLoadStoreSingleStruct) \ |
| 97 |
V(NEONLoadStoreSingleStructPostIndex) \ |
| 98 |
V(NEONModifiedImmediate) \ |
| 99 |
V(NEONScalar2RegMisc) \ |
| 100 |
V(NEONScalar3Diff) \ |
| 101 |
V(NEONScalar3Same) \ |
| 102 |
V(NEONScalarByIndexedElement) \ |
| 103 |
V(NEONScalarCopy) \ |
| 104 |
V(NEONScalarPairwise) \ |
| 105 |
V(NEONScalarShiftImmediate) \ |
| 106 |
V(NEONShiftImmediate) \ |
| 107 |
V(NEONTable) \ |
| 108 |
V(NEONPerm) |
| 109 |
|
| 110 |
#define VISITOR_LIST_THAT_DONT_RETURN(V) \ |
| 111 |
V(Unallocated) \ |
| 112 |
V(Unimplemented) \ |
| 113 |
|
| 114 |
#define VISITOR_LIST(V) \ |
| 115 |
VISITOR_LIST_THAT_RETURN(V) \ |
| 116 |
VISITOR_LIST_THAT_DONT_RETURN(V) \ |
| 117 |
|
| 118 |
namespace vixl { |
| 119 |
|
| 120 |
// The Visitor interface. Disassembler and simulator (and other tools) |
| 121 |
// must provide implementations for all of these functions. |
| 122 |
class DecoderVisitor { |
| 123 |
public: |
| 124 |
enum VisitorConstness { |
| 125 |
kConstVisitor, |
| 126 |
kNonConstVisitor |
| 127 |
}; |
| 128 |
explicit DecoderVisitor(VisitorConstness constness = kConstVisitor) |
| 129 |
: constness_(constness) {} |
| 130 |
|
| 131 |
virtual ~DecoderVisitor() {} |
| 132 |
|
| 133 |
#define DECLARE(A) virtual void Visit##A(const Instruction* instr) = 0; |
| 134 |
VISITOR_LIST(DECLARE) |
| 135 |
#undef DECLARE |
| 136 |
|
| 137 |
bool IsConstVisitor() const { return constness_ == kConstVisitor; } |
| 138 |
Instruction* MutableInstruction(const Instruction* instr) { |
| 139 |
VIXL_ASSERT(!IsConstVisitor()); |
| 140 |
return const_cast<Instruction*>(instr); |
| 141 |
} |
| 142 |
|
| 143 |
private: |
| 144 |
const VisitorConstness constness_; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
class Decoder { |
| 149 |
public: |
| 150 |
Decoder() {} |
| 151 |
|
| 152 |
// Top-level wrappers around the actual decoding function. |
| 153 |
void Decode(const Instruction* instr) { |
| 154 |
#ifdef DEBUG |
| 155 |
for (auto visitor : visitors_) { |
| 156 |
VIXL_ASSERT(visitor->IsConstVisitor()); |
| 157 |
} |
| 158 |
#endif |
| 159 |
DecodeInstruction(instr); |
| 160 |
} |
| 161 |
void Decode(Instruction* instr) { |
| 162 |
DecodeInstruction(const_cast<const Instruction*>(instr)); |
| 163 |
} |
| 164 |
|
| 165 |
// Register a new visitor class with the decoder. |
| 166 |
// Decode() will call the corresponding visitor method from all registered |
| 167 |
// visitor classes when decoding reaches the leaf node of the instruction |
| 168 |
// decode tree. |
| 169 |
// Visitors are called in order. |
| 170 |
// A visitor can be registered multiple times. |
| 171 |
// |
| 172 |
// d.AppendVisitor(V1); |
| 173 |
// d.AppendVisitor(V2); |
| 174 |
// d.PrependVisitor(V2); |
| 175 |
// d.AppendVisitor(V3); |
| 176 |
// |
| 177 |
// d.Decode(i); |
| 178 |
// |
| 179 |
// will call in order visitor methods in V2, V1, V2, V3. |
| 180 |
void AppendVisitor(DecoderVisitor* visitor); |
| 181 |
void PrependVisitor(DecoderVisitor* visitor); |
| 182 |
// These helpers register `new_visitor` before or after the first instance of |
| 183 |
// `registered_visiter` in the list. |
| 184 |
// So if |
| 185 |
// V1, V2, V1, V2 |
| 186 |
// are registered in this order in the decoder, calls to |
| 187 |
// d.InsertVisitorAfter(V3, V1); |
| 188 |
// d.InsertVisitorBefore(V4, V2); |
| 189 |
// will yield the order |
| 190 |
// V1, V3, V4, V2, V1, V2 |
| 191 |
// |
| 192 |
// For more complex modifications of the order of registered visitors, one can |
| 193 |
// directly access and modify the list of visitors via the `visitors()' |
| 194 |
// accessor. |
| 195 |
void InsertVisitorBefore(DecoderVisitor* new_visitor, |
| 196 |
DecoderVisitor* registered_visitor); |
| 197 |
void InsertVisitorAfter(DecoderVisitor* new_visitor, |
| 198 |
DecoderVisitor* registered_visitor); |
| 199 |
|
| 200 |
// Remove all instances of a previously registered visitor class from the list |
| 201 |
// of visitors stored by the decoder. |
| 202 |
void RemoveVisitor(DecoderVisitor* visitor); |
| 203 |
|
| 204 |
#define DECLARE(A) void Visit##A(const Instruction* instr); |
| 205 |
VISITOR_LIST(DECLARE) |
| 206 |
#undef DECLARE |
| 207 |
|
| 208 |
|
| 209 |
private: |
| 210 |
// Decodes an instruction and calls the visitor functions registered with the |
| 211 |
// Decoder class. |
| 212 |
void DecodeInstruction(const Instruction* instr); |
| 213 |
|
| 214 |
// Decode the PC relative addressing instruction, and call the corresponding |
| 215 |
// visitors. |
| 216 |
// On entry, instruction bits 27:24 = 0x0. |
| 217 |
void DecodePCRelAddressing(const Instruction* instr); |
| 218 |
|
| 219 |
// Decode the add/subtract immediate instruction, and call the correspoding |
| 220 |
// visitors. |
| 221 |
// On entry, instruction bits 27:24 = 0x1. |
| 222 |
void DecodeAddSubImmediate(const Instruction* instr); |
| 223 |
|
| 224 |
// Decode the branch, system command, and exception generation parts of |
| 225 |
// the instruction tree, and call the corresponding visitors. |
| 226 |
// On entry, instruction bits 27:24 = {0x4, 0x5, 0x6, 0x7}. |
| 227 |
void DecodeBranchSystemException(const Instruction* instr); |
| 228 |
|
| 229 |
// Decode the load and store parts of the instruction tree, and call |
| 230 |
// the corresponding visitors. |
| 231 |
// On entry, instruction bits 27:24 = {0x8, 0x9, 0xC, 0xD}. |
| 232 |
void DecodeLoadStore(const Instruction* instr); |
| 233 |
|
| 234 |
// Decode the logical immediate and move wide immediate parts of the |
| 235 |
// instruction tree, and call the corresponding visitors. |
| 236 |
// On entry, instruction bits 27:24 = 0x2. |
| 237 |
void DecodeLogical(const Instruction* instr); |
| 238 |
|
| 239 |
// Decode the bitfield and extraction parts of the instruction tree, |
| 240 |
// and call the corresponding visitors. |
| 241 |
// On entry, instruction bits 27:24 = 0x3. |
| 242 |
void DecodeBitfieldExtract(const Instruction* instr); |
| 243 |
|
| 244 |
// Decode the data processing parts of the instruction tree, and call the |
| 245 |
// corresponding visitors. |
| 246 |
// On entry, instruction bits 27:24 = {0x1, 0xA, 0xB}. |
| 247 |
void DecodeDataProcessing(const Instruction* instr); |
| 248 |
|
| 249 |
// Decode the floating point parts of the instruction tree, and call the |
| 250 |
// corresponding visitors. |
| 251 |
// On entry, instruction bits 27:24 = {0xE, 0xF}. |
| 252 |
void DecodeFP(const Instruction* instr); |
| 253 |
|
| 254 |
// Decode the Advanced SIMD (NEON) load/store part of the instruction tree, |
| 255 |
// and call the corresponding visitors. |
| 256 |
// On entry, instruction bits 29:25 = 0x6. |
| 257 |
void DecodeNEONLoadStore(const Instruction* instr); |
| 258 |
|
| 259 |
// Decode the Advanced SIMD (NEON) vector data processing part of the |
| 260 |
// instruction tree, and call the corresponding visitors. |
| 261 |
// On entry, instruction bits 28:25 = 0x7. |
| 262 |
void DecodeNEONVectorDataProcessing(const Instruction* instr); |
| 263 |
|
| 264 |
// Decode the Advanced SIMD (NEON) scalar data processing part of the |
| 265 |
// instruction tree, and call the corresponding visitors. |
| 266 |
// On entry, instruction bits 28:25 = 0xF. |
| 267 |
void DecodeNEONScalarDataProcessing(const Instruction* instr); |
| 268 |
|
| 269 |
private: |
| 270 |
// Visitors are registered in a list. |
| 271 |
mozilla::Vector<DecoderVisitor*, 8, js::SystemAllocPolicy> visitors_; |
| 272 |
}; |
| 273 |
|
| 274 |
} // namespace vixl |
| 275 |
|
| 276 |
#endif // VIXL_A64_DECODER_A64_H_ |