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