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