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