specialize.isa revision 4548
16145Snate@binkert.org// -*- mode:c++ -*-
26145Snate@binkert.org
36145Snate@binkert.org// Copyright (c) 2007 The Hewlett-Packard Development Company
46145Snate@binkert.org// All rights reserved.
56145Snate@binkert.org//
66145Snate@binkert.org// Redistribution and use of this software in source and binary forms,
76145Snate@binkert.org// with or without modification, are permitted provided that the
86145Snate@binkert.org// following conditions are met:
96145Snate@binkert.org//
106145Snate@binkert.org// The software must be used only for Non-Commercial Use which means any
116145Snate@binkert.org// use which is NOT directed to receiving any direct monetary
126145Snate@binkert.org// compensation for, or commercial advantage from such use.  Illustrative
136145Snate@binkert.org// examples of non-commercial use are academic research, personal study,
146145Snate@binkert.org// teaching, education and corporate research & development.
156145Snate@binkert.org// Illustrative examples of commercial use are distributing products for
166145Snate@binkert.org// commercial advantage and providing services using the software for
176145Snate@binkert.org// commercial advantage.
186145Snate@binkert.org//
196145Snate@binkert.org// If you wish to use this software or functionality therein that may be
206145Snate@binkert.org// covered by patents for commercial use, please contact:
216145Snate@binkert.org//     Director of Intellectual Property Licensing
226145Snate@binkert.org//     Office of Strategy and Technology
236145Snate@binkert.org//     Hewlett-Packard Company
246145Snate@binkert.org//     1501 Page Mill Road
256145Snate@binkert.org//     Palo Alto, California  94304
266145Snate@binkert.org//
276145Snate@binkert.org// Redistributions of source code must retain the above copyright notice,
286145Snate@binkert.org// this list of conditions and the following disclaimer.  Redistributions
297832Snate@binkert.org// in binary form must reproduce the above copyright notice, this list of
307547SBrad.Beckmann@amd.com// conditions and the following disclaimer in the documentation and/or
317547SBrad.Beckmann@amd.com// other materials provided with the distribution.  Neither the name of
328645Snilay@cs.wisc.edu// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
337454Snate@binkert.org// contributors may be used to endorse or promote products derived from
347054Snate@binkert.org// this software without specific prior written permission.  No right of
3510301Snilay@cs.wisc.edu// sublicense is granted herewith.  Derivatives of the software and
368258SBrad.Beckmann@amd.com// output created using the software may be prepared, but only for
376154Snate@binkert.org// Non-Commercial Uses.  Derivatives of the software may be shared with
387054Snate@binkert.org// others provided: (i) the others agree to abide by the list of
397547SBrad.Beckmann@amd.com// conditions herein which includes the Non-Commercial Use restrictions;
406154Snate@binkert.org// and (ii) such Derivatives of the software include the above copyright
416145Snate@binkert.org// notice to acknowledge the contribution from this software where
427055Snate@binkert.org// applicable, this list of conditions and the disclaimer below.
437454Snate@binkert.org//
447055Snate@binkert.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
456876Ssteve.reinhardt@amd.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4611124Snilay@cs.wisc.edu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4711124Snilay@cs.wisc.edu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4811124Snilay@cs.wisc.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
496285Snate@binkert.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
509274Snilay@cs.wisc.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
519593Snilay@cs.wisc.edu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
529593Snilay@cs.wisc.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
539274Snilay@cs.wisc.edu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
549858Snilay@cs.wisc.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
559274Snilay@cs.wisc.edu//
569274Snilay@cs.wisc.edu// Authors: Gabe Black
5711021Sjthestness@gmail.com
5811021Sjthestness@gmail.com////////////////////////////////////////////////////////////////////
5911021Sjthestness@gmail.com//
606881SBrad.Beckmann@amd.com//  Code to "specialize" a microcode sequence to use a particular
616285Snate@binkert.org//  variety of operands
627054Snate@binkert.org//
637054Snate@binkert.org
646881SBrad.Beckmann@amd.comlet {{
657054Snate@binkert.org    # This code builds up a decode block which decodes based on switchval.
666881SBrad.Beckmann@amd.com    # vals is a dict which matches case values with what should be decoded to.
677054Snate@binkert.org    # builder is called on the exploded contents of "vals" values to generate
687054Snate@binkert.org    # whatever code should be used.
697054Snate@binkert.org    def doSplitDecode(Name, builder, switchVal, vals, default = None):
709799Snilay@cs.wisc.edu        blocks = OutputBlocks()
716285Snate@binkert.org        blocks.decode_block = 'switch(%s) {\n' % switchVal
726145Snate@binkert.org        for (val, todo) in vals.items():
736145Snate@binkert.org            new_blocks = builder(Name, *todo)
746145Snate@binkert.org            new_blocks.decode_block = \
759858Snilay@cs.wisc.edu                '\tcase %s: %s\n' % (val, new_blocks.decode_block)
7611021Sjthestness@gmail.com            blocks.append(new_blocks)
776145Snate@binkert.org        if default:
786145Snate@binkert.org            new_blocks = builder(Name, *default)
796145Snate@binkert.org            new_blocks.decode_block = \
807054Snate@binkert.org                '\tdefault: %s\n' % new_blocks.decode_block
8111663Stushar@ece.gatech.edu            blocks.append(new_blocks)
829799Snilay@cs.wisc.edu        blocks.decode_block += '}\n'
836145Snate@binkert.org        return blocks
847054Snate@binkert.org}};
859858Snilay@cs.wisc.edu
869858Snilay@cs.wisc.edulet {{
877054Snate@binkert.org    class OpType(object):
888258SBrad.Beckmann@amd.com        parser = re.compile(r"(?P<tag>[A-Z][A-Z]*)(?P<size>[a-z][a-z]*)|(r(?P<reg>[A-Z0-9])(?P<rsize>[a-z]*))")
898258SBrad.Beckmann@amd.com        def __init__(self, opTypeString):
9010311Snilay@cs.wisc.edu            match = OpType.parser.search(opTypeString)
9110311Snilay@cs.wisc.edu            if match == None:
9210311Snilay@cs.wisc.edu                raise Exception, "Problem parsing operand type %s" % opTypeString
936145Snate@binkert.org            self.reg = match.group("reg")
946145Snate@binkert.org            self.tag = match.group("tag")
956145Snate@binkert.org            self.size = match.group("size")
967054Snate@binkert.org            self.rsize = match.group("rsize")
9711663Stushar@ece.gatech.edu
989799Snilay@cs.wisc.edu    ModRMRegIndex = "(MODRM_REG | (REX_R << 3))"
996145Snate@binkert.org    ModRMRMIndex = "(MODRM_RM | (REX_B << 3))"
1007054Snate@binkert.org
1019858Snilay@cs.wisc.edu    # This function specializes the given piece of code to use a particular
1026145Snate@binkert.org    # set of argument types described by "opTypes".
1036145Snate@binkert.org    def specializeInst(Name, opTypes, env):
1046145Snate@binkert.org        print "Specializing %s with opTypes %s" % (Name, opTypes)
1057054Snate@binkert.org        while len(opTypes):
10610917Sbrandon.potter@amd.com            # Parse the operand type string we're working with
10711664Stushar@ece.gatech.edu            opType = OpType(opTypes[0])
10811664Stushar@ece.gatech.edu
10911664Stushar@ece.gatech.edu            if opType.reg:
1106145Snate@binkert.org                #Figure out what to do with fixed register operands
1116145Snate@binkert.org                #This is the index to use, so we should stick it some place.
11210370Snilay@cs.wisc.edu                if opType.reg in ("A", "B", "C", "D"):
11310370Snilay@cs.wisc.edu                    env.addReg("INTREG_R%sX" % opType.reg)
1146145Snate@binkert.org                else:
1157054Snate@binkert.org                    env.addReg("INTREG_R%s" % opType.reg)
11611021Sjthestness@gmail.com                if opType.size:
11711021Sjthestness@gmail.com                    if opType.rsize in ("l", "h", "b"):
11811021Sjthestness@gmail.com                        print "byte"
11910311Snilay@cs.wisc.edu                    elif opType.rsize == "x":
1207054Snate@binkert.org                        print "word"
12110311Snilay@cs.wisc.edu                    else:
1227054Snate@binkert.org                        print "Didn't recognize fixed register size %s!" % opType.rsize
1238258SBrad.Beckmann@amd.com            elif opType.tag == None or opType.size == None:
1248258SBrad.Beckmann@amd.com                raise Exception, "Problem parsing operand tag: %s" % opType.tag
1259858Snilay@cs.wisc.edu            elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"):
1269858Snilay@cs.wisc.edu                # Use the "reg" field of the ModRM byte to select the register
12710311Snilay@cs.wisc.edu                env.addReg(ModRMRegIndex)
12810311Snilay@cs.wisc.edu            elif opType.tag in ("E", "Q", "W"):
1297054Snate@binkert.org                # This might refer to memory or to a register. We need to
1307054Snate@binkert.org                # divide it up farther.
1317054Snate@binkert.org                regTypes = copy.copy(opTypes)
1329863Snilay@cs.wisc.edu                regTypes.pop(0)
1337054Snate@binkert.org                regEnv = copy.copy(env)
13411523Sdavid.guillen@arm.com                regEnv.addReg(ModRMRMIndex)
13511523Sdavid.guillen@arm.com                # This needs to refer to memory, but we'll fill in the details
1369863Snilay@cs.wisc.edu                # later. It needs to take into account unaligned memory
1379863Snilay@cs.wisc.edu                # addresses.
1389863Snilay@cs.wisc.edu                memTypes = copy.copy(opTypes)
1399863Snilay@cs.wisc.edu                memTypes.pop(0)
1409863Snilay@cs.wisc.edu                memEnv = copy.copy(env)
1419863Snilay@cs.wisc.edu                print "%0"
1429863Snilay@cs.wisc.edu                return doSplitDecode(Name, specializeInst, "MODRM_MOD",
1439863Snilay@cs.wisc.edu                    {"3" : (regTypes, regEnv)}, (memTypes, memEnv))
1449863Snilay@cs.wisc.edu            elif opType.tag in ("I", "J"):
1459863Snilay@cs.wisc.edu                # Immediates
1467547SBrad.Beckmann@amd.com                print "IMMEDIATE"
1479863Snilay@cs.wisc.edu            elif opType.tag == "M":
1489863Snilay@cs.wisc.edu                # This needs to refer to memory, but we'll fill in the details
1499863Snilay@cs.wisc.edu                # later. It needs to take into account unaligned memory
1509863Snilay@cs.wisc.edu                # addresses.
1517547SBrad.Beckmann@amd.com                print "%0"
1529863Snilay@cs.wisc.edu            elif opType.tag in ("PR", "R", "VR"):
1539863Snilay@cs.wisc.edu                # There should probably be a check here to verify that mod
1549863Snilay@cs.wisc.edu                # is equal to 11b
1559863Snilay@cs.wisc.edu                env.addReg(ModRMRMIndex)
1567054Snate@binkert.org            else:
1577054Snate@binkert.org                raise Exception, "Unrecognized tag %s." % opType.tag
1587054Snate@binkert.org            opTypes.pop(0)
1597054Snate@binkert.org
1609863Snilay@cs.wisc.edu        # Generate code to return a macroop of the given name which will
1617054Snate@binkert.org        # operate in the given "emulation environment"
1629858Snilay@cs.wisc.edu        return genMacroop(Name, env)
1639863Snilay@cs.wisc.edu}};
1647054Snate@binkert.org