paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@17 | 5 | |
paul@39 | 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@154 | 22 | from micropython.opt import Optimiser |
paul@83 | 23 | from micropython.common import * |
paul@83 | 24 | from micropython.data import * |
paul@17 | 25 | from micropython.rsvp import * |
paul@17 | 26 | import compiler.ast |
paul@17 | 27 | from compiler.visitor import ASTVisitor |
paul@17 | 28 | |
paul@17 | 29 | # Program visitors. |
paul@17 | 30 | |
paul@17 | 31 | class Translation(ASTVisitor): |
paul@17 | 32 | |
paul@17 | 33 | "A translated module." |
paul@17 | 34 | |
paul@122 | 35 | # Attribute access instructions, for use with the appropriate handlers. |
paul@122 | 36 | |
paul@98 | 37 | attribute_load_instructions = (LoadAddress, LoadAddressContext, LoadAttr, LoadAttrIndex) |
paul@106 | 38 | attribute_store_instructions = (None, None, StoreAttr, StoreAttrIndex) |
paul@122 | 39 | |
paul@122 | 40 | # Name access instructions, for use with the appropriate handlers. |
paul@122 | 41 | |
paul@106 | 42 | name_load_instructions = (LoadName, LoadAddress) |
paul@106 | 43 | name_store_instructions = (StoreName, StoreAddress) |
paul@98 | 44 | |
paul@72 | 45 | def __init__(self, module, importer, optimisations=None): |
paul@18 | 46 | |
paul@21 | 47 | """ |
paul@72 | 48 | Initialise the translation with an inspected 'module', the 'importer' |
paul@154 | 49 | and optional 'optimisations'. |
paul@21 | 50 | """ |
paul@18 | 51 | |
paul@17 | 52 | ASTVisitor.__init__(self) |
paul@17 | 53 | self.visitor = self |
paul@17 | 54 | self.module = module |
paul@72 | 55 | |
paul@72 | 56 | # Global program dependencies. |
paul@72 | 57 | |
paul@72 | 58 | self.importer = importer |
paul@72 | 59 | self.objtable = self.importer.get_object_table() |
paul@72 | 60 | self.paramtable = self.importer.get_parameter_table() |
paul@72 | 61 | self.builtins = self.importer.modules.get("__builtins__") |
paul@61 | 62 | |
paul@140 | 63 | # Optimisation. |
paul@140 | 64 | |
paul@140 | 65 | self.optimiser = Optimiser(self, optimisations) |
paul@62 | 66 | |
paul@61 | 67 | # The current unit being translated. |
paul@61 | 68 | |
paul@17 | 69 | self.unit = None |
paul@17 | 70 | |
paul@103 | 71 | # The temporary storage used by the current assignment expression. |
paul@103 | 72 | |
paul@139 | 73 | self.expr_temp = [] |
paul@103 | 74 | |
paul@19 | 75 | # Wiring within the code. |
paul@19 | 76 | |
paul@19 | 77 | self.labels = {} |
paul@19 | 78 | self.label_number = 0 |
paul@41 | 79 | self.loop_labels = [] |
paul@57 | 80 | self.exception_labels = [] |
paul@41 | 81 | |
paul@72 | 82 | # The code itself. This is limited to the code for a particular block |
paul@117 | 83 | # being processed. Also retained is information about temporary values |
paul@117 | 84 | # and instructions which construct frames. |
paul@41 | 85 | |
paul@19 | 86 | self.code = None |
paul@114 | 87 | self.temp_positions = set() |
paul@116 | 88 | self.max_temp_position = -1 |
paul@117 | 89 | self.frame_makers = [] |
paul@19 | 90 | |
paul@95 | 91 | def __repr__(self): |
paul@95 | 92 | return "Translation(%r)" % self.module |
paul@72 | 93 | |
paul@139 | 94 | def get_module_code(self, final=0): |
paul@139 | 95 | |
paul@139 | 96 | """ |
paul@139 | 97 | Return the top-level module code including finalising code if 'final' is |
paul@139 | 98 | set to a true value. |
paul@139 | 99 | """ |
paul@17 | 100 | |
paul@17 | 101 | self.unit = self.module |
paul@19 | 102 | self.code = [] |
paul@114 | 103 | self.temp_positions = set() |
paul@133 | 104 | self.max_temp_position = -1 |
paul@73 | 105 | |
paul@44 | 106 | if self.module.module is not None: |
paul@44 | 107 | self.dispatch(self.module.module) |
paul@73 | 108 | |
paul@139 | 109 | # Finish off the translated program if appropriate. |
paul@139 | 110 | |
paul@139 | 111 | if final: |
paul@139 | 112 | self.new_op(Return()) |
paul@139 | 113 | |
paul@116 | 114 | self.unit.temp_usage = self.max_temp_position + 1 |
paul@19 | 115 | return self.code |
paul@17 | 116 | |
paul@17 | 117 | def get_code(self, unit): |
paul@17 | 118 | |
paul@17 | 119 | "Return the code for the given 'unit'." |
paul@17 | 120 | |
paul@17 | 121 | self.unit = unit |
paul@19 | 122 | self.code = [] |
paul@114 | 123 | self.temp_positions = set() |
paul@133 | 124 | self.max_temp_position = -1 |
paul@73 | 125 | |
paul@91 | 126 | if unit.astnode is not None: |
paul@91 | 127 | self.dispatch(unit.astnode) |
paul@73 | 128 | |
paul@116 | 129 | self.unit.temp_usage = self.max_temp_position + 1 |
paul@19 | 130 | return self.code |
paul@17 | 131 | |
paul@137 | 132 | def get_instantiator_code(self, cls): |
paul@137 | 133 | |
paul@137 | 134 | "Return the code for the given class 'cls'." |
paul@137 | 135 | |
paul@137 | 136 | self.unit = cls.get_instantiator() |
paul@137 | 137 | self.code = [] |
paul@137 | 138 | self.temp_positions = set() |
paul@137 | 139 | self.max_temp_position = -1 |
paul@137 | 140 | |
paul@137 | 141 | init_method = cls.get_init_method() |
paul@137 | 142 | |
paul@137 | 143 | # Convert this frame back to being an invocation frame. |
paul@137 | 144 | |
paul@137 | 145 | self.new_op(RecoverFrame()) |
paul@137 | 146 | |
paul@137 | 147 | # Fix the current frame to include a new storage slot at the beginning. |
paul@137 | 148 | |
paul@137 | 149 | self.new_op(AdjustFrame(-1)) |
paul@137 | 150 | |
paul@137 | 151 | # Make an object. |
paul@137 | 152 | |
paul@138 | 153 | self.make_object(cls, len(cls.instance_attributes())) |
paul@137 | 154 | self.new_op(StoreFrame(0)) |
paul@137 | 155 | |
paul@137 | 156 | # Invoke the appropriate initialiser. |
paul@137 | 157 | |
paul@138 | 158 | self.new_op(LoadConst(init_method)) |
paul@137 | 159 | self.new_op(LoadCallable()) |
paul@137 | 160 | self.new_op(JumpWithFrame()) |
paul@137 | 161 | |
paul@137 | 162 | # Store the object as the result. |
paul@137 | 163 | |
paul@137 | 164 | self.new_op(LoadName(init_method.all_locals()["self"])) # load the context in the invocation frame |
paul@137 | 165 | self.new_op(StoreResult()) |
paul@137 | 166 | self.new_op(Return()) |
paul@137 | 167 | |
paul@137 | 168 | return self.code |
paul@137 | 169 | |
paul@138 | 170 | # Allocation-related methods. |
paul@138 | 171 | |
paul@138 | 172 | def make_object(self, cls, n): |
paul@138 | 173 | |
paul@138 | 174 | """ |
paul@138 | 175 | Request a new object with the given class 'cls' and with 'n' attributes. |
paul@138 | 176 | """ |
paul@138 | 177 | |
paul@138 | 178 | # NOTE: Object headers are one location. |
paul@138 | 179 | |
paul@138 | 180 | self.new_op(LoadConst(cls)) |
paul@138 | 181 | self.new_op(MakeObject(n + 1)) |
paul@138 | 182 | |
paul@101 | 183 | # Name-related methods. |
paul@101 | 184 | |
paul@17 | 185 | def get_scope(self, name): |
paul@99 | 186 | |
paul@99 | 187 | "Return the scope for the given 'name'." |
paul@99 | 188 | |
paul@17 | 189 | if self.unit.has_key(name): |
paul@17 | 190 | return "local" |
paul@17 | 191 | elif self.module.has_key(name): |
paul@17 | 192 | return "global" |
paul@17 | 193 | else: |
paul@17 | 194 | return "builtins" |
paul@17 | 195 | |
paul@101 | 196 | def load_builtin(self, name, node): |
paul@95 | 197 | |
paul@101 | 198 | "Generate an instruction loading 'name' for the given 'node'." |
paul@95 | 199 | |
paul@101 | 200 | self.new_op(LoadAddress(self.get_builtin(name, node))) |
paul@95 | 201 | |
paul@138 | 202 | def get_builtin_class(self, name, node): |
paul@138 | 203 | return self.get_builtin(name, node).value |
paul@138 | 204 | |
paul@101 | 205 | def get_builtin(self, name, node): |
paul@95 | 206 | |
paul@101 | 207 | """ |
paul@101 | 208 | Return the built-in module definition for the given 'name', used by the |
paul@101 | 209 | given 'node'. |
paul@101 | 210 | """ |
paul@95 | 211 | |
paul@101 | 212 | if self.builtins is not None: |
paul@101 | 213 | try: |
paul@101 | 214 | return self.builtins[name] |
paul@101 | 215 | except KeyError: |
paul@101 | 216 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@101 | 217 | else: |
paul@101 | 218 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@97 | 219 | |
paul@101 | 220 | # Code feature methods. |
paul@97 | 221 | |
paul@19 | 222 | def new_label(self): |
paul@44 | 223 | |
paul@44 | 224 | "Return a new label object for use with set_label." |
paul@44 | 225 | |
paul@19 | 226 | number = self.label_number |
paul@19 | 227 | label = Label(number) |
paul@19 | 228 | self.labels[label] = label |
paul@19 | 229 | self.label_number += 1 |
paul@19 | 230 | return label |
paul@19 | 231 | |
paul@19 | 232 | def set_label(self, label): |
paul@19 | 233 | |
paul@19 | 234 | """ |
paul@19 | 235 | Set the location of 'label' to that within the entire image: the |
paul@19 | 236 | location within the code combined with location of the code unit. |
paul@19 | 237 | """ |
paul@19 | 238 | |
paul@19 | 239 | label.location = len(self.code) + self.unit.code_location |
paul@19 | 240 | |
paul@57 | 241 | def get_loop_labels(self): |
paul@57 | 242 | return self.loop_labels[-1] |
paul@57 | 243 | |
paul@57 | 244 | def add_loop_labels(self, next_label, exit_label): |
paul@57 | 245 | self.loop_labels.append((next_label, exit_label)) |
paul@57 | 246 | |
paul@57 | 247 | def drop_loop_labels(self): |
paul@57 | 248 | self.loop_labels.pop() |
paul@57 | 249 | |
paul@57 | 250 | def get_exception_labels(self): |
paul@57 | 251 | return self.exception_labels[-1] |
paul@57 | 252 | |
paul@57 | 253 | def add_exception_labels(self, handler_label, exit_label): |
paul@57 | 254 | self.exception_labels.append((handler_label, exit_label)) |
paul@57 | 255 | |
paul@57 | 256 | def drop_exception_labels(self): |
paul@57 | 257 | self.exception_labels.pop() |
paul@57 | 258 | |
paul@104 | 259 | # Assignment expression values. |
paul@104 | 260 | |
paul@143 | 261 | def record_value(self, immediate=1): |
paul@143 | 262 | if immediate: |
paul@143 | 263 | temp = self.optimiser.optimise_temp_storage() |
paul@143 | 264 | else: |
paul@143 | 265 | temp = self.get_temp() |
paul@143 | 266 | self.expr_temp.append(temp) |
paul@104 | 267 | |
paul@104 | 268 | def discard_value(self): |
paul@139 | 269 | self.discard_temp(self.expr_temp.pop()) |
paul@104 | 270 | |
paul@104 | 271 | def set_source(self): |
paul@154 | 272 | self.optimiser.set_source(self.expr_temp[-1]) |
paul@104 | 273 | |
paul@104 | 274 | # Optimise away constant storage if appropriate. |
paul@104 | 275 | |
paul@140 | 276 | self.optimiser.optimise_constant_storage() |
paul@104 | 277 | |
paul@104 | 278 | # Temporary storage administration. |
paul@104 | 279 | |
paul@101 | 280 | def get_temp(self): |
paul@101 | 281 | |
paul@101 | 282 | """ |
paul@101 | 283 | Add a temporary storage instruction for the current value and return a |
paul@101 | 284 | sequence of access instructions. |
paul@101 | 285 | """ |
paul@101 | 286 | |
paul@130 | 287 | position_in_frame = self.reserve_temp() |
paul@117 | 288 | self.new_op(StoreTemp(position_in_frame)) |
paul@117 | 289 | return LoadTemp(position_in_frame) |
paul@101 | 290 | |
paul@152 | 291 | def reserve_temp(self, temp_position=None): |
paul@152 | 292 | if temp_position is not None: |
paul@152 | 293 | pass |
paul@152 | 294 | elif not self.temp_positions: |
paul@114 | 295 | temp_position = 0 |
paul@114 | 296 | else: |
paul@139 | 297 | temp_position = max(self.temp_positions) + 1 |
paul@114 | 298 | self.temp_positions.add(temp_position) |
paul@116 | 299 | self.max_temp_position = max(self.max_temp_position, temp_position) |
paul@133 | 300 | return self.unit.all_local_usage + temp_position # position in frame |
paul@73 | 301 | |
paul@152 | 302 | def ensure_temp(self, instruction=None): |
paul@152 | 303 | if isinstance(instruction, LoadTemp): |
paul@152 | 304 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@152 | 305 | self.reserve_temp(temp_position) |
paul@152 | 306 | |
paul@114 | 307 | def discard_temp(self, instruction=None): |
paul@102 | 308 | if isinstance(instruction, LoadTemp): |
paul@133 | 309 | temp_position = instruction.attr - self.unit.all_local_usage |
paul@130 | 310 | self.free_temp(temp_position) |
paul@130 | 311 | |
paul@130 | 312 | def free_temp(self, temp_position): |
paul@130 | 313 | if temp_position in self.temp_positions: |
paul@130 | 314 | self.temp_positions.remove(temp_position) |
paul@73 | 315 | |
paul@137 | 316 | def set_frame_usage(self, node, extend): |
paul@144 | 317 | ntemp = self.max_temp_position + 1 |
paul@144 | 318 | extend.attr = ntemp + node.unit.local_usage # NOTE: See get_code for similar code. |
paul@137 | 319 | |
paul@95 | 320 | # Code writing methods. |
paul@95 | 321 | |
paul@19 | 322 | def new_op(self, op): |
paul@44 | 323 | |
paul@44 | 324 | "Add 'op' to the generated code." |
paul@44 | 325 | |
paul@103 | 326 | # Optimise load operations employed by this instruction. |
paul@103 | 327 | |
paul@140 | 328 | self.optimiser.optimise_load_operations(op) |
paul@140 | 329 | if self.optimiser.optimise_away_no_operations(op): |
paul@113 | 330 | return |
paul@103 | 331 | |
paul@19 | 332 | self.code.append(op) |
paul@154 | 333 | self.optimiser.set_new(op) |
paul@122 | 334 | |
paul@101 | 335 | def remove_op(self): |
paul@95 | 336 | |
paul@101 | 337 | "Remove the last instruction." |
paul@101 | 338 | |
paul@101 | 339 | op = self.code.pop() |
paul@154 | 340 | self.optimiser.clear_active() |
paul@123 | 341 | |
paul@43 | 342 | def replace_op(self, op): |
paul@44 | 343 | |
paul@44 | 344 | "Replace the last added instruction with 'op'." |
paul@44 | 345 | |
paul@101 | 346 | self.remove_op() |
paul@93 | 347 | self.new_op(op) |
paul@93 | 348 | |
paul@129 | 349 | def replace_active_value(self, op): |
paul@129 | 350 | |
paul@129 | 351 | """ |
paul@129 | 352 | Replace the value-providing active instruction with 'op' if appropriate. |
paul@129 | 353 | """ |
paul@129 | 354 | |
paul@154 | 355 | self.optimiser.remove_active_value() |
paul@129 | 356 | self.new_op(op) |
paul@129 | 357 | |
paul@81 | 358 | def last_op(self): |
paul@44 | 359 | |
paul@44 | 360 | "Return the last added instruction." |
paul@44 | 361 | |
paul@70 | 362 | try: |
paul@81 | 363 | return self.code[-1] |
paul@70 | 364 | except IndexError: |
paul@70 | 365 | return None |
paul@26 | 366 | |
paul@103 | 367 | # Visitor methods. |
paul@103 | 368 | |
paul@103 | 369 | def default(self, node, *args): |
paul@103 | 370 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@103 | 371 | |
paul@103 | 372 | def dispatch(self, node, *args): |
paul@103 | 373 | return ASTVisitor.dispatch(self, node, *args) |
paul@103 | 374 | |
paul@56 | 375 | # Internal helper methods. |
paul@17 | 376 | |
paul@43 | 377 | def _visitAttr(self, node, classes): |
paul@44 | 378 | |
paul@44 | 379 | """ |
paul@44 | 380 | Visit the attribute-related 'node', generating instructions based on the |
paul@44 | 381 | given 'classes'. |
paul@44 | 382 | """ |
paul@44 | 383 | |
paul@43 | 384 | self.dispatch(node.expr) |
paul@63 | 385 | self._generateAttr(node, node.attrname, classes) |
paul@43 | 386 | |
paul@63 | 387 | def _generateAttr(self, node, attrname, classes): |
paul@44 | 388 | |
paul@44 | 389 | """ |
paul@44 | 390 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@44 | 391 | """ |
paul@44 | 392 | |
paul@98 | 393 | AddressInstruction, AddressContextInstruction, AttrInstruction, AttrIndexInstruction = classes |
paul@43 | 394 | |
paul@56 | 395 | # Where the last operation (defining the attribute owner) yields a |
paul@56 | 396 | # constant... |
paul@54 | 397 | |
paul@140 | 398 | if self.optimiser.have_constant_input(): |
paul@154 | 399 | last = self.optimiser.active_value |
paul@62 | 400 | |
paul@43 | 401 | # Get the details of the access. |
paul@43 | 402 | |
paul@83 | 403 | if isinstance(last.attr, Const): |
paul@82 | 404 | target_name = last.attr.value_type_name() |
paul@82 | 405 | else: |
paul@82 | 406 | target = last.attr.value |
paul@75 | 407 | |
paul@83 | 408 | if isinstance(target, Const): |
paul@82 | 409 | target_name = target.value_type_name() |
paul@86 | 410 | elif isinstance(target, Instance): |
paul@86 | 411 | target_name = None # skip production of optimised code |
paul@82 | 412 | else: |
paul@82 | 413 | target_name = target.full_name() |
paul@75 | 414 | |
paul@86 | 415 | # Only try and discover the position if the target can be resolved. |
paul@63 | 416 | |
paul@86 | 417 | if target_name is not None: |
paul@86 | 418 | |
paul@86 | 419 | # Access the object table to get the attribute position. |
paul@63 | 420 | |
paul@86 | 421 | try: |
paul@86 | 422 | table_entry = self.objtable.table[target_name] |
paul@86 | 423 | except KeyError: |
paul@86 | 424 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 425 | "No object entry exists for target %r." % target_name) |
paul@63 | 426 | |
paul@86 | 427 | try: |
paul@86 | 428 | pos = table_entry[attrname] |
paul@86 | 429 | except KeyError: |
paul@86 | 430 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 431 | "No attribute entry exists for name %r in target %r." % (attrname, target_name)) |
paul@74 | 432 | |
paul@86 | 433 | # Produce a suitable instruction. |
paul@86 | 434 | |
paul@106 | 435 | if AddressInstruction is not None: |
paul@129 | 436 | self.replace_active_value(AddressInstruction(pos)) |
paul@106 | 437 | else: |
paul@106 | 438 | raise TranslateError(self.module.full_name(), node, |
paul@106 | 439 | "Storing of class or module attribute %r via an object is not permitted." % attrname) |
paul@106 | 440 | |
paul@86 | 441 | return |
paul@54 | 442 | |
paul@54 | 443 | # Where the last operation involves the special 'self' name, check to |
paul@65 | 444 | # see if the attribute is acceptably positioned and produce a direct |
paul@65 | 445 | # access to the attribute. |
paul@54 | 446 | |
paul@140 | 447 | elif self.optimiser.optimise_self_access(attrname, (AddressInstruction, AddressContextInstruction, AttrInstruction), node): |
paul@86 | 448 | return |
paul@54 | 449 | |
paul@54 | 450 | # Otherwise, perform a normal operation. |
paul@43 | 451 | |
paul@86 | 452 | try: |
paul@86 | 453 | index = self.objtable.get_index(attrname) |
paul@86 | 454 | except self.objtable.TableError: |
paul@86 | 455 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 456 | "No attribute entry exists for name %r." % attrname) |
paul@63 | 457 | |
paul@86 | 458 | self.new_op(AttrIndexInstruction(index)) |
paul@43 | 459 | |
paul@101 | 460 | # Invocations involve the following: |
paul@101 | 461 | # |
paul@101 | 462 | # 1. Reservation of a frame for the arguments |
paul@101 | 463 | # 2. Identification of the target which is then held in temporary storage |
paul@101 | 464 | # 3. Optional inclusion of a context (important for methods) |
paul@101 | 465 | # 4. Preparation of the argument frame |
paul@101 | 466 | # 5. Invocation of the target |
paul@101 | 467 | # 6. Discarding of the frame |
paul@101 | 468 | # |
paul@101 | 469 | # In order to support nested invocations - eg. a(b(c)) - use of the |
paul@101 | 470 | # temporary storage is essential. |
paul@101 | 471 | |
paul@45 | 472 | def _startCallFunc(self): |
paul@45 | 473 | |
paul@45 | 474 | "Record the location of the invocation." |
paul@21 | 475 | |
paul@117 | 476 | op = MakeFrame() |
paul@117 | 477 | self.new_op(op) # records the start of the frame |
paul@117 | 478 | self.frame_makers.append(op) |
paul@21 | 479 | |
paul@44 | 480 | def _generateCallFunc(self, args, node): |
paul@44 | 481 | |
paul@86 | 482 | """ |
paul@86 | 483 | Support a generic function invocation using the given 'args', occurring |
paul@86 | 484 | on the given 'node', where the expression providing the invocation |
paul@86 | 485 | target has just been generated. |
paul@86 | 486 | |
paul@86 | 487 | In other situations, the invocation is much simpler and does not need to |
paul@86 | 488 | handle the full flexibility of a typical Python invocation. Internal |
paul@86 | 489 | invocations, such as those employed by operators and certain |
paul@86 | 490 | control-flow mechanisms, use predetermined arguments and arguably do not |
paul@86 | 491 | need to support the same things as the more general invocations. |
paul@86 | 492 | """ |
paul@86 | 493 | |
paul@86 | 494 | target, context, temp = self._generateCallFuncContext() |
paul@109 | 495 | self._generateCallFuncArgs(target, context, temp, args, node) |
paul@137 | 496 | return temp, target |
paul@86 | 497 | |
paul@86 | 498 | def _generateCallFuncContext(self): |
paul@86 | 499 | |
paul@86 | 500 | """ |
paul@86 | 501 | Produce code which loads and checks the context of the current |
paul@86 | 502 | invocation, the instructions for whose target have already been |
paul@86 | 503 | produced, returning a list of instructions which reference the |
paul@86 | 504 | invocation target. |
paul@86 | 505 | """ |
paul@26 | 506 | |
paul@140 | 507 | t = self.optimiser.optimise_known_target() |
paul@77 | 508 | if t: |
paul@77 | 509 | target, context = t |
paul@77 | 510 | else: |
paul@77 | 511 | target, context = None, None |
paul@45 | 512 | |
paul@78 | 513 | # Store the target in temporary storage for subsequent referencing. |
paul@137 | 514 | # NOTE: This may not be appropriate for class invocations |
paul@137 | 515 | # NOTE: (instantiation). |
paul@78 | 516 | |
paul@140 | 517 | temp = self.optimiser.optimise_temp_storage() |
paul@78 | 518 | |
paul@98 | 519 | # Where a target or context are not known or where an instance is known |
paul@98 | 520 | # to be the context, load the context. |
paul@45 | 521 | |
paul@132 | 522 | if target is None or isinstance(context, Instance): |
paul@102 | 523 | self.new_op(temp) |
paul@45 | 524 | self.new_op(LoadContext()) |
paul@98 | 525 | self.new_op(StoreFrame(0)) |
paul@87 | 526 | |
paul@137 | 527 | # For known instantiations, provide a new object as the first argument |
paul@137 | 528 | # to the __init__ method. |
paul@137 | 529 | |
paul@137 | 530 | elif isinstance(target, Class): |
paul@138 | 531 | self.make_object(target, len(target.instance_attributes())) |
paul@137 | 532 | self.new_op(StoreFrame(0)) |
paul@137 | 533 | |
paul@87 | 534 | # Otherwise omit the context. |
paul@87 | 535 | |
paul@59 | 536 | else: |
paul@59 | 537 | pass # NOTE: Class methods should be supported. |
paul@26 | 538 | |
paul@86 | 539 | return target, context, temp |
paul@86 | 540 | |
paul@109 | 541 | def _generateCallFuncArgs(self, target, context, temp, args, node): |
paul@86 | 542 | |
paul@86 | 543 | """ |
paul@109 | 544 | Given invocation 'target' and 'context' information, the 'temp' |
paul@109 | 545 | reference to the target, a list of nodes representing the 'args' |
paul@109 | 546 | (arguments), generate instructions which load the arguments for the |
paul@109 | 547 | invocation defined by the given 'node'. |
paul@86 | 548 | """ |
paul@86 | 549 | |
paul@21 | 550 | # Evaluate the arguments. |
paul@21 | 551 | |
paul@67 | 552 | employed_positions = set() |
paul@132 | 553 | employed_keywords = set() |
paul@59 | 554 | extra_keywords = [] |
paul@22 | 555 | |
paul@132 | 556 | # Find keyword arguments in advance in order to help resolve targets. |
paul@132 | 557 | |
paul@132 | 558 | for arg in args: |
paul@132 | 559 | if isinstance(arg, compiler.ast.Keyword): |
paul@132 | 560 | employed_keywords.add(arg.name) |
paul@132 | 561 | |
paul@132 | 562 | possible_targets = self.paramtable.all_possible_objects(employed_keywords) |
paul@132 | 563 | |
paul@98 | 564 | # Note the presence of the context in the frame where appropriate. |
paul@86 | 565 | |
paul@132 | 566 | if target is None or isinstance(context, Instance): |
paul@86 | 567 | ncontext = 1 |
paul@98 | 568 | expect_context = 0 |
paul@137 | 569 | |
paul@137 | 570 | # Handle calls to classes. |
paul@137 | 571 | |
paul@137 | 572 | elif isinstance(target, Class): |
paul@137 | 573 | ncontext = 1 |
paul@137 | 574 | expect_context = 0 |
paul@137 | 575 | target = target.get_init_method() |
paul@137 | 576 | |
paul@137 | 577 | # Method calls via classes. |
paul@137 | 578 | |
paul@98 | 579 | elif isinstance(context, Class): |
paul@98 | 580 | ncontext = 0 |
paul@98 | 581 | expect_context = 1 |
paul@137 | 582 | |
paul@137 | 583 | # Function calls. |
paul@137 | 584 | |
paul@86 | 585 | else: |
paul@86 | 586 | ncontext = 0 |
paul@98 | 587 | expect_context = 0 |
paul@86 | 588 | |
paul@92 | 589 | first = 1 |
paul@92 | 590 | frame_pos = ncontext |
paul@112 | 591 | max_keyword_pos = -1 |
paul@92 | 592 | |
paul@92 | 593 | for arg in args: |
paul@66 | 594 | |
paul@66 | 595 | # Handle positional and keyword arguments separately. |
paul@66 | 596 | |
paul@21 | 597 | if isinstance(arg, compiler.ast.Keyword): |
paul@66 | 598 | |
paul@26 | 599 | # Optimise where the target is known now. |
paul@26 | 600 | |
paul@26 | 601 | if target is not None: |
paul@39 | 602 | |
paul@39 | 603 | # Find the parameter table entry for the target. |
paul@39 | 604 | |
paul@39 | 605 | target_name = target.full_name() |
paul@39 | 606 | |
paul@39 | 607 | # Look for a callable with the precise target name. |
paul@39 | 608 | |
paul@59 | 609 | table_entry = self.paramtable.table[target_name] |
paul@59 | 610 | |
paul@59 | 611 | # Look the name up in the parameter table entry. |
paul@39 | 612 | |
paul@59 | 613 | try: |
paul@59 | 614 | pos = table_entry[arg.name] |
paul@59 | 615 | |
paul@59 | 616 | # Where no position is found, this could be an extra keyword |
paul@59 | 617 | # argument. |
paul@39 | 618 | |
paul@39 | 619 | except KeyError: |
paul@59 | 620 | extra_keywords.append(arg) |
paul@59 | 621 | continue |
paul@58 | 622 | |
paul@58 | 623 | # Test for illegal conditions. |
paul@58 | 624 | |
paul@67 | 625 | if pos in employed_positions: |
paul@58 | 626 | raise TranslateError(self.module.full_name(), node, |
paul@58 | 627 | "Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@67 | 628 | |
paul@67 | 629 | employed_positions.add(pos) |
paul@58 | 630 | |
paul@59 | 631 | # Generate code for the keyword and the positioning |
paul@59 | 632 | # operation. |
paul@59 | 633 | |
paul@59 | 634 | self.dispatch(arg.expr) |
paul@98 | 635 | self.new_op(StoreFrame(pos)) |
paul@26 | 636 | |
paul@26 | 637 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 638 | # the parameter location. |
paul@26 | 639 | |
paul@26 | 640 | else: |
paul@21 | 641 | |
paul@26 | 642 | # Combine the target details with the name to get the location. |
paul@26 | 643 | # See the access method on the List class. |
paul@26 | 644 | |
paul@39 | 645 | try: |
paul@39 | 646 | paramindex = self.paramtable.get_index(arg.name) |
paul@59 | 647 | |
paul@59 | 648 | # Where no position is found, this could be an extra keyword |
paul@59 | 649 | # argument. |
paul@59 | 650 | |
paul@70 | 651 | except self.paramtable.TableError: |
paul@59 | 652 | extra_keywords.append(arg) |
paul@59 | 653 | continue |
paul@21 | 654 | |
paul@59 | 655 | # Generate code for the keyword and the positioning |
paul@59 | 656 | # operation. |
paul@59 | 657 | |
paul@59 | 658 | self.dispatch(arg.expr) |
paul@26 | 659 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 660 | |
paul@26 | 661 | # use (callable+0)+paramindex+table |
paul@26 | 662 | # checks embedded offset against (callable+0) |
paul@101 | 663 | # moves the current value to frame+position |
paul@21 | 664 | |
paul@111 | 665 | # Record the highest possible frame position for this argument. |
paul@111 | 666 | |
paul@111 | 667 | max_keyword_pos = max(max_keyword_pos, max(self.paramtable.all_attribute_positions(arg.name))) |
paul@111 | 668 | |
paul@21 | 669 | else: |
paul@21 | 670 | self.dispatch(arg) |
paul@98 | 671 | self.new_op(StoreFrame(frame_pos)) |
paul@104 | 672 | |
paul@98 | 673 | employed_positions.add(frame_pos) |
paul@21 | 674 | |
paul@92 | 675 | # Check to see if the first argument is appropriate (compatible with |
paul@98 | 676 | # the target where methods are being invoked via classes). |
paul@92 | 677 | |
paul@98 | 678 | if first and expect_context: |
paul@122 | 679 | |
paul@122 | 680 | # Drop any test if the target and the context are known. |
paul@122 | 681 | |
paul@140 | 682 | if not self.optimiser.have_correct_self_for_target(context): |
paul@122 | 683 | |
paul@122 | 684 | continue_label = self.new_label() |
paul@122 | 685 | self.new_op(CheckSelf()) |
paul@154 | 686 | self.optimiser.set_source(temp) |
paul@122 | 687 | self.new_op(JumpIfTrue(continue_label)) |
paul@122 | 688 | |
paul@122 | 689 | # Where the context is inappropriate, drop the incomplete frame and |
paul@122 | 690 | # raise an exception. |
paul@122 | 691 | |
paul@122 | 692 | self.new_op(DropFrame()) |
paul@122 | 693 | self.new_op(LoadResult()) |
paul@122 | 694 | |
paul@122 | 695 | self.load_builtin("TypeError", node) |
paul@122 | 696 | self.new_op(StoreException()) |
paul@122 | 697 | self.new_op(RaiseException()) |
paul@122 | 698 | self.set_label(continue_label) |
paul@92 | 699 | |
paul@95 | 700 | first = 0 |
paul@92 | 701 | frame_pos += 1 |
paul@66 | 702 | |
paul@75 | 703 | # NOTE: Extra keywords are not supported. |
paul@75 | 704 | # NOTE: Somehow, the above needs to be combined with * arguments. |
paul@59 | 705 | |
paul@112 | 706 | if extra_keywords: |
paul@112 | 707 | print "Warning: extra keyword argument(s) %s not handled." % ", ".join([arg.name for arg in extra_keywords]) |
paul@112 | 708 | |
paul@59 | 709 | # Either test for a complete set of arguments. |
paul@59 | 710 | |
paul@59 | 711 | if target is not None: |
paul@66 | 712 | |
paul@66 | 713 | # Make sure that enough arguments have been given. |
paul@66 | 714 | |
paul@67 | 715 | nargs_max = len(target.positional_names) |
paul@66 | 716 | ndefaults = len(target.defaults) |
paul@66 | 717 | nargs_min = nargs_max - ndefaults |
paul@67 | 718 | |
paul@86 | 719 | for i in range(ncontext, nargs_min): |
paul@67 | 720 | if i not in employed_positions: |
paul@67 | 721 | raise TranslateError(self.module.full_name(), node, |
paul@110 | 722 | "Argument %r not supplied for %r: need at least %d argument(s)." % (i+1, target.name, nargs_min)) |
paul@67 | 723 | |
paul@137 | 724 | nargs = frame_pos |
paul@66 | 725 | |
paul@67 | 726 | if nargs > nargs_max and not target.has_star and not target.has_dstar: |
paul@59 | 727 | raise TranslateError(self.module.full_name(), node, |
paul@110 | 728 | "Too many arguments for %r: need at most %d argument(s)." % (target.name, nargs_max)) |
paul@66 | 729 | |
paul@66 | 730 | # Where defaults are involved, put them into the frame. |
paul@66 | 731 | |
paul@109 | 732 | self._generateCallFuncDefaultArgs(target, temp, nargs_min, nargs_max, employed_positions) |
paul@59 | 733 | |
paul@117 | 734 | # Set the frame size. |
paul@117 | 735 | |
paul@129 | 736 | self._endCallFuncArgs(nargs_max) |
paul@117 | 737 | |
paul@59 | 738 | # Or generate instructions to do this at run-time. |
paul@66 | 739 | # NOTE: CheckFrame has to check the number of arguments and to fill in |
paul@98 | 740 | # NOTE: defaults; it also has to shift the invocation frame according to |
paul@98 | 741 | # NOTE: the context in use. |
paul@59 | 742 | |
paul@59 | 743 | else: |
paul@117 | 744 | max_pos = max(max(employed_positions or [-1]), max_keyword_pos, frame_pos - 1) |
paul@112 | 745 | |
paul@132 | 746 | # Only check non-empty frames (using the callable's details). |
paul@112 | 747 | |
paul@117 | 748 | if employed_positions or max_pos >= 0: |
paul@132 | 749 | self.new_op(temp) |
paul@112 | 750 | self.new_op(CheckFrame(max_pos + 1)) |
paul@59 | 751 | |
paul@117 | 752 | # Set the frame size. |
paul@117 | 753 | |
paul@129 | 754 | self._endCallFuncArgs(max_pos + 1) |
paul@117 | 755 | |
paul@109 | 756 | def _generateCallFuncDefaultArgs(self, target, temp, nargs_min, nargs_max, employed_positions): |
paul@109 | 757 | |
paul@109 | 758 | """ |
paul@109 | 759 | For the given 'target' and 'temp' reference to the target, generate |
paul@109 | 760 | default arguments for those positions in the range 'nargs_min'... |
paul@109 | 761 | 'nargs_max' which are not present in the 'employed_positions' |
paul@109 | 762 | collection. |
paul@109 | 763 | """ |
paul@109 | 764 | |
paul@109 | 765 | # Where a lambda is involved, construct a dynamic object to hold the |
paul@109 | 766 | # defaults. |
paul@109 | 767 | |
paul@109 | 768 | dynamic = target.name is None |
paul@109 | 769 | |
paul@109 | 770 | # Here, we use negative index values to visit the right hand end of |
paul@109 | 771 | # the defaults list. |
paul@109 | 772 | |
paul@109 | 773 | for pos in range(nargs_min, nargs_max): |
paul@109 | 774 | if pos not in employed_positions: |
paul@109 | 775 | if dynamic: |
paul@109 | 776 | self.new_op(temp) |
paul@109 | 777 | self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@109 | 778 | else: |
paul@109 | 779 | self.new_op(LoadAddress(target.default_attrs[pos - nargs_min])) |
paul@109 | 780 | self.new_op(StoreFrame(pos)) |
paul@109 | 781 | |
paul@137 | 782 | def _doCallFunc(self, instruction, target=None): |
paul@97 | 783 | |
paul@97 | 784 | "Make the invocation." |
paul@57 | 785 | |
paul@137 | 786 | if isinstance(target, Class): |
paul@138 | 787 | self.new_op(LoadConst(target.get_init_method())) |
paul@137 | 788 | else: |
paul@137 | 789 | self.new_op(instruction) |
paul@137 | 790 | self.new_op(LoadCallable()) |
paul@97 | 791 | self.new_op(JumpWithFrame()) |
paul@57 | 792 | |
paul@129 | 793 | def _endCallFuncArgs(self, nargs): |
paul@129 | 794 | |
paul@129 | 795 | "Set the frame size." |
paul@129 | 796 | |
paul@129 | 797 | self.frame_makers[-1].attr = nargs |
paul@129 | 798 | self.frame_makers.pop() |
paul@129 | 799 | |
paul@137 | 800 | def _endCallFunc(self, instruction=None, target=None, load_result=1): |
paul@97 | 801 | |
paul@97 | 802 | "Finish the invocation and tidy up afterwards." |
paul@21 | 803 | |
paul@137 | 804 | if isinstance(target, Class): |
paul@137 | 805 | self.new_op(LoadName(target.get_init_method().all_locals()["self"])) # load the context in the invocation frame |
paul@137 | 806 | self.new_op(StoreResult()) |
paul@21 | 807 | self.new_op(DropFrame()) |
paul@113 | 808 | if load_result: |
paul@113 | 809 | self.new_op(LoadResult()) |
paul@97 | 810 | |
paul@97 | 811 | # Discard any temporary storage instructions. |
paul@97 | 812 | |
paul@102 | 813 | if instruction is not None: |
paul@102 | 814 | self.discard_temp(instruction) |
paul@17 | 815 | |
paul@109 | 816 | def _generateFunctionDefaults(self, function): |
paul@109 | 817 | |
paul@109 | 818 | """ |
paul@109 | 819 | Generate the default initialisation code for 'function', returning |
paul@109 | 820 | a temporary storage reference if a dynamic object was created for the |
paul@109 | 821 | function. |
paul@109 | 822 | """ |
paul@109 | 823 | |
paul@109 | 824 | attr_to_default = zip(function.default_attrs, function.defaults) |
paul@109 | 825 | if not attr_to_default: |
paul@109 | 826 | return None |
paul@109 | 827 | |
paul@109 | 828 | # Where a lambda is involved, construct a dynamic object to hold the |
paul@109 | 829 | # defaults. |
paul@109 | 830 | |
paul@109 | 831 | dynamic = function.name is None |
paul@109 | 832 | |
paul@109 | 833 | if dynamic: |
paul@138 | 834 | self.make_object(self.get_builtin_class("function", function), len(attr_to_default)) |
paul@109 | 835 | temp = self.get_temp() |
paul@109 | 836 | |
paul@109 | 837 | for attr, default in attr_to_default: |
paul@109 | 838 | self.dispatch(default) |
paul@109 | 839 | |
paul@109 | 840 | self.record_value() |
paul@109 | 841 | if dynamic: |
paul@109 | 842 | self.new_op(temp) |
paul@109 | 843 | self.new_op(StoreAttr(attr)) |
paul@109 | 844 | else: |
paul@109 | 845 | self.new_op(StoreAddress(attr)) |
paul@109 | 846 | self.set_source() |
paul@109 | 847 | self.discard_value() |
paul@109 | 848 | |
paul@109 | 849 | if dynamic: |
paul@109 | 850 | return temp |
paul@109 | 851 | else: |
paul@109 | 852 | return None |
paul@109 | 853 | |
paul@56 | 854 | def _visitName(self, node, classes): |
paul@56 | 855 | |
paul@56 | 856 | """ |
paul@56 | 857 | Visit the name-related 'node', generating instructions based on the |
paul@56 | 858 | given 'classes'. |
paul@56 | 859 | """ |
paul@56 | 860 | |
paul@56 | 861 | name = node.name |
paul@56 | 862 | scope = self.get_scope(name) |
paul@56 | 863 | #print self.module.name, node.lineno, name, scope |
paul@56 | 864 | self._generateName(name, scope, classes, node) |
paul@56 | 865 | |
paul@56 | 866 | def _generateName(self, name, scope, classes, node): |
paul@56 | 867 | |
paul@56 | 868 | """ |
paul@56 | 869 | Generate code for the access to 'name' in 'scope' using the given |
paul@56 | 870 | 'classes', and using the given 'node' as the source of the access. |
paul@56 | 871 | """ |
paul@56 | 872 | |
paul@72 | 873 | NameInstruction, AddressInstruction = classes |
paul@56 | 874 | |
paul@56 | 875 | if scope == "local": |
paul@56 | 876 | unit = self.unit |
paul@83 | 877 | if isinstance(unit, Function): |
paul@56 | 878 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@83 | 879 | elif isinstance(unit, Class): |
paul@72 | 880 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@83 | 881 | elif isinstance(unit, Module): |
paul@72 | 882 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@56 | 883 | else: |
paul@61 | 884 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@56 | 885 | |
paul@56 | 886 | elif scope == "global": |
paul@56 | 887 | globals = self.module.module_attributes() |
paul@56 | 888 | if globals.has_key(name): |
paul@72 | 889 | self.new_op(AddressInstruction(globals[name])) |
paul@56 | 890 | else: |
paul@61 | 891 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@56 | 892 | |
paul@56 | 893 | else: |
paul@101 | 894 | self.new_op(AddressInstruction(self.get_builtin(name, node))) |
paul@56 | 895 | |
paul@85 | 896 | def _visitUnary(self, node, method): |
paul@85 | 897 | |
paul@85 | 898 | """ |
paul@85 | 899 | _t = node.expr |
paul@85 | 900 | try: |
paul@85 | 901 | _result = _t.__pos__() |
paul@85 | 902 | except AttributeError: |
paul@85 | 903 | raise TypeError |
paul@85 | 904 | """ |
paul@85 | 905 | |
paul@151 | 906 | type_error_label = self.new_label() |
paul@85 | 907 | end_label = self.new_label() |
paul@85 | 908 | |
paul@85 | 909 | # Evaluate and store the operand in temporary storage. |
paul@85 | 910 | |
paul@85 | 911 | self.dispatch(node.expr) |
paul@140 | 912 | temp = self.optimiser.optimise_temp_storage() |
paul@85 | 913 | |
paul@102 | 914 | self.new_op(temp) |
paul@85 | 915 | |
paul@85 | 916 | # Get the method on temp. |
paul@85 | 917 | |
paul@104 | 918 | self._generateAttr(node, method, self.attribute_load_instructions) |
paul@140 | 919 | temp_method = self.optimiser.optimise_temp_storage() |
paul@85 | 920 | |
paul@151 | 921 | self._handleAttributeError(node, type_error_label) |
paul@114 | 922 | |
paul@85 | 923 | # Add arguments. |
paul@86 | 924 | # NOTE: No support for defaults. |
paul@85 | 925 | |
paul@102 | 926 | self.new_op(temp) # Explicit context as first argument. |
paul@113 | 927 | self.new_op(StoreFrame(0)) |
paul@129 | 928 | self._endCallFuncArgs(1) |
paul@97 | 929 | self._doCallFunc(temp_method) |
paul@101 | 930 | self._endCallFunc(temp_method) |
paul@85 | 931 | self.new_op(Jump(end_label)) |
paul@85 | 932 | |
paul@151 | 933 | # Store the result. |
paul@151 | 934 | |
paul@151 | 935 | temp_out = self.get_temp() |
paul@85 | 936 | |
paul@85 | 937 | # Raise a TypeError. |
paul@85 | 938 | |
paul@151 | 939 | self.set_label(type_error_label) |
paul@101 | 940 | self.load_builtin("TypeError", node) |
paul@105 | 941 | self.new_op(StoreException()) |
paul@85 | 942 | self.new_op(RaiseException()) |
paul@85 | 943 | |
paul@152 | 944 | # Prevent incorrect optimisation. |
paul@152 | 945 | |
paul@154 | 946 | self.optimiser.reset() |
paul@152 | 947 | |
paul@85 | 948 | self.set_label(end_label) |
paul@85 | 949 | |
paul@151 | 950 | # Produce the result. |
paul@151 | 951 | |
paul@151 | 952 | self.new_op(temp_out) |
paul@151 | 953 | |
paul@85 | 954 | # Compilation duties... |
paul@85 | 955 | |
paul@85 | 956 | self.discard_temp(temp) |
paul@152 | 957 | self.discard_temp(temp_out) |
paul@85 | 958 | |
paul@57 | 959 | def _visitBinary(self, node, left_method, right_method): |
paul@56 | 960 | |
paul@56 | 961 | """ |
paul@56 | 962 | _t1 = node.left |
paul@56 | 963 | _t2 = node.right |
paul@56 | 964 | try: |
paul@76 | 965 | _result = _t1.__add__(_t2) |
paul@76 | 966 | if _result is NotImplemented: |
paul@76 | 967 | raise AttributeError |
paul@56 | 968 | except AttributeError: |
paul@76 | 969 | try: |
paul@76 | 970 | _result = _t2.__radd__(_t1) |
paul@76 | 971 | if _result is NotImplemented: |
paul@76 | 972 | raise AttributeError |
paul@76 | 973 | except AttributeError: |
paul@76 | 974 | raise TypeError |
paul@56 | 975 | """ |
paul@56 | 976 | |
paul@78 | 977 | # Evaluate and store the left operand in temporary storage. |
paul@73 | 978 | |
paul@57 | 979 | self.dispatch(node.left) |
paul@140 | 980 | temp1 = self.optimiser.optimise_temp_storage() |
paul@74 | 981 | |
paul@78 | 982 | # Evaluate and store the right operand in temporary storage. |
paul@74 | 983 | |
paul@57 | 984 | self.dispatch(node.right) |
paul@140 | 985 | temp2 = self.optimiser.optimise_temp_storage() |
paul@57 | 986 | |
paul@151 | 987 | temp_out = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@151 | 988 | |
paul@151 | 989 | # Produce the result. |
paul@151 | 990 | |
paul@151 | 991 | self.new_op(temp_out) |
paul@113 | 992 | |
paul@112 | 993 | # Compilation duties... |
paul@112 | 994 | |
paul@112 | 995 | self.discard_temp(temp1) |
paul@112 | 996 | self.discard_temp(temp2) |
paul@152 | 997 | self.discard_temp(temp_out) |
paul@112 | 998 | |
paul@113 | 999 | def _generateBinary(self, node, temp1, temp2, left_method, right_method): |
paul@112 | 1000 | |
paul@114 | 1001 | """ |
paul@114 | 1002 | For the given 'node', generate the binary operator pattern for the |
paul@114 | 1003 | operands 'temp1' and 'temp2', employing 'left_method' and 'right_method' |
paul@114 | 1004 | as defined for binary operators, but also used in comparisons (for which |
paul@114 | 1005 | this method is provided). |
paul@114 | 1006 | """ |
paul@114 | 1007 | |
paul@112 | 1008 | right_label = self.new_label() |
paul@112 | 1009 | type_error_label = self.new_label() |
paul@112 | 1010 | end_label = self.new_label() |
paul@112 | 1011 | |
paul@57 | 1012 | # Left method. |
paul@57 | 1013 | |
paul@151 | 1014 | temp_out = self._generateOpMethod(node, temp1, temp2, left_method, right_label, end_label) |
paul@151 | 1015 | self.discard_temp(temp_out) # NOTE: Will re-use the same storage. |
paul@112 | 1016 | |
paul@112 | 1017 | # Right method. |
paul@112 | 1018 | |
paul@112 | 1019 | self.set_label(right_label) |
paul@151 | 1020 | temp_out = self._generateOpMethod(node, temp2, temp1, right_method, type_error_label, end_label) |
paul@112 | 1021 | |
paul@112 | 1022 | # Raise a TypeError. |
paul@112 | 1023 | |
paul@112 | 1024 | self.set_label(type_error_label) |
paul@112 | 1025 | self.load_builtin("TypeError", node) |
paul@112 | 1026 | self.new_op(StoreException()) |
paul@112 | 1027 | self.new_op(RaiseException()) |
paul@112 | 1028 | |
paul@152 | 1029 | # Prevent incorrect optimisation. |
paul@152 | 1030 | |
paul@154 | 1031 | self.optimiser.reset() |
paul@152 | 1032 | |
paul@112 | 1033 | self.set_label(end_label) |
paul@151 | 1034 | return temp_out |
paul@112 | 1035 | |
paul@113 | 1036 | def _generateOpMethod(self, node, temp1, temp2, method_name, next_method_label, end_label): |
paul@112 | 1037 | |
paul@114 | 1038 | """ |
paul@114 | 1039 | For the given 'node', generate the operator method invocation using the |
paul@114 | 1040 | operands 'temp1' and 'temp2', employing the given 'method_name', and |
paul@114 | 1041 | jumping appropriately to 'next_method_label' where a NotImplemented |
paul@114 | 1042 | result is returned, or to 'end_label' if the method call was successful. |
paul@114 | 1043 | """ |
paul@114 | 1044 | |
paul@112 | 1045 | end_attempt_label = self.new_label() |
paul@112 | 1046 | |
paul@102 | 1047 | self.new_op(temp1) |
paul@76 | 1048 | |
paul@112 | 1049 | # Get method on temp1. |
paul@76 | 1050 | |
paul@112 | 1051 | self._generateAttr(node, method_name, self.attribute_load_instructions) |
paul@140 | 1052 | temp_method = self.optimiser.optimise_temp_storage() |
paul@76 | 1053 | |
paul@114 | 1054 | self._handleAttributeError(node, end_attempt_label) |
paul@114 | 1055 | |
paul@76 | 1056 | # Add arguments. |
paul@86 | 1057 | # NOTE: No support for defaults. |
paul@76 | 1058 | |
paul@131 | 1059 | self._startCallFunc() |
paul@114 | 1060 | self.new_op(temp1) |
paul@113 | 1061 | self.new_op(StoreFrame(0)) |
paul@102 | 1062 | self.new_op(temp2) |
paul@113 | 1063 | self.new_op(StoreFrame(1)) |
paul@129 | 1064 | self._endCallFuncArgs(2) |
paul@97 | 1065 | self._doCallFunc(temp_method) |
paul@101 | 1066 | self._endCallFunc(temp_method) |
paul@57 | 1067 | |
paul@151 | 1068 | # Store the result. |
paul@151 | 1069 | |
paul@151 | 1070 | temp_out = self.get_temp() |
paul@151 | 1071 | |
paul@76 | 1072 | # Test for NotImplemented. |
paul@76 | 1073 | # Don't actually raise an exception. |
paul@76 | 1074 | |
paul@101 | 1075 | self.new_op(TestIdentityAddress(self.get_builtin("NotImplemented", node))) |
paul@112 | 1076 | self.new_op(JumpIfTrue(next_method_label)) |
paul@76 | 1077 | self.new_op(Jump(end_label)) |
paul@76 | 1078 | |
paul@112 | 1079 | # End method attempt. |
paul@76 | 1080 | |
paul@112 | 1081 | self.set_label(end_attempt_label) |
paul@151 | 1082 | return temp_out |
paul@57 | 1083 | |
paul@114 | 1084 | def _handleAttributeError(self, node, end_call_label): |
paul@114 | 1085 | |
paul@114 | 1086 | """ |
paul@114 | 1087 | Add exception handling to the method acquisition instructions where the |
paul@114 | 1088 | attribute access cannot be resolved at compile-time. |
paul@114 | 1089 | """ |
paul@114 | 1090 | |
paul@140 | 1091 | if not self.optimiser.optimise_known_target(): |
paul@114 | 1092 | self.load_builtin("AttributeError", node) |
paul@114 | 1093 | self.new_op(CheckException()) |
paul@114 | 1094 | self.new_op(JumpIfTrue(end_call_label)) |
paul@114 | 1095 | |
paul@114 | 1096 | def _generateSequence(self, sequence_type, node): |
paul@114 | 1097 | |
paul@114 | 1098 | "Make a sequence of 'sequence_type' for the given program 'node'." |
paul@114 | 1099 | |
paul@143 | 1100 | self.make_object(self.get_builtin_class(sequence_type, node), len(node.nodes)) |
paul@112 | 1101 | temp = self.get_temp() |
paul@76 | 1102 | |
paul@112 | 1103 | for i, n in enumerate(node.nodes): |
paul@112 | 1104 | self.dispatch(n) |
paul@112 | 1105 | self.record_value() |
paul@112 | 1106 | self.new_op(temp) |
paul@112 | 1107 | self.new_op(StoreAttr(Attr(i, None, None, None))) |
paul@112 | 1108 | self.set_source() |
paul@112 | 1109 | self.discard_value() |
paul@85 | 1110 | |
paul@112 | 1111 | self.new_op(temp) |
paul@112 | 1112 | self.discard_temp(temp) |
paul@73 | 1113 | |
paul@130 | 1114 | def _generateTestBoolean(self, node, temp): |
paul@129 | 1115 | |
paul@129 | 1116 | """ |
paul@129 | 1117 | Generate a test of the boolean status of the current value for the given |
paul@129 | 1118 | program 'node'. |
paul@129 | 1119 | """ |
paul@129 | 1120 | |
paul@129 | 1121 | # Get method on temp. |
paul@129 | 1122 | # NOTE: Using __bool__ instead of __nonzero__. |
paul@129 | 1123 | |
paul@129 | 1124 | self._generateAttr(node, "__bool__", self.attribute_load_instructions) |
paul@140 | 1125 | temp_method = self.optimiser.optimise_temp_storage() |
paul@129 | 1126 | |
paul@129 | 1127 | self._startCallFunc() |
paul@129 | 1128 | self.new_op(temp) |
paul@129 | 1129 | self.new_op(StoreFrame(0)) |
paul@129 | 1130 | self._endCallFuncArgs(1) |
paul@129 | 1131 | self._doCallFunc(temp_method) |
paul@129 | 1132 | self._endCallFunc(temp_method) |
paul@129 | 1133 | |
paul@130 | 1134 | self.discard_temp(temp_method) |
paul@130 | 1135 | |
paul@129 | 1136 | # Convert result to boolean (a StoreBoolean operation). |
paul@129 | 1137 | |
paul@129 | 1138 | self.new_op(TestIdentityAddress(self.get_builtin("True", node))) |
paul@129 | 1139 | |
paul@129 | 1140 | def _generateLoadBoolean(self, node): |
paul@129 | 1141 | |
paul@129 | 1142 | """ |
paul@129 | 1143 | Generate instructions to load the appropriate value given the current |
paul@129 | 1144 | boolean status. |
paul@129 | 1145 | """ |
paul@129 | 1146 | |
paul@129 | 1147 | true_label = self.new_label() |
paul@129 | 1148 | end_label = self.new_label() |
paul@129 | 1149 | |
paul@129 | 1150 | self.new_op(JumpIfTrue(true_label)) |
paul@129 | 1151 | self.load_builtin("False", node) |
paul@129 | 1152 | self.new_op(Jump(end_label)) |
paul@129 | 1153 | |
paul@129 | 1154 | self.set_label(true_label) |
paul@129 | 1155 | self.load_builtin("True", node) |
paul@129 | 1156 | |
paul@129 | 1157 | self.set_label(end_label) |
paul@129 | 1158 | |
paul@103 | 1159 | # Concrete visitor methods. |
paul@103 | 1160 | |
paul@57 | 1161 | def visitAdd(self, node): |
paul@57 | 1162 | self._visitBinary(node, "__add__", "__radd__") |
paul@57 | 1163 | |
paul@101 | 1164 | def visitAnd(self, node): |
paul@130 | 1165 | end_label = self.new_label() |
paul@130 | 1166 | temp_pos = self.reserve_temp() |
paul@130 | 1167 | temp = LoadTemp(temp_pos) |
paul@101 | 1168 | |
paul@101 | 1169 | for n in node.nodes[:-1]: |
paul@101 | 1170 | self.dispatch(n) |
paul@130 | 1171 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 1172 | |
paul@130 | 1173 | self._generateTestBoolean(n, temp) |
paul@130 | 1174 | self.new_op(JumpIfFalse(end_label)) |
paul@101 | 1175 | |
paul@101 | 1176 | self.dispatch(node.nodes[-1]) |
paul@130 | 1177 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 1178 | |
paul@130 | 1179 | self.set_label(end_label) |
paul@101 | 1180 | |
paul@101 | 1181 | # Prevent incorrect optimisation. |
paul@101 | 1182 | |
paul@154 | 1183 | self.optimiser.reset() |
paul@130 | 1184 | |
paul@130 | 1185 | self.new_op(temp) |
paul@130 | 1186 | self.discard_temp(temp) |
paul@56 | 1187 | |
paul@100 | 1188 | def visitAssert(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Assert") |
paul@56 | 1189 | |
paul@56 | 1190 | def visitAssign(self, node): |
paul@56 | 1191 | self.dispatch(node.expr) |
paul@146 | 1192 | |
paul@146 | 1193 | # Check nodes for immediate usage. |
paul@146 | 1194 | |
paul@146 | 1195 | immediate = 1 |
paul@146 | 1196 | |
paul@146 | 1197 | for n in node.nodes: |
paul@150 | 1198 | if not isinstance(n, (compiler.ast.AssName, compiler.ast.AssAttr)): |
paul@146 | 1199 | immediate = 0 |
paul@146 | 1200 | break |
paul@146 | 1201 | |
paul@146 | 1202 | # Record the value and then dispatch to the assignment targets. |
paul@146 | 1203 | |
paul@146 | 1204 | self.record_value(immediate) |
paul@101 | 1205 | |
paul@56 | 1206 | for n in node.nodes: |
paul@143 | 1207 | self.dispatch(n) |
paul@56 | 1208 | |
paul@104 | 1209 | self.discard_value() |
paul@101 | 1210 | |
paul@143 | 1211 | def visitAssAttr(self, node): |
paul@98 | 1212 | self._visitAttr(node, self.attribute_store_instructions) |
paul@104 | 1213 | self.set_source() |
paul@56 | 1214 | |
paul@143 | 1215 | def visitAssList(self, node): |
paul@139 | 1216 | for i, n in enumerate(node.nodes): |
paul@139 | 1217 | self._startCallFunc() |
paul@139 | 1218 | self.new_op(self.expr_temp[-1]) |
paul@139 | 1219 | self._generateAttr(node, "__getitem__", self.attribute_load_instructions) |
paul@139 | 1220 | temp, target = self._generateCallFunc([compiler.ast.Const(i)], node) |
paul@139 | 1221 | self._doCallFunc(temp, target) |
paul@139 | 1222 | self._endCallFunc(temp, target) |
paul@139 | 1223 | |
paul@139 | 1224 | # Provide a different source value. |
paul@139 | 1225 | |
paul@143 | 1226 | self.record_value(0) |
paul@143 | 1227 | self.dispatch(n) |
paul@139 | 1228 | self.discard_value() |
paul@139 | 1229 | |
paul@143 | 1230 | def visitAssName(self, node): |
paul@106 | 1231 | self._visitName(node, self.name_store_instructions) |
paul@143 | 1232 | self.set_source() |
paul@56 | 1233 | |
paul@56 | 1234 | visitAssTuple = visitAssList |
paul@56 | 1235 | |
paul@151 | 1236 | def visitAugAssign(self, node): |
paul@151 | 1237 | use_binary_label = self.new_label() |
paul@151 | 1238 | end_label = self.new_label() |
paul@151 | 1239 | |
paul@151 | 1240 | # Evaluate the expression. |
paul@151 | 1241 | |
paul@151 | 1242 | self.dispatch(node.expr) |
paul@151 | 1243 | temp2 = self.optimiser.optimise_temp_storage() |
paul@151 | 1244 | |
paul@151 | 1245 | # Evaluate the target. |
paul@151 | 1246 | |
paul@151 | 1247 | self.dispatch(node.node) |
paul@151 | 1248 | temp1 = self.optimiser.optimise_temp_storage() |
paul@151 | 1249 | |
paul@151 | 1250 | # Find the augmented assignment method and attempt to use it. |
paul@151 | 1251 | |
paul@151 | 1252 | aug_method, (left_method, right_method) = self.augassign_methods[node.op] |
paul@151 | 1253 | temp_out = self._generateOpMethod(node, temp1, temp2, aug_method, use_binary_label, end_label) |
paul@151 | 1254 | self.discard_temp(temp_out) # NOTE: Will re-use the same storage. |
paul@151 | 1255 | |
paul@151 | 1256 | # Where no such method exists, use the binary operator methods. |
paul@151 | 1257 | |
paul@151 | 1258 | self.set_label(use_binary_label) |
paul@151 | 1259 | temp_out = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@151 | 1260 | |
paul@151 | 1261 | # Assign the result to the name. |
paul@151 | 1262 | |
paul@151 | 1263 | self.set_label(end_label) |
paul@151 | 1264 | self.new_op(temp_out) |
paul@151 | 1265 | self.record_value(1) |
paul@151 | 1266 | |
paul@151 | 1267 | if isinstance(node.node, compiler.ast.Name): |
paul@151 | 1268 | self.visitAssName(node.node) |
paul@151 | 1269 | elif isinstance(node.node, compiler.ast.Getattr): |
paul@151 | 1270 | self.visitAssAttr(node.node) |
paul@151 | 1271 | else: |
paul@151 | 1272 | raise TranslationNotImplementedError(self.module.full_name(), node, "AugAssign(Slice or Subscript)") |
paul@151 | 1273 | |
paul@151 | 1274 | self.discard_value() |
paul@151 | 1275 | |
paul@151 | 1276 | # Compilation duties... |
paul@151 | 1277 | |
paul@151 | 1278 | self.discard_temp(temp1) |
paul@151 | 1279 | self.discard_temp(temp2) |
paul@56 | 1280 | |
paul@100 | 1281 | def visitBackquote(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Backquote") |
paul@56 | 1282 | |
paul@85 | 1283 | def visitBitand(self, node): |
paul@85 | 1284 | self._visitBinary(node, "__and__", "__rand__") |
paul@56 | 1285 | |
paul@85 | 1286 | def visitBitor(self, node): |
paul@85 | 1287 | self._visitBinary(node, "__or__", "__ror__") |
paul@56 | 1288 | |
paul@85 | 1289 | def visitBitxor(self, node): |
paul@85 | 1290 | self._visitBinary(node, "__xor__", "__rxor__") |
paul@56 | 1291 | |
paul@56 | 1292 | def visitBreak(self, node): |
paul@57 | 1293 | next_label, exit_label = self.get_loop_labels() |
paul@56 | 1294 | self.new_op(Jump(exit_label)) |
paul@56 | 1295 | |
paul@56 | 1296 | def visitCallFunc(self, node): |
paul@56 | 1297 | |
paul@56 | 1298 | """ |
paul@56 | 1299 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@56 | 1300 | the correct location, then invoke the function. |
paul@56 | 1301 | """ |
paul@56 | 1302 | |
paul@56 | 1303 | # Mark the frame, evaluate the target, generate the call. |
paul@56 | 1304 | |
paul@56 | 1305 | self._startCallFunc() |
paul@56 | 1306 | self.dispatch(node.node) |
paul@137 | 1307 | temp, target = self._generateCallFunc(node.args, node) |
paul@137 | 1308 | self._doCallFunc(temp, target) |
paul@137 | 1309 | self._endCallFunc(temp, target) |
paul@56 | 1310 | |
paul@38 | 1311 | def visitClass(self, node): |
paul@90 | 1312 | |
paul@90 | 1313 | # Store the name. |
paul@90 | 1314 | |
paul@90 | 1315 | self.new_op(LoadConst(node.unit)) |
paul@104 | 1316 | self.record_value() |
paul@106 | 1317 | self._visitName(node, self.name_store_instructions) |
paul@104 | 1318 | self.set_source() |
paul@104 | 1319 | self.discard_value() |
paul@90 | 1320 | |
paul@90 | 1321 | # Visit the code. |
paul@90 | 1322 | |
paul@38 | 1323 | unit = self.unit |
paul@39 | 1324 | self.unit = node.unit |
paul@44 | 1325 | self.unit.code_location = self.module.code_location # class body code is not independently addressable |
paul@38 | 1326 | self.dispatch(node.code) |
paul@38 | 1327 | self.unit = unit |
paul@17 | 1328 | |
paul@21 | 1329 | def visitCompare(self, node): |
paul@21 | 1330 | |
paul@21 | 1331 | """ |
paul@112 | 1332 | _t1 = node.expr |
paul@112 | 1333 | _t1 op1 _t2 and _t2 op2 _t3 and ... |
paul@112 | 1334 | """ |
paul@112 | 1335 | |
paul@112 | 1336 | end_label = self.new_label() |
paul@112 | 1337 | |
paul@21 | 1338 | self.dispatch(node.expr) |
paul@140 | 1339 | temp2 = self.optimiser.optimise_temp_storage() |
paul@112 | 1340 | |
paul@113 | 1341 | last_op = node.ops[-1] |
paul@113 | 1342 | |
paul@113 | 1343 | for op in node.ops: |
paul@113 | 1344 | op_name, next_node = op |
paul@113 | 1345 | methods = self.comparison_methods[op_name] |
paul@112 | 1346 | |
paul@112 | 1347 | temp1 = temp2 |
paul@112 | 1348 | self.dispatch(next_node) |
paul@140 | 1349 | temp2 = self.optimiser.optimise_temp_storage() |
paul@112 | 1350 | |
paul@116 | 1351 | # Use the appropriate mechanism, setting the boolean status for the |
paul@116 | 1352 | # comparison. |
paul@116 | 1353 | |
paul@21 | 1354 | if methods is not None: |
paul@113 | 1355 | left_method, right_method = methods |
paul@112 | 1356 | |
paul@21 | 1357 | # Generate method call using evaluated argument and next node. |
paul@112 | 1358 | |
paul@151 | 1359 | temp_result = self._generateBinary(node, temp1, temp2, left_method, right_method) |
paul@130 | 1360 | self._generateTestBoolean(node, temp_result) |
paul@151 | 1361 | self.discard_temp(temp_result) |
paul@112 | 1362 | |
paul@21 | 1363 | else: |
paul@114 | 1364 | # Deal with the special operators. |
paul@114 | 1365 | |
paul@114 | 1366 | if op_name.startswith("is"): |
paul@114 | 1367 | self.new_op(temp1) |
paul@114 | 1368 | self.record_value() |
paul@114 | 1369 | self.new_op(temp2) |
paul@114 | 1370 | self.new_op(TestIdentity()) |
paul@114 | 1371 | self.set_source() |
paul@114 | 1372 | self.discard_value() |
paul@114 | 1373 | |
paul@114 | 1374 | elif op_name.endswith("in"): |
paul@114 | 1375 | self._startCallFunc() |
paul@114 | 1376 | self.new_op(temp2) |
paul@114 | 1377 | |
paul@114 | 1378 | # Get method on temp2. |
paul@114 | 1379 | |
paul@114 | 1380 | self._generateAttr(node, "__contains__", self.attribute_load_instructions) |
paul@140 | 1381 | temp_method = self.optimiser.optimise_temp_storage() |
paul@114 | 1382 | |
paul@114 | 1383 | # Add arguments. |
paul@114 | 1384 | # NOTE: No support for defaults. |
paul@114 | 1385 | |
paul@114 | 1386 | self.new_op(temp2) |
paul@114 | 1387 | self.new_op(StoreFrame(0)) |
paul@114 | 1388 | self.new_op(temp1) |
paul@114 | 1389 | self.new_op(StoreFrame(1)) |
paul@129 | 1390 | self._endCallFuncArgs(2) |
paul@114 | 1391 | self._doCallFunc(temp_method) |
paul@114 | 1392 | self._endCallFunc(temp_method) |
paul@130 | 1393 | |
paul@151 | 1394 | temp_result = self.get_temp() |
paul@130 | 1395 | self._generateTestBoolean(node, temp_result) |
paul@151 | 1396 | self.discard_temp(temp_result) |
paul@114 | 1397 | |
paul@114 | 1398 | if op_name.find("not") != -1: |
paul@114 | 1399 | self.new_op(InvertBoolean()) |
paul@114 | 1400 | |
paul@114 | 1401 | # Test the result and jump to the end label if false. |
paul@114 | 1402 | |
paul@114 | 1403 | if op is not last_op: |
paul@114 | 1404 | self.new_op(JumpIfFalse(end_label)) |
paul@112 | 1405 | |
paul@112 | 1406 | # Compilation duties... |
paul@112 | 1407 | |
paul@112 | 1408 | self.discard_temp(temp1) |
paul@112 | 1409 | |
paul@112 | 1410 | self.discard_temp(temp2) |
paul@112 | 1411 | self.set_label(end_label) |
paul@17 | 1412 | |
paul@116 | 1413 | # Yield the appropriate value. |
paul@116 | 1414 | |
paul@129 | 1415 | self._generateLoadBoolean(node) |
paul@113 | 1416 | |
paul@22 | 1417 | def visitConst(self, node): |
paul@22 | 1418 | const = self.module.constant_values[node.value] |
paul@22 | 1419 | self.new_op(LoadConst(const)) |
paul@17 | 1420 | |
paul@41 | 1421 | def visitContinue(self, node): |
paul@57 | 1422 | next_label, exit_label = self.get_loop_labels() |
paul@41 | 1423 | self.new_op(Jump(next_label)) |
paul@17 | 1424 | |
paul@100 | 1425 | def visitDecorators(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Decorators") |
paul@17 | 1426 | |
paul@100 | 1427 | def visitDict(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Dict") |
paul@17 | 1428 | |
paul@18 | 1429 | def visitDiscard(self, node): |
paul@19 | 1430 | self.dispatch(node.expr) |
paul@140 | 1431 | self.optimiser.optimise_unused_results() |
paul@17 | 1432 | |
paul@57 | 1433 | def visitDiv(self, node): |
paul@57 | 1434 | self._visitBinary(node, "__div__", "__rdiv__") |
paul@17 | 1435 | |
paul@100 | 1436 | def visitEllipsis(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Ellipsis") |
paul@17 | 1437 | |
paul@100 | 1438 | def visitExec(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Exec") |
paul@17 | 1439 | |
paul@100 | 1440 | def visitExpression(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Expression") |
paul@17 | 1441 | |
paul@57 | 1442 | def visitFloorDiv(self, node): |
paul@57 | 1443 | self._visitBinary(node, "__floordiv__", "__rfloordiv__") |
paul@17 | 1444 | |
paul@44 | 1445 | def visitFor(self, node): |
paul@146 | 1446 | next_handler_label = self.new_label() |
paul@146 | 1447 | end_handler_label = self.new_label() |
paul@44 | 1448 | exit_label = self.new_label() |
paul@44 | 1449 | next_label = self.new_label() |
paul@44 | 1450 | else_label = self.new_label() |
paul@44 | 1451 | |
paul@44 | 1452 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 1453 | |
paul@45 | 1454 | self._startCallFunc() |
paul@44 | 1455 | self.dispatch(node.list) |
paul@104 | 1456 | self._generateAttr(node, "__iter__", self.attribute_load_instructions) |
paul@137 | 1457 | temp, target = self._generateCallFunc([], node) |
paul@137 | 1458 | self._doCallFunc(temp, target) |
paul@137 | 1459 | self._endCallFunc(temp, target) |
paul@57 | 1460 | |
paul@140 | 1461 | temp_iterator = self.optimiser.optimise_temp_storage() |
paul@98 | 1462 | |
paul@44 | 1463 | # In the loop... |
paul@44 | 1464 | |
paul@44 | 1465 | self.set_label(next_label) |
paul@44 | 1466 | |
paul@146 | 1467 | # Handle exceptions when calling "next"... |
paul@146 | 1468 | |
paul@146 | 1469 | self.new_op(PushHandler(next_handler_label)) |
paul@146 | 1470 | |
paul@44 | 1471 | # Use the iterator to get the next value. |
paul@44 | 1472 | |
paul@45 | 1473 | self._startCallFunc() |
paul@102 | 1474 | self.new_op(temp_iterator) |
paul@104 | 1475 | self._generateAttr(node, "next", self.attribute_load_instructions) |
paul@137 | 1476 | temp, target = self._generateCallFunc([], node) |
paul@137 | 1477 | self._doCallFunc(temp, target) |
paul@137 | 1478 | self._endCallFunc(temp, target) |
paul@44 | 1479 | |
paul@139 | 1480 | # Record the value to be assigned. |
paul@139 | 1481 | |
paul@139 | 1482 | self.record_value() |
paul@139 | 1483 | |
paul@146 | 1484 | # Skip the handler where the call was successful. |
paul@146 | 1485 | |
paul@146 | 1486 | self.new_op(Jump(end_handler_label)) |
paul@146 | 1487 | |
paul@146 | 1488 | # Enter the exception handler. |
paul@146 | 1489 | |
paul@146 | 1490 | self.set_label(next_handler_label) |
paul@146 | 1491 | self.new_op(PopHandler()) |
paul@146 | 1492 | |
paul@44 | 1493 | # Test for StopIteration. |
paul@44 | 1494 | |
paul@101 | 1495 | self.load_builtin("StopIteration", node) |
paul@57 | 1496 | self.new_op(CheckException()) |
paul@44 | 1497 | if node.else_ is not None: |
paul@44 | 1498 | self.new_op(JumpIfTrue(else_label)) |
paul@44 | 1499 | else: |
paul@44 | 1500 | self.new_op(JumpIfTrue(exit_label)) |
paul@44 | 1501 | |
paul@146 | 1502 | # Re-raise the exception otherwise. |
paul@146 | 1503 | |
paul@146 | 1504 | self.new_op(RaiseException()) |
paul@146 | 1505 | |
paul@146 | 1506 | # After the handler. |
paul@146 | 1507 | |
paul@146 | 1508 | self.set_label(end_handler_label) |
paul@146 | 1509 | |
paul@44 | 1510 | # Assign to the target. |
paul@44 | 1511 | |
paul@44 | 1512 | self.dispatch(node.assign) |
paul@139 | 1513 | self.discard_value() |
paul@44 | 1514 | |
paul@44 | 1515 | # Process the body with the current next and exit points. |
paul@44 | 1516 | |
paul@57 | 1517 | self.add_loop_labels(next_label, exit_label) |
paul@44 | 1518 | self.dispatch(node.body) |
paul@57 | 1519 | self.drop_loop_labels() |
paul@44 | 1520 | |
paul@44 | 1521 | # Repeat the loop. |
paul@44 | 1522 | |
paul@44 | 1523 | self.new_op(Jump(next_label)) |
paul@44 | 1524 | |
paul@44 | 1525 | # Produce the "else" section. |
paul@44 | 1526 | |
paul@44 | 1527 | if node.else_ is not None: |
paul@44 | 1528 | self.set_label(exit_label) |
paul@44 | 1529 | self.dispatch(node.else_) |
paul@44 | 1530 | |
paul@98 | 1531 | # After the loop... |
paul@44 | 1532 | |
paul@44 | 1533 | self.set_label(exit_label) |
paul@17 | 1534 | |
paul@98 | 1535 | # Compilation duties... |
paul@98 | 1536 | |
paul@98 | 1537 | self.discard_temp(temp_iterator) |
paul@98 | 1538 | |
paul@19 | 1539 | def visitFrom(self, node): pass |
paul@17 | 1540 | |
paul@17 | 1541 | def visitFunction(self, node): |
paul@17 | 1542 | |
paul@17 | 1543 | # Only store the name when visiting this node from outside. |
paul@17 | 1544 | |
paul@39 | 1545 | if self.unit is not node.unit: |
paul@55 | 1546 | self.new_op(LoadConst(node.unit)) |
paul@104 | 1547 | |
paul@104 | 1548 | self.record_value() |
paul@108 | 1549 | self._visitName(node, self.name_store_instructions) # AssName equivalent |
paul@104 | 1550 | self.set_source() |
paul@104 | 1551 | self.discard_value() |
paul@22 | 1552 | |
paul@108 | 1553 | self._generateFunctionDefaults(node.unit) |
paul@73 | 1554 | |
paul@22 | 1555 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 1556 | |
paul@17 | 1557 | else: |
paul@132 | 1558 | extend = ExtendFrame() |
paul@132 | 1559 | self.new_op(extend) |
paul@132 | 1560 | |
paul@19 | 1561 | self.dispatch(node.code) |
paul@68 | 1562 | if not isinstance(self.last_op(), Return): |
paul@68 | 1563 | self.dispatch(compiler.ast.Name("None")) |
paul@104 | 1564 | self.new_op(StoreResult()) |
paul@104 | 1565 | |
paul@68 | 1566 | self.new_op(Return()) |
paul@17 | 1567 | |
paul@137 | 1568 | self.set_frame_usage(node, extend) |
paul@132 | 1569 | |
paul@100 | 1570 | def visitGenExpr(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExpr") |
paul@17 | 1571 | |
paul@100 | 1572 | def visitGenExprFor(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExprFor") |
paul@17 | 1573 | |
paul@100 | 1574 | def visitGenExprIf(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExprIf") |
paul@17 | 1575 | |
paul@100 | 1576 | def visitGenExprInner(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "GenExprInner") |
paul@17 | 1577 | |
paul@43 | 1578 | def visitGetattr(self, node): |
paul@98 | 1579 | self._visitAttr(node, self.attribute_load_instructions) |
paul@17 | 1580 | |
paul@19 | 1581 | def visitGlobal(self, node): pass |
paul@17 | 1582 | |
paul@17 | 1583 | def visitIf(self, node): |
paul@19 | 1584 | first = 1 |
paul@19 | 1585 | exit_label = self.new_label() |
paul@17 | 1586 | |
paul@108 | 1587 | clauses = node.tests + [(None, node.else_)] |
paul@108 | 1588 | last_clause = clauses[-1] |
paul@108 | 1589 | |
paul@108 | 1590 | for clause in clauses: |
paul@108 | 1591 | test, body = clause |
paul@19 | 1592 | if body is None: |
paul@19 | 1593 | break |
paul@19 | 1594 | if not first: |
paul@19 | 1595 | self.set_label(next_label) |
paul@19 | 1596 | if test is not None: |
paul@19 | 1597 | self.dispatch(test) |
paul@19 | 1598 | next_label = self.new_label() |
paul@41 | 1599 | self.new_op(JumpIfFalse(next_label)) |
paul@19 | 1600 | self.dispatch(body) |
paul@108 | 1601 | if clause is not last_clause: |
paul@108 | 1602 | self.new_op(Jump(exit_label)) |
paul@19 | 1603 | first = 0 |
paul@17 | 1604 | |
paul@19 | 1605 | self.set_label(exit_label) |
paul@17 | 1606 | |
paul@19 | 1607 | def visitImport(self, node): pass |
paul@19 | 1608 | |
paul@85 | 1609 | def visitInvert(self, node): |
paul@85 | 1610 | self._visitUnary(node, "__invert__") |
paul@17 | 1611 | |
paul@19 | 1612 | def visitKeyword(self, node): pass |
paul@17 | 1613 | |
paul@108 | 1614 | def visitLambda(self, node): |
paul@108 | 1615 | |
paul@112 | 1616 | """ |
paul@112 | 1617 | Lambda functions can be represented as globally defined functions |
paul@112 | 1618 | provided they do not define any default parameter values, since these |
paul@112 | 1619 | may defined in a non-global scope. |
paul@112 | 1620 | |
paul@112 | 1621 | Where defaults are defined, an object must be created and its content |
paul@112 | 1622 | defined: the callable member of the object's structure must be set to |
paul@112 | 1623 | the lambda function definition; each default must be attached to the |
paul@112 | 1624 | object as an attribute, as is the case with normal functions and |
paul@112 | 1625 | methods. |
paul@112 | 1626 | """ |
paul@112 | 1627 | |
paul@108 | 1628 | # Produce the reference to this function when visiting this node from |
paul@108 | 1629 | # outside. |
paul@108 | 1630 | |
paul@108 | 1631 | if self.unit is not node.unit: |
paul@109 | 1632 | temp = self._generateFunctionDefaults(node.unit) |
paul@108 | 1633 | self.new_op(LoadConst(node.unit)) |
paul@108 | 1634 | |
paul@109 | 1635 | # Populate the new object required for the function. |
paul@109 | 1636 | |
paul@109 | 1637 | if temp is not None: |
paul@134 | 1638 | self.new_op(LoadCallable()) |
paul@109 | 1639 | self.new_op(temp) |
paul@109 | 1640 | self.new_op(StoreCallable()) |
paul@109 | 1641 | |
paul@109 | 1642 | self.new_op(temp) |
paul@116 | 1643 | #self.discard_temp(temp) |
paul@109 | 1644 | |
paul@108 | 1645 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@108 | 1646 | |
paul@108 | 1647 | else: |
paul@108 | 1648 | self.dispatch(node.code) |
paul@108 | 1649 | self.new_op(StoreResult()) |
paul@108 | 1650 | self.new_op(Return()) |
paul@19 | 1651 | |
paul@85 | 1652 | def visitLeftShift(self, node): |
paul@85 | 1653 | self._visitBinary(node, "__lshift__", "__rlshift__") |
paul@17 | 1654 | |
paul@112 | 1655 | def visitList(self, node): |
paul@114 | 1656 | self._generateSequence("list", node) |
paul@17 | 1657 | |
paul@100 | 1658 | def visitListComp(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "ListComp") |
paul@17 | 1659 | |
paul@100 | 1660 | def visitListCompFor(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "ListCompFor") |
paul@17 | 1661 | |
paul@100 | 1662 | def visitListCompIf(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "ListCompIf") |
paul@19 | 1663 | |
paul@57 | 1664 | def visitMod(self, node): |
paul@57 | 1665 | self._visitBinary(node, "__mod__", "__rmod__") |
paul@17 | 1666 | |
paul@17 | 1667 | def visitModule(self, node): |
paul@143 | 1668 | extend = ExtendFrame() |
paul@143 | 1669 | self.new_op(extend) |
paul@19 | 1670 | self.dispatch(node.node) |
paul@143 | 1671 | self.set_frame_usage(node, extend) |
paul@17 | 1672 | |
paul@57 | 1673 | def visitMul(self, node): |
paul@57 | 1674 | self._visitBinary(node, "__mul__", "__rmul__") |
paul@17 | 1675 | |
paul@17 | 1676 | def visitName(self, node): |
paul@82 | 1677 | if node.name == "None": |
paul@82 | 1678 | const = self.module.constant_values[None] |
paul@82 | 1679 | self.new_op(LoadConst(const)) |
paul@82 | 1680 | else: |
paul@106 | 1681 | self._visitName(node, self.name_load_instructions) |
paul@17 | 1682 | |
paul@101 | 1683 | def visitNot(self, node): |
paul@101 | 1684 | self.dispatch(node.expr) |
paul@130 | 1685 | |
paul@140 | 1686 | temp = self.optimiser.optimise_temp_storage() |
paul@130 | 1687 | self._generateTestBoolean(node.expr, temp) |
paul@130 | 1688 | self.discard_temp(temp) |
paul@130 | 1689 | |
paul@116 | 1690 | self.new_op(InvertBoolean()) |
paul@129 | 1691 | self._generateLoadBoolean(node) |
paul@101 | 1692 | |
paul@101 | 1693 | # Prevent incorrect optimisation. |
paul@17 | 1694 | |
paul@154 | 1695 | self.optimiser.reset() |
paul@101 | 1696 | |
paul@101 | 1697 | def visitOr(self, node): |
paul@130 | 1698 | end_label = self.new_label() |
paul@130 | 1699 | temp_pos = self.reserve_temp() |
paul@130 | 1700 | temp = LoadTemp(temp_pos) |
paul@101 | 1701 | |
paul@101 | 1702 | for n in node.nodes[:-1]: |
paul@101 | 1703 | self.dispatch(n) |
paul@130 | 1704 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 1705 | |
paul@130 | 1706 | self._generateTestBoolean(n, temp) |
paul@130 | 1707 | self.new_op(JumpIfTrue(end_label)) |
paul@101 | 1708 | |
paul@101 | 1709 | self.dispatch(node.nodes[-1]) |
paul@130 | 1710 | self.new_op(StoreTemp(temp_pos)) |
paul@130 | 1711 | |
paul@130 | 1712 | self.set_label(end_label) |
paul@101 | 1713 | |
paul@101 | 1714 | # Prevent incorrect optimisation. |
paul@101 | 1715 | |
paul@154 | 1716 | self.optimiser.reset() |
paul@130 | 1717 | |
paul@130 | 1718 | self.new_op(temp) |
paul@130 | 1719 | self.discard_temp(temp) |
paul@17 | 1720 | |
paul@19 | 1721 | def visitPass(self, node): pass |
paul@17 | 1722 | |
paul@85 | 1723 | def visitPower(self, node): |
paul@85 | 1724 | self._visitBinary(node, "__pow__", "__rpow__") |
paul@17 | 1725 | |
paul@100 | 1726 | def visitPrint(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Print") |
paul@17 | 1727 | |
paul@100 | 1728 | def visitPrintnl(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Printnl") |
paul@17 | 1729 | |
paul@97 | 1730 | def visitRaise(self, node): |
paul@97 | 1731 | # NOTE: expr1 only => instance provided |
paul@97 | 1732 | self.dispatch(node.expr1) |
paul@97 | 1733 | |
paul@97 | 1734 | if node.expr2 is not None: |
paul@140 | 1735 | temp = self.optimiser.optimise_temp_storage() |
paul@102 | 1736 | |
paul@97 | 1737 | self.dispatch(node.expr2) |
paul@140 | 1738 | temp_arg = self.optimiser.optimise_temp_storage() |
paul@102 | 1739 | |
paul@102 | 1740 | self._startCallFunc() |
paul@102 | 1741 | self.new_op(temp_arg) |
paul@102 | 1742 | self.new_op(StoreFrame(0)) |
paul@129 | 1743 | self._endCallFuncArgs(1) |
paul@102 | 1744 | self._doCallFunc(temp) |
paul@102 | 1745 | self._endCallFunc(temp) |
paul@102 | 1746 | |
paul@102 | 1747 | self.discard_temp(temp_arg) |
paul@97 | 1748 | |
paul@105 | 1749 | self.new_op(StoreException()) |
paul@97 | 1750 | self.new_op(RaiseException()) |
paul@17 | 1751 | |
paul@21 | 1752 | def visitReturn(self, node): |
paul@21 | 1753 | if node.value is not None: |
paul@21 | 1754 | self.dispatch(node.value) |
paul@68 | 1755 | else: |
paul@68 | 1756 | self.dispatch(compiler.ast.Name("None")) |
paul@104 | 1757 | |
paul@104 | 1758 | self.new_op(StoreResult()) |
paul@21 | 1759 | self.new_op(Return()) |
paul@17 | 1760 | |
paul@85 | 1761 | def visitRightShift(self, node): |
paul@85 | 1762 | self._visitBinary(node, "__rshift__", "__rrshift__") |
paul@17 | 1763 | |
paul@100 | 1764 | def visitSlice(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Slice") |
paul@17 | 1765 | |
paul@17 | 1766 | def visitStmt(self, node): |
paul@17 | 1767 | for n in node.nodes: |
paul@19 | 1768 | self.dispatch(n) |
paul@143 | 1769 | if self.temp_positions: |
paul@143 | 1770 | #print "Had temp", self.temp_positions |
paul@143 | 1771 | self.temp_positions = set() |
paul@19 | 1772 | |
paul@57 | 1773 | def visitSub(self, node): |
paul@57 | 1774 | self._visitBinary(node, "__sub__", "__rsub__") |
paul@17 | 1775 | |
paul@100 | 1776 | def visitSubscript(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Subscript") |
paul@17 | 1777 | |
paul@57 | 1778 | def visitTryExcept(self, node): |
paul@57 | 1779 | exit_label = self.new_label() |
paul@102 | 1780 | success_label = self.new_label() |
paul@57 | 1781 | handler_label = self.new_label() |
paul@57 | 1782 | |
paul@57 | 1783 | self.add_exception_labels(handler_label, exit_label) |
paul@57 | 1784 | |
paul@68 | 1785 | # Try... |
paul@68 | 1786 | # Produce the code, then jump to the exit. |
paul@68 | 1787 | |
paul@102 | 1788 | self.new_op(PushHandler(handler_label)) |
paul@57 | 1789 | self.dispatch(node.body) |
paul@103 | 1790 | self.new_op(PopHandler()) |
paul@57 | 1791 | self.new_op(Jump(exit_label)) |
paul@57 | 1792 | |
paul@68 | 1793 | # Start of handlers. |
paul@68 | 1794 | |
paul@57 | 1795 | self.set_label(handler_label) |
paul@103 | 1796 | self.new_op(PopHandler()) |
paul@102 | 1797 | |
paul@57 | 1798 | for name, assignment, handler in node.handlers: |
paul@57 | 1799 | next_label = self.new_label() |
paul@57 | 1800 | |
paul@68 | 1801 | # Test the given exception against the current exception. |
paul@68 | 1802 | |
paul@57 | 1803 | if name is not None: |
paul@57 | 1804 | self.dispatch(name) |
paul@145 | 1805 | |
paul@57 | 1806 | self.new_op(CheckException()) |
paul@57 | 1807 | self.new_op(JumpIfFalse(next_label)) |
paul@57 | 1808 | |
paul@145 | 1809 | # Handle assignment to exception variable. |
paul@145 | 1810 | |
paul@145 | 1811 | if assignment is not None: |
paul@145 | 1812 | self.new_op(LoadException()) |
paul@145 | 1813 | |
paul@145 | 1814 | # Record the value to be assigned. |
paul@145 | 1815 | |
paul@145 | 1816 | self.record_value() |
paul@145 | 1817 | self.dispatch(assignment) |
paul@145 | 1818 | self.discard_value() |
paul@57 | 1819 | |
paul@68 | 1820 | # Produce the handler code, then jump to the exit. |
paul@68 | 1821 | |
paul@57 | 1822 | self.dispatch(handler) |
paul@57 | 1823 | self.new_op(Jump(exit_label)) |
paul@57 | 1824 | |
paul@57 | 1825 | self.set_label(next_label) |
paul@57 | 1826 | |
paul@57 | 1827 | # Unhandled exceptions. |
paul@57 | 1828 | |
paul@57 | 1829 | self.new_op(RaiseException()) |
paul@57 | 1830 | |
paul@57 | 1831 | # Optional else clause. |
paul@57 | 1832 | |
paul@57 | 1833 | if node.else_ is not None: |
paul@57 | 1834 | self.dispatch(node.else_) |
paul@57 | 1835 | |
paul@102 | 1836 | self.set_label(exit_label) |
paul@57 | 1837 | self.drop_exception_labels() |
paul@17 | 1838 | |
paul@100 | 1839 | def visitTryFinally(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "TryFinally") |
paul@17 | 1840 | |
paul@110 | 1841 | def visitTuple(self, node): |
paul@114 | 1842 | self._generateSequence("tuple", node) |
paul@17 | 1843 | |
paul@85 | 1844 | def visitUnaryAdd(self, node): |
paul@85 | 1845 | self._visitUnary(node, "__pos__") |
paul@17 | 1846 | |
paul@85 | 1847 | def visitUnarySub(self, node): |
paul@85 | 1848 | self._visitUnary(node, "__neg__") |
paul@17 | 1849 | |
paul@41 | 1850 | def visitWhile(self, node): |
paul@41 | 1851 | exit_label = self.new_label() |
paul@44 | 1852 | next_label = self.new_label() |
paul@44 | 1853 | else_label = self.new_label() |
paul@41 | 1854 | |
paul@41 | 1855 | self.set_label(next_label) |
paul@44 | 1856 | self.dispatch(node.test) |
paul@44 | 1857 | if node.else_ is not None: |
paul@44 | 1858 | self.new_op(JumpIfFalse(else_label)) |
paul@44 | 1859 | else: |
paul@44 | 1860 | self.new_op(JumpIfFalse(exit_label)) |
paul@44 | 1861 | |
paul@57 | 1862 | self.add_loop_labels(next_label, exit_label) |
paul@41 | 1863 | |
paul@41 | 1864 | self.dispatch(node.body) |
paul@41 | 1865 | self.new_op(Jump(next_label)) |
paul@41 | 1866 | |
paul@41 | 1867 | if node.else_ is not None: |
paul@44 | 1868 | self.set_label(else_label) |
paul@41 | 1869 | self.dispatch(node.else_) |
paul@41 | 1870 | |
paul@41 | 1871 | self.set_label(exit_label) |
paul@57 | 1872 | self.drop_loop_labels() |
paul@17 | 1873 | |
paul@101 | 1874 | # Prevent incorrect optimisation. |
paul@101 | 1875 | |
paul@154 | 1876 | self.optimiser.reset() |
paul@101 | 1877 | |
paul@100 | 1878 | def visitWith(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "With") |
paul@17 | 1879 | |
paul@100 | 1880 | def visitYield(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Yield") |
paul@17 | 1881 | |
paul@21 | 1882 | # Useful data. |
paul@21 | 1883 | |
paul@21 | 1884 | comparison_methods = { |
paul@21 | 1885 | "==" : ("__eq__", "__ne__"), |
paul@21 | 1886 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 1887 | "<" : ("__lt__", "__gt__"), |
paul@21 | 1888 | "<=" : ("__le__", "__ge__"), |
paul@21 | 1889 | ">=" : ("__ge__", "__le__"), |
paul@21 | 1890 | ">" : ("__gt__", "__lt__"), |
paul@21 | 1891 | "is" : None, |
paul@21 | 1892 | "is not" : None, |
paul@21 | 1893 | "in" : None, |
paul@21 | 1894 | "not in" : None |
paul@21 | 1895 | } |
paul@21 | 1896 | |
paul@151 | 1897 | augassign_methods = { |
paul@151 | 1898 | "+=" : ("__iadd__", ("__add__", "__radd__")), |
paul@151 | 1899 | "-=" : ("__isub__", ("__sub__", "__rsub__")), |
paul@151 | 1900 | "*=" : ("__imul__", ("__mul__", "__rmul__")), |
paul@151 | 1901 | "/=" : ("__idiv__", ("__div__", "__rdiv__")), |
paul@151 | 1902 | "%=" : ("__imod__", ("__mod__", "__rmod__")), |
paul@151 | 1903 | "**=" : ("__ipow__", ("__pow__", "__rpow__")), |
paul@151 | 1904 | "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), |
paul@151 | 1905 | ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), |
paul@151 | 1906 | "&=" : ("__iand__", ("__and__", "__rand__")), |
paul@151 | 1907 | "^=" : ("__ixor__", ("__xor__", "__rxor__")), |
paul@151 | 1908 | "|=" : ("__ior__", ("__or__", "__ror__")) |
paul@151 | 1909 | } |
paul@151 | 1910 | |
paul@17 | 1911 | # vim: tabstop=4 expandtab shiftwidth=4 |