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