branch64.isa revision 14172
14604Sgblack@eecs.umich.edu// -*- mode:c++ -*-
24604Sgblack@eecs.umich.edu
34604Sgblack@eecs.umich.edu// Copyright (c) 2011-2013, 2016,2018 ARM Limited
44604Sgblack@eecs.umich.edu// All rights reserved
54604Sgblack@eecs.umich.edu//
64604Sgblack@eecs.umich.edu// The license below extends only to copyright in the software and shall
74604Sgblack@eecs.umich.edu// not be construed as granting a license to any other intellectual
84604Sgblack@eecs.umich.edu// property including but not limited to intellectual property relating
94604Sgblack@eecs.umich.edu// to a hardware implementation of the functionality of the software
104604Sgblack@eecs.umich.edu// licensed hereunder.  You may use the software subject to the license
114604Sgblack@eecs.umich.edu// terms below provided that you ensure that this notice is replicated
124604Sgblack@eecs.umich.edu// unmodified and in its entirety in all distributions of the software,
134604Sgblack@eecs.umich.edu// modified or unmodified, in source code or in binary form.
144604Sgblack@eecs.umich.edu//
154604Sgblack@eecs.umich.edu// Redistribution and use in source and binary forms, with or without
164604Sgblack@eecs.umich.edu// modification, are permitted provided that the following conditions are
174604Sgblack@eecs.umich.edu// met: redistributions of source code must retain the above copyright
184604Sgblack@eecs.umich.edu// notice, this list of conditions and the following disclaimer;
194604Sgblack@eecs.umich.edu// redistributions in binary form must reproduce the above copyright
204604Sgblack@eecs.umich.edu// notice, this list of conditions and the following disclaimer in the
214604Sgblack@eecs.umich.edu// documentation and/or other materials provided with the distribution;
224604Sgblack@eecs.umich.edu// neither the name of the copyright holders nor the names of its
234604Sgblack@eecs.umich.edu// contributors may be used to endorse or promote products derived from
244604Sgblack@eecs.umich.edu// this software without specific prior written permission.
254604Sgblack@eecs.umich.edu//
264604Sgblack@eecs.umich.edu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
274604Sgblack@eecs.umich.edu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
284604Sgblack@eecs.umich.edu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
294604Sgblack@eecs.umich.edu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
304604Sgblack@eecs.umich.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
314604Sgblack@eecs.umich.edu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
324604Sgblack@eecs.umich.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
334604Sgblack@eecs.umich.edu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
344604Sgblack@eecs.umich.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
354604Sgblack@eecs.umich.edu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
364604Sgblack@eecs.umich.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
374604Sgblack@eecs.umich.edu//
384604Sgblack@eecs.umich.edu// Authors: Gabe Black
394604Sgblack@eecs.umich.edu//          Giacomo Gabrielli
404604Sgblack@eecs.umich.edu
414604Sgblack@eecs.umich.edulet {{
424604Sgblack@eecs.umich.edu
434604Sgblack@eecs.umich.edu    header_output = ""
444604Sgblack@eecs.umich.edu    decoder_output = ""
454604Sgblack@eecs.umich.edu    exec_output = ""
464604Sgblack@eecs.umich.edu
474604Sgblack@eecs.umich.edu    # B, BL
484604Sgblack@eecs.umich.edu    for (mnem, link) in (("b", False), ("bl", True)):
494604Sgblack@eecs.umich.edu        bCode = ('NPC = purifyTaggedAddr(RawPC + imm, xc->tcBase(), '
504604Sgblack@eecs.umich.edu                 'currEL(xc->tcBase()));\n')
514604Sgblack@eecs.umich.edu        instFlags = ['IsDirectControl', 'IsUncondControl']
524604Sgblack@eecs.umich.edu        if (link):
534604Sgblack@eecs.umich.edu            bCode += 'XLR = RawPC + 4;\n'
544604Sgblack@eecs.umich.edu            instFlags += ['IsCall']
554604Sgblack@eecs.umich.edu
564604Sgblack@eecs.umich.edu        bIop = InstObjParams(mnem, mnem.capitalize() + "64",
574604Sgblack@eecs.umich.edu                             "BranchImm64", bCode, instFlags)
584604Sgblack@eecs.umich.edu        header_output += BranchImm64Declare.subst(bIop)
594604Sgblack@eecs.umich.edu        decoder_output += BranchImm64Constructor.subst(bIop)
604604Sgblack@eecs.umich.edu        exec_output += BasicExecute.subst(bIop)
614863Sgblack@eecs.umich.edu
624863Sgblack@eecs.umich.edu    # BR, BLR
636329Sgblack@eecs.umich.edu    for (mnem, link) in (("br", False), ("blr", True)):
644604Sgblack@eecs.umich.edu        bCode = ('NPC = purifyTaggedAddr(XOp1, xc->tcBase(), '
654604Sgblack@eecs.umich.edu                 'currEL(xc->tcBase()));\n')
664604Sgblack@eecs.umich.edu        instFlags = ['IsIndirectControl', 'IsUncondControl']
674604Sgblack@eecs.umich.edu        if (link):
684604Sgblack@eecs.umich.edu            bCode += 'XLR = RawPC + 4;\n'
694604Sgblack@eecs.umich.edu            instFlags += ['IsCall']
704604Sgblack@eecs.umich.edu
714604Sgblack@eecs.umich.edu        bIop = InstObjParams(mnem, mnem.capitalize() + "64",
724863Sgblack@eecs.umich.edu                             "BranchReg64", bCode, instFlags)
734604Sgblack@eecs.umich.edu        header_output += BranchReg64Declare.subst(bIop)
744604Sgblack@eecs.umich.edu        decoder_output += BranchReg64Constructor.subst(bIop)
754604Sgblack@eecs.umich.edu        exec_output += BasicExecute.subst(bIop)
764604Sgblack@eecs.umich.edu
774604Sgblack@eecs.umich.edu    # B conditional
784604Sgblack@eecs.umich.edu    bCode = '''
794604Sgblack@eecs.umich.edu        if (testPredicate(CondCodesNZ, CondCodesC, CondCodesV, condCode))
804604Sgblack@eecs.umich.edu            NPC = purifyTaggedAddr(RawPC + imm, xc->tcBase(),
814604Sgblack@eecs.umich.edu                                   currEL(xc->tcBase()));
824863Sgblack@eecs.umich.edu        else
834604Sgblack@eecs.umich.edu            NPC = NPC;
844604Sgblack@eecs.umich.edu    '''
854604Sgblack@eecs.umich.edu    bIop = InstObjParams("b", "BCond64", "BranchImmCond64", bCode,
864604Sgblack@eecs.umich.edu                         ['IsCondControl', 'IsDirectControl'])
874604Sgblack@eecs.umich.edu    header_output += BranchImmCond64Declare.subst(bIop)
884604Sgblack@eecs.umich.edu    decoder_output += BranchImmCond64Constructor.subst(bIop)
894604Sgblack@eecs.umich.edu    exec_output += BasicExecute.subst(bIop)
905966Sgblack@eecs.umich.edu
914604Sgblack@eecs.umich.edu    # RET
924604Sgblack@eecs.umich.edu    bCode = ('NPC = purifyTaggedAddr(XOp1, xc->tcBase(), '
934604Sgblack@eecs.umich.edu             'currEL(xc->tcBase()));\n')
944604Sgblack@eecs.umich.edu    instFlags = ['IsIndirectControl', 'IsUncondControl', 'IsReturn']
95
96    bIop = InstObjParams('ret', 'Ret64', "BranchRet64", bCode, instFlags)
97    header_output += BranchReg64Declare.subst(bIop)
98    decoder_output += BranchReg64Constructor.subst(bIop)
99    exec_output += BasicExecute.subst(bIop)
100
101    # ERET
102    bCode = '''Addr newPc;
103                CPSR cpsr = Cpsr;
104                CPSR spsr = Spsr;
105
106                ExceptionLevel curr_el = currEL(cpsr);
107                switch (curr_el) {
108                  case EL3:
109                    newPc = xc->tcBase()->readMiscReg(MISCREG_ELR_EL3);
110                    break;
111                  case EL2:
112                    newPc = xc->tcBase()->readMiscReg(MISCREG_ELR_EL2);
113                    break;
114                  case EL1:
115                    newPc = xc->tcBase()->readMiscReg(MISCREG_ELR_EL1);
116                    break;
117                  default:
118                    return std::make_shared<UndefinedInstruction>(machInst,
119                                                                  false,
120                                                                  mnemonic);
121                    break;
122                }
123                if (spsr.width) {
124                    // Exception return to AArch32.
125                    // 32 most significant bits are ignored
126                    newPc &= mask(32);
127
128                    if (newPc & mask(2)) {
129                        // Mask bits to avoid PC Alignment fault when returning
130                        // to AArch32
131                        if (spsr.t)
132                            newPc = newPc & ~mask(1);
133                        else
134                            newPc = newPc & ~mask(2);
135                    }
136                }
137
138                CPSR new_cpsr = getPSTATEFromPSR(xc->tcBase(), cpsr, spsr);
139
140                Cpsr = new_cpsr;
141                CondCodesNZ = new_cpsr.nz;
142                CondCodesC  = new_cpsr.c;
143                CondCodesV  = new_cpsr.v;
144
145                NextAArch64 = !new_cpsr.width;
146                NextItState = itState(new_cpsr);
147                NPC = purifyTaggedAddr(newPc, xc->tcBase(),
148                    currEL(new_cpsr));
149
150                LLSCLock = 0;  // Clear exclusive monitor
151                SevMailbox = 1; //Set Event Register
152    '''
153    instFlags = ['IsSerializeAfter', 'IsNonSpeculative', 'IsSquashAfter']
154    bIop = InstObjParams('eret', 'Eret64', "BranchEret64", bCode, instFlags)
155    header_output += BasicDeclare.subst(bIop)
156    decoder_output += BasicConstructor64.subst(bIop)
157    exec_output += BasicExecute.subst(bIop)
158
159    # CBNZ, CBZ
160    for (mnem, test) in (("cbz", "=="), ("cbnz", "!=")):
161        code = ('NPC = (Op164 %(test)s 0) ? '
162                'purifyTaggedAddr(RawPC + imm, xc->tcBase(), '
163                'currEL(xc->tcBase())) : NPC;\n')
164        code = code % {"test": test}
165        iop = InstObjParams(mnem, mnem.capitalize() + "64",
166                            "BranchImmReg64", code,
167                            ['IsCondControl', 'IsDirectControl'])
168        header_output += BranchImmReg64Declare.subst(iop)
169        decoder_output += BranchImmReg64Constructor.subst(iop)
170        exec_output += BasicExecute.subst(iop)
171
172    # TBNZ, TBZ
173    for (mnem, test) in (("tbz", "=="), ("tbnz", "!=")):
174        code = ('NPC = ((Op164 & imm1) %(test)s 0) ? '
175                'purifyTaggedAddr(RawPC + imm2, xc->tcBase(), '
176                'currEL(xc->tcBase())) : NPC;\n')
177        code = code % {"test": test}
178        iop = InstObjParams(mnem, mnem.capitalize() + "64",
179                            "BranchImmImmReg64", code,
180                            ['IsCondControl', 'IsDirectControl'])
181        header_output += BranchImmImmReg64Declare.subst(iop)
182        decoder_output += BranchImmImmReg64Constructor.subst(iop)
183        exec_output += BasicExecute.subst(iop)
184}};
185