Deleted Added
sdiff udiff text old ( 10170:7e7cd19c9d9e ) new ( 10196:be0e1724eb39 )
full compact
1# Copyright (c) 2003-2005 The Regents of The University of Michigan
2# Copyright (c) 2013 Advanced Micro Devices, Inc.
3# All rights reserved.
4#
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;

--- 13 unchanged lines hidden (view full) ---

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.
27#
28# Authors: Steve Reinhardt
29
30import os
31import sys
32import re
33import string
34import inspect, traceback
35# get type names
36from types import *
37

--- 270 unchanged lines hidden (view full) ---

308 # dictionary. For all other output types they get collapsed into
309 # a single string.
310 def __init__(self, parser,
311 header_output = '', decoder_output = '', exec_output = '',
312 decode_block = '', has_decode_default = False):
313 self.parser = parser
314 self.header_output = parser.expandCpuSymbolsToString(header_output)
315 self.decoder_output = parser.expandCpuSymbolsToString(decoder_output)
316 if isinstance(exec_output, dict):
317 self.exec_output = exec_output
318 elif isinstance(exec_output, str):
319 # If the exec_output arg is a single string, we replicate
320 # it for each of the CPU models, substituting and
321 # %(CPU_foo)s params appropriately.
322 self.exec_output = parser.expandCpuSymbolsToDict(exec_output)
323 self.decode_block = parser.expandCpuSymbolsToString(decode_block)
324 self.has_decode_default = has_decode_default
325
326 # Override '+' operator: generate a new GenCode object that
327 # concatenates all the individual strings in the operands.
328 def __add__(self, other):
329 exec_output = {}
330 for cpu in self.parser.cpuModels:
331 n = cpu.name
332 exec_output[n] = self.exec_output[n] + other.exec_output[n]
333 return GenCode(self.parser,
334 self.header_output + other.header_output,
335 self.decoder_output + other.decoder_output,
336 exec_output,
337 self.decode_block + other.decode_block,
338 self.has_decode_default or other.has_decode_default)
339
340 # Prepend a string (typically a comment) to all the strings.
341 def prepend_all(self, pre):
342 self.header_output = pre + self.header_output
343 self.decoder_output = pre + self.decoder_output
344 self.decode_block = pre + self.decode_block
345 for cpu in self.parser.cpuModels:
346 self.exec_output[cpu.name] = pre + self.exec_output[cpu.name]
347
348 # Wrap the decode block in a pair of strings (e.g., 'case foo:'
349 # and 'break;'). Used to build the big nested switch statement.
350 def wrap_decode_block(self, pre, post = ''):
351 self.decode_block = pre + indent(self.decode_block) + post
352
353#####################################################################
354#

--- 811 unchanged lines hidden (view full) ---

