Skip to content

Commit

Permalink
Add tests for lambdas and functions.
Browse files Browse the repository at this point in the history
whitequark committed Jul 23, 2015
1 parent f8c2709 commit acb8810
Showing 5 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions artiq/compiler/module.py
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ def __init__(self, source_buffer, engine=None):
monomorphism_validator.visit(self.typedtree)
escape_validator.visit(self.typedtree)
self.artiq_ir = artiq_ir_generator.visit(self.typedtree)
print(self.artiq_ir[0])
print(self.artiq_ir[1])
dead_code_eliminator.process(self.artiq_ir)
local_access_validator.process(self.artiq_ir)
self.llvm_ir = llvm_ir_generator.process(self.artiq_ir)
39 changes: 29 additions & 10 deletions artiq/compiler/transforms/artiq_ir_generator.py
Original file line number Diff line number Diff line change
@@ -1250,18 +1250,37 @@ def visit_CallT(self, node):
else:
typ = node.func.type.find()
func = self.visit(node.func)
args = [self.visit(arg) for arg in node.args]
for index, optarg_name in enumerate(typ.optargs):
if len(typ.args) + index >= len(args):
optarg_typ = ir.TOption(typ.optargs[optarg_name])
for keyword in node.keywords:
args = [None] * (len(typ.args) + len(typ.optargs))

for index, arg_node in enumerate(node.args):
arg = self.visit(arg_node)
if index < len(typ.args):
args[index] = arg
else:
args[index] = self.append(ir.Alloc([arg], ir.TOption(arg.type)))

for keyword in node.keywords:
arg = self.visit(keyword.value)
if keyword.arg in typ.args:
for index, arg_name in enumerate(typ.args):
if keyword.arg == arg_name:
assert args[index] is None
args[index] = arg
break
elif keyword.arg in typ.optargs:
for index, optarg_name in enumerate(typ.optargs):
if keyword.arg == optarg_name:
value = self.append(ir.Alloc([self.visit(keyword.value)], optarg_typ))
args.append(value)
assert args[len(typ.args) + index] is None
args[len(typ.args) + index] = \
self.append(ir.Alloc([arg], ir.TOption(arg.type)))
break
else:
value = self.append(ir.Alloc([], optarg_typ))
args.append(value)

for index, optarg_name in enumerate(typ.optargs):
if args[len(typ.args) + index] is None:
args[len(typ.args) + index] = \
self.append(ir.Alloc([], ir.TOption(typ.optargs[optarg_name])))

assert None not in args

if self.unwind_target is None:
return self.append(ir.Call(func, args))
2 changes: 1 addition & 1 deletion artiq/compiler/transforms/llvm_ir_generator.py
Original file line number Diff line number Diff line change
@@ -519,7 +519,7 @@ def process_Return(self, insn):
if builtins.is_none(insn.value().type):
return self.llbuilder.ret_void()
else:
return self.llbuilder.ret(self.llmap[insn.value()])
return self.llbuilder.ret(self.map(insn.value()))

def process_Unreachable(self, insn):
return self.llbuilder.unreachable()
10 changes: 10 additions & 0 deletions lit-test/compiler/integration/function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: %python -m artiq.compiler.testbench.jit %s

def fib(x):
if x == 1:
return x
else:
return x * fib(x - 1)
assert fib(5) == 120

# argument combinations handled in lambda.py
9 changes: 9 additions & 0 deletions lit-test/compiler/integration/lambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# RUN: %python -m artiq.compiler.testbench.jit %s

assert (lambda: 1)() == 1
assert (lambda x: x)(1) == 1
assert (lambda x, y: x + y)(1, 2) == 3
assert (lambda x, y=1: x + y)(1) == 2
assert (lambda x, y=1: x + y)(1, 2) == 3
assert (lambda x, y=1: x + y)(x=3) == 4
assert (lambda x, y=1: x + y)(y=2, x=3) == 5

0 comments on commit acb8810

Please sign in to comment.