[gforth] / gforth / kernel / cond.fs  

gforth: gforth/kernel/cond.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help