specialize.isa revision 4601
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.
694575Sgblack@eecs.umich.edu    def doSplitDecode(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():
734575Sgblack@eecs.umich.edu            new_blocks = builder(*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:
784575Sgblack@eecs.umich.edu            new_blocks = builder(*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):
884601Sgblack@eecs.umich.edu        parser = re.compile(r"(?P<tag>[A-Z]+)(?P<size>[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
984548Sgblack@eecs.umich.edu    ModRMRegIndex = "(MODRM_REG | (REX_R << 3))"
994548Sgblack@eecs.umich.edu    ModRMRMIndex = "(MODRM_RM | (REX_B << 3))"
1004548Sgblack@eecs.umich.edu
1014348Sgblack@eecs.umich.edu    # This function specializes the given piece of code to use a particular
1024528Sgblack@eecs.umich.edu    # set of argument types described by "opTypes".
1034528Sgblack@eecs.umich.edu    def specializeInst(Name, opTypes, env):
1044568Sgblack@eecs.umich.edu        # print "Specializing %s with opTypes %s" % (Name, opTypes)
1054348Sgblack@eecs.umich.edu        while len(opTypes):
1064528Sgblack@eecs.umich.edu            # Parse the operand type string we're working with
1074542Sgblack@eecs.umich.edu            opType = OpType(opTypes[0])
1084601Sgblack@eecs.umich.edu            opTypes.pop(0)
1094348Sgblack@eecs.umich.edu
1104348Sgblack@eecs.umich.edu            if opType.reg:
1114348Sgblack@eecs.umich.edu                #Figure out what to do with fixed register operands
1124528Sgblack@eecs.umich.edu                #This is the index to use, so we should stick it some place.
1134542Sgblack@eecs.umich.edu                if opType.reg in ("A", "B", "C", "D"):
1144601Sgblack@eecs.umich.edu                    env.addReg("INTREG_R%sX | (REX_B << 3)" % opType.reg)
1154542Sgblack@eecs.umich.edu                else:
1164601Sgblack@eecs.umich.edu                    env.addReg("INTREG_R%s | (REX_B << 3)" % opType.reg)
1174528Sgblack@eecs.umich.edu                if opType.size:
1184528Sgblack@eecs.umich.edu                    if opType.rsize in ("l", "h", "b"):
1194528Sgblack@eecs.umich.edu                        print "byte"
1204528Sgblack@eecs.umich.edu                    elif opType.rsize == "x":
1214528Sgblack@eecs.umich.edu                        print "word"
1224528Sgblack@eecs.umich.edu                    else:
1234528Sgblack@eecs.umich.edu                        print "Didn't recognize fixed register size %s!" % opType.rsize
1244575Sgblack@eecs.umich.edu                Name += "_R"
1254601Sgblack@eecs.umich.edu            elif opType.tag == "M":
1264601Sgblack@eecs.umich.edu                # This refers to memory. The macroop constructor sets up modrm
1274601Sgblack@eecs.umich.edu                # addressing. Non memory modrm settings should cause an error.
1284601Sgblack@eecs.umich.edu                Name += "_M"
1294601Sgblack@eecs.umich.edu                env.doModRM = True
1304348Sgblack@eecs.umich.edu            elif opType.tag == None or opType.size == None:
1314348Sgblack@eecs.umich.edu                raise Exception, "Problem parsing operand tag: %s" % opType.tag
1324348Sgblack@eecs.umich.edu            elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"):
1334348Sgblack@eecs.umich.edu                # Use the "reg" field of the ModRM byte to select the register
1344548Sgblack@eecs.umich.edu                env.addReg(ModRMRegIndex)
1354575Sgblack@eecs.umich.edu                Name += "_R"
1364348Sgblack@eecs.umich.edu            elif opType.tag in ("E", "Q", "W"):
1374348Sgblack@eecs.umich.edu                # This might refer to memory or to a register. We need to
1384348Sgblack@eecs.umich.edu                # divide it up farther.
1394528Sgblack@eecs.umich.edu                regEnv = copy.copy(env)
1404548Sgblack@eecs.umich.edu                regEnv.addReg(ModRMRMIndex)
1414601Sgblack@eecs.umich.edu                # This refers to memory. The macroop constructor should set up
1424601Sgblack@eecs.umich.edu                # modrm addressing.
1434528Sgblack@eecs.umich.edu                memEnv = copy.copy(env)
1444601Sgblack@eecs.umich.edu                memEnv.doModRM = True
1454575Sgblack@eecs.umich.edu                return doSplitDecode(specializeInst, "MODRM_MOD",
1464601Sgblack@eecs.umich.edu                    {"3" : (Name + "_R", copy.copy(opTypes), regEnv)},
1474601Sgblack@eecs.umich.edu                           (Name + "_M", copy.copy(opTypes), memEnv))
1484348Sgblack@eecs.umich.edu            elif opType.tag in ("I", "J"):
1494532Sgblack@eecs.umich.edu                # Immediates
1504575Sgblack@eecs.umich.edu                Name += "_I"
1514348Sgblack@eecs.umich.edu            elif opType.tag in ("PR", "R", "VR"):
1524601Sgblack@eecs.umich.edu                # Non register modrm settings should cause an error
1534548Sgblack@eecs.umich.edu                env.addReg(ModRMRMIndex)
1544575Sgblack@eecs.umich.edu                Name += "_R"
1554348Sgblack@eecs.umich.edu            else:
1564348Sgblack@eecs.umich.edu                raise Exception, "Unrecognized tag %s." % opType.tag
1574348Sgblack@eecs.umich.edu
1584532Sgblack@eecs.umich.edu        # Generate code to return a macroop of the given name which will
1594559Sgblack@eecs.umich.edu        # operate in the "emulation environment" env
1604528Sgblack@eecs.umich.edu        return genMacroop(Name, env)
1614348Sgblack@eecs.umich.edu}};
162