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