summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/forth/bootstrap/bootstrap.fs
blob: 0668cf7d8e6ccc06603eb896d75013a53a545f09 (plain)
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
\ tag: bootstrap of basic forth words
\ 
\ Copyright (C) 2003-2005 Stefan Reinauer, Patrick Mauritz
\ 
\ See the file "COPYING" for further information about
\ the copyright and warranty status of this work.
\ 

\ 
\ this file contains almost all forth words described
\ by the open firmware user interface. Some more complex
\ parts are found in seperate files (memory management,
\ vocabulary support)
\ 

\ 
\ often used constants (reduces dictionary size)
\ 

1 constant 1
2 constant 2
3 constant 3
-1 constant -1
0 constant 0

0 value my-self

\ 
\ 7.3.5.1 Numeric-base control
\ 

: decimal 10 base ! ;
: hex 16 base ! ;
: octal 8 base ! ;
hex

\ 
\ vocabulary words
\ 

variable current forth-last current !

: last 
  current @ 
  ;

variable #order 0 #order !

defer context
0 value vocabularies?

defer locals-end
0 value locals-dict
variable locals-dict-buf

\ 
\ 7.3.7 Flag constants
\ 

1 1 = constant true
0 1 = constant false

\ 
\ 7.3.9.2.2 Immediate words (part 1)
\ 

: (immediate) ( xt -- )
  1 - dup c@ 1 or swap c!
  ;

: (compile-only)
  1 - dup c@ 2 or swap c!
  ;

: immediate 
  last @ (immediate) 
  ;
  
: compile-only 
  last @ (compile-only) 
  ;

: flags? ( xt -- flags )
  /n /c + - c@ 7f and
  ;

: immediate? ( xt -- true|false )
  flags? 1 and 1 =
  ;

: compile-only? ( xt -- true|false )
  flags? 2 and 2 =
  ;

: [  0 state ! ; compile-only
: ] -1 state ! ; 



\ 
\ 7.3.9.2.1 Data space allocation
\ 

: allot here + here! ;
: ,  here /n allot ! ;
: c, here /c allot c! ;

: align
  /n here /n 1 - and -   \ how many bytes to next alignment
  /n 1 - and allot       \ mask out everything that is bigger 
  ;                      \ than cellsize-1

: null-align
  here dup align here swap - 0 fill 
  ;

: w, 
  here 1 and allot       \ if here is not even, we have to align.
  here /w allot w! 
  ;

: l, 
  /l here /l 1 - and -   \ same as in align, with /l
  /l 1 - and             \ if it's /l we are already aligned.
  allot
  here /l allot l! 
  ;


\ 
\ 7.3.6 comparison operators (part 1)
\ 

: <> = invert ;


\ 
\ 7.3.9.2.4 Miscellaneous dictionary (part 1)
\ 

: (to) ( xt-new xt-defer -- )
  /n + !
  ;

: >body ( xt -- a-addr )  /n 1 lshift + ;
: body> ( a-addr -- xt )  /n 1 lshift - ;

: reveal latest @ last ! ;
: recursive reveal ; immediate
: recurse latest @ /n +  , ; immediate

: noop ;

defer environment?
: no-environment?
  2drop false 
  ;

['] no-environment? ['] environment? (to)


\ 
\ 7.3.8.1 Conditional branches
\ 

\ A control stack entry is implemented using 2 data stack items
\ of the form ( addr type ). type can be one of the
\ following:
\   0 - orig
\   1 - dest
\   2 - do-sys

: resolve-orig here nip over /n + - swap ! ;
: (if) ['] do?branch , here 0 0 , ; compile-only
: (then) resolve-orig ; compile-only

variable tmp-comp-depth -1 tmp-comp-depth !
variable tmp-comp-buf 0 tmp-comp-buf !

: setup-tmp-comp ( -- )
  state @ 0 = (if)
    here tmp-comp-buf @ here! ,     \ save here and switch to tmp directory
    1 ,                              \ DOCOL
    depth tmp-comp-depth !          \ save control depth
    ]
  (then)
;

