transforms/lower_time: optimize time_to_cycles(cycles_to_time(x)) -> x

This commit is contained in:
Sebastien Bourdeauducq 2014-10-11 12:04:14 +08:00
parent e3ef077cb2
commit 3ee9a87a00
1 changed files with 20 additions and 11 deletions

View File

@ -17,6 +17,15 @@ def _time_to_cycles(ref_period, node):
divided) divided)
def _time_to_cycles_opt(ref_period, node):
if (isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == "cycles_to_time"):
return node.args[0]
else:
return _time_to_cycles(ref_period, node)
def _cycles_to_time(ref_period, node): def _cycles_to_time(ref_period, node):
return ast.copy_location( return ast.copy_location(
ast.BinOp(left=node, ast.BinOp(left=node,
@ -52,27 +61,27 @@ class _TimeLowerer(ast.NodeTransformer):
return node return node
def visit_Expr(self, node): def visit_Expr(self, node):
self.generic_visit(node) r = node
if (isinstance(node.value, ast.Call) if (isinstance(node.value, ast.Call)
and isinstance(node.value.func, ast.Name)): and isinstance(node.value.func, ast.Name)):
funcname = node.value.func.id funcname = node.value.func.id
if funcname == "delay": if funcname == "delay":
return ast.copy_location( r = ast.copy_location(
ast.AugAssign(target=ast.Name("now", ast.Store()), ast.AugAssign(target=ast.Name("now", ast.Store()),
op=ast.Add(), op=ast.Add(),
value=_time_to_cycles(self.ref_period, value=_time_to_cycles_opt(
node.value.args[0])), self.ref_period,
node.value.args[0])),
node) node)
elif funcname == "at": elif funcname == "at":
return ast.copy_location( r = ast.copy_location(
ast.Assign(targets=[ast.Name("now", ast.Store())], ast.Assign(targets=[ast.Name("now", ast.Store())],
value=_time_to_cycles(self.ref_period, value=_time_to_cycles_opt(
node.value.args[0])), self.ref_period,
node.value.args[0])),
node) node)
else: self.generic_visit(r)
return node return r
else:
return node
def lower_time(func_def, initial_time, ref_period): def lower_time(func_def, initial_time, ref_period):