isa_parser.py (3841:9fe36a649632) isa_parser.py (3949:b6664282d899)
1# Copyright (c) 2003-2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

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

803
804################
805# Format object.
806#
807# A format object encapsulates an instruction format. It must provide
808# a defineInst() method that generates the code for an instruction
809# definition.
810
1# Copyright (c) 2003-2005 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

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

803
804################
805# Format object.
806#
807# A format object encapsulates an instruction format. It must provide
808# a defineInst() method that generates the code for an instruction
809# definition.
810
811exportContextSymbols = ('InstObjParams', 'CodeBlock',
812 'makeList', 're', 'string')
811exportContextSymbols = ('InstObjParams', 'makeList', 're', 'string')
813
814exportContext = {}
815
816def updateExportContext():
817 exportContext.update(exportDict(*exportContextSymbols))
818 exportContext.update(templateMap)
819
820def exportDict(*symNames):

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

998
999
1000####################
1001# Template objects.
1002#
1003# Template objects are format strings that allow substitution from
1004# the attribute spaces of other objects (e.g. InstObjParams instances).
1005
812
813exportContext = {}
814
815def updateExportContext():
816 exportContext.update(exportDict(*exportContextSymbols))
817 exportContext.update(templateMap)
818
819def exportDict(*symNames):

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

997
998
999####################
1000# Template objects.
1001#
1002# Template objects are format strings that allow substitution from
1003# the attribute spaces of other objects (e.g. InstObjParams instances).
1004
1005labelRE = re.compile(r'[^%]%\(([^\)]+)\)[sd]')
1006
1006class Template:
1007 def __init__(self, t):
1008 self.template = t
1009
1010 def subst(self, d):
1007class Template:
1008 def __init__(self, t):
1009 self.template = t
1010
1011 def subst(self, d):
1011 # Start with the template namespace. Make a copy since we're
1012 # going to modify it.
1013 myDict = templateMap.copy()
1014 # if the argument is a dictionary, we just use it.
1015 if isinstance(d, dict):
1016 myDict.update(d)
1017 # if the argument is an object, we use its attribute map.
1018 elif hasattr(d, '__dict__'):
1019 myDict.update(d.__dict__)
1020 else:
1021 raise TypeError, "Template.subst() arg must be or have dictionary"
1012 myDict = None
1013
1022 # Protect non-Python-dict substitutions (e.g. if there's a printf
1023 # in the templated C++ code)
1024 template = protect_non_subst_percents(self.template)
1025 # CPU-model-specific substitutions are handled later (in GenCode).
1026 template = protect_cpu_symbols(template)
1014 # Protect non-Python-dict substitutions (e.g. if there's a printf
1015 # in the templated C++ code)
1016 template = protect_non_subst_percents(self.template)
1017 # CPU-model-specific substitutions are handled later (in GenCode).
1018 template = protect_cpu_symbols(template)
1019
1020 # if we're dealing with an InstObjParams object, we need to be a
1021 # little more sophisticated. Otherwise, just do what we've always
1022 # done
1023 if isinstance(d, InstObjParams):
1024 # The instruction wide parameters are already formed, but the
1025 # parameters which are only function wide still need to be
1026 # generated.
1027 perFuncNames = ['op_decl', 'op_src_decl', 'op_dest_decl', \
1028 'op_rd', 'op_wb', 'mem_acc_size', 'mem_acc_type']
1029 compositeCode = ''
1030
1031 myDict = templateMap.copy()
1032 myDict.update(d.__dict__)
1033 # The "operands" and "snippets" attributes of the InstObjParams
1034 # objects are for internal use and not substitution.
1035 del myDict['operands']
1036 del myDict['snippets']
1037
1038 for name in labelRE.findall(template):
1039 # Don't try to find a snippet to go with things that will
1040 # match against attributes of d, or that are other templates,
1041 # or that we're going to generate later, or that we've already
1042 # found.
1043 if not hasattr(d, name) and \
1044 not templateMap.has_key(name) and \
1045 not myDict.has_key(name) and \
1046 name not in perFuncNames:
1047 myDict[name] = d.snippets[name]
1048 if isinstance(myDict[name], str):
1049 myDict[name] = substMungedOpNames(substBitOps(myDict[name]))
1050 compositeCode += (" " + myDict[name])
1051 operands = SubOperandList(compositeCode, d.operands)
1052
1053 myDict['op_decl'] = operands.concatAttrStrings('op_decl')
1054
1055 is_src = lambda op: op.is_src
1056 is_dest = lambda op: op.is_dest
1057
1058 myDict['op_src_decl'] = \
1059 operands.concatSomeAttrStrings(is_src, 'op_src_decl')
1060 myDict['op_dest_decl'] = \
1061 operands.concatSomeAttrStrings(is_dest, 'op_dest_decl')
1062
1063 myDict['op_rd'] = operands.concatAttrStrings('op_rd')
1064 myDict['op_wb'] = operands.concatAttrStrings('op_wb')
1065
1066 if d.operands.memOperand:
1067 myDict['mem_acc_size'] = d.operands.memOperand.mem_acc_size
1068 myDict['mem_acc_type'] = d.operands.memOperand.mem_acc_type
1069
1070 else:
1071 # Start with the template namespace. Make a copy since we're
1072 # going to modify it.
1073 myDict = templateMap.copy()
1074 # if the argument is a dictionary, we just use it.
1075 if isinstance(d, dict):
1076 myDict.update(d)
1077 # if the argument is an object, we use its attribute map.
1078 elif hasattr(d, '__dict__'):
1079 myDict.update(d.__dict__)
1080 else:
1081 raise TypeError, "Template.subst() arg must be or have dictionary"
1027 return template % myDict
1028
1029 # Convert to string. This handles the case when a template with a
1030 # CPU-specific term gets interpolated into another template or into
1031 # an output block.
1032 def __str__(self):
1033 return expand_cpu_symbols_to_string(self.template)
1034

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

