]> gitweb.factorcode.org Git - factor.git/blob - extra/tools/image-analyzer/gc-info/gc-info-tests.factor
36433079c854fc95fd4b79fb0ce429e77642089d
[factor.git] / extra / tools / image-analyzer / gc-info / gc-info-tests.factor
1 USING: accessors alien.c-types alien.syntax arrays assocs bit-arrays
2 classes.struct combinators combinators.short-circuit compiler
3 compiler.cfg.debugger compiler.cfg.instructions compiler.cfg.linearization
4 compiler.cfg.stack-frame compiler.codegen.gc-maps compiler.units fry generic
5 grouping io io.encodings.binary io.streams.byte-array kernel math namespaces
6 random sequences sequences.generalizations
7 tools.image-analyzer.gc-info tools.image-analyzer.utils tools.test vm
8 vocabs words ;
9 IN: tools.image-analyzer.gc-info.tests
10 QUALIFIED: cpu.x86.features.private
11 QUALIFIED: crypto.aes.utils
12 QUALIFIED: effects
13 QUALIFIED: gml.coremath
14 QUALIFIED: opencl
15
16 : normal? ( word -- ? )
17     { [ generic? ] [ primitive? ] [ inline? ] [ no-compile? ] } 1|| not ;
18
19 : word>gc-info ( word -- gc-info )
20     word>byte-array binary <byte-reader> <backwards-reader> [
21         gc-info read-struct-safe
22     ] with-input-stream ;
23
24 : cfg>gc-maps ( cfg -- gc-maps )
25     cfg>insns [ gc-map-insn? ] filter [ gc-map>> ] map
26     [ gc-map-needed? ] filter ;
27
28 : tally-gc-maps ( gc-maps -- seq/f )
29     [ f ] [ {
30         [ [ scrub-d>> length ] map supremum ]
31         [ [ scrub-r>> length ] map supremum ]
32         [ [ gc-root-offsets ] map largest-spill-slot ]
33         [ [ derived-root-offsets ] map [ keys ] map largest-spill-slot ]
34         [ length ]
35     } cleave 5 narray ] if-empty ;
36
37 ! Like word>gc-info but uses the compiler
38 : word>gc-info-expected ( word -- seq/f )
39     test-regs first dup stack-frame>> stack-frame
40     [ cfg>gc-maps tally-gc-maps ] with-variable ;
41
42 : same-gc-info? ( compiler-gc-info gc-info -- ? )
43     [ struct-slot-values = ]
44     [ [ not ] dip return-address-count>> 0 = and ] 2bi or ;
45
46 : base-pointer-groups-expected ( word -- seq )
47     test-regs first dup stack-frame>> stack-frame [
48         cfg>gc-maps [ derived-root-offsets { } like ] { } map-as
49     ] with-variable ;
50
51 : base-pointer-groups-decoded ( word -- seq )
52     word>gc-maps [
53         second second [ swap 2array ] map-index
54         [ nip -1 = ] assoc-reject
55     ] map ;
56
57 ! byte-array>bit-array
58 {
59     ?{
60         t t t t f t t t
61         t f f f f f f f
62     }
63 } [
64     B{ 239 1 } byte-array>bit-array
65 ] unit-test
66
67 { ?{ t t t t t t t t } } [ B{ 255 } byte-array>bit-array ] unit-test
68
69 ! word>gc-maps
70 { f } [
71     \ effects:<effect> word>gc-maps empty?
72 ] unit-test
73
74 { f } [
75     \ + word>gc-maps empty?
76 ] unit-test
77
78 { { } } [
79     \ word>gc-maps word>gc-maps
80 ] unit-test
81
82 ! Big test
83 { { } } [
84     all-words [ normal? ] filter 50 sample
85     [ [ word>gc-info-expected ] [ word>gc-info ] bi same-gc-info? ] reject
86 ] unit-test
87
88
89 ! Originally from llvm.types, but llvm moved to unmaintained
90 TYPEDEF: void* LLVMTypeRef
91 TYPEDEF: void* LLVMTypeHandleRef
92 FUNCTION: LLVMTypeRef LLVMResolveTypeHandle ( LLVMTypeHandleRef TypeHandle )
93 FUNCTION: LLVMTypeHandleRef LLVMCreateTypeHandle ( LLVMTypeRef PotentiallyAbstractTy )
94 FUNCTION: void LLVMRefineType ( LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy )
95 FUNCTION: void LLVMDisposeTypeHandle ( LLVMTypeHandleRef TypeHandle )
96
97 : resolve-types ( typeref typeref -- typeref )
98     over LLVMCreateTypeHandle [ LLVMRefineType ] dip
99     [ LLVMResolveTypeHandle ] keep LLVMDisposeTypeHandle ;
100
101 ! base-pointer-groups
102 { t } [
103     \ resolve-types
104     [ base-pointer-groups-expected ] [ base-pointer-groups-decoded ] bi =
105 ] unit-test
106
107
108 ! Tough words #1227
109 { t } [
110     \ resolve-types
111     [ word>gc-info-expected ] [ word>gc-info ] bi same-gc-info?
112 ] unit-test
113
114 { t } [
115     \ opencl:cl-queue-kernel
116     [ word>gc-info-expected ] [ word>gc-info ] bi same-gc-info?
117 ] unit-test
118
119 { t } [
120     \ crypto.aes.utils:bytes>words
121     [ word>gc-info-expected ] [ word>gc-info ] bi same-gc-info?
122 ] unit-test
123
124 { t } [
125     \ cpu.x86.features.private:(sse-version)
126     [ word>gc-info-expected ] [ word>gc-info ] bi same-gc-info?
127 ] unit-test
128
129 ! Ensure deterministic gc map generation.
130 : recompile-word>gc-info ( word -- gc-info )
131     [ 1array compile ] keep word>gc-info ;
132
133 : deterministic-gc-info? ( word -- ? )
134     20 swap '[
135         _ recompile-word>gc-info struct-slot-values
136         dup last 0 = [ drop f ] when
137     ] replicate all-equal? ;
138
139 { t t } [
140     \ opencl:cl-queue-kernel deterministic-gc-info?
141     \ gml.coremath:gml-determinant deterministic-gc-info?
142 ] unit-test