microasm.isa (4309:47807357f0d7) microasm.isa (4323:13ca4002d2ac)
1// -*- mode:c++ -*-
2
3// Copyright (c) 2007 The Hewlett-Packard Development Company
4// All rights reserved.
5//
6// Redistribution and use of this software in source and binary forms,
7// with or without modification, are permitted provided that the
8// following conditions are met:

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

62
63let {{
64 class MicroOpStatement:
65 def __init__(self):
66 self.className = ''
67 self.label = ''
68 self.args = []
69
1// -*- mode:c++ -*-
2
3// Copyright (c) 2007 The Hewlett-Packard Development Company
4// All rights reserved.
5//
6// Redistribution and use of this software in source and binary forms,
7// with or without modification, are permitted provided that the
8// following conditions are met:

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

62
63let {{
64 class MicroOpStatement:
65 def __init__(self):
66 self.className = ''
67 self.label = ''
68 self.args = []
69
70 def getAllocator(self, labelDict = {}):
70 # This converts a list of python bools into
71 # a comma seperated list of C++ bools.
72 def microFlagsText(self, vals):
73 text = ""
74 for val in vals:
75 if val:
76 text += ", true"
77 else:
78 text += ", false"
79 return text
80
81 def getAllocator(self, *microFlags):
71 args = ''
72 for arg in self.args:
73 if arg.has_key("operandConst"):
74 args += ", %s" % arg["operandConst"]
75 elif arg.has_key("operandCode"):
76 args += ", %s" % arg["operandCode"]
77 elif arg.has_key("operandLabel"):
82 args = ''
83 for arg in self.args:
84 if arg.has_key("operandConst"):
85 args += ", %s" % arg["operandConst"]
86 elif arg.has_key("operandCode"):
87 args += ", %s" % arg["operandCode"]
88 elif arg.has_key("operandLabel"):
78 if not labelDict.has_key(arg["operandLabel"]):
79 print "Unrecognized label %s!" % arg["operandLabel"]
80 args += ", %s" % labelDict[arg["operandLabel"]]
89 raise Exception, "Found a label while creating allocator string."
81 else:
90 else:
82 print "Unrecognized operand type!"
83 return 'new %s(machInst %s)' % (self.className, args)
91 raise Exception, "Unrecognized operand type."
92 return 'new %s(machInst%s%s)' % (self.className, self.microFlagsText(microFlags), args)
93}};
84
94
95let {{
96 def buildLabelDict(ops):
97 labels = {}
98 micropc = 0
99 for op in ops:
100 if op.label:
101 labels[op.label] = count
102 micropc += 1
103 return labels
85
86 def assembleMicro(code):
87 # This function takes in a block of microcode assembly and returns
88 # a python list of objects which describe it.
89
90 # Keep this around in case we need it later
91 orig_code = code
92 # A list of the statements we've found thus far

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

108 # capital or small letters, underscores, or digts between 0 and 9
109 opRe = re.compile( \
110 r'^[ \t]*((?P<operandLabel>[a-zA-Z_]\w*)|(?P<operandConst>[0-9][0-9]*)|(\{(?P<operandCode>[^}]*)\}))')
111 lineMatch = lineRe.search(code)
112 while lineMatch != None:
113 statement = MicroOpStatement()
114 # Get a line and seperate it from the rest of the code
115 line = lineMatch.group("line")
104
105 def assembleMicro(code):
106 # This function takes in a block of microcode assembly and returns
107 # a python list of objects which describe it.
108
109 # Keep this around in case we need it later
110 orig_code = code
111 # A list of the statements we've found thus far

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

127 # capital or small letters, underscores, or digts between 0 and 9
128 opRe = re.compile( \
129 r'^[ \t]*((?P<operandLabel>[a-zA-Z_]\w*)|(?P<operandConst>[0-9][0-9]*)|(\{(?P<operandCode>[^}]*)\}))')
130 lineMatch = lineRe.search(code)
131 while lineMatch != None:
132 statement = MicroOpStatement()
133 # Get a line and seperate it from the rest of the code
134 line = lineMatch.group("line")
116 print "Parsing line %s" % line
135 orig_line = line
136 # print "Parsing line %s" % line
117 code = lineRe.sub('', code, 1)
118
119 # Find the label, if any
120 labelMatch = labelRe.search(line)
121 if labelMatch != None:
122 statement.label = labelMatch.group("label")
137 code = lineRe.sub('', code, 1)
138
139 # Find the label, if any
140 labelMatch = labelRe.search(line)
141 if labelMatch != None:
142 statement.label = labelMatch.group("label")
123 print "Found label %s." % statement.label
143 # print "Found label %s." % statement.label
124 # Clear the label from the statement
125 line = labelRe.sub('', line, 1)
126
127 # Find the class name which is roughly equivalent to the op name
128 classMatch = classRe.search(line)
129 if classMatch == None:
144 # Clear the label from the statement
145 line = labelRe.sub('', line, 1)
146
147 # Find the class name which is roughly equivalent to the op name
148 classMatch = classRe.search(line)
149 if classMatch == None:
130 print "Oh no! I can't find what instruction you want!"
131 print "I should really bail out here, but I don't know how!"
150 raise Exception, "Couldn't find class name in statement: %s" \
151 % orig_line
132 else:
133 statement.className = classMatch.group("className")
152 else:
153 statement.className = classMatch.group("className")
134 print "Found class name %s." % statement.className
154 # print "Found class name %s." % statement.className
135
136 # Clear the class name from the statement
137 line = classRe.sub('', line, 1)
138
139 #Find as many arguments as you can
140 statement.args = []
141 opMatch = opRe.search(line)
142 while opMatch is not None:
143 statement.args.append({})
144 # args is a list of dicts which collect different
145 # representations of operand values. Different forms might be
146 # needed in different places, for instance to replace a label
147 # with an offset.
148 for opType in ("operandLabel", "operandConst", "operandCode"):
149 if opMatch.group(opType):
150 statement.args[-1][opType] = opMatch.group(opType)
151 if len(statement.args[-1]) == 0:
155
156 # Clear the class name from the statement
157 line = classRe.sub('', line, 1)
158
159 #Find as many arguments as you can
160 statement.args = []
161 opMatch = opRe.search(line)
162 while opMatch is not None:
163 statement.args.append({})
164 # args is a list of dicts which collect different
165 # representations of operand values. Different forms might be
166 # needed in different places, for instance to replace a label
167 # with an offset.
168 for opType in ("operandLabel", "operandConst", "operandCode"):
169 if opMatch.group(opType):
170 statement.args[-1][opType] = opMatch.group(opType)
171 if len(statement.args[-1]) == 0:
152 print "I had a problem parsing an operand!"
172 print "Problem parsing operand in statement: %s" \
173 % orig_line
153 line = opRe.sub('', line, 1)
174 line = opRe.sub('', line, 1)
154 print "Found operand %s." % statement.args[-1]
175 # print "Found operand %s." % statement.args[-1]
155 opMatch = opRe.search(line)
176 opMatch = opRe.search(line)
156 print "Found operands", statement.args
177 # print "Found operands", statement.args
157
158 # Add this statement to our collection
159 statements.append(statement)
160
161 # Get the next line
162 lineMatch = lineRe.search(code)
178
179 # Add this statement to our collection
180 statements.append(statement)
181
182 # Get the next line
183 lineMatch = lineRe.search(code)
163 return statements
164
184
165 def buildLabelDict(ops):
166 labels = {}
167 count = 0
168 for op in ops:
169 if op.label:
170 labels[op.label] = count
171 count += 1
185 # Decode the labels into displacements
186 labels = buildLabelDict(statements)
187 micropc = 0
188 for statement in statements:
189 for arg in statement.args:
190 if arg.has_key("operandLabel"):
191 if not labels.has_key(arg["operandLabel"]):
192 raise Exception, "Unrecognized label: %s." % arg["operandLabel"]
193 # This is assuming that intra microcode branches go to
194 # the next micropc + displacement, or
195 # micropc + 1 + displacement.
196 arg["operandConst"] = labels[arg["operandLabel"]] - micropc - 1
197 micropc += 1
198 return statements
172}};
199}};