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@91 | 108 | if unit.astnode is not None: |
paul@91 | 109 | self.dispatch(unit.astnode) |
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@91 | 514 | #self.new_op(LoadConst(target)) |
paul@91 | 515 | #self.new_op(LoadAttr(target.default_attrs[pos - nargs_min])) |
paul@91 | 516 | self.new_op(LoadAddress(target.default_attrs[pos - nargs_min])) |
paul@66 | 517 | |
paul@91 | 518 | # If the position corresponds to the current frame element, |
paul@91 | 519 | # skip generating the instruction. |
paul@66 | 520 | |
paul@91 | 521 | if frame_pos != pos: |
paul@91 | 522 | self.new_op(StoreFrame(pos)) |
paul@66 | 523 | |
paul@66 | 524 | frame_pos += 1 |
paul@59 | 525 | |
paul@59 | 526 | # Or generate instructions to do this at run-time. |
paul@66 | 527 | # NOTE: CheckFrame has to check the number of arguments and to fill in |
paul@66 | 528 | # NOTE: defaults. |
paul@59 | 529 | |
paul@59 | 530 | else: |
paul@59 | 531 | self.new_op(CheckFrame()) |
paul@59 | 532 | |
paul@78 | 533 | def _endCallFunc(self, temp): |
paul@57 | 534 | |
paul@57 | 535 | "Make the invocation and tidy up afterwards." |
paul@57 | 536 | |
paul@79 | 537 | self.new_ops(temp) |
paul@68 | 538 | self.new_op(JumpWithFrame()) |
paul@21 | 539 | |
paul@21 | 540 | # NOTE: Exception handling required. |
paul@21 | 541 | |
paul@21 | 542 | self.new_op(DropFrame()) |
paul@79 | 543 | self.discard_temp(temp) |
paul@17 | 544 | |
paul@56 | 545 | def _visitName(self, node, classes): |
paul@56 | 546 | |
paul@56 | 547 | """ |
paul@56 | 548 | Visit the name-related 'node', generating instructions based on the |
paul@56 | 549 | given 'classes'. |
paul@56 | 550 | """ |
paul@56 | 551 | |
paul@56 | 552 | name = node.name |
paul@56 | 553 | scope = self.get_scope(name) |
paul@56 | 554 | #print self.module.name, node.lineno, name, scope |
paul@56 | 555 | self._generateName(name, scope, classes, node) |
paul@56 | 556 | |
paul@56 | 557 | def _generateName(self, name, scope, classes, node): |
paul@56 | 558 | |
paul@56 | 559 | """ |
paul@56 | 560 | Generate code for the access to 'name' in 'scope' using the given |
paul@56 | 561 | 'classes', and using the given 'node' as the source of the access. |
paul@56 | 562 | """ |
paul@56 | 563 | |
paul@72 | 564 | NameInstruction, AddressInstruction = classes |
paul@56 | 565 | |
paul@87 | 566 | # Optimise away the constant storage if appropriate. |
paul@87 | 567 | # The target and value loading operations are removed. |
paul@87 | 568 | |
paul@56 | 569 | if self._optimise_constant_storage(NameInstruction, 0): |
paul@56 | 570 | return |
paul@56 | 571 | |
paul@56 | 572 | if scope == "local": |
paul@56 | 573 | unit = self.unit |
paul@83 | 574 | if isinstance(unit, Function): |
paul@56 | 575 | self.new_op(NameInstruction(unit.all_locals()[name])) |
paul@83 | 576 | elif isinstance(unit, Class): |
paul@72 | 577 | self.new_op(AddressInstruction(unit.all_class_attributes()[name])) |
paul@83 | 578 | elif isinstance(unit, Module): |
paul@72 | 579 | self.new_op(AddressInstruction(unit.module_attributes()[name])) |
paul@56 | 580 | else: |
paul@61 | 581 | raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name)) |
paul@56 | 582 | |
paul@56 | 583 | elif scope == "global": |
paul@56 | 584 | globals = self.module.module_attributes() |
paul@56 | 585 | if globals.has_key(name): |
paul@72 | 586 | self.new_op(AddressInstruction(globals[name])) |
paul@56 | 587 | else: |
paul@61 | 588 | raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name)) |
paul@56 | 589 | |
paul@56 | 590 | else: |
paul@72 | 591 | self.new_op(AddressInstruction(self._get_builtin(name, node))) |
paul@56 | 592 | |
paul@61 | 593 | def _get_builtin(self, name, node): |
paul@61 | 594 | if self.builtins is not None: |
paul@63 | 595 | try: |
paul@63 | 596 | return self.builtins[name] |
paul@63 | 597 | except KeyError: |
paul@63 | 598 | raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name) |
paul@61 | 599 | else: |
paul@61 | 600 | raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name) |
paul@56 | 601 | |
paul@62 | 602 | # Optimisation tests. |
paul@62 | 603 | |
paul@62 | 604 | def _should_optimise_constant_storage(self): |
paul@62 | 605 | return "constant_storage" in self.optimisations |
paul@62 | 606 | |
paul@76 | 607 | def _should_optimise_constant_test(self): |
paul@76 | 608 | return "constant_test" in self.optimisations |
paul@76 | 609 | |
paul@62 | 610 | def _should_optimise_known_target(self): |
paul@62 | 611 | return "known_target" in self.optimisations |
paul@62 | 612 | |
paul@65 | 613 | def _should_optimise_self_access(self): |
paul@65 | 614 | return "self_access" in self.optimisations |
paul@65 | 615 | |
paul@74 | 616 | def _should_optimise_temp_storage(self): |
paul@74 | 617 | return "temp_storage" in self.optimisations |
paul@74 | 618 | |
paul@81 | 619 | def _have_constant_input(self, n): |
paul@81 | 620 | last = self.last_ops(n+1) |
paul@73 | 621 | return len(last) > n and (isinstance(last[n], LoadAddress) and last[n].attr.assignments == 1 or |
paul@87 | 622 | isinstance(last[n], LoadConst)) |
paul@62 | 623 | |
paul@81 | 624 | def _have_known_target(self): |
paul@81 | 625 | return self._have_constant_input(0) |
paul@62 | 626 | |
paul@65 | 627 | def _have_self_input(self): |
paul@65 | 628 | last = self.last_op() |
paul@83 | 629 | return isinstance(self.unit, Function) and \ |
paul@65 | 630 | self.unit.is_method() and isinstance(last, LoadName) and \ |
paul@65 | 631 | last.attr.name == "self" |
paul@65 | 632 | |
paul@74 | 633 | def _have_temp_compatible_access(self): |
paul@74 | 634 | last = self.last_op() |
paul@74 | 635 | # NOTE: Should expand to cover LoadAttr and LoadAttrIndex, but this |
paul@74 | 636 | # NOTE: would require inspection of the stack operands. |
paul@76 | 637 | return isinstance(last, (LoadName, LoadTemp, LoadAddress, LoadConst)) |
paul@74 | 638 | |
paul@62 | 639 | # Optimisation methods. See the supported_optimisations class attribute. |
paul@56 | 640 | |
paul@74 | 641 | def _optimise_temp_storage(self): |
paul@74 | 642 | |
paul@74 | 643 | """ |
paul@74 | 644 | Where the next operation would involve storing a value into temporary |
paul@78 | 645 | storage at 'temp_position', record and remove any simple instruction |
paul@78 | 646 | which produced the value to be stored such that instead of subsequently |
paul@78 | 647 | accessing the temporary storage, that instruction is substituted. |
paul@78 | 648 | |
paul@78 | 649 | If no optimisation can be achieved, a StoreTemp instruction is produced |
paul@78 | 650 | and the appropriate LoadTemp instruction is returned. |
paul@79 | 651 | |
paul@79 | 652 | All returned instructions are provided in a list. |
paul@74 | 653 | """ |
paul@74 | 654 | |
paul@74 | 655 | if self._should_optimise_temp_storage() and \ |
paul@74 | 656 | self._have_temp_compatible_access(): |
paul@74 | 657 | |
paul@74 | 658 | last = self.last_op() |
paul@74 | 659 | self.remove_ops(1) |
paul@79 | 660 | return [last] |
paul@74 | 661 | else: |
paul@78 | 662 | temp_position = self.reserve_temp(1) |
paul@78 | 663 | self.new_op(StoreTemp(temp_position)) |
paul@79 | 664 | return [LoadTemp(temp_position)] |
paul@74 | 665 | |
paul@65 | 666 | def _optimise_constant_storage(self, instruction, n): |
paul@56 | 667 | |
paul@56 | 668 | """ |
paul@56 | 669 | Where this operation should store a constant into a target which is |
paul@56 | 670 | also constant, optimise away both operations. |
paul@56 | 671 | """ |
paul@56 | 672 | |
paul@65 | 673 | if self._should_optimise_constant_storage() and \ |
paul@72 | 674 | instruction in (StoreAddress, StoreName) and \ |
paul@65 | 675 | self._have_constant_input(n) and \ |
paul@65 | 676 | (n == 0 or self._have_constant_input(n-1)): |
paul@65 | 677 | |
paul@56 | 678 | self.remove_ops(n+1) |
paul@56 | 679 | return 1 |
paul@56 | 680 | else: |
paul@56 | 681 | return 0 |
paul@56 | 682 | |
paul@76 | 683 | def _optimise_constant_test(self, instruction): |
paul@76 | 684 | |
paul@76 | 685 | """ |
paul@76 | 686 | Where this operation tests the topmost stack value which happens to be |
paul@76 | 687 | a constant against another stack value, merge the last instruction which |
paul@76 | 688 | loaded the constant into the current 'instruction'. |
paul@76 | 689 | """ |
paul@76 | 690 | |
paul@76 | 691 | if self._should_optimise_constant_test() and \ |
paul@76 | 692 | instruction is TestIdentity and \ |
paul@76 | 693 | self._have_constant_input(0): |
paul@76 | 694 | |
paul@76 | 695 | last = self.last_op() |
paul@76 | 696 | self.replace_op(TestIdentityAddress(last.attr)) |
paul@76 | 697 | return 1 |
paul@76 | 698 | else: |
paul@76 | 699 | return 0 |
paul@76 | 700 | |
paul@81 | 701 | def _optimise_known_target(self): |
paul@59 | 702 | |
paul@59 | 703 | """ |
paul@59 | 704 | Where the target of an invocation is known, provide information about it |
paul@59 | 705 | and its context. If a class is being invoked and the conditions are |
paul@59 | 706 | appropriate, get information about the specific initialiser. |
paul@59 | 707 | """ |
paul@59 | 708 | |
paul@81 | 709 | if self._should_optimise_known_target() and self._have_known_target(): |
paul@81 | 710 | last = self.last_op() |
paul@59 | 711 | target = last.attr.value |
paul@87 | 712 | context = last.attr.context |
paul@59 | 713 | |
paul@59 | 714 | # Handle calls to classes. |
paul@59 | 715 | |
paul@83 | 716 | if isinstance(target, Class): |
paul@61 | 717 | target = target.get_instantiator() |
paul@83 | 718 | context = Instance() |
paul@59 | 719 | |
paul@59 | 720 | # A special context is chosen to avoid generating unnecessary |
paul@59 | 721 | # context loading and checking instructions. |
paul@59 | 722 | |
paul@77 | 723 | return target, context |
paul@59 | 724 | else: |
paul@77 | 725 | return None |
paul@59 | 726 | |
paul@86 | 727 | def _optimise_self_access(self, attrname, classes): |
paul@65 | 728 | |
paul@65 | 729 | """ |
paul@65 | 730 | Where the provided 'attrname' accesses an attribute which occupies the |
paul@65 | 731 | same position in all possible objects which can be accessed, generate an |
paul@86 | 732 | instruction using one of the given 'classes', accessing the attribute |
paul@86 | 733 | directly. |
paul@65 | 734 | """ |
paul@65 | 735 | |
paul@86 | 736 | AddressInstruction, AttrInstruction = classes |
paul@86 | 737 | |
paul@65 | 738 | if self._should_optimise_self_access() and self._have_self_input() and \ |
paul@65 | 739 | not self.unit.is_relocated(attrname): |
paul@65 | 740 | |
paul@87 | 741 | # Either generate an instruction operating on an instance attribute. |
paul@87 | 742 | |
paul@87 | 743 | try: |
paul@87 | 744 | attr = self.unit.parent.instance_attributes()[attrname] |
paul@86 | 745 | self.new_op(AttrInstruction(attr)) |
paul@87 | 746 | |
paul@87 | 747 | # Or generate an instruction operating on a class attribute. |
paul@87 | 748 | |
paul@87 | 749 | except KeyError: |
paul@87 | 750 | attr = self.unit.parent.all_attributes()[attrname] |
paul@87 | 751 | new_attr = attr.via_instance() |
paul@87 | 752 | self.replace_op(AddressInstruction(new_attr)) |
paul@87 | 753 | |
paul@65 | 754 | return 1 |
paul@65 | 755 | else: |
paul@65 | 756 | return 0 |
paul@65 | 757 | |
paul@56 | 758 | # Visitor methods. |
paul@56 | 759 | |
paul@56 | 760 | def default(self, node, *args): |
paul@56 | 761 | raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@56 | 762 | |
paul@56 | 763 | def dispatch(self, node, *args): |
paul@56 | 764 | return ASTVisitor.dispatch(self, node, *args) |
paul@56 | 765 | |
paul@85 | 766 | def _visitUnary(self, node, method): |
paul@85 | 767 | |
paul@85 | 768 | """ |
paul@85 | 769 | _t = node.expr |
paul@85 | 770 | try: |
paul@85 | 771 | _result = _t.__pos__() |
paul@85 | 772 | except AttributeError: |
paul@85 | 773 | raise TypeError |
paul@85 | 774 | """ |
paul@85 | 775 | |
paul@85 | 776 | end_call_label = self.new_label() |
paul@85 | 777 | end_label = self.new_label() |
paul@85 | 778 | |
paul@85 | 779 | # Evaluate and store the operand in temporary storage. |
paul@85 | 780 | |
paul@85 | 781 | self.dispatch(node.expr) |
paul@85 | 782 | temp = self._optimise_temp_storage() |
paul@85 | 783 | |
paul@85 | 784 | # Produce the invocation. |
paul@85 | 785 | |
paul@85 | 786 | self._startCallFunc() |
paul@85 | 787 | self.new_ops(temp) |
paul@85 | 788 | |
paul@85 | 789 | # Get the method on temp. |
paul@85 | 790 | |
paul@85 | 791 | self._generateAttr(node, method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@85 | 792 | |
paul@85 | 793 | # Add exception handling to the method acquisition instructions where |
paul@85 | 794 | # the attribute access cannot be resolved at compile-time. |
paul@85 | 795 | |
paul@85 | 796 | if not self._optimise_known_target(): |
paul@85 | 797 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@85 | 798 | self.new_op(CheckException()) |
paul@85 | 799 | self.new_op(JumpIfTrue(end_call_label)) |
paul@85 | 800 | |
paul@85 | 801 | temp_method = self._optimise_temp_storage() |
paul@85 | 802 | |
paul@85 | 803 | # Add arguments. |
paul@86 | 804 | # NOTE: No support for defaults. |
paul@85 | 805 | |
paul@85 | 806 | self.new_ops(temp) # Explicit context as first argument. |
paul@85 | 807 | self._endCallFunc(temp_method) |
paul@85 | 808 | self.new_op(Jump(end_label)) |
paul@85 | 809 | |
paul@85 | 810 | # End method attempt. |
paul@85 | 811 | |
paul@85 | 812 | self.set_label(end_call_label) |
paul@85 | 813 | self.new_op(DropFrame()) # From the method call. |
paul@85 | 814 | |
paul@85 | 815 | # Raise a TypeError. |
paul@85 | 816 | |
paul@85 | 817 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@85 | 818 | self.new_op(RaiseException()) |
paul@85 | 819 | |
paul@85 | 820 | self.set_label(end_label) |
paul@85 | 821 | |
paul@85 | 822 | # Compilation duties... |
paul@85 | 823 | |
paul@85 | 824 | self.discard_temp(temp) |
paul@85 | 825 | |
paul@57 | 826 | def _visitBinary(self, node, left_method, right_method): |
paul@56 | 827 | |
paul@56 | 828 | """ |
paul@56 | 829 | _t1 = node.left |
paul@56 | 830 | _t2 = node.right |
paul@56 | 831 | try: |
paul@76 | 832 | _result = _t1.__add__(_t2) |
paul@76 | 833 | if _result is NotImplemented: |
paul@76 | 834 | raise AttributeError |
paul@56 | 835 | except AttributeError: |
paul@76 | 836 | try: |
paul@76 | 837 | _result = _t2.__radd__(_t1) |
paul@76 | 838 | if _result is NotImplemented: |
paul@76 | 839 | raise AttributeError |
paul@76 | 840 | except AttributeError: |
paul@76 | 841 | raise TypeError |
paul@56 | 842 | """ |
paul@56 | 843 | |
paul@76 | 844 | end_left_label = self.new_label() |
paul@57 | 845 | right_label = self.new_label() |
paul@85 | 846 | end_right_label = self.new_label() |
paul@76 | 847 | type_error_label = self.new_label() |
paul@57 | 848 | end_label = self.new_label() |
paul@57 | 849 | |
paul@78 | 850 | # Evaluate and store the left operand in temporary storage. |
paul@73 | 851 | |
paul@57 | 852 | self.dispatch(node.left) |
paul@78 | 853 | temp1 = self._optimise_temp_storage() |
paul@74 | 854 | |
paul@78 | 855 | # Evaluate and store the right operand in temporary storage. |
paul@74 | 856 | |
paul@57 | 857 | self.dispatch(node.right) |
paul@74 | 858 | temp2 = self._optimise_temp_storage() |
paul@57 | 859 | |
paul@57 | 860 | # Left method. |
paul@57 | 861 | |
paul@57 | 862 | self._startCallFunc() |
paul@79 | 863 | self.new_ops(temp1) |
paul@76 | 864 | |
paul@76 | 865 | # Get left method on temp1. |
paul@76 | 866 | |
paul@72 | 867 | self._generateAttr(node, left_method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@77 | 868 | |
paul@79 | 869 | # Add exception handling to the method acquisition instructions where |
paul@79 | 870 | # the attribute access cannot be resolved at compile-time. |
paul@79 | 871 | |
paul@80 | 872 | if not self._optimise_known_target(): |
paul@77 | 873 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@80 | 874 | self.new_op(CheckException()) |
paul@80 | 875 | self.new_op(JumpIfTrue(end_left_label)) |
paul@80 | 876 | |
paul@80 | 877 | temp_method = self._optimise_temp_storage() |
paul@76 | 878 | |
paul@76 | 879 | # Add arguments. |
paul@86 | 880 | # NOTE: No support for defaults. |
paul@76 | 881 | |
paul@79 | 882 | self.new_ops(temp1) # Explicit context as first argument. |
paul@79 | 883 | self.new_ops(temp2) |
paul@78 | 884 | self._endCallFunc(temp_method) |
paul@57 | 885 | |
paul@76 | 886 | # Test for NotImplemented. |
paul@76 | 887 | # Don't actually raise an exception. |
paul@76 | 888 | |
paul@76 | 889 | self.dispatch(compiler.ast.Name("NotImplemented")) |
paul@76 | 890 | if not self._optimise_constant_test(TestIdentity): |
paul@76 | 891 | self.new_op(TestIdentity()) |
paul@76 | 892 | self.new_op(JumpIfTrue(right_label)) |
paul@76 | 893 | self.new_op(Jump(end_label)) |
paul@76 | 894 | |
paul@76 | 895 | # End left method attempt. |
paul@76 | 896 | |
paul@76 | 897 | self.set_label(end_left_label) |
paul@76 | 898 | self.new_op(DropFrame()) # From the left method call. |
paul@57 | 899 | |
paul@57 | 900 | # Right method. |
paul@57 | 901 | |
paul@57 | 902 | self.set_label(right_label) |
paul@57 | 903 | self._startCallFunc() |
paul@79 | 904 | self.new_ops(temp2) |
paul@76 | 905 | |
paul@76 | 906 | # Get right method on temp2. |
paul@76 | 907 | |
paul@72 | 908 | self._generateAttr(node, right_method, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@77 | 909 | |
paul@79 | 910 | # Add exception handling to the method acquisition instructions where |
paul@79 | 911 | # the attribute access cannot be resolved at compile-time. |
paul@79 | 912 | |
paul@80 | 913 | if not self._optimise_known_target(): |
paul@77 | 914 | self.dispatch(compiler.ast.Name("AttributeError")) |
paul@80 | 915 | self.new_op(CheckException()) |
paul@85 | 916 | self.new_op(JumpIfTrue(end_right_label)) |
paul@80 | 917 | |
paul@80 | 918 | temp_method = self._optimise_temp_storage() |
paul@76 | 919 | |
paul@76 | 920 | # Add arguments. |
paul@86 | 921 | # NOTE: No support for defaults. |
paul@76 | 922 | |
paul@79 | 923 | self.new_ops(temp2) # Explicit context as first argument. |
paul@79 | 924 | self.new_ops(temp1) |
paul@78 | 925 | self._endCallFunc(temp_method) |
paul@57 | 926 | |
paul@76 | 927 | # Test for NotImplemented. |
paul@76 | 928 | # Don't actually raise an exception. |
paul@76 | 929 | |
paul@76 | 930 | self.dispatch(compiler.ast.Name("NotImplemented")) |
paul@76 | 931 | if not self._optimise_constant_test(TestIdentity): |
paul@76 | 932 | self.new_op(TestIdentity()) |
paul@76 | 933 | self.new_op(JumpIfTrue(type_error_label)) |
paul@76 | 934 | self.new_op(Jump(end_label)) |
paul@76 | 935 | |
paul@85 | 936 | # End right method attempt. |
paul@85 | 937 | |
paul@85 | 938 | self.set_label(end_right_label) |
paul@85 | 939 | self.new_op(DropFrame()) # From the right method call. |
paul@85 | 940 | |
paul@76 | 941 | # Raise a TypeError. |
paul@76 | 942 | |
paul@76 | 943 | self.set_label(type_error_label) |
paul@76 | 944 | self.dispatch(compiler.ast.Name("TypeError")) |
paul@76 | 945 | self.new_op(RaiseException()) |
paul@76 | 946 | |
paul@57 | 947 | self.set_label(end_label) |
paul@57 | 948 | |
paul@76 | 949 | # Compilation duties... |
paul@76 | 950 | |
paul@79 | 951 | self.discard_temp(temp1) |
paul@79 | 952 | self.discard_temp(temp2) |
paul@73 | 953 | |
paul@57 | 954 | def visitAdd(self, node): |
paul@57 | 955 | self._visitBinary(node, "__add__", "__radd__") |
paul@57 | 956 | |
paul@56 | 957 | def visitAnd(self, node): pass |
paul@56 | 958 | |
paul@56 | 959 | def visitAssert(self, node): pass |
paul@56 | 960 | |
paul@56 | 961 | def visitAssign(self, node): |
paul@56 | 962 | self.dispatch(node.expr) |
paul@56 | 963 | for n in node.nodes: |
paul@56 | 964 | self.dispatch(n) |
paul@56 | 965 | |
paul@56 | 966 | def visitAssAttr(self, node): |
paul@72 | 967 | self._visitAttr(node, (StoreAddress, StoreAttr, StoreAttrIndex)) |
paul@56 | 968 | |
paul@56 | 969 | def visitAssList(self, node): pass |
paul@56 | 970 | |
paul@56 | 971 | def visitAssName(self, node): |
paul@72 | 972 | self._visitName(node, (StoreName, StoreAddress)) |
paul@56 | 973 | |
paul@56 | 974 | visitAssTuple = visitAssList |
paul@56 | 975 | |
paul@56 | 976 | def visitAugAssign(self, node): pass |
paul@56 | 977 | |
paul@56 | 978 | def visitBackquote(self, node): pass |
paul@56 | 979 | |
paul@85 | 980 | def visitBitand(self, node): |
paul@85 | 981 | self._visitBinary(node, "__and__", "__rand__") |
paul@56 | 982 | |
paul@85 | 983 | def visitBitor(self, node): |
paul@85 | 984 | self._visitBinary(node, "__or__", "__ror__") |
paul@56 | 985 | |
paul@85 | 986 | def visitBitxor(self, node): |
paul@85 | 987 | self._visitBinary(node, "__xor__", "__rxor__") |
paul@56 | 988 | |
paul@56 | 989 | def visitBreak(self, node): |
paul@57 | 990 | next_label, exit_label = self.get_loop_labels() |
paul@56 | 991 | self.new_op(Jump(exit_label)) |
paul@56 | 992 | |
paul@56 | 993 | def visitCallFunc(self, node): |
paul@56 | 994 | |
paul@56 | 995 | """ |
paul@56 | 996 | Evaluate positional arguments, evaluate and store keyword arguments in |
paul@56 | 997 | the correct location, then invoke the function. |
paul@56 | 998 | """ |
paul@56 | 999 | |
paul@56 | 1000 | # Mark the frame, evaluate the target, generate the call. |
paul@56 | 1001 | |
paul@56 | 1002 | self._startCallFunc() |
paul@56 | 1003 | self.dispatch(node.node) |
paul@78 | 1004 | temp = self._generateCallFunc(node.args, node) |
paul@78 | 1005 | self._endCallFunc(temp) |
paul@56 | 1006 | |
paul@38 | 1007 | def visitClass(self, node): |
paul@90 | 1008 | |
paul@90 | 1009 | # Store the name. |
paul@90 | 1010 | |
paul@90 | 1011 | self.new_op(LoadConst(node.unit)) |
paul@90 | 1012 | self._visitName(node, (StoreName, StoreAddress)) |
paul@90 | 1013 | |
paul@90 | 1014 | # Visit the code. |
paul@90 | 1015 | |
paul@38 | 1016 | unit = self.unit |
paul@39 | 1017 | self.unit = node.unit |
paul@44 | 1018 | self.unit.code_location = self.module.code_location # class body code is not independently addressable |
paul@38 | 1019 | self.dispatch(node.code) |
paul@38 | 1020 | self.unit = unit |
paul@17 | 1021 | |
paul@21 | 1022 | def visitCompare(self, node): |
paul@21 | 1023 | |
paul@21 | 1024 | """ |
paul@21 | 1025 | self.dispatch(node.expr) |
paul@21 | 1026 | for op_name, next_node in compare.ops: |
paul@21 | 1027 | methods = self.comparison_methods[op_name] |
paul@21 | 1028 | if methods is not None: |
paul@21 | 1029 | # Generate method call using evaluated argument and next node. |
paul@21 | 1030 | else: |
paul@21 | 1031 | # Deal with the special operators. |
paul@21 | 1032 | # Provide short-circuiting. |
paul@21 | 1033 | """ |
paul@17 | 1034 | |
paul@22 | 1035 | def visitConst(self, node): |
paul@22 | 1036 | const = self.module.constant_values[node.value] |
paul@22 | 1037 | self.new_op(LoadConst(const)) |
paul@17 | 1038 | |
paul@41 | 1039 | def visitContinue(self, node): |
paul@57 | 1040 | next_label, exit_label = self.get_loop_labels() |
paul@41 | 1041 | self.new_op(Jump(next_label)) |
paul@17 | 1042 | |
paul@19 | 1043 | def visitDecorators(self, node): pass |
paul@17 | 1044 | |
paul@19 | 1045 | def visitDict(self, node): pass |
paul@17 | 1046 | |
paul@18 | 1047 | def visitDiscard(self, node): |
paul@19 | 1048 | self.dispatch(node.expr) |
paul@90 | 1049 | self.new_op(Pop()) |
paul@17 | 1050 | |
paul@57 | 1051 | def visitDiv(self, node): |
paul@57 | 1052 | self._visitBinary(node, "__div__", "__rdiv__") |
paul@17 | 1053 | |
paul@19 | 1054 | def visitEllipsis(self, node): pass |
paul@17 | 1055 | |
paul@19 | 1056 | def visitExec(self, node): pass |
paul@17 | 1057 | |
paul@19 | 1058 | def visitExpression(self, node): pass |
paul@17 | 1059 | |
paul@57 | 1060 | def visitFloorDiv(self, node): |
paul@57 | 1061 | self._visitBinary(node, "__floordiv__", "__rfloordiv__") |
paul@17 | 1062 | |
paul@44 | 1063 | def visitFor(self, node): |
paul@44 | 1064 | exit_label = self.new_label() |
paul@44 | 1065 | next_label = self.new_label() |
paul@44 | 1066 | else_label = self.new_label() |
paul@44 | 1067 | |
paul@44 | 1068 | # Get the "list" to be iterated over, obtain its iterator. |
paul@44 | 1069 | |
paul@45 | 1070 | self._startCallFunc() |
paul@44 | 1071 | self.dispatch(node.list) |
paul@72 | 1072 | self._generateAttr(node, "__iter__", (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@78 | 1073 | temp = self._generateCallFunc([], node) |
paul@78 | 1074 | self._endCallFunc(temp) |
paul@57 | 1075 | |
paul@44 | 1076 | # Iterator on stack. |
paul@44 | 1077 | |
paul@44 | 1078 | # In the loop... |
paul@44 | 1079 | |
paul@44 | 1080 | self.set_label(next_label) |
paul@44 | 1081 | |
paul@44 | 1082 | # Use the iterator to get the next value. |
paul@44 | 1083 | |
paul@45 | 1084 | self._startCallFunc() |
paul@44 | 1085 | self.new_op(Duplicate()) |
paul@72 | 1086 | self._generateAttr(node, "next", (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@78 | 1087 | temp = self._generateCallFunc([], node) |
paul@78 | 1088 | self._endCallFunc(temp) |
paul@44 | 1089 | |
paul@44 | 1090 | # Test for StopIteration. |
paul@44 | 1091 | |
paul@57 | 1092 | self.dispatch(compiler.ast.Name("StopIteration")) |
paul@57 | 1093 | self.new_op(CheckException()) |
paul@44 | 1094 | if node.else_ is not None: |
paul@44 | 1095 | self.new_op(JumpIfTrue(else_label)) |
paul@44 | 1096 | else: |
paul@44 | 1097 | self.new_op(JumpIfTrue(exit_label)) |
paul@44 | 1098 | |
paul@44 | 1099 | # Assign to the target. |
paul@44 | 1100 | |
paul@44 | 1101 | self.dispatch(node.assign) |
paul@44 | 1102 | |
paul@44 | 1103 | # Process the body with the current next and exit points. |
paul@44 | 1104 | |
paul@57 | 1105 | self.add_loop_labels(next_label, exit_label) |
paul@44 | 1106 | self.dispatch(node.body) |
paul@57 | 1107 | self.drop_loop_labels() |
paul@44 | 1108 | |
paul@44 | 1109 | # Repeat the loop. |
paul@44 | 1110 | |
paul@44 | 1111 | self.new_op(Jump(next_label)) |
paul@44 | 1112 | |
paul@44 | 1113 | # Produce the "else" section. |
paul@44 | 1114 | |
paul@44 | 1115 | if node.else_ is not None: |
paul@44 | 1116 | self.set_label(exit_label) |
paul@44 | 1117 | self.dispatch(node.else_) |
paul@44 | 1118 | |
paul@44 | 1119 | # Pop the iterator. |
paul@44 | 1120 | |
paul@44 | 1121 | self.set_label(exit_label) |
paul@44 | 1122 | self.new_op(Pop()) |
paul@17 | 1123 | |
paul@19 | 1124 | def visitFrom(self, node): pass |
paul@17 | 1125 | |
paul@17 | 1126 | def visitFunction(self, node): |
paul@17 | 1127 | |
paul@17 | 1128 | # Only store the name when visiting this node from outside. |
paul@17 | 1129 | |
paul@39 | 1130 | if self.unit is not node.unit: |
paul@55 | 1131 | self.new_op(LoadConst(node.unit)) |
paul@72 | 1132 | self._visitName(node, (StoreName, StoreAddress)) |
paul@22 | 1133 | |
paul@73 | 1134 | # Generate the default initialisation code. |
paul@73 | 1135 | |
paul@73 | 1136 | for attr, default in zip(node.unit.default_attrs, node.unit.defaults): |
paul@73 | 1137 | self.dispatch(default) |
paul@73 | 1138 | self.new_op(StoreAddress(attr)) |
paul@73 | 1139 | |
paul@22 | 1140 | # Visiting of the code occurs when get_code is invoked on this node. |
paul@22 | 1141 | |
paul@17 | 1142 | else: |
paul@19 | 1143 | self.dispatch(node.code) |
paul@68 | 1144 | if not isinstance(self.last_op(), Return): |
paul@68 | 1145 | self.dispatch(compiler.ast.Name("None")) |
paul@68 | 1146 | self.new_op(Return()) |
paul@17 | 1147 | |
paul@19 | 1148 | def visitGenExpr(self, node): pass |
paul@17 | 1149 | |
paul@19 | 1150 | def visitGenExprFor(self, node): pass |
paul@17 | 1151 | |
paul@19 | 1152 | def visitGenExprIf(self, node): pass |
paul@17 | 1153 | |
paul@19 | 1154 | def visitGenExprInner(self, node): pass |
paul@17 | 1155 | |
paul@43 | 1156 | def visitGetattr(self, node): |
paul@72 | 1157 | self._visitAttr(node, (LoadAddress, LoadAttr, LoadAttrIndex)) |
paul@17 | 1158 | |
paul@19 | 1159 | def visitGlobal(self, node): pass |
paul@17 | 1160 | |
paul@17 | 1161 | def visitIf(self, node): |
paul@19 | 1162 | first = 1 |
paul@19 | 1163 | exit_label = self.new_label() |
paul@17 | 1164 | |
paul@19 | 1165 | for test, body in node.tests + [(None, node.else_)]: |
paul@19 | 1166 | if body is None: |
paul@19 | 1167 | break |
paul@19 | 1168 | if not first: |
paul@19 | 1169 | self.set_label(next_label) |
paul@19 | 1170 | if test is not None: |
paul@19 | 1171 | self.dispatch(test) |
paul@19 | 1172 | next_label = self.new_label() |
paul@41 | 1173 | self.new_op(JumpIfFalse(next_label)) |
paul@19 | 1174 | self.dispatch(body) |
paul@19 | 1175 | self.new_op(Jump(exit_label)) |
paul@19 | 1176 | first = 0 |
paul@17 | 1177 | |
paul@19 | 1178 | self.set_label(exit_label) |
paul@17 | 1179 | |
paul@19 | 1180 | def visitImport(self, node): pass |
paul@19 | 1181 | |
paul@85 | 1182 | def visitInvert(self, node): |
paul@85 | 1183 | self._visitUnary(node, "__invert__") |
paul@17 | 1184 | |
paul@19 | 1185 | def visitKeyword(self, node): pass |
paul@17 | 1186 | |
paul@19 | 1187 | def visitLambda(self, node): pass |
paul@19 | 1188 | |
paul@85 | 1189 | def visitLeftShift(self, node): |
paul@85 | 1190 | self._visitBinary(node, "__lshift__", "__rlshift__") |
paul@17 | 1191 | |
paul@19 | 1192 | def visitList(self, node): pass |
paul@17 | 1193 | |
paul@19 | 1194 | def visitListComp(self, node): pass |
paul@17 | 1195 | |
paul@19 | 1196 | def visitListCompFor(self, node): pass |
paul@17 | 1197 | |
paul@19 | 1198 | def visitListCompIf(self, node): pass |
paul@19 | 1199 | |
paul@57 | 1200 | def visitMod(self, node): |
paul@57 | 1201 | self._visitBinary(node, "__mod__", "__rmod__") |
paul@17 | 1202 | |
paul@17 | 1203 | def visitModule(self, node): |
paul@19 | 1204 | self.dispatch(node.node) |
paul@17 | 1205 | |
paul@57 | 1206 | def visitMul(self, node): |
paul@57 | 1207 | self._visitBinary(node, "__mul__", "__rmul__") |
paul@17 | 1208 | |
paul@17 | 1209 | def visitName(self, node): |
paul@82 | 1210 | if node.name == "None": |
paul@82 | 1211 | const = self.module.constant_values[None] |
paul@82 | 1212 | self.new_op(LoadConst(const)) |
paul@82 | 1213 | else: |
paul@82 | 1214 | self._visitName(node, (LoadName, LoadAddress)) |
paul@17 | 1215 | |
paul@19 | 1216 | def visitNot(self, node): pass |
paul@17 | 1217 | |
paul@19 | 1218 | def visitOr(self, node): pass |
paul@17 | 1219 | |
paul@19 | 1220 | def visitPass(self, node): pass |
paul@17 | 1221 | |
paul@85 | 1222 | def visitPower(self, node): |
paul@85 | 1223 | self._visitBinary(node, "__pow__", "__rpow__") |
paul@17 | 1224 | |
paul@19 | 1225 | def visitPrint(self, node): pass |
paul@17 | 1226 | |
paul@19 | 1227 | def visitPrintnl(self, node): pass |
paul@17 | 1228 | |
paul@19 | 1229 | def visitRaise(self, node): pass |
paul@17 | 1230 | |
paul@21 | 1231 | def visitReturn(self, node): |
paul@21 | 1232 | if node.value is not None: |
paul@21 | 1233 | self.dispatch(node.value) |
paul@68 | 1234 | else: |
paul@68 | 1235 | self.dispatch(compiler.ast.Name("None")) |
paul@21 | 1236 | self.new_op(Return()) |
paul@17 | 1237 | |
paul@85 | 1238 | def visitRightShift(self, node): |
paul@85 | 1239 | self._visitBinary(node, "__rshift__", "__rrshift__") |
paul@17 | 1240 | |
paul@19 | 1241 | def visitSlice(self, node): pass |
paul@17 | 1242 | |
paul@17 | 1243 | def visitStmt(self, node): |
paul@17 | 1244 | for n in node.nodes: |
paul@19 | 1245 | self.dispatch(n) |
paul@19 | 1246 | |
paul@57 | 1247 | def visitSub(self, node): |
paul@57 | 1248 | self._visitBinary(node, "__sub__", "__rsub__") |
paul@17 | 1249 | |
paul@19 | 1250 | def visitSubscript(self, node): pass |
paul@17 | 1251 | |
paul@57 | 1252 | def visitTryExcept(self, node): |
paul@57 | 1253 | exit_label = self.new_label() |
paul@57 | 1254 | handler_label = self.new_label() |
paul@57 | 1255 | |
paul@57 | 1256 | self.add_exception_labels(handler_label, exit_label) |
paul@57 | 1257 | |
paul@68 | 1258 | # Try... |
paul@68 | 1259 | # Produce the code, then jump to the exit. |
paul@68 | 1260 | |
paul@57 | 1261 | self.dispatch(node.body) |
paul@57 | 1262 | self.new_op(Jump(exit_label)) |
paul@57 | 1263 | |
paul@68 | 1264 | # Start of handlers. |
paul@68 | 1265 | |
paul@57 | 1266 | self.set_label(handler_label) |
paul@57 | 1267 | for name, assignment, handler in node.handlers: |
paul@57 | 1268 | next_label = self.new_label() |
paul@57 | 1269 | |
paul@68 | 1270 | # Test the given exception against the current exception. |
paul@68 | 1271 | |
paul@57 | 1272 | if name is not None: |
paul@57 | 1273 | self.dispatch(name) |
paul@57 | 1274 | self.new_op(CheckException()) |
paul@57 | 1275 | self.new_op(JumpIfFalse(next_label)) |
paul@57 | 1276 | |
paul@68 | 1277 | # Handle assignment to exception variable. |
paul@68 | 1278 | |
paul@57 | 1279 | if assignment is not None: |
paul@57 | 1280 | self.dispatch(assignment) |
paul@57 | 1281 | |
paul@68 | 1282 | # Produce the handler code, then jump to the exit. |
paul@68 | 1283 | |
paul@57 | 1284 | self.dispatch(handler) |
paul@57 | 1285 | self.new_op(Jump(exit_label)) |
paul@57 | 1286 | |
paul@57 | 1287 | self.set_label(next_label) |
paul@57 | 1288 | |
paul@57 | 1289 | # Unhandled exceptions. |
paul@57 | 1290 | |
paul@57 | 1291 | self.new_op(RaiseException()) |
paul@57 | 1292 | |
paul@57 | 1293 | # After exception |
paul@57 | 1294 | |
paul@57 | 1295 | self.set_label(exit_label) |
paul@57 | 1296 | |
paul@57 | 1297 | # Optional else clause. |
paul@57 | 1298 | |
paul@57 | 1299 | if node.else_ is not None: |
paul@57 | 1300 | self.dispatch(node.else_) |
paul@57 | 1301 | |
paul@57 | 1302 | self.drop_exception_labels() |
paul@17 | 1303 | |
paul@19 | 1304 | def visitTryFinally(self, node): pass |
paul@17 | 1305 | |
paul@19 | 1306 | def visitTuple(self, node): pass |
paul@17 | 1307 | |
paul@85 | 1308 | def visitUnaryAdd(self, node): |
paul@85 | 1309 | self._visitUnary(node, "__pos__") |
paul@17 | 1310 | |
paul@85 | 1311 | def visitUnarySub(self, node): |
paul@85 | 1312 | self._visitUnary(node, "__neg__") |
paul@17 | 1313 | |
paul@41 | 1314 | def visitWhile(self, node): |
paul@41 | 1315 | exit_label = self.new_label() |
paul@44 | 1316 | next_label = self.new_label() |
paul@44 | 1317 | else_label = self.new_label() |
paul@41 | 1318 | |
paul@41 | 1319 | self.set_label(next_label) |
paul@44 | 1320 | self.dispatch(node.test) |
paul@44 | 1321 | if node.else_ is not None: |
paul@44 | 1322 | self.new_op(JumpIfFalse(else_label)) |
paul@44 | 1323 | else: |
paul@44 | 1324 | self.new_op(JumpIfFalse(exit_label)) |
paul@44 | 1325 | |
paul@57 | 1326 | self.add_loop_labels(next_label, exit_label) |
paul@41 | 1327 | |
paul@41 | 1328 | self.dispatch(node.body) |
paul@41 | 1329 | self.new_op(Jump(next_label)) |
paul@41 | 1330 | |
paul@41 | 1331 | if node.else_ is not None: |
paul@44 | 1332 | self.set_label(else_label) |
paul@41 | 1333 | self.dispatch(node.else_) |
paul@41 | 1334 | |
paul@41 | 1335 | self.set_label(exit_label) |
paul@57 | 1336 | self.drop_loop_labels() |
paul@17 | 1337 | |
paul@19 | 1338 | def visitWith(self, node): pass |
paul@17 | 1339 | |
paul@19 | 1340 | def visitYield(self, node): pass |
paul@17 | 1341 | |
paul@21 | 1342 | # Useful data. |
paul@21 | 1343 | |
paul@21 | 1344 | comparison_methods = { |
paul@21 | 1345 | "==" : ("__eq__", "__ne__"), |
paul@21 | 1346 | "!=" : ("__ne__", "__eq__"), |
paul@21 | 1347 | "<" : ("__lt__", "__gt__"), |
paul@21 | 1348 | "<=" : ("__le__", "__ge__"), |
paul@21 | 1349 | ">=" : ("__ge__", "__le__"), |
paul@21 | 1350 | ">" : ("__gt__", "__lt__"), |
paul@21 | 1351 | "is" : None, |
paul@21 | 1352 | "is not" : None, |
paul@21 | 1353 | "in" : None, |
paul@21 | 1354 | "not in" : None |
paul@21 | 1355 | } |
paul@21 | 1356 | |
paul@17 | 1357 | # vim: tabstop=4 expandtab shiftwidth=4 |