File:
[gforth] /
gforth /
prim
Revision
1.65:
download - view:
text,
annotated -
select for diffs
Sun Nov 12 18:14:09 2000 UTC (23 years, 4 months ago) by
anton
Branches:
MAIN
CVS tags:
HEAD
prims2x.fs now generates the return stack stuff from the stack comment
most references to rp were eliminated from prim
currently this comes with a performance penalty, because
conditional branches now use just one NEXT instead of one for the
branch and one for the fallthrough
1: \ Gforth primitives
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000 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., 59 Temple Place, Suite 330, Boston, MA 02111, 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: \ Note: Fields in brackets are optional. Word specifications have to
36: \ be separated by at least one empty line
37: \
38: \ Both pronounciation and stack items (in the stack effect) must
39: \ conform to the C identifier syntax or the C compiler will complain.
40: \ If you don't have a pronounciation field, the Forth name is used,
41: \ and has to conform to the C identifier syntax.
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: ""Perform the semantics represented by the execution token, @i{xt}.""
119: ip=IP;
120: IF_spTOS(spTOS = sp[0]);
121: EXEC(xt);
122:
123: perform ( a_addr -- ) gforth
124: ""@code{@@ execute}.""
125: /* and pfe */
126: ip=IP;
127: IF_spTOS(spTOS = sp[0]);
128: EXEC(*(Xt *)a_addr);
129: :
130: @ execute ;
131:
132: \fhas? skipbranchprims 0= [IF]
133: \+glocals
134:
135: branch-lp+!# ( -- ) gforth branch_lp_plus_store_number
136: /* this will probably not be used */
137: lp += (Cell)(IP[1]);
138: goto branch;
139:
140: \+
141:
142: branch ( -- ) gforth
143: branch:
144: SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
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 SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
153: }
154: else
155: INC_IP(1);
156: $4
157:
158: \+glocals
159:
160: $1-lp+!# $2_lp_plus_store_number
161: $3 lp += (Cell)(IP[1]);
162: SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
163: }
164: else
165: INC_IP(2);
166:
167: \+
168: )
169:
170: condbranch(?branch,( f -- ) f83 question_branch,
171: if (f==0) {
172: IF_spTOS(spTOS = sp[0]);
173: ,:
174: 0= dup \ !f !f
175: r> dup @ \ !f !f IP branchoffset
176: rot and + \ !f IP|IP+branchoffset
177: swap 0= cell and + \ IP''
178: >r ;)
179:
180: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
181: \ is only used in if's (yet)
182:
183: \+xconds
184:
185: ?dup-?branch ( f -- f ) new question_dupe_question_branch
186: ""The run-time procedure compiled by @code{?DUP-IF}.""
187: if (f==0) {
188: sp++;
189: IF_spTOS(spTOS = sp[0]);
190: SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
191: NEXT;
192: }
193: else
194: INC_IP(1);
195:
196: ?dup-0=-?branch ( f -- ) new question_dupe_zero_equals_question_branch
197: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
198: /* the approach taken here of declaring the word as having the stack
199: effect ( f -- ) and correcting for it in the branch-taken case costs a
200: few cycles in that case, but is easy to convert to a CONDBRANCH
201: invocation */
202: if (f!=0) {
203: sp--;
204: SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
205: NEXT;
206: }
207: else
208: INC_IP(1);
209:
210: \+
211: \f[THEN]
212: \fhas? skiploopprims 0= [IF]
213:
214: condbranch((next),( R:n1 -- R:n2 ) cmFORTH paren_next,
215: n2=n1-1;
216: if (n1) {
217: ,:
218: r> r> dup 1- >r
219: IF dup @ + >r ELSE cell+ >r THEN ;)
220:
221: condbranch((loop),( R:nlimit R:n1 -- R:nlimit R:n2 ) gforth paren_loop,
222: n2=n1+1;
223: if (n2 != nlimit) {
224: ,:
225: r> r> 1+ r> 2dup =
226: IF >r 1- >r cell+ >r
227: ELSE >r >r dup @ + >r THEN ;)
228:
229: condbranch((+loop),( n R:nlimit R:n1 -- R:nlimit R:n2 ) gforth paren_plus_loop,
230: /* !! check this thoroughly */
231: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
232: /* dependent upon two's complement arithmetic */
233: Cell olddiff = n1-nlimit;
234: n2=n1+n;
235: if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
236: || (olddiff^n)>=0 /* it is a wrap-around effect */) {
237: ,:
238: r> swap
239: r> r> 2dup - >r
240: 2 pick r@ + r@ xor 0< 0=
241: 3 pick r> xor 0< 0= or
242: IF >r + >r dup @ + >r
243: ELSE >r >r drop cell+ >r THEN ;)
244:
245: \+xconds
246:
247: condbranch((-loop),( u R:nlimit R:n1 -- R:nlimit R:n2 ) gforth paren_minus_loop,
248: UCell olddiff = n1-nlimit;
249: n2=n1-u;
250: if (olddiff>u) {
251: ,)
252:
253: condbranch((s+loop),( n R:nlimit R:n1 -- R:nlimit R:n2 ) gforth paren_symmetric_plus_loop,
254: ""The run-time procedure compiled by S+LOOP. It loops until the index
255: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
256: version of (+LOOP).""
257: /* !! check this thoroughly */
258: Cell diff = n1-nlimit;
259: Cell newdiff = diff+n;
260: if (n<0) {
261: diff = -diff;
262: newdiff = -newdiff;
263: }
264: n2=n1+n;
265: if (diff>=0 || newdiff<0) {
266: ,)
267:
268: \+
269:
270: unloop ( R:w1 R:w2 -- ) core
271: /* !! alias for 2rdrop */
272: :
273: r> rdrop rdrop >r ;
274:
275: (for) ( ncount -- R:nlimit R:ncount ) cmFORTH paren_for
276: /* or (for) = >r -- collides with unloop! */
277: nlimit=0;
278: :
279: r> swap 0 >r >r >r ;
280:
281: (do) ( nlimit nstart -- R:nlimit R:nstart ) gforth paren_do
282: :
283: r> swap rot >r >r >r ;
284:
285: (?do) ( nlimit nstart -- R:nlimit R:nstart ) gforth paren_question_do
286: if (nstart == nlimit) {
287: IF_spTOS(spTOS = sp[0]);
288: goto branch;
289: }
290: else {
291: INC_IP(1);
292: }
293: :
294: 2dup =
295: IF r> swap rot >r >r
296: dup @ + >r
297: ELSE r> swap rot >r >r
298: cell+ >r
299: THEN ; \ --> CORE-EXT
300:
301: \+xconds
302:
303: (+do) ( nlimit nstart -- R:nlimit R:nstart ) gforth paren_plus_do
304: if (nstart >= nlimit) {
305: IF_spTOS(spTOS = sp[0]);
306: goto branch;
307: }
308: else {
309: INC_IP(1);
310: }
311: :
312: swap 2dup
313: r> swap >r swap >r
314: >=
315: IF
316: dup @ +
317: ELSE
318: cell+
319: THEN >r ;
320:
321: (u+do) ( ulimit ustart -- R:ulimit R:ustart ) gforth paren_u_plus_do
322: if (ustart >= ulimit) {
323: IF_spTOS(spTOS = sp[0]);
324: goto branch;
325: }
326: else {
327: INC_IP(1);
328: }
329: :
330: swap 2dup
331: r> swap >r swap >r
332: u>=
333: IF
334: dup @ +
335: ELSE
336: cell+
337: THEN >r ;
338:
339: (-do) ( nlimit nstart -- R:nlimit R:nstart ) gforth paren_minus_do
340: if (nstart <= nlimit) {
341: IF_spTOS(spTOS = sp[0]);
342: goto branch;
343: }
344: else {
345: INC_IP(1);
346: }
347: :
348: swap 2dup
349: r> swap >r swap >r
350: <=
351: IF
352: dup @ +
353: ELSE
354: cell+
355: THEN >r ;
356:
357: (u-do) ( ulimit ustart -- R:ulimit R:ustart ) gforth paren_u_minus_do
358: if (ustart <= ulimit) {
359: IF_spTOS(spTOS = sp[0]);
360: goto branch;
361: }
362: else {
363: INC_IP(1);
364: }
365: :
366: swap 2dup
367: r> swap >r swap >r
368: u<=
369: IF
370: dup @ +
371: ELSE
372: cell+
373: THEN >r ;
374:
375: \+
376:
377: \ don't make any assumptions where the return stack is!!
378: \ implement this in machine code if it should run quickly!
379:
380: i ( R:n -- R:n n ) core
381: :
382: \ rp@ cell+ @ ;
383: r> r> tuck >r >r ;
384:
385: i' ( R:w R:w2 -- R:w R:w2 w ) gforth i_tick
386: :
387: \ rp@ cell+ cell+ @ ;
388: r> r> r> dup itmp ! >r >r >r itmp @ ;
389: variable itmp
390:
391: j ( R:n R:d1 -- n R:n R:d1 ) core
392: :
393: \ rp@ cell+ cell+ cell+ @ ;
394: r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
395: [IFUNDEF] itmp variable itmp [THEN]
396:
397: k ( R:n R:d1 R:d2 -- n R:n R:d1 R:d2 ) gforth
398: :
399: \ rp@ [ 5 cells ] Literal + @ ;
400: r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
401: [IFUNDEF] itmp variable itmp [THEN]
402:
403: \f[THEN]
404:
405: \ digit is high-level: 0/0%
406:
407: move ( c_from c_to ucount -- ) core
408: ""Copy the contents of @i{ucount} aus at @i{c-from} to
409: @i{c-to}. @code{move} works correctly even if the two areas overlap.""
410: /* !! note that the standard specifies addr, not c-addr */
411: memmove(c_to,c_from,ucount);
412: /* make an Ifdef for bsd and others? */
413: :
414: >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
415:
416: cmove ( c_from c_to u -- ) string c_move
417: ""Copy the contents of @i{ucount} characters from data space at
418: @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
419: from low address to high address; i.e., for overlapping areas it is
420: safe if @i{c-to}=<@i{c-from}.""
421: while (u-- > 0)
422: *c_to++ = *c_from++;
423: :
424: bounds ?DO dup c@ I c! 1+ LOOP drop ;
425:
426: cmove> ( c_from c_to u -- ) string c_move_up
427: ""Copy the contents of @i{ucount} characters from data space at
428: @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
429: from high address to low address; i.e., for overlapping areas it is
430: safe if @i{c-to}>=@i{c-from}.""
431: while (u-- > 0)
432: c_to[u] = c_from[u];
433: :
434: dup 0= IF drop 2drop exit THEN
435: rot over + -rot bounds swap 1-
436: DO 1- dup c@ I c! -1 +LOOP drop ;
437:
438: fill ( c_addr u c -- ) core
439: ""Store @i{c} in @i{u} chars starting at @i{c-addr}.""
440: memset(c_addr,c,u);
441: :
442: -rot bounds
443: ?DO dup I c! LOOP drop ;
444:
445: compare ( c_addr1 u1 c_addr2 u2 -- n ) string
446: ""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
447: the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
448: is 1. Currently this is based on the machine's character
449: comparison. In the future, this may change to consider the current
450: locale and its collation order.""
451: /* close ' to keep fontify happy */
452: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
453: if (n==0)
454: n = u1-u2;
455: if (n<0)
456: n = -1;
457: else if (n>0)
458: n = 1;
459: :
460: rot 2dup swap - >r min swap -text dup
461: IF rdrop ELSE drop r> sgn THEN ;
462: : sgn ( n -- -1/0/1 )
463: dup 0= IF EXIT THEN 0< 2* 1+ ;
464:
465: -text ( c_addr1 u c_addr2 -- n ) new dash_text
466: n = memcmp(c_addr1, c_addr2, u);
467: if (n<0)
468: n = -1;
469: else if (n>0)
470: n = 1;
471: :
472: swap bounds
473: ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
474: ELSE c@ I c@ - unloop THEN sgn ;
475: : sgn ( n -- -1/0/1 )
476: dup 0= IF EXIT THEN 0< 2* 1+ ;
477:
478: toupper ( c1 -- c2 ) gforth
479: ""If @i{c1} is a lower-case character (in the current locale), @i{c2}
480: is the equivalent upper-case character. All other characters are unchanged.""
481: c2 = toupper(c1);
482: :
483: dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
484:
485: capscomp ( c_addr1 u c_addr2 -- n ) new
486: n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
487: if (n<0)
488: n = -1;
489: else if (n>0)
490: n = 1;
491: :
492: swap bounds
493: ?DO dup c@ I c@ <>
494: IF dup c@ toupper I c@ toupper =
495: ELSE true THEN WHILE 1+ LOOP drop 0
496: ELSE c@ toupper I c@ toupper - unloop THEN sgn ;
497:
498: -trailing ( c_addr u1 -- c_addr u2 ) string dash_trailing
499: ""Adjust the string specified by @i{c-addr, u1} to remove all trailing
500: spaces. @i{u2} is the length of the modified string.""
501: u2 = u1;
502: while (u2>0 && c_addr[u2-1] == ' ')
503: u2--;
504: :
505: BEGIN 1- 2dup + c@ bl = WHILE
506: dup 0= UNTIL ELSE 1+ THEN ;
507:
508: /string ( c_addr1 u1 n -- c_addr2 u2 ) string slash_string
509: ""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
510: characters from the start of the string.""
511: c_addr2 = c_addr1+n;
512: u2 = u1-n;
513: :
514: tuck - >r + r> dup 0< IF - 0 THEN ;
515:
516: + ( n1 n2 -- n ) core plus
517: n = n1+n2;
518:
519: \ PFE-0.9.14 has it differently, but the next release will have it as follows
520: under+ ( n1 n2 n3 -- n n2 ) gforth under_plus
521: ""add @i{n3} to @i{n1} (giving @i{n})""
522: n = n1+n3;
523: :
524: rot + swap ;
525:
526: - ( n1 n2 -- n ) core minus
527: n = n1-n2;
528: :
529: negate + ;
530:
531: negate ( n1 -- n2 ) core
532: /* use minus as alias */
533: n2 = -n1;
534: :
535: invert 1+ ;
536:
537: 1+ ( n1 -- n2 ) core one_plus
538: n2 = n1+1;
539: :
540: 1 + ;
541:
542: 1- ( n1 -- n2 ) core one_minus
543: n2 = n1-1;
544: :
545: 1 - ;
546:
547: max ( n1 n2 -- n ) core
548: if (n1<n2)
549: n = n2;
550: else
551: n = n1;
552: :
553: 2dup < IF swap THEN drop ;
554:
555: min ( n1 n2 -- n ) core
556: if (n1<n2)
557: n = n1;
558: else
559: n = n2;
560: :
561: 2dup > IF swap THEN drop ;
562:
563: abs ( n -- u ) core
564: if (n<0)
565: u = -n;
566: else
567: u = n;
568: :
569: dup 0< IF negate THEN ;
570:
571: * ( n1 n2 -- n ) core star
572: n = n1*n2;
573: :
574: um* drop ;
575:
576: / ( n1 n2 -- n ) core slash
577: n = n1/n2;
578: :
579: /mod nip ;
580:
581: mod ( n1 n2 -- n ) core
582: n = n1%n2;
583: :
584: /mod drop ;
585:
586: /mod ( n1 n2 -- n3 n4 ) core slash_mod
587: n4 = n1/n2;
588: n3 = n1%n2; /* !! is this correct? look into C standard! */
589: :
590: >r s>d r> fm/mod ;
591:
592: 2* ( n1 -- n2 ) core two_star
593: ""Shift left by 1; also works on unsigned numbers""
594: n2 = 2*n1;
595: :
596: dup + ;
597:
598: 2/ ( n1 -- n2 ) core two_slash
599: ""Arithmetic shift right by 1. For signed numbers this is a floored
600: division by 2 (note that @code{/} not necessarily floors).""
601: n2 = n1>>1;
602: :
603: dup MINI and IF 1 ELSE 0 THEN
604: [ bits/byte cell * 1- ] literal
605: 0 DO 2* swap dup 2* >r MINI and
606: IF 1 ELSE 0 THEN or r> swap
607: LOOP nip ;
608:
609: fm/mod ( d1 n1 -- n2 n3 ) core f_m_slash_mod
610: ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}.""
611: #ifdef BUGGY_LONG_LONG
612: DCell r = fmdiv(d1,n1);
613: n2=r.hi;
614: n3=r.lo;
615: #else
616: /* assumes that the processor uses either floored or symmetric division */
617: n3 = d1/n1;
618: n2 = d1%n1;
619: /* note that this 1%-3>0 is optimized by the compiler */
620: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
621: n3--;
622: n2+=n1;
623: }
624: #endif
625: :
626: dup >r dup 0< IF negate >r dnegate r> THEN
627: over 0< IF tuck + swap THEN
628: um/mod
629: r> 0< IF swap negate swap THEN ;
630:
631: sm/rem ( d1 n1 -- n2 n3 ) core s_m_slash_rem
632: ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0.""
633: #ifdef BUGGY_LONG_LONG
634: DCell r = smdiv(d1,n1);
635: n2=r.hi;
636: n3=r.lo;
637: #else
638: /* assumes that the processor uses either floored or symmetric division */
639: n3 = d1/n1;
640: n2 = d1%n1;
641: /* note that this 1%-3<0 is optimized by the compiler */
642: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
643: n3++;
644: n2-=n1;
645: }
646: #endif
647: :
648: over >r dup >r abs -rot
649: dabs rot um/mod
650: r> r@ xor 0< IF negate THEN
651: r> 0< IF swap negate swap THEN ;
652:
653: m* ( n1 n2 -- d ) core m_star
654: #ifdef BUGGY_LONG_LONG
655: d = mmul(n1,n2);
656: #else
657: d = (DCell)n1 * (DCell)n2;
658: #endif
659: :
660: 2dup 0< and >r
661: 2dup swap 0< and >r
662: um* r> - r> - ;
663:
664: um* ( u1 u2 -- ud ) core u_m_star
665: /* use u* as alias */
666: #ifdef BUGGY_LONG_LONG
667: ud = ummul(u1,u2);
668: #else
669: ud = (UDCell)u1 * (UDCell)u2;
670: #endif
671: :
672: >r >r 0 0 r> r> [ 8 cells ] literal 0
673: DO
674: over >r dup >r 0< and d2*+ drop
675: r> 2* r> swap
676: LOOP 2drop ;
677: : d2*+ ( ud n -- ud+n c )
678: over MINI
679: and >r >r 2dup d+ swap r> + swap r> ;
680:
681: um/mod ( ud u1 -- u2 u3 ) core u_m_slash_mod
682: ""ud=u3*u1+u2, u1>u2>=0""
683: #ifdef BUGGY_LONG_LONG
684: UDCell r = umdiv(ud,u1);
685: u2=r.hi;
686: u3=r.lo;
687: #else
688: u3 = ud/u1;
689: u2 = ud%u1;
690: #endif
691: :
692: 0 swap [ 8 cells 1 + ] literal 0
693: ?DO /modstep
694: LOOP drop swap 1 rshift or swap ;
695: : /modstep ( ud c R: u -- ud-?u c R: u )
696: >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN d2*+ r> ;
697: : d2*+ ( ud n -- ud+n c )
698: over MINI
699: and >r >r 2dup d+ swap r> + swap r> ;
700:
701: m+ ( d1 n -- d2 ) double m_plus
702: #ifdef BUGGY_LONG_LONG
703: d2.lo = d1.lo+n;
704: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
705: #else
706: d2 = d1+n;
707: #endif
708: :
709: s>d d+ ;
710:
711: d+ ( d1 d2 -- d ) double d_plus
712: #ifdef BUGGY_LONG_LONG
713: d.lo = d1.lo+d2.lo;
714: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
715: #else
716: d = d1+d2;
717: #endif
718: :
719: rot + >r tuck + swap over u> r> swap - ;
720:
721: d- ( d1 d2 -- d ) double d_minus
722: #ifdef BUGGY_LONG_LONG
723: d.lo = d1.lo - d2.lo;
724: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
725: #else
726: d = d1-d2;
727: #endif
728: :
729: dnegate d+ ;
730:
731: dnegate ( d1 -- d2 ) double d_negate
732: /* use dminus as alias */
733: #ifdef BUGGY_LONG_LONG
734: d2 = dnegate(d1);
735: #else
736: d2 = -d1;
737: #endif
738: :
739: invert swap negate tuck 0= - ;
740:
741: d2* ( d1 -- d2 ) double d_two_star
742: ""Shift left by 1; also works on unsigned numbers""
743: #ifdef BUGGY_LONG_LONG
744: d2.lo = d1.lo<<1;
745: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
746: #else
747: d2 = 2*d1;
748: #endif
749: :
750: 2dup d+ ;
751:
752: d2/ ( d1 -- d2 ) double d_two_slash
753: ""Arithmetic shift right by 1. For signed numbers this is a floored
754: division by 2.""
755: #ifdef BUGGY_LONG_LONG
756: d2.hi = d1.hi>>1;
757: d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
758: #else
759: d2 = d1>>1;
760: #endif
761: :
762: dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
763: r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
764:
765: and ( w1 w2 -- w ) core
766: w = w1&w2;
767:
768: or ( w1 w2 -- w ) core
769: w = w1|w2;
770: :
771: invert swap invert and invert ;
772:
773: xor ( w1 w2 -- w ) core x_or
774: w = w1^w2;
775:
776: invert ( w1 -- w2 ) core
777: w2 = ~w1;
778: :
779: MAXU xor ;
780:
781: rshift ( u1 n -- u2 ) core r_shift
782: ""Logical shift right by @i{n} bits.""
783: u2 = u1>>n;
784: :
785: 0 ?DO 2/ MAXI and LOOP ;
786:
787: lshift ( u1 n -- u2 ) core l_shift
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 $3not_equals
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_than
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_than
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 $3not_equals
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_than
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_than
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: \+dcomps
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: \+
900:
901: within ( u1 u2 u3 -- f ) core-ext
902: ""u2=<u1<u3 or: u3=<u2 and u1 is not in [u3,u2). This works for
903: unsigned and signed numbers (but not a mixture). Another way to think
904: about this word is to consider the numbers as a circle (wrapping
905: around from @code{max-u} to 0 for unsigned, and from @code{max-n} to
906: min-n for signed numbers); now consider the range from u2 towards
907: increasing numbers up to and excluding u3 (giving an empty range if
908: u2=u3); if u1 is in this range, @code{within} returns true.""
909: f = FLAG(u1-u2 < u3-u2);
910: :
911: over - >r - r> u< ;
912:
913: sp@ ( -- a_addr ) gforth sp_fetch
914: a_addr = sp+1;
915:
916: sp! ( a_addr -- ) gforth sp_store
917: sp = a_addr;
918: /* works with and without spTOS caching */
919:
920: rp@ ( -- a_addr ) gforth rp_fetch
921: a_addr = rp;
922:
923: rp! ( a_addr -- ) gforth rp_store
924: rp = a_addr;
925:
926: \+floating
927:
928: fp@ ( -- f_addr ) gforth fp_fetch
929: f_addr = fp;
930:
931: fp! ( f_addr -- ) gforth fp_store
932: fp = f_addr;
933:
934: \+
935:
936: ;s ( R:w -- ) gforth semis
937: ""The primitive compiled by @code{EXIT}.""
938: SET_IP((Xt *)w);
939:
940: >r ( w -- R:w ) core to_r
941: :
942: (>r) ;
943: : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
944:
945: r> ( R:w -- w ) core r_from
946: :
947: rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
948: Create (rdrop) ' ;s A,
949:
950: rdrop ( R:w -- ) gforth
951: :
952: r> r> drop >r ;
953:
954: 2>r ( w1 w2 -- R:w1 R:w2 ) core-ext two_to_r
955: :
956: swap r> swap >r swap >r >r ;
957:
958: 2r> ( R:w1 R:w2 -- w1 w2 ) core-ext two_r_from
959: :
960: r> r> swap r> swap >r swap ;
961:
962: 2r@ ( R:w1 R:w2 -- R:w1 R:w2 w1 w2 ) core-ext two_r_fetch
963: :
964: i' j ;
965:
966: 2rdrop ( R:w1 R:w2 -- ) gforth two_r_drop
967: :
968: r> r> drop r> drop >r ;
969:
970: over ( w1 w2 -- w1 w2 w1 ) core
971: :
972: sp@ cell+ @ ;
973:
974: drop ( w -- ) core
975: :
976: IF THEN ;
977:
978: swap ( w1 w2 -- w2 w1 ) core
979: :
980: >r (swap) ! r> (swap) @ ;
981: Variable (swap)
982:
983: dup ( w -- w w ) core dupe
984: :
985: sp@ @ ;
986:
987: rot ( w1 w2 w3 -- w2 w3 w1 ) core rote
988: :
989: [ defined? (swap) [IF] ]
990: (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
991: Variable (rot)
992: [ELSE] ]
993: >r swap r> swap ;
994: [THEN]
995:
996: -rot ( w1 w2 w3 -- w3 w1 w2 ) gforth not_rote
997: :
998: rot rot ;
999:
1000: nip ( w1 w2 -- w2 ) core-ext
1001: :
1002: swap drop ;
1003:
1004: tuck ( w1 w2 -- w2 w1 w2 ) core-ext
1005: :
1006: swap over ;
1007:
1008: ?dup ( w -- w ) core question_dupe
1009: ""Actually the stack effect is: @code{( w -- 0 | w w )}. It performs a
1010: @code{dup} if w is nonzero.""
1011: if (w!=0) {
1012: IF_spTOS(*sp-- = w;)
1013: #ifndef USE_TOS
1014: *--sp = w;
1015: #endif
1016: }
1017: :
1018: dup IF dup THEN ;
1019:
1020: pick ( u -- w ) core-ext
1021: ""Actually the stack effect is @code{ x0 ... xu u -- x0 ... xu x0 }.""
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: ""@i{w} is the cell stored at @i{a_addr}.""
1058: w = *a_addr;
1059:
1060: ! ( w a_addr -- ) core store
1061: ""Store @i{w} into the cell at @i{a-addr}.""
1062: *a_addr = w;
1063:
1064: +! ( n a_addr -- ) core plus_store
1065: ""Add @i{n} to the cell at @i{a-addr}.""
1066: *a_addr += n;
1067: :
1068: tuck @ + swap ! ;
1069:
1070: c@ ( c_addr -- c ) core c_fetch
1071: ""@i{c} is the char stored at @i{c_addr}.""
1072: c = *c_addr;
1073: :
1074: [ bigendian [IF] ]
1075: [ cell>bit 4 = [IF] ]
1076: dup [ 0 cell - ] Literal and @ swap 1 and
1077: IF $FF and ELSE 8>> THEN ;
1078: [ [ELSE] ]
1079: dup [ cell 1- ] literal and
1080: tuck - @ swap [ cell 1- ] literal xor
1081: 0 ?DO 8>> LOOP $FF and
1082: [ [THEN] ]
1083: [ [ELSE] ]
1084: [ cell>bit 4 = [IF] ]
1085: dup [ 0 cell - ] Literal and @ swap 1 and
1086: IF 8>> ELSE $FF and THEN
1087: [ [ELSE] ]
1088: dup [ cell 1- ] literal and
1089: tuck - @ swap
1090: 0 ?DO 8>> LOOP 255 and
1091: [ [THEN] ]
1092: [ [THEN] ]
1093: ;
1094: : 8>> 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ ;
1095:
1096: c! ( c c_addr -- ) core c_store
1097: ""Store @i{c} into the char at @i{c-addr}.""
1098: *c_addr = c;
1099: :
1100: [ bigendian [IF] ]
1101: [ cell>bit 4 = [IF] ]
1102: tuck 1 and IF $FF and ELSE 8<< THEN >r
1103: dup -2 and @ over 1 and cells masks + @ and
1104: r> or swap -2 and ! ;
1105: Create masks $00FF , $FF00 ,
1106: [ELSE] ]
1107: dup [ cell 1- ] literal and dup
1108: [ cell 1- ] literal xor >r
1109: - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1110: rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1111: [THEN]
1112: [ELSE] ]
1113: [ cell>bit 4 = [IF] ]
1114: tuck 1 and IF 8<< ELSE $FF and THEN >r
1115: dup -2 and @ over 1 and cells masks + @ and
1116: r> or swap -2 and ! ;
1117: Create masks $FF00 , $00FF ,
1118: [ELSE] ]
1119: dup [ cell 1- ] literal and dup >r
1120: - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1121: rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1122: [THEN]
1123: [THEN]
1124: : 8<< 2* 2* 2* 2* 2* 2* 2* 2* ;
1125:
1126: 2! ( w1 w2 a_addr -- ) core two_store
1127: ""Store @i{w2} into the cell at @i{c-addr} and @i{w1} into the next cell.""
1128: a_addr[0] = w2;
1129: a_addr[1] = w1;
1130: :
1131: tuck ! cell+ ! ;
1132:
1133: 2@ ( a_addr -- w1 w2 ) core two_fetch
1134: ""@i{w2} is the content of the cell stored at @i{a-addr}, @i{w1} is
1135: the content of the next cell.""
1136: w2 = a_addr[0];
1137: w1 = a_addr[1];
1138: :
1139: dup cell+ @ swap @ ;
1140:
1141: cell+ ( a_addr1 -- a_addr2 ) core cell_plus
1142: ""@code{1 cells +}""
1143: a_addr2 = a_addr1+1;
1144: :
1145: cell + ;
1146:
1147: cells ( n1 -- n2 ) core
1148: "" @i{n2} is the number of address units of @i{n1} cells.""
1149: n2 = n1 * sizeof(Cell);
1150: :
1151: [ cell
1152: 2/ dup [IF] ] 2* [ [THEN]
1153: 2/ dup [IF] ] 2* [ [THEN]
1154: 2/ dup [IF] ] 2* [ [THEN]
1155: 2/ dup [IF] ] 2* [ [THEN]
1156: drop ] ;
1157:
1158: char+ ( c_addr1 -- c_addr2 ) core char_plus
1159: ""@code{1 chars +}.""
1160: c_addr2 = c_addr1 + 1;
1161: :
1162: 1+ ;
1163:
1164: (chars) ( n1 -- n2 ) gforth paren_chars
1165: n2 = n1 * sizeof(Char);
1166: :
1167: ;
1168:
1169: count ( c_addr1 -- c_addr2 u ) core
1170: ""@i{c-addr2} is the first character and @i{u} the length of the
1171: counted string at @i{c-addr1}.""
1172: u = *c_addr1;
1173: c_addr2 = c_addr1+1;
1174: :
1175: dup 1+ swap c@ ;
1176:
1177: (f83find) ( c_addr u f83name1 -- f83name2 ) new paren_f83find
1178: for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
1179: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1180: memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1181: break;
1182: f83name2=f83name1;
1183: :
1184: BEGIN dup WHILE (find-samelen) dup WHILE
1185: >r 2dup r@ cell+ char+ capscomp 0=
1186: IF 2drop r> EXIT THEN
1187: r> @
1188: REPEAT THEN nip nip ;
1189: : (find-samelen) ( u f83name1 -- u f83name2/0 )
1190: BEGIN 2dup cell+ c@ $1F and <> WHILE @ dup 0= UNTIL THEN ;
1191:
1192: \+hash
1193:
1194: (hashfind) ( c_addr u a_addr -- f83name2 ) new paren_hashfind
1195: struct F83Name *f83name1;
1196: f83name2=NULL;
1197: while(a_addr != NULL)
1198: {
1199: f83name1=(struct F83Name *)(a_addr[1]);
1200: a_addr=(Cell *)(a_addr[0]);
1201: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1202: memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1203: {
1204: f83name2=f83name1;
1205: break;
1206: }
1207: }
1208: :
1209: BEGIN dup WHILE
1210: 2@ >r >r dup r@ cell+ c@ $1F and =
1211: IF 2dup r@ cell+ char+ capscomp 0=
1212: IF 2drop r> rdrop EXIT THEN THEN
1213: rdrop r>
1214: REPEAT nip nip ;
1215:
1216: (tablefind) ( c_addr u a_addr -- f83name2 ) new paren_tablefind
1217: ""A case-sensitive variant of @code{(hashfind)}""
1218: struct F83Name *f83name1;
1219: f83name2=NULL;
1220: while(a_addr != NULL)
1221: {
1222: f83name1=(struct F83Name *)(a_addr[1]);
1223: a_addr=(Cell *)(a_addr[0]);
1224: if ((UCell)F83NAME_COUNT(f83name1)==u &&
1225: memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1226: {
1227: f83name2=f83name1;
1228: break;
1229: }
1230: }
1231: :
1232: BEGIN dup WHILE
1233: 2@ >r >r dup r@ cell+ c@ $1F and =
1234: IF 2dup r@ cell+ char+ -text 0=
1235: IF 2drop r> rdrop EXIT THEN THEN
1236: rdrop r>
1237: REPEAT nip nip ;
1238:
1239: (hashkey) ( c_addr u1 -- u2 ) gforth paren_hashkey
1240: u2=0;
1241: while(u1--)
1242: u2+=(Cell)toupper(*c_addr++);
1243: :
1244: 0 -rot bounds ?DO I c@ toupper + LOOP ;
1245:
1246: (hashkey1) ( c_addr u ubits -- ukey ) gforth paren_hashkey1
1247: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1248: /* this hash function rotates the key at every step by rot bits within
1249: ubits bits and xors it with the character. This function does ok in
1250: the chi-sqare-test. Rot should be <=7 (preferably <=5) for
1251: ASCII strings (larger if ubits is large), and should share no
1252: divisors with ubits.
1253: */
1254: 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];
1255: Char *cp = c_addr;
1256: for (ukey=0; cp<c_addr+u; cp++)
1257: ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
1258: ^ toupper(*cp))
1259: & ((1<<ubits)-1));
1260: :
1261: dup rot-values + c@ over 1 swap lshift 1- >r
1262: tuck - 2swap r> 0 2swap bounds
1263: ?DO dup 4 pick lshift swap 3 pick rshift or
1264: I c@ toupper xor
1265: over and LOOP
1266: nip nip nip ;
1267: Create rot-values
1268: 5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
1269: 3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
1270: 7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1271: 7 c, 5 c, 5 c,
1272:
1273: \+
1274:
1275: (parse-white) ( c_addr1 u1 -- c_addr2 u2 ) gforth paren_parse_white
1276: /* use !isgraph instead of isspace? */
1277: Char *endp = c_addr1+u1;
1278: while (c_addr1<endp && isspace(*c_addr1))
1279: c_addr1++;
1280: if (c_addr1<endp) {
1281: for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1282: ;
1283: u2 = c_addr1-c_addr2;
1284: }
1285: else {
1286: c_addr2 = c_addr1;
1287: u2 = 0;
1288: }
1289: :
1290: BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1291: REPEAT THEN 2dup
1292: BEGIN dup WHILE over c@ bl > WHILE 1 /string
1293: REPEAT THEN nip - ;
1294:
1295: aligned ( c_addr -- a_addr ) core
1296: "" @i{a-addr} is the first aligned address greater than or equal to @i{c-addr}.""
1297: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1298: :
1299: [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1300:
1301: faligned ( c_addr -- f_addr ) float f_aligned
1302: "" @i{f-addr} is the first float-aligned address greater than or equal to @i{c-addr}.""
1303: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1304: :
1305: [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1306:
1307: >body ( xt -- a_addr ) core to_body
1308: "" Get the address of the body of the word represented by @i{xt} (the address
1309: of the word's data field).""
1310: a_addr = PFA(xt);
1311: :
1312: 2 cells + ;
1313:
1314: \ threading stuff is currently only interesting if we have a compiler
1315: \fhas? standardthreading has? compiler and [IF]
1316:
1317: >code-address ( xt -- c_addr ) gforth to_code_address
1318: ""@i{c-addr} is the code address of the word @i{xt}.""
1319: /* !! This behaves installation-dependently for DOES-words */
1320: c_addr = (Address)CODE_ADDRESS(xt);
1321: :
1322: @ ;
1323:
1324: >does-code ( xt -- a_addr ) gforth to_does_code
1325: ""If @i{xt} is the execution token of a child of a @code{DOES>} word,
1326: @i{a-addr} is the start of the Forth code after the @code{DOES>};
1327: Otherwise @i{a-addr} is 0.""
1328: a_addr = (Cell *)DOES_CODE(xt);
1329: :
1330: cell+ @ ;
1331:
1332: code-address! ( c_addr xt -- ) gforth code_address_store
1333: ""Create a code field with code address @i{c-addr} at @i{xt}.""
1334: MAKE_CF(xt, c_addr);
1335: CACHE_FLUSH(xt,(size_t)PFA(0));
1336: :
1337: ! ;
1338:
1339: does-code! ( a_addr xt -- ) gforth does_code_store
1340: ""Create a code field at @i{xt} for a child of a @code{DOES>}-word;
1341: @i{a-addr} is the start of the Forth code after @code{DOES>}.""
1342: MAKE_DOES_CF(xt, a_addr);
1343: CACHE_FLUSH(xt,(size_t)PFA(0));
1344: :
1345: dodoes: over ! cell+ ! ;
1346:
1347: does-handler! ( a_addr -- ) gforth does_handler_store
1348: ""Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
1349: @i{a-addr} points just behind a @code{DOES>}.""
1350: MAKE_DOES_HANDLER(a_addr);
1351: CACHE_FLUSH((caddr_t)a_addr,DOES_HANDLER_SIZE);
1352: :
1353: drop ;
1354:
1355: /does-handler ( -- n ) gforth slash_does_handler
1356: ""The size of a @code{DOES>}-handler (includes possible padding).""
1357: /* !! a constant or environmental query might be better */
1358: n = DOES_HANDLER_SIZE;
1359: :
1360: 2 cells ;
1361:
1362: threading-method ( -- n ) gforth threading_method
1363: ""0 if the engine is direct threaded. Note that this may change during
1364: the lifetime of an image.""
1365: #if defined(DOUBLY_INDIRECT)
1366: n=2;
1367: #else
1368: # if defined(DIRECT_THREADED)
1369: n=0;
1370: # else
1371: n=1;
1372: # endif
1373: #endif
1374: :
1375: 1 ;
1376:
1377: \f[THEN]
1378:
1379: key-file ( wfileid -- n ) gforth paren_key_file
1380: #ifdef HAS_FILE
1381: fflush(stdout);
1382: n = key((FILE*)wfileid);
1383: #else
1384: n = key(stdin);
1385: #endif
1386:
1387: key?-file ( wfileid -- n ) facility key_q_file
1388: #ifdef HAS_FILE
1389: fflush(stdout);
1390: n = key_query((FILE*)wfileid);
1391: #else
1392: n = key_query(stdin);
1393: #endif
1394:
1395: \+os
1396:
1397: stdin ( -- wfileid ) gforth
1398: wfileid = (Cell)stdin;
1399:
1400: stdout ( -- wfileid ) gforth
1401: wfileid = (Cell)stdout;
1402:
1403: stderr ( -- wfileid ) gforth
1404: wfileid = (Cell)stderr;
1405:
1406: form ( -- urows ucols ) gforth
1407: ""The number of lines and columns in the terminal. These numbers may change
1408: with the window size.""
1409: /* we could block SIGWINCH here to get a consistent size, but I don't
1410: think this is necessary or always beneficial */
1411: urows=rows;
1412: ucols=cols;
1413:
1414: flush-icache ( c_addr u -- ) gforth flush_icache
1415: ""Make sure that the instruction cache of the processor (if there is
1416: one) does not contain stale data at @i{c-addr} and @i{u} bytes
1417: afterwards. @code{END-CODE} performs a @code{flush-icache}
1418: automatically. Caveat: @code{flush-icache} might not work on your
1419: installation; this is usually the case if direct threading is not
1420: supported on your machine (take a look at your @file{machine.h}) and
1421: your machine has a separate instruction cache. In such cases,
1422: @code{flush-icache} does nothing instead of flushing the instruction
1423: cache.""
1424: FLUSH_ICACHE(c_addr,u);
1425:
1426: (bye) ( n -- ) gforth paren_bye
1427: return (Label *)n;
1428:
1429: (system) ( c_addr u -- wretval wior ) gforth peren_system
1430: #ifndef MSDOS
1431: int old_tp=terminal_prepped;
1432: deprep_terminal();
1433: #endif
1434: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1435: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1436: #ifndef MSDOS
1437: if (old_tp)
1438: prep_terminal();
1439: #endif
1440:
1441: getenv ( c_addr1 u1 -- c_addr2 u2 ) gforth
1442: ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2}
1443: is the host operating system's expansion of that environment variable. If the
1444: environment variable does not exist, @i{c-addr2 u2} specifies a string 0 characters
1445: in length.""
1446: /* close ' to keep fontify happy */
1447: c_addr2 = getenv(cstr(c_addr1,u1,1));
1448: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1449:
1450: open-pipe ( c_addr u wfam -- wfileid wior ) gforth open_pipe
1451: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[wfam]); /* ~ expansion of 1st arg? */
1452: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1453:
1454: close-pipe ( wfileid -- wretval wior ) gforth close_pipe
1455: wretval = pclose((FILE *)wfileid);
1456: wior = IOR(wretval==-1);
1457:
1458: time&date ( -- nsec nmin nhour nday nmonth nyear ) facility-ext time_and_date
1459: ""Report the current time of day. Seconds, minutes and hours are numbered from 0.
1460: Months are numbered from 1.""
1461: struct timeval time1;
1462: struct timezone zone1;
1463: struct tm *ltime;
1464: gettimeofday(&time1,&zone1);
1465: /* !! Single Unix specification:
1466: If tzp is not a null pointer, the behaviour is unspecified. */
1467: ltime=localtime((time_t *)&time1.tv_sec);
1468: nyear =ltime->tm_year+1900;
1469: nmonth=ltime->tm_mon+1;
1470: nday =ltime->tm_mday;
1471: nhour =ltime->tm_hour;
1472: nmin =ltime->tm_min;
1473: nsec =ltime->tm_sec;
1474:
1475: ms ( n -- ) facility-ext
1476: ""Wait at least @i{n} milli-second.""
1477: struct timeval timeout;
1478: timeout.tv_sec=n/1000;
1479: timeout.tv_usec=1000*(n%1000);
1480: (void)select(0,0,0,0,&timeout);
1481:
1482: allocate ( u -- a_addr wior ) memory
1483: ""Allocate @i{u} address units of contiguous data space. The initial
1484: contents of the data space is undefined. If the allocation is successful,
1485: @i{a-addr} is the start address of the allocated region and @i{wior}
1486: is 0. If the allocation fails, @i{a-addr} is undefined and @i{wior}
1487: is a non-zero I/O result code.""
1488: a_addr = (Cell *)malloc(u?u:1);
1489: wior = IOR(a_addr==NULL);
1490:
1491: free ( a_addr -- wior ) memory
1492: ""Return the region of data space starting at @i{a-addr} to the system.
1493: The region must originally have been obtained using @code{allocate} or
1494: @code{resize}. If the operational is successful, @i{wior} is 0.
1495: If the operation fails, @i{wior} is a non-zero I/O result code.""
1496: free(a_addr);
1497: wior = 0;
1498:
1499: resize ( a_addr1 u -- a_addr2 wior ) memory
1500: ""Change the size of the allocated area at @i{a-addr1} to @i{u}
1501: address units, possibly moving the contents to a different
1502: area. @i{a-addr2} is the address of the resulting area.
1503: If the operation is successful, @i{wior} is 0.
1504: If the operation fails, @i{wior} is a non-zero
1505: I/O result code. If @i{a-addr1} is 0, Gforth's (but not the Standard)
1506: @code{resize} @code{allocate}s @i{u} address units.""
1507: /* the following check is not necessary on most OSs, but it is needed
1508: on SunOS 4.1.2. */
1509: /* close ' to keep fontify happy */
1510: if (a_addr1==NULL)
1511: a_addr2 = (Cell *)malloc(u);
1512: else
1513: a_addr2 = (Cell *)realloc(a_addr1, u);
1514: wior = IOR(a_addr2==NULL); /* !! Define a return code */
1515:
1516: strerror ( n -- c_addr u ) gforth
1517: c_addr = strerror(n);
1518: u = strlen(c_addr);
1519:
1520: strsignal ( n -- c_addr u ) gforth
1521: c_addr = strsignal(n);
1522: u = strlen(c_addr);
1523:
1524: call-c ( w -- ) gforth call_c
1525: ""Call the C function pointed to by @i{w}. The C function has to
1526: access the stack itself. The stack pointers are exported in the global
1527: variables @code{SP} and @code{FP}.""
1528: /* This is a first attempt at support for calls to C. This may change in
1529: the future */
1530: IF_fpTOS(fp[0]=fpTOS);
1531: FP=fp;
1532: SP=sp;
1533: ((void (*)())w)();
1534: sp=SP;
1535: fp=FP;
1536: IF_spTOS(spTOS=sp[0]);
1537: IF_fpTOS(fpTOS=fp[0]);
1538:
1539: \+
1540: \+file
1541:
1542: close-file ( wfileid -- wior ) file close_file
1543: wior = IOR(fclose((FILE *)wfileid)==EOF);
1544:
1545: open-file ( c_addr u wfam -- wfileid wior ) file open_file
1546: wfileid = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[wfam]);
1547: wior = IOR(wfileid == 0);
1548:
1549: create-file ( c_addr u wfam -- wfileid wior ) file create_file
1550: Cell fd;
1551: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[wfam], 0666);
1552: if (fd != -1) {
1553: wfileid = (Cell)fdopen(fd, fileattr[wfam]);
1554: wior = IOR(wfileid == 0);
1555: } else {
1556: wfileid = 0;
1557: wior = IOR(1);
1558: }
1559:
1560: delete-file ( c_addr u -- wior ) file delete_file
1561: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1562:
1563: rename-file ( c_addr1 u1 c_addr2 u2 -- wior ) file-ext rename_file
1564: ""Rename file @i{c_addr1 u1} to new name @i{c_addr2 u2}""
1565: char *s1=tilde_cstr(c_addr2, u2, 1);
1566: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1567:
1568: file-position ( wfileid -- ud wior ) file file_position
1569: /* !! use tell and lseek? */
1570: ud = LONG2UD(ftell((FILE *)wfileid));
1571: wior = IOR(UD2LONG(ud)==-1);
1572:
1573: reposition-file ( ud wfileid -- wior ) file reposition_file
1574: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1575:
1576: file-size ( wfileid -- ud wior ) file file_size
1577: struct stat buf;
1578: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1579: ud = LONG2UD(buf.st_size);
1580:
1581: resize-file ( ud wfileid -- wior ) file resize_file
1582: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1583:
1584: read-file ( c_addr u1 wfileid -- u2 wior ) file read_file
1585: /* !! fread does not guarantee enough */
1586: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1587: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1588: /* !! is the value of ferror errno-compatible? */
1589: if (wior)
1590: clearerr((FILE *)wfileid);
1591:
1592: read-line ( c_addr u1 wfileid -- u2 flag wior ) file read_line
1593: ""this is only for backward compatibility""
1594: Cell c;
1595: flag=-1;
1596: for(u2=0; u2<u1; u2++)
1597: {
1598: c = getc((FILE *)wfileid);
1599: if (c=='\n') break;
1600: if (c=='\r') {
1601: if ((c = getc((FILE *)wfileid))!='\n')
1602: ungetc(c,(FILE *)wfileid);
1603: break;
1604: }
1605: if (c==EOF) {
1606: flag=FLAG(u2!=0);
1607: break;
1608: }
1609: c_addr[u2] = (Char)c;
1610: }
1611: wior=FILEIO(ferror((FILE *)wfileid));
1612:
1613: \+
1614:
1615: write-file ( c_addr u1 wfileid -- wior ) file write_file
1616: /* !! fwrite does not guarantee enough */
1617: #ifdef HAS_FILE
1618: {
1619: UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1620: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1621: if (wior)
1622: clearerr((FILE *)wfileid);
1623: }
1624: #else
1625: TYPE(c_addr, u1);
1626: #endif
1627:
1628: emit-file ( c wfileid -- wior ) gforth emit_file
1629: #ifdef HAS_FILE
1630: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1631: if (wior)
1632: clearerr((FILE *)wfileid);
1633: #else
1634: PUTC(c);
1635: #endif
1636:
1637: \+file
1638:
1639: flush-file ( wfileid -- wior ) file-ext flush_file
1640: wior = IOR(fflush((FILE *) wfileid)==EOF);
1641:
1642: file-status ( c_addr u -- wfam wior ) file-ext file_status
1643: char *filename=tilde_cstr(c_addr, u, 1);
1644: if (access (filename, F_OK) != 0) {
1645: wfam=0;
1646: wior=IOR(1);
1647: }
1648: else if (access (filename, R_OK | W_OK) == 0) {
1649: wfam=2; /* r/w */
1650: wior=0;
1651: }
1652: else if (access (filename, R_OK) == 0) {
1653: wfam=0; /* r/o */
1654: wior=0;
1655: }
1656: else if (access (filename, W_OK) == 0) {
1657: wfam=4; /* w/o */
1658: wior=0;
1659: }
1660: else {
1661: wfam=1; /* well, we cannot access the file, but better deliver a legal
1662: access mode (r/o bin), so we get a decent error later upon open. */
1663: wior=0;
1664: }
1665:
1666: \+
1667: \+floating
1668:
1669: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1670: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1671:
1672: d>f ( d -- r ) float d_to_f
1673: #ifdef BUGGY_LONG_LONG
1674: extern double ldexp(double x, int exp);
1675: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
1676: #else
1677: r = d;
1678: #endif
1679:
1680: f>d ( r -- d ) float f_to_d
1681: #ifdef BUGGY_LONG_LONG
1682: d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
1683: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1684: #else
1685: d = r;
1686: #endif
1687:
1688: f! ( r f_addr -- ) float f_store
1689: ""Store @i{r} into the float at address @i{f-addr}.""
1690: *f_addr = r;
1691:
1692: f@ ( f_addr -- r ) float f_fetch
1693: ""@i{r} is the float at address @i{f-addr}.""
1694: r = *f_addr;
1695:
1696: df@ ( df_addr -- r ) float-ext d_f_fetch
1697: ""Fetch the double-precision IEEE floating-point value @i{r} from the address @i{df-addr}.""
1698: #ifdef IEEE_FP
1699: r = *df_addr;
1700: #else
1701: !! df@
1702: #endif
1703:
1704: df! ( r df_addr -- ) float-ext d_f_store
1705: ""Store @i{r} as double-precision IEEE floating-point value to the
1706: address @i{df-addr}.""
1707: #ifdef IEEE_FP
1708: *df_addr = r;
1709: #else
1710: !! df!
1711: #endif
1712:
1713: sf@ ( sf_addr -- r ) float-ext s_f_fetch
1714: ""Fetch the single-precision IEEE floating-point value @i{r} from the address @i{sf-addr}.""
1715: #ifdef IEEE_FP
1716: r = *sf_addr;
1717: #else
1718: !! sf@
1719: #endif
1720:
1721: sf! ( r sf_addr -- ) float-ext s_f_store
1722: ""Store @i{r} as single-precision IEEE floating-point value to the
1723: address @i{sf-addr}.""
1724: #ifdef IEEE_FP
1725: *sf_addr = r;
1726: #else
1727: !! sf!
1728: #endif
1729:
1730: f+ ( r1 r2 -- r3 ) float f_plus
1731: r3 = r1+r2;
1732:
1733: f- ( r1 r2 -- r3 ) float f_minus
1734: r3 = r1-r2;
1735:
1736: f* ( r1 r2 -- r3 ) float f_star
1737: r3 = r1*r2;
1738:
1739: f/ ( r1 r2 -- r3 ) float f_slash
1740: r3 = r1/r2;
1741:
1742: f** ( r1 r2 -- r3 ) float-ext f_star_star
1743: ""@i{r3} is @i{r1} raised to the @i{r2}th power.""
1744: r3 = pow(r1,r2);
1745:
1746: fnegate ( r1 -- r2 ) float f_negate
1747: r2 = - r1;
1748:
1749: fdrop ( r -- ) float f_drop
1750:
1751: fdup ( r -- r r ) float f_dupe
1752:
1753: fswap ( r1 r2 -- r2 r1 ) float f_swap
1754:
1755: fover ( r1 r2 -- r1 r2 r1 ) float f_over
1756:
1757: frot ( r1 r2 r3 -- r2 r3 r1 ) float f_rote
1758:
1759: fnip ( r1 r2 -- r2 ) gforth f_nip
1760:
1761: ftuck ( r1 r2 -- r2 r1 r2 ) gforth f_tuck
1762:
1763: float+ ( f_addr1 -- f_addr2 ) float float_plus
1764: ""@code{1 floats +}.""
1765: f_addr2 = f_addr1+1;
1766:
1767: floats ( n1 -- n2 ) float
1768: ""@i{n2} is the number of address units of @i{n1} floats.""
1769: n2 = n1*sizeof(Float);
1770:
1771: floor ( r1 -- r2 ) float
1772: ""Round towards the next smaller integral value, i.e., round toward negative infinity.""
1773: /* !! unclear wording */
1774: r2 = floor(r1);
1775:
1776: fround ( r1 -- r2 ) float f_round
1777: ""Round to the nearest integral value.""
1778: /* !! unclear wording */
1779: #ifdef HAVE_RINT
1780: r2 = rint(r1);
1781: #else
1782: r2 = floor(r1+0.5);
1783: /* !! This is not quite true to the rounding rules given in the standard */
1784: #endif
1785:
1786: fmax ( r1 r2 -- r3 ) float f_max
1787: if (r1<r2)
1788: r3 = r2;
1789: else
1790: r3 = r1;
1791:
1792: fmin ( r1 r2 -- r3 ) float f_min
1793: if (r1<r2)
1794: r3 = r1;
1795: else
1796: r3 = r2;
1797:
1798: represent ( r c_addr u -- n f1 f2 ) float
1799: char *sig;
1800: int flag;
1801: int decpt;
1802: sig=ecvt(r, u, &decpt, &flag);
1803: n=(r==0 ? 1 : decpt);
1804: f1=FLAG(flag!=0);
1805: f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
1806: memmove(c_addr,sig,u);
1807:
1808: >float ( c_addr u -- flag ) float to_float
1809: ""Actual stack effect: ( c_addr u -- r t | f ). Attempt to convert the
1810: character string @i{c-addr u} to internal floating-point
1811: representation. If the string represents a valid floating-point number
1812: @i{r} is placed on the floating-point stack and @i{flag} is
1813: true. Otherwise, @i{flag} is false. A string of blanks is a special
1814: case and represents the floating-point number 0.""
1815: /* real signature: c_addr u -- r t / f */
1816: Float r;
1817: char *number=cstr(c_addr, u, 1);
1818: char *endconv;
1819: int sign = 0;
1820: if(number[0]=='-') {
1821: sign = 1;
1822: number++;
1823: u--;
1824: }
1825: while(isspace((unsigned)(number[--u])) && u>0);
1826: switch(number[u])
1827: {
1828: case 'd':
1829: case 'D':
1830: case 'e':
1831: case 'E': break;
1832: default : u++; break;
1833: }
1834: number[u]='\0';
1835: r=strtod(number,&endconv);
1836: if((flag=FLAG(!(Cell)*endconv)))
1837: {
1838: IF_fpTOS(fp[0] = fpTOS);
1839: fp += -1;
1840: fpTOS = sign ? -r : r;
1841: }
1842: else if(*endconv=='d' || *endconv=='D')
1843: {
1844: *endconv='E';
1845: r=strtod(number,&endconv);
1846: if((flag=FLAG(!(Cell)*endconv)))
1847: {
1848: IF_fpTOS(fp[0] = fpTOS);
1849: fp += -1;
1850: fpTOS = sign ? -r : r;
1851: }
1852: }
1853:
1854: fabs ( r1 -- r2 ) float-ext f_abs
1855: r2 = fabs(r1);
1856:
1857: facos ( r1 -- r2 ) float-ext f_a_cos
1858: r2 = acos(r1);
1859:
1860: fasin ( r1 -- r2 ) float-ext f_a_sine
1861: r2 = asin(r1);
1862:
1863: fatan ( r1 -- r2 ) float-ext f_a_tan
1864: r2 = atan(r1);
1865:
1866: fatan2 ( r1 r2 -- r3 ) float-ext f_a_tan_two
1867: ""@i{r1/r2}=tan(@i{r3}). ANS Forth does not require, but probably
1868: intends this to be the inverse of @code{fsincos}. In gforth it is.""
1869: r3 = atan2(r1,r2);
1870:
1871: fcos ( r1 -- r2 ) float-ext f_cos
1872: r2 = cos(r1);
1873:
1874: fexp ( r1 -- r2 ) float-ext f_e_x_p
1875: r2 = exp(r1);
1876:
1877: fexpm1 ( r1 -- r2 ) float-ext f_e_x_p_m_one
1878: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1879: #ifdef HAVE_EXPM1
1880: extern double
1881: #ifdef NeXT
1882: const
1883: #endif
1884: expm1(double);
1885: r2 = expm1(r1);
1886: #else
1887: r2 = exp(r1)-1.;
1888: #endif
1889:
1890: fln ( r1 -- r2 ) float-ext f_l_n
1891: r2 = log(r1);
1892:
1893: flnp1 ( r1 -- r2 ) float-ext f_l_n_p_one
1894: ""@i{r2}=ln(@i{r1}+1)""
1895: #ifdef HAVE_LOG1P
1896: extern double
1897: #ifdef NeXT
1898: const
1899: #endif
1900: log1p(double);
1901: r2 = log1p(r1);
1902: #else
1903: r2 = log(r1+1.);
1904: #endif
1905:
1906: flog ( r1 -- r2 ) float-ext f_log
1907: ""The decimal logarithm.""
1908: r2 = log10(r1);
1909:
1910: falog ( r1 -- r2 ) float-ext f_a_log
1911: ""@i{r2}=10**@i{r1}""
1912: extern double pow10(double);
1913: r2 = pow10(r1);
1914:
1915: fsin ( r1 -- r2 ) float-ext f_sine
1916: r2 = sin(r1);
1917:
1918: fsincos ( r1 -- r2 r3 ) float-ext f_sine_cos
1919: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1920: r2 = sin(r1);
1921: r3 = cos(r1);
1922:
1923: fsqrt ( r1 -- r2 ) float-ext f_square_root
1924: r2 = sqrt(r1);
1925:
1926: ftan ( r1 -- r2 ) float-ext f_tan
1927: r2 = tan(r1);
1928: :
1929: fsincos f/ ;
1930:
1931: fsinh ( r1 -- r2 ) float-ext f_cinch
1932: r2 = sinh(r1);
1933: :
1934: fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1935:
1936: fcosh ( r1 -- r2 ) float-ext f_cosh
1937: r2 = cosh(r1);
1938: :
1939: fexp fdup 1/f f+ f2/ ;
1940:
1941: ftanh ( r1 -- r2 ) float-ext f_tan_h
1942: r2 = tanh(r1);
1943: :
1944: f2* fexpm1 fdup 2. d>f f+ f/ ;
1945:
1946: fasinh ( r1 -- r2 ) float-ext f_a_cinch
1947: r2 = asinh(r1);
1948: :
1949: fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1950:
1951: facosh ( r1 -- r2 ) float-ext f_a_cosh
1952: r2 = acosh(r1);
1953: :
1954: fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1955:
1956: fatanh ( r1 -- r2 ) float-ext f_a_tan_h
1957: r2 = atanh(r1);
1958: :
1959: fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1960: r> IF fnegate THEN ;
1961:
1962: sfloats ( n1 -- n2 ) float-ext s_floats
1963: ""@i{n2} is the number of address units of @i{n1}
1964: single-precision IEEE floating-point numbers.""
1965: n2 = n1*sizeof(SFloat);
1966:
1967: dfloats ( n1 -- n2 ) float-ext d_floats
1968: ""@i{n2} is the number of address units of @i{n1}
1969: double-precision IEEE floating-point numbers.""
1970: n2 = n1*sizeof(DFloat);
1971:
1972: sfaligned ( c_addr -- sf_addr ) float-ext s_f_aligned
1973: ""@i{sf-addr} is the first single-float-aligned address greater
1974: than or equal to @i{c-addr}.""
1975: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1976: :
1977: [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1978:
1979: dfaligned ( c_addr -- df_addr ) float-ext d_f_aligned
1980: ""@i{df-addr} is the first double-float-aligned address greater
1981: than or equal to @i{c-addr}.""
1982: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1983: :
1984: [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1985:
1986: \ The following words access machine/OS/installation-dependent
1987: \ Gforth internals
1988: \ !! how about environmental queries DIRECT-THREADED,
1989: \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1990:
1991: \ local variable implementation primitives
1992: \+
1993: \+glocals
1994:
1995: @local# ( -- w ) gforth fetch_local_number
1996: w = *(Cell *)(lp+(Cell)NEXT_INST);
1997: INC_IP(1);
1998:
1999: @local0 ( -- w ) new fetch_local_zero
2000: w = *(Cell *)(lp+0*sizeof(Cell));
2001:
2002: @local1 ( -- w ) new fetch_local_four
2003: w = *(Cell *)(lp+1*sizeof(Cell));
2004:
2005: @local2 ( -- w ) new fetch_local_eight
2006: w = *(Cell *)(lp+2*sizeof(Cell));
2007:
2008: @local3 ( -- w ) new fetch_local_twelve
2009: w = *(Cell *)(lp+3*sizeof(Cell));
2010:
2011: \+floating
2012:
2013: f@local# ( -- r ) gforth f_fetch_local_number
2014: r = *(Float *)(lp+(Cell)NEXT_INST);
2015: INC_IP(1);
2016:
2017: f@local0 ( -- r ) new f_fetch_local_zero
2018: r = *(Float *)(lp+0*sizeof(Float));
2019:
2020: f@local1 ( -- r ) new f_fetch_local_eight
2021: r = *(Float *)(lp+1*sizeof(Float));
2022:
2023: \+
2024:
2025: laddr# ( -- c_addr ) gforth laddr_number
2026: /* this can also be used to implement lp@ */
2027: c_addr = (Char *)(lp+(Cell)NEXT_INST);
2028: INC_IP(1);
2029:
2030: lp+!# ( -- ) gforth lp_plus_store_number
2031: ""used with negative immediate values it allocates memory on the
2032: local stack, a positive immediate argument drops memory from the local
2033: stack""
2034: lp += (Cell)NEXT_INST;
2035: INC_IP(1);
2036:
2037: lp- ( -- ) new minus_four_lp_plus_store
2038: lp += -sizeof(Cell);
2039:
2040: lp+ ( -- ) new eight_lp_plus_store
2041: lp += sizeof(Float);
2042:
2043: lp+2 ( -- ) new sixteen_lp_plus_store
2044: lp += 2*sizeof(Float);
2045:
2046: lp! ( c_addr -- ) gforth lp_store
2047: lp = (Address)c_addr;
2048:
2049: >l ( w -- ) gforth to_l
2050: lp -= sizeof(Cell);
2051: *(Cell *)lp = w;
2052:
2053: \+floating
2054:
2055: f>l ( r -- ) gforth f_to_l
2056: lp -= sizeof(Float);
2057: *(Float *)lp = r;
2058:
2059: fpick ( u -- r ) gforth
2060: ""Actually the stack effect is @code{ r0 ... ru u -- r0 ... ru r0 }.""
2061: r = fp[u+1]; /* +1, because update of fp happens before this fragment */
2062: :
2063: floats fp@ + f@ ;
2064:
2065: \+
2066: \+
2067:
2068: \+OS
2069:
2070: define(`uploop',
2071: `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
2072: define(`_uploop',
2073: `ifelse($1, `$3', `$5',
2074: `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
2075: \ argflist(argnum): Forth argument list
2076: define(argflist,
2077: `ifelse($1, 0, `',
2078: `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
2079: \ argdlist(argnum): declare C's arguments
2080: define(argdlist,
2081: `ifelse($1, 0, `',
2082: `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
2083: \ argclist(argnum): pass C's arguments
2084: define(argclist,
2085: `ifelse($1, 0, `',
2086: `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
2087: \ icall(argnum)
2088: define(icall,
2089: `icall$1 ( argflist($1)u -- uret ) gforth
2090: uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
2091:
2092: ')
2093: define(fcall,
2094: `fcall$1 ( argflist($1)u -- rret ) gforth
2095: rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
2096:
2097: ')
2098:
2099: \ close ' to keep fontify happy
2100:
2101: open-lib ( c_addr1 u1 -- u2 ) gforth open_lib
2102: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2103: #ifndef RTLD_GLOBAL
2104: #define RTLD_GLOBAL 0
2105: #endif
2106: u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_GLOBAL | RTLD_LAZY);
2107: #else
2108: # ifdef _WIN32
2109: u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
2110: # else
2111: #warning Define open-lib!
2112: u2 = 0;
2113: # endif
2114: #endif
2115:
2116: lib-sym ( c_addr1 u1 u2 -- u3 ) gforth lib_sym
2117: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2118: u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
2119: #else
2120: # ifdef _WIN32
2121: u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
2122: # else
2123: #warning Define lib-sym!
2124: u3 = 0;
2125: # endif
2126: #endif
2127:
2128: uploop(i, 0, 7, `icall(i)')
2129: icall(20)
2130: uploop(i, 0, 7, `fcall(i)')
2131: fcall(20)
2132:
2133: \+
2134:
2135: up! ( a_addr -- ) gforth up_store
2136: UP=up=(char *)a_addr;
2137: :
2138: up ! ;
2139: Variable UP
2140:
2141: wcall ( u -- ) gforth
2142: IF_fpTOS(fp[0]=fpTOS);
2143: FP=fp;
2144: sp=(SYSCALL(Cell(*)(Cell *, void *))u)(sp, &FP);
2145: fp=FP;
2146: IF_spTOS(spTOS=sp[0];)
2147: IF_fpTOS(fpTOS=fp[0]);
2148:
2149: \+file
2150:
2151: open-dir ( c_addr u -- wdirid wior ) gforth open_dir
2152: wdirid = (Cell)opendir(tilde_cstr(c_addr, u, 1));
2153: wior = IOR(wdirid == 0);
2154:
2155: read-dir ( c_addr u1 wdirid -- u2 flag wior ) gforth read_dir
2156: struct dirent * dent;
2157: dent = readdir((DIR *)wdirid);
2158: wior = 0;
2159: flag = -1;
2160: if(dent == NULL) {
2161: u2 = 0;
2162: flag = 0;
2163: } else {
2164: u2 = strlen(dent->d_name);
2165: if(u2 > u1)
2166: u2 = u1;
2167: memmove(c_addr, dent->d_name, u2);
2168: }
2169:
2170: close-dir ( wdirid -- wior ) gforth close_dir
2171: wior = IOR(closedir((DIR *)wdirid));
2172:
2173: filename-match ( c_addr1 u1 c_addr2 u2 -- flag ) gforth match_file
2174: char * string = cstr(c_addr1, u1, 1);
2175: char * pattern = cstr(c_addr2, u2, 0);
2176: flag = FLAG(!fnmatch(pattern, string, 0));
2177:
2178: \+
2179:
2180: newline ( -- c_addr u ) gforth
2181: ""String containing the newline sequence of the host OS""
2182: char newline[] = {
2183: #ifdef unix
2184: '\n'
2185: #else
2186: '\r','\n'
2187: #endif
2188: };
2189: c_addr=newline;
2190: u=sizeof(newline);
2191: :
2192: "newline count ;
2193: Create "newline e? crlf [IF] 2 c, $0D c, [ELSE] 1 c, [THEN] $0A c,
2194:
2195: \+os
2196:
2197: utime ( -- dtime ) gforth
2198: ""Report the current time in microseconds since some epoch.""
2199: struct timeval time1;
2200: gettimeofday(&time1,NULL);
2201: dtime = timeval2us(&time1);
2202:
2203: cputime ( -- duser dsystem ) gforth
2204: ""duser and dsystem are the respective user- and system-level CPU
2205: times used since the start of the Forth system (excluding child
2206: processes), in microseconds (the granularity may be much larger,
2207: however). On platforms without the getrusage call, it reports elapsed
2208: time (since some epoch) for duser and 0 for dsystem.""
2209: #ifdef HAVE_GETRUSAGE
2210: struct rusage usage;
2211: getrusage(RUSAGE_SELF, &usage);
2212: duser = timeval2us(&usage.ru_utime);
2213: dsystem = timeval2us(&usage.ru_stime);
2214: #else
2215: struct timeval time1;
2216: gettimeofday(&time1,NULL);
2217: duser = timeval2us(&time1);
2218: #ifndef BUGGY_LONG_LONG
2219: dsystem = (DCell)0;
2220: #else
2221: dsystem=(DCell){0,0};
2222: #endif
2223: #endif
2224:
2225: \+
2226:
2227: \+floating
2228:
2229: v* ( f_addr1 nstride1 f_addr2 nstride2 ucount -- r ) gforth v_star
2230: ""dot-product: r=v1*v2. The first element of v1 is at f_addr1, the
2231: next at f_addr1+nstride1 and so on (similar for v2). Both vectors have
2232: ucount elements.""
2233: for (r=0.; ucount>0; ucount--) {
2234: r += *f_addr1 * *f_addr2;
2235: f_addr1 = (Float *)(((Address)f_addr1)+nstride1);
2236: f_addr2 = (Float *)(((Address)f_addr2)+nstride2);
2237: }
2238: :
2239: >r swap 2swap swap 0e r> 0 ?DO
2240: dup f@ over + 2swap dup f@ f* f+ over + 2swap
2241: LOOP 2drop 2drop ;
2242:
2243: faxpy ( ra f_x nstridex f_y nstridey ucount -- ) gforth
2244: ""vy=ra*vx+vy""
2245: for (; ucount>0; ucount--) {
2246: *f_y += ra * *f_x;
2247: f_x = (Float *)(((Address)f_x)+nstridex);
2248: f_y = (Float *)(((Address)f_y)+nstridey);
2249: }
2250: :
2251: >r swap 2swap swap r> 0 ?DO
2252: fdup dup f@ f* over + 2swap dup f@ f+ dup f! over + 2swap
2253: LOOP 2drop 2drop fdrop ;
2254:
2255: \+
2256:
2257: \+file
2258:
2259: (read-line) ( c_addr u1 wfileid -- u2 flag u3 wior ) file paren_read_line
2260: Cell c;
2261: flag=-1;
2262: u3=0;
2263: for(u2=0; u2<u1; u2++)
2264: {
2265: c = getc((FILE *)wfileid);
2266: u3++;
2267: if (c=='\n') break;
2268: if (c=='\r') {
2269: if ((c = getc((FILE *)wfileid))!='\n')
2270: ungetc(c,(FILE *)wfileid);
2271: else
2272: u3++;
2273: break;
2274: }
2275: if (c==EOF) {
2276: flag=FLAG(u2!=0);
2277: break;
2278: }
2279: c_addr[u2] = (Char)c;
2280: }
2281: wior=FILEIO(ferror((FILE *)wfileid));
2282:
2283: \+
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>