specialize.isa revision 4348:5c21bdb46e6d
111986Sandreas.sandberg@arm.com// -*- mode:c++ -*-
211986Sandreas.sandberg@arm.com
311986Sandreas.sandberg@arm.com// Copyright (c) 2007 The Hewlett-Packard Development Company
411986Sandreas.sandberg@arm.com// All rights reserved.
511986Sandreas.sandberg@arm.com//
611986Sandreas.sandberg@arm.com// Redistribution and use of this software in source and binary forms,
712037Sandreas.sandberg@arm.com// with or without modification, are permitted provided that the
812037Sandreas.sandberg@arm.com// following conditions are met:
911986Sandreas.sandberg@arm.com//
1011986Sandreas.sandberg@arm.com// The software must be used only for Non-Commercial Use which means any
1111986Sandreas.sandberg@arm.com// use which is NOT directed to receiving any direct monetary
1211986Sandreas.sandberg@arm.com// compensation for, or commercial advantage from such use.  Illustrative
1311986Sandreas.sandberg@arm.com// examples of non-commercial use are academic research, personal study,
1411986Sandreas.sandberg@arm.com// teaching, education and corporate research & development.
1511986Sandreas.sandberg@arm.com// Illustrative examples of commercial use are distributing products for
1611986Sandreas.sandberg@arm.com// commercial advantage and providing services using the software for
1711986Sandreas.sandberg@arm.com// commercial advantage.
1811986Sandreas.sandberg@arm.com//
1911986Sandreas.sandberg@arm.com// If you wish to use this software or functionality therein that may be
2011986Sandreas.sandberg@arm.com// covered by patents for commercial use, please contact:
2111986Sandreas.sandberg@arm.com//     Director of Intellectual Property Licensing
2211986Sandreas.sandberg@arm.com//     Office of Strategy and Technology
2311986Sandreas.sandberg@arm.com//     Hewlett-Packard Company
2411986Sandreas.sandberg@arm.com//     1501 Page Mill Road
2511986Sandreas.sandberg@arm.com//     Palo Alto, California  94304
2611986Sandreas.sandberg@arm.com//
2711986Sandreas.sandberg@arm.com// Redistributions of source code must retain the above copyright notice,
2811986Sandreas.sandberg@arm.com// this list of conditions and the following disclaimer.  Redistributions
2911986Sandreas.sandberg@arm.com// in binary form must reproduce the above copyright notice, this list of
3011986Sandreas.sandberg@arm.com// conditions and the following disclaimer in the documentation and/or
3111986Sandreas.sandberg@arm.com// other materials provided with the distribution.  Neither the name of
3211986Sandreas.sandberg@arm.com// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
3311986Sandreas.sandberg@arm.com// contributors may be used to endorse or promote products derived from
3411986Sandreas.sandberg@arm.com// this software without specific prior written permission.  No right of
3511986Sandreas.sandberg@arm.com// sublicense is granted herewith.  Derivatives of the software and
3611986Sandreas.sandberg@arm.com// output created using the software may be prepared, but only for
3711986Sandreas.sandberg@arm.com// Non-Commercial Uses.  Derivatives of the software may be shared with
3811986Sandreas.sandberg@arm.com// others provided: (i) the others agree to abide by the list of
3911986Sandreas.sandberg@arm.com// conditions herein which includes the Non-Commercial Use restrictions;
4011986Sandreas.sandberg@arm.com// and (ii) such Derivatives of the software include the above copyright
4111986Sandreas.sandberg@arm.com// notice to acknowledge the contribution from this software where
4211986Sandreas.sandberg@arm.com// applicable, this list of conditions and the disclaimer below.
4311986Sandreas.sandberg@arm.com//
4411986Sandreas.sandberg@arm.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4511986Sandreas.sandberg@arm.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4611986Sandreas.sandberg@arm.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4711986Sandreas.sandberg@arm.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4811986Sandreas.sandberg@arm.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4911986Sandreas.sandberg@arm.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5011986Sandreas.sandberg@arm.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5111986Sandreas.sandberg@arm.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5211986Sandreas.sandberg@arm.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5311986Sandreas.sandberg@arm.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5411986Sandreas.sandberg@arm.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5511986Sandreas.sandberg@arm.com//
5611986Sandreas.sandberg@arm.com// Authors: Gabe Black
5711986Sandreas.sandberg@arm.com
5811986Sandreas.sandberg@arm.com////////////////////////////////////////////////////////////////////
5911986Sandreas.sandberg@arm.com//
6011986Sandreas.sandberg@arm.com//  Code to "specialize" a microcode sequence to use a particular
6111986Sandreas.sandberg@arm.com//  variety of operands
6211986Sandreas.sandberg@arm.com//
6311986Sandreas.sandberg@arm.com
6411986Sandreas.sandberg@arm.comlet {{
6511986Sandreas.sandberg@arm.com    # This code builds up a decode block which decodes based on switchval.
6611986Sandreas.sandberg@arm.com    # vals is a dict which matches case values with what should be decoded to.
6711986Sandreas.sandberg@arm.com    # builder is called on the exploded contents of "vals" values to generate
6811986Sandreas.sandberg@arm.com    # whatever code should be used.
6911986Sandreas.sandberg@arm.com    def doSplitDecode(name, Name, builder, switchVal, vals, default = None):
7011986Sandreas.sandberg@arm.com        header_output = ''
7111986Sandreas.sandberg@arm.com        decoder_output = ''
7211986Sandreas.sandberg@arm.com        decode_block = 'switch(%s) {\n' % switchVal
7311986Sandreas.sandberg@arm.com        exec_output = ''
7411986Sandreas.sandberg@arm.com        for (val, todo) in vals.items():
7511986Sandreas.sandberg@arm.com            (new_header_output,
7611986Sandreas.sandberg@arm.com             new_decoder_output,
7711986Sandreas.sandberg@arm.com             new_decode_block,
7811986Sandreas.sandberg@arm.com             new_exec_output) = builder(name, Name, *todo)
7911986Sandreas.sandberg@arm.com            header_output += new_header_output
8011986Sandreas.sandberg@arm.com            decoder_output += new_decoder_output
8111986Sandreas.sandberg@arm.com            decode_block += '\tcase %s: %s\n' % (val, new_decode_block)
82            exec_output += new_exec_output
83        if default:
84            (new_header_output,
85             new_decoder_output,
86             new_decode_block,
87             new_exec_output) = builder(name, Name, *default)
88            header_output += new_header_output
89            decoder_output += new_decoder_output
90            decode_block += '\tdefault: %s\n' % new_decode_block
91            exec_output += new_exec_output
92        decode_block += '}\n'
93        return (header_output, decoder_output, decode_block, exec_output)
94}};
95
96let {{
97    class OpType(object):
98        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]*))")
99        def __init__(self, opTypeString):
100            match = OpType.parser.search(opTypeString)
101            if match == None:
102                raise Exception, "Problem parsing operand type %s" % opTypeString
103            self.reg = match.group("reg")
104            self.tag = match.group("tag")
105            self.size = match.group("size")
106
107    # This function specializes the given piece of code to use a particular
108    # set of argument types described by "opTypes". These are "implemented"
109    # in reverse order.
110    def specializeInst(name, Name, code, opTypes):
111        opNum = len(opTypes) - 1
112        while len(opTypes):
113            # print "Building a composite op with tags", opTypes
114            # print "And code", code
115            opNum = len(opTypes) - 1
116            # A regular expression to find the operand placeholders we're
117            # interested in.
118            opRe = re.compile("\\^(?P<operandNum>%d)(?=[^0-9]|$)" % opNum)
119
120            # Parse the operand type strign we're working with
121            opType = OpType(opTypes[opNum])
122
123            if opType.reg:
124                #Figure out what to do with fixed register operands
125                if opType.reg in ("Ax", "Bx", "Cx", "Dx"):
126                    code = opRe.sub("%%{INTREG_R%s}" % opType.reg.upper(), code)
127                elif opType.reg == "Al":
128                    # We need a way to specify register width
129                    code = opRe.sub("%{INTREG_RAX}", code)
130                else:
131                    print "Didn't know how to encode fixed register %s!" % opType.reg
132            elif opType.tag == None or opType.size == None:
133                raise Exception, "Problem parsing operand tag: %s" % opType.tag
134            elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"):
135                # Use the "reg" field of the ModRM byte to select the register
136                code = opRe.sub("%{(uint8_t)MODRM_REG}", code)
137            elif opType.tag in ("E", "Q", "W"):
138                # This might refer to memory or to a register. We need to
139                # divide it up farther.
140                regCode = opRe.sub("%{(uint8_t)MODRM_RM}", code)
141                regTypes = copy.copy(opTypes)
142                regTypes.pop(-1)
143                # This needs to refer to memory, but we'll fill in the details
144                # later. It needs to take into account unaligned memory
145                # addresses.
146                memCode = opRe.sub("%0", code)
147                memTypes = copy.copy(opTypes)
148                memTypes.pop(-1)
149                return doSplitDecode(name, Name, specializeInst, "MODRM_MOD",
150                    {"3" : (regCode, regTypes)}, (memCode, memTypes))
151            elif opType.tag in ("I", "J"):
152                # Immediates are already in the instruction, so don't leave in
153                # those parameters
154                code = opRe.sub("${IMMEDIATE}", code)
155            elif opType.tag == "M":
156                # This needs to refer to memory, but we'll fill in the details
157                # later. It needs to take into account unaligned memory
158                # addresses.
159                code = opRe.sub("%0", code)
160            elif opType.tag in ("PR", "R", "VR"):
161                # There should probably be a check here to verify that mod
162                # is equal to 11b
163                code = opRe.sub("%{(uint8_t)MODRM_RM}", code)
164            else:
165                raise Exception, "Unrecognized tag %s." % opType.tag
166            opTypes.pop(-1)
167
168        # At this point, we've built up "code" to have all the necessary extra
169        # instructions needed to implement whatever types of operands were
170        # specified. Now we'll assemble it it into a StaticInst.
171        return assembleMicro(name, Name, code)
172}};
173