paul@201 | 1 | #!/usr/bin/env python |
paul@201 | 2 | |
paul@201 | 3 | """ |
paul@201 | 4 | Program code and data representations. |
paul@253 | 5 | |
paul@554 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> |
paul@253 | 7 | |
paul@253 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@253 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@253 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@253 | 11 | version. |
paul@253 | 12 | |
paul@253 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@253 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@253 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@253 | 16 | details. |
paul@253 | 17 | |
paul@253 | 18 | You should have received a copy of the GNU General Public License along with |
paul@253 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@201 | 20 | """ |
paul@201 | 21 | |
paul@582 | 22 | try: |
paul@582 | 23 | set |
paul@582 | 24 | except NameError: |
paul@582 | 25 | from sets import Set as set |
paul@582 | 26 | |
paul@554 | 27 | class Location: |
paul@554 | 28 | |
paul@554 | 29 | """ |
paul@554 | 30 | A special representation for locations which are to be compared to program |
paul@554 | 31 | objects. |
paul@554 | 32 | """ |
paul@554 | 33 | |
paul@554 | 34 | def __init__(self, location): |
paul@554 | 35 | self.location = location |
paul@554 | 36 | |
paul@554 | 37 | def _op(self, other, op): |
paul@554 | 38 | if hasattr(other, "location"): |
paul@554 | 39 | return op(self.location, other.location) |
paul@554 | 40 | else: |
paul@554 | 41 | raise NotImplemented |
paul@554 | 42 | |
paul@554 | 43 | def __eq__(self, other): |
paul@554 | 44 | return self._op(other, operator.eq) |
paul@554 | 45 | |
paul@554 | 46 | def __ne__(self, other): |
paul@554 | 47 | return self._op(other, operator.ne) |
paul@554 | 48 | |
paul@554 | 49 | def __lt__(self, other): |
paul@554 | 50 | return self._op(other, operator.lt) |
paul@554 | 51 | |
paul@554 | 52 | def __le__(self, other): |
paul@554 | 53 | return self._op(other, operator.le) |
paul@554 | 54 | |
paul@554 | 55 | def __gt__(self, other): |
paul@554 | 56 | return self._op(other, operator.gt) |
paul@554 | 57 | |
paul@554 | 58 | def __ge__(self, other): |
paul@554 | 59 | return self._op(other, operator.ge) |
paul@554 | 60 | |
paul@554 | 61 | def __repr__(self): |
paul@554 | 62 | return "Location(%r)" % self.location |
paul@554 | 63 | |
paul@201 | 64 | class Block: |
paul@201 | 65 | |
paul@201 | 66 | "A code block." |
paul@201 | 67 | |
paul@491 | 68 | def __init__(self, unit): |
paul@491 | 69 | self.unit = unit |
paul@201 | 70 | self.code = [] |
paul@201 | 71 | self.location = None |
paul@452 | 72 | self.active_values = set() |
paul@201 | 73 | |
paul@201 | 74 | def __repr__(self): |
paul@491 | 75 | return "Block(%r, id=%r, location=%r)" % (self.unit, id(self), self.location) |
paul@201 | 76 | |
paul@452 | 77 | def set_active_values(self, values): |
paul@452 | 78 | self.active_values = values |
paul@452 | 79 | |
paul@452 | 80 | def get_active_values(self): |
paul@452 | 81 | return self.active_values |
paul@452 | 82 | |
paul@464 | 83 | def insert(self, pos, op): |
paul@464 | 84 | self.code.insert(pos, op) |
paul@464 | 85 | |
paul@464 | 86 | def append(self, op): |
paul@464 | 87 | self.code.append(op) |
paul@464 | 88 | |
paul@469 | 89 | def __len__(self): |
paul@469 | 90 | return len(self.code) |
paul@469 | 91 | |
paul@264 | 92 | class DataValue: |
paul@264 | 93 | |
paul@264 | 94 | "A representation of a raw program value." |
paul@264 | 95 | |
paul@264 | 96 | def __init__(self, context, ref): |
paul@264 | 97 | self.context = context |
paul@264 | 98 | self.ref = ref |
paul@264 | 99 | |
paul@264 | 100 | def __repr__(self): |
paul@265 | 101 | return "value: (%r, %r)" % ( |
paul@264 | 102 | self.context, self.ref |
paul@264 | 103 | ) |
paul@264 | 104 | |
paul@201 | 105 | class DataObject: |
paul@201 | 106 | |
paul@201 | 107 | "A representation of a raw program data object." |
paul@201 | 108 | |
paul@219 | 109 | def __init__(self, classcode, attrcode, codeaddr, name, size, funccode=None): |
paul@201 | 110 | self.classcode = classcode |
paul@201 | 111 | self.attrcode = attrcode |
paul@201 | 112 | self.codeaddr = codeaddr |
paul@201 | 113 | self.name = name |
paul@203 | 114 | self.size = size |
paul@201 | 115 | self.funccode = funccode |
paul@201 | 116 | |
paul@203 | 117 | def with_size(self, size): |
paul@219 | 118 | return DataObject(self.classcode, self.attrcode, self.codeaddr, self.name, size, self.funccode) |
paul@203 | 119 | |
paul@233 | 120 | def with_callable(self, codeaddr): |
paul@233 | 121 | return DataObject(self.classcode, self.attrcode, codeaddr, self.name, self.size, self.funccode) |
paul@233 | 122 | |
paul@201 | 123 | def __repr__(self): |
paul@265 | 124 | return "object: %r # %s" % ( |
paul@219 | 125 | (self.classcode, self.attrcode, self.codeaddr, self.funccode, self.size), self.name |
paul@203 | 126 | ) |
paul@201 | 127 | |
paul@246 | 128 | class FragmentObject: |
paul@246 | 129 | |
paul@246 | 130 | "A representation of a list fragment, used by list instances." |
paul@246 | 131 | |
paul@246 | 132 | def __init__(self, occupied_size, allocated_size): |
paul@246 | 133 | self.occupied_size = occupied_size |
paul@246 | 134 | self.allocated_size = allocated_size |
paul@246 | 135 | |
paul@246 | 136 | def __repr__(self): |
paul@246 | 137 | return "%r" % ((self.occupied_size, self.allocated_size),) |
paul@246 | 138 | |
paul@237 | 139 | class Context: |
paul@237 | 140 | |
paul@237 | 141 | """ |
paul@237 | 142 | A representation of a context used in a program, providing the special |
paul@237 | 143 | context value types. |
paul@237 | 144 | """ |
paul@237 | 145 | |
paul@237 | 146 | def __init__(self, truth_value, repr): |
paul@237 | 147 | self.truth_value = truth_value |
paul@237 | 148 | self.repr = repr |
paul@237 | 149 | |
paul@237 | 150 | def __repr__(self): |
paul@237 | 151 | return self.repr |
paul@237 | 152 | |
paul@237 | 153 | def __nonzero__(self): |
paul@237 | 154 | return self.truth_value |
paul@237 | 155 | |
paul@243 | 156 | __shortrepr__ = __repr__ |
paul@243 | 157 | |
paul@237 | 158 | # A representation of a context that is replaced upon certain assignment |
paul@237 | 159 | # operations. |
paul@237 | 160 | |
paul@237 | 161 | ReplaceableContext = Context(0, "Replace") |
paul@237 | 162 | |
paul@237 | 163 | # A representation of a context that is employed by classes so that |
paul@237 | 164 | # instantiators have enough slots in the invocation frame, yet does not cause |
paul@237 | 165 | # testing of the context or adjustment of the frame. |
paul@237 | 166 | |
paul@237 | 167 | PlaceholderContext = Context(0, "Placeholder") |
paul@237 | 168 | |
paul@201 | 169 | # vim: tabstop=4 expandtab shiftwidth=4 |