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