1 // Copyright 2014, ARM Limited 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 <list> 31 32 #include "vixl/globals.h" 33 #include "vixl/a64/instructions-a64.h" 34 35 36 // List macro containing all visitors needed by the decoder class. 37 38 #define VISITOR_LIST(V) \ 39 V(PCRelAddressing) \ 40 V(AddSubImmediate) \ 41 V(LogicalImmediate) \ 42 V(MoveWideImmediate) \ 43 V(Bitfield) \ 44 V(Extract) \ 45 V(UnconditionalBranch) \ 46 V(UnconditionalBranchToRegister) \ 47 V(CompareBranch) \ 48 V(TestBranch) \ 49 V(ConditionalBranch) \ 50 V(System) \ 51 V(Exception) \ 52 V(LoadStorePairPostIndex) \ 53 V(LoadStorePairOffset) \ 54 V(LoadStorePairPreIndex) \ 55 V(LoadStorePairNonTemporal) \ 56 V(LoadLiteral) \ 57 V(LoadStoreUnscaledOffset) \ 58 V(LoadStorePostIndex) \ 59 V(LoadStorePreIndex) \ 60 V(LoadStoreRegisterOffset) \ 61 V(LoadStoreUnsignedOffset) \ 62 V(LoadStoreExclusive) \ 63 V(LogicalShifted) \ 64 V(AddSubShifted) \ 65 V(AddSubExtended) \ 66 V(AddSubWithCarry) \ 67 V(ConditionalCompareRegister) \ 68 V(ConditionalCompareImmediate) \ 69 V(ConditionalSelect) \ 70 V(DataProcessing1Source) \ 71 V(DataProcessing2Source) \ 72 V(DataProcessing3Source) \ 73 V(FPCompare) \ 74 V(FPConditionalCompare) \ 75 V(FPConditionalSelect) \ 76 V(FPImmediate) \ 77 V(FPDataProcessing1Source) \ 78 V(FPDataProcessing2Source) \ 79 V(FPDataProcessing3Source) \ 80 V(FPIntegerConvert) \ 81 V(FPFixedPointConvert) \ 82 V(Crypto2RegSHA) \ 83 V(Crypto3RegSHA) \ 84 V(CryptoAES) \ 85 V(NEON2RegMisc) \ 86 V(NEON3Different) \ 87 V(NEON3Same) \ 88 V(NEONAcrossLanes) \ 89 V(NEONByIndexedElement) \ 90 V(NEONCopy) \ 91 V(NEONExtract) \ 92 V(NEONLoadStoreMultiStruct) \ 93 V(NEONLoadStoreMultiStructPostIndex) \ 94 V(NEONLoadStoreSingleStruct) \ 95 V(NEONLoadStoreSingleStructPostIndex) \ 96 V(NEONModifiedImmediate) \ 97 V(NEONScalar2RegMisc) \ 98 V(NEONScalar3Diff) \ 99 V(NEONScalar3Same) \ 100 V(NEONScalarByIndexedElement) \ 101 V(NEONScalarCopy) \ 102 V(NEONScalarPairwise) \ 103 V(NEONScalarShiftImmediate) \ 104 V(NEONShiftImmediate) \ 105 V(NEONTable) \ 106 V(NEONPerm) \ 107 V(Unallocated) \ 108 V(Unimplemented) 109 110 namespace vixl { 111 112 // The Visitor interface. Disassembler and simulator (and other tools) 113 // must provide implementations for all of these functions. 114 class DecoderVisitor { 115 public: 116 enum VisitorConstness { 117 kConstVisitor, 118 kNonConstVisitor 119 }; 120 explicit DecoderVisitor(VisitorConstness constness = kConstVisitor) constness_(constness)121 : constness_(constness) {} 122 ~DecoderVisitor()123 virtual ~DecoderVisitor() {} 124 125 #define DECLARE(A) virtual void Visit##A(const Instruction* instr) = 0; VISITOR_LIST(DECLARE)126 VISITOR_LIST(DECLARE) 127 #undef DECLARE 128 129 bool IsConstVisitor() const { return constness_ == kConstVisitor; } MutableInstruction(const Instruction * instr)130 Instruction* MutableInstruction(const Instruction* instr) { 131 VIXL_ASSERT(!IsConstVisitor()); 132 return const_cast<Instruction*>(instr); 133 } 134 135 private: 136 const VisitorConstness constness_; 137 }; 138 139 140 class Decoder { 141 public: Decoder()142 Decoder() {} 143 144 // Top-level wrappers around the actual decoding function. Decode(const Instruction * instr)145 void Decode(const Instruction* instr) { 146 std::list<DecoderVisitor*>::iterator it; 147 for (it = visitors_.begin(); it != visitors_.end(); it++) { 148 VIXL_ASSERT((*it)->IsConstVisitor()); 149 } 150 DecodeInstruction(instr); 151 } Decode(Instruction * instr)152 void Decode(Instruction* instr) { 153 DecodeInstruction(const_cast<const Instruction*>(instr)); 154 } 155 156 // Register a new visitor class with the decoder. 157 // Decode() will call the corresponding visitor method from all registered 158 // visitor classes when decoding reaches the leaf node of the instruction 159 // decode tree. 160 // Visitors are called in order. 161 // A visitor can be registered multiple times. 162 // 163 // d.AppendVisitor(V1); 164 // d.AppendVisitor(V2); 165 // d.PrependVisitor(V2); 166 // d.AppendVisitor(V3); 167 // 168 // d.Decode(i); 169 // 170 // will call in order visitor methods in V2, V1, V2, V3. 171 void AppendVisitor(DecoderVisitor* visitor); 172 void PrependVisitor(DecoderVisitor* visitor); 173 // These helpers register `new_visitor` before or after the first instance of 174 // `registered_visiter` in the list. 175 // So if 176 // V1, V2, V1, V2 177 // are registered in this order in the decoder, calls to 178 // d.InsertVisitorAfter(V3, V1); 179 // d.InsertVisitorBefore(V4, V2); 180 // will yield the order 181 // V1, V3, V4, V2, V1, V2 182 // 183 // For more complex modifications of the order of registered visitors, one can 184 // directly access and modify the list of visitors via the `visitors()' 185 // accessor. 186 void InsertVisitorBefore(DecoderVisitor* new_visitor, 187 DecoderVisitor* registered_visitor); 188 void InsertVisitorAfter(DecoderVisitor* new_visitor, 189 DecoderVisitor* registered_visitor); 190 191 // Remove all instances of a previously registered visitor class from the list 192 // of visitors stored by the decoder. 193 void RemoveVisitor(DecoderVisitor* visitor); 194 195 #define DECLARE(A) void Visit##A(const Instruction* instr); VISITOR_LIST(DECLARE)196 VISITOR_LIST(DECLARE) 197 #undef DECLARE 198 199 200 std::list<DecoderVisitor*>* visitors() { return &visitors_; } 201 202 private: 203 // Decodes an instruction and calls the visitor functions registered with the 204 // Decoder class. 205 void DecodeInstruction(const Instruction* instr); 206 207 // Decode the PC relative addressing instruction, and call the corresponding 208 // visitors. 209 // On entry, instruction bits 27:24 = 0x0. 210 void DecodePCRelAddressing(const Instruction* instr); 211 212 // Decode the add/subtract immediate instruction, and call the correspoding 213 // visitors. 214 // On entry, instruction bits 27:24 = 0x1. 215 void DecodeAddSubImmediate(const Instruction* instr); 216 217 // Decode the branch, system command, and exception generation parts of 218 // the instruction tree, and call the corresponding visitors. 219 // On entry, instruction bits 27:24 = {0x4, 0x5, 0x6, 0x7}. 220 void DecodeBranchSystemException(const Instruction* instr); 221 222 // Decode the load and store parts of the instruction tree, and call 223 // the corresponding visitors. 224 // On entry, instruction bits 27:24 = {0x8, 0x9, 0xC, 0xD}. 225 void DecodeLoadStore(const Instruction* instr); 226 227 // Decode the logical immediate and move wide immediate parts of the 228 // instruction tree, and call the corresponding visitors. 229 // On entry, instruction bits 27:24 = 0x2. 230 void DecodeLogical(const Instruction* instr); 231 232 // Decode the bitfield and extraction parts of the instruction tree, 233 // and call the corresponding visitors. 234 // On entry, instruction bits 27:24 = 0x3. 235 void DecodeBitfieldExtract(const Instruction* instr); 236 237 // Decode the data processing parts of the instruction tree, and call the 238 // corresponding visitors. 239 // On entry, instruction bits 27:24 = {0x1, 0xA, 0xB}. 240 void DecodeDataProcessing(const Instruction* instr); 241 242 // Decode the floating point parts of the instruction tree, and call the 243 // corresponding visitors. 244 // On entry, instruction bits 27:24 = {0xE, 0xF}. 245 void DecodeFP(const Instruction* instr); 246 247 // Decode the Advanced SIMD (NEON) load/store part of the instruction tree, 248 // and call the corresponding visitors. 249 // On entry, instruction bits 29:25 = 0x6. 250 void DecodeNEONLoadStore(const Instruction* instr); 251 252 // Decode the Advanced SIMD (NEON) vector data processing part of the 253 // instruction tree, and call the corresponding visitors. 254 // On entry, instruction bits 28:25 = 0x7. 255 void DecodeNEONVectorDataProcessing(const Instruction* instr); 256 257 // Decode the Advanced SIMD (NEON) scalar data processing part of the 258 // instruction tree, and call the corresponding visitors. 259 // On entry, instruction bits 28:25 = 0xF. 260 void DecodeNEONScalarDataProcessing(const Instruction* instr); 261 262 private: 263 // Visitors are registered in a list. 264 std::list<DecoderVisitor*> visitors_; 265 }; 266 267 } // namespace vixl 268 269 #endif // VIXL_A64_DECODER_A64_H_ 270