forked from cantora/pyc
/
pyc_closure.py
180 lines (153 loc) · 4.38 KB
/
pyc_closure.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright 2013 anthony cantor
# This file is part of pyc.
#
# pyc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyc. If not, see <http://www.gnu.org/licenses/>.
from pyc_astvisitor import ASTTxformer
from pyc_astvisitor import ASTSearcher
import pyc_astvisitor
import pyc_vis
from pyc_log import *
import pyc_gen_name
from pyc_validator import assert_valid
import pyc_localize
from pyc_ir_nodes import *
import pyc_constants
import pyc_heapify
import pyc_ir
import pyc_lineage
import ast
import copy
class Converter(ASTTxformer):
def __init__(self):
ASTTxformer.__init__(self)
def default_accumulator(self):
return {
"defs": [],
"free_vars": set([]),
"bloc_vars": set([])
}
def default_accumulate(self, current, output):
(node, d) = output
new_d = {
"defs": current["defs"] + d["defs"],
"free_vars": current["free_vars"] | d["free_vars"],
"bloc_vars": current["bloc_vars"] | d["bloc_vars"]
}
return (node, new_d)
def visit_Name(self, node):
d = self.default_accumulator()
if not node.id in pyc_constants.internal_names \
and not node.id[0:2] == "ir":
d["bloc_vars"] = set([node.id])
return (
copy_name(node),
d
)
def visit_Module(self, node):
main_body = []
d = self.default_accumulator()
for n in node.body:
output = pyc_vis.visit(self, n)
(out_n, d) = self.default_accumulate(d, output)
main_body.append(out_n)
return (
BlocDef(
name = "main",
params = [],
body = main_body
),
d
)
def visit_Bloc(self, node):
bname = pyc_gen_name.new("bloc")
new_bd = BlocDef(
name = bname,
body = node.body,
params = [var_ref("fvs")] + node.args.args
)
pyc_lineage.bequeath_lineage(node, new_bd, self.__class__.__name__)
(def_node, d) = pyc_vis.visit(
self,
new_bd
)
self.log(self.depth_fmt("bloc vars: %r" % d["bloc_vars"]))
locals = pyc_localize.locals(node)
self.log(self.depth_fmt("locals: %r" % locals))
#note: the params which were heapified will have their normal
#name in this params set, and their heapified versions are initialized
#as a local to the value of the non-heap param
params = set(pyc_astvisitor.names(node.args)) | set(["fvs"])
self.log(self.depth_fmt("params: %r" % params))
d["free_vars"] = (d["free_vars"] | d["bloc_vars"]) - locals - params
self.log(self.depth_fmt("free vars: %r" % d["free_vars"]))
def_node.fvs = list(d["free_vars"])
fvs_inits = []
for i in range(0, len(def_node.fvs)):
assign = make_assign(
var_set(def_node.fvs[i]),
#is it ok to use a canonical name for fvs?
make_subn("fvs", ast.Load, i)
)
pyc_lineage.bequeath_lineage(node, assign, self.__class__.__name__)
fvs_inits.append(pyc_ir.txform(
assign,
tracer=self.tracer
))
def_node.body = fvs_inits + def_node.body
d["defs"].append(def_node)
d["bloc_vars"] = set([])
return (
InjectFromBig(arg=CreateClosure(
name = bname,
#dont set tracer in this txform b.c. self.tracer will run on this tree
free_vars = pyc_ir.txform(
ast.List(
elts = [var_ref(x) for x in def_node.fvs],
ctx = ast.Load()
)
)
)),
d
)
def visit_UserCall(self, node):
(func_node, d) = pyc_vis.visit(self, node.func)
arg_nodes = []
for a in node.args:
output = pyc_vis.visit(self, a)
(a_node, d) = self.default_accumulate(d, output)
arg_nodes.append(a_node)
lname = pyc_gen_name.new("closurecall")
return (
Let(
name = var_set(lname),
rhs = func_node,
body = ClosureCall(
var = var_ref(lname),
args = [ClosureFVS(var = var_ref(lname))] + arg_nodes
)
),
d
)
def txform(as_tree, **kwargs):
v = Converter()
v.log = lambda s: log("Converter : %s" % s)
if 'tracer' in kwargs:
v.tracer = kwargs['tracer']
(conv_tree, d) = pyc_vis.walk(v, as_tree)
result = ast.Module(
body = [conv_tree] + d["defs"]
)
result.parent = as_tree.parent
result.cpass = v.__class__.__name__
return result