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