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