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