[gforth] / gforth / kernel / cond.fs  

gforth: gforth/kernel/cond.fs


1 : anton 1.1 \ Structural Conditionals 12dec92py
2 :    
3 : anton 1.21 \ Copyright (C) 1995,1996,1997,2000,2003,2004,2007 Free Software Foundation, Inc.
4 : anton 1.1
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 : anton 1.22 \ as published by the Free Software Foundation, either version 3
10 : anton 1.1 \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 : anton 1.22 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.1
20 :     here 0 , \ just a dummy, the real value of locals-list is patched into it in glocals.fs
21 :     AConstant locals-list \ acts like a variable that contains
22 :     \ a linear list of locals names
23 :    
24 :    
25 :     variable dead-code \ true if normal code at "here" would be dead
26 :     variable backedge-locals
27 :     \ contains the locals list that BEGIN will assume to be live on
28 :     \ the back edge if the BEGIN is unreachable from above. Set by
29 :     \ ASSUME-LIVE, reset by UNREACHABLE.
30 :    
31 :     : UNREACHABLE ( -- ) \ gforth
32 :     \ declares the current point of execution as unreachable
33 :     dead-code on
34 :     0 backedge-locals ! ; immediate
35 :    
36 :     : ASSUME-LIVE ( orig -- orig ) \ gforth
37 :     \ used immediatly before a BEGIN that is not reachable from
38 :     \ above. causes the BEGIN to assume that the same locals are live
39 :     \ as at the orig point
40 :     dup orig?
41 :     2 pick backedge-locals ! ; immediate
42 :    
43 :     \ Control Flow Stack
44 :     \ orig, etc. have the following structure:
45 :     \ type ( defstart, live-orig, dead-orig, dest, do-dest, scopestart) ( TOS )
46 :     \ address (of the branch or the instruction to be branched to) (second)
47 :     \ locals-list (valid at address) (third)
48 :    
49 :     \ types
50 : jwilke 1.6 [IFUNDEF] defstart
51 :     0 constant defstart \ usally defined in comp.fs
52 :     [THEN]
53 : anton 1.1 1 constant live-orig
54 :     2 constant dead-orig
55 :     3 constant dest \ the loopback branch is always assumed live
56 :     4 constant do-dest
57 :     5 constant scopestart
58 :    
59 :     : def? ( n -- )
60 :     defstart <> abort" unstructured " ;
61 :    
62 :     : orig? ( n -- )
63 :     dup live-orig <> swap dead-orig <> and abort" expected orig " ;
64 :    
65 :     : dest? ( n -- )
66 :     dest <> abort" expected dest " ;
67 :    
68 :     : do-dest? ( n -- )
69 :     do-dest <> abort" expected do-dest " ;
70 :    
71 :     : scope? ( n -- )
72 :     scopestart <> abort" expected scope " ;
73 :    
74 :     : non-orig? ( n -- )
75 :     dest scopestart 1+ within 0= abort" expected dest, do-dest or scope" ;
76 :    
77 :     : cs-item? ( n -- )
78 :     live-orig scopestart 1+ within 0= abort" expected control flow stack item" ;
79 :    
80 :     3 constant cs-item-size
81 :    
82 : crook 1.7 : CS-PICK ( ... u -- ... destu ) \ tools-ext c-s-pick
83 : anton 1.1 1+ cs-item-size * 1- >r
84 :     r@ pick r@ pick r@ pick
85 :     rdrop
86 :     dup non-orig? ;
87 :    
88 : crook 1.7 : CS-ROLL ( destu/origu .. dest0/orig0 u -- .. dest0/orig0 destu/origu ) \ tools-ext c-s-roll
89 : anton 1.1 1+ cs-item-size * 1- >r
90 :     r@ roll r@ roll r@ roll
91 :     rdrop
92 :     dup cs-item? ;
93 :    
94 :     : cs-push-part ( -- list addr )
95 : pazsan 1.3 locals-list @ here ;
96 : anton 1.1
97 :     : cs-push-orig ( -- orig )
98 :     cs-push-part dead-code @
99 :     if
100 :     dead-orig
101 :     else
102 :     live-orig
103 :     then ;
104 :    
105 :     \ Structural Conditionals 12dec92py
106 :    
107 : anton 1.18 defer other-control-flow ( -- )
108 :     \ hook for control-flow stuff that's not handled by begin-like etc.
109 :    
110 : anton 1.1 : ?struc ( flag -- ) abort" unstructured " ;
111 :     : sys? ( sys -- ) dup 0= ?struc ;
112 :     : >mark ( -- orig )
113 : anton 1.18 cs-push-orig 0 , other-control-flow ;
114 : anton 1.11 : >resolve ( addr -- )
115 : anton 1.13 here swap !
116 : anton 1.12 basic-block-end ;
117 : anton 1.13 : <resolve ( addr -- ) , ;
118 : anton 1.1
119 :     : BUT
120 :     1 cs-roll ; immediate restrict
121 :     : YET
122 :     0 cs-pick ; immediate restrict
123 :    
124 :     \ Structural Conditionals 12dec92py
125 :    
126 :     : AHEAD ( compilation -- orig ; run-time -- ) \ tools-ext
127 : pazsan 1.14 POSTPONE branch >mark POSTPONE unreachable ; immediate restrict
128 : anton 1.1
129 :     : IF ( compilation -- orig ; run-time f -- ) \ core
130 : pazsan 1.14 POSTPONE ?branch >mark ; immediate restrict
131 : anton 1.1
132 :     : ?DUP-IF ( compilation -- orig ; run-time n -- n| ) \ gforth question-dupe-if
133 : crook 1.5 \G This is the preferred alternative to the idiom "@code{?DUP IF}", since it can be
134 : anton 1.1 \G better handled by tools like stack checkers. Besides, it's faster.
135 : pazsan 1.14 POSTPONE ?dup-?branch >mark ; immediate restrict
136 : anton 1.1
137 :     : ?DUP-0=-IF ( compilation -- orig ; run-time n -- n| ) \ gforth question-dupe-zero-equals-if
138 : pazsan 1.14 POSTPONE ?dup-0=-?branch >mark ; immediate restrict
139 : anton 1.1
140 :     Defer then-like ( orig -- )
141 :     : cs>addr ( orig/dest -- ) drop >resolve drop ;
142 :     ' cs>addr IS then-like
143 :    
144 :     : THEN ( compilation orig -- ; run-time -- ) \ core
145 :     dup orig? then-like ; immediate restrict
146 :    
147 :     ' THEN alias ENDIF ( compilation orig -- ; run-time -- ) \ gforth
148 :     immediate restrict
149 :     \ Same as "THEN". This is what you use if your program will be seen by
150 :     \ people who have not been brought up with Forth (or who have been
151 :     \ brought up with fig-Forth).
152 :    
153 : anton 1.20 : ELSE ( compilation orig1 -- orig2 ; run-time -- ) \ core
154 : anton 1.1 POSTPONE ahead
155 :     1 cs-roll
156 :     POSTPONE then ; immediate restrict
157 :    
158 :     Defer begin-like ( -- )
159 :     ' noop IS begin-like
160 :    
161 :     : BEGIN ( compilation -- dest ; run-time -- ) \ core
162 : anton 1.11 begin-like cs-push-part dest
163 : anton 1.12 basic-block-end ; immediate restrict
164 : anton 1.1
165 :     Defer again-like ( dest -- addr )
166 :     ' nip IS again-like
167 :    
168 :     : AGAIN ( compilation dest -- ; run-time -- ) \ core-ext
169 : pazsan 1.14 dest? again-like POSTPONE branch <resolve ; immediate restrict
170 : anton 1.1
171 : anton 1.8 Defer until-like ( list addr xt1 xt2 -- )
172 :     :noname ( list addr xt1 xt2 -- )
173 :     drop compile, <resolve drop ;
174 :     IS until-like
175 : anton 1.1
176 :     : UNTIL ( compilation dest -- ; run-time f -- ) \ core
177 : pazsan 1.14 dest? ['] ?branch ['] ?branch-lp+!# until-like ; immediate restrict
178 : anton 1.1
179 :     : WHILE ( compilation dest -- orig dest ; run-time f -- ) \ core
180 :     POSTPONE if
181 :     1 cs-roll ; immediate restrict
182 :    
183 :     : REPEAT ( compilation orig dest -- ; run-time -- ) \ core
184 :     POSTPONE again
185 :     POSTPONE then ; immediate restrict
186 :    
187 :     \ counted loops
188 :    
189 :     \ leave poses a little problem here
190 :     \ we have to store more than just the address of the branch, so the
191 :     \ traditional linked list approach is no longer viable.
192 :     \ This is solved by storing the information about the leavings in a
193 :     \ special stack.
194 :    
195 :     \ !! remove the fixed size limit. 'Tis not hard.
196 :     20 constant leave-stack-size
197 :     create leave-stack 60 cells allot
198 :     Avariable leave-sp leave-stack 3 cells + leave-sp !
199 :    
200 :     : clear-leave-stack ( -- )
201 :     leave-stack leave-sp ! ;
202 :    
203 :     \ : leave-empty? ( -- f )
204 :     \ leave-sp @ leave-stack = ;
205 :    
206 :     : >leave ( orig -- )
207 :     \ push on leave-stack
208 :     leave-sp @
209 :     dup [ leave-stack 60 cells + ] Aliteral
210 :     >= abort" leave-stack full"
211 :     tuck ! cell+
212 :     tuck ! cell+
213 :     tuck ! cell+
214 :     leave-sp ! ;
215 :    
216 :     : leave> ( -- orig )
217 :     \ pop from leave-stack
218 :     leave-sp @
219 :     dup leave-stack <= IF
220 :     drop 0 0 0 EXIT THEN
221 :     cell - dup @ swap
222 :     cell - dup @ swap
223 :     cell - dup @ swap
224 :     leave-sp ! ;
225 :    
226 :     : DONE ( compilation orig -- ; run-time -- ) \ gforth
227 : pazsan 1.23 \g resolves all LEAVEs up to the compilaton orig (from a BEGIN)
228 : anton 1.1 drop >r drop
229 :     begin
230 :     leave>
231 :     over r@ u>=
232 :     while
233 :     POSTPONE then
234 :     repeat
235 :     >leave rdrop ; immediate restrict
236 :    
237 :     : LEAVE ( compilation -- ; run-time loop-sys -- ) \ core
238 :     POSTPONE ahead
239 :     >leave ; immediate restrict
240 :    
241 :     : ?LEAVE ( compilation -- ; run-time f | f loop-sys -- ) \ gforth question-leave
242 :     POSTPONE 0= POSTPONE if
243 :     >leave ; immediate restrict
244 :    
245 :     : DO ( compilation -- do-sys ; run-time w1 w2 -- loop-sys ) \ core
246 :     POSTPONE (do)
247 :     POSTPONE begin drop do-dest
248 :     ( 0 0 0 >leave ) ; immediate restrict
249 :    
250 :     : ?do-like ( -- do-sys )
251 :     ( 0 0 0 >leave )
252 :     >mark >leave
253 :     POSTPONE begin drop do-dest ;
254 :    
255 :     : ?DO ( compilation -- do-sys ; run-time w1 w2 -- | loop-sys ) \ core-ext question-do
256 : pazsan 1.14 POSTPONE (?do) ?do-like ; immediate restrict
257 : anton 1.1
258 :     : +DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys ) \ gforth plus-do
259 : pazsan 1.14 POSTPONE (+do) ?do-like ; immediate restrict
260 : anton 1.1
261 :     : U+DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys ) \ gforth u-plus-do
262 : pazsan 1.14 POSTPONE (u+do) ?do-like ; immediate restrict
263 : anton 1.1
264 :     : -DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys ) \ gforth minus-do
265 : pazsan 1.14 POSTPONE (-do) ?do-like ; immediate restrict
266 : anton 1.1
267 :     : U-DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys ) \ gforth u-minus-do
268 : pazsan 1.14 POSTPONE (u-do) ?do-like ; immediate restrict
269 : anton 1.1
270 :     : FOR ( compilation -- do-sys ; run-time u -- loop-sys ) \ gforth
271 :     POSTPONE (for)
272 :     POSTPONE begin drop do-dest
273 :     ( 0 0 0 >leave ) ; immediate restrict
274 :    
275 :     \ LOOP etc. are just like UNTIL
276 :    
277 :     : loop-like ( do-sys xt1 xt2 -- )
278 :     >r >r 0 cs-pick swap cell - swap 1 cs-roll r> r> rot do-dest?
279 :     until-like POSTPONE done POSTPONE unloop ;
280 :    
281 :     : LOOP ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 ) \ core
282 : pazsan 1.14 ['] (loop) ['] (loop)-lp+!# loop-like ; immediate restrict
283 : anton 1.1
284 :     : +LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 ) \ core plus-loop
285 : pazsan 1.14 ['] (+loop) ['] (+loop)-lp+!# loop-like ; immediate restrict
286 : anton 1.1
287 :     \ !! should the compiler warn about +DO..-LOOP?
288 :     : -LOOP ( compilation do-sys -- ; run-time loop-sys1 u -- | loop-sys2 ) \ gforth minus-loop
289 : pazsan 1.14 ['] (-loop) ['] (-loop)-lp+!# loop-like ; immediate restrict
290 : anton 1.1
291 :     \ A symmetric version of "+LOOP". I.e., "-high -low ?DO -inc S+LOOP"
292 :     \ will iterate as often as "high low ?DO inc S+LOOP". For positive
293 :     \ increments it behaves like "+LOOP". Use S+LOOP instead of +LOOP for
294 :     \ negative increments.
295 :     : S+LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 ) \ gforth s-plus-loop
296 : pazsan 1.14 ['] (s+loop) ['] (s+loop)-lp+!# loop-like ; immediate restrict
297 : anton 1.1
298 :     : NEXT ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 ) \ gforth
299 : pazsan 1.14 ['] (next) ['] (next)-lp+!# loop-like ; immediate restrict
300 : anton 1.1
301 :     \ Structural Conditionals 12dec92py
302 :    
303 :     Defer exit-like ( -- )
304 :     ' noop IS exit-like
305 :    
306 :     : EXIT ( compilation -- ; run-time nest-sys -- ) \ core
307 : anton 1.12 \G Return to the calling definition; usually used as a way of
308 :     \G forcing an early return from a definition. Before
309 :     \G @code{EXIT}ing you must clean up the return stack and
310 :     \G @code{UNLOOP} any outstanding @code{?DO}...@code{LOOP}s.
311 : anton 1.1 exit-like
312 :     POSTPONE ;s
313 : anton 1.12 basic-block-end
314 : anton 1.1 POSTPONE unreachable ; immediate restrict
315 :    
316 :     : ?EXIT ( -- ) ( compilation -- ; run-time nest-sys f -- | nest-sys ) \ gforth
317 :     POSTPONE if POSTPONE exit POSTPONE then ; immediate restrict
318 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help