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