Source

pypy-postgresql / pypy / jit / backend / llsupport / test / test_descr.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from pypy.rpython.lltypesystem import lltype, rffi, rstr
from pypy.jit.backend.llsupport.descr import *
from pypy.jit.backend.llsupport import symbolic
from pypy.rlib.objectmodel import Symbolic
from pypy.rpython.annlowlevel import llhelper
from pypy.jit.metainterp import history
from pypy.jit.codewriter import longlong
import sys, struct, py

def test_get_size_descr():
    c0 = GcCache(False)
    c1 = GcCache(True)
    T = lltype.GcStruct('T')
    S = lltype.GcStruct('S', ('x', lltype.Char),
                             ('y', lltype.Ptr(T)))
    descr_s = get_size_descr(c0, S)
    descr_t = get_size_descr(c0, T)
    assert descr_s.size == symbolic.get_size(S, False)
    assert descr_t.size == symbolic.get_size(T, False)
    assert descr_s.count_fields_if_immutable() == -1
    assert descr_t.count_fields_if_immutable() == -1
    assert descr_s == get_size_descr(c0, S)
    assert descr_s != get_size_descr(c1, S)
    #
    descr_s = get_size_descr(c1, S)
    assert isinstance(descr_s.size, Symbolic)
    assert descr_s.count_fields_if_immutable() == -1

def test_get_size_descr_immut():
    S = lltype.GcStruct('S', hints={'immutable': True})
    T = lltype.GcStruct('T', ('parent', S),
                        ('x', lltype.Char),
                        hints={'immutable': True})
    U = lltype.GcStruct('U', ('parent', T),
                        ('u', lltype.Ptr(T)),
                        ('v', lltype.Signed),
                        hints={'immutable': True})
    V = lltype.GcStruct('V', ('parent', U),
                        ('miss1', lltype.Void),
                        ('miss2', lltype.Void),
                        hints={'immutable': True})
    for STRUCT, expected in [(S, 0), (T, 1), (U, 3), (V, 3)]:
        for translated in [False, True]:
            c0 = GcCache(translated)
            descr_s = get_size_descr(c0, STRUCT)
            assert descr_s.count_fields_if_immutable() == expected

def test_get_field_descr():
    U = lltype.Struct('U')
    T = lltype.GcStruct('T')
    S = lltype.GcStruct('S', ('x', lltype.Char),
                             ('y', lltype.Ptr(T)),
                             ('z', lltype.Ptr(U)),
                             ('f', lltype.Float),
                             ('s', lltype.SingleFloat))
    #
    c0 = GcCache(False)
    c1 = GcCache(True)
    assert get_field_descr(c0, S, 'y') == get_field_descr(c0, S, 'y')
    assert get_field_descr(c0, S, 'y') != get_field_descr(c1, S, 'y')
    for tsc in [False, True]:
        c2 = GcCache(tsc)
        descr_x = get_field_descr(c2, S, 'x')
        descr_y = get_field_descr(c2, S, 'y')
        descr_z = get_field_descr(c2, S, 'z')
        descr_f = get_field_descr(c2, S, 'f')
        descr_s = get_field_descr(c2, S, 's')
        assert isinstance(descr_x, FieldDescr)
        assert descr_x.name == 'S.x'
        assert descr_y.name == 'S.y'
        assert descr_z.name == 'S.z'
        assert descr_f.name == 'S.f'
        assert descr_s.name == 'S.s'
        if not tsc:
            assert descr_x.offset < descr_y.offset < descr_z.offset
            assert descr_x.sort_key() < descr_y.sort_key() < descr_z.sort_key()
            assert descr_x.field_size == rffi.sizeof(lltype.Char)
            assert descr_y.field_size == rffi.sizeof(lltype.Ptr(T))
            assert descr_z.field_size == rffi.sizeof(lltype.Ptr(U))
            assert descr_f.field_size == rffi.sizeof(lltype.Float)
            assert descr_s.field_size == rffi.sizeof(lltype.SingleFloat)
        else:
            assert isinstance(descr_x.offset, Symbolic)
            assert isinstance(descr_y.offset, Symbolic)
            assert isinstance(descr_z.offset, Symbolic)
            assert isinstance(descr_f.offset, Symbolic)
            assert isinstance(descr_s.offset, Symbolic)
            assert isinstance(descr_x.field_size, Symbolic)
            assert isinstance(descr_y.field_size, Symbolic)
            assert isinstance(descr_z.field_size, Symbolic)
            assert isinstance(descr_f.field_size, Symbolic)
            assert isinstance(descr_s.field_size, Symbolic)
        assert descr_x.flag == FLAG_UNSIGNED
        assert descr_y.flag == FLAG_POINTER
        assert descr_z.flag == FLAG_UNSIGNED
        assert descr_f.flag == FLAG_FLOAT
        assert descr_s.flag == FLAG_UNSIGNED


