specialize.isa revision 4542
14348Sgblack@eecs.umich.edu// -*- mode:c++ -*-
24348Sgblack@eecs.umich.edu
34348Sgblack@eecs.umich.edu// Copyright (c) 2007 The Hewlett-Packard Development Company
44348Sgblack@eecs.umich.edu// All rights reserved.
54348Sgblack@eecs.umich.edu//
64348Sgblack@eecs.umich.edu// Redistribution and use of this software in source and binary forms,
74348Sgblack@eecs.umich.edu// with or without modification, are permitted provided that the
84348Sgblack@eecs.umich.edu// following conditions are met:
94348Sgblack@eecs.umich.edu//
104348Sgblack@eecs.umich.edu// The software must be used only for Non-Commercial Use which means any
114348Sgblack@eecs.umich.edu// use which is NOT directed to receiving any direct monetary
124348Sgblack@eecs.umich.edu// compensation for, or commercial advantage from such use.  Illustrative
134348Sgblack@eecs.umich.edu// examples of non-commercial use are academic research, personal study,
144348Sgblack@eecs.umich.edu// teaching, education and corporate research & development.
154348Sgblack@eecs.umich.edu// Illustrative examples of commercial use are distributing products for
164348Sgblack@eecs.umich.edu// commercial advantage and providing services using the software for
174348Sgblack@eecs.umich.edu// commercial advantage.
184348Sgblack@eecs.umich.edu//
194348Sgblack@eecs.umich.edu// If you wish to use this software or functionality therein that may be
204348Sgblack@eecs.umich.edu// covered by patents for commercial use, please contact:
214348Sgblack@eecs.umich.edu//     Director of Intellectual Property Licensing
224348Sgblack@eecs.umich.edu//     Office of Strategy and Technology
234348Sgblack@eecs.umich.edu//     Hewlett-Packard Company
244348Sgblack@eecs.umich.edu//     1501 Page Mill Road
254348Sgblack@eecs.umich.edu//     Palo Alto, California  94304
264348Sgblack@eecs.umich.edu//
274348Sgblack@eecs.umich.edu// Redistributions of source code must retain the above copyright notice,
284348Sgblack@eecs.umich.edu// this list of conditions and the following disclaimer.  Redistributions
294348Sgblack@eecs.umich.edu// in binary form must reproduce the above copyright notice, this list of
304348Sgblack@eecs.umich.edu// conditions and the following disclaimer in the documentation and/or
314348Sgblack@eecs.umich.edu// other materials provided with the distribution.  Neither the name of
324348Sgblack@eecs.umich.edu// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
334348Sgblack@eecs.umich.edu// contributors may be used to endorse or promote products derived from
344348Sgblack@eecs.umich.edu// this software without specific prior written permission.  No right of
354348Sgblack@eecs.umich.edu// sublicense is granted herewith.  Derivatives of the software and
364348Sgblack@eecs.umich.edu// output created using the software may be prepared, but only for
374348Sgblack@eecs.umich.edu// Non-Commercial Uses.  Derivatives of the software may be shared with
384348Sgblack@eecs.umich.edu// others provided: (i) the others agree to abide by the list of
394348Sgblack@eecs.umich.edu// conditions herein which includes the Non-Commercial Use restrictions;
404348Sgblack@eecs.umich.edu// and (ii) such Derivatives of the software include the above copyright
414348Sgblack@eecs.umich.edu// notice to acknowledge the contribution from this software where
424348Sgblack@eecs.umich.edu// applicable, this list of conditions and the disclaimer below.
434348Sgblack@eecs.umich.edu//
444348Sgblack@eecs.umich.edu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
454348Sgblack@eecs.umich.edu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
464348Sgblack@eecs.umich.edu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
474348Sgblack@eecs.umich.edu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
484348Sgblack@eecs.umich.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
494348Sgblack@eecs.umich.edu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
504348Sgblack@eecs.umich.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
514348Sgblack@eecs.umich.edu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
524348Sgblack@eecs.umich.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
534348Sgblack@eecs.umich.edu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
544348Sgblack@eecs.umich.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
554348Sgblack@eecs.umich.edu//
564348Sgblack@eecs.umich.edu// Authors: Gabe Black
574348Sgblack@eecs.umich.edu
584348Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////
594348Sgblack@eecs.umich.edu//
604348Sgblack@eecs.umich.edu//  Code to "specialize" a microcode sequence to use a particular
614348Sgblack@eecs.umich.edu//  variety of operands
624348Sgblack@eecs.umich.edu//
634348Sgblack@eecs.umich.edu
644348Sgblack@eecs.umich.edulet {{
654348Sgblack@eecs.umich.edu    # This code builds up a decode block which decodes based on switchval.
664348Sgblack@eecs.umich.edu    # vals is a dict which matches case values with what should be decoded to.
674348Sgblack@eecs.umich.edu    # builder is called on the exploded contents of "vals" values to generate
684348Sgblack@eecs.umich.edu    # whatever code should be used.
694528Sgblack@eecs.umich.edu    def doSplitDecode(Name, builder, switchVal, vals, default = None):
704542Sgblack@eecs.umich.edu        blocks = OutputBlocks()
714542Sgblack@eecs.umich.edu        blocks.decode_block = 'switch(%s) {\n' % switchVal
724348Sgblack@eecs.umich.edu        for (val, todo) in vals.items():
734542Sgblack@eecs.umich.edu            new_blocks = builder(Name, *todo)
744542Sgblack@eecs.umich.edu            new_blocks.decode_block = \
754542Sgblack@eecs.umich.edu                '\tcase %s: %s\n' % (val, new_blocks.decode_block)
764542Sgblack@eecs.umich.edu            blocks.append(new_blocks)
774348Sgblack@eecs.umich.edu        if default:
784542Sgblack@eecs.umich.edu            new_blocks = builder(Name, *default)
794542Sgblack@eecs.umich.edu            new_blocks.decode_block = \
804542Sgblack@eecs.umich.edu                '\tdefault: %s\n' % new_blocks.decode_block
814542Sgblack@eecs.umich.edu            blocks.append(new_blocks)
824542Sgblack@eecs.umich.edu        blocks.decode_block += '}\n'
834542Sgblack@eecs.umich.edu        return blocks
844348Sgblack@eecs.umich.edu}};
854348Sgblack@eecs.umich.edu
864348Sgblack@eecs.umich.edulet {{
874348Sgblack@eecs.umich.edu    class OpType(object):
884528Sgblack@eecs.umich.edu        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]*))")
894348Sgblack@eecs.umich.edu        def __init__(self, opTypeString):
904348Sgblack@eecs.umich.edu            match = OpType.parser.search(opTypeString)
914348Sgblack@eecs.umich.edu            if match == None:
924348Sgblack@eecs.umich.edu                raise Exception, "Problem parsing operand type %s" % opTypeString
934348Sgblack@eecs.umich.edu            self.reg = match.group("reg")
944348Sgblack@eecs.umich.edu            self.tag = match.group("tag")
954348Sgblack@eecs.umich.edu            self.size = match.group("size")
964528Sgblack@eecs.umich.edu            self.rsize = match.group("rsize")
974348Sgblack@eecs.umich.edu
984348Sgblack@eecs.umich.edu    # This function specializes the given piece of code to use a particular
994528Sgblack@eecs.umich.edu    # set of argument types described by "opTypes".
1004528Sgblack@eecs.umich.edu    def specializeInst(Name, opTypes, env):
1014542Sgblack@eecs.umich.edu        print "Specializing %s with opTypes %s" % (Name, opTypes)
1024348Sgblack@eecs.umich.edu        while len(opTypes):
1034528Sgblack@eecs.umich.edu            # Parse the operand type string we're working with
1044542Sgblack@eecs.umich.edu            opType = OpType(opTypes[0])
1054348Sgblack@eecs.umich.edu
1064348Sgblack@eecs.umich.edu            if opType.reg:
1074348Sgblack@eecs.umich.edu                #Figure out what to do with fixed register operands
1084528Sgblack@eecs.umich.edu                #This is the index to use, so we should stick it some place.
1094542Sgblack@eecs.umich.edu                if opType.reg in ("A", "B", "C", "D"):
1104542Sgblack@eecs.umich.edu                    env.addReg("INTREG_R%sX" % opType.reg)
1114542Sgblack@eecs.umich.edu                else:
1124542Sgblack@eecs.umich.edu                    env.addReg("INTREG_R%s" % opType.reg)
1134528Sgblack@eecs.umich.edu                if opType.size:
1144528Sgblack@eecs.umich.edu                    if opType.rsize in ("l", "h", "b"):
1154528Sgblack@eecs.umich.edu                        print "byte"
1164528Sgblack@eecs.umich.edu                    elif opType.rsize == "x":
1174528Sgblack@eecs.umich.edu                        print "word"
1184528Sgblack@eecs.umich.edu                    else:
1194528Sgblack@eecs.umich.edu                        print "Didn't recognize fixed register size %s!" % opType.rsize
1204348Sgblack@eecs.umich.edu            elif opType.tag == None or opType.size == None:
1214348Sgblack@eecs.umich.edu                raise Exception, "Problem parsing operand tag: %s" % opType.tag
1224348Sgblack@eecs.umich.edu            elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"):
1234348Sgblack@eecs.umich.edu                # Use the "reg" field of the ModRM byte to select the register
1244542Sgblack@eecs.umich.edu                env.addReg("(uint8_t)MODRM_REG")
1254348Sgblack@eecs.umich.edu            elif opType.tag in ("E", "Q", "W"):
1264348Sgblack@eecs.umich.edu                # This might refer to memory or to a register. We need to
1274348Sgblack@eecs.umich.edu                # divide it up farther.
1284348Sgblack@eecs.umich.edu                regTypes = copy.copy(opTypes)
1294528Sgblack@eecs.umich.edu                regTypes.pop(0)
1304528Sgblack@eecs.umich.edu                regEnv = copy.copy(env)
1314542Sgblack@eecs.umich.edu                regEnv.addReg("(uint8_t)MODRM_RM")
1324348Sgblack@eecs.umich.edu                # This needs to refer to memory, but we'll fill in the details
1334348Sgblack@eecs.umich.edu                # later. It needs to take into account unaligned memory
1344348Sgblack@eecs.umich.edu                # addresses.
1354348Sgblack@eecs.umich.edu                memTypes = copy.copy(opTypes)
1364528Sgblack@eecs.umich.edu                memTypes.pop(0)
1374528Sgblack@eecs.umich.edu                memEnv = copy.copy(env)
1384542Sgblack@eecs.umich.edu                print "%0"
1394528Sgblack@eecs.umich.edu                return doSplitDecode(Name, specializeInst, "MODRM_MOD",
1404542Sgblack@eecs.umich.edu                    {"3" : (regTypes, regEnv)}, (memTypes, memEnv))
1414348Sgblack@eecs.umich.edu            elif opType.tag in ("I", "J"):
1424532Sgblack@eecs.umich.edu                # Immediates
1434528Sgblack@eecs.umich.edu                print "IMMEDIATE"
1444348Sgblack@eecs.umich.edu            elif opType.tag == "M":
1454348Sgblack@eecs.umich.edu                # This needs to refer to memory, but we'll fill in the details
1464348Sgblack@eecs.umich.edu                # later. It needs to take into account unaligned memory
1474348Sgblack@eecs.umich.edu                # addresses.
1484528Sgblack@eecs.umich.edu                print "%0"
1494348Sgblack@eecs.umich.edu            elif opType.tag in ("PR", "R", "VR"):
1504348Sgblack@eecs.umich.edu                # There should probably be a check here to verify that mod
1514348Sgblack@eecs.umich.edu                # is equal to 11b
1524542Sgblack@eecs.umich.edu                env.addReg("(uint8_t)MODRM_RM")
1534348Sgblack@eecs.umich.edu            else:
1544348Sgblack@eecs.umich.edu                raise Exception, "Unrecognized tag %s." % opType.tag
1554528Sgblack@eecs.umich.edu            opTypes.pop(0)
1564348Sgblack@eecs.umich.edu
1574532Sgblack@eecs.umich.edu        # Generate code to return a macroop of the given name which will
1584532Sgblack@eecs.umich.edu        # operate in the given "emulation environment"
1594528Sgblack@eecs.umich.edu        return genMacroop(Name, env)
1604348Sgblack@eecs.umich.edu}};
161