paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | RSVP instruction classes. |
paul@17 | 5 | |
paul@54 | 6 | Copyright (C) 2007, 2008 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@54 | 22 | from micropython.inspect import Const, Instance |
paul@54 | 23 | |
paul@17 | 24 | class Instruction: |
paul@17 | 25 | |
paul@17 | 26 | "A generic instruction." |
paul@17 | 27 | |
paul@21 | 28 | def __init__(self, attr=None): |
paul@17 | 29 | self.attr = attr |
paul@17 | 30 | |
paul@17 | 31 | def __repr__(self): |
paul@26 | 32 | if self.attr is not None: |
paul@26 | 33 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@26 | 34 | else: |
paul@26 | 35 | return "%s()" % self.__class__.__name__ |
paul@17 | 36 | |
paul@55 | 37 | class StackRelativeInstruction(Instruction): |
paul@55 | 38 | |
paul@55 | 39 | "An instruction operating on the local value stack." |
paul@55 | 40 | |
paul@55 | 41 | def __repr__(self): |
paul@55 | 42 | return "%s(%r)" % (self.__class__.__name__, self.attr.position) |
paul@55 | 43 | |
paul@55 | 44 | SR = StackRelativeInstruction |
paul@55 | 45 | |
paul@55 | 46 | class AddressRelativeInstruction(Instruction): |
paul@55 | 47 | |
paul@55 | 48 | "An instruction accessing an object's attribute." |
paul@55 | 49 | |
paul@55 | 50 | def __repr__(self): |
paul@55 | 51 | position = self.attr.position |
paul@55 | 52 | if isinstance(self.attr.parent, Instance): |
paul@55 | 53 | location = "instance" |
paul@55 | 54 | result = position |
paul@55 | 55 | else: |
paul@55 | 56 | location = self.attr.parent.location |
paul@56 | 57 | |
paul@56 | 58 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 59 | |
paul@56 | 60 | if location is not None and position is not None: |
paul@56 | 61 | result = location + position + 1 |
paul@56 | 62 | else: |
paul@56 | 63 | location = self.attr.parent.name |
paul@56 | 64 | position = self.attr.name |
paul@56 | 65 | result = None |
paul@55 | 66 | return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) |
paul@55 | 67 | |
paul@55 | 68 | AR = AddressRelativeInstruction |
paul@55 | 69 | |
paul@55 | 70 | class ImmediateInstruction(Instruction): |
paul@55 | 71 | |
paul@55 | 72 | "An instruction loading an address directly." |
paul@55 | 73 | |
paul@55 | 74 | def __repr__(self): |
paul@55 | 75 | return "%s(%r)" % (self.__class__.__name__, self.attr.location) |
paul@55 | 76 | |
paul@55 | 77 | Immediate = ImmediateInstruction |
paul@55 | 78 | |
paul@21 | 79 | # Instructions operating on the value stack. |
paul@21 | 80 | |
paul@55 | 81 | class LoadConst(Immediate): "Load the constant from the specified location." |
paul@44 | 82 | class Duplicate(Instruction): "Duplicate the top of stack." |
paul@44 | 83 | class Pop(Instruction): "Pop the top of stack." |
paul@21 | 84 | |
paul@21 | 85 | # Access within an invocation frame. |
paul@21 | 86 | |
paul@55 | 87 | class LoadName(SR): "Load the object from the given local attribute/variable." |
paul@55 | 88 | class StoreName(SR): "Store the object in the given local attribute/variable." |
paul@57 | 89 | class LoadTemp(SR): "Load the object from the given temporary location." |
paul@57 | 90 | class StoreTemp(SR): "Store the object in the given temporary location." |
paul@21 | 91 | |
paul@21 | 92 | # Access to address-relative data. |
paul@21 | 93 | |
paul@26 | 94 | class MakeObject(Instruction): "Make a new object." |
paul@26 | 95 | # ... DropObject not defined: Assume garbage collection. |
paul@55 | 96 | class LoadAttr(AR): "Load the object from the given attribute." |
paul@55 | 97 | class StoreAttr(AR): "Store an object in the given attribute." |
paul@68 | 98 | class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." |
paul@68 | 99 | class StoreAttrIndex(Immediate): "Store an object in the attribute with the given index." |
paul@21 | 100 | |
paul@21 | 101 | # Access to invocation frames in preparation. |
paul@21 | 102 | |
paul@26 | 103 | class MakeFrame(Instruction): "Make a new invocation frame." |
paul@68 | 104 | class ReserveFrame(Immediate): "Reserve the given number of entries for the invocation frame." |
paul@26 | 105 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@26 | 106 | class StoreFrame(Instruction): "Store an argument at the given frame location." |
paul@68 | 107 | class StoreFrameIndex(Immediate): "Store an argument for the parameter with the given index." |
paul@59 | 108 | class CheckFrame(Instruction): "Check the invocation frame for the target." |
paul@68 | 109 | class JumpWithFrame(Instruction): "Jump, adopting the invocation frame." |
paul@21 | 110 | |
paul@21 | 111 | # Invocation-related instructions. |
paul@21 | 112 | |
paul@45 | 113 | class Jump(Instruction): "Jump unconditionally." |
paul@45 | 114 | class JumpIfFalse(Instruction): "Jump if the last evaluation gave a false result." |
paul@45 | 115 | class JumpIfTrue(Instruction): "Jump if the last evaluation gave a true result." |
paul@45 | 116 | class LoadCallable(Instruction): "Load the target of an invocation." |
paul@45 | 117 | class LoadContext(Instruction): "Load the context of an invocation." |
paul@59 | 118 | class CheckContext(Instruction): "Check the context of an invocation against the target, potentially discarding the context." |
paul@45 | 119 | class RaiseException(Instruction): "Raise an exception." |
paul@45 | 120 | class Return(Instruction): "Return a value from a subprogram." |
paul@45 | 121 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 122 | |
paul@17 | 123 | # vim: tabstop=4 expandtab shiftwidth=4 |