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