paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@265 | 4 | RSVP instruction and serialisation classes. |
paul@17 | 5 | |
paul@308 | 6 | Copyright (C) 2007, 2008, 2009, 2010 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@265 | 22 | from micropython.data import Attr, Const, Class, Function, Module |
paul@265 | 23 | from micropython.program import Block, DataObject, DataValue |
paul@165 | 24 | |
paul@91 | 25 | def name(attr): |
paul@91 | 26 | if isinstance(attr, Attr): |
paul@107 | 27 | return attr.name or "<unnamed>" |
paul@181 | 28 | elif isinstance(attr, (Block, Const)): |
paul@180 | 29 | return attr |
paul@91 | 30 | else: |
paul@180 | 31 | return attr.full_name() or "<unnamed>" |
paul@91 | 32 | |
paul@265 | 33 | # Serialisation-related classes. |
paul@265 | 34 | |
paul@265 | 35 | class RSVPObject: |
paul@265 | 36 | |
paul@265 | 37 | "A generic data object wrapper." |
paul@265 | 38 | |
paul@265 | 39 | def __init__(self, item): |
paul@265 | 40 | self.item = item |
paul@265 | 41 | |
paul@265 | 42 | def set_location(self, location, with_builtins): |
paul@265 | 43 | self.item.location = location |
paul@265 | 44 | return location + 1 |
paul@265 | 45 | |
paul@265 | 46 | def finalise_location(self, with_builtins): |
paul@265 | 47 | pass |
paul@265 | 48 | |
paul@265 | 49 | def _finalise_location(self, with_builtins): |
paul@265 | 50 | |
paul@265 | 51 | """ |
paul@265 | 52 | Set the code body location for items now that the code blocks have been |
paul@265 | 53 | positioned. |
paul@265 | 54 | """ |
paul@265 | 55 | |
paul@265 | 56 | item = self.item |
paul@265 | 57 | |
paul@265 | 58 | if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None: |
paul@265 | 59 | item.code_body_location = item.full_name() |
paul@265 | 60 | else: |
paul@265 | 61 | item.code_body_location = item.get_body_block().location |
paul@265 | 62 | |
paul@265 | 63 | class RSVPAttr(RSVPObject): |
paul@265 | 64 | |
paul@265 | 65 | "A wrapper for attributes/values." |
paul@265 | 66 | |
paul@265 | 67 | def as_raw(self, objtable, paramtable, with_builtins): |
paul@265 | 68 | item = self.item |
paul@265 | 69 | return [ |
paul@265 | 70 | DataValue( |
paul@265 | 71 | item.get_context() and item.get_context().location, |
paul@265 | 72 | item.get_value() and item.get_value().location |
paul@265 | 73 | ) |
paul@265 | 74 | ] |
paul@265 | 75 | |
paul@265 | 76 | class RSVPBlock(RSVPObject): |
paul@265 | 77 | |
paul@265 | 78 | "A wrapper for blocks." |
paul@265 | 79 | |
paul@265 | 80 | def set_location(self, location, with_builtins): |
paul@265 | 81 | item = self.item |
paul@265 | 82 | item.location = location |
paul@265 | 83 | return location + len(item.code) |
paul@265 | 84 | |
paul@265 | 85 | def as_raw(self, objtable, paramtable, with_builtins): |
paul@265 | 86 | return self.item.code |
paul@265 | 87 | |
paul@265 | 88 | class RSVPClass(RSVPObject): |
paul@265 | 89 | |
paul@265 | 90 | "A wrapper for classes." |
paul@265 | 91 | |
paul@265 | 92 | def set_location(self, location, with_builtins): |
paul@265 | 93 | self.item.instance_template_location = location |
paul@265 | 94 | return RSVPObject.set_location(self, location + 1, with_builtins) |
paul@265 | 95 | |
paul@265 | 96 | def finalise_location(self, with_builtins): |
paul@265 | 97 | self._finalise_location(with_builtins) |
paul@265 | 98 | |
paul@265 | 99 | def as_raw(self, objtable, paramtable, with_builtins): |
paul@265 | 100 | item = self.item |
paul@265 | 101 | classcode = objtable.as_list().get_code(item.full_name()) |
paul@265 | 102 | attrcode = objtable.get_index(item.full_name()) |
paul@265 | 103 | |
paul@265 | 104 | # Include a template of an instance for use when instantiating classes. |
paul@265 | 105 | |
paul@265 | 106 | call_method = item.all_class_attributes().get("__call__") |
paul@265 | 107 | call_method_value = call_method and call_method.get_value() |
paul@265 | 108 | call_method_code_location = call_method_value and call_method_value.code_location |
paul@265 | 109 | call_method_funccode = call_method_value and paramtable.as_list().get_code(call_method_value.full_name()) |
paul@265 | 110 | |
paul@265 | 111 | instantiator_funccode = paramtable.as_list().get_code(item.get_instantiator().full_name()) |
paul@265 | 112 | |
paul@265 | 113 | # NOTE: The instantiator code is the first block of the class. |
paul@265 | 114 | |
paul@265 | 115 | with_instantiator = with_builtins or item.module.name != "__builtins__" or item.astnode.doc is not None |
paul@265 | 116 | |
paul@265 | 117 | if not with_instantiator: |
paul@265 | 118 | instantiator_code_location = item.full_name() |
paul@265 | 119 | else: |
paul@265 | 120 | instantiator_code_location = item.get_instantiator().blocks[0].location |
paul@265 | 121 | |
paul@265 | 122 | return [ |
paul@265 | 123 | |
paul@265 | 124 | # Template instance... |
paul@265 | 125 | |
paul@265 | 126 | DataObject( |
paul@265 | 127 | classcode, |
paul@265 | 128 | attrcode, # is instance |
paul@265 | 129 | call_method_code_location, |
paul@265 | 130 | item.full_name(), |
paul@265 | 131 | len(item.instance_attributes()) + 1, # size |
paul@265 | 132 | call_method_funccode # funccode |
paul@265 | 133 | ), |
paul@265 | 134 | |
paul@265 | 135 | # Class... |
paul@265 | 136 | |
paul@265 | 137 | DataObject( |
paul@265 | 138 | classcode, |
paul@265 | 139 | None, # is not instance |
paul@265 | 140 | instantiator_code_location, |
paul@265 | 141 | item.full_name(), |
paul@265 | 142 | len(item.class_attributes()) + 1, # size |
paul@265 | 143 | instantiator_funccode # funccode |
paul@265 | 144 | ) |
paul@265 | 145 | ] |
paul@265 | 146 | |
paul@265 | 147 | class RSVPConst(RSVPObject): |
paul@265 | 148 | |
paul@265 | 149 | "A wrapper for constants." |
paul@265 | 150 | |
paul@265 | 151 | def set_location(self, location, with_builtins): |
paul@265 | 152 | location = RSVPObject.set_location(self, location, with_builtins) |
paul@265 | 153 | return location + len(self.raw_data()) |
paul@265 | 154 | |
paul@265 | 155 | def as_raw(self, objtable, paramtable, with_builtins): |
paul@265 | 156 | item = self.item |
paul@265 | 157 | # NOTE: Need class details! |
paul@265 | 158 | return [ |
paul@265 | 159 | DataObject( |
paul@265 | 160 | objtable.as_list().get_code(item.value_type_name()), |
paul@265 | 161 | objtable.get_index(item.value_type_name()), # is instance |
paul@265 | 162 | None, |
paul@265 | 163 | item.value_type_name(), |
paul@265 | 164 | 1 # size |
paul@265 | 165 | ) |
paul@265 | 166 | ] + self.raw_data() |
paul@265 | 167 | |
paul@265 | 168 | def raw_data(self): |
paul@265 | 169 | item = self.item |
paul@265 | 170 | value = item.get_value() |
paul@265 | 171 | # NOTE: Start simple and use single entries for most types. |
paul@265 | 172 | if item.value_type_name() in ("__builtins__.tuple", "__builtins__.list"): |
paul@265 | 173 | return [len(value)] + list(value) |
paul@265 | 174 | else: |
paul@265 | 175 | return [value] |
paul@265 | 176 | |
paul@265 | 177 | class RSVPFunction(RSVPObject): |
paul@265 | 178 | |
paul@265 | 179 | "A wrapper for functions." |
paul@265 | 180 | |
paul@265 | 181 | def set_location(self, location, with_builtins): |
paul@265 | 182 | item = self.item |
paul@265 | 183 | location = RSVPObject.set_location(self, location, with_builtins) |
paul@265 | 184 | |
paul@265 | 185 | # Set the code location only where the code has been |
paul@265 | 186 | # generated. |
paul@265 | 187 | |
paul@265 | 188 | if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None: |
paul@265 | 189 | item.code_location = item.full_name() |
paul@265 | 190 | |
paul@265 | 191 | # Skip any defaults for named functions. |
paul@265 | 192 | |
paul@265 | 193 | elif item.name is not None: |
paul@265 | 194 | item.code_location = location + len(item.defaults) |
paul@265 | 195 | |
paul@265 | 196 | # Skip any defaults for lambda functions. |
paul@265 | 197 | |
paul@265 | 198 | else: |
paul@265 | 199 | item.code_location = location |
paul@265 | 200 | |
paul@265 | 201 | return location |
paul@265 | 202 | |
paul@265 | 203 | def finalise_location(self, with_builtins): |
paul@265 | 204 | self._finalise_location(with_builtins) |
paul@265 | 205 | |
paul@265 | 206 | def as_raw(self, objtable, paramtable, with_builtins): |
paul@265 | 207 | item = self.item |
paul@265 | 208 | # NOTE: Need class and parameter details! Should arguably be an instance of types.FunctionType. |
paul@265 | 209 | return [ |
paul@265 | 210 | DataObject( |
paul@265 | 211 | objtable.as_list().get_code("__builtins__.function"), |
paul@265 | 212 | objtable.get_index("__builtins__.function"), # is instance |
paul@265 | 213 | item.code_location, |
paul@265 | 214 | "__builtins__.function", |
paul@265 | 215 | len(item.defaults) + 1, # size (not accurate for lambda functions before instantiation) |
paul@265 | 216 | paramtable.as_list().get_code(item.full_name()) # funccode |
paul@265 | 217 | ) |
paul@265 | 218 | ] |
paul@265 | 219 | |
paul@265 | 220 | class RSVPModule(RSVPObject): |
paul@265 | 221 | |
paul@265 | 222 | "A wrapper for modules." |
paul@265 | 223 | |
paul@265 | 224 | def as_raw(self, objtable, paramtable, with_builtins): |
paul@265 | 225 | item = self.item |
paul@265 | 226 | return [ |
paul@265 | 227 | DataObject( |
paul@265 | 228 | objtable.as_list().get_code(item.full_name()), |
paul@265 | 229 | None, # modules treated like classes |
paul@265 | 230 | None, |
paul@265 | 231 | item.full_name(), |
paul@265 | 232 | len(item.module_attributes()) + 1 # size |
paul@265 | 233 | ) |
paul@265 | 234 | ] |
paul@265 | 235 | |
paul@265 | 236 | # Serialisation-related data and functions. |
paul@265 | 237 | |
paul@265 | 238 | def get_object(item): |
paul@265 | 239 | if isinstance(item, Attr): |
paul@265 | 240 | cls = RSVPAttr |
paul@265 | 241 | elif isinstance(item, Block): |
paul@265 | 242 | cls = RSVPBlock |
paul@265 | 243 | elif isinstance(item, Class): |
paul@265 | 244 | cls = RSVPClass |
paul@265 | 245 | elif isinstance(item, Const): |
paul@265 | 246 | cls = RSVPConst |
paul@265 | 247 | elif isinstance(item, Function): |
paul@265 | 248 | cls = RSVPFunction |
paul@265 | 249 | elif isinstance(item, Module): |
paul@265 | 250 | cls = RSVPModule |
paul@265 | 251 | else: |
paul@265 | 252 | cls = None |
paul@265 | 253 | |
paul@265 | 254 | if cls is not None: |
paul@265 | 255 | return cls(item) |
paul@265 | 256 | else: |
paul@265 | 257 | return None |
paul@265 | 258 | |
paul@265 | 259 | # Instruction-related classes. |
paul@265 | 260 | |
paul@17 | 261 | class Instruction: |
paul@17 | 262 | |
paul@17 | 263 | "A generic instruction." |
paul@17 | 264 | |
paul@87 | 265 | def __init__(self, attr=None): |
paul@17 | 266 | self.attr = attr |
paul@103 | 267 | self.input = None |
paul@103 | 268 | self.source = None # for storage instructions |
paul@95 | 269 | |
paul@95 | 270 | def copy(self): |
paul@95 | 271 | return self.__class__(self.attr) |
paul@95 | 272 | |
paul@101 | 273 | def __repr__(self): |
paul@101 | 274 | if self.attr is not None: |
paul@103 | 275 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) |
paul@101 | 276 | else: |
paul@103 | 277 | return "%s()%s" % (self.__class__.__name__, self.show_input()) |
paul@103 | 278 | |
paul@103 | 279 | def show_input(self): |
paul@103 | 280 | if self.input is not None: |
paul@103 | 281 | if self.source is not None: |
paul@103 | 282 | return " <- (%r, %r)" % (self.input, self.source) |
paul@103 | 283 | else: |
paul@103 | 284 | return " <- %r" % self.input |
paul@104 | 285 | elif self.source is not None: |
paul@104 | 286 | return " <-- %r" % self.source |
paul@103 | 287 | else: |
paul@103 | 288 | return "" |
paul@95 | 289 | |
paul@137 | 290 | def get_operand(self): |
paul@137 | 291 | return None |
paul@137 | 292 | |
paul@101 | 293 | class FrameRelativeInstruction(Instruction): |
paul@95 | 294 | |
paul@101 | 295 | "An instruction operating on the current frame." |
paul@17 | 296 | |
paul@17 | 297 | def __repr__(self): |
paul@103 | 298 | return "%s(%r)%s" % (self.__class__.__name__, self.get_operand(), self.show_input()) |
paul@90 | 299 | |
paul@90 | 300 | def get_operand(self): |
paul@90 | 301 | return self.attr.position |
paul@55 | 302 | |
paul@101 | 303 | FR = FrameRelativeInstruction |
paul@55 | 304 | |
paul@55 | 305 | class AddressRelativeInstruction(Instruction): |
paul@55 | 306 | |
paul@55 | 307 | "An instruction accessing an object's attribute." |
paul@55 | 308 | |
paul@55 | 309 | def __repr__(self): |
paul@91 | 310 | position = self.get_operand() |
paul@91 | 311 | if position is not None: |
paul@103 | 312 | return "%s(%r)%s # %s" % (self.__class__.__name__, position, self.show_input(), name(self.attr)) |
paul@91 | 313 | else: |
paul@103 | 314 | return "%s(%r)%s" % (self.__class__.__name__, name(self.attr), self.show_input()) |
paul@90 | 315 | |
paul@90 | 316 | def get_operand(self): |
paul@90 | 317 | return self.attr.position |
paul@82 | 318 | |
paul@82 | 319 | AR = AddressRelativeInstruction |
paul@82 | 320 | |
paul@82 | 321 | class AddressInstruction(Instruction): |
paul@82 | 322 | |
paul@82 | 323 | "An instruction loading an address directly." |
paul@82 | 324 | |
paul@82 | 325 | def __repr__(self): |
paul@90 | 326 | location, position, result = self.get_operands() |
paul@90 | 327 | if location is not None: |
paul@103 | 328 | return "%s(%r)%s # %r, %r (%s)" % ( |
paul@103 | 329 | self.__class__.__name__, result, self.show_input(), location, position, name(self.attr)) |
paul@91 | 330 | elif result is not None: |
paul@103 | 331 | return "%s(%r)%s # %s" % ( |
paul@103 | 332 | self.__class__.__name__, result, self.show_input(), name(self.attr)) |
paul@90 | 333 | else: |
paul@103 | 334 | return "%s(...)%s # %s" % ( |
paul@103 | 335 | self.__class__.__name__, self.show_input(), name(self.attr)) |
paul@90 | 336 | |
paul@90 | 337 | def get_operands(self): |
paul@82 | 338 | if isinstance(self.attr, Attr): |
paul@82 | 339 | position = self.attr.position |
paul@55 | 340 | location = self.attr.parent.location |
paul@56 | 341 | |
paul@56 | 342 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 343 | |
paul@56 | 344 | if location is not None and position is not None: |
paul@56 | 345 | result = location + position + 1 |
paul@56 | 346 | else: |
paul@56 | 347 | location = self.attr.parent.name |
paul@56 | 348 | position = self.attr.name |
paul@56 | 349 | result = None |
paul@90 | 350 | return location, position, result |
paul@82 | 351 | else: |
paul@90 | 352 | return None, None, self.attr.location |
paul@90 | 353 | |
paul@90 | 354 | def get_operand(self): |
paul@90 | 355 | return self.get_operands()[-1] |
paul@55 | 356 | |
paul@70 | 357 | Address = AddressInstruction |
paul@70 | 358 | |
paul@215 | 359 | class TargetInstruction(Instruction): |
paul@215 | 360 | |
paul@215 | 361 | "An instruction loading the address of an invocation target." |
paul@215 | 362 | |
paul@215 | 363 | def __repr__(self): |
paul@215 | 364 | return "%s(%r) # %r" % (self.__class__.__name__, self.get_operand(), name(self.attr)) |
paul@215 | 365 | |
paul@215 | 366 | def get_operand(self): |
paul@240 | 367 | return self.attr.code_body_location |
paul@215 | 368 | |
paul@215 | 369 | Target = TargetInstruction |
paul@215 | 370 | |
paul@70 | 371 | class ImmediateInstruction(Instruction): |
paul@70 | 372 | |
paul@70 | 373 | "An instruction employing a constant." |
paul@70 | 374 | |
paul@70 | 375 | def __repr__(self): |
paul@103 | 376 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) |
paul@70 | 377 | |
paul@90 | 378 | def get_operand(self): |
paul@90 | 379 | return self.attr |
paul@90 | 380 | |
paul@55 | 381 | Immediate = ImmediateInstruction |
paul@55 | 382 | |
paul@86 | 383 | # Access to stored constant data. |
paul@86 | 384 | |
paul@313 | 385 | class LoadConst(Address): |
paul@313 | 386 | "Load the constant or module from the specified location." |
paul@313 | 387 | cost = 1 |
paul@313 | 388 | |
paul@313 | 389 | class LoadClass(Address): |
paul@313 | 390 | "Load the class from the specified location." |
paul@313 | 391 | cost = 1 |
paul@313 | 392 | |
paul@313 | 393 | class LoadFunction(Address): |
paul@313 | 394 | "Load the function from the specified location." |
paul@313 | 395 | cost = 1 |
paul@86 | 396 | |
paul@21 | 397 | # Access within an invocation frame. |
paul@21 | 398 | |
paul@313 | 399 | class LoadName(FR): |
paul@313 | 400 | "Load the current value from the given local attribute/variable." |
paul@313 | 401 | cost = 2 |
paul@313 | 402 | |
paul@313 | 403 | class StoreName(FR): |
paul@313 | 404 | "Store the source value into the given local attribute/variable." |
paul@313 | 405 | cost = 2 |
paul@313 | 406 | |
paul@313 | 407 | class LoadTemp(Immediate): |
paul@313 | 408 | "Load the current value from the given temporary location." |
paul@313 | 409 | cost = 2 |
paul@313 | 410 | |
paul@313 | 411 | class StoreTemp(Immediate): |
paul@313 | 412 | "Store the current value into the given temporary location." |
paul@313 | 413 | cost = 2 |
paul@21 | 414 | |
paul@98 | 415 | # Access to static data. |
paul@98 | 416 | |
paul@313 | 417 | class LoadAddress(Address): |
paul@313 | 418 | "Load the current value from the given fixed attribute address." |
paul@313 | 419 | cost = 1 |
paul@313 | 420 | |
paul@313 | 421 | class StoreAddress(Address): |
paul@313 | 422 | "Store the source value into the given fixed attribute address." |
paul@313 | 423 | cost = 1 |
paul@313 | 424 | |
paul@313 | 425 | class LoadAddressContext(Address): |
paul@313 | 426 | "Load the current value from the given fixed attribute address, using the current value as context." |
paul@313 | 427 | cost = 2 |
paul@313 | 428 | |
paul@313 | 429 | class StoreAddressContext(Address): |
paul@313 | 430 | "Store the current value into the given fixed attribute address, using the current value as context." |
paul@313 | 431 | cost = 2 |
paul@313 | 432 | |
paul@194 | 433 | class LoadAddressContextCond(Address): |
paul@313 | 434 | """ |
paul@313 | 435 | Load the current value from the given fixed attribute address, only using the current value as |
paul@313 | 436 | context if the attribute is compatible. |
paul@313 | 437 | """ |
paul@313 | 438 | cost = 4 |
paul@313 | 439 | |
paul@313 | 440 | class MakeInstance(Immediate): |
paul@313 | 441 | "Make a new instance using the current value as a reference to a template." |
paul@313 | 442 | cost = 5 |
paul@313 | 443 | |
paul@313 | 444 | class MakeFragment(Immediate): |
paul@313 | 445 | "Make a new list fragment." |
paul@313 | 446 | cost = 5 |
paul@98 | 447 | |
paul@234 | 448 | # Access to address-relative data. (LoadAttrIndexContext not defined.) |
paul@21 | 449 | |
paul@313 | 450 | class LoadAttr(AR): |
paul@313 | 451 | "Load into the current value the given attribute of the object referenced by the current value." |
paul@313 | 452 | cost = 2 |
paul@313 | 453 | |
paul@313 | 454 | class StoreAttr(AR): |
paul@313 | 455 | "Store the source value into the given attribute of the object referenced by the current value." |
paul@313 | 456 | cost = 2 |
paul@313 | 457 | |
paul@313 | 458 | class LoadAttrIndex(Immediate): |
paul@313 | 459 | "Load into the current value the attribute of the current value with the given index." |
paul@313 | 460 | cost = 6 |
paul@313 | 461 | |
paul@313 | 462 | class StoreAttrIndex(Immediate): |
paul@313 | 463 | "Store the source value into the attribute of the current value with the given index." |
paul@313 | 464 | cost = 6 |
paul@313 | 465 | |
paul@194 | 466 | class LoadAttrIndexContextCond(Immediate): |
paul@313 | 467 | """ |
paul@313 | 468 | Load into the current value the attribute of the current value with the given index, only making the |
paul@313 | 469 | current value the context if the attribute is compatible. |
paul@313 | 470 | """ |
paul@313 | 471 | cost = 8 |
paul@21 | 472 | |
paul@138 | 473 | # Access to object details. |
paul@138 | 474 | |
paul@313 | 475 | class LoadCallable(Instruction): |
paul@313 | 476 | "Load the target of an invocation." |
paul@313 | 477 | cost = 2 |
paul@313 | 478 | |
paul@313 | 479 | class StoreCallable(Instruction): |
paul@313 | 480 | "Store the source value into the object referenced by the current value." |
paul@313 | 481 | cost = 3 |
paul@138 | 482 | |
paul@21 | 483 | # Access to invocation frames in preparation. |
paul@21 | 484 | |
paul@313 | 485 | class MakeFrame(Immediate): |
paul@313 | 486 | "Make a new invocation frame." |
paul@313 | 487 | cost = 2 |
paul@313 | 488 | |
paul@313 | 489 | class AdjustFrame(Immediate): |
paul@313 | 490 | "Adjust the current invocation frame for corrected invocations." |
paul@313 | 491 | cost = 2 |
paul@313 | 492 | |
paul@313 | 493 | class DropFrame(Instruction): |
paul@313 | 494 | "Drop an invocation frame." |
paul@313 | 495 | cost = 2 |
paul@313 | 496 | |
paul@313 | 497 | class StoreFrame(Immediate): |
paul@313 | 498 | "Store the current value as an argument for the parameter with the given position." |
paul@313 | 499 | cost = 2 |
paul@313 | 500 | |
paul@313 | 501 | class StoreFrameIndex(Immediate): |
paul@313 | 502 | "Store the source value as an argument of the current value for the parameter with the given index." |
paul@313 | 503 | cost = 6 |
paul@313 | 504 | |
paul@313 | 505 | class LoadContext(Instruction): |
paul@313 | 506 | "Load the context of an invocation." |
paul@313 | 507 | cost = 2 |
paul@215 | 508 | |
paul@234 | 509 | # Context-related tests. |
paul@215 | 510 | |
paul@313 | 511 | class CheckContext(Instruction): |
paul@313 | 512 | "Check to see if the context is valid." |
paul@313 | 513 | cost = 2 |
paul@313 | 514 | |
paul@313 | 515 | class CheckClass(Instruction): |
paul@313 | 516 | "Check the current value to determine whether it is a class." |
paul@313 | 517 | cost = 2 |
paul@313 | 518 | |
paul@313 | 519 | class CheckInstance(Instruction): |
paul@313 | 520 | """ |
paul@313 | 521 | Check the current value as an instance of a class or its subclasses (used with 'self' in an |
paul@313 | 522 | invocation). |
paul@313 | 523 | """ |
paul@313 | 524 | cost = 6 |
paul@313 | 525 | |
paul@313 | 526 | class CheckType(Instruction): |
paul@313 | 527 | "Check the current value as an instance of a specific class only." |
paul@313 | 528 | cost = 5 |
paul@234 | 529 | |
paul@234 | 530 | # Access to frames upon invocation. |
paul@234 | 531 | |
paul@313 | 532 | class CheckFrame(Immediate): |
paul@313 | 533 | "Check the frame for the correct number of arguments." |
paul@313 | 534 | cost = 3 |
paul@313 | 535 | |
paul@313 | 536 | class CheckExtra(Immediate): |
paul@313 | 537 | "Ensure that the frame can provide extra arguments." |
paul@313 | 538 | cost = 3 |
paul@313 | 539 | |
paul@313 | 540 | class FillDefaults(Immediate): |
paul@313 | 541 | "Fill frame positions with defaults, if appropriate." |
paul@313 | 542 | cost = 8 # variable |
paul@313 | 543 | |
paul@313 | 544 | class ExtendFrame(Immediate): |
paul@313 | 545 | "Extend the current frame for temporary storage use." |
paul@313 | 546 | cost = 1 |
paul@313 | 547 | |
paul@313 | 548 | class CopyExtra(Immediate): |
paul@313 | 549 | "Copy extra arguments into a separate sequence, starting from the given position." |
paul@313 | 550 | cost = 10 |
paul@97 | 551 | |
paul@97 | 552 | # Invocation-related instructions, using a special result "register". |
paul@97 | 553 | |
paul@313 | 554 | class JumpInFrame(Instruction): |
paul@313 | 555 | "Jump, using the current locals, to the current callable." |
paul@313 | 556 | cost = 2 |
paul@313 | 557 | |
paul@313 | 558 | class JumpWithFrame(Instruction): |
paul@313 | 559 | "Jump, adopting the invocation frame, to the current callable." |
paul@313 | 560 | cost = 3 |
paul@313 | 561 | |
paul@313 | 562 | class JumpWithFrameDirect(Target): |
paul@313 | 563 | "Jump to the specified address, adopting the invocation frame." |
paul@313 | 564 | cost = 3 |
paul@313 | 565 | |
paul@313 | 566 | class Return(Instruction): |
paul@313 | 567 | "Return from a subprogram." |
paul@313 | 568 | cost = 2 |
paul@313 | 569 | |
paul@313 | 570 | class LoadResult(Instruction): |
paul@313 | 571 | "Load into the current value a returned value." |
paul@313 | 572 | cost = 1 |
paul@313 | 573 | |
paul@313 | 574 | class StoreResult(Instruction): |
paul@313 | 575 | "Store the current value as a value to be returned." |
paul@313 | 576 | cost = 1 |
paul@21 | 577 | |
paul@97 | 578 | # Branch-related instructions. |
paul@21 | 579 | |
paul@313 | 580 | class Jump(Address): |
paul@313 | 581 | "Jump unconditionally." |
paul@313 | 582 | cost = 1 |
paul@313 | 583 | |
paul@313 | 584 | class JumpIfFalse(Address): |
paul@313 | 585 | "Jump if the last evaluation gave a false result." |
paul@313 | 586 | cost = 2 |
paul@313 | 587 | |
paul@313 | 588 | class JumpIfTrue(Address): |
paul@313 | 589 | "Jump if the last evaluation gave a true result." |
paul@313 | 590 | cost = 2 |
paul@97 | 591 | |
paul@97 | 592 | # Exception-related instructions, using a special exception "register". |
paul@97 | 593 | |
paul@313 | 594 | class LoadException(Instruction): |
paul@313 | 595 | "Load the raised exception." |
paul@313 | 596 | cost = 1 |
paul@313 | 597 | |
paul@313 | 598 | class StoreException(Instruction): |
paul@313 | 599 | "Store the current object in the exception register." |
paul@313 | 600 | cost = 1 |
paul@313 | 601 | |
paul@313 | 602 | class ClearException(Instruction): |
paul@313 | 603 | "Reset the exception register." |
paul@313 | 604 | cost = 1 |
paul@313 | 605 | |
paul@313 | 606 | class RaiseException(Instruction): |
paul@313 | 607 | "Raise an exception, jumping to the active handler." |
paul@313 | 608 | cost = 2 |
paul@313 | 609 | |
paul@313 | 610 | class PushHandler(Address): |
paul@313 | 611 | "Push an exception handler onto the handler stack." |
paul@313 | 612 | cost = 3 |
paul@313 | 613 | |
paul@313 | 614 | class PopHandler(Instruction): |
paul@313 | 615 | "Pop an exception handler from the handler stack." |
paul@313 | 616 | cost = 3 |
paul@313 | 617 | |
paul@313 | 618 | class CheckException(Instruction): |
paul@313 | 619 | "Check the raised exception against another." |
paul@313 | 620 | cost = 6 |
paul@17 | 621 | |
paul@116 | 622 | # Test instructions, operating on the boolean status register. |
paul@76 | 623 | |
paul@313 | 624 | class TestIdentity(Instruction): |
paul@313 | 625 | "Test whether the current value is identical to the source value, setting the boolean status." |
paul@313 | 626 | cost = 2 |
paul@313 | 627 | |
paul@313 | 628 | class TestIdentityAddress(Address): |
paul@313 | 629 | "Test whether the current value is identical to the given address, setting the boolean status." |
paul@313 | 630 | cost = 2 |
paul@313 | 631 | |
paul@313 | 632 | class InvertBoolean(Instruction): |
paul@313 | 633 | "Invert the boolean status." |
paul@313 | 634 | cost = 1 |
paul@76 | 635 | |
paul@234 | 636 | # Instructions which affect the current value. (LoadAttrIndexContext not defined.) |
paul@140 | 637 | |
paul@140 | 638 | current_value_instructions = ( |
paul@237 | 639 | LoadConst, LoadClass, LoadFunction, LoadName, LoadTemp, |
paul@194 | 640 | LoadAddress, LoadAddressContext, LoadAddressContextCond, |
paul@226 | 641 | LoadAttr, LoadAttrIndex, LoadAttrIndexContextCond, |
paul@194 | 642 | LoadCallable, LoadContext, LoadResult, |
paul@255 | 643 | LoadException, MakeInstance, MakeFragment, |
paul@255 | 644 | CopyExtra |
paul@140 | 645 | ) |
paul@140 | 646 | |
paul@313 | 647 | # Instructions which use the current value. (LoadAttrIndexContext not defined.) |
paul@256 | 648 | |
paul@256 | 649 | simple_input_user_instructions = ( |
paul@256 | 650 | StoreTemp, StoreFrame, StoreResult, StoreException, # as the value being stored |
paul@256 | 651 | LoadAddressContext, LoadAddressContextCond, # as the object being referenced |
paul@313 | 652 | LoadAttr, LoadAttrIndex, LoadAttrIndexContextCond, # as the object being referenced |
paul@256 | 653 | StoreAttr, StoreAttrIndex, StoreCallable, # as the object being referenced |
paul@256 | 654 | StoreFrameIndex, # as the object being referenced |
paul@256 | 655 | StoreAddressContext, # as the context |
paul@256 | 656 | LoadCallable, |
paul@285 | 657 | TestIdentity, TestIdentityAddress, CheckInstance, # as one of the operands |
paul@256 | 658 | CheckException, CheckFrame, FillDefaults, |
paul@256 | 659 | MakeInstance, |
paul@308 | 660 | CheckContext, CheckClass, CheckType, |
paul@256 | 661 | LoadContext # as the object providing the result |
paul@256 | 662 | ) |
paul@256 | 663 | |
paul@17 | 664 | # vim: tabstop=4 expandtab shiftwidth=4 |