data.isa revision 7185
16145Snate@binkert.org// -*- mode:c++ -*-
26145Snate@binkert.org
36145Snate@binkert.org// Copyright (c) 2010 ARM Limited
46145Snate@binkert.org// All rights reserved
56145Snate@binkert.org//
66145Snate@binkert.org// The license below extends only to copyright in the software and shall
76145Snate@binkert.org// not be construed as granting a license to any other intellectual
86145Snate@binkert.org// property including but not limited to intellectual property relating
96145Snate@binkert.org// to a hardware implementation of the functionality of the software
106145Snate@binkert.org// licensed hereunder.  You may use the software subject to the license
116145Snate@binkert.org// terms below provided that you ensure that this notice is replicated
126145Snate@binkert.org// unmodified and in its entirety in all distributions of the software,
136145Snate@binkert.org// modified or unmodified, in source code or in binary form.
146145Snate@binkert.org//
156145Snate@binkert.org// Redistribution and use in source and binary forms, with or without
166145Snate@binkert.org// modification, are permitted provided that the following conditions are
176145Snate@binkert.org// met: redistributions of source code must retain the above copyright
186145Snate@binkert.org// notice, this list of conditions and the following disclaimer;
196145Snate@binkert.org// redistributions in binary form must reproduce the above copyright
206145Snate@binkert.org// notice, this list of conditions and the following disclaimer in the
216145Snate@binkert.org// documentation and/or other materials provided with the distribution;
226145Snate@binkert.org// neither the name of the copyright holders nor the names of its
236145Snate@binkert.org// contributors may be used to endorse or promote products derived from
246145Snate@binkert.org// this software without specific prior written permission.
256145Snate@binkert.org//
266145Snate@binkert.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
276145Snate@binkert.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
286145Snate@binkert.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
296145Snate@binkert.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
306145Snate@binkert.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
316145Snate@binkert.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
326145Snate@binkert.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
336145Snate@binkert.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
346145Snate@binkert.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
356145Snate@binkert.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
366145Snate@binkert.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
376145Snate@binkert.org//
386145Snate@binkert.org// Authors: Gabe Black
396145Snate@binkert.org
406145Snate@binkert.orglet {{
416145Snate@binkert.org
426154Snate@binkert.org    header_output = ""
436154Snate@binkert.org    decoder_output = ""
446145Snate@binkert.org    exec_output = ""
456145Snate@binkert.org
466145Snate@binkert.org    calcQCode = '''
476145Snate@binkert.org        cprintf("canOverflow: %%d\\n", Dest < resTemp);
486145Snate@binkert.org        replaceBits(CondCodes, 27, Dest < resTemp);
496145Snate@binkert.org    '''
506145Snate@binkert.org
516145Snate@binkert.org    calcCcCode = '''
526145Snate@binkert.org        uint16_t _ic, _iv, _iz, _in;
536145Snate@binkert.org        _in = (resTemp >> %(negBit)d) & 1;
546145Snate@binkert.org        _iz = (resTemp == 0);
556145Snate@binkert.org        _iv = %(ivValue)s & 1;
566145Snate@binkert.org        _ic = %(icValue)s & 1;
576145Snate@binkert.org
586145Snate@binkert.org        CondCodes =  _in << 31 | _iz << 30 | _ic << 29 | _iv << 28 |
596145Snate@binkert.org                    (CondCodes & 0x0FFFFFFF);
606145Snate@binkert.org
616145Snate@binkert.org        DPRINTF(Arm, "(in, iz, ic, iv) = (%%d, %%d, %%d, %%d)\\n",
626145Snate@binkert.org                     _in, _iz, _ic, _iv);
636145Snate@binkert.org       '''
646145Snate@binkert.org
656145Snate@binkert.org    # Dict of code to set the carry flag. (imm, reg, reg-reg)
666145Snate@binkert.org    oldC = 'CondCodes<29:>'
676145Snate@binkert.org    oldV = 'CondCodes<28:>'
686145Snate@binkert.org    carryCode = {
696145Snate@binkert.org        "none": (oldC, oldC, oldC),
706145Snate@binkert.org        "llbit": (oldC, oldC, oldC),
716145Snate@binkert.org        "overflow": ('0', '0', '0'),
726145Snate@binkert.org        "add": ('findCarry(32, resTemp, Op1, secondOp)',
736145Snate@binkert.org                'findCarry(32, resTemp, Op1, secondOp)',
746145Snate@binkert.org                'findCarry(32, resTemp, Op1, secondOp)'),
756145Snate@binkert.org        "sub": ('findCarry(32, resTemp, Op1, ~secondOp)',
766145Snate@binkert.org                'findCarry(32, resTemp, Op1, ~secondOp)',
776145Snate@binkert.org                'findCarry(32, resTemp, Op1, ~secondOp)'),
786145Snate@binkert.org        "rsb": ('findCarry(32, resTemp, secondOp, ~Op1)',
796145Snate@binkert.org                'findCarry(32, resTemp, secondOp, ~Op1)',
806145Snate@binkert.org                'findCarry(32, resTemp, secondOp, ~Op1)'),
816145Snate@binkert.org        "logic": ('(rotC ? bits(secondOp, 31) : %s)' % oldC,
826145Snate@binkert.org                  'shift_carry_imm(Op2, shiftAmt, shiftType, %s)' % oldC,
836145Snate@binkert.org                  'shift_carry_rs(Op2, Shift<7:0>, shiftType, %s)' % oldC)
846145Snate@binkert.org    }
856145Snate@binkert.org    # Dict of code to set the overflow flag.
866145Snate@binkert.org    overflowCode = {
876145Snate@binkert.org        "none": oldV,
886145Snate@binkert.org        "llbit": oldV,
896145Snate@binkert.org        "overflow": '0',
90        "add": 'findOverflow(32, resTemp, Op1, secondOp)',
91        "sub": 'findOverflow(32, resTemp, Op1, ~secondOp)',
92        "rsb": 'findOverflow(32, resTemp, secondOp, ~Op1)',
93        "logic": oldV
94    }
95
96    secondOpRe = re.compile("secondOp")
97    immOp2 = "imm"
98    regOp2 = "shift_rm_imm(Op2, shiftAmt, shiftType, CondCodes<29:>)"
99    regRegOp2 = "shift_rm_rs(Op2, Shift<7:0>, shiftType, CondCodes<29:>)"
100
101    def buildImmDataInst(mnem, code, flagType = "logic"):
102        global header_output, decoder_output, exec_output
103        cCode = carryCode[flagType]
104        vCode = overflowCode[flagType]
105        negBit = 31
106        if flagType == "llbit":
107            negBit = 63
108        if flagType == "overflow":
109            immCcCode = calcQCode
110        else:
111            immCcCode = calcCcCode % {
112                "icValue": secondOpRe.sub(immOp2, cCode[0]),
113                "ivValue": secondOpRe.sub(immOp2, vCode),
114                "negBit": negBit
115            }
116        immCode = secondOpRe.sub(immOp2, code)
117        immIop = InstObjParams(mnem, mnem.capitalize() + "Imm", "DataImmOp",
118                               {"code" : immCode,
119                                "predicate_test": predicateTest})
120        immIopCc = InstObjParams(mnem + "s", mnem.capitalize() + "ImmCc",
121                                 "DataImmOp",
122                                 {"code" : immCode + immCcCode,
123                                  "predicate_test": predicateTest})
124        header_output += DataImmDeclare.subst(immIop) + \
125                         DataImmDeclare.subst(immIopCc)
126        decoder_output += DataImmConstructor.subst(immIop) + \
127                          DataImmConstructor.subst(immIopCc)
128        exec_output += PredOpExecute.subst(immIop) + \
129                       PredOpExecute.subst(immIopCc)
130
131    def buildRegDataInst(mnem, code, flagType = "logic"):
132        global header_output, decoder_output, exec_output
133        cCode = carryCode[flagType]
134        vCode = overflowCode[flagType]
135        negBit = 31
136        if flagType == "llbit":
137            negBit = 63
138        if flagType == "overflow":
139            regCcCode = calcQCode
140        else:
141            regCcCode = calcCcCode % {
142                "icValue": secondOpRe.sub(regOp2, cCode[1]),
143                "ivValue": secondOpRe.sub(regOp2, vCode),
144                "negBit": negBit
145            }
146        regCode = secondOpRe.sub(regOp2, code)
147        regIop = InstObjParams(mnem, mnem.capitalize() + "Reg", "DataRegOp",
148                               {"code" : regCode,
149                                "predicate_test": predicateTest})
150        regIopCc = InstObjParams(mnem + "s", mnem.capitalize() + "RegCc",
151                                 "DataRegOp",
152                                 {"code" : regCode + regCcCode,
153                                  "predicate_test": predicateTest})
154        header_output += DataRegDeclare.subst(regIop) + \
155                         DataRegDeclare.subst(regIopCc)
156        decoder_output += DataRegConstructor.subst(regIop) + \
157                          DataRegConstructor.subst(regIopCc)
158        exec_output += PredOpExecute.subst(regIop) + \
159                       PredOpExecute.subst(regIopCc)
160
161    def buildRegRegDataInst(mnem, code, flagType = "logic"):
162        global header_output, decoder_output, exec_output
163        cCode = carryCode[flagType]
164        vCode = overflowCode[flagType]
165        negBit = 31
166        if flagType == "llbit":
167            negBit = 63
168        if flagType == "overflow":
169            regRegCcCode = calcQCode
170        else:
171            regRegCcCode = calcCcCode % {
172                "icValue": secondOpRe.sub(regRegOp2, cCode[2]),
173                "ivValue": secondOpRe.sub(regRegOp2, vCode),
174                "negBit": negBit
175            }
176        regRegCode = secondOpRe.sub(regRegOp2, code)
177        regRegIop = InstObjParams(mnem, mnem.capitalize() + "RegReg",
178                                  "DataRegRegOp",
179                                  {"code" : regRegCode,
180                                   "predicate_test": predicateTest})
181        regRegIopCc = InstObjParams(mnem + "s",
182                                    mnem.capitalize() + "RegRegCc",
183                                    "DataRegRegOp",
184                                    {"code" : regRegCode + regRegCcCode,
185                                     "predicate_test": predicateTest})
186        header_output += DataRegRegDeclare.subst(regRegIop) + \
187                         DataRegRegDeclare.subst(regRegIopCc)
188        decoder_output += DataRegRegConstructor.subst(regRegIop) + \
189                          DataRegRegConstructor.subst(regRegIopCc)
190        exec_output += PredOpExecute.subst(regRegIop) + \
191                       PredOpExecute.subst(regRegIopCc)
192
193    def buildDataInst(mnem, code, flagType = "logic"):
194        buildImmDataInst(mnem, code, flagType)
195        buildRegDataInst(mnem, code, flagType)
196        buildRegRegDataInst(mnem, code, flagType)
197
198    buildDataInst("and", "AIWDest = resTemp = Op1 & secondOp;")
199    buildDataInst("eor", "AIWDest = resTemp = Op1 ^ secondOp;")
200    buildDataInst("sub", "AIWDest = resTemp = Op1 - secondOp;", "sub")
201    buildDataInst("rsb", "AIWDest = resTemp = secondOp - Op1;", "rsb")
202    buildDataInst("add", "AIWDest = resTemp = Op1 + secondOp;", "add")
203    buildImmDataInst("adr", '''
204                               AIWDest = resTemp = (readPC(xc) & ~0x3) +
205                               (op1 ? secondOp : -secondOp);
206                            ''')
207    buildDataInst("adc", "AIWDest = resTemp = Op1 + secondOp + %s;" % oldC,
208                  "add")
209    buildDataInst("sbc", "AIWDest = resTemp = Op1 - secondOp - !%s;" % oldC,
210                  "sub")
211    buildDataInst("rsc", "AIWDest = resTemp = secondOp - Op1 - !%s;" % oldC,
212                  "rsb")
213    buildDataInst("tst", "resTemp = Op1 & secondOp;")
214    buildDataInst("teq", "resTemp = Op1 ^ secondOp;")
215    buildDataInst("cmp", "resTemp = Op1 - secondOp;", "sub")
216    buildDataInst("cmn", "resTemp = Op1 + secondOp;", "add")
217    buildDataInst("orr", "AIWDest = resTemp = Op1 | secondOp;")
218    buildDataInst("orn", "Dest = resTemp = Op1 | ~secondOp;")
219    buildImmDataInst("mov", "AIWDest = resTemp = secondOp;")
220    buildRegDataInst("mov", "AIWDest = resTemp = secondOp;")
221    buildRegRegDataInst("mov", "Dest = resTemp = secondOp;")
222    buildDataInst("bic", "AIWDest = resTemp = Op1 & ~secondOp;")
223    buildDataInst("mvn", "AIWDest = resTemp = ~secondOp;")
224    buildDataInst("movt",
225            "Dest = resTemp = insertBits(Op1, 31, 16, secondOp);")
226}};
227