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