1291 return 1
1292
1293 def isControlReg(self):
1294 return 1
1295
1296 def makeConstructor(self):
1297 c = ''
1298 if self.is_src:
1082 return template % myDict
1083
1084 # Convert to string. This handles the case when a template with a
1085 # CPU-specific term gets interpolated into another template or into
1086 # an output block.
1087 def __str__(self):
1088 return expand_cpu_symbols_to_string(self.template)
1089

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

1346 return 1
1347
1348 def isControlReg(self):
1349 return 1
1350
1351 def makeConstructor(self):
1352 c = ''
1353 if self.is_src:
1299 c += '\n\t_srcRegIdx[%d] = %s;' % \
1354 c += '\n\t_srcRegIdx[%d] = %s + Ctrl_Base_DepTag;' % \
1300 (self.src_reg_idx, self.reg_spec)
1301 if self.is_dest:
1355 (self.src_reg_idx, self.reg_spec)
1356 if self.is_dest:
1302 c += '\n\t_destRegIdx[%d] = %s;' % \
1357 c += '\n\t_destRegIdx[%d] = %s + Ctrl_Base_DepTag;' % \
1303 (self.dest_reg_idx, self.reg_spec)
1304 return c
1305
1306 def makeRead(self):
1307 bit_select = 0
1308 if (self.ctype == 'float' or self.ctype == 'double'):
1309 error(0, 'Attempt to read control register as FP')
1358 (self.dest_reg_idx, self.reg_spec)
1359 return c
1360
1361 def makeRead(self):
1362 bit_select = 0
1363 if (self.ctype == 'float' or self.ctype == 'double'):
1364 error(0, 'Attempt to read control register as FP')
1310 base = 'xc->readMiscRegWithEffect(%s)' % self.reg_spec
1365 base = 'xc->readMiscRegOperandWithEffect(%s)' % self.reg_spec
1311 if self.size == self.dflt_size:
1312 return '%s = %s;\n' % (self.base_name, base)
1313 else:
1314 return '%s = bits(%s, %d, 0);\n' % \
1315 (self.base_name, base, self.size-1)
1316
1317 def makeWrite(self):
1318 if (self.ctype == 'float' or self.ctype == 'double'):
1319 error(0, 'Attempt to write control register as FP')
1366 if self.size == self.dflt_size:
1367 return '%s = %s;\n' % (self.base_name, base)
1368 else:
1369 return '%s = bits(%s, %d, 0);\n' % \
1370 (self.base_name, base, self.size-1)
1371
1372 def makeWrite(self):
1373 if (self.ctype == 'float' or self.ctype == 'double'):
1374 error(0, 'Attempt to write control register as FP')
1320 wb = 'xc->setMiscRegWithEffect(%s, %s);\n' % (self.reg_spec, self.base_name)
1375 wb = 'xc->setMiscRegOperandWithEffect(this, %s, %s);\n' % \
1376 (self.dest_reg_idx, self.base_name)
1321 wb += 'if (traceData) { traceData->setData(%s); }' % \
1322 self.base_name
1323 return wb
1324
1325class MemOperand(Operand):
1326 def isMem(self):
1327 return 1
1328

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

1545 # like concatAttrLists, but only include the values for the operands
1546 # for which the provided filter function returns true
1547 def concatSomeAttrLists(self, filter, attr_name):
1548 return self.__internalConcatAttrs(attr_name, filter, [])
1549
1550 def sort(self):
1551 self.items.sort(lambda a, b: a.sort_pri - b.sort_pri)
1552
1377 wb += 'if (traceData) { traceData->setData(%s); }' % \
1378 self.base_name
1379 return wb
1380
1381class MemOperand(Operand):
1382 def isMem(self):
1383 return 1
1384

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

