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