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