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