1// -*- mode:c++ -*-
2
3// Copyright (c) 2010-2013,2017-2018 ARM Limited
4// All rights reserved
5//
6// The license below extends only to copyright in the software and shall
7// not be construed as granting a license to any other intellectual
8// property including but not limited to intellectual property relating
9// to a hardware implementation of the functionality of the software
10// licensed hereunder.  You may use the software subject to the license
11// terms below provided that you ensure that this notice is replicated
12// unmodified and in its entirety in all distributions of the software,
13// modified or unmodified, in source code or in binary form.
14//
15// Redistribution and use in source and binary forms, with or without
16// modification, are permitted provided that the following conditions are
17// met: redistributions of source code must retain the above copyright
18// notice, this list of conditions and the following disclaimer;
19// redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution;
22// neither the name of the copyright holders nor the names of its
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Authors: Gabe Black
39
40let {{
41
42    svcCode = '''
43    ThreadContext *tc = xc->tcBase();
44
45    const auto semihost_imm = Thumb? 0xAB : 0x123456;
46
47    if (ArmSystem::haveSemihosting(tc) && imm == semihost_imm) {
48        R0 = ArmSystem::callSemihosting32(tc, R0, R1);
49    } else {
50        fault = std::make_shared<SupervisorCall>(machInst, imm);
51    }
52    '''
53
54    svcIop = InstObjParams("svc", "Svc", "ImmOp",
55                           { "code": svcCode,
56                             "predicate_test": predicateTest,
57                             "thumb_semihost": '0xAB',
58                             "arm_semihost": '0x123456' },
59                           ["IsSyscall", "IsNonSpeculative",
60                            "IsSerializeAfter"])
61    header_output = ImmOpDeclare.subst(svcIop)
62    decoder_output = SemihostConstructor.subst(svcIop)
63    exec_output = PredOpExecute.subst(svcIop)
64
65    hltCode = '''
66    ThreadContext *tc = xc->tcBase();
67
68    const auto semihost_imm = Thumb? 0x3C : 0xF000;
69
70    if (ArmSystem::haveSemihosting(tc) && imm == semihost_imm) {
71        R0 = ArmSystem::callSemihosting32(tc, R0, R1);
72    } else {
73        // HLT instructions aren't implemented, so treat them as undefined
74        // instructions.
75        fault = std::make_shared<UndefinedInstruction>(
76            machInst, false, mnemonic);
77    }
78    '''
79
80    hltIop = InstObjParams("hlt", "Hlt", "ImmOp",
81                           { "code": hltCode,
82                             "predicate_test": predicateTest,
83                             "thumb_semihost": '0x3C',
84                             "arm_semihost": '0xF000' },
85                           ["IsNonSpeculative"])
86    header_output += ImmOpDeclare.subst(hltIop)
87    decoder_output += SemihostConstructor.subst(hltIop)
88    exec_output += PredOpExecute.subst(hltIop)
89
90    smcCode = '''
91    HCR  hcr  = Hcr;
92    CPSR cpsr = Cpsr;
93    SCR  scr  = Scr;
94
95    if ((cpsr.mode != MODE_USER) && FullSystem) {
96        if (ArmSystem::haveVirtualization(xc->tcBase()) &&
97            !inSecureState(scr, cpsr) && (cpsr.mode != MODE_HYP) && hcr.tsc) {
98            fault = std::make_shared<HypervisorTrap>(machInst, 0,
99                                                     EC_SMC_TO_HYP);
100        } else {
101            if (scr.scd) {
102                fault = disabledFault();
103            } else {
104                fault = std::make_shared<SecureMonitorCall>(machInst);
105            }
106        }
107    } else {
108        fault = disabledFault();
109    }
110    '''
111
112    smcIop = InstObjParams("smc", "Smc", "PredOp",
113                           { "code": smcCode,
114                             "predicate_test": predicateTest },
115                           ["IsNonSpeculative", "IsSerializeAfter"])
116    header_output += BasicDeclare.subst(smcIop)
117    decoder_output += BasicConstructor.subst(smcIop)
118    exec_output += PredOpExecute.subst(smcIop)
119
120    hvcCode = '''
121    CPSR cpsr = Cpsr;
122    SCR  scr  = Scr;
123
124    // Filter out the various cases where this instruction isn't defined
125    if (!FullSystem || !ArmSystem::haveVirtualization(xc->tcBase()) ||
126        (cpsr.mode == MODE_USER) ||
127        (ArmSystem::haveSecurity(xc->tcBase()) && (!scr.ns || !scr.hce))) {
128        fault = disabledFault();
129    } else {
130        fault = std::make_shared<HypervisorCall>(machInst, imm);
131    }
132    '''
133
134    hvcIop = InstObjParams("hvc", "Hvc", "ImmOp",
135                           { "code": hvcCode,
136                             "predicate_test": predicateTest },
137                           ["IsNonSpeculative", "IsSerializeAfter"])
138    header_output += ImmOpDeclare.subst(hvcIop)
139    decoder_output += ImmOpConstructor.subst(hvcIop)
140    exec_output += PredOpExecute.subst(hvcIop)
141
142    eretCode = '''
143        SCTLR sctlr   = Sctlr;
144        CPSR old_cpsr = Cpsr;
145        old_cpsr.nz   = CondCodesNZ;
146        old_cpsr.c    = CondCodesC;
147        old_cpsr.v    = CondCodesV;
148        old_cpsr.ge   = CondCodesGE;
149
150        CPSR new_cpsr = cpsrWriteByInstr(old_cpsr, Spsr, Scr, Nsacr, 0xF,
151                            true, sctlr.nmfi, xc->tcBase());
152        Cpsr        = ~CondCodesMask & new_cpsr;
153        CondCodesNZ = new_cpsr.nz;
154        CondCodesC  = new_cpsr.c;
155        CondCodesV  = new_cpsr.v;
156        CondCodesGE = new_cpsr.ge;
157
158        NextThumb = (new_cpsr).t;
159                    NextJazelle = (new_cpsr).j;
160                    NextItState = (((new_cpsr).it2 << 2) & 0xFC)
161                        | ((new_cpsr).it1 & 0x3);
162
163        NPC = (old_cpsr.mode == MODE_HYP) ? ElrHyp : LR;
164    '''
165
166    eretIop = InstObjParams("eret", "Eret", "PredOp",
167                           { "code": eretCode,
168                             "predicate_test": predicateTest },
169                           ["IsNonSpeculative", "IsSerializeAfter",
170                            "IsSquashAfter"])
171    header_output += BasicDeclare.subst(eretIop)
172    decoder_output += BasicConstructor.subst(eretIop)
173    exec_output += PredOpExecute.subst(eretIop)
174
175    crcCode = '''
176    constexpr uint8_t size_bytes = %(sz)d;
177    constexpr uint32_t poly = %(polynom)s;
178
179    uint32_t data = htole(Op2);
180    auto data_buffer = reinterpret_cast<uint8_t*>(&data);
181
182    Dest = crc32<poly>(
183        data_buffer,   /* Message Register */
184        Op1,           /* Initial Value  of the CRC */
185        size_bytes     /* Size of the original Message */
186    );
187    '''
188
189    def crc32Emit(mnem, implCode, castagnoli, size):
190        global header_output, decoder_output, exec_output
191
192        if castagnoli:
193            # crc32c instructions
194            poly = "0x1EDC6F41"
195        else:
196            # crc32 instructions
197            poly = "0x04C11DB7"
198
199        data = {'sz' : size, 'polynom': poly}
200
201        instCode = implCode % data
202
203        crcIop = InstObjParams(mnem, mnem.capitalize(), "RegRegRegOp",
204                               { "code": instCode,
205                                 "predicate_test": predicateTest }, [])
206        header_output += RegRegRegOpDeclare.subst(crcIop)
207        decoder_output += RegRegRegOpConstructor.subst(crcIop)
208        exec_output += PredOpExecute.subst(crcIop)
209
210    crc32Emit("crc32b", crcCode, False, 1);
211    crc32Emit("crc32h", crcCode, False, 2);
212    crc32Emit("crc32w", crcCode, False, 4);
213    crc32Emit("crc32cb", crcCode, True, 1);
214    crc32Emit("crc32ch", crcCode, True, 2);
215    crc32Emit("crc32cw", crcCode, True, 4);
216
217}};
218
219let {{
220
221    header_output = decoder_output = exec_output = ""
222
223    mrsCpsrCode = '''
224        CPSR cpsr = Cpsr;
225        cpsr.nz = CondCodesNZ;
226        cpsr.c = CondCodesC;
227        cpsr.v = CondCodesV;
228        cpsr.ge = CondCodesGE;
229        Dest = cpsr & (cpsr.mode == MODE_USER ? ApsrMask : CpsrMask);
230    '''
231
232    mrsCpsrIop = InstObjParams("mrs", "MrsCpsr", "MrsOp",
233                               { "code": mrsCpsrCode,
234                                 "predicate_test": condPredicateTest },
235                               ["IsSerializeBefore"])
236    header_output += MrsDeclare.subst(mrsCpsrIop)
237    decoder_output += MrsConstructor.subst(mrsCpsrIop)
238    exec_output += PredOpExecute.subst(mrsCpsrIop)
239
240    mrsSpsrCode = "Dest = Spsr"
241    mrsSpsrIop = InstObjParams("mrs", "MrsSpsr", "MrsOp",
242                               { "code": mrsSpsrCode,
243                                 "predicate_test": predicateTest },
244                               ["IsSerializeBefore"])
245    header_output += MrsDeclare.subst(mrsSpsrIop)
246    decoder_output += MrsConstructor.subst(mrsSpsrIop)
247    exec_output += PredOpExecute.subst(mrsSpsrIop)
248
249    mrsBankedRegCode = '''
250        bool isIntReg;
251        int  regIdx;
252
253        if (decodeMrsMsrBankedReg(byteMask, r, isIntReg, regIdx, Cpsr, Scr, Nsacr)) {
254            if (isIntReg) {
255                Dest = DecodedBankedIntReg;
256            } else {
257                Dest = xc->readMiscReg(regIdx);
258            }
259        } else {
260            return std::make_shared<UndefinedInstruction>(machInst, false,
261                                                          mnemonic);
262        }
263    '''
264    mrsBankedRegIop = InstObjParams("mrs", "MrsBankedReg", "MrsOp",
265                                    { "code": mrsBankedRegCode,
266                                      "predicate_test": predicateTest },
267                                    ["IsSerializeBefore"])
268    header_output += MrsBankedRegDeclare.subst(mrsBankedRegIop)
269    decoder_output += MrsBankedRegConstructor.subst(mrsBankedRegIop)
270    exec_output += PredOpExecute.subst(mrsBankedRegIop)
271
272    msrBankedRegCode = '''
273        bool isIntReg;
274        int  regIdx;
275
276        if (decodeMrsMsrBankedReg(byteMask, r, isIntReg, regIdx, Cpsr, Scr, Nsacr)) {
277            if (isIntReg) {
278                // This is a bit nasty, you would have thought that
279                // DecodedBankedIntReg wouldn't be written to unless the
280                // conditions on the IF statements above are met, however if
281                // you look at the generated C code you'll find that they are.
282                // However this is safe as DecodedBankedIntReg (which is used
283                // in operands.isa to get the index of DecodedBankedIntReg)
284                // will return INTREG_DUMMY if its not a valid integer
285                // register, so redirecting the write to somewhere we don't
286                // care about.
287                DecodedBankedIntReg = Op1;
288            } else {
289                xc->setMiscReg(regIdx, Op1);
290            }
291        } else {
292            return std::make_shared<UndefinedInstruction>(machInst, false,
293                                                          mnemonic);
294        }
295    '''
296    msrBankedRegIop = InstObjParams("msr", "MsrBankedReg", "MsrRegOp",
297                                    { "code": msrBankedRegCode,
298                                      "predicate_test": predicateTest },
299                                    ["IsSerializeAfter", "IsNonSpeculative"])
300    header_output += MsrBankedRegDeclare.subst(msrBankedRegIop)
301    decoder_output += MsrBankedRegConstructor.subst(msrBankedRegIop)
302    exec_output += PredOpExecute.subst(msrBankedRegIop)
303
304    msrCpsrRegCode = '''
305        SCTLR sctlr = Sctlr;
306        CPSR old_cpsr = Cpsr;
307        old_cpsr.nz = CondCodesNZ;
308        old_cpsr.c = CondCodesC;
309        old_cpsr.v = CondCodesV;
310        old_cpsr.ge = CondCodesGE;
311
312        CPSR new_cpsr =
313            cpsrWriteByInstr(old_cpsr, Op1, Scr, Nsacr, byteMask, false,
314                             sctlr.nmfi, xc->tcBase());
315        Cpsr = ~CondCodesMask & new_cpsr;
316        CondCodesNZ = new_cpsr.nz;
317        CondCodesC = new_cpsr.c;
318        CondCodesV = new_cpsr.v;
319        CondCodesGE = new_cpsr.ge;
320    '''
321    msrCpsrRegIop = InstObjParams("msr", "MsrCpsrReg", "MsrRegOp",
322                                  { "code": msrCpsrRegCode,
323                                    "predicate_test": condPredicateTest },
324                                  ["IsSerializeAfter","IsNonSpeculative"])
325    header_output += MsrRegDeclare.subst(msrCpsrRegIop)
326    decoder_output += MsrRegConstructor.subst(msrCpsrRegIop)
327    exec_output += PredOpExecute.subst(msrCpsrRegIop)
328
329    msrSpsrRegCode = "Spsr = spsrWriteByInstr(Spsr, Op1, byteMask, false);"
330    msrSpsrRegIop = InstObjParams("msr", "MsrSpsrReg", "MsrRegOp",
331                                  { "code": msrSpsrRegCode,
332                                    "predicate_test": predicateTest },
333                                  ["IsSerializeAfter","IsNonSpeculative"])
334    header_output += MsrRegDeclare.subst(msrSpsrRegIop)
335    decoder_output += MsrRegConstructor.subst(msrSpsrRegIop)
336    exec_output += PredOpExecute.subst(msrSpsrRegIop)
337
338    msrCpsrImmCode = '''
339        SCTLR sctlr = Sctlr;
340        CPSR old_cpsr = Cpsr;
341        old_cpsr.nz = CondCodesNZ;
342        old_cpsr.c = CondCodesC;
343        old_cpsr.v = CondCodesV;
344        old_cpsr.ge = CondCodesGE;
345        CPSR new_cpsr =
346            cpsrWriteByInstr(old_cpsr, imm, Scr, Nsacr, byteMask, false,
347                             sctlr.nmfi, xc->tcBase());
348        Cpsr = ~CondCodesMask & new_cpsr;
349        CondCodesNZ = new_cpsr.nz;
350        CondCodesC = new_cpsr.c;
351        CondCodesV = new_cpsr.v;
352        CondCodesGE = new_cpsr.ge;
353    '''
354    msrCpsrImmIop = InstObjParams("msr", "MsrCpsrImm", "MsrImmOp",
355                                  { "code": msrCpsrImmCode,
356                                    "predicate_test": condPredicateTest },
357                                  ["IsSerializeAfter","IsNonSpeculative"])
358    header_output += MsrImmDeclare.subst(msrCpsrImmIop)
359    decoder_output += MsrImmConstructor.subst(msrCpsrImmIop)
360    exec_output += PredOpExecute.subst(msrCpsrImmIop)
361
362    msrSpsrImmCode = "Spsr = spsrWriteByInstr(Spsr, imm, byteMask, false);"
363    msrSpsrImmIop = InstObjParams("msr", "MsrSpsrImm", "MsrImmOp",
364                                  { "code": msrSpsrImmCode,
365                                    "predicate_test": predicateTest },
366                                  ["IsSerializeAfter","IsNonSpeculative"])
367    header_output += MsrImmDeclare.subst(msrSpsrImmIop)
368    decoder_output += MsrImmConstructor.subst(msrSpsrImmIop)
369    exec_output += PredOpExecute.subst(msrSpsrImmIop)
370
371    revCode = '''
372    uint32_t val = Op1;
373    Dest = swap_byte(val);
374    '''
375    revIop = InstObjParams("rev", "Rev", "RegRegOp",
376                           { "code": revCode,
377                             "predicate_test": predicateTest }, [])
378    header_output += RegRegOpDeclare.subst(revIop)
379    decoder_output += RegRegOpConstructor.subst(revIop)
380    exec_output += PredOpExecute.subst(revIop)
381
382    rev16Code = '''
383    uint32_t val = Op1;
384    Dest = (bits(val, 15, 8) << 0) |
385           (bits(val, 7, 0) << 8) |
386           (bits(val, 31, 24) << 16) |
387           (bits(val, 23, 16) << 24);
388    '''
389    rev16Iop = InstObjParams("rev16", "Rev16", "RegRegOp",
390                             { "code": rev16Code,
391                               "predicate_test": predicateTest }, [])
392    header_output += RegRegOpDeclare.subst(rev16Iop)
393    decoder_output += RegRegOpConstructor.subst(rev16Iop)
394    exec_output += PredOpExecute.subst(rev16Iop)
395
396    revshCode = '''
397    uint16_t val = Op1;
398    Dest = sext<16>(swap_byte(val));
399    '''
400    revshIop = InstObjParams("revsh", "Revsh", "RegRegOp",
401                             { "code": revshCode,
402                               "predicate_test": predicateTest }, [])
403    header_output += RegRegOpDeclare.subst(revshIop)
404    decoder_output += RegRegOpConstructor.subst(revshIop)
405    exec_output += PredOpExecute.subst(revshIop)
406
407    rbitCode = '''
408    Dest = reverseBits(Op1);
409    '''
410    rbitIop = InstObjParams("rbit", "Rbit", "RegRegOp",
411                            { "code": rbitCode,
412                              "predicate_test": predicateTest }, [])
413    header_output += RegRegOpDeclare.subst(rbitIop)
414    decoder_output += RegRegOpConstructor.subst(rbitIop)
415    exec_output += PredOpExecute.subst(rbitIop)
416
417    clzCode = '''
418        Dest = (Op1 == 0) ? 32 : (31 - findMsbSet(Op1));
419    '''
420    clzIop = InstObjParams("clz", "Clz", "RegRegOp",
421                           { "code": clzCode,
422                             "predicate_test": predicateTest }, [])
423    header_output += RegRegOpDeclare.subst(clzIop)
424    decoder_output += RegRegOpConstructor.subst(clzIop)
425    exec_output += PredOpExecute.subst(clzIop)
426
427    ssatCode = '''
428        int32_t operand = shift_rm_imm(Op1, shiftAmt, shiftType, 0);
429        int32_t res;
430        if (satInt(res, operand, imm))
431            CpsrQ = 1 << 27;
432        Dest = res;
433    '''
434    ssatIop = InstObjParams("ssat", "Ssat", "RegImmRegShiftOp",
435                            { "code": ssatCode,
436                              "predicate_test": pickPredicate(ssatCode) }, [])
437    header_output += RegImmRegShiftOpDeclare.subst(ssatIop)
438    decoder_output += RegImmRegShiftOpConstructor.subst(ssatIop)
439    exec_output += PredOpExecute.subst(ssatIop)
440
441    usatCode = '''
442        int32_t operand = shift_rm_imm(Op1, shiftAmt, shiftType, 0);
443        int32_t res;
444        if (uSatInt(res, operand, imm))
445            CpsrQ = 1 << 27;
446        Dest = res;
447    '''
448    usatIop = InstObjParams("usat", "Usat", "RegImmRegShiftOp",
449                            { "code": usatCode,
450                              "predicate_test": pickPredicate(usatCode) }, [])
451    header_output += RegImmRegShiftOpDeclare.subst(usatIop)
452    decoder_output += RegImmRegShiftOpConstructor.subst(usatIop)
453    exec_output += PredOpExecute.subst(usatIop)
454
455    ssat16Code = '''
456        int32_t res;
457        uint32_t resTemp = 0;
458        int32_t argLow = sext<16>(bits(Op1, 15, 0));
459        int32_t argHigh = sext<16>(bits(Op1, 31, 16));
460        if (satInt(res, argLow, imm))
461            CpsrQ = 1 << 27;
462        replaceBits(resTemp, 15, 0, res);
463        if (satInt(res, argHigh, imm))
464            CpsrQ = 1 << 27;
465        replaceBits(resTemp, 31, 16, res);
466        Dest = resTemp;
467    '''
468    ssat16Iop = InstObjParams("ssat16", "Ssat16", "RegImmRegOp",
469                              { "code": ssat16Code,
470                                "predicate_test": pickPredicate(ssat16Code) }, [])
471    header_output += RegImmRegOpDeclare.subst(ssat16Iop)
472    decoder_output += RegImmRegOpConstructor.subst(ssat16Iop)
473    exec_output += PredOpExecute.subst(ssat16Iop)
474
475    usat16Code = '''
476        int32_t res;
477        uint32_t resTemp = 0;
478        int32_t argLow = sext<16>(bits(Op1, 15, 0));
479        int32_t argHigh = sext<16>(bits(Op1, 31, 16));
480        if (uSatInt(res, argLow, imm))
481            CpsrQ = 1 << 27;
482        replaceBits(resTemp, 15, 0, res);
483        if (uSatInt(res, argHigh, imm))
484            CpsrQ = 1 << 27;
485        replaceBits(resTemp, 31, 16, res);
486        Dest = resTemp;
487    '''
488    usat16Iop = InstObjParams("usat16", "Usat16", "RegImmRegOp",
489                              { "code": usat16Code,
490                                "predicate_test": pickPredicate(usat16Code) }, [])
491    header_output += RegImmRegOpDeclare.subst(usat16Iop)
492    decoder_output += RegImmRegOpConstructor.subst(usat16Iop)
493    exec_output += PredOpExecute.subst(usat16Iop)
494
495    sxtbIop = InstObjParams("sxtb", "Sxtb", "RegImmRegOp",
496                            { "code":
497                              "Dest = sext<8>((uint8_t)(Op1_ud >> imm));",
498                              "predicate_test": predicateTest }, [])
499    header_output += RegImmRegOpDeclare.subst(sxtbIop)
500    decoder_output += RegImmRegOpConstructor.subst(sxtbIop)
501    exec_output += PredOpExecute.subst(sxtbIop)
502
503    sxtabIop = InstObjParams("sxtab", "Sxtab", "RegRegRegImmOp",
504                             { "code":
505                               '''
506                                   Dest = sext<8>((uint8_t)(Op2_ud >> imm)) +
507                                          Op1;
508                               ''',
509                               "predicate_test": predicateTest }, [])
510    header_output += RegRegRegImmOpDeclare.subst(sxtabIop)
511    decoder_output += RegRegRegImmOpConstructor.subst(sxtabIop)
512    exec_output += PredOpExecute.subst(sxtabIop)
513
514    sxtb16Code = '''
515    uint32_t resTemp = 0;
516    replaceBits(resTemp, 15, 0, sext<8>(bits(Op1, imm + 7, imm)));
517    replaceBits(resTemp, 31, 16,
518                sext<8>(bits(Op1, (imm + 23) % 32, (imm + 16) % 32)));
519    Dest = resTemp;
520    '''
521    sxtb16Iop = InstObjParams("sxtb16", "Sxtb16", "RegImmRegOp",
522                              { "code": sxtb16Code,
523                                "predicate_test": predicateTest }, [])
524    header_output += RegImmRegOpDeclare.subst(sxtb16Iop)
525    decoder_output += RegImmRegOpConstructor.subst(sxtb16Iop)
526    exec_output += PredOpExecute.subst(sxtb16Iop)
527
528    sxtab16Code = '''
529    uint32_t resTemp = 0;
530    replaceBits(resTemp, 15, 0, sext<8>(bits(Op2, imm + 7, imm)) +
531                                        bits(Op1, 15, 0));
532    replaceBits(resTemp, 31, 16,
533                sext<8>(bits(Op2, (imm + 23) % 32, (imm + 16) % 32)) +
534                bits(Op1, 31, 16));
535    Dest = resTemp;
536    '''
537    sxtab16Iop = InstObjParams("sxtab16", "Sxtab16", "RegRegRegImmOp",
538                               { "code": sxtab16Code,
539                                 "predicate_test": predicateTest }, [])
540    header_output += RegRegRegImmOpDeclare.subst(sxtab16Iop)
541    decoder_output += RegRegRegImmOpConstructor.subst(sxtab16Iop)
542    exec_output += PredOpExecute.subst(sxtab16Iop)
543
544    sxthCode = '''
545    uint64_t rotated = (uint32_t)Op1;
546    rotated = (rotated | (rotated << 32)) >> imm;
547    Dest = sext<16>((uint16_t)rotated);
548    '''
549    sxthIop = InstObjParams("sxth", "Sxth", "RegImmRegOp",
550                              { "code": sxthCode,
551                                "predicate_test": predicateTest }, [])
552    header_output += RegImmRegOpDeclare.subst(sxthIop)
553    decoder_output += RegImmRegOpConstructor.subst(sxthIop)
554    exec_output += PredOpExecute.subst(sxthIop)
555
556    sxtahCode = '''
557    uint64_t rotated = (uint32_t)Op2;
558    rotated = (rotated | (rotated << 32)) >> imm;
559    Dest = sext<16>((uint16_t)rotated) + Op1;
560    '''
561    sxtahIop = InstObjParams("sxtah", "Sxtah", "RegRegRegImmOp",
562                             { "code": sxtahCode,
563                               "predicate_test": predicateTest }, [])
564    header_output += RegRegRegImmOpDeclare.subst(sxtahIop)
565    decoder_output += RegRegRegImmOpConstructor.subst(sxtahIop)
566    exec_output += PredOpExecute.subst(sxtahIop)
567
568    uxtbIop = InstObjParams("uxtb", "Uxtb", "RegImmRegOp",
569                            { "code": "Dest = (uint8_t)(Op1_ud >> imm);",
570                              "predicate_test": predicateTest }, [])
571    header_output += RegImmRegOpDeclare.subst(uxtbIop)
572    decoder_output += RegImmRegOpConstructor.subst(uxtbIop)
573    exec_output += PredOpExecute.subst(uxtbIop)
574
575    uxtabIop = InstObjParams("uxtab", "Uxtab", "RegRegRegImmOp",
576                             { "code":
577                               "Dest = (uint8_t)(Op2_ud >> imm) + Op1;",
578                               "predicate_test": predicateTest }, [])
579    header_output += RegRegRegImmOpDeclare.subst(uxtabIop)
580    decoder_output += RegRegRegImmOpConstructor.subst(uxtabIop)
581    exec_output += PredOpExecute.subst(uxtabIop)
582
583    uxtb16Code = '''
584    uint32_t resTemp = 0;
585    replaceBits(resTemp, 15, 0, (uint8_t)(bits(Op1, imm + 7, imm)));
586    replaceBits(resTemp, 31, 16,
587                (uint8_t)(bits(Op1, (imm + 23) % 32, (imm + 16) % 32)));
588    Dest = resTemp;
589    '''
590    uxtb16Iop = InstObjParams("uxtb16", "Uxtb16", "RegImmRegOp",
591                              { "code": uxtb16Code,
592                                "predicate_test": predicateTest }, [])
593    header_output += RegImmRegOpDeclare.subst(uxtb16Iop)
594    decoder_output += RegImmRegOpConstructor.subst(uxtb16Iop)
595    exec_output += PredOpExecute.subst(uxtb16Iop)
596
597    uxtab16Code = '''
598    uint32_t resTemp = 0;
599    replaceBits(resTemp, 15, 0, (uint8_t)(bits(Op2, imm + 7, imm)) +
600                                        bits(Op1, 15, 0));
601    replaceBits(resTemp, 31, 16,
602                (uint8_t)(bits(Op2, (imm + 23) % 32, (imm + 16) % 32)) +
603                bits(Op1, 31, 16));
604    Dest = resTemp;
605    '''
606    uxtab16Iop = InstObjParams("uxtab16", "Uxtab16", "RegRegRegImmOp",
607                               { "code": uxtab16Code,
608                                 "predicate_test": predicateTest }, [])
609    header_output += RegRegRegImmOpDeclare.subst(uxtab16Iop)
610    decoder_output += RegRegRegImmOpConstructor.subst(uxtab16Iop)
611    exec_output += PredOpExecute.subst(uxtab16Iop)
612
613    uxthCode = '''
614    uint64_t rotated = (uint32_t)Op1;
615    rotated = (rotated | (rotated << 32)) >> imm;
616    Dest = (uint16_t)rotated;
617    '''
618    uxthIop = InstObjParams("uxth", "Uxth", "RegImmRegOp",
619                              { "code": uxthCode,
620                                "predicate_test": predicateTest }, [])
621    header_output += RegImmRegOpDeclare.subst(uxthIop)
622    decoder_output += RegImmRegOpConstructor.subst(uxthIop)
623    exec_output += PredOpExecute.subst(uxthIop)
624
625    uxtahCode = '''
626    uint64_t rotated = (uint32_t)Op2;
627    rotated = (rotated | (rotated << 32)) >> imm;
628    Dest = (uint16_t)rotated + Op1;
629    '''
630    uxtahIop = InstObjParams("uxtah", "Uxtah", "RegRegRegImmOp",
631                             { "code": uxtahCode,
632                               "predicate_test": predicateTest }, [])
633    header_output += RegRegRegImmOpDeclare.subst(uxtahIop)
634    decoder_output += RegRegRegImmOpConstructor.subst(uxtahIop)
635    exec_output += PredOpExecute.subst(uxtahIop)
636
637    selCode = '''
638        uint32_t resTemp = 0;
639        for (unsigned i = 0; i < 4; i++) {
640            int low = i * 8;
641            int high = low + 7;
642            replaceBits(resTemp, high, low,
643                        bits(CondCodesGE, i) ?
644                            bits(Op1, high, low) : bits(Op2, high, low));
645        }
646        Dest = resTemp;
647    '''
648    selIop = InstObjParams("sel", "Sel", "RegRegRegOp",
649                           { "code": selCode,
650                             "predicate_test": predicateTest }, [])
651    header_output += RegRegRegOpDeclare.subst(selIop)
652    decoder_output += RegRegRegOpConstructor.subst(selIop)
653    exec_output += PredOpExecute.subst(selIop)
654
655    usad8Code = '''
656        uint32_t resTemp = 0;
657        for (unsigned i = 0; i < 4; i++) {
658            int low = i * 8;
659            int high = low + 7;
660            int32_t diff = bits(Op1, high, low) -
661                           bits(Op2, high, low);
662            resTemp += ((diff < 0) ? -diff : diff);
663        }
664        Dest = resTemp;
665    '''
666    usad8Iop = InstObjParams("usad8", "Usad8", "RegRegRegOp",
667                             { "code": usad8Code,
668                               "predicate_test": predicateTest }, [])
669    header_output += RegRegRegOpDeclare.subst(usad8Iop)
670    decoder_output += RegRegRegOpConstructor.subst(usad8Iop)
671    exec_output += PredOpExecute.subst(usad8Iop)
672
673    usada8Code = '''
674        uint32_t resTemp = 0;
675        for (unsigned i = 0; i < 4; i++) {
676            int low = i * 8;
677            int high = low + 7;
678            int32_t diff = bits(Op1, high, low) -
679                           bits(Op2, high, low);
680            resTemp += ((diff < 0) ? -diff : diff);
681        }
682        Dest = Op3 + resTemp;
683    '''
684    usada8Iop = InstObjParams("usada8", "Usada8", "RegRegRegRegOp",
685                              { "code": usada8Code,
686                                "predicate_test": predicateTest }, [])
687    header_output += RegRegRegRegOpDeclare.subst(usada8Iop)
688    decoder_output += RegRegRegRegOpConstructor.subst(usada8Iop)
689    exec_output += PredOpExecute.subst(usada8Iop)
690
691    bkptCode = '''
692        uint16_t imm16;
693        if (!machInst.thumb)
694            imm16 = ((bits(machInst, 19, 8) << 4) | bits(machInst, 3, 0));
695        else
696            imm16 = bits(machInst, 7, 0);
697
698        return softwareBreakpoint32(xc, imm16);
699    '''
700    bkptIop = InstObjParams("bkpt", "BkptInst", "PredOp", bkptCode)
701    header_output += BasicDeclare.subst(bkptIop)
702    decoder_output += BasicConstructor.subst(bkptIop)
703    exec_output += BasicExecute.subst(bkptIop)
704
705    nopIop = InstObjParams("nop", "NopInst", "ArmStaticInst", "", ['IsNop'])
706    header_output += BasicDeclare.subst(nopIop)
707    decoder_output += BasicConstructor64.subst(nopIop)
708    exec_output += BasicExecute.subst(nopIop)
709
710    yieldIop = InstObjParams("yield", "YieldInst", "PredOp", \
711            { "code" : "", "predicate_test" : predicateTest })
712    header_output += BasicDeclare.subst(yieldIop)
713    decoder_output += BasicConstructor.subst(yieldIop)
714    exec_output += PredOpExecute.subst(yieldIop)
715
716    wfeCode = '''
717    CPSR cpsr = Cpsr;
718    SCR  scr  = Scr64;
719
720    // WFE Sleeps if SevMailbox==0 and no unmasked interrupts are pending,
721    ThreadContext *tc = xc->tcBase();
722    if (SevMailbox == 1) {
723        SevMailbox = 0;
724        PseudoInst::quiesceSkip(tc);
725    } else if (tc->getCpuPtr()->getInterruptController(
726                tc->threadId())->checkInterrupts(tc)) {
727        PseudoInst::quiesceSkip(tc);
728    } else {
729        fault = trapWFx(tc, cpsr, scr, true);
730        if (fault == NoFault) {
731            PseudoInst::quiesce(tc);
732        } else {
733            PseudoInst::quiesceSkip(tc);
734        }
735    }
736    '''
737    wfePredFixUpCode = '''
738    // WFE is predicated false, reset SevMailbox to reduce spurious sleeps
739    // and SEV interrupts
740    SevMailbox = 1;
741    '''
742    wfeIop = InstObjParams("wfe", "WfeInst", "PredOp", \
743            { "code" : wfeCode,
744              "pred_fixup" : wfePredFixUpCode,
745              "predicate_test" : predicateTest },
746            ["IsNonSpeculative", "IsQuiesce",
747             "IsSerializeAfter", "IsUnverifiable"])
748    header_output += BasicDeclare.subst(wfeIop)
749    decoder_output += BasicConstructor.subst(wfeIop)
750    exec_output += QuiescePredOpExecuteWithFixup.subst(wfeIop)
751
752    wfiCode = '''
753    HCR  hcr  = Hcr;
754    CPSR cpsr = Cpsr;
755    SCR  scr  = Scr64;
756
757    // WFI doesn't sleep if interrupts are pending (masked or not)
758    ThreadContext *tc = xc->tcBase();
759    if (tc->getCpuPtr()->getInterruptController(
760                tc->threadId())->checkWfiWake(hcr, cpsr, scr)) {
761        PseudoInst::quiesceSkip(tc);
762    } else {
763        fault = trapWFx(tc, cpsr, scr, false);
764        if (fault == NoFault) {
765            PseudoInst::quiesce(tc);
766        } else {
767            PseudoInst::quiesceSkip(tc);
768        }
769    }
770    tc->getCpuPtr()->clearInterrupt(tc->threadId(), INT_ABT, 0);
771    '''
772    wfiIop = InstObjParams("wfi", "WfiInst", "PredOp", \
773            { "code" : wfiCode, "predicate_test" : predicateTest },
774            ["IsNonSpeculative", "IsQuiesce",
775             "IsSerializeAfter", "IsUnverifiable"])
776    header_output += BasicDeclare.subst(wfiIop)
777    decoder_output += BasicConstructor.subst(wfiIop)
778    exec_output += QuiescePredOpExecute.subst(wfiIop)
779
780    sevCode = '''
781    SevMailbox = 1;
782    System *sys = xc->tcBase()->getSystemPtr();
783    for (int x = 0; x < sys->numContexts(); x++) {
784        ThreadContext *oc = sys->getThreadContext(x);
785        if (oc == xc->tcBase())
786            continue;
787        // Wake CPU with interrupt if they were sleeping
788        if (oc->readMiscReg(MISCREG_SEV_MAILBOX) == 0) {
789            // Post Interrupt and wake cpu if needed
790            oc->getCpuPtr()->postInterrupt(oc->threadId(), INT_SEV, 0);
791        }
792    }
793    '''
794    sevIop = InstObjParams("sev", "SevInst", "PredOp", \
795            { "code" : sevCode, "predicate_test" : predicateTest },
796            ["IsNonSpeculative", "IsSquashAfter", "IsUnverifiable"])
797    header_output += BasicDeclare.subst(sevIop)
798    decoder_output += BasicConstructor.subst(sevIop)
799    exec_output += PredOpExecute.subst(sevIop)
800
801    sevlCode = '''
802    SevMailbox = 1;
803    '''
804    sevlIop = InstObjParams("sevl", "SevlInst", "PredOp", \
805            { "code" : sevlCode, "predicate_test" : predicateTest },
806            ["IsNonSpeculative", "IsSquashAfter", "IsUnverifiable"])
807    header_output += BasicDeclare.subst(sevlIop)
808    decoder_output += BasicConstructor.subst(sevlIop)
809    exec_output += BasicExecute.subst(sevlIop)
810
811    itIop = InstObjParams("it", "ItInst", "PredOp", \
812            { "code" : ";",
813              "predicate_test" : predicateTest }, [])
814    header_output += BasicDeclare.subst(itIop)
815    decoder_output += BasicConstructor.subst(itIop)
816    exec_output += PredOpExecute.subst(itIop)
817    unknownCode = '''
818        return std::make_shared<UndefinedInstruction>(machInst, true);
819    '''
820    unknownIop = InstObjParams("unknown", "Unknown", "UnknownOp", \
821                               { "code": unknownCode,
822                                 "predicate_test": predicateTest })
823    header_output += BasicDeclare.subst(unknownIop)
824    decoder_output += BasicConstructor.subst(unknownIop)
825    exec_output += PredOpExecute.subst(unknownIop)
826
827    ubfxCode = '''
828        Dest = bits(Op1, imm2, imm1);
829    '''
830    ubfxIop = InstObjParams("ubfx", "Ubfx", "RegRegImmImmOp",
831                            { "code": ubfxCode,
832                              "predicate_test": predicateTest }, [])
833    header_output += RegRegImmImmOpDeclare.subst(ubfxIop)
834    decoder_output += RegRegImmImmOpConstructor.subst(ubfxIop)
835    exec_output += PredOpExecute.subst(ubfxIop)
836
837    sbfxCode = '''
838        int32_t resTemp = bits(Op1, imm2, imm1);
839        Dest = resTemp | -(resTemp & (1 << (imm2 - imm1)));
840    '''
841    sbfxIop = InstObjParams("sbfx", "Sbfx", "RegRegImmImmOp",
842                            { "code": sbfxCode,
843                              "predicate_test": predicateTest }, [])
844    header_output += RegRegImmImmOpDeclare.subst(sbfxIop)
845    decoder_output += RegRegImmImmOpConstructor.subst(sbfxIop)
846    exec_output += PredOpExecute.subst(sbfxIop)
847
848    bfcCode = '''
849        Dest = Op1 & ~(mask(imm2 - imm1 + 1) << imm1);
850    '''
851    bfcIop = InstObjParams("bfc", "Bfc", "RegRegImmImmOp",
852                           { "code": bfcCode,
853                             "predicate_test": predicateTest }, [])
854    header_output += RegRegImmImmOpDeclare.subst(bfcIop)
855    decoder_output += RegRegImmImmOpConstructor.subst(bfcIop)
856    exec_output += PredOpExecute.subst(bfcIop)
857
858    bfiCode = '''
859        uint32_t bitMask = (mask(imm2 - imm1 + 1) << imm1);
860        Dest = ((Op1 << imm1) & bitMask) | (Dest & ~bitMask);
861    '''
862    bfiIop = InstObjParams("bfi", "Bfi", "RegRegImmImmOp",
863                           { "code": bfiCode,
864                             "predicate_test": predicateTest }, [])
865    header_output += RegRegImmImmOpDeclare.subst(bfiIop)
866    decoder_output += RegRegImmImmOpConstructor.subst(bfiIop)
867    exec_output += PredOpExecute.subst(bfiIop)
868
869    mrc14code = '''
870    MiscRegIndex miscReg = (MiscRegIndex) xc->tcBase()->flattenRegId(
871                               RegId(MiscRegClass, op1)).index();
872    bool can_read, undefined;
873    std::tie(can_read, undefined) = canReadCoprocReg(miscReg, Scr, Cpsr);
874    if (!can_read || undefined) {
875        return std::make_shared<UndefinedInstruction>(machInst, false,
876                                                      mnemonic);
877    }
878    if (mcrMrc14TrapToHyp((MiscRegIndex) op1, Hcr, Cpsr, Scr, Hdcr,
879                          Hstr, Hcptr, imm)) {
880        return std::make_shared<HypervisorTrap>(machInst, imm,
881                                                EC_TRAPPED_CP14_MCR_MRC);
882    }
883    Dest = MiscOp1;
884    '''
885
886    mrc14Iop = InstObjParams("mrc", "Mrc14", "RegMiscRegImmOp",
887                             { "code": mrc14code,
888                               "predicate_test": predicateTest }, [])
889    header_output += RegMiscRegImmOpDeclare.subst(mrc14Iop)
890    decoder_output += RegMiscRegImmOpConstructor.subst(mrc14Iop)
891    exec_output += PredOpExecute.subst(mrc14Iop)
892
893
894    mcr14code = '''
895    MiscRegIndex miscReg = (MiscRegIndex) xc->tcBase()->flattenRegId(
896                               RegId(MiscRegClass, dest)).index();
897    bool can_write, undefined;
898    std::tie(can_write, undefined) = canWriteCoprocReg(miscReg, Scr, Cpsr);
899    if (undefined || !can_write) {
900        return std::make_shared<UndefinedInstruction>(machInst, false,
901                                                      mnemonic);
902    }
903    if (mcrMrc14TrapToHyp(miscReg, Hcr, Cpsr, Scr, Hdcr,
904                          Hstr, Hcptr, imm)) {
905        return std::make_shared<HypervisorTrap>(machInst, imm,
906                                                EC_TRAPPED_CP14_MCR_MRC);
907    }
908    MiscDest = Op1;
909    '''
910    mcr14Iop = InstObjParams("mcr", "Mcr14", "MiscRegRegImmOp",
911                             { "code": mcr14code,
912                               "predicate_test": predicateTest },
913                               ["IsSerializeAfter","IsNonSpeculative"])
914    header_output += MiscRegRegImmOpDeclare.subst(mcr14Iop)
915    decoder_output += MiscRegRegImmOpConstructor.subst(mcr14Iop)
916    exec_output += PredOpExecute.subst(mcr14Iop)
917
918    mrc15code = '''
919    int preFlatOp1 = snsBankedIndex(op1, xc->tcBase());
920    MiscRegIndex miscReg = (MiscRegIndex)
921                           xc->tcBase()->flattenRegId(RegId(MiscRegClass,
922                                                      preFlatOp1)).index();
923
924    bool hypTrap = mcrMrc15TrapToHyp(miscReg, xc->tcBase(), imm);
925
926    bool can_read, undefined;
927    std::tie(can_read, undefined) = canReadCoprocReg(miscReg, Scr, Cpsr);
928    // if we're in non secure PL1 mode then we can trap regargless of whether
929    // the register is accessable, in other modes we trap if only if the register
930    // IS accessable.
931    if (undefined || (!can_read && !(hypTrap && !inUserMode(Cpsr) &&
932                                    !inSecureState(Scr, Cpsr)))) {
933        return std::make_shared<UndefinedInstruction>(machInst, false,
934                                                      mnemonic);
935    }
936    if (hypTrap) {
937        return std::make_shared<HypervisorTrap>(machInst, imm,
938                                                EC_TRAPPED_CP15_MCR_MRC);
939    }
940    Dest = MiscNsBankedOp1;
941    '''
942
943    mrc15Iop = InstObjParams("mrc", "Mrc15", "RegMiscRegImmOp",
944                             { "code": mrc15code,
945                               "predicate_test": predicateTest }, [])
946    header_output += RegMiscRegImmOpDeclare.subst(mrc15Iop)
947    decoder_output += RegMiscRegImmOpConstructor.subst(mrc15Iop)
948    exec_output += PredOpExecute.subst(mrc15Iop)
949
950
951    mcr15code = '''
952    int preFlatDest = snsBankedIndex(dest, xc->tcBase());
953    MiscRegIndex miscReg = (MiscRegIndex)
954                       xc->tcBase()->flattenRegId(RegId(MiscRegClass,
955                                                  preFlatDest)).index();
956
957    bool hypTrap = mcrMrc15TrapToHyp(miscReg, xc->tcBase(), imm);
958
959    bool can_write, undefined;
960    std::tie(can_write, undefined) = canWriteCoprocReg(miscReg, Scr, Cpsr);
961
962    // if we're in non secure PL1 mode then we can trap regargless of whether
963    // the register is accessable, in other modes we trap if only if the register
964    // IS accessable.
965    if (undefined || (!can_write && !(hypTrap && !inUserMode(Cpsr) &&
966                                      !inSecureState(Scr, Cpsr)))) {
967        return std::make_shared<UndefinedInstruction>(machInst, false,
968                                                      mnemonic);
969    }
970    if (hypTrap) {
971        return std::make_shared<HypervisorTrap>(machInst, imm,
972                                                EC_TRAPPED_CP15_MCR_MRC);
973    }
974    MiscNsBankedDest = Op1;
975    '''
976    mcr15Iop = InstObjParams("mcr", "Mcr15", "MiscRegRegImmOp",
977                             { "code": mcr15code,
978                               "predicate_test": predicateTest },
979                               ["IsSerializeAfter","IsNonSpeculative"])
980    header_output += MiscRegRegImmOpDeclare.subst(mcr15Iop)
981    decoder_output += MiscRegRegImmOpConstructor.subst(mcr15Iop)
982    exec_output += PredOpExecute.subst(mcr15Iop)
983
984
985    mrrc15code = '''
986    int preFlatOp1 = snsBankedIndex(op1, xc->tcBase());
987    MiscRegIndex miscReg = (MiscRegIndex)
988                           xc->tcBase()->flattenRegId(RegId(MiscRegClass,
989                                                      preFlatOp1)).index();
990    bool hypTrap = mcrrMrrc15TrapToHyp(miscReg, Cpsr, Scr, Hstr, Hcr, imm);
991    bool can_read, undefined;
992    std::tie(can_read, undefined) = canReadCoprocReg(miscReg, Scr, Cpsr);
993    // if we're in non secure PL1 mode then we can trap regargless of whether
994    // the register is accessable, in other modes we trap if only if the register
995    // IS accessable.
996    if (undefined || (!can_read && !(hypTrap && !inUserMode(Cpsr) &&
997                                     !inSecureState(Scr, Cpsr)))) {
998        return std::make_shared<UndefinedInstruction>(machInst, false,
999                                                      mnemonic);
1000    }
1001    if (hypTrap) {
1002        return std::make_shared<HypervisorTrap>(machInst, imm,
1003                                                EC_TRAPPED_CP15_MCRR_MRRC);
1004    }
1005    Dest = bits(MiscNsBankedOp164, 63, 32);
1006    Dest2 = bits(MiscNsBankedOp164, 31, 0);
1007    '''
1008    mrrc15Iop = InstObjParams("mrrc", "Mrrc15", "MrrcOp",
1009                              { "code": mrrc15code,
1010                                "predicate_test": predicateTest }, [])
1011    header_output += MrrcOpDeclare.subst(mrrc15Iop)
1012    decoder_output += MrrcOpConstructor.subst(mrrc15Iop)
1013    exec_output += PredOpExecute.subst(mrrc15Iop)
1014
1015
1016    mcrr15code = '''
1017    int preFlatDest = snsBankedIndex(dest, xc->tcBase());
1018    MiscRegIndex miscReg = (MiscRegIndex)
1019                           xc->tcBase()->flattenRegId(RegId(MiscRegClass,
1020                                                      preFlatDest)).index();
1021    bool hypTrap  = mcrrMrrc15TrapToHyp(miscReg, Cpsr, Scr, Hstr, Hcr, imm);
1022    bool can_write, undefined;
1023    std::tie(can_write, undefined) = canWriteCoprocReg(miscReg, Scr, Cpsr);
1024
1025    // if we're in non secure PL1 mode then we can trap regargless of whether
1026    // the register is accessable, in other modes we trap if only if the register
1027    // IS accessable.
1028    if (undefined || (!can_write && !(hypTrap && !inUserMode(Cpsr) &&
1029                                     !inSecureState(Scr, Cpsr)))) {
1030        return std::make_shared<UndefinedInstruction>(machInst, false,
1031                                                      mnemonic);
1032    }
1033    if (hypTrap) {
1034        return std::make_shared<HypervisorTrap>(machInst, imm,
1035                                                EC_TRAPPED_CP15_MCRR_MRRC);
1036    }
1037    MiscNsBankedDest64 = ((uint64_t) Op1 << 32) | Op2;
1038    '''
1039    mcrr15Iop = InstObjParams("mcrr", "Mcrr15", "McrrOp",
1040                              { "code": mcrr15code,
1041                                "predicate_test": predicateTest }, [])
1042    header_output += McrrOpDeclare.subst(mcrr15Iop)
1043    decoder_output += McrrOpConstructor.subst(mcrr15Iop)
1044    exec_output += PredOpExecute.subst(mcrr15Iop)
1045
1046
1047    enterxCode = '''
1048        NextThumb = true;
1049        NextJazelle = true;
1050    '''
1051    enterxIop = InstObjParams("enterx", "Enterx", "PredOp",
1052                              { "code": enterxCode,
1053                                "predicate_test": predicateTest }, [])
1054    header_output += BasicDeclare.subst(enterxIop)
1055    decoder_output += BasicConstructor.subst(enterxIop)
1056    exec_output += PredOpExecute.subst(enterxIop)
1057
1058    leavexCode = '''
1059        NextThumb = true;
1060        NextJazelle = false;
1061    '''
1062    leavexIop = InstObjParams("leavex", "Leavex", "PredOp",
1063                              { "code": leavexCode,
1064                                "predicate_test": predicateTest }, [])
1065    header_output += BasicDeclare.subst(leavexIop)
1066    decoder_output += BasicConstructor.subst(leavexIop)
1067    exec_output += PredOpExecute.subst(leavexIop)
1068
1069    setendCode = '''
1070        CPSR cpsr = Cpsr;
1071        cpsr.e = imm;
1072        Cpsr = cpsr;
1073        fault = checkSETENDEnabled(xc->tcBase(), cpsr);
1074    '''
1075    setendIop = InstObjParams("setend", "Setend", "ImmOp",
1076                              { "code": setendCode,
1077                                "predicate_test": predicateTest },
1078                              ["IsSerializeAfter","IsNonSpeculative"])
1079    header_output += ImmOpDeclare.subst(setendIop)
1080    decoder_output += ImmOpConstructor.subst(setendIop)
1081    exec_output += PredOpExecute.subst(setendIop)
1082
1083    clrexCode = '''
1084        LLSCLock = 0;
1085    '''
1086    clrexIop = InstObjParams("clrex", "Clrex","PredOp",
1087                             { "code": clrexCode,
1088                               "predicate_test": predicateTest },[])
1089    header_output += BasicDeclare.subst(clrexIop)
1090    decoder_output += BasicConstructor.subst(clrexIop)
1091    exec_output += PredOpExecute.subst(clrexIop)
1092
1093    McrDcCheckCode = '''
1094        int preFlatDest = snsBankedIndex(dest, xc->tcBase());
1095        MiscRegIndex miscReg = (MiscRegIndex) xc->tcBase()->flattenRegId(
1096            RegId(MiscRegClass, preFlatDest)).index();
1097
1098        bool hypTrap = mcrMrc15TrapToHyp(miscReg, xc->tcBase(), imm);
1099
1100        bool can_write, undefined;
1101        std::tie(can_write, undefined) = canWriteCoprocReg(miscReg, Scr, Cpsr);
1102
1103        // if we're in non secure PL1 mode then we can trap regardless
1104        // of whether the register is accessible, in other modes we
1105        // trap if only if the register IS accessible.
1106        if (undefined || (!can_write & !(hypTrap & !inUserMode(Cpsr) &
1107                                         !inSecureState(Scr, Cpsr)))) {
1108            return std::make_shared<UndefinedInstruction>(machInst, false,
1109                                                          mnemonic);
1110        }
1111        if (hypTrap) {
1112            return std::make_shared<HypervisorTrap>(machInst, imm,
1113                                                    EC_TRAPPED_CP15_MCR_MRC);
1114        }
1115    '''
1116
1117    McrDcimvacCode = '''
1118        const Request::Flags memAccessFlags(ArmISA::TLB::MustBeOne |
1119                                            Request::INVALIDATE |
1120                                            Request::DST_POC);
1121        EA = Op1;
1122    '''
1123    McrDcimvacIop = InstObjParams("mcr", "McrDcimvac",
1124                                  "MiscRegRegImmOp",
1125                                  {"memacc_code": McrDcCheckCode,
1126                                   "postacc_code": "",
1127                                   "ea_code": McrDcimvacCode,
1128                                   "predicate_test": predicateTest},
1129                                ['IsMemRef', 'IsStore'])
1130    header_output += MiscRegRegImmMemOpDeclare.subst(McrDcimvacIop)
1131    decoder_output += MiscRegRegImmOpConstructor.subst(McrDcimvacIop)
1132    exec_output += Mcr15Execute.subst(McrDcimvacIop) + \
1133                   Mcr15InitiateAcc.subst(McrDcimvacIop) + \
1134                   Mcr15CompleteAcc.subst(McrDcimvacIop)
1135
1136    McrDccmvacCode = '''
1137        const Request::Flags memAccessFlags(ArmISA::TLB::MustBeOne |
1138                                            Request::CLEAN |
1139                                            Request::DST_POC);
1140        EA = Op1;
1141    '''
1142    McrDccmvacIop = InstObjParams("mcr", "McrDccmvac",
1143                                  "MiscRegRegImmOp",
1144                                  {"memacc_code": McrDcCheckCode,
1145                                   "postacc_code": "",
1146                                   "ea_code": McrDccmvacCode,
1147                                   "predicate_test": predicateTest},
1148                                ['IsMemRef', 'IsStore'])
1149    header_output += MiscRegRegImmMemOpDeclare.subst(McrDccmvacIop)
1150    decoder_output += MiscRegRegImmOpConstructor.subst(McrDccmvacIop)
1151    exec_output += Mcr15Execute.subst(McrDccmvacIop) + \
1152                   Mcr15InitiateAcc.subst(McrDccmvacIop) + \
1153                   Mcr15CompleteAcc.subst(McrDccmvacIop)
1154
1155    McrDccmvauCode = '''
1156        const Request::Flags memAccessFlags(ArmISA::TLB::MustBeOne |
1157                                            Request::CLEAN |
1158                                            Request::DST_POU);
1159        EA = Op1;
1160    '''
1161    McrDccmvauIop = InstObjParams("mcr", "McrDccmvau",
1162                                  "MiscRegRegImmOp",
1163                                  {"memacc_code": McrDcCheckCode,
1164                                   "postacc_code": "",
1165                                   "ea_code": McrDccmvauCode,
1166                                   "predicate_test": predicateTest},
1167                                ['IsMemRef', 'IsStore'])
1168    header_output += MiscRegRegImmMemOpDeclare.subst(McrDccmvauIop)
1169    decoder_output += MiscRegRegImmOpConstructor.subst(McrDccmvauIop)
1170    exec_output += Mcr15Execute.subst(McrDccmvauIop) + \
1171                   Mcr15InitiateAcc.subst(McrDccmvauIop) + \
1172                   Mcr15CompleteAcc.subst(McrDccmvauIop)
1173
1174    McrDccimvacCode = '''
1175        const Request::Flags memAccessFlags(ArmISA::TLB::MustBeOne |
1176                                            Request::CLEAN |
1177                                            Request::INVALIDATE |
1178                                            Request::DST_POC);
1179        EA = Op1;
1180    '''
1181    McrDccimvacIop = InstObjParams("mcr", "McrDccimvac",
1182                                  "MiscRegRegImmOp",
1183                                  {"memacc_code": McrDcCheckCode,
1184                                   "postacc_code": "",
1185                                   "ea_code": McrDccimvacCode,
1186                                   "predicate_test": predicateTest},
1187                                ['IsMemRef', 'IsStore'])
1188    header_output += MiscRegRegImmMemOpDeclare.subst(McrDccimvacIop)
1189    decoder_output += MiscRegRegImmOpConstructor.subst(McrDccimvacIop)
1190    exec_output += Mcr15Execute.subst(McrDccimvacIop) + \
1191                   Mcr15InitiateAcc.subst(McrDccimvacIop) + \
1192                   Mcr15CompleteAcc.subst(McrDccimvacIop)
1193
1194    isbCode = '''
1195        // If the barrier is due to a CP15 access check for hyp traps
1196        if ((imm != 0) && mcrMrc15TrapToHyp(MISCREG_CP15ISB,
1197            xc->tcBase(), imm)) {
1198            return std::make_shared<HypervisorTrap>(machInst, imm,
1199                EC_TRAPPED_CP15_MCR_MRC);
1200        }
1201    '''
1202    isbIop = InstObjParams("isb", "Isb", "ImmOp",
1203                             {"code": isbCode,
1204                               "predicate_test": predicateTest},
1205                                ['IsSquashAfter'])
1206    header_output += ImmOpDeclare.subst(isbIop)
1207    decoder_output += ImmOpConstructor.subst(isbIop)
1208    exec_output += PredOpExecute.subst(isbIop)
1209
1210    dsbCode = '''
1211        // If the barrier is due to a CP15 access check for hyp traps
1212        if ((imm != 0) && mcrMrc15TrapToHyp(MISCREG_CP15DSB,
1213            xc->tcBase(), imm)) {
1214            return std::make_shared<HypervisorTrap>(machInst, imm,
1215                EC_TRAPPED_CP15_MCR_MRC);
1216        }
1217    '''
1218    dsbIop = InstObjParams("dsb", "Dsb", "ImmOp",
1219                             {"code": dsbCode,
1220                               "predicate_test": predicateTest},
1221                              ['IsMemBarrier', 'IsSerializeAfter'])
1222    header_output += ImmOpDeclare.subst(dsbIop)
1223    decoder_output += ImmOpConstructor.subst(dsbIop)
1224    exec_output += PredOpExecute.subst(dsbIop)
1225
1226    dmbCode = '''
1227        // If the barrier is due to a CP15 access check for hyp traps
1228        if ((imm != 0) && mcrMrc15TrapToHyp(MISCREG_CP15DMB,
1229            xc->tcBase(), imm)) {
1230            return std::make_shared<HypervisorTrap>(machInst, imm,
1231                EC_TRAPPED_CP15_MCR_MRC);
1232        }
1233    '''
1234    dmbIop = InstObjParams("dmb", "Dmb", "ImmOp",
1235                             {"code": dmbCode,
1236                               "predicate_test": predicateTest},
1237                               ['IsMemBarrier'])
1238    header_output += ImmOpDeclare.subst(dmbIop)
1239    decoder_output += ImmOpConstructor.subst(dmbIop)
1240    exec_output += PredOpExecute.subst(dmbIop)
1241
1242    dbgCode = '''
1243    '''
1244    dbgIop = InstObjParams("dbg", "Dbg", "PredOp",
1245                             {"code": dbgCode,
1246                               "predicate_test": predicateTest})
1247    header_output += BasicDeclare.subst(dbgIop)
1248    decoder_output += BasicConstructor.subst(dbgIop)
1249    exec_output += PredOpExecute.subst(dbgIop)
1250
1251    cpsCode = '''
1252    uint32_t mode = bits(imm, 4, 0);
1253    uint32_t f = bits(imm, 5);
1254    uint32_t i = bits(imm, 6);
1255    uint32_t a = bits(imm, 7);
1256    bool setMode = bits(imm, 8);
1257    bool enable = bits(imm, 9);
1258    CPSR cpsr = Cpsr;
1259    SCTLR sctlr = Sctlr;
1260    if (cpsr.mode != MODE_USER) {
1261        if (enable) {
1262            if (f) cpsr.f = 0;
1263            if (i) cpsr.i = 0;
1264            if (a) cpsr.a = 0;
1265        } else {
1266            if (f && !sctlr.nmfi) cpsr.f = 1;
1267            if (i) cpsr.i = 1;
1268            if (a) cpsr.a = 1;
1269        }
1270        if (setMode) {
1271            cpsr.mode = mode;
1272        }
1273    }
1274    Cpsr = cpsr;
1275    '''
1276    cpsIop = InstObjParams("cps", "Cps", "ImmOp",
1277                           { "code": cpsCode,
1278                             "predicate_test": predicateTest },
1279                           ["IsSerializeAfter","IsNonSpeculative"])
1280    header_output += ImmOpDeclare.subst(cpsIop)
1281    decoder_output += ImmOpConstructor.subst(cpsIop)
1282    exec_output += PredOpExecute.subst(cpsIop)
1283}};
1284