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 --- 1003 unchanged lines hidden (view full) --- 1012 myDict = None 1013 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 # Build a dict ('myDict') to use for the template substitution. 1021 # Start with the template namespace. Make a copy since we're 1022 # going to modify it. 1023 myDict = templateMap.copy() 1024 |
1025 if isinstance(d, InstObjParams): |
1026 # If we're dealing with an InstObjParams object, we need 1027 # to be a little more sophisticated. The instruction-wide 1028 # parameters are already formed, but the parameters which 1029 # are only function wide still need to be generated. |
1030 compositeCode = '' 1031 |
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 snippetLabels = [l for l in labelRE.findall(template) 1039 if d.snippets.has_key(l)] |
1040 |
1041 snippets = dict([(s, mungeSnippet(d.snippets[s])) 1042 for s in snippetLabels]) |
1043 |
1044 myDict.update(snippets) 1045 1046 compositeCode = ' '.join(map(str, snippets.values())) 1047 1048 # Add in template itself in case it references any 1049 # operands explicitly (like Mem) 1050 compositeCode += ' ' + template 1051 |
1052 operands = SubOperandList(compositeCode, d.operands) 1053 1054 myDict['op_decl'] = operands.concatAttrStrings('op_decl') 1055 1056 is_src = lambda op: op.is_src 1057 is_dest = lambda op: op.is_dest 1058 1059 myDict['op_src_decl'] = \ 1060 operands.concatSomeAttrStrings(is_src, 'op_src_decl') 1061 myDict['op_dest_decl'] = \ 1062 operands.concatSomeAttrStrings(is_dest, 'op_dest_decl') 1063 1064 myDict['op_rd'] = operands.concatAttrStrings('op_rd') 1065 myDict['op_wb'] = operands.concatAttrStrings('op_wb') 1066 1067 if d.operands.memOperand: 1068 myDict['mem_acc_size'] = d.operands.memOperand.mem_acc_size 1069 myDict['mem_acc_type'] = d.operands.memOperand.mem_acc_type 1070 |
1071 elif isinstance(d, dict): |
1072 # if the argument is a dictionary, we just use it. |
1073 myDict.update(d) 1074 elif hasattr(d, '__dict__'): |
1075 # if the argument is an object, we use its attribute map. |
1076 myDict.update(d.__dict__) 1077 else: 1078 raise TypeError, "Template.subst() arg must be or have dictionary" |
1079 return template % myDict 1080 1081 # Convert to string. This handles the case when a template with a 1082 # CPU-specific term gets interpolated into another template or into 1083 # an output block. 1084 def __str__(self): 1085 return expand_cpu_symbols_to_string(self.template) 1086 --- 567 unchanged lines hidden (view full) --- 1654assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE) 1655 1656# Munge operand names in code string to make legal C++ variable names. 1657# This means getting rid of the type extension if any. 1658# (Will match base_name attribute of Operand object.) 1659def substMungedOpNames(code): 1660 return operandsWithExtRE.sub(r'\1', code) 1661 |
1662# Fix up code snippets for final substitution in templates. 1663def mungeSnippet(s): 1664 if isinstance(s, str): 1665 return substMungedOpNames(substBitOps(s)) 1666 else: 1667 return s |
1668 1669def makeFlagConstructor(flag_list): 1670 if len(flag_list) == 0: 1671 return '' 1672 # filter out repeated flags 1673 flag_list.sort() 1674 i = 1 1675 while i < len(flag_list): --- 9 unchanged lines hidden (view full) --- 1685# Assume all instruction flags are of the form 'IsFoo' 1686instFlagRE = re.compile(r'Is.*') 1687 1688# OpClass constants end in 'Op' except No_OpClass 1689opClassRE = re.compile(r'.*Op|No_OpClass') 1690 1691class InstObjParams: 1692 def __init__(self, mnem, class_name, base_class = '', |
1693 snippets = {}, opt_args = []): |
1694 self.mnemonic = mnem 1695 self.class_name = class_name 1696 self.base_class = base_class |
1697 if not isinstance(snippets, dict): 1698 snippets = {'code' : snippets} 1699 compositeCode = ' '.join(map(str, snippets.values())) |
1700 self.snippets = snippets 1701 1702 self.operands = OperandList(compositeCode) 1703 self.constructor = self.operands.concatAttrStrings('constructor') 1704 self.constructor += \ 1705 '\n\t_numSrcRegs = %d;' % self.operands.numSrcRegs 1706 self.constructor += \ 1707 '\n\t_numDestRegs = %d;' % self.operands.numDestRegs --- 174 unchanged lines hidden --- |