Add qemu 2.4.0
[kvmfornfv.git] / qemu / disas / libvixl / a64 / decoder-a64.h
1 // Copyright 2013, 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 "globals.h"
33 #include "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(Unallocated)                    \
83   V(Unimplemented)
84
85 namespace vixl {
86
87 // The Visitor interface. Disassembler and simulator (and other tools)
88 // must provide implementations for all of these functions.
89 class DecoderVisitor {
90  public:
91   enum VisitorConstness {
92     kConstVisitor,
93     kNonConstVisitor
94   };
95   explicit DecoderVisitor(VisitorConstness constness = kConstVisitor)
96       : constness_(constness) {}
97
98   virtual ~DecoderVisitor() {}
99
100   #define DECLARE(A) virtual void Visit##A(const Instruction* instr) = 0;
101   VISITOR_LIST(DECLARE)
102   #undef DECLARE
103
104   bool IsConstVisitor() const { return constness_ == kConstVisitor; }
105   Instruction* MutableInstruction(const Instruction* instr) {
106     VIXL_ASSERT(!IsConstVisitor());
107     return const_cast<Instruction*>(instr);
108   }
109
110  private:
111   const VisitorConstness constness_;
112 };
113
114
115 class Decoder {
116  public:
117   Decoder() {}
118
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());
124     }
125     DecodeInstruction(instr);
126   }
127   void Decode(Instruction* instr) {
128     DecodeInstruction(const_cast<const Instruction*>(instr));
129   }
130
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
134   // decode tree.
135   // Visitors are called in order.
136   // A visitor can be registered multiple times.
137   //
138   //   d.AppendVisitor(V1);
139   //   d.AppendVisitor(V2);
140   //   d.PrependVisitor(V2);
141   //   d.AppendVisitor(V3);
142   //
143   //   d.Decode(i);
144   //
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.
150   // So if
151   //   V1, V2, V1, V2
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
157   //
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()'
160   // accessor.
161   void InsertVisitorBefore(DecoderVisitor* new_visitor,
162                            DecoderVisitor* registered_visitor);
163   void InsertVisitorAfter(DecoderVisitor* new_visitor,
164                           DecoderVisitor* registered_visitor);
165
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);
169
170   #define DECLARE(A) void Visit##A(const Instruction* instr);
171   VISITOR_LIST(DECLARE)
172   #undef DECLARE
173
174
175   std::list<DecoderVisitor*>* visitors() { return &visitors_; }
176
177  private:
178   // Decodes an instruction and calls the visitor functions registered with the
179   // Decoder class.
180   void DecodeInstruction(const Instruction* instr);
181
182   // Decode the PC relative addressing instruction, and call the corresponding
183   // visitors.
184   // On entry, instruction bits 27:24 = 0x0.
185   void DecodePCRelAddressing(const Instruction* instr);
186
187   // Decode the add/subtract immediate instruction, and call the correspoding
188   // visitors.
189   // On entry, instruction bits 27:24 = 0x1.
190   void DecodeAddSubImmediate(const Instruction* instr);
191
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);
196
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);
201
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);
206
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);
211
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);
216
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);
221
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);
226
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);
231
232  private:
233   // Visitors are registered in a list.
234   std::list<DecoderVisitor*> visitors_;
235 };
236
237 }  // namespace vixl
238
239 #endif  // VIXL_A64_DECODER_A64_H_