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