示例#1
0
 def test_frac_simplify(self):
     simplify_encode_c = CompiledFunction(
         simplify_encode, {"a": base_types.VInt(), "b": base_types.VInt()})
     for a in _test_range():
         for b in _test_range():
             self.assertEqual(
                 simplify_encode_c(a, b), simplify_encode(a, b))
示例#2
0
def _chr_to_value(c):
    if c == "n":
        return base_types.VNone()
    if c == "b":
        return base_types.VBool()
    if c == "i":
        return base_types.VInt()
    if c == "I":
        return base_types.VInt(64)
    raise ValueError
示例#3
0
 def _test_frac_arith_float(self, op, rev):
     f = frac_arith_float_rev if rev else frac_arith_float
     f_c = CompiledFunction(f, {
         "op": base_types.VInt(),
         "a": base_types.VInt(), "b": base_types.VInt(),
         "x": base_types.VFloat()})
     for a in _test_range():
         for b in _test_range():
             for x in _test_range():
                 self.assertAlmostEqual(
                     f_c(op, a, b, x/2),
                     f(op, a, b, x/2))
示例#4
0
 def _test_frac_arith_int(self, op, rev):
     f = frac_arith_encode_int_rev if rev else frac_arith_encode_int
     f_c = CompiledFunction(f, {
         "op": base_types.VInt(),
         "a": base_types.VInt(), "b": base_types.VInt(),
         "x": base_types.VInt()})
     for a in _test_range():
         for b in _test_range():
             for x in _test_range():
                 self.assertEqual(
                     f_c(op, a, b, x),
                     f(op, a, b, x))
示例#5
0
 def _test_frac_arith(self, op):
     frac_arith_encode_c = CompiledFunction(
         frac_arith_encode, {
             "op": base_types.VInt(),
             "a": base_types.VInt(), "b": base_types.VInt(),
             "c": base_types.VInt(), "d": base_types.VInt()})
     for a in _test_range():
         for b in _test_range():
             for c in _test_range():
                 for d in _test_range():
                     self.assertEqual(
                         frac_arith_encode_c(op, a, b, c, d),
                         frac_arith_encode(op, a, b, c, d))
示例#6
0
 def _visit_expr_Num(self, node):
     n = node.n
     if isinstance(n, int):
         if abs(n) < 2**31:
             r = base_types.VInt()
         else:
             r = base_types.VInt(64)
     elif isinstance(n, float):
         r = base_types.VFloat()
     else:
         raise NotImplementedError
     if self.builder is not None:
         r.set_const_value(self.builder, n)
     return r
示例#7
0
 def _test_float_arith(self, op):
     arith_c = CompiledFunction(arith, {
         "op": base_types.VInt(),
         "a": base_types.VFloat(), "b": base_types.VFloat()})
     for a in _test_range():
         for b in _test_range():
             self.assertEqual(arith_c(op, a/2, b/2), arith(op, a/2, b/2))
示例#8
0
    def _build_rpc(self, args, builder):
        r = base_types.VInt()
        if builder is not None:
            new_args = []
            new_args.append(args[0].auto_load(builder))  # RPC number
            for arg in args[1:]:
                # type tag
                arg_type_str = _value_to_str(arg)
                arg_type_int = 0
                for c in reversed(arg_type_str):
                    arg_type_int <<= 8
                    arg_type_int |= ord(c)
                new_args.append(ll.Constant(ll.IntType(32), arg_type_int))

                # pointer to value
                if not isinstance(arg, base_types.VNone):
                    if isinstance(arg.llvm_value.type, ll.PointerType):
                        new_args.append(arg.llvm_value)
                    else:
                        arg_ptr = arg.new()
                        arg_ptr.alloca(builder)
                        arg_ptr.auto_store(builder, arg.llvm_value)
                        new_args.append(arg_ptr.llvm_value)
            # end marker
            new_args.append(ll.Constant(ll.IntType(32), 0))
            r.auto_store(builder, builder.call(self.rpc, new_args))
        return r
示例#9
0
    def _visit_stmt_Assign(self, node):
        val = self.visit_expression(node.value)
        if isinstance(node.value, ast.List):
            if len(node.targets) > 1:
                raise NotImplementedError
            target = self.visit_expression(node.targets[0])
            target.set_count(self.builder, val.alloc_count)
            for i, elt in enumerate(val.elts):
                idx = base_types.VInt()
                idx.set_const_value(self.builder, i)
                target.o_subscript(idx,
                                   self.builder).set_value(self.builder, elt)
        elif isinstance(node.value, ast.ListComp):
            if len(node.targets) > 1:
                raise NotImplementedError
            target = self.visit_expression(node.targets[0])
            target.set_count(self.builder, val.alloc_count)

            i = base_types.VInt()
            i.alloca(self.builder)
            i.auto_store(self.builder, ll.Constant(ll.IntType(32), 0))

            function = self.builder.basic_block.function
            copy_block = function.append_basic_block("ai_copy")
            end_block = function.append_basic_block("ai_end")
            self.builder.branch(copy_block)

            self.builder.position_at_end(copy_block)
            target.o_subscript(i,
                               self.builder).set_value(self.builder, val.elt)
            i.auto_store(
                self.builder,
                self.builder.add(i.auto_load(self.builder),
                                 ll.Constant(ll.IntType(32), 1)))
            cont = self.builder.icmp_signed(
                "<", i.auto_load(self.builder),
                ll.Constant(ll.IntType(32), val.alloc_count))
            self.builder.cbranch(cont, copy_block, end_block)

            self.builder.position_at_end(end_block)
        else:
            for target in node.targets:
                target = self.visit_expression(target)
                target.set_value(self.builder, val)
示例#10
0
 def test_is_prime(self):
     is_prime_c = CompiledFunction(is_prime, {"x": base_types.VInt()})
     for i in range(200):
         self.assertEqual(is_prime_c(i), is_prime(i))