paul@196 | 1 | #!/usr/bin/env python |
paul@196 | 2 | |
paul@196 | 3 | """ |
paul@196 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@196 | 5 | |
paul@506 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> |
paul@196 | 7 | |
paul@196 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@196 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@196 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@196 | 11 | version. |
paul@196 | 12 | |
paul@196 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@196 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@196 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@196 | 16 | details. |
paul@196 | 17 | |
paul@196 | 18 | You should have received a copy of the GNU General Public License along with |
paul@196 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@196 | 20 | """ |
paul@196 | 21 | |
paul@555 | 22 | from micropython.common import operator_functions |
paul@196 | 23 | from micropython.data import * |
paul@555 | 24 | from micropython.errors import * |
paul@196 | 25 | from micropython.rsvp import * |
paul@196 | 26 | import compiler.ast |
paul@196 | 27 | |
paul@196 | 28 | class Helper: |
paul@196 | 29 | |
paul@196 | 30 | "Internal helper methods for AST visitors." |
paul@196 | 31 | |
paul@196 | 32 | # Common methods. |
paul@196 | 33 | |
paul@279 | 34 | def _generateGuards(self, node): |
paul@279 | 35 | |
paul@381 | 36 | if not (self.optimiser.should_optimise_accesses_by_attribute_usage() and hasattr(node, "_attrtypes")): |
paul@279 | 37 | return |
paul@279 | 38 | |
paul@279 | 39 | # For each name, attempt to restrict the type employed. |
paul@279 | 40 | |
paul@381 | 41 | for name, targets in node._attrtypes.items(): |
paul@307 | 42 | |
paul@307 | 43 | # NOTE: Need to merge targets using the same type but suggesting |
paul@309 | 44 | # NOTE: different kinds of attributes (instance vs. class). |
paul@279 | 45 | |
paul@279 | 46 | # Where only one object type is suggested, produce a guard. |
paul@279 | 47 | # NOTE: This only supports classes as types, not modules. |
paul@279 | 48 | |
paul@297 | 49 | if len(targets) == 1: |
paul@307 | 50 | target_name, is_static = list(targets)[0] |
paul@279 | 51 | |
paul@279 | 52 | # Access the object table to get the attribute. |
paul@535 | 53 | # This depends on a special entry in the table for class |
paul@535 | 54 | # equivalence tests. |
paul@279 | 55 | |
paul@279 | 56 | try: |
paul@535 | 57 | obj = self.objtable.get_object(target_name) |
paul@291 | 58 | |
paul@291 | 59 | # Where no attribute entry exists, the target could be a module. |
paul@417 | 60 | # NOTE: Should perhaps raise an error. |
paul@291 | 61 | |
paul@279 | 62 | except TableError, exc: |
paul@291 | 63 | print "Possible guard for", target_name, "not enforceable." |
paul@291 | 64 | continue |
paul@279 | 65 | |
paul@299 | 66 | # NOTE: Could test the correctness of the guard where the nature |
paul@299 | 67 | # NOTE: of the name is known. |
paul@299 | 68 | # NOTE: The known value would be retrieved from the unit's |
paul@299 | 69 | # NOTE: locals and tested as being a class or an instance of a |
paul@299 | 70 | # NOTE: particular class. |
paul@299 | 71 | |
paul@299 | 72 | # Generate the guard by loading a reference to the class. |
paul@299 | 73 | |
paul@279 | 74 | after_test_block = self.new_block() |
paul@279 | 75 | |
paul@450 | 76 | self.new_op(LoadClass(obj, target="source")) |
paul@285 | 77 | |
paul@297 | 78 | # For only static attributes, classes are acceptable. |
paul@297 | 79 | |
paul@297 | 80 | if is_static: |
paul@297 | 81 | |
paul@297 | 82 | # Generate name is target (for classes). |
paul@285 | 83 | |
paul@297 | 84 | self.dispatch(compiler.ast.Name(name)) |
paul@450 | 85 | self.new_op(TestIdentity(source="source", target="status")) |
paul@285 | 86 | |
paul@297 | 87 | # Jump to the next guard or the code if successful. |
paul@285 | 88 | |
paul@450 | 89 | self.new_op(JumpIfTrue(after_test_block, working="status")) |
paul@297 | 90 | |
paul@297 | 91 | # Where instance attributes are involved, only instances are |
paul@297 | 92 | # acceptable. |
paul@285 | 93 | |
paul@563 | 94 | if not isinstance(obj, Module): |
paul@563 | 95 | |
paul@563 | 96 | # Generate isinstance(name, target). |
paul@279 | 97 | |
paul@563 | 98 | self.dispatch(compiler.ast.Name(name)) |
paul@563 | 99 | self.new_op(CheckInstance(source="source", target="status")) |
paul@279 | 100 | |
paul@563 | 101 | # Jump to the next guard or the code if successful. |
paul@279 | 102 | |
paul@563 | 103 | self.new_op(JumpIfTrue(after_test_block, working="status")) |
paul@279 | 104 | |
paul@279 | 105 | # Where the type is inappropriate, raise an exception. |
paul@279 | 106 | |
paul@446 | 107 | self.make_exception("TypeError") |
paul@450 | 108 | self.set_target("exception") |
paul@279 | 109 | self.new_op(RaiseException()) |
paul@279 | 110 | |
paul@279 | 111 | self.set_block(after_test_block) |
paul@279 | 112 | |
paul@196 | 113 | def _visitAttr(self, node, classes): |
paul@196 | 114 | |
paul@196 | 115 | """ |
paul@196 | 116 | Visit the attribute-related 'node', generating instructions based on the |
paul@196 | 117 | given 'classes'. |
paul@196 | 118 | """ |
paul@196 | 119 | |
paul@196 | 120 | self.dispatch(node.expr) |
paul@196 | 121 | self._generateAttr(node, node.attrname, classes) |
paul@196 | 122 | |
paul@196 | 123 | def _generateAttr(self, node, attrname, classes): |
paul@196 | 124 | |
paul@196 | 125 | """ |
paul@196 | 126 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@196 | 127 | """ |
paul@196 | 128 | |
paul@196 | 129 | AddressInstruction, AddressContextInstruction, AddressContextCondInstruction, \ |
paul@226 | 130 | AttrInstruction, AttrIndexInstruction, AttrIndexContextCondInstruction = classes |
paul@196 | 131 | |
paul@196 | 132 | # Where the last operation (defining the attribute owner) yields a |
paul@196 | 133 | # constant... |
paul@196 | 134 | |
paul@401 | 135 | target_plus_name = self.optimiser.optimise_constant_accessor() |
paul@196 | 136 | |
paul@196 | 137 | # Only try and discover the position if the target can be resolved. |
paul@469 | 138 | # Since instances cannot be constants in general, this involves classes |
paul@469 | 139 | # and modules, but constants known at compile-time must also be handled. |
paul@196 | 140 | |
paul@401 | 141 | if target_plus_name is not None: |
paul@401 | 142 | target, target_name = target_plus_name |
paul@401 | 143 | |
paul@401 | 144 | # Check for class.__class__. |
paul@401 | 145 | |
paul@401 | 146 | if attrname == "__class__": |
paul@401 | 147 | if isinstance(target, Class): |
paul@405 | 148 | if AddressInstruction is LoadAddress: |
paul@484 | 149 | self.replace_active_value("working", LoadAddress(self.get_builtin("type"))) |
paul@405 | 150 | return |
paul@405 | 151 | else: |
paul@405 | 152 | raise TranslateError("Assigning to __class__ is not permitted.") |
paul@196 | 153 | |
paul@268 | 154 | # Access the object table to get the attribute. |
paul@196 | 155 | |
paul@196 | 156 | try: |
paul@268 | 157 | attr = self.objtable.access(target_name, attrname) |
paul@268 | 158 | except TableError, exc: |
paul@339 | 159 | raise TranslateError(exc.args[0]) |
paul@196 | 160 | |
paul@196 | 161 | # Produce a suitable instruction. |
paul@196 | 162 | |
paul@401 | 163 | if AddressInstruction is not None: |
paul@469 | 164 | |
paul@469 | 165 | # Where the target is a constant instance, the constant input |
paul@469 | 166 | # needs to be retained as the context of the resulting |
paul@469 | 167 | # attribute. |
paul@469 | 168 | |
paul@469 | 169 | if isinstance(target, Instance): |
paul@469 | 170 | self.new_op(AddressContextInstruction(attr)) |
paul@469 | 171 | |
paul@469 | 172 | # It is acceptable to replace the instruction providing the |
paul@469 | 173 | # constant input because doing so does not lose any input |
paul@469 | 174 | # information required by the replacement instructions. |
paul@469 | 175 | |
paul@469 | 176 | else: |
paul@484 | 177 | self.replace_active_value("working", AddressInstruction(attr)) |
paul@196 | 178 | else: |
paul@339 | 179 | raise TranslateError("Storing of class or module attribute %r via an object is not permitted." % attrname) |
paul@196 | 180 | |
paul@196 | 181 | return |
paul@196 | 182 | |
paul@196 | 183 | # Where the last operation involves the special 'self' name, check to |
paul@196 | 184 | # see if the attribute is acceptably positioned and produce a direct |
paul@196 | 185 | # access to the attribute. |
paul@196 | 186 | |
paul@196 | 187 | # This is the only reliable way of detecting instance accesses at |
paul@196 | 188 | # compile-time since in general, objects could be classes or modules, |
paul@196 | 189 | # but 'self' should only refer to instances. |
paul@196 | 190 | |
paul@196 | 191 | elif self.optimiser.optimise_self_access(self.unit, attrname): |
paul@196 | 192 | |
paul@196 | 193 | # Either generate an instruction operating on an instance attribute. |
paul@196 | 194 | |
paul@196 | 195 | try: |
paul@196 | 196 | attr = self.unit.parent.instance_attributes()[attrname] |
paul@196 | 197 | self.new_op(AttrInstruction(attr)) |
paul@196 | 198 | return |
paul@196 | 199 | |
paul@196 | 200 | # Or generate an instruction operating on a class attribute. |
paul@196 | 201 | # NOTE: Any simple instruction providing self is not removed. |
paul@196 | 202 | |
paul@196 | 203 | except KeyError: |
paul@196 | 204 | |
paul@196 | 205 | try: |
paul@196 | 206 | attr = self.unit.parent.all_attributes()[attrname] |
paul@196 | 207 | |
paul@196 | 208 | # Switch the context if the class attribute is compatible with |
paul@196 | 209 | # the instance. |
paul@196 | 210 | |
paul@196 | 211 | if attr.defined_within_hierarchy(): |
paul@196 | 212 | |
paul@196 | 213 | # Only permit loading (not storing) of class attributes via self. |
paul@196 | 214 | |
paul@196 | 215 | if AddressContextInstruction is not None: |
paul@196 | 216 | self.new_op(AddressContextInstruction(attr)) |
paul@196 | 217 | else: |
paul@339 | 218 | raise TranslateError("Storing of class attribute %r via self not permitted." % attrname) |
paul@196 | 219 | |
paul@196 | 220 | # Preserve the context if the class attribute comes from an |
paul@196 | 221 | # incompatible class. |
paul@196 | 222 | |
paul@196 | 223 | elif attr.defined_outside_hierarchy(): |
paul@196 | 224 | |
paul@196 | 225 | # Only permit loading (not storing) of class attributes via self. |
paul@196 | 226 | |
paul@196 | 227 | if AddressInstruction is not None: |
paul@196 | 228 | self.new_op(AddressInstruction(attr)) |
paul@196 | 229 | else: |
paul@339 | 230 | raise TranslateError("Storing of class attribute %r via self not permitted." % attrname) |
paul@196 | 231 | |
paul@196 | 232 | # Otherwise, test for a suitable context at run-time. |
paul@196 | 233 | |
paul@196 | 234 | else: |
paul@196 | 235 | |
paul@196 | 236 | # Only permit loading (not storing) of class attributes via self. |
paul@196 | 237 | |
paul@196 | 238 | if AddressContextCondInstruction is not None: |
paul@196 | 239 | self.new_op(AddressContextCondInstruction(attr)) |
paul@196 | 240 | else: |
paul@339 | 241 | raise TranslateError("Storing of class attribute %r via self not permitted." % attrname) |
paul@196 | 242 | |
paul@196 | 243 | return |
paul@196 | 244 | |
paul@196 | 245 | # Or delegate the attribute access to a general instruction |
paul@196 | 246 | # since the kind of attribute cannot be deduced. |
paul@196 | 247 | |
paul@196 | 248 | except KeyError: |
paul@196 | 249 | pass |
paul@196 | 250 | |
paul@279 | 251 | # Attempt to deduce the target of an attribute access by searching for a |
paul@279 | 252 | # unique type providing the names associated with the accessed object. |
paul@279 | 253 | |
paul@268 | 254 | elif self.optimiser.should_optimise_accesses_by_attribute_usage(): |
paul@268 | 255 | |
paul@357 | 256 | target_names = self.possible_accessor_types(node) |
paul@309 | 257 | |
paul@357 | 258 | if target_names is not None and len(target_names) == 1: |
paul@401 | 259 | target_name, is_static = list(target_names)[0] |
paul@401 | 260 | |
paul@401 | 261 | # Check for class.__class__. |
paul@401 | 262 | |
paul@401 | 263 | if attrname == "__class__": |
paul@401 | 264 | if is_static: |
paul@401 | 265 | self.load_builtin("type", node) |
paul@401 | 266 | return |
paul@268 | 267 | |
paul@357 | 268 | # Access the object table to get the attribute. |
paul@268 | 269 | |
paul@357 | 270 | try: |
paul@357 | 271 | attr = self.objtable.access(target_name, attrname) |
paul@268 | 272 | |
paul@357 | 273 | # Disallow non-class/instance optimisations. |
paul@292 | 274 | |
paul@357 | 275 | except TableError, exc: |
paul@357 | 276 | print "Possible optimisation for", target_name, "not permissable." |
paul@357 | 277 | |
paul@357 | 278 | # Produce a suitable instruction. |
paul@292 | 279 | |
paul@357 | 280 | else: |
paul@357 | 281 | if AddressContextCondInstruction is not None and attr.is_static_attribute(): |
paul@357 | 282 | self.new_op(AddressContextCondInstruction(attr)) |
paul@357 | 283 | elif AttrInstruction is not None and not attr.is_static_attribute(): |
paul@357 | 284 | self.new_op(AttrInstruction(attr)) |
paul@268 | 285 | else: |
paul@357 | 286 | raise TranslateError("Storing of class or module attribute %r via an object is not permitted." % attrname) |
paul@268 | 287 | |
paul@357 | 288 | return |
paul@268 | 289 | |
paul@401 | 290 | # Check for class.__class__. |
paul@401 | 291 | |
paul@401 | 292 | if attrname == "__class__": |
paul@401 | 293 | |
paul@401 | 294 | # Remember the accessor. |
paul@401 | 295 | |
paul@450 | 296 | temp_accessor = self.get_temp() |
paul@401 | 297 | |
paul@401 | 298 | attr_block = self.new_block() |
paul@401 | 299 | end_block = self.new_block() |
paul@401 | 300 | |
paul@450 | 301 | self.new_op(CheckClass(target="status")) |
paul@450 | 302 | self.new_op(JumpIfFalse(attr_block, working="status")) |
paul@401 | 303 | self.load_builtin("type", node) |
paul@401 | 304 | self.new_op(Jump(end_block)) |
paul@401 | 305 | self.set_block(attr_block) |
paul@401 | 306 | |
paul@401 | 307 | # Recall the accessor. |
paul@401 | 308 | |
paul@464 | 309 | self.new_op(temp_accessor) |
paul@401 | 310 | |
paul@196 | 311 | # Otherwise, perform a normal operation. |
paul@196 | 312 | |
paul@196 | 313 | try: |
paul@196 | 314 | index = self.objtable.get_index(attrname) |
paul@196 | 315 | |
paul@196 | 316 | except self.objtable.TableError: |
paul@196 | 317 | |
paul@196 | 318 | # If this error arises on generated code, check the names_used |
paul@196 | 319 | # attribute on the Importer. |
paul@196 | 320 | |
paul@339 | 321 | raise TranslateError("No attribute entry exists for name %r." % attrname) |
paul@196 | 322 | |
paul@196 | 323 | # NOTE: Test for class vs. instance attributes, generating |
paul@196 | 324 | # NOTE: context-related instructions. |
paul@196 | 325 | |
paul@196 | 326 | if AttrIndexContextCondInstruction is not None: |
paul@196 | 327 | self.new_op(AttrIndexContextCondInstruction(index)) |
paul@196 | 328 | |
paul@196 | 329 | # Store instructions do not need to consider context modifications. |
paul@196 | 330 | |
paul@196 | 331 | else: |
paul@196 | 332 | self.new_op(AttrIndexInstruction(index)) |
paul@196 | 333 | |
paul@401 | 334 | # Where __class__ was involved, define the start of the following code. |
paul@401 | 335 | |
paul@401 | 336 | if attrname == "__class__": |
paul@401 | 337 | self.set_block(end_block) |
paul@450 | 338 | self.discard_temp(temp_accessor) |
paul@401 | 339 | |
paul@196 | 340 | # Invocations involve the following: |
paul@196 | 341 | # |
paul@196 | 342 | # 1. Reservation of a frame for the arguments |
paul@196 | 343 | # 2. Identification of the target which is then held in temporary storage |
paul@196 | 344 | # 3. Optional inclusion of a context (important for methods) |
paul@196 | 345 | # 4. Preparation of the argument frame |
paul@196 | 346 | # 5. Invocation of the target |
paul@196 | 347 | # 6. Discarding of the frame |
paul@196 | 348 | # |
paul@196 | 349 | # In order to support nested invocations - such as a(b(c)) - use of the |
paul@196 | 350 | # temporary storage is essential. |
paul@196 | 351 | |
paul@196 | 352 | def _startCallFunc(self): |
paul@196 | 353 | |
paul@196 | 354 | "Record the location of the invocation." |
paul@196 | 355 | |
paul@196 | 356 | op = MakeFrame() |
paul@196 | 357 | self.new_op(op) # records the start of the frame |
paul@196 | 358 | self.frame_makers.append(op) |
paul@196 | 359 | |
paul@196 | 360 | def _generateCallFunc(self, args, node): |
paul@196 | 361 | |
paul@196 | 362 | """ |
paul@196 | 363 | Support a generic function invocation using the given 'args', occurring |
paul@196 | 364 | on the given 'node', where the expression providing the invocation |
paul@196 | 365 | target has just been generated. |
paul@196 | 366 | |
paul@196 | 367 | In other situations, the invocation is much simpler and does not need to |
paul@196 | 368 | handle the full flexibility of a typical Python invocation. Internal |
paul@196 | 369 | invocations, such as those employed by operators and certain |
paul@196 | 370 | control-flow mechanisms, use predetermined arguments and arguably do not |
paul@196 | 371 | need to support the same things as the more general invocations. |
paul@196 | 372 | """ |
paul@196 | 373 | |
paul@234 | 374 | target, context, temp_target, temp_context = self._generateCallFuncContext() |
paul@234 | 375 | self._generateCallFuncArgs(target, context, temp_target, temp_context, args, node) |
paul@234 | 376 | return temp_target, target, temp_context |
paul@196 | 377 | |
paul@196 | 378 | def _generateCallFuncContext(self): |
paul@196 | 379 | |
paul@196 | 380 | """ |
paul@196 | 381 | Produce code which loads and checks the context of the current |
paul@196 | 382 | invocation, the instructions for whose target have already been |
paul@196 | 383 | produced, returning a list of instructions which reference the |
paul@196 | 384 | invocation target. |
paul@196 | 385 | """ |
paul@196 | 386 | |
paul@196 | 387 | t = self.optimiser.optimise_known_target() |
paul@196 | 388 | if t: |
paul@196 | 389 | target, context = t |
paul@331 | 390 | |
paul@331 | 391 | # Detect dynamic functions acting like instances. |
paul@331 | 392 | |
paul@331 | 393 | if isinstance(target, Function) and target.is_dynamic(): |
paul@196 | 394 | target, context = None, None |
paul@196 | 395 | else: |
paul@196 | 396 | target, context = None, None |
paul@196 | 397 | |
paul@196 | 398 | # Store the target in temporary storage for subsequent referencing. |
paul@196 | 399 | |
paul@234 | 400 | temp_target = self.optimiser.optimise_temp_storage() |
paul@196 | 401 | |
paul@196 | 402 | # Where a target or context are not known or where an instance is known |
paul@196 | 403 | # to be the context, load the context. |
paul@196 | 404 | |
paul@196 | 405 | if target is None or isinstance(context, Instance): |
paul@234 | 406 | self.new_op(temp_target) |
paul@450 | 407 | self.new_op(Transfer(source="working_context", target="working")) |
paul@234 | 408 | temp_context = self.optimiser.optimise_temp_storage() |
paul@196 | 409 | self.new_op(StoreFrame(0)) |
paul@196 | 410 | |
paul@234 | 411 | # Class contexts should be made available for testing of the first |
paul@234 | 412 | # argument. |
paul@234 | 413 | # NOTE: Class methods should eventually be supported. |
paul@234 | 414 | |
paul@234 | 415 | elif isinstance(context, Class): |
paul@234 | 416 | self.new_op(temp_target) |
paul@450 | 417 | self.new_op(Transfer(source="working_context", target="working")) |
paul@234 | 418 | temp_context = self.optimiser.optimise_temp_storage() |
paul@234 | 419 | |
paul@196 | 420 | # Otherwise omit the context. |
paul@196 | 421 | |
paul@196 | 422 | else: |
paul@234 | 423 | temp_context = None |
paul@196 | 424 | |
paul@234 | 425 | return target, context, temp_target, temp_context |
paul@196 | 426 | |
paul@234 | 427 | def _generateCallFuncArgs(self, target, context, temp_target, temp_context, args, node): |
paul@196 | 428 | |
paul@196 | 429 | """ |
paul@234 | 430 | Given invocation 'target' and 'context' information, the 'temp_target' |
paul@234 | 431 | reference to the target, the 'temp_context' reference to the context, a |
paul@234 | 432 | list of nodes representing the 'args' (arguments), generate instructions |
paul@234 | 433 | which load the arguments for the invocation defined by the given 'node'. |
paul@196 | 434 | """ |
paul@196 | 435 | |
paul@196 | 436 | # Evaluate the arguments. |
paul@196 | 437 | |
paul@196 | 438 | employed_positions = set() |
paul@196 | 439 | employed_keywords = set() |
paul@196 | 440 | extra_keywords = [] |
paul@234 | 441 | positional_args = [] |
paul@234 | 442 | keyword_args = [] |
paul@196 | 443 | |
paul@196 | 444 | # Find keyword arguments in advance in order to help resolve targets. |
paul@196 | 445 | |
paul@591 | 446 | have_keywords = False |
paul@234 | 447 | |
paul@196 | 448 | for arg in args: |
paul@196 | 449 | if isinstance(arg, compiler.ast.Keyword): |
paul@196 | 450 | employed_keywords.add(arg.name) |
paul@234 | 451 | keyword_args.append(arg) |
paul@591 | 452 | have_keywords = True |
paul@234 | 453 | elif not have_keywords: |
paul@234 | 454 | positional_args.append(arg) |
paul@196 | 455 | |
paul@196 | 456 | possible_targets = self.paramtable.all_possible_objects(employed_keywords) |
paul@196 | 457 | |
paul@196 | 458 | # Note the presence of the context in the frame where appropriate. |
paul@196 | 459 | |
paul@234 | 460 | # For unknown invocations and method invocations. |
paul@234 | 461 | |
paul@196 | 462 | if target is None or isinstance(context, Instance): |
paul@196 | 463 | ncontext = 1 |
paul@591 | 464 | expect_testable_self = False |
paul@196 | 465 | |
paul@234 | 466 | # Handle calls to classes by obtaining the instantiator function. |
paul@230 | 467 | # A context is reserved for the new instance, but this is not provided |
paul@230 | 468 | # in the invocation (since the instantiator will fill the locals slot |
paul@230 | 469 | # concerned). |
paul@196 | 470 | |
paul@196 | 471 | elif isinstance(target, Class): |
paul@230 | 472 | ncontext = 1 |
paul@591 | 473 | expect_testable_self = False |
paul@203 | 474 | target = target.get_instantiator() |
paul@196 | 475 | |
paul@196 | 476 | # Method calls via classes. |
paul@196 | 477 | |
paul@196 | 478 | elif isinstance(context, Class): |
paul@196 | 479 | ncontext = 0 |
paul@591 | 480 | expect_testable_self = True |
paul@196 | 481 | |
paul@196 | 482 | # Function calls. |
paul@196 | 483 | |
paul@196 | 484 | else: |
paul@196 | 485 | ncontext = 0 |
paul@591 | 486 | expect_testable_self = False |
paul@234 | 487 | |
paul@234 | 488 | # Traverse the positional arguments adding them using the incrementing |
paul@234 | 489 | # frame position. |
paul@196 | 490 | |
paul@591 | 491 | first = True |
paul@196 | 492 | frame_pos = ncontext |
paul@234 | 493 | temp_first_argument = None |
paul@196 | 494 | |
paul@234 | 495 | for arg in positional_args: |
paul@234 | 496 | self.dispatch(arg) |
paul@234 | 497 | self.new_op(StoreFrame(frame_pos)) |
paul@234 | 498 | employed_positions.add(frame_pos) |
paul@196 | 499 | |
paul@196 | 500 | # Check to see if the first argument is appropriate (compatible with |
paul@196 | 501 | # the target where methods are being invoked via classes). |
paul@196 | 502 | |
paul@234 | 503 | if first and (expect_testable_self or target is None): |
paul@196 | 504 | |
paul@196 | 505 | # Drop any test if the target and the context are known. |
paul@196 | 506 | |
paul@196 | 507 | if not self.optimiser.have_correct_self_for_target(context, self.unit): |
paul@196 | 508 | |
paul@234 | 509 | # Otherwise, remember the first argument for a subsequent |
paul@234 | 510 | # test. |
paul@196 | 511 | |
paul@234 | 512 | temp_first_argument = self.optimiser.optimise_temp_storage() |
paul@196 | 513 | |
paul@591 | 514 | first = False |
paul@196 | 515 | frame_pos += 1 |
paul@196 | 516 | |
paul@234 | 517 | # Adjust the invocation frame for unknown invocations. |
paul@234 | 518 | # Test the first argument if appropriate. |
paul@234 | 519 | |
paul@455 | 520 | self._generateCallFuncContextTest(target, temp_context, temp_first_argument, node) |
paul@234 | 521 | |
paul@234 | 522 | # Traverse the keyword arguments adding them at the appropriate frame |
paul@234 | 523 | # positions. |
paul@234 | 524 | |
paul@234 | 525 | max_keyword_pos = -1 |
paul@234 | 526 | |
paul@234 | 527 | for arg in keyword_args: |
paul@234 | 528 | |
paul@234 | 529 | # Optimise where the target is known now. |
paul@234 | 530 | |
paul@234 | 531 | if target is not None: |
paul@234 | 532 | |
paul@234 | 533 | # Find the parameter table entry for the target. |
paul@234 | 534 | |
paul@234 | 535 | target_name = target.full_name() |
paul@234 | 536 | |
paul@234 | 537 | # Look for a callable with the precise target name. |
paul@234 | 538 | |
paul@234 | 539 | table_entry = self.paramtable.table[target_name] |
paul@234 | 540 | |
paul@234 | 541 | # Look the name up in the parameter table entry. |
paul@234 | 542 | |
paul@234 | 543 | try: |
paul@234 | 544 | pos = table_entry[arg.name] |
paul@234 | 545 | |
paul@234 | 546 | # Where no position is found, this could be an extra keyword |
paul@234 | 547 | # argument. |
paul@234 | 548 | |
paul@234 | 549 | except KeyError: |
paul@234 | 550 | extra_keywords.append(arg) |
paul@234 | 551 | continue |
paul@234 | 552 | |
paul@234 | 553 | # Test for illegal conditions. |
paul@234 | 554 | |
paul@234 | 555 | if pos in employed_positions: |
paul@339 | 556 | raise TranslateError("Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@234 | 557 | |
paul@234 | 558 | employed_positions.add(pos) |
paul@234 | 559 | |
paul@234 | 560 | # Generate code for the keyword and the positioning |
paul@234 | 561 | # operation. |
paul@234 | 562 | |
paul@234 | 563 | self.dispatch(arg.expr) |
paul@234 | 564 | self.new_op(StoreFrame(pos)) |
paul@234 | 565 | |
paul@234 | 566 | # Otherwise, generate the code needed to obtain the details of |
paul@234 | 567 | # the parameter location. |
paul@234 | 568 | |
paul@234 | 569 | else: |
paul@234 | 570 | |
paul@234 | 571 | # Combine the target details with the name to get the location. |
paul@234 | 572 | # See the access method on the List class. |
paul@234 | 573 | |
paul@234 | 574 | try: |
paul@234 | 575 | paramindex = self.paramtable.get_index(arg.name) |
paul@234 | 576 | |
paul@234 | 577 | # Where no position is found, this could be an extra keyword |
paul@234 | 578 | # argument. |
paul@234 | 579 | |
paul@234 | 580 | except self.paramtable.TableError: |
paul@234 | 581 | extra_keywords.append(arg) |
paul@234 | 582 | continue |
paul@234 | 583 | |
paul@234 | 584 | # Generate code for the keyword and the positioning |
paul@234 | 585 | # operation. Get the value as the source of the assignment. |
paul@234 | 586 | |
paul@234 | 587 | self.dispatch(arg.expr) |
paul@234 | 588 | self.record_value() |
paul@469 | 589 | self.start_target() |
paul@234 | 590 | |
paul@234 | 591 | # Store the source value using the callable's parameter |
paul@234 | 592 | # table information. |
paul@234 | 593 | |
paul@234 | 594 | self.new_op(temp_target) |
paul@234 | 595 | self.new_op(StoreFrameIndex(paramindex)) |
paul@234 | 596 | |
paul@451 | 597 | self.assign_value() |
paul@234 | 598 | self.discard_value() |
paul@234 | 599 | |
paul@234 | 600 | # Record the highest possible frame position for this argument. |
paul@234 | 601 | |
paul@234 | 602 | max_keyword_pos = max(max_keyword_pos, max(self.paramtable.all_attribute_positions(arg.name))) |
paul@234 | 603 | |
paul@234 | 604 | # Use the frame position counter as a general argument counter. |
paul@234 | 605 | |
paul@234 | 606 | frame_pos += 1 |
paul@234 | 607 | |
paul@196 | 608 | # NOTE: Extra keywords are not supported. |
paul@196 | 609 | # NOTE: Somehow, the above needs to be combined with * arguments. |
paul@196 | 610 | |
paul@196 | 611 | if extra_keywords: |
paul@196 | 612 | print "Warning: extra keyword argument(s) %s not handled." % ", ".join([arg.name for arg in extra_keywords]) |
paul@196 | 613 | |
paul@196 | 614 | # Either test for a complete set of arguments. |
paul@196 | 615 | |
paul@196 | 616 | if target is not None: |
paul@196 | 617 | |
paul@196 | 618 | # Make sure that enough arguments have been given. |
paul@196 | 619 | |
paul@196 | 620 | nargs_max = len(target.positional_names) |
paul@196 | 621 | ndefaults = len(target.defaults) |
paul@196 | 622 | nargs_min = nargs_max - ndefaults |
paul@196 | 623 | |
paul@213 | 624 | # Visit each argument position and look for a supplied argument. |
paul@213 | 625 | |
paul@196 | 626 | for i in range(ncontext, nargs_min): |
paul@196 | 627 | if i not in employed_positions: |
paul@339 | 628 | raise TranslateError( |
paul@196 | 629 | "Argument %r not supplied for %r: need at least %d argument(s)." % (i+1, target.name, nargs_min)) |
paul@196 | 630 | |
paul@196 | 631 | nargs = frame_pos |
paul@196 | 632 | |
paul@213 | 633 | # Determine whether too many arguments have been given and how big |
paul@213 | 634 | # the frame should be. |
paul@213 | 635 | |
paul@213 | 636 | # For parameter lists with * or ** parameters, accept as many |
paul@213 | 637 | # arguments as are allowed or as many as we have. |
paul@213 | 638 | |
paul@213 | 639 | if target.has_star or target.has_dstar: |
paul@213 | 640 | frame_size = max(nargs, nargs_max) |
paul@213 | 641 | |
paul@214 | 642 | # NOTE: We now need to pack these arguments into a suitable |
paul@214 | 643 | # NOTE: structure for the * parameter. |
paul@214 | 644 | |
paul@213 | 645 | # For other parameter lists, only accept as many arguments as we are |
paul@213 | 646 | # allowed. |
paul@213 | 647 | |
paul@213 | 648 | elif nargs > nargs_max: |
paul@339 | 649 | raise TranslateError( |
paul@196 | 650 | "Too many arguments for %r: need at most %d argument(s)." % (target.name, nargs_max)) |
paul@196 | 651 | |
paul@213 | 652 | else: |
paul@213 | 653 | frame_size = nargs_max |
paul@213 | 654 | |
paul@196 | 655 | # Where defaults are involved, put them into the frame. |
paul@196 | 656 | |
paul@234 | 657 | self._generateCallFuncDefaultArgs(target, temp_target, nargs_min, nargs_max, employed_positions) |
paul@196 | 658 | |
paul@196 | 659 | # Set the frame size. |
paul@196 | 660 | |
paul@213 | 661 | self._endCallFuncArgs(frame_size) |
paul@196 | 662 | |
paul@215 | 663 | # Or just set the frame size and have the function check the arguments. |
paul@196 | 664 | |
paul@196 | 665 | else: |
paul@196 | 666 | max_pos = max(max(employed_positions or [-1]), max_keyword_pos, frame_pos - 1) |
paul@196 | 667 | self._endCallFuncArgs(max_pos + 1) |
paul@196 | 668 | |
paul@234 | 669 | def _generateCallFuncDefaultArgs(self, target, temp_target, nargs_min, nargs_max, employed_positions): |
paul@196 | 670 | |
paul@196 | 671 | """ |
paul@234 | 672 | For the given 'target' and 'temp_target' reference to the target, |
paul@234 | 673 | generate default arguments for those positions in the range |
paul@234 | 674 | 'nargs_min'...'nargs_max' which are not present in the |
paul@234 | 675 | 'employed_positions' collection. |
paul@196 | 676 | """ |
paul@196 | 677 | |
paul@331 | 678 | # Where appropriate, construct a dynamic object to hold the defaults. |
paul@196 | 679 | |
paul@331 | 680 | dynamic = target.is_dynamic() |
paul@196 | 681 | |
paul@196 | 682 | # Here, we use negative index values to visit the right hand end of |
paul@196 | 683 | # the defaults list. |
paul@196 | 684 | |
paul@196 | 685 | for pos in range(nargs_min, nargs_max): |
paul@196 | 686 | if pos not in employed_positions: |
paul@196 | 687 | if dynamic: |
paul@234 | 688 | self.new_op(temp_target) |
paul@196 | 689 | self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@196 | 690 | else: |
paul@196 | 691 | self.new_op(LoadAddress(target.default_attrs[pos - nargs_min])) |
paul@196 | 692 | self.new_op(StoreFrame(pos)) |
paul@196 | 693 | |
paul@455 | 694 | def _generateCallFuncContextTest(self, target, temp_context, temp_first_argument, node): |
paul@234 | 695 | |
paul@234 | 696 | """ |
paul@455 | 697 | Generate code involved in a call to the given 'target' to test the |
paul@455 | 698 | context provided by 'temp_context' against 'temp_first_argument', and to |
paul@479 | 699 | signal an exception if the context is incompatible with the first frame |
paul@479 | 700 | argument. |
paul@234 | 701 | |
paul@234 | 702 | In addition, the invocation frame will be shifted if 'temp_context' |
paul@234 | 703 | indicates a function or a class. |
paul@234 | 704 | """ |
paul@234 | 705 | |
paul@479 | 706 | # Need to store the explicit first argument in the working register for |
paul@479 | 707 | # the eventual test. |
paul@479 | 708 | |
paul@479 | 709 | if temp_first_argument is not None: |
paul@479 | 710 | self.new_op(temp_first_argument) |
paul@479 | 711 | |
paul@479 | 712 | # Put the context in the source register. |
paul@479 | 713 | |
paul@479 | 714 | if target is None or temp_first_argument is not None: |
paul@479 | 715 | if self.new_op(temp_context.copy()): |
paul@479 | 716 | self.last_op().target = "source" |
paul@479 | 717 | else: |
paul@479 | 718 | self.new_op(Transfer(source="working", target="source")) |
paul@234 | 719 | |
paul@234 | 720 | # Add some preliminary tests where the target is not known. |
paul@234 | 721 | |
paul@234 | 722 | if target is None: |
paul@479 | 723 | if temp_first_argument is not None: |
paul@479 | 724 | self.new_op(JumpInFrameDirect(self.native.getCallFuncUnknownTargetTestableSelf())) |
paul@479 | 725 | else: |
paul@479 | 726 | self.new_op(JumpInFrameDirect(self.native.getCallFuncUnknownTarget())) |
paul@234 | 727 | |
paul@479 | 728 | elif temp_first_argument is not None: |
paul@479 | 729 | self.new_op(JumpInFrameDirect(self.native.getCallFuncKnownTargetTestableSelf())) |
paul@234 | 730 | |
paul@234 | 731 | def _doCallFunc(self, temp_target, target=None): |
paul@196 | 732 | |
paul@196 | 733 | "Make the invocation." |
paul@196 | 734 | |
paul@203 | 735 | # For classes, the target itself is used, since the instantiator will be |
paul@203 | 736 | # obtained via the class. |
paul@203 | 737 | |
paul@215 | 738 | if isinstance(target, (Class, Function)): |
paul@450 | 739 | self.new_op(JumpWithFrameDirect(target, working="status")) |
paul@196 | 740 | else: |
paul@234 | 741 | self.new_op(temp_target) |
paul@215 | 742 | self.new_op(LoadCallable()) |
paul@215 | 743 | self.new_op(JumpWithFrame()) |
paul@196 | 744 | |
paul@196 | 745 | def _endCallFuncArgs(self, nargs): |
paul@196 | 746 | |
paul@196 | 747 | "Set the frame size." |
paul@196 | 748 | |
paul@196 | 749 | self.frame_makers[-1].attr = nargs |
paul@196 | 750 | self.frame_makers.pop() |
paul@196 | 751 | |
paul@464 | 752 | def _endCallFunc(self, temp_target=None, temp_context=None): |
paul@196 | 753 | |
paul@196 | 754 | "Finish the invocation and tidy up afterwards." |
paul@196 | 755 | |
paul@196 | 756 | self.new_op(DropFrame()) |
paul@196 | 757 | |
paul@196 | 758 | # Discard any temporary storage instructions. |
paul@196 | 759 | |
paul@234 | 760 | if temp_target is not None: |
paul@234 | 761 | self.discard_temp(temp_target) |
paul@234 | 762 | |
paul@234 | 763 | if temp_context is not None: |
paul@234 | 764 | self.discard_temp(temp_context) |
paul@196 | 765 | |
paul@330 | 766 | def _visitFunctionDeclaration(self, node): |
paul@330 | 767 | |
paul@330 | 768 | """ |
paul@330 | 769 | Visit the function declaration at 'node', which can be a lambda or a |
paul@330 | 770 | named function. As a consequence an instruction will be generated which |
paul@330 | 771 | provides a reference to the function. |
paul@330 | 772 | """ |
paul@330 | 773 | |
paul@330 | 774 | fn = node.unit |
paul@330 | 775 | ndefaults = len(fn.defaults) |
paul@330 | 776 | temp = self._generateFunctionDefaults(fn) |
paul@330 | 777 | |
paul@330 | 778 | # Populate the new object required for the function. |
paul@330 | 779 | |
paul@330 | 780 | if temp is not None: |
paul@331 | 781 | self.new_op(LoadConst(fn)) |
paul@450 | 782 | self.new_op(LoadCallable(target="source")) |
paul@330 | 783 | self.new_op(temp) |
paul@450 | 784 | self.new_op(StoreCallable(source="source")) |
paul@464 | 785 | self.new_op(temp) |
paul@330 | 786 | #self.discard_temp(temp) |
paul@331 | 787 | else: |
paul@331 | 788 | self.new_op(LoadFunction(fn)) |
paul@330 | 789 | |
paul@330 | 790 | def _visitFunctionDefinition(self, node): |
paul@330 | 791 | |
paul@330 | 792 | """ |
paul@330 | 793 | Visit the function definition at 'node', which can be a lambda or a |
paul@330 | 794 | named function, generating the prelude with argument and default |
paul@330 | 795 | checking, plus the body of the function itself. |
paul@330 | 796 | """ |
paul@330 | 797 | |
paul@330 | 798 | # Check frames using the function's details. |
paul@330 | 799 | |
paul@330 | 800 | fn = node.unit |
paul@330 | 801 | nparams = len(fn.positional_names) |
paul@330 | 802 | ndefaults = len(fn.defaults) |
paul@330 | 803 | |
paul@330 | 804 | fn.body_block = self.new_block() |
paul@330 | 805 | |
paul@330 | 806 | # Check the number of parameters and defaults. |
paul@330 | 807 | |
paul@450 | 808 | self.new_op(CheckFrame((nparams, ndefaults), target="status")) |
paul@331 | 809 | |
paul@354 | 810 | if ndefaults > 0: |
paul@354 | 811 | if fn.is_dynamic(): |
paul@354 | 812 | self.new_op(LoadTemp(0)) # context provides storage |
paul@354 | 813 | else: |
paul@354 | 814 | self.new_op(LoadFunction(fn)) |
paul@331 | 815 | |
paul@330 | 816 | self.new_op(FillDefaults((nparams, ndefaults))) |
paul@330 | 817 | |
paul@330 | 818 | # Produce the body. |
paul@330 | 819 | |
paul@330 | 820 | self.set_block(fn.body_block) |
paul@330 | 821 | |
paul@330 | 822 | # For functions with star parameters, make a special list for the |
paul@330 | 823 | # extra arguments and re-map the parameter. |
paul@330 | 824 | |
paul@330 | 825 | if fn.has_star: |
paul@330 | 826 | self.new_op(CopyExtra(nparams)) |
paul@330 | 827 | |
paul@330 | 828 | # Ensure that the star parameter has a slot in the frame. |
paul@330 | 829 | |
paul@450 | 830 | self.new_op(CheckExtra(nparams, target="status")) |
paul@330 | 831 | self.new_op(StoreTemp(nparams)) |
paul@330 | 832 | |
paul@408 | 833 | # Extend the frame for local usage. |
paul@408 | 834 | |
paul@408 | 835 | extend = ExtendFrame() |
paul@408 | 836 | self.new_op(extend) |
paul@408 | 837 | |
paul@408 | 838 | # Perform tuple assignment for any tuple parameters. |
paul@408 | 839 | |
paul@408 | 840 | self._visitFunctionTupleParameters(fn, node) |
paul@408 | 841 | |
paul@330 | 842 | # Add any attribute usage guards. |
paul@330 | 843 | |
paul@381 | 844 | self._generateGuards(node) |
paul@330 | 845 | |
paul@330 | 846 | # Visit the actual code. |
paul@330 | 847 | |
paul@330 | 848 | self.dispatch(node.code) |
paul@330 | 849 | |
paul@330 | 850 | # Add a return statement where one is not already produced. |
paul@330 | 851 | |
paul@330 | 852 | if not isinstance(self.last_op(), Return): |
paul@330 | 853 | |
paul@330 | 854 | # Return None for normal functions without explicit return |
paul@330 | 855 | # statements. |
paul@330 | 856 | |
paul@351 | 857 | if not fn.is_lambda(): |
paul@330 | 858 | self.dispatch(compiler.ast.Name("None")) |
paul@330 | 859 | |
paul@330 | 860 | self.new_op(Return()) |
paul@330 | 861 | |
paul@330 | 862 | # Make sure that enough frame space is reserved from the start. |
paul@330 | 863 | |
paul@330 | 864 | self.set_frame_usage(node, extend) |
paul@330 | 865 | |
paul@368 | 866 | def _visitFunctionTupleParameters(self, fn, node, parameters=None): |
paul@368 | 867 | |
paul@368 | 868 | """ |
paul@368 | 869 | Visit the tuple parameters for function 'fn', obtaining the appropriate |
paul@368 | 870 | elements from each supplied argument and assigning them to the specified |
paul@368 | 871 | names for each parameter. |
paul@368 | 872 | """ |
paul@368 | 873 | |
paul@368 | 874 | if parameters is not None: |
paul@368 | 875 | self._generateAttr(node, "__getitem__", self.attribute_load_instructions) |
paul@368 | 876 | temp_getitem = self.optimiser.optimise_temp_storage() |
paul@368 | 877 | |
paul@368 | 878 | for i, parameter in parameters or fn.tuple_parameters(): |
paul@368 | 879 | |
paul@368 | 880 | # Either load the parameter from the frame. |
paul@368 | 881 | |
paul@368 | 882 | if parameters is None: |
paul@368 | 883 | self.new_op(LoadName(Attr(i, None, None))) |
paul@368 | 884 | |
paul@368 | 885 | # Or load a value from the current collection. |
paul@368 | 886 | |
paul@368 | 887 | else: |
paul@368 | 888 | self._startCallFunc() |
paul@368 | 889 | self.new_op(temp_getitem) |
paul@368 | 890 | temp_target, target, temp_context = self._generateCallFunc([compiler.ast.Const(i)], node) |
paul@368 | 891 | self._doCallFunc(temp_target, target) |
paul@369 | 892 | self._endCallFunc() |
paul@368 | 893 | |
paul@368 | 894 | # Where a tuple is the target, attempt to descend into the value |
paul@368 | 895 | # obtained. |
paul@368 | 896 | |
paul@368 | 897 | if isinstance(parameter, list): |
paul@368 | 898 | self._visitFunctionTupleParameters(fn, node, parameter) |
paul@368 | 899 | |
paul@368 | 900 | # Store the item in the namespace entry for the given name. |
paul@368 | 901 | |
paul@368 | 902 | else: |
paul@391 | 903 | self.record_value() |
paul@469 | 904 | self.start_target() |
paul@368 | 905 | self.new_op(StoreName(fn[parameter])) |
paul@451 | 906 | self.assign_value() |
paul@391 | 907 | self.discard_value() |
paul@368 | 908 | |
paul@368 | 909 | if parameters is not None: |
paul@368 | 910 | self.discard_temp(temp_getitem) |
paul@368 | 911 | |
paul@196 | 912 | def _generateFunctionDefaults(self, function): |
paul@196 | 913 | |
paul@196 | 914 | """ |
paul@196 | 915 | Generate the default initialisation code for 'function', returning |
paul@196 | 916 | a temporary storage reference if a dynamic object was created for the |
paul@196 | 917 | function. |
paul@196 | 918 | """ |
paul@196 | 919 | |
paul@196 | 920 | attr_to_default = zip(function.default_attrs, function.defaults) |
paul@196 | 921 | if not attr_to_default: |
paul@196 | 922 | return None |
paul@196 | 923 | |
paul@330 | 924 | # Where non-constant defaults are involved, construct a dynamic object |
paul@330 | 925 | # to hold the defaults. |
paul@196 | 926 | |
paul@330 | 927 | dynamic = function.is_dynamic() |
paul@196 | 928 | |
paul@196 | 929 | if dynamic: |
paul@446 | 930 | self.make_instance(self.get_builtin_class("function"), len(attr_to_default)) |
paul@196 | 931 | temp = self.get_temp() |
paul@196 | 932 | |
paul@196 | 933 | for attr, default in attr_to_default: |
paul@196 | 934 | self.dispatch(default) |
paul@196 | 935 | |
paul@196 | 936 | self.record_value() |
paul@469 | 937 | self.start_target() |
paul@196 | 938 | if dynamic: |
paul@196 | 939 | self.new_op(temp) |
paul@196 | 940 | self.new_op(StoreAttr(attr)) |
paul@196 | 941 | else: |
paul@196 | 942 | self.new_op(StoreAddress(attr)) |
paul@451 | 943 | self.assign_value() |
paul@196 | 944 | self.discard_value() |
paul@196 | 945 | |
paul@196 | 946 | if dynamic: |
paul@464 | 947 | return temp |
paul@196 | 948 | else: |
paul@196 | 949 | return None |
paul@196 | 950 | |
paul@563 | 951 | def _storeName(self, node, name=None): |
paul@563 | 952 | |
paul@563 | 953 | """ |
paul@563 | 954 | A convenience method that stores the current working value using the |
paul@563 | 955 | name provided by the given 'node' or the specified 'name' if provided. |
paul@563 | 956 | """ |
paul@563 | 957 | |
paul@563 | 958 | self.record_value() |
paul@563 | 959 | self.start_target() |
paul@563 | 960 | self._generateName(name or node.name, self.name_store_instructions, node) |
paul@563 | 961 | self.assign_value() |
paul@563 | 962 | self.discard_value() |
paul@563 | 963 | |
paul@196 | 964 | def _visitName(self, node, classes): |
paul@196 | 965 | |
paul@196 | 966 | """ |
paul@196 | 967 | Visit the name-related 'node', generating instructions based on the |
paul@196 | 968 | given 'classes'. |
paul@196 | 969 | """ |
paul@196 | 970 | |
paul@196 | 971 | name = node.name |
paul@563 | 972 | self._generateName(name, classes, node) |
paul@334 | 973 | |
paul@563 | 974 | def _generateName(self, name, classes, node): |
paul@196 | 975 | |
paul@196 | 976 | """ |
paul@196 | 977 | Generate code for the access to 'name' in 'scope' using the given |
paul@196 | 978 | 'classes', and using the given 'node' as the source of the access. |
paul@196 | 979 | """ |
paul@196 | 980 | |
paul@223 | 981 | NameInstruction, AddressInstruction, AddressContextInstruction = classes |
paul@196 | 982 | |
paul@563 | 983 | # Get the expected scope of the name. |
paul@563 | 984 | |
paul@563 | 985 | scope = getattr(node, "_scope", None) or self.get_scope(name) |
paul@563 | 986 | |
paul@343 | 987 | # Handle names referring to constants. |
paul@343 | 988 | |
paul@343 | 989 | if scope == "constant": |
paul@343 | 990 | const = self.importer.get_predefined_constant(name) |
paul@343 | 991 | self.new_op(LoadConst(const)) |
paul@343 | 992 | |
paul@343 | 993 | # Handle all other names. |
paul@343 | 994 | |
paul@343 | 995 | elif scope == "local": |
paul@196 | 996 | unit = self.unit |
paul@196 | 997 | if isinstance(unit, Function): |
paul@196 | 998 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@196 | 999 | elif isinstance(unit, Class): |
paul@223 | 1000 | if AddressContextInstruction is not None: |
paul@223 | 1001 | self.new_op(LoadConst(unit)) |
paul@223 | 1002 | self.new_op(AddressContextInstruction(unit.all_class_attributes()[name])) |
paul@223 | 1003 | else: |
paul@223 | 1004 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@196 | 1005 | elif isinstance(unit, Module): |
paul@196 | 1006 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@196 | 1007 | else: |
paul@339 | 1008 | raise TranslateError("Program unit has no local %r." % name) |
paul@196 | 1009 | |
paul@196 | 1010 | elif scope == "global": |
paul@196 | 1011 | globals = self.module.module_attributes() |
paul@196 | 1012 | if globals.has_key(name): |
paul@196 | 1013 | self.new_op(AddressInstruction(globals[name])) |
paul@196 | 1014 | else: |
paul@339 | 1015 | raise TranslateError("Module has no attribute %r." % name) |
paul@196 | 1016 | |
paul@338 | 1017 | elif scope == "builtins": |
paul@446 | 1018 | self.new_op(AddressInstruction(self.get_builtin(name))) |
paul@338 | 1019 | |
paul@196 | 1020 | else: |
paul@343 | 1021 | # NOTE: This may happen because a class attribute is optimised away. |
paul@581 | 1022 | print >>sys.stderr, "Program unit uses unknown name %r." % name |
paul@196 | 1023 | |
paul@196 | 1024 | def _visitUnary(self, node): |
paul@196 | 1025 | |
paul@196 | 1026 | """ |
paul@354 | 1027 | Invoke the appropriate operator module function for the operation |
paul@354 | 1028 | represented by 'node'. |
paul@196 | 1029 | """ |
paul@196 | 1030 | |
paul@354 | 1031 | temp_fn = self._getOperatorFunction(node) |
paul@374 | 1032 | self._visitCall(node, temp_fn, (node.expr,)) |
paul@354 | 1033 | self.discard_temp(temp_fn) |
paul@354 | 1034 | |
paul@259 | 1035 | def _visitBinaryBit(self, node): |
paul@259 | 1036 | |
paul@259 | 1037 | """ |
paul@259 | 1038 | Need to impose binary rules over a sequence of nodes. The |
paul@259 | 1039 | short-circuiting of the similar logical operators is not imposed by the |
paul@259 | 1040 | bitwise operators. |
paul@259 | 1041 | """ |
paul@259 | 1042 | |
paul@354 | 1043 | temp_fn = self._getOperatorFunction(node) |
paul@259 | 1044 | left = None |
paul@259 | 1045 | |
paul@259 | 1046 | for right in node.nodes: |
paul@259 | 1047 | if left is not None: |
paul@374 | 1048 | self._visitCall(node, temp_fn, (left, right)) |
paul@259 | 1049 | left = right |
paul@259 | 1050 | |
paul@354 | 1051 | self.discard_temp(temp_fn) |
paul@354 | 1052 | |
paul@196 | 1053 | def _visitBinary(self, node): |
paul@196 | 1054 | |
paul@196 | 1055 | """ |
paul@354 | 1056 | Invoke the appropriate operator module function for the operation |
paul@354 | 1057 | represented by 'node'. |
paul@196 | 1058 | """ |
paul@196 | 1059 | |
paul@354 | 1060 | temp_fn = self._getOperatorFunction(node) |
paul@374 | 1061 | self._visitCall(node, temp_fn, (node.left, node.right)) |
paul@354 | 1062 | self.discard_temp(temp_fn) |
paul@354 | 1063 | |
paul@374 | 1064 | def _visitCall(self, node, temp_fn, args): |
paul@259 | 1065 | |
paul@354 | 1066 | """ |
paul@354 | 1067 | Invoke the appropriate operator module function for the operation |
paul@354 | 1068 | represented by 'node', given a 'temp_fn' reference to a function, along |
paul@374 | 1069 | with the 'args' (the operand nodes). |
paul@354 | 1070 | """ |
paul@196 | 1071 | |
paul@374 | 1072 | # Evaluate and store the operands in temporary storage. |
paul@196 | 1073 | |
paul@374 | 1074 | temp_list = [] |
paul@196 | 1075 | |
paul@374 | 1076 | for arg in args: |
paul@374 | 1077 | self.dispatch(arg) |
paul@374 | 1078 | temp_list.append(self.optimiser.optimise_temp_storage()) |
paul@196 | 1079 | |
paul@374 | 1080 | self._generateInvocation(temp_fn, temp_list) |
paul@196 | 1081 | |
paul@196 | 1082 | # Compilation duties... |
paul@196 | 1083 | |
paul@374 | 1084 | for temp in temp_list: |
paul@374 | 1085 | self.discard_temp(temp) |
paul@196 | 1086 | |
paul@354 | 1087 | def _generateInvocation(self, temp_fn, temp_list): |
paul@196 | 1088 | |
paul@196 | 1089 | """ |
paul@354 | 1090 | Invoke the function 'temp_fn' using the operands from 'temp_list' as |
paul@354 | 1091 | arguments. |
paul@196 | 1092 | """ |
paul@196 | 1093 | |
paul@196 | 1094 | self._startCallFunc() |
paul@354 | 1095 | |
paul@354 | 1096 | for i, temp in enumerate(temp_list): |
paul@354 | 1097 | self.new_op(temp) |
paul@354 | 1098 | self.new_op(StoreFrame(i)) |
paul@196 | 1099 | |
paul@354 | 1100 | self._endCallFuncArgs(len(temp_list)) |
paul@354 | 1101 | self._doCallFunc(temp_fn) |
paul@354 | 1102 | self._endCallFunc(temp_fn) |
paul@354 | 1103 | |
paul@374 | 1104 | def _getOperatorFunction(self, node, operator_name=None): |
paul@196 | 1105 | |
paul@354 | 1106 | "Return an operator function reference for the given 'node'." |
paul@354 | 1107 | |
paul@374 | 1108 | return self._generateOperatorFunction(operator_name or node.__class__.__name__) |
paul@354 | 1109 | |
paul@354 | 1110 | def _getOperatorAugAssignFunction(self, node): |
paul@196 | 1111 | |
paul@354 | 1112 | """ |
paul@354 | 1113 | Return an operator augmented assignment function reference for the given |
paul@354 | 1114 | 'node'. |
paul@354 | 1115 | """ |
paul@196 | 1116 | |
paul@354 | 1117 | return self._generateOperatorFunction(node.op) |
paul@354 | 1118 | |
paul@354 | 1119 | def _generateOperatorFunction(self, opname): |
paul@354 | 1120 | |
paul@354 | 1121 | "Return an operator function reference for the given 'opname'." |
paul@196 | 1122 | |
paul@354 | 1123 | operator_fn = operator_functions[opname] |
paul@196 | 1124 | |
paul@354 | 1125 | # Get the operator module. |
paul@232 | 1126 | |
paul@354 | 1127 | operator_module = self.importer.get_module("operator") |
paul@232 | 1128 | |
paul@354 | 1129 | # Get the appropriate function from the operator module. |
paul@354 | 1130 | |
paul@354 | 1131 | self.new_op(LoadAddress(operator_module[operator_fn])) |
paul@354 | 1132 | return self.optimiser.optimise_temp_storage() |
paul@196 | 1133 | |
paul@232 | 1134 | def _handleAttributeError(self, node, temp_method, handled_block): |
paul@196 | 1135 | |
paul@196 | 1136 | """ |
paul@196 | 1137 | Add exception handling to the method acquisition instructions where the |
paul@196 | 1138 | attribute access cannot be resolved at compile-time. |
paul@196 | 1139 | """ |
paul@196 | 1140 | |
paul@232 | 1141 | if not (self.optimiser.should_optimise_known_target() and self.optimiser.is_constant_input(temp_method)): |
paul@196 | 1142 | self.load_builtin("AttributeError", node) |
paul@450 | 1143 | self.new_op(CheckException(target="status")) |
paul@450 | 1144 | self.new_op(JumpIfTrue(handled_block, working="status")) |
paul@232 | 1145 | self.new_op(RaiseException()) |
paul@196 | 1146 | |
paul@246 | 1147 | def _generateTuple(self, node): |
paul@246 | 1148 | |
paul@246 | 1149 | "Make a tuple using the given program 'node'." |
paul@246 | 1150 | |
paul@401 | 1151 | # Reserve space for the elements themselves. |
paul@395 | 1152 | |
paul@446 | 1153 | self.make_instance(self.get_builtin_class("tuple"), len(node.nodes)) |
paul@246 | 1154 | temp = self.get_temp() |
paul@395 | 1155 | |
paul@401 | 1156 | # Store using 0-based index values. |
paul@395 | 1157 | |
paul@506 | 1158 | self._populateSequence(temp, node.nodes) |
paul@395 | 1159 | |
paul@464 | 1160 | self.new_op(temp) |
paul@246 | 1161 | self.discard_temp(temp) |
paul@246 | 1162 | |
paul@506 | 1163 | def _generateList(self, node, nodes=None): |
paul@246 | 1164 | |
paul@506 | 1165 | "Make a list using the given program 'node' and optional 'nodes'." |
paul@506 | 1166 | |
paul@506 | 1167 | nodes = nodes or [] |
paul@246 | 1168 | |
paul@246 | 1169 | # Make a fragment containing the list elements. |
paul@196 | 1170 | |
paul@506 | 1171 | self.new_op(MakeFragment(len(nodes) + 1)) |
paul@246 | 1172 | temp = self.get_temp() |
paul@506 | 1173 | self._populateSequence(temp, nodes) |
paul@246 | 1174 | self.new_op(temp) |
paul@246 | 1175 | self.record_value() |
paul@196 | 1176 | |
paul@401 | 1177 | # Reserve space for _elements (the fragment reference). |
paul@395 | 1178 | |
paul@446 | 1179 | self.make_instance(self.get_builtin_class("list"), 1) |
paul@246 | 1180 | list_temp = self.get_temp() |
paul@469 | 1181 | |
paul@469 | 1182 | self.start_target() |
paul@246 | 1183 | self.new_op(list_temp) |
paul@401 | 1184 | self.new_op(StoreAttr(Attr(0, None, None))) # _elements is at position 0 |
paul@451 | 1185 | self.assign_value() |
paul@246 | 1186 | self.discard_value() |
paul@246 | 1187 | |
paul@464 | 1188 | self.new_op(list_temp) |
paul@246 | 1189 | self.discard_temp(temp) |
paul@246 | 1190 | self.discard_temp(list_temp) |
paul@246 | 1191 | |
paul@506 | 1192 | def _populateSequence(self, temp, nodes, offset=0): |
paul@246 | 1193 | |
paul@246 | 1194 | """ |
paul@506 | 1195 | Populate a sequence using the given 'temp' reference and 'nodes'. |
paul@246 | 1196 | """ |
paul@196 | 1197 | |
paul@506 | 1198 | for i, n in enumerate(nodes): |
paul@196 | 1199 | self.dispatch(n) |
paul@196 | 1200 | self.record_value() |
paul@438 | 1201 | self._storeInSequence(temp, i, offset) |
paul@196 | 1202 | self.discard_value() |
paul@196 | 1203 | |
paul@438 | 1204 | def _storeInSequence(self, temp, i, offset=0): |
paul@438 | 1205 | |
paul@438 | 1206 | """ |
paul@438 | 1207 | Store the current active value in the fragment referenced by 'temp' at |
paul@438 | 1208 | position 'i' with the given starting 'offset'. |
paul@438 | 1209 | """ |
paul@438 | 1210 | |
paul@469 | 1211 | self.start_target() |
paul@438 | 1212 | self.new_op(temp) |
paul@438 | 1213 | self.new_op(StoreAttr(Attr(i + offset, None, None))) |
paul@451 | 1214 | self.assign_value() |
paul@438 | 1215 | |
paul@196 | 1216 | def _generateTestBoolean(self, node, temp): |
paul@196 | 1217 | |
paul@196 | 1218 | """ |
paul@196 | 1219 | Generate a test of the boolean status of the current value for the given |
paul@196 | 1220 | program 'node'. |
paul@196 | 1221 | """ |
paul@196 | 1222 | |
paul@196 | 1223 | # Get method on temp. |
paul@196 | 1224 | # NOTE: Using __bool__ instead of __nonzero__. |
paul@196 | 1225 | |
paul@196 | 1226 | self._generateAttr(node, "__bool__", self.attribute_load_instructions) |
paul@196 | 1227 | temp_method = self.optimiser.optimise_temp_storage() |
paul@196 | 1228 | |
paul@354 | 1229 | self._generateInvocation(temp_method, (temp,)) |
paul@196 | 1230 | |
paul@196 | 1231 | self.discard_temp(temp_method) |
paul@196 | 1232 | |
paul@196 | 1233 | # Convert result to boolean (a StoreBoolean operation). |
paul@196 | 1234 | |
paul@450 | 1235 | self.new_op(TestIdentityAddress(self.importer.get_predefined_constant("True"), target="status")) |
paul@196 | 1236 | |
paul@196 | 1237 | def _generateLoadBoolean(self, node): |
paul@196 | 1238 | |
paul@196 | 1239 | """ |
paul@196 | 1240 | Generate instructions to load the appropriate value given the current |
paul@196 | 1241 | boolean status. |
paul@196 | 1242 | """ |
paul@196 | 1243 | |
paul@452 | 1244 | false_block = self.get_block() |
paul@196 | 1245 | true_block = self.new_block() |
paul@196 | 1246 | end_block = self.new_block() |
paul@196 | 1247 | |
paul@450 | 1248 | self.new_op(JumpIfTrue(true_block, working="status")) |
paul@197 | 1249 | self.new_op(LoadConst(self.importer.get_predefined_constant("False"))) |
paul@196 | 1250 | self.new_op(Jump(end_block)) |
paul@196 | 1251 | |
paul@196 | 1252 | self.set_block(true_block) |
paul@197 | 1253 | self.new_op(LoadConst(self.importer.get_predefined_constant("True"))) |
paul@196 | 1254 | |
paul@452 | 1255 | self.set_block(end_block, [false_block, true_block]) |
paul@196 | 1256 | |
paul@506 | 1257 | # Common AST operations. |
paul@506 | 1258 | |
paul@506 | 1259 | def _startFor(self, node, else_=None): |
paul@506 | 1260 | |
paul@506 | 1261 | """ |
paul@506 | 1262 | Generate the start of a loop using the given 'node' and 'else_' clause, |
paul@506 | 1263 | if defined. The iterator and next, exit and else blocks are returned. |
paul@506 | 1264 | """ |
paul@506 | 1265 | |
paul@506 | 1266 | next_handler_block = self.new_block() |
paul@506 | 1267 | end_handler_block = self.new_block() |
paul@506 | 1268 | exit_block = self.new_block() |
paul@506 | 1269 | next_block = self.new_block() |
paul@506 | 1270 | else_block = self.new_block() |
paul@506 | 1271 | |
paul@506 | 1272 | temp_iterator = self._visitForList(node) |
paul@506 | 1273 | |
paul@506 | 1274 | # In the loop... |
paul@506 | 1275 | |
paul@506 | 1276 | self.set_block(next_block) |
paul@506 | 1277 | |
paul@506 | 1278 | # Handle exceptions when calling "next"... |
paul@506 | 1279 | |
paul@506 | 1280 | self.add_exception_blocks(next_handler_block, end_handler_block) |
paul@506 | 1281 | self.new_op(PushHandler(next_handler_block)) |
paul@506 | 1282 | |
paul@506 | 1283 | # Invoke the next method. |
paul@506 | 1284 | |
paul@506 | 1285 | self._visitForNext(node, temp_iterator) |
paul@506 | 1286 | |
paul@506 | 1287 | # Record the value to be assigned. |
paul@506 | 1288 | |
paul@506 | 1289 | self.record_value() |
paul@506 | 1290 | |
paul@506 | 1291 | # Skip the handler where the call was successful. |
paul@506 | 1292 | |
paul@506 | 1293 | self.new_op(PopHandler(1)) |
paul@506 | 1294 | self.new_op(Jump(end_handler_block)) |
paul@506 | 1295 | |
paul@506 | 1296 | # Enter the exception handler. |
paul@506 | 1297 | |
paul@506 | 1298 | self.set_block(next_handler_block) |
paul@506 | 1299 | self.new_op(PopHandler(1)) |
paul@506 | 1300 | |
paul@506 | 1301 | # Disable the handlers. |
paul@506 | 1302 | |
paul@506 | 1303 | self.drop_exception_blocks() |
paul@506 | 1304 | |
paul@506 | 1305 | # Test for StopIteration. |
paul@506 | 1306 | |
paul@506 | 1307 | self.load_builtin("StopIteration", node) |
paul@506 | 1308 | self.new_op(CheckException(target="status")) |
paul@506 | 1309 | if else_ is not None: |
paul@506 | 1310 | self.new_op(JumpIfTrue(else_block, working="status")) |
paul@506 | 1311 | else: |
paul@506 | 1312 | self.new_op(JumpIfTrue(exit_block, working="status")) |
paul@506 | 1313 | |
paul@506 | 1314 | # Re-raise the exception otherwise. |
paul@506 | 1315 | |
paul@506 | 1316 | self.new_op(RaiseException()) |
paul@506 | 1317 | |
paul@506 | 1318 | # After the handler, clear the exception. |
paul@506 | 1319 | |
paul@506 | 1320 | self.set_block(end_handler_block) |
paul@506 | 1321 | |
paul@506 | 1322 | # Assign to the target. |
paul@506 | 1323 | |
paul@506 | 1324 | self.dispatch(node.assign) |
paul@506 | 1325 | self.discard_value() |
paul@506 | 1326 | |
paul@506 | 1327 | # Process the body with the current next and exit points. |
paul@506 | 1328 | |
paul@506 | 1329 | self.add_loop_blocks(next_block, exit_block) |
paul@506 | 1330 | |
paul@506 | 1331 | return temp_iterator, next_block, exit_block, else_block |
paul@506 | 1332 | |
paul@506 | 1333 | def _endFor(self, node, temp_iterator, next_block, exit_block, else_block=None, else_=None): |
paul@506 | 1334 | |
paul@506 | 1335 | """ |
paul@506 | 1336 | Generate the end of a loop for the given 'node' using the given |
paul@506 | 1337 | 'temp_iterator' and 'next_block', 'exit_block' and 'else_block' |
paul@506 | 1338 | definitions, together with an 'else_' clause, if defined. |
paul@506 | 1339 | """ |
paul@506 | 1340 | |
paul@506 | 1341 | self.drop_loop_blocks() |
paul@506 | 1342 | |
paul@506 | 1343 | # Repeat the loop. |
paul@506 | 1344 | |
paul@506 | 1345 | self.new_op(Jump(next_block)) |
paul@506 | 1346 | |
paul@506 | 1347 | # Produce the "else" section. |
paul@506 | 1348 | |
paul@506 | 1349 | if else_ is not None: |
paul@506 | 1350 | self.set_block(else_block) |
paul@506 | 1351 | self.new_op(ClearException(target="exception")) |
paul@506 | 1352 | self.dispatch(else_) |
paul@506 | 1353 | |
paul@506 | 1354 | # After the loop... |
paul@506 | 1355 | |
paul@506 | 1356 | self.set_block(exit_block) |
paul@506 | 1357 | |
paul@506 | 1358 | else: |
paul@506 | 1359 | # After the loop... |
paul@506 | 1360 | |
paul@506 | 1361 | self.set_block(exit_block) |
paul@506 | 1362 | self.new_op(ClearException(target="exception")) |
paul@506 | 1363 | |
paul@506 | 1364 | # Compilation duties... |
paul@506 | 1365 | |
paul@506 | 1366 | self.discard_temp(temp_iterator) |
paul@506 | 1367 | |
paul@506 | 1368 | def _visitForList(self, node): |
paul@506 | 1369 | |
paul@506 | 1370 | "Get the list to be iterated over, returning its iterator." |
paul@506 | 1371 | |
paul@506 | 1372 | self._startCallFunc() |
paul@506 | 1373 | self.dispatch(node.list) |
paul@506 | 1374 | self._generateAttr(node, "__iter__", self.attribute_load_instructions) |
paul@506 | 1375 | temp_target, target, temp_context = self._generateCallFunc([], node) |
paul@506 | 1376 | self._doCallFunc(temp_target, target) |
paul@506 | 1377 | self._endCallFunc(temp_target, temp_context) |
paul@506 | 1378 | |
paul@506 | 1379 | # Use a long-lasting temporary storage slot, since any result from the |
paul@506 | 1380 | # __iter__ method will not remain around for long. |
paul@506 | 1381 | |
paul@506 | 1382 | return self.get_temp() |
paul@506 | 1383 | |
paul@506 | 1384 | def _visitForNext(self, node, temp_iterator): |
paul@506 | 1385 | |
paul@506 | 1386 | "Use the iterator to get the next value." |
paul@506 | 1387 | |
paul@506 | 1388 | self._startCallFunc() |
paul@506 | 1389 | self.new_op(temp_iterator) |
paul@506 | 1390 | self._generateAttr(node, "next", self.attribute_load_instructions) |
paul@506 | 1391 | temp_target, target, temp_context = self._generateCallFunc([], node) |
paul@506 | 1392 | self._doCallFunc(temp_target, target) |
paul@506 | 1393 | self._endCallFunc(temp_target, temp_context) |
paul@506 | 1394 | |
paul@393 | 1395 | def _visitPrint(self, node, function_name): |
paul@393 | 1396 | self._startCallFunc() |
paul@393 | 1397 | self.load_builtin(function_name, node) |
paul@393 | 1398 | |
paul@393 | 1399 | args = [node.dest or compiler.ast.Name("None")] + node.nodes |
paul@393 | 1400 | |
paul@393 | 1401 | temp_target, target, temp_context = self._generateCallFunc(args, node) |
paul@393 | 1402 | self._doCallFunc(temp_target, target) |
paul@393 | 1403 | self._endCallFunc(temp_target, temp_context) |
paul@393 | 1404 | |
paul@563 | 1405 | def _visitImport(self, node): |
paul@563 | 1406 | |
paul@563 | 1407 | """ |
paul@563 | 1408 | Although imported code already resides in any generated image, the |
paul@563 | 1409 | module code must be executed. Module code uses its own frame for any |
paul@563 | 1410 | temporary storage and thus behaves like a function. |
paul@563 | 1411 | """ |
paul@563 | 1412 | |
paul@563 | 1413 | modules = self.importer.importers.get(node) |
paul@563 | 1414 | if modules: |
paul@563 | 1415 | for module in modules: |
paul@563 | 1416 | self.new_op(MakeFrame(0)) |
paul@563 | 1417 | self.new_op(LoadConst(module)) |
paul@563 | 1418 | self.new_op(LoadCallable()) |
paul@563 | 1419 | self.new_op(JumpWithFrame()) |
paul@563 | 1420 | self.new_op(DropFrame()) |
paul@563 | 1421 | |
paul@563 | 1422 | def _importName(self, module, name, alias, node): |
paul@563 | 1423 | |
paul@563 | 1424 | # Get the source of the name. |
paul@563 | 1425 | |
paul@563 | 1426 | attr = module[name] |
paul@563 | 1427 | self.new_op(LoadAddress(attr)) |
paul@563 | 1428 | |
paul@563 | 1429 | # Record the object in the current namespace. |
paul@563 | 1430 | |
paul@563 | 1431 | self.record_value() |
paul@563 | 1432 | |
paul@563 | 1433 | self.start_target() |
paul@563 | 1434 | self._generateName(alias or name, self.name_store_instructions, node) # AssName equivalent |
paul@563 | 1435 | self.assign_value() |
paul@563 | 1436 | self.discard_value() |
paul@563 | 1437 | |
paul@196 | 1438 | # vim: tabstop=4 expandtab shiftwidth=4 |