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