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