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@479 | 320 | return "%s(%s)" % (self.__class__.__name__, |
paul@479 | 321 | ", ".join( |
paul@479 | 322 | self.format_operand() + |
paul@479 | 323 | self.format_working() + |
paul@479 | 324 | self.format_source() + |
paul@479 | 325 | self.format_target() |
paul@479 | 326 | )) |
paul@103 | 327 | |
paul@451 | 328 | def __hash__(self): |
paul@451 | 329 | return hash(self.get_details()) |
paul@451 | 330 | |
paul@451 | 331 | def __eq__(self, other): |
paul@451 | 332 | return self.get_details() == other.get_details() |
paul@451 | 333 | |
paul@451 | 334 | def __ne__(self, other): |
paul@451 | 335 | return not self.__eq__(other) |
paul@451 | 336 | |
paul@450 | 337 | def format_operand(self): |
paul@450 | 338 | operand = self.get_operand() |
paul@479 | 339 | return operand is not None and [repr(operand)] or [] |
paul@450 | 340 | |
paul@450 | 341 | def format_working(self): |
paul@479 | 342 | return self.working != self.default_working and ["working=%r" % self.working] or [] |
paul@450 | 343 | |
paul@450 | 344 | def format_source(self): |
paul@479 | 345 | return self.source != self.default_source and ["source=%r" % self.source] or [] |
paul@450 | 346 | |
paul@450 | 347 | def format_target(self): |
paul@479 | 348 | return self.target != self.default_target and ["target=%r" % self.target] or [] |
paul@95 | 349 | |
paul@137 | 350 | def get_operand(self): |
paul@137 | 351 | return None |
paul@137 | 352 | |
paul@101 | 353 | class FrameRelativeInstruction(Instruction): |
paul@95 | 354 | |
paul@101 | 355 | "An instruction operating on the current frame." |
paul@17 | 356 | |
paul@90 | 357 | def get_operand(self): |
paul@90 | 358 | return self.attr.position |
paul@55 | 359 | |
paul@101 | 360 | FR = FrameRelativeInstruction |
paul@55 | 361 | |
paul@55 | 362 | class AddressRelativeInstruction(Instruction): |
paul@55 | 363 | |
paul@55 | 364 | "An instruction accessing an object's attribute." |
paul@55 | 365 | |
paul@450 | 366 | def format_operand(self): |
paul@479 | 367 | return Instruction.format_operand(self) + ["name=%r" % name(self.attr)] |
paul@90 | 368 | |
paul@90 | 369 | def get_operand(self): |
paul@90 | 370 | return self.attr.position |
paul@82 | 371 | |
paul@82 | 372 | AR = AddressRelativeInstruction |
paul@82 | 373 | |
paul@82 | 374 | class AddressInstruction(Instruction): |
paul@82 | 375 | |
paul@82 | 376 | "An instruction loading an address directly." |
paul@82 | 377 | |
paul@450 | 378 | def format_operand(self): |
paul@90 | 379 | location, position, result = self.get_operands() |
paul@90 | 380 | if location is not None: |
paul@479 | 381 | return ["%s" % result, "location=%s" % location, "position=%s" % position, |
paul@479 | 382 | "name=%s" % name(self.attr)] |
paul@91 | 383 | elif result is not None: |
paul@479 | 384 | return ["%s" % result, "name=%s" % name(self.attr)] |
paul@90 | 385 | else: |
paul@479 | 386 | return ["name=%s" % name(self.attr)] |
paul@90 | 387 | |
paul@90 | 388 | def get_operands(self): |
paul@82 | 389 | if isinstance(self.attr, Attr): |
paul@82 | 390 | position = self.attr.position |
paul@55 | 391 | location = self.attr.parent.location |
paul@56 | 392 | |
paul@56 | 393 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 394 | |
paul@56 | 395 | if location is not None and position is not None: |
paul@56 | 396 | result = location + position + 1 |
paul@56 | 397 | else: |
paul@56 | 398 | location = self.attr.parent.name |
paul@56 | 399 | position = self.attr.name |
paul@56 | 400 | result = None |
paul@90 | 401 | return location, position, result |
paul@82 | 402 | else: |
paul@90 | 403 | return None, None, self.attr.location |
paul@90 | 404 | |
paul@90 | 405 | def get_operand(self): |
paul@90 | 406 | return self.get_operands()[-1] |
paul@55 | 407 | |
paul@70 | 408 | Address = AddressInstruction |
paul@70 | 409 | |
paul@215 | 410 | class TargetInstruction(Instruction): |
paul@215 | 411 | |
paul@215 | 412 | "An instruction loading the address of an invocation target." |
paul@215 | 413 | |
paul@450 | 414 | def format_operand(self): |
paul@479 | 415 | return Instruction.format_operand(self) + ["name=%r" % name(self.attr)] |
paul@215 | 416 | |
paul@215 | 417 | def get_operand(self): |
paul@240 | 418 | return self.attr.code_body_location |
paul@215 | 419 | |
paul@215 | 420 | Target = TargetInstruction |
paul@215 | 421 | |
paul@70 | 422 | class ImmediateInstruction(Instruction): |
paul@70 | 423 | |
paul@70 | 424 | "An instruction employing a constant." |
paul@70 | 425 | |
paul@90 | 426 | def get_operand(self): |
paul@90 | 427 | return self.attr |
paul@90 | 428 | |
paul@55 | 429 | Immediate = ImmediateInstruction |
paul@55 | 430 | |
paul@450 | 431 | # Low-level instructions. |
paul@450 | 432 | |
paul@450 | 433 | class Transfer(Instruction): |
paul@450 | 434 | "Transfer a register's contents into another." |
paul@450 | 435 | cost = 1 |
paul@450 | 436 | |
paul@463 | 437 | class LoadMemory(Instruction): |
paul@463 | 438 | "Load a value from a memory address given by a register, optionally adding the operand." |
paul@463 | 439 | cost = 1 |
paul@463 | 440 | |
paul@463 | 441 | class Add(Instruction): |
paul@463 | 442 | "Add the value given by the working register to the operand." |
paul@463 | 443 | cost = 1 |
paul@463 | 444 | |
paul@463 | 445 | class Multiply(Instruction): |
paul@463 | 446 | "Multiply the value given by the working register by the operand." |
paul@463 | 447 | cost = 1 |
paul@463 | 448 | |
paul@86 | 449 | # Access to stored constant data. |
paul@86 | 450 | |
paul@313 | 451 | class LoadConst(Address): |
paul@313 | 452 | "Load the constant or module from the specified location." |
paul@313 | 453 | cost = 1 |
paul@313 | 454 | |
paul@313 | 455 | class LoadClass(Address): |
paul@313 | 456 | "Load the class from the specified location." |
paul@313 | 457 | cost = 1 |
paul@313 | 458 | |
paul@313 | 459 | class LoadFunction(Address): |
paul@313 | 460 | "Load the function from the specified location." |
paul@313 | 461 | cost = 1 |
paul@86 | 462 | |
paul@21 | 463 | # Access within an invocation frame. |
paul@21 | 464 | |
paul@313 | 465 | class LoadName(FR): |
paul@313 | 466 | "Load the current value from the given local attribute/variable." |
paul@313 | 467 | cost = 2 |
paul@313 | 468 | |
paul@313 | 469 | class StoreName(FR): |
paul@313 | 470 | "Store the source value into the given local attribute/variable." |
paul@313 | 471 | cost = 2 |
paul@464 | 472 | default_target = None |
paul@313 | 473 | |
paul@313 | 474 | class LoadTemp(Immediate): |
paul@313 | 475 | "Load the current value from the given temporary location." |
paul@313 | 476 | cost = 2 |
paul@313 | 477 | |
paul@313 | 478 | class StoreTemp(Immediate): |
paul@313 | 479 | "Store the current value into the given temporary location." |
paul@313 | 480 | cost = 2 |
paul@464 | 481 | default_target = None |
paul@21 | 482 | |
paul@98 | 483 | # Access to static data. |
paul@98 | 484 | |
paul@313 | 485 | class LoadAddress(Address): |
paul@313 | 486 | "Load the current value from the given fixed attribute address." |
paul@313 | 487 | cost = 1 |
paul@313 | 488 | |
paul@313 | 489 | class StoreAddress(Address): |
paul@313 | 490 | "Store the source value into the given fixed attribute address." |
paul@313 | 491 | cost = 1 |
paul@464 | 492 | default_working = None |
paul@464 | 493 | default_target = None |
paul@313 | 494 | |
paul@313 | 495 | class LoadAddressContext(Address): |
paul@313 | 496 | "Load the current value from the given fixed attribute address, using the current value as context." |
paul@313 | 497 | cost = 2 |
paul@313 | 498 | |
paul@313 | 499 | class StoreAddressContext(Address): |
paul@313 | 500 | "Store the current value into the given fixed attribute address, using the current value as context." |
paul@313 | 501 | cost = 2 |
paul@464 | 502 | default_target = None |
paul@313 | 503 | |
paul@194 | 504 | class LoadAddressContextCond(Address): |
paul@313 | 505 | """ |
paul@313 | 506 | Load the current value from the given fixed attribute address, only using the current value as |
paul@313 | 507 | context if the attribute is compatible. |
paul@313 | 508 | """ |
paul@313 | 509 | cost = 4 |
paul@313 | 510 | |
paul@313 | 511 | class MakeInstance(Immediate): |
paul@313 | 512 | "Make a new instance using the current value as a reference to a template." |
paul@313 | 513 | cost = 5 |
paul@313 | 514 | |
paul@313 | 515 | class MakeFragment(Immediate): |
paul@313 | 516 | "Make a new list fragment." |
paul@313 | 517 | cost = 5 |
paul@98 | 518 | |
paul@234 | 519 | # Access to address-relative data. (LoadAttrIndexContext not defined.) |
paul@21 | 520 | |
paul@313 | 521 | class LoadAttr(AR): |
paul@313 | 522 | "Load into the current value the given attribute of the object referenced by the current value." |
paul@313 | 523 | cost = 2 |
paul@313 | 524 | |
paul@313 | 525 | class StoreAttr(AR): |
paul@313 | 526 | "Store the source value into the given attribute of the object referenced by the current value." |
paul@313 | 527 | cost = 2 |
paul@464 | 528 | default_target = None |
paul@313 | 529 | |
paul@313 | 530 | class LoadAttrIndex(Immediate): |
paul@313 | 531 | "Load into the current value the attribute of the current value with the given index." |
paul@313 | 532 | cost = 6 |
paul@313 | 533 | |
paul@313 | 534 | class StoreAttrIndex(Immediate): |
paul@313 | 535 | "Store the source value into the attribute of the current value with the given index." |
paul@313 | 536 | cost = 6 |
paul@464 | 537 | default_target = None |
paul@313 | 538 | |
paul@194 | 539 | class LoadAttrIndexContextCond(Immediate): |
paul@313 | 540 | """ |
paul@313 | 541 | Load into the current value the attribute of the current value with the given index, only making the |
paul@313 | 542 | current value the context if the attribute is compatible. |
paul@313 | 543 | """ |
paul@313 | 544 | cost = 8 |
paul@21 | 545 | |
paul@138 | 546 | # Access to object details. |
paul@138 | 547 | |
paul@313 | 548 | class LoadCallable(Instruction): |
paul@313 | 549 | "Load the target of an invocation." |
paul@313 | 550 | cost = 2 |
paul@313 | 551 | |
paul@313 | 552 | class StoreCallable(Instruction): |
paul@313 | 553 | "Store the source value into the object referenced by the current value." |
paul@313 | 554 | cost = 3 |
paul@464 | 555 | default_target = None |
paul@138 | 556 | |
paul@21 | 557 | # Access to invocation frames in preparation. |
paul@21 | 558 | |
paul@313 | 559 | class MakeFrame(Immediate): |
paul@313 | 560 | "Make a new invocation frame." |
paul@313 | 561 | cost = 2 |
paul@464 | 562 | default_target = None |
paul@313 | 563 | |
paul@313 | 564 | class AdjustFrame(Immediate): |
paul@313 | 565 | "Adjust the current invocation frame for corrected invocations." |
paul@313 | 566 | cost = 2 |
paul@464 | 567 | default_target = None |
paul@313 | 568 | |
paul@313 | 569 | class DropFrame(Instruction): |
paul@313 | 570 | "Drop an invocation frame." |
paul@313 | 571 | cost = 2 |
paul@464 | 572 | default_target = None |
paul@313 | 573 | |
paul@313 | 574 | class StoreFrame(Immediate): |
paul@313 | 575 | "Store the current value as an argument for the parameter with the given position." |
paul@313 | 576 | cost = 2 |
paul@464 | 577 | default_target = None |
paul@313 | 578 | |
paul@313 | 579 | class StoreFrameIndex(Immediate): |
paul@313 | 580 | "Store the source value as an argument of the current value for the parameter with the given index." |
paul@313 | 581 | cost = 6 |
paul@464 | 582 | default_target = None |
paul@313 | 583 | |
paul@234 | 584 | # Context-related tests. |
paul@215 | 585 | |
paul@313 | 586 | class CheckContext(Instruction): |
paul@313 | 587 | "Check to see if the context is valid." |
paul@313 | 588 | cost = 2 |
paul@313 | 589 | |
paul@313 | 590 | class CheckClass(Instruction): |
paul@313 | 591 | "Check the current value to determine whether it is a class." |
paul@313 | 592 | cost = 2 |
paul@313 | 593 | |
paul@313 | 594 | class CheckInstance(Instruction): |
paul@313 | 595 | """ |
paul@313 | 596 | Check the current value as an instance of a class or its subclasses (used with 'self' in an |
paul@313 | 597 | invocation). |
paul@313 | 598 | """ |
paul@313 | 599 | cost = 6 |
paul@313 | 600 | |
paul@234 | 601 | # Access to frames upon invocation. |
paul@234 | 602 | |
paul@313 | 603 | class CheckFrame(Immediate): |
paul@313 | 604 | "Check the frame for the correct number of arguments." |
paul@313 | 605 | cost = 3 |
paul@313 | 606 | |
paul@313 | 607 | class CheckExtra(Immediate): |
paul@313 | 608 | "Ensure that the frame can provide extra arguments." |
paul@313 | 609 | cost = 3 |
paul@313 | 610 | |
paul@313 | 611 | class FillDefaults(Immediate): |
paul@313 | 612 | "Fill frame positions with defaults, if appropriate." |
paul@313 | 613 | cost = 8 # variable |
paul@464 | 614 | default_target = None |
paul@313 | 615 | |
paul@313 | 616 | class ExtendFrame(Immediate): |
paul@313 | 617 | "Extend the current frame for temporary storage use." |
paul@313 | 618 | cost = 1 |
paul@464 | 619 | default_target = None |
paul@313 | 620 | |
paul@313 | 621 | class CopyExtra(Immediate): |
paul@313 | 622 | "Copy extra arguments into a separate sequence, starting from the given position." |
paul@313 | 623 | cost = 10 |
paul@97 | 624 | |
paul@97 | 625 | # Invocation-related instructions, using a special result "register". |
paul@97 | 626 | |
paul@313 | 627 | class JumpInFrame(Instruction): |
paul@313 | 628 | "Jump, using the current locals, to the current callable." |
paul@313 | 629 | cost = 2 |
paul@464 | 630 | default_target = None |
paul@313 | 631 | |
paul@479 | 632 | class JumpInFrameDirect(Address): |
paul@479 | 633 | "Jump, using the current locals, to the specified address." |
paul@479 | 634 | cost = 2 |
paul@479 | 635 | default_target = None |
paul@479 | 636 | |
paul@313 | 637 | class JumpWithFrame(Instruction): |
paul@313 | 638 | "Jump, adopting the invocation frame, to the current callable." |
paul@313 | 639 | cost = 3 |
paul@464 | 640 | default_target = None |
paul@313 | 641 | |
paul@313 | 642 | class JumpWithFrameDirect(Target): |
paul@313 | 643 | "Jump to the specified address, adopting the invocation frame." |
paul@313 | 644 | cost = 3 |
paul@464 | 645 | default_target = None |
paul@313 | 646 | |
paul@313 | 647 | class Return(Instruction): |
paul@313 | 648 | "Return from a subprogram." |
paul@313 | 649 | cost = 2 |
paul@464 | 650 | default_target = None |
paul@313 | 651 | |
paul@97 | 652 | # Branch-related instructions. |
paul@21 | 653 | |
paul@313 | 654 | class Jump(Address): |
paul@313 | 655 | "Jump unconditionally." |
paul@313 | 656 | cost = 1 |
paul@464 | 657 | default_target = None |
paul@313 | 658 | |
paul@313 | 659 | class JumpIfFalse(Address): |
paul@313 | 660 | "Jump if the last evaluation gave a false result." |
paul@313 | 661 | cost = 2 |
paul@464 | 662 | default_target = None |
paul@313 | 663 | |
paul@313 | 664 | class JumpIfTrue(Address): |
paul@313 | 665 | "Jump if the last evaluation gave a true result." |
paul@313 | 666 | cost = 2 |
paul@464 | 667 | default_target = None |
paul@97 | 668 | |
paul@97 | 669 | # Exception-related instructions, using a special exception "register". |
paul@97 | 670 | |
paul@313 | 671 | class RaiseException(Instruction): |
paul@313 | 672 | "Raise an exception, jumping to the active handler." |
paul@313 | 673 | cost = 2 |
paul@464 | 674 | default_target = None |
paul@313 | 675 | |
paul@313 | 676 | class PushHandler(Address): |
paul@313 | 677 | "Push an exception handler onto the handler stack." |
paul@313 | 678 | cost = 3 |
paul@464 | 679 | default_target = None |
paul@313 | 680 | |
paul@398 | 681 | class PopHandler(Immediate): |
paul@398 | 682 | "Pop exception handlers from the handler stack." |
paul@313 | 683 | cost = 3 |
paul@464 | 684 | default_target = None |
paul@313 | 685 | |
paul@313 | 686 | class CheckException(Instruction): |
paul@313 | 687 | "Check the raised exception against another." |
paul@313 | 688 | cost = 6 |
paul@17 | 689 | |
paul@450 | 690 | class ClearException(Instruction): |
paul@450 | 691 | "Clear any raised exception." |
paul@450 | 692 | cost = 1 |
paul@450 | 693 | |
paul@116 | 694 | # Test instructions, operating on the boolean status register. |
paul@76 | 695 | |
paul@313 | 696 | class TestIdentity(Instruction): |
paul@313 | 697 | "Test whether the current value is identical to the source value, setting the boolean status." |
paul@313 | 698 | cost = 2 |
paul@313 | 699 | |
paul@313 | 700 | class TestIdentityAddress(Address): |
paul@313 | 701 | "Test whether the current value is identical to the given address, setting the boolean status." |
paul@313 | 702 | cost = 2 |
paul@313 | 703 | |
paul@313 | 704 | class InvertBoolean(Instruction): |
paul@313 | 705 | "Invert the boolean status." |
paul@313 | 706 | cost = 1 |
paul@76 | 707 | |
paul@450 | 708 | # Instructions which can affect the current value. (LoadAttrIndexContext not defined.) |
paul@140 | 709 | |
paul@464 | 710 | def affects_register(instruction, register): |
paul@464 | 711 | |
paul@464 | 712 | """ |
paul@464 | 713 | Returns whether 'instruction' affects the given 'register', either directly |
paul@464 | 714 | or as a consequence of its execution. |
paul@464 | 715 | """ |
paul@464 | 716 | |
paul@464 | 717 | return instruction.target == register or isinstance(instruction, ( |
paul@479 | 718 | JumpInFrame, JumpInFrameDirect, |
paul@479 | 719 | JumpWithFrame, JumpWithFrameDirect, |
paul@479 | 720 | JumpIfTrue, JumpIfFalse, |
paul@479 | 721 | Jump |
paul@464 | 722 | )) |
paul@256 | 723 | |
paul@17 | 724 | # vim: tabstop=4 expandtab shiftwidth=4 |