-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.fs
More file actions
496 lines (431 loc) · 10.6 KB
/
control.fs
File metadata and controls
496 lines (431 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
\ control.fs control structur words
\
\ Copyright (C) 1995-97 Martin Anton Ertl, Christian Pirker
\
\ This file is part of RAFTS.
\
\ RAFTS is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation; either version 2
\ of the License, or (at your option) any later version.
\
\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\ GNU General Public License for more details.
\
\ You should have received a copy of the GNU General Public License
\ along with this program; if not, write to the Free Software
\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
variable dead-code
dead-code off
: unreachable ( -- )
dead-code on ;
: assume-live ( -- )
dead-code off ;
: compile-forward-branch ( il -- )
0 over il-reg !
NULL over il-val !
inst-ils-insert
basic-exit
branch-info 2@ drop 0 0 >control
?trace $0010 [IF]
." forward branch " .cs cr
[THEN]
basic-init ;
: compile-if ( flag -- ) ( C: -- orig )
data> I_0BRANCH uop
compile-forward-branch ;
: intermediate-if ( flag -- )
0 -1 word-good word-regs-adjust
word-regs-get >control
?trace $0010 [IF]
." IF: "
word-regs-print .cs cr
[THEN]
;
: compile-ahead ( -- ) ( C: -- orig )
0 0 I_BRANCH terminal
compile-forward-branch ;
: intermediate-ahead ( -- )
word-regs-get >control
unreachable
?trace $0010 [IF]
." AHEAD: "
word-regs-print .cs cr
[THEN]
;
: compile-then ( -- ) ( C: orig -- )
basic-exit
?trace $0010 [IF]
." then " hex.s .cs cr
[THEN]
here control> 2drop back-patch-beq
basic-init ;
: intermediate-then ( -- )
control>
dead-code @ if
?trace $0010 [IF]
." UNREACHABLE (then):" cr
[THEN]
dead-code off
else
>r word-regs-get >r
2over 2over + rot rot +
?trace $0010 [IF]
." REACHABLE (then):"
2dup . . cr
[THEN]
<> if
?trace $0010 [IF]
." stackdepth different (then) !!!" cr
[THEN]
endif
word-regs-max
2r> or
endif
word-regs-put
?trace $0010 [IF]
." THEN: "
word-regs-print .cs cr
[THEN]
;
>target
: if ( flag -- )
intermediate-if
['] compile-if gforth-compile, ; immediate compile-only
: ahead ( -- )
intermediate-ahead
['] compile-ahead gforth-compile, ; immediate compile-only
: then ( -- )
intermediate-then
['] compile-then gforth-compile, ; immediate compile-only
: endif ( -- )
vtarget
postpone then
vsource ; immediate compile-only
: else ( -- )
vtarget
postpone ahead 1 cs-roll postpone then
vsource ; immediate compile-only
>source
: compile-begin ( -- ) ( C: -- dest )
basic-exit
here 0 0 >control
?trace $0010 [IF]
." begin " .cs cr
[THEN]
basic-init ;
: intermediate-begin ( -- )
word-regs-get >control
?trace $0010 [IF]
." BEGIN: "
word-regs-print .cs cr
[THEN]
;
: compile-again ( -- ) ( C: dest -- )
?trace $0010 [IF]
." again " .cs cr
[THEN]
0 0 I_BRANCH terminal 0 over il-reg !
control> 2drop over il-val ! inst-ils-insert
basic-exit
basic-init ;
: intermediate-again ( -- )
control>
dead-code @ if
?trace $0010 [IF]
." UNREACHABLE (again):" cr
[THEN]
dead-code off
else
>r word-regs-get >r
2over 2over + rot rot +
?trace $0010 [IF]
." REACHABLE (again):"
2dup . . cr
[THEN]
<> if
?trace $0010 [IF]
." stackdepth different (again) !!!" cr
[THEN]
endif
word-regs-max
2r> or
endif
word-regs-put
?trace $0010 [IF]
." AGAIN: "
word-regs-print .cs cr
[THEN]
unreachable
;
: compile-until ( flag -- ) ( C: dest -- )
?trace $0010 [IF]
." until " .cs cr
[THEN]
data> I_0BRANCH uop 0 over il-reg !
control> 2drop over il-val ! inst-ils-insert
basic-exit
basic-init ;
: intermediate-until ( flag -- )
0 -1 word-good word-regs-adjust
control>
dead-code @ if
?trace $0010 [IF]
." UNREACHABLE (until):" cr
[THEN]
dead-code off
else
>r word-regs-get >r
2over 2over + rot rot +
?trace $0010 [IF]
." REACHABLE (until):"
2dup . . cr
[THEN]
<> if
?trace $0010 [IF]
." stackdepth different (until) !!!" cr
[THEN]
endif
word-regs-max
2r> or
endif
word-regs-put
?trace $0010 [IF]
." UNTIL: "
word-regs-print .cs cr
[THEN]
;
>target
: begin ( -- )
intermediate-begin
['] compile-begin gforth-compile, ; immediate compile-only
: again ( -- )
intermediate-again
['] compile-again gforth-compile, ; immediate compile-only
: until ( flag -- )
intermediate-until
['] compile-until gforth-compile, ; immediate compile-only
: while ( flag -- ) ( C: dest -- \rig dest )
?trace $0010 [IF]
." WHILE: "
word-regs-print .cs cr
[THEN]
vtarget
postpone if 1 cs-roll
vsource ; immediate compile-only
: repeat ( -- ) ( C: orig dest -- )
?trace $0010 [IF]
." REPEAT: "
word-regs-print .cs cr
[THEN]
vtarget
postpone again postpone then
vsource ; immediate compile-only
>source
: compile-exit ( -- ) ( C: -- )
check-ra
basic-exit
(word-exit)
basic-init ;
>target
: exit ( -- )
unreachable
['] compile-exit gforth-compile, ; immediate compile-only
>source
$20 constant ls-size
variable ls-tos
0 ls-tos !
ls-size array ls-data
: >leave ( orig -- ) ( L: -- orig )
ls-tos @ ls-data !
1 ls-tos +! ;
: leave> ( -- orig ) ( L: orig -- )
-1 ls-tos +!
ls-tos @ ls-data @ ;
: compile-leave ( -- ) ( C: -- ) ( L: -- orig )
control> 2drop >leave ;
: compile-leave-init ( -- )
0 >leave ;
: intermediate-leave-init ( -- )
0 >leave ;
: compile-leave-exit ( -- )
begin
leave> dup
while
0 0 >control
compile-then
repeat
drop ;
: intermediate-leave-exit ( -- )
begin
leave> dup
while
drop
intermediate-then
repeat
drop ;
>target
: leave ( -- ) ( C: -- ) ( L: -- orig )
here >leave
vtarget
postpone ahead
vsource
['] compile-leave gforth-compile, ; immediate compile-only
: leave-init ( -- )
intermediate-leave-init
['] compile-leave-init gforth-compile, ; immediate compile-only
: leave-exit ( -- )
intermediate-leave-exit
['] compile-leave-exit gforth-compile, ; immediate compile-only
: do ( to from -- ) ( C: -- dest ) ( L: -- 0 ) ( R: -- to from )
vtarget
postpone swap postpone >r postpone >r
postpone leave-init
postpone begin
vsource ; immediate compile-only
: unloop ( -- ) ( C: -- ) ( L: -- ) ( R: to from -- )
vtarget
postpone rdrop postpone rdrop
vsource ; immediate compile-only
: loop ( -- ) ( C: dest -- ) ( L: 0 destu ... dest0 -- ) ( R: to from -- )
vtarget
postpone r> 1 postpone literal postpone + postpone dup postpone r@ postpone = postpone swap postpone >r postpone until
postpone leave-exit
postpone unloop
vsource ; immediate compile-only
: ?do ( to from -- ) ( C: -- dest ) ( L: -- 0 ) ( R: -- to from )
vtarget
postpone over postpone >r postpone dup postpone >r
postpone leave-init
postpone = postpone if
postpone leave postpone then
postpone begin
vsource ; immediate compile-only
: +loop ( n -- ) ( C: dest -- ) ( L: 0 destu ... dest0 -- ) ( R: to from -- )
vtarget
postpone dup postpone r> postpone dup postpone r@ postpone - postpone swap postpone rot postpone + postpone >r
postpone over postpone over postpone swap postpone over postpone + postpone xor
postpone rot postpone rot postpone xor postpone and postpone 0< postpone until
postpone leave-exit
postpone unloop
vsource ; immediate compile-only
: i ( -- n ) ( C: -- ) ( L: -- ) ( R: to from -- to from )
vtarget
postpone r@
vsource ; immediate compile-only
: i' ( -- n ) ( C: -- ) ( L: -- ) ( R: to from -- to from )
vtarget
postpone r> postpone r@ postpone swap postpone >r
vsource ; immediate compile-only
: j ( -- n ) ( C: -- ) ( L: -- ) ( R: to1 from1 to0 from0 -- to1 from1 to0 from0 )
vtarget
postpone r> postpone r> postpone r@ postpone swap postpone >r postpone swap postpone >r
vsource ; immediate compile-only
: case ( -- ) ( C: -- 0 )
0 0 0 >control ; immediate compile-only
: of ( x1 x2 -- | x1 ) ( C: u -- orig u+1 )
control> rot 1+ rot rot 2>r >r
?trace $0010 [IF]
." of:" hex.s cr
[THEN]
vtarget
postpone over postpone = postpone if postpone drop
vsource
r> 2r> >control ; immediate compile-only
: endof ( -- ) ( C: orig1 u -- orig2 u )
control> 2>r >r
vtarget
postpone else
vsource
r> 2r> >control ; immediate compile-only
: endcase ( x -- ) ( C: destu ... dest0 u -- )
vtarget
postpone drop
vsource
control> 2drop 0 ?do
vtarget
postpone then
vsource
loop ; immediate compile-only
>source
: compile-recurse ( -- )
basic-code-ptr @ NIL I_CALL terminal inst-ils-insert
basic-exit
basic-init
;
>target
: recurse ( -- )
['] compile-recurse gforth-compile, ; immediate compile-only
>source
$200 constant does-size
create does-addr
cell a,
does-size cells allot
: does-addr-inc ( -- )
cell does-addr +! ;
: !does ( addr -- )
word-regs-init dup word-regs-read lastih word-regs-write
?word-mode-direct [IF]
j,-docode:
[THEN]
\ insert comments !!!
lastxt tuck !
\ check later !!!
\ ['] compile,-nonative-xt over ih-compile-xt !
over swap ih-does-xt !
here basic-code-sav !
basic-code-ptr @ dp !
basic-init
lastxt ih-cfsize + compile,-literal
ih-cfsize + 0 I_BRANCH terminal inst-ils-insert
basic-exit
basic-code-ptr @ dup here over - flush-icache
here basic-code-ptr !
basic-code-sav @ dp !
lastxt 2cells + !
['] compile,-does lastxt 3cells + !
here lastxt tuck - flush-icache ;
: dodoes, ( -- )
dodoes:
?word-mode-direct [IF]
jal,
[ELSE]
j,
[THEN]
nop, ;
: ;dodoes ( n -- )
does-addr + @
!does ;
>target
also vtarget
word-good 0 0 :word ;dodoes
previous
>source
: compile-does> ( -- )
does-addr @
compile,-literal
vtarget ['] ;dodoes vsource compile,-now
compile-word-exit-does
here does-addr dup @ + ! does-addr-inc
lastih-init
dodoes,
compile-word-init-does ;
>target
: does> ( -- )
lastih word-regs-write
word-regs-init
1 0 word-good word-regs-adjust
['] compile-does> gforth-compile, ; immediate compile-only
>source
: postpone, ( w xt -- )
dup ['] execute = if
drop compile,
else
swap postpone literal compile,
endif ;
?test $0010 [IF]
cr ." Test for control.fs" cr
finish
[THEN]