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