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