def test_get_field_descr_sign():
    for RESTYPE, signed in [(rffi.SIGNEDCHAR, True), (rffi.UCHAR,  False),
                            (rffi.SHORT,      True), (rffi.USHORT, False),
                            (rffi.INT,        True), (rffi.UINT,   False),
                            (rffi.LONG,       True), (rffi.ULONG,  False)]:
        S = lltype.GcStruct('S', ('x', RESTYPE))
        for tsc in [False, True]:
            c2 = GcCache(tsc)
            descr_x = get_field_descr(c2, S, 'x')
            assert descr_x.flag == {False: FLAG_UNSIGNED,
                                    True:  FLAG_SIGNED  }[signed]

def test_get_field_descr_longlong():
    if sys.maxint > 2147483647:
        py.test.skip("long long: for 32-bit only")
    c0 = GcCache(False)
    S = lltype.GcStruct('S', ('y', lltype.UnsignedLongLong))
    descr = get_field_descr(c0, S, 'y')
    assert descr.flag == FLAG_FLOAT
    assert descr.field_size == 8


def test_get_array_descr():
    U = lltype.Struct('U')
    T = lltype.GcStruct('T')
    A1 = lltype.GcArray(lltype.Char)
    A2 = lltype.GcArray(lltype.Ptr(T))
    A3 = lltype.GcArray(lltype.Ptr(U))
    A4 = lltype.GcArray(lltype.Float)
    A5 = lltype.GcArray(lltype.Struct('x', ('v', lltype.Signed),
                                           ('k', lltype.Signed)))
    A6 = lltype.GcArray(lltype.SingleFloat)
    #
    c0 = GcCache(False)
    descr1 = get_array_descr(c0, A1)
    descr2 = get_array_descr(c0, A2)
    descr3 = get_array_descr(c0, A3)
    descr4 = get_array_descr(c0, A4)
    descr5 = get_array_descr(c0, A5)
    descr6 = get_array_descr(c0, A6)
    assert isinstance(descr1, ArrayDescr)
    assert descr1 == get_array_descr(c0, lltype.GcArray(lltype.Char))
    assert descr1.flag == FLAG_UNSIGNED
    assert descr2.flag == FLAG_POINTER
    assert descr3.flag == FLAG_UNSIGNED
    assert descr4.flag == FLAG_FLOAT
    assert descr5.flag == FLAG_STRUCT
    assert descr6.flag == FLAG_UNSIGNED
    #
    def get_alignment(code):
        # Retrieve default alignment for the compiler/platform
        return struct.calcsize(lltype.SignedFmt + code) - struct.calcsize(code)
    assert descr1.basesize == get_alignment('c')
    assert descr2.basesize == get_alignment('p')
    assert descr3.basesize == get_alignment('p')
    assert descr4.basesize == get_alignment('d')
    assert descr5.basesize == get_alignment('f')
    assert descr1.lendescr.offset == 0
    assert descr2.lendescr.offset == 0
    assert descr3.lendescr.offset == 0
    assert descr4.lendescr.offset == 0
    assert descr5.lendescr.offset == 0
    assert descr1.itemsize == rffi.sizeof(lltype.Char)
    assert descr2.itemsize == rffi.sizeof(lltype.Ptr(T))
    assert descr3.itemsize == rffi.sizeof(lltype.Ptr(U))
    assert descr4.itemsize == rffi.sizeof(lltype.Float)
    assert descr5.itemsize == rffi.sizeof(lltype.Signed) * 2
    assert descr6.itemsize == rffi.sizeof(lltype.SingleFloat)
    #
    CA = rffi.CArray(lltype.Signed)
    descr = get_array_descr(c0, CA)
    assert descr.flag == FLAG_SIGNED
    assert descr.basesize == 0
    assert descr.lendescr is None
    CA = rffi.CArray(lltype.Ptr(lltype.GcStruct('S')))
    descr = get_array_descr(c0, CA)
    assert descr.flag == FLAG_POINTER
    assert descr.basesize == 0
    assert descr.lendescr is None
    CA = rffi.CArray(lltype.Ptr(lltype.Struct('S')))
    descr = get_array_descr(c0, CA)
    assert descr.flag == FLAG_UNSIGNED
    assert descr.basesize == 0
    assert descr.lendescr is None
    CA = rffi.CArray(lltype.Float)
    descr = get_array_descr(c0, CA)
    assert descr.flag == FLAG_FLOAT
    assert descr.basesize == 0
    assert descr.lendescr is None
    CA = rffi.CArray(rffi.FLOAT)
    descr = get_array_descr(c0, CA)
    assert descr.flag == FLAG_UNSIGNED
    assert descr.basesize == 0
    assert descr.itemsize == rffi.sizeof(lltype.SingleFloat)
    assert descr.lendescr is None


