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