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 IN: compiler.tree.checker
9 ! Check some invariants; this can help catch compiler bugs.
11 ERROR: check-use-error value message ;
13 : check-use ( value uses -- )
14 [ empty? [ "No use" check-use-error ] [ drop ] if ]
15 [ all-unique? [ drop ] [ "Uses not all unique" check-use-error ] if ] 2bi ;
17 : check-def-use ( -- )
18 def-use get [ uses>> check-use ] assoc-each ;
20 GENERIC: check-node* ( node -- )
22 M: #shuffle check-node*
23 [ [ mapping>> values ] [ [ in-d>> ] [ in-r>> ] bi append ] bi subset? [ "Bad mapping inputs" throw ] unless ]
24 [ [ mapping>> keys ] [ [ out-d>> ] [ out-r>> ] bi append ] bi set= [ "Bad mapping outputs" throw ] unless ]
27 : check-lengths ( seq -- )
28 [ length ] map all-equal? [ "Bad lengths" throw ] unless ;
30 M: #copy check-node* inputs/outputs 2array check-lengths ;
32 M: #return-recursive check-node* inputs/outputs 2array check-lengths ;
35 [ [ phi-in-d>> <flipped> ] [ out-d>> ] bi 2array check-lengths ]
36 [ phi-in-d>> check-lengths ]
39 M: #enter-recursive check-node*
40 [ [ label>> enter-out>> ] [ out-d>> ] bi assert= ]
41 [ [ in-d>> ] [ out-d>> ] bi 2array check-lengths ]
42 [ recursive-phi-in check-lengths ]
46 out-d>> length 1 = [ "Bad #push" throw ] unless ;
48 M: node check-node* drop ;
50 : check-values ( seq -- )
51 [ integer? ] all? [ "Bad values" throw ] unless ;
53 ERROR: check-node-error node error ;
55 : check-node ( node -- )
57 [ node-uses-values check-values ]
58 [ node-defs-values check-values ]
61 ] [ check-node-error ] recover ;
67 GENERIC: check-stack-flow* ( node -- )
69 : (check-stack-flow) ( nodes -- )
70 [ check-stack-flow* terminated? get not ] all? drop ;
72 : init-stack-flow ( -- )
73 V{ } clone datastack set
74 V{ } clone retainstack set ;
76 : check-stack-flow ( nodes -- )
82 : check-inputs ( seq var -- )
83 [ dup length ] dip [ swap cut* swap ] change
84 sequence= [ "Bad stack flow" throw ] unless ;
86 : check-in-d ( node -- )
87 in-d>> datastack check-inputs ;
89 : check-in-r ( node -- )
90 in-r>> retainstack check-inputs ;
92 : check-outputs ( node var -- )
95 : check-out-d ( node -- )
96 out-d>> datastack check-outputs ;
98 : check-out-r ( node -- )
99 out-r>> retainstack check-outputs ;
101 M: #introduce check-stack-flow* check-out-d ;
103 M: #push check-stack-flow* check-out-d ;
105 M: #call check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
107 M: #shuffle check-stack-flow*
108 { [ check-in-d ] [ check-in-r ] [ check-out-d ] [ check-out-r ] } cleave ;
110 : assert-datastack-empty ( -- )
111 datastack get empty? [ "Data stack not empty" throw ] unless ;
113 : assert-retainstack-empty ( -- )
114 retainstack get empty? [ "Retain stack not empty" throw ] unless ;
116 M: #return check-stack-flow*
118 assert-datastack-empty
119 terminated? get [ assert-retainstack-empty ] unless ;
121 M: #enter-recursive check-stack-flow*
124 M: #return-recursive check-stack-flow*
125 [ check-in-d ] [ check-out-d ] bi ;
127 M: #call-recursive check-stack-flow*
128 [ check-in-d ] [ check-out-d ] bi ;
130 : check-terminate-in-d ( #terminate -- )
131 in-d>> datastack get over length tail* sequence=
132 [ "Bad terminate data stack" throw ] unless ;
134 : check-terminate-in-r ( #terminate -- )
135 in-r>> retainstack get over length tail* sequence=
136 [ "Bad terminate retain stack" throw ] unless ;
138 M: #terminate check-stack-flow*
140 [ check-terminate-in-d ]
141 [ check-terminate-in-r ] bi ;
145 : check-branch ( nodes -- stack )
147 datastack [ clone ] change
148 V{ } clone retainstack set
150 terminated? get [ assert-retainstack-empty ] unless
151 terminated? get f datastack get ?
154 M: #branch check-stack-flow*
156 [ children>> [ check-branch ] map branch-out set ]
159 : check-phi-in ( #phi -- )
160 phi-in-d>> branch-out get [
162 over length tail* sequence= [
163 "Branch outputs don't match phi inputs"
171 : set-phi-datastack ( #phi -- )
172 phi-in-d>> first length
173 branch-out get [ ] find nip swap head* >vector datastack set ;
175 M: #phi check-stack-flow*
176 branch-out get [ ] any? [
177 [ check-phi-in ] [ set-phi-datastack ] [ check-out-d ] tri
178 ] [ drop terminated? on ] if ;
180 M: #recursive check-stack-flow*
181 [ check-in-d ] [ child>> (check-stack-flow) ] bi ;
183 M: #copy check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
185 M: #alien-node check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
187 M: #alien-callback check-stack-flow* child>> check-stack-flow ;
189 M: #declare check-stack-flow* drop ;
191 : check-nodes ( nodes -- )
194 [ [ check-node ] each-node ]