forked from M-Labs/artiq
1
0
Fork 0

Add tests for lambdas and functions.

This commit is contained in:
whitequark 2015-07-23 03:07:30 +03:00
parent f8c2709943
commit acb8810e62
5 changed files with 52 additions and 12 deletions

View File

@ -32,6 +32,8 @@ class Module:
monomorphism_validator.visit(self.typedtree) monomorphism_validator.visit(self.typedtree)
escape_validator.visit(self.typedtree) escape_validator.visit(self.typedtree)
self.artiq_ir = artiq_ir_generator.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) dead_code_eliminator.process(self.artiq_ir)
local_access_validator.process(self.artiq_ir) local_access_validator.process(self.artiq_ir)
self.llvm_ir = llvm_ir_generator.process(self.artiq_ir) self.llvm_ir = llvm_ir_generator.process(self.artiq_ir)

View File

@ -1250,18 +1250,37 @@ class ARTIQIRGenerator(algorithm.Visitor):
else: else:
typ = node.func.type.find() typ = node.func.type.find()
func = self.visit(node.func) func = self.visit(node.func)
args = [self.visit(arg) for arg in node.args] args = [None] * (len(typ.args) + len(typ.optargs))
for index, optarg_name in enumerate(typ.optargs):
if len(typ.args) + index >= len(args): for index, arg_node in enumerate(node.args):
optarg_typ = ir.TOption(typ.optargs[optarg_name]) arg = self.visit(arg_node)
for keyword in node.keywords: if index < len(typ.args):
if keyword.arg == optarg_name: args[index] = arg
value = self.append(ir.Alloc([self.visit(keyword.value)], optarg_typ))
args.append(value)
break
else: else:
value = self.append(ir.Alloc([], optarg_typ)) args[index] = self.append(ir.Alloc([arg], ir.TOption(arg.type)))
args.append(value)
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:
assert args[len(typ.args) + index] is None
args[len(typ.args) + index] = \
self.append(ir.Alloc([arg], ir.TOption(arg.type)))
break
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: if self.unwind_target is None:
return self.append(ir.Call(func, args)) return self.append(ir.Call(func, args))

View File

@ -519,7 +519,7 @@ class LLVMIRGenerator:
if builtins.is_none(insn.value().type): if builtins.is_none(insn.value().type):
return self.llbuilder.ret_void() return self.llbuilder.ret_void()
else: else:
return self.llbuilder.ret(self.llmap[insn.value()]) return self.llbuilder.ret(self.map(insn.value()))
def process_Unreachable(self, insn): def process_Unreachable(self, insn):
return self.llbuilder.unreachable() return self.llbuilder.unreachable()

View File

@ -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

View File

@ -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