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