1 #!/usr/bin/env python 2 3 """ 4 RSVP instruction classes. 5 6 Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from micropython.data import Attr, Const, Instance 23 24 class Instruction: 25 26 "A generic instruction." 27 28 stack_usage = 0 29 30 def __init__(self, attr=None, context=None): 31 self.attr = attr 32 if self.attr is not None and isinstance(self.attr, Attr): 33 self.context = context or attr.parent 34 else: 35 self.context = context 36 37 def __repr__(self): 38 if self.attr is not None: 39 return "%s(%r)" % (self.__class__.__name__, self.attr) 40 else: 41 return "%s()" % self.__class__.__name__ 42 43 class StackRelativeInstruction(Instruction): 44 45 "An instruction operating on the local value stack." 46 47 def __repr__(self): 48 return "%s(%r)" % (self.__class__.__name__, self.attr.position) 49 50 SR = StackRelativeInstruction 51 52 class AddressRelativeInstruction(Instruction): 53 54 "An instruction accessing an object's attribute." 55 56 def __repr__(self): 57 position = self.attr.position 58 location = "instance" 59 result = position 60 return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) 61 62 AR = AddressRelativeInstruction 63 64 class AddressInstruction(Instruction): 65 66 "An instruction loading an address directly." 67 68 def __repr__(self): 69 if isinstance(self.attr, Attr): 70 position = self.attr.position 71 location = self.attr.parent.location 72 73 # NOTE: Unpositioned attributes are handled here. 74 75 if location is not None and position is not None: 76 result = location + position + 1 77 else: 78 location = self.attr.parent.name 79 position = self.attr.name 80 result = None 81 return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) 82 else: 83 return "%s(%r)" % (self.__class__.__name__, self.attr.location) 84 85 Address = AddressInstruction 86 87 class ImmediateInstruction(Instruction): 88 89 "An instruction employing a constant." 90 91 def __repr__(self): 92 return "%s(%r)" % (self.__class__.__name__, self.attr) 93 94 Immediate = ImmediateInstruction 95 96 # Mix-in classes for stack effects. 97 98 class StackAdd: 99 100 """ 101 Indicate that the stack must grow to accommodate the result of this 102 instruction. 103 """ 104 105 stack_usage = 1 106 107 class StackRemove: 108 109 "Indicate that the stack must shrink as an effect of this instruction." 110 111 stack_usage = -1 112 113 class StackRemove2: 114 115 "Indicate that the stack must shrink as an effect of this instruction." 116 117 stack_usage = -2 118 119 # Instructions operating on the value stack. 120 121 class Duplicate(StackAdd, Instruction): "Duplicate the top of the stack." 122 class Pop(StackRemove, Immediate): "Pop entries from the top of the stack." 123 124 # Access to stored constant data. 125 126 class LoadConst(StackAdd, Address): "Load the constant, class, function, module from the specified location." 127 128 # Access within an invocation frame. 129 130 class LoadName(StackAdd, SR): "Load the object from the given local attribute/variable." 131 class StoreName(StackRemove, SR): "Store the object in the given local attribute/variable." 132 class LoadTemp(Immediate): "Load the object from the given temporary location." 133 class StoreTemp(Immediate): "Store the object in the given temporary location." 134 135 # Access to address-relative data. 136 137 class MakeObject(StackAdd, Instruction): "Make a new object. There isn't a complementary DropObject." 138 class LoadAttr(AR): "Load the object from the given attribute." 139 class StoreAttr(StackRemove2, AR): "Store an object in the given attribute." 140 class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." 141 class StoreAttrIndex(StackRemove2, Immediate): "Store an object in the attribute with the given index." 142 class LoadAddress(StackAdd, Address): "Load the object from the given fixed attribute address." 143 class StoreAddress(StackRemove, Address): "Store an object in the given fixed attribute address." 144 145 # Access to invocation frames in preparation. 146 147 class MakeFrame(Instruction): "Make a new invocation frame." 148 class ReserveFrame(Immediate): "Reserve the given number of entries for the invocation frame." 149 class DropFrame(Instruction): "Drop an invocation frame." 150 class StoreFrame(StackRemove, Instruction): "Store an argument at the given frame location." 151 class StoreFrameIndex(StackRemove, Immediate): "Store an argument for the parameter with the given index." 152 class CheckFrame(Instruction): "Check the invocation frame for the target." 153 class JumpWithFrame(Instruction): "Jump, adopting the invocation frame." 154 155 # Invocation-related instructions. 156 157 class Jump(Instruction): "Jump unconditionally." 158 class JumpIfFalse(Instruction): "Jump if the last evaluation gave a false result." 159 class JumpIfTrue(Instruction): "Jump if the last evaluation gave a true result." 160 class LoadCallable(Instruction): "Load the target of an invocation." 161 class LoadContext(Instruction): "Load the context of an invocation." 162 class CheckContext(Instruction): """Check the context of an invocation against the target, 163 potentially discarding the context.""" 164 class RaiseException(Instruction): "Raise an exception." 165 class Return(Instruction): "Return a value from a subprogram." 166 class CheckException(Instruction): "Check the raised exception against another." 167 168 # General instructions. 169 170 class TestIdentity(Instruction): "Test whether the two topmost stack values are identical." 171 class TestIdentityAddress(Address): "Test whether the topmost stack value is identical to the given address." 172 173 # vim: tabstop=4 expandtab shiftwidth=4