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