From f31386d15d86cdce308348526c6a94900f5a8dab Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 18 Dec 2014 11:13:50 +0800 Subject: [PATCH] py2llvm: len() support on lists --- artiq/py2llvm/ast_body.py | 4 ++-- artiq/py2llvm/lists.py | 10 ++++++++++ artiq/test/full_stack.py | 2 +- artiq/transforms/tools.py | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/artiq/py2llvm/ast_body.py b/artiq/py2llvm/ast_body.py index 7f67af79d..306ffb821 100644 --- a/artiq/py2llvm/ast_body.py +++ b/artiq/py2llvm/ast_body.py @@ -167,9 +167,9 @@ class Visitor: def _visit_expr_Call(self, node): fn = node.func.id - if fn in {"bool", "int", "int64", "round", "round64", "float"}: + if fn in {"bool", "int", "int64", "round", "round64", "float", "len"}: value = self.visit_expression(node.args[0]) - return getattr(value, "o_"+fn)(self.builder) + return getattr(value, "o_" + fn)(self.builder) elif fn == "Fraction": r = fractions.VFraction() if self.builder is not None: diff --git a/artiq/py2llvm/lists.py b/artiq/py2llvm/lists.py index cd705f3be..d8f814ff0 100644 --- a/artiq/py2llvm/lists.py +++ b/artiq/py2llvm/lists.py @@ -1,6 +1,7 @@ import llvmlite.ir as ll from artiq.py2llvm.values import VGeneric +from artiq.py2llvm.base_types import VInt class VList(VGeneric): @@ -40,6 +41,15 @@ class VList(VGeneric): ll.Constant(ll.IntType(32), 0)]) builder.store(ll.Constant(ll.IntType(32), count), count_ptr) + def o_len(self, builder): + r = VInt() + if builder is not None: + count_ptr = builder.gep(self.llvm_value, [ + ll.Constant(ll.IntType(32), 0), + ll.Constant(ll.IntType(32), 0)]) + r.auto_store(builder, builder.load(count_ptr)) + return r + def o_subscript(self, index, builder): r = self.el_type.new() if builder is not None: diff --git a/artiq/test/full_stack.py b/artiq/test/full_stack.py index 8ef013f59..fd19ac8f6 100644 --- a/artiq/test/full_stack.py +++ b/artiq/test/full_stack.py @@ -61,7 +61,7 @@ class _Misc(AutoContext): self.inhomogeneous_units.append(Quantity(1000, "Hz")) self.inhomogeneous_units.append(Quantity(10, "s")) self.acc = 0 - for i in range(5): + for i in range(len(self.al)): self.acc += self.al[i] @kernel diff --git a/artiq/transforms/tools.py b/artiq/transforms/tools.py index ddf69457c..098458bb0 100644 --- a/artiq/transforms/tools.py +++ b/artiq/transforms/tools.py @@ -9,7 +9,7 @@ embeddable_funcs = ( core_language.delay, core_language.at, core_language.now, core_language.time_to_cycles, core_language.cycles_to_time, core_language.syscall, - range, bool, int, float, round, + range, bool, int, float, round, len, core_language.int64, core_language.round64, Fraction, units.Quantity, units.check_unit, core_language.EncodedException )