pypy /rpython/jit/backend/llsupport/test/test_zrpy_gc_direct.py

Language Python Lines 207
MD5 Hash 35ad15c3fcd3b0dfcd22df0d7cf94f0e Estimated Cost $4,359 (why?)
Repository https://bitbucket.org/pypy/pypy/ View Raw File View Project SPDX
  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
from rpython.jit.backend.detect_cpu import getcpuclass
from rpython.jit.tool.oparser import parse
from rpython.jit.metainterp.history import JitCellToken, NoStats
from rpython.jit.metainterp.history import BasicFinalDescr, BasicFailDescr
from rpython.jit.metainterp.gc import get_description
from rpython.jit.metainterp.optimize import SpeculativeError
from rpython.annotator.listdef import s_list_of_strings
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
from rpython.rtyper.rclass import getclassrepr, getinstancerepr
from rpython.translator.unsimplify import call_initial_function
from rpython.translator.translator import TranslationContext
from rpython.translator.c import genc


def run_guards_translated(gcremovetypeptr):
    class A(object):
        pass
    class B(A):
        pass
    class C(B):
        pass
    def main(argv):
        A(); B().foo = len(argv); C()
        return 0

    t = TranslationContext()
    t.config.translation.gc = "minimark"
    t.config.translation.gcremovetypeptr = gcremovetypeptr
    ann = t.buildannotator()
    ann.build_types(main, [s_list_of_strings], main_entry_point=True)
    rtyper = t.buildrtyper()
    rtyper.specialize()

    classdef = ann.bookkeeper.getuniqueclassdef(B)
    rclass = getclassrepr(rtyper, classdef)
    rinstance = getinstancerepr(rtyper, classdef)
    LLB = rinstance.lowleveltype.TO
    ptr_vtable_B = rclass.getvtable()
    adr_vtable_B = llmemory.cast_ptr_to_adr(ptr_vtable_B)
    vtable_B = llmemory.cast_adr_to_int(adr_vtable_B, mode="symbolic")

    CPU = getcpuclass()
    cpu = CPU(rtyper, NoStats(),
              translate_support_code=True,
              gcdescr=get_description(t.config))
    execute_token = cpu.make_execute_token(llmemory.GCREF)
    finaldescr = BasicFinalDescr()
    faildescr = BasicFailDescr()

    descr_B = cpu.sizeof(LLB, ptr_vtable_B)
    typeid_B = descr_B.get_type_id()
    fielddescr_B = cpu.fielddescrof(LLB, 'inst_foo')

    LLD = lltype.GcStruct('D', ('dd', lltype.Signed))
    descr_D = cpu.sizeof(LLD)
    fielddescr_D = cpu.fielddescrof(LLD, 'dd')

    ARRAY = lltype.GcArray(lltype.Signed)
    arraydescr = cpu.arraydescrof(ARRAY)

    loop1 = parse("""
    [p0]
    guard_class(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'vtable_B': vtable_B})

    loop2 = parse("""
    [p0]
    guard_gc_type(p0, ConstInt(typeid_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'typeid_B': typeid_B})

    loop3 = parse("""
    [p0]
    guard_is_object(p0, descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr})

    loop4 = parse("""
    [p0]
    guard_subclass(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'vtable_B': vtable_B})

    def g():
        cpu.setup_once()
        token1 = JitCellToken()
        token2 = JitCellToken()
        token3 = JitCellToken()
        token4 = JitCellToken()
        cpu.compile_loop(loop1.inputargs, loop1.operations, token1)
        cpu.compile_loop(loop2.inputargs, loop2.operations, token2)
        cpu.compile_loop(loop3.inputargs, loop3.operations, token3)
        cpu.compile_loop(loop4.inputargs, loop4.operations, token4)

        for token, p0 in [
                (token1, rffi.cast(llmemory.GCREF, A())),
                (token1, rffi.cast(llmemory.GCREF, B())),
                (token1, rffi.cast(llmemory.GCREF, C())),

                (token2, rffi.cast(llmemory.GCREF, A())),
                (token2, rffi.cast(llmemory.GCREF, B())),
                (token2, rffi.cast(llmemory.GCREF, C())),
                (token2, rffi.cast(llmemory.GCREF, [42, 43])),

                (token3, rffi.cast(llmemory.GCREF, A())),
                (token3, rffi.cast(llmemory.GCREF, B())),
                (token3, rffi.cast(llmemory.GCREF, [44, 45])),

                (token4, rffi.cast(llmemory.GCREF, A())),
                (token4, rffi.cast(llmemory.GCREF, B())),
                (token4, rffi.cast(llmemory.GCREF, C())),
                ]:
            frame = execute_token(token, p0)
            descr = cpu.get_latest_descr(frame)
            if descr is finaldescr:
                print 'match'
            elif descr is faildescr:
                print 'fail'
            else:
                print '???'
            #
            if token is token2:    # guard_gc_type
                print int(cpu.get_actual_typeid(p0) == typeid_B)
            if token is token3:    # guard_is_object
                print int(cpu.check_is_object(p0))

        for p0 in [lltype.nullptr(llmemory.GCREF.TO),
                   rffi.cast(llmemory.GCREF, A()),
                   rffi.cast(llmemory.GCREF, B()),
                   rffi.cast(llmemory.GCREF, C()),
                   rffi.cast(llmemory.GCREF, lltype.malloc(LLD)),
                   rffi.cast(llmemory.GCREF, lltype.malloc(ARRAY, 5)),
                   rffi.cast(llmemory.GCREF, "foobar"),
                   rffi.cast(llmemory.GCREF, u"foobaz")]:
            results = ['B', 'D', 'A', 'S', 'U']
            try:
                cpu.protect_speculative_field(p0, fielddescr_B)
            except SpeculativeError:
                results[0] = '-'
            try:
                cpu.protect_speculative_field(p0, fielddescr_D)
            except SpeculativeError:
                results[1] = '-'
            try:
                cpu.protect_speculative_array(p0, arraydescr)
            except SpeculativeError:
                results[2] = '-'
            try:
                cpu.protect_speculative_string(p0)
            except SpeculativeError:
                results[3] = '-'
            try:
                cpu.protect_speculative_unicode(p0)
            except SpeculativeError:
                results[4] = '-'
            print ''.join(results)


    call_initial_function(t, g)

    cbuilder = genc.CStandaloneBuilder(t, main, t.config)
    cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
    cbuilder.compile()
    
    data = cbuilder.cmdexec('')
    assert data == ('fail\n'
                    'match\n'
                    'fail\n'

                    'fail\n'  '0\n'
                    'match\n' '1\n'
                    'fail\n'  '0\n'
                    'fail\n'  '0\n'

                    'match\n' '1\n'
                    'match\n' '1\n'
                    'fail\n'  '0\n'

                    'fail\n'
                    'match\n'
                    'match\n'

                    '-----\n'   # null
                    '-----\n'   # instance of A
                    'B----\n'   # instance of B
                    'B----\n'   # instance of C
                    '-D---\n'
                    '--A--\n'
                    '---S-\n'
                    '----U\n'
                    )


def test_guards_translated_with_gctypeptr():
    run_guards_translated(gcremovetypeptr=False)

def test_guards_translated_without_gctypeptr():
    run_guards_translated(gcremovetypeptr=True)
Back to Top