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