]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/checker/checker.factor
basis: ERROR: changes.
[factor.git] / basis / compiler / tree / checker / checker.factor
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
8
9 ! Check some invariants; this can help catch compiler bugs.
10
11 ERROR: check-use-error value message ;
12
13 : check-use ( value uses -- )
14     [ empty? [ "No use" throw-check-use-error ] [ drop ] if ]
15     [
16         all-unique?
17         [ drop ]
18         [ "Uses not all unique" throw-check-use-error ] if
19     ] 2bi ;
20
21 : check-def-use ( -- )
22     def-use get [ uses>> check-use ] assoc-each ;
23
24 GENERIC: check-node* ( node -- )
25
26 M: #shuffle check-node*
27     [ [ mapping>> values ] [ [ in-d>> ] [ in-r>> ] bi append ] bi subset? [ "Bad mapping inputs" throw ] unless ]
28     [ [ mapping>> keys ] [ [ out-d>> ] [ out-r>> ] bi append ] bi set= [ "Bad mapping outputs" throw ] unless ]
29     bi ;
30
31 : check-lengths ( seq -- )
32     [ length ] map all-equal? [ "Bad lengths" throw ] unless ;
33
34 M: #copy check-node* inputs/outputs 2array check-lengths ;
35
36 M: #return-recursive check-node* inputs/outputs 2array check-lengths ;
37
38 M: #phi check-node*
39     [ [ phi-in-d>> <flipped> ] [ out-d>> ] bi 2array check-lengths ]
40     [ phi-in-d>> check-lengths ]
41     bi ;
42
43 M: #enter-recursive check-node*
44     [ [ label>> enter-out>> ] [ out-d>> ] bi assert= ]
45     [ [ in-d>> ] [ out-d>> ] bi 2array check-lengths ]
46     [ recursive-phi-in check-lengths ]
47     tri ;
48
49 M: #push check-node*
50     out-d>> length 1 = [ "Bad #push" throw ] unless ;
51
52 M: node check-node* drop ;
53
54 : check-values ( seq -- )
55     [ integer? ] all? [ "Bad values" throw ] unless ;
56
57 ERROR: check-node-error node error ;
58
59 : check-node ( node -- )
60     [
61         [ node-uses-values check-values ]
62         [ node-defs-values check-values ]
63         [ check-node* ]
64         tri
65     ] [ throw-check-node-error ] recover ;
66
67 SYMBOL: datastack
68 SYMBOL: retainstack
69 SYMBOL: terminated?
70
71 GENERIC: check-stack-flow* ( node -- )
72
73 : (check-stack-flow) ( nodes -- )
74     [ check-stack-flow* terminated? get not ] all? drop ;
75
76 : init-stack-flow ( -- )
77     V{ } clone datastack set
78     V{ } clone retainstack set ;
79
80 : check-stack-flow ( nodes -- )
81     [
82         init-stack-flow
83         (check-stack-flow)
84     ] with-scope ;
85
86 : check-inputs ( seq var -- )
87     [ dup length ] dip [ swap cut* swap ] change
88     sequence= [ "Bad stack flow" throw ] unless ;
89
90 : check-in-d ( node -- )
91     in-d>> datastack check-inputs ;
92
93 : check-in-r ( node -- )
94     in-r>> retainstack check-inputs ;
95
96 : check-outputs ( node var -- )
97     get push-all ;
98
99 : check-out-d ( node -- )
100     out-d>> datastack check-outputs ;
101
102 : check-out-r ( node -- )
103     out-r>> retainstack check-outputs ;
104
105 M: #introduce check-stack-flow* check-out-d ;
106
107 M: #push check-stack-flow* check-out-d ;
108
109 M: #call check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
110
111 M: #shuffle check-stack-flow*
112     { [ check-in-d ] [ check-in-r ] [ check-out-d ] [ check-out-r ] } cleave ;
113
114 : assert-datastack-empty ( -- )
115     datastack get empty? [ "Data stack not empty" throw ] unless ;
116
117 : assert-retainstack-empty ( -- )
118     retainstack get empty? [ "Retain stack not empty" throw ] unless ;
119
120 M: #return check-stack-flow*
121     check-in-d
122     assert-datastack-empty
123     terminated? get [ assert-retainstack-empty ] unless ;
124
125 M: #enter-recursive check-stack-flow*
126     check-out-d ;
127
128 M: #return-recursive check-stack-flow*
129     [ check-in-d ] [ check-out-d ] bi ;
130
131 M: #call-recursive check-stack-flow*
132     [ check-in-d ] [ check-out-d ] bi ;
133
134 : check-terminate-in-d ( #terminate -- )
135     in-d>> datastack get over length tail* sequence=
136     [ "Bad terminate data stack" throw ] unless ;
137
138 : check-terminate-in-r ( #terminate -- )
139     in-r>> retainstack get over length tail* sequence=
140     [ "Bad terminate retain stack" throw ] unless ;
141
142 M: #terminate check-stack-flow*
143     terminated? on
144     [ check-terminate-in-d ]
145     [ check-terminate-in-r ] bi ;
146
147 SYMBOL: branch-out
148
149 : check-branch ( nodes -- stack )
150     [
151         datastack [ clone ] change
152         V{ } clone retainstack set
153         (check-stack-flow)
154         terminated? get [ assert-retainstack-empty ] unless
155         terminated? get f datastack get ?
156     ] with-scope ;
157
158 M: #branch check-stack-flow*
159     [ check-in-d ]
160     [ children>> [ check-branch ] map branch-out set ]
161     bi ;
162
163 : check-phi-in ( #phi -- )
164     phi-in-d>> branch-out get [
165         dup [
166             over length tail* sequence= [
167                 "Branch outputs don't match phi inputs"
168                 throw
169             ] unless
170         ] [
171             2drop
172         ] if
173     ] 2each ;
174
175 : set-phi-datastack ( #phi -- )
176     phi-in-d>> first length
177     branch-out get [ ] find nip swap head* >vector datastack set ;
178
179 M: #phi check-stack-flow*
180     branch-out get [ ] any? [
181         [ check-phi-in ] [ set-phi-datastack ] [ check-out-d ] tri
182     ] [ drop terminated? on ] if ;
183
184 M: #recursive check-stack-flow*
185     [ check-in-d ] [ child>> (check-stack-flow) ] bi ;
186
187 M: #copy check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
188
189 M: #alien-node check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
190
191 M: #alien-callback check-stack-flow* child>> check-stack-flow ;
192
193 M: #declare check-stack-flow* drop ;
194
195 : check-nodes ( nodes -- )
196     compute-def-use
197     check-def-use
198     [ [ check-node ] each-node ]
199     [ check-stack-flow ]
200     bi ;