示例#1
0
 def createEmptyList(self, context, out):
     context.pushEffect(
         out.expr.store(
             runtime_functions.malloc.call(28).cast(self.getNativeLayoutType())
         )
         >> out.nonref_expr.ElementPtrIntegers(0, 0).store(native_ast.const_int_expr(1))  # refcount
         >> out.nonref_expr.ElementPtrIntegers(0, 1).store(native_ast.const_int32_expr(-1))  # hash cache
         >> out.nonref_expr.ElementPtrIntegers(0, 2).store(native_ast.const_int32_expr(0))  # count
         >> out.nonref_expr.ElementPtrIntegers(0, 3).store(native_ast.const_int32_expr(1))  # reserved
         >> out.nonref_expr.ElementPtrIntegers(0, 4).store(
             runtime_functions.malloc.call(self.underlyingWrapperType.getBytecount())
         )  # data
     )
    def test_teardowns(self):
        converter = native_ast_to_llvm.Converter()

        ct = externalCallTarget("thrower", Void)

        f = Function(args=[('a', Int32)],
                     output_type=Void,
                     body=FunctionBody.Internal(
                         Expression.Finally(
                             expr=ct.call() >>
                             Expression.ActivatesTeardown('a1') >> ct.call() >>
                             Expression.ActivatesTeardown('a2') >> nullExpr,
                             teardowns=[
                                 Teardown.ByTag(tag='a1',
                                                expr=Expression.Branch(
                                                    cond=const_int32_expr(10),
                                                    true=ct.call(),
                                                    false=nullExpr)),
                                 Teardown.ByTag(tag='a1', expr=nullExpr)
                             ])))

        text = converter.add_functions({'f': f})
        print(text)
        mod = llvm.parse_assembly(text)
        mod.verify()
示例#3
0
    def generatePop(self, context, out, inst, ix):
        ix = context.push(int, lambda tgt: tgt.expr.store(ix.nonref_expr))

        with context.ifelse(ix < 0) as (then, otherwise):
            with then:
                context.pushEffect(
                    ix.expr.store(ix + inst.convert_len())
                )
        with context.ifelse((ix < 0).nonref_expr.bitor(ix >= inst.convert_len())) as (then, otherwise):
            with then:
                context.pushException(IndexError, "pop index out of range")

        # we are just moving this - we assume no layouts have selfpointers throughout nativepython
        result = context.push(
            self.underlyingWrapperType,
            lambda result: result.expr.store(inst.convert_getitem_unsafe(ix).nonref_expr)
        )

        context.pushEffect(
            inst.nonref_expr.ElementPtrIntegers(0, 2).store(
                inst.nonref_expr.ElementPtrIntegers(0, 2).load().add(native_ast.const_int32_expr(-1))
            )
        )

        data = inst.nonref_expr.ElementPtrIntegers(0, 4).load()

        context.pushEffect(
            runtime_functions.memmove.call(
                data.elemPtr(ix * self.underlyingWrapperType.getBytecount()),
                data.elemPtr((ix+1) * self.underlyingWrapperType.getBytecount()),
                inst.nonref_expr.ElementPtrIntegers(0, 2).load().cast(native_ast.Int64).sub(ix.nonref_expr).mul(
                    self.underlyingWrapperType.getBytecount()
                )
            )
        )

        if not self.underlyingWrapperType.is_pass_by_ref:
            context.pushEffect(
                native_ast.Expression.Return(arg=result.nonref_expr)
            )
        else:
            context.pushEffect(
                out.expr.store(result.nonref_expr)
            )
示例#4
0
    def generateConcatenateTuple(self, context, out, left, right):
        def elt_ref(tupPtrExpr, iExpr):
            return context.pushReference(
                self.underlyingWrapperType,
                tupPtrExpr.ElementPtrIntegers(0, 4).load().cast(
                    self.underlyingWrapperType.getNativeLayoutType().pointer(
                    )).elemPtr(iExpr))

        left_size = left.convert_len()
        right_size = right.convert_len()

        context.pushEffect(
            out.expr.store(
                runtime_functions.malloc.call(native_ast.const_int_expr(
                    28)).cast(self.getNativeLayoutType())) >>
            out.expr.load().ElementPtrIntegers(0, 4).store(
                runtime_functions.malloc.call(
                    left_size.nonref_expr.add(right_size.nonref_expr).mul(
                        native_ast.const_int_expr(
                            self.underlyingWrapperType.getBytecount()))).cast(
                                native_ast.UInt8Ptr)) >> out.expr.load().
            ElementPtrIntegers(0, 0).store(native_ast.const_int_expr(
                1)) >> out.expr.load().ElementPtrIntegers(0, 1).store(
                    native_ast.const_int32_expr(
                        -1)) >> out.expr.load().ElementPtrIntegers(0, 2).store(
                            left_size.nonref_expr.add(
                                right_size.nonref_expr).cast(native_ast.Int32))
            >> out.expr.load().ElementPtrIntegers(0, 3).store(
                left_size.nonref_expr.add(right_size.nonref_expr).cast(
                    native_ast.Int32)))

        with context.loop(left_size) as i:
            out.convert_getitem_unsafe(i).convert_copy_initialize(
                left.convert_getitem_unsafe(i))

        with context.loop(right_size) as i:
            out.convert_getitem_unsafe(i + left_size).convert_copy_initialize(
                right.convert_getitem_unsafe(i))