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