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