1 // Copyright 2013, ARM Limited
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
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.
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.
27 #ifndef VIXL_A64_DECODER_A64_H_
28 #define VIXL_A64_DECODER_A64_H_
33 #include "a64/instructions-a64.h"
36 // List macro containing all visitors needed by the decoder class.
38 #define VISITOR_LIST(V) \
42 V(MoveWideImmediate) \
45 V(UnconditionalBranch) \
46 V(UnconditionalBranchToRegister) \
49 V(ConditionalBranch) \
52 V(LoadStorePairPostIndex) \
53 V(LoadStorePairOffset) \
54 V(LoadStorePairPreIndex) \
55 V(LoadStorePairNonTemporal) \
57 V(LoadStoreUnscaledOffset) \
58 V(LoadStorePostIndex) \
59 V(LoadStorePreIndex) \
60 V(LoadStoreRegisterOffset) \
61 V(LoadStoreUnsignedOffset) \
62 V(LoadStoreExclusive) \
67 V(ConditionalCompareRegister) \
68 V(ConditionalCompareImmediate) \
69 V(ConditionalSelect) \
70 V(DataProcessing1Source) \
71 V(DataProcessing2Source) \
72 V(DataProcessing3Source) \
74 V(FPConditionalCompare) \
75 V(FPConditionalSelect) \
77 V(FPDataProcessing1Source) \
78 V(FPDataProcessing2Source) \
79 V(FPDataProcessing3Source) \
81 V(FPFixedPointConvert) \
87 // The Visitor interface. Disassembler and simulator (and other tools)
88 // must provide implementations for all of these functions.
89 class DecoderVisitor {
91 enum VisitorConstness {
95 explicit DecoderVisitor(VisitorConstness constness = kConstVisitor)
96 : constness_(constness) {}
98 virtual ~DecoderVisitor() {}
100 #define DECLARE(A) virtual void Visit##A(const Instruction* instr) = 0;
101 VISITOR_LIST(DECLARE)
104 bool IsConstVisitor() const { return constness_ == kConstVisitor; }
105 Instruction* MutableInstruction(const Instruction* instr) {
106 VIXL_ASSERT(!IsConstVisitor());
107 return const_cast<Instruction*>(instr);
111 const VisitorConstness constness_;
119 // Top-level wrappers around the actual decoding function.
120 void Decode(const Instruction* instr) {
121 std::list<DecoderVisitor*>::iterator it;
122 for (it = visitors_.begin(); it != visitors_.end(); it++) {
123 VIXL_ASSERT((*it)->IsConstVisitor());
125 DecodeInstruction(instr);
127 void Decode(Instruction* instr) {
128 DecodeInstruction(const_cast<const Instruction*>(instr));
131 // Register a new visitor class with the decoder.
132 // Decode() will call the corresponding visitor method from all registered
133 // visitor classes when decoding reaches the leaf node of the instruction
135 // Visitors are called in order.
136 // A visitor can be registered multiple times.
138 // d.AppendVisitor(V1);
139 // d.AppendVisitor(V2);
140 // d.PrependVisitor(V2);
141 // d.AppendVisitor(V3);
145 // will call in order visitor methods in V2, V1, V2, V3.
146 void AppendVisitor(DecoderVisitor* visitor);
147 void PrependVisitor(DecoderVisitor* visitor);
148 // These helpers register `new_visitor` before or after the first instance of
149 // `registered_visiter` in the list.
152 // are registered in this order in the decoder, calls to
153 // d.InsertVisitorAfter(V3, V1);
154 // d.InsertVisitorBefore(V4, V2);
155 // will yield the order
156 // V1, V3, V4, V2, V1, V2
158 // For more complex modifications of the order of registered visitors, one can
159 // directly access and modify the list of visitors via the `visitors()'
161 void InsertVisitorBefore(DecoderVisitor* new_visitor,
162 DecoderVisitor* registered_visitor);
163 void InsertVisitorAfter(DecoderVisitor* new_visitor,
164 DecoderVisitor* registered_visitor);
166 // Remove all instances of a previously registered visitor class from the list
167 // of visitors stored by the decoder.
168 void RemoveVisitor(DecoderVisitor* visitor);
170 #define DECLARE(A) void Visit##A(const Instruction* instr);
171 VISITOR_LIST(DECLARE)
175 std::list<DecoderVisitor*>* visitors() { return &visitors_; }
178 // Decodes an instruction and calls the visitor functions registered with the
180 void DecodeInstruction(const Instruction* instr);
182 // Decode the PC relative addressing instruction, and call the corresponding
184 // On entry, instruction bits 27:24 = 0x0.
185 void DecodePCRelAddressing(const Instruction* instr);
187 // Decode the add/subtract immediate instruction, and call the correspoding
189 // On entry, instruction bits 27:24 = 0x1.
190 void DecodeAddSubImmediate(const Instruction* instr);
192 // Decode the branch, system command, and exception generation parts of
193 // the instruction tree, and call the corresponding visitors.
194 // On entry, instruction bits 27:24 = {0x4, 0x5, 0x6, 0x7}.
195 void DecodeBranchSystemException(const Instruction* instr);
197 // Decode the load and store parts of the instruction tree, and call
198 // the corresponding visitors.
199 // On entry, instruction bits 27:24 = {0x8, 0x9, 0xC, 0xD}.
200 void DecodeLoadStore(const Instruction* instr);
202 // Decode the logical immediate and move wide immediate parts of the
203 // instruction tree, and call the corresponding visitors.
204 // On entry, instruction bits 27:24 = 0x2.
205 void DecodeLogical(const Instruction* instr);
207 // Decode the bitfield and extraction parts of the instruction tree,
208 // and call the corresponding visitors.
209 // On entry, instruction bits 27:24 = 0x3.
210 void DecodeBitfieldExtract(const Instruction* instr);
212 // Decode the data processing parts of the instruction tree, and call the
213 // corresponding visitors.
214 // On entry, instruction bits 27:24 = {0x1, 0xA, 0xB}.
215 void DecodeDataProcessing(const Instruction* instr);
217 // Decode the floating point parts of the instruction tree, and call the
218 // corresponding visitors.
219 // On entry, instruction bits 27:24 = {0xE, 0xF}.
220 void DecodeFP(const Instruction* instr);
222 // Decode the Advanced SIMD (NEON) load/store part of the instruction tree,
223 // and call the corresponding visitors.
224 // On entry, instruction bits 29:25 = 0x6.
225 void DecodeAdvSIMDLoadStore(const Instruction* instr);
227 // Decode the Advanced SIMD (NEON) data processing part of the instruction
228 // tree, and call the corresponding visitors.
229 // On entry, instruction bits 27:25 = 0x7.
230 void DecodeAdvSIMDDataProcessing(const Instruction* instr);
233 // Visitors are registered in a list.
234 std::list<DecoderVisitor*> visitors_;
239 #endif // VIXL_A64_DECODER_A64_H_