paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | RSVP instruction classes. |
paul@17 | 5 | |
paul@54 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@17 | 7 | |
paul@17 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@17 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@17 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@17 | 11 | version. |
paul@17 | 12 | |
paul@17 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@17 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@17 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@17 | 16 | details. |
paul@17 | 17 | |
paul@17 | 18 | You should have received a copy of the GNU General Public License along with |
paul@17 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@17 | 20 | """ |
paul@17 | 21 | |
paul@165 | 22 | from micropython.common import Block |
paul@162 | 23 | from micropython.data import Attr, Class, Const, Function, Module |
paul@54 | 24 | |
paul@137 | 25 | def raw(code, objtable, paramtable): |
paul@146 | 26 | |
paul@146 | 27 | """ |
paul@146 | 28 | Return the raw image representation of the given 'code', using the given |
paul@146 | 29 | 'objtable' and 'paramtable' to populate structures. |
paul@146 | 30 | """ |
paul@146 | 31 | |
paul@90 | 32 | new_code = [] |
paul@165 | 33 | |
paul@90 | 34 | for item in code: |
paul@145 | 35 | |
paul@90 | 36 | if isinstance(item, Attr): |
paul@137 | 37 | new_code.append(( |
paul@137 | 38 | item.context and item.context.location, |
paul@137 | 39 | item.value and item.value.location # no useful context is provided |
paul@137 | 40 | )) |
paul@145 | 41 | |
paul@165 | 42 | elif isinstance(item, Block): |
paul@165 | 43 | new_code += raw_block(item, len(new_code)) |
paul@165 | 44 | |
paul@145 | 45 | # Using classcode, attrcode, codeaddr, codedetails, instance. |
paul@145 | 46 | |
paul@137 | 47 | elif isinstance(item, Class): |
paul@137 | 48 | # NOTE: Need initialiser details! |
paul@137 | 49 | new_code.append(( |
paul@138 | 50 | objtable.as_list().get_code(item.full_name()), |
paul@138 | 51 | objtable.get_index(item.full_name()), |
paul@138 | 52 | item.get_instantiator().code_location, |
paul@138 | 53 | ( |
paul@138 | 54 | len(item.get_instantiator().positional_names), |
paul@138 | 55 | len(item.get_instantiator().defaults) |
paul@145 | 56 | ), |
paul@145 | 57 | 0 |
paul@145 | 58 | )) |
paul@145 | 59 | |
paul@137 | 60 | elif isinstance(item, Const): |
paul@137 | 61 | # NOTE: Need class details! |
paul@137 | 62 | new_code.append(( |
paul@146 | 63 | objtable.as_list().get_code(item.value_type_name()), |
paul@146 | 64 | objtable.get_index(item.value_type_name()), |
paul@145 | 65 | None, |
paul@145 | 66 | None, |
paul@145 | 67 | 1 |
paul@137 | 68 | )) |
paul@145 | 69 | |
paul@137 | 70 | elif isinstance(item, Function): |
paul@137 | 71 | # NOTE: Need class and parameter details! Should arguably be types.FunctionType. |
paul@137 | 72 | new_code.append(( |
paul@137 | 73 | objtable.as_list().get_code("__builtins__.function"), |
paul@137 | 74 | objtable.get_index("__builtins__.function"), |
paul@137 | 75 | item.code_location, |
paul@138 | 76 | ( |
paul@138 | 77 | len(item.positional_names), |
paul@138 | 78 | len(item.defaults) |
paul@145 | 79 | ), |
paul@145 | 80 | 0 |
paul@145 | 81 | )) |
paul@145 | 82 | |
paul@162 | 83 | elif isinstance(item, Module): |
paul@162 | 84 | new_code.append(( |
paul@162 | 85 | objtable.as_list().get_code(item.full_name()), |
paul@162 | 86 | None, # module name not used as an attribute |
paul@162 | 87 | None, |
paul@162 | 88 | None, |
paul@162 | 89 | 0 |
paul@162 | 90 | )) |
paul@162 | 91 | |
paul@90 | 92 | else: |
paul@90 | 93 | new_code.append(item) |
paul@145 | 94 | |
paul@90 | 95 | return new_code |
paul@90 | 96 | |
paul@165 | 97 | def raw_block(block, location): |
paul@165 | 98 | |
paul@165 | 99 | """ |
paul@165 | 100 | Return the code for the given 'block', appearing at the given location. |
paul@165 | 101 | """ |
paul@165 | 102 | |
paul@165 | 103 | block.location = location |
paul@165 | 104 | for i, item in enumerate(block.code): |
paul@165 | 105 | if hasattr(item, "location"): |
paul@165 | 106 | item.location = location + i |
paul@165 | 107 | return block.code |
paul@165 | 108 | |
paul@91 | 109 | def name(attr): |
paul@91 | 110 | if isinstance(attr, Attr): |
paul@107 | 111 | return attr.name or "<unnamed>" |
paul@91 | 112 | else: |
paul@107 | 113 | return attr or "<unnamed>" |
paul@91 | 114 | |
paul@17 | 115 | class Instruction: |
paul@17 | 116 | |
paul@17 | 117 | "A generic instruction." |
paul@17 | 118 | |
paul@87 | 119 | def __init__(self, attr=None): |
paul@17 | 120 | self.attr = attr |
paul@103 | 121 | self.input = None |
paul@103 | 122 | self.source = None # for storage instructions |
paul@95 | 123 | |
paul@95 | 124 | def copy(self): |
paul@95 | 125 | return self.__class__(self.attr) |
paul@95 | 126 | |
paul@101 | 127 | def __repr__(self): |
paul@101 | 128 | if self.attr is not None: |
paul@103 | 129 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) |
paul@101 | 130 | else: |
paul@103 | 131 | return "%s()%s" % (self.__class__.__name__, self.show_input()) |
paul@103 | 132 | |
paul@103 | 133 | def show_input(self): |
paul@103 | 134 | if self.input is not None: |
paul@103 | 135 | if self.source is not None: |
paul@103 | 136 | return " <- (%r, %r)" % (self.input, self.source) |
paul@103 | 137 | else: |
paul@103 | 138 | return " <- %r" % self.input |
paul@104 | 139 | elif self.source is not None: |
paul@104 | 140 | return " <-- %r" % self.source |
paul@103 | 141 | else: |
paul@103 | 142 | return "" |
paul@95 | 143 | |
paul@137 | 144 | def get_operand(self): |
paul@137 | 145 | return None |
paul@137 | 146 | |
paul@101 | 147 | class FrameRelativeInstruction(Instruction): |
paul@95 | 148 | |
paul@101 | 149 | "An instruction operating on the current frame." |
paul@17 | 150 | |
paul@17 | 151 | def __repr__(self): |
paul@103 | 152 | return "%s(%r)%s" % (self.__class__.__name__, self.get_operand(), self.show_input()) |
paul@90 | 153 | |
paul@90 | 154 | def get_operand(self): |
paul@90 | 155 | return self.attr.position |
paul@55 | 156 | |
paul@101 | 157 | FR = FrameRelativeInstruction |
paul@55 | 158 | |
paul@55 | 159 | class AddressRelativeInstruction(Instruction): |
paul@55 | 160 | |
paul@55 | 161 | "An instruction accessing an object's attribute." |
paul@55 | 162 | |
paul@55 | 163 | def __repr__(self): |
paul@91 | 164 | position = self.get_operand() |
paul@91 | 165 | if position is not None: |
paul@103 | 166 | return "%s(%r)%s # %s" % (self.__class__.__name__, position, self.show_input(), name(self.attr)) |
paul@91 | 167 | else: |
paul@103 | 168 | return "%s(%r)%s" % (self.__class__.__name__, name(self.attr), self.show_input()) |
paul@90 | 169 | |
paul@90 | 170 | def get_operand(self): |
paul@90 | 171 | return self.attr.position |
paul@82 | 172 | |
paul@82 | 173 | AR = AddressRelativeInstruction |
paul@82 | 174 | |
paul@82 | 175 | class AddressInstruction(Instruction): |
paul@82 | 176 | |
paul@82 | 177 | "An instruction loading an address directly." |
paul@82 | 178 | |
paul@82 | 179 | def __repr__(self): |
paul@90 | 180 | location, position, result = self.get_operands() |
paul@90 | 181 | if location is not None: |
paul@103 | 182 | return "%s(%r)%s # %r, %r (%s)" % ( |
paul@103 | 183 | self.__class__.__name__, result, self.show_input(), location, position, name(self.attr)) |
paul@91 | 184 | elif result is not None: |
paul@103 | 185 | return "%s(%r)%s # %s" % ( |
paul@103 | 186 | self.__class__.__name__, result, self.show_input(), name(self.attr)) |
paul@90 | 187 | else: |
paul@103 | 188 | return "%s(...)%s # %s" % ( |
paul@103 | 189 | self.__class__.__name__, self.show_input(), name(self.attr)) |
paul@90 | 190 | |
paul@90 | 191 | def get_operands(self): |
paul@82 | 192 | if isinstance(self.attr, Attr): |
paul@82 | 193 | position = self.attr.position |
paul@55 | 194 | location = self.attr.parent.location |
paul@56 | 195 | |
paul@56 | 196 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 197 | |
paul@56 | 198 | if location is not None and position is not None: |
paul@56 | 199 | result = location + position + 1 |
paul@56 | 200 | else: |
paul@56 | 201 | location = self.attr.parent.name |
paul@56 | 202 | position = self.attr.name |
paul@56 | 203 | result = None |
paul@90 | 204 | return location, position, result |
paul@82 | 205 | else: |
paul@90 | 206 | return None, None, self.attr.location |
paul@90 | 207 | |
paul@90 | 208 | def get_operand(self): |
paul@90 | 209 | return self.get_operands()[-1] |
paul@55 | 210 | |
paul@70 | 211 | Address = AddressInstruction |
paul@70 | 212 | |
paul@70 | 213 | class ImmediateInstruction(Instruction): |
paul@70 | 214 | |
paul@70 | 215 | "An instruction employing a constant." |
paul@70 | 216 | |
paul@70 | 217 | def __repr__(self): |
paul@103 | 218 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) |
paul@70 | 219 | |
paul@90 | 220 | def get_operand(self): |
paul@90 | 221 | return self.attr |
paul@90 | 222 | |
paul@55 | 223 | Immediate = ImmediateInstruction |
paul@55 | 224 | |
paul@86 | 225 | # Access to stored constant data. |
paul@86 | 226 | |
paul@101 | 227 | class LoadConst(Address): "Load the constant, class, function, module from the specified location." |
paul@86 | 228 | |
paul@21 | 229 | # Access within an invocation frame. |
paul@21 | 230 | |
paul@105 | 231 | class LoadName(FR): "Load the current value from the given local attribute/variable." |
paul@105 | 232 | class StoreName(FR): "Store the source value into the given local attribute/variable." |
paul@105 | 233 | class LoadTemp(Immediate): "Load the current value from the given temporary location." |
paul@105 | 234 | class StoreTemp(Immediate): "Store the current value into the given temporary location." |
paul@21 | 235 | |
paul@98 | 236 | # Access to static data. |
paul@98 | 237 | |
paul@105 | 238 | class LoadAddress(Address): "Load the current value from the given fixed attribute address." |
paul@105 | 239 | class StoreAddress(Address): "Store the source value into the given fixed attribute address." |
paul@105 | 240 | class LoadAddressContext(Address): "Load the current value from the given fixed attribute address, making the current value the context." |
paul@109 | 241 | class MakeObject(Immediate): "Make a new object. There isn't a complementary DropObject." |
paul@98 | 242 | |
paul@21 | 243 | # Access to address-relative data. |
paul@21 | 244 | |
paul@105 | 245 | class LoadAttr(AR): "Load into the current value the given attribute of the object referenced by the current value." |
paul@105 | 246 | class StoreAttr(AR): "Store the source value into the given attribute of the object referenced by the current value." |
paul@105 | 247 | class LoadAttrIndex(Immediate): "Load into the current value the attribute of the current value with the given index." |
paul@101 | 248 | class StoreAttrIndex(Immediate): "Store an object in the attribute with the given index." |
paul@21 | 249 | |
paul@138 | 250 | # Access to object details. |
paul@138 | 251 | |
paul@138 | 252 | class LoadCallable(Instruction): "Load the target of an invocation." |
paul@138 | 253 | class StoreCallable(Instruction): "Store the source value into the object referenced by the current value." |
paul@138 | 254 | |
paul@21 | 255 | # Access to invocation frames in preparation. |
paul@21 | 256 | |
paul@117 | 257 | class MakeFrame(Immediate): "Make a new invocation frame." |
paul@101 | 258 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@137 | 259 | class RecoverFrame(Instruction): "Recover the current frame as an invocation frame." |
paul@105 | 260 | class StoreFrame(Immediate): "Store the current value as an argument for the parameter with the given position." |
paul@105 | 261 | class StoreFrameIndex(Immediate): "Store the current value as an argument for the parameter with the given index." |
paul@101 | 262 | class LoadContext(Instruction): "Load the context of an invocation." |
paul@111 | 263 | class CheckFrame(Immediate): "Check the invocation frame and context for the target." |
paul@101 | 264 | class CheckSelf(Instruction): "Check the first argument of an invocation against the target." |
paul@97 | 265 | |
paul@97 | 266 | # Invocation-related instructions, using a special result "register". |
paul@97 | 267 | |
paul@137 | 268 | class JumpWithFrame(Instruction): "Jump, adopting the invocation frame, to the current callable." |
paul@132 | 269 | class ExtendFrame(Immediate): "Extend the current frame for temporary storage use." |
paul@137 | 270 | class AdjustFrame(Immediate): "Adjust the current frame for corrected invocations." |
paul@104 | 271 | class Return(Instruction): "Return from a subprogram." |
paul@105 | 272 | class LoadResult(Instruction): "Load into the current value a returned value." |
paul@105 | 273 | class StoreResult(Instruction): "Store the current value as a value to be returned." |
paul@21 | 274 | |
paul@97 | 275 | # Branch-related instructions. |
paul@21 | 276 | |
paul@101 | 277 | class Jump(Address): "Jump unconditionally." |
paul@101 | 278 | class JumpIfFalse(Address): "Jump if the last evaluation gave a false result." |
paul@101 | 279 | class JumpIfTrue(Address): "Jump if the last evaluation gave a true result." |
paul@97 | 280 | |
paul@97 | 281 | # Exception-related instructions, using a special exception "register". |
paul@97 | 282 | |
paul@101 | 283 | class LoadException(Instruction): "Load the raised exception." |
paul@105 | 284 | class StoreException(Instruction): "Store the current object in the exception register." |
paul@105 | 285 | class RaiseException(Instruction): "Raise an exception, jumping to the active handler." |
paul@102 | 286 | class PushHandler(Address): "Push an exception handler onto the handler stack." |
paul@103 | 287 | class PopHandler(Instruction): "Pop an exception handler from the handler stack." |
paul@105 | 288 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 289 | |
paul@116 | 290 | # Test instructions, operating on the boolean status register. |
paul@76 | 291 | |
paul@116 | 292 | class TestIdentity(Instruction): "Test whether the current value is identical to the source value, setting the boolean status." |
paul@116 | 293 | class TestIdentityAddress(Address): "Test whether the current value is identical to the given address, setting the boolean status." |
paul@116 | 294 | class InvertBoolean(Instruction): "Invert the boolean status." |
paul@76 | 295 | |
paul@140 | 296 | # Instructions which affect the current value. |
paul@140 | 297 | |
paul@140 | 298 | current_value_instructions = ( |
paul@140 | 299 | LoadConst, LoadName, LoadTemp, LoadAddress, LoadAddressContext, |
paul@140 | 300 | LoadAttr, LoadAttrIndex, LoadCallable, LoadContext, LoadResult, |
paul@140 | 301 | LoadException, MakeObject |
paul@140 | 302 | ) |
paul@140 | 303 | |
paul@17 | 304 | # vim: tabstop=4 expandtab shiftwidth=4 |