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: emitcounter++;
312:
313: (type) c_addr n -- gforth paren_type
314: fwrite(c_addr,sizeof(Char),n,stdout);
315: emitcounter += n;
316:
317: (key) -- n gforth paren_key
318: fflush(stdout);
319: /* !! noecho */
320: n = key();
321:
322: key? -- n facility key_q
323: fflush(stdout);
324: n = key_query;
325:
326: cr -- core
327: puts("");
328: :
329: $0A emit ;
330:
331: move c_from c_to ucount -- core
332: memmove(c_to,c_from,ucount);
333: /* make an Ifdef for bsd and others? */
334: :
335: >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
336:
337: cmove c_from c_to u -- string
338: while (u-- > 0)
339: *c_to++ = *c_from++;
340: :
341: bounds ?DO dup c@ I c! 1+ LOOP drop ;
342:
343: cmove> c_from c_to u -- string c_move_up
344: while (u-- > 0)
345: c_to[u] = c_from[u];
346: :
347: dup 0= IF drop 2drop exit THEN
348: rot over + -rot bounds swap 1-
349: DO 1- dup c@ I c! -1 +LOOP drop ;
350:
351: fill c_addr u c -- core
352: memset(c_addr,c,u);
353: :
354: -rot bounds
355: ?DO dup I c! LOOP drop ;
356:
357: compare c_addr1 u1 c_addr2 u2 -- n string
358: ""Compare the strings lexicographically. If they are equal, n is 0; if
359: the first string is smaller, n is -1; if the first string is larger, n
360: is 1. Currently this is based on the machine's character
361: comparison. In the future, this may change to considering the current
362: locale and its collation order.""
363: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
364: if (n==0)
365: n = u1-u2;
366: if (n<0)
367: n = -1;
368: else if (n>0)
369: n = 1;
370: :
371: rot 2dup - >r min swap -text dup
372: IF rdrop
373: ELSE drop r@ 0>
374: IF rdrop -1
375: ELSE r> 1 and
376: THEN
377: THEN ;
378:
379: -text c_addr1 u c_addr2 -- n new dash_text
380: n = memcmp(c_addr1, c_addr2, u);
381: if (n<0)
382: n = -1;
383: else if (n>0)
384: n = 1;
385: :
386: swap bounds
387: ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
388: ELSE c@ I c@ - unloop THEN -text-flag ;
389: : -text-flag ( n -- -1/0/1 )
390: dup 0< IF drop -1 ELSE 0> IF 1 ELSE 0 THEN THEN ;
391:
392: capscomp c_addr1 u c_addr2 -- n new
393: Char c1, c2;
394: for (;; u--, c_addr1++, c_addr2++) {
395: if (u == 0) {
396: n = 0;
397: break;
398: }
399: c1 = toupper(*c_addr1);
400: c2 = toupper(*c_addr2);
401: if (c1 != c2) {
402: if (c1 < c2)
403: n = -1;
404: else
405: n = 1;
406: break;
407: }
408: }
409: :
410: swap bounds
411: ?DO dup c@ toupper I c@ toupper = WHILE 1+ LOOP drop 0
412: ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
413:
414: -trailing c_addr u1 -- c_addr u2 string dash_trailing
415: u2 = u1;
416: while (c_addr[u2-1] == ' ')
417: u2--;
418: :
419: BEGIN 1- 2dup + c@ bl = WHILE
420: dup 0= UNTIL ELSE 1+ THEN ;
421:
422: /string c_addr1 u1 n -- c_addr2 u2 string slash_string
423: c_addr2 = c_addr1+n;
424: u2 = u1-n;
425: :
426: tuck - >r + r> dup 0< IF - 0 THEN ;
427:
428: + n1 n2 -- n core plus
429: n = n1+n2;
430:
431: - n1 n2 -- n core minus
432: n = n1-n2;
433: :
434: negate + ;
435:
436: negate n1 -- n2 core
437: /* use minus as alias */
438: n2 = -n1;
439: :
440: invert 1+ ;
441:
442: 1+ n1 -- n2 core one_plus
443: n2 = n1+1;
444: :
445: 1 + ;
446:
447: 1- n1 -- n2 core one_minus
448: n2 = n1-1;
449: :
450: 1 - ;
451:
452: max n1 n2 -- n core
453: if (n1<n2)
454: n = n2;
455: else
456: n = n1;
457: :
458: 2dup < IF swap THEN drop ;
459:
460: min n1 n2 -- n core
461: if (n1<n2)
462: n = n1;
463: else
464: n = n2;
465: :
466: 2dup > IF swap THEN drop ;
467:
468: abs n1 -- n2 core
469: if (n1<0)
470: n2 = -n1;
471: else
472: n2 = n1;
473: :
474: dup 0< IF negate THEN ;
475:
476: * n1 n2 -- n core star
477: n = n1*n2;
478: :
479: um* drop ;
480:
481: / n1 n2 -- n core slash
482: n = n1/n2;
483: :
484: /mod nip ;
485:
486: mod n1 n2 -- n core
487: n = n1%n2;
488: :
489: /mod drop ;
490:
491: /mod n1 n2 -- n3 n4 core slash_mod
492: n4 = n1/n2;
493: n3 = n1%n2; /* !! is this correct? look into C standard! */
494: :
495: >r s>d r> fm/mod ;
496:
497: 2* n1 -- n2 core two_star
498: n2 = 2*n1;
499: :
500: dup + ;
501:
502: 2/ n1 -- n2 core two_slash
503: /* !! is this still correct? */
504: n2 = n1>>1;
505:
506: fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
507: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
508: /* assumes that the processor uses either floored or symmetric division */
509: n3 = d1/n1;
510: n2 = d1%n1;
511: /* note that this 1%-3>0 is optimized by the compiler */
512: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
513: n3--;
514: n2+=n1;
515: }
516:
517: sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
518: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
519: /* assumes that the processor uses either floored or symmetric division */
520: n3 = d1/n1;
521: n2 = d1%n1;
522: /* note that this 1%-3<0 is optimized by the compiler */
523: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
524: n3++;
525: n2-=n1;
526: }
527: :
528: over >r dup >r abs -rot
529: dabs rot um/mod
530: r> 0< IF negate THEN
531: r> 0< IF swap negate swap THEN ;
532:
533: m* n1 n2 -- d core m_star
534: d = (DCell)n1 * (DCell)n2;
535: :
536: 2dup 0< and >r
537: 2dup swap 0< and >r
538: um* r> - r> - ;
539:
540: um* u1 u2 -- ud core u_m_star
541: /* use u* as alias */
542: ud = (UDCell)u1 * (UDCell)u2;
543:
544: um/mod ud u1 -- u2 u3 core u_m_slash_mod
545: u3 = ud/u1;
546: u2 = ud%u1;
547: :
548: dup IF 0 (um/mod) THEN nip ;
549: : (um/mod) ( ud ud--ud u)
550: 2dup >r >r dup 0<
551: IF 2drop 0
552: ELSE 2dup d+ (um/mod) 2* THEN
553: -rot r> r> 2over 2over du<
554: IF 2drop rot
555: ELSE dnegate d+ rot 1+ THEN ;
556:
557: m+ d1 n -- d2 double m_plus
558: d2 = d1+n;
559: :
560: s>d d+ ;
561:
562: d+ d1 d2 -- d double d_plus
563: d = d1+d2;
564: :
565: >r swap >r over 2/ over 2/ + >r over 1 and over 1 and + 2/
566: r> + >r + r> 0< r> r> + swap - ;
567:
568: d- d1 d2 -- d double d_minus
569: d = d1-d2;
570: :
571: dnegate d+ ;
572:
573: dnegate d1 -- d2 double
574: /* use dminus as alias */
575: d2 = -d1;
576: :
577: invert swap negate tuck 0= - ;
578:
579: dmax d1 d2 -- d double
580: if (d1<d2)
581: d = d2;
582: else
583: d = d1;
584: :
585: 2over 2over d> IF 2swap THEN 2drop ;
586:
587: dmin d1 d2 -- d double
588: if (d1<d2)
589: d = d1;
590: else
591: d = d2;
592: :
593: 2over 2over d< IF 2swap THEN 2drop ;
594:
595: dabs d1 -- d2 double
596: if (d1<0)
597: d2 = -d1;
598: else
599: d2 = d1;
600: :
601: dup 0< IF dnegate THEN ;
602:
603: d2* d1 -- d2 double d_two_star
604: d2 = 2*d1;
605: :
606: 2dup d+ ;
607:
608: d2/ d1 -- d2 double d_two_slash
609: /* !! is this still correct? */
610: d2 = d1>>1;
611: :
612: dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
613: r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
614:
615: d>s d -- n double d_to_s
616: /* make this an alias for drop? */
617: n = d;
618: :
619: drop ;
620:
621: and w1 w2 -- w core
622: w = w1&w2;
623:
624: or w1 w2 -- w core
625: w = w1|w2;
626:
627: xor w1 w2 -- w core
628: w = w1^w2;
629:
630: invert w1 -- w2 core
631: w2 = ~w1;
632: :
633: -1 xor ;
634:
635: rshift u1 n -- u2 core
636: u2 = u1>>n;
637:
638: lshift u1 n -- u2 core
639: u2 = u1<<n;
640:
641: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
642: define(comparisons,
643: $1= $2 -- f $6 $3equals
644: f = FLAG($4==$5);
645:
646: $1<> $2 -- f $7 $3different
647: /* use != as alias ? */
648: f = FLAG($4!=$5);
649:
650: $1< $2 -- f $8 $3less
651: f = FLAG($4<$5);
652:
653: $1> $2 -- f $9 $3greater
654: f = FLAG($4>$5);
655:
656: $1<= $2 -- f gforth $3less_or_equal
657: f = FLAG($4<=$5);
658:
659: $1>= $2 -- f gforth $3greater_or_equal
660: f = FLAG($4>=$5);
661:
662: )
663:
664: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
665: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
666: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
667: comparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
668: comparisons(d0, d, d_zero_, d, 0, double, gforth, double, gforth)
669: comparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
670:
671: within u1 u2 u3 -- f core-ext
672: f = FLAG(u1-u2 < u3-u2);
673: :
674: over - >r - r> u< ;
675:
676: sp@ -- a_addr gforth spat
677: a_addr = sp+1;
678:
679: sp! a_addr -- gforth spstore
680: sp = a_addr;
681: /* works with and without TOS caching */
682:
683: rp@ -- a_addr gforth rpat
684: a_addr = rp;
685:
686: rp! a_addr -- gforth rpstore
687: rp = a_addr;
688:
689: fp@ -- f_addr gforth fp_fetch
690: f_addr = fp;
691:
692: fp! f_addr -- gforth fp_store
693: fp = f_addr;
694:
695: ;s -- gforth semis
696: ip = (Xt *)(*rp++);
697: NEXT_P0;
698:
699: >r w -- core to_r
700: *--rp = w;
701:
702: r> -- w core r_from
703: w = *rp++;
704:
705: r@ -- w core r_fetch
706: /* use r as alias */
707: /* make r@ an alias for i */
708: w = *rp;
709:
710: rdrop -- gforth
711: rp++;
712:
713: i' -- w gforth i_tick
714: w=rp[1];
715:
716: 2>r w1 w2 -- core-ext two_to_r
717: *--rp = w1;
718: *--rp = w2;
719:
720: 2r> -- w1 w2 core-ext two_r_from
721: w2 = *rp++;
722: w1 = *rp++;
723:
724: 2r@ -- w1 w2 core-ext two_r_fetch
725: w2 = rp[0];
726: w1 = rp[1];
727:
728: 2rdrop -- gforth two_r_drop
729: rp+=2;
730:
731: over w1 w2 -- w1 w2 w1 core
732:
733: drop w -- core
734:
735: swap w1 w2 -- w2 w1 core
736:
737: dup w -- w w core
738:
739: rot w1 w2 w3 -- w2 w3 w1 core rote
740:
741: -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
742: :
743: rot rot ;
744:
745: nip w1 w2 -- w2 core-ext
746: :
747: swap drop ;
748:
749: tuck w1 w2 -- w2 w1 w2 core-ext
750: :
751: swap over ;
752:
753: ?dup w -- w core question_dupe
754: if (w!=0) {
755: IF_TOS(*sp-- = w;)
756: #ifndef USE_TOS
757: *--sp = w;
758: #endif
759: }
760: :
761: dup IF dup THEN ;
762:
763: pick u -- w core-ext
764: w = sp[u+1];
765: :
766: 1+ cells sp@ + @ ;
767:
768: 2drop w1 w2 -- core two_drop
769: :
770: drop drop ;
771:
772: 2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
773: :
774: over over ;
775:
776: 2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
777: :
778: 3 pick 3 pick ;
779:
780: 2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
781: :
782: >r -rot r> -rot ;
783:
784: 2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double-ext two_rote
785: :
786: >r >r 2swap r> r> 2swap ;
787:
788: 2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
789: :
790: 2swap 2drop ;
791:
792: 2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
793: :
794: 2swap 2over ;
795:
796: \ toggle is high-level: 0.11/0.42%
797:
798: @ a_addr -- w core fetch
799: w = *a_addr;
800:
801: ! w a_addr -- core store
802: *a_addr = w;
803:
804: +! n a_addr -- core plus_store
805: *a_addr += n;
806:
807: c@ c_addr -- c core cfetch
808: c = *c_addr;
809:
810: c! c c_addr -- core cstore
811: *c_addr = c;
812:
813: 2! w1 w2 a_addr -- core two_store
814: a_addr[0] = w2;
815: a_addr[1] = w1;
816: :
817: tuck ! cell+ ! ;
818:
819: 2@ a_addr -- w1 w2 core two_fetch
820: w2 = a_addr[0];
821: w1 = a_addr[1];
822: :
823: dup cell+ @ swap @ ;
824:
825: d! d a_addr -- double d_store
826: /* !! alignment problems on some machines */
827: *(DCell *)a_addr = d;
828:
829: d@ a_addr -- d double d_fetch
830: d = *(DCell *)a_addr;
831:
832: cell+ a_addr1 -- a_addr2 core cell_plus
833: a_addr2 = a_addr1+1;
834: :
835: [ cell ] Literal + ;
836:
837: cells n1 -- n2 core
838: n2 = n1 * sizeof(Cell);
839: :
840: [ cell ]
841: [ 2/ dup ] [IF] 2* [THEN]
842: [ 2/ dup ] [IF] 2* [THEN]
843: [ 2/ dup ] [IF] 2* [THEN]
844: [ 2/ dup ] [IF] 2* [THEN]
845: [ drop ] ;
846:
847: char+ c_addr1 -- c_addr2 core care_plus
848: c_addr2 = c_addr1 + 1;
849: :
850: 1+ ;
851:
852: (chars) n1 -- n2 gforth paren_cares
853: n2 = n1 * sizeof(Char);
854: :
855: ;
856:
857: count c_addr1 -- c_addr2 u core
858: u = *c_addr1;
859: c_addr2 = c_addr1+1;
860: :
861: dup 1+ swap c@ ;
862:
863: (bye) n -- gforth paren_bye
864: return (Label *)n;
865:
866: system c_addr u -- n gforth
867: n=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
868:
869: getenv c_addr1 u1 -- c_addr2 u2 gforth
870: c_addr2 = getenv(cstr(c_addr1,u1,1));
871: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
872:
873: popen c_addr u n -- wfileid own
874: static char* mode[2]={"r","w"}; /* !! should we use FAM here? */
875: wfileid=(Cell)popen(cstr(c_addr,u,1),mode[n]); /* ~ expansion of 1st arg? */
876:
877: pclose wfileid -- wior own
878: wior=pclose((FILE *)wfileid); /* !! what to do with the result */
879:
880: time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
881: struct timeval time1;
882: struct timezone zone1;
883: struct tm *ltime;
884: gettimeofday(&time1,&zone1);
885: ltime=localtime((time_t *)&time1.tv_sec);
886: nyear =ltime->tm_year+1900;
887: nmonth=ltime->tm_mon+1;
888: nday =ltime->tm_mday;
889: nhour =ltime->tm_hour;
890: nmin =ltime->tm_min;
891: nsec =ltime->tm_sec;
892:
893: ms n -- facility-ext
894: struct timeval timeout;
895: timeout.tv_sec=n/1000;
896: timeout.tv_usec=1000*(n%1000);
897: (void)select(0,0,0,0,&timeout);
898:
899: allocate u -- a_addr wior memory
900: a_addr = (Cell *)malloc(u?u:1);
901: wior = IOR(a_addr==NULL);
902:
903: free a_addr -- wior memory
904: free(a_addr);
905: wior = 0;
906:
907: resize a_addr1 u -- a_addr2 wior memory
908: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
909: address units, possibly moving the contents to a different
910: area. @i{a_addr2} is the address of the resulting area. If
911: @code{a_addr2} is 0, gforth's (but not the standard) @code{resize}
912: @code{allocate}s @i{u} address units.""
913: /* the following check is not necessary on most OSs, but it is needed
914: on SunOS 4.1.2. */
915: if (a_addr1==NULL)
916: a_addr2 = (Cell *)malloc(u);
917: else
918: a_addr2 = (Cell *)realloc(a_addr1, u);
919: wior = IOR(a_addr2==NULL); /* !! Define a return code */
920:
921: (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
922: for (; f83name1 != NULL; f83name1 = f83name1->next)
923: if (F83NAME_COUNT(f83name1)==u &&
924: strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
925: break;
926: f83name2=f83name1;
927: :
928: BEGIN dup WHILE
929: >r dup r@ cell+ c@ $1F and =
930: IF 2dup r@ cell+ char+ capscomp 0=
931: IF 2drop r> EXIT THEN THEN
932: r> @
933: REPEAT nip nip ;
934:
935: (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
936: F83Name *f83name1;
937: f83name2=NULL;
938: while(a_addr != NULL)
939: {
940: f83name1=(F83Name *)(a_addr[1]);
941: a_addr=(Cell *)(a_addr[0]);
942: if (F83NAME_COUNT(f83name1)==u &&
943: strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
944: {
945: f83name2=f83name1;
946: break;
947: }
948: }
949: :
950: BEGIN dup WHILE
951: 2@ >r >r dup r@ cell+ c@ $1F and =
952: IF 2dup r@ cell+ char+ capscomp 0=
953: IF 2drop r> rdrop EXIT THEN THEN
954: rdrop r>
955: REPEAT nip nip ;
956:
957: (hashkey) c_addr u1 -- u2 gforth paren_hashkey
958: u2=0;
959: while(u1--)
960: u2+=(Cell)toupper(*c_addr++);
961: :
962: 0 -rot bounds ?DO I c@ toupper + LOOP ;
963:
964: (hashkey1) c_addr u ubits -- ukey gforth paren_hashkey1
965: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
966: /* this hash function rotates the key at every step by rot bits within
967: ubits bits and xors it with the character. This function does ok in
968: the chi-sqare-test. Rot should be <=7 (preferably <=5) for
969: ASCII strings (larger if ubits is large), and should share no
970: divisors with ubits.
971: */
972: 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];
973: Char *cp = c_addr;
974: for (ukey=0; cp<c_addr+u; cp++)
975: ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
976: ^ toupper(*cp))
977: & ((1<<ubits)-1));
978: :
979: dup rot-values + c@ over 1 swap lshift 1- >r
980: tuck - 2swap r> 0 2swap bounds
981: ?DO dup 4 pick lshift swap 3 pick rshift or
982: I c@ toupper xor
983: over and LOOP
984: nip nip nip ;
985: Create rot-values
986: 5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
987: 3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
988: 7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
989: 7 c, 5 c, 5 c,
990:
991: (parse-white) c_addr1 u1 -- c_addr2 u2 gforth paren_parse_white
992: /* use !isgraph instead of isspace? */
993: Char *endp = c_addr1+u1;
994: while (c_addr1<endp && isspace(*c_addr1))
995: c_addr1++;
996: if (c_addr1<endp) {
997: for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
998: ;
999: u2 = c_addr1-c_addr2;
1000: }
1001: else {
1002: c_addr2 = c_addr1;
1003: u2 = 0;
1004: }
1005: :
1006: BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1007: REPEAT THEN 2dup
1008: BEGIN dup WHILE over c@ bl > WHILE 1 /string
1009: REPEAT THEN nip - ;
1010:
1011: close-file wfileid -- wior file close_file
1012: wior = IOR(fclose((FILE *)wfileid)==EOF);
1013:
1014: open-file c_addr u ntype -- w2 wior file open_file
1015: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1016: wior = IOR(w2 == 0);
1017:
1018: create-file c_addr u ntype -- w2 wior file create_file
1019: Cell fd;
1020: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_RDWR|O_TRUNC, 0666);
1021: if (fd != -1) {
1022: w2 = (Cell)fdopen(fd, fileattr[ntype]);
1023: wior = IOR(w2 == 0);
1024: } else {
1025: w2 = 0;
1026: wior = IOR(1);
1027: }
1028:
1029: delete-file c_addr u -- wior file delete_file
1030: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1031:
1032: rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1033: char *s1=tilde_cstr(c_addr2, u2, 1);
1034: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1035:
1036: file-position wfileid -- ud wior file file_position
1037: /* !! use tell and lseek? */
1038: ud = ftell((FILE *)wfileid);
1039: wior = IOR(ud==-1);
1040:
1041: reposition-file ud wfileid -- wior file reposition_file
1042: wior = IOR(fseek((FILE *)wfileid, (long)ud, SEEK_SET)==-1);
1043:
1044: file-size wfileid -- ud wior file file_size
1045: struct stat buf;
1046: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1047: ud = buf.st_size;
1048:
1049: resize-file ud wfileid -- wior file resize_file
1050: wior = IOR(ftruncate(fileno((FILE *)wfileid), (Cell)ud)==-1);
1051:
1052: read-file c_addr u1 wfileid -- u2 wior file read_file
1053: /* !! fread does not guarantee enough */
1054: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1055: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1056: /* !! is the value of ferror errno-compatible? */
1057: if (wior)
1058: clearerr((FILE *)wfileid);
1059:
1060: read-line c_addr u1 wfileid -- u2 flag wior file read_line
1061: /*
1062: Cell c;
1063: flag=-1;
1064: for(u2=0; u2<u1; u2++)
1065: {
1066: *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1067: if(c=='\n') break;
1068: if(c==EOF)
1069: {
1070: flag=FLAG(u2!=0);
1071: break;
1072: }
1073: }
1074: wior=FILEIO(ferror((FILE *)wfileid));
1075: */
1076: if ((flag=FLAG(!feof((FILE *)wfileid) &&
1077: fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1078: wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1079: if (wior)
1080: clearerr((FILE *)wfileid);
1081: u2 = strlen(c_addr);
1082: u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1083: }
1084: else {
1085: wior=0;
1086: u2=0;
1087: }
1088:
1089: write-file c_addr u1 wfileid -- wior file write_file
1090: /* !! fwrite does not guarantee enough */
1091: {
1092: Cell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1093: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1094: if (wior)
1095: clearerr((FILE *)wfileid);
1096: }
1097:
1098: flush-file wfileid -- wior file-ext flush_file
1099: wior = IOR(fflush((FILE *) wfileid)==EOF);
1100:
1101: file-status c_addr u -- ntype wior file-ext file_status
1102: char *filename=tilde_cstr(c_addr, u, 1);
1103: if (access (filename, F_OK) != 0) {
1104: ntype=0;
1105: wior=IOR(1);
1106: }
1107: else if (access (filename, R_OK | W_OK) == 0) {
1108: ntype=2; /* r/w */
1109: wior=0;
1110: }
1111: else if (access (filename, R_OK) == 0) {
1112: ntype=0; /* r/o */
1113: wior=0;
1114: }
1115: else if (access (filename, W_OK) == 0) {
1116: ntype=4; /* w/o */
1117: wior=0;
1118: }
1119: else {
1120: ntype=1; /* well, we cannot access the file, but better deliver a legal
1121: access mode (r/o bin), so we get a decent error later upon open. */
1122: wior=0;
1123: }
1124:
1125: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1126: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1127:
1128: d>f d -- r float d_to_f
1129: r = d;
1130:
1131: f>d r -- d float f_to_d
1132: /* !! basis 15 is not very specific */
1133: d = r;
1134:
1135: f! r f_addr -- float f_store
1136: *f_addr = r;
1137:
1138: f@ f_addr -- r float f_fetch
1139: r = *f_addr;
1140:
1141: df@ df_addr -- r float-ext d_f_fetch
1142: #ifdef IEEE_FP
1143: r = *df_addr;
1144: #else
1145: !! df@
1146: #endif
1147:
1148: df! r df_addr -- float-ext d_f_store
1149: #ifdef IEEE_FP
1150: *df_addr = r;
1151: #else
1152: !! df!
1153: #endif
1154:
1155: sf@ sf_addr -- r float-ext s_f_fetch
1156: #ifdef IEEE_FP
1157: r = *sf_addr;
1158: #else
1159: !! sf@
1160: #endif
1161:
1162: sf! r sf_addr -- float-ext s_f_store
1163: #ifdef IEEE_FP
1164: *sf_addr = r;
1165: #else
1166: !! sf!
1167: #endif
1168:
1169: f+ r1 r2 -- r3 float f_plus
1170: r3 = r1+r2;
1171:
1172: f- r1 r2 -- r3 float f_minus
1173: r3 = r1-r2;
1174:
1175: f* r1 r2 -- r3 float f_star
1176: r3 = r1*r2;
1177:
1178: f/ r1 r2 -- r3 float f_slash
1179: r3 = r1/r2;
1180:
1181: f** r1 r2 -- r3 float-ext f_star_star
1182: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1183: r3 = pow(r1,r2);
1184:
1185: fnegate r1 -- r2 float
1186: r2 = - r1;
1187:
1188: fdrop r -- float
1189:
1190: fdup r -- r r float
1191:
1192: fswap r1 r2 -- r2 r1 float
1193:
1194: fover r1 r2 -- r1 r2 r1 float
1195:
1196: frot r1 r2 r3 -- r2 r3 r1 float
1197:
1198: fnip r1 r2 -- r2 gforth
1199:
1200: ftuck r1 r2 -- r2 r1 r2 gforth
1201:
1202: float+ f_addr1 -- f_addr2 float float_plus
1203: f_addr2 = f_addr1+1;
1204:
1205: floats n1 -- n2 float
1206: n2 = n1*sizeof(Float);
1207:
1208: floor r1 -- r2 float
1209: ""round towards the next smaller integral value, i.e., round toward negative infinity""
1210: /* !! unclear wording */
1211: r2 = floor(r1);
1212:
1213: fround r1 -- r2 float
1214: ""round to the nearest integral value""
1215: /* !! unclear wording */
1216: #ifdef HAVE_RINT
1217: r2 = rint(r1);
1218: #else
1219: r2 = floor(r1+0.5);
1220: /* !! This is not quite true to the rounding rules given in the standard */
1221: #endif
1222:
1223: fmax r1 r2 -- r3 float
1224: if (r1<r2)
1225: r3 = r2;
1226: else
1227: r3 = r1;
1228:
1229: fmin r1 r2 -- r3 float
1230: if (r1<r2)
1231: r3 = r1;
1232: else
1233: r3 = r2;
1234:
1235: represent r c_addr u -- n f1 f2 float
1236: char *sig;
1237: Cell flag;
1238: Cell decpt;
1239: sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
1240: n=(r==0 ? 1 : decpt);
1241: f1=FLAG(flag!=0);
1242: f2=FLAG(isdigit(sig[0])!=0);
1243: memmove(c_addr,sig,u);
1244:
1245: >float c_addr u -- flag float to_float
1246: /* real signature: c_addr u -- r t / f */
1247: Float r;
1248: char *number=cstr(c_addr, u, 1);
1249: char *endconv;
1250: while(isspace(number[--u]) && u>0);
1251: switch(number[u])
1252: {
1253: case 'd':
1254: case 'D':
1255: case 'e':
1256: case 'E': break;
1257: default : u++; break;
1258: }
1259: number[u]='\0';
1260: r=strtod(number,&endconv);
1261: if((flag=FLAG(!(Cell)*endconv)))
1262: {
1263: IF_FTOS(fp[0] = FTOS);
1264: fp += -1;
1265: FTOS = r;
1266: }
1267: else if(*endconv=='d' || *endconv=='D')
1268: {
1269: *endconv='E';
1270: r=strtod(number,&endconv);
1271: if((flag=FLAG(!(Cell)*endconv)))
1272: {
1273: IF_FTOS(fp[0] = FTOS);
1274: fp += -1;
1275: FTOS = r;
1276: }
1277: }
1278:
1279: fabs r1 -- r2 float-ext
1280: r2 = fabs(r1);
1281:
1282: facos r1 -- r2 float-ext
1283: r2 = acos(r1);
1284:
1285: fasin r1 -- r2 float-ext
1286: r2 = asin(r1);
1287:
1288: fatan r1 -- r2 float-ext
1289: r2 = atan(r1);
1290:
1291: fatan2 r1 r2 -- r3 float-ext
1292: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1293: intends this to be the inverse of @code{fsincos}. In gforth it is.""
1294: r3 = atan2(r1,r2);
1295:
1296: fcos r1 -- r2 float-ext
1297: r2 = cos(r1);
1298:
1299: fexp r1 -- r2 float-ext
1300: r2 = exp(r1);
1301:
1302: fexpm1 r1 -- r2 float-ext
1303: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1304: #ifdef HAVE_EXPM1
1305: extern double expm1(double);
1306: r2 = expm1(r1);
1307: #else
1308: r2 = exp(r1)-1.;
1309: #endif
1310:
1311: fln r1 -- r2 float-ext
1312: r2 = log(r1);
1313:
1314: flnp1 r1 -- r2 float-ext
1315: ""@i{r2}=ln(@i{r1}+1)""
1316: #ifdef HAVE_LOG1P
1317: extern double log1p(double);
1318: r2 = log1p(r1);
1319: #else
1320: r2 = log(r1+1.);
1321: #endif
1322:
1323: flog r1 -- r2 float-ext
1324: ""the decimal logarithm""
1325: r2 = log10(r1);
1326:
1327: falog r1 -- r2 float-ext
1328: ""@i{r2}=10**@i{r1}""
1329: extern double pow10(double);
1330: r2 = pow10(r1);
1331:
1332: fsin r1 -- r2 float-ext
1333: r2 = sin(r1);
1334:
1335: fsincos r1 -- r2 r3 float-ext
1336: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1337: r2 = sin(r1);
1338: r3 = cos(r1);
1339:
1340: fsqrt r1 -- r2 float-ext
1341: r2 = sqrt(r1);
1342:
1343: ftan r1 -- r2 float-ext
1344: r2 = tan(r1);
1345: :
1346: fsincos f/ ;
1347:
1348: fsinh r1 -- r2 float-ext
1349: r2 = sinh(r1);
1350: :
1351: fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1352:
1353: fcosh r1 -- r2 float-ext
1354: r2 = cosh(r1);
1355: :
1356: fexp fdup 1/f f+ f2/ ;
1357:
1358: ftanh r1 -- r2 float-ext
1359: r2 = tanh(r1);
1360: :
1361: f2* fexpm1 fdup 2. d>f f+ f/ ;
1362:
1363: fasinh r1 -- r2 float-ext
1364: r2 = asinh(r1);
1365: :
1366: fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1367:
1368: facosh r1 -- r2 float-ext
1369: r2 = acosh(r1);
1370: :
1371: fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1372:
1373: fatanh r1 -- r2 float-ext
1374: r2 = atanh(r1);
1375: :
1376: fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1377: r> IF fnegate THEN ;
1378:
1379: sfloats n1 -- n2 float-ext s_floats
1380: n2 = n1*sizeof(SFloat);
1381:
1382: dfloats n1 -- n2 float-ext d_floats
1383: n2 = n1*sizeof(DFloat);
1384:
1385: aligned c_addr -- a_addr core
1386: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1387: :
1388: [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1389:
1390: faligned c_addr -- f_addr float f_aligned
1391: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1392: :
1393: [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1394:
1395: sfaligned c_addr -- sf_addr float-ext s_f_aligned
1396: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1397: :
1398: [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1399:
1400: dfaligned c_addr -- df_addr float-ext d_f_aligned
1401: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1402: :
1403: [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1404:
1405: \ The following words access machine/OS/installation-dependent
1406: \ Gforth internals
1407: \ !! how about environmental queries DIRECT-THREADED,
1408: \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1409:
1410: >body xt -- a_addr core to_body
1411: a_addr = PFA(xt);
1412:
1413: >code-address xt -- c_addr gforth to_code_address
1414: ""c_addr is the code address of the word xt""
1415: /* !! This behaves installation-dependently for DOES-words */
1416: c_addr = CODE_ADDRESS(xt);
1417:
1418: >does-code xt -- a_addr gforth to_does_code
1419: ""If xt ist the execution token of a defining-word-defined word,
1420: a_addr is the start of the Forth code after the DOES>; Otherwise the
1421: behaviour is undefined""
1422: /* !! there is currently no way to determine whether a word is
1423: defining-word-defined */
1424: a_addr = (Cell *)DOES_CODE(xt);
1425:
1426: code-address! c_addr xt -- gforth code_address_store
1427: ""Creates a code field with code address c_addr at xt""
1428: MAKE_CF(xt, c_addr);
1429: CACHE_FLUSH(xt,PFA(0));
1430:
1431: does-code! a_addr xt -- gforth does_code_store
1432: ""creates a code field at xt for a defining-word-defined word; a_addr
1433: is the start of the Forth code after DOES>""
1434: MAKE_DOES_CF(xt, a_addr);
1435: CACHE_FLUSH(xt,PFA(0));
1436:
1437: does-handler! a_addr -- gforth does_handler_store
1438: ""creates a DOES>-handler at address a_addr. a_addr usually points
1439: just behind a DOES>.""
1440: MAKE_DOES_HANDLER(a_addr);
1441: CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1442:
1443: /does-handler -- n gforth slash_does_handler
1444: ""the size of a does-handler (includes possible padding)""
1445: /* !! a constant or environmental query might be better */
1446: n = DOES_HANDLER_SIZE;
1447:
1448: flush-icache c_addr u -- gforth flush_icache
1449: ""Make sure that the instruction cache of the processor (if there is
1450: one) does not contain stale data at @var{c_addr} and @var{u} bytes
1451: afterwards. @code{END-CODE} performs a @code{flush-icache}
1452: automatically. Caveat: @code{flush-icache} might not work on your
1453: installation; this is usually the case if direct threading is not
1454: supported on your machine (take a look at your @file{machine.h}) and
1455: your machine has a separate instruction cache. In such cases,
1456: @code{flush-icache} does nothing instead of flushing the instruction
1457: cache.""
1458: FLUSH_ICACHE(c_addr,u);
1459:
1460: toupper c1 -- c2 gforth
1461: c2 = toupper(c1);
1462:
1463: \ local variable implementation primitives
1464: @local# -- w gforth fetch_local_number
1465: w = *(Cell *)(lp+(Cell)NEXT_INST);
1466: INC_IP(1);
1467:
1468: @local0 -- w new fetch_local_zero
1469: w = *(Cell *)(lp+0*sizeof(Cell));
1470:
1471: @local1 -- w new fetch_local_four
1472: w = *(Cell *)(lp+1*sizeof(Cell));
1473:
1474: @local2 -- w new fetch_local_eight
1475: w = *(Cell *)(lp+2*sizeof(Cell));
1476:
1477: @local3 -- w new fetch_local_twelve
1478: w = *(Cell *)(lp+3*sizeof(Cell));
1479:
1480: f@local# -- r gforth f_fetch_local_number
1481: r = *(Float *)(lp+(Cell)NEXT_INST);
1482: INC_IP(1);
1483:
1484: f@local0 -- r new f_fetch_local_zero
1485: r = *(Float *)(lp+0*sizeof(Float));
1486:
1487: f@local1 -- r new f_fetch_local_eight
1488: r = *(Float *)(lp+1*sizeof(Float));
1489:
1490: laddr# -- c_addr gforth laddr_number
1491: /* this can also be used to implement lp@ */
1492: c_addr = (Char *)(lp+(Cell)NEXT_INST);
1493: INC_IP(1);
1494:
1495: lp+!# -- gforth lp_plus_store_number
1496: ""used with negative immediate values it allocates memory on the
1497: local stack, a positive immediate argument drops memory from the local
1498: stack""
1499: lp += (Cell)NEXT_INST;
1500: INC_IP(1);
1501:
1502: lp- -- new minus_four_lp_plus_store
1503: lp += -sizeof(Cell);
1504:
1505: lp+ -- new eight_lp_plus_store
1506: lp += sizeof(Float);
1507:
1508: lp+2 -- new sixteen_lp_plus_store
1509: lp += 2*sizeof(Float);
1510:
1511: lp! c_addr -- gforth lp_store
1512: lp = (Address)c_addr;
1513:
1514: >l w -- gforth to_l
1515: lp -= sizeof(Cell);
1516: *(Cell *)lp = w;
1517:
1518: f>l r -- gforth f_to_l
1519: lp -= sizeof(Float);
1520: *(Float *)lp = r;
1521:
1522: up! a_addr -- gforth up_store
1523: up0=up=(char *)a_addr;
1524:
1525: call-c w -- gforth call_c
1526: ""Call the C function pointed to by @i{w}. The C function has to
1527: access the stack itself. The stack pointers are exported in the gloabl
1528: variables @code{SP} and @code{FP}.""
1529: /* This is a first attempt at support for calls to C. This may change in
1530: the future */
1531: IF_FTOS(fp[0]=FTOS);
1532: FP=fp;
1533: SP=sp;
1534: ((void (*)())w)();
1535: sp=SP;
1536: fp=FP;
1537: IF_TOS(TOS=sp[0]);
1538: IF_FTOS(FTOS=fp[0]);
1539:
1540: strerror n -- c_addr u gforth
1541: c_addr = strerror(n);
1542: u = strlen(c_addr);
1543:
1544: strsignal n -- c_addr u gforth
1545: c_addr = strsignal(n);
1546: u = strlen(c_addr);
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>