File: zrpy_gc_test.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (905 lines) | stat: -rw-r--r-- 30,405 bytes parent folder | download | duplicates (3)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
"""
This is a test that translates a complete JIT together with a GC and runs it.
It is testing that the GC-dependent aspects basically work, mostly the mallocs
and the various cases of write barrier.
"""

import weakref
import os, py
from rpython.rlib import rgc
from rpython.rtyper.lltypesystem import lltype
from rpython.rlib.jit import JitDriver, dont_look_inside
from rpython.rlib.jit import elidable, unroll_safe
from rpython.rlib.jit import promote
from rpython.jit.backend.llsupport.gc import GcLLDescr_framework
from rpython.tool.udir import udir
from rpython.config.translationoption import DEFL_GC
from rpython.config.config import ConfigError


class X(object):
    def __init__(self, x=0):
        self.x = x

    next = None

class Y(object):
    # for pinning tests we need an object without references to other
    # objects
    def __init__(self, x=0):
        self.x = x

class CheckError(Exception):
    pass

def check(flag):
    if not flag:
        raise CheckError

def get_entry(g):

    def entrypoint(args):
        name = ''
        n = 2000
        argc = len(args)
        if argc > 1:
            name = args[1]
        if argc > 2:
            n = int(args[2])
        r_list = []
        for i in range(20):
            r = g(name, n)
            r_list.append(r)
            rgc.collect()
        rgc.collect(); rgc.collect()
        freed = 0
        for r in r_list:
            if r() is None:
                freed += 1
        print freed
        return 0

    return entrypoint

def get_functions_to_patch():
    from rpython.jit.backend.llsupport import gc
    #
    can_use_nursery_malloc1 = gc.GcLLDescr_framework.can_use_nursery_malloc
    def can_use_nursery_malloc2(*args):
        try:
            if os.environ['PYPY_NO_INLINE_MALLOC']:
                return False
        except KeyError:
            pass
        return can_use_nursery_malloc1(*args)
    #
    return {(gc.GcLLDescr_framework, 'can_use_nursery_malloc'):
                can_use_nursery_malloc2}

def compile(f, gc, **kwds):
    from rpython.annotator.listdef import s_list_of_strings
    from rpython.translator.translator import TranslationContext
    from rpython.jit.metainterp.warmspot import apply_jit
    from rpython.translator.c import genc
    #
    t = TranslationContext()
    t.config.translation.gc = gc
    if gc != 'boehm':
        t.config.translation.gcremovetypeptr = True
    for name, value in kwds.items():
        setattr(t.config.translation, name, value)
    ann = t.buildannotator()
    ann.build_types(f, [s_list_of_strings], main_entry_point=True)
    t.buildrtyper().specialize()

    if kwds['jit']:
        patch = get_functions_to_patch()
        old_value = {}
        try:
            for (obj, attr), value in patch.items():
                old_value[obj, attr] = getattr(obj, attr)
                setattr(obj, attr, value)
            #
            apply_jit(t)
            #
        finally:
            for (obj, attr), oldvalue in old_value.items():
                setattr(obj, attr, oldvalue)

    cbuilder = genc.CStandaloneBuilder(t, f, t.config)
    cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
    cbuilder.compile()
    return cbuilder

def run(cbuilder, args=''):
    #
    pypylog = udir.join('test_zrpy_gc.log')
    env = os.environ.copy()
    env['PYPYLOG'] = ':%s' % pypylog
    data = cbuilder.cmdexec(args, env=env)
    return data.strip()

# ______________________________________________________________________


