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