File: tailrecursion.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (33 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from rpython.flowspace.model import mkentrymap, checkgraph

# this transformation is very academical -- I had too much time

def _remove_tail_call(translator, graph, block):
    print "removing tail call"
    assert len(block.exits) == 1
    assert block.exits[0].target is graph.returnblock
    assert block.operations[-1].result == block.exits[0].args[0]
    op = block.operations[-1]
    block.operations = block.operations[:-1]
    block.exits[0].args = op.args[1:]
    block.exits[0].target = graph.startblock

def remove_tail_calls_to_self(translator, graph):
    entrymap = mkentrymap(graph)
    changed = False
    for link in entrymap[graph.returnblock]:
        block = link.prevblock
        if (len(block.exits) == 1 and
            len(block.operations) > 0 and
            block.operations[-1].opname == 'direct_call' and
            block.operations[-1].result == link.args[0]):
            print "getgraph", graph
            if graph is graph:
                _remove_tail_call(translator, graph, block)
                changed = True
    if changed:
        from rpython.translator import simplify
        checkgraph(graph)
        simplify.remove_identical_vars(graph)
        simplify.eliminate_empty_blocks(graph)
        simplify.join_blocks(graph)