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