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