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