paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paulb@24 | 4 | Simplify AST structures for easier type propagation and analysis. The code in |
paulb@24 | 5 | this module processes AST trees originating from the compiler module and |
paulb@24 | 6 | produces a result tree consisting of instruction-oriented program nodes. |
paul@0 | 7 | |
paul@0 | 8 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This software is free software; you can redistribute it and/or |
paul@0 | 11 | modify it under the terms of the GNU General Public License as |
paul@0 | 12 | published by the Free Software Foundation; either version 2 of |
paul@0 | 13 | the License, or (at your option) any later version. |
paul@0 | 14 | |
paul@0 | 15 | This software is distributed in the hope that it will be useful, |
paul@0 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 18 | GNU General Public License for more details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public |
paul@0 | 21 | License along with this library; see the file LICENCE.txt |
paul@0 | 22 | If not, write to the Free Software Foundation, Inc., |
paul@0 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 24 | |
paulb@46 | 25 | -------- |
paulb@46 | 26 | |
paulb@57 | 27 | To use this module, the easiest approach is to use the simplify function: |
paulb@57 | 28 | |
paulb@57 | 29 | simplify(filename) |
paulb@57 | 30 | |
paulb@57 | 31 | The more complicated approach involves first instantiating a Simplifier object: |
paulb@46 | 32 | |
paulb@46 | 33 | simplifier = Simplifier() |
paulb@46 | 34 | |
paulb@57 | 35 | Then, applying the simplifier to an AST tree: |
paulb@46 | 36 | |
paulb@57 | 37 | module = compiler.parseFile(filename) |
paulb@46 | 38 | simplifier.process(module) |
paul@0 | 39 | """ |
paul@0 | 40 | |
paulb@19 | 41 | from simplified import * |
paul@0 | 42 | import compiler.ast |
paulb@88 | 43 | import os |
paul@0 | 44 | |
paulb@19 | 45 | class Simplifier(Visitor): |
paul@0 | 46 | |
paul@0 | 47 | """ |
paul@0 | 48 | A simplifying visitor for AST nodes. |
paul@0 | 49 | |
paul@5 | 50 | Covered: And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, |
paulb@13 | 51 | CallFunc, Class, Compare, Const, Continue, Dict, Discard, For, |
paulb@13 | 52 | From, Function, Getattr, Global, If, Import, Invert, Keyword, |
paulb@13 | 53 | Lambda, List, Module, Name, Not, Or, Pass, Raise, Return, Slice, |
paulb@13 | 54 | Stmt, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, |
paulb@13 | 55 | UnarySub. |
paul@0 | 56 | |
paulb@13 | 57 | Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Div, |
paulb@13 | 58 | Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor, |
paulb@13 | 59 | ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, Sliceobj, |
paulb@13 | 60 | Sub, Yield. |
paul@0 | 61 | """ |
paul@0 | 62 | |
paulb@34 | 63 | def __init__(self, builtins=0): |
paulb@46 | 64 | |
paulb@46 | 65 | """ |
paulb@46 | 66 | Initialise the simplifier with the optional 'builtins' parameter |
paulb@46 | 67 | indicating whether the module contains the built-in classes and |
paulb@46 | 68 | functions. |
paulb@46 | 69 | """ |
paulb@46 | 70 | |
paulb@19 | 71 | Visitor.__init__(self) |
paul@1 | 72 | self.subprograms = [] # Subprograms outside the tree. |
paulb@24 | 73 | self.structures = [] # Structures/classes. |
paulb@24 | 74 | self.constants = {} # Constants. |
paul@1 | 75 | self.current_subprograms = [] # Current subprograms being processed. |
paulb@120 | 76 | self.current_structures = [] # Current structures being processed. |
paulb@34 | 77 | self.builtins = builtins # Whether the builtins are being processed. |
paul@0 | 78 | |
paulb@65 | 79 | # Convenience attributes. |
paulb@65 | 80 | |
paulb@65 | 81 | self.subnames = {} |
paulb@65 | 82 | |
paulb@51 | 83 | # For compiler package mechanisms. |
paulb@51 | 84 | |
paulb@51 | 85 | self.visitor = self |
paulb@51 | 86 | |
paulb@88 | 87 | def process(self, node, name): |
paulb@88 | 88 | result = self.dispatch(node, name) |
paulb@57 | 89 | result.simplifier = self |
paulb@57 | 90 | return result |
paulb@20 | 91 | |
paulb@6 | 92 | def dispatch_or_none(self, node, *args): |
paulb@6 | 93 | if node is not None: |
paulb@6 | 94 | return self.dispatch(node, *args) |
paulb@6 | 95 | else: |
paulb@79 | 96 | return LoadName(node, name="None") |
paulb@6 | 97 | |
paul@0 | 98 | # Placeholder or deletion transformations. |
paul@0 | 99 | |
paul@0 | 100 | def visitStmt(self, stmt): |
paul@0 | 101 | return self.dispatches(stmt.nodes) |
paul@0 | 102 | |
paul@0 | 103 | def visitPass(self, pass_): |
paulb@79 | 104 | return Pass(pass_, 1) |
paul@0 | 105 | |
paul@0 | 106 | def visitDiscard(self, discard): |
paul@0 | 107 | return self.dispatch(discard.expr) |
paul@0 | 108 | |
paul@0 | 109 | # Relatively trivial transformations. |
paul@0 | 110 | |
paulb@88 | 111 | def visitModule(self, module, name=None): |
paulb@46 | 112 | |
paulb@46 | 113 | """ |
paulb@46 | 114 | Process the given 'module', producing a Module object which contains the |
paulb@88 | 115 | resulting program nodes. If the optional 'name' is provided, the 'name' |
paulb@88 | 116 | attribute is set on the Module object using a value other than None. |
paulb@46 | 117 | """ |
paulb@46 | 118 | |
paulb@118 | 119 | result = self.module = Module(module, 1, name=name) |
paulb@34 | 120 | module_code = self.dispatch(module.node) |
paulb@34 | 121 | |
paulb@34 | 122 | # NOTE: Constant initialisation necessary for annotation but perhaps |
paulb@34 | 123 | # NOTE: redundant in the program. |
paulb@34 | 124 | |
paulb@34 | 125 | init_code = [] |
paulb@34 | 126 | for value, constant in self.constants.items(): |
paulb@59 | 127 | init_code.append( |
paulb@101 | 128 | StoreAttr( |
paulb@101 | 129 | lvalue=LoadRef(ref=constant), |
paulb@59 | 130 | name="__class__", |
paulb@101 | 131 | expr=LoadName(name=constant.typename) |
paulb@59 | 132 | ) |
paulb@59 | 133 | ) |
paulb@34 | 134 | |
paulb@34 | 135 | # NOTE: Hack to ensure correct initialisation of constants. |
paulb@34 | 136 | |
paulb@34 | 137 | if self.builtins: |
paulb@57 | 138 | result.code = module_code + init_code |
paulb@34 | 139 | else: |
paulb@57 | 140 | result.code = init_code + module_code |
paulb@57 | 141 | return result |
paul@0 | 142 | |
paul@0 | 143 | def visitGetattr(self, getattr): |
paulb@79 | 144 | result = LoadAttr(getattr, 1, |
paulb@56 | 145 | name=getattr.attrname, |
paulb@56 | 146 | expr=self.dispatch(getattr.expr) |
paulb@56 | 147 | ) |
paul@0 | 148 | return result |
paul@0 | 149 | |
paul@0 | 150 | def visitKeyword(self, keyword): |
paulb@79 | 151 | result = Keyword(keyword, 1, |
paulb@56 | 152 | name=keyword.name, |
paulb@56 | 153 | expr=self.dispatch(keyword.expr) |
paulb@56 | 154 | ) |
paul@0 | 155 | return result |
paul@0 | 156 | |
paul@1 | 157 | def visitGlobal(self, global_): |
paulb@79 | 158 | result = Global(global_, 1, |
paulb@56 | 159 | names=global_.names |
paulb@56 | 160 | ) |
paul@1 | 161 | return result |
paul@1 | 162 | |
paul@3 | 163 | def visitImport(self, import_): |
paulb@79 | 164 | result = Assign(import_, 1) |
paul@3 | 165 | code = [] |
paul@3 | 166 | for path, alias in import_.names: |
paulb@101 | 167 | importer = Import(name=path) |
paul@3 | 168 | top = alias or path.split(".")[0] |
paulb@101 | 169 | code.append(StoreName(expr=importer, name=top)) |
paul@3 | 170 | result.code = code |
paul@3 | 171 | return result |
paul@3 | 172 | |
paul@3 | 173 | def visitFrom(self, from_): |
paulb@79 | 174 | result = Assign(from_, 1) |
paul@3 | 175 | code = [] |
paulb@101 | 176 | code.append(StoreTemp(expr=Import(name=from_.modname))) |
paul@3 | 177 | for name, alias in from_.names: |
paulb@59 | 178 | code.append( |
paulb@101 | 179 | StoreName( |
paulb@101 | 180 | expr=LoadAttr( |
paulb@101 | 181 | expr=LoadTemp(), |
paulb@59 | 182 | name=name), |
paulb@59 | 183 | name=(alias or name) |
paulb@59 | 184 | ) |
paulb@59 | 185 | ) |
paulb@101 | 186 | code.append(ReleaseTemp()) |
paul@3 | 187 | result.code = code |
paul@3 | 188 | return result |
paul@3 | 189 | |
paul@0 | 190 | def visitName(self, name): |
paulb@79 | 191 | result = LoadName(name, 1, name=name.name) |
paul@0 | 192 | return result |
paul@0 | 193 | |
paul@0 | 194 | def visitConst(self, const): |
paulb@24 | 195 | if not self.constants.has_key(const.value): |
paulb@101 | 196 | self.constants[const.value] = Constant(name=repr(const.value), value=const.value) |
paulb@79 | 197 | result = LoadRef(const, 1, ref=self.constants[const.value]) |
paul@0 | 198 | return result |
paul@0 | 199 | |
paul@0 | 200 | def visitReturn(self, return_): |
paulb@79 | 201 | result = Return(return_, 1, |
paulb@56 | 202 | expr=self.dispatch(return_.value) |
paulb@56 | 203 | ) |
paul@0 | 204 | return result |
paul@0 | 205 | |
paul@0 | 206 | def visitBreak(self, break_): |
paulb@79 | 207 | result = Return(break_, 1) |
paul@0 | 208 | return result |
paul@0 | 209 | |
paul@0 | 210 | def visitContinue(self, continue_): |
paulb@79 | 211 | result = InvokeBlock(continue_, 1, |
paulb@101 | 212 | expr=LoadRef(ref=self.current_subprograms[-1]) |
paulb@56 | 213 | ) |
paul@0 | 214 | return result |
paul@0 | 215 | |
paul@5 | 216 | def visitRaise(self, raise_): |
paulb@112 | 217 | result = Raise(raise_, 1) |
paulb@112 | 218 | if raise_.expr2 is None: |
paulb@112 | 219 | result.expr = self.dispatch(raise_.expr1) |
paulb@112 | 220 | else: |
paulb@112 | 221 | result.expr = InvokeFunction( |
paulb@112 | 222 | expr=self.dispatch(raise_.expr1), |
paulb@112 | 223 | args=[self.dispatch(raise_.expr2)], |
paulb@112 | 224 | star=None, |
paulb@112 | 225 | dstar=None |
paulb@112 | 226 | ) |
paul@5 | 227 | if raise_.expr3 is not None: |
paul@5 | 228 | result.traceback = self.dispatch(raise_.expr3) |
paulb@112 | 229 | else: |
paulb@112 | 230 | result.traceback = None |
paul@5 | 231 | return result |
paul@5 | 232 | |
paul@0 | 233 | def _visitBuiltin(self, builtin, name): |
paulb@101 | 234 | result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) |
paul@0 | 235 | return result |
paul@0 | 236 | |
paul@0 | 237 | def visitTuple(self, tuple): |
paulb@24 | 238 | return self._visitBuiltin(tuple, "tuple") |
paul@0 | 239 | |
paul@0 | 240 | def visitList(self, list): |
paulb@24 | 241 | return self._visitBuiltin(list, "list") |
paul@0 | 242 | |
paul@0 | 243 | def visitDict(self, dict): |
paulb@101 | 244 | result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) |
paul@0 | 245 | args = [] |
paul@0 | 246 | for key, value in dict.items: |
paulb@101 | 247 | tuple = InvokeFunction(expr=LoadName(name="tuple"), star=None, dstar=None) |
paulb@67 | 248 | tuple.set_args([self.dispatch(key), self.dispatch(value)]) |
paul@0 | 249 | args.append(tuple) |
paulb@67 | 250 | result.set_args(args) |
paul@0 | 251 | return result |
paul@0 | 252 | |
paulb@17 | 253 | # Logical and comparison operations plus chained statements. |
paulb@17 | 254 | |
paulb@17 | 255 | def visitIf(self, if_): |
paulb@17 | 256 | |
paulb@26 | 257 | """ |
paulb@35 | 258 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@35 | 259 | and putting each subsequent test as part of the conditional's else |
paulb@35 | 260 | section. |
paulb@35 | 261 | |
paulb@35 | 262 | Convert... |
paulb@26 | 263 | |
paulb@35 | 264 | If (test/body) |
paulb@35 | 265 | (test/body) |
paulb@35 | 266 | ... |
paulb@35 | 267 | (else/body) |
paulb@35 | 268 | |
paulb@35 | 269 | ...to: |
paulb@35 | 270 | |
paulb@35 | 271 | Conditional (test) -> (body) |
paulb@35 | 272 | (else) -> Conditional (test) -> (body) |
paulb@35 | 273 | (else) -> ... |
paulb@26 | 274 | """ |
paulb@26 | 275 | |
paulb@17 | 276 | |
paulb@35 | 277 | results = nodes = [] |
paulb@17 | 278 | |
paulb@59 | 279 | # Produce something like... |
paulb@130 | 280 | # expr.__bool__() ? body |
paulb@59 | 281 | |
paulb@79 | 282 | first = 1 |
paulb@17 | 283 | for compare, stmt in if_.tests: |
paulb@79 | 284 | |
paulb@79 | 285 | # Set the first as the defining node. |
paulb@79 | 286 | |
paulb@79 | 287 | test = Conditional(if_, first, |
paulb@101 | 288 | test=InvokeFunction( |
paulb@101 | 289 | expr=LoadAttr( |
paulb@59 | 290 | expr=self.dispatch(compare), |
paulb@130 | 291 | name="__bool__" |
paulb@59 | 292 | ), |
paulb@59 | 293 | args=[], |
paulb@59 | 294 | star=None, |
paulb@59 | 295 | dstar=None) |
paulb@59 | 296 | ) |
paulb@35 | 297 | test.body = self.dispatch(stmt) |
paulb@17 | 298 | nodes.append(test) |
paulb@35 | 299 | nodes = test.else_ = [] |
paulb@79 | 300 | first = 0 |
paulb@17 | 301 | |
paulb@17 | 302 | # Add the compound statement from any else clause to the end. |
paulb@17 | 303 | |
paulb@17 | 304 | if if_.else_ is not None: |
paulb@35 | 305 | nodes += self.dispatch(if_.else_) |
paulb@34 | 306 | |
paulb@35 | 307 | result = results[0] |
paulb@17 | 308 | return result |
paulb@17 | 309 | |
paulb@35 | 310 | def visitTryExcept(self, tryexcept): |
paulb@35 | 311 | |
paulb@35 | 312 | """ |
paulb@35 | 313 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@35 | 314 | |
paulb@35 | 315 | Convert... |
paulb@17 | 316 | |
paulb@35 | 317 | TryExcept (body) |
paulb@35 | 318 | (else) |
paulb@35 | 319 | (spec/assign/stmt) |
paulb@35 | 320 | ... |
paulb@35 | 321 | |
paulb@35 | 322 | ...to: |
paulb@17 | 323 | |
paulb@35 | 324 | Try (body) |
paulb@35 | 325 | (else) |
paulb@35 | 326 | (handler) -> Conditional (test) -> (stmt) |
paulb@56 | 327 | (body) -> ... |
paulb@35 | 328 | (else) -> Conditional (test) -> (stmt) |
paulb@56 | 329 | (body) -> ... |
paulb@35 | 330 | (else) -> ... |
paulb@35 | 331 | """ |
paulb@35 | 332 | |
paulb@79 | 333 | result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) |
paulb@17 | 334 | |
paulb@35 | 335 | if tryexcept.body is not None: |
paulb@35 | 336 | result.body = self.dispatch(tryexcept.body) |
paulb@35 | 337 | if tryexcept.else_ is not None: |
paulb@35 | 338 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@17 | 339 | |
paulb@35 | 340 | results = nodes = [] |
paulb@118 | 341 | catch_all = 0 |
paulb@118 | 342 | |
paulb@17 | 343 | for spec, assign, stmt in tryexcept.handlers: |
paulb@17 | 344 | |
paulb@17 | 345 | # If no specification exists, produce an unconditional block. |
paulb@17 | 346 | |
paulb@17 | 347 | if spec is None: |
paulb@17 | 348 | nodes += self.dispatch(stmt) |
paulb@118 | 349 | catch_all = 1 |
paulb@17 | 350 | |
paulb@17 | 351 | # Produce something like... |
paulb@17 | 352 | # isinstance(<exc>, <spec>) |
paulb@17 | 353 | |
paulb@17 | 354 | else: |
paulb@17 | 355 | new_spec = self.dispatch(spec) |
paulb@101 | 356 | test = Conditional( |
paulb@101 | 357 | test=InvokeFunction( |
paulb@101 | 358 | expr=LoadName(name="isinstance"), |
paulb@101 | 359 | args=[LoadExc(), new_spec], |
paulb@59 | 360 | star=None, |
paulb@59 | 361 | dstar=None) |
paulb@59 | 362 | ) |
paulb@35 | 363 | test.body = [] |
paulb@35 | 364 | |
paulb@17 | 365 | if assign is not None: |
paulb@59 | 366 | test.body.append( |
paulb@101 | 367 | Assign( |
paulb@80 | 368 | code=[ |
paulb@101 | 369 | StoreTemp(expr=LoadExc()), |
paulb@80 | 370 | self.dispatch(assign), |
paulb@101 | 371 | ReleaseTemp() |
paulb@80 | 372 | ] |
paulb@80 | 373 | ) |
paulb@59 | 374 | ) |
paulb@34 | 375 | |
paulb@34 | 376 | # Always return from conditional sections. |
paulb@34 | 377 | |
paulb@101 | 378 | test.body += self.dispatch(stmt) + [Return()] |
paulb@17 | 379 | nodes.append(test) |
paulb@35 | 380 | nodes = test.else_ = [] |
paulb@17 | 381 | |
paulb@17 | 382 | # Add a raise operation to deal with unhandled exceptions. |
paulb@17 | 383 | |
paulb@118 | 384 | if not catch_all: |
paulb@118 | 385 | nodes.append( |
paulb@118 | 386 | Raise( |
paulb@118 | 387 | expr=LoadExc()) |
paulb@118 | 388 | ) |
paulb@17 | 389 | |
paulb@35 | 390 | result.handler = results |
paulb@17 | 391 | return result |
paulb@13 | 392 | |
paulb@13 | 393 | comparison_methods = { |
paulb@13 | 394 | "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__", |
paulb@13 | 395 | ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None |
paulb@13 | 396 | } |
paulb@13 | 397 | |
paulb@13 | 398 | def visitCompare(self, compare): |
paulb@13 | 399 | |
paulb@35 | 400 | """ |
paulb@35 | 401 | Make a subprogram for the 'compare' node and record its contents inside |
paulb@35 | 402 | the subprogram. Convert... |
paulb@35 | 403 | |
paulb@35 | 404 | Compare (expr) |
paulb@35 | 405 | (name/node) |
paulb@35 | 406 | ... |
paulb@35 | 407 | |
paulb@35 | 408 | ...to: |
paulb@35 | 409 | |
paulb@102 | 410 | InvokeBlock -> Subprogram -> Conditional (test) -> (body) |
paulb@102 | 411 | (else) -> Conditional (test) -> (body) |
paulb@102 | 412 | (else) -> ... |
paulb@35 | 413 | """ |
paulb@13 | 414 | |
paulb@118 | 415 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@13 | 416 | self.current_subprograms.append(subprogram) |
paulb@13 | 417 | |
paulb@13 | 418 | # In the subprogram, make instructions which invoke a method on the |
paulb@13 | 419 | # first operand of each operand pair and, if appropriate, return with |
paulb@13 | 420 | # the value from that method. |
paulb@13 | 421 | |
paulb@13 | 422 | last = compare.ops[-1] |
paulb@13 | 423 | previous = self.dispatch(compare.expr) |
paulb@35 | 424 | results = nodes = [] |
paulb@35 | 425 | |
paulb@102 | 426 | # For viewing purposes, record invocations on the AST node. |
paulb@102 | 427 | |
paulb@102 | 428 | compare._ops = [] |
paulb@102 | 429 | |
paulb@13 | 430 | for op in compare.ops: |
paulb@13 | 431 | op_name, node = op |
paulb@13 | 432 | expr = self.dispatch(node) |
paulb@35 | 433 | |
paulb@35 | 434 | # Identify the operation and produce the appropriate method call. |
paulb@35 | 435 | |
paulb@13 | 436 | method_name = self.comparison_methods[op_name] |
paulb@13 | 437 | if method_name: |
paulb@101 | 438 | invocation = InvokeFunction( |
paulb@101 | 439 | expr=LoadAttr( |
paulb@71 | 440 | expr=previous, |
paulb@71 | 441 | name=method_name), |
paulb@71 | 442 | args=[expr], |
paulb@71 | 443 | star=None, |
paulb@71 | 444 | dstar=None) |
paulb@71 | 445 | |
paulb@13 | 446 | elif op_name == "is": |
paulb@101 | 447 | invocation = InvokeFunction( |
paulb@101 | 448 | expr=LoadName(name="__is__"), |
paulb@71 | 449 | args=[previous, expr], |
paulb@71 | 450 | star=None, |
paulb@71 | 451 | dstar=None) |
paulb@71 | 452 | |
paulb@13 | 453 | elif op_name == "is not": |
paulb@129 | 454 | invocation = self._visitNot( |
paulb@129 | 455 | InvokeFunction( |
paulb@101 | 456 | expr=LoadName(name="__is__"), |
paulb@71 | 457 | args=[previous, expr], |
paulb@71 | 458 | star=None, |
paulb@71 | 459 | dstar=None) |
paulb@71 | 460 | ) |
paulb@13 | 461 | else: |
paulb@13 | 462 | raise NotImplementedError, op_name |
paulb@71 | 463 | |
paulb@101 | 464 | nodes.append(StoreTemp(expr=invocation)) |
paulb@102 | 465 | compare._ops.append(invocation) |
paulb@34 | 466 | |
paulb@35 | 467 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 468 | |
paulb@13 | 469 | if op is not last: |
paulb@71 | 470 | nodes.append( |
paulb@101 | 471 | Conditional( |
paulb@129 | 472 | test=self._visitNot(LoadTemp()), |
paulb@101 | 473 | body=[Return(expr=LoadTemp())]) |
paulb@71 | 474 | ) |
paulb@35 | 475 | |
paulb@35 | 476 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 477 | |
paulb@102 | 478 | nodes[-1].else_ = [ReleaseTemp()] |
paulb@102 | 479 | nodes = nodes[-1].else_ |
paulb@35 | 480 | |
paulb@35 | 481 | # For the last operation, return the result. |
paulb@35 | 482 | |
paulb@13 | 483 | else: |
paulb@71 | 484 | nodes.append( |
paulb@101 | 485 | Return(expr=LoadTemp(release=1)) |
paulb@71 | 486 | ) |
paulb@35 | 487 | |
paulb@15 | 488 | previous = expr |
paulb@35 | 489 | |
paulb@35 | 490 | # Finish the subprogram definition. |
paulb@35 | 491 | |
paulb@35 | 492 | subprogram.code = results |
paulb@13 | 493 | |
paulb@13 | 494 | self.current_subprograms.pop() |
paulb@79 | 495 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@13 | 496 | |
paulb@13 | 497 | # Make an invocation of the subprogram. |
paulb@13 | 498 | |
paulb@79 | 499 | result = InvokeBlock(compare, 1, produces_result=1) |
paulb@101 | 500 | result.expr = LoadRef(ref=subprogram) |
paulb@13 | 501 | return result |
paul@1 | 502 | |
paul@1 | 503 | def visitAnd(self, and_): |
paulb@7 | 504 | |
paulb@35 | 505 | """ |
paulb@35 | 506 | Make a subprogram for the 'and_' node and record its contents inside the |
paulb@35 | 507 | subprogram. Convert... |
paulb@35 | 508 | |
paulb@35 | 509 | And (test) |
paulb@35 | 510 | (test) |
paulb@35 | 511 | ... |
paulb@35 | 512 | |
paulb@35 | 513 | ...to: |
paulb@35 | 514 | |
paulb@35 | 515 | Subprogram -> Conditional (test) -> Return ... |
paulb@35 | 516 | (else) -> Conditional (test) -> Return ... |
paulb@35 | 517 | (else) -> ... |
paulb@35 | 518 | """ |
paulb@7 | 519 | |
paulb@118 | 520 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 521 | self.current_subprograms.append(subprogram) |
paulb@7 | 522 | |
paulb@12 | 523 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 524 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 525 | # subprogram with the value of the operand. |
paulb@12 | 526 | |
paulb@7 | 527 | last = and_.nodes[-1] |
paulb@35 | 528 | results = nodes = [] |
paulb@35 | 529 | |
paul@1 | 530 | for node in and_.nodes: |
paulb@7 | 531 | expr = self.dispatch(node) |
paulb@34 | 532 | |
paulb@35 | 533 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 534 | |
paulb@7 | 535 | if node is not last: |
paulb@101 | 536 | nodes.append(StoreTemp(expr=expr)) |
paulb@130 | 537 | #invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__"), args=[], star=None, dstar=None) |
paulb@129 | 538 | test = Conditional(test=self._visitNot(LoadTemp()), body=[Return(expr=LoadTemp())]) |
paulb@35 | 539 | nodes.append(test) |
paulb@35 | 540 | |
paulb@35 | 541 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 542 | |
paulb@101 | 543 | nodes = test.else_ = [ReleaseTemp()] |
paulb@35 | 544 | |
paulb@35 | 545 | # For the last operation, return the result. |
paulb@35 | 546 | |
paulb@7 | 547 | else: |
paulb@101 | 548 | nodes.append(Return(expr=expr)) |
paulb@35 | 549 | |
paulb@35 | 550 | # Finish the subprogram definition. |
paulb@35 | 551 | |
paulb@35 | 552 | subprogram.code = results |
paulb@7 | 553 | |
paulb@7 | 554 | self.current_subprograms.pop() |
paulb@79 | 555 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@7 | 556 | |
paulb@7 | 557 | # Make an invocation of the subprogram. |
paulb@7 | 558 | |
paulb@79 | 559 | result = InvokeBlock(and_, 1, produces_result=1) |
paulb@101 | 560 | result.expr = LoadRef(ref=subprogram) |
paul@1 | 561 | return result |
paul@1 | 562 | |
paul@1 | 563 | def visitOr(self, or_): |
paulb@7 | 564 | |
paulb@35 | 565 | """ |
paulb@35 | 566 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@35 | 567 | subprogram. Convert... |
paulb@35 | 568 | |
paulb@35 | 569 | Or (test) |
paulb@35 | 570 | (test) |
paulb@35 | 571 | ... |
paulb@35 | 572 | |
paulb@35 | 573 | ...to: |
paulb@35 | 574 | |
paulb@35 | 575 | Subprogram -> Conditional (test) -> Return ... |
paulb@35 | 576 | (else) -> Conditional (test) -> Return ... |
paulb@35 | 577 | (else) -> ... |
paulb@35 | 578 | """ |
paulb@7 | 579 | |
paulb@118 | 580 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 581 | self.current_subprograms.append(subprogram) |
paulb@7 | 582 | |
paulb@12 | 583 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 584 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 585 | # subprogram with the value of the operand. |
paulb@12 | 586 | |
paulb@7 | 587 | last = or_.nodes[-1] |
paulb@35 | 588 | results = nodes = [] |
paulb@35 | 589 | |
paul@1 | 590 | for node in or_.nodes: |
paulb@7 | 591 | expr = self.dispatch(node) |
paulb@34 | 592 | |
paulb@35 | 593 | # Return from the subprogram where the test is satisfied. |
paulb@34 | 594 | |
paulb@7 | 595 | if node is not last: |
paulb@101 | 596 | nodes.append(StoreTemp(expr=expr)) |
paulb@130 | 597 | invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__"), args=[], star=None, dstar=None) |
paulb@101 | 598 | test = Conditional(test=invocation, body=[Return(expr=LoadTemp())]) |
paulb@35 | 599 | nodes.append(test) |
paulb@35 | 600 | |
paulb@35 | 601 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 602 | |
paulb@101 | 603 | nodes = test.else_ = [ReleaseTemp()] |
paulb@35 | 604 | |
paulb@35 | 605 | # For the last operation, return the result. |
paulb@35 | 606 | |
paulb@7 | 607 | else: |
paulb@59 | 608 | nodes.append( |
paulb@101 | 609 | Return(expr=expr) |
paulb@59 | 610 | ) |
paulb@35 | 611 | |
paulb@35 | 612 | # Finish the subprogram definition. |
paulb@35 | 613 | |
paulb@35 | 614 | subprogram.code = results |
paulb@7 | 615 | |
paulb@7 | 616 | self.current_subprograms.pop() |
paulb@79 | 617 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@7 | 618 | |
paulb@7 | 619 | # Make an invocation of the subprogram. |
paulb@7 | 620 | |
paulb@79 | 621 | result = InvokeBlock(or_, 1, produces_result=1) |
paulb@101 | 622 | result.expr = LoadRef(ref=subprogram) |
paul@1 | 623 | return result |
paul@1 | 624 | |
paulb@129 | 625 | def _visitNot(self, expr, not_=None): |
paulb@129 | 626 | invocation = InvokeFunction( |
paulb@129 | 627 | expr=LoadAttr( |
paulb@129 | 628 | expr=expr, |
paulb@130 | 629 | name="__bool__" |
paulb@129 | 630 | ), |
paulb@129 | 631 | args=[], |
paulb@129 | 632 | star=None, |
paulb@129 | 633 | dstar=None |
paulb@129 | 634 | ) |
paulb@129 | 635 | if not_ is not None: |
paulb@129 | 636 | result = Not(not_, 1, expr=invocation) |
paulb@129 | 637 | else: |
paulb@129 | 638 | result = Not(expr=invocation) |
paulb@129 | 639 | return result |
paulb@129 | 640 | |
paul@1 | 641 | def visitNot(self, not_): |
paulb@129 | 642 | return self._visitNot(self.dispatch(not_.expr), not_) |
paul@1 | 643 | |
paul@4 | 644 | # Operators. |
paul@4 | 645 | |
paul@4 | 646 | def visitUnaryAdd(self, unaryadd): |
paulb@92 | 647 | return InvokeFunction(unaryadd, 1, |
paulb@101 | 648 | expr=LoadAttr( |
paulb@92 | 649 | expr=self.dispatch(unaryadd.expr), |
paulb@92 | 650 | name="__pos__" |
paulb@92 | 651 | ), |
paulb@92 | 652 | args=[], |
paulb@92 | 653 | star=None, |
paulb@92 | 654 | dstar=None |
paulb@92 | 655 | ) |
paul@4 | 656 | |
paul@4 | 657 | def visitUnarySub(self, unarysub): |
paulb@92 | 658 | return InvokeFunction(unarysub, 1, |
paulb@101 | 659 | expr=LoadAttr( |
paulb@92 | 660 | expr=self.dispatch(unarysub.expr), |
paulb@92 | 661 | name="__neg__" |
paulb@92 | 662 | ), |
paulb@92 | 663 | args=[], |
paulb@92 | 664 | star=None, |
paulb@92 | 665 | dstar=None |
paulb@92 | 666 | ) |
paul@4 | 667 | |
paul@4 | 668 | def visitInvert(self, invert): |
paulb@92 | 669 | return InvokeFunction(invert, 1, |
paulb@101 | 670 | expr=LoadAttr( |
paulb@92 | 671 | expr=self.dispatch(invert.expr), |
paulb@92 | 672 | name="__invert__" |
paulb@92 | 673 | ), |
paulb@92 | 674 | args=[], |
paulb@92 | 675 | star=None, |
paulb@92 | 676 | dstar=None |
paulb@92 | 677 | ) |
paul@4 | 678 | |
paulb@32 | 679 | def visitAdd(self, add): |
paulb@32 | 680 | |
paulb@56 | 681 | """ |
paulb@56 | 682 | Emulate the current mechanisms by producing nodes as follows: |
paulb@56 | 683 | |
paulb@56 | 684 | Try (body) -> x.__add__(y) |
paulb@56 | 685 | (else) |
paulb@56 | 686 | (handler) -> Conditional (test) -> isinstance(exc, TypeError) |
paulb@56 | 687 | (body) -> y.__radd__(x) |
paulb@56 | 688 | (else) |
paulb@56 | 689 | """ |
paulb@32 | 690 | |
paulb@79 | 691 | result = Try(add, 1, |
paulb@56 | 692 | body=[ |
paulb@101 | 693 | InvokeFunction( |
paulb@101 | 694 | expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), |
paulb@59 | 695 | args=[self.dispatch(add.right)], |
paulb@59 | 696 | star=None, |
paulb@59 | 697 | dstar=None) |
paulb@56 | 698 | ], |
paulb@56 | 699 | else_=[], |
paulb@56 | 700 | finally_=[], |
paulb@56 | 701 | handler=[ |
paulb@101 | 702 | Conditional( |
paulb@101 | 703 | test=InvokeFunction( |
paulb@101 | 704 | expr=LoadName(name="isinstance"), |
paulb@101 | 705 | args=[LoadExc(), LoadName(name="TypeError")], |
paulb@59 | 706 | star=None, |
paulb@59 | 707 | dstar=None), |
paulb@56 | 708 | body=[ |
paulb@101 | 709 | InvokeFunction( |
paulb@101 | 710 | expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), |
paulb@59 | 711 | args=[self.dispatch(add.left)], |
paulb@59 | 712 | star=None, |
paulb@59 | 713 | dstar=None) |
paulb@56 | 714 | ], |
paulb@56 | 715 | else_=[] |
paulb@56 | 716 | ) |
paulb@56 | 717 | ] |
paulb@56 | 718 | ) |
paulb@56 | 719 | |
paulb@32 | 720 | return result |
paulb@32 | 721 | |
paul@0 | 722 | # Assignments. |
paul@0 | 723 | |
paul@2 | 724 | augassign_methods = { |
paulb@9 | 725 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", |
paulb@9 | 726 | "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", |
paulb@9 | 727 | "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" |
paul@2 | 728 | } |
paul@2 | 729 | |
paul@2 | 730 | def visitAugAssign(self, augassign): |
paulb@92 | 731 | |
paulb@92 | 732 | """ |
paulb@92 | 733 | Convert the augmented assignment... |
paulb@92 | 734 | |
paulb@92 | 735 | AugAssign (node) -> Name | Getattr | Slice | Subscript |
paulb@92 | 736 | (op) |
paulb@92 | 737 | (expr) |
paulb@92 | 738 | |
paulb@92 | 739 | ...to: |
paulb@92 | 740 | |
paulb@92 | 741 | Assign (code) -> StoreTemp (expr) -> InvokeFunction (expr) -> LoadAttr (expr) -> <name> |
paulb@92 | 742 | (name) -> <op> |
paulb@92 | 743 | StoreName (name) -> <name> |
paulb@92 | 744 | (expr) -> LoadTemp |
paulb@92 | 745 | ReleaseTemp |
paulb@92 | 746 | """ |
paulb@92 | 747 | |
paulb@79 | 748 | result = Assign(augassign, 1) |
paul@2 | 749 | expr = self.dispatch(augassign.expr) |
paul@2 | 750 | |
paul@2 | 751 | # Simple augmented assignment: name += expr |
paul@2 | 752 | |
paul@2 | 753 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@56 | 754 | result.code = [ |
paulb@101 | 755 | StoreTemp( |
paulb@101 | 756 | expr=InvokeFunction( |
paulb@59 | 757 | args=[expr], |
paulb@59 | 758 | star=None, |
paulb@59 | 759 | dstar=None, |
paulb@101 | 760 | expr=LoadAttr( |
paulb@56 | 761 | expr=self.dispatch(augassign.node), |
paulb@56 | 762 | name=self.augassign_methods[augassign.op] |
paulb@56 | 763 | ) |
paulb@56 | 764 | ) |
paulb@56 | 765 | ), |
paulb@101 | 766 | StoreName( |
paulb@101 | 767 | expr=LoadTemp(), |
paulb@56 | 768 | name=augassign.node.name), |
paulb@101 | 769 | ReleaseTemp() |
paulb@56 | 770 | ] |
paul@2 | 771 | |
paulb@9 | 772 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 773 | |
paul@2 | 774 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 775 | |
paulb@9 | 776 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 777 | |
paulb@56 | 778 | result.code = [ |
paulb@101 | 779 | StoreTemp( |
paulb@56 | 780 | index="expr", |
paulb@56 | 781 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 782 | ), |
paulb@101 | 783 | StoreTemp( |
paulb@101 | 784 | expr=InvokeFunction( |
paulb@56 | 785 | args=[expr], star=None, dstar=None, |
paulb@101 | 786 | expr=LoadAttr( |
paulb@92 | 787 | expr=LoadAttr(augassign.node, 1, |
paulb@101 | 788 | expr=LoadTemp(index="expr"), |
paulb@56 | 789 | name=augassign.node.attrname |
paulb@56 | 790 | ), |
paulb@56 | 791 | name=self.augassign_methods[augassign.op] |
paulb@56 | 792 | ) |
paulb@56 | 793 | ) |
paulb@56 | 794 | ), |
paulb@101 | 795 | StoreAttr( |
paulb@101 | 796 | expr=LoadTemp(), |
paulb@101 | 797 | lvalue=LoadTemp(index="expr"), |
paulb@56 | 798 | name=augassign.node.attrname |
paulb@56 | 799 | ), |
paulb@101 | 800 | ReleaseTemp(index="expr"), |
paulb@101 | 801 | ReleaseTemp() |
paulb@56 | 802 | ] |
paulb@6 | 803 | |
paulb@9 | 804 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 805 | |
paulb@6 | 806 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 807 | |
paulb@9 | 808 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 809 | |
paulb@56 | 810 | result.code = [ |
paulb@101 | 811 | StoreTemp( |
paulb@56 | 812 | index="expr", |
paulb@56 | 813 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 814 | ), |
paulb@101 | 815 | StoreTemp( |
paulb@56 | 816 | index="lower", |
paulb@56 | 817 | expr=self.dispatch_or_none(augassign.node.lower) |
paulb@56 | 818 | ), |
paulb@101 | 819 | StoreTemp( |
paulb@56 | 820 | index="upper", |
paulb@56 | 821 | expr=self.dispatch_or_none(augassign.node.upper) |
paulb@56 | 822 | ), |
paulb@101 | 823 | StoreTemp( |
paulb@101 | 824 | expr=InvokeFunction( |
paulb@56 | 825 | args=[expr], star=None, dstar=None, |
paulb@101 | 826 | expr=LoadAttr( |
paulb@59 | 827 | expr=self._visitSlice( |
paulb@59 | 828 | augassign.node, |
paulb@101 | 829 | LoadTemp(index="expr"), |
paulb@101 | 830 | LoadTemp(index="lower"), |
paulb@101 | 831 | LoadTemp(index="upper"), |
paulb@59 | 832 | "OP_APPLY"), |
paulb@56 | 833 | name=self.augassign_methods[augassign.op] |
paulb@56 | 834 | ) |
paulb@56 | 835 | ) |
paulb@56 | 836 | ), |
paulb@59 | 837 | self._visitSlice( |
paulb@59 | 838 | augassign.node, |
paulb@101 | 839 | LoadTemp(index="expr"), |
paulb@101 | 840 | LoadTemp(index="lower"), |
paulb@101 | 841 | LoadTemp(index="upper"), |
paulb@59 | 842 | "OP_ASSIGN", |
paulb@101 | 843 | LoadTemp() |
paulb@59 | 844 | ), |
paulb@101 | 845 | ReleaseTemp(index="expr"), |
paulb@101 | 846 | ReleaseTemp(index="lower"), |
paulb@101 | 847 | ReleaseTemp(index="upper"), |
paulb@101 | 848 | ReleaseTemp() |
paulb@56 | 849 | ] |
paul@2 | 850 | |
paulb@10 | 851 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 852 | |
paulb@10 | 853 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 854 | |
paulb@10 | 855 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 856 | |
paulb@56 | 857 | result.code = [ |
paulb@101 | 858 | StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), |
paulb@101 | 859 | StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), |
paulb@101 | 860 | StoreTemp( |
paulb@101 | 861 | expr=InvokeFunction( |
paulb@56 | 862 | args=[expr], star=None, dstar=None, |
paulb@101 | 863 | expr=LoadAttr( |
paulb@59 | 864 | expr=self._visitSubscript( |
paulb@59 | 865 | augassign.node, |
paulb@101 | 866 | LoadTemp(index="expr"), |
paulb@101 | 867 | LoadTemp(index="subs"), |
paulb@59 | 868 | "OP_APPLY" |
paulb@59 | 869 | ), |
paulb@56 | 870 | name=self.augassign_methods[augassign.op] |
paulb@56 | 871 | ) |
paulb@56 | 872 | ) |
paulb@56 | 873 | ), |
paulb@59 | 874 | self._visitSubscript( |
paulb@59 | 875 | augassign.node, |
paulb@101 | 876 | LoadTemp(index="expr"), |
paulb@101 | 877 | LoadTemp(index="subs"), |
paulb@59 | 878 | "OP_ASSIGN", |
paulb@101 | 879 | LoadTemp() |
paulb@59 | 880 | ), |
paulb@101 | 881 | ReleaseTemp(index="expr"), |
paulb@101 | 882 | ReleaseTemp(index="subs"), |
paulb@101 | 883 | ReleaseTemp() |
paulb@56 | 884 | ] |
paulb@10 | 885 | |
paul@2 | 886 | else: |
paul@2 | 887 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 888 | |
paul@2 | 889 | return result |
paul@2 | 890 | |
paul@0 | 891 | def visitAssign(self, assign): |
paulb@79 | 892 | result = Assign(assign, 1) |
paulb@101 | 893 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paulb@101 | 894 | release = ReleaseTemp() |
paul@0 | 895 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paul@0 | 896 | return result |
paul@0 | 897 | |
paul@0 | 898 | def visitAssList(self, asslist, in_sequence=0): |
paul@0 | 899 | if not in_sequence: |
paulb@101 | 900 | expr = LoadTemp() |
paul@0 | 901 | else: |
paulb@101 | 902 | expr = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) |
paulb@79 | 903 | result = Assign(asslist, 1) |
paulb@101 | 904 | store = StoreTemp(expr=InvokeFunction(expr=LoadAttr(name="__iter__", expr=expr), star=None, dstar=None, args=[])) |
paulb@101 | 905 | release = ReleaseTemp() |
paul@0 | 906 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paul@0 | 907 | return result |
paul@0 | 908 | |
paul@0 | 909 | visitAssTuple = visitAssList |
paul@0 | 910 | |
paul@0 | 911 | def _visitAssNameOrAttr(self, node, in_sequence): |
paul@0 | 912 | if not in_sequence: |
paulb@101 | 913 | return LoadTemp() |
paul@0 | 914 | else: |
paulb@101 | 915 | return InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) |
paul@0 | 916 | |
paul@0 | 917 | def visitAssName(self, assname, in_sequence=0): |
paul@0 | 918 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paulb@79 | 919 | result = StoreName(assname, 1, name=assname.name, expr=expr) |
paul@0 | 920 | return result |
paul@0 | 921 | |
paul@0 | 922 | def visitAssAttr(self, assattr, in_sequence=0): |
paul@0 | 923 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paul@0 | 924 | lvalue = self.dispatch(assattr.expr) |
paulb@79 | 925 | result = StoreAttr(assattr, 1, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@0 | 926 | return result |
paul@0 | 927 | |
paulb@11 | 928 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@6 | 929 | if flags == "OP_ASSIGN": |
paulb@100 | 930 | result = InvokeFunction(slice, 1, |
paulb@101 | 931 | expr=LoadAttr( |
paulb@100 | 932 | expr=expr, |
paulb@100 | 933 | name="__setslice__" |
paulb@100 | 934 | ), |
paulb@100 | 935 | star=None, |
paulb@100 | 936 | dstar=None, |
paulb@100 | 937 | args=[lower, upper, value] |
paulb@100 | 938 | ) |
paulb@6 | 939 | elif flags == "OP_APPLY": |
paulb@6 | 940 | args = [] |
paulb@100 | 941 | result = InvokeFunction(slice, 1, |
paulb@101 | 942 | expr=LoadAttr( |
paulb@100 | 943 | expr=expr, |
paulb@100 | 944 | name="__getslice__" |
paulb@100 | 945 | ), |
paulb@100 | 946 | star=None, |
paulb@100 | 947 | dstar=None, |
paulb@100 | 948 | args=[lower, upper] |
paulb@100 | 949 | ) |
paulb@10 | 950 | elif flags == "OP_DELETE": |
paulb@10 | 951 | args = [] |
paulb@100 | 952 | result = InvokeFunction(slice, 1, |
paulb@101 | 953 | expr=LoadAttr( |
paulb@100 | 954 | expr=expr, |
paulb@100 | 955 | name="__delslice__" |
paulb@100 | 956 | ), |
paulb@100 | 957 | star=None, |
paulb@100 | 958 | dstar=None, |
paulb@100 | 959 | args=[lower, upper] |
paulb@100 | 960 | ) |
paulb@6 | 961 | else: |
paulb@6 | 962 | raise NotImplementedError, flags |
paulb@6 | 963 | |
paulb@6 | 964 | return result |
paulb@6 | 965 | |
paulb@6 | 966 | def visitSlice(self, slice, in_sequence=0): |
paulb@11 | 967 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@56 | 968 | self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) |
paulb@6 | 969 | |
paulb@11 | 970 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@10 | 971 | if flags == "OP_ASSIGN": |
paulb@100 | 972 | result = InvokeFunction(subscript, 1, |
paulb@101 | 973 | expr=LoadAttr( |
paulb@100 | 974 | expr=expr, |
paulb@100 | 975 | name="__setitem__" |
paulb@100 | 976 | ), |
paulb@100 | 977 | star=None, |
paulb@100 | 978 | dstar=None, |
paulb@100 | 979 | args=[subs, value] |
paulb@100 | 980 | ) |
paulb@10 | 981 | elif flags == "OP_APPLY": |
paulb@10 | 982 | args = [] |
paulb@100 | 983 | result = InvokeFunction(subscript, 1, |
paulb@101 | 984 | expr=LoadAttr( |
paulb@100 | 985 | expr=expr, |
paulb@100 | 986 | name="__getitem__" |
paulb@100 | 987 | ), |
paulb@100 | 988 | star=None, |
paulb@100 | 989 | dstar=None, |
paulb@100 | 990 | args=[subs] |
paulb@100 | 991 | ) |
paulb@10 | 992 | elif flags == "OP_DELETE": |
paulb@10 | 993 | args = [] |
paulb@100 | 994 | result = InvokeFunction(subscript, 1, |
paulb@101 | 995 | expr=LoadAttr( |
paulb@100 | 996 | expr=expr, |
paulb@100 | 997 | name="__delitem__" |
paulb@100 | 998 | ), |
paulb@100 | 999 | star=None, |
paulb@100 | 1000 | dstar=None, |
paulb@100 | 1001 | args=[subs] |
paulb@100 | 1002 | ) |
paulb@10 | 1003 | else: |
paulb@10 | 1004 | raise NotImplementedError, flags |
paulb@10 | 1005 | |
paulb@10 | 1006 | return result |
paulb@10 | 1007 | |
paulb@77 | 1008 | def _visitSubscriptSubs(self, node, subs): |
paulb@10 | 1009 | if len(subs) == 1: |
paulb@10 | 1010 | return self.dispatch(subs[0]) |
paulb@10 | 1011 | else: |
paulb@79 | 1012 | return InvokeFunction(node, 1, |
paulb@101 | 1013 | expr=LoadName(name="tuple"), |
paulb@77 | 1014 | args=self.dispatches(subs), |
paulb@77 | 1015 | star=None, |
paulb@100 | 1016 | dstar=None |
paulb@100 | 1017 | ) |
paulb@10 | 1018 | |
paulb@10 | 1019 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@56 | 1020 | return self._visitSubscript( |
paulb@77 | 1021 | subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, |
paulb@56 | 1022 | self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@56 | 1023 | ) |
paulb@10 | 1024 | |
paul@0 | 1025 | # Invocation and subprogram transformations. |
paul@0 | 1026 | |
paulb@19 | 1027 | def visitClass(self, class_): |
paulb@129 | 1028 | |
paulb@129 | 1029 | # Add "object" if the class is not "object" and has an empty bases list. |
paulb@129 | 1030 | |
paulb@129 | 1031 | if class_.name != "object" and not class_.bases: |
paulb@129 | 1032 | bases = [compiler.ast.Name("object")] |
paulb@129 | 1033 | else: |
paulb@129 | 1034 | bases = class_.bases |
paulb@129 | 1035 | |
paulb@129 | 1036 | structure = Class(name=class_.name, module=self.module, bases=self.dispatches(bases)) |
paulb@19 | 1037 | self.structures.append(structure) |
paulb@19 | 1038 | |
paulb@24 | 1039 | # Make a subprogram which initialises the class structure. |
paulb@24 | 1040 | |
paulb@118 | 1041 | subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 1042 | self.current_subprograms.append(subprogram) |
paulb@120 | 1043 | self.current_structures.append(structure) # mostly for name construction |
paulb@19 | 1044 | |
paulb@28 | 1045 | # The class is initialised using the code found inside. |
paulb@28 | 1046 | |
paulb@101 | 1047 | subprogram.code = self.dispatch(class_.code) + [Return()] |
paulb@19 | 1048 | |
paulb@120 | 1049 | self.current_structures.pop() |
paulb@19 | 1050 | self.current_subprograms.pop() |
paulb@79 | 1051 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@19 | 1052 | |
paulb@19 | 1053 | # Make a definition of the class associating it with a name. |
paulb@19 | 1054 | |
paulb@101 | 1055 | result = Assign( |
paulb@56 | 1056 | code=[ |
paulb@79 | 1057 | StoreName(class_, 1, # defines the class |
paulb@56 | 1058 | name=class_.name, |
paulb@101 | 1059 | expr=LoadRef(ref=structure) |
paulb@56 | 1060 | ), |
paulb@101 | 1061 | InvokeBlock( |
paulb@132 | 1062 | share_locals=0, # override the local sharing usually in InvokeBlock |
paulb@101 | 1063 | expr=LoadRef(ref=subprogram) |
paulb@56 | 1064 | ) |
paulb@56 | 1065 | ] |
paulb@56 | 1066 | ) |
paulb@19 | 1067 | return result |
paulb@19 | 1068 | |
paul@0 | 1069 | def _visitFunction(self, function, subprogram): |
paulb@46 | 1070 | |
paulb@46 | 1071 | """ |
paulb@46 | 1072 | A common function generator which transforms the given 'function' node |
paulb@46 | 1073 | and initialises the given 'subprogram' appropriately. |
paulb@46 | 1074 | """ |
paulb@46 | 1075 | |
paulb@46 | 1076 | # Discover star and dstar parameters. |
paulb@46 | 1077 | |
paulb@46 | 1078 | if function.flags & 4 != 0: |
paulb@46 | 1079 | has_star = 1 |
paulb@46 | 1080 | else: |
paulb@46 | 1081 | has_star = 0 |
paulb@46 | 1082 | if function.flags & 8 != 0: |
paulb@46 | 1083 | has_dstar = 1 |
paulb@46 | 1084 | else: |
paulb@46 | 1085 | has_dstar = 0 |
paulb@46 | 1086 | |
paulb@46 | 1087 | # Discover the number of defaults and positional parameters. |
paulb@46 | 1088 | |
paul@0 | 1089 | ndefaults = len(function.defaults) |
paul@0 | 1090 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@24 | 1091 | |
paulb@24 | 1092 | # Produce star and dstar parameters with appropriate defaults. |
paulb@24 | 1093 | |
paulb@24 | 1094 | if has_star: |
paulb@79 | 1095 | star = ( |
paulb@79 | 1096 | function.argnames[npositional], |
paulb@101 | 1097 | InvokeFunction(expr=LoadName(name="list"), args=[], star=None, dstar=None) |
paulb@79 | 1098 | ) |
paulb@24 | 1099 | else: |
paulb@24 | 1100 | star = None |
paulb@24 | 1101 | if has_dstar: |
paulb@79 | 1102 | dstar = ( |
paulb@79 | 1103 | function.argnames[npositional + has_star], |
paulb@101 | 1104 | InvokeFunction(expr=LoadName(name="dict"), args=[], star=None, dstar=None) |
paulb@79 | 1105 | ) |
paulb@24 | 1106 | else: |
paulb@24 | 1107 | dstar = None |
paul@0 | 1108 | |
paul@0 | 1109 | params = [] |
paul@0 | 1110 | for i in range(0, npositional - ndefaults): |
paul@0 | 1111 | params.append((function.argnames[i], None)) |
paul@0 | 1112 | |
paulb@100 | 1113 | # Process defaults. |
paul@0 | 1114 | |
paul@0 | 1115 | for i in range(0, ndefaults): |
paul@0 | 1116 | default = function.defaults[i] |
paul@0 | 1117 | if default is not None: |
paul@0 | 1118 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paul@0 | 1119 | else: |
paulb@100 | 1120 | params.append((function.argnames[npositional - ndefaults + i], None)) |
paul@0 | 1121 | |
paul@0 | 1122 | subprogram.params = params |
paul@0 | 1123 | subprogram.star = star |
paul@0 | 1124 | subprogram.dstar = dstar |
paulb@79 | 1125 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 1126 | |
paul@0 | 1127 | def visitFunction(self, function): |
paul@0 | 1128 | |
paulb@85 | 1129 | """ |
paulb@85 | 1130 | Make a subprogram for the 'function' and record it outside the main |
paulb@85 | 1131 | tree. Produce something like the following: |
paulb@85 | 1132 | |
paulb@85 | 1133 | StoreName (name) |
paulb@85 | 1134 | (expr) -> Subprogram (params) |
paulb@85 | 1135 | (star) |
paulb@85 | 1136 | (dstar) |
paulb@85 | 1137 | """ |
paul@0 | 1138 | |
paulb@120 | 1139 | subprogram = Subprogram(name=function.name, module=self.module, structures=self.current_structures[:], |
paulb@120 | 1140 | internal=0, returns_value=1, star=None, dstar=None) |
paulb@120 | 1141 | |
paul@0 | 1142 | self.current_subprograms.append(subprogram) |
paulb@101 | 1143 | subprogram.code = self.dispatch(function.code) + [Return()] |
paul@0 | 1144 | self.current_subprograms.pop() |
paul@0 | 1145 | self._visitFunction(function, subprogram) |
paul@0 | 1146 | |
paul@0 | 1147 | # Make a definition of the function associating it with a name. |
paul@0 | 1148 | |
paulb@101 | 1149 | result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) |
paul@0 | 1150 | return result |
paul@0 | 1151 | |
paul@0 | 1152 | def visitLambda(self, lambda_): |
paul@0 | 1153 | |
paul@0 | 1154 | # Make a subprogram for the function and record it outside the main |
paul@0 | 1155 | # tree. |
paul@0 | 1156 | |
paulb@118 | 1157 | subprogram = Subprogram(name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None) |
paul@0 | 1158 | self.current_subprograms.append(subprogram) |
paulb@101 | 1159 | subprogram.code = [Return(expr=self.dispatch(lambda_.code))] |
paul@0 | 1160 | self.current_subprograms.pop() |
paul@0 | 1161 | self._visitFunction(lambda_, subprogram) |
paul@0 | 1162 | |
paul@0 | 1163 | # Get the subprogram reference to the lambda. |
paul@0 | 1164 | |
paulb@79 | 1165 | return LoadRef(lambda_, 1, ref=subprogram) |
paul@0 | 1166 | |
paul@0 | 1167 | def visitCallFunc(self, callfunc): |
paulb@79 | 1168 | result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) |
paul@0 | 1169 | if callfunc.star_args is not None: |
paul@0 | 1170 | result.star = self.dispatch(callfunc.star_args) |
paul@0 | 1171 | if callfunc.dstar_args is not None: |
paul@0 | 1172 | result.dstar = self.dispatch(callfunc.dstar_args) |
paul@0 | 1173 | result.expr = self.dispatch(callfunc.node) |
paul@0 | 1174 | return result |
paul@0 | 1175 | |
paul@0 | 1176 | def visitWhile(self, while_): |
paul@0 | 1177 | |
paulb@35 | 1178 | """ |
paulb@35 | 1179 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@35 | 1180 | subprogram. Convert... |
paulb@35 | 1181 | |
paulb@35 | 1182 | While (test) -> (body) |
paulb@35 | 1183 | (else) |
paulb@35 | 1184 | |
paulb@35 | 1185 | ...to: |
paulb@35 | 1186 | |
paulb@35 | 1187 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@35 | 1188 | (else) -> Conditional (test) -> Return ... |
paulb@35 | 1189 | (else) -> ... |
paulb@35 | 1190 | """ |
paul@0 | 1191 | |
paulb@118 | 1192 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 1193 | self.current_subprograms.append(subprogram) |
paul@0 | 1194 | |
paul@0 | 1195 | # Include a conditional statement in the subprogram. |
paul@0 | 1196 | |
paulb@101 | 1197 | test = Conditional(else_=[]) |
paulb@130 | 1198 | test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__bool__"), args=[], star=None, dstar=None) |
paul@0 | 1199 | |
paul@0 | 1200 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 1201 | # if the test condition was satisfied. |
paul@0 | 1202 | |
paulb@101 | 1203 | continuation = InvokeBlock() |
paulb@101 | 1204 | continuation.expr = LoadRef(ref=subprogram) |
paulb@34 | 1205 | |
paulb@35 | 1206 | # Return within the main section of the loop. |
paulb@34 | 1207 | |
paulb@101 | 1208 | test.body = self.dispatch(while_.body) + [continuation, Return()] |
paulb@35 | 1209 | |
paulb@35 | 1210 | # Provide the else section, if present, along with an explicit return. |
paulb@35 | 1211 | |
paul@0 | 1212 | if while_.else_ is not None: |
paulb@101 | 1213 | test.else_ = self.dispatch(while_.else_) + [Return()] |
paulb@35 | 1214 | |
paulb@35 | 1215 | # Finish the subprogram definition. |
paulb@35 | 1216 | |
paulb@36 | 1217 | subprogram.code = [test] |
paul@0 | 1218 | |
paul@0 | 1219 | self.current_subprograms.pop() |
paulb@79 | 1220 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 1221 | |
paul@0 | 1222 | # Make an invocation of the subprogram. |
paul@0 | 1223 | |
paulb@79 | 1224 | result = InvokeBlock(while_, 1) |
paulb@101 | 1225 | result.expr = LoadRef(ref=subprogram) |
paul@0 | 1226 | return result |
paul@0 | 1227 | |
paul@0 | 1228 | def visitFor(self, for_): |
paul@0 | 1229 | |
paulb@35 | 1230 | """ |
paulb@35 | 1231 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 1232 | subprogram. Convert... |
paulb@35 | 1233 | |
paulb@35 | 1234 | For (assign) |
paulb@35 | 1235 | (body) |
paulb@35 | 1236 | (else) |
paulb@35 | 1237 | |
paulb@35 | 1238 | ...to: |
paulb@35 | 1239 | |
paulb@35 | 1240 | Assign (assign #1) |
paulb@35 | 1241 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 1242 | (body) |
paulb@35 | 1243 | Invoke subprogram |
paulb@35 | 1244 | (handler) -> ... |
paulb@35 | 1245 | (else) -> ... |
paulb@35 | 1246 | """ |
paul@0 | 1247 | |
paulb@118 | 1248 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 1249 | self.current_subprograms.append(subprogram) |
paul@0 | 1250 | |
paulb@34 | 1251 | # Always return from conditional sections/subprograms. |
paulb@34 | 1252 | |
paulb@17 | 1253 | if for_.else_ is not None: |
paulb@101 | 1254 | else_stmt = self.dispatch(for_.else_) + [Return()] |
paulb@17 | 1255 | else: |
paulb@101 | 1256 | else_stmt = [Return()] |
paulb@17 | 1257 | |
paul@0 | 1258 | # Wrap the assignment in a try...except statement. |
paul@0 | 1259 | |
paulb@101 | 1260 | try_except = Try(body=[], else_=[], finally_=[]) |
paulb@101 | 1261 | test = Conditional( |
paulb@101 | 1262 | test=InvokeFunction( |
paulb@101 | 1263 | expr=LoadName(name="isinstance"), |
paulb@101 | 1264 | args=[LoadExc(), LoadName(name="StopIteration")], |
paulb@67 | 1265 | star=None, |
paulb@67 | 1266 | dstar=None), |
paulb@35 | 1267 | body=else_stmt, |
paulb@101 | 1268 | else_=[Raise(expr=LoadExc())]) |
paulb@36 | 1269 | try_except.handler = [test] |
paul@0 | 1270 | |
paulb@101 | 1271 | assign = Assign( |
paulb@59 | 1272 | code=[ |
paulb@101 | 1273 | StoreTemp(expr=InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), args=[], star=None, dstar=None)), |
paulb@59 | 1274 | self.dispatch(for_.assign), |
paulb@101 | 1275 | ReleaseTemp() |
paulb@59 | 1276 | ]) |
paul@0 | 1277 | |
paul@0 | 1278 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 1279 | # if the test condition was satisfied. |
paul@0 | 1280 | |
paulb@112 | 1281 | continuation = InvokeBlock( |
paulb@112 | 1282 | expr=LoadRef(ref=subprogram) |
paulb@112 | 1283 | ) |
paul@0 | 1284 | try_except.body = [assign] + self.dispatch(for_.body) + [continuation] |
paulb@101 | 1285 | subprogram.code = [try_except, Return()] |
paul@0 | 1286 | |
paulb@35 | 1287 | # Finish the subprogram definition. |
paulb@35 | 1288 | |
paul@0 | 1289 | self.current_subprograms.pop() |
paulb@79 | 1290 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 1291 | |
paul@0 | 1292 | # Obtain an iterator for the sequence involved. |
paul@0 | 1293 | # Then, make an invocation of the subprogram. |
paul@0 | 1294 | |
paulb@79 | 1295 | result = Assign(for_, 1) |
paul@0 | 1296 | result.code = [ |
paulb@101 | 1297 | StoreTemp( |
paulb@101 | 1298 | expr=InvokeFunction( |
paulb@101 | 1299 | expr=LoadAttr( |
paulb@79 | 1300 | name="__iter__", |
paulb@79 | 1301 | expr=self.dispatch(for_.list) |
paulb@79 | 1302 | ), |
paulb@79 | 1303 | args=[], |
paulb@79 | 1304 | star=None, |
paulb@79 | 1305 | dstar=None |
paulb@79 | 1306 | ) |
paulb@79 | 1307 | ), |
paulb@101 | 1308 | InvokeBlock(expr=LoadRef(ref=subprogram)), |
paulb@101 | 1309 | ReleaseTemp() |
paul@0 | 1310 | ] |
paul@0 | 1311 | return result |
paul@0 | 1312 | |
paul@0 | 1313 | # Exception node transformations. |
paul@0 | 1314 | |
paul@0 | 1315 | def visitTryFinally(self, tryfinally): |
paulb@79 | 1316 | result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) |
paul@0 | 1317 | if tryfinally.body is not None: |
paul@0 | 1318 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 1319 | if tryfinally.final is not None: |
paul@0 | 1320 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 1321 | return result |
paul@0 | 1322 | |
paulb@57 | 1323 | # Convenience functions. |
paulb@57 | 1324 | |
paulb@57 | 1325 | def simplify(filename, builtins=0): |
paulb@57 | 1326 | |
paulb@57 | 1327 | """ |
paulb@88 | 1328 | Simplify the module stored in the file with the given 'filename'. |
paulb@88 | 1329 | |
paulb@88 | 1330 | If the optional 'builtins' parameter is set to a true value (the default |
paulb@88 | 1331 | being a false value), then the module is considered as the builtins module. |
paulb@57 | 1332 | """ |
paulb@57 | 1333 | |
paulb@57 | 1334 | simplifier = Simplifier(builtins) |
paulb@57 | 1335 | module = compiler.parseFile(filename) |
paulb@57 | 1336 | compiler.misc.set_filename(filename, module) |
paulb@88 | 1337 | if builtins: |
paulb@88 | 1338 | name = "__builtins__" |
paulb@88 | 1339 | else: |
paulb@88 | 1340 | path, ext = os.path.splitext(filename) |
paulb@88 | 1341 | path, name = os.path.split(path) |
paulb@88 | 1342 | simplified = simplifier.process(module, name) |
paulb@57 | 1343 | return simplified |
paulb@57 | 1344 | |
paul@0 | 1345 | # vim: tabstop=4 expandtab shiftwidth=4 |