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