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 "mozilla/Vector.h"
31 
32 #include "jsalloc.h"
33 
34 #include "jit/arm64/vixl/Globals-vixl.h"
35 #include "jit/arm64/vixl/Instructions-vixl.h"
36 
37 
38 // List macro containing all visitors needed by the decoder class.
39 
40 #define VISITOR_LIST_THAT_RETURN(V) \
41   V(PCRelAddressing)                \
42   V(AddSubImmediate)                \
43   V(LogicalImmediate)               \
44   V(MoveWideImmediate)              \
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)
constness_(constness)129       : constness_(constness) {}
130 
~DecoderVisitor()131   virtual ~DecoderVisitor() {}
132 
133   #define DECLARE(A) virtual void Visit##A(const Instruction* instr) = 0;
VISITOR_LIST(DECLARE)134   VISITOR_LIST(DECLARE)
135   #undef DECLARE
136 
137   bool IsConstVisitor() const { return constness_ == kConstVisitor; }
MutableInstruction(const Instruction * instr)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:
Decoder()150   Decoder() {}
151 
152   // Top-level wrappers around the actual decoding function.
Decode(const Instruction * instr)153   void Decode(const Instruction* instr) {
154     for (auto visitor : visitors_) {
155       VIXL_ASSERT(visitor->IsConstVisitor());
156     }
157     DecodeInstruction(instr);
158   }
Decode(Instruction * instr)159   void Decode(Instruction* instr) {
160     DecodeInstruction(const_cast<const Instruction*>(instr));
161   }
162 
163   // Register a new visitor class with the decoder.
164   // Decode() will call the corresponding visitor method from all registered
165   // visitor classes when decoding reaches the leaf node of the instruction
166   // decode tree.
167   // Visitors are called in order.
168   // A visitor can be registered multiple times.
169   //
170   //   d.AppendVisitor(V1);
171   //   d.AppendVisitor(V2);
172   //   d.PrependVisitor(V2);
173   //   d.AppendVisitor(V3);
174   //
175   //   d.Decode(i);
176   //
177   // will call in order visitor methods in V2, V1, V2, V3.
178   void AppendVisitor(DecoderVisitor* visitor);
179   void PrependVisitor(DecoderVisitor* visitor);
180   // These helpers register `new_visitor` before or after the first instance of
181   // `registered_visiter` in the list.
182   // So if
183   //   V1, V2, V1, V2
184   // are registered in this order in the decoder, calls to
185   //   d.InsertVisitorAfter(V3, V1);
186   //   d.InsertVisitorBefore(V4, V2);
187   // will yield the order
188   //   V1, V3, V4, V2, V1, V2
189   //
190   // For more complex modifications of the order of registered visitors, one can
191   // directly access and modify the list of visitors via the `visitors()'
192   // accessor.
193   void InsertVisitorBefore(DecoderVisitor* new_visitor,
194                            DecoderVisitor* registered_visitor);
195   void InsertVisitorAfter(DecoderVisitor* new_visitor,
196                           DecoderVisitor* registered_visitor);
197 
198   // Remove all instances of a previously registered visitor class from the list
199   // of visitors stored by the decoder.
200   void RemoveVisitor(DecoderVisitor* visitor);
201 
202   #define DECLARE(A) void Visit##A(const Instruction* instr);
203   VISITOR_LIST(DECLARE)
204   #undef DECLARE
205 
206 
207  private:
208   // Decodes an instruction and calls the visitor functions registered with the
209   // Decoder class.
210   void DecodeInstruction(const Instruction* instr);
211 
212   // Decode the PC relative addressing instruction, and call the corresponding
213   // visitors.
214   // On entry, instruction bits 27:24 = 0x0.
215   void DecodePCRelAddressing(const Instruction* instr);
216 
217   // Decode the add/subtract immediate instruction, and call the correspoding
218   // visitors.
219   // On entry, instruction bits 27:24 = 0x1.
220   void DecodeAddSubImmediate(const Instruction* instr);
221 
222   // Decode the branch, system command, and exception generation parts of
223   // the instruction tree, and call the corresponding visitors.
224   // On entry, instruction bits 27:24 = {0x4, 0x5, 0x6, 0x7}.
225   void DecodeBranchSystemException(const Instruction* instr);
226 
227   // Decode the load and store parts of the instruction tree, and call
228   // the corresponding visitors.
229   // On entry, instruction bits 27:24 = {0x8, 0x9, 0xC, 0xD}.
230   void DecodeLoadStore(const Instruction* instr);
231 
232   // Decode the logical immediate and move wide immediate parts of the
233   // instruction tree, and call the corresponding visitors.
234   // On entry, instruction bits 27:24 = 0x2.
235   void DecodeLogical(const Instruction* instr);
236 
237   // Decode the bitfield and extraction parts of the instruction tree,
238   // and call the corresponding visitors.
239   // On entry, instruction bits 27:24 = 0x3.
240   void DecodeBitfieldExtract(const Instruction* instr);
241 
242   // Decode the data processing parts of the instruction tree, and call the
243   // corresponding visitors.
244   // On entry, instruction bits 27:24 = {0x1, 0xA, 0xB}.
245   void DecodeDataProcessing(const Instruction* instr);
246 
247   // Decode the floating point parts of the instruction tree, and call the
248   // corresponding visitors.
249   // On entry, instruction bits 27:24 = {0xE, 0xF}.
250   void DecodeFP(const Instruction* instr);
251 
252   // Decode the Advanced SIMD (NEON) load/store part of the instruction tree,
253   // and call the corresponding visitors.
254   // On entry, instruction bits 29:25 = 0x6.
255   void DecodeNEONLoadStore(const Instruction* instr);
256 
257   // Decode the Advanced SIMD (NEON) vector data processing part of the
258   // instruction tree, and call the corresponding visitors.
259   // On entry, instruction bits 28:25 = 0x7.
260   void DecodeNEONVectorDataProcessing(const Instruction* instr);
261 
262   // Decode the Advanced SIMD (NEON) scalar data processing part of the
263   // instruction tree, and call the corresponding visitors.
264   // On entry, instruction bits 28:25 = 0xF.
265   void DecodeNEONScalarDataProcessing(const Instruction* instr);
266 
267  private:
268   // Visitors are registered in a list.
269   mozilla::Vector<DecoderVisitor*, 8, js::SystemAllocPolicy> visitors_;
270 };
271 
272 }  // namespace vixl
273 
274 #endif  // VIXL_A64_DECODER_A64_H_
275