paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | RSVP instruction classes. |
paul@17 | 5 | |
paul@180 | 6 | Copyright (C) 2007, 2008, 2009 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@180 | 22 | from micropython.data import Attr, Const |
paul@165 | 23 | |
paul@91 | 24 | def name(attr): |
paul@91 | 25 | if isinstance(attr, Attr): |
paul@107 | 26 | return attr.name or "<unnamed>" |
paul@180 | 27 | elif isinstance(attr, Const): |
paul@180 | 28 | return attr |
paul@91 | 29 | else: |
paul@180 | 30 | return attr.full_name() or "<unnamed>" |
paul@91 | 31 | |
paul@17 | 32 | class Instruction: |
paul@17 | 33 | |
paul@17 | 34 | "A generic instruction." |
paul@17 | 35 | |
paul@87 | 36 | def __init__(self, attr=None): |
paul@17 | 37 | self.attr = attr |
paul@103 | 38 | self.input = None |
paul@103 | 39 | self.source = None # for storage instructions |
paul@95 | 40 | |
paul@95 | 41 | def copy(self): |
paul@95 | 42 | return self.__class__(self.attr) |
paul@95 | 43 | |
paul@101 | 44 | def __repr__(self): |
paul@101 | 45 | if self.attr is not None: |
paul@103 | 46 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) |
paul@101 | 47 | else: |
paul@103 | 48 | return "%s()%s" % (self.__class__.__name__, self.show_input()) |
paul@103 | 49 | |
paul@103 | 50 | def show_input(self): |
paul@103 | 51 | if self.input is not None: |
paul@103 | 52 | if self.source is not None: |
paul@103 | 53 | return " <- (%r, %r)" % (self.input, self.source) |
paul@103 | 54 | else: |
paul@103 | 55 | return " <- %r" % self.input |
paul@104 | 56 | elif self.source is not None: |
paul@104 | 57 | return " <-- %r" % self.source |
paul@103 | 58 | else: |
paul@103 | 59 | return "" |
paul@95 | 60 | |
paul@137 | 61 | def get_operand(self): |
paul@137 | 62 | return None |
paul@137 | 63 | |
paul@101 | 64 | class FrameRelativeInstruction(Instruction): |
paul@95 | 65 | |
paul@101 | 66 | "An instruction operating on the current frame." |
paul@17 | 67 | |
paul@17 | 68 | def __repr__(self): |
paul@103 | 69 | return "%s(%r)%s" % (self.__class__.__name__, self.get_operand(), self.show_input()) |
paul@90 | 70 | |
paul@90 | 71 | def get_operand(self): |
paul@90 | 72 | return self.attr.position |
paul@55 | 73 | |
paul@101 | 74 | FR = FrameRelativeInstruction |
paul@55 | 75 | |
paul@55 | 76 | class AddressRelativeInstruction(Instruction): |
paul@55 | 77 | |
paul@55 | 78 | "An instruction accessing an object's attribute." |
paul@55 | 79 | |
paul@55 | 80 | def __repr__(self): |
paul@91 | 81 | position = self.get_operand() |
paul@91 | 82 | if position is not None: |
paul@103 | 83 | return "%s(%r)%s # %s" % (self.__class__.__name__, position, self.show_input(), name(self.attr)) |
paul@91 | 84 | else: |
paul@103 | 85 | return "%s(%r)%s" % (self.__class__.__name__, name(self.attr), self.show_input()) |
paul@90 | 86 | |
paul@90 | 87 | def get_operand(self): |
paul@90 | 88 | return self.attr.position |
paul@82 | 89 | |
paul@82 | 90 | AR = AddressRelativeInstruction |
paul@82 | 91 | |
paul@82 | 92 | class AddressInstruction(Instruction): |
paul@82 | 93 | |
paul@82 | 94 | "An instruction loading an address directly." |
paul@82 | 95 | |
paul@82 | 96 | def __repr__(self): |
paul@90 | 97 | location, position, result = self.get_operands() |
paul@90 | 98 | if location is not None: |
paul@103 | 99 | return "%s(%r)%s # %r, %r (%s)" % ( |
paul@103 | 100 | self.__class__.__name__, result, self.show_input(), location, position, name(self.attr)) |
paul@91 | 101 | elif result is not None: |
paul@103 | 102 | return "%s(%r)%s # %s" % ( |
paul@103 | 103 | self.__class__.__name__, result, self.show_input(), name(self.attr)) |
paul@90 | 104 | else: |
paul@103 | 105 | return "%s(...)%s # %s" % ( |
paul@103 | 106 | self.__class__.__name__, self.show_input(), name(self.attr)) |
paul@90 | 107 | |
paul@90 | 108 | def get_operands(self): |
paul@82 | 109 | if isinstance(self.attr, Attr): |
paul@82 | 110 | position = self.attr.position |
paul@55 | 111 | location = self.attr.parent.location |
paul@56 | 112 | |
paul@56 | 113 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 114 | |
paul@56 | 115 | if location is not None and position is not None: |
paul@56 | 116 | result = location + position + 1 |
paul@56 | 117 | else: |
paul@56 | 118 | location = self.attr.parent.name |
paul@56 | 119 | position = self.attr.name |
paul@56 | 120 | result = None |
paul@90 | 121 | return location, position, result |
paul@82 | 122 | else: |
paul@90 | 123 | return None, None, self.attr.location |
paul@90 | 124 | |
paul@90 | 125 | def get_operand(self): |
paul@90 | 126 | return self.get_operands()[-1] |
paul@55 | 127 | |
paul@70 | 128 | Address = AddressInstruction |
paul@70 | 129 | |
paul@70 | 130 | class ImmediateInstruction(Instruction): |
paul@70 | 131 | |
paul@70 | 132 | "An instruction employing a constant." |
paul@70 | 133 | |
paul@70 | 134 | def __repr__(self): |
paul@103 | 135 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_input()) |
paul@70 | 136 | |
paul@90 | 137 | def get_operand(self): |
paul@90 | 138 | return self.attr |
paul@90 | 139 | |
paul@55 | 140 | Immediate = ImmediateInstruction |
paul@55 | 141 | |
paul@86 | 142 | # Access to stored constant data. |
paul@86 | 143 | |
paul@101 | 144 | class LoadConst(Address): "Load the constant, class, function, module from the specified location." |
paul@86 | 145 | |
paul@21 | 146 | # Access within an invocation frame. |
paul@21 | 147 | |
paul@105 | 148 | class LoadName(FR): "Load the current value from the given local attribute/variable." |
paul@105 | 149 | class StoreName(FR): "Store the source value into the given local attribute/variable." |
paul@105 | 150 | class LoadTemp(Immediate): "Load the current value from the given temporary location." |
paul@105 | 151 | class StoreTemp(Immediate): "Store the current value into the given temporary location." |
paul@21 | 152 | |
paul@98 | 153 | # Access to static data. |
paul@98 | 154 | |
paul@105 | 155 | class LoadAddress(Address): "Load the current value from the given fixed attribute address." |
paul@105 | 156 | class StoreAddress(Address): "Store the source value into the given fixed attribute address." |
paul@105 | 157 | class LoadAddressContext(Address): "Load the current value from the given fixed attribute address, making the current value the context." |
paul@109 | 158 | class MakeObject(Immediate): "Make a new object. There isn't a complementary DropObject." |
paul@98 | 159 | |
paul@21 | 160 | # Access to address-relative data. |
paul@21 | 161 | |
paul@105 | 162 | class LoadAttr(AR): "Load into the current value the given attribute of the object referenced by the current value." |
paul@105 | 163 | class StoreAttr(AR): "Store the source value into the given attribute of the object referenced by the current value." |
paul@105 | 164 | class LoadAttrIndex(Immediate): "Load into the current value the attribute of the current value with the given index." |
paul@101 | 165 | class StoreAttrIndex(Immediate): "Store an object in the attribute with the given index." |
paul@21 | 166 | |
paul@138 | 167 | # Access to object details. |
paul@138 | 168 | |
paul@138 | 169 | class LoadCallable(Instruction): "Load the target of an invocation." |
paul@138 | 170 | class StoreCallable(Instruction): "Store the source value into the object referenced by the current value." |
paul@138 | 171 | |
paul@21 | 172 | # Access to invocation frames in preparation. |
paul@21 | 173 | |
paul@117 | 174 | class MakeFrame(Immediate): "Make a new invocation frame." |
paul@101 | 175 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@137 | 176 | class RecoverFrame(Instruction): "Recover the current frame as an invocation frame." |
paul@105 | 177 | class StoreFrame(Immediate): "Store the current value as an argument for the parameter with the given position." |
paul@105 | 178 | class StoreFrameIndex(Immediate): "Store the current value as an argument for the parameter with the given index." |
paul@101 | 179 | class LoadContext(Instruction): "Load the context of an invocation." |
paul@111 | 180 | class CheckFrame(Immediate): "Check the invocation frame and context for the target." |
paul@101 | 181 | class CheckSelf(Instruction): "Check the first argument of an invocation against the target." |
paul@97 | 182 | |
paul@97 | 183 | # Invocation-related instructions, using a special result "register". |
paul@97 | 184 | |
paul@137 | 185 | class JumpWithFrame(Instruction): "Jump, adopting the invocation frame, to the current callable." |
paul@132 | 186 | class ExtendFrame(Immediate): "Extend the current frame for temporary storage use." |
paul@137 | 187 | class AdjustFrame(Immediate): "Adjust the current frame for corrected invocations." |
paul@104 | 188 | class Return(Instruction): "Return from a subprogram." |
paul@105 | 189 | class LoadResult(Instruction): "Load into the current value a returned value." |
paul@105 | 190 | class StoreResult(Instruction): "Store the current value as a value to be returned." |
paul@21 | 191 | |
paul@97 | 192 | # Branch-related instructions. |
paul@21 | 193 | |
paul@101 | 194 | class Jump(Address): "Jump unconditionally." |
paul@101 | 195 | class JumpIfFalse(Address): "Jump if the last evaluation gave a false result." |
paul@101 | 196 | class JumpIfTrue(Address): "Jump if the last evaluation gave a true result." |
paul@97 | 197 | |
paul@97 | 198 | # Exception-related instructions, using a special exception "register". |
paul@97 | 199 | |
paul@101 | 200 | class LoadException(Instruction): "Load the raised exception." |
paul@105 | 201 | class StoreException(Instruction): "Store the current object in the exception register." |
paul@105 | 202 | class RaiseException(Instruction): "Raise an exception, jumping to the active handler." |
paul@102 | 203 | class PushHandler(Address): "Push an exception handler onto the handler stack." |
paul@103 | 204 | class PopHandler(Instruction): "Pop an exception handler from the handler stack." |
paul@105 | 205 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 206 | |
paul@116 | 207 | # Test instructions, operating on the boolean status register. |
paul@76 | 208 | |
paul@116 | 209 | class TestIdentity(Instruction): "Test whether the current value is identical to the source value, setting the boolean status." |
paul@116 | 210 | class TestIdentityAddress(Address): "Test whether the current value is identical to the given address, setting the boolean status." |
paul@116 | 211 | class InvertBoolean(Instruction): "Invert the boolean status." |
paul@76 | 212 | |
paul@140 | 213 | # Instructions which affect the current value. |
paul@140 | 214 | |
paul@140 | 215 | current_value_instructions = ( |
paul@140 | 216 | LoadConst, LoadName, LoadTemp, LoadAddress, LoadAddressContext, |
paul@140 | 217 | LoadAttr, LoadAttrIndex, LoadCallable, LoadContext, LoadResult, |
paul@140 | 218 | LoadException, MakeObject |
paul@140 | 219 | ) |
paul@140 | 220 | |
paul@17 | 221 | # vim: tabstop=4 expandtab shiftwidth=4 |