AArch64: implement GICv3 system registers
[oota-llvm.git] / lib / Target / AArch64 / Utils / AArch64BaseInfo.cpp
1 //===-- AArch64BaseInfo.cpp - AArch64 Base encoding information------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides basic encoding and assembly information for AArch64.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "AArch64BaseInfo.h"
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Regex.h"
18
19 using namespace llvm;
20
21 StringRef NamedImmMapper::toString(uint32_t Value, bool &Valid) const {
22   for (unsigned i = 0; i < NumPairs; ++i) {
23     if (Pairs[i].Value == Value) {
24       Valid = true;
25       return Pairs[i].Name;
26     }
27   }
28
29   Valid = false;
30   return StringRef();
31 }
32
33 uint32_t NamedImmMapper::fromString(StringRef Name, bool &Valid) const {
34   std::string LowerCaseName = Name.lower();
35   for (unsigned i = 0; i < NumPairs; ++i) {
36     if (Pairs[i].Name == LowerCaseName) {
37       Valid = true;
38       return Pairs[i].Value;
39     }
40   }
41
42   Valid = false;
43   return -1;
44 }
45
46 bool NamedImmMapper::validImm(uint32_t Value) const {
47   return Value < TooBigImm;
48 }
49
50 const NamedImmMapper::Mapping A64AT::ATMapper::ATPairs[] = {
51   {"s1e1r", S1E1R},
52   {"s1e2r", S1E2R},
53   {"s1e3r", S1E3R},
54   {"s1e1w", S1E1W},
55   {"s1e2w", S1E2W},
56   {"s1e3w", S1E3W},
57   {"s1e0r", S1E0R},
58   {"s1e0w", S1E0W},
59   {"s12e1r", S12E1R},
60   {"s12e1w", S12E1W},
61   {"s12e0r", S12E0R},
62   {"s12e0w", S12E0W},
63 };
64
65 A64AT::ATMapper::ATMapper()
66   : NamedImmMapper(ATPairs, 0) {}
67
68 const NamedImmMapper::Mapping A64DB::DBarrierMapper::DBarrierPairs[] = {
69   {"oshld", OSHLD},
70   {"oshst", OSHST},
71   {"osh", OSH},
72   {"nshld", NSHLD},
73   {"nshst", NSHST},
74   {"nsh", NSH},
75   {"ishld", ISHLD},
76   {"ishst", ISHST},
77   {"ish", ISH},
78   {"ld", LD},
79   {"st", ST},
80   {"sy", SY}
81 };
82
83 A64DB::DBarrierMapper::DBarrierMapper()
84   : NamedImmMapper(DBarrierPairs, 16u) {}
85
86 const NamedImmMapper::Mapping A64DC::DCMapper::DCPairs[] = {
87   {"zva", ZVA},
88   {"ivac", IVAC},
89   {"isw", ISW},
90   {"cvac", CVAC},
91   {"csw", CSW},
92   {"cvau", CVAU},
93   {"civac", CIVAC},
94   {"cisw", CISW}
95 };
96
97 A64DC::DCMapper::DCMapper()
98   : NamedImmMapper(DCPairs, 0) {}
99
100 const NamedImmMapper::Mapping A64IC::ICMapper::ICPairs[] = {
101   {"ialluis",  IALLUIS},
102   {"iallu", IALLU},
103   {"ivau", IVAU}
104 };
105
106 A64IC::ICMapper::ICMapper()
107   : NamedImmMapper(ICPairs, 0) {}
108
109 const NamedImmMapper::Mapping A64ISB::ISBMapper::ISBPairs[] = {
110   {"sy",  SY},
111 };
112
113 A64ISB::ISBMapper::ISBMapper()
114   : NamedImmMapper(ISBPairs, 16) {}
115
116 const NamedImmMapper::Mapping A64PRFM::PRFMMapper::PRFMPairs[] = {
117   {"pldl1keep", PLDL1KEEP},
118   {"pldl1strm", PLDL1STRM},
119   {"pldl2keep", PLDL2KEEP},
120   {"pldl2strm", PLDL2STRM},
121   {"pldl3keep", PLDL3KEEP},
122   {"pldl3strm", PLDL3STRM},
123   {"plil1keep", PLIL1KEEP},
124   {"plil1strm", PLIL1STRM},
125   {"plil2keep", PLIL2KEEP},
126   {"plil2strm", PLIL2STRM},
127   {"plil3keep", PLIL3KEEP},
128   {"plil3strm", PLIL3STRM},
129   {"pstl1keep", PSTL1KEEP},
130   {"pstl1strm", PSTL1STRM},
131   {"pstl2keep", PSTL2KEEP},
132   {"pstl2strm", PSTL2STRM},
133   {"pstl3keep", PSTL3KEEP},
134   {"pstl3strm", PSTL3STRM}
135 };
136
137 A64PRFM::PRFMMapper::PRFMMapper()
138   : NamedImmMapper(PRFMPairs, 32) {}
139
140 const NamedImmMapper::Mapping A64PState::PStateMapper::PStatePairs[] = {
141   {"spsel", SPSel},
142   {"daifset", DAIFSet},
143   {"daifclr", DAIFClr}
144 };
145
146 A64PState::PStateMapper::PStateMapper()
147   : NamedImmMapper(PStatePairs, 0) {}
148
149 const NamedImmMapper::Mapping A64SysReg::MRSMapper::MRSPairs[] = {
150   {"mdccsr_el0", MDCCSR_EL0},
151   {"dbgdtrrx_el0", DBGDTRRX_EL0},
152   {"mdrar_el1", MDRAR_EL1},
153   {"oslsr_el1", OSLSR_EL1},
154   {"dbgauthstatus_el1", DBGAUTHSTATUS_EL1},
155   {"pmceid0_el0", PMCEID0_EL0},
156   {"pmceid1_el0", PMCEID1_EL0},
157   {"midr_el1", MIDR_EL1},
158   {"ccsidr_el1", CCSIDR_EL1},
159   {"clidr_el1", CLIDR_EL1},
160   {"ctr_el0", CTR_EL0},
161   {"mpidr_el1", MPIDR_EL1},
162   {"revidr_el1", REVIDR_EL1},
163   {"aidr_el1", AIDR_EL1},
164   {"dczid_el0", DCZID_EL0},
165   {"id_pfr0_el1", ID_PFR0_EL1},
166   {"id_pfr1_el1", ID_PFR1_EL1},
167   {"id_dfr0_el1", ID_DFR0_EL1},
168   {"id_afr0_el1", ID_AFR0_EL1},
169   {"id_mmfr0_el1", ID_MMFR0_EL1},
170   {"id_mmfr1_el1", ID_MMFR1_EL1},
171   {"id_mmfr2_el1", ID_MMFR2_EL1},
172   {"id_mmfr3_el1", ID_MMFR3_EL1},
173   {"id_isar0_el1", ID_ISAR0_EL1},
174   {"id_isar1_el1", ID_ISAR1_EL1},
175   {"id_isar2_el1", ID_ISAR2_EL1},
176   {"id_isar3_el1", ID_ISAR3_EL1},
177   {"id_isar4_el1", ID_ISAR4_EL1},
178   {"id_isar5_el1", ID_ISAR5_EL1},
179   {"id_aa64pfr0_el1", ID_AA64PFR0_EL1},
180   {"id_aa64pfr1_el1", ID_AA64PFR1_EL1},
181   {"id_aa64dfr0_el1", ID_AA64DFR0_EL1},
182   {"id_aa64dfr1_el1", ID_AA64DFR1_EL1},
183   {"id_aa64afr0_el1", ID_AA64AFR0_EL1},
184   {"id_aa64afr1_el1", ID_AA64AFR1_EL1},
185   {"id_aa64isar0_el1", ID_AA64ISAR0_EL1},
186   {"id_aa64isar1_el1", ID_AA64ISAR1_EL1},
187   {"id_aa64mmfr0_el1", ID_AA64MMFR0_EL1},
188   {"id_aa64mmfr1_el1", ID_AA64MMFR1_EL1},
189   {"mvfr0_el1", MVFR0_EL1},
190   {"mvfr1_el1", MVFR1_EL1},
191   {"mvfr2_el1", MVFR2_EL1},
192   {"rvbar_el1", RVBAR_EL1},
193   {"rvbar_el2", RVBAR_EL2},
194   {"rvbar_el3", RVBAR_EL3},
195   {"isr_el1", ISR_EL1},
196   {"cntpct_el0", CNTPCT_EL0},
197   {"cntvct_el0", CNTVCT_EL0},
198
199   // GICv3 registers
200   {"icc_iar1_el1", ICC_IAR1_EL1},
201   {"icc_iar0_el1", ICC_IAR0_EL1},
202   {"icc_hppir1_el1", ICC_HPPIR1_EL1},
203   {"icc_hppir0_el1", ICC_HPPIR0_EL1},
204   {"icc_rpr_el1", ICC_RPR_EL1},
205   {"ich_vtr_el2", ICH_VTR_EL2},
206   {"ich_eisr_el2", ICH_EISR_EL2},
207   {"ich_elsr_el2", ICH_ELSR_EL2}
208 };
209
210 A64SysReg::MRSMapper::MRSMapper() {
211     InstPairs = &MRSPairs[0];
212     NumInstPairs = llvm::array_lengthof(MRSPairs);
213 }
214
215 const NamedImmMapper::Mapping A64SysReg::MSRMapper::MSRPairs[] = {
216   {"dbgdtrtx_el0", DBGDTRTX_EL0},
217   {"oslar_el1", OSLAR_EL1},
218   {"pmswinc_el0", PMSWINC_EL0},
219
220   // GICv3 registers
221   {"icc_eoir1_el1", ICC_EOIR1_EL1},
222   {"icc_eoir0_el1", ICC_EOIR0_EL1},
223   {"icc_dir_el1", ICC_DIR_EL1},
224   {"icc_sgi1r_el1", ICC_SGI1R_EL1},
225   {"icc_asgi1r_el1", ICC_ASGI1R_EL1},
226   {"icc_sgi0r_el1", ICC_SGI0R_EL1}
227 };
228
229 A64SysReg::MSRMapper::MSRMapper() {
230     InstPairs = &MSRPairs[0];
231     NumInstPairs = llvm::array_lengthof(MSRPairs);
232 }
233
234
235 const NamedImmMapper::Mapping A64SysReg::SysRegMapper::SysRegPairs[] = {
236   {"osdtrrx_el1", OSDTRRX_EL1},
237   {"osdtrtx_el1",  OSDTRTX_EL1},
238   {"teecr32_el1", TEECR32_EL1},
239   {"mdccint_el1", MDCCINT_EL1},
240   {"mdscr_el1", MDSCR_EL1},
241   {"dbgdtr_el0", DBGDTR_EL0},
242   {"oseccr_el1", OSECCR_EL1},
243   {"dbgvcr32_el2", DBGVCR32_EL2},
244   {"dbgbvr0_el1", DBGBVR0_EL1},
245   {"dbgbvr1_el1", DBGBVR1_EL1},
246   {"dbgbvr2_el1", DBGBVR2_EL1},
247   {"dbgbvr3_el1", DBGBVR3_EL1},
248   {"dbgbvr4_el1", DBGBVR4_EL1},
249   {"dbgbvr5_el1", DBGBVR5_EL1},
250   {"dbgbvr6_el1", DBGBVR6_EL1},
251   {"dbgbvr7_el1", DBGBVR7_EL1},
252   {"dbgbvr8_el1", DBGBVR8_EL1},
253   {"dbgbvr9_el1", DBGBVR9_EL1},
254   {"dbgbvr10_el1", DBGBVR10_EL1},
255   {"dbgbvr11_el1", DBGBVR11_EL1},
256   {"dbgbvr12_el1", DBGBVR12_EL1},
257   {"dbgbvr13_el1", DBGBVR13_EL1},
258   {"dbgbvr14_el1", DBGBVR14_EL1},
259   {"dbgbvr15_el1", DBGBVR15_EL1},
260   {"dbgbcr0_el1", DBGBCR0_EL1},
261   {"dbgbcr1_el1", DBGBCR1_EL1},
262   {"dbgbcr2_el1", DBGBCR2_EL1},
263   {"dbgbcr3_el1", DBGBCR3_EL1},
264   {"dbgbcr4_el1", DBGBCR4_EL1},
265   {"dbgbcr5_el1", DBGBCR5_EL1},
266   {"dbgbcr6_el1", DBGBCR6_EL1},
267   {"dbgbcr7_el1", DBGBCR7_EL1},
268   {"dbgbcr8_el1", DBGBCR8_EL1},
269   {"dbgbcr9_el1", DBGBCR9_EL1},
270   {"dbgbcr10_el1", DBGBCR10_EL1},
271   {"dbgbcr11_el1", DBGBCR11_EL1},
272   {"dbgbcr12_el1", DBGBCR12_EL1},
273   {"dbgbcr13_el1", DBGBCR13_EL1},
274   {"dbgbcr14_el1", DBGBCR14_EL1},
275   {"dbgbcr15_el1", DBGBCR15_EL1},
276   {"dbgwvr0_el1", DBGWVR0_EL1},
277   {"dbgwvr1_el1", DBGWVR1_EL1},
278   {"dbgwvr2_el1", DBGWVR2_EL1},
279   {"dbgwvr3_el1", DBGWVR3_EL1},
280   {"dbgwvr4_el1", DBGWVR4_EL1},
281   {"dbgwvr5_el1", DBGWVR5_EL1},
282   {"dbgwvr6_el1", DBGWVR6_EL1},
283   {"dbgwvr7_el1", DBGWVR7_EL1},
284   {"dbgwvr8_el1", DBGWVR8_EL1},
285   {"dbgwvr9_el1", DBGWVR9_EL1},
286   {"dbgwvr10_el1", DBGWVR10_EL1},
287   {"dbgwvr11_el1", DBGWVR11_EL1},
288   {"dbgwvr12_el1", DBGWVR12_EL1},
289   {"dbgwvr13_el1", DBGWVR13_EL1},
290   {"dbgwvr14_el1", DBGWVR14_EL1},
291   {"dbgwvr15_el1", DBGWVR15_EL1},
292   {"dbgwcr0_el1", DBGWCR0_EL1},
293   {"dbgwcr1_el1", DBGWCR1_EL1},
294   {"dbgwcr2_el1", DBGWCR2_EL1},
295   {"dbgwcr3_el1", DBGWCR3_EL1},
296   {"dbgwcr4_el1", DBGWCR4_EL1},
297   {"dbgwcr5_el1", DBGWCR5_EL1},
298   {"dbgwcr6_el1", DBGWCR6_EL1},
299   {"dbgwcr7_el1", DBGWCR7_EL1},
300   {"dbgwcr8_el1", DBGWCR8_EL1},
301   {"dbgwcr9_el1", DBGWCR9_EL1},
302   {"dbgwcr10_el1", DBGWCR10_EL1},
303   {"dbgwcr11_el1", DBGWCR11_EL1},
304   {"dbgwcr12_el1", DBGWCR12_EL1},
305   {"dbgwcr13_el1", DBGWCR13_EL1},
306   {"dbgwcr14_el1", DBGWCR14_EL1},
307   {"dbgwcr15_el1", DBGWCR15_EL1},
308   {"teehbr32_el1", TEEHBR32_EL1},
309   {"osdlr_el1", OSDLR_EL1},
310   {"dbgprcr_el1", DBGPRCR_EL1},
311   {"dbgclaimset_el1", DBGCLAIMSET_EL1},
312   {"dbgclaimclr_el1", DBGCLAIMCLR_EL1},
313   {"csselr_el1", CSSELR_EL1},
314   {"vpidr_el2", VPIDR_EL2},
315   {"vmpidr_el2", VMPIDR_EL2},
316   {"sctlr_el1", SCTLR_EL1},
317   {"sctlr_el2", SCTLR_EL2},
318   {"sctlr_el3", SCTLR_EL3},
319   {"actlr_el1", ACTLR_EL1},
320   {"actlr_el2", ACTLR_EL2},
321   {"actlr_el3", ACTLR_EL3},
322   {"cpacr_el1", CPACR_EL1},
323   {"hcr_el2", HCR_EL2},
324   {"scr_el3", SCR_EL3},
325   {"mdcr_el2", MDCR_EL2},
326   {"sder32_el3", SDER32_EL3},
327   {"cptr_el2", CPTR_EL2},
328   {"cptr_el3", CPTR_EL3},
329   {"hstr_el2", HSTR_EL2},
330   {"hacr_el2", HACR_EL2},
331   {"mdcr_el3", MDCR_EL3},
332   {"ttbr0_el1", TTBR0_EL1},
333   {"ttbr0_el2", TTBR0_EL2},
334   {"ttbr0_el3", TTBR0_EL3},
335   {"ttbr1_el1", TTBR1_EL1},
336   {"tcr_el1", TCR_EL1},
337   {"tcr_el2", TCR_EL2},
338   {"tcr_el3", TCR_EL3},
339   {"vttbr_el2", VTTBR_EL2},
340   {"vtcr_el2", VTCR_EL2},
341   {"dacr32_el2", DACR32_EL2},
342   {"spsr_el1", SPSR_EL1},
343   {"spsr_el2", SPSR_EL2},
344   {"spsr_el3", SPSR_EL3},
345   {"elr_el1", ELR_EL1},
346   {"elr_el2", ELR_EL2},
347   {"elr_el3", ELR_EL3},
348   {"sp_el0", SP_EL0},
349   {"sp_el1", SP_EL1},
350   {"sp_el2", SP_EL2},
351   {"spsel", SPSel},
352   {"nzcv", NZCV},
353   {"daif", DAIF},
354   {"currentel", CurrentEL},
355   {"spsr_irq", SPSR_irq},
356   {"spsr_abt", SPSR_abt},
357   {"spsr_und", SPSR_und},
358   {"spsr_fiq", SPSR_fiq},
359   {"fpcr", FPCR},
360   {"fpsr", FPSR},
361   {"dspsr_el0", DSPSR_EL0},
362   {"dlr_el0", DLR_EL0},
363   {"ifsr32_el2", IFSR32_EL2},
364   {"afsr0_el1", AFSR0_EL1},
365   {"afsr0_el2", AFSR0_EL2},
366   {"afsr0_el3", AFSR0_EL3},
367   {"afsr1_el1", AFSR1_EL1},
368   {"afsr1_el2", AFSR1_EL2},
369   {"afsr1_el3", AFSR1_EL3},
370   {"esr_el1", ESR_EL1},
371   {"esr_el2", ESR_EL2},
372   {"esr_el3", ESR_EL3},
373   {"fpexc32_el2", FPEXC32_EL2},
374   {"far_el1", FAR_EL1},
375   {"far_el2", FAR_EL2},
376   {"far_el3", FAR_EL3},
377   {"hpfar_el2", HPFAR_EL2},
378   {"par_el1", PAR_EL1},
379   {"pmcr_el0", PMCR_EL0},
380   {"pmcntenset_el0", PMCNTENSET_EL0},
381   {"pmcntenclr_el0", PMCNTENCLR_EL0},
382   {"pmovsclr_el0", PMOVSCLR_EL0},
383   {"pmselr_el0", PMSELR_EL0},
384   {"pmccntr_el0", PMCCNTR_EL0},
385   {"pmxevtyper_el0", PMXEVTYPER_EL0},
386   {"pmxevcntr_el0", PMXEVCNTR_EL0},
387   {"pmuserenr_el0", PMUSERENR_EL0},
388   {"pmintenset_el1", PMINTENSET_EL1},
389   {"pmintenclr_el1", PMINTENCLR_EL1},
390   {"pmovsset_el0", PMOVSSET_EL0},
391   {"mair_el1", MAIR_EL1},
392   {"mair_el2", MAIR_EL2},
393   {"mair_el3", MAIR_EL3},
394   {"amair_el1", AMAIR_EL1},
395   {"amair_el2", AMAIR_EL2},
396   {"amair_el3", AMAIR_EL3},
397   {"vbar_el1", VBAR_EL1},
398   {"vbar_el2", VBAR_EL2},
399   {"vbar_el3", VBAR_EL3},
400   {"rmr_el1", RMR_EL1},
401   {"rmr_el2", RMR_EL2},
402   {"rmr_el3", RMR_EL3},
403   {"contextidr_el1", CONTEXTIDR_EL1},
404   {"tpidr_el0", TPIDR_EL0},
405   {"tpidr_el2", TPIDR_EL2},
406   {"tpidr_el3", TPIDR_EL3},
407   {"tpidrro_el0", TPIDRRO_EL0},
408   {"tpidr_el1", TPIDR_EL1},
409   {"cntfrq_el0", CNTFRQ_EL0},
410   {"cntvoff_el2", CNTVOFF_EL2},
411   {"cntkctl_el1", CNTKCTL_EL1},
412   {"cnthctl_el2", CNTHCTL_EL2},
413   {"cntp_tval_el0", CNTP_TVAL_EL0},
414   {"cnthp_tval_el2", CNTHP_TVAL_EL2},
415   {"cntps_tval_el1", CNTPS_TVAL_EL1},
416   {"cntp_ctl_el0", CNTP_CTL_EL0},
417   {"cnthp_ctl_el2", CNTHP_CTL_EL2},
418   {"cntps_ctl_el1", CNTPS_CTL_EL1},
419   {"cntp_cval_el0", CNTP_CVAL_EL0},
420   {"cnthp_cval_el2", CNTHP_CVAL_EL2},
421   {"cntps_cval_el1", CNTPS_CVAL_EL1},
422   {"cntv_tval_el0", CNTV_TVAL_EL0},
423   {"cntv_ctl_el0", CNTV_CTL_EL0},
424   {"cntv_cval_el0", CNTV_CVAL_EL0},
425   {"pmevcntr0_el0", PMEVCNTR0_EL0},
426   {"pmevcntr1_el0", PMEVCNTR1_EL0},
427   {"pmevcntr2_el0", PMEVCNTR2_EL0},
428   {"pmevcntr3_el0", PMEVCNTR3_EL0},
429   {"pmevcntr4_el0", PMEVCNTR4_EL0},
430   {"pmevcntr5_el0", PMEVCNTR5_EL0},
431   {"pmevcntr6_el0", PMEVCNTR6_EL0},
432   {"pmevcntr7_el0", PMEVCNTR7_EL0},
433   {"pmevcntr8_el0", PMEVCNTR8_EL0},
434   {"pmevcntr9_el0", PMEVCNTR9_EL0},
435   {"pmevcntr10_el0", PMEVCNTR10_EL0},
436   {"pmevcntr11_el0", PMEVCNTR11_EL0},
437   {"pmevcntr12_el0", PMEVCNTR12_EL0},
438   {"pmevcntr13_el0", PMEVCNTR13_EL0},
439   {"pmevcntr14_el0", PMEVCNTR14_EL0},
440   {"pmevcntr15_el0", PMEVCNTR15_EL0},
441   {"pmevcntr16_el0", PMEVCNTR16_EL0},
442   {"pmevcntr17_el0", PMEVCNTR17_EL0},
443   {"pmevcntr18_el0", PMEVCNTR18_EL0},
444   {"pmevcntr19_el0", PMEVCNTR19_EL0},
445   {"pmevcntr20_el0", PMEVCNTR20_EL0},
446   {"pmevcntr21_el0", PMEVCNTR21_EL0},
447   {"pmevcntr22_el0", PMEVCNTR22_EL0},
448   {"pmevcntr23_el0", PMEVCNTR23_EL0},
449   {"pmevcntr24_el0", PMEVCNTR24_EL0},
450   {"pmevcntr25_el0", PMEVCNTR25_EL0},
451   {"pmevcntr26_el0", PMEVCNTR26_EL0},
452   {"pmevcntr27_el0", PMEVCNTR27_EL0},
453   {"pmevcntr28_el0", PMEVCNTR28_EL0},
454   {"pmevcntr29_el0", PMEVCNTR29_EL0},
455   {"pmevcntr30_el0", PMEVCNTR30_EL0},
456   {"pmccfiltr_el0", PMCCFILTR_EL0},
457   {"pmevtyper0_el0", PMEVTYPER0_EL0},
458   {"pmevtyper1_el0", PMEVTYPER1_EL0},
459   {"pmevtyper2_el0", PMEVTYPER2_EL0},
460   {"pmevtyper3_el0", PMEVTYPER3_EL0},
461   {"pmevtyper4_el0", PMEVTYPER4_EL0},
462   {"pmevtyper5_el0", PMEVTYPER5_EL0},
463   {"pmevtyper6_el0", PMEVTYPER6_EL0},
464   {"pmevtyper7_el0", PMEVTYPER7_EL0},
465   {"pmevtyper8_el0", PMEVTYPER8_EL0},
466   {"pmevtyper9_el0", PMEVTYPER9_EL0},
467   {"pmevtyper10_el0", PMEVTYPER10_EL0},
468   {"pmevtyper11_el0", PMEVTYPER11_EL0},
469   {"pmevtyper12_el0", PMEVTYPER12_EL0},
470   {"pmevtyper13_el0", PMEVTYPER13_EL0},
471   {"pmevtyper14_el0", PMEVTYPER14_EL0},
472   {"pmevtyper15_el0", PMEVTYPER15_EL0},
473   {"pmevtyper16_el0", PMEVTYPER16_EL0},
474   {"pmevtyper17_el0", PMEVTYPER17_EL0},
475   {"pmevtyper18_el0", PMEVTYPER18_EL0},
476   {"pmevtyper19_el0", PMEVTYPER19_EL0},
477   {"pmevtyper20_el0", PMEVTYPER20_EL0},
478   {"pmevtyper21_el0", PMEVTYPER21_EL0},
479   {"pmevtyper22_el0", PMEVTYPER22_EL0},
480   {"pmevtyper23_el0", PMEVTYPER23_EL0},
481   {"pmevtyper24_el0", PMEVTYPER24_EL0},
482   {"pmevtyper25_el0", PMEVTYPER25_EL0},
483   {"pmevtyper26_el0", PMEVTYPER26_EL0},
484   {"pmevtyper27_el0", PMEVTYPER27_EL0},
485   {"pmevtyper28_el0", PMEVTYPER28_EL0},
486   {"pmevtyper29_el0", PMEVTYPER29_EL0},
487   {"pmevtyper30_el0", PMEVTYPER30_EL0},
488
489   // GICv3 registers
490   {"icc_bpr1_el1", ICC_BPR1_EL1},
491   {"icc_bpr0_el1", ICC_BPR0_EL1},
492   {"icc_pmr_el1", ICC_PMR_EL1},
493   {"icc_ctlr_el1", ICC_CTLR_EL1},
494   {"icc_ctlr_el3", ICC_CTLR_EL3},
495   {"icc_sre_el1", ICC_SRE_EL1},
496   {"icc_sre_el2", ICC_SRE_EL2},
497   {"icc_sre_el3", ICC_SRE_EL3},
498   {"icc_igrpen0_el1", ICC_IGRPEN0_EL1},
499   {"icc_igrpen1_el1", ICC_IGRPEN1_EL1},
500   {"icc_igrpen1_el3", ICC_IGRPEN1_EL3},
501   {"icc_seien_el1", ICC_SEIEN_EL1},
502   {"icc_ap0r0_el1", ICC_AP0R0_EL1},
503   {"icc_ap0r1_el1", ICC_AP0R1_EL1},
504   {"icc_ap0r2_el1", ICC_AP0R2_EL1},
505   {"icc_ap0r3_el1", ICC_AP0R3_EL1},
506   {"icc_ap1r0_el1", ICC_AP1R0_EL1},
507   {"icc_ap1r1_el1", ICC_AP1R1_EL1},
508   {"icc_ap1r2_el1", ICC_AP1R2_EL1},
509   {"icc_ap1r3_el1", ICC_AP1R3_EL1},
510   {"ich_ap0r0_el2", ICH_AP0R0_EL2},
511   {"ich_ap0r1_el2", ICH_AP0R1_EL2},
512   {"ich_ap0r2_el2", ICH_AP0R2_EL2},
513   {"ich_ap0r3_el2", ICH_AP0R3_EL2},
514   {"ich_ap1r0_el2", ICH_AP1R0_EL2},
515   {"ich_ap1r1_el2", ICH_AP1R1_EL2},
516   {"ich_ap1r2_el2", ICH_AP1R2_EL2},
517   {"ich_ap1r3_el2", ICH_AP1R3_EL2},
518   {"ich_hcr_el2", ICH_HCR_EL2},
519   {"ich_misr_el2", ICH_MISR_EL2},
520   {"ich_vmcr_el2", ICH_VMCR_EL2},
521   {"ich_vseir_el2", ICH_VSEIR_EL2},
522   {"ich_lr0_el2", ICH_LR0_EL2},
523   {"ich_lr1_el2", ICH_LR1_EL2},
524   {"ich_lr2_el2", ICH_LR2_EL2},
525   {"ich_lr3_el2", ICH_LR3_EL2},
526   {"ich_lr4_el2", ICH_LR4_EL2},
527   {"ich_lr5_el2", ICH_LR5_EL2},
528   {"ich_lr6_el2", ICH_LR6_EL2},
529   {"ich_lr7_el2", ICH_LR7_EL2},
530   {"ich_lr8_el2", ICH_LR8_EL2},
531   {"ich_lr9_el2", ICH_LR9_EL2},
532   {"ich_lr10_el2", ICH_LR10_EL2},
533   {"ich_lr11_el2", ICH_LR11_EL2},
534   {"ich_lr12_el2", ICH_LR12_EL2},
535   {"ich_lr13_el2", ICH_LR13_EL2},
536   {"ich_lr14_el2", ICH_LR14_EL2},
537   {"ich_lr15_el2", ICH_LR15_EL2}
538 };
539
540 uint32_t
541 A64SysReg::SysRegMapper::fromString(StringRef Name, bool &Valid) const {
542   // First search the registers shared by all
543   std::string NameLower = Name.lower();
544   for (unsigned i = 0; i < array_lengthof(SysRegPairs); ++i) {
545     if (SysRegPairs[i].Name == NameLower) {
546       Valid = true;
547       return SysRegPairs[i].Value;
548     }
549   }
550
551   // Now try the instruction-specific registers (either read-only or
552   // write-only).
553   for (unsigned i = 0; i < NumInstPairs; ++i) {
554     if (InstPairs[i].Name == NameLower) {
555       Valid = true;
556       return InstPairs[i].Value;
557     }
558   }
559
560   // Try to parse an S<op0>_<op1>_<Cn>_<Cm>_<op2> register name, where the bits
561   // are: 11 xxx 1x11 xxxx xxx
562   Regex GenericRegPattern("^s3_([0-7])_c(1[15])_c([0-9]|1[0-5])_([0-7])$");
563
564   SmallVector<StringRef, 4> Ops;
565   if (!GenericRegPattern.match(NameLower, &Ops)) {
566     Valid = false;
567     return -1;
568   }
569
570   uint32_t Op0 = 3, Op1 = 0, CRn = 0, CRm = 0, Op2 = 0;
571   uint32_t Bits;
572   Ops[1].getAsInteger(10, Op1);
573   Ops[2].getAsInteger(10, CRn);
574   Ops[3].getAsInteger(10, CRm);
575   Ops[4].getAsInteger(10, Op2);
576   Bits = (Op0 << 14) | (Op1 << 11) | (CRn << 7) | (CRm << 3) | Op2;
577
578   Valid = true;
579   return Bits;
580 }
581
582 std::string
583 A64SysReg::SysRegMapper::toString(uint32_t Bits, bool &Valid) const {
584   for (unsigned i = 0; i < array_lengthof(SysRegPairs); ++i) {
585     if (SysRegPairs[i].Value == Bits) {
586       Valid = true;
587       return SysRegPairs[i].Name;
588     }
589   }
590
591   for (unsigned i = 0; i < NumInstPairs; ++i) {
592     if (InstPairs[i].Value == Bits) {
593       Valid = true;
594       return InstPairs[i].Name;
595     }
596   }
597
598   uint32_t Op0 = (Bits >> 14) & 0x3;
599   uint32_t Op1 = (Bits >> 11) & 0x7;
600   uint32_t CRn = (Bits >> 7) & 0xf;
601   uint32_t CRm = (Bits >> 3) & 0xf;
602   uint32_t Op2 = Bits & 0x7;
603
604   // Only combinations matching: 11 xxx 1x11 xxxx xxx are valid for a generic
605   // name.
606   if (Op0 != 3 || (CRn != 11 && CRn != 15)) {
607       Valid = false;
608       return "";
609   }
610
611   assert(Op0 == 3 && (CRn == 11 || CRn == 15) && "Invalid generic sysreg");
612
613   Valid = true;
614   return "s3_" + utostr(Op1) + "_c" + utostr(CRn)
615                + "_c" + utostr(CRm) + "_" + utostr(Op2);
616 }
617
618 const NamedImmMapper::Mapping A64TLBI::TLBIMapper::TLBIPairs[] = {
619   {"ipas2e1is", IPAS2E1IS},
620   {"ipas2le1is", IPAS2LE1IS},
621   {"vmalle1is", VMALLE1IS},
622   {"alle2is", ALLE2IS},
623   {"alle3is", ALLE3IS},
624   {"vae1is", VAE1IS},
625   {"vae2is", VAE2IS},
626   {"vae3is", VAE3IS},
627   {"aside1is", ASIDE1IS},
628   {"vaae1is", VAAE1IS},
629   {"alle1is", ALLE1IS},
630   {"vale1is", VALE1IS},
631   {"vale2is", VALE2IS},
632   {"vale3is", VALE3IS},
633   {"vmalls12e1is", VMALLS12E1IS},
634   {"vaale1is", VAALE1IS},
635   {"ipas2e1", IPAS2E1},
636   {"ipas2le1", IPAS2LE1},
637   {"vmalle1", VMALLE1},
638   {"alle2", ALLE2},
639   {"alle3", ALLE3},
640   {"vae1", VAE1},
641   {"vae2", VAE2},
642   {"vae3", VAE3},
643   {"aside1", ASIDE1},
644   {"vaae1", VAAE1},
645   {"alle1", ALLE1},
646   {"vale1", VALE1},
647   {"vale2", VALE2},
648   {"vale3", VALE3},
649   {"vmalls12e1", VMALLS12E1},
650   {"vaale1", VAALE1}
651 };
652
653 A64TLBI::TLBIMapper::TLBIMapper()
654   : NamedImmMapper(TLBIPairs, 0) {}
655
656 bool A64Imms::isFPImm(const APFloat &Val, uint32_t &Imm8Bits) {
657   const fltSemantics &Sem = Val.getSemantics();
658   unsigned FracBits = APFloat::semanticsPrecision(Sem) - 1;
659
660   uint32_t ExpMask;
661   switch (FracBits) {
662   case 10: // IEEE half-precision
663     ExpMask = 0x1f;
664     break;
665   case 23: // IEEE single-precision
666     ExpMask = 0xff;
667     break;
668   case 52: // IEEE double-precision
669     ExpMask = 0x7ff;
670     break;
671   case 112: // IEEE quad-precision
672     // No immediates are valid for double precision.
673     return false;
674   default:
675     llvm_unreachable("Only half, single and double precision supported");
676   }
677
678   uint32_t ExpStart = FracBits;
679   uint64_t FracMask = (1ULL << FracBits) - 1;
680
681   uint32_t Sign = Val.isNegative();
682
683   uint64_t Bits= Val.bitcastToAPInt().getLimitedValue();
684   uint64_t Fraction = Bits & FracMask;
685   int32_t Exponent = ((Bits >> ExpStart) & ExpMask);
686   Exponent -= ExpMask >> 1;
687
688   // S[d] = imm8<7>:NOT(imm8<6>):Replicate(imm8<6>, 5):imm8<5:0>:Zeros(19)
689   // D[d] = imm8<7>:NOT(imm8<6>):Replicate(imm8<6>, 8):imm8<5:0>:Zeros(48)
690   // This translates to: only 4 bits of fraction; -3 <= exp <= 4.
691   uint64_t A64FracStart = FracBits - 4;
692   uint64_t A64FracMask = 0xf;
693
694   // Are there too many fraction bits?
695   if (Fraction & ~(A64FracMask << A64FracStart))
696     return false;
697
698   if (Exponent < -3 || Exponent > 4)
699     return false;
700
701   uint32_t PackedFraction = (Fraction >> A64FracStart) & A64FracMask;
702   uint32_t PackedExp = (Exponent + 7) & 0x7;
703
704   Imm8Bits = (Sign << 7) | (PackedExp << 4) | PackedFraction;
705   return true;
706 }
707
708 // Encoding of the immediate for logical (immediate) instructions:
709 //
710 // | N | imms   | immr   | size | R            | S            |
711 // |---+--------+--------+------+--------------+--------------|
712 // | 1 | ssssss | rrrrrr |   64 | UInt(rrrrrr) | UInt(ssssss) |
713 // | 0 | 0sssss | xrrrrr |   32 | UInt(rrrrr)  | UInt(sssss)  |
714 // | 0 | 10ssss | xxrrrr |   16 | UInt(rrrr)   | UInt(ssss)   |
715 // | 0 | 110sss | xxxrrr |    8 | UInt(rrr)    | UInt(sss)    |
716 // | 0 | 1110ss | xxxxrr |    4 | UInt(rr)     | UInt(ss)     |
717 // | 0 | 11110s | xxxxxr |    2 | UInt(r)      | UInt(s)      |
718 // | 0 | 11111x | -      |      | UNALLOCATED  |              |
719 //
720 // Columns 'R', 'S' and 'size' specify a "bitmask immediate" of size bits in
721 // which the lower S+1 bits are ones and the remaining bits are zero, then
722 // rotated right by R bits, which is then replicated across the datapath.
723 //
724 // + Values of 'N', 'imms' and 'immr' which do not match the above table are
725 //   RESERVED.
726 // + If all 's' bits in the imms field are set then the instruction is
727 //   RESERVED.
728 // + The 'x' bits in the 'immr' field are IGNORED.
729
730 bool A64Imms::isLogicalImm(unsigned RegWidth, uint64_t Imm, uint32_t &Bits) {
731   int RepeatWidth;
732   int Rotation = 0;
733   int Num1s = 0;
734
735   // Because there are S+1 ones in the replicated mask, an immediate of all
736   // zeros is not allowed. Filtering it here is probably more efficient.
737   if (Imm == 0) return false;
738
739   for (RepeatWidth = RegWidth; RepeatWidth > 1; RepeatWidth /= 2) {
740     uint64_t RepeatMask = RepeatWidth == 64 ? -1 : (1ULL << RepeatWidth) - 1;
741     uint64_t ReplicatedMask = Imm & RepeatMask;
742
743     if (ReplicatedMask == 0) continue;
744
745     // First we have to make sure the mask is actually repeated in each slot for
746     // this width-specifier.
747     bool IsReplicatedMask = true;
748     for (unsigned i = RepeatWidth; i < RegWidth; i += RepeatWidth) {
749       if (((Imm >> i) & RepeatMask) != ReplicatedMask) {
750         IsReplicatedMask = false;
751         break;
752       }
753     }
754     if (!IsReplicatedMask) continue;
755
756     // Now we have to work out the amount of rotation needed. The first part of
757     // this calculation is actually independent of RepeatWidth, but the complex
758     // case will depend on it.
759     Rotation = CountTrailingZeros_64(Imm);
760     if (Rotation == 0) {
761       // There were no leading zeros, which means it's either in place or there
762       // are 1s at each end (e.g. 0x8003 needs rotating).
763       Rotation = RegWidth == 64 ? CountLeadingOnes_64(Imm)
764                                 : CountLeadingOnes_32(Imm);
765       Rotation = RepeatWidth - Rotation;
766     }
767
768     uint64_t ReplicatedOnes = (ReplicatedMask >> Rotation)
769       | ((ReplicatedMask << (RepeatWidth - Rotation)) & RepeatMask);
770     // Of course, they may not actually be ones, so we have to check that:
771     if (!isMask_64(ReplicatedOnes))
772       continue;
773
774     Num1s = CountTrailingOnes_64(ReplicatedOnes);
775
776     // We know we've got an almost valid encoding (certainly, if this is invalid
777     // no other parameters would work).
778     break;
779   }
780
781   // The encodings which would produce all 1s are RESERVED.
782   if (RepeatWidth == 1 || Num1s == RepeatWidth) return false;
783
784   uint32_t N = RepeatWidth == 64;
785   uint32_t ImmR = RepeatWidth - Rotation;
786   uint32_t ImmS = Num1s - 1;
787
788   switch (RepeatWidth) {
789   default: break; // No action required for other valid rotations.
790   case 16: ImmS |= 0x20; break; // 10ssss
791   case 8: ImmS |= 0x30; break;  // 110sss
792   case 4: ImmS |= 0x38; break;  // 1110ss
793   case 2: ImmS |= 0x3c; break;  // 11110s
794   }
795
796   Bits = ImmS | (ImmR << 6) | (N << 12);
797
798   return true;
799 }
800
801
802 bool A64Imms::isLogicalImmBits(unsigned RegWidth, uint32_t Bits,
803                                uint64_t &Imm) {
804   uint32_t N = Bits >> 12;
805   uint32_t ImmR = (Bits >> 6) & 0x3f;
806   uint32_t ImmS = Bits & 0x3f;
807
808   // N=1 encodes a 64-bit replication and is invalid for the 32-bit
809   // instructions.
810   if (RegWidth == 32 && N != 0) return false;
811
812   int Width = 0;
813   if (N == 1)
814     Width = 64;
815   else if ((ImmS & 0x20) == 0)
816     Width = 32;
817   else if ((ImmS & 0x10) == 0)
818     Width = 16;
819   else if ((ImmS & 0x08) == 0)
820     Width = 8;
821   else if ((ImmS & 0x04) == 0)
822     Width = 4;
823   else if ((ImmS & 0x02) == 0)
824     Width = 2;
825   else {
826     // ImmS  is 0b11111x: UNALLOCATED
827     return false;
828   }
829
830   int Num1s = (ImmS & (Width - 1)) + 1;
831
832   // All encodings which would map to -1 (signed) are RESERVED.
833   if (Num1s == Width) return false;
834
835   int Rotation = (ImmR & (Width - 1));
836   uint64_t Mask = (1ULL << Num1s) - 1;
837   uint64_t WidthMask = Width == 64 ? -1 : (1ULL << Width) - 1;
838   Mask = (Mask >> Rotation)
839     | ((Mask << (Width - Rotation)) & WidthMask);
840
841   Imm = 0;
842   for (unsigned i = 0; i < RegWidth / Width; ++i) {
843     Imm |= Mask;
844     Mask <<= Width;
845   }
846
847   return true;
848 }
849
850 bool A64Imms::isMOVZImm(int RegWidth, uint64_t Value, int &UImm16, int &Shift) {
851   // If high bits are set then a 32-bit MOVZ can't possibly work.
852   if (RegWidth == 32 && (Value & ~0xffffffffULL))
853     return false;
854
855   for (int i = 0; i < RegWidth; i += 16) {
856     // If the value is 0 when we mask out all the bits that could be set with
857     // the current LSL value then it's representable.
858     if ((Value & ~(0xffffULL << i)) == 0) {
859       Shift = i / 16;
860       UImm16 = (Value >> i) & 0xffff;
861       return true;
862     }
863   }
864   return false;
865 }
866
867 bool A64Imms::isMOVNImm(int RegWidth, uint64_t Value, int &UImm16, int &Shift) {
868   // MOVN is defined to set its register to NOT(LSL(imm16, shift)).
869
870   // We have to be a little careful about a 32-bit register: 0xffff_1234 *is*
871   // representable, but ~0xffff_1234 == 0xffff_ffff_0000_edcb which is not
872   // a valid input for isMOVZImm.
873   if (RegWidth == 32 && (Value & ~0xffffffffULL))
874     return false;
875
876   uint64_t MOVZEquivalent = RegWidth == 32 ? ~Value & 0xffffffff : ~Value;
877
878   return isMOVZImm(RegWidth, MOVZEquivalent, UImm16, Shift);
879 }
880
881 bool A64Imms::isOnlyMOVNImm(int RegWidth, uint64_t Value,
882                             int &UImm16, int &Shift) {
883   if (isMOVZImm(RegWidth, Value, UImm16, Shift))
884     return false;
885
886   return isMOVNImm(RegWidth, Value, UImm16, Shift);
887 }