1 # Copyright (c) 2003-2005 The Regents of The University of Michigan
2 # Copyright (c) 2013 Advanced Micro Devices, Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 # Authors: Steve Reinhardt
30 from __future__
import with_statement
35 import inspect
, traceback
39 from m5
.util
.grammar
import Grammar
47 # Indent every line in string 's' by two spaces
48 # (except preprocessor directives).
49 # Used to make nested code blocks look pretty.
52 return re
.sub(r
'(?m)^(?!#)', ' ', s
)
55 # Munge a somewhat arbitrarily formatted piece of Python code
56 # (e.g. from a format 'let' block) into something whose indentation
57 # will get by the Python parser.
59 # The two keys here are that Python will give a syntax error if
60 # there's any whitespace at the beginning of the first line, and that
61 # all lines at the same lexical nesting level must have identical
62 # indentation. Unfortunately the way code literals work, an entire
63 # let block tends to have some initial indentation. Rather than
64 # trying to figure out what that is and strip it off, we prepend 'if
65 # 1:' to make the let code the nested block inside the if (and have
66 # the parser automatically deal with the indentation for us).
68 # We don't want to do this if (1) the code block is empty or (2) the
69 # first line of the block doesn't have any whitespace at the front.
71 def fixPythonIndentation(s
):
72 # get rid of blank lines first
73 s
= re
.sub(r
'(?m)^\s*\n', '', s
);
74 if (s
!= '' and re
.match(r
'[ \t]', s
[0])):
78 class ISAParserError(Exception):
79 """Error handler for parser errors"""
80 def __init__(self
, first
, second
=None):
85 if hasattr(first
, 'lexer'):
86 first
= first
.lexer
.lineno
90 def display(self
, filename_stack
, print_traceback
=debug
):
91 # Output formatted to work under Emacs compile-mode. Optional
92 # 'print_traceback' arg, if set to True, prints a Python stack
93 # backtrace too (can be handy when trying to debug the parser
97 for (filename
, line
) in filename_stack
[:-1]:
98 print "%sIn file included from %s:" % (spaces
, filename
)
101 # Print a Python stack backtrace if requested.
102 if print_traceback
or not self
.lineno
:
103 traceback
.print_exc()
105 line_str
= "%s:" % (filename_stack
[-1][0], )
107 line_str
+= "%d:" % (self
.lineno
, )
109 return "%s%s %s" % (spaces
, line_str
, self
.string
)
111 def exit(self
, filename_stack
, print_traceback
=debug
):
114 sys
.exit(self
.display(filename_stack
, print_traceback
))
117 raise ISAParserError(*args
)
122 # Template objects are format strings that allow substitution from
123 # the attribute spaces of other objects (e.g. InstObjParams instances).
125 labelRE
= re
.compile(r
'(?<!%)%\(([^\)]+)\)[sd]')
127 class Template(object):
128 def __init__(self
, parser
, t
):
135 # Protect non-Python-dict substitutions (e.g. if there's a printf
136 # in the templated C++ code)
137 template
= self
.parser
.protectNonSubstPercents(self
.template
)
138 # CPU-model-specific substitutions are handled later (in GenCode).
139 template
= self
.parser
.protectCpuSymbols(template
)
141 # Build a dict ('myDict') to use for the template substitution.
142 # Start with the template namespace. Make a copy since we're
143 # going to modify it.
144 myDict
= self
.parser
.templateMap
.copy()
146 if isinstance(d
, InstObjParams
):
147 # If we're dealing with an InstObjParams object, we need
148 # to be a little more sophisticated. The instruction-wide
149 # parameters are already formed, but the parameters which
150 # are only function wide still need to be generated.
153 myDict
.update(d
.__dict
__)
154 # The "operands" and "snippets" attributes of the InstObjParams
155 # objects are for internal use and not substitution.
156 del myDict
['operands']
157 del myDict
['snippets']
159 snippetLabels
= [l
for l
in labelRE
.findall(template
)
160 if d
.snippets
.has_key(l
)]
162 snippets
= dict([(s
, self
.parser
.mungeSnippet(d
.snippets
[s
]))
163 for s
in snippetLabels
])
165 myDict
.update(snippets
)
167 compositeCode
= ' '.join(map(str, snippets
.values()))
169 # Add in template itself in case it references any
170 # operands explicitly (like Mem)
171 compositeCode
+= ' ' + template
173 operands
= SubOperandList(self
.parser
, compositeCode
, d
.operands
)
175 myDict
['op_decl'] = operands
.concatAttrStrings('op_decl')
176 if operands
.readPC
or operands
.setPC
:
177 myDict
['op_decl'] += 'TheISA::PCState __parserAutoPCState;\n'
179 # In case there are predicated register reads and write, declare
180 # the variables for register indicies. It is being assumed that
181 # all the operands in the OperandList are also in the
182 # SubOperandList and in the same order. Otherwise, it is
183 # expected that predication would not be used for the operands.
184 if operands
.predRead
:
185 myDict
['op_decl'] += 'uint8_t _sourceIndex = 0;\n'
186 if operands
.predWrite
:
187 myDict
['op_decl'] += 'uint8_t M5_VAR_USED _destIndex = 0;\n'
189 is_src
= lambda op
: op
.is_src
190 is_dest
= lambda op
: op
.is_dest
192 myDict
['op_src_decl'] = \
193 operands
.concatSomeAttrStrings(is_src
, 'op_src_decl')
194 myDict
['op_dest_decl'] = \
195 operands
.concatSomeAttrStrings(is_dest
, 'op_dest_decl')
197 myDict
['op_src_decl'] += \
198 'TheISA::PCState __parserAutoPCState;\n'
200 myDict
['op_dest_decl'] += \
201 'TheISA::PCState __parserAutoPCState;\n'
203 myDict
['op_rd'] = operands
.concatAttrStrings('op_rd')
205 myDict
['op_rd'] = '__parserAutoPCState = xc->pcState();\n' + \
208 # Compose the op_wb string. If we're going to write back the
209 # PC state because we changed some of its elements, we'll need to
210 # do that as early as possible. That allows later uncoordinated
211 # modifications to the PC to layer appropriately.
212 reordered
= list(operands
.items
)
215 pcWbStr
= 'xc->pcState(__parserAutoPCState);\n'
216 for op_desc
in reordered
:
217 if op_desc
.isPCPart() and op_desc
.is_dest
:
218 op_wb_str
= op_desc
.op_wb
+ pcWbStr
+ op_wb_str
221 op_wb_str
= op_desc
.op_wb
+ op_wb_str
222 myDict
['op_wb'] = op_wb_str
224 elif isinstance(d
, dict):
225 # if the argument is a dictionary, we just use it.
227 elif hasattr(d
, '__dict__'):
228 # if the argument is an object, we use its attribute map.
229 myDict
.update(d
.__dict
__)
231 raise TypeError, "Template.subst() arg must be or have dictionary"
232 return template
% myDict
234 # Convert to string. This handles the case when a template with a
235 # CPU-specific term gets interpolated into another template or into
238 return self
.parser
.expandCpuSymbolsToString(self
.template
)
243 # A format object encapsulates an instruction format. It must provide
244 # a defineInst() method that generates the code for an instruction
247 class Format(object):
248 def __init__(self
, id, params
, code
):
251 label
= 'def format ' + id
252 self
.user_code
= compile(fixPythonIndentation(code
), label
, 'exec')
253 param_list
= string
.join(params
, ", ")
254 f
= '''def defInst(_code, _context, %s):
255 my_locals = vars().copy()
256 exec _code in _context, my_locals
257 return my_locals\n''' % param_list
258 c
= compile(f
, label
+ ' wrapper', 'exec')
262 def defineInst(self
, parser
, name
, args
, lineno
):
263 parser
.updateExportContext()
264 context
= parser
.exportContext
.copy()
266 Name
= name
[0].upper()
269 context
.update({ 'name' : name
, 'Name' : Name
})
271 vars = self
.func(self
.user_code
, context
, *args
[0], **args
[1])
272 except Exception, exc
:
275 error(lineno
, 'error defining "%s": %s.' % (name
, exc
))
276 for k
in vars.keys():
277 if k
not in ('header_output', 'decoder_output',
278 'exec_output', 'decode_block'):
280 return GenCode(parser
, **vars)
282 # Special null format to catch an implicit-format instruction
283 # definition outside of any format block.
284 class NoFormat(object):
286 self
.defaultInst
= ''
288 def defineInst(self
, parser
, name
, args
, lineno
):
290 'instruction definition "%s" with no active format!' % name
)
295 # The GenCode class encapsulates generated code destined for various
296 # output files. The header_output and decoder_output attributes are
297 # strings containing code destined for decoder.hh and decoder.cc
298 # respectively. The decode_block attribute contains code to be
299 # incorporated in the decode function itself (that will also end up in
300 # decoder.cc). The exec_output attribute is a dictionary with a key
301 # for each CPU model name; the value associated with a particular key
302 # is the string of code for that CPU model's exec.cc file. The
303 # has_decode_default attribute is used in the decode block to allow
304 # explicit default clauses to override default default clauses.
306 class GenCode(object):
307 # Constructor. At this point we substitute out all CPU-specific
308 # symbols. For the exec output, these go into the per-model
309 # dictionary. For all other output types they get collapsed into
311 def __init__(self
, parser
,
312 header_output
= '', decoder_output
= '', exec_output
= '',
313 decode_block
= '', has_decode_default
= False):
315 self
.header_output
= parser
.expandCpuSymbolsToString(header_output
)
316 self
.decoder_output
= parser
.expandCpuSymbolsToString(decoder_output
)
317 self
.exec_output
= exec_output
318 self
.decode_block
= decode_block
319 self
.has_decode_default
= has_decode_default
321 # Write these code chunks out to the filesystem. They will be properly
322 # interwoven by the write_top_level_files().
324 if self
.header_output
:
325 self
.parser
.get_file('header').write(self
.header_output
)
326 if self
.decoder_output
:
327 self
.parser
.get_file('decoder').write(self
.decoder_output
)
329 self
.parser
.get_file('exec').write(self
.exec_output
)
330 if self
.decode_block
:
331 self
.parser
.get_file('decode_block').write(self
.decode_block
)
333 # Override '+' operator: generate a new GenCode object that
334 # concatenates all the individual strings in the operands.
335 def __add__(self
, other
):
336 return GenCode(self
.parser
,
337 self
.header_output
+ other
.header_output
,
338 self
.decoder_output
+ other
.decoder_output
,
339 self
.exec_output
+ other
.exec_output
,
340 self
.decode_block
+ other
.decode_block
,
341 self
.has_decode_default
or other
.has_decode_default
)
343 # Prepend a string (typically a comment) to all the strings.
344 def prepend_all(self
, pre
):
345 self
.header_output
= pre
+ self
.header_output
346 self
.decoder_output
= pre
+ self
.decoder_output
347 self
.decode_block
= pre
+ self
.decode_block
348 self
.exec_output
= pre
+ self
.exec_output
350 # Wrap the decode block in a pair of strings (e.g., 'case foo:'
351 # and 'break;'). Used to build the big nested switch statement.
352 def wrap_decode_block(self
, pre
, post
= ''):
353 self
.decode_block
= pre
+ indent(self
.decode_block
) + post
355 #####################################################################
357 # Bitfield Operator Support
359 #####################################################################
361 bitOp1ArgRE
= re
.compile(r
'<\s*(\w+)\s*:\s*>')
363 bitOpWordRE
= re
.compile(r
'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>')
364 bitOpExprRE
= re
.compile(r
'\)<\s*(\w+)\s*:\s*(\w+)\s*>')
366 def substBitOps(code
):
367 # first convert single-bit selectors to two-index form
368 # i.e., <n> --> <n:n>
369 code
= bitOp1ArgRE
.sub(r
'<\1:\1>', code
)
370 # simple case: selector applied to ID (name)
371 # i.e., foo<a:b> --> bits(foo, a, b)
372 code
= bitOpWordRE
.sub(r
'bits(\1, \2, \3)', code
)
373 # if selector is applied to expression (ending in ')'),
374 # we need to search backward for matching '('
375 match
= bitOpExprRE
.search(code
)
377 exprEnd
= match
.start()
381 if code
[here
] == '(':
383 elif code
[here
] == ')':
387 sys
.exit("Didn't find '('!")
389 newExpr
= r
'bits(%s, %s, %s)' % (code
[exprStart
:exprEnd
+1],
390 match
.group(1), match
.group(2))
391 code
= code
[:exprStart
] + newExpr
+ code
[match
.end():]
392 match
= bitOpExprRE
.search(code
)
396 #####################################################################
400 # The remaining code is the support for automatically extracting
401 # instruction characteristics from pseudocode.
403 #####################################################################
405 # Force the argument to be a list. Useful for flags, where a caller
406 # can specify a singleton flag or a list of flags. Also usful for
407 # converting tuples to lists so they can be modified.
409 if isinstance(arg
, list):
411 elif isinstance(arg
, tuple):
418 class Operand(object):
419 '''Base class for operand descriptors. An instance of this class
420 (or actually a class derived from this one) represents a specific
421 operand for a code block (e.g, "Rc.sq" as a dest). Intermediate
422 derived classes encapsulates the traits of a particular operand
423 type (e.g., "32-bit integer register").'''
425 def buildReadCode(self
, func
= None):
426 subst_dict
= {"name": self
.base_name
,
428 "reg_idx": self
.reg_spec
,
430 if hasattr(self
, 'src_reg_idx'):
431 subst_dict
['op_idx'] = self
.src_reg_idx
432 code
= self
.read_code
% subst_dict
433 return '%s = %s;\n' % (self
.base_name
, code
)
435 def buildWriteCode(self
, func
= None):
436 subst_dict
= {"name": self
.base_name
,
438 "reg_idx": self
.reg_spec
,
440 "final_val": self
.base_name
}
441 if hasattr(self
, 'dest_reg_idx'):
442 subst_dict
['op_idx'] = self
.dest_reg_idx
443 code
= self
.write_code
% subst_dict
448 if (traceData) { traceData->setData(final_val); }
449 }''' % (self
.dflt_ctype
, self
.base_name
, code
)
451 def __init__(self
, parser
, full_name
, ext
, is_src
, is_dest
):
452 self
.full_name
= full_name
455 self
.is_dest
= is_dest
456 # The 'effective extension' (eff_ext) is either the actual
457 # extension, if one was explicitly provided, or the default.
460 elif hasattr(self
, 'dflt_ext'):
461 self
.eff_ext
= self
.dflt_ext
463 if hasattr(self
, 'eff_ext'):
464 self
.ctype
= parser
.operandTypeMap
[self
.eff_ext
]
466 # Finalize additional fields (primarily code fields). This step
467 # is done separately since some of these fields may depend on the
468 # register index enumeration that hasn't been performed yet at the
469 # time of __init__(). The register index enumeration is affected
470 # by predicated register reads/writes. Hence, we forward the flags
471 # that indicate whether or not predication is in use.
472 def finalize(self
, predRead
, predWrite
):
473 self
.flags
= self
.getFlags()
474 self
.constructor
= self
.makeConstructor(predRead
, predWrite
)
475 self
.op_decl
= self
.makeDecl()
478 self
.op_rd
= self
.makeRead(predRead
)
479 self
.op_src_decl
= self
.makeDecl()
482 self
.op_src_decl
= ''
485 self
.op_wb
= self
.makeWrite(predWrite
)
486 self
.op_dest_decl
= self
.makeDecl()
489 self
.op_dest_decl
= ''
497 def isFloatReg(self
):
506 def isControlReg(self
):
513 return self
.isPCState() and self
.reg_spec
515 def hasReadPred(self
):
516 return self
.read_predicate
!= None
518 def hasWritePred(self
):
519 return self
.write_predicate
!= None
522 # note the empty slice '[:]' gives us a copy of self.flags[0]
523 # instead of a reference to it
524 my_flags
= self
.flags
[0][:]
526 my_flags
+= self
.flags
[1]
528 my_flags
+= self
.flags
[2]
532 # Note that initializations in the declarations are solely
533 # to avoid 'uninitialized variable' errors from the compiler.
534 return self
.ctype
+ ' ' + self
.base_name
+ ' = 0;\n';
536 class IntRegOperand(Operand
):
543 def makeConstructor(self
, predRead
, predWrite
):
548 c_src
= '\n\t_srcRegIdx[_numSrcRegs++] = %s;' % (self
.reg_spec
)
549 if self
.hasReadPred():
550 c_src
= '\n\tif (%s) {%s\n\t}' % \
551 (self
.read_predicate
, c_src
)
554 c_dest
= '\n\t_destRegIdx[_numDestRegs++] = %s;' % \
556 c_dest
+= '\n\t_numIntDestRegs++;'
557 if self
.hasWritePred():
558 c_dest
= '\n\tif (%s) {%s\n\t}' % \
559 (self
.write_predicate
, c_dest
)
561 return c_src
+ c_dest
563 def makeRead(self
, predRead
):
564 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
565 error('Attempt to read integer register as FP')
566 if self
.read_code
!= None:
567 return self
.buildReadCode('readIntRegOperand')
571 int_reg_val
= 'xc->readIntRegOperand(this, _sourceIndex++)'
572 if self
.hasReadPred():
573 int_reg_val
= '(%s) ? %s : 0' % \
574 (self
.read_predicate
, int_reg_val
)
576 int_reg_val
= 'xc->readIntRegOperand(this, %d)' % self
.src_reg_idx
578 return '%s = %s;\n' % (self
.base_name
, int_reg_val
)
580 def makeWrite(self
, predWrite
):
581 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
582 error('Attempt to write integer register as FP')
583 if self
.write_code
!= None:
584 return self
.buildWriteCode('setIntRegOperand')
588 if self
.hasWritePred():
589 wp
= self
.write_predicate
591 wcond
= 'if (%s)' % (wp
)
592 windex
= '_destIndex++'
595 windex
= '%d' % self
.dest_reg_idx
601 xc->setIntRegOperand(this, %s, final_val);\n
602 if (traceData) { traceData->setData(final_val); }
603 }''' % (wcond
, self
.ctype
, self
.base_name
, windex
)
607 class FloatRegOperand(Operand
):
611 def isFloatReg(self
):
614 def makeConstructor(self
, predRead
, predWrite
):
619 c_src
= '\n\t_srcRegIdx[_numSrcRegs++] = %s + FP_Reg_Base;' % \
624 '\n\t_destRegIdx[_numDestRegs++] = %s + FP_Reg_Base;' % \
626 c_dest
+= '\n\t_numFPDestRegs++;'
628 return c_src
+ c_dest
630 def makeRead(self
, predRead
):
632 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
633 func
= 'readFloatRegOperand'
635 func
= 'readFloatRegOperandBits'
636 if self
.read_code
!= None:
637 return self
.buildReadCode(func
)
640 rindex
= '_sourceIndex++'
642 rindex
= '%d' % self
.src_reg_idx
644 return '%s = xc->%s(this, %s);\n' % \
645 (self
.base_name
, func
, rindex
)
647 def makeWrite(self
, predWrite
):
648 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
649 func
= 'setFloatRegOperand'
651 func
= 'setFloatRegOperandBits'
652 if self
.write_code
!= None:
653 return self
.buildWriteCode(func
)
658 wp
= '%d' % self
.dest_reg_idx
659 wp
= 'xc->%s(this, %s, final_val);' % (func
, wp
)
665 if (traceData) { traceData->setData(final_val); }
666 }''' % (self
.ctype
, self
.base_name
, wp
)
669 class CCRegOperand(Operand
):
676 def makeConstructor(self
, predRead
, predWrite
):
681 c_src
= '\n\t_srcRegIdx[_numSrcRegs++] = %s + CC_Reg_Base;' % \
683 if self
.hasReadPred():
684 c_src
= '\n\tif (%s) {%s\n\t}' % \
685 (self
.read_predicate
, c_src
)
689 '\n\t_destRegIdx[_numDestRegs++] = %s + CC_Reg_Base;' % \
691 c_dest
+= '\n\t_numCCDestRegs++;'
692 if self
.hasWritePred():
693 c_dest
= '\n\tif (%s) {%s\n\t}' % \
694 (self
.write_predicate
, c_dest
)
696 return c_src
+ c_dest
698 def makeRead(self
, predRead
):
699 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
700 error('Attempt to read condition-code register as FP')
701 if self
.read_code
!= None:
702 return self
.buildReadCode('readCCRegOperand')
706 int_reg_val
= 'xc->readCCRegOperand(this, _sourceIndex++)'
707 if self
.hasReadPred():
708 int_reg_val
= '(%s) ? %s : 0' % \
709 (self
.read_predicate
, int_reg_val
)
711 int_reg_val
= 'xc->readCCRegOperand(this, %d)' % self
.src_reg_idx
713 return '%s = %s;\n' % (self
.base_name
, int_reg_val
)
715 def makeWrite(self
, predWrite
):
716 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
717 error('Attempt to write condition-code register as FP')
718 if self
.write_code
!= None:
719 return self
.buildWriteCode('setCCRegOperand')
723 if self
.hasWritePred():
724 wp
= self
.write_predicate
726 wcond
= 'if (%s)' % (wp
)
727 windex
= '_destIndex++'
730 windex
= '%d' % self
.dest_reg_idx
736 xc->setCCRegOperand(this, %s, final_val);\n
737 if (traceData) { traceData->setData(final_val); }
738 }''' % (wcond
, self
.ctype
, self
.base_name
, windex
)
742 class ControlRegOperand(Operand
):
746 def isControlReg(self
):
749 def makeConstructor(self
, predRead
, predWrite
):
755 '\n\t_srcRegIdx[_numSrcRegs++] = %s + Misc_Reg_Base;' % \
760 '\n\t_destRegIdx[_numDestRegs++] = %s + Misc_Reg_Base;' % \
763 return c_src
+ c_dest
765 def makeRead(self
, predRead
):
767 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
768 error('Attempt to read control register as FP')
769 if self
.read_code
!= None:
770 return self
.buildReadCode('readMiscRegOperand')
773 rindex
= '_sourceIndex++'
775 rindex
= '%d' % self
.src_reg_idx
777 return '%s = xc->readMiscRegOperand(this, %s);\n' % \
778 (self
.base_name
, rindex
)
780 def makeWrite(self
, predWrite
):
781 if (self
.ctype
== 'float' or self
.ctype
== 'double'):
782 error('Attempt to write control register as FP')
783 if self
.write_code
!= None:
784 return self
.buildWriteCode('setMiscRegOperand')
787 windex
= '_destIndex++'
789 windex
= '%d' % self
.dest_reg_idx
791 wb
= 'xc->setMiscRegOperand(this, %s, %s);\n' % \
792 (windex
, self
.base_name
)
793 wb
+= 'if (traceData) { traceData->setData(%s); }' % \
798 class MemOperand(Operand
):
802 def makeConstructor(self
, predRead
, predWrite
):
806 # Note that initializations in the declarations are solely
807 # to avoid 'uninitialized variable' errors from the compiler.
808 # Declare memory data variable.
809 return '%s %s = 0;\n' % (self
.ctype
, self
.base_name
)
811 def makeRead(self
, predRead
):
812 if self
.read_code
!= None:
813 return self
.buildReadCode()
816 def makeWrite(self
, predWrite
):
817 if self
.write_code
!= None:
818 return self
.buildWriteCode()
821 class PCStateOperand(Operand
):
822 def makeConstructor(self
, predRead
, predWrite
):
825 def makeRead(self
, predRead
):
827 # A component of the PC state.
828 return '%s = __parserAutoPCState.%s();\n' % \
829 (self
.base_name
, self
.reg_spec
)
831 # The whole PC state itself.
832 return '%s = xc->pcState();\n' % self
.base_name
834 def makeWrite(self
, predWrite
):
836 # A component of the PC state.
837 return '__parserAutoPCState.%s(%s);\n' % \
838 (self
.reg_spec
, self
.base_name
)
840 # The whole PC state itself.
841 return 'xc->pcState(%s);\n' % self
.base_name
844 ctype
= 'TheISA::PCState'
847 return "%s %s;\n" % (ctype
, self
.base_name
)
852 class OperandList(object):
853 '''Find all the operands in the given code block. Returns an operand
854 descriptor list (instance of class OperandList).'''
855 def __init__(self
, parser
, code
):
858 # delete strings and comments so we don't match on operands inside
859 for regEx
in (stringRE
, commentRE
):
860 code
= regEx
.sub('', code
)
861 # search for operands
864 match
= parser
.operandsRE
.search(code
, next_pos
)
866 # no more matches: we're done
869 # regexp groups are operand full name, base, and extension
870 (op_full
, op_base
, op_ext
) = op
871 # if the token following the operand is an assignment, this is
872 # a destination (LHS), else it's a source (RHS)
873 is_dest
= (assignRE
.match(code
, match
.end()) != None)
875 # see if we've already seen this one
876 op_desc
= self
.find_base(op_base
)
878 if op_desc
.ext
!= op_ext
:
879 error('Inconsistent extensions for operand %s' % \
881 op_desc
.is_src
= op_desc
.is_src
or is_src
882 op_desc
.is_dest
= op_desc
.is_dest
or is_dest
884 # new operand: create new descriptor
885 op_desc
= parser
.operandNameMap
[op_base
](parser
,
886 op_full
, op_ext
, is_src
, is_dest
)
888 # start next search after end of current match
889 next_pos
= match
.end()
891 # enumerate source & dest register operands... used in building
895 self
.numFPDestRegs
= 0
896 self
.numIntDestRegs
= 0
897 self
.numCCDestRegs
= 0
898 self
.numMiscDestRegs
= 0
899 self
.memOperand
= None
901 # Flags to keep track if one or more operands are to be read/written
903 self
.predRead
= False
904 self
.predWrite
= False
906 for op_desc
in self
.items
:
909 op_desc
.src_reg_idx
= self
.numSrcRegs
912 op_desc
.dest_reg_idx
= self
.numDestRegs
913 self
.numDestRegs
+= 1
914 if op_desc
.isFloatReg():
915 self
.numFPDestRegs
+= 1
916 elif op_desc
.isIntReg():
917 self
.numIntDestRegs
+= 1
918 elif op_desc
.isCCReg():
919 self
.numCCDestRegs
+= 1
920 elif op_desc
.isControlReg():
921 self
.numMiscDestRegs
+= 1
922 elif op_desc
.isMem():
924 error("Code block has more than one memory operand.")
925 self
.memOperand
= op_desc
927 # Check if this operand has read/write predication. If true, then
928 # the microop will dynamically index source/dest registers.
929 self
.predRead
= self
.predRead
or op_desc
.hasReadPred()
930 self
.predWrite
= self
.predWrite
or op_desc
.hasWritePred()
932 if parser
.maxInstSrcRegs
< self
.numSrcRegs
:
933 parser
.maxInstSrcRegs
= self
.numSrcRegs
934 if parser
.maxInstDestRegs
< self
.numDestRegs
:
935 parser
.maxInstDestRegs
= self
.numDestRegs
936 if parser
.maxMiscDestRegs
< self
.numMiscDestRegs
:
937 parser
.maxMiscDestRegs
= self
.numMiscDestRegs
939 # now make a final pass to finalize op_desc fields that may depend
940 # on the register enumeration
941 for op_desc
in self
.items
:
942 op_desc
.finalize(self
.predRead
, self
.predWrite
)
945 return len(self
.items
)
947 def __getitem__(self
, index
):
948 return self
.items
[index
]
950 def append(self
, op_desc
):
951 self
.items
.append(op_desc
)
952 self
.bases
[op_desc
.base_name
] = op_desc
954 def find_base(self
, base_name
):
955 # like self.bases[base_name], but returns None if not found
956 # (rather than raising exception)
957 return self
.bases
.get(base_name
)
959 # internal helper function for concat[Some]Attr{Strings|Lists}
960 def __internalConcatAttrs(self
, attr_name
, filter, result
):
961 for op_desc
in self
.items
:
963 result
+= getattr(op_desc
, attr_name
)
966 # return a single string that is the concatenation of the (string)
967 # values of the specified attribute for all operands
968 def concatAttrStrings(self
, attr_name
):
969 return self
.__internalConcatAttrs
(attr_name
, lambda x
: 1, '')
971 # like concatAttrStrings, but only include the values for the operands
972 # for which the provided filter function returns true
973 def concatSomeAttrStrings(self
, filter, attr_name
):
974 return self
.__internalConcatAttrs
(attr_name
, filter, '')
976 # return a single list that is the concatenation of the (list)
977 # values of the specified attribute for all operands
978 def concatAttrLists(self
, attr_name
):
979 return self
.__internalConcatAttrs
(attr_name
, lambda x
: 1, [])
981 # like concatAttrLists, but only include the values for the operands
982 # for which the provided filter function returns true
983 def concatSomeAttrLists(self
, filter, attr_name
):
984 return self
.__internalConcatAttrs
(attr_name
, filter, [])
987 self
.items
.sort(lambda a
, b
: a
.sort_pri
- b
.sort_pri
)
989 class SubOperandList(OperandList
):
990 '''Find all the operands in the given code block. Returns an operand
991 descriptor list (instance of class OperandList).'''
992 def __init__(self
, parser
, code
, master_list
):
995 # delete strings and comments so we don't match on operands inside
996 for regEx
in (stringRE
, commentRE
):
997 code
= regEx
.sub('', code
)
998 # search for operands
1001 match
= parser
.operandsRE
.search(code
, next_pos
)
1003 # no more matches: we're done
1006 # regexp groups are operand full name, base, and extension
1007 (op_full
, op_base
, op_ext
) = op
1008 # find this op in the master list
1009 op_desc
= master_list
.find_base(op_base
)
1011 error('Found operand %s which is not in the master list!' \
1012 ' This is an internal error' % op_base
)
1014 # See if we've already found this operand
1015 op_desc
= self
.find_base(op_base
)
1017 # if not, add a reference to it to this sub list
1018 self
.append(master_list
.bases
[op_base
])
1020 # start next search after end of current match
1021 next_pos
= match
.end()
1023 self
.memOperand
= None
1024 # Whether the whole PC needs to be read so parts of it can be accessed
1026 # Whether the whole PC needs to be written after parts of it were
1029 # Whether this instruction manipulates the whole PC or parts of it.
1030 # Mixing the two is a bad idea and flagged as an error.
1033 # Flags to keep track if one or more operands are to be read/written
1035 self
.predRead
= False
1036 self
.predWrite
= False
1038 for op_desc
in self
.items
:
1039 if op_desc
.isPCPart():
1044 if op_desc
.isPCState():
1045 if self
.pcPart
is not None:
1046 if self
.pcPart
and not op_desc
.isPCPart() or \
1047 not self
.pcPart
and op_desc
.isPCPart():
1048 error("Mixed whole and partial PC state operands.")
1049 self
.pcPart
= op_desc
.isPCPart()
1053 error("Code block has more than one memory operand.")
1054 self
.memOperand
= op_desc
1056 # Check if this operand has read/write predication. If true, then
1057 # the microop will dynamically index source/dest registers.
1058 self
.predRead
= self
.predRead
or op_desc
.hasReadPred()
1059 self
.predWrite
= self
.predWrite
or op_desc
.hasWritePred()
1061 # Regular expression object to match C++ strings
1062 stringRE
= re
.compile(r
'"([^"\\]|\\.)*"')
1064 # Regular expression object to match C++ comments
1065 # (used in findOperands())
1066 commentRE
= re
.compile(r
'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
1067 re
.DOTALL | re
.MULTILINE
)
1069 # Regular expression object to match assignment statements
1070 # (used in findOperands())
1071 assignRE
= re
.compile(r
'\s*=(?!=)', re
.MULTILINE
)
1073 def makeFlagConstructor(flag_list
):
1074 if len(flag_list
) == 0:
1076 # filter out repeated flags
1079 while i
< len(flag_list
):
1080 if flag_list
[i
] == flag_list
[i
-1]:
1086 code
= pre
+ string
.join(flag_list
, post
+ pre
) + post
1089 # Assume all instruction flags are of the form 'IsFoo'
1090 instFlagRE
= re
.compile(r
'Is.*')
1092 # OpClass constants end in 'Op' except No_OpClass
1093 opClassRE
= re
.compile(r
'.*Op|No_OpClass')
1095 class InstObjParams(object):
1096 def __init__(self
, parser
, mnem
, class_name
, base_class
= '',
1097 snippets
= {}, opt_args
= []):
1098 self
.mnemonic
= mnem
1099 self
.class_name
= class_name
1100 self
.base_class
= base_class
1101 if not isinstance(snippets
, dict):
1102 snippets
= {'code' : snippets
}
1103 compositeCode
= ' '.join(map(str, snippets
.values()))
1104 self
.snippets
= snippets
1106 self
.operands
= OperandList(parser
, compositeCode
)
1108 # The header of the constructor declares the variables to be used
1109 # in the body of the constructor.
1111 header
+= '\n\t_numSrcRegs = 0;'
1112 header
+= '\n\t_numDestRegs = 0;'
1113 header
+= '\n\t_numFPDestRegs = 0;'
1114 header
+= '\n\t_numIntDestRegs = 0;'
1115 header
+= '\n\t_numCCDestRegs = 0;'
1117 self
.constructor
= header
+ \
1118 self
.operands
.concatAttrStrings('constructor')
1120 self
.flags
= self
.operands
.concatAttrLists('flags')
1122 # Make a basic guess on the operand class (function unit type).
1123 # These are good enough for most cases, and can be overridden
1125 if 'IsStore' in self
.flags
:
1126 self
.op_class
= 'MemWriteOp'
1127 elif 'IsLoad' in self
.flags
or 'IsPrefetch' in self
.flags
:
1128 self
.op_class
= 'MemReadOp'
1129 elif 'IsFloating' in self
.flags
:
1130 self
.op_class
= 'FloatAddOp'
1132 self
.op_class
= 'IntAluOp'
1134 # Optional arguments are assumed to be either StaticInst flags
1135 # or an OpClass value. To avoid having to import a complete
1136 # list of these values to match against, we do it ad-hoc
1139 if instFlagRE
.match(oa
):
1140 self
.flags
.append(oa
)
1141 elif opClassRE
.match(oa
):
1144 error('InstObjParams: optional arg "%s" not recognized '
1145 'as StaticInst::Flag or OpClass.' % oa
)
1147 # add flag initialization to contructor here to include
1148 # any flags added via opt_args
1149 self
.constructor
+= makeFlagConstructor(self
.flags
)
1151 # if 'IsFloating' is set, add call to the FP enable check
1152 # function (which should be provided by isa_desc via a declare)
1153 if 'IsFloating' in self
.flags
:
1154 self
.fp_enable_check
= 'fault = checkFpEnableFault(xc);'
1156 self
.fp_enable_check
= ''
1159 # Stack: a simple stack object. Used for both formats (formatStack)
1160 # and default cases (defaultStack). Simply wraps a list to give more
1161 # stack-like syntax and enable initialization with an argument list
1162 # (as opposed to an argument that's a list).
1165 def __init__(self
, *items
):
1166 list.__init
__(self
, items
)
1168 def push(self
, item
):
1174 #######################
1177 # parses ISA DSL and emits C++ headers and source
1180 class ISAParser(Grammar
):
1181 class CpuModel(object):
1182 def __init__(self
, name
, filename
, includes
, strings
):
1184 self
.filename
= filename
1185 self
.includes
= includes
1186 self
.strings
= strings
1188 def __init__(self
, output_dir
):
1189 super(ISAParser
, self
).__init
__()
1190 self
.output_dir
= output_dir
1192 self
.filename
= None # for output file watermarking/scaremongering
1195 ISAParser
.CpuModel('ExecContext',
1196 'generic_cpu_exec.cc',
1197 '#include "cpu/exec_context.hh"',
1198 { "CPU_exec_context" : "ExecContext" }),
1201 # variable to hold templates
1202 self
.templateMap
= {}
1204 # This dictionary maps format name strings to Format objects.
1207 # Track open files and, if applicable, how many chunks it has been
1208 # split into so far.
1212 # isa_name / namespace identifier from namespace declaration.
1213 # before the namespace declaration, None.
1214 self
.isa_name
= None
1215 self
.namespace
= None
1218 self
.formatStack
= Stack(NoFormat())
1220 # The default case stack.
1221 self
.defaultStack
= Stack(None)
1223 # Stack that tracks current file and line number. Each
1224 # element is a tuple (filename, lineno) that records the
1225 # *current* filename and the line number in the *previous*
1226 # file where it was included.
1227 self
.fileNameStack
= Stack()
1229 symbols
= ('makeList', 're', 'string')
1230 self
.exportContext
= dict([(s
, eval(s
)) for s
in symbols
])
1232 self
.maxInstSrcRegs
= 0
1233 self
.maxInstDestRegs
= 0
1234 self
.maxMiscDestRegs
= 0
1236 def __getitem__(self
, i
): # Allow object (self) to be
1237 return getattr(self
, i
) # passed to %-substitutions
1239 # Change the file suffix of a base filename:
1240 # (e.g.) decoder.cc -> decoder-g.cc.inc for 'global' outputs
1241 def suffixize(self
, s
, sec
):
1242 extn
= re
.compile('(\.[^\.]+)$') # isolate extension
1244 return extn
.sub(r
'-ns\1.inc', s
) # insert some text on either side
1246 return extn
.sub(r
'-g\1.inc', s
)
1248 # Get the file object for emitting code into the specified section
1249 # (header, decoder, exec, decode_block).
1250 def get_file(self
, section
):
1251 if section
== 'decode_block':
1252 filename
= 'decode-method.cc.inc'
1254 if section
== 'header':
1257 file = '%s.cc' % section
1258 filename
= self
.suffixize(file, section
)
1260 return self
.files
[filename
]
1261 except KeyError: pass
1263 f
= self
.open(filename
)
1264 self
.files
[filename
] = f
1266 # The splittable files are the ones with many independent
1267 # per-instruction functions - the decoder's instruction constructors
1268 # and the instruction execution (execute()) methods. These both have
1269 # the suffix -ns.cc.inc, meaning they are within the namespace part
1270 # of the ISA, contain object-emitting C++ source, and are included
1271 # into other top-level files. These are the files that need special
1272 # #define's to allow parts of them to be compiled separately. Rather
1273 # than splitting the emissions into separate files, the monolithic
1274 # output of the ISA parser is maintained, but the value (or lack
1275 # thereof) of the __SPLIT definition during C preprocessing will
1276 # select the different chunks. If no 'split' directives are used,
1277 # the cpp emissions have no effect.
1278 if re
.search('-ns.cc.inc$', filename
):
1279 print >>f
, '#if !defined(__SPLIT) || (__SPLIT == 1)'
1281 # ensure requisite #include's
1282 elif filename
in ['decoder-g.cc.inc', 'exec-g.cc.inc']:
1283 print >>f
, '#include "decoder.hh"'
1284 elif filename
== 'decoder-g.hh.inc':
1285 print >>f
, '#include "base/bitfield.hh"'
1289 # Weave together the parts of the different output sections by
1290 # #include'ing them into some very short top-level .cc/.hh files.
1291 # These small files make it much clearer how this tool works, since
1292 # you directly see the chunks emitted as files that are #include'd.
1293 def write_top_level_files(self
):
1294 dep
= self
.open('inc.d', bare
=True)
1296 # decoder header - everything depends on this
1298 with self
.open(file) as f
:
1301 fn
= 'decoder-g.hh.inc'
1302 assert(fn
in self
.files
)
1303 f
.write('#include "%s"\n' % fn
)
1306 fn
= 'decoder-ns.hh.inc'
1307 assert(fn
in self
.files
)
1308 f
.write('namespace %s {\n#include "%s"\n}\n'
1309 % (self
.namespace
, fn
))
1312 print >>dep
, file+':', ' '.join(inc
)
1314 # decoder method - cannot be split
1316 with self
.open(file) as f
:
1319 fn
= 'decoder-g.cc.inc'
1320 assert(fn
in self
.files
)
1321 f
.write('#include "%s"\n' % fn
)
1324 fn
= 'decode-method.cc.inc'
1325 # is guaranteed to have been written for parse to complete
1326 f
.write('#include "%s"\n' % fn
)
1329 inc
.append("decoder.hh")
1330 print >>dep
, file+':', ' '.join(inc
)
1332 extn
= re
.compile('(\.[^\.]+)$')
1334 # instruction constructors
1335 splits
= self
.splits
[self
.get_file('decoder')]
1336 file_
= 'inst-constrs.cc'
1337 for i
in range(1, splits
+1):
1339 file = extn
.sub(r
'-%d\1' % i
, file_
)
1342 with self
.open(file) as f
:
1345 fn
= 'decoder-g.cc.inc'
1346 assert(fn
in self
.files
)
1347 f
.write('#include "%s"\n' % fn
)
1350 fn
= 'decoder-ns.cc.inc'
1351 assert(fn
in self
.files
)
1352 print >>f
, 'namespace %s {' % self
.namespace
1354 print >>f
, '#define __SPLIT %u' % i
1355 print >>f
, '#include "%s"' % fn
1359 inc
.append("decoder.hh")
1360 print >>dep
, file+':', ' '.join(inc
)
1362 # instruction execution per-CPU model
1363 splits
= self
.splits
[self
.get_file('exec')]
1364 for cpu
in self
.cpuModels
:
1365 for i
in range(1, splits
+1):
1367 file = extn
.sub(r
'_%d\1' % i
, cpu
.filename
)
1370 with self
.open(file) as f
:
1373 fn
= 'exec-g.cc.inc'
1374 assert(fn
in self
.files
)
1375 f
.write('#include "%s"\n' % fn
)
1378 f
.write(cpu
.includes
+"\n")
1380 fn
= 'exec-ns.cc.inc'
1381 assert(fn
in self
.files
)
1382 print >>f
, 'namespace %s {' % self
.namespace
1383 print >>f
, '#define CPU_EXEC_CONTEXT %s' \
1384 % cpu
.strings
['CPU_exec_context']
1386 print >>f
, '#define __SPLIT %u' % i
1387 print >>f
, '#include "%s"' % fn
1391 inc
.append("decoder.hh")
1392 print >>dep
, file+':', ' '.join(inc
)
1395 self
.update('max_inst_regs.hh',
1396 '''namespace %(namespace)s {
1397 const int MaxInstSrcRegs = %(maxInstSrcRegs)d;
1398 const int MaxInstDestRegs = %(maxInstDestRegs)d;
1399 const int MaxMiscDestRegs = %(maxMiscDestRegs)d;\n}\n''' % self
)
1400 print >>dep
, 'max_inst_regs.hh:'
1405 scaremonger_template
='''// DO NOT EDIT
1406 // This file was automatically generated from an ISA description:
1411 #####################################################################
1415 # The PLY lexer module takes two things as input:
1416 # - A list of token names (the string list 'tokens')
1417 # - A regular expression describing a match for each token. The
1418 # regexp for token FOO can be provided in two ways:
1419 # - as a string variable named t_FOO
1420 # - as the doc string for a function named t_FOO. In this case,
1421 # the function is also executed, allowing an action to be
1422 # associated with each token match.
1424 #####################################################################
1426 # Reserved words. These are listed separately as they are matched
1427 # using the same regexp as generic IDs, but distinguished in the
1428 # t_ID() function. The PLY documentation suggests this approach.
1430 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT',
1431 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS',
1432 'OUTPUT', 'SIGNED', 'SPLIT', 'TEMPLATE'
1435 # List of tokens. The lex module requires this.
1436 tokens
= reserved
+ (
1449 # ( ) [ ] { } < > , ; . : :: *
1451 'LBRACKET', 'RBRACKET',
1453 'LESS', 'GREATER', 'EQUALS',
1454 'COMMA', 'SEMI', 'DOT', 'COLON', 'DBLCOLON',
1457 # C preprocessor directives
1460 # The following are matched but never returned. commented out to
1461 # suppress PLY warning
1469 # Regular expressions for token matching
1486 # Identifiers and reserved words
1489 reserved_map
[r
.lower()] = r
1493 t
.type = self
.reserved_map
.get(t
.value
, 'ID')
1497 def t_INTLIT(self
, t
):
1498 r
'-?(0x[\da-fA-F]+)|\d+'
1500 t
.value
= int(t
.value
,0)
1502 error(t
, 'Integer value "%s" too large' % t
.value
)
1506 # String literal. Note that these use only single quotes, and
1507 # can span multiple lines.
1508 def t_STRLIT(self
, t
):
1511 t
.value
= t
.value
[1:-1]
1512 t
.lexer
.lineno
+= t
.value
.count('\n')
1516 # "Code literal"... like a string literal, but delimiters are
1517 # '{{' and '}}' so they get formatted nicely under emacs c-mode
1518 def t_CODELIT(self
, t
):
1519 r
"(?m)\{\{([^\}]|}(?!\}))+\}\}"
1521 t
.value
= t
.value
[2:-2]
1522 t
.lexer
.lineno
+= t
.value
.count('\n')
1525 def t_CPPDIRECTIVE(self
, t
):
1527 t
.lexer
.lineno
+= t
.value
.count('\n')
1530 def t_NEWFILE(self
, t
):
1531 r
'^\#\#newfile\s+"[^"]*"'
1532 self
.fileNameStack
.push((t
.value
[11:-1], t
.lexer
.lineno
))
1535 def t_ENDFILE(self
, t
):
1537 (old_filename
, t
.lexer
.lineno
) = self
.fileNameStack
.pop()
1540 # The functions t_NEWLINE, t_ignore, and t_error are
1541 # special for the lex module.
1545 def t_NEWLINE(self
, t
):
1547 t
.lexer
.lineno
+= t
.value
.count('\n')
1550 def t_comment(self
, t
):
1553 # Completely ignored characters
1554 t_ignore
= ' \t\x0c'
1557 def t_error(self
, t
):
1558 error(t
, "illegal character '%s'" % t
.value
[0])
1561 #####################################################################
1565 # Every function whose name starts with 'p_' defines a grammar
1566 # rule. The rule is encoded in the function's doc string, while
1567 # the function body provides the action taken when the rule is
1568 # matched. The argument to each function is a list of the values
1569 # of the rule's symbols: t[0] for the LHS, and t[1..n] for the
1570 # symbols on the RHS. For tokens, the value is copied from the
1571 # t.value attribute provided by the lexer. For non-terminals, the
1572 # value is assigned by the producing rule; i.e., the job of the
1573 # grammar rule function is to set the value for the non-terminal
1574 # on the LHS (by assigning to t[0]).
1575 #####################################################################
1577 # The LHS of the first grammar rule is used as the start symbol
1578 # (in this case, 'specification'). Note that this rule enforces
1579 # that there will be exactly one namespace declaration, with 0 or
1580 # more global defs/decls before and after it. The defs & decls
1581 # before the namespace decl will be outside the namespace; those
1582 # after will be inside. The decoder function is always inside the
1584 def p_specification(self
, t
):
1585 'specification : opt_defs_and_outputs top_level_decode_block'
1587 for f
in self
.splits
.iterkeys():
1588 f
.write('\n#endif\n')
1590 for f
in self
.files
.itervalues(): # close ALL the files;
1591 f
.close() # not doing so can cause compilation to fail
1593 self
.write_top_level_files()
1597 # 'opt_defs_and_outputs' is a possibly empty sequence of def and/or
1598 # output statements. Its productions do the hard work of eventually
1599 # instantiating a GenCode, which are generally emitted (written to disk)
1600 # as soon as possible, except for the decode_block, which has to be
1601 # accumulated into one large function of nested switch/case blocks.
1602 def p_opt_defs_and_outputs_0(self
, t
):
1603 'opt_defs_and_outputs : empty'
1605 def p_opt_defs_and_outputs_1(self
, t
):
1606 'opt_defs_and_outputs : defs_and_outputs'
1608 def p_defs_and_outputs_0(self
, t
):
1609 'defs_and_outputs : def_or_output'
1611 def p_defs_and_outputs_1(self
, t
):
1612 'defs_and_outputs : defs_and_outputs def_or_output'
1614 # The list of possible definition/output statements.
1615 # They are all processed as they are seen.
1616 def p_def_or_output(self
, t
):
1617 '''def_or_output : name_decl
1620 | def_bitfield_struct
1628 # Utility function used by both invocations of splitting - explicit
1629 # 'split' keyword and split() function inside "let {{ }};" blocks.
1630 def split(self
, sec
, write
=False):
1631 assert(sec
!= 'header' and "header cannot be split")
1633 f
= self
.get_file(sec
)
1635 s
= '\n#endif\n#if __SPLIT == %u\n' % self
.splits
[f
]
1641 # split output file to reduce compilation time
1642 def p_split(self
, t
):
1643 'split : SPLIT output_type SEMI'
1644 assert(self
.isa_name
and "'split' not allowed before namespace decl")
1646 self
.split(t
[2], True)
1648 def p_output_type(self
, t
):
1649 '''output_type : DECODER
1654 # ISA name declaration looks like "namespace <foo>;"
1655 def p_name_decl(self
, t
):
1656 'name_decl : NAMESPACE ID SEMI'
1657 assert(self
.isa_name
== None and "Only 1 namespace decl permitted")
1658 self
.isa_name
= t
[2]
1659 self
.namespace
= t
[2] + 'Inst'
1661 # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied
1662 # directly to the appropriate output section.
1664 # Massage output block by substituting in template definitions and
1665 # bit operators. We handle '%'s embedded in the string that don't
1666 # indicate template substitutions (or CPU-specific symbols, which
1667 # get handled in GenCode) by doubling them first so that the
1668 # format operation will reduce them back to single '%'s.
1669 def process_output(self
, s
):
1670 s
= self
.protectNonSubstPercents(s
)
1671 # protects cpu-specific symbols too
1672 s
= self
.protectCpuSymbols(s
)
1673 return substBitOps(s
% self
.templateMap
)
1675 def p_output(self
, t
):
1676 'output : OUTPUT output_type CODELIT SEMI'
1677 kwargs
= { t
[2]+'_output' : self
.process_output(t
[3]) }
1678 GenCode(self
, **kwargs
).emit()
1680 # global let blocks 'let {{...}}' (Python code blocks) are
1681 # executed directly when seen. Note that these execute in a
1682 # special variable context 'exportContext' to prevent the code
1683 # from polluting this script's namespace.
1684 def p_global_let(self
, t
):
1685 'global_let : LET CODELIT SEMI'
1687 return self
.split(sec
)
1688 self
.updateExportContext()
1689 self
.exportContext
["header_output"] = ''
1690 self
.exportContext
["decoder_output"] = ''
1691 self
.exportContext
["exec_output"] = ''
1692 self
.exportContext
["decode_block"] = ''
1693 self
.exportContext
["split"] = _split
1697 globals()[sec + '_output'] += func(sec)
1702 # This tricky setup (immediately above) allows us to just write
1703 # (e.g.) "split('exec')" in the Python code and the split #ifdef's
1704 # will automatically be added to the exec_output variable. The inner
1705 # Python execution environment doesn't know about the split points,
1706 # so we carefully inject and wrap a closure that can retrieve the
1707 # next split's #define from the parser and add it to the current
1708 # emission-in-progress.
1710 exec split_setup
+fixPythonIndentation(t
[2]) in self
.exportContext
1711 except Exception, exc
:
1714 error(t
, 'error: %s in global let block "%s".' % (exc
, t
[2]))
1716 header_output
=self
.exportContext
["header_output"],
1717 decoder_output
=self
.exportContext
["decoder_output"],
1718 exec_output
=self
.exportContext
["exec_output"],
1719 decode_block
=self
.exportContext
["decode_block"]).emit()
1721 # Define the mapping from operand type extensions to C++ types and
1722 # bit widths (stored in operandTypeMap).
1723 def p_def_operand_types(self
, t
):
1724 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI'
1726 self
.operandTypeMap
= eval('{' + t
[3] + '}')
1727 except Exception, exc
:
1731 'error: %s in def operand_types block "%s".' % (exc
, t
[3]))
1733 # Define the mapping from operand names to operand classes and
1734 # other traits. Stored in operandNameMap.
1735 def p_def_operands(self
, t
):
1736 'def_operands : DEF OPERANDS CODELIT SEMI'
1737 if not hasattr(self
, 'operandTypeMap'):
1738 error(t
, 'error: operand types must be defined before operands')
1740 user_dict
= eval('{' + t
[3] + '}', self
.exportContext
)
1741 except Exception, exc
:
1744 error(t
, 'error: %s in def operands block "%s".' % (exc
, t
[3]))
1745 self
.buildOperandNameMap(user_dict
, t
.lexer
.lineno
)
1747 # A bitfield definition looks like:
1748 # 'def [signed] bitfield <ID> [<first>:<last>]'
1749 # This generates a preprocessor macro in the output file.
1750 def p_def_bitfield_0(self
, t
):
1751 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI'
1752 expr
= 'bits(machInst, %2d, %2d)' % (t
[6], t
[8])
1753 if (t
[2] == 'signed'):
1754 expr
= 'sext<%d>(%s)' % (t
[6] - t
[8] + 1, expr
)
1755 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
1756 GenCode(self
, header_output
=hash_define
).emit()
1758 # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]'
1759 def p_def_bitfield_1(self
, t
):
1760 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI'
1761 expr
= 'bits(machInst, %2d, %2d)' % (t
[6], t
[6])
1762 if (t
[2] == 'signed'):
1763 expr
= 'sext<%d>(%s)' % (1, expr
)
1764 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
1765 GenCode(self
, header_output
=hash_define
).emit()
1767 # alternate form for structure member: 'def bitfield <ID> <ID>'
1768 def p_def_bitfield_struct(self
, t
):
1769 'def_bitfield_struct : DEF opt_signed BITFIELD ID id_with_dot SEMI'
1771 error(t
, 'error: structure bitfields are always unsigned.')
1772 expr
= 'machInst.%s' % t
[5]
1773 hash_define
= '#undef %s\n#define %s\t%s\n' % (t
[4], t
[4], expr
)
1774 GenCode(self
, header_output
=hash_define
).emit()
1776 def p_id_with_dot_0(self
, t
):
1780 def p_id_with_dot_1(self
, t
):
1781 'id_with_dot : ID DOT id_with_dot'
1782 t
[0] = t
[1] + t
[2] + t
[3]
1784 def p_opt_signed_0(self
, t
):
1785 'opt_signed : SIGNED'
1788 def p_opt_signed_1(self
, t
):
1789 'opt_signed : empty'
1792 def p_def_template(self
, t
):
1793 'def_template : DEF TEMPLATE ID CODELIT SEMI'
1794 if t
[3] in self
.templateMap
:
1795 print "warning: template %s already defined" % t
[3]
1796 self
.templateMap
[t
[3]] = Template(self
, t
[4])
1798 # An instruction format definition looks like
1799 # "def format <fmt>(<params>) {{...}};"
1800 def p_def_format(self
, t
):
1801 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI'
1802 (id, params
, code
) = (t
[3], t
[5], t
[7])
1803 self
.defFormat(id, params
, code
, t
.lexer
.lineno
)
1805 # The formal parameter list for an instruction format is a
1806 # possibly empty list of comma-separated parameters. Positional
1807 # (standard, non-keyword) parameters must come first, followed by
1808 # keyword parameters, followed by a '*foo' parameter that gets
1809 # excess positional arguments (as in Python). Each of these three
1810 # parameter categories is optional.
1812 # Note that we do not support the '**foo' parameter for collecting
1813 # otherwise undefined keyword args. Otherwise the parameter list
1814 # is (I believe) identical to what is supported in Python.
1816 # The param list generates a tuple, where the first element is a
1817 # list of the positional params and the second element is a dict
1818 # containing the keyword params.
1819 def p_param_list_0(self
, t
):
1820 'param_list : positional_param_list COMMA nonpositional_param_list'
1823 def p_param_list_1(self
, t
):
1824 '''param_list : positional_param_list
1825 | nonpositional_param_list'''
1828 def p_positional_param_list_0(self
, t
):
1829 'positional_param_list : empty'
1832 def p_positional_param_list_1(self
, t
):
1833 'positional_param_list : ID'
1836 def p_positional_param_list_2(self
, t
):
1837 'positional_param_list : positional_param_list COMMA ID'
1838 t
[0] = t
[1] + [t
[3]]
1840 def p_nonpositional_param_list_0(self
, t
):
1841 'nonpositional_param_list : keyword_param_list COMMA excess_args_param'
1844 def p_nonpositional_param_list_1(self
, t
):
1845 '''nonpositional_param_list : keyword_param_list
1846 | excess_args_param'''
1849 def p_keyword_param_list_0(self
, t
):
1850 'keyword_param_list : keyword_param'
1853 def p_keyword_param_list_1(self
, t
):
1854 'keyword_param_list : keyword_param_list COMMA keyword_param'
1855 t
[0] = t
[1] + [t
[3]]
1857 def p_keyword_param(self
, t
):
1858 'keyword_param : ID EQUALS expr'
1859 t
[0] = t
[1] + ' = ' + t
[3].__repr
__()
1861 def p_excess_args_param(self
, t
):
1862 'excess_args_param : ASTERISK ID'
1863 # Just concatenate them: '*ID'. Wrap in list to be consistent
1864 # with positional_param_list and keyword_param_list.
1865 t
[0] = [t
[1] + t
[2]]
1867 # End of format definition-related rules.
1871 # A decode block looks like:
1872 # decode <field1> [, <field2>]* [default <inst>] { ... }
1874 def p_top_level_decode_block(self
, t
):
1875 'top_level_decode_block : decode_block'
1877 codeObj
.wrap_decode_block('''
1879 %(isa_name)s::Decoder::decodeInst(%(isa_name)s::ExtMachInst machInst)
1881 using namespace %(namespace)s;
1886 def p_decode_block(self
, t
):
1887 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE'
1888 default_defaults
= self
.defaultStack
.pop()
1890 # use the "default defaults" only if there was no explicit
1891 # default statement in decode_stmt_list
1892 if not codeObj
.has_decode_default
:
1893 codeObj
+= default_defaults
1894 codeObj
.wrap_decode_block('switch (%s) {\n' % t
[2], '}\n')
1897 # The opt_default statement serves only to push the "default
1898 # defaults" onto defaultStack. This value will be used by nested
1899 # decode blocks, and used and popped off when the current
1900 # decode_block is processed (in p_decode_block() above).
1901 def p_opt_default_0(self
, t
):
1902 'opt_default : empty'
1903 # no default specified: reuse the one currently at the top of
1905 self
.defaultStack
.push(self
.defaultStack
.top())
1906 # no meaningful value returned
1909 def p_opt_default_1(self
, t
):
1910 'opt_default : DEFAULT inst'
1911 # push the new default
1913 codeObj
.wrap_decode_block('\ndefault:\n', 'break;\n')
1914 self
.defaultStack
.push(codeObj
)
1915 # no meaningful value returned
1918 def p_decode_stmt_list_0(self
, t
):
1919 'decode_stmt_list : decode_stmt'
1922 def p_decode_stmt_list_1(self
, t
):
1923 'decode_stmt_list : decode_stmt decode_stmt_list'
1924 if (t
[1].has_decode_default
and t
[2].has_decode_default
):
1925 error(t
, 'Two default cases in decode block')
1929 # Decode statement rules
1931 # There are four types of statements allowed in a decode block:
1932 # 1. Format blocks 'format <foo> { ... }'
1933 # 2. Nested decode blocks
1934 # 3. Instruction definitions.
1935 # 4. C preprocessor directives.
1938 # Preprocessor directives found in a decode statement list are
1939 # passed through to the output, replicated to all of the output
1940 # code streams. This works well for ifdefs, so we can ifdef out
1941 # both the declarations and the decode cases generated by an
1942 # instruction definition. Handling them as part of the grammar
1943 # makes it easy to keep them in the right place with respect to
1944 # the code generated by the other statements.
1945 def p_decode_stmt_cpp(self
, t
):
1946 'decode_stmt : CPPDIRECTIVE'
1947 t
[0] = GenCode(self
, t
[1], t
[1], t
[1], t
[1])
1949 # A format block 'format <foo> { ... }' sets the default
1950 # instruction format used to handle instruction definitions inside
1951 # the block. This format can be overridden by using an explicit
1952 # format on the instruction definition or with a nested format
1954 def p_decode_stmt_format(self
, t
):
1955 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE'
1956 # The format will be pushed on the stack when 'push_format_id'
1957 # is processed (see below). Once the parser has recognized
1958 # the full production (though the right brace), we're done
1959 # with the format, so now we can pop it.
1960 self
.formatStack
.pop()
1963 # This rule exists so we can set the current format (& push the
1964 # stack) when we recognize the format name part of the format
1966 def p_push_format_id(self
, t
):
1967 'push_format_id : ID'
1969 self
.formatStack
.push(self
.formatMap
[t
[1]])
1970 t
[0] = ('', '// format %s' % t
[1])
1972 error(t
, 'instruction format "%s" not defined.' % t
[1])
1974 # Nested decode block: if the value of the current field matches
1975 # the specified constant, do a nested decode on some other field.
1976 def p_decode_stmt_decode(self
, t
):
1977 'decode_stmt : case_label COLON decode_block'
1980 # just wrap the decoding code from the block as a case in the
1981 # outer switch statement.
1982 codeObj
.wrap_decode_block('\n%s:\n' % label
)
1983 codeObj
.has_decode_default
= (label
== 'default')
1986 # Instruction definition (finally!).
1987 def p_decode_stmt_inst(self
, t
):
1988 'decode_stmt : case_label COLON inst SEMI'
1991 codeObj
.wrap_decode_block('\n%s:' % label
, 'break;\n')
1992 codeObj
.has_decode_default
= (label
== 'default')
1995 # The case label is either a list of one or more constants or
1997 def p_case_label_0(self
, t
):
1998 'case_label : intlit_list'
1999 def make_case(intlit
):
2001 return 'case ULL(%#x)' % intlit
2003 return 'case %#x' % intlit
2004 t
[0] = ': '.join(map(make_case
, t
[1]))
2006 def p_case_label_1(self
, t
):
2007 'case_label : DEFAULT'
2011 # The constant list for a decode case label must be non-empty, but
2012 # may have one or more comma-separated integer literals in it.
2014 def p_intlit_list_0(self
, t
):
2015 'intlit_list : INTLIT'
2018 def p_intlit_list_1(self
, t
):
2019 'intlit_list : intlit_list COMMA INTLIT'
2023 # Define an instruction using the current instruction format
2024 # (specified by an enclosing format block).
2025 # "<mnemonic>(<args>)"
2026 def p_inst_0(self
, t
):
2027 'inst : ID LPAREN arg_list RPAREN'
2028 # Pass the ID and arg list to the current format class to deal with.
2029 currentFormat
= self
.formatStack
.top()
2030 codeObj
= currentFormat
.defineInst(self
, t
[1], t
[3], t
.lexer
.lineno
)
2031 args
= ','.join(map(str, t
[3]))
2032 args
= re
.sub('(?m)^', '//', args
)
2033 args
= re
.sub('^//', '', args
)
2034 comment
= '\n// %s::%s(%s)\n' % (currentFormat
.id, t
[1], args
)
2035 codeObj
.prepend_all(comment
)
2038 # Define an instruction using an explicitly specified format:
2039 # "<fmt>::<mnemonic>(<args>)"
2040 def p_inst_1(self
, t
):
2041 'inst : ID DBLCOLON ID LPAREN arg_list RPAREN'
2043 format
= self
.formatMap
[t
[1]]
2045 error(t
, 'instruction format "%s" not defined.' % t
[1])
2047 codeObj
= format
.defineInst(self
, t
[3], t
[5], t
.lexer
.lineno
)
2048 comment
= '\n// %s::%s(%s)\n' % (t
[1], t
[3], t
[5])
2049 codeObj
.prepend_all(comment
)
2052 # The arg list generates a tuple, where the first element is a
2053 # list of the positional args and the second element is a dict
2054 # containing the keyword args.
2055 def p_arg_list_0(self
, t
):
2056 'arg_list : positional_arg_list COMMA keyword_arg_list'
2057 t
[0] = ( t
[1], t
[3] )
2059 def p_arg_list_1(self
, t
):
2060 'arg_list : positional_arg_list'
2063 def p_arg_list_2(self
, t
):
2064 'arg_list : keyword_arg_list'
2067 def p_positional_arg_list_0(self
, t
):
2068 'positional_arg_list : empty'
2071 def p_positional_arg_list_1(self
, t
):
2072 'positional_arg_list : expr'
2075 def p_positional_arg_list_2(self
, t
):
2076 'positional_arg_list : positional_arg_list COMMA expr'
2077 t
[0] = t
[1] + [t
[3]]
2079 def p_keyword_arg_list_0(self
, t
):
2080 'keyword_arg_list : keyword_arg'
2083 def p_keyword_arg_list_1(self
, t
):
2084 'keyword_arg_list : keyword_arg_list COMMA keyword_arg'
2088 def p_keyword_arg(self
, t
):
2089 'keyword_arg : ID EQUALS expr'
2090 t
[0] = { t
[1] : t
[3] }
2093 # Basic expressions. These constitute the argument values of
2094 # "function calls" (i.e. instruction definitions in the decode
2095 # block) and default values for formal parameters of format
2098 # Right now, these are either strings, integers, or (recursively)
2099 # lists of exprs (using Python square-bracket list syntax). Note
2100 # that bare identifiers are trated as string constants here (since
2101 # there isn't really a variable namespace to refer to).
2103 def p_expr_0(self
, t
):
2110 def p_expr_1(self
, t
):
2111 '''expr : LBRACKET list_expr RBRACKET'''
2114 def p_list_expr_0(self
, t
):
2118 def p_list_expr_1(self
, t
):
2119 'list_expr : list_expr COMMA expr'
2120 t
[0] = t
[1] + [t
[3]]
2122 def p_list_expr_2(self
, t
):
2127 # Empty production... use in other rules for readability.
2129 def p_empty(self
, t
):
2133 # Parse error handler. Note that the argument here is the
2134 # offending *token*, not a grammar symbol (hence the need to use
2136 def p_error(self
, t
):
2138 error(t
, "syntax error at '%s'" % t
.value
)
2140 error("unknown syntax error")
2142 # END OF GRAMMAR RULES
2144 def updateExportContext(self
):
2146 # create a continuation that allows us to grab the current parser
2147 def wrapInstObjParams(*args
):
2148 return InstObjParams(self
, *args
)
2149 self
.exportContext
['InstObjParams'] = wrapInstObjParams
2150 self
.exportContext
.update(self
.templateMap
)
2152 def defFormat(self
, id, params
, code
, lineno
):
2153 '''Define a new format'''
2155 # make sure we haven't already defined this one
2156 if id in self
.formatMap
:
2157 error(lineno
, 'format %s redefined.' % id)
2159 # create new object and store in global map
2160 self
.formatMap
[id] = Format(id, params
, code
)
2162 def expandCpuSymbolsToDict(self
, template
):
2163 '''Expand template with CPU-specific references into a
2164 dictionary with an entry for each CPU model name. The entry
2165 key is the model name and the corresponding value is the
2166 template with the CPU-specific refs substituted for that
2169 # Protect '%'s that don't go with CPU-specific terms
2170 t
= re
.sub(r
'%(?!\(CPU_)', '%%', template
)
2172 for cpu
in self
.cpuModels
:
2173 result
[cpu
.name
] = t
% cpu
.strings
2176 def expandCpuSymbolsToString(self
, template
):
2177 '''*If* the template has CPU-specific references, return a
2178 single string containing a copy of the template for each CPU
2179 model with the corresponding values substituted in. If the
2180 template has no CPU-specific references, it is returned
2183 if template
.find('%(CPU_') != -1:
2184 return reduce(lambda x
,y
: x
+y
,
2185 self
.expandCpuSymbolsToDict(template
).values())
2189 def protectCpuSymbols(self
, template
):
2190 '''Protect CPU-specific references by doubling the
2191 corresponding '%'s (in preparation for substituting a different
2192 set of references into the template).'''
2194 return re
.sub(r
'%(?=\(CPU_)', '%%', template
)
2196 def protectNonSubstPercents(self
, s
):
2197 '''Protect any non-dict-substitution '%'s in a format string
2198 (i.e. those not followed by '(')'''
2200 return re
.sub(r
'%(?!\()', '%%', s
)
2202 def buildOperandNameMap(self
, user_dict
, lineno
):
2204 for op_name
, val
in user_dict
.iteritems():
2206 # Check if extra attributes have been specified.
2208 error(lineno
, 'error: too many attributes for operand "%s"' %
2211 # Pad val with None in case optional args are missing
2212 val
+= (None, None, None, None)
2213 base_cls_name
, dflt_ext
, reg_spec
, flags
, sort_pri
, \
2214 read_code
, write_code
, read_predicate
, write_predicate
= val
[:9]
2216 # Canonical flag structure is a triple of lists, where each list
2217 # indicates the set of flags implied by this operand always, when
2218 # used as a source, and when used as a dest, respectively.
2219 # For simplicity this can be initialized using a variety of fairly
2220 # obvious shortcuts; we convert these to canonical form here.
2222 # no flags specified (e.g., 'None')
2223 flags
= ( [], [], [] )
2224 elif isinstance(flags
, str):
2225 # a single flag: assumed to be unconditional
2226 flags
= ( [ flags
], [], [] )
2227 elif isinstance(flags
, list):
2228 # a list of flags: also assumed to be unconditional
2229 flags
= ( flags
, [], [] )
2230 elif isinstance(flags
, tuple):
2231 # it's a tuple: it should be a triple,
2232 # but each item could be a single string or a list
2233 (uncond_flags
, src_flags
, dest_flags
) = flags
2234 flags
= (makeList(uncond_flags
),
2235 makeList(src_flags
), makeList(dest_flags
))
2237 # Accumulate attributes of new operand class in tmp_dict
2239 attrList
= ['reg_spec', 'flags', 'sort_pri',
2240 'read_code', 'write_code',
2241 'read_predicate', 'write_predicate']
2243 dflt_ctype
= self
.operandTypeMap
[dflt_ext
]
2244 attrList
.extend(['dflt_ctype', 'dflt_ext'])
2245 for attr
in attrList
:
2246 tmp_dict
[attr
] = eval(attr
)
2247 tmp_dict
['base_name'] = op_name
2249 # New class name will be e.g. "IntReg_Ra"
2250 cls_name
= base_cls_name
+ '_' + op_name
2251 # Evaluate string arg to get class object. Note that the
2252 # actual base class for "IntReg" is "IntRegOperand", i.e. we
2253 # have to append "Operand".
2255 base_cls
= eval(base_cls_name
+ 'Operand')
2258 'error: unknown operand base class "%s"' % base_cls_name
)
2259 # The following statement creates a new class called
2260 # <cls_name> as a subclass of <base_cls> with the attributes
2261 # in tmp_dict, just as if we evaluated a class declaration.
2262 operand_name
[op_name
] = type(cls_name
, (base_cls
,), tmp_dict
)
2264 self
.operandNameMap
= operand_name
2266 # Define operand variables.
2267 operands
= user_dict
.keys()
2268 extensions
= self
.operandTypeMap
.keys()
2270 operandsREString
= r
'''
2271 (?<!\w) # neg. lookbehind assertion: prevent partial matches
2272 ((%s)(?:_(%s))?) # match: operand with optional '_' then suffix
2273 (?!\w) # neg. lookahead assertion: prevent partial matches
2274 ''' % (string
.join(operands
, '|'), string
.join(extensions
, '|'))
2276 self
.operandsRE
= re
.compile(operandsREString
, re
.MULTILINE|re
.VERBOSE
)
2278 # Same as operandsREString, but extension is mandatory, and only two
2279 # groups are returned (base and ext, not full name as above).
2280 # Used for subtituting '_' for '.' to make C++ identifiers.
2281 operandsWithExtREString
= r
'(?<!\w)(%s)_(%s)(?!\w)' \
2282 % (string
.join(operands
, '|'), string
.join(extensions
, '|'))
2284 self
.operandsWithExtRE
= \
2285 re
.compile(operandsWithExtREString
, re
.MULTILINE
)
2287 def substMungedOpNames(self
, code
):
2288 '''Munge operand names in code string to make legal C++
2289 variable names. This means getting rid of the type extension
2290 if any. Will match base_name attribute of Operand object.)'''
2291 return self
.operandsWithExtRE
.sub(r
'\1', code
)
2293 def mungeSnippet(self
, s
):
2294 '''Fix up code snippets for final substitution in templates.'''
2295 if isinstance(s
, str):
2296 return self
.substMungedOpNames(substBitOps(s
))
2300 def open(self
, name
, bare
=False):
2301 '''Open the output file for writing and include scary warning.'''
2302 filename
= os
.path
.join(self
.output_dir
, name
)
2303 f
= open(filename
, 'w')
2306 f
.write(ISAParser
.scaremonger_template
% self
)
2309 def update(self
, file, contents
):
2310 '''Update the output file only. Scons should handle the case when
2311 the new contents are unchanged using its built-in hash feature.'''
2316 # This regular expression matches '##include' directives
2317 includeRE
= re
.compile(r
'^\s*##include\s+"(?P<filename>[^"]*)".*$',
2320 def replace_include(self
, matchobj
, dirname
):
2321 """Function to replace a matched '##include' directive with the
2322 contents of the specified file (with nested ##includes
2323 replaced recursively). 'matchobj' is an re match object
2324 (from a match of includeRE) and 'dirname' is the directory
2325 relative to which the file path should be resolved."""
2327 fname
= matchobj
.group('filename')
2328 full_fname
= os
.path
.normpath(os
.path
.join(dirname
, fname
))
2329 contents
= '##newfile "%s"\n%s\n##endfile\n' % \
2330 (full_fname
, self
.read_and_flatten(full_fname
))
2333 def read_and_flatten(self
, filename
):
2334 """Read a file and recursively flatten nested '##include' files."""
2336 current_dir
= os
.path
.dirname(filename
)
2338 contents
= open(filename
).read()
2340 error('Error including file "%s"' % filename
)
2342 self
.fileNameStack
.push((filename
, 0))
2344 # Find any includes and include them
2345 def replace(matchobj
):
2346 return self
.replace_include(matchobj
, current_dir
)
2347 contents
= self
.includeRE
.sub(replace
, contents
)
2349 self
.fileNameStack
.pop()
2352 AlreadyGenerated
= {}
2354 def _parse_isa_desc(self
, isa_desc_file
):
2355 '''Read in and parse the ISA description.'''
2357 # The build system can end up running the ISA parser twice: once to
2358 # finalize the build dependencies, and then to actually generate
2359 # the files it expects (in src/arch/$ARCH/generated). This code
2360 # doesn't do anything different either time, however; the SCons
2361 # invocations just expect different things. Since this code runs
2362 # within SCons, we can just remember that we've already run and
2363 # not perform a completely unnecessary run, since the ISA parser's
2364 # effect is idempotent.
2365 if isa_desc_file
in ISAParser
.AlreadyGenerated
:
2368 # grab the last three path components of isa_desc_file
2369 self
.filename
= '/'.join(isa_desc_file
.split('/')[-3:])
2371 # Read file and (recursively) all included files into a string.
2372 # PLY requires that the input be in a single string so we have to
2374 isa_desc
= self
.read_and_flatten(isa_desc_file
)
2376 # Initialize filename stack with outer file.
2377 self
.fileNameStack
.push((isa_desc_file
, 0))
2380 self
.parse_string(isa_desc
)
2382 ISAParser
.AlreadyGenerated
[isa_desc_file
] = None
2384 def parse_isa_desc(self
, *args
, **kwargs
):
2386 self
._parse
_isa
_desc
(*args
, **kwargs
)
2387 except ISAParserError
, e
:
2388 e
.exit(self
.fileNameStack
)
2390 # Called as script: get args from command line.
2391 # Args are: <isa desc file> <output dir>
2392 if __name__
== '__main__':
2393 ISAParser(sys
.argv
[2]).parse_isa_desc(sys
.argv
[1])