def test_get_array_descr_sign():
    for RESTYPE, signed in [(rffi.SIGNEDCHAR, True), (rffi.UCHAR,  False),
                            (rffi.SHORT,      True), (rffi.USHORT, False),
                            (rffi.INT,        True), (rffi.UINT,   False),
                            (rffi.LONG,       True), (rffi.ULONG,  False)]:
        A = lltype.GcArray(RESTYPE)
        for tsc in [False, True]:
            c2 = GcCache(tsc)
            arraydescr = get_array_descr(c2, A)
            assert arraydescr.flag == {False: FLAG_UNSIGNED,
                                       True:  FLAG_SIGNED  }[signed]
        #
        RA = rffi.CArray(RESTYPE)
        for tsc in [False, True]:
            c2 = GcCache(tsc)
            arraydescr = get_array_descr(c2, RA)
            assert arraydescr.flag == {False: FLAG_UNSIGNED,
                                       True:  FLAG_SIGNED  }[signed]


def test_get_array_descr_str():
    c0 = GcCache(False)
    descr1 = get_array_descr(c0, rstr.STR)
    assert descr1.itemsize == rffi.sizeof(lltype.Char)
    assert descr1.flag == FLAG_UNSIGNED


def test_get_call_descr_not_translated():
    c0 = GcCache(False)
    descr1 = get_call_descr(c0, [lltype.Char, lltype.Signed], lltype.Char)
    assert descr1.get_result_size() == rffi.sizeof(lltype.Char)
    assert descr1.get_result_type() == history.INT
    assert descr1.arg_classes == "ii"
    #
    T = lltype.GcStruct('T')
    descr2 = get_call_descr(c0, [lltype.Ptr(T)], lltype.Ptr(T))
    assert descr2.get_result_size() == rffi.sizeof(lltype.Ptr(T))
    assert descr2.get_result_type() == history.REF
    assert descr2.arg_classes == "r"
    #
    U = lltype.GcStruct('U', ('x', lltype.Signed))
    assert descr2 == get_call_descr(c0, [lltype.Ptr(U)], lltype.Ptr(U))
    #
    V = lltype.Struct('V', ('x', lltype.Signed))
    assert (get_call_descr(c0, [], lltype.Ptr(V)).get_result_type() ==
            history.INT)
    #
    assert (get_call_descr(c0, [], lltype.Void).get_result_type() ==
            history.VOID)
    #
    descr4 = get_call_descr(c0, [lltype.Float, lltype.Float], lltype.Float)
    assert descr4.get_result_size() == rffi.sizeof(lltype.Float)
    assert descr4.get_result_type() == history.FLOAT
    assert descr4.arg_classes == "ff"
    #
    descr5 = get_call_descr(c0, [lltype.SingleFloat], lltype.SingleFloat)
    assert descr5.get_result_size() == rffi.sizeof(lltype.SingleFloat)
    assert descr5.get_result_type() == "S"
    assert descr5.arg_classes == "S"

