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@98 | 44 | stack_storage = [] |
paul@98 | 45 | stack_access = [] |
paul@72 | 46 | |
paul@87 | 47 | def __init__(self, attr=None): |
paul@17 | 48 | self.attr = attr |
paul@98 | 49 | self.accesses = [op(-1 - n) for n, op in enumerate(self.stack_access)] |
paul@98 | 50 | self.results = [op(n) for n, op in enumerate(self.stack_storage)] |
paul@95 | 51 | |
paul@95 | 52 | def copy(self): |
paul@95 | 53 | return self.__class__(self.attr) |
paul@95 | 54 | |
paul@95 | 55 | def remove_results(self): |
paul@95 | 56 | |
paul@95 | 57 | "Remove the stack side-effects for instructions replacing stack operations." |
paul@95 | 58 | |
paul@95 | 59 | self.results = [] |
paul@93 | 60 | |
paul@93 | 61 | def fix_stack(self, level): |
paul@95 | 62 | |
paul@95 | 63 | """ |
paul@95 | 64 | Use the given 'level' to fix the details of this instruction's internal |
paul@95 | 65 | stack operations. |
paul@95 | 66 | """ |
paul@95 | 67 | |
paul@95 | 68 | effect = 0 |
paul@95 | 69 | for stack_op in self.accesses: |
paul@95 | 70 | stack_op.fix_stack(level) |
paul@95 | 71 | effect += stack_op.get_effect() |
paul@93 | 72 | |
paul@95 | 73 | level += effect |
paul@95 | 74 | for stack_op in self.results: |
paul@95 | 75 | stack_op.fix_stack(level) |
paul@95 | 76 | |
paul@95 | 77 | def get_effect(self): |
paul@95 | 78 | effect = 0 |
paul@95 | 79 | for stack_op in self.accesses + self.results: |
paul@95 | 80 | effect += stack_op.get_effect() |
paul@95 | 81 | return effect |
paul@95 | 82 | |
paul@95 | 83 | def show_stack_ops(self): |
paul@95 | 84 | return "%s%s" % ( |
paul@95 | 85 | self.accesses and (" <- %s" % self.accesses) or "", |
paul@95 | 86 | self.results and (" -> %s" % self.results) or "" |
paul@95 | 87 | ) |
paul@17 | 88 | |
paul@17 | 89 | def __repr__(self): |
paul@26 | 90 | if self.attr is not None: |
paul@95 | 91 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_stack_ops()) |
paul@26 | 92 | else: |
paul@95 | 93 | return "%s()%s" % (self.__class__.__name__, self.show_stack_ops()) |
paul@17 | 94 | |
paul@55 | 95 | class StackRelativeInstruction(Instruction): |
paul@55 | 96 | |
paul@55 | 97 | "An instruction operating on the local value stack." |
paul@55 | 98 | |
paul@55 | 99 | def __repr__(self): |
paul@95 | 100 | return "%s(%r)%s" % (self.__class__.__name__, self.get_operand(), self.show_stack_ops()) |
paul@90 | 101 | |
paul@90 | 102 | def get_operand(self): |
paul@90 | 103 | return self.attr.position |
paul@55 | 104 | |
paul@55 | 105 | SR = StackRelativeInstruction |
paul@55 | 106 | |
paul@55 | 107 | class AddressRelativeInstruction(Instruction): |
paul@55 | 108 | |
paul@55 | 109 | "An instruction accessing an object's attribute." |
paul@55 | 110 | |
paul@55 | 111 | def __repr__(self): |
paul@91 | 112 | position = self.get_operand() |
paul@91 | 113 | if position is not None: |
paul@95 | 114 | return "%s(%r)%s # %s" % (self.__class__.__name__, position, self.show_stack_ops(), name(self.attr)) |
paul@91 | 115 | else: |
paul@95 | 116 | return "%s(%r)%s" % (self.__class__.__name__, self.show_stack_ops(), name(self.attr)) |
paul@90 | 117 | |
paul@90 | 118 | def get_operand(self): |
paul@90 | 119 | return self.attr.position |
paul@82 | 120 | |
paul@82 | 121 | AR = AddressRelativeInstruction |
paul@82 | 122 | |
paul@82 | 123 | class AddressInstruction(Instruction): |
paul@82 | 124 | |
paul@82 | 125 | "An instruction loading an address directly." |
paul@82 | 126 | |
paul@82 | 127 | def __repr__(self): |
paul@90 | 128 | location, position, result = self.get_operands() |
paul@90 | 129 | if location is not None: |
paul@95 | 130 | return "%s(%r)%s # %r, %r (%s)" % ( |
paul@95 | 131 | self.__class__.__name__, result, self.show_stack_ops(), location, position, name(self.attr)) |
paul@91 | 132 | elif result is not None: |
paul@95 | 133 | return "%s(%r)%s # %s" % ( |
paul@95 | 134 | self.__class__.__name__, result, self.show_stack_ops(), name(self.attr)) |
paul@90 | 135 | else: |
paul@95 | 136 | return "%s(...)%s # %s" % ( |
paul@95 | 137 | self.__class__.__name__, self.show_stack_ops(), name(self.attr)) |
paul@90 | 138 | |
paul@90 | 139 | def get_operands(self): |
paul@82 | 140 | if isinstance(self.attr, Attr): |
paul@82 | 141 | position = self.attr.position |
paul@55 | 142 | location = self.attr.parent.location |
paul@56 | 143 | |
paul@56 | 144 | # NOTE: Unpositioned attributes are handled here. |
paul@56 | 145 | |
paul@56 | 146 | if location is not None and position is not None: |
paul@56 | 147 | result = location + position + 1 |
paul@56 | 148 | else: |
paul@56 | 149 | location = self.attr.parent.name |
paul@56 | 150 | position = self.attr.name |
paul@56 | 151 | result = None |
paul@90 | 152 | return location, position, result |
paul@90 | 153 | elif isinstance(self.attr, Label): |
paul@90 | 154 | return None, None, self.attr.location |
paul@82 | 155 | else: |
paul@90 | 156 | return None, None, self.attr.location |
paul@90 | 157 | |
paul@90 | 158 | def get_operand(self): |
paul@90 | 159 | return self.get_operands()[-1] |
paul@55 | 160 | |
paul@70 | 161 | Address = AddressInstruction |
paul@70 | 162 | |
paul@70 | 163 | class ImmediateInstruction(Instruction): |
paul@70 | 164 | |
paul@70 | 165 | "An instruction employing a constant." |
paul@70 | 166 | |
paul@70 | 167 | def __repr__(self): |
paul@95 | 168 | return "%s(%r)%s" % (self.__class__.__name__, self.attr, self.show_stack_ops()) |
paul@70 | 169 | |
paul@90 | 170 | def get_operand(self): |
paul@90 | 171 | return self.attr |
paul@90 | 172 | |
paul@55 | 173 | Immediate = ImmediateInstruction |
paul@55 | 174 | |
paul@98 | 175 | # Internal stack and frame operations for instructions. |
paul@98 | 176 | |
paul@98 | 177 | class StackOp: |
paul@98 | 178 | |
paul@98 | 179 | "A generic stack operation." |
paul@98 | 180 | |
paul@98 | 181 | def __init__(self, n): |
paul@98 | 182 | self.n = n |
paul@98 | 183 | self.level = None |
paul@98 | 184 | |
paul@98 | 185 | def fix_stack(self, level): |
paul@98 | 186 | self.level = self.n + level |
paul@98 | 187 | |
paul@98 | 188 | def __repr__(self): |
paul@98 | 189 | return "%s(%s)" % (self.__class__.__name__, self.level == 0 and "0" or self.level or self.n) |
paul@98 | 190 | |
paul@98 | 191 | class StackPull(StackOp): |
paul@98 | 192 | |
paul@98 | 193 | "Load a value from the stack." |
paul@98 | 194 | |
paul@98 | 195 | def get_effect(self): |
paul@98 | 196 | return -1 |
paul@98 | 197 | |
paul@98 | 198 | class StackPush(StackOp): |
paul@98 | 199 | |
paul@98 | 200 | "Save a value onto the stack." |
paul@98 | 201 | |
paul@98 | 202 | def get_effect(self): |
paul@98 | 203 | return 1 |
paul@98 | 204 | |
paul@98 | 205 | class StackLoad(StackOp): |
paul@98 | 206 | |
paul@98 | 207 | "Load a value from the stack." |
paul@98 | 208 | |
paul@98 | 209 | def get_effect(self): |
paul@98 | 210 | return 0 |
paul@98 | 211 | |
paul@72 | 212 | # Mix-in classes for stack effects. |
paul@72 | 213 | |
paul@72 | 214 | class StackAdd: |
paul@72 | 215 | |
paul@72 | 216 | """ |
paul@72 | 217 | Indicate that the stack must grow to accommodate the result of this |
paul@72 | 218 | instruction. |
paul@72 | 219 | """ |
paul@72 | 220 | |
paul@98 | 221 | stack_storage = [StackPush] |
paul@72 | 222 | |
paul@72 | 223 | class StackRemove: |
paul@72 | 224 | |
paul@72 | 225 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 226 | |
paul@98 | 227 | stack_access = [StackPull] |
paul@72 | 228 | |
paul@72 | 229 | class StackRemove2: |
paul@72 | 230 | |
paul@72 | 231 | "Indicate that the stack must shrink as an effect of this instruction." |
paul@72 | 232 | |
paul@98 | 233 | stack_access = [StackPull, StackPull] |
paul@72 | 234 | |
paul@98 | 235 | class StackReplace(StackAdd, StackRemove): |
paul@95 | 236 | |
paul@95 | 237 | """ |
paul@95 | 238 | Indicate that the stack remains at the same level due to the replacement of |
paul@95 | 239 | the topmost element. |
paul@95 | 240 | """ |
paul@95 | 241 | |
paul@98 | 242 | pass |
paul@98 | 243 | |
paul@98 | 244 | class StackInspect: |
paul@95 | 245 | |
paul@98 | 246 | "Indicate that the stack is inspected but unchanged by this instruction." |
paul@21 | 247 | |
paul@98 | 248 | stack_access = [StackLoad] |
paul@21 | 249 | |
paul@86 | 250 | # Access to stored constant data. |
paul@86 | 251 | |
paul@86 | 252 | class LoadConst(StackAdd, Address): "Load the constant, class, function, module from the specified location." |
paul@86 | 253 | |
paul@21 | 254 | # Access within an invocation frame. |
paul@21 | 255 | |
paul@72 | 256 | class LoadName(StackAdd, SR): "Load the object from the given local attribute/variable." |
paul@72 | 257 | class StoreName(StackRemove, SR): "Store the object in the given local attribute/variable." |
paul@95 | 258 | class LoadTemp(StackAdd, Immediate): "Load the object from the given temporary location." |
paul@93 | 259 | class StoreTemp(StackRemove, Immediate): "Store the object in the given temporary location." |
paul@21 | 260 | |
paul@98 | 261 | # Access to static data. |
paul@98 | 262 | |
paul@98 | 263 | class LoadAddress(StackAdd, Address): "Load the object from the given fixed attribute address." |
paul@98 | 264 | class StoreAddress(StackRemove, Address): "Store an object in the given fixed attribute address." |
paul@98 | 265 | class LoadAddressContext(StackReplace, Address):"Load the object from the given fixed attribute address, changing the context." |
paul@98 | 266 | class StoreAddressContext(StackRemove2, Address):"Store an object in the given fixed attribute address, changing the context." |
paul@98 | 267 | class MakeObject(StackAdd, Instruction): "Make a new object. There isn't a complementary DropObject." |
paul@98 | 268 | |
paul@21 | 269 | # Access to address-relative data. |
paul@21 | 270 | |
paul@95 | 271 | class LoadAttr(StackReplace, AR): "Load the object from the given attribute." |
paul@72 | 272 | class StoreAttr(StackRemove2, AR): "Store an object in the given attribute." |
paul@95 | 273 | class LoadAttrIndex(StackReplace, Immediate): "Load the object for the attribute with the given index." |
paul@72 | 274 | class StoreAttrIndex(StackRemove2, Immediate): "Store an object in the attribute with the given index." |
paul@21 | 275 | |
paul@21 | 276 | # Access to invocation frames in preparation. |
paul@21 | 277 | |
paul@72 | 278 | class MakeFrame(Instruction): "Make a new invocation frame." |
paul@98 | 279 | class DropFrame(Instruction): "Drop an invocation frame." |
paul@98 | 280 | class StoreFrame(StackRemove, Immediate): "Store an argument for the parameter with the given position." |
paul@72 | 281 | class StoreFrameIndex(StackRemove, Immediate): "Store an argument for the parameter with the given index." |
paul@98 | 282 | class LoadCallable(StackInspect, Instruction): "Load the target of an invocation." |
paul@97 | 283 | class LoadContext(StackReplace, Instruction): "Load the context of an invocation." |
paul@98 | 284 | class CheckFrame(Instruction): "Check the invocation frame and context for the target." |
paul@98 | 285 | class CheckSelf(StackAdd, Instruction): "Check the first argument of an invocation against the target." |
paul@97 | 286 | |
paul@97 | 287 | # Invocation-related instructions, using a special result "register". |
paul@97 | 288 | |
paul@98 | 289 | class JumpWithFrame(StackRemove, Instruction): "Jump, adopting the invocation frame, to the callable found on the stack." |
paul@97 | 290 | class Return(StackRemove, Instruction): "Return a value from a subprogram." |
paul@97 | 291 | class LoadResult(StackAdd, Instruction): "Load a returned value." |
paul@21 | 292 | |
paul@97 | 293 | # Branch-related instructions. |
paul@21 | 294 | |
paul@90 | 295 | class Jump(Address): "Jump unconditionally." |
paul@98 | 296 | class JumpIfFalse(StackRemove, Address): "Jump if the last evaluation gave a false result." |
paul@98 | 297 | class JumpIfTrue(StackRemove, Address): "Jump if the last evaluation gave a true result." |
paul@97 | 298 | |
paul@97 | 299 | # Exception-related instructions, using a special exception "register". |
paul@97 | 300 | |
paul@97 | 301 | class LoadException(StackAdd, Instruction): "Load the raised exception." |
paul@95 | 302 | class RaiseException(StackRemove, Instruction): "Raise an exception." |
paul@72 | 303 | class CheckException(Instruction): "Check the raised exception against another." |
paul@17 | 304 | |
paul@76 | 305 | # General instructions. |
paul@76 | 306 | |
paul@76 | 307 | class TestIdentity(Instruction): "Test whether the two topmost stack values are identical." |
paul@76 | 308 | class TestIdentityAddress(Address): "Test whether the topmost stack value is identical to the given address." |
paul@76 | 309 | |
paul@17 | 310 | # vim: tabstop=4 expandtab shiftwidth=4 |