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