paul@17 | 1 | #!/usr/bin/env python |
paul@17 | 2 | |
paul@17 | 3 | """ |
paul@17 | 4 | Translate the AST of a Python program into a more interpretable representation. |
paul@17 | 5 | |
paul@39 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@17 | 7 | |
paul@17 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@17 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@17 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@17 | 11 | version. |
paul@17 | 12 | |
paul@17 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@17 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@17 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@17 | 16 | details. |
paul@17 | 17 | |
paul@17 | 18 | You should have received a copy of the GNU General Public License along with |
paul@17 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@17 | 20 | """ |
paul@17 | 21 | |
paul@83 | 22 | from micropython.common import * |
paul@83 | 23 | from micropython.data import * |
paul@17 | 24 | from micropython.rsvp import * |
paul@17 | 25 | import compiler.ast |
paul@17 | 26 | from compiler.visitor import ASTVisitor |
paul@17 | 27 | |
paul@17 | 28 | # Program visitors. |
paul@17 | 29 | |
paul@17 | 30 | class Translation(ASTVisitor): |
paul@17 | 31 | |
paul@17 | 32 | "A translated module." |
paul@17 | 33 | |
paul@76 | 34 | supported_optimisations = ["constant_storage", "known_target", "self_access", "temp_storage", "constant_test"] |
paul@62 | 35 | |
paul@72 | 36 | def __init__(self, module, importer, optimisations=None): |
paul@18 | 37 | |
paul@21 | 38 | """ |
paul@72 | 39 | Initialise the translation with an inspected 'module', the 'importer' |
paul@72 | 40 | and optional 'optimisations'. See the 'supported_optimisations' |
paul@72 | 41 | attribute of this class for permitted values. |
paul@21 | 42 | """ |
paul@18 | 43 | |
paul@17 | 44 | ASTVisitor.__init__(self) |
paul@17 | 45 | self.visitor = self |
paul@17 | 46 | self.module = module |
paul@72 | 47 | |
paul@72 | 48 | # Global program dependencies. |
paul@72 | 49 | |
paul@72 | 50 | self.importer = importer |
paul@72 | 51 | self.objtable = self.importer.get_object_table() |
paul@72 | 52 | self.paramtable = self.importer.get_parameter_table() |
paul@72 | 53 | self.builtins = self.importer.modules.get("__builtins__") |
paul@61 | 54 | |
paul@62 | 55 | # Desired optimisations. |
paul@62 | 56 | |
paul@62 | 57 | self.optimisations = set(optimisations or []) |
paul@62 | 58 | |
paul@61 | 59 | # The current unit being translated. |
paul@61 | 60 | |
paul@17 | 61 | self.unit = None |
paul@17 | 62 | |
paul@19 | 63 | # Wiring within the code. |
paul@19 | 64 | |
paul@19 | 65 | self.labels = {} |
paul@19 | 66 | self.label_number = 0 |
paul@41 | 67 | self.loop_labels = [] |
paul@57 | 68 | self.exception_labels = [] |
paul@41 | 69 | |
paul@72 | 70 | # The code itself. This is limited to the code for a particular block |
paul@72 | 71 | # being processed. |
paul@41 | 72 | |
paul@19 | 73 | self.code = None |
paul@73 | 74 | self.temp_position = 0 |
paul@19 | 75 | |
paul@72 | 76 | def calculate_stack_usage(self): |
paul@72 | 77 | max_stack_usage = 0 |
paul@72 | 78 | stack_usage = 0 |
paul@72 | 79 | |
paul@72 | 80 | for op in self.code: |
paul@72 | 81 | stack_usage += op.stack_usage |
paul@72 | 82 | max_stack_usage = max(max_stack_usage, stack_usage) |
paul@72 | 83 | |
paul@72 | 84 | self.unit.stack_usage = max_stack_usage |
paul@72 | 85 | |
paul@17 | 86 | def get_module_code(self): |
paul@17 | 87 | |
paul@17 | 88 | "Return the top-level module code." |
paul@17 | 89 | |
paul@17 | 90 | self.unit = self.module |
paul@19 | 91 | self.code = [] |
paul@73 | 92 | self.temp_position = self.unit.stack_local_usage |
paul@73 | 93 | |
paul@44 | 94 | if self.module.module is not None: |
paul@44 | 95 | self.dispatch(self.module.module) |
paul@73 | 96 | |
paul@72 | 97 | self.calculate_stack_usage() |
paul@19 | 98 | return self.code |
paul@17 | 99 | |
paul@17 | 100 | def get_code(self, unit): |
paul@17 | 101 | |
paul@17 | 102 | "Return the code for the given 'unit'." |
paul@17 | 103 | |
paul@17 | 104 | self.unit = unit |
paul@19 | 105 | self.code = [] |
paul@73 | 106 | self.temp_position = self.unit.stack_local_usage |
paul@73 | 107 | |
paul@25 | 108 | if unit.node is not None: |
paul@25 | 109 | self.dispatch(unit.node) |
paul@73 | 110 | |
paul@72 | 111 | self.calculate_stack_usage() |
paul@19 | 112 | return self.code |
paul@17 | 113 | |
paul@17 | 114 | def __repr__(self): |
paul@17 | 115 | return "Translation(%r)" % self.module |
paul@17 | 116 | |
paul@17 | 117 | def get_scope(self, name): |
paul@17 | 118 | if self.unit.has_key(name): |
paul@17 | 119 | return "local" |
paul@17 | 120 | elif self.module.has_key(name): |
paul@17 | 121 | return "global" |
paul@17 | 122 | else: |
paul@17 | 123 | return "builtins" |
paul@17 | 124 | |
paul@19 | 125 | # Code writing methods. |
paul@19 | 126 | |
paul@19 | 127 | def new_label(self): |
paul@44 | 128 | |
paul@44 | 129 | "Return a new label object for use with set_label." |
paul@44 | 130 | |
paul@19 | 131 | number = self.label_number |
paul@19 | 132 | label = Label(number) |
paul@19 | 133 | self.labels[label] = label |
paul@19 | 134 | self.label_number += 1 |
paul@19 | 135 | return label |
paul@19 | 136 | |
paul@19 | 137 | def set_label(self, label): |
paul@19 | 138 | |
paul@19 | 139 | """ |
paul@19 | 140 | Set the location of 'label' to that within the entire image: the |
paul@19 | 141 | location within the code combined with location of the code unit. |
paul@19 | 142 | """ |
paul@19 | 143 | |
paul@19 | 144 | label.location = len(self.code) + self.unit.code_location |
paul@19 | 145 | |
paul@57 | 146 | def get_loop_labels(self): |
paul@57 | 147 | return self.loop_labels[-1] |
paul@57 | 148 | |
paul@57 | 149 | def add_loop_labels(self, next_label, exit_label): |
paul@57 | 150 | self.loop_labels.append((next_label, exit_label)) |
paul@57 | 151 | |
paul@57 | 152 | def drop_loop_labels(self): |
paul@57 | 153 | self.loop_labels.pop() |
paul@57 | 154 | |
paul@57 | 155 | def get_exception_labels(self): |
paul@57 | 156 | return self.exception_labels[-1] |
paul@57 | 157 | |
paul@57 | 158 | def add_exception_labels(self, handler_label, exit_label): |
paul@57 | 159 | self.exception_labels.append((handler_label, exit_label)) |
paul@57 | 160 | |
paul@57 | 161 | def drop_exception_labels(self): |
paul@57 | 162 | self.exception_labels.pop() |
paul@57 | 163 | |
paul@73 | 164 | def reserve_temp(self, n): |
paul@73 | 165 | temp_position = self.temp_position |
paul@73 | 166 | self.temp_position += n |
paul@73 | 167 | self.unit.stack_temp_usage = max(self.unit.stack_temp_usage, self.temp_position) |
paul@73 | 168 | return temp_position |
paul@73 | 169 | |
paul@78 | 170 | def discard_temp(self, instructions): |
paul@78 | 171 | for temp in instructions: |
paul@78 | 172 | if isinstance(temp, LoadTemp): |
paul@78 | 173 | self.temp_position -= 1 |
paul@73 | 174 | |
paul@19 | 175 | def new_op(self, op): |
paul@44 | 176 | |
paul@44 | 177 | "Add 'op' to the generated code." |
paul@44 | 178 | |
paul@19 | 179 | self.code.append(op) |
paul@19 | 180 | |
paul@79 | 181 | def new_ops(self, ops): |
paul@79 | 182 | |
paul@79 | 183 | "Add 'ops' to the generated code." |
paul@79 | 184 | |
paul@79 | 185 | self.code += ops |
paul@79 | 186 | |
paul@43 | 187 | def replace_op(self, op): |
paul@44 | 188 | |
paul@44 | 189 | "Replace the last added instruction with 'op'." |
paul@44 | 190 | |
paul@43 | 191 | self.code[-1] = op |
paul@43 | 192 | |
paul@56 | 193 | def remove_ops(self, n): |
paul@56 | 194 | |
paul@56 | 195 | "Remove the last 'n' instructions." |
paul@56 | 196 | |
paul@56 | 197 | del self.code[-n:] |
paul@56 | 198 | |
paul@81 | 199 | def last_ops(self, n): |
paul@56 | 200 | |
paul@56 | 201 | "Return the last 'n' added instructions in reverse chronological order." |
paul@56 | 202 | |
paul@81 | 203 | ops = self.code[-n:] |
paul@56 | 204 | ops.reverse() |
paul@56 | 205 | return ops |
paul@56 | 206 | |
paul@81 | 207 | def last_op(self): |
paul@44 | 208 | |
paul@44 | 209 | "Return the last added instruction." |
paul@44 | 210 | |
paul@70 | 211 | try: |
paul@81 | 212 | return self.code[-1] |
paul@70 | 213 | except IndexError: |
paul@70 | 214 | return None |
paul@26 | 215 | |
paul@56 | 216 | # Internal helper methods. |
paul@17 | 217 | |
paul@43 | 218 | def _visitAttr(self, node, classes): |
paul@44 | 219 | |
paul@44 | 220 | """ |
paul@44 | 221 | Visit the attribute-related 'node', generating instructions based on the |
paul@44 | 222 | given 'classes'. |
paul@44 | 223 | """ |
paul@44 | 224 | |
paul@43 | 225 | self.dispatch(node.expr) |
paul@63 | 226 | self._generateAttr(node, node.attrname, classes) |
paul@43 | 227 | |
paul@63 | 228 | def _generateAttr(self, node, attrname, classes): |
paul@44 | 229 | |
paul@44 | 230 | """ |
paul@44 | 231 | Generate code for the access to 'attrname' using the given 'classes'. |
paul@44 | 232 | """ |
paul@44 | 233 | |
paul@72 | 234 | AddressInstruction, AttrInstruction, AttrIndexInstruction = classes |
paul@43 | 235 | |
paul@43 | 236 | last = self.last_op() |
paul@54 | 237 | |
paul@56 | 238 | # Where the last operation (defining the attribute owner) yields a |
paul@56 | 239 | # constant... |
paul@54 | 240 | |
paul@62 | 241 | if self._have_constant_input(0): |
paul@62 | 242 | |
paul@62 | 243 | # Optimise away the constant storage if appropriate. |
paul@87 | 244 | # The target and value loading operations are removed. |
paul@43 | 245 | |
paul@72 | 246 | if self._optimise_constant_storage(AddressInstruction, 1): |
paul@56 | 247 | return |
paul@56 | 248 | |
paul@43 | 249 | # Get the details of the access. |
paul@43 | 250 | |
paul@83 | 251 | if isinstance(last.attr, Const): |
paul@82 | 252 | target_name = last.attr.value_type_name() |
paul@82 | 253 | else: |
paul@82 | 254 | target = last.attr.value |
paul@75 | 255 | |
paul@83 | 256 | if isinstance(target, Const): |
paul@82 | 257 | target_name = target.value_type_name() |
paul@86 | 258 | elif isinstance(target, Instance): |
paul@86 | 259 | target_name = None # skip production of optimised code |
paul@82 | 260 | else: |
paul@82 | 261 | target_name = target.full_name() |
paul@75 | 262 | |
paul@86 | 263 | # Only try and discover the position if the target can be resolved. |
paul@63 | 264 | |
paul@86 | 265 | if target_name is not None: |
paul@86 | 266 | |
paul@86 | 267 | # Access the object table to get the attribute position. |
paul@63 | 268 | |
paul@86 | 269 | try: |
paul@86 | 270 | table_entry = self.objtable.table[target_name] |
paul@86 | 271 | except KeyError: |
paul@86 | 272 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 273 | "No object entry exists for target %r." % target_name) |
paul@63 | 274 | |
paul@86 | 275 | try: |
paul@86 | 276 | pos = table_entry[attrname] |
paul@86 | 277 | except KeyError: |
paul@86 | 278 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 279 | "No attribute entry exists for name %r in target %r." % (attrname, target_name)) |
paul@74 | 280 | |
paul@86 | 281 | # Produce a suitable instruction. |
paul@86 | 282 | |
paul@86 | 283 | self.replace_op(AddressInstruction(pos)) |
paul@86 | 284 | return |
paul@54 | 285 | |
paul@54 | 286 | # Where the last operation involves the special 'self' name, check to |
paul@65 | 287 | # see if the attribute is acceptably positioned and produce a direct |
paul@65 | 288 | # access to the attribute. |
paul@54 | 289 | |
paul@86 | 290 | elif self._optimise_self_access(attrname, (AddressInstruction, AttrInstruction)): |
paul@86 | 291 | return |
paul@54 | 292 | |
paul@54 | 293 | # Otherwise, perform a normal operation. |
paul@43 | 294 | |
paul@86 | 295 | try: |
paul@86 | 296 | index = self.objtable.get_index(attrname) |
paul@86 | 297 | except self.objtable.TableError: |
paul@86 | 298 | raise TranslateError(self.module.full_name(), node, |
paul@86 | 299 | "No attribute entry exists for name %r." % attrname) |
paul@63 | 300 | |
paul@86 | 301 | self.new_op(AttrIndexInstruction(index)) |
paul@43 | 302 | |
paul@45 | 303 | def _startCallFunc(self): |
paul@45 | 304 | |
paul@45 | 305 | "Record the location of the invocation." |
paul@21 | 306 | |
paul@21 | 307 | self.new_op(MakeFrame()) # records the start of the frame |
paul@21 | 308 | |
paul@44 | 309 | def _generateCallFunc(self, args, node): |
paul@44 | 310 | |
paul@86 | 311 | """ |
paul@86 | 312 | Support a generic function invocation using the given 'args', occurring |
paul@86 | 313 | on the given 'node', where the expression providing the invocation |
paul@86 | 314 | target has just been generated. |
paul@86 | 315 | |
paul@86 | 316 | In other situations, the invocation is much simpler and does not need to |
paul@86 | 317 | handle the full flexibility of a typical Python invocation. Internal |
paul@86 | 318 | invocations, such as those employed by operators and certain |
paul@86 | 319 | control-flow mechanisms, use predetermined arguments and arguably do not |
paul@86 | 320 | need to support the same things as the more general invocations. |
paul@86 | 321 | """ |
paul@86 | 322 | |
paul@86 | 323 | target, context, temp = self._generateCallFuncContext() |
paul@86 | 324 | self._generateCallFuncArgs(target, context, args, node) |
paul@86 | 325 | return temp |
paul@86 | 326 | |
paul@86 | 327 | def _generateCallFuncContext(self): |
paul@86 | 328 | |
paul@86 | 329 | """ |
paul@86 | 330 | Produce code which loads and checks the context of the current |
paul@86 | 331 | invocation, the instructions for whose target have already been |
paul@86 | 332 | produced, returning a list of instructions which reference the |
paul@86 | 333 | invocation target. |
paul@86 | 334 | """ |
paul@26 | 335 | |
paul@77 | 336 | t = self._optimise_known_target() |
paul@77 | 337 | if t: |
paul@77 | 338 | target, context = t |
paul@77 | 339 | else: |
paul@77 | 340 | target, context = None, None |
paul@45 | 341 | |
paul@78 | 342 | # Store the target in temporary storage for subsequent referencing. |
paul@78 | 343 | |
paul@78 | 344 | temp = self._optimise_temp_storage() |
paul@78 | 345 | |
paul@87 | 346 | # Where a target or context are not known, load the target and context. |
paul@45 | 347 | |
paul@59 | 348 | if context is None: |
paul@45 | 349 | continue_label = self.new_label() |
paul@87 | 350 | self.new_ops(temp) |
paul@45 | 351 | self.new_op(LoadContext()) |
paul@45 | 352 | self.new_op(CheckContext()) |
paul@45 | 353 | self.new_op(JumpIfTrue(continue_label)) |
paul@86 | 354 | |
paul@86 | 355 | # Where the context is inappropriate, drop the incomplete frame and |
paul@86 | 356 | # raise an exception. |
paul@86 | 357 | |
paul@86 | 358 | self.new_op(DropFrame()) |
paul@57 | 359 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@45 | 360 | self.new_op(RaiseException()) |
paul@45 | 361 | self.set_label(continue_label) |
paul@87 | 362 | |
paul@87 | 363 | # Where an instance is known to be the context, load the context. |
paul@87 | 364 | |
paul@87 | 365 | elif isinstance(context, Instance): |
paul@87 | 366 | self.new_ops(temp) |
paul@87 | 367 | self.new_op(LoadContext()) |
paul@87 | 368 | |
paul@87 | 369 | # Otherwise omit the context. |
paul@87 | 370 | |
paul@59 | 371 | else: |
paul@59 | 372 | pass # NOTE: Class methods should be supported. |
paul@26 | 373 | |
paul@86 | 374 | return target, context, temp |
paul@86 | 375 | |
paul@86 | 376 | def _generateCallFuncArgs(self, target, context, args, node): |
paul@86 | 377 | |
paul@86 | 378 | """ |
paul@86 | 379 | Given invocation 'target' and 'context' information, a list of nodes |
paul@86 | 380 | representing the 'args' (arguments), generate instructions which load |
paul@86 | 381 | the arguments for the invocation defined by the given 'node'. |
paul@86 | 382 | """ |
paul@86 | 383 | |
paul@21 | 384 | # Evaluate the arguments. |
paul@21 | 385 | |
paul@67 | 386 | employed_positions = set() |
paul@59 | 387 | extra_keywords = [] |
paul@22 | 388 | |
paul@86 | 389 | # NOTE: Fix context for self-accessed methods. |
paul@86 | 390 | |
paul@90 | 391 | if context is not None and isinstance(context, Instance): |
paul@86 | 392 | ncontext = 1 |
paul@86 | 393 | else: |
paul@86 | 394 | ncontext = 0 |
paul@86 | 395 | |
paul@66 | 396 | for frame_pos, arg in enumerate(args): |
paul@66 | 397 | |
paul@66 | 398 | # Handle positional and keyword arguments separately. |
paul@66 | 399 | |
paul@21 | 400 | if isinstance(arg, compiler.ast.Keyword): |
paul@66 | 401 | |
paul@26 | 402 | # Optimise where the target is known now. |
paul@26 | 403 | |
paul@26 | 404 | if target is not None: |
paul@39 | 405 | |
paul@39 | 406 | # Find the parameter table entry for the target. |
paul@39 | 407 | |
paul@39 | 408 | target_name = target.full_name() |
paul@39 | 409 | |
paul@39 | 410 | # Look for a callable with the precise target name. |
paul@39 | 411 | |
paul@59 | 412 | table_entry = self.paramtable.table[target_name] |
paul@59 | 413 | |
paul@59 | 414 | # Look the name up in the parameter table entry. |
paul@39 | 415 | |
paul@59 | 416 | try: |
paul@59 | 417 | pos = table_entry[arg.name] |
paul@59 | 418 | |
paul@59 | 419 | # Where no position is found, this could be an extra keyword |
paul@59 | 420 | # argument. |
paul@39 | 421 | |
paul@39 | 422 | except KeyError: |
paul@59 | 423 | extra_keywords.append(arg) |
paul@59 | 424 | continue |
paul@58 | 425 | |
paul@58 | 426 | # Test for illegal conditions. |
paul@58 | 427 | |
paul@67 | 428 | if pos in employed_positions: |
paul@58 | 429 | raise TranslateError(self.module.full_name(), node, |
paul@58 | 430 | "Keyword argument %r overwrites parameter %r." % (arg.name, pos)) |
paul@67 | 431 | |
paul@67 | 432 | employed_positions.add(pos) |
paul@58 | 433 | |
paul@67 | 434 | # Add space for arguments appearing before this one. |
paul@67 | 435 | |
paul@86 | 436 | if frame_pos + ncontext < pos: |
paul@86 | 437 | self.new_op(ReserveFrame(pos - frame_pos - ncontext)) |
paul@59 | 438 | |
paul@59 | 439 | # Generate code for the keyword and the positioning |
paul@59 | 440 | # operation. |
paul@59 | 441 | |
paul@59 | 442 | self.dispatch(arg.expr) |
paul@66 | 443 | |
paul@66 | 444 | # If the position corresponds to the current frame element, |
paul@67 | 445 | # skip generating the store instruction. |
paul@66 | 446 | |
paul@86 | 447 | if frame_pos + ncontext > pos: |
paul@66 | 448 | self.new_op(StoreFrame(pos)) |
paul@26 | 449 | |
paul@26 | 450 | # Otherwise, generate the code needed to obtain the details of |
paul@26 | 451 | # the parameter location. |
paul@26 | 452 | |
paul@26 | 453 | else: |
paul@21 | 454 | |
paul@26 | 455 | # Combine the target details with the name to get the location. |
paul@26 | 456 | # See the access method on the List class. |
paul@26 | 457 | |
paul@39 | 458 | try: |
paul@39 | 459 | paramindex = self.paramtable.get_index(arg.name) |
paul@59 | 460 | |
paul@59 | 461 | # Where no position is found, this could be an extra keyword |
paul@59 | 462 | # argument. |
paul@59 | 463 | |
paul@70 | 464 | except self.paramtable.TableError: |
paul@59 | 465 | extra_keywords.append(arg) |
paul@59 | 466 | continue |
paul@21 | 467 | |
paul@59 | 468 | # Generate code for the keyword and the positioning |
paul@59 | 469 | # operation. |
paul@59 | 470 | |
paul@59 | 471 | self.dispatch(arg.expr) |
paul@26 | 472 | self.new_op(StoreFrameIndex(paramindex)) |
paul@26 | 473 | |
paul@26 | 474 | # use (callable+0)+paramindex+table |
paul@26 | 475 | # checks embedded offset against (callable+0) |
paul@26 | 476 | # moves the top of stack to frame+position |
paul@21 | 477 | |
paul@21 | 478 | else: |
paul@21 | 479 | self.dispatch(arg) |
paul@86 | 480 | employed_positions.add(frame_pos + ncontext) |
paul@21 | 481 | |
paul@86 | 482 | frame_pos = len(args) + ncontext |
paul@66 | 483 | |
paul@75 | 484 | # NOTE: Extra keywords are not supported. |
paul@75 | 485 | # NOTE: Somehow, the above needs to be combined with * arguments. |
paul@59 | 486 | |
paul@59 | 487 | # Either test for a complete set of arguments. |
paul@59 | 488 | |
paul@59 | 489 | if target is not None: |
paul@66 | 490 | |
paul@66 | 491 | # Make sure that enough arguments have been given. |
paul@66 | 492 | |
paul@67 | 493 | nargs_max = len(target.positional_names) |
paul@66 | 494 | ndefaults = len(target.defaults) |
paul@66 | 495 | nargs_min = nargs_max - ndefaults |
paul@67 | 496 | |
paul@86 | 497 | for i in range(ncontext, nargs_min): |
paul@67 | 498 | if i not in employed_positions: |
paul@67 | 499 | raise TranslateError(self.module.full_name(), node, |
paul@67 | 500 | "Argument %r not supplied for %r: need at least %d arguments." % (i+1, target.name, nargs_min)) |
paul@67 | 501 | |
paul@66 | 502 | nargs = len(args) |
paul@66 | 503 | |
paul@67 | 504 | if nargs > nargs_max and not target.has_star and not target.has_dstar: |
paul@59 | 505 | raise TranslateError(self.module.full_name(), node, |
paul@66 | 506 | "Too many arguments for %r: need at most %d arguments." % (target.name, nargs_max)) |
paul@66 | 507 | |
paul@66 | 508 | # Where defaults are involved, put them into the frame. |
paul@66 | 509 | # Here, we use negative index values to visit the right hand end of |
paul@66 | 510 | # the defaults list. |
paul@66 | 511 | |
paul@67 | 512 | for pos in range(nargs_min, nargs_max): |
paul@67 | 513 | if pos not in employed_positions: |
paul@67 | 514 | self.new_op(LoadConst(target)) |
paul@67 | 515 | self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@66 | 516 | |
paul@66 | 517 | # If the position corresponds to the current frame element, |
paul@66 | 518 | # skip generating the instruction. |
paul@66 | 519 | |
paul@66 | 520 | if frame_pos != pos: |
paul@66 | 521 | self.new_op(StoreFrame(pos)) |
paul@66 | 522 | |
paul@66 | 523 | frame_pos += 1 |
paul@59 | 524 | |
paul@59 | 525 | # Or generate instructions to do this at run-time. |
paul@66 | 526 | # NOTE: CheckFrame has to check the number of arguments and to fill in |
paul@66 | 527 | # NOTE: defaults. |
paul@59 | 528 | |
paul@59 | 529 | else: |
paul@59 | 530 | self.new_op(CheckFrame()) |
paul@59 | 531 | |
paul@78 | 532 | def _endCallFunc(self, temp): |
paul@57 | 533 | |
paul@57 | 534 | "Make the invocation and tidy up afterwards." |
paul@57 | 535 | |
paul@79 | 536 | self.new_ops(temp) |
paul@68 | 537 | self.new_op(JumpWithFrame()) |
paul@21 | 538 | |
paul@21 | 539 | # NOTE: Exception handling required. |
paul@21 | 540 | |
paul@21 | 541 | self.new_op(DropFrame()) |
paul@79 | 542 | self.discard_temp(temp) |
paul@17 | 543 | |
paul@56 | 544 | def _visitName(self, node, classes): |
paul@56 | 545 | |
paul@56 | 546 | """ |
paul@56 | 547 | Visit the name-related 'node', generating instructions based on the |
paul@56 | 548 | given 'classes'. |
paul@56 | 549 | """ |
paul@56 | 550 | |
paul@56 | 551 | name = node.name |
paul@56 | 552 | scope = self.get_scope(name) |
paul@56 | 553 | #print self.module.name, node.lineno, name, scope |
paul@56 | 554 | self._generateName(name, scope, classes, node) |
paul@56 | 555 | |
paul@56 | 556 | def _generateName(self, name, scope, classes, node): |
paul@56 | 557 | |
paul@56 | 558 | """ |
paul@56 | 559 | Generate code for the access to 'name' in 'scope' using the given |
paul@56 | 560 | 'classes', and using the given 'node' as the source of the access. |
paul@56 | 561 | """ |
paul@56 | 562 | |
paul@72 | 563 | NameInstruction, AddressInstruction = classes |
paul@56 | 564 | |
paul@87 | 565 | # Optimise away the constant storage if appropriate. |
paul@87 | 566 | # The target and value loading operations are removed. |
paul@87 | 567 | |
paul@56 | 568 | if self._optimise_constant_storage(NameInstruction, 0): |
paul@56 | 569 | return |
paul@56 | 570 | |
paul@56 | 571 | if scope == "local": |
paul@56 | 572 | unit = self.unit |
paul@83 | 573 | if isinstance(unit, Function): |
paul@56 | 574 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@83 | 575 | elif isinstance(unit, Class): |
paul@72 | 576 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@83 | 577 | elif isinstance(unit, Module): |
paul@72 | 578 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@56 | 579 | else: |
paul@61 | 580 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@56 | 581 | |
paul@56 | 582 | elif scope == "global": |
paul@56 | 583 | globals = self.module.module_attributes() |
paul@56 | 584 | if globals.has_key(name): |
paul@72 | 585 | self.new_op(AddressInstruction(globals[name])) |
paul@56 | 586 | else: |
paul@61 | 587 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@56 | 588 | |
paul@56 | 589 | else: |
paul@72 | 590 | self.new_op(AddressInstruction(self._get_builtin(name, node))) |
paul@56 | 591 | |
paul@61 | 592 | def _get_builtin(self, name, node): |
paul@61 | 593 | if self.builtins is not None: |
paul@63 | 594 | try: |
paul@63 | 595 | return self.builtins[name] |
paul@63 | 596 | except KeyError: |
paul@63 | 597 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@61 | 598 | else: |
paul@61 | 599 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@56 | 600 | |
paul@62 | 601 | # Optimisation tests. |
paul@62 | 602 | |
paul@62 | 603 | def _should_optimise_constant_storage(self): |
paul@62 | 604 | return "constant_storage" in self.optimisations |
paul@62 | 605 | |
paul@76 | 606 | def _should_optimise_constant_test(self): |
paul@76 | 607 | return "constant_test" in self.optimisations |
paul@76 | 608 | |
paul@62 | 609 | def _should_optimise_known_target(self): |
paul@62 | 610 | return "known_target" in self.optimisations |
paul@62 | 611 | |
paul@65 | 612 | def _should_optimise_self_access(self): |
paul@65 | 613 | return "self_access" in self.optimisations |
paul@65 | 614 | |
paul@74 | 615 | def _should_optimise_temp_storage(self): |
paul@74 | 616 | return "temp_storage" in self.optimisations |
paul@74 | 617 | |
paul@81 | 618 | def _have_constant_input(self, n): |
paul@81 | 619 | last = self.last_ops(n+1) |
paul@73 | 620 | return len(last) > n and (isinstance(last[n], LoadAddress) and last[n].attr.assignments == 1 or |
paul@87 | 621 | isinstance(last[n], LoadConst)) |
paul@62 | 622 | |
paul@81 | 623 | def _have_known_target(self): |
paul@81 | 624 | return self._have_constant_input(0) |
paul@62 | 625 | |
paul@65 | 626 | def _have_self_input(self): |
paul@65 | 627 | last = self.last_op() |
paul@83 | 628 | return isinstance(self.unit, Function) and \ |
paul@65 | 629 | self.unit.is_method() and isinstance(last, LoadName) and \ |
paul@65 | 630 | last.attr.name == "self" |
paul@65 | 631 | |
paul@74 | 632 | def _have_temp_compatible_access(self): |
paul@74 | 633 | last = self.last_op() |
paul@74 | 634 | # NOTE: Should expand to cover LoadAttr and LoadAttrIndex, but this |
paul@74 | 635 | # NOTE: would require inspection of the stack operands. |
paul@76 | 636 | return isinstance(last, (LoadName, LoadTemp, LoadAddress, LoadConst)) |
paul@74 | 637 | |
paul@62 | 638 | # Optimisation methods. See the supported_optimisations class attribute. |
paul@56 | 639 | |
paul@74 | 640 | def _optimise_temp_storage(self): |
paul@74 | 641 | |
paul@74 | 642 | """ |
paul@74 | 643 | Where the next operation would involve storing a value into temporary |
paul@78 | 644 | storage at 'temp_position', record and remove any simple instruction |
paul@78 | 645 | which produced the value to be stored such that instead of subsequently |
paul@78 | 646 | accessing the temporary storage, that instruction is substituted. |
paul@78 | 647 | |
paul@78 | 648 | If no optimisation can be achieved, a StoreTemp instruction is produced |
paul@78 | 649 | and the appropriate LoadTemp instruction is returned. |
paul@79 | 650 | |
paul@79 | 651 | All returned instructions are provided in a list. |
paul@74 | 652 | """ |
paul@74 | 653 | |
paul@74 | 654 | if self._should_optimise_temp_storage() and \ |
paul@74 | 655 | self._have_temp_compatible_access(): |
paul@74 | 656 | |
paul@74 | 657 | last = self.last_op() |
paul@74 | 658 | self.remove_ops(1) |
paul@79 | 659 | return [last] |
paul@74 | 660 | else: |
paul@78 | 661 | temp_position = self.reserve_temp(1) |
paul@78 | 662 | self.new_op(StoreTemp(temp_position)) |
paul@79 | 663 | return [LoadTemp(temp_position)] |
paul@74 | 664 | |
paul@65 | 665 | def _optimise_constant_storage(self, instruction, n): |
paul@56 | 666 | |
paul@56 | 667 | """ |
paul@56 | 668 | Where this operation should store a constant into a target which is |
paul@56 | 669 | also constant, optimise away both operations. |
paul@56 | 670 | """ |
paul@56 | 671 | |
paul@65 | 672 | if self._should_optimise_constant_storage() and \ |
paul@72 | 673 | instruction in (StoreAddress, StoreName) and \ |
paul@65 | 674 | self._have_constant_input(n) and \ |
paul@65 | 675 | (n == 0 or self._have_constant_input(n-1)): |
paul@65 | 676 | |
paul@56 | 677 | self.remove_ops(n+1) |
paul@56 | 678 | return 1 |
paul@56 | 679 | else: |
paul@56 | 680 | return 0 |
paul@56 | 681 | |
paul@76 | 682 | def _optimise_constant_test(self, instruction): |
paul@76 | 683 | |
paul@76 | 684 | """ |
paul@76 | 685 | Where this operation tests the topmost stack value which happens to be |
paul@76 | 686 | a constant against another stack value, merge the last instruction which |
paul@76 | 687 | loaded the constant into the current 'instruction'. |
paul@76 | 688 | """ |
paul@76 | 689 | |
paul@76 | 690 | if self._should_optimise_constant_test() and \ |
paul@76 | 691 | instruction is TestIdentity and \ |
paul@76 | 692 | self._have_constant_input(0): |
paul@76 | 693 | |
paul@76 | 694 | last = self.last_op() |
paul@76 | 695 | self.replace_op(TestIdentityAddress(last.attr)) |
paul@76 | 696 | return 1 |
paul@76 | 697 | else: |
paul@76 | 698 | return 0 |
paul@76 | 699 | |
paul@81 | 700 | def _optimise_known_target(self): |
paul@59 | 701 | |
paul@59 | 702 | """ |
paul@59 | 703 | Where the target of an invocation is known, provide information about it |
paul@59 | 704 | and its context. If a class is being invoked and the conditions are |
paul@59 | 705 | appropriate, get information about the specific initialiser. |
paul@59 | 706 | """ |
paul@59 | 707 | |
paul@81 | 708 | if self._should_optimise_known_target() and self._have_known_target(): |
paul@81 | 709 | last = self.last_op() |
paul@59 | 710 | target = last.attr.value |
paul@87 | 711 | context = last.attr.context |
paul@59 | 712 | |
paul@59 | 713 | # Handle calls to classes. |
paul@59 | 714 | |
paul@83 | 715 | if isinstance(target, Class): |
paul@61 | 716 | target = target.get_instantiator() |
paul@83 | 717 | context = Instance() |
paul@59 | 718 | |
paul@59 | 719 | # A special context is chosen to avoid generating unnecessary |
paul@59 | 720 | # context loading and checking instructions. |
paul@59 | 721 | |
paul@77 | 722 | return target, context |
paul@59 | 723 | else: |
paul@77 | 724 | return None |
paul@59 | 725 | |
paul@86 | 726 | def _optimise_self_access(self, attrname, classes): |
paul@65 | 727 | |
paul@65 | 728 | """ |
paul@65 | 729 | Where the provided 'attrname' accesses an attribute which occupies the |
paul@65 | 730 | same position in all possible objects which can be accessed, generate an |
paul@86 | 731 | instruction using one of the given 'classes', accessing the attribute |
paul@86 | 732 | directly. |
paul@65 | 733 | """ |
paul@65 | 734 | |
paul@86 | 735 | AddressInstruction, AttrInstruction = classes |
paul@86 | 736 | |
paul@65 | 737 | if self._should_optimise_self_access() and self._have_self_input() and \ |
paul@65 | 738 | not self.unit.is_relocated(attrname): |
paul@65 | 739 | |
paul@87 | 740 | # Either generate an instruction operating on an instance attribute. |
paul@87 | 741 | |
paul@87 | 742 | try: |
paul@87 | 743 | attr = self.unit.parent.instance_attributes()[attrname] |
paul@86 | 744 | self.new_op(AttrInstruction(attr)) |
paul@87 | 745 | |
paul@87 | 746 | # Or generate an instruction operating on a class attribute. |
paul@87 | 747 | |
paul@87 | 748 | except KeyError: |
paul@87 | 749 | attr = self.unit.parent.all_attributes()[attrname] |
paul@87 | 750 | new_attr = attr.via_instance() |
paul@87 | 751 | self.replace_op(AddressInstruction(new_attr)) |
paul@87 | 752 | |
paul@65 | 753 | return 1 |
paul@65 | 754 | else: |
paul@65 | 755 | return 0 |
paul@65 | 756 | |
paul@56 | 757 | # Visitor methods. |
paul@56 | 758 | |
paul@56 | 759 | def default(self, node, *args): |
paul@56 | 760 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@56 | 761 | |
paul@56 | 762 | def dispatch(self, node, *args): |
paul@56 | 763 | return ASTVisitor.dispatch(self, node, *args) |
paul@56 | 764 | |
paul@85 | 765 | def _visitUnary(self, node, method): |
paul@85 | 766 | |
paul@85 | 767 | """ |
paul@85 | 768 | _t = node.expr |
paul@85 | 769 | try: |
paul@85 | 770 | _result = _t.__pos__() |
paul@85 | 771 | except AttributeError: |
paul@85 | 772 | raise TypeError |
paul@85 | 773 | """ |
paul@85 | 774 | |
paul@85 | 775 | end_call_label = self.new_label() |
paul@85 | 776 | end_label = self.new_label() |
paul@85 | 777 | |
paul@85 | 778 | # Evaluate and store the operand in temporary storage. |
paul@85 | 779 | |
paul@85 | 780 | self.dispatch(node.expr) |
paul@85 | 781 | temp = self._optimise_temp_storage() |
paul@85 | 782 | |
paul@85 | 783 | # Produce the invocation. |
paul@85 | 784 | |
paul@85 | 785 | self._startCallFunc() |
paul@85 | 786 | self.new_ops(temp) |
paul@85 | 787 | |
paul@85 | 788 | # Get the method on temp. |
paul@85 | 789 | |
paul@85 | 790 | self._generateAttr(node, method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@85 | 791 | |
paul@85 | 792 | # Add exception handling to the method acquisition instructions where |
paul@85 | 793 | # the attribute access cannot be resolved at compile-time. |
paul@85 | 794 | |
paul@85 | 795 | if not self._optimise_known_target(): |
paul@85 | 796 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@85 | 797 | self.new_op(CheckException()) |
paul@85 | 798 | self.new_op(JumpIfTrue(end_call_label)) |
paul@85 | 799 | |
paul@85 | 800 | temp_method = self._optimise_temp_storage() |
paul@85 | 801 | |
paul@85 | 802 | # Add arguments. |
paul@86 | 803 | # NOTE: No support for defaults. |
paul@85 | 804 | |
paul@85 | 805 | self.new_ops(temp) # Explicit context as first argument. |
paul@85 | 806 | self._endCallFunc(temp_method) |
paul@85 | 807 | self.new_op(Jump(end_label)) |
paul@85 | 808 | |
paul@85 | 809 | # End method attempt. |
paul@85 | 810 | |
paul@85 | 811 | self.set_label(end_call_label) |
paul@85 | 812 | self.new_op(DropFrame()) # From the method call. |
paul@85 | 813 | |
paul@85 | 814 | # Raise a TypeError. |
paul@85 | 815 | |
paul@85 | 816 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@85 | 817 | self.new_op(RaiseException()) |
paul@85 | 818 | |
paul@85 | 819 | self.set_label(end_label) |
paul@85 | 820 | |
paul@85 | 821 | # Compilation duties... |
paul@85 | 822 | |
paul@85 | 823 | self.discard_temp(temp) |
paul@85 | 824 | |
paul@57 | 825 | def _visitBinary(self, node, left_method, right_method): |
paul@56 | 826 | |
paul@56 | 827 | """ |
paul@56 | 828 | _t1 = node.left |
paul@56 | 829 | _t2 = node.right |
paul@56 | 830 | try: |
paul@76 | 831 | _result = _t1.__add__(_t2) |
paul@76 | 832 | if _result is NotImplemented: |
paul@76 | 833 | raise AttributeError |
paul@56 | 834 | except AttributeError: |
paul@76 | 835 | try: |
paul@76 | 836 | _result = _t2.__radd__(_t1) |
paul@76 | 837 | if _result is NotImplemented: |
paul@76 | 838 | raise AttributeError |
paul@76 | 839 | except AttributeError: |
paul@76 | 840 | raise TypeError |
paul@56 | 841 | """ |
paul@56 | 842 | |
paul@76 | 843 | end_left_label = self.new_label() |
paul@57 | 844 | right_label = self.new_label() |
paul@85 | 845 | end_right_label = self.new_label() |
paul@76 | 846 | type_error_label = self.new_label() |
paul@57 | 847 | end_label = self.new_label() |
paul@57 | 848 | |
paul@78 | 849 | # Evaluate and store the left operand in temporary storage. |
paul@73 | 850 | |
paul@57 | 851 | self.dispatch(node.left) |
paul@78 | 852 | temp1 = self._optimise_temp_storage() |
paul@74 | 853 | |
paul@78 | 854 | # Evaluate and store the right operand in temporary storage. |
paul@74 | 855 | |
paul@57 | 856 | self.dispatch(node.right) |
paul@74 | 857 | temp2 = self._optimise_temp_storage() |
paul@57 | 858 | |
paul@57 | 859 | # Left method. |
paul@57 | 860 | |
paul@57 | 861 | self._startCallFunc() |
paul@79 | 862 | self.new_ops(temp1) |
paul@76 | 863 | |
paul@76 | 864 | # Get left method on temp1. |
paul@76 | 865 | |
paul@72 | 866 | self._generateAttr(node, left_method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@77 | 867 | |
paul@79 | 868 | # Add exception handling to the method acquisition instructions where |
paul@79 | 869 | # the attribute access cannot be resolved at compile-time. |
paul@79 | 870 | |
paul@80 | 871 | if not self._optimise_known_target(): |
paul@77 | 872 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@80 | 873 | self.new_op(CheckException()) |
paul@80 | 874 | self.new_op(JumpIfTrue(end_left_label)) |
paul@80 | 875 | |
paul@80 | 876 | temp_method = self._optimise_temp_storage() |
paul@76 | 877 | |
paul@76 | 878 | # Add arguments. |
paul@86 | 879 | # NOTE: No support for defaults. |
paul@76 | 880 | |
paul@79 | 881 | self.new_ops(temp1) # Explicit context as first argument. |
paul@79 | 882 | self.new_ops(temp2) |
paul@78 | 883 | self._endCallFunc(temp_method) |
paul@57 | 884 | |
paul@76 | 885 | # Test for NotImplemented. |
paul@76 | 886 | # Don't actually raise an exception. |
paul@76 | 887 | |
paul@76 | 888 | self.dispatch(compiler.ast.Name("NotImplemented")) |
paul@76 | 889 | if not self._optimise_constant_test(TestIdentity): |
paul@76 | 890 | self.new_op(TestIdentity()) |
paul@76 | 891 | self.new_op(JumpIfTrue(right_label)) |
paul@76 | 892 | self.new_op(Jump(end_label)) |
paul@76 | 893 | |
paul@76 | 894 | # End left method attempt. |
paul@76 | 895 | |
paul@76 | 896 | self.set_label(end_left_label) |
paul@76 | 897 | self.new_op(DropFrame()) # From the left method call. |
paul@57 | 898 | |
paul@57 | 899 | # Right method. |
paul@57 | 900 | |
paul@57 | 901 | self.set_label(right_label) |
paul@57 | 902 | self._startCallFunc() |
paul@79 | 903 | self.new_ops(temp2) |
paul@76 | 904 | |
paul@76 | 905 | # Get right method on temp2. |
paul@76 | 906 | |
paul@72 | 907 | self._generateAttr(node, right_method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@77 | 908 | |
paul@79 | 909 | # Add exception handling to the method acquisition instructions where |
paul@79 | 910 | # the attribute access cannot be resolved at compile-time. |
paul@79 | 911 | |
paul@80 | 912 | if not self._optimise_known_target(): |
paul@77 | 913 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@80 | 914 | self.new_op(CheckException()) |
paul@85 | 915 | self.new_op(JumpIfTrue(end_right_label)) |
paul@80 | 916 | |
paul@80 | 917 | temp_method = self._optimise_temp_storage() |
paul@76 | 918 | |
paul@76 | 919 | # Add arguments. |
paul@86 | 920 | # NOTE: No support for defaults. |
paul@76 | 921 | |
paul@79 | 922 | self.new_ops(temp2) # Explicit context as first argument. |
paul@79 | 923 | self.new_ops(temp1) |
paul@78 | 924 | self._endCallFunc(temp_method) |
paul@57 | 925 | |
paul@76 | 926 | # Test for NotImplemented. |
paul@76 | 927 | # Don't actually raise an exception. |
paul@76 | 928 | |
paul@76 | 929 | self.dispatch(compiler.ast.Name("NotImplemented")) |
paul@76 | 930 | if not self._optimise_constant_test(TestIdentity): |
paul@76 | 931 | self.new_op(TestIdentity()) |
paul@76 | 932 | self.new_op(JumpIfTrue(type_error_label)) |
paul@76 | 933 | self.new_op(Jump(end_label)) |
paul@76 | 934 | |
paul@85 | 935 | # End right method attempt. |
paul@85 | 936 | |
paul@85 | 937 | self.set_label(end_right_label) |
paul@85 | 938 | self.new_op(DropFrame()) # From the right method call. |
paul@85 | 939 | |
paul@76 | 940 | # Raise a TypeError. |
paul@76 | 941 | |
paul@76 | 942 | self.set_label(type_error_label) |
paul@76 | 943 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@76 | 944 | self.new_op(RaiseException()) |
paul@76 | 945 | |
paul@57 | 946 | self.set_label(end_label) |
paul@57 | 947 | |
paul@76 | 948 | # Compilation duties... |
paul@76 | 949 | |
paul@79 | 950 | self.discard_temp(temp1) |
paul@79 | 951 | self.discard_temp(temp2) |
paul@73 | 952 | |
paul@57 | 953 | def visitAdd(self, node): |
paul@57 | 954 | self._visitBinary(node, "__add__", "__radd__") |
paul@57 | 955 | |
paul@56 | 956 | def visitAnd(self, node): pass |
paul@56 | 957 | |
paul@56 | 958 | def visitAssert(self, node): pass |
paul@56 | 959 | |
paul@56 | 960 | def visitAssign(self, node): |
paul@56 | 961 | self.dispatch(node.expr) |
paul@56 | 962 | for n in node.nodes: |
paul@56 | 963 | self.dispatch(n) |
paul@56 | 964 | |
paul@56 | 965 | def visitAssAttr(self, node): |
paul@72 | 966 | self._visitAttr(node, (StoreAddress, StoreAttr, StoreAttrIndex)) |
paul@56 | 967 | |
paul@56 | 968 | def visitAssList(self, node): pass |
paul@56 | 969 | |
paul@56 | 970 | def visitAssName(self, node): |
paul@72 | 971 | self._visitName(node, (StoreName, StoreAddress)) |
paul@56 | 972 | |
paul@56 | 973 | visitAssTuple = visitAssList |
paul@56 | 974 | |
paul@56 | 975 | def visitAugAssign(self, node): pass |
paul@56 | 976 | |
paul@56 | 977 | def visitBackquote(self, node): pass |
paul@56 | 978 | |
paul@85 | 979 | def visitBitand(self, node): |
paul@85 | 980 | self._visitBinary(node, "__and__", "__rand__") |
paul@56 | 981 | |
paul@85 | 982 | def visitBitor(self, node): |
paul@85 | 983 | self._visitBinary(node, "__or__", "__ror__") |
paul@56 | 984 | |
paul@85 | 985 | def visitBitxor(self, node): |
paul@85 | 986 | self._visitBinary(node, "__xor__", "__rxor__") |
paul@56 | 987 | |
paul@56 | 988 | def visitBreak(self, node): |
paul@57 | 989 | next_label, exit_label = self.get_loop_labels() |
paul@56 | 990 | self.new_op(Jump(exit_label)) |
paul@56 | 991 | |
paul@56 | 992 | def visitCallFunc(self, node): |
paul@56 | 993 | |
paul@56 | 994 | """ |
paul@56 | 995 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@56 | 996 | the correct location, then invoke the function. |
paul@56 | 997 | """ |
paul@56 | 998 | |
paul@56 | 999 | # Mark the frame, evaluate the target, generate the call. |
paul@56 | 1000 | |
paul@56 | 1001 | self._startCallFunc() |
paul@56 | 1002 | self.dispatch(node.node) |
paul@78 | 1003 | temp = self._generateCallFunc(node.args, node) |
paul@78 | 1004 | self._endCallFunc(temp) |
paul@56 | 1005 | |
paul@38 | 1006 | def visitClass(self, node): |
paul@90 | 1007 | |
paul@90 | 1008 | # Store the name. |
paul@90 | 1009 | |
paul@90 | 1010 | self.new_op(LoadConst(node.unit)) |
paul@90 | 1011 | self._visitName(node, (StoreName, StoreAddress)) |
paul@90 | 1012 | |
paul@90 | 1013 | # Visit the code. |
paul@90 | 1014 | |
paul@38 | 1015 | unit = self.unit |
paul@39 | 1016 | self.unit = node.unit |
paul@44 | 1017 | self.unit.code_location = self.module.code_location # class body code is not independently addressable |
paul@38 | 1018 | self.dispatch(node.code) |
paul@38 | 1019 | self.unit = unit |
paul@17 | 1020 | |
paul@21 | 1021 | def visitCompare(self, node): |
paul@21 | 1022 | |
paul@21 | 1023 | """ |
paul@21 | 1024 | self.dispatch(node.expr) |
paul@21 | 1025 | for op_name, next_node in compare.ops: |
paul@21 | 1026 | methods = self.comparison_methods[op_name] |
paul@21 | 1027 | if methods is not None: |
paul@21 | 1028 | # Generate method call using evaluated argument and next node. |
paul@21 | 1029 | else: |
paul@21 | 1030 | # Deal with the special operators. |
paul@21 | 1031 | # Provide short-circuiting. |
paul@21 | 1032 | """ |
paul@17 | 1033 | |
paul@22 | 1034 | def visitConst(self, node): |
paul@22 | 1035 | const = self.module.constant_values[node.value] |
paul@22 | 1036 | self.new_op(LoadConst(const)) |
paul@17 | 1037 | |
paul@41 | 1038 | def visitContinue(self, node): |
paul@57 | 1039 | next_label, exit_label = self.get_loop_labels() |
paul@41 | 1040 | self.new_op(Jump(next_label)) |
paul@17 | 1041 | |
paul@19 | 1042 | def visitDecorators(self, node): pass |
paul@17 | 1043 | |
paul@19 | 1044 | def visitDict(self, node): pass |
paul@17 | 1045 | |
paul@18 | 1046 | def visitDiscard(self, node): |
paul@19 | 1047 | self.dispatch(node.expr) |
paul@90 | 1048 | self.new_op(Pop()) |
paul@17 | 1049 | |
paul@57 | 1050 | def visitDiv(self, node): |
paul@57 | 1051 | self._visitBinary(node, "__div__", "__rdiv__") |
paul@17 | 1052 | |
paul@19 | 1053 | def visitEllipsis(self, node): pass |
paul@17 | 1054 | |
paul@19 | 1055 | def visitExec(self, node): pass |
paul@17 | 1056 | |
paul@19 | 1057 | def visitExpression(self, node): pass |
paul@17 | 1058 | |
paul@57 | 1059 | def visitFloorDiv(self, node): |
paul@57 | 1060 | self._visitBinary(node, "__floordiv__", "__rfloordiv__") |
paul@17 | 1061 | |
paul@44 | 1062 | def visitFor(self, node): |
paul@44 | 1063 | exit_label = self.new_label() |
paul@44 | 1064 | next_label = self.new_label() |
paul@44 | 1065 | else_label = self.new_label() |
paul@44 | 1066 | |
paul@44 | 1067 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 1068 | |
paul@45 | 1069 | self._startCallFunc() |
paul@44 | 1070 | self.dispatch(node.list) |
paul@72 | 1071 | self._generateAttr(node, "__iter__", (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@78 | 1072 | temp = self._generateCallFunc([], node) |
paul@78 | 1073 | self._endCallFunc(temp) |
paul@57 | 1074 | |
paul@44 | 1075 | # Iterator on stack. |
paul@44 | 1076 | |
paul@44 | 1077 | # In the loop... |
paul@44 | 1078 | |
paul@44 | 1079 | self.set_label(next_label) |
paul@44 | 1080 | |
paul@44 | 1081 | # Use the iterator to get the next value. |
paul@44 | 1082 | |
paul@45 | 1083 | self._startCallFunc() |
paul@44 | 1084 | self.new_op(Duplicate()) |
paul@72 | 1085 | self._generateAttr(node, "next", (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@78 | 1086 | temp = self._generateCallFunc([], node) |
paul@78 | 1087 | self._endCallFunc(temp) |
paul@44 | 1088 | |
paul@44 | 1089 | # Test for StopIteration. |
paul@44 | 1090 | |
paul@57 | 1091 | self.dispatch(compiler.ast.Name("StopIteration")) |
paul@57 | 1092 | self.new_op(CheckException()) |
paul@44 | 1093 | if node.else_ is not None: |
paul@44 | 1094 | self.new_op(JumpIfTrue(else_label)) |
paul@44 | 1095 | else: |
paul@44 | 1096 | self.new_op(JumpIfTrue(exit_label)) |
paul@44 | 1097 | |
paul@44 | 1098 | # Assign to the target. |
paul@44 | 1099 | |
paul@44 | 1100 | self.dispatch(node.assign) |
paul@44 | 1101 | |
paul@44 | 1102 | # Process the body with the current next and exit points. |
paul@44 | 1103 | |
paul@57 | 1104 | self.add_loop_labels(next_label, exit_label) |
paul@44 | 1105 | self.dispatch(node.body) |
paul@57 | 1106 | self.drop_loop_labels() |
paul@44 | 1107 | |
paul@44 | 1108 | # Repeat the loop. |
paul@44 | 1109 | |
paul@44 | 1110 | self.new_op(Jump(next_label)) |
paul@44 | 1111 | |
paul@44 | 1112 | # Produce the "else" section. |
paul@44 | 1113 | |
paul@44 | 1114 | if node.else_ is not None: |
paul@44 | 1115 | self.set_label(exit_label) |
paul@44 | 1116 | self.dispatch(node.else_) |
paul@44 | 1117 | |
paul@44 | 1118 | # Pop the iterator. |
paul@44 | 1119 | |
paul@44 | 1120 | self.set_label(exit_label) |
paul@44 | 1121 | self.new_op(Pop()) |
paul@17 | 1122 | |
paul@19 | 1123 | def visitFrom(self, node): pass |
paul@17 | 1124 | |
paul@17 | 1125 | def visitFunction(self, node): |
paul@17 | 1126 | |
paul@17 | 1127 | # Only store the name when visiting this node from outside. |
paul@17 | 1128 | |
paul@39 | 1129 | if self.unit is not node.unit: |
paul@55 | 1130 | self.new_op(LoadConst(node.unit)) |
paul@72 | 1131 | self._visitName(node, (StoreName, StoreAddress)) |
paul@22 | 1132 | |
paul@73 | 1133 | # Generate the default initialisation code. |
paul@73 | 1134 | |
paul@73 | 1135 | for attr, default in zip(node.unit.default_attrs, node.unit.defaults): |
paul@73 | 1136 | self.dispatch(default) |
paul@73 | 1137 | self.new_op(StoreAddress(attr)) |
paul@73 | 1138 | |
paul@22 | 1139 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 1140 | |
paul@17 | 1141 | else: |
paul@19 | 1142 | self.dispatch(node.code) |
paul@68 | 1143 | if not isinstance(self.last_op(), Return): |
paul@68 | 1144 | self.dispatch(compiler.ast.Name("None")) |
paul@68 | 1145 | self.new_op(Return()) |
paul@17 | 1146 | |
paul@19 | 1147 | def visitGenExpr(self, node): pass |
paul@17 | 1148 | |
paul@19 | 1149 | def visitGenExprFor(self, node): pass |
paul@17 | 1150 | |
paul@19 | 1151 | def visitGenExprIf(self, node): pass |
paul@17 | 1152 | |
paul@19 | 1153 | def visitGenExprInner(self, node): pass |
paul@17 | 1154 | |
paul@43 | 1155 | def visitGetattr(self, node): |
paul@72 | 1156 | self._visitAttr(node, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@17 | 1157 | |
paul@19 | 1158 | def visitGlobal(self, node): pass |
paul@17 | 1159 | |
paul@17 | 1160 | def visitIf(self, node): |
paul@19 | 1161 | first = 1 |
paul@19 | 1162 | exit_label = self.new_label() |
paul@17 | 1163 | |
paul@19 | 1164 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 1165 | if body is None: |
paul@19 | 1166 | break |
paul@19 | 1167 | if not first: |
paul@19 | 1168 | self.set_label(next_label) |
paul@19 | 1169 | if test is not None: |
paul@19 | 1170 | self.dispatch(test) |
paul@19 | 1171 | next_label = self.new_label() |
paul@41 | 1172 | self.new_op(JumpIfFalse(next_label)) |
paul@19 | 1173 | self.dispatch(body) |
paul@19 | 1174 | self.new_op(Jump(exit_label)) |
paul@19 | 1175 | first = 0 |
paul@17 | 1176 | |
paul@19 | 1177 | self.set_label(exit_label) |
paul@17 | 1178 | |
paul@19 | 1179 | def visitImport(self, node): pass |
paul@19 | 1180 | |
paul@85 | 1181 | def visitInvert(self, node): |
paul@85 | 1182 | self._visitUnary(node, "__invert__") |
paul@17 | 1183 | |
paul@19 | 1184 | def visitKeyword(self, node): pass |
paul@17 | 1185 | |
paul@19 | 1186 | def visitLambda(self, node): pass |
paul@19 | 1187 | |
paul@85 | 1188 | def visitLeftShift(self, node): |
paul@85 | 1189 | self._visitBinary(node, "__lshift__", "__rlshift__") |
paul@17 | 1190 | |
paul@19 | 1191 | def visitList(self, node): pass |
paul@17 | 1192 | |
paul@19 | 1193 | def visitListComp(self, node): pass |
paul@17 | 1194 | |
paul@19 | 1195 | def visitListCompFor(self, node): pass |
paul@17 | 1196 | |
paul@19 | 1197 | def visitListCompIf(self, node): pass |
paul@19 | 1198 | |
paul@57 | 1199 | def visitMod(self, node): |
paul@57 | 1200 | self._visitBinary(node, "__mod__", "__rmod__") |
paul@17 | 1201 | |
paul@17 | 1202 | def visitModule(self, node): |
paul@19 | 1203 | self.dispatch(node.node) |
paul@17 | 1204 | |
paul@57 | 1205 | def visitMul(self, node): |
paul@57 | 1206 | self._visitBinary(node, "__mul__", "__rmul__") |
paul@17 | 1207 | |
paul@17 | 1208 | def visitName(self, node): |
paul@82 | 1209 | if node.name == "None": |
paul@82 | 1210 | const = self.module.constant_values[None] |
paul@82 | 1211 | self.new_op(LoadConst(const)) |
paul@82 | 1212 | else: |
paul@82 | 1213 | self._visitName(node, (LoadName, LoadAddress)) |
paul@17 | 1214 | |
paul@19 | 1215 | def visitNot(self, node): pass |
paul@17 | 1216 | |
paul@19 | 1217 | def visitOr(self, node): pass |
paul@17 | 1218 | |
paul@19 | 1219 | def visitPass(self, node): pass |
paul@17 | 1220 | |
paul@85 | 1221 | def visitPower(self, node): |
paul@85 | 1222 | self._visitBinary(node, "__pow__", "__rpow__") |
paul@17 | 1223 | |
paul@19 | 1224 | def visitPrint(self, node): pass |
paul@17 | 1225 | |
paul@19 | 1226 | def visitPrintnl(self, node): pass |
paul@17 | 1227 | |
paul@19 | 1228 | def visitRaise(self, node): pass |
paul@17 | 1229 | |
paul@21 | 1230 | def visitReturn(self, node): |
paul@21 | 1231 | if node.value is not None: |
paul@21 | 1232 | self.dispatch(node.value) |
paul@68 | 1233 | else: |
paul@68 | 1234 | self.dispatch(compiler.ast.Name("None")) |
paul@21 | 1235 | self.new_op(Return()) |
paul@17 | 1236 | |
paul@85 | 1237 | def visitRightShift(self, node): |
paul@85 | 1238 | self._visitBinary(node, "__rshift__", "__rrshift__") |
paul@17 | 1239 | |
paul@19 | 1240 | def visitSlice(self, node): pass |
paul@17 | 1241 | |
paul@17 | 1242 | def visitStmt(self, node): |
paul@17 | 1243 | for n in node.nodes: |
paul@19 | 1244 | self.dispatch(n) |
paul@19 | 1245 | |
paul@57 | 1246 | def visitSub(self, node): |
paul@57 | 1247 | self._visitBinary(node, "__sub__", "__rsub__") |
paul@17 | 1248 | |
paul@19 | 1249 | def visitSubscript(self, node): pass |
paul@17 | 1250 | |
paul@57 | 1251 | def visitTryExcept(self, node): |
paul@57 | 1252 | exit_label = self.new_label() |
paul@57 | 1253 | handler_label = self.new_label() |
paul@57 | 1254 | |
paul@57 | 1255 | self.add_exception_labels(handler_label, exit_label) |
paul@57 | 1256 | |
paul@68 | 1257 | # Try... |
paul@68 | 1258 | # Produce the code, then jump to the exit. |
paul@68 | 1259 | |
paul@57 | 1260 | self.dispatch(node.body) |
paul@57 | 1261 | self.new_op(Jump(exit_label)) |
paul@57 | 1262 | |
paul@68 | 1263 | # Start of handlers. |
paul@68 | 1264 | |
paul@57 | 1265 | self.set_label(handler_label) |
paul@57 | 1266 | for name, assignment, handler in node.handlers: |
paul@57 | 1267 | next_label = self.new_label() |
paul@57 | 1268 | |
paul@68 | 1269 | # Test the given exception against the current exception. |
paul@68 | 1270 | |
paul@57 | 1271 | if name is not None: |
paul@57 | 1272 | self.dispatch(name) |
paul@57 | 1273 | self.new_op(CheckException()) |
paul@57 | 1274 | self.new_op(JumpIfFalse(next_label)) |
paul@57 | 1275 | |
paul@68 | 1276 | # Handle assignment to exception variable. |
paul@68 | 1277 | |
paul@57 | 1278 | if assignment is not None: |
paul@57 | 1279 | self.dispatch(assignment) |
paul@57 | 1280 | |
paul@68 | 1281 | # Produce the handler code, then jump to the exit. |
paul@68 | 1282 | |
paul@57 | 1283 | self.dispatch(handler) |
paul@57 | 1284 | self.new_op(Jump(exit_label)) |
paul@57 | 1285 | |
paul@57 | 1286 | self.set_label(next_label) |
paul@57 | 1287 | |
paul@57 | 1288 | # Unhandled exceptions. |
paul@57 | 1289 | |
paul@57 | 1290 | self.new_op(RaiseException()) |
paul@57 | 1291 | |
paul@57 | 1292 | # After exception |
paul@57 | 1293 | |
paul@57 | 1294 | self.set_label(exit_label) |
paul@57 | 1295 | |
paul@57 | 1296 | # Optional else clause. |
paul@57 | 1297 | |
paul@57 | 1298 | if node.else_ is not None: |
paul@57 | 1299 | self.dispatch(node.else_) |
paul@57 | 1300 | |
paul@57 | 1301 | self.drop_exception_labels() |
paul@17 | 1302 | |
paul@19 | 1303 | def visitTryFinally(self, node): pass |
paul@17 | 1304 | |
paul@19 | 1305 | def visitTuple(self, node): pass |
paul@17 | 1306 | |
paul@85 | 1307 | def visitUnaryAdd(self, node): |
paul@85 | 1308 | self._visitUnary(node, "__pos__") |
paul@17 | 1309 | |
paul@85 | 1310 | def visitUnarySub(self, node): |
paul@85 | 1311 | self._visitUnary(node, "__neg__") |
paul@17 | 1312 | |
paul@41 | 1313 | def visitWhile(self, node): |
paul@41 | 1314 | exit_label = self.new_label() |
paul@44 | 1315 | next_label = self.new_label() |
paul@44 | 1316 | else_label = self.new_label() |
paul@41 | 1317 | |
paul@41 | 1318 | self.set_label(next_label) |
paul@44 | 1319 | self.dispatch(node.test) |
paul@44 | 1320 | if node.else_ is not None: |
paul@44 | 1321 | self.new_op(JumpIfFalse(else_label)) |
paul@44 | 1322 | else: |
paul@44 | 1323 | self.new_op(JumpIfFalse(exit_label)) |
paul@44 | 1324 | |
paul@57 | 1325 | self.add_loop_labels(next_label, exit_label) |
paul@41 | 1326 | |
paul@41 | 1327 | self.dispatch(node.body) |
paul@41 | 1328 | self.new_op(Jump(next_label)) |
paul@41 | 1329 | |
paul@41 | 1330 | if node.else_ is not None: |
paul@44 | 1331 | self.set_label(else_label) |
paul@41 | 1332 | self.dispatch(node.else_) |
paul@41 | 1333 | |
paul@41 | 1334 | self.set_label(exit_label) |
paul@57 | 1335 | self.drop_loop_labels() |
paul@17 | 1336 | |
paul@19 | 1337 | def visitWith(self, node): pass |
paul@17 | 1338 | |
paul@19 | 1339 | def visitYield(self, node): pass |
paul@17 | 1340 | |
paul@21 | 1341 | # Useful data. |
paul@21 | 1342 | |
paul@21 | 1343 | comparison_methods = { |
paul@21 | 1344 | "==" : ("__eq__", "__ne__"), |
paul@21 | 1345 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 1346 | "<" : ("__lt__", "__gt__"), |
paul@21 | 1347 | "<=" : ("__le__", "__ge__"), |
paul@21 | 1348 | ">=" : ("__ge__", "__le__"), |
paul@21 | 1349 | ">" : ("__gt__", "__lt__"), |
paul@21 | 1350 | "is" : None, |
paul@21 | 1351 | "is not" : None, |
paul@21 | 1352 | "in" : None, |
paul@21 | 1353 | "not in" : None |
paul@21 | 1354 | } |
paul@21 | 1355 | |
paul@17 | 1356 | # vim: tabstop=4 expandtab shiftwidth=4 |