: execute-tmp-comp ( -- )
  depth tmp-comp-depth @ =
  (if)
    -1 tmp-comp-depth !
    ['] (semis) ,
    tmp-comp-buf @
    dup @ here!
    0 state !
    /n + execute
  (then)
;

: if setup-tmp-comp ['] do?branch , here 0 0 , ; immediate
: then resolve-orig execute-tmp-comp ; compile-only
: else ['] dobranch , here 0 0 , 2swap resolve-orig ; compile-only

\ 
\ 7.3.8.3 Conditional loops
\ 

\ some dummy words for see
: (begin) ;
: (again) ;
: (until) ;
: (while) ;
: (repeat) ;

\ resolve-dest requires a loop...
: (resolve-dest) here /n + nip - , ;
: (resolve-begin) setup-tmp-comp ['] (begin) , here 1 ; immediate
: (resolve-until) ['] (until) , ['] do?branch , (resolve-dest) execute-tmp-comp ; compile-only

: resolve-dest ( dest origN ... orig )
  2 >r
  (resolve-begin)
    \ Find topmost control stack entry with a type of 1 (dest)
    r> dup dup pick 1 = if
      \ Move it to the top
      roll
      swap 1 - roll
      \ Resolve it
      (resolve-dest)
      1		\ force exit
    else
      drop
      2 + >r
      0
    then
  (resolve-until)
;

: begin
  setup-tmp-comp
  ['] (begin) , 
  here
  1
  ; immediate

: again
  ['] (again) ,
  ['] dobranch , 
  resolve-dest
  execute-tmp-comp
  ; compile-only

: until
  ['] (until) ,
  ['] do?branch , 
  resolve-dest
  execute-tmp-comp
  ; compile-only

: while
  setup-tmp-comp
  ['] (while) ,
  ['] do?branch , 
  here 0 0 , 2swap  
  ; immediate

: repeat
  ['] (repeat) ,
  ['] dobranch , 
  resolve-dest resolve-orig
  execute-tmp-comp
  ; compile-only


\ 
\ 7.3.8.4 Counted loops
\ 

variable leaves 0 leaves !

: resolve-loop
  leaves @
  begin
    ?dup 
  while 
    dup @               \ leaves -- leaves *leaves )
    swap                \ -- *leaves leaves )
    here over -         \ -- *leaves leaves here-leaves
    swap !              \ -- *leaves
  repeat
  here nip - , 
  leaves !
  ;

: do
  setup-tmp-comp
  leaves @
  here 2
  ['] (do) , 
  0 leaves !
  ; immediate

: ?do
  setup-tmp-comp
  leaves @ 
  ['] (?do) ,
  here 2
  here leaves !
  0 ,
  ; immediate

: loop
  ['] (loop) ,
  resolve-loop
  execute-tmp-comp
  ; immediate 

: +loop
  ['] (+loop) ,
  resolve-loop
  execute-tmp-comp
  ; immediate


\ Using primitive versions of i and j
\ speeds up loops by 300%
\ : i r> r@ swap >r ;
\ : j r> r> r> r@ -rot >r >r swap >r ;

: unloop r> r> r> 2drop >r ;

: leave 
  ['] unloop , 
  ['] dobranch , 
  leaves @ 
  here leaves !  
  , 
  ; immediate

: ?leave if leave then ;

\ 
\ 7.3.8.2  Case statement
\ 
 
: case
  setup-tmp-comp
  0
; immediate

: endcase
  ['] drop , 
  0 ?do
    ['] then execute
  loop
  execute-tmp-comp
; immediate

: of
  1 + >r 
  ['] over , 
  ['] = , 
  ['] if execute 
  ['] drop , 
  r> 
  ; immediate

: endof
  >r 
  ['] else execute 
  r> 
  ; immediate

\ 
\ 7.3.8.5    Other control flow commands
\ 

: exit r> drop ;


\ 
\ 7.3.4.3 ASCII constants (part 1)
\ 

20 constant bl
07 constant bell
08 constant bs
0d constant carret
0a constant linefeed


\ 
\ 7.3.1.1 - stack duplication
\ 
: tuck swap over ;
: 3dup 2 pick 2 pick 2 pick ;

\ 
\ 7.3.1.2 - stack removal
\ 
: clear 0 depth! ;
: 3drop 2drop drop ;

\ 
\ 7.3.1.3 - stack rearrangement
\ 

: 2rot >r >r 2swap r> r> 2swap ;

\
\ 7.3.1.4 - return stack
\

\ Note: these words are not part of the official OF specification, however
\ they are part of the ANSI DPANS94 core extensions (see section 6.2) and
\ so this seems an appropriate place for them.
: 2>r r> -rot swap >r >r >r ;
: 2r> r> r> r> rot >r swap ;
: 2r@ r> r> r> 2dup >r >r rot >r swap ;

\ 
\ 7.3.2.1 - single precision integer arithmetic (part 1)
\ 

: u/mod 0 swap mu/mod drop ;
: 1+ 1 + ;
: 1- 1 - ;
: 2+ 2 + ;
: 2- 2 - ;
: even 1+ -2 and ;
: bounds over + swap ;

\ 
\ 7.3.2.2 bitwise logical operators
\ 
: << lshift ;
: >> rshift ;
: 2* 1 lshift ;
: u2/ 1 rshift ;
: 2/ 1 >>a ;
: not invert ;

\ 
\ 7.3.2.3 double number arithmetic
\ 

: s>d      dup 0 < ; 
: dnegate  0 0 2swap d- ;
: dabs     dup 0 < if dnegate then ;
: um/mod   mu/mod drop ;

\ symmetric division
: sm/rem  ( d n -- rem quot )
  over >r >r dabs r@ abs um/mod r> 0 < 
  if 
    negate 
  then 
  r> 0 < if 
    negate swap negate swap
  then
  ;

\ floored division
: fm/mod ( d n -- rem quot ) 
  dup >r 2dup xor 0 < >r sm/rem over 0 <> r> and if 
    1 - swap r> + swap exit 
  then
  r> drop
  ;

\ 
\ 7.3.2.1 - single precision integer arithmetic (part 2)
\ 

: */mod ( n1 n2 n3 -- quot rem ) >r m* r> fm/mod  ;
: */ ( n1 n2 n3 -- n1*n2/n3 ) */mod nip ;
: /mod >r s>d r> fm/mod ;
: mod /mod drop ;
: / /mod nip ;


\ 
\ 7.3.2.4 Data type conversion
\ 

: lwsplit ( quad -- w.lo w.hi )
  dup ffff and swap 10 rshift ffff and
;

: wbsplit ( word -- b.lo b.hi )
  dup ff and swap 8 rshift ff and
;

: lbsplit ( quad -- b.lo b2 b3 b.hi )
  lwsplit swap wbsplit rot wbsplit
;

: bwjoin ( b.lo b.hi -- word )
  ff and 8 lshift swap ff and or
;

: wljoin ( w.lo w.hi -- quad )
  ffff and 10 lshift swap ffff and or
;

: bljoin ( b.lo b2 b3 b.hi -- quad )
  bwjoin -rot bwjoin swap wljoin
;

: wbflip ( word -- word ) \ flips bytes in a word
  dup 8 rshift ff and swap ff and bwjoin
;

: lwflip ( q1 -- q2 ) 
  dup 10 rshift ffff and swap ffff and wljoin
;

: lbflip ( q1 -- q2 )
  dup 10 rshift ffff and wbflip swap ffff and wbflip wljoin
;

\ 
\ 7.3.2.5 address arithmetic
\ 

: /c* /c * ;
: /w* /w * ;
: /l* /l * ;
: /n* /n * ;
: ca+ /c* + ;
: wa+ /w* + ;
: la+ /l* + ;
: na+ /n* + ;
: ca1+ /c + ;
: wa1+ /w + ;
: la1+ /l + ;
: na1+ /n + ;
: aligned /n 1- + /n negate and ;
: char+ ca1+ ;
: cell+ na1+ ;
: chars /c* ;
: cells /n* ;
/n constant cell

\ 
\ 7.3.6 Comparison operators
\ 

: <= > not ;
: >= < not ;
: 0= 0 = ;
: 0<= 0 <= ;
: 0< 0 < ;
: 0<> 0 <> ;
: 0> 0 > ;
: 0>=  0 >= ;
: u<= u> not ;
: u>= u< not ;
: within  >r over > swap r> >= or not ;
: between 1 + within ;

\ 
\ 7.3.3.1 Memory access
\ 

: 2@ dup cell+ @ swap @  ;
: 2! dup >r ! r> cell+ ! ;

: <w@ w@ dup 8000 >= if 10000 - then ;

: comp ( str1 str2 len -- 0|1|-1 )
  >r 0 -rot r>
  bounds ?do
    dup c@ i c@ - dup if
      < if 1 else -1 then swap leave
    then 
    drop ca1+
  loop
  drop
;

\ compare two string

: $= ( str1 len1 str2 len2 -- true|false )
    rot ( str1 str2 len2 len1 )
    over ( str1 str2 len2 len1 len2 )  
    <> if ( str1 str2 len2 )
        3drop
        false
    else ( str1 str2 len2 )
        comp
	0=
    then
;

\ : +! tuck @ + swap ! ;
: off false swap ! ;
: on true swap ! ;
: blank bl fill ;
: erase 0 fill ;
: wbflips ( waddr len -- )
  bounds do i w@ wbflip i w! /w +loop
;

: lwflips ( qaddr len -- )
  bounds do i l@ lwflip i l! /l +loop
;

: lbflips ( qaddr len -- )
  bounds do i l@ lbflip i l! /l +loop
;


\ 
\ 7.3.8.6    Error handling (part 1)
\ 

variable catchframe
0 catchframe !

: catch
  my-self >r
  depth >r
  catchframe @ >r
  rdepth catchframe !
  execute
  r> catchframe !
  r> r> 2drop 0
  ;

: throw
  ?dup if
    catchframe @ rdepth!
    r> catchframe !
    r> swap >r depth!
    drop r>
    r> ['] my-self (to)
  then
  ;

\ 
\ 7.3.3.2 memory allocation
\ 

include memory.fs


\ 
\ 7.3.4.4 Console output (part 1)
\ 

defer emit

: type bounds ?do i c@ emit loop ;

\ this one obviously only works when called 
\ with a forth string as count fetches addr-1.
\ openfirmware has no such req. therefore it has to go:

\ : type 0 do count emit loop drop ;

: debug-type bounds ?do i c@ (emit) loop ;

\ 
\ 7.3.4.1 Text Input
\ 

0 value source-id 
0 value ib
variable #ib 0 #ib !
variable >in 0 >in !

: source ( -- addr len )
  ib #ib @
  ;

: /string  ( c-addr1 u1 n -- c-addr2 u2 )
   tuck - -rot + swap 
; 


\ 
\ pockets implementation for 7.3.4.1

100 constant pocketsize
4   constant numpockets
variable pockets 0 pockets !
variable whichpocket 0 whichpocket !

\ allocate 4 pockets to begin with
: init-pockets     ( -- )
  pocketsize numpockets * alloc-mem pockets !
  ;

: pocket ( ?? -- ?? )
  pocketsize whichpocket @ *
  pockets @ +
  whichpocket @ 1 + numpockets mod
  whichpocket !
  ;

\ span variable from 7.3.4.2
variable span 0 span !

\ if char is bl then any control character is matched
: findchar ( str len char -- offs true | false )
  swap 0 do
    over i + c@
    over dup bl = if <= else = then if
      2drop i dup dup leave
      \ i nip nip true exit \ replaces above
    then
  loop
  =
  \ drop drop false
  ;

: parse ( delim  text<delim>  -- str len )
  >r              \ save delimiter
  ib >in @ +
  span @ >in @ -  \ ib+offs len-offset.
  dup 0 < if      \ if we are already at the end of the string, return an empty string
    + 0	          \ move to end of input string
    r> drop
    exit
  then
  2dup r>         \ ib+offs len-offset ib+offs len-offset delim
  findchar if     \ look for the delimiter. 
    nip dup 1+
  else
     dup
  then
  >in +!
  \ dup -1 = if drop 0 then \ workaround for negative length
  ;

: skipws ( -- )
  ib span @        ( -- ib recvchars )
  begin
    dup >in @ > if    ( -- recvchars>offs )
      over >in @ +
      c@ bl <=
    else
      false
    then
  while
      1 >in +!
  repeat
  2drop
  ;

: parse-word (  < >text< >  -- str len )
  skipws bl parse
  ;

: word ( delim  <delims>text<delim>  -- pstr )
  pocket >r parse dup r@ c! bounds r> dup 2swap
  do
    char+ i c@ over c!
  loop
  drop
  ;

: ( 29 parse 2drop ; immediate
: \ span @ >in !   ; immediate



\ 
\ 7.3.4.7 String literals
\ 

: ",
  bounds ?do
    i c@ c,
  loop
  ;

: (")  ( -- addr len )
  r> dup 
  2 cells +                   ( r-addr addr )
  over cell+ @                ( r-addr addr len )
  rot over + aligned cell+ >r ( addr len R: r-addr )
  ;
 
: handle-text ( temp-addr len -- addr len )
  state @ if
    ['] (") , dup , ", null-align
  else
    pocket swap
    dup >r
    0 ?do
      over i + c@ over i + c!
    loop
    nip r>
  then
  ;

: s"
  22 parse handle-text
  ; immediate



\ 
\ 7.3.4.4 Console output (part 2)
\ 

: ."
  22 parse handle-text
  ['] type
  state @ if
    ,
  else
    execute
  then
  ; immediate

: .(
  29 parse handle-text
  ['] type
  state @ if
    ,
  else
    execute
  then
  ; immediate



\ 
\ 7.3.4.8 String manipulation
\ 

: count ( pstr -- str len ) 1+ dup 1- c@ ;

: pack  ( str len addr -- pstr )
  2dup c!     \ store len
  1+ swap 0 ?do
    over i + c@ over i + c!
  loop nip 1-
  ;

: lcc   ( char1 -- char2 ) dup 41 5a between if 20 + then ;
: upc   ( char1 -- char2 ) dup 61 7a between if 20 - then ;

: -trailing ( str len1 -- str len2 )
  begin 
    dup 0<> if  \ len != 0 ?
      2dup 1- + 
      c@ bl =
    else 
      false 
    then
  while
    1-
  repeat
  ;


\ 
\ 7.3.4.5   Output formatting
\ 

: cr linefeed emit ;
: debug-cr linefeed (emit) ;
: (cr carret emit ;
: space bl emit ;
: spaces 0 ?do space loop ;
variable #line 0 #line !
variable #out  0 #out  !


\ 
\ 7.3.9.2.3 Dictionary search
\ 

\ helper functions

: lfa2name ( lfa -- name len )
  1-                   \ skip flag byte
  begin                \ skip 0 padding 
    1- dup c@ ?dup 
  until
  7f and               \ clear high bit in length

  tuck - swap          ( ptr-to-len len - name len )
  ;

: comp-nocase ( str1 str2 len -- true|false )
  0 do
    2dup i + c@ upc    ( str1 str2 byteX )
    swap i + c@ upc ( str1 str2 byte1 byte2 )
    <> if
      0 leave
    then
  loop
  if -1 else drop 0 then
  swap drop
  ;

: comp-word ( b-str len lfa -- true | false )
  lfa2name        ( str len str len -- )
  >r swap r>      ( str str len len )
  over = if       ( str str len )
    comp-nocase
  else
    drop drop drop false   \ if len does not match, string does not match
  then
;

\ $find is an fcode word, but we place it here since we use it for find.

: find-wordlist ( name-str name-len last -- xt true | name-str name-len false )

  @ >r

  begin
    2dup r@ dup if comp-word dup false = then
  while
    r> @ >r drop
  repeat

  r@ if \ successful?
    -rot 2drop r> cell+ swap
  else
    r> drop drop drop false
  then

  ;

: $find ( name-str name-len -- xt true | name-str name-len false )
  locals-dict 0<> if
    locals-dict-buf @ find-wordlist ?dup if
      exit
    then
  then
  vocabularies? if
    #order @ 0 ?do
      i cells context + @
      find-wordlist
      ?dup if
        unloop exit
      then
    loop
    false
  else
    forth-last find-wordlist
  then
  ;

\ look up a word in the current wordlist
: $find1 ( name-str name-len -- xt true | name-str name-len false )
  vocabularies? if
    current @
  else
    forth-last
  then
  find-wordlist
  ;

  
: '
  parse-word $find 0= if 
    type 3a emit -13 throw
  then
  ;

: ['] 
  parse-word $find 0= if
    type 3a emit -13 throw
  then 
  state @ if
    ['] (lit) , , 
  then
  ; immediate

: find ( pstr -- xt n | pstr false )
  dup count $find           \  pstr xt true | pstr name-str name-len false
  if
    nip true
    over immediate? if
      negate                \ immediate returns 1
    then
  else
    2drop false
  then
  ;


\ 
\ 7.3.9.2.2 Immediate words (part 2)
\ 

: literal ['] (lit) , , ; immediate
: compile, , ; immediate
: compile r> cell+ dup @ , >r ;
: [compile] ['] ' execute , ; immediate

: postpone
  parse-word $find if
    dup immediate? not if
      ['] (lit) , , ['] ,
    then
    ,
  else
    s" undefined word " type type cr
  then
  ; immediate


\ 
\ 7.3.9.2.4 Miscellaneous dictionary (part 2)
\ 

variable #instance

: instance ( -- )
  true #instance !
;

: #instance-base
  my-self dup if @ then
;

: #instance-offs
  my-self dup if na1+ then
;

\ the following instance words are used internally
\ to implement variable instantiation.

: instance-cfa? ( cfa -- true | false )
  b e within                              \ b,c and d are instance defining words
;

: behavior ( xt-defer -- xt )
  dup @ instance-cfa? if
    #instance-base ?dup if
      swap na1+ @ + @
    else
      3 /n* + @
    then
  else
    na1+ @
  then
;

: (ito) ( xt-new xt-defer -- )
  #instance-base ?dup if
    swap na1+ @ + !
  else
    3 /n* + !
  then
;
  
: (to-xt) ( xt -- )  
  dup @ instance-cfa?
  state @ if
    swap ['] (lit) , , if ['] (ito) else ['] (to) then ,
  else
    if (ito) else /n + ! then
  then
;

: to
  ['] ' execute
  (to-xt)
  ; immediate
  
: is ( xt "wordname<>" -- )
  parse-word $find if
    (to)
  else
    s" could not find " type type
  then
  ;

\ 
\ 7.3.4.2 Console Input
\ 

defer key?
defer key

: accept ( addr len -- len2 )
  tuck 0 do
    key
    dup linefeed = if
      space drop drop drop i 0 leave
    then
    dup emit over c! 1 +
  loop
  drop ( cr )
  ;

: expect ( addr len -- )
  accept span !
  ;


\ 
\ 7.3.4.3 ASCII constants (part 2)
\ 

: handle-lit
  state @ if
    2 = if
      ['] (lit) ,  ,
    then
    ['] (lit) ,  ,
  else
    drop
  then
  ;

: char
  parse-word 0<> if c@ else s" Unexpected EOL." type cr then ;
  ;

: ascii  char 1 handle-lit ; immediate
: [char] char 1 handle-lit ; immediate

: control   
  char bl 1- and 1 handle-lit 
; immediate



\ 
\ 7.3.8.6    Error handling (part 2)
\ 

: abort 
  -1 throw
  ;

: abort"
  ['] if execute
  22 parse handle-text 
  ['] type , 
  ['] (lit) , 
  -2 , 
  ['] throw ,
  ['] then execute
  ; compile-only 

\ 
\ 7.5.3.1 Dictionary search
\ 

\ this does not belong here, but its nice for testing

: words ( -- )
  last
  begin @ 
    ?dup while
    dup lfa2name

    \ Don't print spaces for headerless words
    dup if
      type space
    else
      type
    then

  repeat
  cr
  ;

\ 
\ 7.3.5.4 Numeric output primitives
\ 

false value capital-hex?

: pad       ( -- addr )      here 100 + aligned ;

: todigit   ( num -- ascii ) 
  dup 9 > if 
    capital-hex? not if
      20 +
    then
    7 + 
  then 
  30 + 
  ;

: <#   pad dup ! ;
: hold pad dup @ 1- tuck swap ! c! ;
: sign 
  0< if 
    2d hold 
  then 
  ;

: #    base @ mu/mod rot todigit hold ;
: #s   begin # 2dup or 0= until ;
: #>   2drop pad dup @ tuck - ;
: (.)  <# dup >r abs 0 #s r> sign #> ;

: u#   base @ u/mod swap todigit hold ;
: u#s  begin u# dup 0= until ;
: u#> 0 #> ;
: (u.) <# u#s u#> ;

\ 
\ 7.3.5.3 Numeric output
\ 

: .    (.) type space ;
: s.   . ;
: u.   (u.) type space ;
: .r   swap (.) rot 2dup < if over - spaces else drop then type ;
: u.r  swap (u.) rot 2dup < if over - spaces else drop then type ;
: .d   base @ swap decimal . base ! ;
: .h   base @ swap hex . base ! ;

: .s 
  3c emit depth dup (.) type 3e emit space
  0 
  ?do
    depth i - 1- pick .
  loop 
  cr
  ;

\ 
\ 7.3.5.2 Numeric input
\ 

: digit ( char base -- n true | char false )
  swap dup upc dup 
  41 5a ( A - Z ) between if
    7 -
  else
    dup 39 > if \ protect from : and ;
      -rot 2drop false exit
    then
  then
  
  30 ( number 0 ) - rot over swap 0 swap within  if
    nip true
  else
    drop false
  then  
  ;

: >number
   begin 
      dup 
   while
      over c@ base @ digit 0= if    
         drop exit 
      then  >r 2swap r> swap base @ um* drop rot base @ um* d+ 2swap 
      1 /string 
   repeat 
   ;

: numdelim?   
   dup 2e = swap 2c = or 
; 


: $dnumber?   
   0 0 2swap dup 0= if    
      2drop 2drop 0 exit 
   then  over c@ 2d = dup >r negate /string begin 
      >number dup 1 > 
   while
      over c@ numdelim? 0= if    
         2drop 2drop r> drop 0 exit 
      then  1 /string 
   repeat if    
      c@ 2e = if    
         true 
      else
         2drop r> drop 0 exit 
      then  
   else
      drop false 
   then  over or if    
      r> if    
         dnegate 
      then  2 
   else
     drop r> if    
         negate 
      then  1 
   then  
; 


: $number (  )
   $dnumber? 
   case
   0 of   true endof
   1 of   false endof
   2 of   drop false endof
   endcase
; 

: d#
  parse-word
  base @ >r

  decimal

  $number if
    s" illegal number" type cr 0
  then
  r> base !
  1 handle-lit
  ; immediate

: h#
  parse-word
  base @ >r

  hex

  $number if
    s" illegal number" type cr 0
  then
  r> base !
  1 handle-lit
  ; immediate

: o#
  parse-word
  base @ >r

  octal

  $number if
    s" illegal number" type cr 0
  then
  r> base !
  1 handle-lit
  ; immediate


\ 
\ 7.3.4.7 String Literals (part 2)
\ 

: "
  pocket dup
  begin
    span @ >in @ > if
      22 parse >r         ( pocket pocket str  R: len )
      over r@ move        \ copy string
      r> +                ( pocket nextdest )
      ib >in @ + c@       ( pocket nextdest nexchar )
      1 >in +!
      28 =                \ is nextchar a parenthesis?
      span @ >in @ >      \ more input?
      and
    else
      false
    then
  while
    29 parse              \ parse everything up to the next ')'
    bounds ?do
      i c@ 10 digit if
        i 1+ c@ 10 digit if
          swap 4 lshift or
        else
          drop
        then
        over c! 1+
        2
      else
        drop 1
      then
    +loop
  repeat
  over -
  handle-text
; immediate


\ 
\ 7.3.3.1 Memory Access (part 2)
\ 

: dump ( addr len -- )
  over + swap
  cr
  do i u. space
    10 0 do
      j i + c@
      dup 10 / todigit emit
      10 mod todigit emit
      space
      i 7 = if space then
    loop
    3 spaces
    10 0 do
      j i + c@
      dup 20 < if drop 2e then \ non-printables as dots?
      emit
    loop
    cr
  10 +loop
;



\ 
\ 7.3.9.1 Defining words
\ 

: header ( name len -- )
  dup if                            \ might be a noname...
    2dup $find1 if
      drop 2dup type s"  isn't unique." type cr
    else
      2drop
    then
  then
  null-align
  dup -rot ", 80 or c,              \ write name and len
  here /n 1- and 0= if 0 c, then    \ pad and space for flags
  null-align
  80 here 1- c!                     \ write flags byte
  here last @ , latest !            \ write backlink and set latest
 ;


: :
  parse-word header
  1 , ]
  ;

: :noname 
  0 0 header 
  here
  1 , ]
  ;

: ;
  locals-dict 0<> if
    0 ['] locals-dict /n + !
    ['] locals-end , 
  then
  ['] (semis) , reveal ['] [ execute
  ; immediate

: constant
  parse-word header
  3 , ,                             \ compile DOCON and value
  reveal
  ;

0 value active-package
: instance, ( size -- )
  \ first word of the device node holds the instance size
  dup active-package @ dup rot + active-package !
  , ,      \ offset size
;

: instance? ( -- flag )
  #instance @ dup if
    false #instance !
  then
;

: value
  parse-word header
  instance? if
    /n b , instance, ,              \ DOIVAL
  else
    3 , ,
  then
  reveal
  ;

: variable
  parse-word header
  instance? if
    /n c , instance, 0 ,
  else
    4 , 0 ,
  then
  reveal
  ;

: $buffer: ( size str len -- where )
  header
  instance? if
    /n over /n 1- and - /n 1- and +     \ align buffer size
    dup c , instance,                   \ DOIVAR
  else
    4 ,
  then
  here swap
  2dup 0 fill                            \ zerofill
  allot
  reveal
;

: buffer: ( size -- )
  parse-word $buffer: drop
;

: (undefined-defer)  ( -- )
  \ XXX: this does not work with behavior ... execute
  r@ 2 cells - lfa2name
  s" undefined defer word " type type cr ;

: (undefined-idefer)  ( -- )
  s" undefined idefer word " type cr ;

: defer  (  new-name< >  -- )
  parse-word header
  instance? if
    2 /n* d , instance,                 \ DOIDEFER
    ['] (undefined-idefer)
  else
    5 ,
    ['] (undefined-defer)
  then
  ,
  ['] (semis) ,
  reveal
  ;

: alias  (  new-name< >old-name< >  -- )
  parse-word
  parse-word $find if
    -rot                     \ move xt behind.
    header
    1 ,                      \ fixme we want our own cfa here.
    ,                        \ compile old name xt
    ['] (semis) ,
    reveal
  else
    s" undefined word " type type space
    2drop
  then
  ;

: $create
  header 6 ,
  ['] noop ,
  reveal
  ;

: create
  parse-word $create
  ;

: (does>)
  r> cell+              \ get address of code to execute
  latest @              \ backlink of just "create"d word
  cell+ cell+ !         \ write code to execute after the
                        \ new word's CFA
  ;

: does>
  ['] (does>) ,         \ compile does handling
  1 ,                   \ compile docol
  ; immediate

0 constant struct

: field
  create
    over ,
    +
  does>
    @ +
  ;

: 2constant
  create , ,
  does> 2@ reveal
  ;

\ 
\ initializer for the temporary compile buffer
\ 

: init-tmp-comp
  here 200 allot tmp-comp-buf !
;

\ the end