-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprim
3483 lines (3010 loc) · 81.6 KB
/
prim
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
\ Gforth primitives
\ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Free Software Foundation, Inc.
\ This file is part of Gforth.
\ Gforth 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 3
\ 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, see http://www.gnu.org/licenses/.
\ WARNING: This file is processed by m4. Make sure your identifiers
\ don't collide with m4's (e.g. by undefining them).
\
\
\
\ This file contains primitive specifications in the following format:
\
\ forth name ( stack effect ) category [pronunciation]
\ [""glossary entry""]
\ C code
\ [:
\ Forth code]
\
\ Note: Fields in brackets are optional. Word specifications have to
\ be separated by at least one empty line
\
\ Both pronounciation and stack items (in the stack effect) must
\ conform to the C identifier syntax or the C compiler will complain.
\ If you don't have a pronounciation field, the Forth name is used,
\ and has to conform to the C identifier syntax.
\
\ These specifications are automatically translated into C-code for the
\ interpreter and into some other files. I hope that your C compiler has
\ decent optimization, otherwise the automatically generated code will
\ be somewhat slow. The Forth version of the code is included for manual
\ compilers, so they will need to compile only the important words.
\
\ Note that stack pointer adjustment is performed according to stack
\ effect by automatically generated code and NEXT is automatically
\ appended to the C code. Also, you can use the names in the stack
\ effect in the C code. Stack access is automatic. One exception: if
\ your code does not fall through, the results are not stored into the
\ stack. Use different names on both sides of the '--', if you change a
\ value (some stores to the stack are optimized away).
\
\ For superinstructions the syntax is:
\
\ forth-name [/ c-name] = forth-name forth-name ...
\
\
\ The stack variables have the following types:
\
\ name matches type
\ f.* Bool
\ c.* Char
\ [nw].* Cell
\ u.* UCell
\ d.* DCell
\ ud.* UDCell
\ r.* Float
\ a_.* Cell *
\ c_.* Char *
\ f_.* Float *
\ df_.* DFloat *
\ sf_.* SFloat *
\ xt.* XT
\ f83name.* F83Name *
\E stack data-stack sp Cell
\E stack fp-stack fp Float
\E stack return-stack rp Cell
\E
\E get-current prefixes set-current
\E
\E s" Bool" single data-stack type-prefix f
\E s" Char" single data-stack type-prefix c
\E s" Cell" single data-stack type-prefix n
\E s" Cell" single data-stack type-prefix w
\E s" UCell" single data-stack type-prefix u
\E s" DCell" double data-stack type-prefix d
\E s" UDCell" double data-stack type-prefix ud
\E s" Float" single fp-stack type-prefix r
\E s" Cell *" single data-stack type-prefix a_
\E s" Char *" single data-stack type-prefix c_
\E s" Float *" single data-stack type-prefix f_
\E s" DFloat *" single data-stack type-prefix df_
\E s" SFloat *" single data-stack type-prefix sf_
\E s" Xt" single data-stack type-prefix xt
\E s" struct F83Name *" single data-stack type-prefix f83name
\E s" struct Longname *" single data-stack type-prefix longname
\E
\E data-stack stack-prefix S:
\E fp-stack stack-prefix F:
\E return-stack stack-prefix R:
\E inst-stream stack-prefix #
\E
\E set-current
\E store-optimization on
\E ' noop tail-nextp2 ! \ now INST_TAIL just stores, but does not jump
\E
\E `include-skipped-insts' on \ static superinsts include cells for components
\E \ useful for dynamic programming and
\E \ superinsts across entry points
\
\
\
\ In addition the following names can be used:
\ ip the instruction pointer
\ sp the data stack pointer
\ rp the parameter stack pointer
\ lp the locals stack pointer
\ NEXT executes NEXT
\ cfa
\ NEXT1 executes NEXT1
\ FLAG(x) makes a Forth flag from a C flag
\
\
\
\ Percentages in comments are from Koopmans book: average/maximum use
\ (taken from four, not very representative benchmarks)
\
\
\
\ To do:
\
\ throw execute, cfa and NEXT1 out?
\ macroize *ip, ip++, *ip++ (pipelining)?
\ Stack caching setup
ifdef(`STACK_CACHE_FILE', `include(STACK_CACHE_FILE)', `include(cache0.vmg)')
\ these m4 macros would collide with identifiers
undefine(`index')
undefine(`shift')
undefine(`symbols')
\F 0 [if]
\ run-time routines for non-primitives. They are defined as
\ primitives, because that simplifies things.
(docol) ( -- R:a_retaddr ) gforth-internal paren_docol
""run-time routine for colon definitions""
#ifdef DEBUG
{
CFA_TO_NAME(CFA);
debugp(stderr,"%08lx: call %08lx %.*s\n",(Cell)ip,(Cell)CFA,
len,name);
}
#endif
#ifdef NO_IP
a_retaddr = next_code;
INST_TAIL;
JUMP(PFA(CFA));
#else /* !defined(NO_IP) */
a_retaddr = (Cell *)IP;
SET_IP((Xt *)PFA(CFA));
#endif /* !defined(NO_IP) */
(docon) ( -- w ) gforth-internal paren_docon
""run-time routine for constants""
w = *(Cell *)PFA(CFA);
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(dovar) ( -- a_body ) gforth-internal paren_dovar
""run-time routine for variables and CREATEd words""
a_body = PFA(CFA);
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(douser) ( -- a_user ) gforth-internal paren_douser
""run-time routine for constants""
a_user = (Cell *)(((Address)up)+*(Cell *)PFA(CFA));
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(dodefer) ( -- ) gforth-internal paren_dodefer
""run-time routine for deferred words""
#ifndef NO_IP
ip=IP; /* undo any ip updating that may have been performed by NEXT_P0 */
#endif /* !defined(NO_IP) */
SUPER_END; /* !! probably unnecessary and may lead to measurement errors */
VM_JUMP(EXEC1(*(Xt *)PFA(CFA)));
(dofield) ( n1 -- n2 ) gforth-internal paren_field
""run-time routine for fields""
n2 = n1 + *(Cell *)PFA(CFA);
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(dovalue) ( -- w ) gforth-internal paren_doval
""run-time routine for constants""
w = *(Cell *)PFA(CFA);
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(dodoes) ( -- a_body R:a_retaddr ) gforth-internal paren_dodoes
""run-time routine for @code{does>}-defined words""
#ifdef NO_IP
a_retaddr = next_code;
a_body = PFA(CFA);
INST_TAIL;
#ifdef DEBUG
debugp(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body);
#endif
goto **(Label *)DOES_CODE1(CFA);
#else /* !defined(NO_IP) */
a_retaddr = (Cell *)IP;
a_body = PFA(CFA);
#ifdef DEBUG
debugp(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body);
#endif
SET_IP(DOES_CODE1(CFA));
#endif /* !defined(NO_IP) */
(doabicode) ( ... -- ...) gforth-internal paren_doabicode
""run-time routine for @code{ABI-code} definitions""
abifunc *f = (abifunc *)PFA(CFA);
Float *fp_mem = fp;
sp = (*f)(sp, &fp_mem);
fp = fp_mem;
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(do;abicode) ( ... -- ... ) gforth-internal paren_do_semicolon_abi_code
""run-time routine for @code{;abi-code}-defined words""
Float *fp_mem = fp;
Address body = (Address)PFA(CFA);
semiabifunc *f = (semiabifunc *)EXTRA_CODE(CFA);
sp = (*f)(sp, &fp_mem, body);
fp = fp_mem;
#ifdef NO_IP
INST_TAIL;
goto *next_code;
#endif /* defined(NO_IP) */
(doextra) ( -- a_body R:a_retaddr ) gforth-internal paren_doextra
""run-time routine for @code{does>}-defined words""
#ifdef NO_IP
a_retaddr = next_code;
a_body = PFA(CFA);
INST_TAIL;
#ifdef DEBUG
debugp(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body);
#endif
goto **(Label *)EXTRA_CODE(CFA);
#else /* !defined(NO_IP) */
a_retaddr = (Cell *)IP;
a_body = PFA(CFA);
#ifdef DEBUG
debugp(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body);
#endif
SET_IP(EXTRA_CODE(CFA));
#endif /* !defined(NO_IP) */
(dodoesxt) ( -- a_body ) gforth-internal paren_dodoesxt
a_body = PFA(CFA);
#ifndef NO_IP
ip=IP; /* undo any ip updating that may have been performed by NEXT_P0 */
#endif /* !defined(NO_IP) */
SUPER_END; /* !! probably unnecessary and may lead to measurement errors */
VM_JUMP(EXEC1((Xt)DOES_CODEXT(CFA)));
\F [endif]
\g control
noop ( -- ) gforth
:
;
call ( #a_callee -- R:a_retaddr ) new
""Call callee (a variant of docol with inline argument).""
#ifdef NO_IP
assert(0);
INST_TAIL;
JUMP(a_callee);
#else
#ifdef DEBUG
{
CFA_TO_NAME((((Cell *)a_callee)-2));
debugp(stderr,"%08lx: call %08lx %.*s\n",(Cell)ip,(Cell)a_callee,
len,name);
}
#endif
a_retaddr = (Cell *)IP;
SET_IP((Xt *)a_callee);
#endif
execute ( xt -- ) core
""Perform the semantics represented by the execution token, @i{xt}.""
#ifdef DEBUG
debugp(stderr, "execute %08x\n", xt);
#endif
#ifndef NO_IP
ip=IP;
#endif
SUPER_END;
VM_JUMP(EXEC1(xt));
perform ( a_addr -- ) gforth
""@code{@@ execute}.""
/* and pfe */
#ifndef NO_IP
ip=IP;
#endif
SUPER_END;
VM_JUMP(EXEC1(*(Xt *)a_addr));
:
@ execute ;
;s ( R:w -- ) gforth semis
""The primitive compiled by @code{EXIT}.""
#ifdef NO_IP
INST_TAIL;
goto *(void *)w;
#else
SET_IP((Xt *)w);
#endif
unloop ( R:w1 R:w2 -- ) core
/* !! alias for 2rdrop */
:
r> rdrop rdrop >r ;
lit-perform ( #a_addr -- ) new lit_perform
#ifndef NO_IP
ip=IP;
#endif
SUPER_END;
VM_JUMP(EXEC1(*(Xt *)a_addr));
does-exec ( #a_cfa -- R:a_retaddr a_body ) new does_exec
#ifdef NO_IP
/* compiled to LIT CALL by compile_prim */
assert(0);
#else
a_body = PFA(a_cfa);
a_retaddr = (Cell *)IP;
#ifdef DEBUG
{
CFA_TO_NAME(a_cfa);
debugp(stderr,"%08lx: does %08lx %.*s\n",
(Cell)ip,(Cell)a_cfa,len,name);
}
#endif
SET_IP(DOES_CODE1(a_cfa));
#endif
extra-exec ( #a_cfa -- R:a_retaddr a_body ) new extra_exec
#ifdef NO_IP
/* compiled to LIT CALL by compile_prim */
assert(0);
#else
a_body = PFA(a_cfa);
a_retaddr = (Cell *)IP;
#ifdef DEBUG
{
CFA_TO_NAME(a_cfa);
debugp(stderr,"%08lx: does %08lx %.*s\n",
(Cell)ip,(Cell)a_cfa,len,name);
}
#endif
SET_IP(EXTRA_CODE(a_cfa));
#endif
\+glocals
branch-lp+!# ( #a_target #nlocals -- ) gforth branch_lp_plus_store_number
/* this will probably not be used */
lp += nlocals;
#ifdef NO_IP
INST_TAIL;
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
\+
branch ( #a_target -- ) gforth
#ifdef NO_IP
INST_TAIL;
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
:
r> @ >r ;
\ condbranch(forthname,stackeffect,restline,code1,code2,forthcode)
\ this is non-syntactical: code must open a brace that is closed by the macro
define(condbranch,
$1 ( `#'a_target $2 ) $3
$4 #ifdef NO_IP
INST_TAIL;
#endif
$5 #ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
ifelse(condbranch_opt,`1',`INST_TAIL; NEXT_P2;',`/* condbranch_opt=0 */')
#endif
}
ifelse(condbranch_opt,`1',`SUPER_CONTINUE;',`/* condbranch_opt=0 */')
$6
\+glocals
$1-lp+!`#' ( `#'a_target `#'nlocals $2 ) $3_lp_plus_store_number
$4 #ifdef NO_IP
INST_TAIL;
#endif
$5 lp += nlocals;
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
ifelse(condbranch_opt,`1',`INST_TAIL; NEXT_P2;',`/* condbranch_opt=0 */')
#endif
}
ifelse(condbranch_opt,`1',`SUPER_CONTINUE;',`/* condbranch_opt=0 */')
\+
)
condbranch(?branch,f --,f83 question_branch,
,if (f==0) {
,:
0= dup 0= \ !f f
r> tuck cell+ \ !f branchoffset f IP+
and -rot @ and or \ f&IP+|!f&branch
>r ;)
\ we don't need an lp_plus_store version of the ?dup-stuff, because it
\ is only used in if's (yet)
\+xconds
?dup-?branch ( #a_target f -- S:... ) new question_dupe_question_branch
""The run-time procedure compiled by @code{?DUP-IF}.""
if (f==0) {
#ifdef NO_IP
INST_TAIL;
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
} else {
sp--;
sp[0]=f;
}
?dup-0=-?branch ( #a_target f -- S:... ) new question_dupe_zero_equals_question_branch
""The run-time procedure compiled by @code{?DUP-0=-IF}.""
if (f!=0) {
sp--;
sp[0]=f;
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
}
\+
\fhas? skiploopprims 0= [IF]
condbranch((next),R:n1 -- R:n2,cmFORTH paren_next,
n2=n1-1;
,if (n1) {
,:
r> r> dup 1- >r
IF @ >r ELSE cell+ >r THEN ;)
condbranch((loop),R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_loop,
n2=n1+1;
,if (n2 != nlimit) {
,:
r> r> 1+ r> 2dup =
IF >r 1- >r cell+ >r
ELSE >r >r @ >r THEN ;)
condbranch((+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_plus_loop,
/* !! check this thoroughly */
/* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
/* dependent upon two's complement arithmetic */
Cell olddiff = n1-nlimit;
n2=n1+n;
,if (((olddiff^(olddiff+n)) /* the limit is not crossed */
&(olddiff^n)) /* OR it is a wrap-around effect */
>=0) { /* & is used to avoid having two branches for gforth-native */
,:
r> swap
r> r> 2dup - >r
2 pick r@ + r@ xor 0< 0=
3 pick r> xor 0< 0= or
IF >r + >r @ >r
ELSE >r >r drop cell+ >r THEN ;)
\+xconds
condbranch((-loop),u R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_minus_loop,
UCell olddiff = n1-nlimit;
n2=n1-u;
,if (olddiff>u) {
,)
condbranch((s+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_symmetric_plus_loop,
""The run-time procedure compiled by S+LOOP. It loops until the index
crosses the boundary between limit and limit-sign(n). I.e. a symmetric
version of (+LOOP).""
/* !! check this thoroughly */
Cell diff = n1-nlimit;
Cell newdiff = diff+n;
if (n<0) {
diff = -diff;
newdiff = -newdiff;
}
n2=n1+n;
,if (((~diff)|newdiff)<0) { /* use | to avoid two branches for gforth-native */
,)
\+
(for) ( ncount -- R:nlimit R:ncount ) cmFORTH paren_for
/* or (for) = >r -- collides with unloop! */
nlimit=0;
:
r> swap 0 >r >r >r ;
(do) ( nlimit nstart -- R:nlimit R:nstart ) gforth paren_do
:
r> swap rot >r >r >r ;
(?do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth paren_question_do
#ifdef NO_IP
INST_TAIL;
#endif
if (nstart == nlimit) {
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
}
:
2dup =
IF r> swap rot >r >r
@ >r
ELSE r> swap rot >r >r
cell+ >r
THEN ; \ --> CORE-EXT
\+xconds
(+do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth paren_plus_do
#ifdef NO_IP
INST_TAIL;
#endif
if (nstart >= nlimit) {
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
}
:
swap 2dup
r> swap >r swap >r
>=
IF
@
ELSE
cell+
THEN >r ;
(u+do) ( #a_target ulimit ustart -- R:ulimit R:ustart ) gforth paren_u_plus_do
#ifdef NO_IP
INST_TAIL;
#endif
if (ustart >= ulimit) {
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
}
:
swap 2dup
r> swap >r swap >r
u>=
IF
@
ELSE
cell+
THEN >r ;
(-do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth paren_minus_do
#ifdef NO_IP
INST_TAIL;
#endif
if (nstart <= nlimit) {
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
}
:
swap 2dup
r> swap >r swap >r
<=
IF
@
ELSE
cell+
THEN >r ;
(u-do) ( #a_target ulimit ustart -- R:ulimit R:ustart ) gforth paren_u_minus_do
#ifdef NO_IP
INST_TAIL;
#endif
if (ustart <= ulimit) {
#ifdef NO_IP
JUMP(a_target);
#else
SET_IP((Xt *)a_target);
#endif
}
:
swap 2dup
r> swap >r swap >r
u<=
IF
@
ELSE
cell+
THEN >r ;
(try1) ( ... a_oldhandler a_recovery -- R:a_recovery R:a_sp R:c_op R:f_fp R:c_lp R:a_oldhandler a_newhandler ) gforth paren_try1
a_sp = sp-1;
c_op = op;
f_fp = fp;
c_lp = lp;
a_newhandler = rp-6;
(throw1) ( ... wball a_handler -- ... wball ) gforth paren_throw1
rp = a_handler;
lp = (Address)rp[1];
fp = (Float *)rp[2];
op = (Char *)rp[3];
sp = (Cell *)rp[4];
#ifndef NO_IP
ip=IP;
#endif
SUPER_END;
VM_JUMP(EXEC1(*(Xt *)rp[5]));
\+
\ don't make any assumptions where the return stack is!!
\ implement this in machine code if it should run quickly!
i ( R:n -- R:n n ) core
:
\ rp@ cell+ @ ;
r> r> tuck >r >r ;
i' ( R:w R:w2 -- R:w R:w2 w ) gforth i_tick
:
\ rp@ cell+ cell+ @ ;
r> r> r> dup itmp ! >r >r >r itmp @ ;
variable itmp
j ( R:w R:w1 R:w2 -- w R:w R:w1 R:w2 ) core
:
\ rp@ cell+ cell+ cell+ @ ;
r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
[IFUNDEF] itmp variable itmp [THEN]
k ( R:w R:w1 R:w2 R:w3 R:w4 -- w R:w R:w1 R:w2 R:w3 R:w4 ) gforth
:
\ rp@ [ 5 cells ] Literal + @ ;
r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
[IFUNDEF] itmp variable itmp [THEN]
\f[THEN]
\ digit is high-level: 0/0%
\g strings
move ( c_from c_to ucount -- ) core
""Copy the contents of @i{ucount} aus at @i{c-from} to
@i{c-to}. @code{move} works correctly even if the two areas overlap.""
/* !! note that the standard specifies addr, not c-addr */
memmove(c_to,c_from,ucount);
/* make an Ifdef for bsd and others? */
:
>r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
cmove ( c_from c_to u -- ) string c_move
""Copy the contents of @i{ucount} characters from data space at
@i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
from low address to high address; i.e., for overlapping areas it is
safe if @i{c-to}<=@i{c-from}.""
cmove(c_from,c_to,u);
:
bounds ?DO dup c@ I c! 1+ LOOP drop ;
cmove> ( c_from c_to u -- ) string c_move_up
""Copy the contents of @i{ucount} characters from data space at
@i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
from high address to low address; i.e., for overlapping areas it is
safe if @i{c-to}>=@i{c-from}.""
cmove_up(c_from,c_to,u);
:
dup 0= IF drop 2drop exit THEN
rot over + -rot bounds swap 1-
DO 1- dup c@ I c! -1 +LOOP drop ;
fill ( c_addr u c -- ) core
""Store @i{c} in @i{u} chars starting at @i{c-addr}.""
memset(c_addr,c,u);
:
-rot bounds
?DO dup I c! LOOP drop ;
compare ( c_addr1 u1 c_addr2 u2 -- n ) string
""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
is 1. Currently this is based on the machine's character
comparison. In the future, this may change to consider the current
locale and its collation order.""
/* close ' to keep fontify happy */
n = compare(c_addr1, u1, c_addr2, u2);
:
rot 2dup swap - >r min swap -text dup
IF rdrop ELSE drop r> sgn THEN ;
: -text ( c_addr1 u c_addr2 -- n )
swap bounds
?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
ELSE c@ I c@ - unloop THEN sgn ;
: sgn ( n -- -1/0/1 )
dup 0= IF EXIT THEN 0< 2* 1+ ;
\ -text is only used by replaced primitives now; move it elsewhere
\ -text ( c_addr1 u c_addr2 -- n ) new dash_text
\ n = memcmp(c_addr1, c_addr2, u);
\ if (n<0)
\ n = -1;
\ else if (n>0)
\ n = 1;
\ :
\ swap bounds
\ ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
\ ELSE c@ I c@ - unloop THEN sgn ;
\ : sgn ( n -- -1/0/1 )
\ dup 0= IF EXIT THEN 0< 2* 1+ ;
toupper ( c1 -- c2 ) gforth
""If @i{c1} is a lower-case character (in the current locale), @i{c2}
is the equivalent upper-case character. All other characters are unchanged.""
c2 = toupper(c1);
:
dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
capscompare ( c_addr1 u1 c_addr2 u2 -- n ) gforth
""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
is 1. Currently this is based on the machine's character
comparison. In the future, this may change to consider the current
locale and its collation order.""
/* close ' to keep fontify happy */
n = capscompare(c_addr1, u1, c_addr2, u2);
/string ( c_addr1 u1 n -- c_addr2 u2 ) string slash_string
""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
characters from the start of the string.""
c_addr2 = c_addr1+n;
u2 = u1-n;
:
tuck - >r + r> dup 0< IF - 0 THEN ;
safe/string ( c_addr1 u1 n -- c_addr2 u2 ) string safe_slash_string
""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
characters from the start of the string. Clamps on overflow""
c_addr2 = c_addr1+n;
u2 = u1-n;
if(n>=0) {
if(u2>u1) { c_addr2 += u2; u2 = 0; }
} else {
if(u2<u1) { c_addr2 += u2+1; u2 = -1; }
}
:
tuck - >r + r> dup 0< IF - 0 THEN ;
\g arith
lit ( #w -- w ) gforth
:
r> dup @ swap cell+ >r ;
+ ( n1 n2 -- n ) core plus
n = n1+n2;
\ lit+ / lit_plus = lit +
lit+ ( n1 #n2 -- n ) new lit_plus
#ifdef DEBUG
debugp(stderr, "lit+ %08x\n", n2);
#endif
n=n1+n2;
\ PFE-0.9.14 has it differently, but the next release will have it as follows
under+ ( n1 n2 n3 -- n n2 ) gforth under_plus
""add @i{n3} to @i{n1} (giving @i{n})""
n = n1+n3;
:
rot + swap ;
- ( n1 n2 -- n ) core minus
n = n1-n2;
:
negate + ;
negate ( n1 -- n2 ) core
/* use minus as alias */
n2 = -n1;
:
invert 1+ ;
1+ ( n1 -- n2 ) core one_plus
n2 = n1+1;
:
1 + ;
1- ( n1 -- n2 ) core one_minus
n2 = n1-1;
:
1 - ;
max ( n1 n2 -- n ) core
if (n1<n2)
n = n2;
else
n = n1;
:
2dup < IF swap THEN drop ;
min ( n1 n2 -- n ) core
if (n1<n2)
n = n1;
else
n = n2;
:
2dup > IF swap THEN drop ;
abs ( n -- u ) core
if (n<0)
u = -n;
else
u = n;
:
dup 0< IF negate THEN ;
* ( n1 n2 -- n ) core star
n = n1*n2;
:
um* drop ;
/ ( n1 n2 -- n ) core slash
n = n1/n2;
if (FLOORED_DIV) {
Cell correct = -((Cell)((n1^n2) < 0) & (Cell)(n1%n2 != 0));
n += correct;
}
if (CHECK_DIVISION_SW && n2 == 0)
throw(BALL_DIVZERO);
if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN)
throw(BALL_RESULTRANGE);
:
/mod nip ;
mod ( n1 n2 -- n ) core
n = n1%n2;
if (FLOORED_DIV) {
Cell correct = -((Cell)((n1^n2) < 0) & (Cell)(n != 0));
n += correct & n2;
}
if (CHECK_DIVISION_SW && n2 == 0)
throw(BALL_DIVZERO);
if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN)
throw(BALL_RESULTRANGE);
:
/mod drop ;
/mod ( n1 n2 -- n3 n4 ) core slash_mod
n4 = n1/n2;
n3 = n1%n2; /* !! is this correct? look into C standard! */
if (FLOORED_DIV) {
Cell correct = -((Cell)((n1^n2) < 0) & (Cell)(n3!=0));
n4 += correct;
n3 += correct & n2;
}
if (CHECK_DIVISION_SW && n2 == 0)
throw(BALL_DIVZERO);
if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN)
throw(BALL_RESULTRANGE);
:
>r s>d r> fm/mod ;
*/mod ( n1 n2 n3 -- n4 n5 ) core star_slash_mod
""n1*n2=n3*n5+n4, with the intermediate result (n1*n2) being double.""
#ifdef BUGGY_LL_MUL
DCell d = mmul(n1,n2);
#else
DCell d = (DCell)n1 * (DCell)n2;
#endif
#ifdef ASM_SM_SLASH_REM
ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, n4, n5);
if (FLOORED_DIV) {
Cell correct=-((Cell)((DHI(d)^n3)<0) & (Cell)(n4!=0));
if (CHECK_DIVISION && correct && n5 == CELL_MIN)
throw(BALL_RESULTRANGE);
n5 += correct;
n4 += correct & n3;
}
#else
DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3);
n4=DHI(r);
n5=DLO(r);
#endif
:
>r m* r> fm/mod ;
*/ ( n1 n2 n3 -- n4 ) core star_slash
""n4=(n1*n2)/n3, with the intermediate result being double.""
#ifdef BUGGY_LL_MUL
DCell d = mmul(n1,n2);
#else
DCell d = (DCell)n1 * (DCell)n2;
#endif
#ifdef ASM_SM_SLASH_REM
Cell remainder;
ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, remainder, n4);
if (FLOORED_DIV) {
Cell correct = -(((DHI(d)^n3)<0) & (remainder!=0));
if (CHECK_DIVISION && correct && n4 == CELL_MIN)
throw(BALL_RESULTRANGE);
n4 += correct;
}
#else
DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3);
n4=DLO(r);
#endif
:
*/mod nip ;
2* ( n1 -- n2 ) core two_star
""Shift left by 1; also works on unsigned numbers""
n2 = 2*n1;
:
dup + ;
2/ ( n1 -- n2 ) core two_slash
""Arithmetic shift right by 1. For signed numbers this is a floored
division by 2 (note that @code{/} not necessarily floors).""
n2 = n1>>1;
:
dup MINI and IF 1 ELSE 0 THEN
[ bits/char cell * 1- ] literal
0 DO 2* swap dup 2* >r MINI and