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