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