microasm.isa revision 4336
14309Sgblack@eecs.umich.edu// -*- mode:c++ -*-
24309Sgblack@eecs.umich.edu
34309Sgblack@eecs.umich.edu// Copyright (c) 2007 The Hewlett-Packard Development Company
44309Sgblack@eecs.umich.edu// All rights reserved.
54309Sgblack@eecs.umich.edu//
64309Sgblack@eecs.umich.edu// Redistribution and use of this software in source and binary forms,
74309Sgblack@eecs.umich.edu// with or without modification, are permitted provided that the
84309Sgblack@eecs.umich.edu// following conditions are met:
94309Sgblack@eecs.umich.edu//
104309Sgblack@eecs.umich.edu// The software must be used only for Non-Commercial Use which means any
114309Sgblack@eecs.umich.edu// use which is NOT directed to receiving any direct monetary
124309Sgblack@eecs.umich.edu// compensation for, or commercial advantage from such use.  Illustrative
134309Sgblack@eecs.umich.edu// examples of non-commercial use are academic research, personal study,
144309Sgblack@eecs.umich.edu// teaching, education and corporate research & development.
154309Sgblack@eecs.umich.edu// Illustrative examples of commercial use are distributing products for
164309Sgblack@eecs.umich.edu// commercial advantage and providing services using the software for
174309Sgblack@eecs.umich.edu// commercial advantage.
184309Sgblack@eecs.umich.edu//
194309Sgblack@eecs.umich.edu// If you wish to use this software or functionality therein that may be
204309Sgblack@eecs.umich.edu// covered by patents for commercial use, please contact:
214309Sgblack@eecs.umich.edu//     Director of Intellectual Property Licensing
224309Sgblack@eecs.umich.edu//     Office of Strategy and Technology
234309Sgblack@eecs.umich.edu//     Hewlett-Packard Company
244309Sgblack@eecs.umich.edu//     1501 Page Mill Road
254309Sgblack@eecs.umich.edu//     Palo Alto, California  94304
264309Sgblack@eecs.umich.edu//
274309Sgblack@eecs.umich.edu// Redistributions of source code must retain the above copyright notice,
284309Sgblack@eecs.umich.edu// this list of conditions and the following disclaimer.  Redistributions
294309Sgblack@eecs.umich.edu// in binary form must reproduce the above copyright notice, this list of
304309Sgblack@eecs.umich.edu// conditions and the following disclaimer in the documentation and/or
314309Sgblack@eecs.umich.edu// other materials provided with the distribution.  Neither the name of
324309Sgblack@eecs.umich.edu// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
334309Sgblack@eecs.umich.edu// contributors may be used to endorse or promote products derived from
344309Sgblack@eecs.umich.edu// this software without specific prior written permission.  No right of
354309Sgblack@eecs.umich.edu// sublicense is granted herewith.  Derivatives of the software and
364309Sgblack@eecs.umich.edu// output created using the software may be prepared, but only for
374309Sgblack@eecs.umich.edu// Non-Commercial Uses.  Derivatives of the software may be shared with
384309Sgblack@eecs.umich.edu// others provided: (i) the others agree to abide by the list of
394309Sgblack@eecs.umich.edu// conditions herein which includes the Non-Commercial Use restrictions;
404309Sgblack@eecs.umich.edu// and (ii) such Derivatives of the software include the above copyright
414309Sgblack@eecs.umich.edu// notice to acknowledge the contribution from this software where
424309Sgblack@eecs.umich.edu// applicable, this list of conditions and the disclaimer below.
434309Sgblack@eecs.umich.edu//
444309Sgblack@eecs.umich.edu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
454309Sgblack@eecs.umich.edu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
464309Sgblack@eecs.umich.edu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
474309Sgblack@eecs.umich.edu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
484309Sgblack@eecs.umich.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
494309Sgblack@eecs.umich.edu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
504309Sgblack@eecs.umich.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
514309Sgblack@eecs.umich.edu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
524309Sgblack@eecs.umich.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
534309Sgblack@eecs.umich.edu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
544309Sgblack@eecs.umich.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
554309Sgblack@eecs.umich.edu//
564309Sgblack@eecs.umich.edu// Authors: Gabe Black
574309Sgblack@eecs.umich.edu
584309Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////
594309Sgblack@eecs.umich.edu//
604336Sgblack@eecs.umich.edu//  Code to "specialize" a microcode sequence to use a particular
614336Sgblack@eecs.umich.edu//  variety of operands
624309Sgblack@eecs.umich.edu//
634309Sgblack@eecs.umich.edu
644309Sgblack@eecs.umich.edulet {{
654336Sgblack@eecs.umich.edu    # This builds either a regular or macro op to implement the sequence of
664336Sgblack@eecs.umich.edu    # ops we give it.
674336Sgblack@eecs.umich.edu    def genInst(name, Name, ops):
684336Sgblack@eecs.umich.edu        # If we can implement this instruction with exactly one microop, just
694336Sgblack@eecs.umich.edu        # use that directly.
704336Sgblack@eecs.umich.edu        newStmnt = ''
714336Sgblack@eecs.umich.edu        if len(ops) == 1:
724336Sgblack@eecs.umich.edu            decode_block = "return (X86StaticInst *)(%s);" % \
734336Sgblack@eecs.umich.edu                            ops[0].getAllocator()
744336Sgblack@eecs.umich.edu            return ('', '', decode_block, '')
754336Sgblack@eecs.umich.edu        else:
764336Sgblack@eecs.umich.edu            # Build a macroop to contain the sequence of microops we've
774336Sgblack@eecs.umich.edu            # been given.
784336Sgblack@eecs.umich.edu            return genMacroOp(name, Name, ops)
794336Sgblack@eecs.umich.edu}};
804336Sgblack@eecs.umich.edu
814336Sgblack@eecs.umich.edulet {{
824336Sgblack@eecs.umich.edu    # This code builds up a decode block which decodes based on switchval.
834336Sgblack@eecs.umich.edu    # vals is a dict which matches case values with what should be decoded to.
844336Sgblack@eecs.umich.edu    # builder is called on the exploded contents of "vals" values to generate
854336Sgblack@eecs.umich.edu    # whatever code should be used.
864336Sgblack@eecs.umich.edu    def doSplitDecode(name, Name, builder, switchVal, vals, default = None):
874336Sgblack@eecs.umich.edu        header_output = ''
884336Sgblack@eecs.umich.edu        decoder_output = ''
894336Sgblack@eecs.umich.edu        decode_block = 'switch(%s) {\n' % switchVal
904336Sgblack@eecs.umich.edu        exec_output = ''
914336Sgblack@eecs.umich.edu        for (val, todo) in vals.items():
924336Sgblack@eecs.umich.edu            (new_header_output,
934336Sgblack@eecs.umich.edu             new_decoder_output,
944336Sgblack@eecs.umich.edu             new_decode_block,
954336Sgblack@eecs.umich.edu             new_exec_output) = builder(name, Name, *todo)
964336Sgblack@eecs.umich.edu            header_output += new_header_output
974336Sgblack@eecs.umich.edu            decoder_output += new_decoder_output
984336Sgblack@eecs.umich.edu            decode_block += '\tcase %s: %s\n' % (val, new_decode_block)
994336Sgblack@eecs.umich.edu            exec_output += new_exec_output
1004336Sgblack@eecs.umich.edu        if default:
1014336Sgblack@eecs.umich.edu            (new_header_output,
1024336Sgblack@eecs.umich.edu             new_decoder_output,
1034336Sgblack@eecs.umich.edu             new_decode_block,
1044336Sgblack@eecs.umich.edu             new_exec_output) = builder(name, Name, *default)
1054336Sgblack@eecs.umich.edu            header_output += new_header_output
1064336Sgblack@eecs.umich.edu            decoder_output += new_decoder_output
1074336Sgblack@eecs.umich.edu            decode_block += '\tdefault: %s\n' % new_decode_block
1084336Sgblack@eecs.umich.edu            exec_output += new_exec_output
1094336Sgblack@eecs.umich.edu        decode_block += '}\n'
1104336Sgblack@eecs.umich.edu        return (header_output, decoder_output, decode_block, exec_output)
1114336Sgblack@eecs.umich.edu}};
1124336Sgblack@eecs.umich.edu
1134336Sgblack@eecs.umich.edulet {{
1144336Sgblack@eecs.umich.edu    class OpType(object):
1154336Sgblack@eecs.umich.edu        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]*))")
1164336Sgblack@eecs.umich.edu        def __init__(self, opTypeString):
1174336Sgblack@eecs.umich.edu            match = OpType.parser.search(opTypeString)
1184336Sgblack@eecs.umich.edu            if match == None:
1194336Sgblack@eecs.umich.edu                raise Exception, "Problem parsing operand type %s" % opTypeString
1204336Sgblack@eecs.umich.edu            self.reg = match.group("reg")
1214336Sgblack@eecs.umich.edu            self.tag = match.group("tag")
1224336Sgblack@eecs.umich.edu            self.size = match.group("size")
1234336Sgblack@eecs.umich.edu}};
1244336Sgblack@eecs.umich.edu
1254336Sgblack@eecs.umich.edulet {{
1264336Sgblack@eecs.umich.edu
1274336Sgblack@eecs.umich.edu    # This function specializes the given piece of code to use a particular
1284336Sgblack@eecs.umich.edu    # set of argument types described by "opTypes". These are "implemented"
1294336Sgblack@eecs.umich.edu    # in reverse order.
1304336Sgblack@eecs.umich.edu    def specializeInst(name, Name, code, opTypes):
1314336Sgblack@eecs.umich.edu        opNum = len(opTypes) - 1
1324336Sgblack@eecs.umich.edu        while len(opTypes):
1334336Sgblack@eecs.umich.edu            # print "Building a composite op with tags", opTypes
1344336Sgblack@eecs.umich.edu            # print "And code", code
1354336Sgblack@eecs.umich.edu            opNum = len(opTypes) - 1
1364336Sgblack@eecs.umich.edu            # A regular expression to find the operand placeholders we're
1374336Sgblack@eecs.umich.edu            # interested in.
1384336Sgblack@eecs.umich.edu            opRe = re.compile("%%(?P<operandNum>%d)(?=[^0-9]|$)" % opNum)
1394336Sgblack@eecs.umich.edu
1404336Sgblack@eecs.umich.edu            # Parse the operand type strign we're working with
1414336Sgblack@eecs.umich.edu            print "About to parse tag %s" % opTypes[opNum]
1424336Sgblack@eecs.umich.edu            opType = OpType(opTypes[opNum])
1434336Sgblack@eecs.umich.edu
1444336Sgblack@eecs.umich.edu            if opType.reg:
1454336Sgblack@eecs.umich.edu                #Figure out what to do with fixed register operands
1464336Sgblack@eecs.umich.edu                if opType.reg in ("Ax", "Bx", "Cx", "Dx"):
1474336Sgblack@eecs.umich.edu                    code = opRe.sub("{INTREG_R%s}" % opType.reg.upper(), code)
1484336Sgblack@eecs.umich.edu                elif opType.reg == "Al":
1494336Sgblack@eecs.umich.edu                    # We need a way to specify register width
1504336Sgblack@eecs.umich.edu                    code = opRe.sub("{INTREG_RAX}", code)
1514336Sgblack@eecs.umich.edu                else:
1524336Sgblack@eecs.umich.edu                    print "Didn't know how to encode fixed register %s!" % opType.reg
1534336Sgblack@eecs.umich.edu            elif opType.tag == None or opType.size == None:
1544336Sgblack@eecs.umich.edu                raise Exception, "Problem parsing operand tag: %s" % opType.tag
1554336Sgblack@eecs.umich.edu            elif opType.tag in ("C", "D", "G", "P", "S", "T", "V"):
1564336Sgblack@eecs.umich.edu                # Use the "reg" field of the ModRM byte to select the register
1574336Sgblack@eecs.umich.edu                code = opRe.sub("{(uint8_t)MODRM_REG}", code)
1584336Sgblack@eecs.umich.edu            elif opType.tag in ("E", "Q", "W"):
1594336Sgblack@eecs.umich.edu                # This might refer to memory or to a register. We need to
1604336Sgblack@eecs.umich.edu                # divide it up farther.
1614336Sgblack@eecs.umich.edu                regCode = opRe.sub("{(uint8_t)MODRM_RM}", code)
1624336Sgblack@eecs.umich.edu                regTypes = copy.copy(opTypes)
1634336Sgblack@eecs.umich.edu                regTypes.pop(-1)
1644336Sgblack@eecs.umich.edu                # This needs to refer to memory, but we'll fill in the details
1654336Sgblack@eecs.umich.edu                # later. It needs to take into account unaligned memory
1664336Sgblack@eecs.umich.edu                # addresses.
1674336Sgblack@eecs.umich.edu                memCode = opRe.sub("0", code)
1684336Sgblack@eecs.umich.edu                memTypes = copy.copy(opTypes)
1694336Sgblack@eecs.umich.edu                memTypes.pop(-1)
1704336Sgblack@eecs.umich.edu                return doSplitDecode(name, Name, specializeInst, "MODRM_MOD",
1714336Sgblack@eecs.umich.edu                    {"3" : (regCode, regTypes)}, (memCode, memTypes))
1724336Sgblack@eecs.umich.edu            elif opType.tag in ("I", "J"):
1734336Sgblack@eecs.umich.edu                # Immediates are already in the instruction, so don't leave in
1744336Sgblack@eecs.umich.edu                # those parameters
1754336Sgblack@eecs.umich.edu                code = opRe.sub("", code)
1764336Sgblack@eecs.umich.edu            elif opType.tag == "M":
1774336Sgblack@eecs.umich.edu                # This needs to refer to memory, but we'll fill in the details
1784336Sgblack@eecs.umich.edu                # later. It needs to take into account unaligned memory
1794336Sgblack@eecs.umich.edu                # addresses.
1804336Sgblack@eecs.umich.edu                code = opRe.sub("0", code)
1814336Sgblack@eecs.umich.edu            elif opType.tag in ("PR", "R", "VR"):
1824336Sgblack@eecs.umich.edu                # There should probably be a check here to verify that mod
1834336Sgblack@eecs.umich.edu                # is equal to 11b
1844336Sgblack@eecs.umich.edu                code = opRe.sub("{(uint8_t)MODRM_RM}", code)
1854336Sgblack@eecs.umich.edu            else:
1864336Sgblack@eecs.umich.edu                raise Exception, "Unrecognized tag %s." % opType.tag
1874336Sgblack@eecs.umich.edu            opTypes.pop(-1)
1884336Sgblack@eecs.umich.edu
1894336Sgblack@eecs.umich.edu        # At this point, we've built up "code" to have all the necessary extra
1904336Sgblack@eecs.umich.edu        # instructions needed to implement whatever types of operands were
1914336Sgblack@eecs.umich.edu        # specified. Now we'll assemble it it into a microOp sequence.
1924336Sgblack@eecs.umich.edu        ops = assembleMicro(code)
1934336Sgblack@eecs.umich.edu
1944336Sgblack@eecs.umich.edu        # Build a macroop to contain the sequence of microops we've
1954336Sgblack@eecs.umich.edu        # constructed. The decode block will be used to fill in our
1964336Sgblack@eecs.umich.edu        # inner decode structure, and the rest will be concatenated and
1974336Sgblack@eecs.umich.edu        # passed back.
1984336Sgblack@eecs.umich.edu        return genInst(name, Name, ops)
1994336Sgblack@eecs.umich.edu}};
2004336Sgblack@eecs.umich.edu
2014336Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////
2024336Sgblack@eecs.umich.edu//
2034336Sgblack@eecs.umich.edu//  The microcode assembler
2044336Sgblack@eecs.umich.edu//
2054336Sgblack@eecs.umich.edu
2064336Sgblack@eecs.umich.edulet {{
2074336Sgblack@eecs.umich.edu    class MicroOpStatement(object):
2084309Sgblack@eecs.umich.edu        def __init__(self):
2094309Sgblack@eecs.umich.edu            self.className = ''
2104309Sgblack@eecs.umich.edu            self.label = ''
2114309Sgblack@eecs.umich.edu            self.args = []
2124309Sgblack@eecs.umich.edu
2134323Sgblack@eecs.umich.edu        # This converts a list of python bools into
2144323Sgblack@eecs.umich.edu        # a comma seperated list of C++ bools.
2154323Sgblack@eecs.umich.edu        def microFlagsText(self, vals):
2164323Sgblack@eecs.umich.edu            text = ""
2174323Sgblack@eecs.umich.edu            for val in vals:
2184323Sgblack@eecs.umich.edu                if val:
2194323Sgblack@eecs.umich.edu                    text += ", true"
2204323Sgblack@eecs.umich.edu                else:
2214323Sgblack@eecs.umich.edu                    text += ", false"
2224323Sgblack@eecs.umich.edu            return text
2234323Sgblack@eecs.umich.edu
2244323Sgblack@eecs.umich.edu        def getAllocator(self, *microFlags):
2254309Sgblack@eecs.umich.edu            args = ''
2264309Sgblack@eecs.umich.edu            for arg in self.args:
2274309Sgblack@eecs.umich.edu                if arg.has_key("operandConst"):
2284309Sgblack@eecs.umich.edu                    args += ", %s" % arg["operandConst"]
2294309Sgblack@eecs.umich.edu                elif arg.has_key("operandCode"):
2304309Sgblack@eecs.umich.edu                    args += ", %s" % arg["operandCode"]
2314309Sgblack@eecs.umich.edu                elif arg.has_key("operandLabel"):
2324323Sgblack@eecs.umich.edu                    raise Exception, "Found a label while creating allocator string."
2334309Sgblack@eecs.umich.edu                else:
2344323Sgblack@eecs.umich.edu                    raise Exception, "Unrecognized operand type."
2354323Sgblack@eecs.umich.edu            return 'new %s(machInst%s%s)' % (self.className, self.microFlagsText(microFlags), args)
2364323Sgblack@eecs.umich.edu}};
2374309Sgblack@eecs.umich.edu
2384323Sgblack@eecs.umich.edulet {{
2394323Sgblack@eecs.umich.edu    def buildLabelDict(ops):
2404323Sgblack@eecs.umich.edu        labels = {}
2414323Sgblack@eecs.umich.edu        micropc = 0
2424323Sgblack@eecs.umich.edu        for op in ops:
2434323Sgblack@eecs.umich.edu            if op.label:
2444323Sgblack@eecs.umich.edu                labels[op.label] = count
2454323Sgblack@eecs.umich.edu            micropc += 1
2464323Sgblack@eecs.umich.edu        return labels
2474336Sgblack@eecs.umich.edu}};
2484309Sgblack@eecs.umich.edu
2494336Sgblack@eecs.umich.edulet{{
2504309Sgblack@eecs.umich.edu    def assembleMicro(code):
2514309Sgblack@eecs.umich.edu        # This function takes in a block of microcode assembly and returns
2524309Sgblack@eecs.umich.edu        # a python list of objects which describe it.
2534309Sgblack@eecs.umich.edu
2544309Sgblack@eecs.umich.edu        # Keep this around in case we need it later
2554309Sgblack@eecs.umich.edu        orig_code = code
2564309Sgblack@eecs.umich.edu        # A list of the statements we've found thus far
2574309Sgblack@eecs.umich.edu        statements = []
2584309Sgblack@eecs.umich.edu
2594309Sgblack@eecs.umich.edu        # Regular expressions to pull each piece of the statement out at a
2604309Sgblack@eecs.umich.edu        # time. Each expression expects the thing it's looking for to be at
2614309Sgblack@eecs.umich.edu        # the beginning of the line, so the previous component is stripped
2624309Sgblack@eecs.umich.edu        # before continuing.
2634309Sgblack@eecs.umich.edu        labelRe = re.compile(r'^[ \t]*(?P<label>[a-zA-Z_]\w*)[ \t]:')
2644309Sgblack@eecs.umich.edu        lineRe = re.compile(r'^(?P<line>[^\n][^\n]*)$')
2654309Sgblack@eecs.umich.edu        classRe = re.compile(r'^[ \t]*(?P<className>[a-zA-Z_]\w*)')
2664309Sgblack@eecs.umich.edu        # This recognizes three different flavors of operands:
2674309Sgblack@eecs.umich.edu        # 1. Raw decimal numbers composed of digits between 0 and 9
2684309Sgblack@eecs.umich.edu        # 2. Code beginning with "{" and continuing until the first "}"
2694309Sgblack@eecs.umich.edu        #         ^ This one might need revising
2704309Sgblack@eecs.umich.edu        # 3. A label, which starts with a capital or small letter, or
2714309Sgblack@eecs.umich.edu        #    underscore, which is optionally followed by a sequence of
2724309Sgblack@eecs.umich.edu        #    capital or small letters, underscores, or digts between 0 and 9
2734309Sgblack@eecs.umich.edu        opRe = re.compile( \
2744309Sgblack@eecs.umich.edu            r'^[ \t]*((?P<operandLabel>[a-zA-Z_]\w*)|(?P<operandConst>[0-9][0-9]*)|(\{(?P<operandCode>[^}]*)\}))')
2754309Sgblack@eecs.umich.edu        lineMatch = lineRe.search(code)
2764309Sgblack@eecs.umich.edu        while lineMatch != None:
2774309Sgblack@eecs.umich.edu            statement = MicroOpStatement()
2784309Sgblack@eecs.umich.edu            # Get a line and seperate it from the rest of the code
2794309Sgblack@eecs.umich.edu            line = lineMatch.group("line")
2804323Sgblack@eecs.umich.edu            orig_line = line
2814323Sgblack@eecs.umich.edu            # print "Parsing line %s" % line
2824309Sgblack@eecs.umich.edu            code = lineRe.sub('', code, 1)
2834309Sgblack@eecs.umich.edu
2844309Sgblack@eecs.umich.edu            # Find the label, if any
2854309Sgblack@eecs.umich.edu            labelMatch = labelRe.search(line)
2864309Sgblack@eecs.umich.edu            if labelMatch != None:
2874309Sgblack@eecs.umich.edu                statement.label = labelMatch.group("label")
2884323Sgblack@eecs.umich.edu                # print "Found label %s." % statement.label
2894309Sgblack@eecs.umich.edu            # Clear the label from the statement
2904309Sgblack@eecs.umich.edu            line = labelRe.sub('', line, 1)
2914309Sgblack@eecs.umich.edu
2924309Sgblack@eecs.umich.edu            # Find the class name which is roughly equivalent to the op name
2934309Sgblack@eecs.umich.edu            classMatch = classRe.search(line)
2944309Sgblack@eecs.umich.edu            if classMatch == None:
2954323Sgblack@eecs.umich.edu                raise Exception, "Couldn't find class name in statement: %s" \
2964323Sgblack@eecs.umich.edu                        % orig_line
2974309Sgblack@eecs.umich.edu            else:
2984309Sgblack@eecs.umich.edu                statement.className = classMatch.group("className")
2994323Sgblack@eecs.umich.edu                # print "Found class name %s." % statement.className
3004309Sgblack@eecs.umich.edu
3014309Sgblack@eecs.umich.edu            # Clear the class name from the statement
3024309Sgblack@eecs.umich.edu            line = classRe.sub('', line, 1)
3034309Sgblack@eecs.umich.edu
3044309Sgblack@eecs.umich.edu            #Find as many arguments as you can
3054309Sgblack@eecs.umich.edu            statement.args = []
3064309Sgblack@eecs.umich.edu            opMatch = opRe.search(line)
3074309Sgblack@eecs.umich.edu            while opMatch is not None:
3084309Sgblack@eecs.umich.edu                statement.args.append({})
3094309Sgblack@eecs.umich.edu                # args is a list of dicts which collect different
3104309Sgblack@eecs.umich.edu                # representations of operand values. Different forms might be
3114309Sgblack@eecs.umich.edu                # needed in different places, for instance to replace a label
3124309Sgblack@eecs.umich.edu                # with an offset.
3134309Sgblack@eecs.umich.edu                for opType in ("operandLabel", "operandConst", "operandCode"):
3144309Sgblack@eecs.umich.edu                    if opMatch.group(opType):
3154309Sgblack@eecs.umich.edu                        statement.args[-1][opType] = opMatch.group(opType)
3164309Sgblack@eecs.umich.edu                if len(statement.args[-1]) == 0:
3174323Sgblack@eecs.umich.edu                    print "Problem parsing operand in statement: %s" \
3184323Sgblack@eecs.umich.edu                            % orig_line
3194309Sgblack@eecs.umich.edu                line = opRe.sub('', line, 1)
3204323Sgblack@eecs.umich.edu                # print "Found operand %s." % statement.args[-1]
3214309Sgblack@eecs.umich.edu                opMatch = opRe.search(line)
3224323Sgblack@eecs.umich.edu            # print "Found operands", statement.args
3234309Sgblack@eecs.umich.edu
3244309Sgblack@eecs.umich.edu            # Add this statement to our collection
3254309Sgblack@eecs.umich.edu            statements.append(statement)
3264309Sgblack@eecs.umich.edu
3274309Sgblack@eecs.umich.edu            # Get the next line
3284309Sgblack@eecs.umich.edu            lineMatch = lineRe.search(code)
3294323Sgblack@eecs.umich.edu
3304323Sgblack@eecs.umich.edu        # Decode the labels into displacements
3314323Sgblack@eecs.umich.edu        labels = buildLabelDict(statements)
3324323Sgblack@eecs.umich.edu        micropc = 0
3334323Sgblack@eecs.umich.edu        for statement in statements:
3344323Sgblack@eecs.umich.edu            for arg in statement.args:
3354323Sgblack@eecs.umich.edu                if arg.has_key("operandLabel"):
3364323Sgblack@eecs.umich.edu                    if not labels.has_key(arg["operandLabel"]):
3374323Sgblack@eecs.umich.edu                        raise Exception, "Unrecognized label: %s." % arg["operandLabel"]
3384323Sgblack@eecs.umich.edu                    # This is assuming that intra microcode branches go to
3394323Sgblack@eecs.umich.edu                    # the next micropc + displacement, or
3404323Sgblack@eecs.umich.edu                    # micropc + 1 + displacement.
3414323Sgblack@eecs.umich.edu                    arg["operandConst"] = labels[arg["operandLabel"]] - micropc - 1
3424323Sgblack@eecs.umich.edu            micropc += 1
3434309Sgblack@eecs.umich.edu        return statements
3444309Sgblack@eecs.umich.edu}};
345