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