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@54 | 22 | from micropython.inspect import Const, Instance |
paul@54 | 23 | |
paul@17 | 24 | class Instruction: |
paul@17 | 25 | |
paul@17 | 26 | "A generic instruction." |
paul@17 | 27 | |
paul@72 | 28 | stack_usage = 0 |
paul@72 | 29 | |
paul@21 | 30 | def __init__(self, attr=None): |
paul@17 | 31 | self.attr = attr |
paul@17 | 32 | |
paul@17 | 33 | def __repr__(self): |
paul@26 | 34 | if self.attr is not None: |
paul@26 | 35 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@26 | 36 | else: |
paul@26 | 37 | return "%s()" % self.__class__.__name__ |
paul@17 | 38 | |
paul@55 | 39 | class StackRelativeInstruction(Instruction): |
paul@55 | 40 | |
paul@55 | 41 | "An instruction operating on the local value stack." |
paul@55 | 42 | |
paul@55 | 43 | def __repr__(self): |
paul@55 | 44 | return "%s(%r)" % (self.__class__.__name__, self.attr.position) |
paul@55 | 45 | |
paul@55 | 46 | SR = StackRelativeInstruction |
paul@55 | 47 | |
paul@55 | 48 | class AddressRelativeInstruction(Instruction): |
paul@55 | 49 | |
paul@55 | 50 | "An instruction accessing an object's attribute." |
paul@55 | 51 | |
paul@55 | 52 | def __repr__(self): |
paul@55 | 53 | position = self.attr.position |
paul@55 | 54 | if isinstance(self.attr.parent, Instance): |
paul@55 | 55 | location = "instance" |
paul@55 | 56 | result = position |
paul@55 | 57 | else: |
paul@55 | 58 | location = self.attr.parent.location |
paul@56 | 59 | |
paul@56 | 60 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 61 | |
paul@56 | 62 | if location is not None and position is not None: |
paul@56 | 63 | result = location + position + 1 |
paul@56 | 64 | else: |
paul@56 | 65 | location = self.attr.parent.name |
paul@56 | 66 | position = self.attr.name |
paul@56 | 67 | result = None |
paul@55 | 68 | return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) |
paul@55 | 69 | |
paul@55 | 70 | AR = AddressRelativeInstruction |
paul@55 | 71 | |
paul@70 | 72 | class AddressInstruction(Instruction): |
paul@55 | 73 | |
paul@55 | 74 | "An instruction loading an address directly." |
paul@55 | 75 | |
paul@55 | 76 | def __repr__(self): |
paul@55 | 77 | return "%s(%r)" % (self.__class__.__name__, self.attr.location) |
paul@55 | 78 | |
paul@70 | 79 | Address = AddressInstruction |
paul@70 | 80 | |
paul@70 | 81 | class ImmediateInstruction(Instruction): |
paul@70 | 82 | |
paul@70 | 83 | "An instruction employing a constant." |
paul@70 | 84 | |
paul@70 | 85 | def __repr__(self): |
paul@70 | 86 | return "%s(%r)" % (self.__class__.__name__, self.attr) |
paul@70 | 87 | |
paul@55 | 88 | Immediate = ImmediateInstruction |
paul@55 | 89 | |
paul@72 | 90 | # Mix-in classes for stack effects. |
paul@72 | 91 | |
paul@72 | 92 | class StackAdd: |
paul@72 | 93 | |
paul@72 | 94 | """ |
paul@72 | 95 | Indicate that the stack must grow to accommodate the result of this |
paul@72 | 96 | instruction. |
paul@72 | 97 | """ |
paul@72 | 98 | |
paul@72 | 99 | stack_usage = 1 |
paul@72 | 100 | |
paul@72 | 101 | class StackRemove: |
paul@72 | 102 | |
paul@72 | 103 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 104 | |
paul@72 | 105 | stack_usage = -1 |
paul@72 | 106 | |
paul@72 | 107 | class StackRemove2: |
paul@72 | 108 | |
paul@72 | 109 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 110 | |
paul@72 | 111 | stack_usage = -2 |
paul@72 | 112 | |
paul@21 | 113 | # Instructions operating on the value stack. |
paul@21 | 114 | |
paul@72 | 115 | class LoadConst(StackAdd, Address): "Load the constant from the specified location." |
paul@72 | 116 | class Duplicate(StackAdd, Instruction): "Duplicate the top of the stack." |
paul@72 | 117 | class Pop(StackRemove, Immediate): "Pop entries from the top of the stack." |
paul@21 | 118 | |
paul@21 | 119 | # Access within an invocation frame. |
paul@21 | 120 | |
paul@72 | 121 | class LoadName(StackAdd, SR): "Load the object from the given local attribute/variable." |
paul@72 | 122 | class StoreName(StackRemove, SR): "Store the object in the given local attribute/variable." |
paul@73 | 123 | class LoadTemp(Immediate): "Load the object from the given temporary location." |
paul@73 | 124 | class StoreTemp(Immediate): "Store the object in the given temporary location." |
paul@21 | 125 | |
paul@21 | 126 | # Access to address-relative data. |
paul@21 | 127 | |
paul@72 | 128 | class MakeObject(StackAdd, Instruction): "Make a new object." |
paul@72 | 129 | # ... DropObject not defined: Assume garbage collection. |
paul@72 | 130 | class LoadAttr(AR): "Load the object from the given attribute." |
paul@72 | 131 | class StoreAttr(StackRemove2, AR): "Store an object in the given attribute." |
paul@72 | 132 | class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." |
paul@72 | 133 | class StoreAttrIndex(StackRemove2, Immediate): "Store an object in the attribute with the given index." |
paul@72 | 134 | class LoadAddress(StackAdd, AR): "Load the object from the given fixed attribute address." |
paul@72 | 135 | class StoreAddress(StackRemove, AR): "Store an object in the given fixed attribute address." |
paul@21 | 136 | |
paul@21 | 137 | # Access to invocation frames in preparation. |
paul@21 | 138 | |
paul@72 | 139 | class MakeFrame(Instruction): "Make a new invocation frame." |
paul@72 | 140 | class ReserveFrame(Immediate): "Reserve the given number of entries for the invocation frame." |
paul@72 | 141 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@72 | 142 | class StoreFrame(StackRemove, Instruction): "Store an argument at the given frame location." |
paul@72 | 143 | class StoreFrameIndex(StackRemove, Immediate): "Store an argument for the parameter with the given index." |
paul@72 | 144 | class CheckFrame(Instruction): "Check the invocation frame for the target." |
paul@72 | 145 | class JumpWithFrame(Instruction): "Jump, adopting the invocation frame." |
paul@21 | 146 | |
paul@21 | 147 | # Invocation-related instructions. |
paul@21 | 148 | |
paul@72 | 149 | class Jump(Instruction): "Jump unconditionally." |
paul@72 | 150 | class JumpIfFalse(Instruction): "Jump if the last evaluation gave a false result." |
paul@72 | 151 | class JumpIfTrue(Instruction): "Jump if the last evaluation gave a true result." |
paul@72 | 152 | class LoadCallable(Instruction): "Load the target of an invocation." |
paul@72 | 153 | class LoadContext(Instruction): "Load the context of an invocation." |
paul@72 | 154 | class CheckContext(Instruction): """Check the context of an invocation against the target, |
paul@72 | 155 | potentially discarding the context.""" |
paul@72 | 156 | class RaiseException(Instruction): "Raise an exception." |
paul@72 | 157 | class Return(Instruction): "Return a value from a subprogram." |
paul@72 | 158 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 159 | |
paul@76 | 160 | # General instructions. |
paul@76 | 161 | |
paul@76 | 162 | class TestIdentity(Instruction): "Test whether the two topmost stack values are identical." |
paul@76 | 163 | class TestIdentityAddress(Address): "Test whether the topmost stack value is identical to the given address." |
paul@76 | 164 | |
paul@17 | 165 | # vim: tabstop=4 expandtab shiftwidth=4 |