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