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