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 |
paul@0 | 24 | """ |
paul@0 | 25 | |
paulb@19 | 26 | from simplified import * |
paul@0 | 27 | import compiler.ast |
paul@0 | 28 | |
paulb@19 | 29 | class Simplifier(Visitor): |
paul@0 | 30 | |
paul@0 | 31 | """ |
paul@0 | 32 | A simplifying visitor for AST nodes. |
paul@0 | 33 | |
paul@5 | 34 | Covered: And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, |
paulb@13 | 35 | CallFunc, Class, Compare, Const, Continue, Dict, Discard, For, |
paulb@13 | 36 | From, Function, Getattr, Global, If, Import, Invert, Keyword, |
paulb@13 | 37 | Lambda, List, Module, Name, Not, Or, Pass, Raise, Return, Slice, |
paulb@13 | 38 | Stmt, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, |
paulb@13 | 39 | UnarySub. |
paul@0 | 40 | |
paulb@13 | 41 | Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Div, |
paulb@13 | 42 | Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor, |
paulb@13 | 43 | ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, Sliceobj, |
paulb@13 | 44 | Sub, Yield. |
paul@0 | 45 | """ |
paul@0 | 46 | |
paul@0 | 47 | def __init__(self): |
paulb@19 | 48 | Visitor.__init__(self) |
paul@1 | 49 | self.result = None # The resulting tree. |
paul@1 | 50 | self.subprograms = [] # Subprograms outside the tree. |
paulb@24 | 51 | self.structures = [] # Structures/classes. |
paulb@24 | 52 | self.constants = {} # Constants. |
paul@1 | 53 | self.current_subprograms = [] # Current subprograms being processed. |
paul@0 | 54 | |
paulb@20 | 55 | def process(self, node): |
paulb@20 | 56 | self.visitor = self |
paulb@20 | 57 | return self.dispatch(node) |
paulb@20 | 58 | |
paulb@6 | 59 | def dispatch_or_none(self, node, *args): |
paulb@6 | 60 | if node is not None: |
paulb@6 | 61 | return self.dispatch(node, *args) |
paulb@6 | 62 | else: |
paulb@6 | 63 | return LoadName(name="None") |
paulb@6 | 64 | |
paul@0 | 65 | # Placeholder or deletion transformations. |
paul@0 | 66 | |
paul@0 | 67 | def visitStmt(self, stmt): |
paul@0 | 68 | return self.dispatches(stmt.nodes) |
paul@0 | 69 | |
paul@0 | 70 | def visitPass(self, pass_): |
paul@0 | 71 | return Pass(pass_) |
paul@0 | 72 | |
paul@0 | 73 | def visitDiscard(self, discard): |
paul@0 | 74 | return self.dispatch(discard.expr) |
paul@0 | 75 | |
paul@0 | 76 | # Relatively trivial transformations. |
paul@0 | 77 | |
paul@0 | 78 | def visitModule(self, module): |
paul@0 | 79 | self.result = Module(module) |
paul@0 | 80 | self.result.code = self.dispatch(module.node) |
paul@0 | 81 | return self.result |
paul@0 | 82 | |
paul@0 | 83 | def visitGetattr(self, getattr): |
paul@0 | 84 | result = LoadAttr(getattr, name=getattr.attrname) |
paul@0 | 85 | result.expr = self.dispatch(getattr.expr) |
paul@0 | 86 | return result |
paul@0 | 87 | |
paul@0 | 88 | def visitKeyword(self, keyword): |
paul@0 | 89 | result = Keyword(keyword, name=keyword.name) |
paul@0 | 90 | result.expr = self.dispatch(keyword.expr) |
paul@0 | 91 | return result |
paul@0 | 92 | |
paul@1 | 93 | def visitGlobal(self, global_): |
paul@1 | 94 | result = Global(global_, names=global_.names) |
paul@1 | 95 | return result |
paul@1 | 96 | |
paul@3 | 97 | def visitImport(self, import_): |
paul@3 | 98 | result = Assign(import_) |
paul@3 | 99 | code = [] |
paul@3 | 100 | for path, alias in import_.names: |
paul@3 | 101 | importer = Import(name=path) |
paul@3 | 102 | top = alias or path.split(".")[0] |
paul@3 | 103 | code.append(StoreName(expr=importer, name=top)) |
paul@3 | 104 | result.code = code |
paul@3 | 105 | return result |
paul@3 | 106 | |
paul@3 | 107 | def visitFrom(self, from_): |
paul@3 | 108 | result = Assign(from_) |
paul@3 | 109 | code = [] |
paul@3 | 110 | code.append(StoreTemp(expr=Import(name=from_.modname))) |
paul@3 | 111 | for name, alias in from_.names: |
paul@3 | 112 | code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name))) |
paul@3 | 113 | result.code = code |
paul@3 | 114 | return result |
paul@3 | 115 | |
paul@0 | 116 | def visitName(self, name): |
paul@0 | 117 | result = LoadName(name, name=name.name) |
paul@0 | 118 | return result |
paul@0 | 119 | |
paul@0 | 120 | def visitConst(self, const): |
paulb@24 | 121 | if not self.constants.has_key(const.value): |
paulb@24 | 122 | self.constants[const.value] = Constant(name=repr(const.value), value=const.value) |
paulb@24 | 123 | result = LoadRef(ref=self.constants[const.value]) |
paul@0 | 124 | return result |
paul@0 | 125 | |
paul@0 | 126 | def visitReturn(self, return_): |
paul@0 | 127 | result = Return(return_) |
paul@0 | 128 | result.expr = self.dispatch(return_.value) |
paul@0 | 129 | return result |
paul@0 | 130 | |
paul@0 | 131 | def visitBreak(self, break_): |
paul@0 | 132 | result = Return(break_) |
paul@0 | 133 | return result |
paul@0 | 134 | |
paul@0 | 135 | def visitContinue(self, continue_): |
paulb@7 | 136 | result = Invoke(continue_, same_frame=1, produces_result=0, star=None, dstar=None, args=[]) |
paul@0 | 137 | result.expr = LoadRef(ref=self.current_subprograms[-1]) |
paul@0 | 138 | return result |
paul@0 | 139 | |
paul@5 | 140 | def visitRaise(self, raise_): |
paul@5 | 141 | result = Raise(raise_, expr=self.dispatch(raise_.expr1), traceback=None) |
paul@5 | 142 | if raise_.expr2 is not None: |
paul@5 | 143 | result.args = [self.dispatch(raise_.expr2)] |
paul@5 | 144 | if raise_.expr3 is not None: |
paul@5 | 145 | result.traceback = self.dispatch(raise_.expr3) |
paul@5 | 146 | return result |
paul@5 | 147 | |
paul@0 | 148 | def _visitBuiltin(self, builtin, name): |
paul@1 | 149 | result = Invoke(builtin, expr=LoadName(name=name)) |
paul@1 | 150 | result.args = self.dispatches(builtin.nodes) |
paul@0 | 151 | return result |
paul@0 | 152 | |
paul@0 | 153 | def visitTuple(self, tuple): |
paulb@24 | 154 | return self._visitBuiltin(tuple, "tuple") |
paul@0 | 155 | |
paul@0 | 156 | def visitList(self, list): |
paulb@24 | 157 | return self._visitBuiltin(list, "list") |
paul@0 | 158 | |
paul@0 | 159 | def visitDict(self, dict): |
paulb@24 | 160 | result = Invoke(dict, expr=LoadName(name="dict")) |
paul@0 | 161 | args = [] |
paul@0 | 162 | for key, value in dict.items: |
paulb@24 | 163 | tuple = Invoke(expr=LoadName(name="tuple"), star=None, dstar=None) |
paul@0 | 164 | tuple.args = [self.dispatch(key), self.dispatch(value)] |
paul@0 | 165 | args.append(tuple) |
paul@0 | 166 | result.args = args |
paul@0 | 167 | return result |
paul@0 | 168 | |
paulb@17 | 169 | # Logical and comparison operations plus chained statements. |
paulb@17 | 170 | |
paulb@17 | 171 | def visitIf(self, if_): |
paulb@17 | 172 | |
paulb@17 | 173 | # Make a subprogram for the statement and record it outside the main tree. |
paulb@17 | 174 | |
paulb@17 | 175 | subprogram = Subprogram(name=hex(id(if_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@17 | 176 | self.current_subprograms.append(subprogram) |
paulb@17 | 177 | |
paulb@17 | 178 | # In the subprogram, make conditionals for each test plus statement, |
paulb@17 | 179 | # adding a return onto the end of each statement block. |
paulb@17 | 180 | |
paulb@17 | 181 | nodes = [] |
paulb@17 | 182 | for compare, stmt in if_.tests: |
paulb@17 | 183 | # Produce something like... |
paulb@17 | 184 | # expr.__true__() ? body |
paulb@17 | 185 | test = Conditional(else_=[], test=Invoke( |
paulb@17 | 186 | expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), |
paulb@17 | 187 | args=[], star=None, dstar=None)) |
paulb@17 | 188 | test.body = self.dispatch(stmt) + [Return()] |
paulb@17 | 189 | nodes.append(test) |
paulb@17 | 190 | |
paulb@17 | 191 | # Add the compound statement from any else clause to the end. |
paulb@17 | 192 | |
paulb@17 | 193 | if if_.else_ is not None: |
paulb@17 | 194 | nodes += self.dispatch(if_.else_) |
paulb@17 | 195 | |
paulb@17 | 196 | subprogram.code = nodes |
paulb@17 | 197 | |
paulb@17 | 198 | self.current_subprograms.pop() |
paulb@17 | 199 | self.subprograms.append(subprogram) |
paulb@17 | 200 | |
paulb@17 | 201 | # Make an invocation of the subprogram. |
paulb@17 | 202 | |
paulb@17 | 203 | result = Invoke(compare, same_frame=1, star=None, dstar=None, args=[]) |
paulb@17 | 204 | result.expr = LoadRef(ref=subprogram) |
paulb@17 | 205 | return result |
paulb@17 | 206 | |
paulb@17 | 207 | def _visitTryExcept(self, tryexcept): |
paulb@17 | 208 | |
paulb@17 | 209 | # Make a subprogram for the statement and record it outside the main tree. |
paulb@17 | 210 | |
paulb@17 | 211 | subprogram = Subprogram(name=hex(id(tryexcept)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@17 | 212 | self.current_subprograms.append(subprogram) |
paulb@17 | 213 | |
paulb@17 | 214 | # In the subprogram, make conditionals for each test plus statement, |
paulb@17 | 215 | # adding a return onto the end of each statement block. |
paulb@17 | 216 | |
paulb@17 | 217 | nodes = [] |
paulb@17 | 218 | for spec, assign, stmt in tryexcept.handlers: |
paulb@17 | 219 | |
paulb@17 | 220 | # If no specification exists, produce an unconditional block. |
paulb@17 | 221 | |
paulb@17 | 222 | if spec is None: |
paulb@17 | 223 | nodes += self.dispatch(stmt) |
paulb@17 | 224 | |
paulb@17 | 225 | # Produce something like... |
paulb@17 | 226 | # isinstance(<exc>, <spec>) |
paulb@17 | 227 | |
paulb@17 | 228 | else: |
paulb@17 | 229 | new_spec = self.dispatch(spec) |
paulb@17 | 230 | test = Conditional(body=[], else_=[], test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), new_spec], star=None, dstar=None)) |
paulb@17 | 231 | if assign is not None: |
paulb@17 | 232 | test.body.append(Assign(code=[StoreTemp(expr=LoadExc()), self.dispatch(assign), ReleaseTemp()])) |
paulb@17 | 233 | test.body += self.dispatch(stmt) + [Return()] |
paulb@17 | 234 | nodes.append(test) |
paulb@17 | 235 | |
paulb@17 | 236 | # Add a raise operation to deal with unhandled exceptions. |
paulb@17 | 237 | |
paulb@17 | 238 | nodes.append(Raise(expr=LoadExc())) |
paulb@17 | 239 | subprogram.code = nodes |
paulb@17 | 240 | |
paulb@17 | 241 | self.current_subprograms.pop() |
paulb@17 | 242 | self.subprograms.append(subprogram) |
paulb@17 | 243 | |
paulb@17 | 244 | # Make an invocation of the subprogram. |
paulb@17 | 245 | |
paulb@17 | 246 | result = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paulb@17 | 247 | result.expr = LoadRef(ref=subprogram) |
paulb@17 | 248 | return result |
paulb@17 | 249 | |
paulb@17 | 250 | def visitTryExcept(self, tryexcept): |
paulb@17 | 251 | result = Try(tryexcept, body=[], else_=[], finally_=[]) |
paulb@17 | 252 | if tryexcept.body is not None: |
paulb@17 | 253 | result.body = self.dispatch(tryexcept.body) |
paulb@17 | 254 | if tryexcept.else_ is not None: |
paulb@17 | 255 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@17 | 256 | result.handler = self._visitTryExcept(tryexcept) |
paulb@17 | 257 | return result |
paulb@13 | 258 | |
paulb@13 | 259 | comparison_methods = { |
paulb@13 | 260 | "==" : "__eq__", "!=" : "__ne__", "<" : "__lt__", "<=" : "__le__", |
paulb@13 | 261 | ">=" : "__ge__", ">" : "__gt__", "is" : None, "is not" : None |
paulb@13 | 262 | } |
paulb@13 | 263 | |
paulb@13 | 264 | def visitCompare(self, compare): |
paulb@13 | 265 | |
paulb@13 | 266 | # Make a subprogram for the expression and record it outside the main tree. |
paulb@13 | 267 | |
paulb@13 | 268 | subprogram = Subprogram(name=hex(id(compare)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@13 | 269 | self.current_subprograms.append(subprogram) |
paulb@13 | 270 | |
paulb@13 | 271 | # In the subprogram, make instructions which invoke a method on the |
paulb@13 | 272 | # first operand of each operand pair and, if appropriate, return with |
paulb@13 | 273 | # the value from that method. |
paulb@13 | 274 | |
paulb@13 | 275 | nodes = [] |
paulb@13 | 276 | last = compare.ops[-1] |
paulb@13 | 277 | previous = self.dispatch(compare.expr) |
paulb@13 | 278 | for op in compare.ops: |
paulb@13 | 279 | op_name, node = op |
paulb@13 | 280 | expr = self.dispatch(node) |
paulb@13 | 281 | method_name = self.comparison_methods[op_name] |
paulb@13 | 282 | if method_name: |
paulb@15 | 283 | invocation = Invoke(expr=LoadAttr(expr=previous, name=method_name), args=[expr], star=None, dstar=None) |
paulb@13 | 284 | elif op_name == "is": |
paulb@15 | 285 | invocation = Invoke(expr=LoadName(name="__is__"), args=[previous, expr], star=None, dstar=None) |
paulb@13 | 286 | elif op_name == "is not": |
paulb@15 | 287 | invocation = Not(expr=Invoke(expr=LoadName(name="__is__"), args=[previous, expr], star=None, dstar=None)) |
paulb@13 | 288 | else: |
paulb@13 | 289 | raise NotImplementedError, op_name |
paulb@13 | 290 | nodes.append(StoreTemp(expr=invocation)) |
paulb@13 | 291 | if op is not last: |
paulb@13 | 292 | nodes.append(Conditional(test=Not(expr=LoadTemp()), body=[Return(expr=LoadTemp())])) |
paulb@13 | 293 | nodes.append(ReleaseTemp()) |
paulb@13 | 294 | else: |
paulb@13 | 295 | nodes.append(Return(expr=LoadTemp())) |
paulb@15 | 296 | previous = expr |
paulb@13 | 297 | subprogram.code = nodes |
paulb@13 | 298 | |
paulb@13 | 299 | self.current_subprograms.pop() |
paulb@13 | 300 | self.subprograms.append(subprogram) |
paulb@13 | 301 | |
paulb@13 | 302 | # Make an invocation of the subprogram. |
paulb@13 | 303 | |
paulb@13 | 304 | result = Invoke(compare, same_frame=1, star=None, dstar=None, args=[]) |
paulb@13 | 305 | result.expr = LoadRef(ref=subprogram) |
paulb@13 | 306 | return result |
paul@1 | 307 | |
paul@1 | 308 | def visitAnd(self, and_): |
paulb@7 | 309 | |
paulb@7 | 310 | # Make a subprogram for the expression and record it outside the main tree. |
paulb@7 | 311 | |
paulb@11 | 312 | subprogram = Subprogram(name=hex(id(and_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 313 | self.current_subprograms.append(subprogram) |
paulb@7 | 314 | |
paulb@12 | 315 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 316 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 317 | # subprogram with the value of the operand. |
paulb@12 | 318 | |
paul@1 | 319 | nodes = [] |
paulb@7 | 320 | last = and_.nodes[-1] |
paul@1 | 321 | for node in and_.nodes: |
paulb@7 | 322 | expr = self.dispatch(node) |
paulb@7 | 323 | if node is not last: |
paulb@12 | 324 | nodes.append(StoreTemp(expr=expr)) |
paulb@15 | 325 | invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) |
paulb@13 | 326 | nodes.append(Conditional(test=Not(expr=invocation), body=[Return(expr=LoadTemp())])) |
paulb@7 | 327 | nodes.append(ReleaseTemp()) |
paulb@7 | 328 | else: |
paulb@7 | 329 | nodes.append(Return(expr=expr)) |
paulb@7 | 330 | subprogram.code = nodes |
paulb@7 | 331 | |
paulb@7 | 332 | self.current_subprograms.pop() |
paulb@7 | 333 | self.subprograms.append(subprogram) |
paulb@7 | 334 | |
paulb@7 | 335 | # Make an invocation of the subprogram. |
paulb@7 | 336 | |
paulb@7 | 337 | result = Invoke(and_, same_frame=1, star=None, dstar=None, args=[]) |
paulb@7 | 338 | result.expr = LoadRef(ref=subprogram) |
paul@1 | 339 | return result |
paul@1 | 340 | |
paul@1 | 341 | def visitOr(self, or_): |
paulb@7 | 342 | |
paulb@7 | 343 | # Make a subprogram for the expression and record it outside the main tree. |
paulb@7 | 344 | |
paulb@11 | 345 | subprogram = Subprogram(name=hex(id(or_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 346 | self.current_subprograms.append(subprogram) |
paulb@7 | 347 | |
paulb@12 | 348 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 349 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 350 | # subprogram with the value of the operand. |
paulb@12 | 351 | |
paul@1 | 352 | nodes = [] |
paulb@7 | 353 | last = or_.nodes[-1] |
paul@1 | 354 | for node in or_.nodes: |
paulb@7 | 355 | expr = self.dispatch(node) |
paulb@7 | 356 | if node is not last: |
paulb@12 | 357 | nodes.append(StoreTemp(expr=expr)) |
paulb@15 | 358 | invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) |
paulb@7 | 359 | nodes.append(Conditional(test=invocation, body=[Return(expr=LoadTemp())])) |
paulb@7 | 360 | nodes.append(ReleaseTemp()) |
paulb@7 | 361 | else: |
paulb@7 | 362 | nodes.append(Return(expr=expr)) |
paulb@7 | 363 | subprogram.code = nodes |
paulb@7 | 364 | |
paulb@7 | 365 | self.current_subprograms.pop() |
paulb@7 | 366 | self.subprograms.append(subprogram) |
paulb@7 | 367 | |
paulb@7 | 368 | # Make an invocation of the subprogram. |
paulb@7 | 369 | |
paulb@7 | 370 | result = Invoke(or_, same_frame=1, star=None, dstar=None, args=[]) |
paulb@7 | 371 | result.expr = LoadRef(ref=subprogram) |
paul@1 | 372 | return result |
paul@1 | 373 | |
paul@1 | 374 | def visitNot(self, not_): |
paul@1 | 375 | result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), |
paulb@15 | 376 | args=[], star=None, dstar=None)) |
paul@1 | 377 | return result |
paul@1 | 378 | |
paul@4 | 379 | # Operators. |
paul@4 | 380 | |
paul@4 | 381 | def visitUnaryAdd(self, unaryadd): |
paulb@11 | 382 | return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[]) |
paul@4 | 383 | |
paul@4 | 384 | def visitUnarySub(self, unarysub): |
paulb@11 | 385 | return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[]) |
paul@4 | 386 | |
paul@4 | 387 | def visitInvert(self, invert): |
paulb@11 | 388 | return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[]) |
paul@4 | 389 | |
paul@0 | 390 | # Assignments. |
paul@0 | 391 | |
paul@2 | 392 | augassign_methods = { |
paulb@9 | 393 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", |
paulb@9 | 394 | "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", |
paulb@9 | 395 | "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" |
paul@2 | 396 | } |
paul@2 | 397 | |
paul@2 | 398 | def visitAugAssign(self, augassign): |
paul@2 | 399 | result = Assign(augassign) |
paul@2 | 400 | expr = self.dispatch(augassign.expr) |
paul@2 | 401 | |
paul@2 | 402 | # Simple augmented assignment: name += expr |
paul@2 | 403 | |
paul@2 | 404 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@6 | 405 | name = augassign.node |
paulb@6 | 406 | node = self.dispatch(name) |
paul@2 | 407 | get_incremented = StoreTemp( |
paul@2 | 408 | expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 409 | ) |
paulb@6 | 410 | store = StoreName(expr=LoadTemp(), name=name.name) |
paul@2 | 411 | result.code = [get_incremented, store, ReleaseTemp()] |
paul@2 | 412 | |
paulb@9 | 413 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 414 | |
paul@2 | 415 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 416 | |
paulb@9 | 417 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 418 | |
paulb@6 | 419 | getattr = augassign.node |
paulb@6 | 420 | store_expr = StoreTemp(index="expr", expr=self.dispatch(getattr.expr)) |
paulb@6 | 421 | node_attr = LoadAttr(expr=LoadTemp(index="expr"), name=getattr.attrname) |
paul@2 | 422 | get_incremented = StoreTemp( |
paul@2 | 423 | expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr]) |
paul@2 | 424 | ) |
paulb@6 | 425 | store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index="expr"), name=getattr.attrname) |
paulb@6 | 426 | result.code = [store_expr, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp()] |
paulb@6 | 427 | |
paulb@9 | 428 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 429 | |
paulb@6 | 430 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 431 | |
paulb@9 | 432 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 433 | |
paulb@6 | 434 | slice = augassign.node |
paulb@6 | 435 | store_expr = StoreTemp(index="expr", expr=self.dispatch(slice.expr)) |
paulb@6 | 436 | store_lower = StoreTemp(index="lower", expr=self.dispatch_or_none(slice.lower)) |
paulb@6 | 437 | store_upper = StoreTemp(index="upper", expr=self.dispatch_or_none(slice.upper)) |
paulb@11 | 438 | node_slice = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_APPLY") |
paulb@6 | 439 | get_incremented = StoreTemp( |
paulb@6 | 440 | expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr]) |
paulb@6 | 441 | ) |
paulb@11 | 442 | store = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_ASSIGN", LoadTemp()) |
paulb@6 | 443 | result.code = [store_expr, store_lower, store_upper, get_incremented, store, |
paulb@6 | 444 | ReleaseTemp(index="expr"), ReleaseTemp(index="lower"), ReleaseTemp(index="upper"), ReleaseTemp()] |
paul@2 | 445 | |
paulb@10 | 446 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 447 | |
paulb@10 | 448 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 449 | |
paulb@10 | 450 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 451 | |
paulb@10 | 452 | subscript = augassign.node |
paulb@10 | 453 | store_expr = StoreTemp(index="expr", expr=self.dispatch(subscript.expr)) |
paulb@10 | 454 | subs = self._visitSubscriptSubs(subscript.subs) |
paulb@10 | 455 | store_subs = StoreTemp(index="subs", expr=subs) |
paulb@11 | 456 | node_subscript = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_APPLY") |
paulb@10 | 457 | get_incremented = StoreTemp( |
paulb@10 | 458 | expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr]) |
paulb@10 | 459 | ) |
paulb@11 | 460 | store = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_ASSIGN", LoadTemp()) |
paulb@10 | 461 | result.code = [store_expr, store_subs, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp(index="subs"), ReleaseTemp()] |
paulb@10 | 462 | |
paul@2 | 463 | else: |
paul@2 | 464 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 465 | |
paul@2 | 466 | return result |
paul@2 | 467 | |
paul@0 | 468 | def visitAssign(self, assign): |
paul@0 | 469 | result = Assign(assign) |
paul@0 | 470 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paul@0 | 471 | release = ReleaseTemp() |
paul@0 | 472 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paul@0 | 473 | return result |
paul@0 | 474 | |
paul@0 | 475 | def visitAssList(self, asslist, in_sequence=0): |
paul@0 | 476 | if not in_sequence: |
paulb@11 | 477 | expr = LoadTemp() |
paul@0 | 478 | else: |
paulb@11 | 479 | expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 480 | result = Assign(asslist) |
paul@0 | 481 | store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr))) |
paul@0 | 482 | release = ReleaseTemp() |
paul@0 | 483 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paul@0 | 484 | return result |
paul@0 | 485 | |
paul@0 | 486 | visitAssTuple = visitAssList |
paul@0 | 487 | |
paul@0 | 488 | def _visitAssNameOrAttr(self, node, in_sequence): |
paul@0 | 489 | if not in_sequence: |
paulb@11 | 490 | return LoadTemp() |
paul@0 | 491 | else: |
paulb@11 | 492 | return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next")) |
paul@0 | 493 | |
paul@0 | 494 | def visitAssName(self, assname, in_sequence=0): |
paul@0 | 495 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paul@0 | 496 | result = StoreName(assname, name=assname.name, expr=expr) |
paul@0 | 497 | return result |
paul@0 | 498 | |
paul@0 | 499 | def visitAssAttr(self, assattr, in_sequence=0): |
paul@0 | 500 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paul@0 | 501 | lvalue = self.dispatch(assattr.expr) |
paul@0 | 502 | result = StoreAttr(assattr, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@0 | 503 | return result |
paul@0 | 504 | |
paulb@11 | 505 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@6 | 506 | if flags == "OP_ASSIGN": |
paulb@6 | 507 | args = [value] |
paulb@6 | 508 | result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__")) |
paulb@6 | 509 | elif flags == "OP_APPLY": |
paulb@6 | 510 | args = [] |
paulb@6 | 511 | result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__")) |
paulb@10 | 512 | elif flags == "OP_DELETE": |
paulb@10 | 513 | args = [] |
paulb@10 | 514 | result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__")) |
paulb@6 | 515 | else: |
paulb@6 | 516 | raise NotImplementedError, flags |
paulb@6 | 517 | |
paulb@6 | 518 | # Add the dimensions. |
paulb@6 | 519 | |
paulb@6 | 520 | args.insert(0, lower) |
paulb@6 | 521 | args.insert(1, upper) |
paulb@6 | 522 | |
paulb@11 | 523 | result.original = slice |
paulb@6 | 524 | result.args = args |
paulb@6 | 525 | return result |
paulb@6 | 526 | |
paulb@6 | 527 | def visitSlice(self, slice, in_sequence=0): |
paulb@6 | 528 | value = self._visitAssNameOrAttr(slice, in_sequence) |
paulb@11 | 529 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@11 | 530 | self.dispatch_or_none(slice.upper), slice.flags, value) |
paulb@6 | 531 | |
paulb@11 | 532 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@10 | 533 | if flags == "OP_ASSIGN": |
paulb@10 | 534 | args = [value] |
paulb@10 | 535 | result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__")) |
paulb@10 | 536 | elif flags == "OP_APPLY": |
paulb@10 | 537 | args = [] |
paulb@10 | 538 | result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__")) |
paulb@10 | 539 | elif flags == "OP_DELETE": |
paulb@10 | 540 | args = [] |
paulb@10 | 541 | result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__")) |
paulb@10 | 542 | else: |
paulb@10 | 543 | raise NotImplementedError, flags |
paulb@10 | 544 | |
paulb@10 | 545 | # Add the dimensions. |
paulb@10 | 546 | |
paulb@10 | 547 | args.insert(0, subs) |
paulb@10 | 548 | |
paulb@11 | 549 | result.original = subscript |
paulb@10 | 550 | result.args = args |
paulb@10 | 551 | return result |
paulb@10 | 552 | |
paulb@10 | 553 | def _visitSubscriptSubs(self, subs): |
paulb@10 | 554 | if len(subs) == 1: |
paulb@10 | 555 | return self.dispatch(subs[0]) |
paulb@10 | 556 | else: |
paulb@24 | 557 | return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs)) |
paulb@10 | 558 | |
paulb@10 | 559 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@10 | 560 | value = self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@10 | 561 | subs = self._visitSubscriptSubs(subscript.subs) |
paulb@11 | 562 | return self._visitSubscript(subscript, self.dispatch(subscript.expr), subs, subscript.flags, value) |
paulb@10 | 563 | |
paul@0 | 564 | # Invocation and subprogram transformations. |
paul@0 | 565 | |
paulb@19 | 566 | def visitClass(self, class_): |
paulb@19 | 567 | structure = Class(name=hex(id(class_)), bases=class_.bases) |
paulb@19 | 568 | self.structures.append(structure) |
paulb@19 | 569 | |
paulb@24 | 570 | # Make a subprogram which initialises the class structure. |
paulb@24 | 571 | |
paulb@24 | 572 | subprogram = Subprogram(name=hex(id(class_)), structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 573 | self.current_subprograms.append(subprogram) |
paulb@19 | 574 | |
paulb@19 | 575 | subprogram.code = self.dispatch(class_.code) |
paulb@19 | 576 | |
paulb@19 | 577 | self.current_subprograms.pop() |
paulb@19 | 578 | self.subprograms.append(subprogram) |
paulb@19 | 579 | |
paulb@19 | 580 | # Make a definition of the class associating it with a name. |
paulb@19 | 581 | |
paulb@19 | 582 | result = Assign(class_) |
paulb@19 | 583 | init = Invoke(expr=LoadRef(ref=subprogram), args=[], star=None, dstar=None) |
paulb@19 | 584 | store = StoreName(name=class_.name, expr=LoadRef(ref=structure)) |
paulb@19 | 585 | result.code = [init, store] |
paulb@19 | 586 | return result |
paulb@19 | 587 | |
paul@0 | 588 | def _visitFunction(self, function, subprogram): |
paul@0 | 589 | if function.flags & 4 != 0: has_star = 1 |
paul@0 | 590 | else: has_star = 0 |
paul@0 | 591 | if function.flags & 8 != 0: has_dstar = 1 |
paul@0 | 592 | else: has_dstar = 0 |
paul@0 | 593 | ndefaults = len(function.defaults) |
paul@0 | 594 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@24 | 595 | |
paulb@24 | 596 | # Produce star and dstar parameters with appropriate defaults. |
paulb@24 | 597 | |
paulb@24 | 598 | if has_star: |
paulb@24 | 599 | star = (function.argnames[npositional], Invoke(expr=LoadName(name="list"), args=[], star=None, dstar=None)) |
paulb@24 | 600 | else: |
paulb@24 | 601 | star = None |
paulb@24 | 602 | if has_dstar: |
paulb@24 | 603 | dstar = (function.argnames[npositional + has_star], Invoke(expr=LoadName(name="dict"), args=[], star=None, dstar=None)) |
paulb@24 | 604 | else: |
paulb@24 | 605 | dstar = None |
paul@0 | 606 | |
paul@0 | 607 | params = [] |
paul@0 | 608 | for i in range(0, npositional - ndefaults): |
paul@0 | 609 | params.append((function.argnames[i], None)) |
paul@0 | 610 | |
paul@0 | 611 | # NOTE: Fix/process defaults. |
paul@0 | 612 | |
paul@0 | 613 | for i in range(0, ndefaults): |
paul@0 | 614 | default = function.defaults[i] |
paul@0 | 615 | if default is not None: |
paul@0 | 616 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paul@0 | 617 | else: |
paul@0 | 618 | params.append((function.argnames[npositional - ndefaults + i], default)) |
paul@0 | 619 | |
paul@0 | 620 | subprogram.params = params |
paul@0 | 621 | subprogram.star = star |
paul@0 | 622 | subprogram.dstar = dstar |
paul@0 | 623 | self.subprograms.append(subprogram) |
paul@0 | 624 | |
paul@0 | 625 | def visitFunction(self, function): |
paul@0 | 626 | |
paul@0 | 627 | # Make a subprogram for the function and record it outside the main |
paul@0 | 628 | # tree. |
paul@0 | 629 | |
paulb@18 | 630 | subprogram = Subprogram(name=hex(id(function)), returns_value=1, star=None, dstar=None) |
paul@0 | 631 | self.current_subprograms.append(subprogram) |
paul@0 | 632 | subprogram.code = self.dispatch(function.code) |
paul@0 | 633 | self.current_subprograms.pop() |
paul@0 | 634 | self._visitFunction(function, subprogram) |
paul@0 | 635 | |
paul@0 | 636 | # Make a definition of the function associating it with a name. |
paul@0 | 637 | |
paulb@19 | 638 | result = StoreName(name=function.name, expr=LoadRef(ref=subprogram)) |
paul@0 | 639 | return result |
paul@0 | 640 | |
paul@0 | 641 | def visitLambda(self, lambda_): |
paul@0 | 642 | |
paul@0 | 643 | # Make a subprogram for the function and record it outside the main |
paul@0 | 644 | # tree. |
paul@0 | 645 | |
paulb@11 | 646 | subprogram = Subprogram(name=hex(id(lambda_)), returns_value=1, star=None, dstar=None) |
paul@0 | 647 | self.current_subprograms.append(subprogram) |
paul@0 | 648 | subprogram.code = [Return(expr=self.dispatch(lambda_.code))] |
paul@0 | 649 | self.current_subprograms.pop() |
paul@0 | 650 | self._visitFunction(lambda_, subprogram) |
paul@0 | 651 | |
paul@0 | 652 | # Get the subprogram reference to the lambda. |
paul@0 | 653 | |
paulb@11 | 654 | return LoadRef(lambda_, ref=subprogram) |
paul@0 | 655 | |
paul@0 | 656 | def visitCallFunc(self, callfunc): |
paul@0 | 657 | result = Invoke(callfunc, same_frame=0, star=None, dstar=None) |
paul@0 | 658 | result.args = self.dispatches(callfunc.args) |
paul@0 | 659 | if callfunc.star_args is not None: |
paul@0 | 660 | result.star = self.dispatch(callfunc.star_args) |
paul@0 | 661 | if callfunc.dstar_args is not None: |
paul@0 | 662 | result.dstar = self.dispatch(callfunc.dstar_args) |
paul@0 | 663 | result.expr = self.dispatch(callfunc.node) |
paul@0 | 664 | return result |
paul@0 | 665 | |
paul@0 | 666 | def visitWhile(self, while_): |
paul@0 | 667 | |
paul@0 | 668 | # Make a subprogram for the block and record it outside the main tree. |
paul@0 | 669 | |
paulb@11 | 670 | subprogram = Subprogram(name=hex(id(while_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 671 | self.current_subprograms.append(subprogram) |
paul@0 | 672 | |
paul@0 | 673 | # Include a conditional statement in the subprogram. |
paul@0 | 674 | |
paul@0 | 675 | test = Conditional(else_=[]) |
paul@0 | 676 | test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), |
paulb@17 | 677 | args=[], star=None, dstar=None) |
paul@0 | 678 | |
paul@0 | 679 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 680 | # if the test condition was satisfied. |
paul@0 | 681 | |
paul@0 | 682 | continuation = Invoke(same_frame=1, star=None, dstar=None, args=[]) |
paul@0 | 683 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 684 | test.body = self.dispatch(while_.body) + [continuation] |
paul@0 | 685 | if while_.else_ is not None: |
paul@0 | 686 | test.else_ = self.dispatch(while_.else_) |
paul@0 | 687 | subprogram.code = [test] |
paul@0 | 688 | |
paul@0 | 689 | self.current_subprograms.pop() |
paul@0 | 690 | self.subprograms.append(subprogram) |
paul@0 | 691 | |
paul@0 | 692 | # Make an invocation of the subprogram. |
paul@0 | 693 | |
paulb@7 | 694 | result = Invoke(while_, same_frame=1, produces_result=0, star=None, dstar=None, args=[]) |
paul@0 | 695 | result.expr = LoadRef(ref=subprogram) |
paul@0 | 696 | return result |
paul@0 | 697 | |
paul@0 | 698 | def visitFor(self, for_): |
paul@0 | 699 | |
paul@0 | 700 | # Make a subprogram for the block and record it outside the main tree. |
paul@0 | 701 | |
paulb@11 | 702 | subprogram = Subprogram(name=hex(id(for_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) |
paul@0 | 703 | self.current_subprograms.append(subprogram) |
paul@0 | 704 | |
paulb@17 | 705 | if for_.else_ is not None: |
paulb@17 | 706 | else_stmt = self.dispatch(for_.else_) |
paulb@17 | 707 | else: |
paulb@17 | 708 | else_stmt = [] |
paulb@17 | 709 | |
paul@0 | 710 | # Wrap the assignment in a try...except statement. |
paul@0 | 711 | |
paulb@17 | 712 | try_except = Try(body=[], else_=[], finally_=[]) |
paulb@17 | 713 | try_except.handler = Conditional(test=Invoke(expr=LoadName(name="isinstance"), |
paulb@17 | 714 | args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None), |
paulb@17 | 715 | body=else_stmt, else_=[Raise(expr=LoadExc())]) |
paul@0 | 716 | |
paul@0 | 717 | assign = Assign() |
paul@0 | 718 | assign.code = [ |
paul@0 | 719 | StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))), |
paul@0 | 720 | self.dispatch(for_.assign), |
paul@0 | 721 | ReleaseTemp() |
paul@0 | 722 | ] |
paul@0 | 723 | |
paul@0 | 724 | # Inside the conditional, add a recursive invocation to the subprogram |
paul@0 | 725 | # if the test condition was satisfied. |
paul@0 | 726 | |
paulb@7 | 727 | continuation = Invoke(same_frame=1, produces_result=0, star=None, dstar=None, args=[]) |
paul@0 | 728 | continuation.expr = LoadRef(ref=subprogram) |
paul@0 | 729 | try_except.body = [assign] + self.dispatch(for_.body) + [continuation] |
paul@0 | 730 | subprogram.code = [try_except] |
paul@0 | 731 | |
paul@0 | 732 | self.subprograms.append(subprogram) |
paul@0 | 733 | self.current_subprograms.pop() |
paul@0 | 734 | |
paul@0 | 735 | # Obtain an iterator for the sequence involved. |
paul@0 | 736 | # Then, make an invocation of the subprogram. |
paul@0 | 737 | |
paul@0 | 738 | result = Assign(for_) |
paul@0 | 739 | result.code = [ |
paul@0 | 740 | StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))), |
paulb@7 | 741 | Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]), |
paul@0 | 742 | ReleaseTemp() |
paul@0 | 743 | ] |
paul@0 | 744 | return result |
paul@0 | 745 | |
paul@0 | 746 | # Exception node transformations. |
paul@0 | 747 | |
paul@0 | 748 | def visitTryFinally(self, tryfinally): |
paul@0 | 749 | result = Try(tryfinally, body=[], handlers=[], else_=[], finally_=[]) |
paul@0 | 750 | if tryfinally.body is not None: |
paul@0 | 751 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 752 | if tryfinally.final is not None: |
paul@0 | 753 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 754 | return result |
paul@0 | 755 | |
paul@0 | 756 | # vim: tabstop=4 expandtab shiftwidth=4 |