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