1601 # like concatAttrLists, but only include the values for the operands
1602 # for which the provided filter function returns true
1603 def concatSomeAttrLists(self, filter, attr_name):
1604 return self.__internalConcatAttrs(attr_name, filter, [])
1605
1606 def sort(self):
1607 self.items.sort(lambda a, b: a.sort_pri - b.sort_pri)
1608
1609class SubOperandList(OperandList):
1610
1611 # Find all the operands in the given code block. Returns an operand
1612 # descriptor list (instance of class OperandList).
1613 def __init__(self, code, master_list):
1614 self.items = []
1615 self.bases = {}
1616 # delete comments so we don't match on reg specifiers inside
1617 code = commentRE.sub('', code)
1618 # search for operands
1619 next_pos = 0
1620 while 1:
1621 match = operandsRE.search(code, next_pos)
1622 if not match:
1623 # no more matches: we're done
1624 break
1625 op = match.groups()
1626 # regexp groups are operand full name, base, and extension
1627 (op_full, op_base, op_ext) = op
1628 # find this op in the master list
1629 op_desc = master_list.find_base(op_base)
1630 if not op_desc:
1631 error(0, 'Found operand %s which is not in the master list!' \
1632 ' This is an internal error' % \
1633 op_base)
1634 else:
1635 # See if we've already found this operand
1636 op_desc = self.find_base(op_base)
1637 if not op_desc:
1638 # if not, add a reference to it to this sub list
1639 self.append(master_list.bases[op_base])
1640
1641 # start next search after end of current match
1642 next_pos = match.end()
1643 self.sort()
1644 self.memOperand = None
1645 for op_desc in self.items:
1646 if op_desc.isMem():
1647 if self.memOperand:
1648 error(0, "Code block has more than one memory operand.")
1649 self.memOperand = op_desc
1650
1553# Regular expression object to match C++ comments
1554# (used in findOperands())
1555commentRE = re.compile(r'//.*\n')
1556
1557# Regular expression object to match assignment statements
1558# (used in findOperands())
1559assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE)
1560

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

1578 del flag_list[i]
1579 else:
1580 i += 1
1581 pre = '\n\tflags['
1582 post = '] = true;'
1583 code = pre + string.join(flag_list, post + pre) + post
1584 return code
1585
1651# Regular expression object to match C++ comments
1652# (used in findOperands())
1653commentRE = re.compile(r'//.*\n')
1654
1655# Regular expression object to match assignment statements
1656# (used in findOperands())
1657assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE)
1658

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

