1 # Copyright (c) 2003-2005 The Regents of The University of Michigan
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # Authors: Steve Reinhardt
33 import inspect
, traceback
37 from m5
.util
.grammar
import Grammar
45 # Indent every line in string 's' by two spaces
46 # (except preprocessor directives).
47 # Used to make nested code blocks look pretty.
50 return re
.sub(r
'(?m)^(?!#)', ' ', s
)
53 # Munge a somewhat arbitrarily formatted piece of Python code
54 # (e.g. from a format 'let' block) into something whose indentation
55 # will get by the Python parser.
57 # The two keys here are that Python will give a syntax error if
58 # there's any whitespace at the beginning of the first line, and that
59 # all lines at the same lexical nesting level must have identical
60 # indentation. Unfortunately the way code literals work, an entire
61 # let block tends to have some initial indentation. Rather than
62 # trying to figure out what that is and strip it off, we prepend 'if
63 # 1:' to make the let code the nested block inside the if (and have
64 # the parser automatically deal with the indentation for us).
66 # We don't want to do this if (1) the code block is empty or (2) the
67 # first line of the block doesn't have any whitespace at the front.
69 def fixPythonIndentation(s
):
70 # get rid of blank lines first
71 s
= re
.sub(r
'(?m)^\s*\n', '', s
);
72 if (s
!= '' and re
.match(r
'[ \t]', s
[0])):
76 class ISAParserError(Exception):
77 """Error handler for parser errors"""
78 def __init__(self
, first
, second
=None):
83 if hasattr(first
, 'lexer'):
84 first
= first
.lexer
.lineno
88 def display(self
, filename_stack
, print_traceback
=debug
):
89 # Output formatted to work under Emacs compile-mode. Optional
90 # 'print_traceback' arg, if set to True, prints a Python stack
91 # backtrace too (can be handy when trying to debug the parser
95 for (filename
, line
) in filename_stack
[:-1]:
96 print "%sIn file included from %s:" % (spaces
, filename
)
99 # Print a Python stack backtrace if requested.
100 if print_traceback
or not self
.lineno
:
101 traceback
.print_exc()
103 line_str
= "%s:" % (filename_stack
[-1][0], )
105 line_str
+= "%d:" % (self
.lineno
, )
107 return "%s%s %s" % (spaces
, line_str
, self
.string
)
109 def exit(self
, filename_stack
, print_traceback
=debug
):
112 sys
.exit(self
.display(filename_stack
, print_traceback
))
115 raise ISAParserError(*args
)
120 # Template objects are format strings that allow substitution from
121 # the attribute spaces of other objects (e.g. InstObjParams instances).
123 labelRE
= re
.compile(r
'(?<!%)%\(([^\)]+)\)[sd]')
125 class Template(object):
126 def __init__(self
, parser
, t
):
133 # Protect non-Python-dict substitutions (e.g. if there's a printf
134 # in the templated C++ code)
135 template
= self
.parser
.protectNonSubstPercents(self
.template
)
136 # CPU-model-specific substitutions are handled later (in GenCode).
137 template
= self
.parser
.protectCpuSymbols(template
)
139 # Build a dict ('myDict') to use for the template substitution.
140 # Start with the template namespace. Make a copy since we're
141 # going to modify it.
142 myDict
= self
.parser
.templateMap
.copy()
144 if isinstance(d
, InstObjParams
):
145 # If we're dealing with an InstObjParams object, we need
146 # to be a little more sophisticated. The instruction-wide
147 # parameters are already formed, but the parameters which
148 # are only function wide still need to be generated.
151 myDict
.update(d
.__dict
__)
152 # The "operands" and "snippets" attributes of the InstObjParams
153 # objects are for internal use and not substitution.
154 del myDict
['operands']
155 del myDict
['snippets']
157 snippetLabels
= [l
for l
in labelRE
.findall(template
)
158 if d
.snippets
.has_key(l
)]
160 snippets
= dict([(s
, self
.parser
.mungeSnippet(d
.snippets
[s
]))
161 for s
in snippetLabels
])
163 myDict
.update(snippets
)
165 compositeCode
= ' '.join(map(str, snippets
.values()))
167 # Add in template itself in case it references any
168 # operands explicitly (like Mem)
169 compositeCode
+= ' ' + template
171 operands
= SubOperandList(self
.parser
, compositeCode
, d
.operands
)
173 myDict
['op_decl'] = operands
.concatAttrStrings('op_decl')
174 if operands
.readPC
or operands
.setPC
:
175 myDict
['op_decl'] += 'TheISA::PCState __parserAutoPCState;\n'
177 is_src
= lambda op
: op
.is_src
178 is_dest
= lambda op
: op
.is_dest
180 myDict
['op_src_decl'] = \
181 operands
.concatSomeAttrStrings(is_src
, 'op_src_decl')
182 myDict
['op_dest_decl'] = \
183 operands
.concatSomeAttrStrings(is_dest
, 'op_dest_decl')
185 myDict
['op_src_decl'] += \
186 'TheISA::PCState __parserAutoPCState;\n'
188 myDict
['op_dest_decl'] += \
189 'TheISA::PCState __parserAutoPCState;\n'
191 myDict
['op_rd'] = operands
.concatAttrStrings('op_rd')
193 myDict
['op_rd'] = '__parserAutoPCState = xc->pcState();\n' + \
196 # Compose the op_wb string. If we're going to write back the
197 # PC state because we changed some of its elements, we'll need to
198 # do that as early as possible. That allows later uncoordinated
199 # modifications to the PC to layer appropriately.
200 reordered
= list(operands
.items
)
203 pcWbStr
= 'xc->pcState(__parserAutoPCState);\n'
204 for op_desc
in reordered
:
205 if op_desc
.isPCPart() and op_desc
.is_dest
:
206 op_wb_str
= op_desc
.op_wb
+ pcWbStr
+ op_wb_str
209 op_wb_str
= op_desc
.op_wb
+ op_wb_str
210 myDict
['op_wb'] = op_wb_str
212 elif isinstance(d
, dict):
213 # if the argument is a dictionary, we just use it.
215 elif hasattr(d
, '__dict__'):
216 # if the argument is an object, we use its attribute map.
217 myDict
.update(d
.__dict
__)
219 raise TypeError, "Template.subst() arg must be or have dictionary"
220 return template
% myDict
222 # Convert to string. This handles the case when a template with a
223 # CPU-specific term gets interpolated into another template or into
226 return self
.parser
.expandCpuSymbolsToString(self
.template
)
231 # A format object encapsulates an instruction format. It must provide
232 # a defineInst() method that generates the code for an instruction
235 class Format(object):
236 def __init__(self
, id, params
, code
):
239 label
= 'def format ' + id
240 self
.user_code
= compile(fixPythonIndentation(code
), label
, 'exec')
241 param_list
= string
.join(params
, ", ")
242 f
= '''def defInst(_code, _context, %s):
243 my_locals = vars().copy()
244 exec _code in _context, my_locals
245 return my_locals\n''' % param_list
246 c
= compile(f
, label
+ ' wrapper', 'exec')
250 def defineInst(self
, parser
, name
, args
, lineno
):
251 parser
.updateExportContext()
252 context
= parser
.exportContext
.copy()
254 Name
= name
[0].upper()
257 context
.update({ 'name' : name
, 'Name' : Name
})
259 vars = self
.func(self
.user_code
, context
, *args
[0], **args
[1])
260 except Exception, exc
:
263 error(lineno
, 'error defining "%s": %s.' % (name
, exc
))
264 for k
in vars.keys():
265 if k
not in ('header_output', 'decoder_output',
266 'exec_output', 'decode_block'):
268 return GenCode(parser
, **vars)
270 # Special null format to catch an implicit-format instruction
271 # definition outside of any format block.
272 class NoFormat(object):
274 self
.defaultInst
= ''
276 def defineInst(self
, parser
, name
, args
, lineno
):
278 'instruction definition "%s" with no active format!' % name
)
283 # The GenCode class encapsulates generated code destined for various
284 # output files. The header_output and decoder_output attributes are
285 # strings containing code destined for decoder.hh and decoder.cc
286 # respectively. The decode_block attribute contains code to be
287 # incorporated in the decode function itself (that will also end up in
288 # decoder.cc). The exec_output attribute is a dictionary with a key
289 # for each CPU model name; the value associated with a particular key
290 # is the string of code for that CPU model's exec.cc file. The
291 # has_decode_default attribute is used in the decode block to allow
292 # explicit default clauses to override default default clauses.
294 class GenCode(object):
295 # Constructor. At this point we substitute out all CPU-specific
296 # symbols. For the exec output, these go into the per-model
297 # dictionary. For all other output types they get collapsed into
299 def __init__(self
, parser
,
300 header_output
= '', decoder_output
= '', exec_output
= '',
301 decode_block
= '', has_decode_default
= False):
303 self
.header_output
= parser
.expandCpuSymbolsToString(header_output
)
304 self
.decoder_output
= parser
.expandCpuSymbolsToString(decoder_output
)
305 if isinstance(exec_output
, dict):
306 self
.exec_output
= exec_output
307 elif isinstance(exec_output
, str):
308 # If the exec_output arg is a single string, we replicate
309 # it for each of the CPU models, substituting and
310 # %(CPU_foo)s params appropriately.
311 self
.exec_output
= parser
.expandCpuSymbolsToDict(exec_output
)
312 self
.decode_block
= parser
.expandCpuSymbolsToString(decode_block
)
313 self
.has_decode_default
= has_decode_default
315 # Override '+' operator: generate a new GenCode object that
316 # concatenates all the individual strings in the operands.
317 def __add__(self
, other
):
319 for cpu
in self
.parser
.cpuModels
:
321 exec_output
[n
] = self
.exec_output
[n
] + other
.exec_output
[n
]
322 return GenCode(self
.parser
,
323 self
.header_output
+ other
.header_output
,
324 self
.decoder_output
+ other
.decoder_output
,
326 self
.decode_block
+ other
.decode_block
,
327 self
.has_decode_default
or other
.has_decode_default
)
329 # Prepend a string (typically a comment) to all the strings.
330 def prepend_all(self
, pre
):
331 self
.header_output
= pre
+ self
.header_output
332 self
.decoder_output
= pre
+ self
.decoder_output
333 self
.decode_block
= pre
+ self
.decode_block
334 for cpu
in self
.parser
.cpuModels
:
335 self
.exec_output
[cpu
.name
] = pre
+ self
.exec_output
[cpu
.name
]
337 # Wrap the decode block in a pair of strings (e.g., 'case foo:'
338 # and 'break;'). Used to build the big nested switch statement.
339 def wrap_decode_block(self
, pre
, post
= ''):
340 self
.decode_block
= pre
+ indent(self
.decode_block
) + post
342 #####################################################################
344 # Bitfield Operator Support
346 #####################################################################
348 bitOp1ArgRE
= re
.compile(r
'<\s*(\w+)\s*:\s*>')
350 bitOpWordRE
= re
.compile(r
'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>')
351 bitOpExprRE
= re
.compile(r
'\)<\s*(\w+)\s*:\s*(\w+)\s*>')
353 def substBitOps(code
):
354 # first convert single-bit selectors to two-index form
355 # i.e., <n> --> <n:n>
356 code
= bitOp1ArgRE
.sub(r
'<\1:\1>', code
)
357 # simple case: selector applied to ID (name)
358 # i.e., foo<a:b> --> bits(foo, a, b)
359 code
= bitOpWordRE
.sub(r
'bits(\1, \2, \3)', code
)
360 # if selector is applied to expression (ending in ')'),
361 # we need to search backward for matching '('
362 match
= bitOpExprRE
.search(code
)
364 exprEnd
= match
.start()
368 if code
[here
] == '(':
370 elif code
[here
] == ')':
374 sys
.exit("Didn't find '('!")
376 newExpr
= r
'bits(%s, %s, %s)' % (code
[exprStart
:exprEnd
+1],
377 match
.group(1), match
.group(2))
378 code
= code
[:exprStart
] + newExpr
+ code
[match
.end():]
379 match
= bitOpExprRE
.search(code
)
383 #####################################################################
387 # The remaining code is the support for automatically extracting
388 # instruction characteristics from pseudocode.
390 #####################################################################
392 # Force the argument to be a list. Useful for flags, where a caller
393 # can specify a singleton flag or a list of flags. Also usful for
394 # converting tuples to lists so they can be modified.
396 if isinstance(arg
, list):
398 elif isinstance(arg
, tuple):
405 class Operand(object):
406 '''Base class for operand descriptors. An instance of this class
407 (or actually a class derived from this one) represents a specific
408 operand for a code block (e.g, "Rc.sq" as a dest). Intermediate
409 derived classes encapsulates the traits of a particular operand
410 type (e.g., "32-bit integer register").'''
412 def buildReadCode(self
, func
= None):
413 subst_dict
= {"name": self
.base_name
,
415 "reg_idx": self
.reg_spec
,
417 if hasattr(self
, 'src_reg_idx'):
418 subst_dict
['op_idx'] = self
.src_reg_idx
419 code
= self
.read_code
% subst_dict
420 return '%s = %s;\n' % (self
.base_name
, code
)
422 def buildWriteCode(self
, func
= None):
423 subst_dict
= {"name": self
.base_name
,
425 "reg_idx": self
.reg_spec
,
427 "final_val": self
.base_name
}
428 if hasattr(self
, 'dest_reg_idx'):
429 subst_dict
['op_idx'] = self
.dest_reg_idx
430 code
= self
.write_code
% subst_dict
435 if (traceData) { traceData->setData(final_val); }
436 }''' % (self
.dflt_ctype
, self
.base_name
, code
)
438 def __init__(self
, parser
, full_name
, ext
, is_src
, is_dest
):
439 self
.full_name
= full_name
442 self
.is_dest
= is_dest
443 # The 'effective extension' (eff_ext) is either the actual
444 # extension, if one was explicitly provided, or the default.
447 elif hasattr(self
, 'dflt_ext'):
448 self
.eff_ext
= self
.dflt_ext
450 if hasattr(self
, 'eff_ext'):
451 self
.ctype
= parser
.operandTypeMap
[self
.eff_ext
]
453 # Finalize additional fields (primarily code fields). This step
454 # is done separately since some of these fields may depend on the
455 # register index enumeration that hasn't been performed yet at the
456 # time of __init__().
458 self
.flags
= self
.getFlags()
459 self
.constructor
= self
.makeConstructor()
460 self
.op_decl
= self
.makeDecl()
463 self
.op_rd
= self
.makeRead()
464 self
.op_src_decl
= self
.makeDecl()
467 self
.op_src_decl
= ''
470 self
.op_wb
= self
.makeWrite()
471 self
.op_dest_decl
= self
.makeDecl()
474 self
.op_dest_decl
= ''
482 def isFloatReg(self
):
488 def isControlReg(self
):
495 return self
.isPCState() and self
.reg_spec
498 # note the empty slice '[:]' gives us a copy of self.flags[0]
499 # instead of a reference to it
500 my_flags
= self
.flags
[0][:]
502 my_flags
+= self
.flags
[1]
504 my_flags
+= self
.flags
[2]
508 # Note that initializations in the declarations are solely
509 # to avoid 'uninitialized variable' errors from the compiler.
510 return self
.ctype
+ ' ' + self
.base_name
+ ' = 0;\n';
512 class IntRegOperand(Operand
):
519 def makeConstructor(self
):
522 c
+= '\n\t_srcRegIdx[%d] = %s;' % \
523 (self
.src_reg_idx
, self
.reg_spec
)
525 c
+= '\n\t_destRegIdx[%d] = %s;' % \
526 (self
.dest_reg_idx
, self
.reg_spec
)
530 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
531 error('Attempt to read integer register as FP')
532 if self
.read_code
!= None:
533 return self
.buildReadCode('readIntRegOperand')
534 int_reg_val
= 'xc->readIntRegOperand(this, %d)' % self
.src_reg_idx
535 return '%s = %s;\n' % (self
.base_name
, int_reg_val
)
538 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
539 error('Attempt to write integer register as FP')
540 if self
.write_code
!= None:
541 return self
.buildWriteCode('setIntRegOperand')
545 xc->setIntRegOperand(this, %d, final_val);\n
546 if (traceData) { traceData->setData(final_val); }
547 }''' % (self
.ctype
, self
.base_name
, self
.dest_reg_idx
)
550 class FloatRegOperand(Operand
):
554 def isFloatReg(self
):
557 def makeConstructor(self
):
560 c
+= '\n\t_srcRegIdx[%d] = %s + FP_Base_DepTag;' % \
561 (self
.src_reg_idx
, self
.reg_spec
)
563 c
+= '\n\t_destRegIdx[%d] = %s + FP_Base_DepTag;' % \
564 (self
.dest_reg_idx
, self
.reg_spec
)
569 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
570 func
= 'readFloatRegOperand'
572 func
= 'readFloatRegOperandBits'
573 if self
.read_code
!= None:
574 return self
.buildReadCode(func
)
575 return '%s = xc->%s(this, %d);\n' % \
576 (self
.base_name
, func
, self
.src_reg_idx
)
579 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
580 func
= 'setFloatRegOperand'
582 func
= 'setFloatRegOperandBits'
583 if self
.write_code
!= None:
584 return self
.buildWriteCode(func
)
588 xc->%s(this, %d, final_val);\n
589 if (traceData) { traceData->setData(final_val); }
590 }''' % (self
.ctype
, self
.base_name
, func
, self
.dest_reg_idx
)
593 class ControlRegOperand(Operand
):
597 def isControlReg(self
):
600 def makeConstructor(self
):
603 c
+= '\n\t_srcRegIdx[%d] = %s + Ctrl_Base_DepTag;' % \
604 (self
.src_reg_idx
, self
.reg_spec
)
606 c
+= '\n\t_destRegIdx[%d] = %s + Ctrl_Base_DepTag;' % \
607 (self
.dest_reg_idx
, self
.reg_spec
)
612 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
613 error('Attempt to read control register as FP')
614 if self
.read_code
!= None:
615 return self
.buildReadCode('readMiscRegOperand')
616 return '%s = xc->readMiscRegOperand(this, %s);\n' % \
617 (self
.base_name
, self
.src_reg_idx
)
620 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
621 error('Attempt to write control register as FP')
622 if self
.write_code
!= None:
623 return self
.buildWriteCode('setMiscRegOperand')
624 wb
= 'xc->setMiscRegOperand(this, %s, %s);\n' % \
625 (self
.dest_reg_idx
, self
.base_name
)
626 wb
+= 'if (traceData) { traceData->setData(%s); }' % \
630 class MemOperand(Operand
):
634 def makeConstructor(self
):
638 # Note that initializations in the declarations are solely
639 # to avoid 'uninitialized variable' errors from the compiler.
640 # Declare memory data variable.
641 return '%s %s = 0;\n' % (self
.ctype
, self
.base_name
)
644 if self
.read_code
!= None:
645 return self
.buildReadCode()
649 if self
.write_code
!= None:
650 return self
.buildWriteCode()
653 class PCStateOperand(Operand
):
654 def makeConstructor(self
):
659 # A component of the PC state.
660 return '%s = __parserAutoPCState.%s();\n' % \
661 (self
.base_name
, self
.reg_spec
)
663 # The whole PC state itself.
664 return '%s = xc->pcState();\n' % self
.base_name
668 # A component of the PC state.
669 return '__parserAutoPCState.%s(%s);\n' % \
670 (self
.reg_spec
, self
.base_name
)
672 # The whole PC state itself.
673 return 'xc->pcState(%s);\n' % self
.base_name
676 ctype
= 'TheISA::PCState'
679 return "%s %s;\n" % (ctype
, self
.base_name
)
684 class OperandList(object):
685 '''Find all the operands in the given code block. Returns an operand
686 descriptor list (instance of class OperandList).'''
687 def __init__(self
, parser
, code
):
690 # delete comments so we don't match on reg specifiers inside
691 code
= commentRE
.sub('', code
)
692 # search for operands
695 match
= parser
.operandsRE
.search(code
, next_pos
)
697 # no more matches: we're done
700 # regexp groups are operand full name, base, and extension
701 (op_full
, op_base
, op_ext
) = op
702 # if the token following the operand is an assignment, this is
703 # a destination (LHS), else it's a source (RHS)
704 is_dest
= (assignRE
.match(code
, match
.end()) != None)
706 # see if we've already seen this one
707 op_desc
= self
.find_base(op_base
)
709 if op_desc
.ext
!= op_ext
:
710 error('Inconsistent extensions for operand %s' % \
712 op_desc
.is_src
= op_desc
.is_src
or is_src
713 op_desc
.is_dest
= op_desc
.is_dest
or is_dest
715 # new operand: create new descriptor
716 op_desc
= parser
.operandNameMap
[op_base
](parser
,
717 op_full
, op_ext
, is_src
, is_dest
)
719 # start next search after end of current match
720 next_pos
= match
.end()
722 # enumerate source & dest register operands... used in building
726 self
.numFPDestRegs
= 0
727 self
.numIntDestRegs
= 0
728 self
.memOperand
= None
729 for op_desc
in self
.items
:
732 op_desc
.src_reg_idx
= self
.numSrcRegs
735 op_desc
.dest_reg_idx
= self
.numDestRegs
736 self
.numDestRegs
+= 1
737 if op_desc
.isFloatReg():
738 self
.numFPDestRegs
+= 1
739 elif op_desc
.isIntReg():
740 self
.numIntDestRegs
+= 1
741 elif op_desc
.isMem():
743 error("Code block has more than one memory operand.")
744 self
.memOperand
= op_desc
745 if parser
.maxInstSrcRegs
< self
.numSrcRegs
:
746 parser
.maxInstSrcRegs
= self
.numSrcRegs
747 if parser
.maxInstDestRegs
< self
.numDestRegs
:
748 parser
.maxInstDestRegs
= self
.numDestRegs
749 # now make a final pass to finalize op_desc fields that may depend
750 # on the register enumeration
751 for op_desc
in self
.items
:
755 return len(self
.items
)
757 def __getitem__(self
, index
):
758 return self
.items
[index
]
760 def append(self
, op_desc
):
761 self
.items
.append(op_desc
)
762 self
.bases
[op_desc
.base_name
] = op_desc
764 def find_base(self
, base_name
):
765 # like self.bases[base_name], but returns None if not found
766 # (rather than raising exception)
767 return self
.bases
.get(base_name
)
769 # internal helper function for concat[Some]Attr{Strings|Lists}
770 def __internalConcatAttrs(self
, attr_name
, filter, result
):
771 for op_desc
in self
.items
:
773 result
+= getattr(op_desc
, attr_name
)
776 # return a single string that is the concatenation of the (string)
777 # values of the specified attribute for all operands
778 def concatAttrStrings(self
, attr_name
):
779 return self
.__internalConcatAttrs
(attr_name
, lambda x
: 1, '')
781 # like concatAttrStrings, but only include the values for the operands
782 # for which the provided filter function returns true
783 def concatSomeAttrStrings(self
, filter, attr_name
):
784 return self
.__internalConcatAttrs
(attr_name
, filter, '')
786 # return a single list that is the concatenation of the (list)
787 # values of the specified attribute for all operands
788 def concatAttrLists(self
, attr_name
):
789 return self
.__internalConcatAttrs
(attr_name
, lambda x
: 1, [])
791 # like concatAttrLists, but only include the values for the operands
792 # for which the provided filter function returns true
793 def concatSomeAttrLists(self
, filter, attr_name
):
794 return self
.__internalConcatAttrs
(attr_name
, filter, [])
797 self
.items
.sort(lambda a
, b
: a
.sort_pri
- b
.sort_pri
)
799 class SubOperandList(OperandList
):
800 '''Find all the operands in the given code block. Returns an operand
801 descriptor list (instance of class OperandList).'''
802 def __init__(self
, parser
, code
, master_list
):
805 # delete comments so we don't match on reg specifiers inside
806 code
= commentRE
.sub('', code
)
807 # search for operands
810 match
= parser
.operandsRE
.search(code
, next_pos
)
812 # no more matches: we're done
815 # regexp groups are operand full name, base, and extension
816 (op_full
, op_base
, op_ext
) = op
817 # find this op in the master list
818 op_desc
= master_list
.find_base(op_base
)
820 error('Found operand %s which is not in the master list!' \
821 ' This is an internal error' % op_base
)
823 # See if we've already found this operand
824 op_desc
= self
.find_base(op_base
)
826 # if not, add a reference to it to this sub list
827 self
.append(master_list
.bases
[op_base
])
829 # start next search after end of current match
830 next_pos
= match
.end()
832 self
.memOperand
= None
833 # Whether the whole PC needs to be read so parts of it can be accessed
835 # Whether the whole PC needs to be written after parts of it were
838 # Whether this instruction manipulates the whole PC or parts of it.
839 # Mixing the two is a bad idea and flagged as an error.
841 for op_desc
in self
.items
:
842 if op_desc
.isPCPart():
846 if op_desc
.isPCState():
847 if self
.pcPart
is not None:
848 if self
.pcPart
and not op_desc
.isPCPart() or \
849 not self
.pcPart
and op_desc
.isPCPart():
850 error("Mixed whole and partial PC state operands.")
851 self
.pcPart
= op_desc
.isPCPart()
854 error("Code block has more than one memory operand.")
855 self
.memOperand
= op_desc
857 # Regular expression object to match C++ comments
858 # (used in findOperands())
859 commentRE
= re
.compile(r
'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
860 re
.DOTALL | re
.MULTILINE
)
862 # Regular expression object to match assignment statements
863 # (used in findOperands())
864 assignRE
= re
.compile(r
'\s*=(?!=)', re
.MULTILINE
)
866 def makeFlagConstructor(flag_list
):
867 if len(flag_list
) == 0:
869 # filter out repeated flags
872 while i
< len(flag_list
):
873 if flag_list
[i
] == flag_list
[i
-1]:
879 code
= pre
+ string
.join(flag_list
, post
+ pre
) + post
882 # Assume all instruction flags are of the form 'IsFoo'
883 instFlagRE
= re
.compile(r
'Is.*')
885 # OpClass constants end in 'Op' except No_OpClass
886 opClassRE
= re
.compile(r
'.*Op|No_OpClass')
888 class InstObjParams(object):
889 def __init__(self
, parser
, mnem
, class_name
, base_class
= '',
890 snippets
= {}, opt_args
= []):
892 self
.class_name
= class_name
893 self
.base_class
= base_class
894 if not isinstance(snippets
, dict):
895 snippets
= {'code' : snippets
}
896 compositeCode
= ' '.join(map(str, snippets
.values()))
897 self
.snippets
= snippets
899 self
.operands
= OperandList(parser
, compositeCode
)
900 self
.constructor
= self
.operands
.concatAttrStrings('constructor')
901 self
.constructor
+= \
902 '\n\t_numSrcRegs = %d;' % self
.operands
.numSrcRegs
903 self
.constructor
+= \
904 '\n\t_numDestRegs = %d;' % self
.operands
.numDestRegs
905 self
.constructor
+= \
906 '\n\t_numFPDestRegs = %d;' % self
.operands
.numFPDestRegs
907 self
.constructor
+= \
908 '\n\t_numIntDestRegs = %d;' % self
.operands
.numIntDestRegs
909 self
.flags
= self
.operands
.concatAttrLists('flags')
911 # Make a basic guess on the operand class (function unit type).
912 # These are good enough for most cases, and can be overridden
914 if 'IsStore' in self
.flags
:
915 self
.op_class
= 'MemWriteOp'
916 elif 'IsLoad' in self
.flags
or 'IsPrefetch' in self
.flags
:
917 self
.op_class
= 'MemReadOp'
918 elif 'IsFloating' in self
.flags
:
919 self
.op_class
= 'FloatAddOp'
921 self
.op_class
= 'IntAluOp'
923 # Optional arguments are assumed to be either StaticInst flags
924 # or an OpClass value. To avoid having to import a complete
925 # list of these values to match against, we do it ad-hoc
928 if instFlagRE
.match(oa
):
929 self
.flags
.append(oa
)
930 elif opClassRE
.match(oa
):
933 error('InstObjParams: optional arg "%s" not recognized '
934 'as StaticInst::Flag or OpClass.' % oa
)
936 # add flag initialization to contructor here to include
937 # any flags added via opt_args
938 self
.constructor
+= makeFlagConstructor(self
.flags
)
940 # if 'IsFloating' is set, add call to the FP enable check
941 # function (which should be provided by isa_desc via a declare)
942 if 'IsFloating' in self
.flags
:
943 self
.fp_enable_check
= 'fault = checkFpEnableFault(xc);'
945 self
.fp_enable_check
= ''
948 # Stack: a simple stack object. Used for both formats (formatStack)
949 # and default cases (defaultStack). Simply wraps a list to give more
950 # stack-like syntax and enable initialization with an argument list
951 # (as opposed to an argument that's a list).
954 def __init__(self
, *items
):
955 list.__init
__(self
, items
)
957 def push(self
, item
):
963 #######################
965 # Output file template
970 * DO NOT EDIT THIS FILE!!!
972 * It was automatically generated from the ISA description in %(filename)s
979 namespace %(namespace)s {
983 } // namespace %(namespace)s
988 max_inst_regs_template
= '''
990 * DO NOT EDIT THIS FILE!!!
992 * It was automatically generated from the ISA description in %(filename)s
995 namespace %(namespace)s {
997 const int MaxInstSrcRegs = %(MaxInstSrcRegs)d;
998 const int MaxInstDestRegs = %(MaxInstDestRegs)d;
1000 } // namespace %(namespace)s
1004 class ISAParser(Grammar
):
1005 def __init__(self
, output_dir
, cpu_models
):
1006 super(ISAParser
, self
).__init
__()
1007 self
.output_dir
= output_dir
1009 self
.cpuModels
= cpu_models
1011 # variable to hold templates
1012 self
.templateMap
= {}
1014 # This dictionary maps format name strings to Format objects.
1018 self
.formatStack
= Stack(NoFormat())
1020 # The default case stack.
1021 self
.defaultStack
= Stack(None)
1023 # Stack that tracks current file and line number. Each
1024 # element is a tuple (filename, lineno) that records the
1025 # *current* filename and the line number in the *previous*
1026 # file where it was included.
1027 self
.fileNameStack
= Stack()
1029 symbols
= ('makeList', 're', 'string')
1030 self
.exportContext
= dict([(s
, eval(s
)) for s
in symbols
])
1032 self
.maxInstSrcRegs
= 0
1033 self
.maxInstDestRegs
= 0
1035 #####################################################################
1039 # The PLY lexer module takes two things as input:
1040 # - A list of token names (the string list 'tokens')
1041 # - A regular expression describing a match for each token. The
1042 # regexp for token FOO can be provided in two ways:
1043 # - as a string variable named t_FOO
1044 # - as the doc string for a function named t_FOO. In this case,
1045 # the function is also executed, allowing an action to be
1046 # associated with each token match.
1048 #####################################################################
1050 # Reserved words. These are listed separately as they are matched
1051 # using the same regexp as generic IDs, but distinguished in the
1052 # t_ID() function. The PLY documentation suggests this approach.
1054 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT',
1055 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS',
1056 'OUTPUT', 'SIGNED', 'TEMPLATE'
1059 # List of tokens. The lex module requires this.
1060 tokens
= reserved
+ (
1073 # ( ) [ ] { } < > , ; . : :: *
1075 'LBRACKET', 'RBRACKET',
1077 'LESS', 'GREATER', 'EQUALS',
1078 'COMMA', 'SEMI', 'DOT', 'COLON', 'DBLCOLON',
1081 # C preprocessor directives
1084 # The following are matched but never returned. commented out to
1085 # suppress PLY warning
1093 # Regular expressions for token matching
1110 # Identifiers and reserved words
1113 reserved_map
[r
.lower()] = r
1117 t
.type = self
.reserved_map
.get(t
.value
, 'ID')
1121 def t_INTLIT(self
, t
):
1122 r
'-?(0x[\da-fA-F]+)|\d+'
1124 t
.value
= int(t
.value
,0)
1126 error(t
, 'Integer value "%s" too large' % t
.value
)
1130 # String literal. Note that these use only single quotes, and
1131 # can span multiple lines.
1132 def t_STRLIT(self
, t
):
1135 t
.value
= t
.value
[1:-1]
1136 t
.lexer
.lineno
+= t
.value
.count('\n')
1140 # "Code literal"... like a string literal, but delimiters are
1141 # '{{' and '}}' so they get formatted nicely under emacs c-mode
1142 def t_CODELIT(self
, t
):
1143 r
"(?m)\{\{([^\}]|}(?!\}))+\}\}"
1145 t
.value
= t
.value
[2:-2]
1146 t
.lexer
.lineno
+= t
.value
.count('\n')
1149 def t_CPPDIRECTIVE(self
, t
):
1151 t
.lexer
.lineno
+= t
.value
.count('\n')
1154 def t_NEWFILE(self
, t
):
1155 r
'^\#\#newfile\s+"[^"]*"'
1156 self
.fileNameStack
.push((t
.value
[11:-1], t
.lexer
.lineno
))
1159 def t_ENDFILE(self
, t
):
1161 (old_filename
, t
.lexer
.lineno
) = self
.fileNameStack
.pop()
1164 # The functions t_NEWLINE, t_ignore, and t_error are
1165 # special for the lex module.
1169 def t_NEWLINE(self
, t
):
1171 t
.lexer
.lineno
+= t
.value
.count('\n')
1174 def t_comment(self
, t
):
1177 # Completely ignored characters
1178 t_ignore
= ' \t\x0c'
1181 def t_error(self
, t
):
1182 error(t
, "illegal character '%s'" % t
.value
[0])
1185 #####################################################################
1189 # Every function whose name starts with 'p_' defines a grammar
1190 # rule. The rule is encoded in the function's doc string, while
1191 # the function body provides the action taken when the rule is
1192 # matched. The argument to each function is a list of the values
1193 # of the rule's symbols: t[0] for the LHS, and t[1..n] for the
1194 # symbols on the RHS. For tokens, the value is copied from the
1195 # t.value attribute provided by the lexer. For non-terminals, the
1196 # value is assigned by the producing rule; i.e., the job of the
1197 # grammar rule function is to set the value for the non-terminal
1198 # on the LHS (by assigning to t[0]).
1199 #####################################################################
1201 # The LHS of the first grammar rule is used as the start symbol
1202 # (in this case, 'specification'). Note that this rule enforces
1203 # that there will be exactly one namespace declaration, with 0 or
1204 # more global defs/decls before and after it. The defs & decls
1205 # before the namespace decl will be outside the namespace; those
1206 # after will be inside. The decoder function is always inside the
1208 def p_specification(self
, t
):
1209 'specification : opt_defs_and_outputs name_decl opt_defs_and_outputs decode_block'
1212 namespace
= isa_name
+ "Inst"
1213 # wrap the decode block as a function definition
1214 t
[4].wrap_decode_block('''
1216 %(isa_name)s::decodeInst(%(isa_name)s::ExtMachInst machInst)
1218 using namespace %(namespace)s;
1220 # both the latter output blocks and the decode block are in
1222 namespace_code
= t
[3] + t
[4]
1223 # pass it all back to the caller of yacc.parse()
1224 t
[0] = (isa_name
, namespace
, global_code
, namespace_code
)
1226 # ISA name declaration looks like "namespace <foo>;"
1227 def p_name_decl(self
, t
):
1228 'name_decl : NAMESPACE ID SEMI'
1231 # 'opt_defs_and_outputs' is a possibly empty sequence of
1232 # def and/or output statements.
1233 def p_opt_defs_and_outputs_0(self
, t
):
1234 'opt_defs_and_outputs : empty'
1235 t
[0] = GenCode(self
)
1237 def p_opt_defs_and_outputs_1(self
, t
):
1238 'opt_defs_and_outputs : defs_and_outputs'
1241 def p_defs_and_outputs_0(self
, t
):
1242 'defs_and_outputs : def_or_output'
1245 def p_defs_and_outputs_1(self
, t
):
1246 'defs_and_outputs : defs_and_outputs def_or_output'
1249 # The list of possible definition/output statements.
1250 def p_def_or_output(self
, t
):
1251 '''def_or_output : def_format
1253 | def_bitfield_struct
1263 # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied
1264 # directly to the appropriate output section.
1266 # Massage output block by substituting in template definitions and
1267 # bit operators. We handle '%'s embedded in the string that don't
1268 # indicate template substitutions (or CPU-specific symbols, which
1269 # get handled in GenCode) by doubling them first so that the
1270 # format operation will reduce them back to single '%'s.
1271 def process_output(self
, s
):
1272 s
= self
.protectNonSubstPercents(s
)
1273 # protects cpu-specific symbols too
1274 s
= self
.protectCpuSymbols(s
)
1275 return substBitOps(s
% self
.templateMap
)
1277 def p_output_header(self
, t
):
1278 'output_header : OUTPUT HEADER CODELIT SEMI'
1279 t
[0] = GenCode(self
, header_output
= self
.process_output(t
[3]))
1281 def p_output_decoder(self
, t
):
1282 'output_decoder : OUTPUT DECODER CODELIT SEMI'
1283 t
[0] = GenCode(self
, decoder_output
= self
.process_output(t
[3]))
1285 def p_output_exec(self
, t
):
1286 'output_exec : OUTPUT EXEC CODELIT SEMI'
1287 t
[0] = GenCode(self
, exec_output
= self
.process_output(t
[3]))
1289 # global let blocks 'let {{...}}' (Python code blocks) are
1290 # executed directly when seen. Note that these execute in a
1291 # special variable context 'exportContext' to prevent the code
1292 # from polluting this script's namespace.
1293 def p_global_let(self
, t
):
1294 'global_let : LET CODELIT SEMI'
1295 self
.updateExportContext()
1296 self
.exportContext
["header_output"] = ''
1297 self
.exportContext
["decoder_output"] = ''
1298 self
.exportContext
["exec_output"] = ''
1299 self
.exportContext
["decode_block"] = ''
1301 exec fixPythonIndentation(t
[2]) in self
.exportContext
1302 except Exception, exc
:
1305 error(t
, 'error: %s in global let block "%s".' % (exc
, t
[2]))
1306 t
[0] = GenCode(self
,
1307 header_output
=self
.exportContext
["header_output"],
1308 decoder_output
=self
.exportContext
["decoder_output"],
1309 exec_output
=self
.exportContext
["exec_output"],
1310 decode_block
=self
.exportContext
["decode_block"])
1312 # Define the mapping from operand type extensions to C++ types and
1313 # bit widths (stored in operandTypeMap).
1314 def p_def_operand_types(self
, t
):
1315 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI'
1317 self
.operandTypeMap
= eval('{' + t
[3] + '}')
1318 except Exception, exc
:
1322 'error: %s in def operand_types block "%s".' % (exc
, t
[3]))
1323 t
[0] = GenCode(self
) # contributes nothing to the output C++ file
1325 # Define the mapping from operand names to operand classes and
1326 # other traits. Stored in operandNameMap.
1327 def p_def_operands(self
, t
):
1328 'def_operands : DEF OPERANDS CODELIT SEMI'
1329 if not hasattr(self
, 'operandTypeMap'):
1330 error(t
, 'error: operand types must be defined before operands')
1332 user_dict
= eval('{' + t
[3] + '}', self
.exportContext
)
1333 except Exception, exc
:
1336 error(t
, 'error: %s in def operands block "%s".' % (exc
, t
[3]))
1337 self
.buildOperandNameMap(user_dict
, t
.lexer
.lineno
)
1338 t
[0] = GenCode(self
) # contributes nothing to the output C++ file
1340 # A bitfield definition looks like:
1341 # 'def [signed] bitfield <ID> [<first>:<last>]'
1342 # This generates a preprocessor macro in the output file.
1343 def p_def_bitfield_0(self
, t
):
1344 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI'
1345 expr
= 'bits(machInst, %2d, %2d)' % (t
[6], t
[8])
1346 if (t
[2] == 'signed'):
1347 expr
= 'sext<%d>(%s)' % (t
[6] - t
[8] + 1, expr
)
1348 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
1349 t
[0] = GenCode(self
, header_output
=hash_define
)
1351 # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]'
1352 def p_def_bitfield_1(self
, t
):
1353 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI'
1354 expr
= 'bits(machInst, %2d, %2d)' % (t
[6], t
[6])
1355 if (t
[2] == 'signed'):
1356 expr
= 'sext<%d>(%s)' % (1, expr
)
1357 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
1358 t
[0] = GenCode(self
, header_output
=hash_define
)
1360 # alternate form for structure member: 'def bitfield <ID> <ID>'
1361 def p_def_bitfield_struct(self
, t
):
1362 'def_bitfield_struct : DEF opt_signed BITFIELD ID id_with_dot SEMI'
1364 error(t
, 'error: structure bitfields are always unsigned.')
1365 expr
= 'machInst.%s' % t
[5]
1366 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
1367 t
[0] = GenCode(self
, header_output
=hash_define
)
1369 def p_id_with_dot_0(self
, t
):
1373 def p_id_with_dot_1(self
, t
):
1374 'id_with_dot : ID DOT id_with_dot'
1375 t
[0] = t
[1] + t
[2] + t
[3]
1377 def p_opt_signed_0(self
, t
):
1378 'opt_signed : SIGNED'
1381 def p_opt_signed_1(self
, t
):
1382 'opt_signed : empty'
1385 def p_def_template(self
, t
):
1386 'def_template : DEF TEMPLATE ID CODELIT SEMI'
1387 self
.templateMap
[t
[3]] = Template(self
, t
[4])
1388 t
[0] = GenCode(self
)
1390 # An instruction format definition looks like
1391 # "def format <fmt>(<params>) {{...}};"
1392 def p_def_format(self
, t
):
1393 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI'
1394 (id, params
, code
) = (t
[3], t
[5], t
[7])
1395 self
.defFormat(id, params
, code
, t
.lexer
.lineno
)
1396 t
[0] = GenCode(self
)
1398 # The formal parameter list for an instruction format is a
1399 # possibly empty list of comma-separated parameters. Positional
1400 # (standard, non-keyword) parameters must come first, followed by
1401 # keyword parameters, followed by a '*foo' parameter that gets
1402 # excess positional arguments (as in Python). Each of these three
1403 # parameter categories is optional.
1405 # Note that we do not support the '**foo' parameter for collecting
1406 # otherwise undefined keyword args. Otherwise the parameter list
1407 # is (I believe) identical to what is supported in Python.
1409 # The param list generates a tuple, where the first element is a
1410 # list of the positional params and the second element is a dict
1411 # containing the keyword params.
1412 def p_param_list_0(self
, t
):
1413 'param_list : positional_param_list COMMA nonpositional_param_list'
1416 def p_param_list_1(self
, t
):
1417 '''param_list : positional_param_list
1418 | nonpositional_param_list'''
1421 def p_positional_param_list_0(self
, t
):
1422 'positional_param_list : empty'
1425 def p_positional_param_list_1(self
, t
):
1426 'positional_param_list : ID'
1429 def p_positional_param_list_2(self
, t
):
1430 'positional_param_list : positional_param_list COMMA ID'
1431 t
[0] = t
[1] + [t
[3]]
1433 def p_nonpositional_param_list_0(self
, t
):
1434 'nonpositional_param_list : keyword_param_list COMMA excess_args_param'
1437 def p_nonpositional_param_list_1(self
, t
):
1438 '''nonpositional_param_list : keyword_param_list
1439 | excess_args_param'''
1442 def p_keyword_param_list_0(self
, t
):
1443 'keyword_param_list : keyword_param'
1446 def p_keyword_param_list_1(self
, t
):
1447 'keyword_param_list : keyword_param_list COMMA keyword_param'
1448 t
[0] = t
[1] + [t
[3]]
1450 def p_keyword_param(self
, t
):
1451 'keyword_param : ID EQUALS expr'
1452 t
[0] = t
[1] + ' = ' + t
[3].__repr
__()
1454 def p_excess_args_param(self
, t
):
1455 'excess_args_param : ASTERISK ID'
1456 # Just concatenate them: '*ID'. Wrap in list to be consistent
1457 # with positional_param_list and keyword_param_list.
1458 t
[0] = [t
[1] + t
[2]]
1460 # End of format definition-related rules.
1464 # A decode block looks like:
1465 # decode <field1> [, <field2>]* [default <inst>] { ... }
1467 def p_decode_block(self
, t
):
1468 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE'
1469 default_defaults
= self
.defaultStack
.pop()
1471 # use the "default defaults" only if there was no explicit
1472 # default statement in decode_stmt_list
1473 if not codeObj
.has_decode_default
:
1474 codeObj
+= default_defaults
1475 codeObj
.wrap_decode_block('switch (%s) {\n' % t
[2], '}\n')
1478 # The opt_default statement serves only to push the "default
1479 # defaults" onto defaultStack. This value will be used by nested
1480 # decode blocks, and used and popped off when the current
1481 # decode_block is processed (in p_decode_block() above).
1482 def p_opt_default_0(self
, t
):
1483 'opt_default : empty'
1484 # no default specified: reuse the one currently at the top of
1486 self
.defaultStack
.push(self
.defaultStack
.top())
1487 # no meaningful value returned
1490 def p_opt_default_1(self
, t
):
1491 'opt_default : DEFAULT inst'
1492 # push the new default
1494 codeObj
.wrap_decode_block('\ndefault:\n', 'break;\n')
1495 self
.defaultStack
.push(codeObj
)
1496 # no meaningful value returned
1499 def p_decode_stmt_list_0(self
, t
):
1500 'decode_stmt_list : decode_stmt'
1503 def p_decode_stmt_list_1(self
, t
):
1504 'decode_stmt_list : decode_stmt decode_stmt_list'
1505 if (t
[1].has_decode_default
and t
[2].has_decode_default
):
1506 error(t
, 'Two default cases in decode block')
1510 # Decode statement rules
1512 # There are four types of statements allowed in a decode block:
1513 # 1. Format blocks 'format <foo> { ... }'
1514 # 2. Nested decode blocks
1515 # 3. Instruction definitions.
1516 # 4. C preprocessor directives.
1519 # Preprocessor directives found in a decode statement list are
1520 # passed through to the output, replicated to all of the output
1521 # code streams. This works well for ifdefs, so we can ifdef out
1522 # both the declarations and the decode cases generated by an
1523 # instruction definition. Handling them as part of the grammar
1524 # makes it easy to keep them in the right place with respect to
1525 # the code generated by the other statements.
1526 def p_decode_stmt_cpp(self
, t
):
1527 'decode_stmt : CPPDIRECTIVE'
1528 t
[0] = GenCode(self
, t
[1], t
[1], t
[1], t
[1])
1530 # A format block 'format <foo> { ... }' sets the default
1531 # instruction format used to handle instruction definitions inside
1532 # the block. This format can be overridden by using an explicit
1533 # format on the instruction definition or with a nested format
1535 def p_decode_stmt_format(self
, t
):
1536 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE'
1537 # The format will be pushed on the stack when 'push_format_id'
1538 # is processed (see below). Once the parser has recognized
1539 # the full production (though the right brace), we're done
1540 # with the format, so now we can pop it.
1541 self
.formatStack
.pop()
1544 # This rule exists so we can set the current format (& push the
1545 # stack) when we recognize the format name part of the format
1547 def p_push_format_id(self
, t
):
1548 'push_format_id : ID'
1550 self
.formatStack
.push(self
.formatMap
[t
[1]])
1551 t
[0] = ('', '// format %s' % t
[1])
1553 error(t
, 'instruction format "%s" not defined.' % t
[1])
1555 # Nested decode block: if the value of the current field matches
1556 # the specified constant, do a nested decode on some other field.
1557 def p_decode_stmt_decode(self
, t
):
1558 'decode_stmt : case_label COLON decode_block'
1561 # just wrap the decoding code from the block as a case in the
1562 # outer switch statement.
1563 codeObj
.wrap_decode_block('\n%s:\n' % label
)
1564 codeObj
.has_decode_default
= (label
== 'default')
1567 # Instruction definition (finally!).
1568 def p_decode_stmt_inst(self
, t
):
1569 'decode_stmt : case_label COLON inst SEMI'
1572 codeObj
.wrap_decode_block('\n%s:' % label
, 'break;\n')
1573 codeObj
.has_decode_default
= (label
== 'default')
1576 # The case label is either a list of one or more constants or
1578 def p_case_label_0(self
, t
):
1579 'case_label : intlit_list'
1580 def make_case(intlit
):
1582 return 'case ULL(%#x)' % intlit
1584 return 'case %#x' % intlit
1585 t
[0] = ': '.join(map(make_case
, t
[1]))
1587 def p_case_label_1(self
, t
):
1588 'case_label : DEFAULT'
1592 # The constant list for a decode case label must be non-empty, but
1593 # may have one or more comma-separated integer literals in it.
1595 def p_intlit_list_0(self
, t
):
1596 'intlit_list : INTLIT'
1599 def p_intlit_list_1(self
, t
):
1600 'intlit_list : intlit_list COMMA INTLIT'
1604 # Define an instruction using the current instruction format
1605 # (specified by an enclosing format block).
1606 # "<mnemonic>(<args>)"
1607 def p_inst_0(self
, t
):
1608 'inst : ID LPAREN arg_list RPAREN'
1609 # Pass the ID and arg list to the current format class to deal with.
1610 currentFormat
= self
.formatStack
.top()
1611 codeObj
= currentFormat
.defineInst(self
, t
[1], t
[3], t
.lexer
.lineno
)
1612 args
= ','.join(map(str, t
[3]))
1613 args
= re
.sub('(?m)^', '//', args
)
1614 args
= re
.sub('^//', '', args
)
1615 comment
= '\n// %s::%s(%s)\n' % (currentFormat
.id, t
[1], args
)
1616 codeObj
.prepend_all(comment
)
1619 # Define an instruction using an explicitly specified format:
1620 # "<fmt>::<mnemonic>(<args>)"
1621 def p_inst_1(self
, t
):
1622 'inst : ID DBLCOLON ID LPAREN arg_list RPAREN'
1624 format
= self
.formatMap
[t
[1]]
1626 error(t
, 'instruction format "%s" not defined.' % t
[1])
1628 codeObj
= format
.defineInst(self
, t
[3], t
[5], t
.lexer
.lineno
)
1629 comment
= '\n// %s::%s(%s)\n' % (t
[1], t
[3], t
[5])
1630 codeObj
.prepend_all(comment
)
1633 # The arg list generates a tuple, where the first element is a
1634 # list of the positional args and the second element is a dict
1635 # containing the keyword args.
1636 def p_arg_list_0(self
, t
):
1637 'arg_list : positional_arg_list COMMA keyword_arg_list'
1638 t
[0] = ( t
[1], t
[3] )
1640 def p_arg_list_1(self
, t
):
1641 'arg_list : positional_arg_list'
1644 def p_arg_list_2(self
, t
):
1645 'arg_list : keyword_arg_list'
1648 def p_positional_arg_list_0(self
, t
):
1649 'positional_arg_list : empty'
1652 def p_positional_arg_list_1(self
, t
):
1653 'positional_arg_list : expr'
1656 def p_positional_arg_list_2(self
, t
):
1657 'positional_arg_list : positional_arg_list COMMA expr'
1658 t
[0] = t
[1] + [t
[3]]
1660 def p_keyword_arg_list_0(self
, t
):
1661 'keyword_arg_list : keyword_arg'
1664 def p_keyword_arg_list_1(self
, t
):
1665 'keyword_arg_list : keyword_arg_list COMMA keyword_arg'
1669 def p_keyword_arg(self
, t
):
1670 'keyword_arg : ID EQUALS expr'
1671 t
[0] = { t
[1] : t
[3] }
1674 # Basic expressions. These constitute the argument values of
1675 # "function calls" (i.e. instruction definitions in the decode
1676 # block) and default values for formal parameters of format
1679 # Right now, these are either strings, integers, or (recursively)
1680 # lists of exprs (using Python square-bracket list syntax). Note
1681 # that bare identifiers are trated as string constants here (since
1682 # there isn't really a variable namespace to refer to).
1684 def p_expr_0(self
, t
):
1691 def p_expr_1(self
, t
):
1692 '''expr : LBRACKET list_expr RBRACKET'''
1695 def p_list_expr_0(self
, t
):
1699 def p_list_expr_1(self
, t
):
1700 'list_expr : list_expr COMMA expr'
1701 t
[0] = t
[1] + [t
[3]]
1703 def p_list_expr_2(self
, t
):
1708 # Empty production... use in other rules for readability.
1710 def p_empty(self
, t
):
1714 # Parse error handler. Note that the argument here is the
1715 # offending *token*, not a grammar symbol (hence the need to use
1717 def p_error(self
, t
):
1719 error(t
, "syntax error at '%s'" % t
.value
)
1721 error("unknown syntax error")
1723 # END OF GRAMMAR RULES
1725 def updateExportContext(self
):
1727 # create a continuation that allows us to grab the current parser
1728 def wrapInstObjParams(*args
):
1729 return InstObjParams(self
, *args
)
1730 self
.exportContext
['InstObjParams'] = wrapInstObjParams
1731 self
.exportContext
.update(self
.templateMap
)
1733 def defFormat(self
, id, params
, code
, lineno
):
1734 '''Define a new format'''
1736 # make sure we haven't already defined this one
1737 if id in self
.formatMap
:
1738 error(lineno
, 'format %s redefined.' % id)
1740 # create new object and store in global map
1741 self
.formatMap
[id] = Format(id, params
, code
)
1743 def expandCpuSymbolsToDict(self
, template
):
1744 '''Expand template with CPU-specific references into a
1745 dictionary with an entry for each CPU model name. The entry
1746 key is the model name and the corresponding value is the
1747 template with the CPU-specific refs substituted for that
1750 # Protect '%'s that don't go with CPU-specific terms
1751 t
= re
.sub(r
'%(?!\(CPU_)', '%%', template
)
1753 for cpu
in self
.cpuModels
:
1754 result
[cpu
.name
] = t
% cpu
.strings
1757 def expandCpuSymbolsToString(self
, template
):
1758 '''*If* the template has CPU-specific references, return a
1759 single string containing a copy of the template for each CPU
1760 model with the corresponding values substituted in. If the
1761 template has no CPU-specific references, it is returned
1764 if template
.find('%(CPU_') != -1:
1765 return reduce(lambda x
,y
: x
+y
,
1766 self
.expandCpuSymbolsToDict(template
).values())
1770 def protectCpuSymbols(self
, template
):
1771 '''Protect CPU-specific references by doubling the
1772 corresponding '%'s (in preparation for substituting a different
1773 set of references into the template).'''
1775 return re
.sub(r
'%(?=\(CPU_)', '%%', template
)
1777 def protectNonSubstPercents(self
, s
):
1778 '''Protect any non-dict-substitution '%'s in a format string
1779 (i.e. those not followed by '(')'''
1781 return re
.sub(r
'%(?!\()', '%%', s
)
1783 def buildOperandNameMap(self
, user_dict
, lineno
):
1785 for op_name
, val
in user_dict
.iteritems():
1786 base_cls_name
, dflt_ext
, reg_spec
, flags
, sort_pri
= val
[:5]
1797 'error: too many attributes for operand "%s"' %
1800 # Canonical flag structure is a triple of lists, where each list
1801 # indicates the set of flags implied by this operand always, when
1802 # used as a source, and when used as a dest, respectively.
1803 # For simplicity this can be initialized using a variety of fairly
1804 # obvious shortcuts; we convert these to canonical form here.
1806 # no flags specified (e.g., 'None')
1807 flags
= ( [], [], [] )
1808 elif isinstance(flags
, str):
1809 # a single flag: assumed to be unconditional
1810 flags
= ( [ flags
], [], [] )
1811 elif isinstance(flags
, list):
1812 # a list of flags: also assumed to be unconditional
1813 flags
= ( flags
, [], [] )
1814 elif isinstance(flags
, tuple):
1815 # it's a tuple: it should be a triple,
1816 # but each item could be a single string or a list
1817 (uncond_flags
, src_flags
, dest_flags
) = flags
1818 flags
= (makeList(uncond_flags
),
1819 makeList(src_flags
), makeList(dest_flags
))
1820 # Accumulate attributes of new operand class in tmp_dict
1822 attrList
= ['reg_spec', 'flags', 'sort_pri',
1823 'read_code', 'write_code']
1825 dflt_ctype
= self
.operandTypeMap
[dflt_ext
]
1826 attrList
.extend(['dflt_ctype', 'dflt_ext'])
1827 for attr
in attrList
:
1828 tmp_dict
[attr
] = eval(attr
)
1829 tmp_dict
['base_name'] = op_name
1830 # New class name will be e.g. "IntReg_Ra"
1831 cls_name
= base_cls_name
+ '_' + op_name
1832 # Evaluate string arg to get class object. Note that the
1833 # actual base class for "IntReg" is "IntRegOperand", i.e. we
1834 # have to append "Operand".
1836 base_cls
= eval(base_cls_name
+ 'Operand')
1839 'error: unknown operand base class "%s"' % base_cls_name
)
1840 # The following statement creates a new class called
1841 # <cls_name> as a subclass of <base_cls> with the attributes
1842 # in tmp_dict, just as if we evaluated a class declaration.
1843 operand_name
[op_name
] = type(cls_name
, (base_cls
,), tmp_dict
)
1845 self
.operandNameMap
= operand_name
1847 # Define operand variables.
1848 operands
= user_dict
.keys()
1850 operandsREString
= (r
'''
1851 (?<![\w\.]) # neg. lookbehind assertion: prevent partial matches
1852 ((%s)(?:\.(\w+))?) # match: operand with optional '.' then suffix
1853 (?![\w\.]) # neg. lookahead assertion: prevent partial matches
1855 % string
.join(operands
, '|'))
1857 self
.operandsRE
= re
.compile(operandsREString
, re
.MULTILINE|re
.VERBOSE
)
1859 # Same as operandsREString, but extension is mandatory, and only two
1860 # groups are returned (base and ext, not full name as above).
1861 # Used for subtituting '_' for '.' to make C++ identifiers.
1862 operandsWithExtREString
= (r
'(?<![\w\.])(%s)\.(\w+)(?![\w\.])'
1863 % string
.join(operands
, '|'))
1865 self
.operandsWithExtRE
= \
1866 re
.compile(operandsWithExtREString
, re
.MULTILINE
)
1868 def substMungedOpNames(self
, code
):
1869 '''Munge operand names in code string to make legal C++
1870 variable names. This means getting rid of the type extension
1871 if any. Will match base_name attribute of Operand object.)'''
1872 return self
.operandsWithExtRE
.sub(r
'\1', code
)
1874 def mungeSnippet(self
, s
):
1875 '''Fix up code snippets for final substitution in templates.'''
1876 if isinstance(s
, str):
1877 return self
.substMungedOpNames(substBitOps(s
))
1881 def update_if_needed(self
, file, contents
):
1882 '''Update the output file only if the new contents are
1883 different from the current contents. Minimizes the files that
1884 need to be rebuilt after minor changes.'''
1886 file = os
.path
.join(self
.output_dir
, file)
1888 if os
.access(file, os
.R_OK
):
1890 old_contents
= f
.read()
1892 if contents
!= old_contents
:
1893 os
.remove(file) # in case it's write-protected
1896 print 'File', file, 'is unchanged'
1904 # This regular expression matches '##include' directives
1905 includeRE
= re
.compile(r
'^\s*##include\s+"(?P<filename>[^"]*)".*$',
1908 def replace_include(self
, matchobj
, dirname
):
1909 """Function to replace a matched '##include' directive with the
1910 contents of the specified file (with nested ##includes
1911 replaced recursively). 'matchobj' is an re match object
1912 (from a match of includeRE) and 'dirname' is the directory
1913 relative to which the file path should be resolved."""
1915 fname
= matchobj
.group('filename')
1916 full_fname
= os
.path
.normpath(os
.path
.join(dirname
, fname
))
1917 contents
= '##newfile "%s"\n%s\n##endfile\n' % \
1918 (full_fname
, self
.read_and_flatten(full_fname
))
1921 def read_and_flatten(self
, filename
):
1922 """Read a file and recursively flatten nested '##include' files."""
1924 current_dir
= os
.path
.dirname(filename
)
1926 contents
= open(filename
).read()
1928 error('Error including file "%s"' % filename
)
1930 self
.fileNameStack
.push((filename
, 0))
1932 # Find any includes and include them
1933 def replace(matchobj
):
1934 return self
.replace_include(matchobj
, current_dir
)
1935 contents
= self
.includeRE
.sub(replace
, contents
)
1937 self
.fileNameStack
.pop()
1940 def _parse_isa_desc(self
, isa_desc_file
):
1941 '''Read in and parse the ISA description.'''
1943 # Read file and (recursively) all included files into a string.
1944 # PLY requires that the input be in a single string so we have to
1946 isa_desc
= self
.read_and_flatten(isa_desc_file
)
1948 # Initialize filename stack with outer file.
1949 self
.fileNameStack
.push((isa_desc_file
, 0))
1952 (isa_name
, namespace
, global_code
, namespace_code
) = \
1953 self
.parse_string(isa_desc
)
1955 # grab the last three path components of isa_desc_file to put in
1957 filename
= '/'.join(isa_desc_file
.split('/')[-3:])
1959 # generate decoder.hh
1960 includes
= '#include "base/bitfield.hh" // for bitfield support'
1961 global_output
= global_code
.header_output
1962 namespace_output
= namespace_code
.header_output
1963 decode_function
= ''
1964 self
.update_if_needed('decoder.hh', file_template
% vars())
1966 # generate decoder.cc
1967 includes
= '#include "decoder.hh"'
1968 global_output
= global_code
.decoder_output
1969 namespace_output
= namespace_code
.decoder_output
1970 # namespace_output += namespace_code.decode_block
1971 decode_function
= namespace_code
.decode_block
1972 self
.update_if_needed('decoder.cc', file_template
% vars())
1974 # generate per-cpu exec files
1975 for cpu
in self
.cpuModels
:
1976 includes
= '#include "decoder.hh"\n'
1977 includes
+= cpu
.includes
1978 global_output
= global_code
.exec_output
[cpu
.name
]
1979 namespace_output
= namespace_code
.exec_output
[cpu
.name
]
1980 decode_function
= ''
1981 self
.update_if_needed(cpu
.filename
, file_template
% vars())
1983 # The variable names here are hacky, but this will creat local
1984 # variables which will be referenced in vars() which have the
1985 # value of the globals.
1986 MaxInstSrcRegs
= self
.maxInstSrcRegs
1987 MaxInstDestRegs
= self
.maxInstDestRegs
1989 self
.update_if_needed('max_inst_regs.hh',
1990 max_inst_regs_template
% vars())
1992 def parse_isa_desc(self
, *args
, **kwargs
):
1994 self
._parse
_isa
_desc
(*args
, **kwargs
)
1995 except ISAParserError
, e
:
1996 e
.exit(self
.fileNameStack
)
1998 # Called as script: get args from command line.
1999 # Args are: <path to cpu_models.py> <isa desc file> <output dir> <cpu models>
2000 if __name__
== '__main__':
2001 execfile(sys
.argv
[1]) # read in CpuModel definitions
2002 cpu_models
= [CpuModel
.dict[cpu
] for cpu
in sys
.argv
[4:]]
2003 ISAParser(sys
.argv
[3], cpu_models
).parse_isa_desc(sys
.argv
[2])