sim: drop --enable-sim-cflags option
[external/binutils.git] / sim / aarch64 / decode.h
1 /* decode.h -- Prototypes for AArch64 simulator decoder functions.
2
3    Copyright (C) 2015-2016 Free Software Foundation, Inc.
4
5    Contributed by Red Hat.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #ifndef _DECODE_H
23 #define _DECODE_H
24
25 #include <sys/types.h>
26 #include "cpustate.h"
27
28 /* Codes used in conditional instructions
29
30    These are passed to conditional operations to identify which
31    condition to test for.  */
32
33 typedef enum CondCode
34 {
35   EQ = 0x0, /* meaning Z == 1 */
36   NE = 0x1, /* meaning Z == 0 */
37   HS = 0x2, /* meaning C == 1 */
38   CS = HS,
39   LO = 0x3, /* meaning C == 0 */
40   CC = LO,
41   MI = 0x4, /* meaning N == 1 */
42   PL = 0x5, /* meaning N == 0 */
43   VS = 0x6, /* meaning V == 1 */
44   VC = 0x7, /* meaning V == 0 */
45   HI = 0x8, /* meaning C == 1 && Z == 0 */
46   LS = 0x9, /* meaning !(C == 1 && Z == 0) */
47   GE = 0xa, /* meaning N == V */
48   LT = 0xb, /* meaning N != V */
49   GT = 0xc, /* meaning Z == 0 && N == V */
50   LE = 0xd, /* meaning !(Z == 0 && N == V) */
51   AL = 0xe, /* meaning ANY */
52   NV = 0xf  /* ditto */
53 } CondCode;
54
55 /* Certain addressing modes for load require pre or post writeback of
56    the computed address to a base register.  */
57
58 typedef enum WriteBack
59 {
60   Post = 0,
61   Pre = 1,
62   NoWriteBack = -1
63 } WriteBack;
64
65 /* Certain addressing modes for load require an offset to
66    be optionally scaled so the decode needs to pass that
67    through to the execute routine.  */
68
69 typedef enum Scaling
70 {
71   Unscaled = 0,
72   Scaled = 1,
73   NoScaling = -1
74 } Scaling;
75
76 /* When we do have to scale we do so by shifting using
77    log(bytes in data element - 1) as the shift count.
78    so we don't have to scale offsets when loading
79    bytes.  */
80
81 typedef enum ScaleShift
82 {
83   ScaleShift16 = 1,
84   ScaleShift32 = 2,
85   ScaleShift64 = 3,
86   ScaleShift128 = 4
87 } ScaleShift;
88
89 /* One of the addressing modes for load requires a 32-bit register
90    value to be either zero- or sign-extended for these instructions
91    UXTW or SXTW should be passed.
92
93    Arithmetic register data processing operations can optionally
94    extend a portion of the second register value for these
95    instructions the value supplied must identify the portion of the
96    register which is to be zero- or sign-exended.  */
97
98 typedef enum Extension
99 {
100   UXTB = 0,
101   UXTH = 1,
102   UXTW = 2,
103   UXTX = 3,
104   SXTB = 4,
105   SXTH = 5,
106   SXTW = 6,
107   SXTX = 7,
108   NoExtension = -1
109 } Extension;
110
111 /* Arithmetic and logical register data processing operations
112    optionally perform a shift on the second register value.  */
113
114 typedef enum Shift
115 {
116   LSL = 0,
117   LSR = 1,
118   ASR = 2,
119   ROR = 3
120 } Shift;
121
122 /* Bit twiddling helpers for instruction decode.  */
123
124 /* 32 bit mask with bits [hi,...,lo] set.  */
125 static inline uint32_t
126 mask32 (int hi, int lo)
127 {
128   int nbits = (hi + 1) - lo;
129   return ((1 << nbits) - 1) << lo;
130 }
131
132 /* 64 bit mask with bits [hi,...,lo] set.  */
133 static inline uint64_t
134 mask64 (int hi, int lo)
135 {
136   int nbits = (hi + 1) - lo;
137   return ((1L << nbits) - 1) << lo;
138 }
139
140 /* Pick bits [hi,...,lo] from val.  */
141 static inline uint32_t
142 pick32 (uint32_t val, int hi, int lo)
143 {
144   return val & mask32 (hi, lo);
145 }
146
147 /* Pick bits [hi,...,lo] from val.  */
148 static inline uint64_t
149 pick64 (uint64_t val, int hi, int lo)
150 {
151   return val & mask64 (hi, lo);
152 }
153
154 /* Pick bits [hi,...,lo] from val and shift to [(hi-(newlo - lo)),newlo].  */
155 static inline uint32_t
156 pickshift32 (uint32_t val, int hi, int lo, int newlo)
157 {
158   uint32_t bits = pick32 (val, hi, lo);
159
160   if (lo < newlo)
161     return bits << (newlo - lo);
162
163   return bits >> (lo - newlo);
164 }
165
166 /* Mask [hi,lo] and shift down to start at bit 0.  */
167 static inline uint32_t
168 pickbits32 (uint32_t val, int hi, int lo)
169 {
170   return pick32 (val, hi, lo) >> lo;
171 }
172
173 /* Mask [hi,lo] and shift down to start at bit 0.  */
174 static inline uint64_t
175 pickbits64 (uint64_t val, int hi, int lo)
176 {
177   return pick64 (val, hi, lo) >> lo;
178 }
179
180 /* Decode registers, immediates and constants of various types.  */
181
182 static inline GReg
183 greg (uint32_t val, int lo)
184 {
185   return (GReg) pickbits32 (val, lo + 4, lo);
186 }
187
188 static inline VReg
189 vreg (uint32_t val, int lo)
190 {
191   return (VReg) pickbits32 (val, lo + 4, lo);
192 }
193
194 static inline uint32_t
195 uimm (uint32_t val, int hi, int lo)
196 {
197   return pickbits32 (val, hi, lo);
198 }
199
200 static inline int32_t
201 simm32 (uint32_t val, int hi, int lo)
202 {
203   union
204   {
205     uint32_t u;
206     int32_t n;
207   } x;
208
209   x.u = val << (31 - hi);
210   return x.n >> (31 - hi + lo);
211 }
212
213 static inline int64_t
214 simm64 (uint64_t val, int hi, int lo)
215 {
216   union
217   {
218     uint64_t u;
219     int64_t n;
220   } x;
221
222   x.u = val << (63 - hi);
223   return x.n >> (63 - hi + lo);
224 }
225
226 static inline Shift
227 shift (uint32_t val, int lo)
228 {
229   return (Shift) pickbits32 (val, lo + 1, lo);
230 }
231
232 static inline Extension
233 extension (uint32_t val, int lo)
234 {
235   return (Extension) pickbits32 (val, lo + 2, lo);
236 }
237
238 static inline Scaling
239 scaling (uint32_t val, int lo)
240 {
241   return (Scaling) pickbits32 (val, lo, lo);
242 }
243
244 static inline WriteBack
245 writeback (uint32_t val, int lo)
246 {
247   return (WriteBack) pickbits32 (val, lo, lo);
248 }
249
250 static inline CondCode
251 condcode (uint32_t val, int lo)
252 {
253   return (CondCode) pickbits32 (val, lo + 3, lo);
254 }
255
256 /* Operation decode.
257    Bits [28,24] are the primary dispatch vector.  */
258
259 static inline uint32_t
260 dispatchGroup (uint32_t val)
261 {
262   return pickshift32 (val, 28, 25, 0);
263 }
264
265 /* The 16 possible values for bits [28,25] identified by tags which
266    map them to the 5 main instruction groups LDST, DPREG, ADVSIMD,
267    BREXSYS and DPIMM.
268
269    An extra group PSEUDO is included in one of the unallocated ranges
270    for simulator-specific pseudo-instructions.  */
271
272 enum DispatchGroup
273 {
274   GROUP_PSEUDO_0000,
275   GROUP_UNALLOC_0001,
276   GROUP_UNALLOC_0010,
277   GROUP_UNALLOC_0011,
278   GROUP_LDST_0100,
279   GROUP_DPREG_0101,
280   GROUP_LDST_0110,
281   GROUP_ADVSIMD_0111,
282   GROUP_DPIMM_1000,
283   GROUP_DPIMM_1001,
284   GROUP_BREXSYS_1010,
285   GROUP_BREXSYS_1011,
286   GROUP_LDST_1100,
287   GROUP_DPREG_1101,
288   GROUP_LDST_1110,
289   GROUP_ADVSIMD_1111
290 };
291
292 /* Bits [31, 29] of a Pseudo are the secondary dispatch vector.  */
293
294 static inline uint32_t
295 dispatchPseudo (uint32_t val)
296 {
297   return pickshift32 (val, 31, 29, 0);
298 }
299
300 /* The 8 possible values for bits [31,29] in a Pseudo Instruction.
301    Bits [28,25] are always 0000.  */
302
303 enum DispatchPseudo
304 {
305   PSEUDO_UNALLOC_000, /* Unallocated.  */
306   PSEUDO_UNALLOC_001, /* Ditto.  */
307   PSEUDO_UNALLOC_010, /* Ditto.  */
308   PSEUDO_UNALLOC_011, /* Ditto.  */
309   PSEUDO_UNALLOC_100, /* Ditto.  */
310   PSEUDO_UNALLOC_101, /* Ditto.  */
311   PSEUDO_CALLOUT_110, /* CALLOUT -- bits [24,0] identify call/ret sig.  */
312   PSEUDO_HALT_111     /* HALT -- bits [24, 0] identify halt code.  */
313 };
314
315 /* Bits [25, 23] of a DPImm are the secondary dispatch vector.  */
316
317 static inline uint32_t
318 dispatchDPImm (uint32_t instr)
319 {
320   return pickshift32 (instr, 25, 23, 0);
321 }
322
323 /* The 8 possible values for bits [25,23] in a Data Processing Immediate
324    Instruction. Bits [28,25] are always 100_.  */
325
326 enum DispatchDPImm
327 {
328   DPIMM_PCADR_000,  /* PC-rel-addressing.  */
329   DPIMM_PCADR_001,  /* Ditto.  */
330   DPIMM_ADDSUB_010, /* Add/Subtract (immediate).  */
331   DPIMM_ADDSUB_011, /* Ditto.  */
332   DPIMM_LOG_100,    /* Logical (immediate).  */
333   DPIMM_MOV_101,    /* Move Wide (immediate).  */
334   DPIMM_BITF_110,   /* Bitfield.  */
335   DPIMM_EXTR_111    /* Extract.  */
336 };
337
338 /* Bits [29,28:26] of a LS are the secondary dispatch vector.  */
339
340 static inline uint32_t
341 dispatchLS (uint32_t instr)
342 {
343   return (  pickshift32 (instr, 29, 28, 1)
344           | pickshift32 (instr, 26, 26, 0));
345 }
346
347 /* The 8 possible values for bits [29,28:26] in a Load/Store
348    Instruction. Bits [28,25] are always _1_0.  */
349
350 enum DispatchLS
351 {
352   LS_EXCL_000,    /* Load/store exclusive (includes some unallocated).  */
353   LS_ADVSIMD_001, /* AdvSIMD load/store (various -- includes some unallocated).  */
354   LS_LIT_010,     /* Load register literal (includes some unallocated).  */
355   LS_LIT_011,     /* Ditto.  */
356   LS_PAIR_100,    /* Load/store register pair (various).  */
357   LS_PAIR_101,    /* Ditto.  */
358   LS_OTHER_110,   /* Other load/store formats.  */
359   LS_OTHER_111    /* Ditto.  */
360 };
361
362 /* Bits [28:24:21] of a DPReg are the secondary dispatch vector.  */
363
364 static inline uint32_t
365 dispatchDPReg (uint32_t instr)
366 {
367   return (  pickshift32 (instr, 28, 28, 2)
368           | pickshift32 (instr, 24, 24, 1)
369           | pickshift32 (instr, 21, 21, 0));
370 }
371
372 /* The 8 possible values for bits [28:24:21] in a Data Processing
373    Register Instruction. Bits [28,25] are always _101.  */
374
375 enum DispatchDPReg
376 {
377   DPREG_LOG_000,     /* Logical (shifted register).  */
378   DPREG_LOG_001,     /* Ditto.  */
379   DPREG_ADDSHF_010,  /* Add/subtract (shifted register).  */
380   DPREG_ADDEXT_011,  /* Add/subtract (extended register).  */
381   DPREG_ADDCOND_100, /* Add/subtract (with carry) AND
382                         Cond compare/select AND
383                         Data Processing (1/2 source).  */
384   DPREG_UNALLOC_101, /* Unallocated.  */
385   DPREG_3SRC_110,    /* Data Processing (3 source).  */
386   DPREG_3SRC_111     /* Data Processing (3 source).  */
387 };
388
389 /* bits [31,29] of a BrExSys are the secondary dispatch vector.  */
390
391 static inline uint32_t
392 dispatchBrExSys (uint32_t instr)
393 {
394   return pickbits32 (instr, 31, 29);
395 }
396
397 /* The 8 possible values for bits [31,29] in a Branch/Exception/System
398    Instruction. Bits [28,25] are always 101_.  */
399
400 enum DispatchBr
401 {
402   BR_IMM_000,     /* Unconditional branch (immediate).  */
403   BR_IMMCMP_001,  /* Compare & branch (immediate) AND
404                      Test & branch (immediate).  */
405   BR_IMMCOND_010, /* Conditional branch (immediate) AND Unallocated.  */
406   BR_UNALLOC_011, /* Unallocated.  */
407   BR_IMM_100,     /* Unconditional branch (immediate).  */
408   BR_IMMCMP_101,  /* Compare & branch (immediate) AND
409                      Test & branch (immediate).  */
410   BR_REG_110,     /* Unconditional branch (register) AND System AND
411                      Excn gen AND Unallocated.  */
412   BR_UNALLOC_111  /* Unallocated.  */
413 };
414
415 /* TODO still need to provide secondary decode and dispatch for
416    AdvSIMD Insructions with instr[28,25] = 0111 or 1111.  */
417
418 #endif /* _DECODE_H */