specialize.isa revision 4371:c5003760793e
16899SN/A// -*- mode:c++ -*- 26899SN/A 36899SN/A// Copyright (c) 2007 The Hewlett-Packard Development Company 46899SN/A// All rights reserved. 56899SN/A// 66899SN/A// Redistribution and use of this software in source and binary forms, 76899SN/A// with or without modification, are permitted provided that the 86899SN/A// following conditions are met: 96899SN/A// 106899SN/A// The software must be used only for Non-Commercial Use which means any 116899SN/A// use which is NOT directed to receiving any direct monetary 126899SN/A// compensation for, or commercial advantage from such use. Illustrative 136899SN/A// examples of non-commercial use are academic research, personal study, 146899SN/A// teaching, education and corporate research & development. 156899SN/A// Illustrative examples of commercial use are distributing products for 166899SN/A// commercial advantage and providing services using the software for 176899SN/A// commercial advantage. 186899SN/A// 196899SN/A// If you wish to use this software or functionality therein that may be 206899SN/A// covered by patents for commercial use, please contact: 216899SN/A// Director of Intellectual Property Licensing 226899SN/A// Office of Strategy and Technology 236899SN/A// Hewlett-Packard Company 246899SN/A// 1501 Page Mill Road 256899SN/A// Palo Alto, California 94304 266899SN/A// 276899SN/A// Redistributions of source code must retain the above copyright notice, 286899SN/A// this list of conditions and the following disclaimer. Redistributions 296899SN/A// in binary form must reproduce the above copyright notice, this list of 306899SN/A// conditions and the following disclaimer in the documentation and/or 316899SN/A// other materials provided with the distribution. Neither the name of 326899SN/A// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 336899SN/A// contributors may be used to endorse or promote products derived from 346899SN/A// this software without specific prior written permission. No right of 356899SN/A// sublicense is granted herewith. Derivatives of the software and 366899SN/A// output created using the software may be prepared, but only for 376899SN/A// Non-Commercial Uses. Derivatives of the software may be shared with 386899SN/A// others provided: (i) the others agree to abide by the list of 398928Sandreas.hansson@arm.com// conditions herein which includes the Non-Commercial Use restrictions; 406899SN/A// and (ii) such Derivatives of the software include the above copyright 416899SN/A// notice to acknowledge the contribution from this software where 426899SN/A// applicable, this list of conditions and the disclaimer below. 436899SN/A// 446899SN/A// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 456899SN/A// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 466899SN/A// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 476899SN/A// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 488928Sandreas.hansson@arm.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 496899SN/A// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 507553SN/A// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 517553SN/A// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 526899SN/A// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 536899SN/A// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 547553SN/A// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 557553SN/A// 566899SN/A// Authors: Gabe Black 576899SN/A 587538SN/A//////////////////////////////////////////////////////////////////// 596899SN/A// 607538SN/A// Code to "specialize" a microcode sequence to use a particular 616899SN/A// variety of operands 626899SN/A// 636899SN/A 646899SN/Alet {{ 656899SN/A # This code builds up a decode block which decodes based on switchval. 666899SN/A # vals is a dict which matches case values with what should be decoded to. 676899SN/A # builder is called on the exploded contents of "vals" values to generate 686899SN/A # whatever code should be used. 696899SN/A def doSplitDecode(name, Name, builder, switchVal, vals, default = None): 706899SN/A blocks = OutputBlocks() 717632SBrad.Beckmann@amd.com blocks.decode_block += 'switch(%s) {\n' % switchVal 726899SN/A for (val, todo) in vals.items(): 737553SN/A built = builder(name, Name, *todo) 747553SN/A built.decode_block = '\tcase %s: %s\n' % (val, built.decode_block) 757553SN/A blocks.append(built) 767553SN/A if default: 777553SN/A built = builder(name, Name, *default) 787553SN/A built.decode_block = '\tdefault: %s\n' % built.decode_block 797553SN/A blocks.append(built) 807553SN/A blocks.decode_block += '}\n' 817553SN/A return blocks 827632SBrad.Beckmann@amd.com}}; 837553SN/A 846899SN/Alet {{ 856899SN/A class OpType(object): 866899SN/A parser = re.compile(r"(?P<tag>[A-Z][A-Z]*)(?P<size>[a-z][a-z]*)|(r(?P<reg>[A-Za-z0-9][A-Za-z0-9]*))") 876899SN/A def __init__(self, opTypeString): 886899SN/A match = OpType.parser.search(opTypeString) 896899SN/A if match == None: 906899SN/A raise Exception, "Problem parsing operand type %s" % opTypeString 916899SN/A self.reg = match.group("reg") 927553SN/A self.tag = match.group("tag") 937553SN/A self.size = match.group("size") 947553SN/A 957553SN/A # This function specializes the given piece of code to use a particular 967553SN/A # set of argument types described by "opTypes". These are "implemented" 977632SBrad.Beckmann@amd.com # in reverse order. 987553SN/A def specializeInst(name, Name, code, opTypes): 998436SBrad.Beckmann@amd.com opNum = len(opTypes) - 1 1006899SN/A while len(opTypes): 1018322Ssteve.reinhardt@amd.com # print "Building a composite op with tags", opTypes 1026899SN/A # print "And code", code 1038322Ssteve.reinhardt@amd.com opNum = len(opTypes) - 1 1046899SN/A # A regular expression to find the operand placeholders we're 1056899SN/A # interested in. 1066899SN/A opRe = re.compile("\\^(?P<operandNum>%d)(?=[^0-9]|$)" % opNum) 1078845Sandreas.hansson@arm.com 1086899SN/A # Parse the operand type strign we're working with 1096899SN/A opType = OpType(opTypes[opNum]) 1106899SN/A 1116899SN/A if opType.reg: 1126899SN/A #Figure out what to do with fixed register operands 1138801Sgblack@eecs.umich.edu if opType.reg in ("Ax", "Bx", "Cx", "Dx"): 1146899SN/A code = opRe.sub("%%{INTREG_R%s}" % opType.reg.upper(), code) 1156899SN/A elif opType.reg == "Al": 1166899SN/A # We need a way to specify register width 1176899SN/A code = opRe.sub("%{INTREG_RAX}", code) 1186899SN/A else: 1196899SN/A print "Didn't know how to encode fixed register %s!" % opType.reg 1207525SN/A elif opType.tag == None or opType.size == None: 1216899SN/A raise Exception, "Problem parsing operand tag: %s" % opType.tag 1226899SN/A elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"): 1236899SN/A # Use the "reg" field of the ModRM byte to select the register 1246899SN/A code = opRe.sub("%{(uint8_t)MODRM_REG}", code) 1256899SN/A elif opType.tag in ("E", "Q", "W"): 126 # This might refer to memory or to a register. We need to 127 # divide it up farther. 128 regCode = opRe.sub("%{(uint8_t)MODRM_RM}", code) 129 regTypes = copy.copy(opTypes) 130 regTypes.pop(-1) 131 # This needs to refer to memory, but we'll fill in the details 132 # later. It needs to take into account unaligned memory 133 # addresses. 134 code = "GenFault ${new UnimpInstFault}\n" + code 135 memCode = opRe.sub("%0", code) 136 memTypes = copy.copy(opTypes) 137 memTypes.pop(-1) 138 return doSplitDecode(name, Name, specializeInst, "MODRM_MOD", 139 {"3" : (regCode, regTypes)}, (memCode, memTypes)) 140 elif opType.tag in ("I", "J"): 141 # Immediates are already in the instruction, so don't leave in 142 # those parameters 143 code = opRe.sub("${IMMEDIATE}", code) 144 elif opType.tag == "M": 145 # This needs to refer to memory, but we'll fill in the details 146 # later. It needs to take into account unaligned memory 147 # addresses. 148 code = "GenFault ${new UnimpInstFault}\n" + code 149 code = opRe.sub("%0", code) 150 elif opType.tag in ("PR", "R", "VR"): 151 # There should probably be a check here to verify that mod 152 # is equal to 11b 153 code = opRe.sub("%{(uint8_t)MODRM_RM}", code) 154 else: 155 raise Exception, "Unrecognized tag %s." % opType.tag 156 opTypes.pop(-1) 157 158 # At this point, we've built up "code" to have all the necessary extra 159 # instructions needed to implement whatever types of operands were 160 # specified. Now we'll assemble it it into a StaticInst. 161 blocks = OutputBlocks() 162 blocks.append(assembleMicro(name, Name, code)) 163 return blocks 164}}; 165