def test_get_call_descr_not_translated_longlong():
    if sys.maxint > 2147483647:
        py.test.skip("long long: for 32-bit only")
    c0 = GcCache(False)
    #
    descr5 = get_call_descr(c0, [lltype.SignedLongLong], lltype.Signed)
    assert descr5.get_result_size() == 4
    assert descr5.get_result_type() == history.INT
    assert descr5.arg_classes == "L"
    #
    descr6 = get_call_descr(c0, [lltype.Signed], lltype.SignedLongLong)
    assert descr6.get_result_size() == 8
    assert descr6.get_result_type() == "L"
    assert descr6.arg_classes == "i"

def test_get_call_descr_translated():
    c1 = GcCache(True)
    T = lltype.GcStruct('T')
    U = lltype.GcStruct('U', ('x', lltype.Signed))
    descr3 = get_call_descr(c1, [lltype.Ptr(T)], lltype.Ptr(U))
    assert isinstance(descr3.get_result_size(), Symbolic)
    assert descr3.get_result_type() == history.REF
    assert descr3.arg_classes == "r"
    #
    descr4 = get_call_descr(c1, [lltype.Float, lltype.Float], lltype.Float)
    assert isinstance(descr4.get_result_size(), Symbolic)
    assert descr4.get_result_type() == history.FLOAT
    assert descr4.arg_classes == "ff"
    #
    descr5 = get_call_descr(c1, [lltype.SingleFloat], lltype.SingleFloat)
    assert isinstance(descr5.get_result_size(), Symbolic)
    assert descr5.get_result_type() == "S"
    assert descr5.arg_classes == "S"

def test_call_descr_extra_info():
    c1 = GcCache(True)
    T = lltype.GcStruct('T')
    U = lltype.GcStruct('U', ('x', lltype.Signed))
    descr1 = get_call_descr(c1, [lltype.Ptr(T)], lltype.Ptr(U), "hello")
    extrainfo = descr1.get_extra_info()
    assert extrainfo == "hello"
    descr2 = get_call_descr(c1, [lltype.Ptr(T)], lltype.Ptr(U), "hello")
    assert descr1 is descr2
    descr3 = get_call_descr(c1, [lltype.Ptr(T)], lltype.Ptr(U))
    extrainfo = descr3.get_extra_info()
    assert extrainfo is None

def test_get_call_descr_sign():
    for RESTYPE, signed in [(rffi.SIGNEDCHAR, True), (rffi.UCHAR,  False),
                            (rffi.SHORT,      True), (rffi.USHORT, False),
                            (rffi.INT,        True), (rffi.UINT,   False),
                            (rffi.LONG,       True), (rffi.ULONG,  False)]:
        for tsc in [False, True]:
            c2 = GcCache(tsc)
            descr1 = get_call_descr(c2, [], RESTYPE)
            assert descr1.is_result_signed() == signed


