| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright (c) 2012 The Native Client Authors. All rights reserved. | 3 # Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 # | 6 # |
| 7 | 7 |
| 8 """ | 8 """ |
| 9 The core object model for the Decoder Generator. The dg_input and | 9 The core object model for the Decoder Generator. The dg_input and |
| 10 dg_output modules both operate in terms of these classes. | 10 dg_output modules both operate in terms of these classes. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 count = count + ((int >> bit) & 1) | 22 count = count + ((int >> bit) & 1) |
| 23 return count | 23 return count |
| 24 | 24 |
| 25 def neutral_repr(value): | 25 def neutral_repr(value): |
| 26 """Returns a neutral representation for the value. | 26 """Returns a neutral representation for the value. |
| 27 | 27 |
| 28 Used to remove identifier references from values, so that we can | 28 Used to remove identifier references from values, so that we can |
| 29 merge rows/actions of the table. | 29 merge rows/actions of the table. |
| 30 """ | 30 """ |
| 31 if (isinstance(value, BitExpr) or isinstance(value, SymbolTable) or | 31 if (isinstance(value, BitExpr) or isinstance(value, SymbolTable) or |
| 32 isinstance(value, Row) or isinstance(value, DecoderAction)): | 32 isinstance(value, Row) or isinstance(value, DecoderAction) or |
| 33 isinstance(value, RuleRestrictions)): |
| 33 return value.neutral_repr() | 34 return value.neutral_repr() |
| 34 elif isinstance(value, list): | 35 elif isinstance(value, list): |
| 35 return [ neutral_repr(v) for v in value ] | 36 return [ neutral_repr(v) for v in value ] |
| 36 else: | 37 else: |
| 37 return repr(value) | 38 return repr(value) |
| 38 | 39 |
| 39 class BitExpr(object): | 40 class BitExpr(object): |
| 40 """Define a bit expression.""" | 41 """Define a bit expression.""" |
| 41 | 42 |
| 42 def negate(self): | 43 def negate(self): |
| (...skipping 1184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1227 | 1228 |
| 1228 class RuleRestrictions(object): | 1229 class RuleRestrictions(object): |
| 1229 """A rule restriction defines zero or more (anded) bit patterns, and | 1230 """A rule restriction defines zero or more (anded) bit patterns, and |
| 1230 an optional other (i.e. base class) restriction to be used when testing. | 1231 an optional other (i.e. base class) restriction to be used when testing. |
| 1231 """ | 1232 """ |
| 1232 | 1233 |
| 1233 def __init__(self, restrictions=[], other=None): | 1234 def __init__(self, restrictions=[], other=None): |
| 1234 self.restrictions = restrictions[:] | 1235 self.restrictions = restrictions[:] |
| 1235 self.other = other | 1236 self.other = other |
| 1236 | 1237 |
| 1237 def __hash__(self): | |
| 1238 return sum([hash(r) for r in self.restrictions]) + hash(self.other) | |
| 1239 | |
| 1240 def IsEmpty(self): | 1238 def IsEmpty(self): |
| 1241 return not self.restrictions and not self.other | 1239 return not self.restrictions and not self.other |
| 1242 | 1240 |
| 1243 def add(self, restriction): | 1241 def add(self, restriction): |
| 1244 self.restrictions = self.restrictions + [restriction] | 1242 self.restrictions = self.restrictions + [restriction] |
| 1245 | 1243 |
| 1246 def __repr__(self): | 1244 def __repr__(self): |
| 1247 """ Returns the printable string for the restrictions. """ | 1245 """ Returns the printable string for the restrictions. """ |
| 1248 rep = '' | 1246 rep = '' |
| 1249 if self.restrictions: | 1247 if self.restrictions: |
| 1250 for r in self.restrictions: | 1248 for r in self.restrictions: |
| 1251 rep += '& ' + r | 1249 rep += '& %s' % r |
| 1252 rep += ' ' | 1250 rep += ' ' |
| 1253 if self.other: | 1251 if self.other: |
| 1254 rep += ('& other: %s' % self.other) | 1252 rep += ('& other: %s' % self.other) |
| 1255 return rep | 1253 return rep |
| 1256 | 1254 |
| 1255 def neutral_repr(self): |
| 1256 """Returns a normalized neutral representation of the rule restrictions.""" |
| 1257 rep = '' |
| 1258 if self.restrictions: |
| 1259 for r in self.restrictions: |
| 1260 rep += '& %s' % neutral_repr(r) |
| 1261 rep += ' ' |
| 1262 if self.other: |
| 1263 rep += ('& other: %s' % self.other) |
| 1264 return rep |
| 1265 |
| 1266 def __hash__(self): |
| 1267 return hash(self.neutral_repr()) |
| 1268 |
| 1257 def __cmp__(self, v): | 1269 def __cmp__(self, v): |
| 1258 value = cmp(self.other, v.other) | 1270 return cmp(self.neutral_repr(), neutral_repr(v)) |
| 1259 if value != 0: return value | |
| 1260 return cmp(self.restrictions, v.restrictions) | |
| 1261 | 1271 |
| 1262 TABLE_FORMAT=""" | 1272 TABLE_FORMAT=""" |
| 1263 Table %s | 1273 Table %s |
| 1264 %s | 1274 %s |
| 1265 %s | 1275 %s |
| 1266 """ | 1276 """ |
| 1267 class Table(object): | 1277 class Table(object): |
| 1268 """A table in the instruction set definition. Each table contains 1+ | 1278 """A table in the instruction set definition. Each table contains 1+ |
| 1269 columns, and 1+ rows. Each row contains a bit pattern for each column, plus | 1279 columns, and 1+ rows. Each row contains a bit pattern for each column, plus |
| 1270 the action to be taken if the row matches.""" | 1280 the action to be taken if the row matches.""" |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1679 the rule field. | 1689 the rule field. |
| 1680 """ | 1690 """ |
| 1681 return sorted(filter(lambda (r): r.rule, self.decoders())) | 1691 return sorted(filter(lambda (r): r.rule, self.decoders())) |
| 1682 | 1692 |
| 1683 def show_table(self, table): | 1693 def show_table(self, table): |
| 1684 tbl = self.get_table(table) | 1694 tbl = self.get_table(table) |
| 1685 if tbl != None: | 1695 if tbl != None: |
| 1686 print "%s" % tbl | 1696 print "%s" % tbl |
| 1687 else: | 1697 else: |
| 1688 raise Exception("Can't find table %s" % table) | 1698 raise Exception("Can't find table %s" % table) |
| OLD | NEW |