1: \ Gforth primitives
2:
3: \ Copyright (C) 1995,1996 Free Software Foundation, Inc.
4:
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: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21:
22: \ WARNING: This file is processed by m4. Make sure your identifiers
23: \ don't collide with m4's (e.g. by undefining them).
24: \
25: \
26: \
27: \ This file contains primitive specifications in the following format:
28: \
29: \ forth name stack effect category [pronunciation]
30: \ [""glossary entry""]
31: \ C code
32: \ [:
33: \ Forth code]
34: \
35: \ prims2x is pedantic about tabs vs. blanks. The fields of the first
36: \ line of a primitive are separated by tabs, the stack items in a
37: \ stack effect by blanks.
38: \
39: \ Both pronounciation and stack items (in the stack effect) must
40: \ conform to the C name syntax or the C compiler will complain.
41: \
42: \
43: \ These specifications are automatically translated into C-code for the
44: \ interpreter and into some other files. I hope that your C compiler has
45: \ decent optimization, otherwise the automatically generated code will
46: \ be somewhat slow. The Forth version of the code is included for manual
47: \ compilers, so they will need to compile only the important words.
48: \
49: \ Note that stack pointer adjustment is performed according to stack
50: \ effect by automatically generated code and NEXT is automatically
51: \ appended to the C code. Also, you can use the names in the stack
52: \ effect in the C code. Stack access is automatic. One exception: if
53: \ your code does not fall through, the results are not stored into the
54: \ stack. Use different names on both sides of the '--', if you change a
55: \ value (some stores to the stack are optimized away).
56: \
57: \
58: \
59: \ The stack variables have the following types:
60: \
61: \ name matches type
62: \ f.* Bool
63: \ c.* Char
64: \ [nw].* Cell
65: \ u.* UCell
66: \ d.* DCell
67: \ ud.* UDCell
68: \ r.* Float
69: \ a_.* Cell *
70: \ c_.* Char *
71: \ f_.* Float *
72: \ df_.* DFloat *
73: \ sf_.* SFloat *
74: \ xt.* XT
75: \ wid.* WID
76: \ f83name.* F83Name *
77: \
78: \
79: \
80: \ In addition the following names can be used:
81: \ ip the instruction pointer
82: \ sp the data stack pointer
83: \ rp the parameter stack pointer
84: \ lp the locals stack pointer
85: \ NEXT executes NEXT
86: \ cfa
87: \ NEXT1 executes NEXT1
88: \ FLAG(x) makes a Forth flag from a C flag
89: \
90: \
91: \
92: \ Percentages in comments are from Koopmans book: average/maximum use
93: \ (taken from four, not very representative benchmarks)
94: \
95: \
96: \
97: \ To do:
98: \
99: \ throw execute, cfa and NEXT1 out?
100: \ macroize *ip, ip++, *ip++ (pipelining)?
101:
102: \ these m4 macros would collide with identifiers
103: undefine(`index')
104: undefine(`shift')
105:
106: noop -- gforth
107: ;
108: :
109: ;
110:
111: lit -- w gforth
112: w = (Cell)NEXT_INST;
113: INC_IP(1);
114: :
115: r> dup @ swap cell+ >r ;
116:
117: execute xt -- core
118: ip=IP;
119: IF_TOS(TOS = sp[0]);
120: EXEC(xt);
121:
122: perform a_addr -- gforth
123: ""equivalent to @code{@ execute}""
124: /* and pfe */
125: ip=IP;
126: IF_TOS(TOS = sp[0]);
127: EXEC(*(Xt *)a_addr);
128: :
129: @ execute ;
130:
131: \+has-locals [IF]
132:
133: branch-lp+!# -- gforth branch_lp_plus_store_number
134: /* this will probably not be used */
135: branch_adjust_lp:
136: lp += (Cell)(IP[1]);
137: goto branch;
138:
139: \+[THEN]
140:
141: branch -- gforth
142: branch:
143: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
144: NEXT_P0;
145: :
146: r> dup @ + >r ;
147:
148: \ condbranch(forthname,restline,code,forthcode)
149: \ this is non-syntactical: code must open a brace that is closed by the macro
150: define(condbranch,
151: $1 $2
152: $3 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
153: NEXT_P0;
154: NEXT;
155: }
156: else
157: INC_IP(1);
158: $4
159:
160: \+has-locals [IF]
161:
162: $1-lp+!# $2_lp_plus_store_number
163: $3 goto branch_adjust_lp;
164: }
165: else
166: INC_IP(2);
167:
168: \+[THEN]
169: )
170:
171: condbranch(?branch,f -- f83 question_branch,
172: if (f==0) {
173: IF_TOS(TOS = sp[0]);
174: ,)
175:
176: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
177: \ is only used in if's (yet)
178:
179: \+has-xconds [IF]
180:
181: ?dup-?branch f -- f new question_dupe_question_branch
182: ""The run-time procedure compiled by @code{?DUP-IF}.""
183: if (f==0) {
184: sp++;
185: IF_TOS(TOS = sp[0]);
186: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
187: NEXT_P0;
188: NEXT;
189: }
190: else
191: INC_IP(1);
192:
193: ?dup-0=-?branch f -- new question_dupe_zero_equals_question_branch
194: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
195: /* the approach taken here of declaring the word as having the stack
196: effect ( f -- ) and correcting for it in the branch-taken case costs a
197: few cycles in that case, but is easy to convert to a CONDBRANCH
198: invocation */
199: if (f!=0) {
200: sp--;
201: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
202: NEXT_P0;
203: NEXT;
204: }
205: else
206: INC_IP(1);
207:
208: \+[THEN]
209:
210: condbranch((next),-- cmFORTH paren_next,
211: if ((*rp)--) {
212: ,:
213: r> r> dup 1- >r
214: IF dup @ + >r ELSE cell+ >r THEN ;)
215:
216: condbranch((loop),-- gforth paren_loop,
217: Cell index = *rp+1;
218: Cell limit = rp[1];
219: if (index != limit) {
220: *rp = index;
221: ,:
222: r> r> 1+ r> 2dup =
223: IF >r 1- >r cell+ >r
224: ELSE >r >r dup @ + >r THEN ;)
225:
226: condbranch((+loop),n -- gforth paren_plus_loop,
227: /* !! check this thoroughly */
228: Cell index = *rp;
229: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
230: /* dependent upon two's complement arithmetic */
231: Cell olddiff = index-rp[1];
232: if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
233: || (olddiff^n)>=0 /* it is a wrap-around effect */) {
234: #ifdef i386
235: *rp += n;
236: #else
237: *rp = index + n;
238: #endif
239: IF_TOS(TOS = sp[0]);
240: ,:
241: r> swap
242: r> r> 2dup - >r
243: 2 pick r@ + r@ xor 0< 0=
244: 3 pick r> xor 0< 0= or
245: IF >r + >r dup @ + >r
246: ELSE >r >r drop cell+ >r THEN ;)
247:
248: \+has-xconds [IF]
249:
250: condbranch((-loop),u -- gforth paren_minus_loop,
251: /* !! check this thoroughly */
252: Cell index = *rp;
253: UCell olddiff = index-rp[1];
254: if (olddiff>u) {
255: #ifdef i386
256: *rp -= u;
257: #else
258: *rp = index - u;
259: #endif
260: IF_TOS(TOS = sp[0]);
261: ,)
262:
263: condbranch((s+loop),n -- gforth paren_symmetric_plus_loop,
264: ""The run-time procedure compiled by S+LOOP. It loops until the index
265: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
266: version of (+LOOP).""
267: /* !! check this thoroughly */
268: Cell index = *rp;
269: Cell diff = index-rp[1];
270: Cell newdiff = diff+n;
271: if (n<0) {
272: diff = -diff;
273: newdiff = -newdiff;
274: }
275: if (diff>=0 || newdiff<0) {
276: #ifdef i386
277: *rp += n;
278: #else
279: *rp = index + n;
280: #endif
281: IF_TOS(TOS = sp[0]);
282: ,)
283:
284: \+[THEN]
285:
286: unloop -- core
287: rp += 2;
288: :
289: r> rdrop rdrop >r ;
290:
291: (for) ncount -- cmFORTH paren_for
292: /* or (for) = >r -- collides with unloop! */
293: *--rp = 0;
294: *--rp = ncount;
295: :
296: r> swap 0 >r >r >r ;
297:
298: (do) nlimit nstart -- gforth paren_do
299: /* or do it in high-level? 0.09/0.23% */
300: *--rp = nlimit;
301: *--rp = nstart;
302: :
303: r> swap rot >r >r >r ;
304:
305: (?do) nlimit nstart -- gforth paren_question_do
306: *--rp = nlimit;
307: *--rp = nstart;
308: if (nstart == nlimit) {
309: IF_TOS(TOS = sp[0]);
310: goto branch;
311: }
312: else {
313: INC_IP(1);
314: }
315: :
316: 2dup =
317: IF r> swap rot >r >r
318: dup @ + >r
319: ELSE r> swap rot >r >r
320: cell+ >r
321: THEN ; \ --> CORE-EXT
322:
323: \+has-xconds [IF]
324:
325: (+do) nlimit nstart -- gforth paren_plus_do
326: *--rp = nlimit;
327: *--rp = nstart;
328: if (nstart >= nlimit) {
329: IF_TOS(TOS = sp[0]);
330: goto branch;
331: }
332: else {
333: INC_IP(1);
334: }
335: :
336: swap 2dup
337: r> swap >r swap >r
338: >=
339: IF
340: dup @ +
341: ELSE
342: cell+
343: THEN >r ;
344:
345: (u+do) ulimit ustart -- gforth paren_u_plus_do
346: *--rp = ulimit;
347: *--rp = ustart;
348: if (ustart >= ulimit) {
349: IF_TOS(TOS = sp[0]);
350: goto branch;
351: }
352: else {
353: INC_IP(1);
354: }
355: :
356: swap 2dup
357: r> swap >r swap >r
358: u>=
359: IF
360: dup @ +
361: ELSE
362: cell+
363: THEN >r ;
364:
365: (-do) nlimit nstart -- gforth paren_minus_do
366: *--rp = nlimit;
367: *--rp = nstart;
368: if (nstart <= nlimit) {
369: IF_TOS(TOS = sp[0]);
370: goto branch;
371: }
372: else {
373: INC_IP(1);
374: }
375: :
376: swap 2dup
377: r> swap >r swap >r
378: <=
379: IF
380: dup @ +
381: ELSE
382: cell+
383: THEN >r ;
384:
385: (u-do) ulimit ustart -- gforth paren_u_minus_do
386: *--rp = ulimit;
387: *--rp = ustart;
388: if (ustart <= ulimit) {
389: IF_TOS(TOS = sp[0]);
390: goto branch;
391: }
392: else {
393: INC_IP(1);
394: }
395: :
396: swap 2dup
397: r> swap >r swap >r
398: u<=
399: IF
400: dup @ +
401: ELSE
402: cell+
403: THEN >r ;
404:
405: \+[THEN]
406:
407: i -- n core
408: n = *rp;
409: :
410: rp@ cell+ @ ;
411:
412: i' -- w gforth i_tick
413: ""loop end value""
414: w = rp[1];
415: :
416: rp@ cell+ cell+ @ ;
417:
418: j -- n core
419: n = rp[2];
420: :
421: rp@ cell+ cell+ cell+ @ ;
422:
423: k -- n gforth
424: n = rp[4];
425: :
426: rp@ [ 5 cells ] Literal + @ ;
427:
428: \ digit is high-level: 0/0%
429:
430: move c_from c_to ucount -- core
431: memmove(c_to,c_from,ucount);
432: /* make an Ifdef for bsd and others? */
433: :
434: >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
435:
436: cmove c_from c_to u -- string
437: while (u-- > 0)
438: *c_to++ = *c_from++;
439: :
440: bounds ?DO dup c@ I c! 1+ LOOP drop ;
441:
442: cmove> c_from c_to u -- string c_move_up
443: while (u-- > 0)
444: c_to[u] = c_from[u];
445: :
446: dup 0= IF drop 2drop exit THEN
447: rot over + -rot bounds swap 1-
448: DO 1- dup c@ I c! -1 +LOOP drop ;
449:
450: fill c_addr u c -- core
451: memset(c_addr,c,u);
452: :
453: -rot bounds
454: ?DO dup I c! LOOP drop ;
455:
456: compare c_addr1 u1 c_addr2 u2 -- n string
457: ""Compare the strings lexicographically. If they are equal, n is 0; if
458: the first string is smaller, n is -1; if the first string is larger, n
459: is 1. Currently this is based on the machine's character
460: comparison. In the future, this may change to considering the current
461: locale and its collation order.""
462: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
463: if (n==0)
464: n = u1-u2;
465: if (n<0)
466: n = -1;
467: else if (n>0)
468: n = 1;
469: :
470: rot 2dup - >r min swap -text dup
471: IF rdrop
472: ELSE drop r@ 0>
473: IF rdrop -1
474: ELSE r> 1 and
475: THEN
476: THEN ;
477:
478: -text c_addr1 u c_addr2 -- n new dash_text
479: n = memcmp(c_addr1, c_addr2, u);
480: if (n<0)
481: n = -1;
482: else if (n>0)
483: n = 1;
484: :
485: swap bounds
486: ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
487: ELSE c@ I c@ - unloop THEN -text-flag ;
488: : -text-flag ( n -- -1/0/1 )
489: dup 0< IF drop -1 ELSE 0> 1 and THEN ;
490:
491: toupper c1 -- c2 gforth
492: c2 = toupper(c1);
493: :
494: dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
495:
496: capscomp c_addr1 u c_addr2 -- n new
497: n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
498: if (n<0)
499: n = -1;
500: else if (n>0)
501: n = 1;
502: :
503: swap bounds
504: ?DO dup c@ I c@ <>
505: IF dup c@ toupper I c@ toupper =
506: ELSE true THEN WHILE 1+ LOOP drop 0
507: ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
508:
509: -trailing c_addr u1 -- c_addr u2 string dash_trailing
510: u2 = u1;
511: while (c_addr[u2-1] == ' ')
512: u2--;
513: :
514: BEGIN 1- 2dup + c@ bl = WHILE
515: dup 0= UNTIL ELSE 1+ THEN ;
516:
517: /string c_addr1 u1 n -- c_addr2 u2 string slash_string
518: c_addr2 = c_addr1+n;
519: u2 = u1-n;
520: :
521: tuck - >r + r> dup 0< IF - 0 THEN ;
522:
523: + n1 n2 -- n core plus
524: n = n1+n2;
525:
526: \ PFE-0.9.14 has it differently, but the next release will have it as follows
527: under+ n1 n2 n3 -- n n2 gforth under_plus
528: ""add @var{n3} to @var{n1} (giving @var{n})""
529: n = n1+n3;
530: :
531: rot + swap ;
532:
533: - n1 n2 -- n core minus
534: n = n1-n2;
535: :
536: negate + ;
537:
538: negate n1 -- n2 core
539: /* use minus as alias */
540: n2 = -n1;
541: :
542: invert 1+ ;
543:
544: 1+ n1 -- n2 core one_plus
545: n2 = n1+1;
546: :
547: 1 + ;
548:
549: 1- n1 -- n2 core one_minus
550: n2 = n1-1;
551: :
552: 1 - ;
553:
554: max n1 n2 -- n core
555: if (n1<n2)
556: n = n2;
557: else
558: n = n1;
559: :
560: 2dup < IF swap THEN drop ;
561:
562: min n1 n2 -- n core
563: if (n1<n2)
564: n = n1;
565: else
566: n = n2;
567: :
568: 2dup > IF swap THEN drop ;
569:
570: abs n1 -- n2 core
571: if (n1<0)
572: n2 = -n1;
573: else
574: n2 = n1;
575: :
576: dup 0< IF negate THEN ;
577:
578: * n1 n2 -- n core star
579: n = n1*n2;
580: :
581: um* drop ;
582:
583: / n1 n2 -- n core slash
584: n = n1/n2;
585: :
586: /mod nip ;
587:
588: mod n1 n2 -- n core
589: n = n1%n2;
590: :
591: /mod drop ;
592:
593: /mod n1 n2 -- n3 n4 core slash_mod
594: n4 = n1/n2;
595: n3 = n1%n2; /* !! is this correct? look into C standard! */
596: :
597: >r s>d r> fm/mod ;
598:
599: 2* n1 -- n2 core two_star
600: n2 = 2*n1;
601: :
602: dup + ;
603:
604: 2/ n1 -- n2 core two_slash
605: /* !! is this still correct? */
606: n2 = n1>>1;
607: :
608: dup MINI and IF 1 ELSE 0 THEN
609: [ bits/byte cell * 1- ] literal
610: 0 DO 2* swap dup 2* >r U-HIGHBIT and
611: IF 1 ELSE 0 THEN or r> swap
612: LOOP nip ;
613:
614: fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
615: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
616: #ifdef BUGGY_LONG_LONG
617: DCell r = fmdiv(d1,n1);
618: n2=r.hi;
619: n3=r.lo;
620: #else
621: /* assumes that the processor uses either floored or symmetric division */
622: n3 = d1/n1;
623: n2 = d1%n1;
624: /* note that this 1%-3>0 is optimized by the compiler */
625: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
626: n3--;
627: n2+=n1;
628: }
629: #endif
630: :
631: dup >r dup 0< IF negate >r dnegate r> THEN
632: over 0< IF tuck + swap THEN
633: um/mod
634: r> 0< IF swap negate swap THEN ;
635:
636: sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
637: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
638: #ifdef BUGGY_LONG_LONG
639: DCell r = smdiv(d1,n1);
640: n2=r.hi;
641: n3=r.lo;
642: #else
643: /* assumes that the processor uses either floored or symmetric division */
644: n3 = d1/n1;
645: n2 = d1%n1;
646: /* note that this 1%-3<0 is optimized by the compiler */
647: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
648: n3++;
649: n2-=n1;
650: }
651: #endif
652: :
653: over >r dup >r abs -rot
654: dabs rot um/mod
655: r> r@ xor 0< IF negate THEN
656: r> 0< IF swap negate swap THEN ;
657:
658: m* n1 n2 -- d core m_star
659: #ifdef BUGGY_LONG_LONG
660: d = mmul(n1,n2);
661: #else
662: d = (DCell)n1 * (DCell)n2;
663: #endif
664: :
665: 2dup 0< and >r
666: 2dup swap 0< and >r
667: um* r> - r> - ;
668:
669: um* u1 u2 -- ud core u_m_star
670: /* use u* as alias */
671: #ifdef BUGGY_LONG_LONG
672: ud = ummul(u1,u2);
673: #else
674: ud = (UDCell)u1 * (UDCell)u2;
675: #endif
676: :
677: >r >r 0 0 r> r> [ 8 cells ] literal 0
678: DO
679: over >r dup >r 0< and d2*+ drop
680: r> 2* r> swap
681: LOOP 2drop ;
682: : d2*+ ( ud n -- ud+n c )
683: over MINI
684: and >r >r 2dup d+ swap r> + swap r> ;
685:
686: um/mod ud u1 -- u2 u3 core u_m_slash_mod
687: #ifdef BUGGY_LONG_LONG
688: UDCell r = umdiv(ud,u1);
689: u2=r.hi;
690: u3=r.lo;
691: #else
692: u3 = ud/u1;
693: u2 = ud%u1;
694: #endif
695: :
696: 0 swap [ 8 cells 1 + ] literal 0
697: ?DO >r /modstep r>
698: LOOP drop swap 1 rshift or swap ;
699: : /modstep ( ud c R: u -- ud-?u c R: u )
700: over I' u< 0= or IF I' - 1 ELSE 0 THEN d2*+ ;
701: : d2*+ ( ud n -- ud+n c )
702: over MINI
703: and >r >r 2dup d+ swap r> + swap r> ;
704:
705: m+ d1 n -- d2 double m_plus
706: #ifdef BUGGY_LONG_LONG
707: d2.lo = d1.lo+n;
708: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
709: #else
710: d2 = d1+n;
711: #endif
712: :
713: s>d d+ ;
714:
715: d+ d1 d2 -- d double d_plus
716: #ifdef BUGGY_LONG_LONG
717: d.lo = d1.lo+d2.lo;
718: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
719: #else
720: d = d1+d2;
721: #endif
722: :
723: rot + >r tuck + swap over u> r> swap - ;
724:
725: d- d1 d2 -- d double d_minus
726: #ifdef BUGGY_LONG_LONG
727: d.lo = d1.lo - d2.lo;
728: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
729: #else
730: d = d1-d2;
731: #endif
732: :
733: dnegate d+ ;
734:
735: dnegate d1 -- d2 double
736: /* use dminus as alias */
737: #ifdef BUGGY_LONG_LONG
738: d2 = dnegate(d1);
739: #else
740: d2 = -d1;
741: #endif
742: :
743: invert swap negate tuck 0= - ;
744:
745: d2* d1 -- d2 double d_two_star
746: #ifdef BUGGY_LONG_LONG
747: d2.lo = d1.lo<<1;
748: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
749: #else
750: d2 = 2*d1;
751: #endif
752: :
753: 2dup d+ ;
754:
755: d2/ d1 -- d2 double d_two_slash
756: #ifdef BUGGY_LONG_LONG
757: d2.hi = d1.hi>>1;
758: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
759: #else
760: d2 = d1>>1;
761: #endif
762: :
763: dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
764: r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
765:
766: and w1 w2 -- w core
767: w = w1&w2;
768:
769: or w1 w2 -- w core
770: w = w1|w2;
771: :
772: invert swap invert and invert ;
773:
774: xor w1 w2 -- w core
775: w = w1^w2;
776:
777: invert w1 -- w2 core
778: w2 = ~w1;
779: :
780: MAXU xor ;
781:
782: rshift u1 n -- u2 core
783: u2 = u1>>n;
784: :
785: 0 ?DO 2/ MAXI and LOOP ;
786:
787: lshift u1 n -- u2 core
788: u2 = u1<<n;
789: :
790: 0 ?DO 2* LOOP ;
791:
792: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
793: define(comparisons,
794: $1= $2 -- f $6 $3equals
795: f = FLAG($4==$5);
796: :
797: [ char $1x char 0 = [IF]
798: ] IF false ELSE true THEN [
799: [ELSE]
800: ] xor 0= [
801: [THEN] ] ;
802:
803: $1<> $2 -- f $7 $3different
804: f = FLAG($4!=$5);
805: :
806: [ char $1x char 0 = [IF]
807: ] IF true ELSE false THEN [
808: [ELSE]
809: ] xor 0<> [
810: [THEN] ] ;
811:
812: $1< $2 -- f $8 $3less
813: f = FLAG($4<$5);
814: :
815: [ char $1x char 0 = [IF]
816: ] MINI and 0<> [
817: [ELSE] char $1x char u = [IF]
818: ] 2dup xor 0< IF nip ELSE - THEN 0< [
819: [ELSE]
820: ] MINI xor >r MINI xor r> u< [
821: [THEN]
822: [THEN] ] ;
823:
824: $1> $2 -- f $9 $3greater
825: f = FLAG($4>$5);
826: :
827: [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
828: $1< ;
829:
830: $1<= $2 -- f gforth $3less_or_equal
831: f = FLAG($4<=$5);
832: :
833: $1> 0= ;
834:
835: $1>= $2 -- f gforth $3greater_or_equal
836: f = FLAG($4>=$5);
837: :
838: [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
839: $1<= ;
840:
841: )
842:
843: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
844: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
845: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
846:
847: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
848: define(dcomparisons,
849: $1= $2 -- f $6 $3equals
850: #ifdef BUGGY_LONG_LONG
851: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
852: #else
853: f = FLAG($4==$5);
854: #endif
855:
856: $1<> $2 -- f $7 $3different
857: #ifdef BUGGY_LONG_LONG
858: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
859: #else
860: f = FLAG($4!=$5);
861: #endif
862:
863: $1< $2 -- f $8 $3less
864: #ifdef BUGGY_LONG_LONG
865: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
866: #else
867: f = FLAG($4<$5);
868: #endif
869:
870: $1> $2 -- f $9 $3greater
871: #ifdef BUGGY_LONG_LONG
872: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
873: #else
874: f = FLAG($4>$5);
875: #endif
876:
877: $1<= $2 -- f gforth $3less_or_equal
878: #ifdef BUGGY_LONG_LONG
879: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
880: #else
881: f = FLAG($4<=$5);
882: #endif
883:
884: $1>= $2 -- f gforth $3greater_or_equal
885: #ifdef BUGGY_LONG_LONG
886: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
887: #else
888: f = FLAG($4>=$5);
889: #endif
890:
891: )
892:
893: \+has-dcomps [IF]
894:
895: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
896: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
897: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
898:
899: \+[THEN]
900:
901: within u1 u2 u3 -- f core-ext
902: f = FLAG(u1-u2 < u3-u2);
903: :
904: over - >r - r> u< ;
905:
906: sp@ -- a_addr gforth spat
907: a_addr = sp+1;
908:
909: sp! a_addr -- gforth spstore
910: sp = a_addr;
911: /* works with and without TOS caching */
912:
913: rp@ -- a_addr gforth rpat
914: a_addr = rp;
915:
916: rp! a_addr -- gforth rpstore
917: rp = a_addr;
918:
919: \+has-floats [IF]
920:
921: fp@ -- f_addr gforth fp_fetch
922: f_addr = fp;
923:
924: fp! f_addr -- gforth fp_store
925: fp = f_addr;
926:
927: \+[THEN]
928:
929: ;s -- gforth semis
930: ip = (Xt *)(*rp++);
931: NEXT_P0;
932:
933: >r w -- core to_r
934: *--rp = w;
935: :
936: (>r) ;
937: : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
938:
939: r> -- w core r_from
940: w = *rp++;
941: :
942: rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
943: Create (rdrop) ' ;s A,
944:
945: rdrop -- gforth
946: rp++;
947: :
948: r> r> drop >r ;
949:
950: 2>r w1 w2 -- core-ext two_to_r
951: *--rp = w1;
952: *--rp = w2;
953: :
954: swap r> swap >r swap >r >r ;
955:
956: 2r> -- w1 w2 core-ext two_r_from
957: w2 = *rp++;
958: w1 = *rp++;
959: :
960: r> r> swap r> swap >r swap ;
961:
962: 2r@ -- w1 w2 core-ext two_r_fetch
963: w2 = rp[0];
964: w1 = rp[1];
965: :
966: i' j ;
967:
968: 2rdrop -- gforth two_r_drop
969: rp+=2;
970: :
971: r> r> drop r> drop >r ;
972:
973: over w1 w2 -- w1 w2 w1 core
974: :
975: sp@ cell+ @ ;
976:
977: drop w -- core
978: :
979: IF THEN ;
980:
981: swap w1 w2 -- w2 w1 core
982: :
983: >r (swap) ! r> (swap) @ ;
984: Variable (swap)
985:
986: dup w -- w w core
987: :
988: sp@ @ ;
989:
990: rot w1 w2 w3 -- w2 w3 w1 core rote
991: :
992: [ defined? (swap) [IF] ]
993: (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
994: Variable (rot)
995: [ELSE] ]
996: >r swap r> swap ;
997: [THEN]
998:
999: -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
1000: :
1001: rot rot ;
1002:
1003: nip w1 w2 -- w2 core-ext
1004: :
1005: >r drop r> ;
1006:
1007: tuck w1 w2 -- w2 w1 w2 core-ext
1008: :
1009: swap over ;
1010:
1011: ?dup w -- w core question_dupe
1012: if (w!=0) {
1013: IF_TOS(*sp-- = w;)
1014: #ifndef USE_TOS
1015: *--sp = w;
1016: #endif
1017: }
1018: :
1019: dup IF dup THEN ;
1020:
1021: pick u -- w core-ext
1022: w = sp[u+1];
1023: :
1024: 1+ cells sp@ + @ ;
1025:
1026: 2drop w1 w2 -- core two_drop
1027: :
1028: drop drop ;
1029:
1030: 2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
1031: :
1032: over over ;
1033:
1034: 2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
1035: :
1036: 3 pick 3 pick ;
1037:
1038: 2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
1039: :
1040: rot >r rot r> ;
1041:
1042: 2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double-ext two_rote
1043: :
1044: >r >r 2swap r> r> 2swap ;
1045:
1046: 2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
1047: :
1048: 2swap 2drop ;
1049:
1050: 2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
1051: :
1052: 2swap 2over ;
1053:
1054: \ toggle is high-level: 0.11/0.42%
1055:
1056: @ a_addr -- w core fetch
1057: w = *a_addr;
1058:
1059: ! w a_addr -- core store
1060: *a_addr = w;
1061:
1062: +! n a_addr -- core plus_store
1063: *a_addr += n;
1064: :
1065: tuck @ + swap ! ;
1066:
1067: c@ c_addr -- c core cfetch
1068: c = *c_addr;
1069: :
1070: [ bigendian [IF] ]
1071: [ cell>bit 4 = [IF] ]
1072: dup [ 0 cell - ] Literal and @ swap 1 and
1073: IF $FF and ELSE 8>> THEN ;
1074: [ [ELSE] ]
1075: dup [ cell 1- ] literal and
1076: tuck - @ swap [ cell 1- ] literal xor
1077: 0 ?DO 8>> LOOP $FF and
1078: [ [THEN] ]
1079: [ [ELSE] ]
1080: [ cell>bit 4 = [IF] ]
1081: dup [ 0 cell - ] Literal and @ swap 1 and
1082: IF 8>> ELSE $FF and THEN
1083: [ [ELSE] ]
1084: dup [ cell 1- ] literal and
1085: tuck - @ swap
1086: 0 ?DO 8>> LOOP 255 and
1087: [ [THEN] ]
1088: [ [THEN] ]
1089: ;
1090: : 8>> 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ ;
1091:
1092: c! c c_addr -- core cstore
1093: *c_addr = c;
1094: :
1095: [ bigendian [IF] ]
1096: [ cell>bit 4 = [IF] ]
1097: tuck 1 and IF $FF and ELSE 8<< THEN >r
1098: dup -2 and @ over 1 and cells masks + @ and
1099: r> or swap -2 and ! ;
1100: Create masks $00FF , $FF00 ,
1101: [ELSE] ]
1102: dup [ cell 1- ] literal and dup
1103: [ cell 1- ] literal xor >r
1104: - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1105: rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1106: [THEN]
1107: [ELSE] ]
1108: [ cell>bit 4 = [IF] ]
1109: tuck 1 and IF 8<< ELSE $FF and THEN >r
1110: dup -2 and @ over 1 and cells masks + @ and
1111: r> or swap -2 and ! ;
1112: Create masks $FF00 , $00FF ,
1113: [ELSE] ]
1114: dup [ cell 1- ] literal and dup >r
1115: - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1116: rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1117: [THEN]
1118: [THEN]
1119: : 8<< 2* 2* 2* 2* 2* 2* 2* 2* ;
1120:
1121: 2! w1 w2 a_addr -- core two_store
1122: a_addr[0] = w2;
1123: a_addr[1] = w1;
1124: :
1125: tuck ! cell+ ! ;
1126:
1127: 2@ a_addr -- w1 w2 core two_fetch
1128: w2 = a_addr[0];
1129: w1 = a_addr[1];
1130: :
1131: dup cell+ @ swap @ ;
1132:
1133: cell+ a_addr1 -- a_addr2 core cell_plus
1134: a_addr2 = a_addr1+1;
1135: :
1136: cell + ;
1137:
1138: cells n1 -- n2 core
1139: n2 = n1 * sizeof(Cell);
1140: :
1141: [ cell
1142: 2/ dup [IF] ] 2* [ [THEN]
1143: 2/ dup [IF] ] 2* [ [THEN]
1144: 2/ dup [IF] ] 2* [ [THEN]
1145: 2/ dup [IF] ] 2* [ [THEN]
1146: drop ] ;
1147:
1148: char+ c_addr1 -- c_addr2 core care_plus
1149: c_addr2 = c_addr1 + 1;
1150: :
1151: 1+ ;
1152:
1153: (chars) n1 -- n2 gforth paren_cares
1154: n2 = n1 * sizeof(Char);
1155: :
1156: ;
1157:
1158: count c_addr1 -- c_addr2 u core
1159: u = *c_addr1;
1160: c_addr2 = c_addr1+1;
1161: :
1162: dup 1+ swap c@ ;
1163:
1164: (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
1165: for (; f83name1 != NULL; f83name1 = f83name1->next)
1166: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1167: memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1168: break;
1169: f83name2=f83name1;
1170: :
1171: BEGIN dup WHILE (find-samelen) dup WHILE
1172: >r 2dup r@ cell+ char+ capscomp 0=
1173: IF 2drop r> EXIT THEN
1174: r> @
1175: REPEAT THEN nip nip ;
1176: : (find-samelen) ( u f83name1 -- u f83name2/0 )
1177: BEGIN 2dup cell+ c@ $1F and <> WHILE @ dup 0= UNTIL THEN ;
1178:
1179: \+has-hash [IF]
1180:
1181: (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
1182: F83Name *f83name1;
1183: f83name2=NULL;
1184: while(a_addr != NULL)
1185: {
1186: f83name1=(F83Name *)(a_addr[1]);
1187: a_addr=(Cell *)(a_addr[0]);
1188: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1189: memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1190: {
1191: f83name2=f83name1;
1192: break;
1193: }
1194: }
1195: :
1196: BEGIN dup WHILE
1197: 2@ >r >r dup r@ cell+ c@ $1F and =
1198: IF 2dup r@ cell+ char+ capscomp 0=
1199: IF 2drop r> rdrop EXIT THEN THEN
1200: rdrop r>
1201: REPEAT nip nip ;
1202:
1203: (tablefind) c_addr u a_addr -- f83name2 new paren_tablefind
1204: ""A case-sensitive variant of @code{(hashfind)}""
1205: F83Name *f83name1;
1206: f83name2=NULL;
1207: while(a_addr != NULL)
1208: {
1209: f83name1=(F83Name *)(a_addr[1]);
1210: a_addr=(Cell *)(a_addr[0]);
1211: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1212: memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1213: {
1214: f83name2=f83name1;
1215: break;
1216: }
1217: }
1218: :
1219: BEGIN dup WHILE
1220: 2@ >r >r dup r@ cell+ c@ $1F and =
1221: IF 2dup r@ cell+ char+ -text 0=
1222: IF 2drop r> rdrop EXIT THEN THEN
1223: rdrop r>
1224: REPEAT nip nip ;
1225:
1226: (hashkey) c_addr u1 -- u2 gforth paren_hashkey
1227: u2=0;
1228: while(u1--)
1229: u2+=(Cell)toupper(*c_addr++);
1230: :
1231: 0 -rot bounds ?DO I c@ toupper + LOOP ;
1232:
1233: (hashkey1) c_addr u ubits -- ukey gforth paren_hashkey1
1234: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1235: /* this hash function rotates the key at every step by rot bits within
1236: ubits bits and xors it with the character. This function does ok in
1237: the chi-sqare-test. Rot should be <=7 (preferably <=5) for
1238: ASCII strings (larger if ubits is large), and should share no
1239: divisors with ubits.
1240: */
1241: unsigned rot = ((char []){5,0,1,2,3,4,5,5,5,5,3,5,5,5,5,7,5,5,5,5,7,5,5,5,5,6,5,5,5,5,7,5,5})[ubits];
1242: Char *cp = c_addr;
1243: for (ukey=0; cp<c_addr+u; cp++)
1244: ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
1245: ^ toupper(*cp))
1246: & ((1<<ubits)-1));
1247: :
1248: dup rot-values + c@ over 1 swap lshift 1- >r
1249: tuck - 2swap r> 0 2swap bounds
1250: ?DO dup 4 pick lshift swap 3 pick rshift or
1251: I c@ toupper xor
1252: over and LOOP
1253: nip nip nip ;
1254: Create rot-values
1255: 5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
1256: 3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
1257: 7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1258: 7 c, 5 c, 5 c,
1259:
1260: \+[THEN]
1261:
1262: (parse-white) c_addr1 u1 -- c_addr2 u2 gforth paren_parse_white
1263: /* use !isgraph instead of isspace? */
1264: Char *endp = c_addr1+u1;
1265: while (c_addr1<endp && isspace(*c_addr1))
1266: c_addr1++;
1267: if (c_addr1<endp) {
1268: for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1269: ;
1270: u2 = c_addr1-c_addr2;
1271: }
1272: else {
1273: c_addr2 = c_addr1;
1274: u2 = 0;
1275: }
1276: :
1277: BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1278: REPEAT THEN 2dup
1279: BEGIN dup WHILE over c@ bl > WHILE 1 /string
1280: REPEAT THEN nip - ;
1281:
1282: aligned c_addr -- a_addr core
1283: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1284: :
1285: [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1286:
1287: faligned c_addr -- f_addr float f_aligned
1288: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1289: :
1290: [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1291:
1292: >body xt -- a_addr core to_body
1293: a_addr = PFA(xt);
1294: :
1295: 2 cells + ;
1296:
1297: >code-address xt -- c_addr gforth to_code_address
1298: ""c_addr is the code address of the word xt""
1299: /* !! This behaves installation-dependently for DOES-words */
1300: c_addr = (Address)CODE_ADDRESS(xt);
1301: :
1302: @ ;
1303:
1304: >does-code xt -- a_addr gforth to_does_code
1305: ""If xt ist the execution token of a defining-word-defined word,
1306: a_addr is the start of the Forth code after the DOES>;
1307: Otherwise a_addr is 0.""
1308: a_addr = (Cell *)DOES_CODE(xt);
1309: :
1310: cell+ @ ;
1311:
1312: code-address! c_addr xt -- gforth code_address_store
1313: ""Creates a code field with code address c_addr at xt""
1314: MAKE_CF(xt, c_addr);
1315: CACHE_FLUSH(xt,PFA(0));
1316: :
1317: ! ;
1318:
1319: does-code! a_addr xt -- gforth does_code_store
1320: ""creates a code field at xt for a defining-word-defined word; a_addr
1321: is the start of the Forth code after DOES>""
1322: MAKE_DOES_CF(xt, a_addr);
1323: CACHE_FLUSH(xt,PFA(0));
1324: :
1325: dodoes: over ! cell+ ! ;
1326:
1327: does-handler! a_addr -- gforth does_handler_store
1328: ""creates a DOES>-handler at address a_addr. a_addr usually points
1329: just behind a DOES>.""
1330: MAKE_DOES_HANDLER(a_addr);
1331: CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1332: :
1333: drop ;
1334:
1335: /does-handler -- n gforth slash_does_handler
1336: ""the size of a does-handler (includes possible padding)""
1337: /* !! a constant or environmental query might be better */
1338: n = DOES_HANDLER_SIZE;
1339: :
1340: 2 cells ;
1341:
1342: threading-method -- n gforth threading_method
1343: ""0 if the engine is direct threaded. Note that this may change during
1344: the lifetime of an image.""
1345: #if defined(DOUBLY_INDIRECT)
1346: n=2;
1347: #else
1348: # if defined(DIRECT_THREADED)
1349: n=0;
1350: # else
1351: n=1;
1352: # endif
1353: #endif
1354: :
1355: 1 ;
1356:
1357: \+has-os [IF]
1358:
1359: (key) -- n gforth paren_key
1360: fflush(stdout);
1361: /* !! noecho */
1362: n = key();
1363:
1364: key? -- n facility key_q
1365: fflush(stdout);
1366: n = key_query;
1367:
1368: stdout -- wfileid gforth
1369: wfileid = (Cell)stdout;
1370:
1371: stderr -- wfileid gforth
1372: wfileid = (Cell)stderr;
1373:
1374: form -- urows ucols gforth
1375: ""The number of lines and columns in the terminal. These numbers may change
1376: with the window size.""
1377: /* we could block SIGWINCH here to get a consistent size, but I don't
1378: think this is necessary or always beneficial */
1379: urows=rows;
1380: ucols=cols;
1381:
1382: flush-icache c_addr u -- gforth flush_icache
1383: ""Make sure that the instruction cache of the processor (if there is
1384: one) does not contain stale data at @var{c_addr} and @var{u} bytes
1385: afterwards. @code{END-CODE} performs a @code{flush-icache}
1386: automatically. Caveat: @code{flush-icache} might not work on your
1387: installation; this is usually the case if direct threading is not
1388: supported on your machine (take a look at your @file{machine.h}) and
1389: your machine has a separate instruction cache. In such cases,
1390: @code{flush-icache} does nothing instead of flushing the instruction
1391: cache.""
1392: FLUSH_ICACHE(c_addr,u);
1393:
1394: (bye) n -- gforth paren_bye
1395: return (Label *)n;
1396:
1397: (system) c_addr u -- wretval wior gforth peren_system
1398: int old_tp=terminal_prepped;
1399: deprep_terminal();
1400: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1401: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1402: if (old_tp)
1403: prep_terminal();
1404:
1405: getenv c_addr1 u1 -- c_addr2 u2 gforth
1406: c_addr2 = getenv(cstr(c_addr1,u1,1));
1407: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1408:
1409: open-pipe c_addr u ntype -- wfileid wior gforth open_pipe
1410: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
1411: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1412:
1413: close-pipe wfileid -- wretval wior gforth close_pipe
1414: wretval = pclose((FILE *)wfileid);
1415: wior = IOR(wretval==-1);
1416:
1417: time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
1418: struct timeval time1;
1419: struct timezone zone1;
1420: struct tm *ltime;
1421: gettimeofday(&time1,&zone1);
1422: ltime=localtime((time_t *)&time1.tv_sec);
1423: nyear =ltime->tm_year+1900;
1424: nmonth=ltime->tm_mon+1;
1425: nday =ltime->tm_mday;
1426: nhour =ltime->tm_hour;
1427: nmin =ltime->tm_min;
1428: nsec =ltime->tm_sec;
1429:
1430: ms n -- facility-ext
1431: struct timeval timeout;
1432: timeout.tv_sec=n/1000;
1433: timeout.tv_usec=1000*(n%1000);
1434: (void)select(0,0,0,0,&timeout);
1435:
1436: allocate u -- a_addr wior memory
1437: a_addr = (Cell *)malloc(u?u:1);
1438: wior = IOR(a_addr==NULL);
1439:
1440: free a_addr -- wior memory
1441: free(a_addr);
1442: wior = 0;
1443:
1444: resize a_addr1 u -- a_addr2 wior memory
1445: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
1446: address units, possibly moving the contents to a different
1447: area. @i{a_addr2} is the address of the resulting area. If
1448: @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
1449: @code{allocate}s @i{u} address units.""
1450: /* the following check is not necessary on most OSs, but it is needed
1451: on SunOS 4.1.2. */
1452: if (a_addr1==NULL)
1453: a_addr2 = (Cell *)malloc(u);
1454: else
1455: a_addr2 = (Cell *)realloc(a_addr1, u);
1456: wior = IOR(a_addr2==NULL); /* !! Define a return code */
1457:
1458: strerror n -- c_addr u gforth
1459: c_addr = strerror(n);
1460: u = strlen(c_addr);
1461:
1462: strsignal n -- c_addr u gforth
1463: c_addr = strsignal(n);
1464: u = strlen(c_addr);
1465:
1466: call-c w -- gforth call_c
1467: ""Call the C function pointed to by @i{w}. The C function has to
1468: access the stack itself. The stack pointers are exported in the global
1469: variables @code{SP} and @code{FP}.""
1470: /* This is a first attempt at support for calls to C. This may change in
1471: the future */
1472: IF_FTOS(fp[0]=FTOS);
1473: FP=fp;
1474: SP=sp;
1475: ((void (*)())w)();
1476: sp=SP;
1477: fp=FP;
1478: IF_TOS(TOS=sp[0]);
1479: IF_FTOS(FTOS=fp[0]);
1480:
1481: \+[THEN] ( has-os ) has-files [IF]
1482:
1483: close-file wfileid -- wior file close_file
1484: wior = IOR(fclose((FILE *)wfileid)==EOF);
1485:
1486: open-file c_addr u ntype -- w2 wior file open_file
1487: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1488: wior = IOR(w2 == 0);
1489:
1490: create-file c_addr u ntype -- w2 wior file create_file
1491: Cell fd;
1492: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
1493: if (fd != -1) {
1494: w2 = (Cell)fdopen(fd, fileattr[ntype]);
1495: wior = IOR(w2 == 0);
1496: } else {
1497: w2 = 0;
1498: wior = IOR(1);
1499: }
1500:
1501: delete-file c_addr u -- wior file delete_file
1502: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1503:
1504: rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1505: char *s1=tilde_cstr(c_addr2, u2, 1);
1506: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1507:
1508: file-position wfileid -- ud wior file file_position
1509: /* !! use tell and lseek? */
1510: ud = LONG2UD(ftell((FILE *)wfileid));
1511: wior = IOR(UD2LONG(ud)==-1);
1512:
1513: reposition-file ud wfileid -- wior file reposition_file
1514: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1515:
1516: file-size wfileid -- ud wior file file_size
1517: #include <sys/stat.h>
1518: struct stat buf;
1519: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1520: ud = LONG2UD(buf.st_size);
1521:
1522: resize-file ud wfileid -- wior file resize_file
1523: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1524:
1525: read-file c_addr u1 wfileid -- u2 wior file read_file
1526: /* !! fread does not guarantee enough */
1527: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1528: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1529: /* !! is the value of ferror errno-compatible? */
1530: if (wior)
1531: clearerr((FILE *)wfileid);
1532:
1533: read-line c_addr u1 wfileid -- u2 flag wior file read_line
1534: /*
1535: Cell c;
1536: flag=-1;
1537: for(u2=0; u2<u1; u2++)
1538: {
1539: *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1540: if(c=='\n') break;
1541: if(c==EOF)
1542: {
1543: flag=FLAG(u2!=0);
1544: break;
1545: }
1546: }
1547: wior=FILEIO(ferror((FILE *)wfileid));
1548: */
1549: if ((flag=FLAG(!feof((FILE *)wfileid) &&
1550: fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1551: wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1552: if (wior)
1553: clearerr((FILE *)wfileid);
1554: u2 = strlen(c_addr);
1555: u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1556: }
1557: else {
1558: wior=0;
1559: u2=0;
1560: }
1561:
1562: \+[THEN] has-files [IF] -1 [ELSE] has-os [THEN] [IF]
1563:
1564: write-file c_addr u1 wfileid -- wior file write_file
1565: /* !! fwrite does not guarantee enough */
1566: {
1567: UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1568: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1569: if (wior)
1570: clearerr((FILE *)wfileid);
1571: }
1572:
1573: emit-file c wfileid -- wior gforth emit_file
1574: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1575: if (wior)
1576: clearerr((FILE *)wfileid);
1577:
1578: \+[THEN] has-files [IF]
1579:
1580: flush-file wfileid -- wior file-ext flush_file
1581: wior = IOR(fflush((FILE *) wfileid)==EOF);
1582:
1583: file-status c_addr u -- ntype wior file-ext file_status
1584: char *filename=tilde_cstr(c_addr, u, 1);
1585: if (access (filename, F_OK) != 0) {
1586: ntype=0;
1587: wior=IOR(1);
1588: }
1589: else if (access (filename, R_OK | W_OK) == 0) {
1590: ntype=2; /* r/w */
1591: wior=0;
1592: }
1593: else if (access (filename, R_OK) == 0) {
1594: ntype=0; /* r/o */
1595: wior=0;
1596: }
1597: else if (access (filename, W_OK) == 0) {
1598: ntype=4; /* w/o */
1599: wior=0;
1600: }
1601: else {
1602: ntype=1; /* well, we cannot access the file, but better deliver a legal
1603: access mode (r/o bin), so we get a decent error later upon open. */
1604: wior=0;
1605: }
1606:
1607: \+[THEN] ( has-files ) has-floats [IF]
1608:
1609: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1610: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1611:
1612: d>f d -- r float d_to_f
1613: #ifdef BUGGY_LONG_LONG
1614: extern double ldexp(double x, int exp);
1615: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
1616: #else
1617: r = d;
1618: #endif
1619:
1620: f>d r -- d float f_to_d
1621: #ifdef BUGGY_LONG_LONG
1622: d.hi = ldexp(r,-CELL_BITS) - (r<0);
1623: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1624: #else
1625: d = r;
1626: #endif
1627:
1628: f! r f_addr -- float f_store
1629: *f_addr = r;
1630:
1631: f@ f_addr -- r float f_fetch
1632: r = *f_addr;
1633:
1634: df@ df_addr -- r float-ext d_f_fetch
1635: #ifdef IEEE_FP
1636: r = *df_addr;
1637: #else
1638: !! df@
1639: #endif
1640:
1641: df! r df_addr -- float-ext d_f_store
1642: #ifdef IEEE_FP
1643: *df_addr = r;
1644: #else
1645: !! df!
1646: #endif
1647:
1648: sf@ sf_addr -- r float-ext s_f_fetch
1649: #ifdef IEEE_FP
1650: r = *sf_addr;
1651: #else
1652: !! sf@
1653: #endif
1654:
1655: sf! r sf_addr -- float-ext s_f_store
1656: #ifdef IEEE_FP
1657: *sf_addr = r;
1658: #else
1659: !! sf!
1660: #endif
1661:
1662: f+ r1 r2 -- r3 float f_plus
1663: r3 = r1+r2;
1664:
1665: f- r1 r2 -- r3 float f_minus
1666: r3 = r1-r2;
1667:
1668: f* r1 r2 -- r3 float f_star
1669: r3 = r1*r2;
1670:
1671: f/ r1 r2 -- r3 float f_slash
1672: r3 = r1/r2;
1673:
1674: f** r1 r2 -- r3 float-ext f_star_star
1675: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1676: r3 = pow(r1,r2);
1677:
1678: fnegate r1 -- r2 float
1679: r2 = - r1;
1680:
1681: fdrop r -- float
1682:
1683: fdup r -- r r float
1684:
1685: fswap r1 r2 -- r2 r1 float
1686:
1687: fover r1 r2 -- r1 r2 r1 float
1688:
1689: frot r1 r2 r3 -- r2 r3 r1 float
1690:
1691: fnip r1 r2 -- r2 gforth
1692:
1693: ftuck r1 r2 -- r2 r1 r2 gforth
1694:
1695: float+ f_addr1 -- f_addr2 float float_plus
1696: f_addr2 = f_addr1+1;
1697:
1698: floats n1 -- n2 float
1699: n2 = n1*sizeof(Float);
1700:
1701: floor r1 -- r2 float
1702: ""round towards the next smaller integral value, i.e., round toward negative infinity""
1703: /* !! unclear wording */
1704: r2 = floor(r1);
1705:
1706: fround r1 -- r2 float
1707: ""round to the nearest integral value""
1708: /* !! unclear wording */
1709: #ifdef HAVE_RINT
1710: r2 = rint(r1);
1711: #else
1712: r2 = floor(r1+0.5);
1713: /* !! This is not quite true to the rounding rules given in the standard */
1714: #endif
1715:
1716: fmax r1 r2 -- r3 float
1717: if (r1<r2)
1718: r3 = r2;
1719: else
1720: r3 = r1;
1721:
1722: fmin r1 r2 -- r3 float
1723: if (r1<r2)
1724: r3 = r1;
1725: else
1726: r3 = r2;
1727:
1728: represent r c_addr u -- n f1 f2 float
1729: char *sig;
1730: int flag;
1731: int decpt;
1732: sig=ecvt(r, u, &decpt, &flag);
1733: n=(r==0 ? 1 : decpt);
1734: f1=FLAG(flag!=0);
1735: f2=FLAG(isdigit(sig[0])!=0);
1736: memmove(c_addr,sig,u);
1737:
1738: >float c_addr u -- flag float to_float
1739: /* real signature: c_addr u -- r t / f */
1740: Float r;
1741: char *number=cstr(c_addr, u, 1);
1742: char *endconv;
1743: while(isspace(number[--u]) && u>0);
1744: switch(number[u])
1745: {
1746: case 'd':
1747: case 'D':
1748: case 'e':
1749: case 'E': break;
1750: default : u++; break;
1751: }
1752: number[u]='\0';
1753: r=strtod(number,&endconv);
1754: if((flag=FLAG(!(Cell)*endconv)))
1755: {
1756: IF_FTOS(fp[0] = FTOS);
1757: fp += -1;
1758: FTOS = r;
1759: }
1760: else if(*endconv=='d' || *endconv=='D')
1761: {
1762: *endconv='E';
1763: r=strtod(number,&endconv);
1764: if((flag=FLAG(!(Cell)*endconv)))
1765: {
1766: IF_FTOS(fp[0] = FTOS);
1767: fp += -1;
1768: FTOS = r;
1769: }
1770: }
1771:
1772: fabs r1 -- r2 float-ext
1773: r2 = fabs(r1);
1774:
1775: facos r1 -- r2 float-ext
1776: r2 = acos(r1);
1777:
1778: fasin r1 -- r2 float-ext
1779: r2 = asin(r1);
1780:
1781: fatan r1 -- r2 float-ext
1782: r2 = atan(r1);
1783:
1784: fatan2 r1 r2 -- r3 float-ext
1785: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1786: intends this to be the inverse of @code{fsincos}. In gforth it is.""
1787: r3 = atan2(r1,r2);
1788:
1789: fcos r1 -- r2 float-ext
1790: r2 = cos(r1);
1791:
1792: fexp r1 -- r2 float-ext
1793: r2 = exp(r1);
1794:
1795: fexpm1 r1 -- r2 float-ext
1796: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1797: #ifdef HAVE_EXPM1
1798: extern double expm1(double);
1799: r2 = expm1(r1);
1800: #else
1801: r2 = exp(r1)-1.;
1802: #endif
1803:
1804: fln r1 -- r2 float-ext
1805: r2 = log(r1);
1806:
1807: flnp1 r1 -- r2 float-ext
1808: ""@i{r2}=ln(@i{r1}+1)""
1809: #ifdef HAVE_LOG1P
1810: extern double log1p(double);
1811: r2 = log1p(r1);
1812: #else
1813: r2 = log(r1+1.);
1814: #endif
1815:
1816: flog r1 -- r2 float-ext
1817: ""the decimal logarithm""
1818: r2 = log10(r1);
1819:
1820: falog r1 -- r2 float-ext
1821: ""@i{r2}=10**@i{r1}""
1822: extern double pow10(double);
1823: r2 = pow10(r1);
1824:
1825: fsin r1 -- r2 float-ext
1826: r2 = sin(r1);
1827:
1828: fsincos r1 -- r2 r3 float-ext
1829: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1830: r2 = sin(r1);
1831: r3 = cos(r1);
1832:
1833: fsqrt r1 -- r2 float-ext
1834: r2 = sqrt(r1);
1835:
1836: ftan r1 -- r2 float-ext
1837: r2 = tan(r1);
1838: :
1839: fsincos f/ ;
1840:
1841: fsinh r1 -- r2 float-ext
1842: r2 = sinh(r1);
1843: :
1844: fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1845:
1846: fcosh r1 -- r2 float-ext
1847: r2 = cosh(r1);
1848: :
1849: fexp fdup 1/f f+ f2/ ;
1850:
1851: ftanh r1 -- r2 float-ext
1852: r2 = tanh(r1);
1853: :
1854: f2* fexpm1 fdup 2. d>f f+ f/ ;
1855:
1856: fasinh r1 -- r2 float-ext
1857: r2 = asinh(r1);
1858: :
1859: fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1860:
1861: facosh r1 -- r2 float-ext
1862: r2 = acosh(r1);
1863: :
1864: fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1865:
1866: fatanh r1 -- r2 float-ext
1867: r2 = atanh(r1);
1868: :
1869: fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1870: r> IF fnegate THEN ;
1871:
1872: sfloats n1 -- n2 float-ext s_floats
1873: n2 = n1*sizeof(SFloat);
1874:
1875: dfloats n1 -- n2 float-ext d_floats
1876: n2 = n1*sizeof(DFloat);
1877:
1878: sfaligned c_addr -- sf_addr float-ext s_f_aligned
1879: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1880: :
1881: [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1882:
1883: dfaligned c_addr -- df_addr float-ext d_f_aligned
1884: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1885: :
1886: [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1887:
1888: \ The following words access machine/OS/installation-dependent
1889: \ Gforth internals
1890: \ !! how about environmental queries DIRECT-THREADED,
1891: \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1892:
1893: \ local variable implementation primitives
1894: \+[THEN] ( has-floats ) has-locals [IF]
1895:
1896: @local# -- w gforth fetch_local_number
1897: w = *(Cell *)(lp+(Cell)NEXT_INST);
1898: INC_IP(1);
1899:
1900: @local0 -- w new fetch_local_zero
1901: w = *(Cell *)(lp+0*sizeof(Cell));
1902:
1903: @local1 -- w new fetch_local_four
1904: w = *(Cell *)(lp+1*sizeof(Cell));
1905:
1906: @local2 -- w new fetch_local_eight
1907: w = *(Cell *)(lp+2*sizeof(Cell));
1908:
1909: @local3 -- w new fetch_local_twelve
1910: w = *(Cell *)(lp+3*sizeof(Cell));
1911:
1912: \+has-floats [IF]
1913:
1914: f@local# -- r gforth f_fetch_local_number
1915: r = *(Float *)(lp+(Cell)NEXT_INST);
1916: INC_IP(1);
1917:
1918: f@local0 -- r new f_fetch_local_zero
1919: r = *(Float *)(lp+0*sizeof(Float));
1920:
1921: f@local1 -- r new f_fetch_local_eight
1922: r = *(Float *)(lp+1*sizeof(Float));
1923:
1924: \+[THEN]
1925:
1926: laddr# -- c_addr gforth laddr_number
1927: /* this can also be used to implement lp@ */
1928: c_addr = (Char *)(lp+(Cell)NEXT_INST);
1929: INC_IP(1);
1930:
1931: lp+!# -- gforth lp_plus_store_number
1932: ""used with negative immediate values it allocates memory on the
1933: local stack, a positive immediate argument drops memory from the local
1934: stack""
1935: lp += (Cell)NEXT_INST;
1936: INC_IP(1);
1937:
1938: lp- -- new minus_four_lp_plus_store
1939: lp += -sizeof(Cell);
1940:
1941: lp+ -- new eight_lp_plus_store
1942: lp += sizeof(Float);
1943:
1944: lp+2 -- new sixteen_lp_plus_store
1945: lp += 2*sizeof(Float);
1946:
1947: lp! c_addr -- gforth lp_store
1948: lp = (Address)c_addr;
1949:
1950: >l w -- gforth to_l
1951: lp -= sizeof(Cell);
1952: *(Cell *)lp = w;
1953:
1954: \+has-floats [IF]
1955:
1956: f>l r -- gforth f_to_l
1957: lp -= sizeof(Float);
1958: *(Float *)lp = r;
1959:
1960: \+[THEN] [THEN] \ has-locals
1961:
1962: \+has-OS [IF]
1963:
1964: define(`uploop',
1965: `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
1966: define(`_uploop',
1967: `ifelse($1, `$3', `$5',
1968: `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
1969: \ argflist(argnum): Forth argument list
1970: define(argflist,
1971: `ifelse($1, 0, `',
1972: `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
1973: \ argdlist(argnum): declare C's arguments
1974: define(argdlist,
1975: `ifelse($1, 0, `',
1976: `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
1977: \ argclist(argnum): pass C's arguments
1978: define(argclist,
1979: `ifelse($1, 0, `',
1980: `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
1981: \ icall(argnum)
1982: define(icall,
1983: `icall$1 argflist($1)u -- uret gforth
1984: uret = ((Cell(*)(argdlist($1)))u)(argclist($1));
1985:
1986: ')
1987: define(fcall,
1988: `fcall$1 argflist($1)u -- rret gforth
1989: rret = ((Float(*)(argdlist($1)))u)(argclist($1));
1990:
1991: ')
1992:
1993:
1994: open-lib c_addr1 u1 -- u2 gforth open_lib
1995: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
1996: u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_LAZY);
1997: #else
1998: # ifdef HAVE_LIBKERNEL32
1999: u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
2000: # else
2001: #warning Define open-lib!
2002: u2 = 0;
2003: # endif
2004: #endif
2005:
2006: lib-sym c_addr1 u1 u2 -- u3 gforth lib_sym
2007: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2008: u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
2009: #else
2010: # ifdef HAVE_LIBKERNEL32
2011: u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
2012: # else
2013: #warning Define lib-sym!
2014: u3 = 0;
2015: # endif
2016: #endif
2017:
2018: uploop(i, 0, 7, `icall(i)')
2019: icall(20)
2020: uploop(i, 0, 7, `fcall(i)')
2021: fcall(20)
2022:
2023: \+[THEN] \ has-OS
2024:
2025: up! a_addr -- gforth up_store
2026: UP=up=(char *)a_addr;
2027: :
2028: up ! ;
2029: Variable UP
2030:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>