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