<s> from pypy . translator . interactive import Translation <EOL> from pypy import conftest <EOL> from pypy . rpython . lltypesystem import llmemory , lltype <EOL> from pypy . rlib . objectmodel import ComputedIntSymbolic <EOL> def getcompiled ( f , args ) : <EOL> t = Translation ( f ) <EOL> fn = t . compile_c ( args ) <EOL> if conftest . option . view : <EOL> t . view ( ) <EOL> return fn , t <EOL> def test_offsetof ( ) : <EOL> STRUCT = lltype . GcStruct ( "<STR_LIT:s>" , ( "<STR_LIT:x>" , lltype . Signed ) , ( "<STR_LIT:y>" , lltype . Signed ) ) <EOL> offsetx = llmemory . offsetof ( STRUCT , '<STR_LIT:x>' ) <EOL> offsety = llmemory . offsetof ( STRUCT , '<STR_LIT:y>' ) <EOL> def f ( ) : <EOL> s = lltype . malloc ( STRUCT ) <EOL> s . x = <NUM_LIT:1> <EOL> adr = llmemory . cast_ptr_to_adr ( s ) <EOL> result = ( adr + offsetx ) . signed [ <NUM_LIT:0> ] <EOL> ( adr + offsety ) . signed [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> return result * <NUM_LIT:10> + s . y <EOL> fn , t = getcompiled ( f , [ ] ) <EOL> res = fn ( ) <EOL> assert res == <NUM_LIT:12> <EOL> def test_sizeof_array_with_no_length ( ) : <EOL> A = lltype . Array ( lltype . Signed , hints = { '<STR_LIT>' : True } ) <EOL> arraysize = llmemory . sizeof ( A , <NUM_LIT:10> ) <EOL> signedsize = llmemory . sizeof ( lltype . Signed ) <EOL> def f ( ) : <EOL> return arraysize - signedsize * <NUM_LIT:10> <EOL> fn , t = getcompiled ( f , [ ] ) <EOL> res = fn ( ) <EOL> assert res == <NUM_LIT:0> <EOL> def test_itemoffsetof ( ) : <EOL> ARRAY = lltype . GcArray ( lltype . Signed ) <EOL> itemoffsets = [ llmemory . itemoffsetof ( ARRAY , i ) for i in range ( <NUM_LIT:5> ) ] <EOL> def f ( ) : <EOL> a = lltype . malloc ( ARRAY , <NUM_LIT:5> ) <EOL> adr = llmemory . cast_ptr_to_adr ( a ) <EOL> result = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> a [ i ] = i + <NUM_LIT:1> <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> result = result * <NUM_LIT:10> + ( adr + itemoffsets [ i ] ) . signed [ <NUM_LIT:0> ] <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> ( adr + itemoffsets [ i ] ) . signed [ <NUM_LIT:0> ] = i <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> result = <NUM_LIT:10> * result + a [ i ] <EOL> return result <EOL> fn , t = getcompiled ( f , [ ] ) <EOL> res = fn ( ) <EOL> assert res == <NUM_LIT> <EOL> def test_itemoffsetof_fixedsizearray ( ) : <EOL> ARRAY = lltype . FixedSizeArray ( lltype . Signed , <NUM_LIT:5> ) <EOL> itemoffsets = [ llmemory . itemoffsetof ( ARRAY , i ) for i in range ( <NUM_LIT:5> ) ] <EOL> a = lltype . malloc ( ARRAY , immortal = True ) <EOL> def f ( ) : <EOL> adr = llmemory . cast_ptr_to_adr ( a ) <EOL> result = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> a [ i ] = i + <NUM_LIT:1> <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> result = result * <NUM_LIT:10> + ( adr + itemoffsets [ i ] ) . signed [ <NUM_LIT:0> ] <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> ( adr + itemoffsets [ i ] ) . signed [ <NUM_LIT:0> ] = i <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> result = <NUM_LIT:10> * result + a [ i ] <EOL> return result <EOL> fn , t = getcompiled ( f , [ ] ) <EOL> res = fn ( ) <EOL> assert res == <NUM_LIT> <EOL> def test_sizeof_constsize_struct ( ) : <EOL> STRUCT = lltype . Struct ( "<STR_LIT:s>" , ( "<STR_LIT:x>" , lltype . Signed ) , ( "<STR_LIT:y>" , lltype . Signed ) ) <EOL> STRUCTPTR = lltype . Ptr ( STRUCT ) <EOL> sizeofs = llmemory . sizeof ( STRUCT ) <EOL> offsety = llmemory . offsetof ( STRUCT , '<STR_LIT:y>' ) <EOL> def f ( ) : <EOL> adr = llmemory . raw_malloc ( sizeofs ) <EOL> s = llmemory . cast_adr_to_ptr ( adr , STRUCTPTR ) <EOL> s . y = <NUM_LIT:5> <EOL> result = ( adr + offsety ) . signed [ <NUM_LIT:0> ] * <NUM_LIT:10> + int ( offsety < sizeofs ) <EOL> llmemory . raw_free ( adr ) <EOL> return result <EOL> fn , t = getcompiled ( f , [ ] ) <EOL> res = fn ( ) <EOL> assert res == <NUM_LIT> <EOL> def test_computed_int_symbolic ( ) : <EOL> too_early = True <EOL> def compute_fn ( ) : <EOL> assert not too_early <EOL> return <NUM_LIT:7> <EOL> k = ComputedIntSymbolic ( compute_fn ) <EOL> def f ( ) : <EOL> return k * <NUM_LIT:6> <EOL> t = Translation ( f ) <EOL> t . rtype ( ) <EOL> if conftest . option . view : <EOL> t . view ( ) <EOL> too_early = False <EOL> fn = t . compile_c ( ) <EOL> res = fn ( ) <EOL> assert res == <NUM_LIT> </s>
