1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: sequences kernel sets namespaces accessors assocs
4 arrays combinators continuations columns math vectors
5 grouping stack-checker.branches
8 compiler.tree.recursive
9 compiler.tree.combinators ;
10 IN: compiler.tree.checker
12 ! Check some invariants; this can help catch compiler bugs.
14 ERROR: check-use-error value message ;
16 : check-use ( value uses -- )
17 [ empty? [ "No use" check-use-error ] [ drop ] if ]
18 [ all-unique? [ drop ] [ "Uses not all unique" check-use-error ] if ] 2bi ;
20 : check-def-use ( -- )
21 def-use get [ uses>> check-use ] assoc-each ;
23 GENERIC: check-node* ( node -- )
25 M: #shuffle check-node*
26 [ [ mapping>> values ] [ [ in-d>> ] [ in-r>> ] bi append ] bi subset? [ "Bad mapping inputs" throw ] unless ]
27 [ [ mapping>> keys ] [ [ out-d>> ] [ out-r>> ] bi append ] bi set= [ "Bad mapping outputs" throw ] unless ]
30 : check-lengths ( seq -- )
31 [ length ] map all-equal? [ "Bad lengths" throw ] unless ;
33 M: #copy check-node* inputs/outputs 2array check-lengths ;
35 M: #return-recursive check-node* inputs/outputs 2array check-lengths ;
38 [ [ phi-in-d>> <flipped> ] [ out-d>> ] bi 2array check-lengths ]
39 [ phi-in-d>> check-lengths ]
42 M: #enter-recursive check-node*
43 [ [ label>> enter-out>> ] [ out-d>> ] bi assert= ]
44 [ [ in-d>> ] [ out-d>> ] bi 2array check-lengths ]
45 [ recursive-phi-in check-lengths ]
49 out-d>> length 1 = [ "Bad #push" throw ] unless ;
51 M: node check-node* drop ;
53 : check-values ( seq -- )
54 [ integer? ] all? [ "Bad values" throw ] unless ;
56 ERROR: check-node-error node error ;
58 : check-node ( node -- )
60 [ node-uses-values check-values ]
61 [ node-defs-values check-values ]
64 ] [ check-node-error ] recover ;
70 GENERIC: check-stack-flow* ( node -- )
72 : (check-stack-flow) ( nodes -- )
73 [ check-stack-flow* terminated? get not ] all? drop ;
75 : init-stack-flow ( -- )
76 V{ } clone datastack set
77 V{ } clone retainstack set ;
79 : check-stack-flow ( nodes -- )
85 : check-inputs ( seq var -- )
86 [ dup length ] dip [ swap cut* swap ] change
87 sequence= [ "Bad stack flow" throw ] unless ;
89 : check-in-d ( node -- )
90 in-d>> datastack check-inputs ;
92 : check-in-r ( node -- )
93 in-r>> retainstack check-inputs ;
95 : check-outputs ( node var -- )
98 : check-out-d ( node -- )
99 out-d>> datastack check-outputs ;
101 : check-out-r ( node -- )
102 out-r>> retainstack check-outputs ;
104 M: #introduce check-stack-flow* check-out-d ;
106 M: #push check-stack-flow* check-out-d ;
108 M: #call check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
110 M: #shuffle check-stack-flow*
111 { [ check-in-d ] [ check-in-r ] [ check-out-d ] [ check-out-r ] } cleave ;
113 : assert-datastack-empty ( -- )
114 datastack get empty? [ "Data stack not empty" throw ] unless ;
116 : assert-retainstack-empty ( -- )
117 retainstack get empty? [ "Retain stack not empty" throw ] unless ;
119 M: #return check-stack-flow*
121 assert-datastack-empty
122 terminated? get [ assert-retainstack-empty ] unless ;
124 M: #enter-recursive check-stack-flow*
127 M: #return-recursive check-stack-flow*
128 [ check-in-d ] [ check-out-d ] bi ;
130 M: #call-recursive check-stack-flow*
131 [ check-in-d ] [ check-out-d ] bi ;
133 : check-terminate-in-d ( #terminate -- )
134 in-d>> datastack get over length tail* sequence=
135 [ "Bad terminate data stack" throw ] unless ;
137 : check-terminate-in-r ( #terminate -- )
138 in-r>> retainstack get over length tail* sequence=
139 [ "Bad terminate retain stack" throw ] unless ;
141 M: #terminate check-stack-flow*
143 [ check-terminate-in-d ]
144 [ check-terminate-in-r ] bi ;
148 : check-branch ( nodes -- stack )
150 datastack [ clone ] change
151 V{ } clone retainstack set
153 terminated? get [ assert-retainstack-empty ] unless
154 terminated? get f datastack get ?
157 M: #branch check-stack-flow*
159 [ children>> [ check-branch ] map branch-out set ]
162 : check-phi-in ( #phi -- )
163 phi-in-d>> branch-out get [
165 over length tail* sequence= [
166 "Branch outputs don't match phi inputs"
174 : set-phi-datastack ( #phi -- )
175 phi-in-d>> first length
176 branch-out get [ ] find nip swap head* >vector datastack set ;
178 M: #phi check-stack-flow*
179 branch-out get [ ] any? [
180 [ check-phi-in ] [ set-phi-datastack ] [ check-out-d ] tri
181 ] [ drop terminated? on ] if ;
183 M: #recursive check-stack-flow*
184 [ check-in-d ] [ child>> (check-stack-flow) ] bi ;
186 M: #copy check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
188 M: #alien-invoke check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
190 M: #alien-indirect check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
192 M: #alien-callback check-stack-flow* drop ;
194 M: #declare check-stack-flow* drop ;
196 : check-nodes ( nodes -- )
199 [ [ check-node ] each-node ]