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