isa_parser.py revision 11163:c81401cf5cc2
1# Copyright (c) 2014 ARM Limited
2# All rights reserved
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Copyright (c) 2003-2005 The Regents of The University of Michigan
14# Copyright (c) 2013,2015 Advanced Micro Devices, Inc.
15# All rights reserved.
16#
17# Redistribution and use in source and binary forms, with or without
18# modification, are permitted provided that the following conditions are
19# met: redistributions of source code must retain the above copyright
20# notice, this list of conditions and the following disclaimer;
21# redistributions in binary form must reproduce the above copyright
22# notice, this list of conditions and the following disclaimer in the
23# documentation and/or other materials provided with the distribution;
24# neither the name of the copyright holders nor the names of its
25# contributors may be used to endorse or promote products derived from
26# this software without specific prior written permission.
27#
28# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39#
40# Authors: Steve Reinhardt
41
42from __future__ import with_statement
43import os
44import sys
45import re
46import string
47import inspect, traceback
48# get type names
49from types import *
50
51from m5.util.grammar import Grammar
52
53debug=False
54
55###################
56# Utility functions
57
58#
59# Indent every line in string 's' by two spaces
60# (except preprocessor directives).
61# Used to make nested code blocks look pretty.
62#
63def indent(s):
64    return re.sub(r'(?m)^(?!#)', '  ', s)
65
66#
67# Munge a somewhat arbitrarily formatted piece of Python code
68# (e.g. from a format 'let' block) into something whose indentation
69# will get by the Python parser.
70#
71# The two keys here are that Python will give a syntax error if
72# there's any whitespace at the beginning of the first line, and that
73# all lines at the same lexical nesting level must have identical
74# indentation.  Unfortunately the way code literals work, an entire
75# let block tends to have some initial indentation.  Rather than
76# trying to figure out what that is and strip it off, we prepend 'if
77# 1:' to make the let code the nested block inside the if (and have
78# the parser automatically deal with the indentation for us).
79#
80# We don't want to do this if (1) the code block is empty or (2) the
81# first line of the block doesn't have any whitespace at the front.
82
83def fixPythonIndentation(s):
84    # get rid of blank lines first
85    s = re.sub(r'(?m)^\s*\n', '', s);
86    if (s != '' and re.match(r'[ \t]', s[0])):
87        s = 'if 1:\n' + s
88    return s
89
90class ISAParserError(Exception):
91    """Exception class for parser errors"""
92    def __init__(self, first, second=None):
93        if second is None:
94            self.lineno = 0
95            self.string = first
96        else:
97            self.lineno = first
98            self.string = second
99
100    def __str__(self):
101        return self.string
102
103def error(*args):
104    raise ISAParserError(*args)
105
106####################
107# Template objects.
108#
109# Template objects are format strings that allow substitution from
110# the attribute spaces of other objects (e.g. InstObjParams instances).
111
112labelRE = re.compile(r'(?<!%)%\(([^\)]+)\)[sd]')
113
114class Template(object):
115    def __init__(self, parser, t):
116        self.parser = parser
117        self.template = t
118
119    def subst(self, d):
120        myDict = None
121
122        # Protect non-Python-dict substitutions (e.g. if there's a printf
123        # in the templated C++ code)
124        template = self.parser.protectNonSubstPercents(self.template)
125        # CPU-model-specific substitutions are handled later (in GenCode).
126        template = self.parser.protectCpuSymbols(template)
127
128        # Build a dict ('myDict') to use for the template substitution.
129        # Start with the template namespace.  Make a copy since we're
130        # going to modify it.
131        myDict = self.parser.templateMap.copy()
132
133        if isinstance(d, InstObjParams):
134            # If we're dealing with an InstObjParams object, we need
135            # to be a little more sophisticated.  The instruction-wide
136            # parameters are already formed, but the parameters which
137            # are only function wide still need to be generated.
138            compositeCode = ''
139
140            myDict.update(d.__dict__)
141            # The "operands" and "snippets" attributes of the InstObjParams
142            # objects are for internal use and not substitution.
143            del myDict['operands']
144            del myDict['snippets']
145
146            snippetLabels = [l for l in labelRE.findall(template)
147                             if d.snippets.has_key(l)]
148
149            snippets = dict([(s, self.parser.mungeSnippet(d.snippets[s]))
150                             for s in snippetLabels])
151
152            myDict.update(snippets)
153
154            compositeCode = ' '.join(map(str, snippets.values()))
155
156            # Add in template itself in case it references any
157            # operands explicitly (like Mem)
158            compositeCode += ' ' + template
159
160            operands = SubOperandList(self.parser, compositeCode, d.operands)
161
162            myDict['op_decl'] = operands.concatAttrStrings('op_decl')
163            if operands.readPC or operands.setPC:
164                myDict['op_decl'] += 'TheISA::PCState __parserAutoPCState;\n'
165
166            # In case there are predicated register reads and write, declare
167            # the variables for register indicies. It is being assumed that
168            # all the operands in the OperandList are also in the
169            # SubOperandList and in the same order. Otherwise, it is
170            # expected that predication would not be used for the operands.
171            if operands.predRead:
172                myDict['op_decl'] += 'uint8_t _sourceIndex = 0;\n'
173            if operands.predWrite:
174                myDict['op_decl'] += 'uint8_t M5_VAR_USED _destIndex = 0;\n'
175
176            is_src = lambda op: op.is_src
177            is_dest = lambda op: op.is_dest
178
179            myDict['op_src_decl'] = \
180                      operands.concatSomeAttrStrings(is_src, 'op_src_decl')
181            myDict['op_dest_decl'] = \
182                      operands.concatSomeAttrStrings(is_dest, 'op_dest_decl')
183            if operands.readPC:
184                myDict['op_src_decl'] += \
185                    'TheISA::PCState __parserAutoPCState;\n'
186            if operands.setPC:
187                myDict['op_dest_decl'] += \
188                    'TheISA::PCState __parserAutoPCState;\n'
189
190            myDict['op_rd'] = operands.concatAttrStrings('op_rd')
191            if operands.readPC:
192                myDict['op_rd'] = '__parserAutoPCState = xc->pcState();\n' + \
193                                  myDict['op_rd']
194
195            # Compose the op_wb string. If we're going to write back the
196            # PC state because we changed some of its elements, we'll need to
197            # do that as early as possible. That allows later uncoordinated
198            # modifications to the PC to layer appropriately.
199            reordered = list(operands.items)
200            reordered.reverse()
201            op_wb_str = ''
202            pcWbStr = 'xc->pcState(__parserAutoPCState);\n'
203            for op_desc in reordered:
204                if op_desc.isPCPart() and op_desc.is_dest:
205                    op_wb_str = op_desc.op_wb + pcWbStr + op_wb_str
206                    pcWbStr = ''
207                else:
208                    op_wb_str = op_desc.op_wb + op_wb_str
209            myDict['op_wb'] = op_wb_str
210
211        elif isinstance(d, dict):
212            # if the argument is a dictionary, we just use it.
213            myDict.update(d)
214        elif hasattr(d, '__dict__'):
215            # if the argument is an object, we use its attribute map.
216            myDict.update(d.__dict__)
217        else:
218            raise TypeError, "Template.subst() arg must be or have dictionary"
219        return template % myDict
220
221    # Convert to string.  This handles the case when a template with a
222    # CPU-specific term gets interpolated into another template or into
223    # an output block.
224    def __str__(self):
225        return self.parser.expandCpuSymbolsToString(self.template)
226
227################
228# Format object.
229#
230# A format object encapsulates an instruction format.  It must provide
231# a defineInst() method that generates the code for an instruction
232# definition.
233
234class Format(object):
235    def __init__(self, id, params, code):
236        self.id = id
237        self.params = params
238        label = 'def format ' + id
239        self.user_code = compile(fixPythonIndentation(code), label, 'exec')
240        param_list = string.join(params, ", ")
241        f = '''def defInst(_code, _context, %s):
242                my_locals = vars().copy()
243                exec _code in _context, my_locals
244                return my_locals\n''' % param_list
245        c = compile(f, label + ' wrapper', 'exec')
246        exec c
247        self.func = defInst
248
249    def defineInst(self, parser, name, args, lineno):
250        parser.updateExportContext()
251        context = parser.exportContext.copy()
252        if len(name):
253            Name = name[0].upper()
254            if len(name) > 1:
255                Name += name[1:]
256        context.update({ 'name' : name, 'Name' : Name })
257        try:
258            vars = self.func(self.user_code, context, *args[0], **args[1])
259        except Exception, exc:
260            if debug:
261                raise
262            error(lineno, 'error defining "%s": %s.' % (name, exc))
263        for k in vars.keys():
264            if k not in ('header_output', 'decoder_output',
265                         'exec_output', 'decode_block'):
266                del vars[k]
267        return GenCode(parser, **vars)
268
269# Special null format to catch an implicit-format instruction
270# definition outside of any format block.
271class NoFormat(object):
272    def __init__(self):
273        self.defaultInst = ''
274
275    def defineInst(self, parser, name, args, lineno):
276        error(lineno,
277              'instruction definition "%s" with no active format!' % name)
278
279###############
280# GenCode class
281#
282# The GenCode class encapsulates generated code destined for various
283# output files.  The header_output and decoder_output attributes are
284# strings containing code destined for decoder.hh and decoder.cc
285# respectively.  The decode_block attribute contains code to be
286# incorporated in the decode function itself (that will also end up in
287# decoder.cc).  The exec_output attribute is a dictionary with a key
288# for each CPU model name; the value associated with a particular key
289# is the string of code for that CPU model's exec.cc file.  The
290# has_decode_default attribute is used in the decode block to allow
291# explicit default clauses to override default default clauses.
292
293class GenCode(object):
294    # Constructor.  At this point we substitute out all CPU-specific
295    # symbols.  For the exec output, these go into the per-model
296    # dictionary.  For all other output types they get collapsed into
297    # a single string.
298    def __init__(self, parser,
299                 header_output = '', decoder_output = '', exec_output = '',
300                 decode_block = '', has_decode_default = False):
301        self.parser = parser
302        self.header_output = parser.expandCpuSymbolsToString(header_output)
303        self.decoder_output = parser.expandCpuSymbolsToString(decoder_output)
304        self.exec_output = exec_output
305        self.decode_block = decode_block
306        self.has_decode_default = has_decode_default
307
308    # Write these code chunks out to the filesystem.  They will be properly
309    # interwoven by the write_top_level_files().
310    def emit(self):
311        if self.header_output:
312            self.parser.get_file('header').write(self.header_output)
313        if self.decoder_output:
314            self.parser.get_file('decoder').write(self.decoder_output)
315        if self.exec_output:
316            self.parser.get_file('exec').write(self.exec_output)
317        if self.decode_block:
318            self.parser.get_file('decode_block').write(self.decode_block)
319
320    # Override '+' operator: generate a new GenCode object that
321    # concatenates all the individual strings in the operands.
322    def __add__(self, other):
323        return GenCode(self.parser,
324                       self.header_output + other.header_output,
325                       self.decoder_output + other.decoder_output,
326                       self.exec_output + other.exec_output,
327                       self.decode_block + other.decode_block,
328                       self.has_decode_default or other.has_decode_default)
329
330    # Prepend a string (typically a comment) to all the strings.
331    def prepend_all(self, pre):
332        self.header_output = pre + self.header_output
333        self.decoder_output  = pre + self.decoder_output
334        self.decode_block = pre + self.decode_block
335        self.exec_output  = pre + self.exec_output
336
337    # Wrap the decode block in a pair of strings (e.g., 'case foo:'
338    # and 'break;').  Used to build the big nested switch statement.
339    def wrap_decode_block(self, pre, post = ''):
340        self.decode_block = pre + indent(self.decode_block) + post
341
342#####################################################################
343#
344#                      Bitfield Operator Support
345#
346#####################################################################
347
348bitOp1ArgRE = re.compile(r'<\s*(\w+)\s*:\s*>')
349
350bitOpWordRE = re.compile(r'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>')
351bitOpExprRE = re.compile(r'\)<\s*(\w+)\s*:\s*(\w+)\s*>')
352
353def substBitOps(code):
354    # first convert single-bit selectors to two-index form
355    # i.e., <n> --> <n:n>
356    code = bitOp1ArgRE.sub(r'<\1:\1>', code)
357    # simple case: selector applied to ID (name)
358    # i.e., foo<a:b> --> bits(foo, a, b)
359    code = bitOpWordRE.sub(r'bits(\1, \2, \3)', code)
360    # if selector is applied to expression (ending in ')'),
361    # we need to search backward for matching '('
362    match = bitOpExprRE.search(code)
363    while match:
364        exprEnd = match.start()
365        here = exprEnd - 1
366        nestLevel = 1
367        while nestLevel > 0:
368            if code[here] == '(':
369                nestLevel -= 1
370            elif code[here] == ')':
371                nestLevel += 1
372            here -= 1
373            if here < 0:
374                sys.exit("Didn't find '('!")
375        exprStart = here+1
376        newExpr = r'bits(%s, %s, %s)' % (code[exprStart:exprEnd+1],
377                                         match.group(1), match.group(2))
378        code = code[:exprStart] + newExpr + code[match.end():]
379        match = bitOpExprRE.search(code)
380    return code
381
382
383#####################################################################
384#
385#                             Code Parser
386#
387# The remaining code is the support for automatically extracting
388# instruction characteristics from pseudocode.
389#
390#####################################################################
391
392# Force the argument to be a list.  Useful for flags, where a caller
393# can specify a singleton flag or a list of flags.  Also usful for
394# converting tuples to lists so they can be modified.
395def makeList(arg):
396    if isinstance(arg, list):
397        return arg
398    elif isinstance(arg, tuple):
399        return list(arg)
400    elif not arg:
401        return []
402    else:
403        return [ arg ]
404
405class Operand(object):
406    '''Base class for operand descriptors.  An instance of this class
407    (or actually a class derived from this one) represents a specific
408    operand for a code block (e.g, "Rc.sq" as a dest). Intermediate
409    derived classes encapsulates the traits of a particular operand
410    type (e.g., "32-bit integer register").'''
411
412    def buildReadCode(self, func = None):
413        subst_dict = {"name": self.base_name,
414                      "func": func,
415                      "reg_idx": self.reg_spec,
416                      "ctype": self.ctype}
417        if hasattr(self, 'src_reg_idx'):
418            subst_dict['op_idx'] = self.src_reg_idx
419        code = self.read_code % subst_dict
420        return '%s = %s;\n' % (self.base_name, code)
421
422    def buildWriteCode(self, func = None):
423        subst_dict = {"name": self.base_name,
424                      "func": func,
425                      "reg_idx": self.reg_spec,
426                      "ctype": self.ctype,
427                      "final_val": self.base_name}
428        if hasattr(self, 'dest_reg_idx'):
429            subst_dict['op_idx'] = self.dest_reg_idx
430        code = self.write_code % subst_dict
431        return '''
432        {
433            %s final_val = %s;
434            %s;
435            if (traceData) { traceData->setData(final_val); }
436        }''' % (self.dflt_ctype, self.base_name, code)
437
438    def __init__(self, parser, full_name, ext, is_src, is_dest):
439        self.full_name = full_name
440        self.ext = ext
441        self.is_src = is_src
442        self.is_dest = is_dest
443        # The 'effective extension' (eff_ext) is either the actual
444        # extension, if one was explicitly provided, or the default.
445        if ext:
446            self.eff_ext = ext
447        elif hasattr(self, 'dflt_ext'):
448            self.eff_ext = self.dflt_ext
449
450        if hasattr(self, 'eff_ext'):
451            self.ctype = parser.operandTypeMap[self.eff_ext]
452
453    # Finalize additional fields (primarily code fields).  This step
454    # is done separately since some of these fields may depend on the
455    # register index enumeration that hasn't been performed yet at the
456    # time of __init__(). The register index enumeration is affected
457    # by predicated register reads/writes. Hence, we forward the flags
458    # that indicate whether or not predication is in use.
459    def finalize(self, predRead, predWrite):
460        self.flags = self.getFlags()
461        self.constructor = self.makeConstructor(predRead, predWrite)
462        self.op_decl = self.makeDecl()
463
464        if self.is_src:
465            self.op_rd = self.makeRead(predRead)
466            self.op_src_decl = self.makeDecl()
467        else:
468            self.op_rd = ''
469            self.op_src_decl = ''
470
471        if self.is_dest:
472            self.op_wb = self.makeWrite(predWrite)
473            self.op_dest_decl = self.makeDecl()
474        else:
475            self.op_wb = ''
476            self.op_dest_decl = ''
477
478    def isMem(self):
479        return 0
480
481    def isReg(self):
482        return 0
483
484    def isFloatReg(self):
485        return 0
486
487    def isIntReg(self):
488        return 0
489
490    def isCCReg(self):
491        return 0
492
493    def isControlReg(self):
494        return 0
495
496    def isPCState(self):
497        return 0
498
499    def isPCPart(self):
500        return self.isPCState() and self.reg_spec
501
502    def hasReadPred(self):
503        return self.read_predicate != None
504
505    def hasWritePred(self):
506        return self.write_predicate != None
507
508    def getFlags(self):
509        # note the empty slice '[:]' gives us a copy of self.flags[0]
510        # instead of a reference to it
511        my_flags = self.flags[0][:]
512        if self.is_src:
513            my_flags += self.flags[1]
514        if self.is_dest:
515            my_flags += self.flags[2]
516        return my_flags
517
518    def makeDecl(self):
519        # Note that initializations in the declarations are solely
520        # to avoid 'uninitialized variable' errors from the compiler.
521        return self.ctype + ' ' + self.base_name + ' = 0;\n';
522
523class IntRegOperand(Operand):
524    def isReg(self):
525        return 1
526
527    def isIntReg(self):
528        return 1
529
530    def makeConstructor(self, predRead, predWrite):
531        c_src = ''
532        c_dest = ''
533
534        if self.is_src:
535            c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s;' % (self.reg_spec)
536            if self.hasReadPred():
537                c_src = '\n\tif (%s) {%s\n\t}' % \
538                        (self.read_predicate, c_src)
539
540        if self.is_dest:
541            c_dest = '\n\t_destRegIdx[_numDestRegs++] = %s;' % \
542                    (self.reg_spec)
543            c_dest += '\n\t_numIntDestRegs++;'
544            if self.hasWritePred():
545                c_dest = '\n\tif (%s) {%s\n\t}' % \
546                         (self.write_predicate, c_dest)
547
548        return c_src + c_dest
549
550    def makeRead(self, predRead):
551        if (self.ctype == 'float' or self.ctype == 'double'):
552            error('Attempt to read integer register as FP')
553        if self.read_code != None:
554            return self.buildReadCode('readIntRegOperand')
555
556        int_reg_val = ''
557        if predRead:
558            int_reg_val = 'xc->readIntRegOperand(this, _sourceIndex++)'
559            if self.hasReadPred():
560                int_reg_val = '(%s) ? %s : 0' % \
561                              (self.read_predicate, int_reg_val)
562        else:
563            int_reg_val = 'xc->readIntRegOperand(this, %d)' % self.src_reg_idx
564
565        return '%s = %s;\n' % (self.base_name, int_reg_val)
566
567    def makeWrite(self, predWrite):
568        if (self.ctype == 'float' or self.ctype == 'double'):
569            error('Attempt to write integer register as FP')
570        if self.write_code != None:
571            return self.buildWriteCode('setIntRegOperand')
572
573        if predWrite:
574            wp = 'true'
575            if self.hasWritePred():
576                wp = self.write_predicate
577
578            wcond = 'if (%s)' % (wp)
579            windex = '_destIndex++'
580        else:
581            wcond = ''
582            windex = '%d' % self.dest_reg_idx
583
584        wb = '''
585        %s
586        {
587            %s final_val = %s;
588            xc->setIntRegOperand(this, %s, final_val);\n
589            if (traceData) { traceData->setData(final_val); }
590        }''' % (wcond, self.ctype, self.base_name, windex)
591
592        return wb
593
594class FloatRegOperand(Operand):
595    def isReg(self):
596        return 1
597
598    def isFloatReg(self):
599        return 1
600
601    def makeConstructor(self, predRead, predWrite):
602        c_src = ''
603        c_dest = ''
604
605        if self.is_src:
606            c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s + FP_Reg_Base;' % \
607                    (self.reg_spec)
608
609        if self.is_dest:
610            c_dest = \
611              '\n\t_destRegIdx[_numDestRegs++] = %s + FP_Reg_Base;' % \
612              (self.reg_spec)
613            c_dest += '\n\t_numFPDestRegs++;'
614
615        return c_src + c_dest
616
617    def makeRead(self, predRead):
618        bit_select = 0
619        if (self.ctype == 'float' or self.ctype == 'double'):
620            func = 'readFloatRegOperand'
621        else:
622            func = 'readFloatRegOperandBits'
623        if self.read_code != None:
624            return self.buildReadCode(func)
625
626        if predRead:
627            rindex = '_sourceIndex++'
628        else:
629            rindex = '%d' % self.src_reg_idx
630
631        return '%s = xc->%s(this, %s);\n' % \
632            (self.base_name, func, rindex)
633
634    def makeWrite(self, predWrite):
635        if (self.ctype == 'float' or self.ctype == 'double'):
636            func = 'setFloatRegOperand'
637        else:
638            func = 'setFloatRegOperandBits'
639        if self.write_code != None:
640            return self.buildWriteCode(func)
641
642        if predWrite:
643            wp = '_destIndex++'
644        else:
645            wp = '%d' % self.dest_reg_idx
646        wp = 'xc->%s(this, %s, final_val);' % (func, wp)
647
648        wb = '''
649        {
650            %s final_val = %s;
651            %s\n
652            if (traceData) { traceData->setData(final_val); }
653        }''' % (self.ctype, self.base_name, wp)
654        return wb
655
656class CCRegOperand(Operand):
657    def isReg(self):
658        return 1
659
660    def isCCReg(self):
661        return 1
662
663    def makeConstructor(self, predRead, predWrite):
664        c_src = ''
665        c_dest = ''
666
667        if self.is_src:
668            c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s + CC_Reg_Base;' % \
669                     (self.reg_spec)
670            if self.hasReadPred():
671                c_src = '\n\tif (%s) {%s\n\t}' % \
672                        (self.read_predicate, c_src)
673
674        if self.is_dest:
675            c_dest = \
676              '\n\t_destRegIdx[_numDestRegs++] = %s + CC_Reg_Base;' % \
677              (self.reg_spec)
678            c_dest += '\n\t_numCCDestRegs++;'
679            if self.hasWritePred():
680                c_dest = '\n\tif (%s) {%s\n\t}' % \
681                         (self.write_predicate, c_dest)
682
683        return c_src + c_dest
684
685    def makeRead(self, predRead):
686        if (self.ctype == 'float' or self.ctype == 'double'):
687            error('Attempt to read condition-code register as FP')
688        if self.read_code != None:
689            return self.buildReadCode('readCCRegOperand')
690
691        int_reg_val = ''
692        if predRead:
693            int_reg_val = 'xc->readCCRegOperand(this, _sourceIndex++)'
694            if self.hasReadPred():
695                int_reg_val = '(%s) ? %s : 0' % \
696                              (self.read_predicate, int_reg_val)
697        else:
698            int_reg_val = 'xc->readCCRegOperand(this, %d)' % self.src_reg_idx
699
700        return '%s = %s;\n' % (self.base_name, int_reg_val)
701
702    def makeWrite(self, predWrite):
703        if (self.ctype == 'float' or self.ctype == 'double'):
704            error('Attempt to write condition-code register as FP')
705        if self.write_code != None:
706            return self.buildWriteCode('setCCRegOperand')
707
708        if predWrite:
709            wp = 'true'
710            if self.hasWritePred():
711                wp = self.write_predicate
712
713            wcond = 'if (%s)' % (wp)
714            windex = '_destIndex++'
715        else:
716            wcond = ''
717            windex = '%d' % self.dest_reg_idx
718
719        wb = '''
720        %s
721        {
722            %s final_val = %s;
723            xc->setCCRegOperand(this, %s, final_val);\n
724            if (traceData) { traceData->setData(final_val); }
725        }''' % (wcond, self.ctype, self.base_name, windex)
726
727        return wb
728
729class ControlRegOperand(Operand):
730    def isReg(self):
731        return 1
732
733    def isControlReg(self):
734        return 1
735
736    def makeConstructor(self, predRead, predWrite):
737        c_src = ''
738        c_dest = ''
739
740        if self.is_src:
741            c_src = \
742              '\n\t_srcRegIdx[_numSrcRegs++] = %s + Misc_Reg_Base;' % \
743              (self.reg_spec)
744
745        if self.is_dest:
746            c_dest = \
747              '\n\t_destRegIdx[_numDestRegs++] = %s + Misc_Reg_Base;' % \
748              (self.reg_spec)
749
750        return c_src + c_dest
751
752    def makeRead(self, predRead):
753        bit_select = 0
754        if (self.ctype == 'float' or self.ctype == 'double'):
755            error('Attempt to read control register as FP')
756        if self.read_code != None:
757            return self.buildReadCode('readMiscRegOperand')
758
759        if predRead:
760            rindex = '_sourceIndex++'
761        else:
762            rindex = '%d' % self.src_reg_idx
763
764        return '%s = xc->readMiscRegOperand(this, %s);\n' % \
765            (self.base_name, rindex)
766
767    def makeWrite(self, predWrite):
768        if (self.ctype == 'float' or self.ctype == 'double'):
769            error('Attempt to write control register as FP')
770        if self.write_code != None:
771            return self.buildWriteCode('setMiscRegOperand')
772
773        if predWrite:
774            windex = '_destIndex++'
775        else:
776            windex = '%d' % self.dest_reg_idx
777
778        wb = 'xc->setMiscRegOperand(this, %s, %s);\n' % \
779             (windex, self.base_name)
780        wb += 'if (traceData) { traceData->setData(%s); }' % \
781              self.base_name
782
783        return wb
784
785class MemOperand(Operand):
786    def isMem(self):
787        return 1
788
789    def makeConstructor(self, predRead, predWrite):
790        return ''
791
792    def makeDecl(self):
793        # Note that initializations in the declarations are solely
794        # to avoid 'uninitialized variable' errors from the compiler.
795        # Declare memory data variable.
796        return '%s %s = 0;\n' % (self.ctype, self.base_name)
797
798    def makeRead(self, predRead):
799        if self.read_code != None:
800            return self.buildReadCode()
801        return ''
802
803    def makeWrite(self, predWrite):
804        if self.write_code != None:
805            return self.buildWriteCode()
806        return ''
807
808class PCStateOperand(Operand):
809    def makeConstructor(self, predRead, predWrite):
810        return ''
811
812    def makeRead(self, predRead):
813        if self.reg_spec:
814            # A component of the PC state.
815            return '%s = __parserAutoPCState.%s();\n' % \
816                (self.base_name, self.reg_spec)
817        else:
818            # The whole PC state itself.
819            return '%s = xc->pcState();\n' % self.base_name
820
821    def makeWrite(self, predWrite):
822        if self.reg_spec:
823            # A component of the PC state.
824            return '__parserAutoPCState.%s(%s);\n' % \
825                (self.reg_spec, self.base_name)
826        else:
827            # The whole PC state itself.
828            return 'xc->pcState(%s);\n' % self.base_name
829
830    def makeDecl(self):
831        ctype = 'TheISA::PCState'
832        if self.isPCPart():
833            ctype = self.ctype
834        # Note that initializations in the declarations are solely
835        # to avoid 'uninitialized variable' errors from the compiler.
836        return '%s %s = 0;\n' % (ctype, self.base_name)
837
838    def isPCState(self):
839        return 1
840
841class OperandList(object):
842    '''Find all the operands in the given code block.  Returns an operand
843    descriptor list (instance of class OperandList).'''
844    def __init__(self, parser, code):
845        self.items = []
846        self.bases = {}
847        # delete strings and comments so we don't match on operands inside
848        for regEx in (stringRE, commentRE):
849            code = regEx.sub('', code)
850        # search for operands
851        next_pos = 0
852        while 1:
853            match = parser.operandsRE.search(code, next_pos)
854            if not match:
855                # no more matches: we're done
856                break
857            op = match.groups()
858            # regexp groups are operand full name, base, and extension
859            (op_full, op_base, op_ext) = op
860            # if the token following the operand is an assignment, this is
861            # a destination (LHS), else it's a source (RHS)
862            is_dest = (assignRE.match(code, match.end()) != None)
863            is_src = not is_dest
864            # see if we've already seen this one
865            op_desc = self.find_base(op_base)
866            if op_desc:
867                if op_desc.ext != op_ext:
868                    error('Inconsistent extensions for operand %s' % \
869                          op_base)
870                op_desc.is_src = op_desc.is_src or is_src
871                op_desc.is_dest = op_desc.is_dest or is_dest
872            else:
873                # new operand: create new descriptor
874                op_desc = parser.operandNameMap[op_base](parser,
875                    op_full, op_ext, is_src, is_dest)
876                self.append(op_desc)
877            # start next search after end of current match
878            next_pos = match.end()
879        self.sort()
880        # enumerate source & dest register operands... used in building
881        # constructor later
882        self.numSrcRegs = 0
883        self.numDestRegs = 0
884        self.numFPDestRegs = 0
885        self.numIntDestRegs = 0
886        self.numCCDestRegs = 0
887        self.numMiscDestRegs = 0
888        self.memOperand = None
889
890        # Flags to keep track if one or more operands are to be read/written
891        # conditionally.
892        self.predRead = False
893        self.predWrite = False
894
895        for op_desc in self.items:
896            if op_desc.isReg():
897                if op_desc.is_src:
898                    op_desc.src_reg_idx = self.numSrcRegs
899                    self.numSrcRegs += 1
900                if op_desc.is_dest:
901                    op_desc.dest_reg_idx = self.numDestRegs
902                    self.numDestRegs += 1
903                    if op_desc.isFloatReg():
904                        self.numFPDestRegs += 1
905                    elif op_desc.isIntReg():
906                        self.numIntDestRegs += 1
907                    elif op_desc.isCCReg():
908                        self.numCCDestRegs += 1
909                    elif op_desc.isControlReg():
910                        self.numMiscDestRegs += 1
911            elif op_desc.isMem():
912                if self.memOperand:
913                    error("Code block has more than one memory operand.")
914                self.memOperand = op_desc
915
916            # Check if this operand has read/write predication. If true, then
917            # the microop will dynamically index source/dest registers.
918            self.predRead = self.predRead or op_desc.hasReadPred()
919            self.predWrite = self.predWrite or op_desc.hasWritePred()
920
921        if parser.maxInstSrcRegs < self.numSrcRegs:
922            parser.maxInstSrcRegs = self.numSrcRegs
923        if parser.maxInstDestRegs < self.numDestRegs:
924            parser.maxInstDestRegs = self.numDestRegs
925        if parser.maxMiscDestRegs < self.numMiscDestRegs:
926            parser.maxMiscDestRegs = self.numMiscDestRegs
927
928        # now make a final pass to finalize op_desc fields that may depend
929        # on the register enumeration
930        for op_desc in self.items:
931            op_desc.finalize(self.predRead, self.predWrite)
932
933    def __len__(self):
934        return len(self.items)
935
936    def __getitem__(self, index):
937        return self.items[index]
938
939    def append(self, op_desc):
940        self.items.append(op_desc)
941        self.bases[op_desc.base_name] = op_desc
942
943    def find_base(self, base_name):
944        # like self.bases[base_name], but returns None if not found
945        # (rather than raising exception)
946        return self.bases.get(base_name)
947
948    # internal helper function for concat[Some]Attr{Strings|Lists}
949    def __internalConcatAttrs(self, attr_name, filter, result):
950        for op_desc in self.items:
951            if filter(op_desc):
952                result += getattr(op_desc, attr_name)
953        return result
954
955    # return a single string that is the concatenation of the (string)
956    # values of the specified attribute for all operands
957    def concatAttrStrings(self, attr_name):
958        return self.__internalConcatAttrs(attr_name, lambda x: 1, '')
959
960    # like concatAttrStrings, but only include the values for the operands
961    # for which the provided filter function returns true
962    def concatSomeAttrStrings(self, filter, attr_name):
963        return self.__internalConcatAttrs(attr_name, filter, '')
964
965    # return a single list that is the concatenation of the (list)
966    # values of the specified attribute for all operands
967    def concatAttrLists(self, attr_name):
968        return self.__internalConcatAttrs(attr_name, lambda x: 1, [])
969
970    # like concatAttrLists, but only include the values for the operands
971    # for which the provided filter function returns true
972    def concatSomeAttrLists(self, filter, attr_name):
973        return self.__internalConcatAttrs(attr_name, filter, [])
974
975    def sort(self):
976        self.items.sort(lambda a, b: a.sort_pri - b.sort_pri)
977
978class SubOperandList(OperandList):
979    '''Find all the operands in the given code block.  Returns an operand
980    descriptor list (instance of class OperandList).'''
981    def __init__(self, parser, code, master_list):
982        self.items = []
983        self.bases = {}
984        # delete strings and comments so we don't match on operands inside
985        for regEx in (stringRE, commentRE):
986            code = regEx.sub('', code)
987        # search for operands
988        next_pos = 0
989        while 1:
990            match = parser.operandsRE.search(code, next_pos)
991            if not match:
992                # no more matches: we're done
993                break
994            op = match.groups()
995            # regexp groups are operand full name, base, and extension
996            (op_full, op_base, op_ext) = op
997            # find this op in the master list
998            op_desc = master_list.find_base(op_base)
999            if not op_desc:
1000                error('Found operand %s which is not in the master list!'
1001                      % op_base)
1002            else:
1003                # See if we've already found this operand
1004                op_desc = self.find_base(op_base)
1005                if not op_desc:
1006                    # if not, add a reference to it to this sub list
1007                    self.append(master_list.bases[op_base])
1008
1009            # start next search after end of current match
1010            next_pos = match.end()
1011        self.sort()
1012        self.memOperand = None
1013        # Whether the whole PC needs to be read so parts of it can be accessed
1014        self.readPC = False
1015        # Whether the whole PC needs to be written after parts of it were
1016        # changed
1017        self.setPC = False
1018        # Whether this instruction manipulates the whole PC or parts of it.
1019        # Mixing the two is a bad idea and flagged as an error.
1020        self.pcPart = None
1021
1022        # Flags to keep track if one or more operands are to be read/written
1023        # conditionally.
1024        self.predRead = False
1025        self.predWrite = False
1026
1027        for op_desc in self.items:
1028            if op_desc.isPCPart():
1029                self.readPC = True
1030                if op_desc.is_dest:
1031                    self.setPC = True
1032
1033            if op_desc.isPCState():
1034                if self.pcPart is not None:
1035                    if self.pcPart and not op_desc.isPCPart() or \
1036                            not self.pcPart and op_desc.isPCPart():
1037                        error("Mixed whole and partial PC state operands.")
1038                self.pcPart = op_desc.isPCPart()
1039
1040            if op_desc.isMem():
1041                if self.memOperand:
1042                    error("Code block has more than one memory operand.")
1043                self.memOperand = op_desc
1044
1045            # Check if this operand has read/write predication. If true, then
1046            # the microop will dynamically index source/dest registers.
1047            self.predRead = self.predRead or op_desc.hasReadPred()
1048            self.predWrite = self.predWrite or op_desc.hasWritePred()
1049
1050# Regular expression object to match C++ strings
1051stringRE = re.compile(r'"([^"\\]|\\.)*"')
1052
1053# Regular expression object to match C++ comments
1054# (used in findOperands())
1055commentRE = re.compile(r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
1056        re.DOTALL | re.MULTILINE)
1057
1058# Regular expression object to match assignment statements
1059# (used in findOperands())
1060assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE)
1061
1062def makeFlagConstructor(flag_list):
1063    if len(flag_list) == 0:
1064        return ''
1065    # filter out repeated flags
1066    flag_list.sort()
1067    i = 1
1068    while i < len(flag_list):
1069        if flag_list[i] == flag_list[i-1]:
1070            del flag_list[i]
1071        else:
1072            i += 1
1073    pre = '\n\tflags['
1074    post = '] = true;'
1075    code = pre + string.join(flag_list, post + pre) + post
1076    return code
1077
1078# Assume all instruction flags are of the form 'IsFoo'
1079instFlagRE = re.compile(r'Is.*')
1080
1081# OpClass constants end in 'Op' except No_OpClass
1082opClassRE = re.compile(r'.*Op|No_OpClass')
1083
1084class InstObjParams(object):
1085    def __init__(self, parser, mnem, class_name, base_class = '',
1086                 snippets = {}, opt_args = []):
1087        self.mnemonic = mnem
1088        self.class_name = class_name
1089        self.base_class = base_class
1090        if not isinstance(snippets, dict):
1091            snippets = {'code' : snippets}
1092        compositeCode = ' '.join(map(str, snippets.values()))
1093        self.snippets = snippets
1094
1095        self.operands = OperandList(parser, compositeCode)
1096
1097        # The header of the constructor declares the variables to be used
1098        # in the body of the constructor.
1099        header = ''
1100        header += '\n\t_numSrcRegs = 0;'
1101        header += '\n\t_numDestRegs = 0;'
1102        header += '\n\t_numFPDestRegs = 0;'
1103        header += '\n\t_numIntDestRegs = 0;'
1104        header += '\n\t_numCCDestRegs = 0;'
1105
1106        self.constructor = header + \
1107                           self.operands.concatAttrStrings('constructor')
1108
1109        self.flags = self.operands.concatAttrLists('flags')
1110
1111        self.op_class = None
1112
1113        # Optional arguments are assumed to be either StaticInst flags
1114        # or an OpClass value.  To avoid having to import a complete
1115        # list of these values to match against, we do it ad-hoc
1116        # with regexps.
1117        for oa in opt_args:
1118            if instFlagRE.match(oa):
1119                self.flags.append(oa)
1120            elif opClassRE.match(oa):
1121                self.op_class = oa
1122            else:
1123                error('InstObjParams: optional arg "%s" not recognized '
1124                      'as StaticInst::Flag or OpClass.' % oa)
1125
1126        # Make a basic guess on the operand class if not set.
1127        # These are good enough for most cases.
1128        if not self.op_class:
1129            if 'IsStore' in self.flags:
1130                self.op_class = 'MemWriteOp'
1131            elif 'IsLoad' in self.flags or 'IsPrefetch' in self.flags:
1132                self.op_class = 'MemReadOp'
1133            elif 'IsFloating' in self.flags:
1134                self.op_class = 'FloatAddOp'
1135            else:
1136                self.op_class = 'IntAluOp'
1137
1138        # add flag initialization to contructor here to include
1139        # any flags added via opt_args
1140        self.constructor += makeFlagConstructor(self.flags)
1141
1142        # if 'IsFloating' is set, add call to the FP enable check
1143        # function (which should be provided by isa_desc via a declare)
1144        if 'IsFloating' in self.flags:
1145            self.fp_enable_check = 'fault = checkFpEnableFault(xc);'
1146        else:
1147            self.fp_enable_check = ''
1148
1149##############
1150# Stack: a simple stack object.  Used for both formats (formatStack)
1151# and default cases (defaultStack).  Simply wraps a list to give more
1152# stack-like syntax and enable initialization with an argument list
1153# (as opposed to an argument that's a list).
1154
1155class Stack(list):
1156    def __init__(self, *items):
1157        list.__init__(self, items)
1158
1159    def push(self, item):
1160        self.append(item);
1161
1162    def top(self):
1163        return self[-1]
1164
1165# Format a file include stack backtrace as a string
1166def backtrace(filename_stack):
1167    fmt = "In file included from %s:"
1168    return "\n".join([fmt % f for f in filename_stack])
1169
1170
1171#######################
1172#
1173# LineTracker: track filenames along with line numbers in PLY lineno fields
1174#     PLY explicitly doesn't do anything with 'lineno' except propagate
1175#     it.  This class lets us tie filenames with the line numbers with a
1176#     minimum of disruption to existing increment code.
1177#
1178
1179class LineTracker(object):
1180    def __init__(self, filename, lineno=1):
1181        self.filename = filename
1182        self.lineno = lineno
1183
1184    # Overload '+=' for increments.  We need to create a new object on
1185    # each update else every token ends up referencing the same
1186    # constantly incrementing instance.
1187    def __iadd__(self, incr):
1188        return LineTracker(self.filename, self.lineno + incr)
1189
1190    def __str__(self):
1191        return "%s:%d" % (self.filename, self.lineno)
1192
1193    # In case there are places where someone really expects a number
1194    def __int__(self):
1195        return self.lineno
1196
1197
1198#######################
1199#
1200# ISA Parser
1201#   parses ISA DSL and emits C++ headers and source
1202#
1203
1204class ISAParser(Grammar):
1205    class CpuModel(object):
1206        def __init__(self, name, filename, includes, strings):
1207            self.name = name
1208            self.filename = filename
1209            self.includes = includes
1210            self.strings = strings
1211
1212    def __init__(self, output_dir):
1213        super(ISAParser, self).__init__()
1214        self.output_dir = output_dir
1215
1216        self.filename = None # for output file watermarking/scaremongering
1217
1218        self.cpuModels = [
1219            ISAParser.CpuModel('ExecContext',
1220                               'generic_cpu_exec.cc',
1221                               '#include "cpu/exec_context.hh"',
1222                               { "CPU_exec_context" : "ExecContext" }),
1223            ]
1224
1225        # variable to hold templates
1226        self.templateMap = {}
1227
1228        # This dictionary maps format name strings to Format objects.
1229        self.formatMap = {}
1230
1231        # Track open files and, if applicable, how many chunks it has been
1232        # split into so far.
1233        self.files = {}
1234        self.splits = {}
1235
1236        # isa_name / namespace identifier from namespace declaration.
1237        # before the namespace declaration, None.
1238        self.isa_name = None
1239        self.namespace = None
1240
1241        # The format stack.
1242        self.formatStack = Stack(NoFormat())
1243
1244        # The default case stack.
1245        self.defaultStack = Stack(None)
1246
1247        # Stack that tracks current file and line number.  Each
1248        # element is a tuple (filename, lineno) that records the
1249        # *current* filename and the line number in the *previous*
1250        # file where it was included.
1251        self.fileNameStack = Stack()
1252
1253        symbols = ('makeList', 're', 'string')
1254        self.exportContext = dict([(s, eval(s)) for s in symbols])
1255
1256        self.maxInstSrcRegs = 0
1257        self.maxInstDestRegs = 0
1258        self.maxMiscDestRegs = 0
1259
1260    def __getitem__(self, i):    # Allow object (self) to be
1261        return getattr(self, i)  # passed to %-substitutions
1262
1263    # Change the file suffix of a base filename:
1264    #   (e.g.) decoder.cc -> decoder-g.cc.inc for 'global' outputs
1265    def suffixize(self, s, sec):
1266        extn = re.compile('(\.[^\.]+)$') # isolate extension
1267        if self.namespace:
1268            return extn.sub(r'-ns\1.inc', s) # insert some text on either side
1269        else:
1270            return extn.sub(r'-g\1.inc', s)
1271
1272    # Get the file object for emitting code into the specified section
1273    # (header, decoder, exec, decode_block).
1274    def get_file(self, section):
1275        if section == 'decode_block':
1276            filename = 'decode-method.cc.inc'
1277        else:
1278            if section == 'header':
1279                file = 'decoder.hh'
1280            else:
1281                file = '%s.cc' % section
1282            filename = self.suffixize(file, section)
1283        try:
1284            return self.files[filename]
1285        except KeyError: pass
1286
1287        f = self.open(filename)
1288        self.files[filename] = f
1289
1290        # The splittable files are the ones with many independent
1291        # per-instruction functions - the decoder's instruction constructors
1292        # and the instruction execution (execute()) methods. These both have
1293        # the suffix -ns.cc.inc, meaning they are within the namespace part
1294        # of the ISA, contain object-emitting C++ source, and are included
1295        # into other top-level files. These are the files that need special
1296        # #define's to allow parts of them to be compiled separately. Rather
1297        # than splitting the emissions into separate files, the monolithic
1298        # output of the ISA parser is maintained, but the value (or lack
1299        # thereof) of the __SPLIT definition during C preprocessing will
1300        # select the different chunks. If no 'split' directives are used,
1301        # the cpp emissions have no effect.
1302        if re.search('-ns.cc.inc$', filename):
1303            print >>f, '#if !defined(__SPLIT) || (__SPLIT == 1)'
1304            self.splits[f] = 1
1305        # ensure requisite #include's
1306        elif filename in ['decoder-g.cc.inc', 'exec-g.cc.inc']:
1307            print >>f, '#include "decoder.hh"'
1308        elif filename == 'decoder-g.hh.inc':
1309            print >>f, '#include "base/bitfield.hh"'
1310
1311        return f
1312
1313    # Weave together the parts of the different output sections by
1314    # #include'ing them into some very short top-level .cc/.hh files.
1315    # These small files make it much clearer how this tool works, since
1316    # you directly see the chunks emitted as files that are #include'd.
1317    def write_top_level_files(self):
1318        dep = self.open('inc.d', bare=True)
1319
1320        # decoder header - everything depends on this
1321        file = 'decoder.hh'
1322        with self.open(file) as f:
1323            inc = []
1324
1325            fn = 'decoder-g.hh.inc'
1326            assert(fn in self.files)
1327            f.write('#include "%s"\n' % fn)
1328            inc.append(fn)
1329
1330            fn = 'decoder-ns.hh.inc'
1331            assert(fn in self.files)
1332            f.write('namespace %s {\n#include "%s"\n}\n'
1333                    % (self.namespace, fn))
1334            inc.append(fn)
1335
1336            print >>dep, file+':', ' '.join(inc)
1337
1338        # decoder method - cannot be split
1339        file = 'decoder.cc'
1340        with self.open(file) as f:
1341            inc = []
1342
1343            fn = 'decoder-g.cc.inc'
1344            assert(fn in self.files)
1345            f.write('#include "%s"\n' % fn)
1346            inc.append(fn)
1347
1348            fn = 'decode-method.cc.inc'
1349            # is guaranteed to have been written for parse to complete
1350            f.write('#include "%s"\n' % fn)
1351            inc.append(fn)
1352
1353            inc.append("decoder.hh")
1354            print >>dep, file+':', ' '.join(inc)
1355
1356        extn = re.compile('(\.[^\.]+)$')
1357
1358        # instruction constructors
1359        splits = self.splits[self.get_file('decoder')]
1360        file_ = 'inst-constrs.cc'
1361        for i in range(1, splits+1):
1362            if splits > 1:
1363                file = extn.sub(r'-%d\1' % i, file_)
1364            else:
1365                file = file_
1366            with self.open(file) as f:
1367                inc = []
1368
1369                fn = 'decoder-g.cc.inc'
1370                assert(fn in self.files)
1371                f.write('#include "%s"\n' % fn)
1372                inc.append(fn)
1373
1374                fn = 'decoder-ns.cc.inc'
1375                assert(fn in self.files)
1376                print >>f, 'namespace %s {' % self.namespace
1377                if splits > 1:
1378                    print >>f, '#define __SPLIT %u' % i
1379                print >>f, '#include "%s"' % fn
1380                print >>f, '}'
1381                inc.append(fn)
1382
1383                inc.append("decoder.hh")
1384                print >>dep, file+':', ' '.join(inc)
1385
1386        # instruction execution per-CPU model
1387        splits = self.splits[self.get_file('exec')]
1388        for cpu in self.cpuModels:
1389            for i in range(1, splits+1):
1390                if splits > 1:
1391                    file = extn.sub(r'_%d\1' % i, cpu.filename)
1392                else:
1393                    file = cpu.filename
1394                with self.open(file) as f:
1395                    inc = []
1396
1397                    fn = 'exec-g.cc.inc'
1398                    assert(fn in self.files)
1399                    f.write('#include "%s"\n' % fn)
1400                    inc.append(fn)
1401
1402                    f.write(cpu.includes+"\n")
1403
1404                    fn = 'exec-ns.cc.inc'
1405                    assert(fn in self.files)
1406                    print >>f, 'namespace %s {' % self.namespace
1407                    print >>f, '#define CPU_EXEC_CONTEXT %s' \
1408                               % cpu.strings['CPU_exec_context']
1409                    if splits > 1:
1410                        print >>f, '#define __SPLIT %u' % i
1411                    print >>f, '#include "%s"' % fn
1412                    print >>f, '}'
1413                    inc.append(fn)
1414
1415                    inc.append("decoder.hh")
1416                    print >>dep, file+':', ' '.join(inc)
1417
1418        # max_inst_regs.hh
1419        self.update('max_inst_regs.hh',
1420                    '''namespace %(namespace)s {
1421    const int MaxInstSrcRegs = %(maxInstSrcRegs)d;
1422    const int MaxInstDestRegs = %(maxInstDestRegs)d;
1423    const int MaxMiscDestRegs = %(maxMiscDestRegs)d;\n}\n''' % self)
1424        print >>dep, 'max_inst_regs.hh:'
1425
1426        dep.close()
1427
1428
1429    scaremonger_template ='''// DO NOT EDIT
1430// This file was automatically generated from an ISA description:
1431//   %(filename)s
1432
1433''';
1434
1435    #####################################################################
1436    #
1437    #                                Lexer
1438    #
1439    # The PLY lexer module takes two things as input:
1440    # - A list of token names (the string list 'tokens')
1441    # - A regular expression describing a match for each token.  The
1442    #   regexp for token FOO can be provided in two ways:
1443    #   - as a string variable named t_FOO
1444    #   - as the doc string for a function named t_FOO.  In this case,
1445    #     the function is also executed, allowing an action to be
1446    #     associated with each token match.
1447    #
1448    #####################################################################
1449
1450    # Reserved words.  These are listed separately as they are matched
1451    # using the same regexp as generic IDs, but distinguished in the
1452    # t_ID() function.  The PLY documentation suggests this approach.
1453    reserved = (
1454        'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT',
1455        'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS',
1456        'OUTPUT', 'SIGNED', 'SPLIT', 'TEMPLATE'
1457        )
1458
1459    # List of tokens.  The lex module requires this.
1460    tokens = reserved + (
1461        # identifier
1462        'ID',
1463
1464        # integer literal
1465        'INTLIT',
1466
1467        # string literal
1468        'STRLIT',
1469
1470        # code literal
1471        'CODELIT',
1472
1473        # ( ) [ ] { } < > , ; . : :: *
1474        'LPAREN', 'RPAREN',
1475        'LBRACKET', 'RBRACKET',
1476        'LBRACE', 'RBRACE',
1477        'LESS', 'GREATER', 'EQUALS',
1478        'COMMA', 'SEMI', 'DOT', 'COLON', 'DBLCOLON',
1479        'ASTERISK',
1480
1481        # C preprocessor directives
1482        'CPPDIRECTIVE'
1483
1484    # The following are matched but never returned. commented out to
1485    # suppress PLY warning
1486        # newfile directive
1487    #    'NEWFILE',
1488
1489        # endfile directive
1490    #    'ENDFILE'
1491    )
1492
1493    # Regular expressions for token matching
1494    t_LPAREN           = r'\('
1495    t_RPAREN           = r'\)'
1496    t_LBRACKET         = r'\['
1497    t_RBRACKET         = r'\]'
1498    t_LBRACE           = r'\{'
1499    t_RBRACE           = r'\}'
1500    t_LESS             = r'\<'
1501    t_GREATER          = r'\>'
1502    t_EQUALS           = r'='
1503    t_COMMA            = r','
1504    t_SEMI             = r';'
1505    t_DOT              = r'\.'
1506    t_COLON            = r':'
1507    t_DBLCOLON         = r'::'
1508    t_ASTERISK         = r'\*'
1509
1510    # Identifiers and reserved words
1511    reserved_map = { }
1512    for r in reserved:
1513        reserved_map[r.lower()] = r
1514
1515    def t_ID(self, t):
1516        r'[A-Za-z_]\w*'
1517        t.type = self.reserved_map.get(t.value, 'ID')
1518        return t
1519
1520    # Integer literal
1521    def t_INTLIT(self, t):
1522        r'-?(0x[\da-fA-F]+)|\d+'
1523        try:
1524            t.value = int(t.value,0)
1525        except ValueError:
1526            error(t.lexer.lineno, 'Integer value "%s" too large' % t.value)
1527            t.value = 0
1528        return t
1529
1530    # String literal.  Note that these use only single quotes, and
1531    # can span multiple lines.
1532    def t_STRLIT(self, t):
1533        r"(?m)'([^'])+'"
1534        # strip off quotes
1535        t.value = t.value[1:-1]
1536        t.lexer.lineno += t.value.count('\n')
1537        return t
1538
1539
1540    # "Code literal"... like a string literal, but delimiters are
1541    # '{{' and '}}' so they get formatted nicely under emacs c-mode
1542    def t_CODELIT(self, t):
1543        r"(?m)\{\{([^\}]|}(?!\}))+\}\}"
1544        # strip off {{ & }}
1545        t.value = t.value[2:-2]
1546        t.lexer.lineno += t.value.count('\n')
1547        return t
1548
1549    def t_CPPDIRECTIVE(self, t):
1550        r'^\#[^\#].*\n'
1551        t.lexer.lineno += t.value.count('\n')
1552        return t
1553
1554    def t_NEWFILE(self, t):
1555        r'^\#\#newfile\s+"[^"]*"\n'
1556        self.fileNameStack.push(t.lexer.lineno)
1557        t.lexer.lineno = LineTracker(t.value[11:-2])
1558
1559    def t_ENDFILE(self, t):
1560        r'^\#\#endfile\n'
1561        t.lexer.lineno = self.fileNameStack.pop()
1562
1563    #
1564    # The functions t_NEWLINE, t_ignore, and t_error are
1565    # special for the lex module.
1566    #
1567
1568    # Newlines
1569    def t_NEWLINE(self, t):
1570        r'\n+'
1571        t.lexer.lineno += t.value.count('\n')
1572
1573    # Comments
1574    def t_comment(self, t):
1575        r'//.*'
1576
1577    # Completely ignored characters
1578    t_ignore = ' \t\x0c'
1579
1580    # Error handler
1581    def t_error(self, t):
1582        error(t.lexer.lineno, "illegal character '%s'" % t.value[0])
1583        t.skip(1)
1584
1585    #####################################################################
1586    #
1587    #                                Parser
1588    #
1589    # Every function whose name starts with 'p_' defines a grammar
1590    # rule.  The rule is encoded in the function's doc string, while
1591    # the function body provides the action taken when the rule is
1592    # matched.  The argument to each function is a list of the values
1593    # of the rule's symbols: t[0] for the LHS, and t[1..n] for the
1594    # symbols on the RHS.  For tokens, the value is copied from the
1595    # t.value attribute provided by the lexer.  For non-terminals, the
1596    # value is assigned by the producing rule; i.e., the job of the
1597    # grammar rule function is to set the value for the non-terminal
1598    # on the LHS (by assigning to t[0]).
1599    #####################################################################
1600
1601    # The LHS of the first grammar rule is used as the start symbol
1602    # (in this case, 'specification').  Note that this rule enforces
1603    # that there will be exactly one namespace declaration, with 0 or
1604    # more global defs/decls before and after it.  The defs & decls
1605    # before the namespace decl will be outside the namespace; those
1606    # after will be inside.  The decoder function is always inside the
1607    # namespace.
1608    def p_specification(self, t):
1609        'specification : opt_defs_and_outputs top_level_decode_block'
1610
1611        for f in self.splits.iterkeys():
1612            f.write('\n#endif\n')
1613
1614        for f in self.files.itervalues(): # close ALL the files;
1615            f.close() # not doing so can cause compilation to fail
1616
1617        self.write_top_level_files()
1618
1619        t[0] = True
1620
1621    # 'opt_defs_and_outputs' is a possibly empty sequence of def and/or
1622    # output statements. Its productions do the hard work of eventually
1623    # instantiating a GenCode, which are generally emitted (written to disk)
1624    # as soon as possible, except for the decode_block, which has to be
1625    # accumulated into one large function of nested switch/case blocks.
1626    def p_opt_defs_and_outputs_0(self, t):
1627        'opt_defs_and_outputs : empty'
1628
1629    def p_opt_defs_and_outputs_1(self, t):
1630        'opt_defs_and_outputs : defs_and_outputs'
1631
1632    def p_defs_and_outputs_0(self, t):
1633        'defs_and_outputs : def_or_output'
1634
1635    def p_defs_and_outputs_1(self, t):
1636        'defs_and_outputs : defs_and_outputs def_or_output'
1637
1638    # The list of possible definition/output statements.
1639    # They are all processed as they are seen.
1640    def p_def_or_output(self, t):
1641        '''def_or_output : name_decl
1642                         | def_format
1643                         | def_bitfield
1644                         | def_bitfield_struct
1645                         | def_template
1646                         | def_operand_types
1647                         | def_operands
1648                         | output
1649                         | global_let
1650                         | split'''
1651
1652    # Utility function used by both invocations of splitting - explicit
1653    # 'split' keyword and split() function inside "let {{ }};" blocks.
1654    def split(self, sec, write=False):
1655        assert(sec != 'header' and "header cannot be split")
1656
1657        f = self.get_file(sec)
1658        self.splits[f] += 1
1659        s = '\n#endif\n#if __SPLIT == %u\n' % self.splits[f]
1660        if write:
1661            f.write(s)
1662        else:
1663            return s
1664
1665    # split output file to reduce compilation time
1666    def p_split(self, t):
1667        'split : SPLIT output_type SEMI'
1668        assert(self.isa_name and "'split' not allowed before namespace decl")
1669
1670        self.split(t[2], True)
1671
1672    def p_output_type(self, t):
1673        '''output_type : DECODER
1674                       | HEADER
1675                       | EXEC'''
1676        t[0] = t[1]
1677
1678    # ISA name declaration looks like "namespace <foo>;"
1679    def p_name_decl(self, t):
1680        'name_decl : NAMESPACE ID SEMI'
1681        assert(self.isa_name == None and "Only 1 namespace decl permitted")
1682        self.isa_name = t[2]
1683        self.namespace = t[2] + 'Inst'
1684
1685    # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied
1686    # directly to the appropriate output section.
1687
1688    # Massage output block by substituting in template definitions and
1689    # bit operators.  We handle '%'s embedded in the string that don't
1690    # indicate template substitutions (or CPU-specific symbols, which
1691    # get handled in GenCode) by doubling them first so that the
1692    # format operation will reduce them back to single '%'s.
1693    def process_output(self, s):
1694        s = self.protectNonSubstPercents(s)
1695        # protects cpu-specific symbols too
1696        s = self.protectCpuSymbols(s)
1697        return substBitOps(s % self.templateMap)
1698
1699    def p_output(self, t):
1700        'output : OUTPUT output_type CODELIT SEMI'
1701        kwargs = { t[2]+'_output' : self.process_output(t[3]) }
1702        GenCode(self, **kwargs).emit()
1703
1704    # global let blocks 'let {{...}}' (Python code blocks) are
1705    # executed directly when seen.  Note that these execute in a
1706    # special variable context 'exportContext' to prevent the code
1707    # from polluting this script's namespace.
1708    def p_global_let(self, t):
1709        'global_let : LET CODELIT SEMI'
1710        def _split(sec):
1711            return self.split(sec)
1712        self.updateExportContext()
1713        self.exportContext["header_output"] = ''
1714        self.exportContext["decoder_output"] = ''
1715        self.exportContext["exec_output"] = ''
1716        self.exportContext["decode_block"] = ''
1717        self.exportContext["split"] = _split
1718        split_setup = '''
1719def wrap(func):
1720    def split(sec):
1721        globals()[sec + '_output'] += func(sec)
1722    return split
1723split = wrap(split)
1724del wrap
1725'''
1726        # This tricky setup (immediately above) allows us to just write
1727        # (e.g.) "split('exec')" in the Python code and the split #ifdef's
1728        # will automatically be added to the exec_output variable. The inner
1729        # Python execution environment doesn't know about the split points,
1730        # so we carefully inject and wrap a closure that can retrieve the
1731        # next split's #define from the parser and add it to the current
1732        # emission-in-progress.
1733        try:
1734            exec split_setup+fixPythonIndentation(t[2]) in self.exportContext
1735        except Exception, exc:
1736            if debug:
1737                raise
1738            error(t.lineno(1), 'In global let block: %s' % exc)
1739        GenCode(self,
1740                header_output=self.exportContext["header_output"],
1741                decoder_output=self.exportContext["decoder_output"],
1742                exec_output=self.exportContext["exec_output"],
1743                decode_block=self.exportContext["decode_block"]).emit()
1744
1745    # Define the mapping from operand type extensions to C++ types and
1746    # bit widths (stored in operandTypeMap).
1747    def p_def_operand_types(self, t):
1748        'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI'
1749        try:
1750            self.operandTypeMap = eval('{' + t[3] + '}')
1751        except Exception, exc:
1752            if debug:
1753                raise
1754            error(t.lineno(1),
1755                  'In def operand_types: %s' % exc)
1756
1757    # Define the mapping from operand names to operand classes and
1758    # other traits.  Stored in operandNameMap.
1759    def p_def_operands(self, t):
1760        'def_operands : DEF OPERANDS CODELIT SEMI'
1761        if not hasattr(self, 'operandTypeMap'):
1762            error(t.lineno(1),
1763                  'error: operand types must be defined before operands')
1764        try:
1765            user_dict = eval('{' + t[3] + '}', self.exportContext)
1766        except Exception, exc:
1767            if debug:
1768                raise
1769            error(t.lineno(1), 'In def operands: %s' % exc)
1770        self.buildOperandNameMap(user_dict, t.lexer.lineno)
1771
1772    # A bitfield definition looks like:
1773    # 'def [signed] bitfield <ID> [<first>:<last>]'
1774    # This generates a preprocessor macro in the output file.
1775    def p_def_bitfield_0(self, t):
1776        'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI'
1777        expr = 'bits(machInst, %2d, %2d)' % (t[6], t[8])
1778        if (t[2] == 'signed'):
1779            expr = 'sext<%d>(%s)' % (t[6] - t[8] + 1, expr)
1780        hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
1781        GenCode(self, header_output=hash_define).emit()
1782
1783    # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]'
1784    def p_def_bitfield_1(self, t):
1785        'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI'
1786        expr = 'bits(machInst, %2d, %2d)' % (t[6], t[6])
1787        if (t[2] == 'signed'):
1788            expr = 'sext<%d>(%s)' % (1, expr)
1789        hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
1790        GenCode(self, header_output=hash_define).emit()
1791
1792    # alternate form for structure member: 'def bitfield <ID> <ID>'
1793    def p_def_bitfield_struct(self, t):
1794        'def_bitfield_struct : DEF opt_signed BITFIELD ID id_with_dot SEMI'
1795        if (t[2] != ''):
1796            error(t.lineno(1),
1797                  'error: structure bitfields are always unsigned.')
1798        expr = 'machInst.%s' % t[5]
1799        hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
1800        GenCode(self, header_output=hash_define).emit()
1801
1802    def p_id_with_dot_0(self, t):
1803        'id_with_dot : ID'
1804        t[0] = t[1]
1805
1806    def p_id_with_dot_1(self, t):
1807        'id_with_dot : ID DOT id_with_dot'
1808        t[0] = t[1] + t[2] + t[3]
1809
1810    def p_opt_signed_0(self, t):
1811        'opt_signed : SIGNED'
1812        t[0] = t[1]
1813
1814    def p_opt_signed_1(self, t):
1815        'opt_signed : empty'
1816        t[0] = ''
1817
1818    def p_def_template(self, t):
1819        'def_template : DEF TEMPLATE ID CODELIT SEMI'
1820        if t[3] in self.templateMap:
1821            print "warning: template %s already defined" % t[3]
1822        self.templateMap[t[3]] = Template(self, t[4])
1823
1824    # An instruction format definition looks like
1825    # "def format <fmt>(<params>) {{...}};"
1826    def p_def_format(self, t):
1827        'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI'
1828        (id, params, code) = (t[3], t[5], t[7])
1829        self.defFormat(id, params, code, t.lexer.lineno)
1830
1831    # The formal parameter list for an instruction format is a
1832    # possibly empty list of comma-separated parameters.  Positional
1833    # (standard, non-keyword) parameters must come first, followed by
1834    # keyword parameters, followed by a '*foo' parameter that gets
1835    # excess positional arguments (as in Python).  Each of these three
1836    # parameter categories is optional.
1837    #
1838    # Note that we do not support the '**foo' parameter for collecting
1839    # otherwise undefined keyword args.  Otherwise the parameter list
1840    # is (I believe) identical to what is supported in Python.
1841    #
1842    # The param list generates a tuple, where the first element is a
1843    # list of the positional params and the second element is a dict
1844    # containing the keyword params.
1845    def p_param_list_0(self, t):
1846        'param_list : positional_param_list COMMA nonpositional_param_list'
1847        t[0] = t[1] + t[3]
1848
1849    def p_param_list_1(self, t):
1850        '''param_list : positional_param_list
1851                      | nonpositional_param_list'''
1852        t[0] = t[1]
1853
1854    def p_positional_param_list_0(self, t):
1855        'positional_param_list : empty'
1856        t[0] = []
1857
1858    def p_positional_param_list_1(self, t):
1859        'positional_param_list : ID'
1860        t[0] = [t[1]]
1861
1862    def p_positional_param_list_2(self, t):
1863        'positional_param_list : positional_param_list COMMA ID'
1864        t[0] = t[1] + [t[3]]
1865
1866    def p_nonpositional_param_list_0(self, t):
1867        'nonpositional_param_list : keyword_param_list COMMA excess_args_param'
1868        t[0] = t[1] + t[3]
1869
1870    def p_nonpositional_param_list_1(self, t):
1871        '''nonpositional_param_list : keyword_param_list
1872                                    | excess_args_param'''
1873        t[0] = t[1]
1874
1875    def p_keyword_param_list_0(self, t):
1876        'keyword_param_list : keyword_param'
1877        t[0] = [t[1]]
1878
1879    def p_keyword_param_list_1(self, t):
1880        'keyword_param_list : keyword_param_list COMMA keyword_param'
1881        t[0] = t[1] + [t[3]]
1882
1883    def p_keyword_param(self, t):
1884        'keyword_param : ID EQUALS expr'
1885        t[0] = t[1] + ' = ' + t[3].__repr__()
1886
1887    def p_excess_args_param(self, t):
1888        'excess_args_param : ASTERISK ID'
1889        # Just concatenate them: '*ID'.  Wrap in list to be consistent
1890        # with positional_param_list and keyword_param_list.
1891        t[0] = [t[1] + t[2]]
1892
1893    # End of format definition-related rules.
1894    ##############
1895
1896    #
1897    # A decode block looks like:
1898    #       decode <field1> [, <field2>]* [default <inst>] { ... }
1899    #
1900    def p_top_level_decode_block(self, t):
1901        'top_level_decode_block : decode_block'
1902        codeObj = t[1]
1903        codeObj.wrap_decode_block('''
1904StaticInstPtr
1905%(isa_name)s::Decoder::decodeInst(%(isa_name)s::ExtMachInst machInst)
1906{
1907    using namespace %(namespace)s;
1908''' % self, '}')
1909
1910        codeObj.emit()
1911
1912    def p_decode_block(self, t):
1913        'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE'
1914        default_defaults = self.defaultStack.pop()
1915        codeObj = t[5]
1916        # use the "default defaults" only if there was no explicit
1917        # default statement in decode_stmt_list
1918        if not codeObj.has_decode_default:
1919            codeObj += default_defaults
1920        codeObj.wrap_decode_block('switch (%s) {\n' % t[2], '}\n')
1921        t[0] = codeObj
1922
1923    # The opt_default statement serves only to push the "default
1924    # defaults" onto defaultStack.  This value will be used by nested
1925    # decode blocks, and used and popped off when the current
1926    # decode_block is processed (in p_decode_block() above).
1927    def p_opt_default_0(self, t):
1928        'opt_default : empty'
1929        # no default specified: reuse the one currently at the top of
1930        # the stack
1931        self.defaultStack.push(self.defaultStack.top())
1932        # no meaningful value returned
1933        t[0] = None
1934
1935    def p_opt_default_1(self, t):
1936        'opt_default : DEFAULT inst'
1937        # push the new default
1938        codeObj = t[2]
1939        codeObj.wrap_decode_block('\ndefault:\n', 'break;\n')
1940        self.defaultStack.push(codeObj)
1941        # no meaningful value returned
1942        t[0] = None
1943
1944    def p_decode_stmt_list_0(self, t):
1945        'decode_stmt_list : decode_stmt'
1946        t[0] = t[1]
1947
1948    def p_decode_stmt_list_1(self, t):
1949        'decode_stmt_list : decode_stmt decode_stmt_list'
1950        if (t[1].has_decode_default and t[2].has_decode_default):
1951            error(t.lineno(1), 'Two default cases in decode block')
1952        t[0] = t[1] + t[2]
1953
1954    #
1955    # Decode statement rules
1956    #
1957    # There are four types of statements allowed in a decode block:
1958    # 1. Format blocks 'format <foo> { ... }'
1959    # 2. Nested decode blocks
1960    # 3. Instruction definitions.
1961    # 4. C preprocessor directives.
1962
1963
1964    # Preprocessor directives found in a decode statement list are
1965    # passed through to the output, replicated to all of the output
1966    # code streams.  This works well for ifdefs, so we can ifdef out
1967    # both the declarations and the decode cases generated by an
1968    # instruction definition.  Handling them as part of the grammar
1969    # makes it easy to keep them in the right place with respect to
1970    # the code generated by the other statements.
1971    def p_decode_stmt_cpp(self, t):
1972        'decode_stmt : CPPDIRECTIVE'
1973        t[0] = GenCode(self, t[1], t[1], t[1], t[1])
1974
1975    # A format block 'format <foo> { ... }' sets the default
1976    # instruction format used to handle instruction definitions inside
1977    # the block.  This format can be overridden by using an explicit
1978    # format on the instruction definition or with a nested format
1979    # block.
1980    def p_decode_stmt_format(self, t):
1981        'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE'
1982        # The format will be pushed on the stack when 'push_format_id'
1983        # is processed (see below).  Once the parser has recognized
1984        # the full production (though the right brace), we're done
1985        # with the format, so now we can pop it.
1986        self.formatStack.pop()
1987        t[0] = t[4]
1988
1989    # This rule exists so we can set the current format (& push the
1990    # stack) when we recognize the format name part of the format
1991    # block.
1992    def p_push_format_id(self, t):
1993        'push_format_id : ID'
1994        try:
1995            self.formatStack.push(self.formatMap[t[1]])
1996            t[0] = ('', '// format %s' % t[1])
1997        except KeyError:
1998            error(t.lineno(1), 'instruction format "%s" not defined.' % t[1])
1999
2000    # Nested decode block: if the value of the current field matches
2001    # the specified constant(s), do a nested decode on some other field.
2002    def p_decode_stmt_decode(self, t):
2003        'decode_stmt : case_list COLON decode_block'
2004        case_list = t[1]
2005        codeObj = t[3]
2006        # just wrap the decoding code from the block as a case in the
2007        # outer switch statement.
2008        codeObj.wrap_decode_block('\n%s\n' % ''.join(case_list))
2009        codeObj.has_decode_default = (case_list == ['default:'])
2010        t[0] = codeObj
2011
2012    # Instruction definition (finally!).
2013    def p_decode_stmt_inst(self, t):
2014        'decode_stmt : case_list COLON inst SEMI'
2015        case_list = t[1]
2016        codeObj = t[3]
2017        codeObj.wrap_decode_block('\n%s' % ''.join(case_list), 'break;\n')
2018        codeObj.has_decode_default = (case_list == ['default:'])
2019        t[0] = codeObj
2020
2021    # The constant list for a decode case label must be non-empty, and must
2022    # either be the keyword 'default', or made up of one or more
2023    # comma-separated integer literals or strings which evaluate to
2024    # constants when compiled as C++.
2025    def p_case_list_0(self, t):
2026        'case_list : DEFAULT'
2027        t[0] = ['default:']
2028
2029    def prep_int_lit_case_label(self, lit):
2030        if lit >= 2**32:
2031            return 'case ULL(%#x): ' % lit
2032        else:
2033            return 'case %#x: ' % lit
2034
2035    def prep_str_lit_case_label(self, lit):
2036        return 'case %s: ' % lit
2037
2038    def p_case_list_1(self, t):
2039        'case_list : INTLIT'
2040        t[0] = [self.prep_int_lit_case_label(t[1])]
2041
2042    def p_case_list_2(self, t):
2043        'case_list : STRLIT'
2044        t[0] = [self.prep_str_lit_case_label(t[1])]
2045
2046    def p_case_list_3(self, t):
2047        'case_list : case_list COMMA INTLIT'
2048        t[0] = t[1]
2049        t[0].append(self.prep_int_lit_case_label(t[3]))
2050
2051    def p_case_list_4(self, t):
2052        'case_list : case_list COMMA STRLIT'
2053        t[0] = t[1]
2054        t[0].append(self.prep_str_lit_case_label(t[3]))
2055
2056    # Define an instruction using the current instruction format
2057    # (specified by an enclosing format block).
2058    # "<mnemonic>(<args>)"
2059    def p_inst_0(self, t):
2060        'inst : ID LPAREN arg_list RPAREN'
2061        # Pass the ID and arg list to the current format class to deal with.
2062        currentFormat = self.formatStack.top()
2063        codeObj = currentFormat.defineInst(self, t[1], t[3], t.lexer.lineno)
2064        args = ','.join(map(str, t[3]))
2065        args = re.sub('(?m)^', '//', args)
2066        args = re.sub('^//', '', args)
2067        comment = '\n// %s::%s(%s)\n' % (currentFormat.id, t[1], args)
2068        codeObj.prepend_all(comment)
2069        t[0] = codeObj
2070
2071    # Define an instruction using an explicitly specified format:
2072    # "<fmt>::<mnemonic>(<args>)"
2073    def p_inst_1(self, t):
2074        'inst : ID DBLCOLON ID LPAREN arg_list RPAREN'
2075        try:
2076            format = self.formatMap[t[1]]
2077        except KeyError:
2078            error(t.lineno(1), 'instruction format "%s" not defined.' % t[1])
2079
2080        codeObj = format.defineInst(self, t[3], t[5], t.lexer.lineno)
2081        comment = '\n// %s::%s(%s)\n' % (t[1], t[3], t[5])
2082        codeObj.prepend_all(comment)
2083        t[0] = codeObj
2084
2085    # The arg list generates a tuple, where the first element is a
2086    # list of the positional args and the second element is a dict
2087    # containing the keyword args.
2088    def p_arg_list_0(self, t):
2089        'arg_list : positional_arg_list COMMA keyword_arg_list'
2090        t[0] = ( t[1], t[3] )
2091
2092    def p_arg_list_1(self, t):
2093        'arg_list : positional_arg_list'
2094        t[0] = ( t[1], {} )
2095
2096    def p_arg_list_2(self, t):
2097        'arg_list : keyword_arg_list'
2098        t[0] = ( [], t[1] )
2099
2100    def p_positional_arg_list_0(self, t):
2101        'positional_arg_list : empty'
2102        t[0] = []
2103
2104    def p_positional_arg_list_1(self, t):
2105        'positional_arg_list : expr'
2106        t[0] = [t[1]]
2107
2108    def p_positional_arg_list_2(self, t):
2109        'positional_arg_list : positional_arg_list COMMA expr'
2110        t[0] = t[1] + [t[3]]
2111
2112    def p_keyword_arg_list_0(self, t):
2113        'keyword_arg_list : keyword_arg'
2114        t[0] = t[1]
2115
2116    def p_keyword_arg_list_1(self, t):
2117        'keyword_arg_list : keyword_arg_list COMMA keyword_arg'
2118        t[0] = t[1]
2119        t[0].update(t[3])
2120
2121    def p_keyword_arg(self, t):
2122        'keyword_arg : ID EQUALS expr'
2123        t[0] = { t[1] : t[3] }
2124
2125    #
2126    # Basic expressions.  These constitute the argument values of
2127    # "function calls" (i.e. instruction definitions in the decode
2128    # block) and default values for formal parameters of format
2129    # functions.
2130    #
2131    # Right now, these are either strings, integers, or (recursively)
2132    # lists of exprs (using Python square-bracket list syntax).  Note
2133    # that bare identifiers are trated as string constants here (since
2134    # there isn't really a variable namespace to refer to).
2135    #
2136    def p_expr_0(self, t):
2137        '''expr : ID
2138                | INTLIT
2139                | STRLIT
2140                | CODELIT'''
2141        t[0] = t[1]
2142
2143    def p_expr_1(self, t):
2144        '''expr : LBRACKET list_expr RBRACKET'''
2145        t[0] = t[2]
2146
2147    def p_list_expr_0(self, t):
2148        'list_expr : expr'
2149        t[0] = [t[1]]
2150
2151    def p_list_expr_1(self, t):
2152        'list_expr : list_expr COMMA expr'
2153        t[0] = t[1] + [t[3]]
2154
2155    def p_list_expr_2(self, t):
2156        'list_expr : empty'
2157        t[0] = []
2158
2159    #
2160    # Empty production... use in other rules for readability.
2161    #
2162    def p_empty(self, t):
2163        'empty :'
2164        pass
2165
2166    # Parse error handler.  Note that the argument here is the
2167    # offending *token*, not a grammar symbol (hence the need to use
2168    # t.value)
2169    def p_error(self, t):
2170        if t:
2171            error(t.lexer.lineno, "syntax error at '%s'" % t.value)
2172        else:
2173            error("unknown syntax error")
2174
2175    # END OF GRAMMAR RULES
2176
2177    def updateExportContext(self):
2178
2179        # create a continuation that allows us to grab the current parser
2180        def wrapInstObjParams(*args):
2181            return InstObjParams(self, *args)
2182        self.exportContext['InstObjParams'] = wrapInstObjParams
2183        self.exportContext.update(self.templateMap)
2184
2185    def defFormat(self, id, params, code, lineno):
2186        '''Define a new format'''
2187
2188        # make sure we haven't already defined this one
2189        if id in self.formatMap:
2190            error(lineno, 'format %s redefined.' % id)
2191
2192        # create new object and store in global map
2193        self.formatMap[id] = Format(id, params, code)
2194
2195    def expandCpuSymbolsToDict(self, template):
2196        '''Expand template with CPU-specific references into a
2197        dictionary with an entry for each CPU model name.  The entry
2198        key is the model name and the corresponding value is the
2199        template with the CPU-specific refs substituted for that
2200        model.'''
2201
2202        # Protect '%'s that don't go with CPU-specific terms
2203        t = re.sub(r'%(?!\(CPU_)', '%%', template)
2204        result = {}
2205        for cpu in self.cpuModels:
2206            result[cpu.name] = t % cpu.strings
2207        return result
2208
2209    def expandCpuSymbolsToString(self, template):
2210        '''*If* the template has CPU-specific references, return a
2211        single string containing a copy of the template for each CPU
2212        model with the corresponding values substituted in.  If the
2213        template has no CPU-specific references, it is returned
2214        unmodified.'''
2215
2216        if template.find('%(CPU_') != -1:
2217            return reduce(lambda x,y: x+y,
2218                          self.expandCpuSymbolsToDict(template).values())
2219        else:
2220            return template
2221
2222    def protectCpuSymbols(self, template):
2223        '''Protect CPU-specific references by doubling the
2224        corresponding '%'s (in preparation for substituting a different
2225        set of references into the template).'''
2226
2227        return re.sub(r'%(?=\(CPU_)', '%%', template)
2228
2229    def protectNonSubstPercents(self, s):
2230        '''Protect any non-dict-substitution '%'s in a format string
2231        (i.e. those not followed by '(')'''
2232
2233        return re.sub(r'%(?!\()', '%%', s)
2234
2235    def buildOperandNameMap(self, user_dict, lineno):
2236        operand_name = {}
2237        for op_name, val in user_dict.iteritems():
2238
2239            # Check if extra attributes have been specified.
2240            if len(val) > 9:
2241                error(lineno, 'error: too many attributes for operand "%s"' %
2242                      base_cls_name)
2243
2244            # Pad val with None in case optional args are missing
2245            val += (None, None, None, None)
2246            base_cls_name, dflt_ext, reg_spec, flags, sort_pri, \
2247            read_code, write_code, read_predicate, write_predicate = val[:9]
2248
2249            # Canonical flag structure is a triple of lists, where each list
2250            # indicates the set of flags implied by this operand always, when
2251            # used as a source, and when used as a dest, respectively.
2252            # For simplicity this can be initialized using a variety of fairly
2253            # obvious shortcuts; we convert these to canonical form here.
2254            if not flags:
2255                # no flags specified (e.g., 'None')
2256                flags = ( [], [], [] )
2257            elif isinstance(flags, str):
2258                # a single flag: assumed to be unconditional
2259                flags = ( [ flags ], [], [] )
2260            elif isinstance(flags, list):
2261                # a list of flags: also assumed to be unconditional
2262                flags = ( flags, [], [] )
2263            elif isinstance(flags, tuple):
2264                # it's a tuple: it should be a triple,
2265                # but each item could be a single string or a list
2266                (uncond_flags, src_flags, dest_flags) = flags
2267                flags = (makeList(uncond_flags),
2268                         makeList(src_flags), makeList(dest_flags))
2269
2270            # Accumulate attributes of new operand class in tmp_dict
2271            tmp_dict = {}
2272            attrList = ['reg_spec', 'flags', 'sort_pri',
2273                        'read_code', 'write_code',
2274                        'read_predicate', 'write_predicate']
2275            if dflt_ext:
2276                dflt_ctype = self.operandTypeMap[dflt_ext]
2277                attrList.extend(['dflt_ctype', 'dflt_ext'])
2278            for attr in attrList:
2279                tmp_dict[attr] = eval(attr)
2280            tmp_dict['base_name'] = op_name
2281
2282            # New class name will be e.g. "IntReg_Ra"
2283            cls_name = base_cls_name + '_' + op_name
2284            # Evaluate string arg to get class object.  Note that the
2285            # actual base class for "IntReg" is "IntRegOperand", i.e. we
2286            # have to append "Operand".
2287            try:
2288                base_cls = eval(base_cls_name + 'Operand')
2289            except NameError:
2290                error(lineno,
2291                      'error: unknown operand base class "%s"' % base_cls_name)
2292            # The following statement creates a new class called
2293            # <cls_name> as a subclass of <base_cls> with the attributes
2294            # in tmp_dict, just as if we evaluated a class declaration.
2295            operand_name[op_name] = type(cls_name, (base_cls,), tmp_dict)
2296
2297        self.operandNameMap = operand_name
2298
2299        # Define operand variables.
2300        operands = user_dict.keys()
2301        extensions = self.operandTypeMap.keys()
2302
2303        operandsREString = r'''
2304        (?<!\w)      # neg. lookbehind assertion: prevent partial matches
2305        ((%s)(?:_(%s))?)   # match: operand with optional '_' then suffix
2306        (?!\w)       # neg. lookahead assertion: prevent partial matches
2307        ''' % (string.join(operands, '|'), string.join(extensions, '|'))
2308
2309        self.operandsRE = re.compile(operandsREString, re.MULTILINE|re.VERBOSE)
2310
2311        # Same as operandsREString, but extension is mandatory, and only two
2312        # groups are returned (base and ext, not full name as above).
2313        # Used for subtituting '_' for '.' to make C++ identifiers.
2314        operandsWithExtREString = r'(?<!\w)(%s)_(%s)(?!\w)' \
2315            % (string.join(operands, '|'), string.join(extensions, '|'))
2316
2317        self.operandsWithExtRE = \
2318            re.compile(operandsWithExtREString, re.MULTILINE)
2319
2320    def substMungedOpNames(self, code):
2321        '''Munge operand names in code string to make legal C++
2322        variable names.  This means getting rid of the type extension
2323        if any.  Will match base_name attribute of Operand object.)'''
2324        return self.operandsWithExtRE.sub(r'\1', code)
2325
2326    def mungeSnippet(self, s):
2327        '''Fix up code snippets for final substitution in templates.'''
2328        if isinstance(s, str):
2329            return self.substMungedOpNames(substBitOps(s))
2330        else:
2331            return s
2332
2333    def open(self, name, bare=False):
2334        '''Open the output file for writing and include scary warning.'''
2335        filename = os.path.join(self.output_dir, name)
2336        f = open(filename, 'w')
2337        if f:
2338            if not bare:
2339                f.write(ISAParser.scaremonger_template % self)
2340        return f
2341
2342    def update(self, file, contents):
2343        '''Update the output file only.  Scons should handle the case when
2344        the new contents are unchanged using its built-in hash feature.'''
2345        f = self.open(file)
2346        f.write(contents)
2347        f.close()
2348
2349    # This regular expression matches '##include' directives
2350    includeRE = re.compile(r'^\s*##include\s+"(?P<filename>[^"]*)".*$',
2351                           re.MULTILINE)
2352
2353    def replace_include(self, matchobj, dirname):
2354        """Function to replace a matched '##include' directive with the
2355        contents of the specified file (with nested ##includes
2356        replaced recursively).  'matchobj' is an re match object
2357        (from a match of includeRE) and 'dirname' is the directory
2358        relative to which the file path should be resolved."""
2359
2360        fname = matchobj.group('filename')
2361        full_fname = os.path.normpath(os.path.join(dirname, fname))
2362        contents = '##newfile "%s"\n%s\n##endfile\n' % \
2363                   (full_fname, self.read_and_flatten(full_fname))
2364        return contents
2365
2366    def read_and_flatten(self, filename):
2367        """Read a file and recursively flatten nested '##include' files."""
2368
2369        current_dir = os.path.dirname(filename)
2370        try:
2371            contents = open(filename).read()
2372        except IOError:
2373            error('Error including file "%s"' % filename)
2374
2375        self.fileNameStack.push(LineTracker(filename))
2376
2377        # Find any includes and include them
2378        def replace(matchobj):
2379            return self.replace_include(matchobj, current_dir)
2380        contents = self.includeRE.sub(replace, contents)
2381
2382        self.fileNameStack.pop()
2383        return contents
2384
2385    AlreadyGenerated = {}
2386
2387    def _parse_isa_desc(self, isa_desc_file):
2388        '''Read in and parse the ISA description.'''
2389
2390        # The build system can end up running the ISA parser twice: once to
2391        # finalize the build dependencies, and then to actually generate
2392        # the files it expects (in src/arch/$ARCH/generated). This code
2393        # doesn't do anything different either time, however; the SCons
2394        # invocations just expect different things. Since this code runs
2395        # within SCons, we can just remember that we've already run and
2396        # not perform a completely unnecessary run, since the ISA parser's
2397        # effect is idempotent.
2398        if isa_desc_file in ISAParser.AlreadyGenerated:
2399            return
2400
2401        # grab the last three path components of isa_desc_file
2402        self.filename = '/'.join(isa_desc_file.split('/')[-3:])
2403
2404        # Read file and (recursively) all included files into a string.
2405        # PLY requires that the input be in a single string so we have to
2406        # do this up front.
2407        isa_desc = self.read_and_flatten(isa_desc_file)
2408
2409        # Initialize lineno tracker
2410        self.lex.lineno = LineTracker(isa_desc_file)
2411
2412        # Parse.
2413        self.parse_string(isa_desc)
2414
2415        ISAParser.AlreadyGenerated[isa_desc_file] = None
2416
2417    def parse_isa_desc(self, *args, **kwargs):
2418        try:
2419            self._parse_isa_desc(*args, **kwargs)
2420        except ISAParserError, e:
2421            print backtrace(self.fileNameStack)
2422            print "At %s:" % e.lineno
2423            print e
2424            sys.exit(1)
2425
2426# Called as script: get args from command line.
2427# Args are: <isa desc file> <output dir>
2428if __name__ == '__main__':
2429    ISAParser(sys.argv[2]).parse_isa_desc(sys.argv[1])
2430