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: \ digit is high-level: 0/0%
424:
425: move c_from c_to ucount -- core
426: memmove(c_to,c_from,ucount);
427: /* make an Ifdef for bsd and others? */
428: :
429: >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
430:
431: cmove c_from c_to u -- string
432: while (u-- > 0)
433: *c_to++ = *c_from++;
434: :
435: bounds ?DO dup c@ I c! 1+ LOOP drop ;
436:
437: cmove> c_from c_to u -- string c_move_up
438: while (u-- > 0)
439: c_to[u] = c_from[u];
440: :
441: dup 0= IF drop 2drop exit THEN
442: rot over + -rot bounds swap 1-
443: DO 1- dup c@ I c! -1 +LOOP drop ;
444:
445: fill c_addr u c -- core
446: memset(c_addr,c,u);
447: :
448: -rot bounds
449: ?DO dup I c! LOOP drop ;
450:
451: compare c_addr1 u1 c_addr2 u2 -- n string
452: ""Compare the strings lexicographically. If they are equal, n is 0; if
453: the first string is smaller, n is -1; if the first string is larger, n
454: is 1. Currently this is based on the machine's character
455: comparison. In the future, this may change to considering the current
456: locale and its collation order.""
457: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
458: if (n==0)
459: n = u1-u2;
460: if (n<0)
461: n = -1;
462: else if (n>0)
463: n = 1;
464: :
465: rot 2dup - >r min swap -text dup
466: IF rdrop
467: ELSE drop r@ 0>
468: IF rdrop -1
469: ELSE r> 1 and
470: THEN
471: THEN ;
472:
473: -text c_addr1 u c_addr2 -- n new dash_text
474: n = memcmp(c_addr1, c_addr2, u);
475: if (n<0)
476: n = -1;
477: else if (n>0)
478: n = 1;
479: :
480: swap bounds
481: ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
482: ELSE c@ I c@ - unloop THEN -text-flag ;
483: : -text-flag ( n -- -1/0/1 )
484: dup 0< IF drop -1 ELSE 0> 1 and THEN ;
485:
486: toupper c1 -- c2 gforth
487: c2 = toupper(c1);
488: :
489: dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
490:
491: capscomp c_addr1 u c_addr2 -- n new
492: n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
493: if (n<0)
494: n = -1;
495: else if (n>0)
496: n = 1;
497: :
498: swap bounds
499: ?DO dup c@ I c@ <>
500: IF dup c@ toupper I c@ toupper =
501: ELSE true THEN WHILE 1+ LOOP drop 0
502: ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
503:
504: -trailing c_addr u1 -- c_addr u2 string dash_trailing
505: u2 = u1;
506: while (c_addr[u2-1] == ' ')
507: u2--;
508: :
509: BEGIN 1- 2dup + c@ bl = WHILE
510: dup 0= UNTIL ELSE 1+ THEN ;
511:
512: /string c_addr1 u1 n -- c_addr2 u2 string slash_string
513: c_addr2 = c_addr1+n;
514: u2 = u1-n;
515: :
516: tuck - >r + r> dup 0< IF - 0 THEN ;
517:
518: + n1 n2 -- n core plus
519: n = n1+n2;
520:
521: \ PFE-0.9.14 has it differently, but the next release will have it as follows
522: under+ n1 n2 n3 -- n n2 gforth under_plus
523: ""add @var{n3} to @var{n1} (giving @var{n})""
524: n = n1+n3;
525: :
526: rot + swap ;
527:
528: - n1 n2 -- n core minus
529: n = n1-n2;
530: :
531: negate + ;
532:
533: negate n1 -- n2 core
534: /* use minus as alias */
535: n2 = -n1;
536: :
537: invert 1+ ;
538:
539: 1+ n1 -- n2 core one_plus
540: n2 = n1+1;
541: :
542: 1 + ;
543:
544: 1- n1 -- n2 core one_minus
545: n2 = n1-1;
546: :
547: 1 - ;
548:
549: max n1 n2 -- n core
550: if (n1<n2)
551: n = n2;
552: else
553: n = n1;
554: :
555: 2dup < IF swap THEN drop ;
556:
557: min n1 n2 -- n core
558: if (n1<n2)
559: n = n1;
560: else
561: n = n2;
562: :
563: 2dup > IF swap THEN drop ;
564:
565: abs n1 -- n2 core
566: if (n1<0)
567: n2 = -n1;
568: else
569: n2 = n1;
570: :
571: dup 0< IF negate THEN ;
572:
573: * n1 n2 -- n core star
574: n = n1*n2;
575: :
576: um* drop ;
577:
578: / n1 n2 -- n core slash
579: n = n1/n2;
580: :
581: /mod nip ;
582:
583: mod n1 n2 -- n core
584: n = n1%n2;
585: :
586: /mod drop ;
587:
588: /mod n1 n2 -- n3 n4 core slash_mod
589: n4 = n1/n2;
590: n3 = n1%n2; /* !! is this correct? look into C standard! */
591: :
592: >r s>d r> fm/mod ;
593:
594: 2* n1 -- n2 core two_star
595: n2 = 2*n1;
596: :
597: dup + ;
598:
599: 2/ n1 -- n2 core two_slash
600: /* !! is this still correct? */
601: n2 = n1>>1;
602:
603: fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
604: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
605: #ifdef BUGGY_LONG_LONG
606: DCell r = fmdiv(d1,n1);
607: n2=r.hi;
608: n3=r.lo;
609: #else
610: /* assumes that the processor uses either floored or symmetric division */
611: n3 = d1/n1;
612: n2 = d1%n1;
613: /* note that this 1%-3>0 is optimized by the compiler */
614: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
615: n3--;
616: n2+=n1;
617: }
618: #endif
619: :
620: dup >r dup 0< IF negate >r dnegate r> THEN
621: over 0< IF tuck + swap THEN
622: um/mod
623: r> 0< IF swap negate swap THEN ;
624:
625: sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
626: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
627: #ifdef BUGGY_LONG_LONG
628: DCell r = smdiv(d1,n1);
629: n2=r.hi;
630: n3=r.lo;
631: #else
632: /* assumes that the processor uses either floored or symmetric division */
633: n3 = d1/n1;
634: n2 = d1%n1;
635: /* note that this 1%-3<0 is optimized by the compiler */
636: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
637: n3++;
638: n2-=n1;
639: }
640: #endif
641: :
642: over >r dup >r abs -rot
643: dabs rot um/mod
644: r> r@ xor 0< IF negate THEN
645: r> 0< IF swap negate swap THEN ;
646:
647: m* n1 n2 -- d core m_star
648: #ifdef BUGGY_LONG_LONG
649: d = mmul(n1,n2);
650: #else
651: d = (DCell)n1 * (DCell)n2;
652: #endif
653: :
654: 2dup 0< and >r
655: 2dup swap 0< and >r
656: um* r> - r> - ;
657:
658: um* u1 u2 -- ud core u_m_star
659: /* use u* as alias */
660: #ifdef BUGGY_LONG_LONG
661: ud = ummul(u1,u2);
662: #else
663: ud = (UDCell)u1 * (UDCell)u2;
664: #endif
665: :
666: >r >r 0 0 r> r> [ 8 cells ] literal 0
667: DO
668: over >r dup >r 0< and d2*+ drop
669: r> 2* r> swap
670: LOOP 2drop ;
671: : d2*+ ( ud n -- ud+n c )
672: over MINI
673: and >r >r 2dup d+ swap r> + swap r> ;
674:
675: um/mod ud u1 -- u2 u3 core u_m_slash_mod
676: #ifdef BUGGY_LONG_LONG
677: UDCell r = umdiv(ud,u1);
678: u2=r.hi;
679: u3=r.lo;
680: #else
681: u3 = ud/u1;
682: u2 = ud%u1;
683: #endif
684: :
685: 0 swap [ 8 cells 1 + ] literal 0
686: ?DO >r /modstep r>
687: LOOP drop swap 1 rshift or swap ;
688: : /modstep ( ud c R: u -- ud-?u c R: u )
689: over I' u< 0= or IF I' - 1 ELSE 0 THEN d2*+ ;
690: : d2*+ ( ud n -- ud+n c )
691: over MINI
692: and >r >r 2dup d+ swap r> + swap r> ;
693:
694: m+ d1 n -- d2 double m_plus
695: #ifdef BUGGY_LONG_LONG
696: d2.lo = d1.lo+n;
697: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
698: #else
699: d2 = d1+n;
700: #endif
701: :
702: s>d d+ ;
703:
704: d+ d1 d2 -- d double d_plus
705: #ifdef BUGGY_LONG_LONG
706: d.lo = d1.lo+d2.lo;
707: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
708: #else
709: d = d1+d2;
710: #endif
711: :
712: rot + >r tuck + swap over u> r> swap - ;
713:
714: d- d1 d2 -- d double d_minus
715: #ifdef BUGGY_LONG_LONG
716: d.lo = d1.lo - d2.lo;
717: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
718: #else
719: d = d1-d2;
720: #endif
721: :
722: dnegate d+ ;
723:
724: dnegate d1 -- d2 double
725: /* use dminus as alias */
726: #ifdef BUGGY_LONG_LONG
727: d2 = dnegate(d1);
728: #else
729: d2 = -d1;
730: #endif
731: :
732: invert swap negate tuck 0= - ;
733:
734: d2* d1 -- d2 double d_two_star
735: #ifdef BUGGY_LONG_LONG
736: d2.lo = d1.lo<<1;
737: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
738: #else
739: d2 = 2*d1;
740: #endif
741: :
742: 2dup d+ ;
743:
744: d2/ d1 -- d2 double d_two_slash
745: #ifdef BUGGY_LONG_LONG
746: d2.hi = d1.hi>>1;
747: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
748: #else
749: d2 = d1>>1;
750: #endif
751: :
752: dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
753: r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
754:
755: and w1 w2 -- w core
756: w = w1&w2;
757:
758: or w1 w2 -- w core
759: w = w1|w2;
760:
761: xor w1 w2 -- w core
762: w = w1^w2;
763:
764: invert w1 -- w2 core
765: w2 = ~w1;
766: :
767: -1 xor ;
768:
769: rshift u1 n -- u2 core
770: u2 = u1>>n;
771: :
772: 0 ?DO 2/ MAXI and LOOP ;
773:
774: lshift u1 n -- u2 core
775: u2 = u1<<n;
776: :
777: 0 ?DO 2* LOOP ;
778:
779: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
780: define(comparisons,
781: $1= $2 -- f $6 $3equals
782: f = FLAG($4==$5);
783: :
784: [ char $1x char 0 = [IF]
785: ] IF false ELSE true THEN [
786: [ELSE]
787: ] xor 0= [
788: [THEN] ] ;
789:
790: $1<> $2 -- f $7 $3different
791: f = FLAG($4!=$5);
792: :
793: [ char $1x char 0 = [IF]
794: ] IF true ELSE false THEN [
795: [ELSE]
796: ] xor 0<> [
797: [THEN] ] ;
798:
799: $1< $2 -- f $8 $3less
800: f = FLAG($4<$5);
801: :
802: [ char $1x char 0 = [IF]
803: ] MINI and 0<> [
804: [ELSE] char $1x char u = [IF]
805: ] 2dup xor 0< IF nip ELSE - THEN 0< [
806: [ELSE]
807: ] MINI xor >r MINI xor r> u< [
808: [THEN]
809: [THEN] ] ;
810:
811: $1> $2 -- f $9 $3greater
812: f = FLAG($4>$5);
813: :
814: [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
815: $1< ;
816:
817: $1<= $2 -- f gforth $3less_or_equal
818: f = FLAG($4<=$5);
819: :
820: $1> 0= ;
821:
822: $1>= $2 -- f gforth $3greater_or_equal
823: f = FLAG($4>=$5);
824: :
825: [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
826: $1<= ;
827:
828: )
829:
830: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
831: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
832: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
833:
834: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
835: define(dcomparisons,
836: $1= $2 -- f $6 $3equals
837: #ifdef BUGGY_LONG_LONG
838: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
839: #else
840: f = FLAG($4==$5);
841: #endif
842:
843: $1<> $2 -- f $7 $3different
844: #ifdef BUGGY_LONG_LONG
845: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
846: #else
847: f = FLAG($4!=$5);
848: #endif
849:
850: $1< $2 -- f $8 $3less
851: #ifdef BUGGY_LONG_LONG
852: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
853: #else
854: f = FLAG($4<$5);
855: #endif
856:
857: $1> $2 -- f $9 $3greater
858: #ifdef BUGGY_LONG_LONG
859: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
860: #else
861: f = FLAG($4>$5);
862: #endif
863:
864: $1<= $2 -- f gforth $3less_or_equal
865: #ifdef BUGGY_LONG_LONG
866: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
867: #else
868: f = FLAG($4<=$5);
869: #endif
870:
871: $1>= $2 -- f gforth $3greater_or_equal
872: #ifdef BUGGY_LONG_LONG
873: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
874: #else
875: f = FLAG($4>=$5);
876: #endif
877:
878: )
879:
880: \+has-dcomps [IF]
881:
882: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
883: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
884: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
885:
886: \+[THEN]
887:
888: within u1 u2 u3 -- f core-ext
889: f = FLAG(u1-u2 < u3-u2);
890: :
891: over - >r - r> u< ;
892:
893: sp@ -- a_addr gforth spat
894: a_addr = sp+1;
895:
896: sp! a_addr -- gforth spstore
897: sp = a_addr;
898: /* works with and without TOS caching */
899:
900: rp@ -- a_addr gforth rpat
901: a_addr = rp;
902:
903: rp! a_addr -- gforth rpstore
904: rp = a_addr;
905:
906: \+has-floats [IF]
907:
908: fp@ -- f_addr gforth fp_fetch
909: f_addr = fp;
910:
911: fp! f_addr -- gforth fp_store
912: fp = f_addr;
913:
914: \+[THEN]
915:
916: ;s -- gforth semis
917: ip = (Xt *)(*rp++);
918: NEXT_P0;
919:
920: >r w -- core to_r
921: *--rp = w;
922: :
923: (>r) ;
924: : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
925:
926: r> -- w core r_from
927: w = *rp++;
928: :
929: rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
930: Create (rdrop) ' ;s A,
931:
932: rdrop -- gforth
933: rp++;
934: :
935: r> r> drop >r ;
936:
937: 2>r w1 w2 -- core-ext two_to_r
938: *--rp = w1;
939: *--rp = w2;
940: :
941: swap r> swap >r swap >r >r ;
942:
943: 2r> -- w1 w2 core-ext two_r_from
944: w2 = *rp++;
945: w1 = *rp++;
946: :
947: r> r> swap r> swap >r swap ;
948:
949: 2r@ -- w1 w2 core-ext two_r_fetch
950: w2 = rp[0];
951: w1 = rp[1];
952: :
953: i' j ;
954:
955: 2rdrop -- gforth two_r_drop
956: rp+=2;
957: :
958: r> r> drop r> drop >r ;
959:
960: over w1 w2 -- w1 w2 w1 core
961: :
962: sp@ cell+ @ ;
963:
964: drop w -- core
965: :
966: IF THEN ;
967:
968: swap w1 w2 -- w2 w1 core
969: :
970: >r (swap) ! r> (swap) @ ;
971: Variable (swap)
972:
973: dup w -- w w core
974: :
975: sp@ @ ;
976:
977: rot w1 w2 w3 -- w2 w3 w1 core rote
978: :
979: [ defined? (swap) [IF] ]
980: (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
981: Variable (rot)
982: [ELSE] ]
983: >r swap r> swap ;
984: [THEN]
985:
986: -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
987: :
988: rot rot ;
989:
990: nip w1 w2 -- w2 core-ext
991: :
992: >r drop r> ;
993:
994: tuck w1 w2 -- w2 w1 w2 core-ext
995: :
996: swap over ;
997:
998: ?dup w -- w core question_dupe
999: if (w!=0) {
1000: IF_TOS(*sp-- = w;)
1001: #ifndef USE_TOS
1002: *--sp = w;
1003: #endif
1004: }
1005: :
1006: dup IF dup THEN ;
1007:
1008: pick u -- w core-ext
1009: w = sp[u+1];
1010: :
1011: 1+ cells sp@ + @ ;
1012:
1013: 2drop w1 w2 -- core two_drop
1014: :
1015: drop drop ;
1016:
1017: 2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
1018: :
1019: over over ;
1020:
1021: 2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
1022: :
1023: 3 pick 3 pick ;
1024:
1025: 2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
1026: :
1027: rot >r rot r> ;
1028:
1029: 2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double-ext two_rote
1030: :
1031: >r >r 2swap r> r> 2swap ;
1032:
1033: 2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
1034: :
1035: 2swap 2drop ;
1036:
1037: 2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
1038: :
1039: 2swap 2over ;
1040:
1041: \ toggle is high-level: 0.11/0.42%
1042:
1043: @ a_addr -- w core fetch
1044: w = *a_addr;
1045:
1046: ! w a_addr -- core store
1047: *a_addr = w;
1048:
1049: +! n a_addr -- core plus_store
1050: *a_addr += n;
1051: :
1052: tuck @ + swap ! ;
1053:
1054: c@ c_addr -- c core cfetch
1055: c = *c_addr;
1056: :
1057: [ bigendian [IF] ]
1058: [ cell>bit 4 = [IF] ]
1059: dup [ 0 cell - ] Literal and @ swap 1 and
1060: IF $FF and ELSE 8>> THEN ;
1061: [ [ELSE] ]
1062: dup [ cell 1- ] literal and
1063: tuck - @ swap [ cell 1- ] literal xor
1064: 0 ?DO 8>> LOOP $FF and
1065: [ [THEN] ]
1066: [ [ELSE] ]
1067: [ cell>bit 4 = [IF] ]
1068: dup [ 0 cell - ] Literal and @ swap 1 and
1069: IF 8>> ELSE $FF and THEN
1070: [ [ELSE] ]
1071: dup [ cell 1- ] literal and
1072: tuck - @ swap
1073: 0 ?DO 8>> LOOP 255 and
1074: [ [THEN] ]
1075: [ [THEN] ]
1076: ;
1077: : 8>> 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ ;
1078:
1079: c! c c_addr -- core cstore
1080: *c_addr = c;
1081: :
1082: [ bigendian [IF] ]
1083: [ cell>bit 4 = [IF] ]
1084: tuck 1 and IF $FF and ELSE 8<< THEN >r
1085: dup -2 and @ over 1 and cells masks + @ and
1086: r> or swap -2 and ! ;
1087: Create masks $00FF , $FF00 ,
1088: [ELSE] ]
1089: dup [ cell 1- ] literal and dup
1090: [ cell 1- ] literal xor >r
1091: - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1092: rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1093: [THEN]
1094: [ELSE] ]
1095: [ cell>bit 4 = [IF] ]
1096: tuck 1 and IF 8<< ELSE $FF and THEN >r
1097: dup -2 and @ over 1 and cells masks + @ and
1098: r> or swap -2 and ! ;
1099: Create masks $FF00 , $00FF ,
1100: [ELSE] ]
1101: dup [ cell 1- ] literal and dup >r
1102: - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1103: rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1104: [THEN]
1105: [THEN]
1106: : 8<< 2* 2* 2* 2* 2* 2* 2* 2* ;
1107:
1108: 2! w1 w2 a_addr -- core two_store
1109: a_addr[0] = w2;
1110: a_addr[1] = w1;
1111: :
1112: tuck ! cell+ ! ;
1113:
1114: 2@ a_addr -- w1 w2 core two_fetch
1115: w2 = a_addr[0];
1116: w1 = a_addr[1];
1117: :
1118: dup cell+ @ swap @ ;
1119:
1120: cell+ a_addr1 -- a_addr2 core cell_plus
1121: a_addr2 = a_addr1+1;
1122: :
1123: cell + ;
1124:
1125: cells n1 -- n2 core
1126: n2 = n1 * sizeof(Cell);
1127: :
1128: [ cell
1129: 2/ dup [IF] ] 2* [ [THEN]
1130: 2/ dup [IF] ] 2* [ [THEN]
1131: 2/ dup [IF] ] 2* [ [THEN]
1132: 2/ dup [IF] ] 2* [ [THEN]
1133: drop ] ;
1134:
1135: char+ c_addr1 -- c_addr2 core care_plus
1136: c_addr2 = c_addr1 + 1;
1137: :
1138: 1+ ;
1139:
1140: (chars) n1 -- n2 gforth paren_cares
1141: n2 = n1 * sizeof(Char);
1142: :
1143: ;
1144:
1145: count c_addr1 -- c_addr2 u core
1146: u = *c_addr1;
1147: c_addr2 = c_addr1+1;
1148: :
1149: dup 1+ swap c@ ;
1150:
1151: (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
1152: for (; f83name1 != NULL; f83name1 = f83name1->next)
1153: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1154: memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1155: break;
1156: f83name2=f83name1;
1157: :
1158: BEGIN dup WHILE (find-samelen) dup WHILE
1159: >r 2dup r@ cell+ char+ capscomp 0=
1160: IF 2drop r> EXIT THEN
1161: r> @
1162: REPEAT THEN nip nip ;
1163: : (find-samelen) ( u f83name1 -- u f83name2/0 )
1164: BEGIN 2dup cell+ c@ $1F and <> WHILE @ dup 0= UNTIL THEN ;
1165:
1166: \+has-hash [IF]
1167:
1168: (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
1169: F83Name *f83name1;
1170: f83name2=NULL;
1171: while(a_addr != NULL)
1172: {
1173: f83name1=(F83Name *)(a_addr[1]);
1174: a_addr=(Cell *)(a_addr[0]);
1175: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1176: memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1177: {
1178: f83name2=f83name1;
1179: break;
1180: }
1181: }
1182: :
1183: BEGIN dup WHILE
1184: 2@ >r >r dup r@ cell+ c@ $1F and =
1185: IF 2dup r@ cell+ char+ capscomp 0=
1186: IF 2drop r> rdrop EXIT THEN THEN
1187: rdrop r>
1188: REPEAT nip nip ;
1189:
1190: (tablefind) c_addr u a_addr -- f83name2 new paren_tablefind
1191: ""A case-sensitive variant of @code{(hashfind)}""
1192: F83Name *f83name1;
1193: f83name2=NULL;
1194: while(a_addr != NULL)
1195: {
1196: f83name1=(F83Name *)(a_addr[1]);
1197: a_addr=(Cell *)(a_addr[0]);
1198: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1199: memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1200: {
1201: f83name2=f83name1;
1202: break;
1203: }
1204: }
1205: :
1206: BEGIN dup WHILE
1207: 2@ >r >r dup r@ cell+ c@ $1F and =
1208: IF 2dup r@ cell+ char+ -text 0=
1209: IF 2drop r> rdrop EXIT THEN THEN
1210: rdrop r>
1211: REPEAT nip nip ;
1212:
1213: (hashkey) c_addr u1 -- u2 gforth paren_hashkey
1214: u2=0;
1215: while(u1--)
1216: u2+=(Cell)toupper(*c_addr++);
1217: :
1218: 0 -rot bounds ?DO I c@ toupper + LOOP ;
1219:
1220: (hashkey1) c_addr u ubits -- ukey gforth paren_hashkey1
1221: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1222: /* this hash function rotates the key at every step by rot bits within
1223: ubits bits and xors it with the character. This function does ok in
1224: the chi-sqare-test. Rot should be <=7 (preferably <=5) for
1225: ASCII strings (larger if ubits is large), and should share no
1226: divisors with ubits.
1227: */
1228: 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];
1229: Char *cp = c_addr;
1230: for (ukey=0; cp<c_addr+u; cp++)
1231: ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
1232: ^ toupper(*cp))
1233: & ((1<<ubits)-1));
1234: :
1235: dup rot-values + c@ over 1 swap lshift 1- >r
1236: tuck - 2swap r> 0 2swap bounds
1237: ?DO dup 4 pick lshift swap 3 pick rshift or
1238: I c@ toupper xor
1239: over and LOOP
1240: nip nip nip ;
1241: Create rot-values
1242: 5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
1243: 3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
1244: 7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1245: 7 c, 5 c, 5 c,
1246:
1247: \+[THEN]
1248:
1249: (parse-white) c_addr1 u1 -- c_addr2 u2 gforth paren_parse_white
1250: /* use !isgraph instead of isspace? */
1251: Char *endp = c_addr1+u1;
1252: while (c_addr1<endp && isspace(*c_addr1))
1253: c_addr1++;
1254: if (c_addr1<endp) {
1255: for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1256: ;
1257: u2 = c_addr1-c_addr2;
1258: }
1259: else {
1260: c_addr2 = c_addr1;
1261: u2 = 0;
1262: }
1263: :
1264: BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1265: REPEAT THEN 2dup
1266: BEGIN dup WHILE over c@ bl > WHILE 1 /string
1267: REPEAT THEN nip - ;
1268:
1269: aligned c_addr -- a_addr core
1270: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1271: :
1272: [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1273:
1274: faligned c_addr -- f_addr float f_aligned
1275: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1276: :
1277: [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1278:
1279: >body xt -- a_addr core to_body
1280: a_addr = PFA(xt);
1281: :
1282: 2 cells + ;
1283:
1284: >code-address xt -- c_addr gforth to_code_address
1285: ""c_addr is the code address of the word xt""
1286: /* !! This behaves installation-dependently for DOES-words */
1287: c_addr = CODE_ADDRESS(xt);
1288: :
1289: @ ;
1290:
1291: >does-code xt -- a_addr gforth to_does_code
1292: ""If xt ist the execution token of a defining-word-defined word,
1293: a_addr is the start of the Forth code after the DOES>;
1294: Otherwise a_addr is 0.""
1295: a_addr = (Cell *)DOES_CODE(xt);
1296: :
1297: cell+ @ ;
1298:
1299: code-address! c_addr xt -- gforth code_address_store
1300: ""Creates a code field with code address c_addr at xt""
1301: MAKE_CF(xt, c_addr);
1302: CACHE_FLUSH(xt,PFA(0));
1303: :
1304: ! ;
1305:
1306: does-code! a_addr xt -- gforth does_code_store
1307: ""creates a code field at xt for a defining-word-defined word; a_addr
1308: is the start of the Forth code after DOES>""
1309: MAKE_DOES_CF(xt, a_addr);
1310: CACHE_FLUSH(xt,PFA(0));
1311: :
1312: dodoes: over ! cell+ ! ;
1313:
1314: does-handler! a_addr -- gforth does_handler_store
1315: ""creates a DOES>-handler at address a_addr. a_addr usually points
1316: just behind a DOES>.""
1317: MAKE_DOES_HANDLER(a_addr);
1318: CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1319: :
1320: drop ;
1321:
1322: /does-handler -- n gforth slash_does_handler
1323: ""the size of a does-handler (includes possible padding)""
1324: /* !! a constant or environmental query might be better */
1325: n = DOES_HANDLER_SIZE;
1326: :
1327: 2 cells ;
1328:
1329: threading-method -- n gforth threading_method
1330: ""0 if the engine is direct threaded.""
1331: #if defined(DIRECT_THREADED)
1332: n=0;
1333: #else
1334: n=1;
1335: #endif
1336: :
1337: 1 ;
1338:
1339: \+has-os [IF]
1340:
1341: (key) -- n gforth paren_key
1342: fflush(stdout);
1343: /* !! noecho */
1344: n = key();
1345:
1346: key? -- n facility key_q
1347: fflush(stdout);
1348: n = key_query;
1349:
1350: stdout -- wfileid gforth
1351: wfileid = (Cell)stdout;
1352:
1353: stderr -- wfileid gforth
1354: wfileid = (Cell)stderr;
1355:
1356: form -- urows ucols gforth
1357: ""The number of lines and columns in the terminal. These numbers may change
1358: with the window size.""
1359: /* we could block SIGWINCH here to get a consistent size, but I don't
1360: think this is necessary or always beneficial */
1361: urows=rows;
1362: ucols=cols;
1363:
1364: flush-icache c_addr u -- gforth flush_icache
1365: ""Make sure that the instruction cache of the processor (if there is
1366: one) does not contain stale data at @var{c_addr} and @var{u} bytes
1367: afterwards. @code{END-CODE} performs a @code{flush-icache}
1368: automatically. Caveat: @code{flush-icache} might not work on your
1369: installation; this is usually the case if direct threading is not
1370: supported on your machine (take a look at your @file{machine.h}) and
1371: your machine has a separate instruction cache. In such cases,
1372: @code{flush-icache} does nothing instead of flushing the instruction
1373: cache.""
1374: FLUSH_ICACHE(c_addr,u);
1375:
1376: (bye) n -- gforth paren_bye
1377: return (Label *)n;
1378:
1379: (system) c_addr u -- wretval wior gforth peren_system
1380: int old_tp=terminal_prepped;
1381: deprep_terminal();
1382: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1383: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1384: if (old_tp)
1385: prep_terminal();
1386:
1387: getenv c_addr1 u1 -- c_addr2 u2 gforth
1388: c_addr2 = getenv(cstr(c_addr1,u1,1));
1389: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1390:
1391: open-pipe c_addr u ntype -- wfileid wior gforth open_pipe
1392: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
1393: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1394:
1395: close-pipe wfileid -- wretval wior gforth close_pipe
1396: wretval = pclose((FILE *)wfileid);
1397: wior = IOR(wretval==-1);
1398:
1399: time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
1400: struct timeval time1;
1401: struct timezone zone1;
1402: struct tm *ltime;
1403: gettimeofday(&time1,&zone1);
1404: ltime=localtime((time_t *)&time1.tv_sec);
1405: nyear =ltime->tm_year+1900;
1406: nmonth=ltime->tm_mon+1;
1407: nday =ltime->tm_mday;
1408: nhour =ltime->tm_hour;
1409: nmin =ltime->tm_min;
1410: nsec =ltime->tm_sec;
1411:
1412: ms n -- facility-ext
1413: struct timeval timeout;
1414: timeout.tv_sec=n/1000;
1415: timeout.tv_usec=1000*(n%1000);
1416: (void)select(0,0,0,0,&timeout);
1417:
1418: allocate u -- a_addr wior memory
1419: a_addr = (Cell *)malloc(u?u:1);
1420: wior = IOR(a_addr==NULL);
1421:
1422: free a_addr -- wior memory
1423: free(a_addr);
1424: wior = 0;
1425:
1426: resize a_addr1 u -- a_addr2 wior memory
1427: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
1428: address units, possibly moving the contents to a different
1429: area. @i{a_addr2} is the address of the resulting area. If
1430: @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
1431: @code{allocate}s @i{u} address units.""
1432: /* the following check is not necessary on most OSs, but it is needed
1433: on SunOS 4.1.2. */
1434: if (a_addr1==NULL)
1435: a_addr2 = (Cell *)malloc(u);
1436: else
1437: a_addr2 = (Cell *)realloc(a_addr1, u);
1438: wior = IOR(a_addr2==NULL); /* !! Define a return code */
1439:
1440: strerror n -- c_addr u gforth
1441: c_addr = strerror(n);
1442: u = strlen(c_addr);
1443:
1444: strsignal n -- c_addr u gforth
1445: c_addr = strsignal(n);
1446: u = strlen(c_addr);
1447:
1448: call-c w -- gforth call_c
1449: ""Call the C function pointed to by @i{w}. The C function has to
1450: access the stack itself. The stack pointers are exported in the global
1451: variables @code{SP} and @code{FP}.""
1452: /* This is a first attempt at support for calls to C. This may change in
1453: the future */
1454: IF_FTOS(fp[0]=FTOS);
1455: FP=fp;
1456: SP=sp;
1457: ((void (*)())w)();
1458: sp=SP;
1459: fp=FP;
1460: IF_TOS(TOS=sp[0]);
1461: IF_FTOS(FTOS=fp[0]);
1462:
1463: \+[THEN] ( has-os ) has-files [IF]
1464:
1465: close-file wfileid -- wior file close_file
1466: wior = IOR(fclose((FILE *)wfileid)==EOF);
1467:
1468: open-file c_addr u ntype -- w2 wior file open_file
1469: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1470: wior = IOR(w2 == 0);
1471:
1472: create-file c_addr u ntype -- w2 wior file create_file
1473: Cell fd;
1474: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_RDWR|O_TRUNC, 0666);
1475: if (fd != -1) {
1476: w2 = (Cell)fdopen(fd, fileattr[ntype]);
1477: wior = IOR(w2 == 0);
1478: } else {
1479: w2 = 0;
1480: wior = IOR(1);
1481: }
1482:
1483: delete-file c_addr u -- wior file delete_file
1484: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1485:
1486: rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1487: char *s1=tilde_cstr(c_addr2, u2, 1);
1488: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1489:
1490: file-position wfileid -- ud wior file file_position
1491: /* !! use tell and lseek? */
1492: ud = LONG2UD(ftell((FILE *)wfileid));
1493: wior = IOR(UD2LONG(ud)==-1);
1494:
1495: reposition-file ud wfileid -- wior file reposition_file
1496: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1497:
1498: file-size wfileid -- ud wior file file_size
1499: struct stat buf;
1500: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1501: ud = LONG2UD(buf.st_size);
1502:
1503: resize-file ud wfileid -- wior file resize_file
1504: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1505:
1506: read-file c_addr u1 wfileid -- u2 wior file read_file
1507: /* !! fread does not guarantee enough */
1508: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1509: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1510: /* !! is the value of ferror errno-compatible? */
1511: if (wior)
1512: clearerr((FILE *)wfileid);
1513:
1514: read-line c_addr u1 wfileid -- u2 flag wior file read_line
1515: /*
1516: Cell c;
1517: flag=-1;
1518: for(u2=0; u2<u1; u2++)
1519: {
1520: *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1521: if(c=='\n') break;
1522: if(c==EOF)
1523: {
1524: flag=FLAG(u2!=0);
1525: break;
1526: }
1527: }
1528: wior=FILEIO(ferror((FILE *)wfileid));
1529: */
1530: if ((flag=FLAG(!feof((FILE *)wfileid) &&
1531: fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1532: wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1533: if (wior)
1534: clearerr((FILE *)wfileid);
1535: u2 = strlen(c_addr);
1536: u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1537: }
1538: else {
1539: wior=0;
1540: u2=0;
1541: }
1542:
1543: \+[THEN] has-files [IF] -1 [ELSE] has-os [THEN] [IF]
1544:
1545: write-file c_addr u1 wfileid -- wior file write_file
1546: /* !! fwrite does not guarantee enough */
1547: {
1548: UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1549: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1550: if (wior)
1551: clearerr((FILE *)wfileid);
1552: }
1553:
1554: emit-file c wfileid -- wior gforth emit_file
1555: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1556: if (wior)
1557: clearerr((FILE *)wfileid);
1558:
1559: \+[THEN] has-files [IF]
1560:
1561: flush-file wfileid -- wior file-ext flush_file
1562: wior = IOR(fflush((FILE *) wfileid)==EOF);
1563:
1564: file-status c_addr u -- ntype wior file-ext file_status
1565: char *filename=tilde_cstr(c_addr, u, 1);
1566: if (access (filename, F_OK) != 0) {
1567: ntype=0;
1568: wior=IOR(1);
1569: }
1570: else if (access (filename, R_OK | W_OK) == 0) {
1571: ntype=2; /* r/w */
1572: wior=0;
1573: }
1574: else if (access (filename, R_OK) == 0) {
1575: ntype=0; /* r/o */
1576: wior=0;
1577: }
1578: else if (access (filename, W_OK) == 0) {
1579: ntype=4; /* w/o */
1580: wior=0;
1581: }
1582: else {
1583: ntype=1; /* well, we cannot access the file, but better deliver a legal
1584: access mode (r/o bin), so we get a decent error later upon open. */
1585: wior=0;
1586: }
1587:
1588: \+[THEN] ( has-files ) has-floats [IF]
1589:
1590: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1591: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1592:
1593: d>f d -- r float d_to_f
1594: #ifdef BUGGY_LONG_LONG
1595: extern double ldexp(double x, int exp);
1596: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
1597: #else
1598: r = d;
1599: #endif
1600:
1601: f>d r -- d float f_to_d
1602: #ifdef BUGGY_LONG_LONG
1603: d.hi = ldexp(r,-CELL_BITS) - (r<0);
1604: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1605: #else
1606: d = r;
1607: #endif
1608:
1609: f! r f_addr -- float f_store
1610: *f_addr = r;
1611:
1612: f@ f_addr -- r float f_fetch
1613: r = *f_addr;
1614:
1615: df@ df_addr -- r float-ext d_f_fetch
1616: #ifdef IEEE_FP
1617: r = *df_addr;
1618: #else
1619: !! df@
1620: #endif
1621:
1622: df! r df_addr -- float-ext d_f_store
1623: #ifdef IEEE_FP
1624: *df_addr = r;
1625: #else
1626: !! df!
1627: #endif
1628:
1629: sf@ sf_addr -- r float-ext s_f_fetch
1630: #ifdef IEEE_FP
1631: r = *sf_addr;
1632: #else
1633: !! sf@
1634: #endif
1635:
1636: sf! r sf_addr -- float-ext s_f_store
1637: #ifdef IEEE_FP
1638: *sf_addr = r;
1639: #else
1640: !! sf!
1641: #endif
1642:
1643: f+ r1 r2 -- r3 float f_plus
1644: r3 = r1+r2;
1645:
1646: f- r1 r2 -- r3 float f_minus
1647: r3 = r1-r2;
1648:
1649: f* r1 r2 -- r3 float f_star
1650: r3 = r1*r2;
1651:
1652: f/ r1 r2 -- r3 float f_slash
1653: r3 = r1/r2;
1654:
1655: f** r1 r2 -- r3 float-ext f_star_star
1656: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1657: r3 = pow(r1,r2);
1658:
1659: fnegate r1 -- r2 float
1660: r2 = - r1;
1661:
1662: fdrop r -- float
1663:
1664: fdup r -- r r float
1665:
1666: fswap r1 r2 -- r2 r1 float
1667:
1668: fover r1 r2 -- r1 r2 r1 float
1669:
1670: frot r1 r2 r3 -- r2 r3 r1 float
1671:
1672: fnip r1 r2 -- r2 gforth
1673:
1674: ftuck r1 r2 -- r2 r1 r2 gforth
1675:
1676: float+ f_addr1 -- f_addr2 float float_plus
1677: f_addr2 = f_addr1+1;
1678:
1679: floats n1 -- n2 float
1680: n2 = n1*sizeof(Float);
1681:
1682: floor r1 -- r2 float
1683: ""round towards the next smaller integral value, i.e., round toward negative infinity""
1684: /* !! unclear wording */
1685: r2 = floor(r1);
1686:
1687: fround r1 -- r2 float
1688: ""round to the nearest integral value""
1689: /* !! unclear wording */
1690: #ifdef HAVE_RINT
1691: r2 = rint(r1);
1692: #else
1693: r2 = floor(r1+0.5);
1694: /* !! This is not quite true to the rounding rules given in the standard */
1695: #endif
1696:
1697: fmax r1 r2 -- r3 float
1698: if (r1<r2)
1699: r3 = r2;
1700: else
1701: r3 = r1;
1702:
1703: fmin r1 r2 -- r3 float
1704: if (r1<r2)
1705: r3 = r1;
1706: else
1707: r3 = r2;
1708:
1709: represent r c_addr u -- n f1 f2 float
1710: char *sig;
1711: Cell flag;
1712: Cell decpt;
1713: sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
1714: n=(r==0 ? 1 : decpt);
1715: f1=FLAG(flag!=0);
1716: f2=FLAG(isdigit(sig[0])!=0);
1717: memmove(c_addr,sig,u);
1718:
1719: >float c_addr u -- flag float to_float
1720: /* real signature: c_addr u -- r t / f */
1721: Float r;
1722: char *number=cstr(c_addr, u, 1);
1723: char *endconv;
1724: while(isspace(number[--u]) && u>0);
1725: switch(number[u])
1726: {
1727: case 'd':
1728: case 'D':
1729: case 'e':
1730: case 'E': break;
1731: default : u++; break;
1732: }
1733: number[u]='\0';
1734: r=strtod(number,&endconv);
1735: if((flag=FLAG(!(Cell)*endconv)))
1736: {
1737: IF_FTOS(fp[0] = FTOS);
1738: fp += -1;
1739: FTOS = r;
1740: }
1741: else if(*endconv=='d' || *endconv=='D')
1742: {
1743: *endconv='E';
1744: r=strtod(number,&endconv);
1745: if((flag=FLAG(!(Cell)*endconv)))
1746: {
1747: IF_FTOS(fp[0] = FTOS);
1748: fp += -1;
1749: FTOS = r;
1750: }
1751: }
1752:
1753: fabs r1 -- r2 float-ext
1754: r2 = fabs(r1);
1755:
1756: facos r1 -- r2 float-ext
1757: r2 = acos(r1);
1758:
1759: fasin r1 -- r2 float-ext
1760: r2 = asin(r1);
1761:
1762: fatan r1 -- r2 float-ext
1763: r2 = atan(r1);
1764:
1765: fatan2 r1 r2 -- r3 float-ext
1766: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1767: intends this to be the inverse of @code{fsincos}. In gforth it is.""
1768: r3 = atan2(r1,r2);
1769:
1770: fcos r1 -- r2 float-ext
1771: r2 = cos(r1);
1772:
1773: fexp r1 -- r2 float-ext
1774: r2 = exp(r1);
1775:
1776: fexpm1 r1 -- r2 float-ext
1777: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1778: #ifdef HAVE_EXPM1
1779: extern double expm1(double);
1780: r2 = expm1(r1);
1781: #else
1782: r2 = exp(r1)-1.;
1783: #endif
1784:
1785: fln r1 -- r2 float-ext
1786: r2 = log(r1);
1787:
1788: flnp1 r1 -- r2 float-ext
1789: ""@i{r2}=ln(@i{r1}+1)""
1790: #ifdef HAVE_LOG1P
1791: extern double log1p(double);
1792: r2 = log1p(r1);
1793: #else
1794: r2 = log(r1+1.);
1795: #endif
1796:
1797: flog r1 -- r2 float-ext
1798: ""the decimal logarithm""
1799: r2 = log10(r1);
1800:
1801: falog r1 -- r2 float-ext
1802: ""@i{r2}=10**@i{r1}""
1803: extern double pow10(double);
1804: r2 = pow10(r1);
1805:
1806: fsin r1 -- r2 float-ext
1807: r2 = sin(r1);
1808:
1809: fsincos r1 -- r2 r3 float-ext
1810: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1811: r2 = sin(r1);
1812: r3 = cos(r1);
1813:
1814: fsqrt r1 -- r2 float-ext
1815: r2 = sqrt(r1);
1816:
1817: ftan r1 -- r2 float-ext
1818: r2 = tan(r1);
1819: :
1820: fsincos f/ ;
1821:
1822: fsinh r1 -- r2 float-ext
1823: r2 = sinh(r1);
1824: :
1825: fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1826:
1827: fcosh r1 -- r2 float-ext
1828: r2 = cosh(r1);
1829: :
1830: fexp fdup 1/f f+ f2/ ;
1831:
1832: ftanh r1 -- r2 float-ext
1833: r2 = tanh(r1);
1834: :
1835: f2* fexpm1 fdup 2. d>f f+ f/ ;
1836:
1837: fasinh r1 -- r2 float-ext
1838: r2 = asinh(r1);
1839: :
1840: fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1841:
1842: facosh r1 -- r2 float-ext
1843: r2 = acosh(r1);
1844: :
1845: fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1846:
1847: fatanh r1 -- r2 float-ext
1848: r2 = atanh(r1);
1849: :
1850: fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1851: r> IF fnegate THEN ;
1852:
1853: sfloats n1 -- n2 float-ext s_floats
1854: n2 = n1*sizeof(SFloat);
1855:
1856: dfloats n1 -- n2 float-ext d_floats
1857: n2 = n1*sizeof(DFloat);
1858:
1859: sfaligned c_addr -- sf_addr float-ext s_f_aligned
1860: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1861: :
1862: [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1863:
1864: dfaligned c_addr -- df_addr float-ext d_f_aligned
1865: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1866: :
1867: [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1868:
1869: \ The following words access machine/OS/installation-dependent
1870: \ Gforth internals
1871: \ !! how about environmental queries DIRECT-THREADED,
1872: \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1873:
1874: \ local variable implementation primitives
1875: \+[THEN] ( has-floats ) has-locals [IF]
1876:
1877: @local# -- w gforth fetch_local_number
1878: w = *(Cell *)(lp+(Cell)NEXT_INST);
1879: INC_IP(1);
1880:
1881: @local0 -- w new fetch_local_zero
1882: w = *(Cell *)(lp+0*sizeof(Cell));
1883:
1884: @local1 -- w new fetch_local_four
1885: w = *(Cell *)(lp+1*sizeof(Cell));
1886:
1887: @local2 -- w new fetch_local_eight
1888: w = *(Cell *)(lp+2*sizeof(Cell));
1889:
1890: @local3 -- w new fetch_local_twelve
1891: w = *(Cell *)(lp+3*sizeof(Cell));
1892:
1893: \+has-floats [IF]
1894:
1895: f@local# -- r gforth f_fetch_local_number
1896: r = *(Float *)(lp+(Cell)NEXT_INST);
1897: INC_IP(1);
1898:
1899: f@local0 -- r new f_fetch_local_zero
1900: r = *(Float *)(lp+0*sizeof(Float));
1901:
1902: f@local1 -- r new f_fetch_local_eight
1903: r = *(Float *)(lp+1*sizeof(Float));
1904:
1905: \+[THEN]
1906:
1907: laddr# -- c_addr gforth laddr_number
1908: /* this can also be used to implement lp@ */
1909: c_addr = (Char *)(lp+(Cell)NEXT_INST);
1910: INC_IP(1);
1911:
1912: lp+!# -- gforth lp_plus_store_number
1913: ""used with negative immediate values it allocates memory on the
1914: local stack, a positive immediate argument drops memory from the local
1915: stack""
1916: lp += (Cell)NEXT_INST;
1917: INC_IP(1);
1918:
1919: lp- -- new minus_four_lp_plus_store
1920: lp += -sizeof(Cell);
1921:
1922: lp+ -- new eight_lp_plus_store
1923: lp += sizeof(Float);
1924:
1925: lp+2 -- new sixteen_lp_plus_store
1926: lp += 2*sizeof(Float);
1927:
1928: lp! c_addr -- gforth lp_store
1929: lp = (Address)c_addr;
1930:
1931: >l w -- gforth to_l
1932: lp -= sizeof(Cell);
1933: *(Cell *)lp = w;
1934:
1935: \+has-floats [IF]
1936:
1937: f>l r -- gforth f_to_l
1938: lp -= sizeof(Float);
1939: *(Float *)lp = r;
1940:
1941: \+[THEN] [THEN] \ has-locals
1942:
1943: up! a_addr -- gforth up_store
1944: up0=up=(char *)a_addr;
1945: :
1946: up ! ;
1947: Variable UP
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>