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