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