-
Notifications
You must be signed in to change notification settings - Fork 46
[opt] hacky tick
Shunning Jiang edited this page Oct 6, 2017
·
1 revision
if self.mode == 'hacky':
class RewriteSelf(ast.NodeVisitor):
def visit_Attribute( self, node ):
if isinstance( node.value, ast.Name ): # s.x
if node.value.id == "s": # this is not optimized by "common writer"
node.value.id = "hostobj"
else:
self.visit( node.value )
s = m
rewriter = RewriteSelf()
# Construct a new FunctionDef AST node and the Module wrapper
newfunc = ast.FunctionDef()
newfunc.col_offset = 0
newfunc.lineno = 0
newfunc.name = "tick_hacky"
newfunc.decorator_list = []
newfunc.body = []
newfunc.args = ast.arguments()
newfunc.args.args = []
newfunc.args.kwarg = None
newfunc.args.defaults = []
newfunc.args.vararg = None
newroot = ast.Module()
newroot.body = [ newfunc ]
# Concatenate update block statements
func_globals = dict()
for blk in schedule:
func_globals.update( blk.func_globals )
hostobj = repr( blk.hostobj )
root = blk.ast
# in the form of:
# >>> hostobj = s.reg # this is hostobj_stmt
# >>> hostobj.out = hostobj.in_ # stmt
if hostobj != "s":
hostobj_stmt = ast.parse( "hostobj = " + hostobj ).body[0]
newfunc.body.append( ast.fix_missing_locations( hostobj_stmt ) )
for stmt in root.body[0].body:
if hostobj != "s": rewriter.visit( stmt )
newfunc.body.append( stmt )
if dump:
import astor
gen_tick_src = astor.to_source(newroot)
exec compile( newroot, "<string>", "exec") in locals()
tick_hacky.func_globals.update( func_globals )
ret = tick_hacky