specialize.isa revision 8250:de679a068dd8
16184SN/A// -*- mode:c++ -*- 210330Smitch.hayenga@arm.com 38842Smrinmoy.ghosh@arm.com// Copyright (c) 2007 The Hewlett-Packard Development Company 48842Smrinmoy.ghosh@arm.com// All rights reserved. 58842Smrinmoy.ghosh@arm.com// 68842Smrinmoy.ghosh@arm.com// The license below extends only to copyright in the software and shall 78842Smrinmoy.ghosh@arm.com// not be construed as granting a license to any other intellectual 88842Smrinmoy.ghosh@arm.com// property including but not limited to intellectual property relating 98842Smrinmoy.ghosh@arm.com// to a hardware implementation of the functionality of the software 108842Smrinmoy.ghosh@arm.com// licensed hereunder. You may use the software subject to the license 118842Smrinmoy.ghosh@arm.com// terms below provided that you ensure that this notice is replicated 128842Smrinmoy.ghosh@arm.com// unmodified and in its entirety in all distributions of the software, 138842Smrinmoy.ghosh@arm.com// modified or unmodified, in source code or in binary form. 146184SN/A// 156184SN/A// Redistribution and use in source and binary forms, with or without 166184SN/A// modification, are permitted provided that the following conditions are 176184SN/A// met: redistributions of source code must retain the above copyright 186184SN/A// notice, this list of conditions and the following disclaimer; 196184SN/A// redistributions in binary form must reproduce the above copyright 206184SN/A// notice, this list of conditions and the following disclaimer in the 216184SN/A// documentation and/or other materials provided with the distribution; 226184SN/A// neither the name of the copyright holders nor the names of its 236184SN/A// contributors may be used to endorse or promote products derived from 246184SN/A// this software without specific prior written permission. 256184SN/A// 266184SN/A// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 276184SN/A// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 286184SN/A// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 296184SN/A// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 306184SN/A// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 316184SN/A// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 326184SN/A// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 336184SN/A// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 346184SN/A// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 356184SN/A// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 366184SN/A// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 376184SN/A// 386184SN/A// Authors: Gabe Black 396184SN/A 406184SN/A//////////////////////////////////////////////////////////////////// 416184SN/A// 426184SN/A// Code to "specialize" a microcode sequence to use a particular 4311793Sbrandon.potter@amd.com// variety of operands 4411793Sbrandon.potter@amd.com// 459360SE.Tomusk@sms.ed.ac.uk 466184SN/Alet {{ 476184SN/A # This code builds up a decode block which decodes based on switchval. 4810785Sgope@wisc.edu # vals is a dict which matches case values with what should be decoded to. 499480Snilay@cs.wisc.edu # Each element of the dict is a list containing a function and then the 509691Satgutier@umich.edu # arguments to pass to it. 519480Snilay@cs.wisc.edu def doSplitDecode(switchVal, vals, default = None): 529480Snilay@cs.wisc.edu blocks = OutputBlocks() 539691Satgutier@umich.edu blocks.decode_block = 'switch(%s) {\n' % switchVal 549480Snilay@cs.wisc.edu for (val, todo) in vals.items(): 559480Snilay@cs.wisc.edu new_blocks = todo[0](*todo[1:]) 5611434Smitch.hayenga@arm.com new_blocks.decode_block = \ 579691Satgutier@umich.edu '\tcase %s: %s\n' % (val, new_blocks.decode_block) 589691Satgutier@umich.edu blocks.append(new_blocks) 599691Satgutier@umich.edu if default: 609691Satgutier@umich.edu new_blocks = default[0](*default[1:]) 619691Satgutier@umich.edu new_blocks.decode_block = \ 629480Snilay@cs.wisc.edu '\tdefault: %s\n' % new_blocks.decode_block 6310785Sgope@wisc.edu blocks.append(new_blocks) 646184SN/A blocks.decode_block += '}\n' 659691Satgutier@umich.edu return blocks 669691Satgutier@umich.edu}}; 679691Satgutier@umich.edu 689691Satgutier@umich.edulet {{ 699691Satgutier@umich.edu def doRipRelativeDecode(Name, opTypes, env): 709691Satgutier@umich.edu # print "RIPing %s with opTypes %s" % (Name, opTypes) 719691Satgutier@umich.edu env.memoryInst = True 726184SN/A normEnv = copy.copy(env) 739360SE.Tomusk@sms.ed.ac.uk normEnv.addToDisassembly( 746184SN/A '''printMem(out, env.seg, env.scale, env.index, env.base, 756184SN/A machInst.displacement, env.addressSize, false);''') 766184SN/A normBlocks = specializeInst(Name + "_M", copy.copy(opTypes), normEnv) 776184SN/A ripEnv = copy.copy(env) 786184SN/A ripEnv.addToDisassembly( 799360SE.Tomusk@sms.ed.ac.uk '''printMem(out, env.seg, 1, 0, 0, 806184SN/A machInst.displacement, env.addressSize, true);''') 816184SN/A ripBlocks = specializeInst(Name + "_P", copy.copy(opTypes), ripEnv) 826184SN/A 836184SN/A blocks = OutputBlocks() 846184SN/A blocks.append(normBlocks) 856184SN/A blocks.append(ripBlocks) 866184SN/A 876184SN/A blocks.decode_block = ''' 886184SN/A if(machInst.modRM.mod == 0 && 896184SN/A machInst.modRM.rm == 5 && 906184SN/A machInst.mode.submode == SixtyFourBitMode) 916184SN/A { %s } 926184SN/A else 936184SN/A { %s }''' % \ 946184SN/A (ripBlocks.decode_block, normBlocks.decode_block) 956184SN/A return blocks 966184SN/A}}; 979360SE.Tomusk@sms.ed.ac.uk 989360SE.Tomusk@sms.ed.ac.uklet {{ 999360SE.Tomusk@sms.ed.ac.uk def doBadInstDecode(): 1006184SN/A blocks = OutputBlocks() 1016184SN/A blocks.decode_block = ''' 1026184SN/A return new Unknown(machInst); 1036184SN/A ''' 1046184SN/A return blocks 1059360SE.Tomusk@sms.ed.ac.uk}}; 1069360SE.Tomusk@sms.ed.ac.uk 1079360SE.Tomusk@sms.ed.ac.uklet {{ 1089360SE.Tomusk@sms.ed.ac.uk class OpType(object): 1096184SN/A parser = re.compile(r"(?P<tag>[A-Z]+)(?P<size>[a-z]*)|(r(?P<reg>[A-Z0-9]+)(?P<rsize>[a-z]*))") 1106184SN/A def __init__(self, opTypeString): 1116184SN/A match = OpType.parser.search(opTypeString) 1126184SN/A if match == None: 1136184SN/A raise Exception, "Problem parsing operand type %s" % opTypeString 1146184SN/A self.reg = match.group("reg") 1159360SE.Tomusk@sms.ed.ac.uk self.tag = match.group("tag") 1169360SE.Tomusk@sms.ed.ac.uk self.size = match.group("size") 1179360SE.Tomusk@sms.ed.ac.uk if not self.size: 1189360SE.Tomusk@sms.ed.ac.uk self.size = match.group("rsize") 1199360SE.Tomusk@sms.ed.ac.uk 1209360SE.Tomusk@sms.ed.ac.uk ModRMRegIndex = "(MODRM_REG | (REX_R << 3))" 1219360SE.Tomusk@sms.ed.ac.uk ModRMRMIndex = "(MODRM_RM | (REX_B << 3))" 1229360SE.Tomusk@sms.ed.ac.uk InstRegIndex = "(OPCODE_OP_BOTTOM3 | (REX_B << 3))" 1239360SE.Tomusk@sms.ed.ac.uk 1249360SE.Tomusk@sms.ed.ac.uk # This function specializes the given piece of code to use a particular 1259360SE.Tomusk@sms.ed.ac.uk # set of argument types described by "opTypes". 1269360SE.Tomusk@sms.ed.ac.uk def specializeInst(Name, opTypes, env): 1279360SE.Tomusk@sms.ed.ac.uk # print "Specializing %s with opTypes %s" % (Name, opTypes) 1289360SE.Tomusk@sms.ed.ac.uk while len(opTypes): 1299360SE.Tomusk@sms.ed.ac.uk # Parse the operand type string we're working with 1309360SE.Tomusk@sms.ed.ac.uk opType = OpType(opTypes[0]) 1319360SE.Tomusk@sms.ed.ac.uk opTypes.pop(0) 1329360SE.Tomusk@sms.ed.ac.uk 1339360SE.Tomusk@sms.ed.ac.uk if opType.tag not in ("I", "J", "P", "PR", "Q", "V", "VR", "W"): 1349360SE.Tomusk@sms.ed.ac.uk if opType.size: 1359360SE.Tomusk@sms.ed.ac.uk env.setSize(opType.size) 1366184SN/A 1376184SN/A if opType.reg: 1386184SN/A #Figure out what to do with fixed register operands 1396184SN/A #This is the index to use, so we should stick it some place. 1406184SN/A if opType.reg in ("A", "B", "C", "D"): 1416184SN/A regString = "INTREG_R%sX" % opType.reg 1426184SN/A else: 1436184SN/A regString = "INTREG_R%s" % opType.reg 1446184SN/A env.addReg(regString) 1456184SN/A env.addToDisassembly( 1466184SN/A "printReg(out, %s, regSize);\n" % regString) 1476184SN/A Name += "_R" 14811434Smitch.hayenga@arm.com elif opType.tag == "B": 1496184SN/A # This refers to registers whose index is encoded as part of the opcode 15011434Smitch.hayenga@arm.com env.addToDisassembly( 15111434Smitch.hayenga@arm.com "printReg(out, %s, regSize);\n" % InstRegIndex) 1526184SN/A Name += "_R" 1536184SN/A env.addReg(InstRegIndex) 1546184SN/A elif opType.tag == "M": 1556184SN/A # This refers to memory. The macroop constructor sets up modrm 15611434Smitch.hayenga@arm.com # addressing. Non memory modrm settings should cause an error. 1576184SN/A env.doModRM = True 15811434Smitch.hayenga@arm.com return doSplitDecode("MODRM_MOD", 15911434Smitch.hayenga@arm.com {"3" : (doBadInstDecode,) }, 1606184SN/A (doRipRelativeDecode, Name, opTypes, env)) 1616184SN/A elif opType.tag == None or opType.size == None: 1626184SN/A raise Exception, "Problem parsing operand tag: %s" % opType.tag 1636184SN/A elif opType.tag == "C": 1646184SN/A # A control register indexed by the "reg" field 1656184SN/A env.addReg(ModRMRegIndex) 1666184SN/A env.addToDisassembly( 1676184SN/A "ccprintf(out, \"CR%%d\", %s);\n" % ModRMRegIndex) 1686184SN/A Name += "_C" 1696184SN/A elif opType.tag == "D": 1706184SN/A # A debug register indexed by the "reg" field 1716184SN/A env.addReg(ModRMRegIndex) 1726184SN/A env.addToDisassembly( 1736184SN/A "ccprintf(out, \"DR%%d\", %s);\n" % ModRMRegIndex) 1746184SN/A Name += "_D" 1756184SN/A elif opType.tag == "S": 1766184SN/A # A segment selector register indexed by the "reg" field 1776184SN/A env.addReg(ModRMRegIndex) 1788842Smrinmoy.ghosh@arm.com env.addToDisassembly( 1798842Smrinmoy.ghosh@arm.com "printSegment(out, %s);\n" % ModRMRegIndex) 18011434Smitch.hayenga@arm.com Name += "_S" 1818842Smrinmoy.ghosh@arm.com elif opType.tag in ("G", "P", "T", "V"): 1828842Smrinmoy.ghosh@arm.com # Use the "reg" field of the ModRM byte to select the register 1839360SE.Tomusk@sms.ed.ac.uk env.addReg(ModRMRegIndex) 18411434Smitch.hayenga@arm.com env.addToDisassembly( 1858842Smrinmoy.ghosh@arm.com "printReg(out, %s, regSize);\n" % ModRMRegIndex) 1868842Smrinmoy.ghosh@arm.com if opType.tag == "P": 1879327Smrinmoy.ghosh@arm.com Name += "_MMX" 1888842Smrinmoy.ghosh@arm.com elif opType.tag == "V": 1898842Smrinmoy.ghosh@arm.com Name += "_XMM" 1906184SN/A else: 19111434Smitch.hayenga@arm.com Name += "_R" 1926184SN/A elif opType.tag in ("E", "Q", "W"): 1936184SN/A # This might refer to memory or to a register. We need to 1946184SN/A # divide it up farther. 1956184SN/A regEnv = copy.copy(env) 1966184SN/A regEnv.addReg(ModRMRMIndex) 1976184SN/A regEnv.addToDisassembly( 1986184SN/A "printReg(out, %s, regSize);\n" % ModRMRMIndex) 1996184SN/A # This refers to memory. The macroop constructor should set up 2006184SN/A # modrm addressing. 2016184SN/A memEnv = copy.copy(env) 2026184SN/A memEnv.doModRM = True 2036184SN/A regSuffix = "_R" 2049360SE.Tomusk@sms.ed.ac.uk if opType.tag == "Q": 2056184SN/A regSuffix = "_MMX" 2066184SN/A elif opType.tag == "W": 20711434Smitch.hayenga@arm.com regSuffix = "_XMM" 20811434Smitch.hayenga@arm.com return doSplitDecode("MODRM_MOD", 2096184SN/A {"3" : (specializeInst, Name + regSuffix, 2106184SN/A copy.copy(opTypes), regEnv)}, 21111434Smitch.hayenga@arm.com (doRipRelativeDecode, Name, 21211434Smitch.hayenga@arm.com copy.copy(opTypes), memEnv)) 2136184SN/A elif opType.tag in ("I", "J"): 2146184SN/A # Immediates 2156184SN/A env.addToDisassembly( 21611434Smitch.hayenga@arm.com "ccprintf(out, \"%#x\", machInst.immediate);\n") 2176184SN/A Name += "_I" 2186184SN/A elif opType.tag == "O": 2196184SN/A # Immediate containing a memory offset 22011098Slukefahr@umich.edu Name += "_MI" 2218842Smrinmoy.ghosh@arm.com elif opType.tag in ("PR", "R", "VR"): 2226184SN/A # Non register modrm settings should cause an error 2236184SN/A env.addReg(ModRMRMIndex) 2249360SE.Tomusk@sms.ed.ac.uk env.addToDisassembly( 2256184SN/A "printReg(out, %s, regSize);\n" % ModRMRMIndex) 22611782Sarthur.perais@inria.fr if opType.tag == "PR": 22711782Sarthur.perais@inria.fr Name += "_MMX" 2286184SN/A elif opType.tag == "VR": 2296184SN/A Name += "_XMM" 23011434Smitch.hayenga@arm.com else: 2318842Smrinmoy.ghosh@arm.com Name += "_R" 2326184SN/A elif opType.tag in ("X", "Y"): 2336184SN/A # This type of memory addressing is for string instructions. 23411434Smitch.hayenga@arm.com # They'll use the right index and segment internally. 2358842Smrinmoy.ghosh@arm.com if opType.tag == "X": 2366184SN/A env.addToDisassembly( 2376184SN/A '''printMem(out, env.seg, 2386184SN/A 1, X86ISA::ZeroReg, X86ISA::INTREG_RSI, 0, 2396184SN/A env.addressSize, false);''') 24011434Smitch.hayenga@arm.com else: 2418842Smrinmoy.ghosh@arm.com env.addToDisassembly( 2426184SN/A '''printMem(out, SEGMENT_REG_ES, 2436184SN/A 1, X86ISA::ZeroReg, X86ISA::INTREG_RDI, 0, 24411434Smitch.hayenga@arm.com env.addressSize, false);''') 2458842Smrinmoy.ghosh@arm.com Name += "_M" 2466184SN/A else: 2476184SN/A raise Exception, "Unrecognized tag %s." % opType.tag 2486184SN/A 2496184SN/A # Generate code to return a macroop of the given name which will 2506184SN/A # operate in the "emulation environment" env 2516184SN/A return genMacroop(Name, env) 25211434Smitch.hayenga@arm.com}}; 2536184SN/A