[gforth] / gforth / prim  

gforth: gforth/prim


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help