| 1 |
import py |
| 2 |
from rpython.jit.backend.llsupport.symbolic import * |
| 3 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 4 |
from rpython.memory.lltypelayout import convert_offset_to_int |
| 5 |
|
| 6 |
|
| 7 |
WORD = rffi.sizeof(lltype.Signed) |
| 8 |
PTRWORD = rffi.sizeof(llmemory.GCREF) |
| 9 |
|
| 10 |
S = lltype.GcStruct('S', ('x', lltype.Signed), |
| 11 |
('y', lltype.Signed), |
| 12 |
('z', lltype.Signed)) |
| 13 |
|
| 14 |
def convert1(symb): |
| 15 |
if isinstance(symb, int): |
| 16 |
return symb |
| 17 |
return convert_offset_to_int(symb) |
| 18 |
|
| 19 |
def convert(symbs): |
| 20 |
if isinstance(symbs, tuple): |
| 21 |
return map(convert1, symbs) |
| 22 |
else: |
| 23 |
return convert1(symbs) |
| 24 |
|
| 25 |
|
| 26 |
def test_field_token(): |
| 27 |
for translate_support_code in (True, False): |
| 28 |
ofs_x, size_x = convert(get_field_token(S, 'x', translate_support_code)) |
| 29 |
ofs_y, size_y = convert(get_field_token(S, 'y', translate_support_code)) |
| 30 |
ofs_z, size_z = convert(get_field_token(S, 'z', translate_support_code)) |
| 31 |
|
| 32 |
assert size_x == size_y == size_z == WORD |
| 33 |
assert ofs_x >= 0 |
| 34 |
assert ofs_y == ofs_x + WORD |
| 35 |
assert ofs_z == ofs_y + WORD |
| 36 |
|
| 37 |
def test_struct_size(): |
| 38 |
for translate_support_code in (True, False): |
| 39 |
ofs_z, size_z = convert(get_field_token(S, 'z', translate_support_code)) |
| 40 |
totalsize = convert(get_size(S, translate_support_code)) |
| 41 |
assert totalsize == ofs_z + WORD |
| 42 |
|
| 43 |
def test_primitive_size(): |
| 44 |
for translate_support_code in (True, False): |
| 45 |
assert convert(get_size(lltype.Signed, translate_support_code)) == WORD |
| 46 |
assert convert(get_size(lltype.Char, translate_support_code)) == 1 |
| 47 |
sz = get_size(lltype.Ptr(S), translate_support_code) |
| 48 |
assert convert(sz) == PTRWORD |
| 49 |
|
| 50 |
def test_array_token(): |
| 51 |
for translate_support_code in (True, False): |
| 52 |
A = lltype.GcArray(lltype.Char) |
| 53 |
arraytok = get_array_token(A, translate_support_code) |
| 54 |
basesize, itemsize, ofs_length = convert(arraytok) |
| 55 |
assert basesize >= WORD |
| 56 |
assert itemsize == 1 |
| 57 |
assert ofs_length == basesize - WORD |
| 58 |
A = lltype.GcArray(lltype.Signed) |
| 59 |
arraytok = get_array_token(A, translate_support_code) |
| 60 |
basesize, itemsize, ofs_length = convert(arraytok) |
| 61 |
assert basesize >= WORD |
| 62 |
assert itemsize == WORD |
| 63 |
assert ofs_length == basesize - WORD |
| 64 |
A = rffi.CArray(lltype.Signed) |
| 65 |
arraytok = get_array_token(A, translate_support_code) |
| 66 |
basesize, itemsize, ofs_length = convert(arraytok) |
| 67 |
assert basesize == 0 |
| 68 |
assert itemsize == WORD |
| 69 |
assert ofs_length == -1 |
| 70 |
|
| 71 |
def test_varsized_struct_size(): |
| 72 |
S1 = lltype.GcStruct('S1', ('parent', S), |
| 73 |
('extra', lltype.Signed), |
| 74 |
('chars', lltype.Array(lltype.Char))) |
| 75 |
for translate_support_code in (True, False): |
| 76 |
size_parent = convert(get_size(S, translate_support_code)) |
| 77 |
fldtok = get_field_token(S1, 'extra', translate_support_code) |
| 78 |
ofs_extra, size_extra = convert(fldtok) |
| 79 |
arraytok = get_array_token(S1, translate_support_code) |
| 80 |
basesize, itemsize, ofs_length = convert(arraytok) |
| 81 |
assert size_parent == ofs_extra |
| 82 |
assert size_extra == WORD |
| 83 |
assert ofs_length == ofs_extra + WORD |
| 84 |
assert basesize == ofs_length + WORD |
| 85 |
assert itemsize == 1 |
| 86 |
|
| 87 |
def test_string(): |
| 88 |
STR = lltype.GcStruct('String', ('hash', lltype.Signed), |
| 89 |
('chars', lltype.Array(lltype.Char))) |
| 90 |
for translate_support_code in (True, False): |
| 91 |
rstrtok = get_array_token(STR, translate_support_code) |
| 92 |
basesize, itemsize, ofs_length = convert(rstrtok) |
| 93 |
assert itemsize == 1 |
| 94 |
s1 = lltype.malloc(STR, 4) |
| 95 |
s1.chars[0] = 's' |
| 96 |
s1.chars[1] = 'p' |
| 97 |
s1.chars[2] = 'a' |
| 98 |
s1.chars[3] = 'm' |
| 99 |
x = ll2ctypes.lltype2ctypes(s1) |
| 100 |
rawbytes = ctypes.cast(x, ctypes.POINTER(ctypes.c_char)) |
| 101 |
assert rawbytes[basesize+0] == 's' |
| 102 |
assert rawbytes[basesize+1] == 'p' |
| 103 |
assert rawbytes[basesize+2] == 'a' |
| 104 |
assert rawbytes[basesize+3] == 'm' |