1166 def push(self, item):
1167 self.append(item);
1168
1169 def top(self):
1170 return self[-1]
1171
1172#######################
1173#
1174# Output file template
1175#
1176
1177file_template = '''
1178/*
1179 * DO NOT EDIT THIS FILE!!!
1180 *
1181 * It was automatically generated from the ISA description in %(filename)s
1182 */
1183
1184%(includes)s
1185
1186%(global_output)s
1187
1188namespace %(namespace)s {
1189
1190%(namespace_output)s
1191
1192} // namespace %(namespace)s
1193
1194%(decode_function)s
1195'''
1196
1197max_inst_regs_template = '''
1198/*
1199 * DO NOT EDIT THIS FILE!!!
1200 *
1201 * It was automatically generated from the ISA description in %(filename)s
1202 */
1203
1204namespace %(namespace)s {
1205
1206 const int MaxInstSrcRegs = %(MaxInstSrcRegs)d;
1207 const int MaxInstDestRegs = %(MaxInstDestRegs)d;
1208 const int MaxMiscDestRegs = %(MaxMiscDestRegs)d;
1209
1210} // namespace %(namespace)s
1211
1212'''
1213
1214class ISAParser(Grammar):
1215 def __init__(self, output_dir, cpu_models):
1216 super(ISAParser, self).__init__()
1217 self.output_dir = output_dir
1218
1219 self.cpuModels = cpu_models
1220
1221 # variable to hold templates
1222 self.templateMap = {}
1223
1224 # This dictionary maps format name strings to Format objects.
1225 self.formatMap = {}
1226
1227 # The format stack.
1228 self.formatStack = Stack(NoFormat())
1229
1230 # The default case stack.
1231 self.defaultStack = Stack(None)
1232
1233 # Stack that tracks current file and line number. Each
1234 # element is a tuple (filename, lineno) that records the
1235 # *current* filename and the line number in the *previous*
1236 # file where it was included.
1237 self.fileNameStack = Stack()
1238
1239 symbols = ('makeList', 're', 'string')
1240 self.exportContext = dict([(s, eval(s)) for s in symbols])
1241
1242 self.maxInstSrcRegs = 0
1243 self.maxInstDestRegs = 0
1244 self.maxMiscDestRegs = 0
1245
1246 #####################################################################
1247 #
1248 # Lexer
1249 #
1250 # The PLY lexer module takes two things as input:
1251 # - A list of token names (the string list 'tokens')
1252 # - A regular expression describing a match for each token. The
1253 # regexp for token FOO can be provided in two ways:

--- 5 unchanged lines hidden (view full) ---

