specialize.isa revision 4542:f6ca2384b304
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 296145Snate@binkert.org// in binary form must reproduce the above copyright notice, this list of 306284Snate@binkert.org// conditions and the following disclaimer in the documentation and/or 316284Snate@binkert.org// other materials provided with the distribution. Neither the name of 326284Snate@binkert.org// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 336284Snate@binkert.org// contributors may be used to endorse or promote products derived from 346145Snate@binkert.org// this software without specific prior written permission. No right of 356284Snate@binkert.org// sublicense is granted herewith. Derivatives of the software and 366284Snate@binkert.org// output created using the software may be prepared, but only for 376145Snate@binkert.org// Non-Commercial Uses. Derivatives of the software may be shared with 386284Snate@binkert.org// others provided: (i) the others agree to abide by the list of 396145Snate@binkert.org// conditions herein which includes the Non-Commercial Use restrictions; 406284Snate@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 426284Snate@binkert.org// applicable, this list of conditions and the disclaimer below. 436145Snate@binkert.org// 446145Snate@binkert.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 457048Snate@binkert.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 467048Snate@binkert.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 476145Snate@binkert.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 487055Snate@binkert.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 497455Snate@binkert.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 507055Snate@binkert.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 517454Snate@binkert.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 527055Snate@binkert.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 537455Snate@binkert.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 548164Snilay@cs.wisc.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 556154Snate@binkert.org// 568165Snilay@cs.wisc.edu// Authors: Gabe Black 577048Snate@binkert.org 587048Snate@binkert.org//////////////////////////////////////////////////////////////////// 596154Snate@binkert.org// 606154Snate@binkert.org// Code to "specialize" a microcode sequence to use a particular 617048Snate@binkert.org// variety of operands 627048Snate@binkert.org// 637048Snate@binkert.org 646154Snate@binkert.orglet {{ 657048Snate@binkert.org # This code builds up a decode block which decodes based on switchval. 666285Snate@binkert.org # vals is a dict which matches case values with what should be decoded to. 677048Snate@binkert.org # builder is called on the exploded contents of "vals" values to generate 686876Ssteve.reinhardt@amd.com # whatever code should be used. 696876Ssteve.reinhardt@amd.com def doSplitDecode(Name, builder, switchVal, vals, default = None): 706876Ssteve.reinhardt@amd.com blocks = OutputBlocks() 718174Snilay@cs.wisc.edu blocks.decode_block = 'switch(%s) {\n' % switchVal 726145Snate@binkert.org for (val, todo) in vals.items(): 736145Snate@binkert.org new_blocks = builder(Name, *todo) 747048Snate@binkert.org new_blocks.decode_block = \ 757048Snate@binkert.org '\tcase %s: %s\n' % (val, new_blocks.decode_block) 767048Snate@binkert.org blocks.append(new_blocks) 776876Ssteve.reinhardt@amd.com if default: 787048Snate@binkert.org new_blocks = builder(Name, *default) 797048Snate@binkert.org new_blocks.decode_block = \ 806145Snate@binkert.org '\tdefault: %s\n' % new_blocks.decode_block 817048Snate@binkert.org blocks.append(new_blocks) 826145Snate@binkert.org blocks.decode_block += '}\n' 837055Snate@binkert.org return blocks 847048Snate@binkert.org}}; 856145Snate@binkert.org 867055Snate@binkert.orglet {{ 877055Snate@binkert.org class OpType(object): 887055Snate@binkert.org 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]*))") 897048Snate@binkert.org def __init__(self, opTypeString): 907055Snate@binkert.org match = OpType.parser.search(opTypeString) 917055Snate@binkert.org if match == None: 926145Snate@binkert.org raise Exception, "Problem parsing operand type %s" % opTypeString 937048Snate@binkert.org self.reg = match.group("reg") 947048Snate@binkert.org self.tag = match.group("tag") 956145Snate@binkert.org self.size = match.group("size") 968174Snilay@cs.wisc.edu self.rsize = match.group("rsize") 976145Snate@binkert.org 987055Snate@binkert.org # This function specializes the given piece of code to use a particular 997048Snate@binkert.org # set of argument types described by "opTypes". 1007048Snate@binkert.org def specializeInst(Name, opTypes, env): 1017048Snate@binkert.org print "Specializing %s with opTypes %s" % (Name, opTypes) 1026145Snate@binkert.org while len(opTypes): 1037048Snate@binkert.org # Parse the operand type string we're working with 1046145Snate@binkert.org opType = OpType(opTypes[0]) 1057048Snate@binkert.org 1066145Snate@binkert.org if opType.reg: 1077048Snate@binkert.org #Figure out what to do with fixed register operands 1086145Snate@binkert.org #This is the index to use, so we should stick it some place. 1097048Snate@binkert.org if opType.reg in ("A", "B", "C", "D"): 1107048Snate@binkert.org env.addReg("INTREG_R%sX" % opType.reg) 1117048Snate@binkert.org else: 1127048Snate@binkert.org env.addReg("INTREG_R%s" % opType.reg) 1137048Snate@binkert.org if opType.size: 1146145Snate@binkert.org if opType.rsize in ("l", "h", "b"): 1157048Snate@binkert.org print "byte" 1167048Snate@binkert.org elif opType.rsize == "x": 1177048Snate@binkert.org print "word" 1187048Snate@binkert.org else: 1197048Snate@binkert.org print "Didn't recognize fixed register size %s!" % opType.rsize 1206145Snate@binkert.org elif opType.tag == None or opType.size == None: 1217048Snate@binkert.org raise Exception, "Problem parsing operand tag: %s" % opType.tag 1227048Snate@binkert.org elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"): 1237048Snate@binkert.org # Use the "reg" field of the ModRM byte to select the register 1247048Snate@binkert.org env.addReg("(uint8_t)MODRM_REG") 1257048Snate@binkert.org elif opType.tag in ("E", "Q", "W"): 1266145Snate@binkert.org # This might refer to memory or to a register. We need to 1277048Snate@binkert.org # divide it up farther. 1286145Snate@binkert.org regTypes = copy.copy(opTypes) 1297048Snate@binkert.org regTypes.pop(0) 1307048Snate@binkert.org regEnv = copy.copy(env) 1317048Snate@binkert.org regEnv.addReg("(uint8_t)MODRM_RM") 1326145Snate@binkert.org # This needs to refer to memory, but we'll fill in the details 1337048Snate@binkert.org # later. It needs to take into account unaligned memory 1347048Snate@binkert.org # addresses. 1357546SBrad.Beckmann@amd.com memTypes = copy.copy(opTypes) 1367546SBrad.Beckmann@amd.com memTypes.pop(0) 1377546SBrad.Beckmann@amd.com memEnv = copy.copy(env) 1387546SBrad.Beckmann@amd.com print "%0" 1397546SBrad.Beckmann@amd.com return doSplitDecode(Name, specializeInst, "MODRM_MOD", 1407565SBrad.Beckmann@amd.com {"3" : (regTypes, regEnv)}, (memTypes, memEnv)) 1417565SBrad.Beckmann@amd.com elif opType.tag in ("I", "J"): 1427565SBrad.Beckmann@amd.com # Immediates 1437565SBrad.Beckmann@amd.com print "IMMEDIATE" 1447565SBrad.Beckmann@amd.com elif opType.tag == "M": 1457565SBrad.Beckmann@amd.com # This needs to refer to memory, but we'll fill in the details 1467565SBrad.Beckmann@amd.com # later. It needs to take into account unaligned memory 1477565SBrad.Beckmann@amd.com # addresses. 1487565SBrad.Beckmann@amd.com print "%0" 1497565SBrad.Beckmann@amd.com elif opType.tag in ("PR", "R", "VR"): 1507565SBrad.Beckmann@amd.com # There should probably be a check here to verify that mod 1517565SBrad.Beckmann@amd.com # is equal to 11b 1527546SBrad.Beckmann@amd.com env.addReg("(uint8_t)MODRM_RM") 1538165Snilay@cs.wisc.edu else: 1547546SBrad.Beckmann@amd.com raise Exception, "Unrecognized tag %s." % opType.tag 1557546SBrad.Beckmann@amd.com opTypes.pop(0) 1567048Snate@binkert.org 1576145Snate@binkert.org # Generate code to return a macroop of the given name which will 1587048Snate@binkert.org # operate in the given "emulation environment" 1596145Snate@binkert.org return genMacroop(Name, env) 1607055Snate@binkert.org}}; 1616285Snate@binkert.org