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, Class, Const, Function 24 25 def raw(code, objtable, paramtable): 26 new_code = [] 27 for item in code: 28 29 if isinstance(item, Attr): 30 new_code.append(( 31 item.context and item.context.location, 32 item.value and item.value.location # no useful context is provided 33 )) 34 35 # Using classcode, attrcode, codeaddr, codedetails, instance. 36 37 elif isinstance(item, Class): 38 # NOTE: Need initialiser details! 39 new_code.append(( 40 objtable.as_list().get_code(item.full_name()), 41 objtable.get_index(item.full_name()), 42 item.get_instantiator().code_location, 43 ( 44 len(item.get_instantiator().positional_names), 45 len(item.get_instantiator().defaults) 46 ), 47 0 48 )) 49 50 elif isinstance(item, Const): 51 # NOTE: Need class details! 52 new_code.append(( 53 None, #objtable.as_list().get_code(item.full_name()), 54 None, #objtable.get_index(item.full_name()), 55 None, 56 None, 57 1 58 )) 59 60 elif isinstance(item, Function): 61 # NOTE: Need class and parameter details! Should arguably be types.FunctionType. 62 new_code.append(( 63 objtable.as_list().get_code("__builtins__.function"), 64 objtable.get_index("__builtins__.function"), 65 item.code_location, 66 ( 67 len(item.positional_names), 68 len(item.defaults) 69 ), 70 0 71 )) 72 73 else: 74 new_code.append(item) 75 76 return new_code 77 78 def name(attr): 79 if isinstance(attr, Attr): 80 return attr.name or "<unnamed>" 81 else: 82 return attr or "<unnamed>" 83 84 class Instruction: 85 86 "A generic instruction." 87 88 def __init__(self, attr=None): 89 self.attr = attr 90 self.input = None 91 self.source = None # for storage instructions 92 93 def copy(self): 94 return self.__class__(self.attr) 95 96 def __repr__(self): 97 if self.attr is not None: 98 return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) 99 else: 100 return "%s()%s" % (self.__class__.__name__, self.show_input()) 101 102 def show_input(self): 103 if self.input is not None: 104 if self.source is not None: 105 return " <- (%r, %r)" % (self.input, self.source) 106 else: 107 return " <- %r" % self.input 108 elif self.source is not None: 109 return " <-- %r" % self.source 110 else: 111 return "" 112 113 def get_operand(self): 114 return None 115 116 class FrameRelativeInstruction(Instruction): 117 118 "An instruction operating on the current frame." 119 120 def __repr__(self): 121 return "%s(%r)%s" % (self.__class__.__name__, self.get_operand(), self.show_input()) 122 123 def get_operand(self): 124 return self.attr.position 125 126 FR = FrameRelativeInstruction 127 128 class AddressRelativeInstruction(Instruction): 129 130 "An instruction accessing an object's attribute." 131 132 def __repr__(self): 133 position = self.get_operand() 134 if position is not None: 135 return "%s(%r)%s # %s" % (self.__class__.__name__, position, self.show_input(), name(self.attr)) 136 else: 137 return "%s(%r)%s" % (self.__class__.__name__, name(self.attr), self.show_input()) 138 139 def get_operand(self): 140 return self.attr.position 141 142 AR = AddressRelativeInstruction 143 144 class AddressInstruction(Instruction): 145 146 "An instruction loading an address directly." 147 148 def __repr__(self): 149 location, position, result = self.get_operands() 150 if location is not None: 151 return "%s(%r)%s # %r, %r (%s)" % ( 152 self.__class__.__name__, result, self.show_input(), location, position, name(self.attr)) 153 elif result is not None: 154 return "%s(%r)%s # %s" % ( 155 self.__class__.__name__, result, self.show_input(), name(self.attr)) 156 else: 157 return "%s(...)%s # %s" % ( 158 self.__class__.__name__, self.show_input(), name(self.attr)) 159 160 def get_operands(self): 161 if isinstance(self.attr, Attr): 162 position = self.attr.position 163 location = self.attr.parent.location 164 165 # NOTE: Unpositioned attributes are handled here. 166 167 if location is not None and position is not None: 168 result = location + position + 1 169 else: 170 location = self.attr.parent.name 171 position = self.attr.name 172 result = None 173 return location, position, result 174 elif isinstance(self.attr, Label): 175 return None, None, self.attr.location 176 else: 177 return None, None, self.attr.location 178 179 def get_operand(self): 180 return self.get_operands()[-1] 181 182 Address = AddressInstruction 183 184 class ImmediateInstruction(Instruction): 185 186 "An instruction employing a constant." 187 188 def __repr__(self): 189 return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) 190 191 def get_operand(self): 192 return self.attr 193 194 Immediate = ImmediateInstruction 195 196 # Access to stored constant data. 197 198 class LoadConst(Address): "Load the constant, class, function, module from the specified location." 199 200 # Access within an invocation frame. 201 202 class LoadName(FR): "Load the current value from the given local attribute/variable." 203 class StoreName(FR): "Store the source value into the given local attribute/variable." 204 class LoadTemp(Immediate): "Load the current value from the given temporary location." 205 class StoreTemp(Immediate): "Store the current value into the given temporary location." 206 207 # Access to static data. 208 209 class LoadAddress(Address): "Load the current value from the given fixed attribute address." 210 class StoreAddress(Address): "Store the source value into the given fixed attribute address." 211 class LoadAddressContext(Address): "Load the current value from the given fixed attribute address, making the current value the context." 212 class MakeObject(Immediate): "Make a new object. There isn't a complementary DropObject." 213 214 # Access to address-relative data. 215 216 class LoadAttr(AR): "Load into the current value the given attribute of the object referenced by the current value." 217 class StoreAttr(AR): "Store the source value into the given attribute of the object referenced by the current value." 218 class LoadAttrIndex(Immediate): "Load into the current value the attribute of the current value with the given index." 219 class StoreAttrIndex(Immediate): "Store an object in the attribute with the given index." 220 221 # Access to object details. 222 223 class LoadCallable(Instruction): "Load the target of an invocation." 224 class StoreCallable(Instruction): "Store the source value into the object referenced by the current value." 225 226 # Access to invocation frames in preparation. 227 228 class MakeFrame(Immediate): "Make a new invocation frame." 229 class DropFrame(Instruction): "Drop an invocation frame." 230 class RecoverFrame(Instruction): "Recover the current frame as an invocation frame." 231 class StoreFrame(Immediate): "Store the current value as an argument for the parameter with the given position." 232 class StoreFrameIndex(Immediate): "Store the current value as an argument for the parameter with the given index." 233 class LoadContext(Instruction): "Load the context of an invocation." 234 class CheckFrame(Immediate): "Check the invocation frame and context for the target." 235 class CheckSelf(Instruction): "Check the first argument of an invocation against the target." 236 237 # Invocation-related instructions, using a special result "register". 238 239 class JumpWithFrame(Instruction): "Jump, adopting the invocation frame, to the current callable." 240 class ExtendFrame(Immediate): "Extend the current frame for temporary storage use." 241 class AdjustFrame(Immediate): "Adjust the current frame for corrected invocations." 242 class Return(Instruction): "Return from a subprogram." 243 class LoadResult(Instruction): "Load into the current value a returned value." 244 class StoreResult(Instruction): "Store the current value as a value to be returned." 245 246 # Branch-related instructions. 247 248 class Jump(Address): "Jump unconditionally." 249 class JumpIfFalse(Address): "Jump if the last evaluation gave a false result." 250 class JumpIfTrue(Address): "Jump if the last evaluation gave a true result." 251 252 # Exception-related instructions, using a special exception "register". 253 254 class LoadException(Instruction): "Load the raised exception." 255 class StoreException(Instruction): "Store the current object in the exception register." 256 class RaiseException(Instruction): "Raise an exception, jumping to the active handler." 257 class PushHandler(Address): "Push an exception handler onto the handler stack." 258 class PopHandler(Instruction): "Pop an exception handler from the handler stack." 259 class CheckException(Instruction): "Check the raised exception against another." 260 261 # Test instructions, operating on the boolean status register. 262 263 class TestIdentity(Instruction): "Test whether the current value is identical to the source value, setting the boolean status." 264 class TestIdentityAddress(Address): "Test whether the current value is identical to the given address, setting the boolean status." 265 class InvertBoolean(Instruction): "Invert the boolean status." 266 267 # Instructions which affect the current value. 268 269 current_value_instructions = ( 270 LoadConst, LoadName, LoadTemp, LoadAddress, LoadAddressContext, 271 LoadAttr, LoadAttrIndex, LoadCallable, LoadContext, LoadResult, 272 LoadException, MakeObject 273 ) 274 275 # vim: tabstop=4 expandtab shiftwidth=4