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