1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs columns combinators compiler.tree
4 compiler.tree.combinators compiler.tree.def-use
5 compiler.tree.recursive continuations grouping kernel math
6 namespaces sequences sets vectors ;
7 FROM: namespaces => set ;
8 IN: compiler.tree.checker
10 ! Check some invariants; this can help catch compiler bugs.
12 ERROR: check-use-error value message ;
14 : check-use ( value uses -- )
15 [ empty? [ "No use" check-use-error ] [ drop ] if ]
16 [ all-unique? [ drop ] [ "Uses not all unique" check-use-error ] if ] 2bi ;
18 : check-def-use ( -- )
19 def-use get [ uses>> check-use ] assoc-each ;
21 GENERIC: check-node* ( node -- )
23 M: #shuffle check-node*
24 [ [ mapping>> values ] [ [ in-d>> ] [ in-r>> ] bi append ] bi subset? [ "Bad mapping inputs" throw ] unless ]
25 [ [ mapping>> keys ] [ [ out-d>> ] [ out-r>> ] bi append ] bi set= [ "Bad mapping outputs" throw ] unless ]
28 : check-lengths ( seq -- )
29 [ length ] map all-equal? [ "Bad lengths" throw ] unless ;
31 M: #copy check-node* inputs/outputs 2array check-lengths ;
33 M: #return-recursive check-node* inputs/outputs 2array check-lengths ;
36 [ [ phi-in-d>> <flipped> ] [ out-d>> ] bi 2array check-lengths ]
37 [ phi-in-d>> check-lengths ]
40 M: #enter-recursive check-node*
41 [ [ label>> enter-out>> ] [ out-d>> ] bi assert= ]
42 [ [ in-d>> ] [ out-d>> ] bi 2array check-lengths ]
43 [ recursive-phi-in check-lengths ]
47 out-d>> length 1 = [ "Bad #push" throw ] unless ;
49 M: node check-node* drop ;
51 : check-values ( seq -- )
52 [ integer? ] all? [ "Bad values" throw ] unless ;
54 ERROR: check-node-error node error ;
56 : check-node ( node -- )
58 [ node-uses-values check-values ]
59 [ node-defs-values check-values ]
62 ] [ check-node-error ] recover ;
68 GENERIC: check-stack-flow* ( node -- )
70 : (check-stack-flow) ( nodes -- )
71 [ check-stack-flow* terminated? get not ] all? drop ;
73 : init-stack-flow ( -- )
74 V{ } clone datastack set
75 V{ } clone retainstack set ;
77 : check-stack-flow ( nodes -- )
83 : check-inputs ( seq var -- )
84 [ dup length ] dip [ swap cut* swap ] change
85 sequence= [ "Bad stack flow" throw ] unless ;
87 : check-in-d ( node -- )
88 in-d>> datastack check-inputs ;
90 : check-in-r ( node -- )
91 in-r>> retainstack check-inputs ;
93 : check-outputs ( node var -- )
96 : check-out-d ( node -- )
97 out-d>> datastack check-outputs ;
99 : check-out-r ( node -- )
100 out-r>> retainstack check-outputs ;
102 M: #introduce check-stack-flow* check-out-d ;
104 M: #push check-stack-flow* check-out-d ;
106 M: #call check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
108 M: #shuffle check-stack-flow*
109 { [ check-in-d ] [ check-in-r ] [ check-out-d ] [ check-out-r ] } cleave ;
111 : assert-datastack-empty ( -- )
112 datastack get empty? [ "Data stack not empty" throw ] unless ;
114 : assert-retainstack-empty ( -- )
115 retainstack get empty? [ "Retain stack not empty" throw ] unless ;
117 M: #return check-stack-flow*
119 assert-datastack-empty
120 terminated? get [ assert-retainstack-empty ] unless ;
122 M: #enter-recursive check-stack-flow*
125 M: #return-recursive check-stack-flow*
126 [ check-in-d ] [ check-out-d ] bi ;
128 M: #call-recursive check-stack-flow*
129 [ check-in-d ] [ check-out-d ] bi ;
131 : check-terminate-in-d ( #terminate -- )
132 in-d>> datastack get over length tail* sequence=
133 [ "Bad terminate data stack" throw ] unless ;
135 : check-terminate-in-r ( #terminate -- )
136 in-r>> retainstack get over length tail* sequence=
137 [ "Bad terminate retain stack" throw ] unless ;
139 M: #terminate check-stack-flow*
141 [ check-terminate-in-d ]
142 [ check-terminate-in-r ] bi ;
146 : check-branch ( nodes -- stack )
148 datastack [ clone ] change
149 V{ } clone retainstack set
151 terminated? get [ assert-retainstack-empty ] unless
152 terminated? get f datastack get ?
155 M: #branch check-stack-flow*
157 [ children>> [ check-branch ] map branch-out set ]
160 : check-phi-in ( #phi -- )
161 phi-in-d>> branch-out get [
163 over length tail* sequence= [
164 "Branch outputs don't match phi inputs"
172 : set-phi-datastack ( #phi -- )
173 phi-in-d>> first length
174 branch-out get [ ] find nip swap head* >vector datastack set ;
176 M: #phi check-stack-flow*
177 branch-out get [ ] any? [
178 [ check-phi-in ] [ set-phi-datastack ] [ check-out-d ] tri
179 ] [ drop terminated? on ] if ;
181 M: #recursive check-stack-flow*
182 [ check-in-d ] [ child>> (check-stack-flow) ] bi ;
184 M: #copy check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
186 M: #alien-node check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
188 M: #alien-callback check-stack-flow* child>> check-stack-flow ;
190 M: #declare check-stack-flow* drop ;
192 : check-nodes ( nodes -- )
195 [ [ check-node ] each-node ]