multi.isa revision 4323
1// -*- mode:c++ -*-
2
3// Copyright (c) 2007 The Hewlett-Packard Development Company
4// All rights reserved.
5//
6// Redistribution and use of this software in source and binary forms,
7// with or without modification, are permitted provided that the
8// following conditions are met:
9//
10// The software must be used only for Non-Commercial Use which means any
11// use which is NOT directed to receiving any direct monetary
12// compensation for, or commercial advantage from such use.  Illustrative
13// examples of non-commercial use are academic research, personal study,
14// teaching, education and corporate research & development.
15// Illustrative examples of commercial use are distributing products for
16// commercial advantage and providing services using the software for
17// commercial advantage.
18//
19// If you wish to use this software or functionality therein that may be
20// covered by patents for commercial use, please contact:
21//     Director of Intellectual Property Licensing
22//     Office of Strategy and Technology
23//     Hewlett-Packard Company
24//     1501 Page Mill Road
25//     Palo Alto, California  94304
26//
27// Redistributions of source code must retain the above copyright notice,
28// this list of conditions and the following disclaimer.  Redistributions
29// in binary form must reproduce the above copyright notice, this list of
30// conditions and the following disclaimer in the documentation and/or
31// other materials provided with the distribution.  Neither the name of
32// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
33// contributors may be used to endorse or promote products derived from
34// this software without specific prior written permission.  No right of
35// sublicense is granted herewith.  Derivatives of the software and
36// output created using the software may be prepared, but only for
37// Non-Commercial Uses.  Derivatives of the software may be shared with
38// others provided: (i) the others agree to abide by the list of
39// conditions herein which includes the Non-Commercial Use restrictions;
40// and (ii) such Derivatives of the software include the above copyright
41// notice to acknowledge the contribution from this software where
42// applicable, this list of conditions and the disclaimer below.
43//
44// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55//
56// Authors: Gabe Black
57
58////////////////////////////////////////////////////////////////////
59//
60// Instructions that do the same thing to multiple sets of arguments.
61//
62
63let {{
64    # This builds either a regular or macro op to implement the sequence of
65    # ops we give it.
66    def genInst(name, Name, ops):
67        # If we can implement this instruction with exactly one microop, just
68        # use that directly.
69        newStmnt = ''
70        if len(ops) == 1:
71            decode_block = "return (X86StaticInst *)(%s);" % \
72                            ops[0].getAllocator()
73            return ('', '', decode_block, '')
74        else:
75            # Build a macroop to contain the sequence of microops we've
76            # been given.
77            return genMacroOp(name, Name, ops)
78}};
79
80let {{
81    # This code builds up a decode block which decodes based on switchval.
82    # vals is a dict which matches case values with what should be decoded to.
83    # builder is called on the exploded contents of "vals" values to generate
84    # whatever code should be used.
85    def doMultiOp(name, Name, builder, switchVal, vals, default = None):
86        header_output = ''
87        decoder_output = ''
88        decode_block = 'switch(%s) {\n' % switchVal
89        exec_output = ''
90        for (val, todo) in vals.items():
91            (new_header_output,
92             new_decoder_output,
93             new_decode_block,
94             new_exec_output) = builder(name, Name, *todo)
95            header_output += new_header_output
96            decoder_output += new_decoder_output
97            decode_block += '\tcase %s: %s\n' % (val, new_decode_block)
98            exec_output += new_exec_output
99        if default:
100            (new_header_output,
101             new_decoder_output,
102             new_decode_block,
103             new_exec_output) = builder(name, Name, *default)
104            header_output += new_header_output
105            decoder_output += new_decoder_output
106            decode_block += '\tdefault: %s\n' % new_decode_block
107            exec_output += new_exec_output
108        decode_block += '}\n'
109        return (header_output, decoder_output, decode_block, exec_output)
110}};
111
112let {{
113
114    # This function specializes the given piece of code to use a particular
115    # set of argument types described by "opTags". These are "implemented"
116    # in reverse order.
117    def doCompOps(name, Name, code, opTags, postfix):
118        opNum = len(opTags) - 1
119        while len(opTags):
120            # print "Building a composite op with tags", opTags
121            # print "And code", code
122            opNum = len(opTags) - 1
123            # A regular expression to find the operand placeholders we're
124            # interested in.
125            opRe = re.compile("%%(?P<operandNum>%d)(?=[^0-9]|$)" % opNum)
126            tag = opTags[opNum]
127            # Build up a name for this instructions class using the argument
128            # types. Each variation will get its own name this way.
129            postfix = '_' + tag + postfix
130            tagParser = re.compile(r"(?P<tagType>[A-Z][A-Z]*)(?P<tagSize>[a-z][a-z]*)|(r(?P<tagReg>[A-Za-z0-9][A-Za-z0-9]*))")
131            tagMatch = tagParser.search(tag)
132            if tagMatch == None:
133                raise Exception, "Problem parsing operand tag %s" % tag
134            reg = tagMatch.group("tagReg")
135            tagType = tagMatch.group("tagType")
136            tagSize = tagMatch.group("tagSize")
137            if reg:
138                #Figure out what to do with fixed register operands
139                if reg in ("Ax", "Bx", "Cx", "Dx"):
140                    code = opRe.sub("{INTREG_R%s}" % reg.upper(), code)
141                elif reg == "Al":
142                    # We need a way to specify register width
143                    code = opRe.sub("{INTREG_RAX}", code)
144                else:
145                    print "Didn't know how to encode fixed register %s!" % reg
146            elif tagType == None or tagSize == None:
147                raise Exception, "Problem parsing operand tag: %s" % tag
148            elif tagType == "C" or tagType == "D" or tagType == "G" or \
149                                   tagType == "P" or tagType == "S" or \
150                                   tagType == "T" or tagType == "V":
151                # Use the "reg" field of the ModRM byte to select the register
152                code = opRe.sub("{(uint8_t)MODRM_REG}", code)
153            elif tagType == "E" or tagType == "Q" or tagType == "W":
154                # This might refer to memory or to a register. We need to
155                # divide it up farther.
156                regCode = opRe.sub("{(uint8_t)MODRM_RM}", code)
157                regTags = copy.copy(opTags)
158                regTags.pop(-1)
159                # This needs to refer to memory, but we'll fill in the details
160                # later. It needs to take into account unaligned memory
161                # addresses.
162                memCode = opRe.sub("0", code)
163                memTags = copy.copy(opTags)
164                memTags.pop(-1)
165                return doMultiOp(name, Name, doCompOps, "MODRM_MOD",
166                    {"3" : (regCode, regTags, postfix)},
167                           (memCode, memTags, postfix))
168            elif tagType == "I" or tagType == "J":
169                # Substitute in an immediate
170                code = opRe.sub("{IMMEDIATE}", code)
171            elif tagType == "M":
172                # This needs to refer to memory, but we'll fill in the details
173                # later. It needs to take into account unaligned memory
174                # addresses.
175                code = opRe.sub("0", code)
176            elif tagType == "PR" or tagType == "R" or tagType == "VR":
177                # There should probably be a check here to verify that mod
178                # is equal to 11b
179                code = opRe.sub("{(uint8_t)MODRM_RM}", code)
180            else:
181                raise Exception, "Unrecognized tag %s." % tag
182            opTags.pop(-1)
183
184        # At this point, we've built up "code" to have all the necessary extra
185        # instructions needed to implement whatever types of operands were
186        # specified. Now we'll assemble it it into a microOp sequence.
187        ops = assembleMicro(code)
188
189        # Build a macroop to contain the sequence of microops we've
190        # constructed. The decode block will be used to fill in our
191        # inner decode structure, and the rest will be concatenated and
192        # passed back.
193        return genInst(name, Name + postfix, ops)
194}};
195
196def format TaggedOp(code, tagSet) {{
197    (header_output,
198     decoder_output,
199     decode_block,
200     exec_output) = doCompOps(name, Name, code, tagSet, '')
201}};
202
203def format MultiOp(code, switchVal, opTags, *opt_flags) {{
204    switcher = {}
205    for (count, tagSet) in zip(xrange(len(opTags) - 1), opTags):
206        switcher[count] = (code, tagSet, '')
207    (header_output,
208     decoder_output,
209     decode_block,
210     exec_output) = doMultiOp(name, Name, doCompOps, switchVal, switcher)
211}};
212