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