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 def __init__(self, attr=None): 45 self.attr = attr 46 47 def copy(self): 48 return self.__class__(self.attr) 49 50 def __repr__(self): 51 if self.attr is not None: 52 return "%s(%r)" % (self.__class__.__name__, self.attr) 53 else: 54 return "%s()" % (self.__class__.__name__) 55 56 class FrameRelativeInstruction(Instruction): 57 58 "An instruction operating on the current frame." 59 60 def __repr__(self): 61 return "%s(%r)" % (self.__class__.__name__, self.get_operand()) 62 63 def get_operand(self): 64 return self.attr.position 65 66 FR = FrameRelativeInstruction 67 68 class AddressRelativeInstruction(Instruction): 69 70 "An instruction accessing an object's attribute." 71 72 def __repr__(self): 73 position = self.get_operand() 74 if position is not None: 75 return "%s(%r) # %s" % (self.__class__.__name__, position, name(self.attr)) 76 else: 77 return "%s(%r)" % (self.__class__.__name__, name(self.attr)) 78 79 def get_operand(self): 80 return self.attr.position 81 82 AR = AddressRelativeInstruction 83 84 class AddressInstruction(Instruction): 85 86 "An instruction loading an address directly." 87 88 def __repr__(self): 89 location, position, result = self.get_operands() 90 if location is not None: 91 return "%s(%r) # %r, %r (%s)" % ( 92 self.__class__.__name__, result, location, position, name(self.attr)) 93 elif result is not None: 94 return "%s(%r) # %s" % ( 95 self.__class__.__name__, result, name(self.attr)) 96 else: 97 return "%s(...) # %s" % ( 98 self.__class__.__name__, name(self.attr)) 99 100 def get_operands(self): 101 if isinstance(self.attr, Attr): 102 position = self.attr.position 103 location = self.attr.parent.location 104 105 # NOTE: Unpositioned attributes are handled here. 106 107 if location is not None and position is not None: 108 result = location + position + 1 109 else: 110 location = self.attr.parent.name 111 position = self.attr.name 112 result = None 113 return location, position, result 114 elif isinstance(self.attr, Label): 115 return None, None, self.attr.location 116 else: 117 return None, None, self.attr.location 118 119 def get_operand(self): 120 return self.get_operands()[-1] 121 122 Address = AddressInstruction 123 124 class ImmediateInstruction(Instruction): 125 126 "An instruction employing a constant." 127 128 def __repr__(self): 129 return "%s(%r)" % (self.__class__.__name__, self.attr) 130 131 def get_operand(self): 132 return self.attr 133 134 Immediate = ImmediateInstruction 135 136 # Access to stored constant data. 137 138 class LoadConst(Address): "Load the constant, class, function, module from the specified location." 139 140 # Access within an invocation frame. 141 142 class LoadName(FR): "Load the object from the given local attribute/variable." 143 class StoreName(FR): "Store the object in the given local attribute/variable." 144 class LoadTemp(Immediate): "Load the object from the given temporary location." 145 class StoreTemp(Immediate): "Store the object in the given temporary location." 146 147 # Access to static data. 148 149 class LoadAddress(Address): "Load the object from the given fixed attribute address." 150 class StoreAddress(Address): "Store an object in the given fixed attribute address." 151 class LoadAddressContext(Address): "Load the object from the given fixed attribute address, changing the context." 152 class StoreAddressContext(Address): "Store an object in the given fixed attribute address, changing the context." 153 class MakeObject(Instruction): "Make a new object. There isn't a complementary DropObject." 154 155 # Access to address-relative data. 156 157 class LoadAttr(AR): "Load the object from the given attribute." 158 class StoreAttr(AR): "Store an object in the given attribute." 159 class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." 160 class StoreAttrIndex(Immediate): "Store an object in the attribute with the given index." 161 162 # Access to invocation frames in preparation. 163 164 class MakeFrame(Instruction): "Make a new invocation frame." 165 class DropFrame(Instruction): "Drop an invocation frame." 166 class StoreFrame(Immediate): "Store an argument for the parameter with the given position." 167 class StoreFrameIndex(Immediate): "Store an argument for the parameter with the given index." 168 class LoadCallable(Instruction): "Load the target of an invocation." 169 class LoadContext(Instruction): "Load the context of an invocation." 170 class CheckFrame(Instruction): "Check the invocation frame and context for the target." 171 class CheckSelf(Instruction): "Check the first argument of an invocation against the target." 172 173 # Invocation-related instructions, using a special result "register". 174 175 class JumpWithFrame(Instruction): "Jump, adopting the invocation frame, to the callable found as the current value." 176 class Return(Instruction): "Return a value from a subprogram." 177 class LoadResult(Instruction): "Load a returned value." 178 179 # Branch-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 185 # Exception-related instructions, using a special exception "register". 186 187 class LoadException(Instruction): "Load the raised exception." 188 class RaiseException(Instruction): "Raise an exception." 189 class CheckException(Instruction): "Check the raised exception against another." 190 class PushHandler(Address): "Push an exception handler onto the handler stack." 191 class PopHandler(Address): "Pop an exception handler from the handler stack." 192 193 # General instructions. 194 195 class TestBoolean(Instruction): "Test whether the current value is a true value." 196 class TestIdentityAddress(Address): "Test whether the current value is identical to the given address." 197 198 # vim: tabstop=4 expandtab shiftwidth=4