1676 del flag_list[i]
1677 else:
1678 i += 1
1679 pre = '\n\tflags['
1680 post = '] = true;'
1681 code = pre + string.join(flag_list, post + pre) + post
1682 return code
1683
1586class CodeBlock:
1587 def __init__(self, code):
1588 self.orig_code = code
1589 self.operands = OperandList(code)
1590 self.code = substMungedOpNames(substBitOps(code))
1684# Assume all instruction flags are of the form 'IsFoo'
1685instFlagRE = re.compile(r'Is.*')
1686
1687# OpClass constants end in 'Op' except No_OpClass
1688opClassRE = re.compile(r'.*Op|No_OpClass')
1689
1690class InstObjParams:
1691 def __init__(self, mnem, class_name, base_class = '',
1692 snippets = None, opt_args = []):
1693 self.mnemonic = mnem
1694 self.class_name = class_name
1695 self.base_class = base_class
1696 compositeCode = ''
1697 if snippets:
1698 if not isinstance(snippets, dict):
1699 snippets = {'code' : snippets}
1700 for snippet in snippets.values():
1701 if isinstance(snippet, str):
1702 compositeCode += (" " + snippet)
1703 self.snippets = snippets
1704
1705 self.operands = OperandList(compositeCode)
1591 self.constructor = self.operands.concatAttrStrings('constructor')
1592 self.constructor += \
1593 '\n\t_numSrcRegs = %d;' % self.operands.numSrcRegs
1594 self.constructor += \
1595 '\n\t_numDestRegs = %d;' % self.operands.numDestRegs
1596 self.constructor += \
1597 '\n\t_numFPDestRegs = %d;' % self.operands.numFPDestRegs
1598 self.constructor += \
1599 '\n\t_numIntDestRegs = %d;' % self.operands.numIntDestRegs
1706 self.constructor = self.operands.concatAttrStrings('constructor')
1707 self.constructor += \
1708 '\n\t_numSrcRegs = %d;' % self.operands.numSrcRegs
1709 self.constructor += \
1710 '\n\t_numDestRegs = %d;' % self.operands.numDestRegs
1711 self.constructor += \
1712 '\n\t_numFPDestRegs = %d;' % self.operands.numFPDestRegs
1713 self.constructor += \
1714 '\n\t_numIntDestRegs = %d;' % self.operands.numIntDestRegs
1600
1601 self.op_decl = self.operands.concatAttrStrings('op_decl')
1602
1603 is_src = lambda op: op.is_src
1604 is_dest = lambda op: op.is_dest
1605
1606 self.op_src_decl = \
1607 self.operands.concatSomeAttrStrings(is_src, 'op_src_decl')
1608 self.op_dest_decl = \
1609 self.operands.concatSomeAttrStrings(is_dest, 'op_dest_decl')
1610
1611 self.op_rd = self.operands.concatAttrStrings('op_rd')
1612 self.op_wb = self.operands.concatAttrStrings('op_wb')
1613
1614 self.flags = self.operands.concatAttrLists('flags')
1615
1715 self.flags = self.operands.concatAttrLists('flags')
1716
1616 if self.operands.memOperand:
1617 self.mem_acc_size = self.operands.memOperand.mem_acc_size
1618 self.mem_acc_type = self.operands.memOperand.mem_acc_type
1619
1620 # Make a basic guess on the operand class (function unit type).
1717 # Make a basic guess on the operand class (function unit type).
1621 # These are good enough for most cases, and will be overridden
1718 # These are good enough for most cases, and can be overridden
1622 # later otherwise.
1623 if 'IsStore' in self.flags:
1624 self.op_class = 'MemWriteOp'
1625 elif 'IsLoad' in self.flags or 'IsPrefetch' in self.flags:
1626 self.op_class = 'MemReadOp'
1627 elif 'IsFloating' in self.flags:
1628 self.op_class = 'FloatAddOp'
1629 else:
1630 self.op_class = 'IntAluOp'
1631
1719 # later otherwise.
1720 if 'IsStore' in self.flags:
1721 self.op_class = 'MemWriteOp'
1722 elif 'IsLoad' in self.flags or 'IsPrefetch' in self.flags:
1723 self.op_class = 'MemReadOp'
1724 elif 'IsFloating' in self.flags:
1725 self.op_class = 'FloatAddOp'
1726 else:
1727 self.op_class = 'IntAluOp'
1728
1632# Assume all instruction flags are of the form 'IsFoo'
1633instFlagRE = re.compile(r'Is.*')
1634
1635# OpClass constants end in 'Op' except No_OpClass
1636opClassRE = re.compile(r'.*Op|No_OpClass')
1637
1638class InstObjParams:
1639 def __init__(self, mnem, class_name, base_class = '',
1640 code = None, opt_args = [], extras = {}):
1641 self.mnemonic = mnem
1642 self.class_name = class_name
1643 self.base_class = base_class
1644 if code:
1645 #If the user already made a CodeBlock, pick the parts from it
1646 if isinstance(code, CodeBlock):
1647 origCode = code.orig_code
1648 codeBlock = code
1649 else:
1650 origCode = code
1651 codeBlock = CodeBlock(code)
1652 stringExtras = {}
1653 otherExtras = {}
1654 for (k, v) in extras.items():
1655 if type(v) == str:
1656 stringExtras[k] = v
1657 else:
1658 otherExtras[k] = v
1659 compositeCode = "\n".join([origCode] + stringExtras.values())
1660 # compositeCode = '\n'.join([origCode] +
1661 # [pair[1] for pair in extras])
1662 compositeBlock = CodeBlock(compositeCode)
1663 for code_attr in compositeBlock.__dict__.keys():
1664 setattr(self, code_attr, getattr(compositeBlock, code_attr))
1665 for (key, snippet) in stringExtras.items():
1666 setattr(self, key, CodeBlock(snippet).code)
1667 for (key, item) in otherExtras.items():
1668 setattr(self, key, item)
1669 self.code = codeBlock.code
1670 self.orig_code = origCode
1671 else:
1672 self.constructor = ''
1673 self.flags = []
1674 # Optional arguments are assumed to be either StaticInst flags
1675 # or an OpClass value. To avoid having to import a complete
1676 # list of these values to match against, we do it ad-hoc
1677 # with regexps.
1678 for oa in opt_args:
1679 if instFlagRE.match(oa):
1680 self.flags.append(oa)
1681 elif opClassRE.match(oa):

--- 148 unchanged lines hidden ---
1729 # Optional arguments are assumed to be either StaticInst flags
1730 # or an OpClass value. To avoid having to import a complete
1731 # list of these values to match against, we do it ad-hoc
1732 # with regexps.
1733 for oa in opt_args:
1734 if instFlagRE.match(oa):
1735 self.flags.append(oa)
1736 elif opClassRE.match(oa):

--- 148 unchanged lines hidden ---