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@84 | 22 | from micropython.data import Attr, Const, Instance |
paul@54 | 23 | |
paul@17 | 24 | class Instruction: |
paul@17 | 25 | |
paul@17 | 26 | "A generic instruction." |
paul@17 | 27 | |
paul@72 | 28 | stack_usage = 0 |
paul@72 | 29 | |
paul@86 | 30 | def __init__(self, attr=None, context=None): |
paul@17 | 31 | self.attr = attr |
paul@86 | 32 | if self.attr is not None and isinstance(self.attr, Attr): |
paul@86 | 33 | self.context = context or attr.parent |
paul@86 | 34 | else: |
paul@86 | 35 | self.context = context |
paul@17 | 36 | |
paul@17 | 37 | def __repr__(self): |
paul@26 | 38 | if self.attr is not None: |
paul@26 | 39 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@26 | 40 | else: |
paul@26 | 41 | return "%s()" % self.__class__.__name__ |
paul@17 | 42 | |
paul@55 | 43 | class StackRelativeInstruction(Instruction): |
paul@55 | 44 | |
paul@55 | 45 | "An instruction operating on the local value stack." |
paul@55 | 46 | |
paul@55 | 47 | def __repr__(self): |
paul@55 | 48 | return "%s(%r)" % (self.__class__.__name__, self.attr.position) |
paul@55 | 49 | |
paul@55 | 50 | SR = StackRelativeInstruction |
paul@55 | 51 | |
paul@55 | 52 | class AddressRelativeInstruction(Instruction): |
paul@55 | 53 | |
paul@55 | 54 | "An instruction accessing an object's attribute." |
paul@55 | 55 | |
paul@55 | 56 | def __repr__(self): |
paul@55 | 57 | position = self.attr.position |
paul@82 | 58 | location = "instance" |
paul@82 | 59 | result = position |
paul@82 | 60 | return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) |
paul@82 | 61 | |
paul@82 | 62 | AR = AddressRelativeInstruction |
paul@82 | 63 | |
paul@82 | 64 | class AddressInstruction(Instruction): |
paul@82 | 65 | |
paul@82 | 66 | "An instruction loading an address directly." |
paul@82 | 67 | |
paul@82 | 68 | def __repr__(self): |
paul@82 | 69 | if isinstance(self.attr, Attr): |
paul@82 | 70 | position = self.attr.position |
paul@55 | 71 | location = self.attr.parent.location |
paul@56 | 72 | |
paul@56 | 73 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 74 | |
paul@56 | 75 | if location is not None and position is not None: |
paul@56 | 76 | result = location + position + 1 |
paul@56 | 77 | else: |
paul@56 | 78 | location = self.attr.parent.name |
paul@56 | 79 | position = self.attr.name |
paul@56 | 80 | result = None |
paul@82 | 81 | return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) |
paul@82 | 82 | else: |
paul@82 | 83 | return "%s(%r)" % (self.__class__.__name__, self.attr.location) |
paul@55 | 84 | |
paul@70 | 85 | Address = AddressInstruction |
paul@70 | 86 | |
paul@70 | 87 | class ImmediateInstruction(Instruction): |
paul@70 | 88 | |
paul@70 | 89 | "An instruction employing a constant." |
paul@70 | 90 | |
paul@70 | 91 | def __repr__(self): |
paul@70 | 92 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@70 | 93 | |
paul@55 | 94 | Immediate = ImmediateInstruction |
paul@55 | 95 | |
paul@72 | 96 | # Mix-in classes for stack effects. |
paul@72 | 97 | |
paul@72 | 98 | class StackAdd: |
paul@72 | 99 | |
paul@72 | 100 | """ |
paul@72 | 101 | Indicate that the stack must grow to accommodate the result of this |
paul@72 | 102 | instruction. |
paul@72 | 103 | """ |
paul@72 | 104 | |
paul@72 | 105 | stack_usage = 1 |
paul@72 | 106 | |
paul@72 | 107 | class StackRemove: |
paul@72 | 108 | |
paul@72 | 109 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 110 | |
paul@72 | 111 | stack_usage = -1 |
paul@72 | 112 | |
paul@72 | 113 | class StackRemove2: |
paul@72 | 114 | |
paul@72 | 115 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 116 | |
paul@72 | 117 | stack_usage = -2 |
paul@72 | 118 | |
paul@21 | 119 | # Instructions operating on the value stack. |
paul@21 | 120 | |
paul@72 | 121 | class Duplicate(StackAdd, Instruction): "Duplicate the top of the stack." |
paul@72 | 122 | class Pop(StackRemove, Immediate): "Pop entries from the top of the stack." |
paul@21 | 123 | |
paul@86 | 124 | # Access to stored constant data. |
paul@86 | 125 | |
paul@86 | 126 | class LoadConst(StackAdd, Address): "Load the constant, class, function, module from the specified location." |
paul@86 | 127 | |
paul@21 | 128 | # Access within an invocation frame. |
paul@21 | 129 | |
paul@72 | 130 | class LoadName(StackAdd, SR): "Load the object from the given local attribute/variable." |
paul@72 | 131 | class StoreName(StackRemove, SR): "Store the object in the given local attribute/variable." |
paul@73 | 132 | class LoadTemp(Immediate): "Load the object from the given temporary location." |
paul@73 | 133 | class StoreTemp(Immediate): "Store the object in the given temporary location." |
paul@21 | 134 | |
paul@21 | 135 | # Access to address-relative data. |
paul@21 | 136 | |
paul@86 | 137 | class MakeObject(StackAdd, Instruction): "Make a new object. There isn't a complementary DropObject." |
paul@72 | 138 | class LoadAttr(AR): "Load the object from the given attribute." |
paul@72 | 139 | class StoreAttr(StackRemove2, AR): "Store an object in the given attribute." |
paul@72 | 140 | class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." |
paul@72 | 141 | class StoreAttrIndex(StackRemove2, Immediate): "Store an object in the attribute with the given index." |
paul@82 | 142 | class LoadAddress(StackAdd, Address): "Load the object from the given fixed attribute address." |
paul@82 | 143 | class StoreAddress(StackRemove, Address): "Store an object in the given fixed attribute address." |
paul@21 | 144 | |
paul@21 | 145 | # Access to invocation frames in preparation. |
paul@21 | 146 | |
paul@72 | 147 | class MakeFrame(Instruction): "Make a new invocation frame." |
paul@72 | 148 | class ReserveFrame(Immediate): "Reserve the given number of entries for the invocation frame." |
paul@72 | 149 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@72 | 150 | class StoreFrame(StackRemove, Instruction): "Store an argument at the given frame location." |
paul@72 | 151 | class StoreFrameIndex(StackRemove, Immediate): "Store an argument for the parameter with the given index." |
paul@72 | 152 | class CheckFrame(Instruction): "Check the invocation frame for the target." |
paul@72 | 153 | class JumpWithFrame(Instruction): "Jump, adopting the invocation frame." |
paul@21 | 154 | |
paul@21 | 155 | # Invocation-related instructions. |
paul@21 | 156 | |
paul@72 | 157 | class Jump(Instruction): "Jump unconditionally." |
paul@72 | 158 | class JumpIfFalse(Instruction): "Jump if the last evaluation gave a false result." |
paul@72 | 159 | class JumpIfTrue(Instruction): "Jump if the last evaluation gave a true result." |
paul@72 | 160 | class LoadCallable(Instruction): "Load the target of an invocation." |
paul@72 | 161 | class LoadContext(Instruction): "Load the context of an invocation." |
paul@72 | 162 | class CheckContext(Instruction): """Check the context of an invocation against the target, |
paul@72 | 163 | potentially discarding the context.""" |
paul@72 | 164 | class RaiseException(Instruction): "Raise an exception." |
paul@72 | 165 | class Return(Instruction): "Return a value from a subprogram." |
paul@72 | 166 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 167 | |
paul@76 | 168 | # General instructions. |
paul@76 | 169 | |
paul@76 | 170 | class TestIdentity(Instruction): "Test whether the two topmost stack values are identical." |
paul@76 | 171 | class TestIdentityAddress(Address): "Test whether the topmost stack value is identical to the given address." |
paul@76 | 172 | |
paul@17 | 173 | # vim: tabstop=4 expandtab shiftwidth=4 |