specialize.isa revision 4532
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): 704528Sgblack@eecs.umich.edu decode_block = 'switch(%s) {\n' % switchVal 714348Sgblack@eecs.umich.edu for (val, todo) in vals.items(): 724528Sgblack@eecs.umich.edu new_block = builder(Name, *todo) 734528Sgblack@eecs.umich.edu new_block = '\tcase %s: %s\n' % (val, new_block) 744528Sgblack@eecs.umich.edu decode_block += new_block 754348Sgblack@eecs.umich.edu if default: 764528Sgblack@eecs.umich.edu new_block = builder(Name, *default) 774528Sgblack@eecs.umich.edu new_block = '\tdefault: %s\n' % new_block 784528Sgblack@eecs.umich.edu decode_block += new_block 794528Sgblack@eecs.umich.edu decode_block += '}\n' 804528Sgblack@eecs.umich.edu return decode_block 814348Sgblack@eecs.umich.edu}}; 824348Sgblack@eecs.umich.edu 834348Sgblack@eecs.umich.edulet {{ 844348Sgblack@eecs.umich.edu class OpType(object): 854528Sgblack@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]*))") 864348Sgblack@eecs.umich.edu def __init__(self, opTypeString): 874348Sgblack@eecs.umich.edu match = OpType.parser.search(opTypeString) 884348Sgblack@eecs.umich.edu if match == None: 894348Sgblack@eecs.umich.edu raise Exception, "Problem parsing operand type %s" % opTypeString 904348Sgblack@eecs.umich.edu self.reg = match.group("reg") 914348Sgblack@eecs.umich.edu self.tag = match.group("tag") 924348Sgblack@eecs.umich.edu self.size = match.group("size") 934528Sgblack@eecs.umich.edu self.rsize = match.group("rsize") 944348Sgblack@eecs.umich.edu 954348Sgblack@eecs.umich.edu # This function specializes the given piece of code to use a particular 964528Sgblack@eecs.umich.edu # set of argument types described by "opTypes". 974528Sgblack@eecs.umich.edu def specializeInst(Name, opTypes, env): 984348Sgblack@eecs.umich.edu while len(opTypes): 994348Sgblack@eecs.umich.edu # print "Building a composite op with tags", opTypes 1004348Sgblack@eecs.umich.edu # print "And code", code 1014348Sgblack@eecs.umich.edu opNum = len(opTypes) - 1 1024348Sgblack@eecs.umich.edu 1034528Sgblack@eecs.umich.edu # Parse the operand type string we're working with 1044348Sgblack@eecs.umich.edu opType = OpType(opTypes[opNum]) 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. 1094528Sgblack@eecs.umich.edu print "INTREG_R%s" % (opType.reg + opType.size.upper()) 1104528Sgblack@eecs.umich.edu if opType.size: 1114528Sgblack@eecs.umich.edu if opType.rsize in ("l", "h", "b"): 1124528Sgblack@eecs.umich.edu print "byte" 1134528Sgblack@eecs.umich.edu elif opType.rsize == "x": 1144528Sgblack@eecs.umich.edu print "word" 1154528Sgblack@eecs.umich.edu else: 1164528Sgblack@eecs.umich.edu print "Didn't recognize fixed register size %s!" % opType.rsize 1174348Sgblack@eecs.umich.edu elif opType.tag == None or opType.size == None: 1184348Sgblack@eecs.umich.edu raise Exception, "Problem parsing operand tag: %s" % opType.tag 1194348Sgblack@eecs.umich.edu elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"): 1204348Sgblack@eecs.umich.edu # Use the "reg" field of the ModRM byte to select the register 1214528Sgblack@eecs.umich.edu print "(uint8_t)MODRM_REG" 1224348Sgblack@eecs.umich.edu elif opType.tag in ("E", "Q", "W"): 1234348Sgblack@eecs.umich.edu # This might refer to memory or to a register. We need to 1244348Sgblack@eecs.umich.edu # divide it up farther. 1254528Sgblack@eecs.umich.edu print "(uint8_t)MODRM_RM" 1264348Sgblack@eecs.umich.edu regTypes = copy.copy(opTypes) 1274528Sgblack@eecs.umich.edu regTypes.pop(0) 1284528Sgblack@eecs.umich.edu regEnv = copy.copy(env) 1294348Sgblack@eecs.umich.edu # This needs to refer to memory, but we'll fill in the details 1304348Sgblack@eecs.umich.edu # later. It needs to take into account unaligned memory 1314348Sgblack@eecs.umich.edu # addresses. 1324528Sgblack@eecs.umich.edu print "%0" 1334348Sgblack@eecs.umich.edu memTypes = copy.copy(opTypes) 1344528Sgblack@eecs.umich.edu memTypes.pop(0) 1354528Sgblack@eecs.umich.edu memEnv = copy.copy(env) 1364528Sgblack@eecs.umich.edu return doSplitDecode(Name, specializeInst, "MODRM_MOD", 1374528Sgblack@eecs.umich.edu {"3" : (regTypes, memEnv)}, (memTypes, memEnv)) 1384348Sgblack@eecs.umich.edu elif opType.tag in ("I", "J"): 1394532Sgblack@eecs.umich.edu # Immediates 1404528Sgblack@eecs.umich.edu print "IMMEDIATE" 1414348Sgblack@eecs.umich.edu elif opType.tag == "M": 1424348Sgblack@eecs.umich.edu # This needs to refer to memory, but we'll fill in the details 1434348Sgblack@eecs.umich.edu # later. It needs to take into account unaligned memory 1444348Sgblack@eecs.umich.edu # addresses. 1454528Sgblack@eecs.umich.edu print "%0" 1464348Sgblack@eecs.umich.edu elif opType.tag in ("PR", "R", "VR"): 1474348Sgblack@eecs.umich.edu # There should probably be a check here to verify that mod 1484348Sgblack@eecs.umich.edu # is equal to 11b 1494528Sgblack@eecs.umich.edu print "(uint8_t)MODRM_RM" 1504348Sgblack@eecs.umich.edu else: 1514348Sgblack@eecs.umich.edu raise Exception, "Unrecognized tag %s." % opType.tag 1524528Sgblack@eecs.umich.edu opTypes.pop(0) 1534348Sgblack@eecs.umich.edu 1544532Sgblack@eecs.umich.edu # Generate code to return a macroop of the given name which will 1554532Sgblack@eecs.umich.edu # operate in the given "emulation environment" 1564528Sgblack@eecs.umich.edu return genMacroop(Name, env) 1574348Sgblack@eecs.umich.edu}}; 158