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 | |
paulb@178 | 8 | Copyright (C) 2006, 2007 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@220 | 41 | from simplify.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 | |
paulb@202 | 50 | Covered: Add, And, Assert, AssAttr, AssList, AssName, AssTuple, Assign, |
paulb@203 | 51 | AugAssign, Bitand, Break, CallFunc, Class, Compare, Const, |
paulb@203 | 52 | Continue, Dict, Discard, Div, FloorDiv, For, From, Function, |
paulb@203 | 53 | Getattr, Global, If, Import, Invert, Keyword, Lambda, List, |
paulb@203 | 54 | ListComp, ListCompFor, ListCompIf, Mod, Module, Mul, Name, Not, Or, |
paulb@203 | 55 | Pass, Power, Print, Printnl, Raise, Return, Slice, Sliceobj, Stmt, |
paulb@203 | 56 | Sub, Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, |
paulb@203 | 57 | UnarySub. |
paul@0 | 58 | |
paulb@203 | 59 | Missing: Backquote, Bitor, Bitxor, Decorators, Ellipsis, Exec, LeftShift, |
paulb@203 | 60 | RightShift, Yield. |
paul@0 | 61 | """ |
paul@0 | 62 | |
paulb@34 | 63 | def __init__(self, builtins=0): |
paulb@46 | 64 | |
paulb@46 | 65 | """ |
paulb@46 | 66 | Initialise the simplifier with the optional 'builtins' parameter |
paulb@46 | 67 | indicating whether the module contains the built-in classes and |
paulb@46 | 68 | functions. |
paulb@46 | 69 | """ |
paulb@46 | 70 | |
paulb@19 | 71 | Visitor.__init__(self) |
paul@1 | 72 | self.subprograms = [] # Subprograms outside the tree. |
paulb@24 | 73 | self.structures = [] # Structures/classes. |
paulb@24 | 74 | self.constants = {} # Constants. |
paul@1 | 75 | self.current_subprograms = [] # Current subprograms being processed. |
paulb@120 | 76 | self.current_structures = [] # Current structures being processed. |
paulb@172 | 77 | self.within_class = 0 # Whether a class is being defined. |
paulb@34 | 78 | self.builtins = builtins # Whether the builtins are being processed. |
paul@0 | 79 | |
paulb@65 | 80 | # Convenience attributes. |
paulb@65 | 81 | |
paulb@65 | 82 | self.subnames = {} |
paulb@65 | 83 | |
paulb@51 | 84 | # For compiler package mechanisms. |
paulb@51 | 85 | |
paulb@51 | 86 | self.visitor = self |
paulb@51 | 87 | |
paulb@88 | 88 | def process(self, node, name): |
paulb@88 | 89 | result = self.dispatch(node, name) |
paulb@57 | 90 | result.simplifier = self |
paulb@57 | 91 | return result |
paulb@20 | 92 | |
paulb@6 | 93 | def dispatch_or_none(self, node, *args): |
paulb@6 | 94 | if node is not None: |
paulb@6 | 95 | return self.dispatch(node, *args) |
paulb@6 | 96 | else: |
paulb@79 | 97 | return LoadName(node, name="None") |
paulb@6 | 98 | |
paulb@135 | 99 | # Top-level transformation. |
paul@0 | 100 | |
paulb@88 | 101 | def visitModule(self, module, name=None): |
paulb@46 | 102 | |
paulb@46 | 103 | """ |
paulb@46 | 104 | Process the given 'module', producing a Module object which contains the |
paulb@88 | 105 | resulting program nodes. If the optional 'name' is provided, the 'name' |
paulb@88 | 106 | attribute is set on the Module object using a value other than None. |
paulb@46 | 107 | """ |
paulb@46 | 108 | |
paulb@118 | 109 | result = self.module = Module(module, 1, name=name) |
paulb@212 | 110 | result.code = self.dispatch(module.node) |
paulb@57 | 111 | return result |
paul@0 | 112 | |
paulb@137 | 113 | # Node transformations. |
paulb@17 | 114 | |
paulb@137 | 115 | def visitAdd(self, add): |
paulb@137 | 116 | return self._visitBinary(add, "__add__", "__radd__") |
paul@1 | 117 | |
paul@1 | 118 | def visitAnd(self, and_): |
paulb@7 | 119 | |
paulb@35 | 120 | """ |
paulb@35 | 121 | Make a subprogram for the 'and_' node and record its contents inside the |
paulb@35 | 122 | subprogram. Convert... |
paulb@35 | 123 | |
paulb@35 | 124 | And (test) |
paulb@35 | 125 | (test) |
paulb@35 | 126 | ... |
paulb@35 | 127 | |
paulb@35 | 128 | ...to: |
paulb@35 | 129 | |
paulb@135 | 130 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@135 | 131 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@35 | 132 | (else) -> ... |
paulb@35 | 133 | """ |
paulb@7 | 134 | |
paulb@118 | 135 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@7 | 136 | self.current_subprograms.append(subprogram) |
paulb@7 | 137 | |
paulb@12 | 138 | # In the subprogram, make instructions which store each operand, test |
paulb@12 | 139 | # for each operand's truth status, and if appropriate return from the |
paulb@12 | 140 | # subprogram with the value of the operand. |
paulb@12 | 141 | |
paulb@7 | 142 | last = and_.nodes[-1] |
paulb@35 | 143 | results = nodes = [] |
paulb@35 | 144 | |
paul@1 | 145 | for node in and_.nodes: |
paulb@7 | 146 | expr = self.dispatch(node) |
paulb@34 | 147 | |
paulb@35 | 148 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 149 | |
paulb@7 | 150 | if node is not last: |
paulb@135 | 151 | nodes += [ |
paulb@135 | 152 | StoreTemp(expr=expr), |
paulb@135 | 153 | Conditional( |
paulb@135 | 154 | test=self._visitNot(LoadTemp()), |
paulb@135 | 155 | body=[ |
paulb@135 | 156 | ReturnFromBlock( |
paulb@135 | 157 | expr=LoadTemp() |
paulb@135 | 158 | ) |
paulb@135 | 159 | ], |
paulb@135 | 160 | else_=[ |
paulb@135 | 161 | ReleaseTemp() |
paulb@135 | 162 | # Subsequent operations go here! |
paulb@135 | 163 | ] |
paulb@135 | 164 | ) |
paulb@135 | 165 | ] |
paulb@35 | 166 | |
paulb@35 | 167 | # Put subsequent operations in the else section of this conditional. |
paulb@35 | 168 | |
paulb@135 | 169 | nodes = nodes[-1].else_ |
paulb@35 | 170 | |
paulb@35 | 171 | # For the last operation, return the result. |
paulb@35 | 172 | |
paulb@7 | 173 | else: |
paulb@135 | 174 | nodes.append(ReturnFromBlock(expr=expr)) |
paulb@35 | 175 | |
paulb@35 | 176 | # Finish the subprogram definition. |
paulb@35 | 177 | |
paulb@35 | 178 | subprogram.code = results |
paulb@7 | 179 | |
paulb@7 | 180 | self.current_subprograms.pop() |
paulb@79 | 181 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@7 | 182 | |
paulb@7 | 183 | # Make an invocation of the subprogram. |
paulb@7 | 184 | |
paulb@204 | 185 | result = InvokeRef(and_, 1, produces_result=1, ref=subprogram) |
paul@1 | 186 | return result |
paul@1 | 187 | |
paulb@202 | 188 | def visitAssert(self, assert_): |
paulb@202 | 189 | if assert_.fail: |
paulb@202 | 190 | fail_args = [self.dispatch(assert_.fail)] |
paulb@202 | 191 | else: |
paulb@202 | 192 | fail_args = [] |
paulb@202 | 193 | |
paulb@202 | 194 | result = Conditional(assert_, 1, |
paulb@202 | 195 | test=self.dispatch(assert_.test), |
paulb@202 | 196 | body=[], |
paulb@202 | 197 | else_=[ |
paulb@202 | 198 | Raise(assert_, |
paulb@202 | 199 | expr=InvokeFunction(assert_, |
paulb@202 | 200 | expr=LoadName(name="AssertionError"), |
paulb@202 | 201 | args=fail_args, |
paulb@202 | 202 | star=None, |
paulb@202 | 203 | dstar=None |
paulb@202 | 204 | ) |
paulb@202 | 205 | ) |
paulb@202 | 206 | ] |
paulb@202 | 207 | ) |
paulb@202 | 208 | |
paulb@202 | 209 | # Make nice annotations for the viewer. |
paulb@202 | 210 | |
paulb@202 | 211 | assert_._raises = result.else_[0].expr |
paulb@202 | 212 | return result |
paulb@202 | 213 | |
paulb@137 | 214 | # Assignments. |
paulb@34 | 215 | |
paulb@137 | 216 | def visitAssAttr(self, assattr, in_sequence=0): |
paulb@137 | 217 | expr = self._visitAssNameOrAttr(assattr, in_sequence) |
paulb@137 | 218 | lvalue = self.dispatch(assattr.expr) |
paulb@137 | 219 | result = StoreAttr(assattr, 1, name=assattr.attrname, lvalue=lvalue, expr=expr) |
paul@1 | 220 | return result |
paul@1 | 221 | |
paulb@137 | 222 | def visitAssign(self, assign): |
paulb@137 | 223 | result = Assign(assign, 1) |
paulb@137 | 224 | store = StoreTemp(expr=self.dispatch(assign.expr)) |
paulb@137 | 225 | release = ReleaseTemp() |
paulb@137 | 226 | result.code = [store] + self.dispatches(assign.nodes, 0) + [release] |
paulb@129 | 227 | return result |
paulb@129 | 228 | |
paulb@137 | 229 | def visitAssList(self, asslist, in_sequence=0): |
paulb@137 | 230 | if not in_sequence: |
paulb@137 | 231 | expr = LoadTemp() |
paulb@137 | 232 | else: |
paulb@185 | 233 | expr = InvokeFunction(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paulb@137 | 234 | result = Assign(asslist, 1) |
paulb@185 | 235 | store = StoreTemp(expr=InvokeFunction(asslist, expr=LoadAttr(name="__iter__", expr=expr))) |
paulb@137 | 236 | release = ReleaseTemp() |
paulb@137 | 237 | result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] |
paulb@32 | 238 | return result |
paulb@32 | 239 | |
paulb@137 | 240 | visitAssTuple = visitAssList |
paulb@135 | 241 | |
paulb@137 | 242 | def _visitAssNameOrAttr(self, node, in_sequence): |
paulb@137 | 243 | if not in_sequence: |
paulb@137 | 244 | return LoadTemp() |
paulb@137 | 245 | else: |
paulb@185 | 246 | return InvokeFunction(node, expr=LoadAttr(expr=LoadTemp(), name="next")) |
paulb@135 | 247 | |
paulb@137 | 248 | def visitAssName(self, assname, in_sequence=0): |
paulb@137 | 249 | expr = self._visitAssNameOrAttr(assname, in_sequence) |
paulb@137 | 250 | result = StoreName(assname, 1, name=assname.name, expr=expr) |
paulb@137 | 251 | return result |
paul@0 | 252 | |
paul@2 | 253 | augassign_methods = { |
paulb@9 | 254 | "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", |
paulb@9 | 255 | "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", |
paulb@9 | 256 | "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" |
paul@2 | 257 | } |
paul@2 | 258 | |
paul@2 | 259 | def visitAugAssign(self, augassign): |
paulb@92 | 260 | |
paulb@92 | 261 | """ |
paulb@92 | 262 | Convert the augmented assignment... |
paulb@92 | 263 | |
paulb@92 | 264 | AugAssign (node) -> Name | Getattr | Slice | Subscript |
paulb@92 | 265 | (op) |
paulb@92 | 266 | (expr) |
paulb@92 | 267 | |
paulb@92 | 268 | ...to: |
paulb@92 | 269 | |
paulb@92 | 270 | Assign (code) -> StoreTemp (expr) -> InvokeFunction (expr) -> LoadAttr (expr) -> <name> |
paulb@92 | 271 | (name) -> <op> |
paulb@92 | 272 | StoreName (name) -> <name> |
paulb@92 | 273 | (expr) -> LoadTemp |
paulb@92 | 274 | ReleaseTemp |
paulb@92 | 275 | """ |
paulb@92 | 276 | |
paulb@79 | 277 | result = Assign(augassign, 1) |
paul@2 | 278 | expr = self.dispatch(augassign.expr) |
paul@2 | 279 | |
paul@2 | 280 | # Simple augmented assignment: name += expr |
paul@2 | 281 | |
paul@2 | 282 | if isinstance(augassign.node, compiler.ast.Name): |
paulb@56 | 283 | result.code = [ |
paulb@101 | 284 | StoreTemp( |
paulb@173 | 285 | expr=InvokeFunction( # referenced below |
paulb@185 | 286 | augassign, |
paulb@59 | 287 | args=[expr], |
paulb@59 | 288 | star=None, |
paulb@59 | 289 | dstar=None, |
paulb@101 | 290 | expr=LoadAttr( |
paulb@56 | 291 | expr=self.dispatch(augassign.node), |
paulb@56 | 292 | name=self.augassign_methods[augassign.op] |
paulb@56 | 293 | ) |
paulb@56 | 294 | ) |
paulb@56 | 295 | ), |
paulb@101 | 296 | StoreName( |
paulb@101 | 297 | expr=LoadTemp(), |
paulb@56 | 298 | name=augassign.node.name), |
paulb@101 | 299 | ReleaseTemp() |
paulb@56 | 300 | ] |
paul@2 | 301 | |
paulb@173 | 302 | # Make nice annotations for the viewer. |
paulb@173 | 303 | |
paulb@173 | 304 | augassign._op_call = result.code[0].expr |
paulb@173 | 305 | |
paulb@9 | 306 | # Complicated augmented assignment: lvalue.attr += expr |
paul@2 | 307 | |
paul@2 | 308 | elif isinstance(augassign.node, compiler.ast.Getattr): |
paulb@6 | 309 | |
paulb@9 | 310 | # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) |
paulb@6 | 311 | |
paulb@56 | 312 | result.code = [ |
paulb@101 | 313 | StoreTemp( |
paulb@56 | 314 | index="expr", |
paulb@56 | 315 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 316 | ), |
paulb@101 | 317 | StoreTemp( |
paulb@173 | 318 | expr=InvokeFunction( # referenced below |
paulb@185 | 319 | augassign, |
paulb@56 | 320 | args=[expr], star=None, dstar=None, |
paulb@101 | 321 | expr=LoadAttr( |
paulb@92 | 322 | expr=LoadAttr(augassign.node, 1, |
paulb@101 | 323 | expr=LoadTemp(index="expr"), |
paulb@56 | 324 | name=augassign.node.attrname |
paulb@56 | 325 | ), |
paulb@56 | 326 | name=self.augassign_methods[augassign.op] |
paulb@56 | 327 | ) |
paulb@56 | 328 | ) |
paulb@56 | 329 | ), |
paulb@101 | 330 | StoreAttr( |
paulb@101 | 331 | expr=LoadTemp(), |
paulb@101 | 332 | lvalue=LoadTemp(index="expr"), |
paulb@56 | 333 | name=augassign.node.attrname |
paulb@56 | 334 | ), |
paulb@101 | 335 | ReleaseTemp(index="expr"), |
paulb@101 | 336 | ReleaseTemp() |
paulb@56 | 337 | ] |
paulb@6 | 338 | |
paulb@173 | 339 | # Make nice annotations for the viewer. |
paulb@173 | 340 | |
paulb@173 | 341 | augassign._op_call = result.code[1].expr |
paulb@173 | 342 | |
paulb@9 | 343 | # Complicated augassign using slices: lvalue[lower:upper] += expr |
paulb@6 | 344 | |
paulb@6 | 345 | elif isinstance(augassign.node, compiler.ast.Slice): |
paulb@9 | 346 | |
paulb@9 | 347 | # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) |
paulb@9 | 348 | |
paulb@56 | 349 | result.code = [ |
paulb@101 | 350 | StoreTemp( |
paulb@56 | 351 | index="expr", |
paulb@56 | 352 | expr=self.dispatch(augassign.node.expr) |
paulb@56 | 353 | ), |
paulb@101 | 354 | StoreTemp( |
paulb@56 | 355 | index="lower", |
paulb@56 | 356 | expr=self.dispatch_or_none(augassign.node.lower) |
paulb@56 | 357 | ), |
paulb@101 | 358 | StoreTemp( |
paulb@56 | 359 | index="upper", |
paulb@56 | 360 | expr=self.dispatch_or_none(augassign.node.upper) |
paulb@56 | 361 | ), |
paulb@101 | 362 | StoreTemp( |
paulb@173 | 363 | expr=InvokeFunction( # referenced below |
paulb@185 | 364 | augassign, |
paulb@56 | 365 | args=[expr], star=None, dstar=None, |
paulb@101 | 366 | expr=LoadAttr( |
paulb@59 | 367 | expr=self._visitSlice( |
paulb@59 | 368 | augassign.node, |
paulb@101 | 369 | LoadTemp(index="expr"), |
paulb@101 | 370 | LoadTemp(index="lower"), |
paulb@101 | 371 | LoadTemp(index="upper"), |
paulb@59 | 372 | "OP_APPLY"), |
paulb@56 | 373 | name=self.augassign_methods[augassign.op] |
paulb@56 | 374 | ) |
paulb@56 | 375 | ) |
paulb@56 | 376 | ), |
paulb@59 | 377 | self._visitSlice( |
paulb@59 | 378 | augassign.node, |
paulb@101 | 379 | LoadTemp(index="expr"), |
paulb@101 | 380 | LoadTemp(index="lower"), |
paulb@101 | 381 | LoadTemp(index="upper"), |
paulb@59 | 382 | "OP_ASSIGN", |
paulb@101 | 383 | LoadTemp() |
paulb@59 | 384 | ), |
paulb@101 | 385 | ReleaseTemp(index="expr"), |
paulb@101 | 386 | ReleaseTemp(index="lower"), |
paulb@101 | 387 | ReleaseTemp(index="upper"), |
paulb@101 | 388 | ReleaseTemp() |
paulb@56 | 389 | ] |
paul@2 | 390 | |
paulb@173 | 391 | # Make nice annotations for the viewer. |
paulb@173 | 392 | |
paulb@173 | 393 | augassign._op_call = result.code[3].expr |
paulb@173 | 394 | |
paulb@10 | 395 | # Complicated augassign using subscripts: lvalue[subs] += expr |
paulb@10 | 396 | |
paulb@10 | 397 | elif isinstance(augassign.node, compiler.ast.Subscript): |
paulb@10 | 398 | |
paulb@10 | 399 | # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) |
paulb@10 | 400 | |
paulb@56 | 401 | result.code = [ |
paulb@101 | 402 | StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), |
paulb@101 | 403 | StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), |
paulb@101 | 404 | StoreTemp( |
paulb@173 | 405 | expr=InvokeFunction( # referenced below |
paulb@185 | 406 | augassign, |
paulb@56 | 407 | args=[expr], star=None, dstar=None, |
paulb@101 | 408 | expr=LoadAttr( |
paulb@59 | 409 | expr=self._visitSubscript( |
paulb@59 | 410 | augassign.node, |
paulb@101 | 411 | LoadTemp(index="expr"), |
paulb@101 | 412 | LoadTemp(index="subs"), |
paulb@59 | 413 | "OP_APPLY" |
paulb@59 | 414 | ), |
paulb@56 | 415 | name=self.augassign_methods[augassign.op] |
paulb@56 | 416 | ) |
paulb@56 | 417 | ) |
paulb@56 | 418 | ), |
paulb@59 | 419 | self._visitSubscript( |
paulb@59 | 420 | augassign.node, |
paulb@101 | 421 | LoadTemp(index="expr"), |
paulb@101 | 422 | LoadTemp(index="subs"), |
paulb@59 | 423 | "OP_ASSIGN", |
paulb@101 | 424 | LoadTemp() |
paulb@59 | 425 | ), |
paulb@101 | 426 | ReleaseTemp(index="expr"), |
paulb@101 | 427 | ReleaseTemp(index="subs"), |
paulb@101 | 428 | ReleaseTemp() |
paulb@56 | 429 | ] |
paulb@10 | 430 | |
paulb@173 | 431 | # Make nice annotations for the viewer. |
paulb@173 | 432 | |
paulb@173 | 433 | augassign._op_call = result.code[2].expr |
paulb@173 | 434 | |
paul@2 | 435 | else: |
paul@2 | 436 | raise NotImplementedError, augassign.node.__class__ |
paul@2 | 437 | |
paul@2 | 438 | return result |
paul@2 | 439 | |
paulb@203 | 440 | def visitBitand(self, bitand): |
paulb@203 | 441 | |
paulb@203 | 442 | """ |
paulb@203 | 443 | Make a subprogram for the 'bitand' node and record its contents inside the |
paulb@203 | 444 | subprogram. Convert... |
paulb@203 | 445 | |
paulb@203 | 446 | Bitand (node) |
paulb@203 | 447 | (node) |
paulb@203 | 448 | ... |
paulb@203 | 449 | |
paulb@203 | 450 | ...to: |
paulb@203 | 451 | |
paulb@203 | 452 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@203 | 453 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@203 | 454 | (else) -> ... |
paulb@203 | 455 | """ |
paulb@203 | 456 | |
paulb@203 | 457 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@203 | 458 | self.current_subprograms.append(subprogram) |
paulb@203 | 459 | |
paulb@203 | 460 | # In the subprogram, make instructions which store each operand, test |
paulb@203 | 461 | # for each operand's truth status, and if appropriate return from the |
paulb@203 | 462 | # subprogram with the value of the operand. |
paulb@203 | 463 | |
paulb@203 | 464 | last = bitand.nodes[-1] |
paulb@203 | 465 | results = nodes = [] |
paulb@203 | 466 | |
paulb@203 | 467 | # Start by storing the first operand. |
paulb@203 | 468 | |
paulb@203 | 469 | nodes += [ |
paulb@203 | 470 | StoreTemp(expr=self.dispatch(bitand.nodes[0])) |
paulb@203 | 471 | ] |
paulb@203 | 472 | |
paulb@203 | 473 | # For viewing purposes, record invocations on the AST node. |
paulb@203 | 474 | |
paulb@203 | 475 | bitand._ops = [] |
paulb@203 | 476 | |
paulb@203 | 477 | for node in bitand.nodes[1:]: |
paulb@203 | 478 | |
paulb@203 | 479 | # Make a new AST-style node to wrap the operation program nodes. |
paulb@203 | 480 | |
paulb@203 | 481 | new_op = Op("&", node) |
paulb@203 | 482 | bitand._ops.append(new_op) |
paulb@203 | 483 | |
paulb@203 | 484 | # Generate the operation involving the previous result and the |
paulb@203 | 485 | # current operand. |
paulb@203 | 486 | |
paulb@203 | 487 | expr = self._visitBinaryOp(new_op, LoadTemp(), self.dispatch(node), "__and__", "__rand__") |
paulb@203 | 488 | |
paulb@203 | 489 | # Return from the subprogram where the test is not satisfied. |
paulb@203 | 490 | |
paulb@203 | 491 | if node is not last: |
paulb@203 | 492 | nodes += [ |
paulb@203 | 493 | StoreTemp(expr=expr), |
paulb@203 | 494 | Conditional( |
paulb@203 | 495 | test=self._visitNot(LoadTemp()), |
paulb@203 | 496 | body=[ |
paulb@203 | 497 | ReturnFromBlock( |
paulb@203 | 498 | expr=LoadTemp() |
paulb@203 | 499 | ) |
paulb@203 | 500 | ], |
paulb@203 | 501 | else_=[ |
paulb@203 | 502 | # Subsequent operations go here! |
paulb@203 | 503 | ] |
paulb@203 | 504 | ) |
paulb@203 | 505 | ] |
paulb@203 | 506 | |
paulb@203 | 507 | # Put subsequent operations in the else section of this conditional. |
paulb@203 | 508 | |
paulb@203 | 509 | nodes = nodes[-1].else_ |
paulb@203 | 510 | |
paulb@203 | 511 | # For the last operation, return the result. |
paulb@203 | 512 | |
paulb@203 | 513 | else: |
paulb@203 | 514 | nodes.append(ReturnFromBlock(expr=expr)) |
paulb@203 | 515 | |
paulb@203 | 516 | # Finish the subprogram definition. |
paulb@203 | 517 | |
paulb@203 | 518 | subprogram.code = results |
paulb@203 | 519 | |
paulb@203 | 520 | self.current_subprograms.pop() |
paulb@203 | 521 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@203 | 522 | |
paulb@203 | 523 | # Make an invocation of the subprogram. |
paulb@203 | 524 | |
paulb@204 | 525 | result = InvokeRef(bitand, 1, produces_result=1, ref=subprogram) |
paulb@203 | 526 | return result |
paulb@203 | 527 | |
paulb@137 | 528 | def visitBreak(self, break_): |
paulb@137 | 529 | result = ReturnFromBlock(break_, 1) |
paul@0 | 530 | return result |
paul@0 | 531 | |
paulb@137 | 532 | def visitCallFunc(self, callfunc): |
paulb@137 | 533 | result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) |
paulb@137 | 534 | if callfunc.star_args is not None: |
paulb@137 | 535 | result.star = self.dispatch(callfunc.star_args) |
paulb@137 | 536 | if callfunc.dstar_args is not None: |
paulb@137 | 537 | result.dstar = self.dispatch(callfunc.dstar_args) |
paulb@137 | 538 | result.expr = self.dispatch(callfunc.node) |
paulb@6 | 539 | return result |
paulb@6 | 540 | |
paulb@19 | 541 | def visitClass(self, class_): |
paulb@129 | 542 | |
paulb@129 | 543 | # Add "object" if the class is not "object" and has an empty bases list. |
paulb@129 | 544 | |
paulb@129 | 545 | if class_.name != "object" and not class_.bases: |
paulb@129 | 546 | bases = [compiler.ast.Name("object")] |
paulb@129 | 547 | else: |
paulb@129 | 548 | bases = class_.bases |
paulb@129 | 549 | |
paulb@129 | 550 | structure = Class(name=class_.name, module=self.module, bases=self.dispatches(bases)) |
paulb@19 | 551 | self.structures.append(structure) |
paulb@172 | 552 | within_class = self.within_class |
paulb@172 | 553 | self.within_class = 1 |
paulb@19 | 554 | |
paulb@24 | 555 | # Make a subprogram which initialises the class structure. |
paulb@24 | 556 | |
paulb@118 | 557 | subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) |
paulb@19 | 558 | self.current_subprograms.append(subprogram) |
paulb@120 | 559 | self.current_structures.append(structure) # mostly for name construction |
paulb@19 | 560 | |
paulb@28 | 561 | # The class is initialised using the code found inside. |
paulb@28 | 562 | |
paulb@135 | 563 | subprogram.code = self.dispatch(class_.code) + [ReturnFromBlock()] |
paulb@19 | 564 | |
paulb@172 | 565 | self.within_class = within_class |
paulb@120 | 566 | self.current_structures.pop() |
paulb@19 | 567 | self.current_subprograms.pop() |
paulb@79 | 568 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@19 | 569 | |
paulb@19 | 570 | # Make a definition of the class associating it with a name. |
paulb@19 | 571 | |
paulb@101 | 572 | result = Assign( |
paulb@56 | 573 | code=[ |
paulb@79 | 574 | StoreName(class_, 1, # defines the class |
paulb@56 | 575 | name=class_.name, |
paulb@101 | 576 | expr=LoadRef(ref=structure) |
paulb@56 | 577 | ), |
paulb@204 | 578 | InvokeRef( |
paulb@185 | 579 | class_, |
paulb@204 | 580 | share_locals=0, # override the local sharing usually in InvokeRef |
paulb@204 | 581 | ref=subprogram |
paulb@56 | 582 | ) |
paulb@56 | 583 | ] |
paulb@56 | 584 | ) |
paulb@19 | 585 | return result |
paulb@19 | 586 | |
paulb@137 | 587 | comparison_methods = { |
paulb@192 | 588 | "==" : ("__eq__", "__ne__"), |
paulb@192 | 589 | "!=" : ("__ne__", "__eq__"), |
paulb@192 | 590 | "<" : ("__lt__", "__gt__"), |
paulb@192 | 591 | "<=" : ("__le__", "__ge__"), |
paulb@192 | 592 | ">=" : ("__ge__", "__le__"), |
paulb@192 | 593 | ">" : ("__gt__", "__lt__"), |
paulb@192 | 594 | "is" : None, |
paulb@201 | 595 | "is not" : None, |
paulb@201 | 596 | "in" : None, |
paulb@201 | 597 | "not in" : None |
paulb@137 | 598 | } |
paulb@137 | 599 | |
paulb@137 | 600 | def visitCompare(self, compare): |
paulb@46 | 601 | |
paulb@46 | 602 | """ |
paulb@137 | 603 | Make a subprogram for the 'compare' node and record its contents inside |
paulb@137 | 604 | the subprogram. Convert... |
paul@0 | 605 | |
paulb@137 | 606 | Compare (expr) |
paulb@137 | 607 | (name/node) |
paulb@137 | 608 | ... |
paulb@35 | 609 | |
paulb@35 | 610 | ...to: |
paulb@35 | 611 | |
paulb@204 | 612 | InvokeRef -> Subprogram -> Conditional (test) -> (body) |
paulb@137 | 613 | (else) -> Conditional (test) -> (body) |
paulb@137 | 614 | (else) -> ... |
paulb@35 | 615 | """ |
paul@0 | 616 | |
paulb@137 | 617 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paul@0 | 618 | self.current_subprograms.append(subprogram) |
paul@0 | 619 | |
paulb@137 | 620 | # In the subprogram, make instructions which invoke a method on the |
paulb@137 | 621 | # first operand of each operand pair and, if appropriate, return with |
paulb@137 | 622 | # the value from that method. |
paulb@137 | 623 | |
paulb@137 | 624 | last = compare.ops[-1] |
paulb@137 | 625 | previous = self.dispatch(compare.expr) |
paulb@137 | 626 | results = nodes = [] |
paulb@137 | 627 | |
paulb@137 | 628 | # For viewing purposes, record invocations on the AST node. |
paulb@137 | 629 | |
paulb@137 | 630 | compare._ops = [] |
paulb@137 | 631 | |
paulb@137 | 632 | for op in compare.ops: |
paulb@137 | 633 | op_name, node = op |
paulb@192 | 634 | |
paulb@192 | 635 | # Make a new AST-style node to wrap the operation program nodes. |
paulb@192 | 636 | |
paulb@192 | 637 | new_op = Op(op_name, node) |
paulb@203 | 638 | compare._ops.append(new_op) |
paulb@203 | 639 | |
paulb@137 | 640 | expr = self.dispatch(node) |
paul@0 | 641 | |
paulb@137 | 642 | # Identify the operation and produce the appropriate method call. |
paul@0 | 643 | |
paulb@192 | 644 | method_names = self.comparison_methods[op_name] |
paulb@192 | 645 | if method_names: |
paulb@192 | 646 | first_name, alternative_name = method_names |
paulb@193 | 647 | invocation = self._visitBinaryCompareOp(new_op, previous, expr, first_name, alternative_name) |
paulb@137 | 648 | |
paulb@137 | 649 | elif op_name == "is": |
paulb@137 | 650 | invocation = InvokeFunction( |
paulb@192 | 651 | new_op, 1, |
paulb@137 | 652 | expr=LoadName(name="__is__"), |
paulb@137 | 653 | args=[previous, expr], |
paulb@137 | 654 | star=None, |
paulb@137 | 655 | dstar=None) |
paul@0 | 656 | |
paulb@137 | 657 | elif op_name == "is not": |
paulb@137 | 658 | invocation = Not( |
paulb@192 | 659 | new_op, 1, |
paulb@137 | 660 | expr=InvokeFunction( |
paulb@192 | 661 | new_op, |
paulb@137 | 662 | expr=LoadName(name="__is__"), |
paulb@137 | 663 | args=[previous, expr], |
paulb@137 | 664 | star=None, |
paulb@137 | 665 | dstar=None) |
paulb@137 | 666 | ) |
paulb@201 | 667 | |
paulb@201 | 668 | elif op_name == "in": |
paulb@201 | 669 | invocation = InvokeFunction( |
paulb@201 | 670 | new_op, 1, |
paulb@201 | 671 | expr=LoadAttr( |
paulb@201 | 672 | expr=previous, |
paulb@201 | 673 | name="__contains__" |
paulb@201 | 674 | ), |
paulb@201 | 675 | args=[expr], |
paulb@201 | 676 | star=None, |
paulb@201 | 677 | dstar=None) |
paulb@201 | 678 | |
paulb@201 | 679 | elif op_name == "not in": |
paulb@201 | 680 | invocation = Not( |
paulb@201 | 681 | new_op, 1, |
paulb@201 | 682 | expr=InvokeFunction( |
paulb@201 | 683 | new_op, |
paulb@201 | 684 | expr=LoadAttr( |
paulb@201 | 685 | expr=previous, |
paulb@201 | 686 | name="__contains__" |
paulb@201 | 687 | ), |
paulb@201 | 688 | args=[expr], |
paulb@201 | 689 | star=None, |
paulb@201 | 690 | dstar=None) |
paulb@201 | 691 | ) |
paulb@201 | 692 | |
paulb@137 | 693 | else: |
paulb@137 | 694 | raise NotImplementedError, op_name |
paulb@34 | 695 | |
paulb@137 | 696 | nodes.append(StoreTemp(expr=invocation)) |
paulb@137 | 697 | |
paulb@137 | 698 | # Return from the subprogram where the test is not satisfied. |
paulb@34 | 699 | |
paulb@137 | 700 | if op is not last: |
paulb@137 | 701 | nodes.append( |
paulb@137 | 702 | Conditional( |
paulb@137 | 703 | test=self._visitNot(LoadTemp()), |
paulb@137 | 704 | body=[ |
paulb@137 | 705 | ReturnFromBlock(expr=LoadTemp()) |
paulb@137 | 706 | ], |
paulb@137 | 707 | else_=[ |
paulb@137 | 708 | ReleaseTemp() |
paulb@137 | 709 | # Subsequent operations go here! |
paulb@137 | 710 | ] |
paulb@137 | 711 | ) |
paulb@137 | 712 | ) |
paulb@35 | 713 | |
paulb@137 | 714 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 715 | |
paulb@137 | 716 | nodes = nodes[-1].else_ |
paulb@137 | 717 | |
paulb@137 | 718 | # For the last operation, return the result. |
paulb@35 | 719 | |
paulb@137 | 720 | else: |
paulb@137 | 721 | nodes.append( |
paulb@137 | 722 | ReturnFromBlock(expr=LoadTemp(release=1)) |
paulb@137 | 723 | ) |
paulb@137 | 724 | |
paulb@137 | 725 | previous = expr |
paulb@35 | 726 | |
paulb@35 | 727 | # Finish the subprogram definition. |
paulb@35 | 728 | |
paulb@137 | 729 | subprogram.code = results |
paul@0 | 730 | |
paul@0 | 731 | self.current_subprograms.pop() |
paulb@79 | 732 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 733 | |
paul@0 | 734 | # Make an invocation of the subprogram. |
paul@0 | 735 | |
paulb@204 | 736 | result = InvokeRef(compare, 1, produces_result=1, ref=subprogram) |
paul@0 | 737 | return result |
paul@0 | 738 | |
paulb@137 | 739 | def visitConst(self, const): |
paulb@154 | 740 | key = "%s-%s" % (const.value.__class__.__name__, const.value) |
paulb@154 | 741 | if not self.constants.has_key(key): |
paulb@154 | 742 | self.constants[key] = Constant(name=repr(const.value), value=const.value) |
paulb@212 | 743 | result = InvokeFunction(const, 1, expr=LoadName(name=self.constants[key].typename)) |
paulb@137 | 744 | return result |
paulb@137 | 745 | |
paulb@137 | 746 | def visitContinue(self, continue_): |
paulb@204 | 747 | result = InvokeRef(continue_, 1, ref=self.current_subprograms[-1]) |
paulb@137 | 748 | return result |
paulb@137 | 749 | |
paulb@137 | 750 | def visitDict(self, dict): |
paulb@212 | 751 | result = InvokeFunction(dict, 1, expr=LoadName(name="dict")) |
paulb@137 | 752 | args = [] |
paulb@137 | 753 | for key, value in dict.items: |
paulb@212 | 754 | tuple = InvokeFunction(dict, expr=LoadName(name="tuple")) |
paulb@137 | 755 | tuple.set_args([self.dispatch(key), self.dispatch(value)]) |
paulb@137 | 756 | args.append(tuple) |
paulb@137 | 757 | result.set_args(args) |
paulb@137 | 758 | return result |
paulb@137 | 759 | |
paulb@137 | 760 | def visitDiscard(self, discard): |
paulb@137 | 761 | return self.dispatch(discard.expr) |
paulb@137 | 762 | |
paulb@137 | 763 | def visitDiv(self, div): |
paulb@137 | 764 | return self._visitBinary(div, "__div__", "__rdiv__") |
paulb@137 | 765 | |
paulb@137 | 766 | def visitFloorDiv(self, floordiv): |
paulb@137 | 767 | return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") |
paulb@137 | 768 | |
paul@0 | 769 | def visitFor(self, for_): |
paul@0 | 770 | |
paulb@35 | 771 | """ |
paulb@35 | 772 | Make a subprogram for the 'for_' node and record its contents inside the |
paulb@35 | 773 | subprogram. Convert... |
paulb@35 | 774 | |
paulb@35 | 775 | For (assign) |
paulb@35 | 776 | (body) |
paulb@35 | 777 | (else) |
paulb@35 | 778 | |
paulb@35 | 779 | ...to: |
paulb@35 | 780 | |
paulb@35 | 781 | Assign (assign #1) |
paulb@35 | 782 | Invoke -> Subprogram -> Try (body) -> (assign #2) |
paulb@35 | 783 | (body) |
paulb@35 | 784 | Invoke subprogram |
paulb@35 | 785 | (handler) -> ... |
paulb@35 | 786 | (else) -> ... |
paulb@35 | 787 | """ |
paul@0 | 788 | |
paulb@201 | 789 | return self._visitFor(for_, self.dispatches(for_.body), for_.else_) |
paulb@201 | 790 | |
paulb@201 | 791 | def _visitFor(self, node, body_stmt, else_=None): |
paulb@201 | 792 | |
paulb@212 | 793 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[]) |
paul@0 | 794 | self.current_subprograms.append(subprogram) |
paul@0 | 795 | |
paulb@34 | 796 | # Always return from conditional sections/subprograms. |
paulb@34 | 797 | |
paulb@201 | 798 | if else_ is not None: |
paulb@201 | 799 | else_stmt = self.dispatch(else_) + [ReturnFromBlock()] |
paulb@17 | 800 | else: |
paulb@135 | 801 | else_stmt = [ReturnFromBlock()] |
paulb@17 | 802 | |
paul@0 | 803 | # Wrap the assignment in a try...except statement. |
paulb@135 | 804 | # Inside the body, add a recursive invocation to the subprogram. |
paul@0 | 805 | |
paulb@135 | 806 | subprogram.code = [ |
paulb@135 | 807 | Try( |
paulb@135 | 808 | body=[ |
paulb@135 | 809 | Assign( |
paulb@135 | 810 | code=[ |
paulb@205 | 811 | StoreTemp( |
paulb@205 | 812 | expr=InvokeFunction(node, |
paulb@205 | 813 | expr=LoadAttr( |
paulb@205 | 814 | expr=LoadTemp(), |
paulb@205 | 815 | name="next" |
paulb@205 | 816 | ) |
paulb@205 | 817 | ) |
paulb@205 | 818 | ), |
paulb@201 | 819 | self.dispatch(node.assign), |
paulb@135 | 820 | ReleaseTemp() |
paulb@135 | 821 | ]) |
paulb@201 | 822 | ] + body_stmt + [ |
paulb@204 | 823 | InvokeRef( |
paulb@201 | 824 | node, |
paulb@204 | 825 | ref=subprogram |
paulb@135 | 826 | ) |
paulb@135 | 827 | ], |
paulb@135 | 828 | handler=[ |
paulb@135 | 829 | Conditional( |
paulb@135 | 830 | test=InvokeFunction( |
paulb@201 | 831 | node, |
paulb@135 | 832 | expr=LoadName(name="isinstance"), |
paulb@135 | 833 | args=[LoadExc(), LoadName(name="StopIteration")], |
paulb@135 | 834 | star=None, |
paulb@135 | 835 | dstar=None), |
paulb@135 | 836 | body=else_stmt, |
paulb@205 | 837 | else_=[ |
paulb@205 | 838 | Raise( |
paulb@205 | 839 | expr=LoadExc() |
paulb@205 | 840 | ) |
paulb@205 | 841 | ] |
paulb@135 | 842 | ) |
paulb@135 | 843 | ], |
paulb@135 | 844 | else_=[], |
paulb@135 | 845 | finally_=[] |
paulb@135 | 846 | ), |
paulb@135 | 847 | ReturnFromBlock() |
paulb@135 | 848 | ] |
paul@0 | 849 | |
paulb@35 | 850 | # Finish the subprogram definition. |
paulb@35 | 851 | |
paul@0 | 852 | self.current_subprograms.pop() |
paulb@79 | 853 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paul@0 | 854 | |
paul@0 | 855 | # Obtain an iterator for the sequence involved. |
paul@0 | 856 | # Then, make an invocation of the subprogram. |
paul@0 | 857 | |
paulb@201 | 858 | result = Assign(node, 1, |
paulb@148 | 859 | code=[ |
paulb@148 | 860 | StoreTemp( |
paulb@148 | 861 | expr=InvokeFunction( |
paulb@201 | 862 | node, |
paulb@148 | 863 | expr=LoadAttr( |
paulb@148 | 864 | name="__iter__", |
paulb@201 | 865 | expr=self.dispatch(node.list) |
paulb@148 | 866 | ) |
paulb@148 | 867 | ) |
paulb@148 | 868 | ), |
paulb@204 | 869 | InvokeRef(node, ref=subprogram), |
paulb@148 | 870 | ReleaseTemp() |
paulb@148 | 871 | ] |
paulb@148 | 872 | ) |
paulb@148 | 873 | |
paulb@148 | 874 | # Make nice annotations for the viewer. |
paulb@148 | 875 | |
paulb@201 | 876 | node._iter_call = result.code[0].expr |
paulb@201 | 877 | node._next_call = subprogram.code[0].body[0].code[0].expr |
paulb@148 | 878 | |
paul@0 | 879 | return result |
paul@0 | 880 | |
paulb@137 | 881 | def visitFrom(self, from_): |
paulb@137 | 882 | result = Assign(from_, 1) |
paulb@137 | 883 | code = [] |
paulb@157 | 884 | _names = [] |
paulb@157 | 885 | code.append( |
paulb@157 | 886 | StoreTemp( |
paulb@158 | 887 | expr=Import(name=from_.modname, alias=1) |
paulb@157 | 888 | ) |
paulb@157 | 889 | ) |
paulb@157 | 890 | from_._modname = code[-1].expr |
paulb@137 | 891 | for name, alias in from_.names: |
paulb@137 | 892 | code.append( |
paulb@137 | 893 | StoreName( |
paulb@137 | 894 | expr=LoadAttr( |
paulb@137 | 895 | expr=LoadTemp(), |
paulb@137 | 896 | name=name), |
paulb@137 | 897 | name=(alias or name) |
paulb@137 | 898 | ) |
paulb@137 | 899 | ) |
paulb@157 | 900 | _names.append(code[-1].expr) |
paulb@137 | 901 | code.append(ReleaseTemp()) |
paulb@137 | 902 | result.code = code |
paulb@157 | 903 | from_._names = _names |
paulb@137 | 904 | return result |
paulb@137 | 905 | |
paulb@137 | 906 | def _visitFunction(self, function, subprogram): |
paulb@137 | 907 | |
paulb@137 | 908 | """ |
paulb@137 | 909 | A common function generator which transforms the given 'function' node |
paulb@137 | 910 | and initialises the given 'subprogram' appropriately. |
paulb@137 | 911 | """ |
paulb@137 | 912 | |
paulb@137 | 913 | # Discover star and dstar parameters. |
paulb@137 | 914 | |
paulb@137 | 915 | if function.flags & 4 != 0: |
paulb@137 | 916 | has_star = 1 |
paulb@137 | 917 | else: |
paulb@137 | 918 | has_star = 0 |
paulb@137 | 919 | if function.flags & 8 != 0: |
paulb@137 | 920 | has_dstar = 1 |
paulb@137 | 921 | else: |
paulb@137 | 922 | has_dstar = 0 |
paulb@137 | 923 | |
paulb@137 | 924 | # Discover the number of defaults and positional parameters. |
paulb@137 | 925 | |
paulb@137 | 926 | ndefaults = len(function.defaults) |
paulb@137 | 927 | npositional = len(function.argnames) - has_star - has_dstar |
paulb@137 | 928 | |
paulb@137 | 929 | # Produce star and dstar parameters with appropriate defaults. |
paulb@137 | 930 | |
paulb@137 | 931 | if has_star: |
paulb@137 | 932 | star = ( |
paulb@137 | 933 | function.argnames[npositional], |
paulb@185 | 934 | self.dispatch(compiler.ast.List([])) |
paulb@137 | 935 | ) |
paulb@137 | 936 | else: |
paulb@137 | 937 | star = None |
paulb@137 | 938 | if has_dstar: |
paulb@137 | 939 | dstar = ( |
paulb@137 | 940 | function.argnames[npositional + has_star], |
paulb@185 | 941 | self.dispatch(compiler.ast.Dict([])) |
paulb@137 | 942 | ) |
paulb@137 | 943 | else: |
paulb@137 | 944 | dstar = None |
paulb@137 | 945 | |
paulb@137 | 946 | params = [] |
paulb@137 | 947 | for i in range(0, npositional - ndefaults): |
paulb@137 | 948 | params.append((function.argnames[i], None)) |
paulb@137 | 949 | |
paulb@137 | 950 | # Process defaults. |
paulb@137 | 951 | |
paulb@137 | 952 | for i in range(0, ndefaults): |
paulb@137 | 953 | default = function.defaults[i] |
paulb@137 | 954 | if default is not None: |
paulb@137 | 955 | params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) |
paulb@137 | 956 | else: |
paulb@137 | 957 | params.append((function.argnames[npositional - ndefaults + i], None)) |
paulb@137 | 958 | |
paulb@137 | 959 | subprogram.params = params |
paulb@137 | 960 | subprogram.star = star |
paulb@137 | 961 | subprogram.dstar = dstar |
paulb@137 | 962 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 963 | |
paulb@137 | 964 | def visitFunction(self, function): |
paulb@137 | 965 | |
paulb@137 | 966 | """ |
paulb@137 | 967 | Make a subprogram for the 'function' and record it outside the main |
paulb@137 | 968 | tree. Produce something like the following: |
paulb@137 | 969 | |
paulb@137 | 970 | StoreName (name) |
paulb@147 | 971 | (expr) -> LoadRef (ref) -> Subprogram (params) |
paulb@147 | 972 | (star) |
paulb@147 | 973 | (dstar) |
paulb@137 | 974 | """ |
paulb@137 | 975 | |
paulb@196 | 976 | subprogram = Subprogram(function, name=function.name, module=self.module, structures=self.current_structures[:], |
paulb@196 | 977 | internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) |
paulb@187 | 978 | |
paulb@196 | 979 | # Make nice annotations for the viewer. |
paulb@196 | 980 | |
paulb@196 | 981 | function._subprogram = subprogram |
paulb@137 | 982 | |
paulb@137 | 983 | self.current_subprograms.append(subprogram) |
paulb@172 | 984 | within_class = self.within_class |
paulb@172 | 985 | self.within_class = 0 |
paulb@172 | 986 | |
paulb@137 | 987 | subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] |
paulb@172 | 988 | |
paulb@172 | 989 | self.within_class = within_class |
paulb@137 | 990 | self.current_subprograms.pop() |
paulb@137 | 991 | self._visitFunction(function, subprogram) |
paulb@137 | 992 | |
paulb@137 | 993 | # Make a definition of the function associating it with a name. |
paulb@137 | 994 | |
paulb@137 | 995 | result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) |
paulb@137 | 996 | return result |
paulb@137 | 997 | |
paulb@137 | 998 | def visitGetattr(self, getattr): |
paulb@137 | 999 | result = LoadAttr(getattr, 1, |
paulb@137 | 1000 | name=getattr.attrname, |
paulb@137 | 1001 | expr=self.dispatch(getattr.expr) |
paulb@137 | 1002 | ) |
paulb@137 | 1003 | return result |
paulb@137 | 1004 | |
paulb@137 | 1005 | def visitGlobal(self, global_): |
paulb@137 | 1006 | result = Global(global_, 1, |
paulb@137 | 1007 | names=global_.names |
paulb@137 | 1008 | ) |
paulb@137 | 1009 | return result |
paulb@137 | 1010 | |
paulb@137 | 1011 | def visitIf(self, if_): |
paulb@137 | 1012 | |
paulb@137 | 1013 | """ |
paulb@137 | 1014 | Make conditionals for each test from an 'if_' AST node, adding the body |
paulb@137 | 1015 | and putting each subsequent test as part of the conditional's else |
paulb@137 | 1016 | section. |
paulb@137 | 1017 | |
paulb@137 | 1018 | Convert... |
paulb@137 | 1019 | |
paulb@137 | 1020 | If (test/body) |
paulb@137 | 1021 | (test/body) |
paulb@137 | 1022 | ... |
paulb@137 | 1023 | (else/body) |
paulb@137 | 1024 | |
paulb@137 | 1025 | ...to: |
paulb@137 | 1026 | |
paulb@137 | 1027 | Conditional (test) -> (body) |
paulb@137 | 1028 | (else) -> Conditional (test) -> (body) |
paulb@137 | 1029 | (else) -> ... |
paulb@137 | 1030 | """ |
paulb@137 | 1031 | |
paulb@137 | 1032 | |
paulb@137 | 1033 | results = nodes = [] |
paulb@137 | 1034 | |
paulb@137 | 1035 | # Produce something like... |
paulb@137 | 1036 | # expr.__bool__() ? body |
paulb@137 | 1037 | |
paulb@137 | 1038 | first = 1 |
paulb@137 | 1039 | for compare, stmt in if_.tests: |
paulb@137 | 1040 | |
paulb@137 | 1041 | # Set the first as the defining node. |
paulb@137 | 1042 | |
paulb@137 | 1043 | test = Conditional(if_, first, |
paulb@137 | 1044 | test=InvokeFunction( |
paulb@185 | 1045 | if_, |
paulb@137 | 1046 | expr=LoadAttr( |
paulb@137 | 1047 | expr=self.dispatch(compare), |
paulb@137 | 1048 | name="__bool__" |
paulb@137 | 1049 | ), |
paulb@148 | 1050 | ) |
paulb@137 | 1051 | ) |
paulb@137 | 1052 | test.body = self.dispatch(stmt) |
paulb@137 | 1053 | nodes.append(test) |
paulb@137 | 1054 | nodes = test.else_ = [] |
paulb@137 | 1055 | first = 0 |
paulb@137 | 1056 | |
paulb@137 | 1057 | # Add the compound statement from any else clause to the end. |
paulb@137 | 1058 | |
paulb@137 | 1059 | if if_.else_ is not None: |
paulb@137 | 1060 | nodes += self.dispatch(if_.else_) |
paulb@137 | 1061 | |
paulb@137 | 1062 | result = results[0] |
paulb@137 | 1063 | return result |
paulb@137 | 1064 | |
paulb@137 | 1065 | def visitImport(self, import_): |
paulb@137 | 1066 | result = Assign(import_, 1) |
paulb@137 | 1067 | code = [] |
paulb@157 | 1068 | _names = [] |
paulb@137 | 1069 | for path, alias in import_.names: |
paulb@158 | 1070 | importer = Import(name=path, alias=alias) |
paulb@137 | 1071 | top = alias or path.split(".")[0] |
paulb@137 | 1072 | code.append(StoreName(expr=importer, name=top)) |
paulb@157 | 1073 | _names.append(code[-1].expr) |
paulb@137 | 1074 | result.code = code |
paulb@157 | 1075 | import_._names = _names |
paulb@137 | 1076 | return result |
paulb@137 | 1077 | |
paulb@137 | 1078 | def visitInvert(self, invert): |
paulb@137 | 1079 | return self._visitUnary(invert, "__invert__") |
paulb@137 | 1080 | |
paulb@137 | 1081 | def visitKeyword(self, keyword): |
paulb@137 | 1082 | result = Keyword(keyword, 1, |
paulb@137 | 1083 | name=keyword.name, |
paulb@137 | 1084 | expr=self.dispatch(keyword.expr) |
paulb@137 | 1085 | ) |
paulb@137 | 1086 | return result |
paulb@137 | 1087 | |
paulb@137 | 1088 | def visitLambda(self, lambda_): |
paulb@137 | 1089 | |
paulb@137 | 1090 | # Make a subprogram for the function and record it outside the main |
paulb@137 | 1091 | # tree. |
paulb@196 | 1092 | |
paulb@196 | 1093 | subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) |
paulb@196 | 1094 | |
paulb@196 | 1095 | # Make nice annotations for the viewer. |
paulb@137 | 1096 | |
paulb@202 | 1097 | lambda_._subprogram = subprogram |
paulb@196 | 1098 | |
paulb@196 | 1099 | # Process the lambda contents. |
paulb@196 | 1100 | |
paulb@137 | 1101 | self.current_subprograms.append(subprogram) |
paulb@137 | 1102 | subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] |
paulb@137 | 1103 | self.current_subprograms.pop() |
paulb@137 | 1104 | self._visitFunction(lambda_, subprogram) |
paulb@137 | 1105 | |
paulb@137 | 1106 | # Get the subprogram reference to the lambda. |
paulb@137 | 1107 | |
paulb@137 | 1108 | return LoadRef(lambda_, 1, ref=subprogram) |
paulb@137 | 1109 | |
paulb@137 | 1110 | def visitList(self, list): |
paulb@204 | 1111 | |
paulb@204 | 1112 | # Make a subprogram for the list construction and record it outside the |
paulb@204 | 1113 | # main tree. |
paulb@204 | 1114 | |
paulb@204 | 1115 | subprogram = Subprogram(list, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=list) |
paulb@204 | 1116 | self.current_subprograms.append(subprogram) |
paulb@204 | 1117 | |
paulb@204 | 1118 | # Make nice annotations for the viewer. |
paulb@204 | 1119 | |
paulb@204 | 1120 | list._subprogram = subprogram |
paulb@204 | 1121 | |
paulb@204 | 1122 | subprogram.code=[ |
paulb@204 | 1123 | StoreTemp( |
paulb@204 | 1124 | expr=InvokeFunction( |
paulb@204 | 1125 | list, |
paulb@204 | 1126 | expr=LoadName( |
paulb@204 | 1127 | name="list" |
paulb@204 | 1128 | ), |
paulb@204 | 1129 | args=[], |
paulb@204 | 1130 | star=None, |
paulb@204 | 1131 | dstar=None |
paulb@204 | 1132 | ) |
paulb@204 | 1133 | ) |
paulb@204 | 1134 | ] |
paulb@204 | 1135 | |
paulb@204 | 1136 | for node in list.nodes: |
paulb@204 | 1137 | subprogram.code.append( |
paulb@204 | 1138 | InvokeFunction( |
paulb@204 | 1139 | list, |
paulb@204 | 1140 | expr=LoadAttr( |
paulb@204 | 1141 | expr=LoadTemp(), |
paulb@204 | 1142 | name="append" |
paulb@204 | 1143 | ), |
paulb@204 | 1144 | args=[self.dispatch(node)], |
paulb@204 | 1145 | star=None, |
paulb@204 | 1146 | dstar=None |
paulb@204 | 1147 | ) |
paulb@204 | 1148 | ) |
paulb@204 | 1149 | |
paulb@204 | 1150 | subprogram.code.append( |
paulb@204 | 1151 | ReturnFromBlock( |
paulb@204 | 1152 | expr=LoadTemp(release=1) |
paulb@204 | 1153 | ) |
paulb@204 | 1154 | ) |
paulb@204 | 1155 | |
paulb@204 | 1156 | self.current_subprograms.pop() |
paulb@204 | 1157 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@204 | 1158 | |
paulb@204 | 1159 | # Make an invocation of the subprogram. |
paulb@204 | 1160 | |
paulb@204 | 1161 | result = InvokeRef(list, 1, |
paulb@204 | 1162 | produces_result=1, |
paulb@204 | 1163 | ref=subprogram |
paulb@204 | 1164 | ) |
paulb@204 | 1165 | return result |
paulb@137 | 1166 | |
paulb@201 | 1167 | def visitListComp(self, listcomp): |
paulb@201 | 1168 | |
paulb@201 | 1169 | # Make a subprogram for the list comprehension and record it outside the |
paulb@201 | 1170 | # main tree. |
paulb@201 | 1171 | |
paulb@240 | 1172 | subprogram = Subprogram(listcomp, name=None, module=self.module, internal=1, returns_value=1, star=None, dstar=None, original_def=listcomp) |
paulb@201 | 1173 | self.current_subprograms.append(subprogram) |
paulb@201 | 1174 | |
paulb@201 | 1175 | # Make nice annotations for the viewer. |
paulb@201 | 1176 | |
paulb@201 | 1177 | listcomp._subprogram = subprogram |
paulb@201 | 1178 | |
paulb@201 | 1179 | # Add a temporary variable. |
paulb@201 | 1180 | # Produce for loops within the subprogram. |
paulb@201 | 1181 | # Return the result. |
paulb@201 | 1182 | |
paulb@201 | 1183 | subprogram.code = [ |
paulb@201 | 1184 | StoreTemp( |
paulb@201 | 1185 | index="listcomp", |
paulb@201 | 1186 | expr=InvokeFunction( |
paulb@201 | 1187 | expr=LoadName(name="list"), |
paulb@201 | 1188 | args=[], |
paulb@201 | 1189 | star=None, |
paulb@201 | 1190 | dstar=None |
paulb@201 | 1191 | ) |
paulb@201 | 1192 | ) |
paulb@201 | 1193 | ] + self._visitListCompFor(listcomp, listcomp.quals) + [ |
paulb@201 | 1194 | ReturnFromBlock( |
paulb@201 | 1195 | expr=LoadTemp( |
paulb@201 | 1196 | index="listcomp", |
paulb@201 | 1197 | release=1 |
paulb@201 | 1198 | ) |
paulb@201 | 1199 | ) |
paulb@201 | 1200 | ] |
paulb@201 | 1201 | |
paulb@201 | 1202 | self.current_subprograms.pop() |
paulb@201 | 1203 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@201 | 1204 | |
paulb@201 | 1205 | # Make an invocation of the subprogram. |
paulb@201 | 1206 | |
paulb@204 | 1207 | result = InvokeRef(listcomp, 1, |
paulb@201 | 1208 | produces_result=1, |
paulb@204 | 1209 | ref=subprogram |
paulb@201 | 1210 | ) |
paulb@201 | 1211 | return result |
paulb@201 | 1212 | |
paulb@201 | 1213 | def _visitListCompFor(self, node, quals): |
paulb@201 | 1214 | qual = quals[0] |
paulb@201 | 1215 | if len(quals) > 1: |
paulb@201 | 1216 | body = self._visitListCompFor(node, quals[1:]) |
paulb@201 | 1217 | if qual.ifs: |
paulb@201 | 1218 | body = self._visitListCompIf(node, qual.ifs, body) |
paulb@202 | 1219 | elif qual.ifs: |
paulb@202 | 1220 | body = self._visitListCompIf(node, qual.ifs) |
paulb@201 | 1221 | else: |
paulb@202 | 1222 | body = self._visitListCompBody(node) |
paulb@201 | 1223 | return [self._visitFor(qual, body)] |
paulb@201 | 1224 | |
paulb@201 | 1225 | def _visitListCompIf(self, node, ifs, expr=None): |
paulb@201 | 1226 | if_ = ifs[0] |
paulb@201 | 1227 | if len(ifs) > 1: |
paulb@201 | 1228 | body = self._visitListCompIf(node, ifs[1:], expr) |
paulb@201 | 1229 | elif expr is None: |
paulb@202 | 1230 | body = self._visitListCompBody(node) |
paulb@201 | 1231 | else: |
paulb@201 | 1232 | body = expr |
paulb@201 | 1233 | return [ |
paulb@201 | 1234 | Conditional(if_, 1, |
paulb@201 | 1235 | test=InvokeFunction( |
paulb@201 | 1236 | if_, |
paulb@201 | 1237 | expr=LoadAttr( |
paulb@201 | 1238 | expr=self.dispatch(if_.test), |
paulb@201 | 1239 | name="__bool__" |
paulb@201 | 1240 | ), |
paulb@201 | 1241 | ), |
paulb@201 | 1242 | body=body, |
paulb@201 | 1243 | else_=[] |
paulb@201 | 1244 | ) |
paulb@201 | 1245 | ] |
paulb@201 | 1246 | |
paulb@202 | 1247 | def _visitListCompBody(self, node): |
paulb@202 | 1248 | return [ |
paulb@202 | 1249 | InvokeFunction( |
paulb@202 | 1250 | expr=LoadAttr( |
paulb@202 | 1251 | expr=LoadTemp(index="listcomp"), |
paulb@202 | 1252 | name="append" |
paulb@202 | 1253 | ), |
paulb@202 | 1254 | args=[self.dispatch(node.expr)], |
paulb@202 | 1255 | star=None, |
paulb@202 | 1256 | dstar=None |
paulb@202 | 1257 | ) |
paulb@202 | 1258 | ] |
paulb@202 | 1259 | |
paulb@157 | 1260 | def visitMod(self, mod): |
paulb@157 | 1261 | return self._visitBinary(mod, "__mod__", "__rmod__") |
paulb@157 | 1262 | |
paulb@137 | 1263 | def visitMul(self, mul): |
paulb@137 | 1264 | return self._visitBinary(mul, "__mul__", "__rmul__") |
paulb@137 | 1265 | |
paulb@137 | 1266 | def visitName(self, name): |
paulb@137 | 1267 | result = LoadName(name, 1, name=name.name) |
paulb@137 | 1268 | return result |
paulb@137 | 1269 | |
paulb@137 | 1270 | def _visitNot(self, expr, not_=None): |
paulb@137 | 1271 | invocation = InvokeFunction( |
paulb@185 | 1272 | not_, # NOTE: May need a real original node. |
paulb@137 | 1273 | expr=LoadAttr( |
paulb@137 | 1274 | expr=expr, |
paulb@137 | 1275 | name="__bool__" |
paulb@137 | 1276 | ), |
paulb@137 | 1277 | ) |
paulb@137 | 1278 | if not_ is not None: |
paulb@137 | 1279 | result = Not(not_, 1, expr=invocation) |
paulb@137 | 1280 | else: |
paulb@137 | 1281 | result = Not(expr=invocation) |
paulb@137 | 1282 | return result |
paulb@137 | 1283 | |
paulb@137 | 1284 | def visitNot(self, not_): |
paulb@137 | 1285 | return self._visitNot(self.dispatch(not_.expr), not_) |
paulb@137 | 1286 | |
paulb@137 | 1287 | def visitOr(self, or_): |
paulb@137 | 1288 | |
paulb@137 | 1289 | """ |
paulb@137 | 1290 | Make a subprogram for the 'or_' node and record its contents inside the |
paulb@137 | 1291 | subprogram. Convert... |
paulb@137 | 1292 | |
paulb@137 | 1293 | Or (test) |
paulb@137 | 1294 | (test) |
paulb@137 | 1295 | ... |
paulb@137 | 1296 | |
paulb@137 | 1297 | ...to: |
paulb@137 | 1298 | |
paulb@137 | 1299 | Subprogram -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1300 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1301 | (else) -> ... |
paulb@137 | 1302 | """ |
paulb@137 | 1303 | |
paulb@137 | 1304 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1305 | self.current_subprograms.append(subprogram) |
paulb@137 | 1306 | |
paulb@137 | 1307 | # In the subprogram, make instructions which store each operand, test |
paulb@137 | 1308 | # for each operand's truth status, and if appropriate return from the |
paulb@137 | 1309 | # subprogram with the value of the operand. |
paulb@137 | 1310 | |
paulb@137 | 1311 | last = or_.nodes[-1] |
paulb@137 | 1312 | results = nodes = [] |
paulb@137 | 1313 | |
paulb@137 | 1314 | for node in or_.nodes: |
paulb@137 | 1315 | expr = self.dispatch(node) |
paulb@137 | 1316 | |
paulb@137 | 1317 | # Return from the subprogram where the test is satisfied. |
paulb@137 | 1318 | |
paulb@137 | 1319 | if node is not last: |
paulb@137 | 1320 | nodes.append(StoreTemp(expr=expr)) |
paulb@185 | 1321 | invocation = InvokeFunction(or_, expr=LoadAttr(expr=LoadTemp(), name="__bool__")) |
paulb@137 | 1322 | test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) |
paulb@137 | 1323 | nodes.append(test) |
paulb@137 | 1324 | |
paulb@137 | 1325 | # Put subsequent operations in the else section of this conditional. |
paulb@137 | 1326 | |
paulb@137 | 1327 | nodes = test.else_ = [ReleaseTemp()] |
paulb@137 | 1328 | |
paulb@137 | 1329 | # For the last operation, return the result. |
paulb@137 | 1330 | |
paulb@137 | 1331 | else: |
paulb@137 | 1332 | nodes.append( |
paulb@137 | 1333 | ReturnFromBlock(expr=expr) |
paulb@137 | 1334 | ) |
paulb@137 | 1335 | |
paulb@137 | 1336 | # Finish the subprogram definition. |
paulb@137 | 1337 | |
paulb@137 | 1338 | subprogram.code = results |
paulb@137 | 1339 | |
paulb@137 | 1340 | self.current_subprograms.pop() |
paulb@137 | 1341 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1342 | |
paulb@137 | 1343 | # Make an invocation of the subprogram. |
paulb@137 | 1344 | |
paulb@204 | 1345 | result = InvokeRef(or_, 1, |
paulb@201 | 1346 | produces_result=1, |
paulb@204 | 1347 | ref=subprogram |
paulb@201 | 1348 | ) |
paulb@137 | 1349 | return result |
paulb@137 | 1350 | |
paulb@137 | 1351 | def visitPass(self, pass_): |
paulb@137 | 1352 | return Pass(pass_, 1) |
paulb@137 | 1353 | |
paulb@141 | 1354 | def visitPower(self, power): |
paulb@141 | 1355 | return self._visitBinary(power, "__pow__", "__rpow__") |
paulb@141 | 1356 | |
paulb@147 | 1357 | def visitPrint(self, print_): |
paulb@147 | 1358 | |
paulb@147 | 1359 | """ |
paulb@147 | 1360 | Convert... |
paulb@147 | 1361 | |
paulb@147 | 1362 | Print (dest) -> |
paulb@147 | 1363 | (nodes) |
paulb@147 | 1364 | |
paulb@147 | 1365 | ...to: |
paulb@147 | 1366 | |
paulb@147 | 1367 | StoreTemp (index) -> "print" |
paulb@147 | 1368 | (expr) -> LoadAttr (expr) -> (dest) |
paulb@147 | 1369 | (name) -> "write" |
paulb@147 | 1370 | InvokeFunction (expr) -> LoadTemp (index) -> "print" |
paulb@147 | 1371 | (args) -> [(node)] |
paulb@147 | 1372 | ReleaseTemp (index) -> "print" |
paulb@147 | 1373 | """ |
paulb@147 | 1374 | |
paulb@147 | 1375 | if print_.dest is not None: |
paulb@147 | 1376 | dest = self.dispatch(print_.dest) |
paulb@147 | 1377 | else: |
paulb@147 | 1378 | dest = self.dispatch(compiler.ast.Name("stdout")) |
paulb@147 | 1379 | |
paulb@147 | 1380 | result = Assign(print_, 1, |
paulb@147 | 1381 | code=[ |
paulb@147 | 1382 | StoreTemp( |
paulb@147 | 1383 | index="print", |
paulb@147 | 1384 | expr=LoadAttr( |
paulb@147 | 1385 | expr=dest, |
paulb@147 | 1386 | name="write" |
paulb@147 | 1387 | ) |
paulb@147 | 1388 | ) |
paulb@147 | 1389 | ] |
paulb@147 | 1390 | ) |
paulb@147 | 1391 | |
paulb@147 | 1392 | for node in print_.nodes: |
paulb@147 | 1393 | result.code.append( |
paulb@147 | 1394 | InvokeFunction( |
paulb@185 | 1395 | print_, |
paulb@147 | 1396 | expr=LoadTemp(index="print"), |
paulb@147 | 1397 | args=[self.dispatch(node)], |
paulb@147 | 1398 | star=None, |
paulb@147 | 1399 | dstar=None |
paulb@147 | 1400 | ) |
paulb@147 | 1401 | ) |
paulb@147 | 1402 | |
paulb@147 | 1403 | result.code.append( |
paulb@147 | 1404 | ReleaseTemp(index="print") |
paulb@147 | 1405 | ) |
paulb@147 | 1406 | |
paulb@147 | 1407 | return result |
paulb@147 | 1408 | |
paulb@147 | 1409 | def visitPrintnl(self, printnl): |
paulb@147 | 1410 | result = self.visitPrint(printnl) |
paulb@147 | 1411 | result.code.insert( |
paulb@147 | 1412 | len(result.code) - 1, |
paulb@147 | 1413 | InvokeFunction( |
paulb@185 | 1414 | printnl, |
paulb@147 | 1415 | expr=LoadTemp(index="print"), |
paulb@147 | 1416 | args=[self.dispatch(compiler.ast.Const("\n"))], |
paulb@147 | 1417 | star=None, |
paulb@147 | 1418 | dstar=None |
paulb@147 | 1419 | ) |
paulb@147 | 1420 | ) |
paulb@147 | 1421 | return result |
paulb@147 | 1422 | |
paulb@137 | 1423 | def visitRaise(self, raise_): |
paulb@137 | 1424 | result = Raise(raise_, 1) |
paulb@137 | 1425 | if raise_.expr2 is None: |
paulb@137 | 1426 | result.expr = self.dispatch(raise_.expr1) |
paulb@137 | 1427 | else: |
paulb@137 | 1428 | result.expr = InvokeFunction( |
paulb@185 | 1429 | raise_, |
paulb@137 | 1430 | expr=self.dispatch(raise_.expr1), |
paulb@137 | 1431 | args=[self.dispatch(raise_.expr2)], |
paulb@137 | 1432 | star=None, |
paulb@137 | 1433 | dstar=None |
paulb@137 | 1434 | ) |
paulb@137 | 1435 | if raise_.expr3 is not None: |
paulb@137 | 1436 | result.traceback = self.dispatch(raise_.expr3) |
paulb@137 | 1437 | else: |
paulb@137 | 1438 | result.traceback = None |
paulb@137 | 1439 | return result |
paulb@137 | 1440 | |
paulb@137 | 1441 | def visitReturn(self, return_): |
paulb@137 | 1442 | result = ReturnFromFunction(return_, 1, |
paulb@137 | 1443 | expr=self.dispatch(return_.value) |
paulb@137 | 1444 | ) |
paulb@137 | 1445 | return result |
paulb@137 | 1446 | |
paulb@137 | 1447 | def _visitSlice(self, slice, expr, lower, upper, flags, value=None): |
paulb@137 | 1448 | if flags == "OP_ASSIGN": |
paulb@137 | 1449 | result = InvokeFunction(slice, 1, |
paulb@137 | 1450 | expr=LoadAttr( |
paulb@137 | 1451 | expr=expr, |
paulb@137 | 1452 | name="__setslice__" |
paulb@137 | 1453 | ), |
paulb@137 | 1454 | star=None, |
paulb@137 | 1455 | dstar=None, |
paulb@137 | 1456 | args=[lower, upper, value] |
paulb@137 | 1457 | ) |
paulb@137 | 1458 | elif flags == "OP_APPLY": |
paulb@137 | 1459 | args = [] |
paulb@137 | 1460 | result = InvokeFunction(slice, 1, |
paulb@137 | 1461 | expr=LoadAttr( |
paulb@137 | 1462 | expr=expr, |
paulb@137 | 1463 | name="__getslice__" |
paulb@137 | 1464 | ), |
paulb@137 | 1465 | star=None, |
paulb@137 | 1466 | dstar=None, |
paulb@137 | 1467 | args=[lower, upper] |
paulb@137 | 1468 | ) |
paulb@137 | 1469 | elif flags == "OP_DELETE": |
paulb@137 | 1470 | args = [] |
paulb@137 | 1471 | result = InvokeFunction(slice, 1, |
paulb@137 | 1472 | expr=LoadAttr( |
paulb@137 | 1473 | expr=expr, |
paulb@137 | 1474 | name="__delslice__" |
paulb@137 | 1475 | ), |
paulb@137 | 1476 | star=None, |
paulb@137 | 1477 | dstar=None, |
paulb@137 | 1478 | args=[lower, upper] |
paulb@137 | 1479 | ) |
paulb@137 | 1480 | else: |
paulb@137 | 1481 | raise NotImplementedError, flags |
paulb@137 | 1482 | |
paulb@137 | 1483 | return result |
paulb@137 | 1484 | |
paulb@137 | 1485 | def visitSlice(self, slice, in_sequence=0): |
paulb@137 | 1486 | return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), |
paulb@137 | 1487 | self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) |
paulb@137 | 1488 | |
paulb@201 | 1489 | def visitSliceobj(self, sliceobj): |
paulb@201 | 1490 | return InvokeFunction(sliceobj, 1, |
paulb@201 | 1491 | expr=LoadName(name="slice"), |
paulb@201 | 1492 | args=self.dispatches(sliceobj.nodes), |
paulb@201 | 1493 | star=None, |
paulb@201 | 1494 | dstar=None |
paulb@201 | 1495 | ) |
paulb@201 | 1496 | |
paulb@137 | 1497 | def visitStmt(self, stmt): |
paulb@137 | 1498 | return self.dispatches(stmt.nodes) |
paulb@137 | 1499 | |
paulb@137 | 1500 | def visitSub(self, sub): |
paulb@137 | 1501 | return self._visitBinary(sub, "__sub__", "__rsub__") |
paulb@137 | 1502 | |
paulb@137 | 1503 | def _visitSubscript(self, subscript, expr, subs, flags, value=None): |
paulb@137 | 1504 | if flags == "OP_ASSIGN": |
paulb@137 | 1505 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1506 | expr=LoadAttr( |
paulb@137 | 1507 | expr=expr, |
paulb@137 | 1508 | name="__setitem__" |
paulb@137 | 1509 | ), |
paulb@137 | 1510 | star=None, |
paulb@137 | 1511 | dstar=None, |
paulb@137 | 1512 | args=[subs, value] |
paulb@137 | 1513 | ) |
paulb@137 | 1514 | elif flags == "OP_APPLY": |
paulb@137 | 1515 | args = [] |
paulb@137 | 1516 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1517 | expr=LoadAttr( |
paulb@137 | 1518 | expr=expr, |
paulb@137 | 1519 | name="__getitem__" |
paulb@137 | 1520 | ), |
paulb@137 | 1521 | star=None, |
paulb@137 | 1522 | dstar=None, |
paulb@137 | 1523 | args=[subs] |
paulb@137 | 1524 | ) |
paulb@137 | 1525 | elif flags == "OP_DELETE": |
paulb@137 | 1526 | args = [] |
paulb@137 | 1527 | result = InvokeFunction(subscript, 1, |
paulb@137 | 1528 | expr=LoadAttr( |
paulb@137 | 1529 | expr=expr, |
paulb@137 | 1530 | name="__delitem__" |
paulb@137 | 1531 | ), |
paulb@137 | 1532 | star=None, |
paulb@137 | 1533 | dstar=None, |
paulb@137 | 1534 | args=[subs] |
paulb@137 | 1535 | ) |
paulb@137 | 1536 | else: |
paulb@137 | 1537 | raise NotImplementedError, flags |
paulb@137 | 1538 | |
paulb@137 | 1539 | return result |
paulb@137 | 1540 | |
paulb@137 | 1541 | def _visitSubscriptSubs(self, node, subs): |
paulb@137 | 1542 | if len(subs) == 1: |
paulb@137 | 1543 | return self.dispatch(subs[0]) |
paulb@137 | 1544 | else: |
paulb@137 | 1545 | return InvokeFunction(node, 1, |
paulb@137 | 1546 | expr=LoadName(name="tuple"), |
paulb@137 | 1547 | args=self.dispatches(subs), |
paulb@137 | 1548 | star=None, |
paulb@137 | 1549 | dstar=None |
paulb@137 | 1550 | ) |
paulb@137 | 1551 | |
paulb@137 | 1552 | def visitSubscript(self, subscript, in_sequence=0): |
paulb@137 | 1553 | return self._visitSubscript( |
paulb@137 | 1554 | subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, |
paulb@137 | 1555 | self._visitAssNameOrAttr(subscript, in_sequence) |
paulb@137 | 1556 | ) |
paulb@137 | 1557 | |
paulb@137 | 1558 | def visitTryExcept(self, tryexcept): |
paulb@137 | 1559 | |
paulb@137 | 1560 | """ |
paulb@137 | 1561 | Make conditionals for each handler associated with a 'tryexcept' node. |
paulb@137 | 1562 | |
paulb@137 | 1563 | Convert... |
paulb@137 | 1564 | |
paulb@137 | 1565 | TryExcept (body) |
paulb@137 | 1566 | (else) |
paulb@137 | 1567 | (spec/assign/stmt) |
paulb@137 | 1568 | ... |
paulb@137 | 1569 | |
paulb@137 | 1570 | ...to: |
paulb@137 | 1571 | |
paulb@137 | 1572 | Try (body) |
paulb@137 | 1573 | (else) |
paulb@137 | 1574 | (handler) -> Conditional (test) -> (stmt) |
paulb@190 | 1575 | (body) -> ResetExc ... |
paulb@137 | 1576 | (else) -> Conditional (test) -> (stmt) |
paulb@190 | 1577 | (body) -> ResetExc ... |
paulb@137 | 1578 | (else) -> ... |
paulb@137 | 1579 | """ |
paulb@137 | 1580 | |
paulb@137 | 1581 | result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) |
paulb@137 | 1582 | |
paulb@137 | 1583 | if tryexcept.body is not None: |
paulb@137 | 1584 | result.body = self.dispatch(tryexcept.body) |
paulb@137 | 1585 | if tryexcept.else_ is not None: |
paulb@137 | 1586 | result.else_ = self.dispatch(tryexcept.else_) |
paulb@137 | 1587 | |
paulb@137 | 1588 | results = nodes = [] |
paulb@137 | 1589 | catch_all = 0 |
paulb@137 | 1590 | |
paulb@137 | 1591 | for spec, assign, stmt in tryexcept.handlers: |
paulb@137 | 1592 | |
paulb@137 | 1593 | # If no specification exists, produce an unconditional block. |
paulb@137 | 1594 | |
paulb@137 | 1595 | if spec is None: |
paulb@137 | 1596 | nodes += self.dispatch(stmt) |
paulb@137 | 1597 | catch_all = 1 |
paulb@137 | 1598 | |
paulb@137 | 1599 | # Produce an exception value check. |
paulb@137 | 1600 | |
paulb@137 | 1601 | else: |
paulb@137 | 1602 | test = Conditional( |
paulb@137 | 1603 | isolate_test=1, |
paulb@193 | 1604 | test=CheckType(expr=LoadExc(), choices=self._visitTryExcept(spec)) |
paulb@137 | 1605 | ) |
paulb@137 | 1606 | test.body = [] |
paulb@137 | 1607 | |
paulb@137 | 1608 | if assign is not None: |
paulb@137 | 1609 | test.body.append( |
paulb@137 | 1610 | Assign( |
paulb@137 | 1611 | code=[ |
paulb@137 | 1612 | StoreTemp(expr=LoadExc()), |
paulb@137 | 1613 | self.dispatch(assign), |
paulb@137 | 1614 | ReleaseTemp() |
paulb@137 | 1615 | ] |
paulb@137 | 1616 | ) |
paulb@137 | 1617 | ) |
paulb@137 | 1618 | |
paulb@190 | 1619 | test.body += [ResetExc()] + self.dispatch(stmt) |
paulb@137 | 1620 | nodes.append(test) |
paulb@137 | 1621 | nodes = test.else_ = [] |
paulb@137 | 1622 | |
paulb@137 | 1623 | # Add a raise operation to deal with unhandled exceptions. |
paulb@137 | 1624 | |
paulb@137 | 1625 | if not catch_all: |
paulb@137 | 1626 | nodes.append( |
paulb@137 | 1627 | Raise( |
paulb@137 | 1628 | expr=LoadExc()) |
paulb@137 | 1629 | ) |
paulb@137 | 1630 | |
paulb@137 | 1631 | result.handler = results |
paulb@137 | 1632 | return result |
paulb@137 | 1633 | |
paulb@137 | 1634 | def _visitTryExcept(self, spec): |
paulb@137 | 1635 | |
paulb@137 | 1636 | "Return a list of nodes for the given exception type 'spec'." |
paulb@137 | 1637 | |
paulb@137 | 1638 | if isinstance(spec, compiler.ast.Tuple): |
paulb@137 | 1639 | nodes = [] |
paulb@137 | 1640 | for node in spec.nodes: |
paulb@137 | 1641 | nodes += self._visitTryExcept(node) |
paulb@137 | 1642 | else: |
paulb@137 | 1643 | nodes = [self.dispatch(spec)] |
paulb@137 | 1644 | return nodes |
paul@0 | 1645 | |
paul@0 | 1646 | def visitTryFinally(self, tryfinally): |
paulb@79 | 1647 | result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) |
paul@0 | 1648 | if tryfinally.body is not None: |
paul@0 | 1649 | result.body = self.dispatch(tryfinally.body) |
paul@0 | 1650 | if tryfinally.final is not None: |
paul@0 | 1651 | result.finally_ = self.dispatch(tryfinally.final) |
paul@0 | 1652 | return result |
paul@0 | 1653 | |
paulb@137 | 1654 | def visitTuple(self, tuple): |
paulb@219 | 1655 | |
paulb@219 | 1656 | "Make a MakeTuple node containing the original 'tuple' contents." |
paulb@219 | 1657 | |
paulb@219 | 1658 | result = MakeTuple(tuple, 1, |
paulb@219 | 1659 | nodes=self.dispatches(tuple.nodes) |
paulb@219 | 1660 | ) |
paulb@219 | 1661 | return result |
paulb@137 | 1662 | |
paulb@137 | 1663 | def visitUnaryAdd(self, unaryadd): |
paulb@137 | 1664 | return self._visitUnary(unaryadd, "__pos__") |
paulb@137 | 1665 | |
paulb@137 | 1666 | def visitUnarySub(self, unarysub): |
paulb@137 | 1667 | return self._visitUnary(unarysub, "__neg__") |
paulb@137 | 1668 | |
paulb@137 | 1669 | def visitWhile(self, while_): |
paulb@137 | 1670 | |
paulb@137 | 1671 | """ |
paulb@137 | 1672 | Make a subprogram for the 'while' node and record its contents inside the |
paulb@137 | 1673 | subprogram. Convert... |
paulb@137 | 1674 | |
paulb@137 | 1675 | While (test) -> (body) |
paulb@137 | 1676 | (else) |
paulb@137 | 1677 | |
paulb@137 | 1678 | ...to: |
paulb@137 | 1679 | |
paulb@137 | 1680 | Subprogram -> Conditional (test) -> (body) -> Invoke subprogram |
paulb@137 | 1681 | (else) -> Conditional (test) -> ReturnFromBlock ... |
paulb@137 | 1682 | (else) -> ... |
paulb@137 | 1683 | """ |
paulb@137 | 1684 | |
paulb@137 | 1685 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) |
paulb@137 | 1686 | self.current_subprograms.append(subprogram) |
paulb@137 | 1687 | |
paulb@137 | 1688 | # Include a conditional statement in the subprogram. |
paulb@137 | 1689 | # Inside the conditional, add a recursive invocation to the subprogram |
paulb@137 | 1690 | # if the test condition was satisfied. |
paulb@137 | 1691 | # Return within the main section of the loop. |
paulb@137 | 1692 | |
paulb@147 | 1693 | test = Conditional( |
paulb@147 | 1694 | test=InvokeFunction( |
paulb@185 | 1695 | while_, |
paulb@147 | 1696 | expr=LoadAttr( |
paulb@147 | 1697 | expr=self.dispatch(while_.test), |
paulb@147 | 1698 | name="__bool__"), |
paulb@147 | 1699 | ), |
paulb@147 | 1700 | body=self.dispatch(while_.body) + [ |
paulb@204 | 1701 | InvokeRef( |
paulb@185 | 1702 | while_, |
paulb@204 | 1703 | ref=subprogram |
paulb@147 | 1704 | ), |
paulb@147 | 1705 | ReturnFromBlock() |
paulb@147 | 1706 | ], |
paulb@147 | 1707 | else_=[] |
paulb@147 | 1708 | ) |
paulb@137 | 1709 | |
paulb@137 | 1710 | # Provide the else section, if present, along with an explicit return. |
paulb@137 | 1711 | |
paulb@137 | 1712 | if while_.else_ is not None: |
paulb@137 | 1713 | test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] |
paulb@137 | 1714 | |
paulb@137 | 1715 | # Finish the subprogram definition. |
paulb@137 | 1716 | |
paulb@137 | 1717 | subprogram.code = [test] |
paulb@137 | 1718 | |
paulb@137 | 1719 | self.current_subprograms.pop() |
paulb@137 | 1720 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1721 | |
paulb@137 | 1722 | # Make an invocation of the subprogram. |
paulb@137 | 1723 | |
paulb@204 | 1724 | result = InvokeRef(while_, 1, ref=subprogram) |
paulb@148 | 1725 | |
paulb@148 | 1726 | # Make nice annotations for the viewer. |
paulb@148 | 1727 | |
paulb@148 | 1728 | while_._test_call = subprogram.code[0].test |
paulb@148 | 1729 | |
paulb@137 | 1730 | return result |
paulb@137 | 1731 | |
paulb@203 | 1732 | # NOTE: Not actually supported. |
paulb@203 | 1733 | # NOTE: Virtually the same as visitReturn... |
paulb@203 | 1734 | |
paulb@203 | 1735 | def visitYield(self, yield_): |
paulb@203 | 1736 | result = Yield(yield_, 1, |
paulb@203 | 1737 | expr=self.dispatch(yield_.value) |
paulb@203 | 1738 | ) |
paulb@203 | 1739 | return result |
paulb@203 | 1740 | |
paulb@137 | 1741 | # Convenience methods. |
paulb@137 | 1742 | |
paulb@137 | 1743 | def _visitBinary(self, binary, left_name, right_name): |
paulb@192 | 1744 | return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name) |
paulb@192 | 1745 | |
paulb@193 | 1746 | def _visitBinaryCompareOp(self, binary, left, right, left_name, right_name): |
paulb@193 | 1747 | |
paulb@193 | 1748 | """ |
paulb@193 | 1749 | Emulate the current mechanisms by producing nodes as follows: |
paulb@193 | 1750 | |
paulb@204 | 1751 | InvokeRef -> Subprogram -> StoreTemp (expr) -> x.__lt__(y) |
paulb@193 | 1752 | Conditional (test) -> __is__(LoadTemp, NotImplemented) |
paulb@193 | 1753 | (body) -> ReleaseTemp |
paulb@193 | 1754 | StoreTemp (expr) -> y.__gt__(x) |
paulb@193 | 1755 | Conditional (test) -> __is__(LoadTemp, NotImplemented) |
paulb@193 | 1756 | (body) -> ReturnFromBlock (expr) -> False |
paulb@193 | 1757 | (else) -> ReturnFromBlock (expr) -> LoadTemp |
paulb@193 | 1758 | (else) -> ReturnFromBlock (expr) -> LoadTemp |
paulb@193 | 1759 | """ |
paulb@193 | 1760 | |
paulb@193 | 1761 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@193 | 1762 | self.current_subprograms.append(subprogram) |
paulb@193 | 1763 | |
paulb@193 | 1764 | subprogram.code = [ |
paulb@193 | 1765 | StoreTemp( |
paulb@227 | 1766 | expr=left, |
paulb@227 | 1767 | index="left" |
paulb@227 | 1768 | ), |
paulb@227 | 1769 | StoreTemp( |
paulb@227 | 1770 | expr=right, |
paulb@227 | 1771 | index="right" |
paulb@227 | 1772 | ), |
paulb@227 | 1773 | StoreTemp( |
paulb@193 | 1774 | expr=InvokeFunction( |
paulb@193 | 1775 | binary, |
paulb@227 | 1776 | expr=LoadAttr( |
paulb@227 | 1777 | expr=LoadTemp(index="left"), |
paulb@227 | 1778 | name=left_name), |
paulb@227 | 1779 | args=[ |
paulb@227 | 1780 | LoadTemp(index="right") |
paulb@227 | 1781 | ], |
paulb@193 | 1782 | star=None, |
paulb@193 | 1783 | dstar=None) |
paulb@193 | 1784 | ), |
paulb@193 | 1785 | Conditional( |
paulb@193 | 1786 | isolate_test=1, |
paulb@193 | 1787 | test=CheckType( |
paulb@193 | 1788 | expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1789 | ), |
paulb@193 | 1790 | body=[ |
paulb@193 | 1791 | ReleaseTemp(), |
paulb@193 | 1792 | StoreTemp( |
paulb@193 | 1793 | expr=InvokeFunction( |
paulb@193 | 1794 | binary, |
paulb@227 | 1795 | expr=LoadAttr( |
paulb@227 | 1796 | expr=LoadTemp(index="right"), |
paulb@227 | 1797 | name=right_name), |
paulb@227 | 1798 | args=[ |
paulb@227 | 1799 | LoadTemp(index="left") |
paulb@227 | 1800 | ], |
paulb@193 | 1801 | star=None, |
paulb@193 | 1802 | dstar=None) |
paulb@193 | 1803 | ), |
paulb@193 | 1804 | Conditional( |
paulb@193 | 1805 | isolate_test=1, |
paulb@193 | 1806 | test=CheckType( |
paulb@193 | 1807 | expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1808 | ), |
paulb@193 | 1809 | body=[ |
paulb@193 | 1810 | ReturnFromBlock( |
paulb@193 | 1811 | expr=LoadName(name="False") |
paulb@193 | 1812 | ) |
paulb@193 | 1813 | ], |
paulb@193 | 1814 | else_=[ |
paulb@193 | 1815 | CheckType( |
paulb@193 | 1816 | inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1817 | ), |
paulb@193 | 1818 | ReturnFromBlock( |
paulb@193 | 1819 | expr=LoadTemp() |
paulb@193 | 1820 | ) |
paulb@193 | 1821 | ] |
paulb@193 | 1822 | ) |
paulb@193 | 1823 | ], |
paulb@193 | 1824 | else_=[ |
paulb@193 | 1825 | CheckType( |
paulb@193 | 1826 | inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] |
paulb@193 | 1827 | ), |
paulb@193 | 1828 | ReturnFromBlock( |
paulb@193 | 1829 | expr=LoadTemp() |
paulb@193 | 1830 | ) |
paulb@193 | 1831 | ] |
paulb@193 | 1832 | ) |
paulb@193 | 1833 | ] |
paulb@193 | 1834 | |
paulb@193 | 1835 | self.current_subprograms.pop() |
paulb@193 | 1836 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@193 | 1837 | |
paulb@204 | 1838 | result = InvokeRef( |
paulb@193 | 1839 | binary, |
paulb@193 | 1840 | produces_result=1, |
paulb@204 | 1841 | ref=subprogram |
paulb@193 | 1842 | ) |
paulb@193 | 1843 | |
paulb@193 | 1844 | # Make nice annotations for the viewer. |
paulb@193 | 1845 | |
paulb@227 | 1846 | binary._left_call = subprogram.code[2].expr |
paulb@227 | 1847 | binary._right_call = subprogram.code[3].body[1].expr |
paulb@193 | 1848 | |
paulb@193 | 1849 | return result |
paulb@193 | 1850 | |
paulb@192 | 1851 | def _visitBinaryOp(self, binary, left, right, left_name, right_name): |
paulb@137 | 1852 | |
paulb@137 | 1853 | """ |
paulb@137 | 1854 | Emulate the current mechanisms by producing nodes as follows: |
paulb@137 | 1855 | |
paulb@204 | 1856 | InvokeRef -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) |
paulb@137 | 1857 | (else) |
paulb@193 | 1858 | (handler) -> Conditional (test) -> CheckType (expr) -> LoadExc |
paulb@137 | 1859 | (choices) -> LoadName TypeError |
paulb@137 | 1860 | (body) -> ReturnFromBlock (expr) -> y.__radd__(x) |
paulb@137 | 1861 | (else) |
paulb@137 | 1862 | """ |
paulb@137 | 1863 | |
paulb@137 | 1864 | subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) |
paulb@137 | 1865 | self.current_subprograms.append(subprogram) |
paulb@137 | 1866 | |
paulb@137 | 1867 | subprogram.code = [ |
paulb@227 | 1868 | StoreTemp( |
paulb@227 | 1869 | expr=left, |
paulb@227 | 1870 | index="left" |
paulb@227 | 1871 | ), |
paulb@227 | 1872 | StoreTemp( |
paulb@227 | 1873 | expr=right, |
paulb@227 | 1874 | index="right" |
paulb@227 | 1875 | ), |
paulb@137 | 1876 | Try(binary, 1, |
paulb@137 | 1877 | body=[ |
paulb@137 | 1878 | ReturnFromBlock( |
paulb@137 | 1879 | expr=InvokeFunction( |
paulb@185 | 1880 | binary, |
paulb@227 | 1881 | expr=LoadAttr(expr=LoadTemp(index="left"), name=left_name), |
paulb@227 | 1882 | args=[LoadTemp(index="right")], |
paulb@137 | 1883 | star=None, |
paulb@137 | 1884 | dstar=None) |
paulb@137 | 1885 | ) |
paulb@137 | 1886 | ], |
paulb@137 | 1887 | else_=[], |
paulb@137 | 1888 | finally_=[], |
paulb@137 | 1889 | handler=[ |
paulb@137 | 1890 | Conditional( |
paulb@193 | 1891 | test=CheckType(expr=LoadExc(), choices=[LoadName(name="TypeError")]), |
paulb@137 | 1892 | body=[ |
paulb@137 | 1893 | ReturnFromBlock( |
paulb@137 | 1894 | expr=InvokeFunction( |
paulb@185 | 1895 | binary, |
paulb@227 | 1896 | expr=LoadAttr(expr=LoadTemp(index="right"), name=right_name), |
paulb@227 | 1897 | args=[LoadTemp(index="left")], |
paulb@137 | 1898 | star=None, |
paulb@137 | 1899 | dstar=None) |
paulb@137 | 1900 | ) |
paulb@137 | 1901 | ], |
paulb@137 | 1902 | else_=[] |
paulb@137 | 1903 | ) |
paulb@137 | 1904 | ] |
paulb@137 | 1905 | ) |
paulb@137 | 1906 | ] |
paulb@137 | 1907 | |
paulb@137 | 1908 | self.current_subprograms.pop() |
paulb@137 | 1909 | self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram |
paulb@137 | 1910 | |
paulb@204 | 1911 | result = InvokeRef( |
paulb@185 | 1912 | binary, |
paulb@148 | 1913 | produces_result=1, |
paulb@204 | 1914 | ref=subprogram |
paulb@148 | 1915 | ) |
paulb@148 | 1916 | |
paulb@148 | 1917 | # Make nice annotations for the viewer. |
paulb@148 | 1918 | |
paulb@227 | 1919 | binary._left_call = subprogram.code[2].body[0].expr |
paulb@227 | 1920 | binary._right_call = subprogram.code[2].handler[0].body[0].expr |
paulb@148 | 1921 | |
paulb@137 | 1922 | return result |
paulb@137 | 1923 | |
paulb@137 | 1924 | def _visitBuiltin(self, builtin, name): |
paulb@212 | 1925 | result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes)) |
paulb@137 | 1926 | return result |
paulb@137 | 1927 | |
paulb@137 | 1928 | def _visitUnary(self, unary, name): |
paulb@148 | 1929 | result = InvokeFunction(unary, 1, |
paulb@137 | 1930 | expr=LoadAttr( |
paulb@137 | 1931 | expr=self.dispatch(unary.expr), |
paulb@137 | 1932 | name=name |
paulb@148 | 1933 | ) |
paulb@137 | 1934 | ) |
paulb@137 | 1935 | |
paulb@148 | 1936 | # Make nice annotations for the viewer. |
paulb@148 | 1937 | |
paulb@148 | 1938 | unary._unary_call = result |
paulb@148 | 1939 | |
paulb@148 | 1940 | return result |
paulb@148 | 1941 | |
paulb@57 | 1942 | # Convenience functions. |
paulb@57 | 1943 | |
paulb@158 | 1944 | def simplify(filename, builtins=0, module_name=None): |
paulb@57 | 1945 | |
paulb@57 | 1946 | """ |
paulb@88 | 1947 | Simplify the module stored in the file with the given 'filename'. |
paulb@88 | 1948 | |
paulb@88 | 1949 | If the optional 'builtins' parameter is set to a true value (the default |
paulb@88 | 1950 | being a false value), then the module is considered as the builtins module. |
paulb@57 | 1951 | """ |
paulb@57 | 1952 | |
paulb@57 | 1953 | simplifier = Simplifier(builtins) |
paulb@57 | 1954 | module = compiler.parseFile(filename) |
paulb@57 | 1955 | compiler.misc.set_filename(filename, module) |
paulb@88 | 1956 | if builtins: |
paulb@158 | 1957 | name = module_name or "__builtins__" |
paulb@88 | 1958 | else: |
paulb@88 | 1959 | path, ext = os.path.splitext(filename) |
paulb@88 | 1960 | path, name = os.path.split(path) |
paulb@158 | 1961 | name = module_name or name |
paulb@88 | 1962 | simplified = simplifier.process(module, name) |
paulb@57 | 1963 | return simplified |
paulb@57 | 1964 | |
paul@0 | 1965 | # vim: tabstop=4 expandtab shiftwidth=4 |