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