class BaseFrameworkTests(object):
    gc = DEFL_GC

    def setup_class(cls):
        funcs = []
        name_to_func = {}
        for fullname in dir(cls):
            if not fullname.startswith('define'):
                continue
            definefunc = getattr(cls, fullname)
            _, name = fullname.split('_', 1)
            beforefunc, loopfunc, afterfunc = definefunc.im_func(cls)
            if beforefunc is None:
                def beforefunc(n, x):
                    return n, x, None, None, None, None, None, None, None, None, None, ''
            if afterfunc is None:
                def afterfunc(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
                    pass
            beforefunc.func_name = 'before_'+name
            loopfunc.func_name = 'loop_'+name
            afterfunc.func_name = 'after_'+name
            funcs.append((beforefunc, loopfunc, afterfunc))
            assert name not in name_to_func
            name_to_func[name] = len(name_to_func)
        print name_to_func
        def allfuncs(name, n):
            x = X()
            x.foo = 2
            main_allfuncs(name, n, x)
            x.foo = 5
            return weakref.ref(x)
        def main_allfuncs(name, n, x):
            num = name_to_func[name]
            n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s = funcs[num][0](n, x)
            while n > 0:
                myjitdriver.can_enter_jit(num=num, n=n, x=x, x0=x0, x1=x1,
                        x2=x2, x3=x3, x4=x4, x5=x5, x6=x6, x7=x7, l=l, s=s)
                myjitdriver.jit_merge_point(num=num, n=n, x=x, x0=x0, x1=x1,
                        x2=x2, x3=x3, x4=x4, x5=x5, x6=x6, x7=x7, l=l, s=s)

                n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s = funcs[num][1](
                        n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s)
            funcs[num][2](n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s)
        myjitdriver = JitDriver(greens = ['num'],
                                reds = ['n', 'x', 'x0', 'x1', 'x2', 'x3', 'x4',
                                        'x5', 'x6', 'x7', 'l', 's'], is_recursive=True)
        cls.main_allfuncs = staticmethod(main_allfuncs)
        cls.name_to_func = name_to_func
        OLD_DEBUG = GcLLDescr_framework.DEBUG
        try:
            GcLLDescr_framework.DEBUG = True
            cls.cbuilder = compile(get_entry(allfuncs), cls.gc,
                                   gcrootfinder=cls.gcrootfinder, jit=True,
                                   thread=True)
        except ConfigError as e:        
            assert str(e).startswith('invalid value asmgcc')
            py.test.skip('asmgcc not supported')
        finally:
            GcLLDescr_framework.DEBUG = OLD_DEBUG

    def _run(self, name, n, env):
        res = self.cbuilder.cmdexec("%s %d" %(name, n), env=env)
        assert int(res) == 20

    def run(self, name, n=2000):
        pypylog = udir.join('TestCompileFramework.log')
        env = os.environ.copy()
        env['PYPYLOG'] = ':%s' % pypylog
        env['PYPY_NO_INLINE_MALLOC'] = '1'
        self._run(name, n, env)
        env['PYPY_NO_INLINE_MALLOC'] = ''
        self._run(name, n, env)

    def run_orig(self, name, n, x):
        self.main_allfuncs(name, n, x)


class CompileFrameworkTests(BaseFrameworkTests):
    # Test suite using (so far) the minimark GC.

##    def define_libffi_workaround(cls):
##        # XXX: this is a workaround for a bug in database.py.  It seems that
##        # the problem is triggered by optimizeopt/fficall.py, and in
##        # particular by the ``cast_base_ptr_to_instance(Func, llfunc)``: in
##        # these tests, that line is the only place where libffi.Func is
##        # referenced.
##        #
##        # The problem occurs because the gctransformer tries to annotate a
##        # low-level helper to call the __del__ of libffi.Func when it's too
##        # late.
##        #
##        # This workaround works by forcing the annotator (and all the rest of
##        # the toolchain) to see libffi.Func in a "proper" context, not just as
##        # the target of cast_base_ptr_to_instance.  Note that the function
##        # below is *never* called by any actual test, it's just annotated.
##        #
##        from rpython.rlib.libffi import get_libc_name, CDLL, types, ArgChain
##        libc_name = get_libc_name()
##        def f(n, x, *args):
##            libc = CDLL(libc_name)
##            ptr = libc.getpointer('labs', [types.slong], types.slong)
##            chain = ArgChain()
##            chain.arg(n)
##            n = ptr.call(chain, lltype.Signed)
##            return (n, x) + args
##        return None, f, None

    def define_compile_framework_1(cls):
        # a moving GC.  Simple test, works
        # without write_barriers and root stack enumeration.
        def f(n, x, *args):
            y = X()
            y.foo = x.foo
            n -= y.foo
            return (n, x) + args
        return None, f, None

    def test_compile_framework_1(self):
        self.run('compile_framework_1')

    def define_compile_framework_2(cls):
        # More complex test, requires root stack enumeration but
        # not write_barriers.
        def f(n, x, *args):
            prev = x
            for j in range(101):    # f() runs 20'000 times, thus allocates
                y = X()             # a total of 2'020'000 objects
                y.foo = prev.foo
                prev = y
            n -= prev.foo
            return (n, x) + args
        return None, f, None

    def test_compile_framework_2(self):
        self.run('compile_framework_2')

    def define_compile_framework_3(cls):
        # Third version of the test.  Really requires write_barriers.
        def f(n, x, *args):
            x.next = None
            for j in range(101):    # f() runs 20'000 times, thus allocates
                y = X()             # a total of 2'020'000 objects
                y.foo = j+1
                y.next = x.next
                x.next = y
            check(x.next.foo == 101)
            total = 0
            y = x
            for j in range(101):
                y = y.next
                total += y.foo
            check(not y.next)
            check(total == 101*102/2)
            n -= x.foo
            return (n, x) + args
        return None, f, None



    def test_compile_framework_3(self):
        x_test = X()
        x_test.foo = 5
        self.run_orig('compile_framework_3', 6, x_test)     # check that it does not raise CheckError
        self.run('compile_framework_3')

    def define_compile_framework_3_extra(cls):
        # Extra version of the test, with tons of live vars around the residual
        # call that all contain a GC pointer.
        @dont_look_inside
        def residual(n=26):
            x = X()
            x.next = X()
            x.next.foo = n
            return x
        #
        def before(n, x):
            residual(5)
            x0 = residual()
            x1 = residual()
            x2 = residual()
            x3 = residual()
            x4 = residual()
            x5 = residual()
            x6 = residual()
            x7 = residual()
            n *= 19
            return n, None, x0, x1, x2, x3, x4, x5, x6, x7, None, None
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            x8 = residual()
            x9 = residual()
            check(x0.next.foo == 26)
            check(x1.next.foo == 26)
            check(x2.next.foo == 26)
            check(x3.next.foo == 26)
            check(x4.next.foo == 26)
            check(x5.next.foo == 26)
            check(x6.next.foo == 26)
            check(x7.next.foo == 26)
            check(x8.next.foo == 26)
            check(x9.next.foo == 26)
            x0, x1, x2, x3, x4, x5, x6, x7 = x7, x4, x6, x5, x3, x2, x9, x8
            n -= 1
            return n, None, x0, x1, x2, x3, x4, x5, x6, x7, None, None
        return before, f, None

    def test_compile_framework_3_extra(self):
        self.run_orig('compile_framework_3_extra', 6, None)     # check that it does not raise CheckError
        self.run('compile_framework_3_extra')

    def define_compile_framework_4(cls):
        # Fourth version of the test, with __del__.
        from rpython.rlib.debug import debug_print
        class Counter:
            cnt = 0
        counter = Counter()
        class Z:
            def __del__(self):
                counter.cnt -= 1
        def before(n, x):
            debug_print('counter.cnt =', counter.cnt)
            check(counter.cnt < 5)
            counter.cnt = n // x.foo
            return n, x, None, None, None, None, None, None, None, None, None, None
        def f(n, x, *args):
            Z()
            n -= x.foo
            return (n, x) + args
        return before, f, None

    def test_compile_framework_4(self):
        self.run('compile_framework_4')

    def define_compile_framework_5(cls):
        # Test string manipulation.
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            n -= x.foo
            s += str(n)
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            check(len(s) == 1*5 + 2*45 + 3*450 + 4*500)
        return None, f, after

    def test_compile_framework_5(self):
        self.run('compile_framework_5')

    def define_compile_framework_7(cls):
        # Array of pointers (test the write barrier for setarrayitem_gc)
        def before(n, x):
            return n, x, None, None, None, None, None, None, None, None, [X(123)], None
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            if n < 1900:
                check(l[0].x == 123)
                l = [None] * 16
                l[0] = X(123)
                l[1] = X(n)
                l[2] = X(n+10)
                l[3] = X(n+20)
                l[4] = X(n+30)
                l[5] = X(n+40)
                l[6] = X(n+50)
                l[7] = X(n+60)
                l[8] = X(n+70)
                l[9] = X(n+80)
                l[10] = X(n+90)
                l[11] = X(n+100)
                l[12] = X(n+110)
                l[13] = X(n+120)
                l[14] = X(n+130)
                l[15] = X(n+140)
            if n < 1800:
                check(len(l) == 16)
                check(l[0].x == 123)
                check(l[1].x == n)
                check(l[2].x == n+10)
                check(l[3].x == n+20)
                check(l[4].x == n+30)
                check(l[5].x == n+40)
                check(l[6].x == n+50)
                check(l[7].x == n+60)
                check(l[8].x == n+70)
                check(l[9].x == n+80)
                check(l[10].x == n+90)
                check(l[11].x == n+100)
                check(l[12].x == n+110)
                check(l[13].x == n+120)
                check(l[14].x == n+130)
                check(l[15].x == n+140)
            n -= x.foo
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            check(len(l) == 16)
            check(l[0].x == 123)
            check(l[1].x == 2)
            check(l[2].x == 12)
            check(l[3].x == 22)
            check(l[4].x == 32)
            check(l[5].x == 42)
            check(l[6].x == 52)
            check(l[7].x == 62)
            check(l[8].x == 72)
            check(l[9].x == 82)
            check(l[10].x == 92)
            check(l[11].x == 102)
            check(l[12].x == 112)
            check(l[13].x == 122)
            check(l[14].x == 132)
            check(l[15].x == 142)
        return before, f, after

    def test_compile_framework_7(self):
        self.run('compile_framework_7')

    def define_compile_framework_7_interior(cls):
        # Array of structs containing pointers (test the write barrier
        # for setinteriorfield_gc)
        S = lltype.GcStruct('S', ('i', lltype.Signed))
        A = lltype.GcArray(lltype.Struct('entry', ('x', lltype.Ptr(S)),
                                                  ('y', lltype.Ptr(S)),
                                                  ('z', lltype.Ptr(S))))
        class Glob:
            a = lltype.nullptr(A)
        glob = Glob()
        #
        def make_s(i):
            s = lltype.malloc(S)
            s.i = i
            return s
        #
        @unroll_safe
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            a = glob.a
            if not a:
                a = glob.a = lltype.malloc(A, 10)
            i = 0
            while i < 10:
                a[i].x = make_s(n + i * 100 + 1)
                a[i].y = make_s(n + i * 100 + 2)
                a[i].z = make_s(n + i * 100 + 3)
                i += 1
            i = 0
            while i < 10:
                check(a[i].x.i == n + i * 100 + 1)
                check(a[i].y.i == n + i * 100 + 2)
                check(a[i].z.i == n + i * 100 + 3)
                i += 1
            n -= x.foo
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        return None, f, None

    def test_compile_framework_7_interior(self):
        self.run('compile_framework_7_interior')

    def define_compile_framework_8(cls):
        # Array of pointers, of unknown length (test write_barrier_from_array)
        def before(n, x):
            return n, x, None, None, None, None, None, None, None, None, [X(123)], None
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            if n < 1900:
                check(l[0].x == 123)
                l = [None] * (16 + (n & 7))
                l[0] = X(123)
                l[1] = X(n)
                l[2] = X(n+10)
                l[3] = X(n+20)
                l[4] = X(n+30)
                l[5] = X(n+40)
                l[6] = X(n+50)
                l[7] = X(n+60)
                l[8] = X(n+70)
                l[9] = X(n+80)
                l[10] = X(n+90)
                l[11] = X(n+100)
                l[12] = X(n+110)
                l[13] = X(n+120)
                l[14] = X(n+130)
                l[15] = X(n+140)
            if n < 1800:
                check(len(l) == 16 + (n & 7))
                check(l[0].x == 123)
                check(l[1].x == n)
                check(l[2].x == n+10)
                check(l[3].x == n+20)
                check(l[4].x == n+30)
                check(l[5].x == n+40)
                check(l[6].x == n+50)
                check(l[7].x == n+60)
                check(l[8].x == n+70)
                check(l[9].x == n+80)
                check(l[10].x == n+90)
                check(l[11].x == n+100)
                check(l[12].x == n+110)
                check(l[13].x == n+120)
                check(l[14].x == n+130)
                check(l[15].x == n+140)
            n -= x.foo
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            check(len(l) >= 16)
            check(l[0].x == 123)
            check(l[1].x == 2)
            check(l[2].x == 12)
            check(l[3].x == 22)
            check(l[4].x == 32)
            check(l[5].x == 42)
            check(l[6].x == 52)
            check(l[7].x == 62)
            check(l[8].x == 72)
            check(l[9].x == 82)
            check(l[10].x == 92)
            check(l[11].x == 102)
            check(l[12].x == 112)
            check(l[13].x == 122)
            check(l[14].x == 132)
            check(l[15].x == 142)
        return before, f, after

    def test_compile_framework_8(self):
        self.run('compile_framework_8')

    def define_compile_framework_9(cls):
        # Like compile_framework_8, but with variable indexes and large
        # arrays, testing the card_marking case
        def before(n, x):
            return n, x, None, None, None, None, None, None, None, None, [X(123)], None
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            if n < 1900:
                check(l[0].x == 123)
                num = 512 + (n & 7)
                l = [None] * num
                l[0] = X(123)
                l[1] = X(n)
                l[2] = X(n+10)
                l[3] = X(n+20)
                l[4] = X(n+30)
                l[5] = X(n+40)
                l[6] = X(n+50)
                l[7] = X(n+60)
                l[num-8] = X(n+70)
                l[num-9] = X(n+80)
                l[num-10] = X(n+90)
                l[num-11] = X(n+100)
                l[-12] = X(n+110)
                l[-13] = X(n+120)
                l[-14] = X(n+130)
                l[-15] = X(n+140)
            if n < 1800:
                num = 512 + (n & 7)
                check(len(l) == num)
                check(l[0].x == 123)
                check(l[1].x == n)
                check(l[2].x == n+10)
                check(l[3].x == n+20)
                check(l[4].x == n+30)
                check(l[5].x == n+40)
                check(l[6].x == n+50)
                check(l[7].x == n+60)
                check(l[num-8].x == n+70)
                check(l[num-9].x == n+80)
                check(l[num-10].x == n+90)
                check(l[num-11].x == n+100)
                check(l[-12].x == n+110)
                check(l[-13].x == n+120)
                check(l[-14].x == n+130)
                check(l[-15].x == n+140)
            n -= x.foo
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            check(len(l) >= 512)
            check(l[0].x == 123)
            check(l[1].x == 2)
            check(l[2].x == 12)
            check(l[3].x == 22)
            check(l[4].x == 32)
            check(l[5].x == 42)
            check(l[6].x == 52)
            check(l[7].x == 62)
            check(l[-8].x == 72)
            check(l[-9].x == 82)
            check(l[-10].x == 92)
            check(l[-11].x == 102)
            check(l[-12].x == 112)
            check(l[-13].x == 122)
            check(l[-14].x == 132)
            check(l[-15].x == 142)
        return before, f, after

    def test_compile_framework_9(self):
        self.run('compile_framework_9')

    def define_compile_framework_external_exception_handling(cls):
        def before(n, x):
            x = X(0)
            return n, x, None, None, None, None, None, None, None, None, None, None

        @dont_look_inside
        def g(x):
            if x > 200:
                return 2
            raise ValueError
        @dont_look_inside
        def h(x):
            if x > 150:
                raise ValueError
            return 2

        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            try:
                x.x += g(n)
            except ValueError:
                x.x += 1
            try:
                x.x += h(n)
            except ValueError:
                x.x -= 1
            n -= 1
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s

        def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            check(x.x == 1800 * 2 + 150 * 2 + 200 - 1850)

        return before, f, after

    def test_compile_framework_external_exception_handling(self):
        self.run('compile_framework_external_exception_handling')

    def define_compile_framework_bug1(self):
        @elidable
        def nonmoving():
            x = X(1)
            for i in range(7):
                rgc.collect()
            return x

        @dont_look_inside
        def do_more_stuff():
            x = X(5)
            for i in range(7):
                rgc.collect()
            return x

        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            x0 = do_more_stuff()
            check(nonmoving().x == 1)
            n -= 1
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s

        return None, f, None

    def test_compile_framework_bug1(self):
        self.run('compile_framework_bug1', 200)

    def define_compile_framework_vref(self):
        from rpython.rlib.jit import virtual_ref, virtual_ref_finish
        class A:
            pass
        glob = A()
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            a = A()
            glob.v = vref = virtual_ref(a)
            virtual_ref_finish(vref, a)
            n -= 1
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        return None, f, None

    def test_compile_framework_vref(self):
        self.run('compile_framework_vref', 200)

    def define_compile_framework_float(self):
        # test for a bug: the fastpath_malloc does not save and restore
        # xmm registers around the actual call to the slow path
        class A:
            x0 = x1 = x2 = x3 = x4 = x5 = x6 = x7 = 0
        @dont_look_inside
        def escape1(a):
            a.x0 += 0
            a.x1 += 6
            a.x2 += 12
            a.x3 += 18
            a.x4 += 24
            a.x5 += 30
            a.x6 += 36
            a.x7 += 42
        @dont_look_inside
        def escape2(n, f0, f1, f2, f3, f4, f5, f6, f7):
            check(f0 == n + 0.0)
            check(f1 == n + 0.125)
            check(f2 == n + 0.25)
            check(f3 == n + 0.375)
            check(f4 == n + 0.5)
            check(f5 == n + 0.625)
            check(f6 == n + 0.75)
            check(f7 == n + 0.875)
        @unroll_safe
        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            i = 0
            while i < 42:
                m = n + i
                f0 = m + 0.0
                f1 = m + 0.125
                f2 = m + 0.25
                f3 = m + 0.375
                f4 = m + 0.5
                f5 = m + 0.625
                f6 = m + 0.75
                f7 = m + 0.875
                a1 = A()
                # at this point, all or most f's are still in xmm registers
                escape1(a1)
                escape2(m, f0, f1, f2, f3, f4, f5, f6, f7)
                i += 1
            n -= 1
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        return None, f, None

    def test_compile_framework_float(self):
        self.run('compile_framework_float')

    def define_compile_framework_minimal_size_in_nursery(self):
        S = lltype.GcStruct('S')    # no fields!
        T = lltype.GcStruct('T', ('i', lltype.Signed))
        @unroll_safe
        def f42(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
            lst1 = []
            lst2 = []
            i = 0
            while i < 42:
                s1 = lltype.malloc(S)
                t1 = lltype.malloc(T)
                t1.i = 10000 + i + n
                lst1.append(s1)
                lst2.append(t1)
                i += 1
            i = 0
            while i < 42:
                check(lst2[i].i == 10000 + i + n)
                i += 1
            n -= 1
            return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
        return None, f42, None

    def test_compile_framework_minimal_size_in_nursery(self):
        self.run('compile_framework_minimal_size_in_nursery')

    def define_compile_framework_call_assembler(self):
        S = lltype.GcForwardReference()
        S.become(lltype.GcStruct('S', ('s', lltype.Ptr(S))))
        driver = JitDriver(greens = [], reds = 'auto', is_recursive=True)

        def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s0):
            driver.jit_merge_point()
            i = 0
            prev_s = lltype.nullptr(S)
            while i < 100:
                s = lltype.malloc(S)
                s.s = prev_s
                prev_s = s
                i += 1
            return n - 1, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s0

        return None, f, None

    def test_compile_framework_call_assembler(self):
        self.run('compile_framework_call_assembler')

    def define_pinned_simple(cls):
        class H:
            inst = None
        helper = H()

        @dont_look_inside
        def get_y():
            if not helper.inst:
                helper.inst = Y()
                helper.inst.x = 101
                check(rgc.pin(helper.inst))
            else:
                check(rgc._is_pinned(helper.inst))
            return helper.inst

        def fn(n, x, *args):
            t = get_y()
            promote(t)
            t.x += 11
            n -= 1
            return (n, x) + args

        return None, fn, None

    def test_pinned_simple(self):
        self.run('pinned_simple')

    def define_pinned_unpin(cls):
        class H:
            inst = None
            pinned = False
            count_pinned = 0
            count_unpinned = 0
        helper = H()

        @dont_look_inside
        def get_y(n):
            if not helper.inst:
                helper.inst = Y()
                helper.inst.x = 101
                helper.pinned = True
                check(rgc.pin(helper.inst))
            elif n < 100 and helper.pinned:
                rgc.unpin(helper.inst)
                helper.pinned = False
            #
            if helper.pinned:
                check(rgc._is_pinned(helper.inst))
                helper.count_pinned += 1
            else:
                check(not rgc._is_pinned(helper.inst))
                helper.count_unpinned += 1
            return helper.inst

        def fn(n, x, *args):
            t = get_y(n)
            promote(t)
            check(t.x == 101)
            n -= 1
            return (n, x) + args

        def after(n, x, *args):
            check(helper.count_pinned > 0)
            check(helper.count_unpinned > 0)
            check(not helper.pinned)

        return None, fn, after

    def test_pinned_unpin(self):
        self.run('pinned_unpin')

    def define_multiple_pinned(cls):
        class H:
            inst1 = None
            inst2 = None
            inst3 = None
            initialised = False
        helper = H()

        @dont_look_inside
        def get_instances():
            if not helper.initialised:
                helper.inst1 = Y()
                helper.inst1.x = 101
                check(rgc.pin(helper.inst1))
                #
                helper.inst2 = Y()
                helper.inst2.x = 102
                #
                helper.inst3 = Y()
                helper.inst3.x = 103
                check(rgc.pin(helper.inst3))
                #
                helper.initialised = True
            #
            check(rgc._is_pinned(helper.inst1))
            check(not rgc._is_pinned(helper.inst2))
            check(rgc._is_pinned(helper.inst3))
            return (helper.inst1, helper.inst2, helper.inst3)

        def fn(n, x, *args):
            inst1, inst2, inst3 = get_instances()
            promote(inst1)
            promote(inst2)
            promote(inst3)
            #
            check(inst1.x == 101)
            check(inst2.x == 102)
            check(inst3.x == 103)
            #
            n -= 1
            return (n, x) + args
        
        return None, fn, None

    def test_multiple_pinned(self):
        self.run('multiple_pinned')