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