def test_repr_of_descr():
    def repr_of_descr(descr):
        s = descr.repr_of_descr()
        assert ',' not in s  # makes the life easier for pypy.tool.jitlogparser
        return s
    c0 = GcCache(False)
    T = lltype.GcStruct('T')
    S = lltype.GcStruct('S', ('x', lltype.Char),
                             ('y', lltype.Ptr(T)),
                             ('z', lltype.Ptr(T)))
    descr1 = get_size_descr(c0, S)
    s = symbolic.get_size(S, False)
    assert repr_of_descr(descr1) == '<SizeDescr %d>' % s
    #
    descr2 = get_field_descr(c0, S, 'y')
    o, _ = symbolic.get_field_token(S, 'y', False)
    assert repr_of_descr(descr2) == '<FieldP S.y %d>' % o
    #
    descr2i = get_field_descr(c0, S, 'x')
    o, _ = symbolic.get_field_token(S, 'x', False)
    assert repr_of_descr(descr2i) == '<FieldU S.x %d>' % o
    #
    descr3 = get_array_descr(c0, lltype.GcArray(lltype.Ptr(S)))
    o = symbolic.get_size(lltype.Ptr(S), False)
    assert repr_of_descr(descr3) == '<ArrayP %d>' % o
    #
    descr3i = get_array_descr(c0, lltype.GcArray(lltype.Char))
    assert repr_of_descr(descr3i) == '<ArrayU 1>'
    #
    descr4 = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Ptr(S))
    assert repr_of_descr(descr4) == '<Callr %d ir>' % o
    #
    descr4i = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Char)
    assert repr_of_descr(descr4i) == '<Calli 1 ir>'
    #
    descr4f = get_call_descr(c0, [lltype.Char, lltype.Ptr(S)], lltype.Float)
    assert repr_of_descr(descr4f) == '<Callf 8 ir>'
    #
    descr5f = get_call_descr(c0, [lltype.Char], lltype.SingleFloat)
    assert repr_of_descr(descr5f) == '<CallS 4 i>'

def test_call_stubs_1():
    c0 = GcCache(False)
    ARGS = [lltype.Char, lltype.Signed]
    RES = lltype.Char
    descr1 = get_call_descr(c0, ARGS, RES)
    def f(a, b):
        return 'c'

    fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f)

    res = descr1.call_stub_i(rffi.cast(lltype.Signed, fnptr),
                             [1, 2], None, None)
    assert res == ord('c')

def test_call_stubs_2():
    c0 = GcCache(False)
    ARRAY = lltype.GcArray(lltype.Signed)
    ARGS = [lltype.Float, lltype.Ptr(ARRAY)]
    RES = lltype.Float

    def f2(a, b):
        return float(b[0]) + a

    fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f2)
    descr2 = get_call_descr(c0, ARGS, RES)
    a = lltype.malloc(ARRAY, 3)
    opaquea = lltype.cast_opaque_ptr(llmemory.GCREF, a)
    a[0] = 1
    res = descr2.call_stub_f(rffi.cast(lltype.Signed, fnptr),
                             [], [opaquea], [longlong.getfloatstorage(3.5)])
    assert longlong.getrealfloat(res) == 4.5

def test_call_stubs_single_float():
    from pypy.rlib.longlong2float import uint2singlefloat, singlefloat2uint
    from pypy.rlib.rarithmetic import r_singlefloat, intmask
    #
    c0 = GcCache(False)
    ARGS = [lltype.SingleFloat, lltype.SingleFloat, lltype.SingleFloat]
    RES = lltype.SingleFloat

    def f(a, b, c):
        a = float(a)
        b = float(b)
        c = float(c)
        x = a - (b / c)
        return r_singlefloat(x)

    fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f)
    descr2 = get_call_descr(c0, ARGS, RES)
    a = intmask(singlefloat2uint(r_singlefloat(-10.0)))
    b = intmask(singlefloat2uint(r_singlefloat(3.0)))
    c = intmask(singlefloat2uint(r_singlefloat(2.0)))
    res = descr2.call_stub_i(rffi.cast(lltype.Signed, fnptr),
                             [a, b, c], [], [])
    assert float(uint2singlefloat(rffi.r_uint(res))) == -11.5

def test_field_arraylen_descr():
    c0 = GcCache(True)
    A1 = lltype.GcArray(lltype.Signed)
    fielddescr = get_field_arraylen_descr(c0, A1)
    assert isinstance(fielddescr, FieldDescr)
    ofs = fielddescr.offset
    assert repr(ofs) == '< ArrayLengthOffset <GcArray of Signed > >'
    #
    fielddescr = get_field_arraylen_descr(c0, rstr.STR)
    ofs = fielddescr.offset
    assert repr(ofs) == ("< <FieldOffset <GcStruct rpy_string { hash, chars }>"
                         " 'chars'> + < ArrayLengthOffset"
                         " <Array of Char > > >")
    # caching:
    assert fielddescr is get_field_arraylen_descr(c0, rstr.STR)