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