1259 #####################################################################
1260
1261 # Reserved words. These are listed separately as they are matched
1262 # using the same regexp as generic IDs, but distinguished in the
1263 # t_ID() function. The PLY documentation suggests this approach.
1264 reserved = (
1265 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT',
1266 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS',
1267 'OUTPUT', 'SIGNED', 'TEMPLATE'
1268 )
1269
1270 # List of tokens. The lex module requires this.
1271 tokens = reserved + (
1272 # identifier
1273 'ID',
1274
1275 # integer literal

--- 136 unchanged lines hidden (view full) ---

1412 # The LHS of the first grammar rule is used as the start symbol
1413 # (in this case, 'specification'). Note that this rule enforces
1414 # that there will be exactly one namespace declaration, with 0 or
1415 # more global defs/decls before and after it. The defs & decls
1416 # before the namespace decl will be outside the namespace; those
1417 # after will be inside. The decoder function is always inside the
1418 # namespace.
1419 def p_specification(self, t):
1420 'specification : opt_defs_and_outputs name_decl opt_defs_and_outputs decode_block'
1421 global_code = t[1]
1422 isa_name = t[2]
1423 namespace = isa_name + "Inst"
1424 # wrap the decode block as a function definition
1425 t[4].wrap_decode_block('''
1426StaticInstPtr
1427%(isa_name)s::Decoder::decodeInst(%(isa_name)s::ExtMachInst machInst)
1428{
1429 using namespace %(namespace)s;
1430''' % vars(), '}')
1431 # both the latter output blocks and the decode block are in
1432 # the namespace
1433 namespace_code = t[3] + t[4]
1434 # pass it all back to the caller of yacc.parse()
1435 t[0] = (isa_name, namespace, global_code, namespace_code)
1436
1437 # ISA name declaration looks like "namespace <foo>;"
1438 def p_name_decl(self, t):
1439 'name_decl : NAMESPACE ID SEMI'
1440 t[0] = t[2]
1441
1442 # 'opt_defs_and_outputs' is a possibly empty sequence of
1443 # def and/or output statements.
1444 def p_opt_defs_and_outputs_0(self, t):
1445 'opt_defs_and_outputs : empty'
1446 t[0] = GenCode(self)
1447
1448 def p_opt_defs_and_outputs_1(self, t):
1449 'opt_defs_and_outputs : defs_and_outputs'
1450 t[0] = t[1]
1451
1452 def p_defs_and_outputs_0(self, t):
1453 'defs_and_outputs : def_or_output'
1454 t[0] = t[1]
1455
1456 def p_defs_and_outputs_1(self, t):
1457 'defs_and_outputs : defs_and_outputs def_or_output'
1458 t[0] = t[1] + t[2]
1459
1460 # The list of possible definition/output statements.
1461 def p_def_or_output(self, t):
1462 '''def_or_output : def_format
1463 | def_bitfield
1464 | def_bitfield_struct
1465 | def_template
1466 | def_operand_types
1467 | def_operands
1468 | output_header
1469 | output_decoder
1470 | output_exec
1471 | global_let'''
1472 t[0] = t[1]
1473
1474 # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied
1475 # directly to the appropriate output section.
1476
1477 # Massage output block by substituting in template definitions and
1478 # bit operators. We handle '%'s embedded in the string that don't
1479 # indicate template substitutions (or CPU-specific symbols, which
1480 # get handled in GenCode) by doubling them first so that the
1481 # format operation will reduce them back to single '%'s.
1482 def process_output(self, s):
1483 s = self.protectNonSubstPercents(s)
1484 # protects cpu-specific symbols too
1485 s = self.protectCpuSymbols(s)
1486 return substBitOps(s % self.templateMap)
1487
1488 def p_output_header(self, t):
1489 'output_header : OUTPUT HEADER CODELIT SEMI'
1490 t[0] = GenCode(self, header_output = self.process_output(t[3]))
1491
1492 def p_output_decoder(self, t):
1493 'output_decoder : OUTPUT DECODER CODELIT SEMI'
1494 t[0] = GenCode(self, decoder_output = self.process_output(t[3]))
1495
1496 def p_output_exec(self, t):
1497 'output_exec : OUTPUT EXEC CODELIT SEMI'
1498 t[0] = GenCode(self, exec_output = self.process_output(t[3]))
1499
1500 # global let blocks 'let {{...}}' (Python code blocks) are
1501 # executed directly when seen. Note that these execute in a
1502 # special variable context 'exportContext' to prevent the code
1503 # from polluting this script's namespace.
1504 def p_global_let(self, t):
1505 'global_let : LET CODELIT SEMI'
1506 self.updateExportContext()
1507 self.exportContext["header_output"] = ''
1508 self.exportContext["decoder_output"] = ''
1509 self.exportContext["exec_output"] = ''
1510 self.exportContext["decode_block"] = ''
1511 try:
1512 exec fixPythonIndentation(t[2]) in self.exportContext
1513 except Exception, exc:
1514 if debug:
1515 raise
1516 error(t, 'error: %s in global let block "%s".' % (exc, t[2]))
1517 t[0] = GenCode(self,
1518 header_output=self.exportContext["header_output"],
1519 decoder_output=self.exportContext["decoder_output"],
1520 exec_output=self.exportContext["exec_output"],
1521 decode_block=self.exportContext["decode_block"])
1522
1523 # Define the mapping from operand type extensions to C++ types and
1524 # bit widths (stored in operandTypeMap).
1525 def p_def_operand_types(self, t):
1526 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI'
1527 try:
1528 self.operandTypeMap = eval('{' + t[3] + '}')
1529 except Exception, exc:
1530 if debug:
1531 raise
1532 error(t,
1533 'error: %s in def operand_types block "%s".' % (exc, t[3]))
1534 t[0] = GenCode(self) # contributes nothing to the output C++ file
1535
1536 # Define the mapping from operand names to operand classes and
1537 # other traits. Stored in operandNameMap.
1538 def p_def_operands(self, t):
1539 'def_operands : DEF OPERANDS CODELIT SEMI'
1540 if not hasattr(self, 'operandTypeMap'):
1541 error(t, 'error: operand types must be defined before operands')
1542 try:
1543 user_dict = eval('{' + t[3] + '}', self.exportContext)
1544 except Exception, exc:
1545 if debug:
1546 raise
1547 error(t, 'error: %s in def operands block "%s".' % (exc, t[3]))
1548 self.buildOperandNameMap(user_dict, t.lexer.lineno)
1549 t[0] = GenCode(self) # contributes nothing to the output C++ file
1550
1551 # A bitfield definition looks like:
1552 # 'def [signed] bitfield <ID> [<first>:<last>]'
1553 # This generates a preprocessor macro in the output file.
1554 def p_def_bitfield_0(self, t):
1555 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI'
1556 expr = 'bits(machInst, %2d, %2d)' % (t[6], t[8])
1557 if (t[2] == 'signed'):
1558 expr = 'sext<%d>(%s)' % (t[6] - t[8] + 1, expr)
1559 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
1560 t[0] = GenCode(self, header_output=hash_define)
1561
1562 # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]'
1563 def p_def_bitfield_1(self, t):
1564 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI'
1565 expr = 'bits(machInst, %2d, %2d)' % (t[6], t[6])
1566 if (t[2] == 'signed'):
1567 expr = 'sext<%d>(%s)' % (1, expr)
1568 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
1569 t[0] = GenCode(self, header_output=hash_define)
1570
1571 # alternate form for structure member: 'def bitfield <ID> <ID>'
1572 def p_def_bitfield_struct(self, t):
1573 'def_bitfield_struct : DEF opt_signed BITFIELD ID id_with_dot SEMI'
1574 if (t[2] != ''):
1575 error(t, 'error: structure bitfields are always unsigned.')
1576 expr = 'machInst.%s' % t[5]
1577 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
1578 t[0] = GenCode(self, header_output=hash_define)
1579
1580 def p_id_with_dot_0(self, t):
1581 'id_with_dot : ID'
1582 t[0] = t[1]
1583
1584 def p_id_with_dot_1(self, t):
1585 'id_with_dot : ID DOT id_with_dot'
1586 t[0] = t[1] + t[2] + t[3]
1587
1588 def p_opt_signed_0(self, t):
1589 'opt_signed : SIGNED'
1590 t[0] = t[1]
1591
1592 def p_opt_signed_1(self, t):
1593 'opt_signed : empty'
1594 t[0] = ''
1595
1596 def p_def_template(self, t):
1597 'def_template : DEF TEMPLATE ID CODELIT SEMI'
1598 self.templateMap[t[3]] = Template(self, t[4])
1599 t[0] = GenCode(self)
1600
1601 # An instruction format definition looks like
1602 # "def format <fmt>(<params>) {{...}};"
1603 def p_def_format(self, t):
1604 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI'
1605 (id, params, code) = (t[3], t[5], t[7])
1606 self.defFormat(id, params, code, t.lexer.lineno)
1607 t[0] = GenCode(self)
1608
1609 # The formal parameter list for an instruction format is a
1610 # possibly empty list of comma-separated parameters. Positional
1611 # (standard, non-keyword) parameters must come first, followed by
1612 # keyword parameters, followed by a '*foo' parameter that gets
1613 # excess positional arguments (as in Python). Each of these three
1614 # parameter categories is optional.
1615 #

--- 54 unchanged lines hidden (view full) ---

1670
1671 # End of format definition-related rules.
1672 ##############
1673
1674 #
1675 # A decode block looks like:
1676 # decode <field1> [, <field2>]* [default <inst>] { ... }
1677 #
1678 def p_decode_block(self, t):
1679 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE'
1680 default_defaults = self.defaultStack.pop()
1681 codeObj = t[5]
1682 # use the "default defaults" only if there was no explicit
1683 # default statement in decode_stmt_list
1684 if not codeObj.has_decode_default:
1685 codeObj += default_defaults

--- 398 unchanged lines hidden (view full) ---

2084
2085 def mungeSnippet(self, s):
2086 '''Fix up code snippets for final substitution in templates.'''
2087 if isinstance(s, str):
2088 return self.substMungedOpNames(substBitOps(s))
2089 else:
2090 return s
2091
2092 def update(self, file, contents):
2093 '''Update the output file. If the contents are unchanged,
2094 the scons hash feature will avoid recompilation.'''
2095 file = os.path.join(self.output_dir, file)
2096 f = open(file, 'w')
2097 f.write(contents)
2098 f.close()
2099
2100 # This regular expression matches '##include' directives
2101 includeRE = re.compile(r'^\s*##include\s+"(?P<filename>[^"]*)".*$',
2102 re.MULTILINE)
2103
2104 def replace_include(self, matchobj, dirname):

--- 23 unchanged lines hidden (view full) ---

2128 # Find any includes and include them
2129 def replace(matchobj):
2130 return self.replace_include(matchobj, current_dir)
2131 contents = self.includeRE.sub(replace, contents)
2132
2133 self.fileNameStack.pop()
2134 return contents
2135
2136 def _parse_isa_desc(self, isa_desc_file):
2137 '''Read in and parse the ISA description.'''
2138
2139 # Read file and (recursively) all included files into a string.
2140 # PLY requires that the input be in a single string so we have to
2141 # do this up front.
2142 isa_desc = self.read_and_flatten(isa_desc_file)
2143
2144 # Initialize filename stack with outer file.
2145 self.fileNameStack.push((isa_desc_file, 0))
2146
2147 # Parse it.
2148 (isa_name, namespace, global_code, namespace_code) = \
2149 self.parse_string(isa_desc)
2150
2151 # grab the last three path components of isa_desc_file to put in
2152 # the output
2153 filename = '/'.join(isa_desc_file.split('/')[-3:])
2154
2155 # generate decoder.hh
2156 includes = '#include "base/bitfield.hh" // for bitfield support'
2157 global_output = global_code.header_output
2158 namespace_output = namespace_code.header_output
2159 decode_function = ''
2160 self.update('decoder.hh', file_template % vars())
2161
2162 # generate decoder.cc
2163 includes = '#include "decoder.hh"'
2164 global_output = global_code.decoder_output
2165 namespace_output = namespace_code.decoder_output
2166 # namespace_output += namespace_code.decode_block
2167 decode_function = namespace_code.decode_block
2168 self.update('decoder.cc', file_template % vars())
2169
2170 # generate per-cpu exec files
2171 for cpu in self.cpuModels:
2172 includes = '#include "decoder.hh"\n'
2173 includes += cpu.includes
2174 global_output = global_code.exec_output[cpu.name]
2175 namespace_output = namespace_code.exec_output[cpu.name]
2176 decode_function = ''
2177 self.update(cpu.filename, file_template % vars())
2178
2179 # The variable names here are hacky, but this will creat local
2180 # variables which will be referenced in vars() which have the
2181 # value of the globals.
2182 MaxInstSrcRegs = self.maxInstSrcRegs
2183 MaxInstDestRegs = self.maxInstDestRegs
2184 MaxMiscDestRegs = self.maxMiscDestRegs
2185 # max_inst_regs.hh
2186 self.update('max_inst_regs.hh',
2187 max_inst_regs_template % vars())
2188
2189 def parse_isa_desc(self, *args, **kwargs):
2190 try:
2191 self._parse_isa_desc(*args, **kwargs)
2192 except ISAParserError, e:
2193 e.exit(self.fileNameStack)
2194
2195# Called as script: get args from command line.
2196# Args are: <path to cpu_models.py> <isa desc file> <output dir> <cpu models>
2197if __name__ == '__main__':
2198 execfile(sys.argv[1]) # read in CpuModel definitions
2199 cpu_models = [CpuModel.dict[cpu] for cpu in sys.argv[4:]]
2200 ISAParser(sys.argv[3], cpu_models).parse_isa_desc(sys.argv[2])