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@83 | 22 | from micropython.common import * |
paul@83 | 23 | from micropython.data import * |
paul@17 | 24 | from micropython.rsvp import * |
paul@17 | 25 | import compiler.ast |
paul@17 | 26 | from compiler.visitor import ASTVisitor |
paul@17 | 27 | |
paul@17 | 28 | # Program visitors. |
paul@17 | 29 | |
paul@17 | 30 | class Translation(ASTVisitor): |
paul@17 | 31 | |
paul@17 | 32 | "A translated module." |
paul@17 | 33 | |
paul@93 | 34 | supported_optimisations = ["constant_storage", "known_target", "self_access", "temp_storage", "constant_test", "stack_access"] |
paul@62 | 35 | |
paul@72 | 36 | def __init__(self, module, importer, optimisations=None): |
paul@18 | 37 | |
paul@21 | 38 | """ |
paul@72 | 39 | Initialise the translation with an inspected 'module', the 'importer' |
paul@72 | 40 | and optional 'optimisations'. See the 'supported_optimisations' |
paul@72 | 41 | attribute of this class for permitted values. |
paul@21 | 42 | """ |
paul@18 | 43 | |
paul@17 | 44 | ASTVisitor.__init__(self) |
paul@17 | 45 | self.visitor = self |
paul@17 | 46 | self.module = module |
paul@72 | 47 | |
paul@72 | 48 | # Global program dependencies. |
paul@72 | 49 | |
paul@72 | 50 | self.importer = importer |
paul@72 | 51 | self.objtable = self.importer.get_object_table() |
paul@72 | 52 | self.paramtable = self.importer.get_parameter_table() |
paul@72 | 53 | self.builtins = self.importer.modules.get("__builtins__") |
paul@61 | 54 | |
paul@62 | 55 | # Desired optimisations. |
paul@62 | 56 | |
paul@62 | 57 | self.optimisations = set(optimisations or []) |
paul@62 | 58 | |
paul@61 | 59 | # The current unit being translated. |
paul@61 | 60 | |
paul@17 | 61 | self.unit = None |
paul@17 | 62 | |
paul@19 | 63 | # Wiring within the code. |
paul@19 | 64 | |
paul@19 | 65 | self.labels = {} |
paul@19 | 66 | self.label_number = 0 |
paul@41 | 67 | self.loop_labels = [] |
paul@57 | 68 | self.exception_labels = [] |
paul@41 | 69 | |
paul@72 | 70 | # The code itself. This is limited to the code for a particular block |
paul@93 | 71 | # being processed. Also retained is information about temporary values |
paul@93 | 72 | # and the presumed state of the value stack. |
paul@41 | 73 | |
paul@19 | 74 | self.code = None |
paul@73 | 75 | self.temp_position = 0 |
paul@93 | 76 | self.stack = [] |
paul@19 | 77 | |
paul@95 | 78 | def __repr__(self): |
paul@95 | 79 | return "Translation(%r)" % self.module |
paul@72 | 80 | |
paul@17 | 81 | def get_module_code(self): |
paul@17 | 82 | |
paul@17 | 83 | "Return the top-level module code." |
paul@17 | 84 | |
paul@17 | 85 | self.unit = self.module |
paul@19 | 86 | self.code = [] |
paul@73 | 87 | self.temp_position = self.unit.stack_local_usage |
paul@93 | 88 | self.stack = [] |
paul@73 | 89 | |
paul@44 | 90 | if self.module.module is not None: |
paul@44 | 91 | self.dispatch(self.module.module) |
paul@73 | 92 | |
paul@19 | 93 | return self.code |
paul@17 | 94 | |
paul@17 | 95 | def get_code(self, unit): |
paul@17 | 96 | |
paul@17 | 97 | "Return the code for the given 'unit'." |
paul@17 | 98 | |
paul@17 | 99 | self.unit = unit |
paul@19 | 100 | self.code = [] |
paul@73 | 101 | self.temp_position = self.unit.stack_local_usage |
paul@93 | 102 | self.stack = [] |
paul@73 | 103 | |
paul@91 | 104 | if unit.astnode is not None: |
paul@91 | 105 | self.dispatch(unit.astnode) |
paul@73 | 106 | |
paul@19 | 107 | return self.code |
paul@17 | 108 | |
paul@17 | 109 | def get_scope(self, name): |
paul@17 | 110 | if self.unit.has_key(name): |
paul@17 | 111 | return "local" |
paul@17 | 112 | elif self.module.has_key(name): |
paul@17 | 113 | return "global" |
paul@17 | 114 | else: |
paul@17 | 115 | return "builtins" |
paul@17 | 116 | |
paul@95 | 117 | # Code feature methods. |
paul@95 | 118 | |
paul@95 | 119 | def reset_stack(self): |
paul@95 | 120 | |
paul@95 | 121 | "Reset the stack for the current unit." |
paul@95 | 122 | |
paul@95 | 123 | self.stack = [] |
paul@95 | 124 | |
paul@95 | 125 | def adjust_stack(self, op): |
paul@95 | 126 | |
paul@95 | 127 | "Adjust the stack according to the effect of 'op'." |
paul@95 | 128 | |
paul@95 | 129 | position = len(self.code) |
paul@95 | 130 | stack_level = len(self.stack) |
paul@95 | 131 | |
paul@95 | 132 | op.fix_stack(stack_level) |
paul@95 | 133 | effect = op.get_effect() |
paul@95 | 134 | |
paul@95 | 135 | if effect == 1: |
paul@95 | 136 | self.stack.append((position, op)) |
paul@95 | 137 | elif effect < 0: |
paul@95 | 138 | while effect != 0: |
paul@95 | 139 | self.stack.pop() |
paul@95 | 140 | effect += 1 |
paul@95 | 141 | |
paul@95 | 142 | # Update the stack levels. |
paul@95 | 143 | |
paul@95 | 144 | self.unit.stack_usage = max(self.unit.stack_usage, stack_level) |
paul@95 | 145 | |
paul@95 | 146 | def revert_stack(self, op): |
paul@95 | 147 | |
paul@95 | 148 | "Revert the stack affected by 'op'." |
paul@95 | 149 | |
paul@95 | 150 | effect = op.get_effect() |
paul@95 | 151 | |
paul@95 | 152 | if effect > 0: |
paul@95 | 153 | while effect != 0: |
paul@95 | 154 | self.stack.pop() |
paul@95 | 155 | effect -= 1 |
paul@95 | 156 | elif effect < 0: |
paul@95 | 157 | raise ProcessingError, "Cannot remove instructions which reduce the stack." |
paul@19 | 158 | |
paul@19 | 159 | def new_label(self): |
paul@44 | 160 | |
paul@44 | 161 | "Return a new label object for use with set_label." |
paul@44 | 162 | |
paul@19 | 163 | number = self.label_number |
paul@19 | 164 | label = Label(number) |
paul@19 | 165 | self.labels[label] = label |
paul@19 | 166 | self.label_number += 1 |
paul@19 | 167 | return label |
paul@19 | 168 | |
paul@19 | 169 | def set_label(self, label): |
paul@19 | 170 | |
paul@19 | 171 | """ |
paul@19 | 172 | Set the location of 'label' to that within the entire image: the |
paul@19 | 173 | location within the code combined with location of the code unit. |
paul@19 | 174 | """ |
paul@19 | 175 | |
paul@19 | 176 | label.location = len(self.code) + self.unit.code_location |
paul@19 | 177 | |
paul@57 | 178 | def get_loop_labels(self): |
paul@57 | 179 | return self.loop_labels[-1] |
paul@57 | 180 | |
paul@57 | 181 | def add_loop_labels(self, next_label, exit_label): |
paul@57 | 182 | self.loop_labels.append((next_label, exit_label)) |
paul@57 | 183 | |
paul@57 | 184 | def drop_loop_labels(self): |
paul@57 | 185 | self.loop_labels.pop() |
paul@57 | 186 | |
paul@57 | 187 | def get_exception_labels(self): |
paul@57 | 188 | return self.exception_labels[-1] |
paul@57 | 189 | |
paul@57 | 190 | def add_exception_labels(self, handler_label, exit_label): |
paul@57 | 191 | self.exception_labels.append((handler_label, exit_label)) |
paul@57 | 192 | |
paul@57 | 193 | def drop_exception_labels(self): |
paul@57 | 194 | self.exception_labels.pop() |
paul@57 | 195 | |
paul@73 | 196 | def reserve_temp(self, n): |
paul@73 | 197 | temp_position = self.temp_position |
paul@73 | 198 | self.temp_position += n |
paul@73 | 199 | self.unit.stack_temp_usage = max(self.unit.stack_temp_usage, self.temp_position) |
paul@73 | 200 | return temp_position |
paul@73 | 201 | |
paul@78 | 202 | def discard_temp(self, instructions): |
paul@78 | 203 | for temp in instructions: |
paul@78 | 204 | if isinstance(temp, LoadTemp): |
paul@78 | 205 | self.temp_position -= 1 |
paul@73 | 206 | |
paul@95 | 207 | # Code writing methods. |
paul@95 | 208 | |
paul@19 | 209 | def new_op(self, op): |
paul@44 | 210 | |
paul@44 | 211 | "Add 'op' to the generated code." |
paul@44 | 212 | |
paul@93 | 213 | position = len(self.code) |
paul@93 | 214 | self._optimise_stack_access(op) |
paul@95 | 215 | self.adjust_stack(op) |
paul@93 | 216 | |
paul@19 | 217 | self.code.append(op) |
paul@19 | 218 | |
paul@79 | 219 | def new_ops(self, ops): |
paul@79 | 220 | |
paul@95 | 221 | """ |
paul@95 | 222 | Add copies of 'ops' to the generated code. This is typically used in |
paul@95 | 223 | connection with sequences of temporary storage instructions. |
paul@95 | 224 | """ |
paul@79 | 225 | |
paul@93 | 226 | for op in ops: |
paul@95 | 227 | self.new_op(op.copy()) |
paul@95 | 228 | |
paul@95 | 229 | def remove_ops(self, n): |
paul@95 | 230 | |
paul@95 | 231 | "Remove the last 'n' instructions." |
paul@95 | 232 | |
paul@95 | 233 | for i in range(0, n): |
paul@95 | 234 | op = self.code.pop() |
paul@95 | 235 | self.revert_stack(op) |
paul@79 | 236 | |
paul@43 | 237 | def replace_op(self, op): |
paul@44 | 238 | |
paul@44 | 239 | "Replace the last added instruction with 'op'." |
paul@44 | 240 | |
paul@93 | 241 | self.remove_ops(1) |
paul@93 | 242 | self.new_op(op) |
paul@93 | 243 | |
paul@93 | 244 | def remove_op_using_stack(self): |
paul@93 | 245 | |
paul@93 | 246 | "Remove the instruction which created the top stack position." |
paul@93 | 247 | |
paul@93 | 248 | position, op = self.stack.pop() |
paul@93 | 249 | |
paul@93 | 250 | # NOTE: For now, just erase the instruction. |
paul@93 | 251 | |
paul@93 | 252 | self.code[position] = None |
paul@93 | 253 | |
paul@93 | 254 | op = self.code[-1] |
paul@93 | 255 | while op is None: |
paul@93 | 256 | del self.code[-1] |
paul@93 | 257 | if self.code: |
paul@93 | 258 | op = self.code[-1] |
paul@93 | 259 | else: |
paul@93 | 260 | break |
paul@43 | 261 | |
paul@81 | 262 | def last_ops(self, n): |
paul@56 | 263 | |
paul@56 | 264 | "Return the last 'n' added instructions in reverse chronological order." |
paul@56 | 265 | |
paul@81 | 266 | ops = self.code[-n:] |
paul@56 | 267 | ops.reverse() |
paul@56 | 268 | return ops |
paul@56 | 269 | |
paul@81 | 270 | def last_op(self): |
paul@44 | 271 | |
paul@44 | 272 | "Return the last added instruction." |
paul@44 | 273 | |
paul@70 | 274 | try: |
paul@81 | 275 | return self.code[-1] |
paul@70 | 276 | except IndexError: |
paul@70 | 277 | return None |
paul@26 | 278 | |
paul@56 | 279 | # Internal helper methods. |
paul@17 | 280 | |
paul@43 | 281 | def _visitAttr(self, node, classes): |
paul@44 | 282 | |
paul@44 | 283 | """ |
paul@44 | 284 | Visit the attribute-related 'node', generating instructions based on the |
paul@44 | 285 | given 'classes'. |
paul@44 | 286 | """ |
paul@44 | 287 | |
paul@43 | 288 | self.dispatch(node.expr) |
paul@63 | 289 | self._generateAttr(node, node.attrname, classes) |
paul@43 | 290 | |
paul@63 | 291 | def _generateAttr(self, node, attrname, classes): |
paul@44 | 292 | |
paul@44 | 293 | """ |
paul@44 | 294 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@44 | 295 | """ |
paul@44 | 296 | |
paul@72 | 297 | AddressInstruction, AttrInstruction, AttrIndexInstruction = classes |
paul@43 | 298 | |
paul@43 | 299 | last = self.last_op() |
paul@54 | 300 | |
paul@56 | 301 | # Where the last operation (defining the attribute owner) yields a |
paul@56 | 302 | # constant... |
paul@54 | 303 | |
paul@62 | 304 | if self._have_constant_input(0): |
paul@62 | 305 | |
paul@62 | 306 | # Optimise away the constant storage if appropriate. |
paul@87 | 307 | # The target and value loading operations are removed. |
paul@43 | 308 | |
paul@72 | 309 | if self._optimise_constant_storage(AddressInstruction, 1): |
paul@56 | 310 | return |
paul@56 | 311 | |
paul@43 | 312 | # Get the details of the access. |
paul@43 | 313 | |
paul@83 | 314 | if isinstance(last.attr, Const): |
paul@82 | 315 | target_name = last.attr.value_type_name() |
paul@82 | 316 | else: |
paul@82 | 317 | target = last.attr.value |
paul@75 | 318 | |
paul@83 | 319 | if isinstance(target, Const): |
paul@82 | 320 | target_name = target.value_type_name() |
paul@86 | 321 | elif isinstance(target, Instance): |
paul@86 | 322 | target_name = None # skip production of optimised code |
paul@82 | 323 | else: |
paul@82 | 324 | target_name = target.full_name() |
paul@75 | 325 | |
paul@86 | 326 | # Only try and discover the position if the target can be resolved. |
paul@63 | 327 | |
paul@86 | 328 | if target_name is not None: |
paul@86 | 329 | |
paul@86 | 330 | # Access the object table to get the attribute position. |
paul@63 | 331 | |
paul@86 | 332 | try: |
paul@86 | 333 | table_entry = self.objtable.table[target_name] |
paul@86 | 334 | except KeyError: |
paul@86 | 335 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 336 | "No object entry exists for target %r." % target_name) |
paul@63 | 337 | |
paul@86 | 338 | try: |
paul@86 | 339 | pos = table_entry[attrname] |
paul@86 | 340 | except KeyError: |
paul@86 | 341 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 342 | "No attribute entry exists for name %r in target %r." % (attrname, target_name)) |
paul@74 | 343 | |
paul@86 | 344 | # Produce a suitable instruction. |
paul@86 | 345 | |
paul@86 | 346 | self.replace_op(AddressInstruction(pos)) |
paul@86 | 347 | return |
paul@54 | 348 | |
paul@54 | 349 | # Where the last operation involves the special 'self' name, check to |
paul@65 | 350 | # see if the attribute is acceptably positioned and produce a direct |
paul@65 | 351 | # access to the attribute. |
paul@54 | 352 | |
paul@86 | 353 | elif self._optimise_self_access(attrname, (AddressInstruction, AttrInstruction)): |
paul@86 | 354 | return |
paul@54 | 355 | |
paul@54 | 356 | # Otherwise, perform a normal operation. |
paul@43 | 357 | |
paul@86 | 358 | try: |
paul@86 | 359 | index = self.objtable.get_index(attrname) |
paul@86 | 360 | except self.objtable.TableError: |
paul@86 | 361 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 362 | "No attribute entry exists for name %r." % attrname) |
paul@63 | 363 | |
paul@86 | 364 | self.new_op(AttrIndexInstruction(index)) |
paul@43 | 365 | |
paul@45 | 366 | def _startCallFunc(self): |
paul@45 | 367 | |
paul@45 | 368 | "Record the location of the invocation." |
paul@21 | 369 | |
paul@21 | 370 | self.new_op(MakeFrame()) # records the start of the frame |
paul@21 | 371 | |
paul@44 | 372 | def _generateCallFunc(self, args, node): |
paul@44 | 373 | |
paul@86 | 374 | """ |
paul@86 | 375 | Support a generic function invocation using the given 'args', occurring |
paul@86 | 376 | on the given 'node', where the expression providing the invocation |
paul@86 | 377 | target has just been generated. |
paul@86 | 378 | |
paul@86 | 379 | In other situations, the invocation is much simpler and does not need to |
paul@86 | 380 | handle the full flexibility of a typical Python invocation. Internal |
paul@86 | 381 | invocations, such as those employed by operators and certain |
paul@86 | 382 | control-flow mechanisms, use predetermined arguments and arguably do not |
paul@86 | 383 | need to support the same things as the more general invocations. |
paul@86 | 384 | """ |
paul@86 | 385 | |
paul@86 | 386 | target, context, temp = self._generateCallFuncContext() |
paul@86 | 387 | self._generateCallFuncArgs(target, context, args, node) |
paul@86 | 388 | return temp |
paul@86 | 389 | |
paul@86 | 390 | def _generateCallFuncContext(self): |
paul@86 | 391 | |
paul@86 | 392 | """ |
paul@86 | 393 | Produce code which loads and checks the context of the current |
paul@86 | 394 | invocation, the instructions for whose target have already been |
paul@86 | 395 | produced, returning a list of instructions which reference the |
paul@86 | 396 | invocation target. |
paul@86 | 397 | """ |
paul@26 | 398 | |
paul@77 | 399 | t = self._optimise_known_target() |
paul@77 | 400 | if t: |
paul@77 | 401 | target, context = t |
paul@77 | 402 | else: |
paul@77 | 403 | target, context = None, None |
paul@45 | 404 | |
paul@78 | 405 | # Store the target in temporary storage for subsequent referencing. |
paul@78 | 406 | |
paul@78 | 407 | temp = self._optimise_temp_storage() |
paul@78 | 408 | |
paul@87 | 409 | # Where a target or context are not known, load the target and context. |
paul@45 | 410 | |
paul@59 | 411 | if context is None: |
paul@87 | 412 | self.new_ops(temp) |
paul@45 | 413 | self.new_op(LoadContext()) |
paul@86 | 414 | |
paul@92 | 415 | # Check to see if the context is needed for the target. |
paul@86 | 416 | |
paul@92 | 417 | self.new_op(CheckContext()) |
paul@87 | 418 | |
paul@87 | 419 | # Where an instance is known to be the context, load the context. |
paul@87 | 420 | |
paul@87 | 421 | elif isinstance(context, Instance): |
paul@87 | 422 | self.new_ops(temp) |
paul@87 | 423 | self.new_op(LoadContext()) |
paul@87 | 424 | |
paul@87 | 425 | # Otherwise omit the context. |
paul@87 | 426 | |
paul@59 | 427 | else: |
paul@59 | 428 | pass # NOTE: Class methods should be supported. |
paul@26 | 429 | |
paul@86 | 430 | return target, context, temp |
paul@86 | 431 | |
paul@86 | 432 | def _generateCallFuncArgs(self, target, context, args, node): |
paul@86 | 433 | |
paul@86 | 434 | """ |
paul@86 | 435 | Given invocation 'target' and 'context' information, a list of nodes |
paul@86 | 436 | representing the 'args' (arguments), generate instructions which load |
paul@86 | 437 | the arguments for the invocation defined by the given 'node'. |
paul@86 | 438 | """ |
paul@86 | 439 | |
paul@21 | 440 | # Evaluate the arguments. |
paul@21 | 441 | |
paul@67 | 442 | employed_positions = set() |
paul@59 | 443 | extra_keywords = [] |
paul@22 | 444 | |
paul@86 | 445 | # NOTE: Fix context for self-accessed methods. |
paul@86 | 446 | |
paul@90 | 447 | if context is not None and isinstance(context, Instance): |
paul@86 | 448 | ncontext = 1 |
paul@86 | 449 | else: |
paul@86 | 450 | ncontext = 0 |
paul@86 | 451 | |
paul@92 | 452 | first = 1 |
paul@92 | 453 | frame_pos = ncontext |
paul@92 | 454 | |
paul@92 | 455 | for arg in args: |
paul@66 | 456 | |
paul@66 | 457 | # Handle positional and keyword arguments separately. |
paul@66 | 458 | |
paul@21 | 459 | if isinstance(arg, compiler.ast.Keyword): |
paul@66 | 460 | |
paul@26 | 461 | # Optimise where the target is known now. |
paul@26 | 462 | |
paul@26 | 463 | if target is not None: |
paul@39 | 464 | |
paul@39 | 465 | # Find the parameter table entry for the target. |
paul@39 | 466 | |
paul@39 | 467 | target_name = target.full_name() |
paul@39 | 468 | |
paul@39 | 469 | # Look for a callable with the precise target name. |
paul@39 | 470 | |
paul@59 | 471 | table_entry = self.paramtable.table[target_name] |
paul@59 | 472 | |
paul@59 | 473 | # Look the name up in the parameter table entry. |
paul@39 | 474 | |
paul@59 | 475 | try: |
paul@59 | 476 | pos = table_entry[arg.name] |
paul@59 | 477 | |
paul@59 | 478 | # Where no position is found, this could be an extra keyword |
paul@59 | 479 | # argument. |
paul@39 | 480 | |
paul@39 | 481 | except KeyError: |
paul@59 | 482 | extra_keywords.append(arg) |
paul@59 | 483 | continue |
paul@58 | 484 | |
paul@58 | 485 | # Test for illegal conditions. |
paul@58 | 486 | |
paul@67 | 487 | if pos in employed_positions: |
paul@58 | 488 | raise TranslateError(self.module.full_name(), node, |
paul@58 | 489 | "Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@67 | 490 | |
paul@67 | 491 | employed_positions.add(pos) |
paul@58 | 492 | |
paul@67 | 493 | # Add space for arguments appearing before this one. |
paul@67 | 494 | |
paul@92 | 495 | if frame_pos < pos: |
paul@92 | 496 | self.new_op(ReserveFrame(pos - frame_pos)) |
paul@92 | 497 | frame_pos = pos |
paul@59 | 498 | |
paul@59 | 499 | # Generate code for the keyword and the positioning |
paul@59 | 500 | # operation. |
paul@59 | 501 | |
paul@59 | 502 | self.dispatch(arg.expr) |
paul@66 | 503 | |
paul@66 | 504 | # If the position corresponds to the current frame element, |
paul@67 | 505 | # skip generating the store instruction. |
paul@66 | 506 | |
paul@92 | 507 | if frame_pos > pos: |
paul@66 | 508 | self.new_op(StoreFrame(pos)) |
paul@26 | 509 | |
paul@26 | 510 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 511 | # the parameter location. |
paul@26 | 512 | |
paul@26 | 513 | else: |
paul@21 | 514 | |
paul@26 | 515 | # Combine the target details with the name to get the location. |
paul@26 | 516 | # See the access method on the List class. |
paul@26 | 517 | |
paul@39 | 518 | try: |
paul@39 | 519 | paramindex = self.paramtable.get_index(arg.name) |
paul@59 | 520 | |
paul@59 | 521 | # Where no position is found, this could be an extra keyword |
paul@59 | 522 | # argument. |
paul@59 | 523 | |
paul@70 | 524 | except self.paramtable.TableError: |
paul@59 | 525 | extra_keywords.append(arg) |
paul@59 | 526 | continue |
paul@21 | 527 | |
paul@59 | 528 | # Generate code for the keyword and the positioning |
paul@59 | 529 | # operation. |
paul@59 | 530 | |
paul@59 | 531 | self.dispatch(arg.expr) |
paul@26 | 532 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 533 | |
paul@26 | 534 | # use (callable+0)+paramindex+table |
paul@26 | 535 | # checks embedded offset against (callable+0) |
paul@26 | 536 | # moves the top of stack to frame+position |
paul@21 | 537 | |
paul@21 | 538 | else: |
paul@21 | 539 | self.dispatch(arg) |
paul@86 | 540 | employed_positions.add(frame_pos + ncontext) |
paul@21 | 541 | |
paul@92 | 542 | # Check to see if the first argument is appropriate (compatible with |
paul@92 | 543 | # the # target where methods are being invoked via classes). |
paul@92 | 544 | |
paul@92 | 545 | if first and context is None: |
paul@92 | 546 | continue_label = self.new_label() |
paul@92 | 547 | self.new_op(CheckSelf()) |
paul@92 | 548 | self.new_op(JumpIfTrue(continue_label)) |
paul@92 | 549 | |
paul@92 | 550 | # Where the context is inappropriate, drop the incomplete frame and |
paul@92 | 551 | # raise an exception. |
paul@92 | 552 | |
paul@92 | 553 | self.new_op(DropFrame()) |
paul@92 | 554 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@92 | 555 | self.new_op(RaiseException()) |
paul@92 | 556 | self.set_label(continue_label) |
paul@92 | 557 | |
paul@95 | 558 | first = 0 |
paul@92 | 559 | frame_pos += 1 |
paul@66 | 560 | |
paul@75 | 561 | # NOTE: Extra keywords are not supported. |
paul@75 | 562 | # NOTE: Somehow, the above needs to be combined with * arguments. |
paul@59 | 563 | |
paul@59 | 564 | # Either test for a complete set of arguments. |
paul@59 | 565 | |
paul@59 | 566 | if target is not None: |
paul@66 | 567 | |
paul@66 | 568 | # Make sure that enough arguments have been given. |
paul@66 | 569 | |
paul@67 | 570 | nargs_max = len(target.positional_names) |
paul@66 | 571 | ndefaults = len(target.defaults) |
paul@66 | 572 | nargs_min = nargs_max - ndefaults |
paul@67 | 573 | |
paul@86 | 574 | for i in range(ncontext, nargs_min): |
paul@67 | 575 | if i not in employed_positions: |
paul@67 | 576 | raise TranslateError(self.module.full_name(), node, |
paul@67 | 577 | "Argument %r not supplied for %r: need at least %d arguments." % (i+1, target.name, nargs_min)) |
paul@67 | 578 | |
paul@66 | 579 | nargs = len(args) |
paul@66 | 580 | |
paul@67 | 581 | if nargs > nargs_max and not target.has_star and not target.has_dstar: |
paul@59 | 582 | raise TranslateError(self.module.full_name(), node, |
paul@66 | 583 | "Too many arguments for %r: need at most %d arguments." % (target.name, nargs_max)) |
paul@66 | 584 | |
paul@66 | 585 | # Where defaults are involved, put them into the frame. |
paul@66 | 586 | # Here, we use negative index values to visit the right hand end of |
paul@66 | 587 | # the defaults list. |
paul@66 | 588 | |
paul@67 | 589 | for pos in range(nargs_min, nargs_max): |
paul@67 | 590 | if pos not in employed_positions: |
paul@91 | 591 | #self.new_op(LoadConst(target)) |
paul@91 | 592 | #self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@91 | 593 | self.new_op(LoadAddress(target.default_attrs[pos - nargs_min])) |
paul@66 | 594 | |
paul@91 | 595 | # If the position corresponds to the current frame element, |
paul@91 | 596 | # skip generating the instruction. |
paul@66 | 597 | |
paul@91 | 598 | if frame_pos != pos: |
paul@91 | 599 | self.new_op(StoreFrame(pos)) |
paul@66 | 600 | |
paul@66 | 601 | frame_pos += 1 |
paul@59 | 602 | |
paul@59 | 603 | # Or generate instructions to do this at run-time. |
paul@66 | 604 | # NOTE: CheckFrame has to check the number of arguments and to fill in |
paul@66 | 605 | # NOTE: defaults. |
paul@59 | 606 | |
paul@59 | 607 | else: |
paul@59 | 608 | self.new_op(CheckFrame()) |
paul@59 | 609 | |
paul@78 | 610 | def _endCallFunc(self, temp): |
paul@57 | 611 | |
paul@57 | 612 | "Make the invocation and tidy up afterwards." |
paul@57 | 613 | |
paul@79 | 614 | self.new_ops(temp) |
paul@68 | 615 | self.new_op(JumpWithFrame()) |
paul@21 | 616 | |
paul@21 | 617 | # NOTE: Exception handling required. |
paul@21 | 618 | |
paul@21 | 619 | self.new_op(DropFrame()) |
paul@79 | 620 | self.discard_temp(temp) |
paul@17 | 621 | |
paul@56 | 622 | def _visitName(self, node, classes): |
paul@56 | 623 | |
paul@56 | 624 | """ |
paul@56 | 625 | Visit the name-related 'node', generating instructions based on the |
paul@56 | 626 | given 'classes'. |
paul@56 | 627 | """ |
paul@56 | 628 | |
paul@56 | 629 | name = node.name |
paul@56 | 630 | scope = self.get_scope(name) |
paul@56 | 631 | #print self.module.name, node.lineno, name, scope |
paul@56 | 632 | self._generateName(name, scope, classes, node) |
paul@56 | 633 | |
paul@56 | 634 | def _generateName(self, name, scope, classes, node): |
paul@56 | 635 | |
paul@56 | 636 | """ |
paul@56 | 637 | Generate code for the access to 'name' in 'scope' using the given |
paul@56 | 638 | 'classes', and using the given 'node' as the source of the access. |
paul@56 | 639 | """ |
paul@56 | 640 | |
paul@72 | 641 | NameInstruction, AddressInstruction = classes |
paul@56 | 642 | |
paul@87 | 643 | # Optimise away the constant storage if appropriate. |
paul@87 | 644 | # The target and value loading operations are removed. |
paul@87 | 645 | |
paul@56 | 646 | if self._optimise_constant_storage(NameInstruction, 0): |
paul@56 | 647 | return |
paul@56 | 648 | |
paul@56 | 649 | if scope == "local": |
paul@56 | 650 | unit = self.unit |
paul@83 | 651 | if isinstance(unit, Function): |
paul@56 | 652 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@83 | 653 | elif isinstance(unit, Class): |
paul@72 | 654 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@83 | 655 | elif isinstance(unit, Module): |
paul@72 | 656 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@56 | 657 | else: |
paul@61 | 658 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@56 | 659 | |
paul@56 | 660 | elif scope == "global": |
paul@56 | 661 | globals = self.module.module_attributes() |
paul@56 | 662 | if globals.has_key(name): |
paul@72 | 663 | self.new_op(AddressInstruction(globals[name])) |
paul@56 | 664 | else: |
paul@61 | 665 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@56 | 666 | |
paul@56 | 667 | else: |
paul@72 | 668 | self.new_op(AddressInstruction(self._get_builtin(name, node))) |
paul@56 | 669 | |
paul@61 | 670 | def _get_builtin(self, name, node): |
paul@61 | 671 | if self.builtins is not None: |
paul@63 | 672 | try: |
paul@63 | 673 | return self.builtins[name] |
paul@63 | 674 | except KeyError: |
paul@63 | 675 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@61 | 676 | else: |
paul@61 | 677 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@56 | 678 | |
paul@62 | 679 | # Optimisation tests. |
paul@62 | 680 | |
paul@62 | 681 | def _should_optimise_constant_storage(self): |
paul@62 | 682 | return "constant_storage" in self.optimisations |
paul@62 | 683 | |
paul@76 | 684 | def _should_optimise_constant_test(self): |
paul@76 | 685 | return "constant_test" in self.optimisations |
paul@76 | 686 | |
paul@62 | 687 | def _should_optimise_known_target(self): |
paul@62 | 688 | return "known_target" in self.optimisations |
paul@62 | 689 | |
paul@65 | 690 | def _should_optimise_self_access(self): |
paul@65 | 691 | return "self_access" in self.optimisations |
paul@65 | 692 | |
paul@74 | 693 | def _should_optimise_temp_storage(self): |
paul@74 | 694 | return "temp_storage" in self.optimisations |
paul@74 | 695 | |
paul@93 | 696 | def _should_optimise_stack_access(self): |
paul@93 | 697 | return "stack_access" in self.optimisations |
paul@93 | 698 | |
paul@81 | 699 | def _have_constant_input(self, n): |
paul@81 | 700 | last = self.last_ops(n+1) |
paul@73 | 701 | return len(last) > n and (isinstance(last[n], LoadAddress) and last[n].attr.assignments == 1 or |
paul@87 | 702 | isinstance(last[n], LoadConst)) |
paul@62 | 703 | |
paul@81 | 704 | def _have_known_target(self): |
paul@81 | 705 | return self._have_constant_input(0) |
paul@62 | 706 | |
paul@65 | 707 | def _have_self_input(self): |
paul@65 | 708 | last = self.last_op() |
paul@83 | 709 | return isinstance(self.unit, Function) and \ |
paul@65 | 710 | self.unit.is_method() and isinstance(last, LoadName) and \ |
paul@65 | 711 | last.attr.name == "self" |
paul@65 | 712 | |
paul@74 | 713 | def _have_temp_compatible_access(self): |
paul@74 | 714 | last = self.last_op() |
paul@74 | 715 | # NOTE: Should expand to cover LoadAttr and LoadAttrIndex, but this |
paul@95 | 716 | # NOTE: would require inspection of the stack operations. |
paul@76 | 717 | return isinstance(last, (LoadName, LoadTemp, LoadAddress, LoadConst)) |
paul@74 | 718 | |
paul@93 | 719 | def _have_fixed_sources(self, access): |
paul@93 | 720 | access_ops = [] |
paul@93 | 721 | for i in xrange(0, access): |
paul@93 | 722 | position, op = self.stack[-1 - i] |
paul@93 | 723 | if not isinstance(op, (LoadName, LoadTemp, LoadAddress, LoadConst)): |
paul@93 | 724 | return 0 |
paul@93 | 725 | access_ops.append(op) |
paul@93 | 726 | return access_ops |
paul@93 | 727 | |
paul@62 | 728 | # Optimisation methods. See the supported_optimisations class attribute. |
paul@56 | 729 | |
paul@74 | 730 | def _optimise_temp_storage(self): |
paul@74 | 731 | |
paul@74 | 732 | """ |
paul@74 | 733 | Where the next operation would involve storing a value into temporary |
paul@78 | 734 | storage at 'temp_position', record and remove any simple instruction |
paul@78 | 735 | which produced the value to be stored such that instead of subsequently |
paul@78 | 736 | accessing the temporary storage, that instruction is substituted. |
paul@78 | 737 | |
paul@78 | 738 | If no optimisation can be achieved, a StoreTemp instruction is produced |
paul@78 | 739 | and the appropriate LoadTemp instruction is returned. |
paul@79 | 740 | |
paul@79 | 741 | All returned instructions are provided in a list. |
paul@74 | 742 | """ |
paul@74 | 743 | |
paul@74 | 744 | if self._should_optimise_temp_storage() and \ |
paul@74 | 745 | self._have_temp_compatible_access(): |
paul@74 | 746 | |
paul@74 | 747 | last = self.last_op() |
paul@74 | 748 | self.remove_ops(1) |
paul@79 | 749 | return [last] |
paul@74 | 750 | else: |
paul@78 | 751 | temp_position = self.reserve_temp(1) |
paul@78 | 752 | self.new_op(StoreTemp(temp_position)) |
paul@79 | 753 | return [LoadTemp(temp_position)] |
paul@74 | 754 | |
paul@65 | 755 | def _optimise_constant_storage(self, instruction, n): |
paul@56 | 756 | |
paul@56 | 757 | """ |
paul@56 | 758 | Where this operation should store a constant into a target which is |
paul@56 | 759 | also constant, optimise away both operations. |
paul@56 | 760 | """ |
paul@56 | 761 | |
paul@65 | 762 | if self._should_optimise_constant_storage() and \ |
paul@72 | 763 | instruction in (StoreAddress, StoreName) and \ |
paul@65 | 764 | self._have_constant_input(n) and \ |
paul@65 | 765 | (n == 0 or self._have_constant_input(n-1)): |
paul@65 | 766 | |
paul@56 | 767 | self.remove_ops(n+1) |
paul@56 | 768 | return 1 |
paul@56 | 769 | else: |
paul@56 | 770 | return 0 |
paul@56 | 771 | |
paul@76 | 772 | def _optimise_constant_test(self, instruction): |
paul@76 | 773 | |
paul@76 | 774 | """ |
paul@76 | 775 | Where this operation tests the topmost stack value which happens to be |
paul@76 | 776 | a constant against another stack value, merge the last instruction which |
paul@76 | 777 | loaded the constant into the current 'instruction'. |
paul@76 | 778 | """ |
paul@76 | 779 | |
paul@76 | 780 | if self._should_optimise_constant_test() and \ |
paul@76 | 781 | instruction is TestIdentity and \ |
paul@76 | 782 | self._have_constant_input(0): |
paul@76 | 783 | |
paul@76 | 784 | last = self.last_op() |
paul@76 | 785 | self.replace_op(TestIdentityAddress(last.attr)) |
paul@76 | 786 | return 1 |
paul@76 | 787 | else: |
paul@76 | 788 | return 0 |
paul@76 | 789 | |
paul@81 | 790 | def _optimise_known_target(self): |
paul@59 | 791 | |
paul@59 | 792 | """ |
paul@59 | 793 | Where the target of an invocation is known, provide information about it |
paul@59 | 794 | and its context. If a class is being invoked and the conditions are |
paul@59 | 795 | appropriate, get information about the specific initialiser. |
paul@59 | 796 | """ |
paul@59 | 797 | |
paul@81 | 798 | if self._should_optimise_known_target() and self._have_known_target(): |
paul@81 | 799 | last = self.last_op() |
paul@59 | 800 | target = last.attr.value |
paul@87 | 801 | context = last.attr.context |
paul@59 | 802 | |
paul@59 | 803 | # Handle calls to classes. |
paul@59 | 804 | |
paul@83 | 805 | if isinstance(target, Class): |
paul@61 | 806 | target = target.get_instantiator() |
paul@83 | 807 | context = Instance() |
paul@59 | 808 | |
paul@59 | 809 | # A special context is chosen to avoid generating unnecessary |
paul@59 | 810 | # context loading and checking instructions. |
paul@59 | 811 | |
paul@77 | 812 | return target, context |
paul@59 | 813 | else: |
paul@77 | 814 | return None |
paul@59 | 815 | |
paul@86 | 816 | def _optimise_self_access(self, attrname, classes): |
paul@65 | 817 | |
paul@65 | 818 | """ |
paul@65 | 819 | Where the provided 'attrname' accesses an attribute which occupies the |
paul@65 | 820 | same position in all possible objects which can be accessed, generate an |
paul@86 | 821 | instruction using one of the given 'classes', accessing the attribute |
paul@86 | 822 | directly. |
paul@65 | 823 | """ |
paul@65 | 824 | |
paul@86 | 825 | AddressInstruction, AttrInstruction = classes |
paul@86 | 826 | |
paul@65 | 827 | if self._should_optimise_self_access() and self._have_self_input() and \ |
paul@65 | 828 | not self.unit.is_relocated(attrname): |
paul@65 | 829 | |
paul@87 | 830 | # Either generate an instruction operating on an instance attribute. |
paul@87 | 831 | |
paul@87 | 832 | try: |
paul@87 | 833 | attr = self.unit.parent.instance_attributes()[attrname] |
paul@86 | 834 | self.new_op(AttrInstruction(attr)) |
paul@87 | 835 | |
paul@87 | 836 | # Or generate an instruction operating on a class attribute. |
paul@87 | 837 | |
paul@87 | 838 | except KeyError: |
paul@87 | 839 | attr = self.unit.parent.all_attributes()[attrname] |
paul@87 | 840 | new_attr = attr.via_instance() |
paul@87 | 841 | self.replace_op(AddressInstruction(new_attr)) |
paul@87 | 842 | |
paul@65 | 843 | return 1 |
paul@65 | 844 | else: |
paul@65 | 845 | return 0 |
paul@65 | 846 | |
paul@93 | 847 | def _optimise_stack_access(self, instruction): |
paul@95 | 848 | |
paul@95 | 849 | """ |
paul@95 | 850 | Optimise stack access for the given 'instruction', replacing stack |
paul@95 | 851 | operations with instructions directly accessing the required data. |
paul@95 | 852 | """ |
paul@95 | 853 | |
paul@93 | 854 | if self._should_optimise_stack_access(): |
paul@93 | 855 | ops = self._have_fixed_sources(instruction.stack_access) |
paul@93 | 856 | if ops: |
paul@95 | 857 | #print "Optimised", instruction.accesses, "->", ops |
paul@93 | 858 | for i in range(0, instruction.stack_access): |
paul@93 | 859 | self.remove_op_using_stack() |
paul@95 | 860 | instruction.accesses = ops |
paul@95 | 861 | |
paul@95 | 862 | # Remove the stack side-effects of these accesses. |
paul@95 | 863 | |
paul@95 | 864 | for op in ops: |
paul@95 | 865 | op.remove_results() |
paul@93 | 866 | |
paul@56 | 867 | # Visitor methods. |
paul@56 | 868 | |
paul@56 | 869 | def default(self, node, *args): |
paul@56 | 870 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@56 | 871 | |
paul@56 | 872 | def dispatch(self, node, *args): |
paul@56 | 873 | return ASTVisitor.dispatch(self, node, *args) |
paul@56 | 874 | |
paul@85 | 875 | def _visitUnary(self, node, method): |
paul@85 | 876 | |
paul@85 | 877 | """ |
paul@85 | 878 | _t = node.expr |
paul@85 | 879 | try: |
paul@85 | 880 | _result = _t.__pos__() |
paul@85 | 881 | except AttributeError: |
paul@85 | 882 | raise TypeError |
paul@85 | 883 | """ |
paul@85 | 884 | |
paul@85 | 885 | end_call_label = self.new_label() |
paul@85 | 886 | end_label = self.new_label() |
paul@85 | 887 | |
paul@85 | 888 | # Evaluate and store the operand in temporary storage. |
paul@85 | 889 | |
paul@85 | 890 | self.dispatch(node.expr) |
paul@85 | 891 | temp = self._optimise_temp_storage() |
paul@85 | 892 | |
paul@85 | 893 | # Produce the invocation. |
paul@85 | 894 | |
paul@85 | 895 | self._startCallFunc() |
paul@85 | 896 | self.new_ops(temp) |
paul@85 | 897 | |
paul@85 | 898 | # Get the method on temp. |
paul@85 | 899 | |
paul@85 | 900 | self._generateAttr(node, method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@85 | 901 | |
paul@85 | 902 | # Add exception handling to the method acquisition instructions where |
paul@85 | 903 | # the attribute access cannot be resolved at compile-time. |
paul@85 | 904 | |
paul@85 | 905 | if not self._optimise_known_target(): |
paul@85 | 906 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@85 | 907 | self.new_op(CheckException()) |
paul@85 | 908 | self.new_op(JumpIfTrue(end_call_label)) |
paul@85 | 909 | |
paul@85 | 910 | temp_method = self._optimise_temp_storage() |
paul@85 | 911 | |
paul@85 | 912 | # Add arguments. |
paul@86 | 913 | # NOTE: No support for defaults. |
paul@85 | 914 | |
paul@85 | 915 | self.new_ops(temp) # Explicit context as first argument. |
paul@85 | 916 | self._endCallFunc(temp_method) |
paul@85 | 917 | self.new_op(Jump(end_label)) |
paul@85 | 918 | |
paul@85 | 919 | # End method attempt. |
paul@85 | 920 | |
paul@85 | 921 | self.set_label(end_call_label) |
paul@85 | 922 | self.new_op(DropFrame()) # From the method call. |
paul@85 | 923 | |
paul@85 | 924 | # Raise a TypeError. |
paul@85 | 925 | |
paul@85 | 926 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@85 | 927 | self.new_op(RaiseException()) |
paul@85 | 928 | |
paul@85 | 929 | self.set_label(end_label) |
paul@85 | 930 | |
paul@85 | 931 | # Compilation duties... |
paul@85 | 932 | |
paul@85 | 933 | self.discard_temp(temp) |
paul@85 | 934 | |
paul@57 | 935 | def _visitBinary(self, node, left_method, right_method): |
paul@56 | 936 | |
paul@56 | 937 | """ |
paul@56 | 938 | _t1 = node.left |
paul@56 | 939 | _t2 = node.right |
paul@56 | 940 | try: |
paul@76 | 941 | _result = _t1.__add__(_t2) |
paul@76 | 942 | if _result is NotImplemented: |
paul@76 | 943 | raise AttributeError |
paul@56 | 944 | except AttributeError: |
paul@76 | 945 | try: |
paul@76 | 946 | _result = _t2.__radd__(_t1) |
paul@76 | 947 | if _result is NotImplemented: |
paul@76 | 948 | raise AttributeError |
paul@76 | 949 | except AttributeError: |
paul@76 | 950 | raise TypeError |
paul@56 | 951 | """ |
paul@56 | 952 | |
paul@76 | 953 | end_left_label = self.new_label() |
paul@57 | 954 | right_label = self.new_label() |
paul@85 | 955 | end_right_label = self.new_label() |
paul@76 | 956 | type_error_label = self.new_label() |
paul@57 | 957 | end_label = self.new_label() |
paul@57 | 958 | |
paul@78 | 959 | # Evaluate and store the left operand in temporary storage. |
paul@73 | 960 | |
paul@57 | 961 | self.dispatch(node.left) |
paul@78 | 962 | temp1 = self._optimise_temp_storage() |
paul@74 | 963 | |
paul@78 | 964 | # Evaluate and store the right operand in temporary storage. |
paul@74 | 965 | |
paul@57 | 966 | self.dispatch(node.right) |
paul@74 | 967 | temp2 = self._optimise_temp_storage() |
paul@57 | 968 | |
paul@57 | 969 | # Left method. |
paul@57 | 970 | |
paul@57 | 971 | self._startCallFunc() |
paul@79 | 972 | self.new_ops(temp1) |
paul@76 | 973 | |
paul@76 | 974 | # Get left method on temp1. |
paul@76 | 975 | |
paul@72 | 976 | self._generateAttr(node, left_method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@77 | 977 | |
paul@79 | 978 | # Add exception handling to the method acquisition instructions where |
paul@79 | 979 | # the attribute access cannot be resolved at compile-time. |
paul@79 | 980 | |
paul@80 | 981 | if not self._optimise_known_target(): |
paul@77 | 982 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@80 | 983 | self.new_op(CheckException()) |
paul@80 | 984 | self.new_op(JumpIfTrue(end_left_label)) |
paul@80 | 985 | |
paul@80 | 986 | temp_method = self._optimise_temp_storage() |
paul@76 | 987 | |
paul@76 | 988 | # Add arguments. |
paul@86 | 989 | # NOTE: No support for defaults. |
paul@76 | 990 | |
paul@79 | 991 | self.new_ops(temp1) # Explicit context as first argument. |
paul@79 | 992 | self.new_ops(temp2) |
paul@78 | 993 | self._endCallFunc(temp_method) |
paul@57 | 994 | |
paul@76 | 995 | # Test for NotImplemented. |
paul@76 | 996 | # Don't actually raise an exception. |
paul@76 | 997 | |
paul@76 | 998 | self.dispatch(compiler.ast.Name("NotImplemented")) |
paul@76 | 999 | if not self._optimise_constant_test(TestIdentity): |
paul@76 | 1000 | self.new_op(TestIdentity()) |
paul@76 | 1001 | self.new_op(JumpIfTrue(right_label)) |
paul@76 | 1002 | self.new_op(Jump(end_label)) |
paul@76 | 1003 | |
paul@76 | 1004 | # End left method attempt. |
paul@76 | 1005 | |
paul@76 | 1006 | self.set_label(end_left_label) |
paul@76 | 1007 | self.new_op(DropFrame()) # From the left method call. |
paul@57 | 1008 | |
paul@57 | 1009 | # Right method. |
paul@57 | 1010 | |
paul@57 | 1011 | self.set_label(right_label) |
paul@57 | 1012 | self._startCallFunc() |
paul@79 | 1013 | self.new_ops(temp2) |
paul@76 | 1014 | |
paul@76 | 1015 | # Get right method on temp2. |
paul@76 | 1016 | |
paul@72 | 1017 | self._generateAttr(node, right_method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@77 | 1018 | |
paul@79 | 1019 | # Add exception handling to the method acquisition instructions where |
paul@79 | 1020 | # the attribute access cannot be resolved at compile-time. |
paul@79 | 1021 | |
paul@80 | 1022 | if not self._optimise_known_target(): |
paul@77 | 1023 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@80 | 1024 | self.new_op(CheckException()) |
paul@85 | 1025 | self.new_op(JumpIfTrue(end_right_label)) |
paul@80 | 1026 | |
paul@80 | 1027 | temp_method = self._optimise_temp_storage() |
paul@76 | 1028 | |
paul@76 | 1029 | # Add arguments. |
paul@86 | 1030 | # NOTE: No support for defaults. |
paul@76 | 1031 | |
paul@79 | 1032 | self.new_ops(temp2) # Explicit context as first argument. |
paul@79 | 1033 | self.new_ops(temp1) |
paul@78 | 1034 | self._endCallFunc(temp_method) |
paul@57 | 1035 | |
paul@76 | 1036 | # Test for NotImplemented. |
paul@76 | 1037 | # Don't actually raise an exception. |
paul@76 | 1038 | |
paul@76 | 1039 | self.dispatch(compiler.ast.Name("NotImplemented")) |
paul@76 | 1040 | if not self._optimise_constant_test(TestIdentity): |
paul@76 | 1041 | self.new_op(TestIdentity()) |
paul@76 | 1042 | self.new_op(JumpIfTrue(type_error_label)) |
paul@76 | 1043 | self.new_op(Jump(end_label)) |
paul@76 | 1044 | |
paul@85 | 1045 | # End right method attempt. |
paul@85 | 1046 | |
paul@85 | 1047 | self.set_label(end_right_label) |
paul@85 | 1048 | self.new_op(DropFrame()) # From the right method call. |
paul@85 | 1049 | |
paul@76 | 1050 | # Raise a TypeError. |
paul@76 | 1051 | |
paul@76 | 1052 | self.set_label(type_error_label) |
paul@76 | 1053 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@76 | 1054 | self.new_op(RaiseException()) |
paul@76 | 1055 | |
paul@57 | 1056 | self.set_label(end_label) |
paul@57 | 1057 | |
paul@76 | 1058 | # Compilation duties... |
paul@76 | 1059 | |
paul@79 | 1060 | self.discard_temp(temp1) |
paul@79 | 1061 | self.discard_temp(temp2) |
paul@73 | 1062 | |
paul@57 | 1063 | def visitAdd(self, node): |
paul@57 | 1064 | self._visitBinary(node, "__add__", "__radd__") |
paul@57 | 1065 | |
paul@56 | 1066 | def visitAnd(self, node): pass |
paul@56 | 1067 | |
paul@56 | 1068 | def visitAssert(self, node): pass |
paul@56 | 1069 | |
paul@56 | 1070 | def visitAssign(self, node): |
paul@56 | 1071 | self.dispatch(node.expr) |
paul@56 | 1072 | for n in node.nodes: |
paul@56 | 1073 | self.dispatch(n) |
paul@56 | 1074 | |
paul@56 | 1075 | def visitAssAttr(self, node): |
paul@72 | 1076 | self._visitAttr(node, (StoreAddress, StoreAttr, StoreAttrIndex)) |
paul@56 | 1077 | |
paul@56 | 1078 | def visitAssList(self, node): pass |
paul@56 | 1079 | |
paul@56 | 1080 | def visitAssName(self, node): |
paul@72 | 1081 | self._visitName(node, (StoreName, StoreAddress)) |
paul@56 | 1082 | |
paul@56 | 1083 | visitAssTuple = visitAssList |
paul@56 | 1084 | |
paul@56 | 1085 | def visitAugAssign(self, node): pass |
paul@56 | 1086 | |
paul@56 | 1087 | def visitBackquote(self, node): pass |
paul@56 | 1088 | |
paul@85 | 1089 | def visitBitand(self, node): |
paul@85 | 1090 | self._visitBinary(node, "__and__", "__rand__") |
paul@56 | 1091 | |
paul@85 | 1092 | def visitBitor(self, node): |
paul@85 | 1093 | self._visitBinary(node, "__or__", "__ror__") |
paul@56 | 1094 | |
paul@85 | 1095 | def visitBitxor(self, node): |
paul@85 | 1096 | self._visitBinary(node, "__xor__", "__rxor__") |
paul@56 | 1097 | |
paul@56 | 1098 | def visitBreak(self, node): |
paul@57 | 1099 | next_label, exit_label = self.get_loop_labels() |
paul@56 | 1100 | self.new_op(Jump(exit_label)) |
paul@56 | 1101 | |
paul@56 | 1102 | def visitCallFunc(self, node): |
paul@56 | 1103 | |
paul@56 | 1104 | """ |
paul@56 | 1105 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@56 | 1106 | the correct location, then invoke the function. |
paul@56 | 1107 | """ |
paul@56 | 1108 | |
paul@56 | 1109 | # Mark the frame, evaluate the target, generate the call. |
paul@56 | 1110 | |
paul@56 | 1111 | self._startCallFunc() |
paul@56 | 1112 | self.dispatch(node.node) |
paul@78 | 1113 | temp = self._generateCallFunc(node.args, node) |
paul@78 | 1114 | self._endCallFunc(temp) |
paul@56 | 1115 | |
paul@38 | 1116 | def visitClass(self, node): |
paul@90 | 1117 | |
paul@90 | 1118 | # Store the name. |
paul@90 | 1119 | |
paul@90 | 1120 | self.new_op(LoadConst(node.unit)) |
paul@90 | 1121 | self._visitName(node, (StoreName, StoreAddress)) |
paul@90 | 1122 | |
paul@90 | 1123 | # Visit the code. |
paul@90 | 1124 | |
paul@38 | 1125 | unit = self.unit |
paul@39 | 1126 | self.unit = node.unit |
paul@44 | 1127 | self.unit.code_location = self.module.code_location # class body code is not independently addressable |
paul@38 | 1128 | self.dispatch(node.code) |
paul@38 | 1129 | self.unit = unit |
paul@17 | 1130 | |
paul@21 | 1131 | def visitCompare(self, node): |
paul@21 | 1132 | |
paul@21 | 1133 | """ |
paul@21 | 1134 | self.dispatch(node.expr) |
paul@21 | 1135 | for op_name, next_node in compare.ops: |
paul@21 | 1136 | methods = self.comparison_methods[op_name] |
paul@21 | 1137 | if methods is not None: |
paul@21 | 1138 | # Generate method call using evaluated argument and next node. |
paul@21 | 1139 | else: |
paul@21 | 1140 | # Deal with the special operators. |
paul@21 | 1141 | # Provide short-circuiting. |
paul@21 | 1142 | """ |
paul@17 | 1143 | |
paul@22 | 1144 | def visitConst(self, node): |
paul@22 | 1145 | const = self.module.constant_values[node.value] |
paul@22 | 1146 | self.new_op(LoadConst(const)) |
paul@17 | 1147 | |
paul@41 | 1148 | def visitContinue(self, node): |
paul@57 | 1149 | next_label, exit_label = self.get_loop_labels() |
paul@41 | 1150 | self.new_op(Jump(next_label)) |
paul@17 | 1151 | |
paul@19 | 1152 | def visitDecorators(self, node): pass |
paul@17 | 1153 | |
paul@19 | 1154 | def visitDict(self, node): pass |
paul@17 | 1155 | |
paul@18 | 1156 | def visitDiscard(self, node): |
paul@19 | 1157 | self.dispatch(node.expr) |
paul@17 | 1158 | |
paul@57 | 1159 | def visitDiv(self, node): |
paul@57 | 1160 | self._visitBinary(node, "__div__", "__rdiv__") |
paul@17 | 1161 | |
paul@19 | 1162 | def visitEllipsis(self, node): pass |
paul@17 | 1163 | |
paul@19 | 1164 | def visitExec(self, node): pass |
paul@17 | 1165 | |
paul@19 | 1166 | def visitExpression(self, node): pass |
paul@17 | 1167 | |
paul@57 | 1168 | def visitFloorDiv(self, node): |
paul@57 | 1169 | self._visitBinary(node, "__floordiv__", "__rfloordiv__") |
paul@17 | 1170 | |
paul@44 | 1171 | def visitFor(self, node): |
paul@44 | 1172 | exit_label = self.new_label() |
paul@44 | 1173 | next_label = self.new_label() |
paul@44 | 1174 | else_label = self.new_label() |
paul@44 | 1175 | |
paul@44 | 1176 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 1177 | |
paul@45 | 1178 | self._startCallFunc() |
paul@44 | 1179 | self.dispatch(node.list) |
paul@72 | 1180 | self._generateAttr(node, "__iter__", (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@78 | 1181 | temp = self._generateCallFunc([], node) |
paul@78 | 1182 | self._endCallFunc(temp) |
paul@57 | 1183 | |
paul@44 | 1184 | # Iterator on stack. |
paul@44 | 1185 | |
paul@44 | 1186 | # In the loop... |
paul@44 | 1187 | |
paul@44 | 1188 | self.set_label(next_label) |
paul@44 | 1189 | |
paul@44 | 1190 | # Use the iterator to get the next value. |
paul@44 | 1191 | |
paul@45 | 1192 | self._startCallFunc() |
paul@44 | 1193 | self.new_op(Duplicate()) |
paul@72 | 1194 | self._generateAttr(node, "next", (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@78 | 1195 | temp = self._generateCallFunc([], node) |
paul@78 | 1196 | self._endCallFunc(temp) |
paul@44 | 1197 | |
paul@44 | 1198 | # Test for StopIteration. |
paul@44 | 1199 | |
paul@57 | 1200 | self.dispatch(compiler.ast.Name("StopIteration")) |
paul@57 | 1201 | self.new_op(CheckException()) |
paul@44 | 1202 | if node.else_ is not None: |
paul@44 | 1203 | self.new_op(JumpIfTrue(else_label)) |
paul@44 | 1204 | else: |
paul@44 | 1205 | self.new_op(JumpIfTrue(exit_label)) |
paul@44 | 1206 | |
paul@44 | 1207 | # Assign to the target. |
paul@44 | 1208 | |
paul@44 | 1209 | self.dispatch(node.assign) |
paul@44 | 1210 | |
paul@44 | 1211 | # Process the body with the current next and exit points. |
paul@44 | 1212 | |
paul@57 | 1213 | self.add_loop_labels(next_label, exit_label) |
paul@44 | 1214 | self.dispatch(node.body) |
paul@57 | 1215 | self.drop_loop_labels() |
paul@44 | 1216 | |
paul@44 | 1217 | # Repeat the loop. |
paul@44 | 1218 | |
paul@44 | 1219 | self.new_op(Jump(next_label)) |
paul@44 | 1220 | |
paul@44 | 1221 | # Produce the "else" section. |
paul@44 | 1222 | |
paul@44 | 1223 | if node.else_ is not None: |
paul@44 | 1224 | self.set_label(exit_label) |
paul@44 | 1225 | self.dispatch(node.else_) |
paul@44 | 1226 | |
paul@44 | 1227 | # Pop the iterator. |
paul@44 | 1228 | |
paul@44 | 1229 | self.set_label(exit_label) |
paul@17 | 1230 | |
paul@19 | 1231 | def visitFrom(self, node): pass |
paul@17 | 1232 | |
paul@17 | 1233 | def visitFunction(self, node): |
paul@17 | 1234 | |
paul@17 | 1235 | # Only store the name when visiting this node from outside. |
paul@17 | 1236 | |
paul@39 | 1237 | if self.unit is not node.unit: |
paul@55 | 1238 | self.new_op(LoadConst(node.unit)) |
paul@72 | 1239 | self._visitName(node, (StoreName, StoreAddress)) |
paul@22 | 1240 | |
paul@73 | 1241 | # Generate the default initialisation code. |
paul@73 | 1242 | |
paul@73 | 1243 | for attr, default in zip(node.unit.default_attrs, node.unit.defaults): |
paul@73 | 1244 | self.dispatch(default) |
paul@73 | 1245 | self.new_op(StoreAddress(attr)) |
paul@73 | 1246 | |
paul@22 | 1247 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 1248 | |
paul@17 | 1249 | else: |
paul@19 | 1250 | self.dispatch(node.code) |
paul@68 | 1251 | if not isinstance(self.last_op(), Return): |
paul@68 | 1252 | self.dispatch(compiler.ast.Name("None")) |
paul@68 | 1253 | self.new_op(Return()) |
paul@17 | 1254 | |
paul@19 | 1255 | def visitGenExpr(self, node): pass |
paul@17 | 1256 | |
paul@19 | 1257 | def visitGenExprFor(self, node): pass |
paul@17 | 1258 | |
paul@19 | 1259 | def visitGenExprIf(self, node): pass |
paul@17 | 1260 | |
paul@19 | 1261 | def visitGenExprInner(self, node): pass |
paul@17 | 1262 | |
paul@43 | 1263 | def visitGetattr(self, node): |
paul@72 | 1264 | self._visitAttr(node, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@17 | 1265 | |
paul@19 | 1266 | def visitGlobal(self, node): pass |
paul@17 | 1267 | |
paul@17 | 1268 | def visitIf(self, node): |
paul@19 | 1269 | first = 1 |
paul@19 | 1270 | exit_label = self.new_label() |
paul@17 | 1271 | |
paul@19 | 1272 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 1273 | if body is None: |
paul@19 | 1274 | break |
paul@19 | 1275 | if not first: |
paul@19 | 1276 | self.set_label(next_label) |
paul@19 | 1277 | if test is not None: |
paul@19 | 1278 | self.dispatch(test) |
paul@19 | 1279 | next_label = self.new_label() |
paul@41 | 1280 | self.new_op(JumpIfFalse(next_label)) |
paul@19 | 1281 | self.dispatch(body) |
paul@19 | 1282 | self.new_op(Jump(exit_label)) |
paul@19 | 1283 | first = 0 |
paul@17 | 1284 | |
paul@19 | 1285 | self.set_label(exit_label) |
paul@17 | 1286 | |
paul@19 | 1287 | def visitImport(self, node): pass |
paul@19 | 1288 | |
paul@85 | 1289 | def visitInvert(self, node): |
paul@85 | 1290 | self._visitUnary(node, "__invert__") |
paul@17 | 1291 | |
paul@19 | 1292 | def visitKeyword(self, node): pass |
paul@17 | 1293 | |
paul@19 | 1294 | def visitLambda(self, node): pass |
paul@19 | 1295 | |
paul@85 | 1296 | def visitLeftShift(self, node): |
paul@85 | 1297 | self._visitBinary(node, "__lshift__", "__rlshift__") |
paul@17 | 1298 | |
paul@19 | 1299 | def visitList(self, node): pass |
paul@17 | 1300 | |
paul@19 | 1301 | def visitListComp(self, node): pass |
paul@17 | 1302 | |
paul@19 | 1303 | def visitListCompFor(self, node): pass |
paul@17 | 1304 | |
paul@19 | 1305 | def visitListCompIf(self, node): pass |
paul@19 | 1306 | |
paul@57 | 1307 | def visitMod(self, node): |
paul@57 | 1308 | self._visitBinary(node, "__mod__", "__rmod__") |
paul@17 | 1309 | |
paul@17 | 1310 | def visitModule(self, node): |
paul@19 | 1311 | self.dispatch(node.node) |
paul@17 | 1312 | |
paul@57 | 1313 | def visitMul(self, node): |
paul@57 | 1314 | self._visitBinary(node, "__mul__", "__rmul__") |
paul@17 | 1315 | |
paul@17 | 1316 | def visitName(self, node): |
paul@82 | 1317 | if node.name == "None": |
paul@82 | 1318 | const = self.module.constant_values[None] |
paul@82 | 1319 | self.new_op(LoadConst(const)) |
paul@82 | 1320 | else: |
paul@82 | 1321 | self._visitName(node, (LoadName, LoadAddress)) |
paul@17 | 1322 | |
paul@19 | 1323 | def visitNot(self, node): pass |
paul@17 | 1324 | |
paul@19 | 1325 | def visitOr(self, node): pass |
paul@17 | 1326 | |
paul@19 | 1327 | def visitPass(self, node): pass |
paul@17 | 1328 | |
paul@85 | 1329 | def visitPower(self, node): |
paul@85 | 1330 | self._visitBinary(node, "__pow__", "__rpow__") |
paul@17 | 1331 | |
paul@19 | 1332 | def visitPrint(self, node): pass |
paul@17 | 1333 | |
paul@19 | 1334 | def visitPrintnl(self, node): pass |
paul@17 | 1335 | |
paul@19 | 1336 | def visitRaise(self, node): pass |
paul@17 | 1337 | |
paul@21 | 1338 | def visitReturn(self, node): |
paul@21 | 1339 | if node.value is not None: |
paul@21 | 1340 | self.dispatch(node.value) |
paul@68 | 1341 | else: |
paul@68 | 1342 | self.dispatch(compiler.ast.Name("None")) |
paul@21 | 1343 | self.new_op(Return()) |
paul@17 | 1344 | |
paul@85 | 1345 | def visitRightShift(self, node): |
paul@85 | 1346 | self._visitBinary(node, "__rshift__", "__rrshift__") |
paul@17 | 1347 | |
paul@19 | 1348 | def visitSlice(self, node): pass |
paul@17 | 1349 | |
paul@17 | 1350 | def visitStmt(self, node): |
paul@17 | 1351 | for n in node.nodes: |
paul@19 | 1352 | self.dispatch(n) |
paul@95 | 1353 | self.reset_stack() |
paul@19 | 1354 | |
paul@57 | 1355 | def visitSub(self, node): |
paul@57 | 1356 | self._visitBinary(node, "__sub__", "__rsub__") |
paul@17 | 1357 | |
paul@19 | 1358 | def visitSubscript(self, node): pass |
paul@17 | 1359 | |
paul@57 | 1360 | def visitTryExcept(self, node): |
paul@57 | 1361 | exit_label = self.new_label() |
paul@57 | 1362 | handler_label = self.new_label() |
paul@57 | 1363 | |
paul@57 | 1364 | self.add_exception_labels(handler_label, exit_label) |
paul@57 | 1365 | |
paul@68 | 1366 | # Try... |
paul@68 | 1367 | # Produce the code, then jump to the exit. |
paul@68 | 1368 | |
paul@57 | 1369 | self.dispatch(node.body) |
paul@57 | 1370 | self.new_op(Jump(exit_label)) |
paul@57 | 1371 | |
paul@68 | 1372 | # Start of handlers. |
paul@68 | 1373 | |
paul@57 | 1374 | self.set_label(handler_label) |
paul@57 | 1375 | for name, assignment, handler in node.handlers: |
paul@57 | 1376 | next_label = self.new_label() |
paul@57 | 1377 | |
paul@68 | 1378 | # Test the given exception against the current exception. |
paul@68 | 1379 | |
paul@57 | 1380 | if name is not None: |
paul@57 | 1381 | self.dispatch(name) |
paul@57 | 1382 | self.new_op(CheckException()) |
paul@57 | 1383 | self.new_op(JumpIfFalse(next_label)) |
paul@57 | 1384 | |
paul@68 | 1385 | # Handle assignment to exception variable. |
paul@68 | 1386 | |
paul@57 | 1387 | if assignment is not None: |
paul@57 | 1388 | self.dispatch(assignment) |
paul@57 | 1389 | |
paul@68 | 1390 | # Produce the handler code, then jump to the exit. |
paul@68 | 1391 | |
paul@57 | 1392 | self.dispatch(handler) |
paul@57 | 1393 | self.new_op(Jump(exit_label)) |
paul@57 | 1394 | |
paul@57 | 1395 | self.set_label(next_label) |
paul@57 | 1396 | |
paul@57 | 1397 | # Unhandled exceptions. |
paul@57 | 1398 | |
paul@57 | 1399 | self.new_op(RaiseException()) |
paul@57 | 1400 | |
paul@57 | 1401 | # After exception |
paul@57 | 1402 | |
paul@57 | 1403 | self.set_label(exit_label) |
paul@57 | 1404 | |
paul@57 | 1405 | # Optional else clause. |
paul@57 | 1406 | |
paul@57 | 1407 | if node.else_ is not None: |
paul@57 | 1408 | self.dispatch(node.else_) |
paul@57 | 1409 | |
paul@57 | 1410 | self.drop_exception_labels() |
paul@17 | 1411 | |
paul@19 | 1412 | def visitTryFinally(self, node): pass |
paul@17 | 1413 | |
paul@19 | 1414 | def visitTuple(self, node): pass |
paul@17 | 1415 | |
paul@85 | 1416 | def visitUnaryAdd(self, node): |
paul@85 | 1417 | self._visitUnary(node, "__pos__") |
paul@17 | 1418 | |
paul@85 | 1419 | def visitUnarySub(self, node): |
paul@85 | 1420 | self._visitUnary(node, "__neg__") |
paul@17 | 1421 | |
paul@41 | 1422 | def visitWhile(self, node): |
paul@41 | 1423 | exit_label = self.new_label() |
paul@44 | 1424 | next_label = self.new_label() |
paul@44 | 1425 | else_label = self.new_label() |
paul@41 | 1426 | |
paul@41 | 1427 | self.set_label(next_label) |
paul@44 | 1428 | self.dispatch(node.test) |
paul@44 | 1429 | if node.else_ is not None: |
paul@44 | 1430 | self.new_op(JumpIfFalse(else_label)) |
paul@44 | 1431 | else: |
paul@44 | 1432 | self.new_op(JumpIfFalse(exit_label)) |
paul@44 | 1433 | |
paul@57 | 1434 | self.add_loop_labels(next_label, exit_label) |
paul@41 | 1435 | |
paul@41 | 1436 | self.dispatch(node.body) |
paul@41 | 1437 | self.new_op(Jump(next_label)) |
paul@41 | 1438 | |
paul@41 | 1439 | if node.else_ is not None: |
paul@44 | 1440 | self.set_label(else_label) |
paul@41 | 1441 | self.dispatch(node.else_) |
paul@41 | 1442 | |
paul@41 | 1443 | self.set_label(exit_label) |
paul@57 | 1444 | self.drop_loop_labels() |
paul@17 | 1445 | |
paul@19 | 1446 | def visitWith(self, node): pass |
paul@17 | 1447 | |
paul@19 | 1448 | def visitYield(self, node): pass |
paul@17 | 1449 | |
paul@21 | 1450 | # Useful data. |
paul@21 | 1451 | |
paul@21 | 1452 | comparison_methods = { |
paul@21 | 1453 | "==" : ("__eq__", "__ne__"), |
paul@21 | 1454 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 1455 | "<" : ("__lt__", "__gt__"), |
paul@21 | 1456 | "<=" : ("__le__", "__ge__"), |
paul@21 | 1457 | ">=" : ("__ge__", "__le__"), |
paul@21 | 1458 | ">" : ("__gt__", "__lt__"), |
paul@21 | 1459 | "is" : None, |
paul@21 | 1460 | "is not" : None, |
paul@21 | 1461 | "in" : None, |
paul@21 | 1462 | "not in" : None |
paul@21 | 1463 | } |
paul@21 | 1464 | |
paul@17 | 1465 | # vim: tabstop=4 expandtab shiftwidth=4 |