def convert_getvalue_by_index_unsafe(self, context, expr, item): return context.pushReference( self.keyType, expr.nonref_expr.ElementPtrIntegers(0, 4).elemPtr( item.nonref_expr.mul(native_ast.const_int_expr(self.kvBytecount)) .add(native_ast.const_int_expr(self.keyBytecount)) ).cast(self.valueType.getNativeLayoutType().pointer()) )
def generateConstructor(self, context, out, *args): tupletype = self.typeRepresentation.ElementType context.pushEffect( out.expr.store( runtime_functions.malloc.call(native_ast.const_int_expr(16 + self.underlyingLayout.getBytecount())) .cast(self.getNativeLayoutType()) ) >> out.expr.load().ElementPtrIntegers(0, 0).store(native_ast.const_int_expr(1)) >> # refcount out.expr.load().ElementPtrIntegers(0, 1).store(native_ast.const_int_expr(self.indexInParent)) # which ) assert len(args) == len(tupletype.ElementTypes) self.refToInner(context, out).convert_initialize_from_args(*args)
def generateConstructor(self, context, out, *args): context.pushEffect( out.expr.store( runtime_functions.malloc.call( native_ast.const_int_expr( _types.bytecount(self.typeRepresentation.HeldClass) + 8)).cast(self.getNativeLayoutType())) >> # store a refcount out.expr.load().ElementPtrIntegers(0, 0).store( native_ast.const_int_expr(1))) # clear bits of init flags for byteOffset in range(self.bytesOfInitBits): context.pushEffect( out.nonref_expr.cast( native_ast.UInt8.pointer()).ElementPtrIntegers( 8 + byteOffset).store(native_ast.const_uint8_expr(0))) for i in range(len(self.classType.MemberTypes)): if _types.wantsToDefaultConstruct(self.classType.MemberTypes[i]): name = self.classType.MemberNames[i] if name in self.classType.MemberDefaultValues: defVal = self.classType.MemberDefaultValues.get(name) context.pushReference( self.classType.MemberTypes[i], self.memberPtr(out, i)).convert_copy_initialize( nativepython.python_object_representation. pythonObjectRepresentation(context, defVal)) else: context.pushReference(self.classType.MemberTypes[i], self.memberPtr( out, i)).convert_default_initialize() context.pushEffect(self.setIsInitializedExpr(out, i)) if '__init__' in self.typeRepresentation.MemberFunctions: initFuncType = typeWrapper( self.typeRepresentation.MemberFunctions['__init__']) initFuncType.convert_call(context, context.pushVoid(initFuncType), (out, ) + args, {}) else: if len(args): context.pushException( TypeError, "Can't construct a " + self.typeRepresentation.__qualname__ + " with positional arguments because it doesn't have an __init__" )
def convert_len_native(self, expr): return native_ast.Expression.Branch( cond=expr, false=native_ast.const_int_expr(0), true=( expr.ElementPtrIntegers(0, 1).ElementPtrIntegers(4) .cast(native_ast.Int32.pointer()).load().cast(native_ast.Int64) ) )
def __enter__(scope): scope.counter = self.push(int, lambda counter: counter.expr.store(native_ast.const_int_expr(0))) scope.intermediates = self.intermediates scope.teardowns = self.teardowns self.intermediates = [] self.teardowns = [] return scope.counter
def constant(self, context, s): return context.push( str, lambda strRef: strRef.expr.store( runtime_functions.string_from_utf8_and_len.call( native_ast.const_utf8_cstr(s), native_ast.const_int_expr(len(s)) ).cast(self.layoutType) ) )
def convert_incref(self, context, expr): context.pushEffect( native_ast.Expression.Branch( cond=expr.nonref_expr, false=native_ast.nullExpr, true=expr.nonref_expr.ElementPtrIntegers(0, 0).store( native_ast.Expression.Binop( l=expr.nonref_expr.ElementPtrIntegers(0, 0).load(), op=native_ast.BinaryOp.Add(), r=native_ast.const_int_expr(1)))))
def convert_copy_initialize(self, context, expr, other): expr = expr.expr other = other.nonref_expr context.pushEffect( native_ast.Expression.Branch( cond=other, false=expr.store(other), true=expr.store(other) >> expr.load().ElementPtrIntegers( 0, 0).store(expr.load().ElementPtrIntegers( 0, 0).load().add(native_ast.const_int_expr(1)))))
def constant(self, x): if isinstance(x, bool): return TypedExpression(self, native_ast.const_bool_expr(x), bool, False) if isinstance(x, int): return TypedExpression(self, native_ast.const_int_expr(x), int, False) if isinstance(x, float): return TypedExpression(self, native_ast.const_float_expr(x), float, False) assert False
def convert_getitem(self, context, expr, item): item = item.toInt64() len_expr = self.convert_len(context, expr) with context.ifelse( (item.nonref_expr.lt(len_expr.nonref_expr.negate())).bitor( item.nonref_expr.gte(len_expr.nonref_expr))) as (true, false): with true: context.pushException(IndexError, "index out of range") return context.pushPod( int, expr.nonref_expr.ElementPtrIntegers(0, 1).elemPtr( native_ast.Expression.Branch( cond=item.nonref_expr.lt(native_ast.const_int_expr(0)), false=item.nonref_expr, true=item.nonref_expr.add(len_expr.nonref_expr)).add( native_ast.const_int_expr(8))).load().cast( native_ast.Int64))
def __exit__(scope, t,v,traceback): if t is None: expr = results.get(targets[-1], native_ast.nullExpr) for t in reversed(targets[:-1]): expr = native_ast.Expression.Branch( cond=expression.cast(native_ast.Int64).eq(native_ast.const_int_expr(t)), true=results.get(t, native_ast.nullExpr), false=expr ) self.pushEffect(expr)
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 __exit__(scope, *args): result = self.finalize(None) self.intermediates = scope.intermediates self.teardowns = scope.teardowns self.pushEffect( native_ast.Expression.While( cond=scope.counter.nonref_expr.lt( countExpr.nonref_expr), while_true=result >> scope.counter.expr.store( scope.counter.nonref_expr.add( native_ast.const_int_expr(1))), orelse=native_ast.nullExpr))
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))
def convert_destroy(self, context, target): assert target.isReference targetExpr = target.nonref_expr with context.ifelse(targetExpr) as (true, false): with true: context.pushEffect( targetExpr.ElementPtrIntegers(0, 0).store( targetExpr.ElementPtrIntegers(0, 0).load().sub( native_ast.const_int_expr(1)))) with context.ifelse( targetExpr.ElementPtrIntegers(0, 0).load()) as (subtrue, subfalse): with subfalse: context.pushEffect( self.on_refcount_zero(context, target))
def f(context, out, inst, key): # a linear scan for now. lowIx = context.push(int, lambda x: x.expr.store(native_ast.const_int_expr(0))) highIx = context.push(int, lambda x: x.expr.store(self.convert_len_native(inst.nonref_expr))) with context.whileLoop(lowIx.nonref_expr.lt(highIx.nonref_expr)): mid = context.pushPod(int, lowIx.nonref_expr.add(highIx.nonref_expr).div(2)) isLt = key < self.convert_getkey_by_index_unsafe(context, inst, mid) isEq = key == self.convert_getkey_by_index_unsafe(context, inst, mid) if isLt is not None and isEq is not None: with context.ifelse(isEq.nonref_expr) as (true, false): if containmentOnly: with true: context.pushTerminal(native_ast.Expression.Return(arg=native_ast.const_bool_expr(True))) else: with true: result = self.convert_getvalue_by_index_unsafe(context, inst, mid) if out is not None: context.pushEffect( out.convert_copy_initialize(result) ) context.pushTerminal( native_ast.Expression.Return(arg=None) ) else: context.pushTerminal( native_ast.Expression.Return(arg=result.nonref_expr) ) with context.ifelse(isLt.nonref_expr) as (true, false): with true: context.pushEffect(highIx.expr.store(mid.nonref_expr)) with false: context.pushEffect(lowIx.expr.store(mid.nonref_expr.add(1))) if containmentOnly: context.pushTerminal(native_ast.Expression.Return(arg=native_ast.const_bool_expr(False))) else: context.pushException(KeyError, "Can't find key")
def constant(self, context, s): return context.push( bytes, lambda bytesRef: bytesRef.expr.store( runtime_functions.bytes_from_ptr_and_len.call( native_ast.const_bytes_cstr(s), native_ast.const_int_expr(len(s))).cast(self.layoutType)))