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