File:
[gforth] /
gforth /
Attic /
primitives
Revision
1.50:
download - view:
text,
annotated -
select for diffs
Thu Jan 25 16:45:55 1996 UTC (28 years, 2 months ago) by
anton
Branches:
MAIN
CVS tags:
HEAD
eliminated "make realclean"; it's dangerous and useless.
eliminated relocation stuff in the kernal; was buggy and useless.
SIGPIPE now returns control to Forth.
Implemented form, rows, and cols; SIGWINCH updates these (on good OS's).
changed popen and pclose to open-pipe and close-pipe.
1: \ Gforth primitives
2:
3: \ Copyright (C) 1995 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21:
22: \ WARNING: This file is processed by m4. Make sure your identifiers
23: \ don't collide with m4's (e.g. by undefining them).
24: \
25: \
26: \
27: \ This file contains primitive specifications in the following format:
28: \
29: \ forth name stack effect category [pronunciation]
30: \ [""glossary entry""]
31: \ C code
32: \ [:
33: \ Forth code]
34: \
35: \ prims2x is pedantic about tabs vs. blanks. The fields of the first
36: \ line of a primitive are separated by tabs, the stack items in a
37: \ stack effect by blanks.
38: \
39: \ Both pronounciation and stack items (in the stack effect) must
40: \ conform to the C name syntax or the C compiler will complain.
41: \
42: \
43: \ These specifications are automatically translated into C-code for the
44: \ interpreter and into some other files. I hope that your C compiler has
45: \ decent optimization, otherwise the automatically generated code will
46: \ be somewhat slow. The Forth version of the code is included for manual
47: \ compilers, so they will need to compile only the important words.
48: \
49: \ Note that stack pointer adjustment is performed according to stack
50: \ effect by automatically generated code and NEXT is automatically
51: \ appended to the C code. Also, you can use the names in the stack
52: \ effect in the C code. Stack access is automatic. One exception: if
53: \ your code does not fall through, the results are not stored into the
54: \ stack. Use different names on both sides of the '--', if you change a
55: \ value (some stores to the stack are optimized away).
56: \
57: \
58: \
59: \ The stack variables have the following types:
60: \
61: \ name matches type
62: \ f.* Bool
63: \ c.* Char
64: \ [nw].* Cell
65: \ u.* UCell
66: \ d.* DCell
67: \ ud.* UDCell
68: \ r.* Float
69: \ a_.* Cell *
70: \ c_.* Char *
71: \ f_.* Float *
72: \ df_.* DFloat *
73: \ sf_.* SFloat *
74: \ xt.* XT
75: \ wid.* WID
76: \ f83name.* F83Name *
77: \
78: \
79: \
80: \ In addition the following names can be used:
81: \ ip the instruction pointer
82: \ sp the data stack pointer
83: \ rp the parameter stack pointer
84: \ lp the locals stack pointer
85: \ NEXT executes NEXT
86: \ cfa
87: \ NEXT1 executes NEXT1
88: \ FLAG(x) makes a Forth flag from a C flag
89: \
90: \
91: \
92: \ Percentages in comments are from Koopmans book: average/maximum use
93: \ (taken from four, not very representative benchmarks)
94: \
95: \
96: \
97: \ To do:
98: \
99: \ throw execute, cfa and NEXT1 out?
100: \ macroize *ip, ip++, *ip++ (pipelining)?
101:
102: \ these m4 macros would collide with identifiers
103: undefine(`index')
104: undefine(`shift')
105:
106: noop -- gforth
107: ;
108: :
109: ;
110:
111: lit -- w gforth
112: w = (Cell)NEXT_INST;
113: INC_IP(1);
114:
115: execute xt -- core
116: ip=IP;
117: IF_TOS(TOS = sp[0]);
118: EXEC(xt);
119:
120: branch-lp+!# -- gforth branch_lp_plus_store_number
121: /* this will probably not be used */
122: branch_adjust_lp:
123: lp += (Cell)(IP[1]);
124: goto branch;
125:
126: branch -- gforth
127: branch:
128: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
129: NEXT_P0;
130: :
131: r> dup @ + >r ;
132:
133: \ condbranch(forthname,restline,code)
134: \ this is non-syntactical: code must open a brace that is closed by the macro
135: define(condbranch,
136: $1 $2
137: $3 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
138: NEXT_P0;
139: NEXT;
140: }
141: else
142: INC_IP(1);
143:
144: $1-lp+!# $2_lp_plus_store_number
145: $3 goto branch_adjust_lp;
146: }
147: else
148: INC_IP(2);
149:
150: )
151:
152: condbranch(?branch,f -- f83 question_branch,
153: if (f==0) {
154: IF_TOS(TOS = sp[0]);
155: )
156:
157: condbranch((next),-- cmFORTH paren_next,
158: if ((*rp)--) {
159: )
160:
161: condbranch((loop),-- gforth paren_loop,
162: Cell index = *rp+1;
163: Cell limit = rp[1];
164: if (index != limit) {
165: *rp = index;
166: )
167:
168: condbranch((+loop),n -- gforth paren_plus_loop,
169: /* !! check this thoroughly */
170: Cell index = *rp;
171: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
172: /* dependent upon two's complement arithmetic */
173: Cell olddiff = index-rp[1];
174: #ifndef undefined
175: if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
176: || (olddiff^n)>=0 /* it is a wrap-around effect */) {
177: #else
178: #ifndef MAXINT
179: #define MAXINT ((((Cell)1)<<(8*sizeof(Cell)-1))-1)
180: #endif
181: if(((olddiff^MAXINT) >= n) ^ ((olddiff+n) < 0)) {
182: #endif
183: #ifdef i386
184: *rp += n;
185: #else
186: *rp = index + n;
187: #endif
188: IF_TOS(TOS = sp[0]);
189: )
190:
191: condbranch((-loop),u -- gforth paren_minus_loop,
192: /* !! check this thoroughly */
193: Cell index = *rp;
194: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
195: /* dependent upon two's complement arithmetic */
196: UCell olddiff = index-rp[1];
197: if (olddiff>u) {
198: #ifdef i386
199: *rp -= u;
200: #else
201: *rp = index - u;
202: #endif
203: IF_TOS(TOS = sp[0]);
204: )
205:
206: condbranch((s+loop),n -- gforth paren_symmetric_plus_loop,
207: ""The run-time procedure compiled by S+LOOP. It loops until the index
208: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
209: version of (+LOOP).""
210: /* !! check this thoroughly */
211: Cell index = *rp;
212: Cell diff = index-rp[1];
213: Cell newdiff = diff+n;
214: if (n<0) {
215: diff = -diff;
216: newdiff = -newdiff;
217: }
218: if (diff>=0 || newdiff<0) {
219: #ifdef i386
220: *rp += n;
221: #else
222: *rp = index + n;
223: #endif
224: IF_TOS(TOS = sp[0]);
225: )
226:
227: unloop -- core
228: rp += 2;
229: :
230: r> rdrop rdrop >r ;
231:
232: (for) ncount -- cmFORTH paren_for
233: /* or (for) = >r -- collides with unloop! */
234: *--rp = 0;
235: *--rp = ncount;
236: :
237: r> swap 0 >r >r >r ;
238:
239: (do) nlimit nstart -- gforth paren_do
240: /* or do it in high-level? 0.09/0.23% */
241: *--rp = nlimit;
242: *--rp = nstart;
243: :
244: r> -rot swap >r >r >r ;
245:
246: (?do) nlimit nstart -- gforth paren_question_do
247: *--rp = nlimit;
248: *--rp = nstart;
249: if (nstart == nlimit) {
250: IF_TOS(TOS = sp[0]);
251: goto branch;
252: }
253: else {
254: INC_IP(1);
255: }
256:
257: (+do) nlimit nstart -- gforth paren_plus_do
258: *--rp = nlimit;
259: *--rp = nstart;
260: if (nstart >= nlimit) {
261: IF_TOS(TOS = sp[0]);
262: goto branch;
263: }
264: else {
265: INC_IP(1);
266: }
267:
268: (u+do) ulimit ustart -- gforth paren_u_plus_do
269: *--rp = ulimit;
270: *--rp = ustart;
271: if (ustart >= ulimit) {
272: IF_TOS(TOS = sp[0]);
273: goto branch;
274: }
275: else {
276: INC_IP(1);
277: }
278:
279: (-do) nlimit nstart -- gforth paren_minus_do
280: *--rp = nlimit;
281: *--rp = nstart;
282: if (nstart <= nlimit) {
283: IF_TOS(TOS = sp[0]);
284: goto branch;
285: }
286: else {
287: INC_IP(1);
288: }
289:
290: (u-do) ulimit ustart -- gforth paren_u_minus_do
291: *--rp = ulimit;
292: *--rp = ustart;
293: if (ustart <= ulimit) {
294: IF_TOS(TOS = sp[0]);
295: goto branch;
296: }
297: else {
298: INC_IP(1);
299: }
300:
301: i -- n core
302: n = *rp;
303:
304: j -- n core
305: n = rp[2];
306:
307: \ digit is high-level: 0/0%
308:
309: (emit) c -- gforth paren_emit
310: putchar(c);
311: #if 0
312: emitcounter++;
313: #endif
314:
315: (type) c_addr n -- gforth paren_type
316: fwrite(c_addr,sizeof(Char),n,stdout);
317: #if 0
318: emitcounter += n;
319: #endif
320:
321: (key) -- n gforth paren_key
322: fflush(stdout);
323: /* !! noecho */
324: n = key();
325:
326: key? -- n facility key_q
327: fflush(stdout);
328: n = key_query;
329:
330: form -- urows ucols gforth
331: ""The number of lines and columns in the terminal. These numbers may change
332: with the window size.""
333: /* we could block SIGWINCH here to get a consistent size, but I don't
334: think this is necessary or always beneficial */
335: urows=rows;
336: ucols=cols;
337:
338: move c_from c_to ucount -- core
339: memmove(c_to,c_from,ucount);
340: /* make an Ifdef for bsd and others? */
341: :
342: >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
343:
344: cmove c_from c_to u -- string
345: while (u-- > 0)
346: *c_to++ = *c_from++;
347: :
348: bounds ?DO dup c@ I c! 1+ LOOP drop ;
349:
350: cmove> c_from c_to u -- string c_move_up
351: while (u-- > 0)
352: c_to[u] = c_from[u];
353: :
354: dup 0= IF drop 2drop exit THEN
355: rot over + -rot bounds swap 1-
356: DO 1- dup c@ I c! -1 +LOOP drop ;
357:
358: fill c_addr u c -- core
359: memset(c_addr,c,u);
360: :
361: -rot bounds
362: ?DO dup I c! LOOP drop ;
363:
364: compare c_addr1 u1 c_addr2 u2 -- n string
365: ""Compare the strings lexicographically. If they are equal, n is 0; if
366: the first string is smaller, n is -1; if the first string is larger, n
367: is 1. Currently this is based on the machine's character
368: comparison. In the future, this may change to considering the current
369: locale and its collation order.""
370: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
371: if (n==0)
372: n = u1-u2;
373: if (n<0)
374: n = -1;
375: else if (n>0)
376: n = 1;
377: :
378: rot 2dup - >r min swap -text dup
379: IF rdrop
380: ELSE drop r@ 0>
381: IF rdrop -1
382: ELSE r> 1 and
383: THEN
384: THEN ;
385:
386: -text c_addr1 u c_addr2 -- n new dash_text
387: n = memcmp(c_addr1, c_addr2, u);
388: if (n<0)
389: n = -1;
390: else if (n>0)
391: n = 1;
392: :
393: swap bounds
394: ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
395: ELSE c@ I c@ - unloop THEN -text-flag ;
396: : -text-flag ( n -- -1/0/1 )
397: dup 0< IF drop -1 ELSE 0> IF 1 ELSE 0 THEN THEN ;
398:
399: capscomp c_addr1 u c_addr2 -- n new
400: Char c1, c2;
401: for (;; u--, c_addr1++, c_addr2++) {
402: if (u == 0) {
403: n = 0;
404: break;
405: }
406: c1 = toupper(*c_addr1);
407: c2 = toupper(*c_addr2);
408: if (c1 != c2) {
409: if (c1 < c2)
410: n = -1;
411: else
412: n = 1;
413: break;
414: }
415: }
416: :
417: swap bounds
418: ?DO dup c@ toupper I c@ toupper = WHILE 1+ LOOP drop 0
419: ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
420:
421: -trailing c_addr u1 -- c_addr u2 string dash_trailing
422: u2 = u1;
423: while (c_addr[u2-1] == ' ')
424: u2--;
425: :
426: BEGIN 1- 2dup + c@ bl = WHILE
427: dup 0= UNTIL ELSE 1+ THEN ;
428:
429: /string c_addr1 u1 n -- c_addr2 u2 string slash_string
430: c_addr2 = c_addr1+n;
431: u2 = u1-n;
432: :
433: tuck - >r + r> dup 0< IF - 0 THEN ;
434:
435: + n1 n2 -- n core plus
436: n = n1+n2;
437:
438: - n1 n2 -- n core minus
439: n = n1-n2;
440: :
441: negate + ;
442:
443: negate n1 -- n2 core
444: /* use minus as alias */
445: n2 = -n1;
446: :
447: invert 1+ ;
448:
449: 1+ n1 -- n2 core one_plus
450: n2 = n1+1;
451: :
452: 1 + ;
453:
454: 1- n1 -- n2 core one_minus
455: n2 = n1-1;
456: :
457: 1 - ;
458:
459: max n1 n2 -- n core
460: if (n1<n2)
461: n = n2;
462: else
463: n = n1;
464: :
465: 2dup < IF swap THEN drop ;
466:
467: min n1 n2 -- n core
468: if (n1<n2)
469: n = n1;
470: else
471: n = n2;
472: :
473: 2dup > IF swap THEN drop ;
474:
475: abs n1 -- n2 core
476: if (n1<0)
477: n2 = -n1;
478: else
479: n2 = n1;
480: :
481: dup 0< IF negate THEN ;
482:
483: * n1 n2 -- n core star
484: n = n1*n2;
485: :
486: um* drop ;
487:
488: / n1 n2 -- n core slash
489: n = n1/n2;
490: :
491: /mod nip ;
492:
493: mod n1 n2 -- n core
494: n = n1%n2;
495: :
496: /mod drop ;
497:
498: /mod n1 n2 -- n3 n4 core slash_mod
499: n4 = n1/n2;
500: n3 = n1%n2; /* !! is this correct? look into C standard! */
501: :
502: >r s>d r> fm/mod ;
503:
504: 2* n1 -- n2 core two_star
505: n2 = 2*n1;
506: :
507: dup + ;
508:
509: 2/ n1 -- n2 core two_slash
510: /* !! is this still correct? */
511: n2 = n1>>1;
512:
513: fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
514: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
515: /* assumes that the processor uses either floored or symmetric division */
516: n3 = d1/n1;
517: n2 = d1%n1;
518: /* note that this 1%-3>0 is optimized by the compiler */
519: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
520: n3--;
521: n2+=n1;
522: }
523:
524: sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
525: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
526: /* assumes that the processor uses either floored or symmetric division */
527: n3 = d1/n1;
528: n2 = d1%n1;
529: /* note that this 1%-3<0 is optimized by the compiler */
530: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
531: n3++;
532: n2-=n1;
533: }
534: :
535: over >r dup >r abs -rot
536: dabs rot um/mod
537: r> 0< IF negate THEN
538: r> 0< IF swap negate swap THEN ;
539:
540: m* n1 n2 -- d core m_star
541: d = (DCell)n1 * (DCell)n2;
542: :
543: 2dup 0< and >r
544: 2dup swap 0< and >r
545: um* r> - r> - ;
546:
547: um* u1 u2 -- ud core u_m_star
548: /* use u* as alias */
549: ud = (UDCell)u1 * (UDCell)u2;
550:
551: um/mod ud u1 -- u2 u3 core u_m_slash_mod
552: u3 = ud/u1;
553: u2 = ud%u1;
554: :
555: dup IF 0 (um/mod) THEN nip ;
556: : (um/mod) ( ud ud--ud u)
557: 2dup >r >r dup 0<
558: IF 2drop 0
559: ELSE 2dup d+ (um/mod) 2* THEN
560: -rot r> r> 2over 2over du<
561: IF 2drop rot
562: ELSE dnegate d+ rot 1+ THEN ;
563:
564: m+ d1 n -- d2 double m_plus
565: d2 = d1+n;
566: :
567: s>d d+ ;
568:
569: d+ d1 d2 -- d double d_plus
570: d = d1+d2;
571: :
572: >r swap >r over 2/ over 2/ + >r over 1 and over 1 and + 2/
573: r> + >r + r> 0< r> r> + swap - ;
574:
575: d- d1 d2 -- d double d_minus
576: d = d1-d2;
577: :
578: dnegate d+ ;
579:
580: dnegate d1 -- d2 double
581: /* use dminus as alias */
582: d2 = -d1;
583: :
584: invert swap negate tuck 0= - ;
585:
586: dmax d1 d2 -- d double
587: if (d1<d2)
588: d = d2;
589: else
590: d = d1;
591: :
592: 2over 2over d> IF 2swap THEN 2drop ;
593:
594: dmin d1 d2 -- d double
595: if (d1<d2)
596: d = d1;
597: else
598: d = d2;
599: :
600: 2over 2over d< IF 2swap THEN 2drop ;
601:
602: dabs d1 -- d2 double
603: if (d1<0)
604: d2 = -d1;
605: else
606: d2 = d1;
607: :
608: dup 0< IF dnegate THEN ;
609:
610: d2* d1 -- d2 double d_two_star
611: d2 = 2*d1;
612: :
613: 2dup d+ ;
614:
615: d2/ d1 -- d2 double d_two_slash
616: /* !! is this still correct? */
617: d2 = d1>>1;
618: :
619: dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
620: r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
621:
622: d>s d -- n double d_to_s
623: /* make this an alias for drop? */
624: n = d;
625: :
626: drop ;
627:
628: and w1 w2 -- w core
629: w = w1&w2;
630:
631: or w1 w2 -- w core
632: w = w1|w2;
633:
634: xor w1 w2 -- w core
635: w = w1^w2;
636:
637: invert w1 -- w2 core
638: w2 = ~w1;
639: :
640: -1 xor ;
641:
642: rshift u1 n -- u2 core
643: u2 = u1>>n;
644:
645: lshift u1 n -- u2 core
646: u2 = u1<<n;
647:
648: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
649: define(comparisons,
650: $1= $2 -- f $6 $3equals
651: f = FLAG($4==$5);
652:
653: $1<> $2 -- f $7 $3different
654: /* use != as alias ? */
655: f = FLAG($4!=$5);
656:
657: $1< $2 -- f $8 $3less
658: f = FLAG($4<$5);
659:
660: $1> $2 -- f $9 $3greater
661: f = FLAG($4>$5);
662:
663: $1<= $2 -- f gforth $3less_or_equal
664: f = FLAG($4<=$5);
665:
666: $1>= $2 -- f gforth $3greater_or_equal
667: f = FLAG($4>=$5);
668:
669: )
670:
671: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
672: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
673: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
674: comparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
675: comparisons(d0, d, d_zero_, d, 0, double, gforth, double, gforth)
676: comparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
677:
678: within u1 u2 u3 -- f core-ext
679: f = FLAG(u1-u2 < u3-u2);
680: :
681: over - >r - r> u< ;
682:
683: sp@ -- a_addr gforth spat
684: a_addr = sp+1;
685:
686: sp! a_addr -- gforth spstore
687: sp = a_addr;
688: /* works with and without TOS caching */
689:
690: rp@ -- a_addr gforth rpat
691: a_addr = rp;
692:
693: rp! a_addr -- gforth rpstore
694: rp = a_addr;
695:
696: fp@ -- f_addr gforth fp_fetch
697: f_addr = fp;
698:
699: fp! f_addr -- gforth fp_store
700: fp = f_addr;
701:
702: ;s -- gforth semis
703: ip = (Xt *)(*rp++);
704: NEXT_P0;
705:
706: >r w -- core to_r
707: *--rp = w;
708:
709: r> -- w core r_from
710: w = *rp++;
711:
712: r@ -- w core r_fetch
713: /* use r as alias */
714: /* make r@ an alias for i */
715: w = *rp;
716:
717: rdrop -- gforth
718: rp++;
719:
720: i' -- w gforth i_tick
721: w=rp[1];
722:
723: 2>r w1 w2 -- core-ext two_to_r
724: *--rp = w1;
725: *--rp = w2;
726:
727: 2r> -- w1 w2 core-ext two_r_from
728: w2 = *rp++;
729: w1 = *rp++;
730:
731: 2r@ -- w1 w2 core-ext two_r_fetch
732: w2 = rp[0];
733: w1 = rp[1];
734:
735: 2rdrop -- gforth two_r_drop
736: rp+=2;
737:
738: over w1 w2 -- w1 w2 w1 core
739:
740: drop w -- core
741:
742: swap w1 w2 -- w2 w1 core
743:
744: dup w -- w w core
745:
746: rot w1 w2 w3 -- w2 w3 w1 core rote
747:
748: -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
749: :
750: rot rot ;
751:
752: nip w1 w2 -- w2 core-ext
753: :
754: swap drop ;
755:
756: tuck w1 w2 -- w2 w1 w2 core-ext
757: :
758: swap over ;
759:
760: ?dup w -- w core question_dupe
761: if (w!=0) {
762: IF_TOS(*sp-- = w;)
763: #ifndef USE_TOS
764: *--sp = w;
765: #endif
766: }
767: :
768: dup IF dup THEN ;
769:
770: pick u -- w core-ext
771: w = sp[u+1];
772: :
773: 1+ cells sp@ + @ ;
774:
775: 2drop w1 w2 -- core two_drop
776: :
777: drop drop ;
778:
779: 2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
780: :
781: over over ;
782:
783: 2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
784: :
785: 3 pick 3 pick ;
786:
787: 2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
788: :
789: >r -rot r> -rot ;
790:
791: 2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double-ext two_rote
792: :
793: >r >r 2swap r> r> 2swap ;
794:
795: 2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
796: :
797: 2swap 2drop ;
798:
799: 2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
800: :
801: 2swap 2over ;
802:
803: \ toggle is high-level: 0.11/0.42%
804:
805: @ a_addr -- w core fetch
806: w = *a_addr;
807:
808: ! w a_addr -- core store
809: *a_addr = w;
810:
811: +! n a_addr -- core plus_store
812: *a_addr += n;
813:
814: c@ c_addr -- c core cfetch
815: c = *c_addr;
816:
817: c! c c_addr -- core cstore
818: *c_addr = c;
819:
820: 2! w1 w2 a_addr -- core two_store
821: a_addr[0] = w2;
822: a_addr[1] = w1;
823: :
824: tuck ! cell+ ! ;
825:
826: 2@ a_addr -- w1 w2 core two_fetch
827: w2 = a_addr[0];
828: w1 = a_addr[1];
829: :
830: dup cell+ @ swap @ ;
831:
832: d! d a_addr -- double d_store
833: /* !! alignment problems on some machines */
834: *(DCell *)a_addr = d;
835:
836: d@ a_addr -- d double d_fetch
837: d = *(DCell *)a_addr;
838:
839: cell+ a_addr1 -- a_addr2 core cell_plus
840: a_addr2 = a_addr1+1;
841: :
842: [ cell ] Literal + ;
843:
844: cells n1 -- n2 core
845: n2 = n1 * sizeof(Cell);
846: :
847: [ cell ]
848: [ 2/ dup ] [IF] 2* [THEN]
849: [ 2/ dup ] [IF] 2* [THEN]
850: [ 2/ dup ] [IF] 2* [THEN]
851: [ 2/ dup ] [IF] 2* [THEN]
852: [ drop ] ;
853:
854: char+ c_addr1 -- c_addr2 core care_plus
855: c_addr2 = c_addr1 + 1;
856: :
857: 1+ ;
858:
859: (chars) n1 -- n2 gforth paren_cares
860: n2 = n1 * sizeof(Char);
861: :
862: ;
863:
864: count c_addr1 -- c_addr2 u core
865: u = *c_addr1;
866: c_addr2 = c_addr1+1;
867: :
868: dup 1+ swap c@ ;
869:
870: (bye) n -- gforth paren_bye
871: return (Label *)n;
872:
873: system c_addr u -- n gforth
874: int old_tp=terminal_prepped;
875: deprep_terminal();
876: n=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
877: if (old_tp)
878: prep_terminal();
879:
880: getenv c_addr1 u1 -- c_addr2 u2 gforth
881: c_addr2 = getenv(cstr(c_addr1,u1,1));
882: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
883:
884: open-pipe c_addr u ntype -- wfileid wior gforth open_pipe
885: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
886: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
887:
888: close-pipe wfileid -- wior gforth close_pipe
889: wior = IOR(pclose((FILE *)wfileid)==-1);
890:
891: time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
892: struct timeval time1;
893: struct timezone zone1;
894: struct tm *ltime;
895: gettimeofday(&time1,&zone1);
896: ltime=localtime((time_t *)&time1.tv_sec);
897: nyear =ltime->tm_year+1900;
898: nmonth=ltime->tm_mon+1;
899: nday =ltime->tm_mday;
900: nhour =ltime->tm_hour;
901: nmin =ltime->tm_min;
902: nsec =ltime->tm_sec;
903:
904: ms n -- facility-ext
905: struct timeval timeout;
906: timeout.tv_sec=n/1000;
907: timeout.tv_usec=1000*(n%1000);
908: (void)select(0,0,0,0,&timeout);
909:
910: allocate u -- a_addr wior memory
911: a_addr = (Cell *)malloc(u?u:1);
912: wior = IOR(a_addr==NULL);
913:
914: free a_addr -- wior memory
915: free(a_addr);
916: wior = 0;
917:
918: resize a_addr1 u -- a_addr2 wior memory
919: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
920: address units, possibly moving the contents to a different
921: area. @i{a_addr2} is the address of the resulting area. If
922: @code{a_addr2} is 0, Gforth's (but not the standard) @code{resize}
923: @code{allocate}s @i{u} address units.""
924: /* the following check is not necessary on most OSs, but it is needed
925: on SunOS 4.1.2. */
926: if (a_addr1==NULL)
927: a_addr2 = (Cell *)malloc(u);
928: else
929: a_addr2 = (Cell *)realloc(a_addr1, u);
930: wior = IOR(a_addr2==NULL); /* !! Define a return code */
931:
932: (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
933: for (; f83name1 != NULL; f83name1 = f83name1->next)
934: if (F83NAME_COUNT(f83name1)==u &&
935: strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
936: break;
937: f83name2=f83name1;
938: :
939: BEGIN dup WHILE
940: >r dup r@ cell+ c@ $1F and =
941: IF 2dup r@ cell+ char+ capscomp 0=
942: IF 2drop r> EXIT THEN THEN
943: r> @
944: REPEAT nip nip ;
945:
946: (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
947: F83Name *f83name1;
948: f83name2=NULL;
949: while(a_addr != NULL)
950: {
951: f83name1=(F83Name *)(a_addr[1]);
952: a_addr=(Cell *)(a_addr[0]);
953: if (F83NAME_COUNT(f83name1)==u &&
954: strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
955: {
956: f83name2=f83name1;
957: break;
958: }
959: }
960: :
961: BEGIN dup WHILE
962: 2@ >r >r dup r@ cell+ c@ $1F and =
963: IF 2dup r@ cell+ char+ capscomp 0=
964: IF 2drop r> rdrop EXIT THEN THEN
965: rdrop r>
966: REPEAT nip nip ;
967:
968: (hashkey) c_addr u1 -- u2 gforth paren_hashkey
969: u2=0;
970: while(u1--)
971: u2+=(Cell)toupper(*c_addr++);
972: :
973: 0 -rot bounds ?DO I c@ toupper + LOOP ;
974:
975: (hashkey1) c_addr u ubits -- ukey gforth paren_hashkey1
976: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
977: /* this hash function rotates the key at every step by rot bits within
978: ubits bits and xors it with the character. This function does ok in
979: the chi-sqare-test. Rot should be <=7 (preferably <=5) for
980: ASCII strings (larger if ubits is large), and should share no
981: divisors with ubits.
982: */
983: 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];
984: Char *cp = c_addr;
985: for (ukey=0; cp<c_addr+u; cp++)
986: ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
987: ^ toupper(*cp))
988: & ((1<<ubits)-1));
989: :
990: dup rot-values + c@ over 1 swap lshift 1- >r
991: tuck - 2swap r> 0 2swap bounds
992: ?DO dup 4 pick lshift swap 3 pick rshift or
993: I c@ toupper xor
994: over and LOOP
995: nip nip nip ;
996: Create rot-values
997: 5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
998: 3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
999: 7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1000: 7 c, 5 c, 5 c,
1001:
1002: (parse-white) c_addr1 u1 -- c_addr2 u2 gforth paren_parse_white
1003: /* use !isgraph instead of isspace? */
1004: Char *endp = c_addr1+u1;
1005: while (c_addr1<endp && isspace(*c_addr1))
1006: c_addr1++;
1007: if (c_addr1<endp) {
1008: for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1009: ;
1010: u2 = c_addr1-c_addr2;
1011: }
1012: else {
1013: c_addr2 = c_addr1;
1014: u2 = 0;
1015: }
1016: :
1017: BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1018: REPEAT THEN 2dup
1019: BEGIN dup WHILE over c@ bl > WHILE 1 /string
1020: REPEAT THEN nip - ;
1021:
1022: close-file wfileid -- wior file close_file
1023: wior = IOR(fclose((FILE *)wfileid)==EOF);
1024:
1025: open-file c_addr u ntype -- w2 wior file open_file
1026: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1027: wior = IOR(w2 == 0);
1028:
1029: create-file c_addr u ntype -- w2 wior file create_file
1030: Cell fd;
1031: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_RDWR|O_TRUNC, 0666);
1032: if (fd != -1) {
1033: w2 = (Cell)fdopen(fd, fileattr[ntype]);
1034: wior = IOR(w2 == 0);
1035: } else {
1036: w2 = 0;
1037: wior = IOR(1);
1038: }
1039:
1040: delete-file c_addr u -- wior file delete_file
1041: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1042:
1043: rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1044: char *s1=tilde_cstr(c_addr2, u2, 1);
1045: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1046:
1047: file-position wfileid -- ud wior file file_position
1048: /* !! use tell and lseek? */
1049: ud = ftell((FILE *)wfileid);
1050: wior = IOR(ud==-1);
1051:
1052: reposition-file ud wfileid -- wior file reposition_file
1053: wior = IOR(fseek((FILE *)wfileid, (long)ud, SEEK_SET)==-1);
1054:
1055: file-size wfileid -- ud wior file file_size
1056: struct stat buf;
1057: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1058: ud = buf.st_size;
1059:
1060: resize-file ud wfileid -- wior file resize_file
1061: wior = IOR(ftruncate(fileno((FILE *)wfileid), (Cell)ud)==-1);
1062:
1063: read-file c_addr u1 wfileid -- u2 wior file read_file
1064: /* !! fread does not guarantee enough */
1065: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1066: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1067: /* !! is the value of ferror errno-compatible? */
1068: if (wior)
1069: clearerr((FILE *)wfileid);
1070:
1071: read-line c_addr u1 wfileid -- u2 flag wior file read_line
1072: /*
1073: Cell c;
1074: flag=-1;
1075: for(u2=0; u2<u1; u2++)
1076: {
1077: *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1078: if(c=='\n') break;
1079: if(c==EOF)
1080: {
1081: flag=FLAG(u2!=0);
1082: break;
1083: }
1084: }
1085: wior=FILEIO(ferror((FILE *)wfileid));
1086: */
1087: if ((flag=FLAG(!feof((FILE *)wfileid) &&
1088: fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1089: wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1090: if (wior)
1091: clearerr((FILE *)wfileid);
1092: u2 = strlen(c_addr);
1093: u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1094: }
1095: else {
1096: wior=0;
1097: u2=0;
1098: }
1099:
1100: write-file c_addr u1 wfileid -- wior file write_file
1101: /* !! fwrite does not guarantee enough */
1102: {
1103: Cell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1104: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1105: if (wior)
1106: clearerr((FILE *)wfileid);
1107: }
1108:
1109: flush-file wfileid -- wior file-ext flush_file
1110: wior = IOR(fflush((FILE *) wfileid)==EOF);
1111:
1112: file-status c_addr u -- ntype wior file-ext file_status
1113: char *filename=tilde_cstr(c_addr, u, 1);
1114: if (access (filename, F_OK) != 0) {
1115: ntype=0;
1116: wior=IOR(1);
1117: }
1118: else if (access (filename, R_OK | W_OK) == 0) {
1119: ntype=2; /* r/w */
1120: wior=0;
1121: }
1122: else if (access (filename, R_OK) == 0) {
1123: ntype=0; /* r/o */
1124: wior=0;
1125: }
1126: else if (access (filename, W_OK) == 0) {
1127: ntype=4; /* w/o */
1128: wior=0;
1129: }
1130: else {
1131: ntype=1; /* well, we cannot access the file, but better deliver a legal
1132: access mode (r/o bin), so we get a decent error later upon open. */
1133: wior=0;
1134: }
1135:
1136: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1137: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1138:
1139: d>f d -- r float d_to_f
1140: r = d;
1141:
1142: f>d r -- d float f_to_d
1143: /* !! basis 15 is not very specific */
1144: d = r;
1145:
1146: f! r f_addr -- float f_store
1147: *f_addr = r;
1148:
1149: f@ f_addr -- r float f_fetch
1150: r = *f_addr;
1151:
1152: df@ df_addr -- r float-ext d_f_fetch
1153: #ifdef IEEE_FP
1154: r = *df_addr;
1155: #else
1156: !! df@
1157: #endif
1158:
1159: df! r df_addr -- float-ext d_f_store
1160: #ifdef IEEE_FP
1161: *df_addr = r;
1162: #else
1163: !! df!
1164: #endif
1165:
1166: sf@ sf_addr -- r float-ext s_f_fetch
1167: #ifdef IEEE_FP
1168: r = *sf_addr;
1169: #else
1170: !! sf@
1171: #endif
1172:
1173: sf! r sf_addr -- float-ext s_f_store
1174: #ifdef IEEE_FP
1175: *sf_addr = r;
1176: #else
1177: !! sf!
1178: #endif
1179:
1180: f+ r1 r2 -- r3 float f_plus
1181: r3 = r1+r2;
1182:
1183: f- r1 r2 -- r3 float f_minus
1184: r3 = r1-r2;
1185:
1186: f* r1 r2 -- r3 float f_star
1187: r3 = r1*r2;
1188:
1189: f/ r1 r2 -- r3 float f_slash
1190: r3 = r1/r2;
1191:
1192: f** r1 r2 -- r3 float-ext f_star_star
1193: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1194: r3 = pow(r1,r2);
1195:
1196: fnegate r1 -- r2 float
1197: r2 = - r1;
1198:
1199: fdrop r -- float
1200:
1201: fdup r -- r r float
1202:
1203: fswap r1 r2 -- r2 r1 float
1204:
1205: fover r1 r2 -- r1 r2 r1 float
1206:
1207: frot r1 r2 r3 -- r2 r3 r1 float
1208:
1209: fnip r1 r2 -- r2 gforth
1210:
1211: ftuck r1 r2 -- r2 r1 r2 gforth
1212:
1213: float+ f_addr1 -- f_addr2 float float_plus
1214: f_addr2 = f_addr1+1;
1215:
1216: floats n1 -- n2 float
1217: n2 = n1*sizeof(Float);
1218:
1219: floor r1 -- r2 float
1220: ""round towards the next smaller integral value, i.e., round toward negative infinity""
1221: /* !! unclear wording */
1222: r2 = floor(r1);
1223:
1224: fround r1 -- r2 float
1225: ""round to the nearest integral value""
1226: /* !! unclear wording */
1227: #ifdef HAVE_RINT
1228: r2 = rint(r1);
1229: #else
1230: r2 = floor(r1+0.5);
1231: /* !! This is not quite true to the rounding rules given in the standard */
1232: #endif
1233:
1234: fmax r1 r2 -- r3 float
1235: if (r1<r2)
1236: r3 = r2;
1237: else
1238: r3 = r1;
1239:
1240: fmin r1 r2 -- r3 float
1241: if (r1<r2)
1242: r3 = r1;
1243: else
1244: r3 = r2;
1245:
1246: represent r c_addr u -- n f1 f2 float
1247: char *sig;
1248: Cell flag;
1249: Cell decpt;
1250: sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
1251: n=(r==0 ? 1 : decpt);
1252: f1=FLAG(flag!=0);
1253: f2=FLAG(isdigit(sig[0])!=0);
1254: memmove(c_addr,sig,u);
1255:
1256: >float c_addr u -- flag float to_float
1257: /* real signature: c_addr u -- r t / f */
1258: Float r;
1259: char *number=cstr(c_addr, u, 1);
1260: char *endconv;
1261: while(isspace(number[--u]) && u>0);
1262: switch(number[u])
1263: {
1264: case 'd':
1265: case 'D':
1266: case 'e':
1267: case 'E': break;
1268: default : u++; break;
1269: }
1270: number[u]='\0';
1271: r=strtod(number,&endconv);
1272: if((flag=FLAG(!(Cell)*endconv)))
1273: {
1274: IF_FTOS(fp[0] = FTOS);
1275: fp += -1;
1276: FTOS = r;
1277: }
1278: else if(*endconv=='d' || *endconv=='D')
1279: {
1280: *endconv='E';
1281: r=strtod(number,&endconv);
1282: if((flag=FLAG(!(Cell)*endconv)))
1283: {
1284: IF_FTOS(fp[0] = FTOS);
1285: fp += -1;
1286: FTOS = r;
1287: }
1288: }
1289:
1290: fabs r1 -- r2 float-ext
1291: r2 = fabs(r1);
1292:
1293: facos r1 -- r2 float-ext
1294: r2 = acos(r1);
1295:
1296: fasin r1 -- r2 float-ext
1297: r2 = asin(r1);
1298:
1299: fatan r1 -- r2 float-ext
1300: r2 = atan(r1);
1301:
1302: fatan2 r1 r2 -- r3 float-ext
1303: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1304: intends this to be the inverse of @code{fsincos}. In gforth it is.""
1305: r3 = atan2(r1,r2);
1306:
1307: fcos r1 -- r2 float-ext
1308: r2 = cos(r1);
1309:
1310: fexp r1 -- r2 float-ext
1311: r2 = exp(r1);
1312:
1313: fexpm1 r1 -- r2 float-ext
1314: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1315: #ifdef HAVE_EXPM1
1316: extern double expm1(double);
1317: r2 = expm1(r1);
1318: #else
1319: r2 = exp(r1)-1.;
1320: #endif
1321:
1322: fln r1 -- r2 float-ext
1323: r2 = log(r1);
1324:
1325: flnp1 r1 -- r2 float-ext
1326: ""@i{r2}=ln(@i{r1}+1)""
1327: #ifdef HAVE_LOG1P
1328: extern double log1p(double);
1329: r2 = log1p(r1);
1330: #else
1331: r2 = log(r1+1.);
1332: #endif
1333:
1334: flog r1 -- r2 float-ext
1335: ""the decimal logarithm""
1336: r2 = log10(r1);
1337:
1338: falog r1 -- r2 float-ext
1339: ""@i{r2}=10**@i{r1}""
1340: extern double pow10(double);
1341: r2 = pow10(r1);
1342:
1343: fsin r1 -- r2 float-ext
1344: r2 = sin(r1);
1345:
1346: fsincos r1 -- r2 r3 float-ext
1347: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1348: r2 = sin(r1);
1349: r3 = cos(r1);
1350:
1351: fsqrt r1 -- r2 float-ext
1352: r2 = sqrt(r1);
1353:
1354: ftan r1 -- r2 float-ext
1355: r2 = tan(r1);
1356: :
1357: fsincos f/ ;
1358:
1359: fsinh r1 -- r2 float-ext
1360: r2 = sinh(r1);
1361: :
1362: fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1363:
1364: fcosh r1 -- r2 float-ext
1365: r2 = cosh(r1);
1366: :
1367: fexp fdup 1/f f+ f2/ ;
1368:
1369: ftanh r1 -- r2 float-ext
1370: r2 = tanh(r1);
1371: :
1372: f2* fexpm1 fdup 2. d>f f+ f/ ;
1373:
1374: fasinh r1 -- r2 float-ext
1375: r2 = asinh(r1);
1376: :
1377: fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1378:
1379: facosh r1 -- r2 float-ext
1380: r2 = acosh(r1);
1381: :
1382: fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1383:
1384: fatanh r1 -- r2 float-ext
1385: r2 = atanh(r1);
1386: :
1387: fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1388: r> IF fnegate THEN ;
1389:
1390: sfloats n1 -- n2 float-ext s_floats
1391: n2 = n1*sizeof(SFloat);
1392:
1393: dfloats n1 -- n2 float-ext d_floats
1394: n2 = n1*sizeof(DFloat);
1395:
1396: aligned c_addr -- a_addr core
1397: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1398: :
1399: [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1400:
1401: faligned c_addr -- f_addr float f_aligned
1402: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1403: :
1404: [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1405:
1406: sfaligned c_addr -- sf_addr float-ext s_f_aligned
1407: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1408: :
1409: [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1410:
1411: dfaligned c_addr -- df_addr float-ext d_f_aligned
1412: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1413: :
1414: [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1415:
1416: \ The following words access machine/OS/installation-dependent
1417: \ Gforth internals
1418: \ !! how about environmental queries DIRECT-THREADED,
1419: \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1420:
1421: >body xt -- a_addr core to_body
1422: a_addr = PFA(xt);
1423:
1424: >code-address xt -- c_addr gforth to_code_address
1425: ""c_addr is the code address of the word xt""
1426: /* !! This behaves installation-dependently for DOES-words */
1427: c_addr = CODE_ADDRESS(xt);
1428:
1429: >does-code xt -- a_addr gforth to_does_code
1430: ""If xt ist the execution token of a defining-word-defined word,
1431: a_addr is the start of the Forth code after the DOES>; Otherwise the
1432: behaviour is undefined""
1433: /* !! there is currently no way to determine whether a word is
1434: defining-word-defined */
1435: a_addr = (Cell *)DOES_CODE(xt);
1436:
1437: code-address! c_addr xt -- gforth code_address_store
1438: ""Creates a code field with code address c_addr at xt""
1439: MAKE_CF(xt, c_addr);
1440: CACHE_FLUSH(xt,PFA(0));
1441:
1442: does-code! a_addr xt -- gforth does_code_store
1443: ""creates a code field at xt for a defining-word-defined word; a_addr
1444: is the start of the Forth code after DOES>""
1445: MAKE_DOES_CF(xt, a_addr);
1446: CACHE_FLUSH(xt,PFA(0));
1447:
1448: does-handler! a_addr -- gforth does_handler_store
1449: ""creates a DOES>-handler at address a_addr. a_addr usually points
1450: just behind a DOES>.""
1451: MAKE_DOES_HANDLER(a_addr);
1452: CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1453:
1454: /does-handler -- n gforth slash_does_handler
1455: ""the size of a does-handler (includes possible padding)""
1456: /* !! a constant or environmental query might be better */
1457: n = DOES_HANDLER_SIZE;
1458:
1459: flush-icache c_addr u -- gforth flush_icache
1460: ""Make sure that the instruction cache of the processor (if there is
1461: one) does not contain stale data at @var{c_addr} and @var{u} bytes
1462: afterwards. @code{END-CODE} performs a @code{flush-icache}
1463: automatically. Caveat: @code{flush-icache} might not work on your
1464: installation; this is usually the case if direct threading is not
1465: supported on your machine (take a look at your @file{machine.h}) and
1466: your machine has a separate instruction cache. In such cases,
1467: @code{flush-icache} does nothing instead of flushing the instruction
1468: cache.""
1469: FLUSH_ICACHE(c_addr,u);
1470:
1471: toupper c1 -- c2 gforth
1472: c2 = toupper(c1);
1473:
1474: \ local variable implementation primitives
1475: @local# -- w gforth fetch_local_number
1476: w = *(Cell *)(lp+(Cell)NEXT_INST);
1477: INC_IP(1);
1478:
1479: @local0 -- w new fetch_local_zero
1480: w = *(Cell *)(lp+0*sizeof(Cell));
1481:
1482: @local1 -- w new fetch_local_four
1483: w = *(Cell *)(lp+1*sizeof(Cell));
1484:
1485: @local2 -- w new fetch_local_eight
1486: w = *(Cell *)(lp+2*sizeof(Cell));
1487:
1488: @local3 -- w new fetch_local_twelve
1489: w = *(Cell *)(lp+3*sizeof(Cell));
1490:
1491: f@local# -- r gforth f_fetch_local_number
1492: r = *(Float *)(lp+(Cell)NEXT_INST);
1493: INC_IP(1);
1494:
1495: f@local0 -- r new f_fetch_local_zero
1496: r = *(Float *)(lp+0*sizeof(Float));
1497:
1498: f@local1 -- r new f_fetch_local_eight
1499: r = *(Float *)(lp+1*sizeof(Float));
1500:
1501: laddr# -- c_addr gforth laddr_number
1502: /* this can also be used to implement lp@ */
1503: c_addr = (Char *)(lp+(Cell)NEXT_INST);
1504: INC_IP(1);
1505:
1506: lp+!# -- gforth lp_plus_store_number
1507: ""used with negative immediate values it allocates memory on the
1508: local stack, a positive immediate argument drops memory from the local
1509: stack""
1510: lp += (Cell)NEXT_INST;
1511: INC_IP(1);
1512:
1513: lp- -- new minus_four_lp_plus_store
1514: lp += -sizeof(Cell);
1515:
1516: lp+ -- new eight_lp_plus_store
1517: lp += sizeof(Float);
1518:
1519: lp+2 -- new sixteen_lp_plus_store
1520: lp += 2*sizeof(Float);
1521:
1522: lp! c_addr -- gforth lp_store
1523: lp = (Address)c_addr;
1524:
1525: >l w -- gforth to_l
1526: lp -= sizeof(Cell);
1527: *(Cell *)lp = w;
1528:
1529: f>l r -- gforth f_to_l
1530: lp -= sizeof(Float);
1531: *(Float *)lp = r;
1532:
1533: up! a_addr -- gforth up_store
1534: up0=up=(char *)a_addr;
1535:
1536: call-c w -- gforth call_c
1537: ""Call the C function pointed to by @i{w}. The C function has to
1538: access the stack itself. The stack pointers are exported in the gloabl
1539: variables @code{SP} and @code{FP}.""
1540: /* This is a first attempt at support for calls to C. This may change in
1541: the future */
1542: IF_FTOS(fp[0]=FTOS);
1543: FP=fp;
1544: SP=sp;
1545: ((void (*)())w)();
1546: sp=SP;
1547: fp=FP;
1548: IF_TOS(TOS=sp[0]);
1549: IF_FTOS(FTOS=fp[0]);
1550:
1551: strerror n -- c_addr u gforth
1552: c_addr = strerror(n);
1553: u = strlen(c_addr);
1554:
1555: strsignal n -- c_addr u gforth
1556: c_addr = strsignal(n);
1557: u = strlen(c_addr);
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>