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@91 | 36 | return attr.name |
paul@91 | 37 | else: |
paul@91 | 38 | return attr |
paul@91 | 39 | |
paul@17 | 40 | class Instruction: |
paul@17 | 41 | |
paul@17 | 42 | "A generic instruction." |
paul@17 | 43 | |
paul@72 | 44 | stack_usage = 0 |
paul@72 | 45 | |
paul@87 | 46 | def __init__(self, attr=None): |
paul@17 | 47 | self.attr = attr |
paul@17 | 48 | |
paul@17 | 49 | def __repr__(self): |
paul@26 | 50 | if self.attr is not None: |
paul@26 | 51 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@26 | 52 | else: |
paul@26 | 53 | return "%s()" % self.__class__.__name__ |
paul@17 | 54 | |
paul@55 | 55 | class StackRelativeInstruction(Instruction): |
paul@55 | 56 | |
paul@55 | 57 | "An instruction operating on the local value stack." |
paul@55 | 58 | |
paul@55 | 59 | def __repr__(self): |
paul@90 | 60 | return "%s(%r)" % (self.__class__.__name__, self.get_operand()) |
paul@90 | 61 | |
paul@90 | 62 | def get_operand(self): |
paul@90 | 63 | return self.attr.position |
paul@55 | 64 | |
paul@55 | 65 | SR = StackRelativeInstruction |
paul@55 | 66 | |
paul@55 | 67 | class AddressRelativeInstruction(Instruction): |
paul@55 | 68 | |
paul@55 | 69 | "An instruction accessing an object's attribute." |
paul@55 | 70 | |
paul@55 | 71 | def __repr__(self): |
paul@91 | 72 | position = self.get_operand() |
paul@91 | 73 | if position is not None: |
paul@91 | 74 | return "%s(%r) # %s" % (self.__class__.__name__, position, name(self.attr)) |
paul@91 | 75 | else: |
paul@91 | 76 | return "%s(%r)" % (self.__class__.__name__, name(self.attr)) |
paul@90 | 77 | |
paul@90 | 78 | def get_operand(self): |
paul@90 | 79 | return self.attr.position |
paul@82 | 80 | |
paul@82 | 81 | AR = AddressRelativeInstruction |
paul@82 | 82 | |
paul@82 | 83 | class AddressInstruction(Instruction): |
paul@82 | 84 | |
paul@82 | 85 | "An instruction loading an address directly." |
paul@82 | 86 | |
paul@82 | 87 | def __repr__(self): |
paul@90 | 88 | location, position, result = self.get_operands() |
paul@90 | 89 | if location is not None: |
paul@91 | 90 | return "%s(%r) # %r, %r (%s)" % (self.__class__.__name__, result, location, position, name(self.attr)) |
paul@91 | 91 | elif result is not None: |
paul@91 | 92 | return "%s(%r) # %s" % (self.__class__.__name__, result, name(self.attr)) |
paul@90 | 93 | else: |
paul@91 | 94 | return "%s(...) # %s" % (self.__class__.__name__, name(self.attr)) |
paul@90 | 95 | |
paul@90 | 96 | def get_operands(self): |
paul@82 | 97 | if isinstance(self.attr, Attr): |
paul@82 | 98 | position = self.attr.position |
paul@55 | 99 | location = self.attr.parent.location |
paul@56 | 100 | |
paul@56 | 101 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 102 | |
paul@56 | 103 | if location is not None and position is not None: |
paul@56 | 104 | result = location + position + 1 |
paul@56 | 105 | else: |
paul@56 | 106 | location = self.attr.parent.name |
paul@56 | 107 | position = self.attr.name |
paul@56 | 108 | result = None |
paul@90 | 109 | return location, position, result |
paul@90 | 110 | elif isinstance(self.attr, Label): |
paul@90 | 111 | return None, None, self.attr.location |
paul@82 | 112 | else: |
paul@90 | 113 | return None, None, self.attr.location |
paul@90 | 114 | |
paul@90 | 115 | def get_operand(self): |
paul@90 | 116 | return self.get_operands()[-1] |
paul@55 | 117 | |
paul@70 | 118 | Address = AddressInstruction |
paul@70 | 119 | |
paul@70 | 120 | class ImmediateInstruction(Instruction): |
paul@70 | 121 | |
paul@70 | 122 | "An instruction employing a constant." |
paul@70 | 123 | |
paul@70 | 124 | def __repr__(self): |
paul@70 | 125 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@70 | 126 | |
paul@90 | 127 | def get_operand(self): |
paul@90 | 128 | return self.attr |
paul@90 | 129 | |
paul@55 | 130 | Immediate = ImmediateInstruction |
paul@55 | 131 | |
paul@72 | 132 | # Mix-in classes for stack effects. |
paul@72 | 133 | |
paul@72 | 134 | class StackAdd: |
paul@72 | 135 | |
paul@72 | 136 | """ |
paul@72 | 137 | Indicate that the stack must grow to accommodate the result of this |
paul@72 | 138 | instruction. |
paul@72 | 139 | """ |
paul@72 | 140 | |
paul@72 | 141 | stack_usage = 1 |
paul@72 | 142 | |
paul@72 | 143 | class StackRemove: |
paul@72 | 144 | |
paul@72 | 145 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 146 | |
paul@72 | 147 | stack_usage = -1 |
paul@72 | 148 | |
paul@72 | 149 | class StackRemove2: |
paul@72 | 150 | |
paul@72 | 151 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 152 | |
paul@72 | 153 | stack_usage = -2 |
paul@72 | 154 | |
paul@21 | 155 | # Instructions operating on the value stack. |
paul@21 | 156 | |
paul@72 | 157 | class Duplicate(StackAdd, Instruction): "Duplicate the top of the stack." |
paul@72 | 158 | class Pop(StackRemove, Immediate): "Pop entries from the top of the stack." |
paul@21 | 159 | |
paul@86 | 160 | # Access to stored constant data. |
paul@86 | 161 | |
paul@86 | 162 | class LoadConst(StackAdd, Address): "Load the constant, class, function, module from the specified location." |
paul@86 | 163 | |
paul@21 | 164 | # Access within an invocation frame. |
paul@21 | 165 | |
paul@72 | 166 | class LoadName(StackAdd, SR): "Load the object from the given local attribute/variable." |
paul@72 | 167 | class StoreName(StackRemove, SR): "Store the object in the given local attribute/variable." |
paul@73 | 168 | class LoadTemp(Immediate): "Load the object from the given temporary location." |
paul@73 | 169 | class StoreTemp(Immediate): "Store the object in the given temporary location." |
paul@21 | 170 | |
paul@21 | 171 | # Access to address-relative data. |
paul@21 | 172 | |
paul@86 | 173 | class MakeObject(StackAdd, Instruction): "Make a new object. There isn't a complementary DropObject." |
paul@72 | 174 | class LoadAttr(AR): "Load the object from the given attribute." |
paul@72 | 175 | class StoreAttr(StackRemove2, AR): "Store an object in the given attribute." |
paul@72 | 176 | class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." |
paul@72 | 177 | class StoreAttrIndex(StackRemove2, Immediate): "Store an object in the attribute with the given index." |
paul@82 | 178 | class LoadAddress(StackAdd, Address): "Load the object from the given fixed attribute address." |
paul@82 | 179 | class StoreAddress(StackRemove, Address): "Store an object in the given fixed attribute address." |
paul@21 | 180 | |
paul@21 | 181 | # Access to invocation frames in preparation. |
paul@21 | 182 | |
paul@72 | 183 | class MakeFrame(Instruction): "Make a new invocation frame." |
paul@72 | 184 | class ReserveFrame(Immediate): "Reserve the given number of entries for the invocation frame." |
paul@72 | 185 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@90 | 186 | class StoreFrame(StackRemove, Immediate): "Store an argument at the given frame location." |
paul@72 | 187 | class StoreFrameIndex(StackRemove, Immediate): "Store an argument for the parameter with the given index." |
paul@72 | 188 | class CheckFrame(Instruction): "Check the invocation frame for the target." |
paul@90 | 189 | class JumpWithFrame(Instruction): "Jump, adopting the invocation frame, to the callable found on the stack." |
paul@21 | 190 | |
paul@21 | 191 | # Invocation-related instructions. |
paul@21 | 192 | |
paul@90 | 193 | class Jump(Address): "Jump unconditionally." |
paul@90 | 194 | class JumpIfFalse(Address): "Jump if the last evaluation gave a false result." |
paul@90 | 195 | class JumpIfTrue(Address): "Jump if the last evaluation gave a true result." |
paul@72 | 196 | class LoadCallable(Instruction): "Load the target of an invocation." |
paul@72 | 197 | class LoadContext(Instruction): "Load the context of an invocation." |
paul@72 | 198 | class CheckContext(Instruction): """Check the context of an invocation against the target, |
paul@72 | 199 | potentially discarding the context.""" |
paul@72 | 200 | class RaiseException(Instruction): "Raise an exception." |
paul@72 | 201 | class Return(Instruction): "Return a value from a subprogram." |
paul@72 | 202 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 203 | |
paul@76 | 204 | # General instructions. |
paul@76 | 205 | |
paul@76 | 206 | class TestIdentity(Instruction): "Test whether the two topmost stack values are identical." |
paul@76 | 207 | class TestIdentityAddress(Address): "Test whether the topmost stack value is identical to the given address." |
paul@76 | 208 | |
paul@17 | 209 | # vim: tabstop=4 expandtab shiftwidth=4 |