forked from M-Labs/artiq
1
0
Fork 0

py2llvm: len() support on lists

This commit is contained in:
Sebastien Bourdeauducq 2014-12-18 11:13:50 +08:00
parent 8af0301185
commit f31386d15d
4 changed files with 14 additions and 4 deletions

View File

@ -167,9 +167,9 @@ class Visitor:
def _visit_expr_Call(self, node): def _visit_expr_Call(self, node):
fn = node.func.id 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]) value = self.visit_expression(node.args[0])
return getattr(value, "o_"+fn)(self.builder) return getattr(value, "o_" + fn)(self.builder)
elif fn == "Fraction": elif fn == "Fraction":
r = fractions.VFraction() r = fractions.VFraction()
if self.builder is not None: if self.builder is not None:

View File

@ -1,6 +1,7 @@
import llvmlite.ir as ll import llvmlite.ir as ll
from artiq.py2llvm.values import VGeneric from artiq.py2llvm.values import VGeneric
from artiq.py2llvm.base_types import VInt
class VList(VGeneric): class VList(VGeneric):
@ -40,6 +41,15 @@ class VList(VGeneric):
ll.Constant(ll.IntType(32), 0)]) ll.Constant(ll.IntType(32), 0)])
builder.store(ll.Constant(ll.IntType(32), count), count_ptr) 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): def o_subscript(self, index, builder):
r = self.el_type.new() r = self.el_type.new()
if builder is not None: if builder is not None:

View File

@ -61,7 +61,7 @@ class _Misc(AutoContext):
self.inhomogeneous_units.append(Quantity(1000, "Hz")) self.inhomogeneous_units.append(Quantity(1000, "Hz"))
self.inhomogeneous_units.append(Quantity(10, "s")) self.inhomogeneous_units.append(Quantity(10, "s"))
self.acc = 0 self.acc = 0
for i in range(5): for i in range(len(self.al)):
self.acc += self.al[i] self.acc += self.al[i]
@kernel @kernel

View File

@ -9,7 +9,7 @@ embeddable_funcs = (
core_language.delay, core_language.at, core_language.now, core_language.delay, core_language.at, core_language.now,
core_language.time_to_cycles, core_language.cycles_to_time, core_language.time_to_cycles, core_language.cycles_to_time,
core_language.syscall, core_language.syscall,
range, bool, int, float, round, range, bool, int, float, round, len,
core_language.int64, core_language.round64, core_language.int64, core_language.round64,
Fraction, units.Quantity, units.check_unit, core_language.EncodedException Fraction, units.Quantity, units.check_unit, core_language.EncodedException
) )