1 # Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
27 # Authors: Steve Reinhardt
37 from m5
.util
.grammar
import Grammar
39 class ISAParser(Grammar
):
40 def __init__(self
, *args
, **kwargs
):
41 super(ISAParser
, self
).__init
__(*args
, **kwargs
)
44 #####################################################################
48 # The PLY lexer module takes two things as input:
49 # - A list of token names (the string list 'tokens')
50 # - A regular expression describing a match for each token. The
51 # regexp for token FOO can be provided in two ways:
52 # - as a string variable named t_FOO
53 # - as the doc string for a function named t_FOO. In this case,
54 # the function is also executed, allowing an action to be
55 # associated with each token match.
57 #####################################################################
59 # Reserved words. These are listed separately as they are matched
60 # using the same regexp as generic IDs, but distinguished in the
61 # t_ID() function. The PLY documentation suggests this approach.
63 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT',
64 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS',
65 'OUTPUT', 'SIGNED', 'TEMPLATE'
68 # List of tokens. The lex module requires this.
82 # ( ) [ ] { } < > , ; . : :: *
84 'LBRACKET', 'RBRACKET',
86 'LESS', 'GREATER', 'EQUALS',
87 'COMMA', 'SEMI', 'DOT', 'COLON', 'DBLCOLON',
90 # C preprocessor directives
93 # The following are matched but never returned. commented out to
94 # suppress PLY warning
102 # Regular expressions for token matching
119 # Identifiers and reserved words
122 reserved_map
[r
.lower()] = r
126 t
.type = self
.reserved_map
.get(t
.value
, 'ID')
130 def t_INTLIT(self
, t
):
131 r
'(0x[\da-fA-F]+)|\d+'
133 t
.value
= int(t
.value
,0)
135 error(t
.lexer
.lineno
, 'Integer value "%s" too large' % t
.value
)
139 # String literal. Note that these use only single quotes, and
140 # can span multiple lines.
141 def t_STRLIT(self
, t
):
144 t
.value
= t
.value
[1:-1]
145 t
.lexer
.lineno
+= t
.value
.count('\n')
149 # "Code literal"... like a string literal, but delimiters are
150 # '{{' and '}}' so they get formatted nicely under emacs c-mode
151 def t_CODELIT(self
, t
):
152 r
"(?m)\{\{([^\}]|}(?!\}))+\}\}"
154 t
.value
= t
.value
[2:-2]
155 t
.lexer
.lineno
+= t
.value
.count('\n')
158 def t_CPPDIRECTIVE(self
, t
):
160 t
.lexer
.lineno
+= t
.value
.count('\n')
163 def t_NEWFILE(self
, t
):
164 r
'^\#\#newfile\s+"[\w/.-]*"'
165 fileNameStack
.push((t
.value
[11:-1], t
.lexer
.lineno
))
168 def t_ENDFILE(self
, t
):
170 (old_filename
, t
.lexer
.lineno
) = fileNameStack
.pop()
173 # The functions t_NEWLINE, t_ignore, and t_error are
174 # special for the lex module.
178 def t_NEWLINE(self
, t
):
180 t
.lexer
.lineno
+= t
.value
.count('\n')
183 def t_comment(self
, t
):
186 # Completely ignored characters
190 def t_error(self
, t
):
191 error(t
.lexer
.lineno
, "illegal character '%s'" % t
.value
[0])
194 #####################################################################
198 # Every function whose name starts with 'p_' defines a grammar
199 # rule. The rule is encoded in the function's doc string, while
200 # the function body provides the action taken when the rule is
201 # matched. The argument to each function is a list of the values
202 # of the rule's symbols: t[0] for the LHS, and t[1..n] for the
203 # symbols on the RHS. For tokens, the value is copied from the
204 # t.value attribute provided by the lexer. For non-terminals, the
205 # value is assigned by the producing rule; i.e., the job of the
206 # grammar rule function is to set the value for the non-terminal
207 # on the LHS (by assigning to t[0]).
208 #####################################################################
210 # The LHS of the first grammar rule is used as the start symbol
211 # (in this case, 'specification'). Note that this rule enforces
212 # that there will be exactly one namespace declaration, with 0 or
213 # more global defs/decls before and after it. The defs & decls
214 # before the namespace decl will be outside the namespace; those
215 # after will be inside. The decoder function is always inside the
217 def p_specification(self
, t
):
218 'specification : opt_defs_and_outputs name_decl opt_defs_and_outputs decode_block'
221 namespace
= isa_name
+ "Inst"
222 # wrap the decode block as a function definition
223 t
[4].wrap_decode_block('''
225 %(isa_name)s::decodeInst(%(isa_name)s::ExtMachInst machInst)
227 using namespace %(namespace)s;
229 # both the latter output blocks and the decode block are in
231 namespace_code
= t
[3] + t
[4]
232 # pass it all back to the caller of yacc.parse()
233 t
[0] = (isa_name
, namespace
, global_code
, namespace_code
)
235 # ISA name declaration looks like "namespace <foo>;"
236 def p_name_decl(self
, t
):
237 'name_decl : NAMESPACE ID SEMI'
240 # 'opt_defs_and_outputs' is a possibly empty sequence of
241 # def and/or output statements.
242 def p_opt_defs_and_outputs_0(self
, t
):
243 'opt_defs_and_outputs : empty'
246 def p_opt_defs_and_outputs_1(self
, t
):
247 'opt_defs_and_outputs : defs_and_outputs'
250 def p_defs_and_outputs_0(self
, t
):
251 'defs_and_outputs : def_or_output'
254 def p_defs_and_outputs_1(self
, t
):
255 'defs_and_outputs : defs_and_outputs def_or_output'
258 # The list of possible definition/output statements.
259 def p_def_or_output(self
, t
):
260 '''def_or_output : def_format
262 | def_bitfield_struct
272 # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied
273 # directly to the appropriate output section.
275 # Massage output block by substituting in template definitions and
276 # bit operators. We handle '%'s embedded in the string that don't
277 # indicate template substitutions (or CPU-specific symbols, which
278 # get handled in GenCode) by doubling them first so that the
279 # format operation will reduce them back to single '%'s.
280 def process_output(self
, s
):
281 s
= protect_non_subst_percents(s
)
282 # protects cpu-specific symbols too
283 s
= protect_cpu_symbols(s
)
284 return substBitOps(s
% self
.templateMap
)
286 def p_output_header(self
, t
):
287 'output_header : OUTPUT HEADER CODELIT SEMI'
288 t
[0] = GenCode(header_output
= self
.process_output(t
[3]))
290 def p_output_decoder(self
, t
):
291 'output_decoder : OUTPUT DECODER CODELIT SEMI'
292 t
[0] = GenCode(decoder_output
= self
.process_output(t
[3]))
294 def p_output_exec(self
, t
):
295 'output_exec : OUTPUT EXEC CODELIT SEMI'
296 t
[0] = GenCode(exec_output
= self
.process_output(t
[3]))
298 # global let blocks 'let {{...}}' (Python code blocks) are
299 # executed directly when seen. Note that these execute in a
300 # special variable context 'exportContext' to prevent the code
301 # from polluting this script's namespace.
302 def p_global_let(self
, t
):
303 'global_let : LET CODELIT SEMI'
304 updateExportContext()
305 exportContext
["header_output"] = ''
306 exportContext
["decoder_output"] = ''
307 exportContext
["exec_output"] = ''
308 exportContext
["decode_block"] = ''
310 exec fixPythonIndentation(t
[2]) in exportContext
311 except Exception, exc
:
312 error(t
.lexer
.lineno
,
313 'error: %s in global let block "%s".' % (exc
, t
[2]))
314 t
[0] = GenCode(header_output
= exportContext
["header_output"],
315 decoder_output
= exportContext
["decoder_output"],
316 exec_output
= exportContext
["exec_output"],
317 decode_block
= exportContext
["decode_block"])
319 # Define the mapping from operand type extensions to C++ types and
320 # bit widths (stored in operandTypeMap).
321 def p_def_operand_types(self
, t
):
322 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI'
324 userDict
= eval('{' + t
[3] + '}')
325 except Exception, exc
:
326 error(t
.lexer
.lineno
,
327 'error: %s in def operand_types block "%s".' % (exc
, t
[3]))
328 buildOperandTypeMap(userDict
, t
.lexer
.lineno
)
329 t
[0] = GenCode() # contributes nothing to the output C++ file
331 # Define the mapping from operand names to operand classes and
332 # other traits. Stored in operandNameMap.
333 def p_def_operands(self
, t
):
334 'def_operands : DEF OPERANDS CODELIT SEMI'
335 if not globals().has_key('operandTypeMap'):
336 error(t
.lexer
.lineno
,
337 'error: operand types must be defined before operands')
339 userDict
= eval('{' + t
[3] + '}', exportContext
)
340 except Exception, exc
:
341 error(t
.lexer
.lineno
,
342 'error: %s in def operands block "%s".' % (exc
, t
[3]))
343 buildOperandNameMap(userDict
, t
.lexer
.lineno
)
344 t
[0] = GenCode() # contributes nothing to the output C++ file
346 # A bitfield definition looks like:
347 # 'def [signed] bitfield <ID> [<first>:<last>]'
348 # This generates a preprocessor macro in the output file.
349 def p_def_bitfield_0(self
, t
):
350 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI'
351 expr
= 'bits(machInst, %2d, %2d)' % (t
[6], t
[8])
352 if (t
[2] == 'signed'):
353 expr
= 'sext<%d>(%s)' % (t
[6] - t
[8] + 1, expr
)
354 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
355 t
[0] = GenCode(header_output
= hash_define
)
357 # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]'
358 def p_def_bitfield_1(self
, t
):
359 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI'
360 expr
= 'bits(machInst, %2d, %2d)' % (t
[6], t
[6])
361 if (t
[2] == 'signed'):
362 expr
= 'sext<%d>(%s)' % (1, expr
)
363 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
364 t
[0] = GenCode(header_output
= hash_define
)
366 # alternate form for structure member: 'def bitfield <ID> <ID>'
367 def p_def_bitfield_struct(self
, t
):
368 'def_bitfield_struct : DEF opt_signed BITFIELD ID id_with_dot SEMI'
370 error(t
.lexer
.lineno
,
371 'error: structure bitfields are always unsigned.')
372 expr
= 'machInst.%s' % t
[5]
373 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
374 t
[0] = GenCode(header_output
= hash_define
)
376 def p_id_with_dot_0(self
, t
):
380 def p_id_with_dot_1(self
, t
):
381 'id_with_dot : ID DOT id_with_dot'
382 t
[0] = t
[1] + t
[2] + t
[3]
384 def p_opt_signed_0(self
, t
):
385 'opt_signed : SIGNED'
388 def p_opt_signed_1(self
, t
):
392 def p_def_template(self
, t
):
393 'def_template : DEF TEMPLATE ID CODELIT SEMI'
394 self
.templateMap
[t
[3]] = Template(t
[4])
397 # An instruction format definition looks like
398 # "def format <fmt>(<params>) {{...}};"
399 def p_def_format(self
, t
):
400 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI'
401 (id, params
, code
) = (t
[3], t
[5], t
[7])
402 defFormat(id, params
, code
, t
.lexer
.lineno
)
405 # The formal parameter list for an instruction format is a
406 # possibly empty list of comma-separated parameters. Positional
407 # (standard, non-keyword) parameters must come first, followed by
408 # keyword parameters, followed by a '*foo' parameter that gets
409 # excess positional arguments (as in Python). Each of these three
410 # parameter categories is optional.
412 # Note that we do not support the '**foo' parameter for collecting
413 # otherwise undefined keyword args. Otherwise the parameter list
414 # is (I believe) identical to what is supported in Python.
416 # The param list generates a tuple, where the first element is a
417 # list of the positional params and the second element is a dict
418 # containing the keyword params.
419 def p_param_list_0(self
, t
):
420 'param_list : positional_param_list COMMA nonpositional_param_list'
423 def p_param_list_1(self
, t
):
424 '''param_list : positional_param_list
425 | nonpositional_param_list'''
428 def p_positional_param_list_0(self
, t
):
429 'positional_param_list : empty'
432 def p_positional_param_list_1(self
, t
):
433 'positional_param_list : ID'
436 def p_positional_param_list_2(self
, t
):
437 'positional_param_list : positional_param_list COMMA ID'
440 def p_nonpositional_param_list_0(self
, t
):
441 'nonpositional_param_list : keyword_param_list COMMA excess_args_param'
444 def p_nonpositional_param_list_1(self
, t
):
445 '''nonpositional_param_list : keyword_param_list
446 | excess_args_param'''
449 def p_keyword_param_list_0(self
, t
):
450 'keyword_param_list : keyword_param'
453 def p_keyword_param_list_1(self
, t
):
454 'keyword_param_list : keyword_param_list COMMA keyword_param'
457 def p_keyword_param(self
, t
):
458 'keyword_param : ID EQUALS expr'
459 t
[0] = t
[1] + ' = ' + t
[3].__repr
__()
461 def p_excess_args_param(self
, t
):
462 'excess_args_param : ASTERISK ID'
463 # Just concatenate them: '*ID'. Wrap in list to be consistent
464 # with positional_param_list and keyword_param_list.
467 # End of format definition-related rules.
471 # A decode block looks like:
472 # decode <field1> [, <field2>]* [default <inst>] { ... }
474 def p_decode_block(self
, t
):
475 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE'
476 default_defaults
= defaultStack
.pop()
478 # use the "default defaults" only if there was no explicit
479 # default statement in decode_stmt_list
480 if not codeObj
.has_decode_default
:
481 codeObj
+= default_defaults
482 codeObj
.wrap_decode_block('switch (%s) {\n' % t
[2], '}\n')
485 # The opt_default statement serves only to push the "default
486 # defaults" onto defaultStack. This value will be used by nested
487 # decode blocks, and used and popped off when the current
488 # decode_block is processed (in p_decode_block() above).
489 def p_opt_default_0(self
, t
):
490 'opt_default : empty'
491 # no default specified: reuse the one currently at the top of
493 defaultStack
.push(defaultStack
.top())
494 # no meaningful value returned
497 def p_opt_default_1(self
, t
):
498 'opt_default : DEFAULT inst'
499 # push the new default
501 codeObj
.wrap_decode_block('\ndefault:\n', 'break;\n')
502 defaultStack
.push(codeObj
)
503 # no meaningful value returned
506 def p_decode_stmt_list_0(self
, t
):
507 'decode_stmt_list : decode_stmt'
510 def p_decode_stmt_list_1(self
, t
):
511 'decode_stmt_list : decode_stmt decode_stmt_list'
512 if (t
[1].has_decode_default
and t
[2].has_decode_default
):
513 error(t
.lexer
.lineno
, 'Two default cases in decode block')
517 # Decode statement rules
519 # There are four types of statements allowed in a decode block:
520 # 1. Format blocks 'format <foo> { ... }'
521 # 2. Nested decode blocks
522 # 3. Instruction definitions.
523 # 4. C preprocessor directives.
526 # Preprocessor directives found in a decode statement list are
527 # passed through to the output, replicated to all of the output
528 # code streams. This works well for ifdefs, so we can ifdef out
529 # both the declarations and the decode cases generated by an
530 # instruction definition. Handling them as part of the grammar
531 # makes it easy to keep them in the right place with respect to
532 # the code generated by the other statements.
533 def p_decode_stmt_cpp(self
, t
):
534 'decode_stmt : CPPDIRECTIVE'
535 t
[0] = GenCode(t
[1], t
[1], t
[1], t
[1])
537 # A format block 'format <foo> { ... }' sets the default
538 # instruction format used to handle instruction definitions inside
539 # the block. This format can be overridden by using an explicit
540 # format on the instruction definition or with a nested format
542 def p_decode_stmt_format(self
, t
):
543 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE'
544 # The format will be pushed on the stack when 'push_format_id'
545 # is processed (see below). Once the parser has recognized
546 # the full production (though the right brace), we're done
547 # with the format, so now we can pop it.
551 # This rule exists so we can set the current format (& push the
552 # stack) when we recognize the format name part of the format
554 def p_push_format_id(self
, t
):
555 'push_format_id : ID'
557 formatStack
.push(formatMap
[t
[1]])
558 t
[0] = ('', '// format %s' % t
[1])
560 error(t
.lexer
.lineno
,
561 'instruction format "%s" not defined.' % t
[1])
563 # Nested decode block: if the value of the current field matches
564 # the specified constant, do a nested decode on some other field.
565 def p_decode_stmt_decode(self
, t
):
566 'decode_stmt : case_label COLON decode_block'
569 # just wrap the decoding code from the block as a case in the
570 # outer switch statement.
571 codeObj
.wrap_decode_block('\n%s:\n' % label
)
572 codeObj
.has_decode_default
= (label
== 'default')
575 # Instruction definition (finally!).
576 def p_decode_stmt_inst(self
, t
):
577 'decode_stmt : case_label COLON inst SEMI'
580 codeObj
.wrap_decode_block('\n%s:' % label
, 'break;\n')
581 codeObj
.has_decode_default
= (label
== 'default')
584 # The case label is either a list of one or more constants or
586 def p_case_label_0(self
, t
):
587 'case_label : intlit_list'
588 def make_case(intlit
):
590 return 'case ULL(%#x)' % intlit
592 return 'case %#x' % intlit
593 t
[0] = ': '.join(map(make_case
, t
[1]))
595 def p_case_label_1(self
, t
):
596 'case_label : DEFAULT'
600 # The constant list for a decode case label must be non-empty, but
601 # may have one or more comma-separated integer literals in it.
603 def p_intlit_list_0(self
, t
):
604 'intlit_list : INTLIT'
607 def p_intlit_list_1(self
, t
):
608 'intlit_list : intlit_list COMMA INTLIT'
612 # Define an instruction using the current instruction format
613 # (specified by an enclosing format block).
614 # "<mnemonic>(<args>)"
615 def p_inst_0(self
, t
):
616 'inst : ID LPAREN arg_list RPAREN'
617 # Pass the ID and arg list to the current format class to deal with.
618 currentFormat
= formatStack
.top()
619 codeObj
= currentFormat
.defineInst(t
[1], t
[3], t
.lexer
.lineno
)
620 args
= ','.join(map(str, t
[3]))
621 args
= re
.sub('(?m)^', '//', args
)
622 args
= re
.sub('^//', '', args
)
623 comment
= '\n// %s::%s(%s)\n' % (currentFormat
.id, t
[1], args
)
624 codeObj
.prepend_all(comment
)
627 # Define an instruction using an explicitly specified format:
628 # "<fmt>::<mnemonic>(<args>)"
629 def p_inst_1(self
, t
):
630 'inst : ID DBLCOLON ID LPAREN arg_list RPAREN'
632 format
= formatMap
[t
[1]]
634 error(t
.lexer
.lineno
,
635 'instruction format "%s" not defined.' % t
[1])
636 codeObj
= format
.defineInst(t
[3], t
[5], t
.lexer
.lineno
)
637 comment
= '\n// %s::%s(%s)\n' % (t
[1], t
[3], t
[5])
638 codeObj
.prepend_all(comment
)
641 # The arg list generates a tuple, where the first element is a
642 # list of the positional args and the second element is a dict
643 # containing the keyword args.
644 def p_arg_list_0(self
, t
):
645 'arg_list : positional_arg_list COMMA keyword_arg_list'
646 t
[0] = ( t
[1], t
[3] )
648 def p_arg_list_1(self
, t
):
649 'arg_list : positional_arg_list'
652 def p_arg_list_2(self
, t
):
653 'arg_list : keyword_arg_list'
656 def p_positional_arg_list_0(self
, t
):
657 'positional_arg_list : empty'
660 def p_positional_arg_list_1(self
, t
):
661 'positional_arg_list : expr'
664 def p_positional_arg_list_2(self
, t
):
665 'positional_arg_list : positional_arg_list COMMA expr'
668 def p_keyword_arg_list_0(self
, t
):
669 'keyword_arg_list : keyword_arg'
672 def p_keyword_arg_list_1(self
, t
):
673 'keyword_arg_list : keyword_arg_list COMMA keyword_arg'
677 def p_keyword_arg(self
, t
):
678 'keyword_arg : ID EQUALS expr'
679 t
[0] = { t
[1] : t
[3] }
682 # Basic expressions. These constitute the argument values of
683 # "function calls" (i.e. instruction definitions in the decode
684 # block) and default values for formal parameters of format
687 # Right now, these are either strings, integers, or (recursively)
688 # lists of exprs (using Python square-bracket list syntax). Note
689 # that bare identifiers are trated as string constants here (since
690 # there isn't really a variable namespace to refer to).
692 def p_expr_0(self
, t
):
699 def p_expr_1(self
, t
):
700 '''expr : LBRACKET list_expr RBRACKET'''
703 def p_list_expr_0(self
, t
):
707 def p_list_expr_1(self
, t
):
708 'list_expr : list_expr COMMA expr'
711 def p_list_expr_2(self
, t
):
716 # Empty production... use in other rules for readability.
718 def p_empty(self
, t
):
722 # Parse error handler. Note that the argument here is the
723 # offending *token*, not a grammar symbol (hence the need to use
725 def p_error(self
, t
):
727 error(t
.lexer
.lineno
, "syntax error at '%s'" % t
.value
)
729 error(0, "unknown syntax error", True)
731 # END OF GRAMMAR RULES
733 # Now build the parser.
736 #####################################################################
740 #####################################################################
742 # Expand template with CPU-specific references into a dictionary with
743 # an entry for each CPU model name. The entry key is the model name
744 # and the corresponding value is the template with the CPU-specific
745 # refs substituted for that model.
746 def expand_cpu_symbols_to_dict(template
):
747 # Protect '%'s that don't go with CPU-specific terms
748 t
= re
.sub(r
'%(?!\(CPU_)', '%%', template
)
750 for cpu
in cpu_models
:
751 result
[cpu
.name
] = t
% cpu
.strings
754 # *If* the template has CPU-specific references, return a single
755 # string containing a copy of the template for each CPU model with the
756 # corresponding values substituted in. If the template has no
757 # CPU-specific references, it is returned unmodified.
758 def expand_cpu_symbols_to_string(template
):
759 if template
.find('%(CPU_') != -1:
760 return reduce(lambda x
,y
: x
+y
,
761 expand_cpu_symbols_to_dict(template
).values())
765 # Protect CPU-specific references by doubling the corresponding '%'s
766 # (in preparation for substituting a different set of references into
768 def protect_cpu_symbols(template
):
769 return re
.sub(r
'%(?=\(CPU_)', '%%', template
)
771 # Protect any non-dict-substitution '%'s in a format string
772 # (i.e. those not followed by '(')
773 def protect_non_subst_percents(s
):
774 return re
.sub(r
'%(?!\()', '%%', s
)
779 # The GenCode class encapsulates generated code destined for various
780 # output files. The header_output and decoder_output attributes are
781 # strings containing code destined for decoder.hh and decoder.cc
782 # respectively. The decode_block attribute contains code to be
783 # incorporated in the decode function itself (that will also end up in
784 # decoder.cc). The exec_output attribute is a dictionary with a key
785 # for each CPU model name; the value associated with a particular key
786 # is the string of code for that CPU model's exec.cc file. The
787 # has_decode_default attribute is used in the decode block to allow
788 # explicit default clauses to override default default clauses.
791 # Constructor. At this point we substitute out all CPU-specific
792 # symbols. For the exec output, these go into the per-model
793 # dictionary. For all other output types they get collapsed into
796 header_output
= '', decoder_output
= '', exec_output
= '',
797 decode_block
= '', has_decode_default
= False):
798 self
.header_output
= expand_cpu_symbols_to_string(header_output
)
799 self
.decoder_output
= expand_cpu_symbols_to_string(decoder_output
)
800 if isinstance(exec_output
, dict):
801 self
.exec_output
= exec_output
802 elif isinstance(exec_output
, str):
803 # If the exec_output arg is a single string, we replicate
804 # it for each of the CPU models, substituting and
805 # %(CPU_foo)s params appropriately.
806 self
.exec_output
= expand_cpu_symbols_to_dict(exec_output
)
807 self
.decode_block
= expand_cpu_symbols_to_string(decode_block
)
808 self
.has_decode_default
= has_decode_default
810 # Override '+' operator: generate a new GenCode object that
811 # concatenates all the individual strings in the operands.
812 def __add__(self
, other
):
814 for cpu
in cpu_models
:
816 exec_output
[n
] = self
.exec_output
[n
] + other
.exec_output
[n
]
817 return GenCode(self
.header_output
+ other
.header_output
,
818 self
.decoder_output
+ other
.decoder_output
,
820 self
.decode_block
+ other
.decode_block
,
821 self
.has_decode_default
or other
.has_decode_default
)
823 # Prepend a string (typically a comment) to all the strings.
824 def prepend_all(self
, pre
):
825 self
.header_output
= pre
+ self
.header_output
826 self
.decoder_output
= pre
+ self
.decoder_output
827 self
.decode_block
= pre
+ self
.decode_block
828 for cpu
in cpu_models
:
829 self
.exec_output
[cpu
.name
] = pre
+ self
.exec_output
[cpu
.name
]
831 # Wrap the decode block in a pair of strings (e.g., 'case foo:'
832 # and 'break;'). Used to build the big nested switch statement.
833 def wrap_decode_block(self
, pre
, post
= ''):
834 self
.decode_block
= pre
+ indent(self
.decode_block
) + post
839 # A format object encapsulates an instruction format. It must provide
840 # a defineInst() method that generates the code for an instruction
843 exportContextSymbols
= ('InstObjParams', 'makeList', 're', 'string')
847 def updateExportContext():
848 exportContext
.update(exportDict(*exportContextSymbols
))
849 exportContext
.update(parser
.templateMap
)
851 def exportDict(*symNames
):
852 return dict([(s
, eval(s
)) for s
in symNames
])
856 def __init__(self
, id, params
, code
):
857 # constructor: just save away arguments
860 label
= 'def format ' + id
861 self
.user_code
= compile(fixPythonIndentation(code
), label
, 'exec')
862 param_list
= string
.join(params
, ", ")
863 f
= '''def defInst(_code, _context, %s):
864 my_locals = vars().copy()
865 exec _code in _context, my_locals
866 return my_locals\n''' % param_list
867 c
= compile(f
, label
+ ' wrapper', 'exec')
871 def defineInst(self
, name
, args
, lineno
):
873 updateExportContext()
874 context
.update(exportContext
)
876 Name
= name
[0].upper()
879 context
.update({ 'name': name
, 'Name': Name
})
881 vars = self
.func(self
.user_code
, context
, *args
[0], **args
[1])
882 except Exception, exc
:
883 error(lineno
, 'error defining "%s": %s.' % (name
, exc
))
884 for k
in vars.keys():
885 if k
not in ('header_output', 'decoder_output',
886 'exec_output', 'decode_block'):
888 return GenCode(**vars)
890 # Special null format to catch an implicit-format instruction
891 # definition outside of any format block.
894 self
.defaultInst
= ''
896 def defineInst(self
, name
, args
, lineno
):
898 'instruction definition "%s" with no active format!' % name
)
900 # This dictionary maps format name strings to Format objects.
903 # Define a new format
904 def defFormat(id, params
, code
, lineno
):
905 # make sure we haven't already defined this one
906 if formatMap
.get(id, None) != None:
907 error(lineno
, 'format %s redefined.' % id)
908 # create new object and store in global map
909 formatMap
[id] = Format(id, params
, code
)
913 # Stack: a simple stack object. Used for both formats (formatStack)
914 # and default cases (defaultStack). Simply wraps a list to give more
915 # stack-like syntax and enable initialization with an argument list
916 # (as opposed to an argument that's a list).
919 def __init__(self
, *items
):
920 list.__init
__(self
, items
)
922 def push(self
, item
):
928 # The global format stack.
929 formatStack
= Stack(NoFormat())
931 # The global default case stack.
932 defaultStack
= Stack( None )
934 # Global stack that tracks current file and line number.
935 # Each element is a tuple (filename, lineno) that records the
936 # *current* filename and the line number in the *previous* file where
938 fileNameStack
= Stack()
944 # Indent every line in string 's' by two spaces
945 # (except preprocessor directives).
946 # Used to make nested code blocks look pretty.
949 return re
.sub(r
'(?m)^(?!#)', ' ', s
)
952 # Munge a somewhat arbitrarily formatted piece of Python code
953 # (e.g. from a format 'let' block) into something whose indentation
954 # will get by the Python parser.
956 # The two keys here are that Python will give a syntax error if
957 # there's any whitespace at the beginning of the first line, and that
958 # all lines at the same lexical nesting level must have identical
959 # indentation. Unfortunately the way code literals work, an entire
960 # let block tends to have some initial indentation. Rather than
961 # trying to figure out what that is and strip it off, we prepend 'if
962 # 1:' to make the let code the nested block inside the if (and have
963 # the parser automatically deal with the indentation for us).
965 # We don't want to do this if (1) the code block is empty or (2) the
966 # first line of the block doesn't have any whitespace at the front.
968 def fixPythonIndentation(s
):
969 # get rid of blank lines first
970 s
= re
.sub(r
'(?m)^\s*\n', '', s
);
971 if (s
!= '' and re
.match(r
'[ \t]', s
[0])):
975 # Error handler. Just call exit. Output formatted to work under
976 # Emacs compile-mode. Optional 'print_traceback' arg, if set to True,
977 # prints a Python stack backtrace too (can be handy when trying to
978 # debug the parser itself).
979 def error(lineno
, string
, print_traceback
= False):
981 for (filename
, line
) in fileNameStack
[0:-1]:
982 print spaces
+ "In file included from " + filename
+ ":"
984 # Print a Python stack backtrace if requested.
985 if (print_traceback
):
986 traceback
.print_exc()
988 line_str
= "%d:" % lineno
991 sys
.exit(spaces
+ "%s:%s %s" % (fileNameStack
[-1][0], line_str
, string
))
994 #####################################################################
996 # Bitfield Operator Support
998 #####################################################################
1000 bitOp1ArgRE
= re
.compile(r
'<\s*(\w+)\s*:\s*>')
1002 bitOpWordRE
= re
.compile(r
'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>')
1003 bitOpExprRE
= re
.compile(r
'\)<\s*(\w+)\s*:\s*(\w+)\s*>')
1005 def substBitOps(code
):
1006 # first convert single-bit selectors to two-index form
1007 # i.e., <n> --> <n:n>
1008 code
= bitOp1ArgRE
.sub(r
'<\1:\1>', code
)
1009 # simple case: selector applied to ID (name)
1010 # i.e., foo<a:b> --> bits(foo, a, b)
1011 code
= bitOpWordRE
.sub(r
'bits(\1, \2, \3)', code
)
1012 # if selector is applied to expression (ending in ')'),
1013 # we need to search backward for matching '('
1014 match
= bitOpExprRE
.search(code
)
1016 exprEnd
= match
.start()
1019 while nestLevel
> 0:
1020 if code
[here
] == '(':
1022 elif code
[here
] == ')':
1026 sys
.exit("Didn't find '('!")
1028 newExpr
= r
'bits(%s, %s, %s)' % (code
[exprStart
:exprEnd
+1],
1029 match
.group(1), match
.group(2))
1030 code
= code
[:exprStart
] + newExpr
+ code
[match
.end():]
1031 match
= bitOpExprRE
.search(code
)
1035 ####################
1038 # Template objects are format strings that allow substitution from
1039 # the attribute spaces of other objects (e.g. InstObjParams instances).
1041 labelRE
= re
.compile(r
'(?<!%)%\(([^\)]+)\)[sd]')
1044 def __init__(self
, t
):
1050 # Protect non-Python-dict substitutions (e.g. if there's a printf
1051 # in the templated C++ code)
1052 template
= protect_non_subst_percents(self
.template
)
1053 # CPU-model-specific substitutions are handled later (in GenCode).
1054 template
= protect_cpu_symbols(template
)
1056 # Build a dict ('myDict') to use for the template substitution.
1057 # Start with the template namespace. Make a copy since we're
1058 # going to modify it.
1059 myDict
= parser
.templateMap
.copy()
1061 if isinstance(d
, InstObjParams
):
1062 # If we're dealing with an InstObjParams object, we need
1063 # to be a little more sophisticated. The instruction-wide
1064 # parameters are already formed, but the parameters which
1065 # are only function wide still need to be generated.
1068 myDict
.update(d
.__dict
__)
1069 # The "operands" and "snippets" attributes of the InstObjParams
1070 # objects are for internal use and not substitution.
1071 del myDict
['operands']
1072 del myDict
['snippets']
1074 snippetLabels
= [l
for l
in labelRE
.findall(template
)
1075 if d
.snippets
.has_key(l
)]
1077 snippets
= dict([(s
, mungeSnippet(d
.snippets
[s
]))
1078 for s
in snippetLabels
])
1080 myDict
.update(snippets
)
1082 compositeCode
= ' '.join(map(str, snippets
.values()))
1084 # Add in template itself in case it references any
1085 # operands explicitly (like Mem)
1086 compositeCode
+= ' ' + template
1088 operands
= SubOperandList(compositeCode
, d
.operands
)
1090 myDict
['op_decl'] = operands
.concatAttrStrings('op_decl')
1092 is_src
= lambda op
: op
.is_src
1093 is_dest
= lambda op
: op
.is_dest
1095 myDict
['op_src_decl'] = \
1096 operands
.concatSomeAttrStrings(is_src
, 'op_src_decl')
1097 myDict
['op_dest_decl'] = \
1098 operands
.concatSomeAttrStrings(is_dest
, 'op_dest_decl')
1100 myDict
['op_rd'] = operands
.concatAttrStrings('op_rd')
1101 myDict
['op_wb'] = operands
.concatAttrStrings('op_wb')
1103 if d
.operands
.memOperand
:
1104 myDict
['mem_acc_size'] = d
.operands
.memOperand
.mem_acc_size
1105 myDict
['mem_acc_type'] = d
.operands
.memOperand
.mem_acc_type
1107 elif isinstance(d
, dict):
1108 # if the argument is a dictionary, we just use it.
1110 elif hasattr(d
, '__dict__'):
1111 # if the argument is an object, we use its attribute map.
1112 myDict
.update(d
.__dict
__)
1114 raise TypeError, "Template.subst() arg must be or have dictionary"
1115 return template
% myDict
1117 # Convert to string. This handles the case when a template with a
1118 # CPU-specific term gets interpolated into another template or into
1121 return expand_cpu_symbols_to_string(self
.template
)
1123 #####################################################################
1127 # The remaining code is the support for automatically extracting
1128 # instruction characteristics from pseudocode.
1130 #####################################################################
1132 # Force the argument to be a list. Useful for flags, where a caller
1133 # can specify a singleton flag or a list of flags. Also usful for
1134 # converting tuples to lists so they can be modified.
1136 if isinstance(arg
, list):
1138 elif isinstance(arg
, tuple):
1145 # Generate operandTypeMap from the user's 'def operand_types'
1147 def buildOperandTypeMap(userDict
, lineno
):
1148 global operandTypeMap
1150 for (ext
, (desc
, size
)) in userDict
.iteritems():
1151 if desc
== 'signed int':
1152 ctype
= 'int%d_t' % size
1154 elif desc
== 'unsigned int':
1155 ctype
= 'uint%d_t' % size
1157 elif desc
== 'float':
1158 is_signed
= 1 # shouldn't really matter
1163 elif desc
== 'twin64 int':
1166 elif desc
== 'twin32 int':
1170 error(lineno
, 'Unrecognized type description "%s" in userDict')
1171 operandTypeMap
[ext
] = (size
, ctype
, is_signed
)
1176 # Base class for operand descriptors. An instance of this class (or
1177 # actually a class derived from this one) represents a specific
1178 # operand for a code block (e.g, "Rc.sq" as a dest). Intermediate
1179 # derived classes encapsulates the traits of a particular operand type
1180 # (e.g., "32-bit integer register").
1182 class Operand(object):
1183 def buildReadCode(self
, func
= None):
1184 code
= self
.read_code
% {"name": self
.base_name
,
1186 "op_idx": self
.src_reg_idx
,
1187 "reg_idx": self
.reg_spec
,
1189 "ctype": self
.ctype
}
1190 if self
.size
!= self
.dflt_size
:
1191 return '%s = bits(%s, %d, 0);\n' % \
1192 (self
.base_name
, code
, self
.size
-1)
1194 return '%s = %s;\n' % \
1195 (self
.base_name
, code
)
1197 def buildWriteCode(self
, func
= None):
1198 if (self
.size
!= self
.dflt_size
and self
.is_signed
):
1199 final_val
= 'sext<%d>(%s)' % (self
.size
, self
.base_name
)
1201 final_val
= self
.base_name
1202 code
= self
.write_code
% {"name": self
.base_name
,
1204 "op_idx": self
.dest_reg_idx
,
1205 "reg_idx": self
.reg_spec
,
1207 "ctype": self
.ctype
,
1208 "final_val": final_val
}
1213 if (traceData) { traceData->setData(final_val); }
1214 }''' % (self
.dflt_ctype
, final_val
, code
)
1216 def __init__(self
, full_name
, ext
, is_src
, is_dest
):
1217 self
.full_name
= full_name
1219 self
.is_src
= is_src
1220 self
.is_dest
= is_dest
1221 # The 'effective extension' (eff_ext) is either the actual
1222 # extension, if one was explicitly provided, or the default.
1226 self
.eff_ext
= self
.dflt_ext
1228 (self
.size
, self
.ctype
, self
.is_signed
) = operandTypeMap
[self
.eff_ext
]
1230 # note that mem_acc_size is undefined for non-mem operands...
1231 # template must be careful not to use it if it doesn't apply.
1233 self
.mem_acc_size
= self
.makeAccSize()
1234 if self
.ctype
in ['Twin32_t', 'Twin64_t']:
1235 self
.mem_acc_type
= 'Twin'
1237 self
.mem_acc_type
= 'uint'
1239 # Finalize additional fields (primarily code fields). This step
1240 # is done separately since some of these fields may depend on the
1241 # register index enumeration that hasn't been performed yet at the
1242 # time of __init__().
1244 self
.flags
= self
.getFlags()
1245 self
.constructor
= self
.makeConstructor()
1246 self
.op_decl
= self
.makeDecl()
1249 self
.op_rd
= self
.makeRead()
1250 self
.op_src_decl
= self
.makeDecl()
1253 self
.op_src_decl
= ''
1256 self
.op_wb
= self
.makeWrite()
1257 self
.op_dest_decl
= self
.makeDecl()
1260 self
.op_dest_decl
= ''
1268 def isFloatReg(self
):
1274 def isControlReg(self
):
1278 # note the empty slice '[:]' gives us a copy of self.flags[0]
1279 # instead of a reference to it
1280 my_flags
= self
.flags
[0][:]
1282 my_flags
+= self
.flags
[1]
1284 my_flags
+= self
.flags
[2]
1288 # Note that initializations in the declarations are solely
1289 # to avoid 'uninitialized variable' errors from the compiler.
1290 return self
.ctype
+ ' ' + self
.base_name
+ ' = 0;\n';
1292 class IntRegOperand(Operand
):
1299 def makeConstructor(self
):
1302 c
+= '\n\t_srcRegIdx[%d] = %s;' % \
1303 (self
.src_reg_idx
, self
.reg_spec
)
1305 c
+= '\n\t_destRegIdx[%d] = %s;' % \
1306 (self
.dest_reg_idx
, self
.reg_spec
)
1310 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
1311 error(0, 'Attempt to read integer register as FP')
1312 if self
.read_code
!= None:
1313 return self
.buildReadCode('readIntRegOperand')
1314 if (self
.size
== self
.dflt_size
):
1315 return '%s = xc->readIntRegOperand(this, %d);\n' % \
1316 (self
.base_name
, self
.src_reg_idx
)
1317 elif (self
.size
> self
.dflt_size
):
1318 int_reg_val
= 'xc->readIntRegOperand(this, %d)' % \
1320 if (self
.is_signed
):
1321 int_reg_val
= 'sext<%d>(%s)' % (self
.dflt_size
, int_reg_val
)
1322 return '%s = %s;\n' % (self
.base_name
, int_reg_val
)
1324 return '%s = bits(xc->readIntRegOperand(this, %d), %d, 0);\n' % \
1325 (self
.base_name
, self
.src_reg_idx
, self
.size
-1)
1327 def makeWrite(self
):
1328 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
1329 error(0, 'Attempt to write integer register as FP')
1330 if self
.write_code
!= None:
1331 return self
.buildWriteCode('setIntRegOperand')
1332 if (self
.size
!= self
.dflt_size
and self
.is_signed
):
1333 final_val
= 'sext<%d>(%s)' % (self
.size
, self
.base_name
)
1335 final_val
= self
.base_name
1339 xc->setIntRegOperand(this, %d, final_val);\n
1340 if (traceData) { traceData->setData(final_val); }
1341 }''' % (self
.dflt_ctype
, final_val
, self
.dest_reg_idx
)
1344 class FloatRegOperand(Operand
):
1348 def isFloatReg(self
):
1351 def makeConstructor(self
):
1354 c
+= '\n\t_srcRegIdx[%d] = %s + FP_Base_DepTag;' % \
1355 (self
.src_reg_idx
, self
.reg_spec
)
1357 c
+= '\n\t_destRegIdx[%d] = %s + FP_Base_DepTag;' % \
1358 (self
.dest_reg_idx
, self
.reg_spec
)
1363 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
1364 func
= 'readFloatRegOperand'
1366 func
= 'readFloatRegOperandBits'
1367 if (self
.size
!= self
.dflt_size
):
1369 base
= 'xc->%s(this, %d)' % (func
, self
.src_reg_idx
)
1370 if self
.read_code
!= None:
1371 return self
.buildReadCode(func
)
1373 return '%s = bits(%s, %d, 0);\n' % \
1374 (self
.base_name
, base
, self
.size
-1)
1376 return '%s = %s;\n' % (self
.base_name
, base
)
1378 def makeWrite(self
):
1379 final_val
= self
.base_name
1380 final_ctype
= self
.ctype
1381 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
1382 func
= 'setFloatRegOperand'
1383 elif (self
.ctype
== 'uint32_t' or self
.ctype
== 'uint64_t'):
1384 func
= 'setFloatRegOperandBits'
1386 func
= 'setFloatRegOperandBits'
1387 final_ctype
= 'uint%d_t' % self
.dflt_size
1388 if (self
.size
!= self
.dflt_size
and self
.is_signed
):
1389 final_val
= 'sext<%d>(%s)' % (self
.size
, self
.base_name
)
1390 if self
.write_code
!= None:
1391 return self
.buildWriteCode(func
)
1395 xc->%s(this, %d, final_val);\n
1396 if (traceData) { traceData->setData(final_val); }
1397 }''' % (final_ctype
, final_val
, func
, self
.dest_reg_idx
)
1400 class ControlRegOperand(Operand
):
1404 def isControlReg(self
):
1407 def makeConstructor(self
):
1410 c
+= '\n\t_srcRegIdx[%d] = %s + Ctrl_Base_DepTag;' % \
1411 (self
.src_reg_idx
, self
.reg_spec
)
1413 c
+= '\n\t_destRegIdx[%d] = %s + Ctrl_Base_DepTag;' % \
1414 (self
.dest_reg_idx
, self
.reg_spec
)
1419 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
1420 error(0, 'Attempt to read control register as FP')
1421 if self
.read_code
!= None:
1422 return self
.buildReadCode('readMiscRegOperand')
1423 base
= 'xc->readMiscRegOperand(this, %s)' % self
.src_reg_idx
1424 if self
.size
== self
.dflt_size
:
1425 return '%s = %s;\n' % (self
.base_name
, base
)
1427 return '%s = bits(%s, %d, 0);\n' % \
1428 (self
.base_name
, base
, self
.size
-1)
1430 def makeWrite(self
):
1431 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
1432 error(0, 'Attempt to write control register as FP')
1433 if self
.write_code
!= None:
1434 return self
.buildWriteCode('setMiscRegOperand')
1435 wb
= 'xc->setMiscRegOperand(this, %s, %s);\n' % \
1436 (self
.dest_reg_idx
, self
.base_name
)
1437 wb
+= 'if (traceData) { traceData->setData(%s); }' % \
1441 class MemOperand(Operand
):
1445 def makeConstructor(self
):
1449 # Note that initializations in the declarations are solely
1450 # to avoid 'uninitialized variable' errors from the compiler.
1451 # Declare memory data variable.
1452 if self
.ctype
in ['Twin32_t','Twin64_t']:
1453 return "%s %s; %s.a = 0; %s.b = 0;\n" % (self
.ctype
, self
.base_name
,
1454 self
.base_name
, self
.base_name
)
1455 c
= '%s %s = 0;\n' % (self
.ctype
, self
.base_name
)
1459 if self
.read_code
!= None:
1460 return self
.buildReadCode()
1463 def makeWrite(self
):
1464 if self
.write_code
!= None:
1465 return self
.buildWriteCode()
1468 # Return the memory access size *in bits*, suitable for
1469 # forming a type via "uint%d_t". Divide by 8 if you want bytes.
1470 def makeAccSize(self
):
1473 class PCOperand(Operand
):
1474 def makeConstructor(self
):
1478 return '%s = xc->readPC();\n' % self
.base_name
1480 def makeWrite(self
):
1481 return 'xc->setPC(%s);\n' % self
.base_name
1483 class UPCOperand(Operand
):
1484 def makeConstructor(self
):
1488 if self
.read_code
!= None:
1489 return self
.buildReadCode('readMicroPC')
1490 return '%s = xc->readMicroPC();\n' % self
.base_name
1492 def makeWrite(self
):
1493 if self
.write_code
!= None:
1494 return self
.buildWriteCode('setMicroPC')
1495 return 'xc->setMicroPC(%s);\n' % self
.base_name
1497 class NUPCOperand(Operand
):
1498 def makeConstructor(self
):
1502 if self
.read_code
!= None:
1503 return self
.buildReadCode('readNextMicroPC')
1504 return '%s = xc->readNextMicroPC();\n' % self
.base_name
1506 def makeWrite(self
):
1507 if self
.write_code
!= None:
1508 return self
.buildWriteCode('setNextMicroPC')
1509 return 'xc->setNextMicroPC(%s);\n' % self
.base_name
1511 class NPCOperand(Operand
):
1512 def makeConstructor(self
):
1516 if self
.read_code
!= None:
1517 return self
.buildReadCode('readNextPC')
1518 return '%s = xc->readNextPC();\n' % self
.base_name
1520 def makeWrite(self
):
1521 if self
.write_code
!= None:
1522 return self
.buildWriteCode('setNextPC')
1523 return 'xc->setNextPC(%s);\n' % self
.base_name
1525 class NNPCOperand(Operand
):
1526 def makeConstructor(self
):
1530 if self
.read_code
!= None:
1531 return self
.buildReadCode('readNextNPC')
1532 return '%s = xc->readNextNPC();\n' % self
.base_name
1534 def makeWrite(self
):
1535 if self
.write_code
!= None:
1536 return self
.buildWriteCode('setNextNPC')
1537 return 'xc->setNextNPC(%s);\n' % self
.base_name
1539 def buildOperandNameMap(userDict
, lineno
):
1540 global operandNameMap
1542 for (op_name
, val
) in userDict
.iteritems():
1543 (base_cls_name
, dflt_ext
, reg_spec
, flags
, sort_pri
) = val
[:5]
1554 'error: too many attributes for operand "%s"' %
1557 (dflt_size
, dflt_ctype
, dflt_is_signed
) = operandTypeMap
[dflt_ext
]
1558 # Canonical flag structure is a triple of lists, where each list
1559 # indicates the set of flags implied by this operand always, when
1560 # used as a source, and when used as a dest, respectively.
1561 # For simplicity this can be initialized using a variety of fairly
1562 # obvious shortcuts; we convert these to canonical form here.
1564 # no flags specified (e.g., 'None')
1565 flags
= ( [], [], [] )
1566 elif isinstance(flags
, str):
1567 # a single flag: assumed to be unconditional
1568 flags
= ( [ flags
], [], [] )
1569 elif isinstance(flags
, list):
1570 # a list of flags: also assumed to be unconditional
1571 flags
= ( flags
, [], [] )
1572 elif isinstance(flags
, tuple):
1573 # it's a tuple: it should be a triple,
1574 # but each item could be a single string or a list
1575 (uncond_flags
, src_flags
, dest_flags
) = flags
1576 flags
= (makeList(uncond_flags
),
1577 makeList(src_flags
), makeList(dest_flags
))
1578 # Accumulate attributes of new operand class in tmp_dict
1580 for attr
in ('dflt_ext', 'reg_spec', 'flags', 'sort_pri',
1581 'dflt_size', 'dflt_ctype', 'dflt_is_signed',
1582 'read_code', 'write_code'):
1583 tmp_dict
[attr
] = eval(attr
)
1584 tmp_dict
['base_name'] = op_name
1585 # New class name will be e.g. "IntReg_Ra"
1586 cls_name
= base_cls_name
+ '_' + op_name
1587 # Evaluate string arg to get class object. Note that the
1588 # actual base class for "IntReg" is "IntRegOperand", i.e. we
1589 # have to append "Operand".
1591 base_cls
= eval(base_cls_name
+ 'Operand')
1594 'error: unknown operand base class "%s"' % base_cls_name
)
1595 # The following statement creates a new class called
1596 # <cls_name> as a subclass of <base_cls> with the attributes
1597 # in tmp_dict, just as if we evaluated a class declaration.
1598 operandNameMap
[op_name
] = type(cls_name
, (base_cls
,), tmp_dict
)
1600 # Define operand variables.
1601 operands
= userDict
.keys()
1603 operandsREString
= (r
'''
1604 (?<![\w\.]) # neg. lookbehind assertion: prevent partial matches
1605 ((%s)(?:\.(\w+))?) # match: operand with optional '.' then suffix
1606 (?![\w\.]) # neg. lookahead assertion: prevent partial matches
1608 % string
.join(operands
, '|'))
1611 operandsRE
= re
.compile(operandsREString
, re
.MULTILINE|re
.VERBOSE
)
1613 # Same as operandsREString, but extension is mandatory, and only two
1614 # groups are returned (base and ext, not full name as above).
1615 # Used for subtituting '_' for '.' to make C++ identifiers.
1616 operandsWithExtREString
= (r
'(?<![\w\.])(%s)\.(\w+)(?![\w\.])'
1617 % string
.join(operands
, '|'))
1619 global operandsWithExtRE
1620 operandsWithExtRE
= re
.compile(operandsWithExtREString
, re
.MULTILINE
)
1627 # Find all the operands in the given code block. Returns an operand
1628 # descriptor list (instance of class OperandList).
1629 def __init__(self
, code
):
1632 # delete comments so we don't match on reg specifiers inside
1633 code
= commentRE
.sub('', code
)
1634 # search for operands
1637 match
= operandsRE
.search(code
, next_pos
)
1639 # no more matches: we're done
1642 # regexp groups are operand full name, base, and extension
1643 (op_full
, op_base
, op_ext
) = op
1644 # if the token following the operand is an assignment, this is
1645 # a destination (LHS), else it's a source (RHS)
1646 is_dest
= (assignRE
.match(code
, match
.end()) != None)
1647 is_src
= not is_dest
1648 # see if we've already seen this one
1649 op_desc
= self
.find_base(op_base
)
1651 if op_desc
.ext
!= op_ext
:
1652 error(0, 'Inconsistent extensions for operand %s' % \
1654 op_desc
.is_src
= op_desc
.is_src
or is_src
1655 op_desc
.is_dest
= op_desc
.is_dest
or is_dest
1657 # new operand: create new descriptor
1658 op_desc
= operandNameMap
[op_base
](op_full
, op_ext
,
1660 self
.append(op_desc
)
1661 # start next search after end of current match
1662 next_pos
= match
.end()
1664 # enumerate source & dest register operands... used in building
1667 self
.numDestRegs
= 0
1668 self
.numFPDestRegs
= 0
1669 self
.numIntDestRegs
= 0
1670 self
.memOperand
= None
1671 for op_desc
in self
.items
:
1674 op_desc
.src_reg_idx
= self
.numSrcRegs
1675 self
.numSrcRegs
+= 1
1677 op_desc
.dest_reg_idx
= self
.numDestRegs
1678 self
.numDestRegs
+= 1
1679 if op_desc
.isFloatReg():
1680 self
.numFPDestRegs
+= 1
1681 elif op_desc
.isIntReg():
1682 self
.numIntDestRegs
+= 1
1683 elif op_desc
.isMem():
1685 error(0, "Code block has more than one memory operand.")
1686 self
.memOperand
= op_desc
1687 global maxInstSrcRegs
1688 global maxInstDestRegs
1689 if maxInstSrcRegs
< self
.numSrcRegs
:
1690 maxInstSrcRegs
= self
.numSrcRegs
1691 if maxInstDestRegs
< self
.numDestRegs
:
1692 maxInstDestRegs
= self
.numDestRegs
1693 # now make a final pass to finalize op_desc fields that may depend
1694 # on the register enumeration
1695 for op_desc
in self
.items
:
1699 return len(self
.items
)
1701 def __getitem__(self
, index
):
1702 return self
.items
[index
]
1704 def append(self
, op_desc
):
1705 self
.items
.append(op_desc
)
1706 self
.bases
[op_desc
.base_name
] = op_desc
1708 def find_base(self
, base_name
):
1709 # like self.bases[base_name], but returns None if not found
1710 # (rather than raising exception)
1711 return self
.bases
.get(base_name
)
1713 # internal helper function for concat[Some]Attr{Strings|Lists}
1714 def __internalConcatAttrs(self
, attr_name
, filter, result
):
1715 for op_desc
in self
.items
:
1717 result
+= getattr(op_desc
, attr_name
)
1720 # return a single string that is the concatenation of the (string)
1721 # values of the specified attribute for all operands
1722 def concatAttrStrings(self
, attr_name
):
1723 return self
.__internalConcatAttrs
(attr_name
, lambda x
: 1, '')
1725 # like concatAttrStrings, but only include the values for the operands
1726 # for which the provided filter function returns true
1727 def concatSomeAttrStrings(self
, filter, attr_name
):
1728 return self
.__internalConcatAttrs
(attr_name
, filter, '')
1730 # return a single list that is the concatenation of the (list)
1731 # values of the specified attribute for all operands
1732 def concatAttrLists(self
, attr_name
):
1733 return self
.__internalConcatAttrs
(attr_name
, lambda x
: 1, [])
1735 # like concatAttrLists, but only include the values for the operands
1736 # for which the provided filter function returns true
1737 def concatSomeAttrLists(self
, filter, attr_name
):
1738 return self
.__internalConcatAttrs
(attr_name
, filter, [])
1741 self
.items
.sort(lambda a
, b
: a
.sort_pri
- b
.sort_pri
)
1743 class SubOperandList(OperandList
):
1745 # Find all the operands in the given code block. Returns an operand
1746 # descriptor list (instance of class OperandList).
1747 def __init__(self
, code
, master_list
):
1750 # delete comments so we don't match on reg specifiers inside
1751 code
= commentRE
.sub('', code
)
1752 # search for operands
1755 match
= operandsRE
.search(code
, next_pos
)
1757 # no more matches: we're done
1760 # regexp groups are operand full name, base, and extension
1761 (op_full
, op_base
, op_ext
) = op
1762 # find this op in the master list
1763 op_desc
= master_list
.find_base(op_base
)
1765 error(0, 'Found operand %s which is not in the master list!' \
1766 ' This is an internal error' % \
1769 # See if we've already found this operand
1770 op_desc
= self
.find_base(op_base
)
1772 # if not, add a reference to it to this sub list
1773 self
.append(master_list
.bases
[op_base
])
1775 # start next search after end of current match
1776 next_pos
= match
.end()
1778 self
.memOperand
= None
1779 for op_desc
in self
.items
:
1782 error(0, "Code block has more than one memory operand.")
1783 self
.memOperand
= op_desc
1785 # Regular expression object to match C++ comments
1786 # (used in findOperands())
1787 commentRE
= re
.compile(r
'//.*\n')
1789 # Regular expression object to match assignment statements
1790 # (used in findOperands())
1791 assignRE
= re
.compile(r
'\s*=(?!=)', re
.MULTILINE
)
1793 # Munge operand names in code string to make legal C++ variable names.
1794 # This means getting rid of the type extension if any.
1795 # (Will match base_name attribute of Operand object.)
1796 def substMungedOpNames(code
):
1797 return operandsWithExtRE
.sub(r
'\1', code
)
1799 # Fix up code snippets for final substitution in templates.
1800 def mungeSnippet(s
):
1801 if isinstance(s
, str):
1802 return substMungedOpNames(substBitOps(s
))
1806 def makeFlagConstructor(flag_list
):
1807 if len(flag_list
) == 0:
1809 # filter out repeated flags
1812 while i
< len(flag_list
):
1813 if flag_list
[i
] == flag_list
[i
-1]:
1819 code
= pre
+ string
.join(flag_list
, post
+ pre
) + post
1822 # Assume all instruction flags are of the form 'IsFoo'
1823 instFlagRE
= re
.compile(r
'Is.*')
1825 # OpClass constants end in 'Op' except No_OpClass
1826 opClassRE
= re
.compile(r
'.*Op|No_OpClass')
1828 class InstObjParams
:
1829 def __init__(self
, mnem
, class_name
, base_class
= '',
1830 snippets
= {}, opt_args
= []):
1831 self
.mnemonic
= mnem
1832 self
.class_name
= class_name
1833 self
.base_class
= base_class
1834 if not isinstance(snippets
, dict):
1835 snippets
= {'code' : snippets
}
1836 compositeCode
= ' '.join(map(str, snippets
.values()))
1837 self
.snippets
= snippets
1839 self
.operands
= OperandList(compositeCode
)
1840 self
.constructor
= self
.operands
.concatAttrStrings('constructor')
1841 self
.constructor
+= \
1842 '\n\t_numSrcRegs = %d;' % self
.operands
.numSrcRegs
1843 self
.constructor
+= \
1844 '\n\t_numDestRegs = %d;' % self
.operands
.numDestRegs
1845 self
.constructor
+= \
1846 '\n\t_numFPDestRegs = %d;' % self
.operands
.numFPDestRegs
1847 self
.constructor
+= \
1848 '\n\t_numIntDestRegs = %d;' % self
.operands
.numIntDestRegs
1849 self
.flags
= self
.operands
.concatAttrLists('flags')
1851 # Make a basic guess on the operand class (function unit type).
1852 # These are good enough for most cases, and can be overridden
1854 if 'IsStore' in self
.flags
:
1855 self
.op_class
= 'MemWriteOp'
1856 elif 'IsLoad' in self
.flags
or 'IsPrefetch' in self
.flags
:
1857 self
.op_class
= 'MemReadOp'
1858 elif 'IsFloating' in self
.flags
:
1859 self
.op_class
= 'FloatAddOp'
1861 self
.op_class
= 'IntAluOp'
1863 # Optional arguments are assumed to be either StaticInst flags
1864 # or an OpClass value. To avoid having to import a complete
1865 # list of these values to match against, we do it ad-hoc
1868 if instFlagRE
.match(oa
):
1869 self
.flags
.append(oa
)
1870 elif opClassRE
.match(oa
):
1873 error(0, 'InstObjParams: optional arg "%s" not recognized '
1874 'as StaticInst::Flag or OpClass.' % oa
)
1876 # add flag initialization to contructor here to include
1877 # any flags added via opt_args
1878 self
.constructor
+= makeFlagConstructor(self
.flags
)
1880 # if 'IsFloating' is set, add call to the FP enable check
1881 # function (which should be provided by isa_desc via a declare)
1882 if 'IsFloating' in self
.flags
:
1883 self
.fp_enable_check
= 'fault = checkFpEnableFault(xc);'
1885 self
.fp_enable_check
= ''
1887 #######################
1889 # Output file template
1894 * DO NOT EDIT THIS FILE!!!
1896 * It was automatically generated from the ISA description in %(filename)s
1903 namespace %(namespace)s {
1905 %(namespace_output)s
1907 } // namespace %(namespace)s
1912 max_inst_regs_template
= '''
1914 * DO NOT EDIT THIS FILE!!!
1916 * It was automatically generated from the ISA description in %(filename)s
1919 namespace %(namespace)s {
1921 const int MaxInstSrcRegs = %(MaxInstSrcRegs)d;
1922 const int MaxInstDestRegs = %(MaxInstDestRegs)d;
1924 } // namespace %(namespace)s
1929 # Update the output file only if the new contents are different from
1930 # the current contents. Minimizes the files that need to be rebuilt
1931 # after minor changes.
1932 def update_if_needed(file, contents
):
1934 if os
.access(file, os
.R_OK
):
1936 old_contents
= f
.read()
1938 if contents
!= old_contents
:
1939 print 'Updating', file
1940 os
.remove(file) # in case it's write-protected
1943 print 'File', file, 'is unchanged'
1945 print 'Generating', file
1952 # This regular expression matches '##include' directives
1953 includeRE
= re
.compile(r
'^\s*##include\s+"(?P<filename>[\w/.-]*)".*$',
1956 # Function to replace a matched '##include' directive with the
1957 # contents of the specified file (with nested ##includes replaced
1958 # recursively). 'matchobj' is an re match object (from a match of
1959 # includeRE) and 'dirname' is the directory relative to which the file
1960 # path should be resolved.
1961 def replace_include(matchobj
, dirname
):
1962 fname
= matchobj
.group('filename')
1963 full_fname
= os
.path
.normpath(os
.path
.join(dirname
, fname
))
1964 contents
= '##newfile "%s"\n%s\n##endfile\n' % \
1965 (full_fname
, read_and_flatten(full_fname
))
1968 # Read a file and recursively flatten nested '##include' files.
1969 def read_and_flatten(filename
):
1970 current_dir
= os
.path
.dirname(filename
)
1972 contents
= open(filename
).read()
1974 error(0, 'Error including file "%s"' % filename
)
1975 fileNameStack
.push((filename
, 0))
1976 # Find any includes and include them
1977 contents
= includeRE
.sub(lambda m
: replace_include(m
, current_dir
),
1983 # Read in and parse the ISA description.
1985 def parse_isa_desc(isa_desc_file
, output_dir
):
1986 # Read file and (recursively) all included files into a string.
1987 # PLY requires that the input be in a single string so we have to
1989 isa_desc
= read_and_flatten(isa_desc_file
)
1991 # Initialize filename stack with outer file.
1992 fileNameStack
.push((isa_desc_file
, 0))
1995 (isa_name
, namespace
, global_code
, namespace_code
) = parser
.parse(isa_desc
)
1997 # grab the last three path components of isa_desc_file to put in
1999 filename
= '/'.join(isa_desc_file
.split('/')[-3:])
2001 # generate decoder.hh
2002 includes
= '#include "base/bitfield.hh" // for bitfield support'
2003 global_output
= global_code
.header_output
2004 namespace_output
= namespace_code
.header_output
2005 decode_function
= ''
2006 update_if_needed(output_dir
+ '/decoder.hh', file_template
% vars())
2008 # generate decoder.cc
2009 includes
= '#include "decoder.hh"'
2010 global_output
= global_code
.decoder_output
2011 namespace_output
= namespace_code
.decoder_output
2012 # namespace_output += namespace_code.decode_block
2013 decode_function
= namespace_code
.decode_block
2014 update_if_needed(output_dir
+ '/decoder.cc', file_template
% vars())
2016 # generate per-cpu exec files
2017 for cpu
in cpu_models
:
2018 includes
= '#include "decoder.hh"\n'
2019 includes
+= cpu
.includes
2020 global_output
= global_code
.exec_output
[cpu
.name
]
2021 namespace_output
= namespace_code
.exec_output
[cpu
.name
]
2022 decode_function
= ''
2023 update_if_needed(output_dir
+ '/' + cpu
.filename
,
2024 file_template
% vars())
2026 # The variable names here are hacky, but this will creat local variables
2027 # which will be referenced in vars() which have the value of the globals.
2028 global maxInstSrcRegs
2029 MaxInstSrcRegs
= maxInstSrcRegs
2030 global maxInstDestRegs
2031 MaxInstDestRegs
= maxInstDestRegs
2033 update_if_needed(output_dir
+ '/max_inst_regs.hh', \
2034 max_inst_regs_template
% vars())
2036 # global list of CpuModel objects (see cpu_models.py)
2039 # Called as script: get args from command line.
2040 # Args are: <path to cpu_models.py> <isa desc file> <output dir> <cpu models>
2041 if __name__
== '__main__':
2042 execfile(sys
.argv
[1]) # read in CpuModel definitions
2043 cpu_models
= [CpuModel
.dict[cpu
] for cpu
in sys
.argv
[4:]]
2044 parse_isa_desc(sys
.argv
[2], sys
.argv
[3])