[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 :     ip=IP;
119 :     IF_TOS(TOS = sp[0]);
120 :     EXEC(xt);
121 :    
122 :     perform a_addr -- gforth
123 :     ""equivalent to @code{@ execute}""
124 :     /* and pfe */
125 :     ip=IP;
126 :     IF_TOS(TOS = sp[0]);
127 :     EXEC(*(Xt *)a_addr);
128 :     :
129 :     @ execute ;
130 :    
131 : pazsan 1.15 \+glocals
132 : anton 1.1
133 :     branch-lp+!# -- gforth branch_lp_plus_store_number
134 :     /* this will probably not be used */
135 :     branch_adjust_lp:
136 :     lp += (Cell)(IP[1]);
137 :     goto branch;
138 :    
139 : pazsan 1.15 \+
140 : anton 1.1
141 :     branch -- gforth
142 :     branch:
143 :     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
144 :     NEXT_P0;
145 :     :
146 :     r> dup @ + >r ;
147 :    
148 :     \ condbranch(forthname,restline,code,forthcode)
149 :     \ this is non-syntactical: code must open a brace that is closed by the macro
150 :     define(condbranch,
151 :     $1 $2
152 :     $3 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
153 :     NEXT_P0;
154 :     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 :     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
192 :     NEXT_P0;
193 :     NEXT;
194 :     }
195 :     else
196 :     INC_IP(1);
197 :    
198 :     ?dup-0=-?branch f -- new question_dupe_zero_equals_question_branch
199 :     ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
200 :     /* the approach taken here of declaring the word as having the stack
201 :     effect ( f -- ) and correcting for it in the branch-taken case costs a
202 :     few cycles in that case, but is easy to convert to a CONDBRANCH
203 :     invocation */
204 :     if (f!=0) {
205 :     sp--;
206 :     ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
207 :     NEXT_P0;
208 :     NEXT;
209 :     }
210 :     else
211 :     INC_IP(1);
212 :    
213 : pazsan 1.15 \+
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 : anton 1.1
443 :     \ digit is high-level: 0/0%
444 :    
445 :     move c_from c_to ucount -- core
446 :     memmove(c_to,c_from,ucount);
447 :     /* make an Ifdef for bsd and others? */
448 :     :
449 :     >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
450 :    
451 :     cmove c_from c_to u -- string
452 :     while (u-- > 0)
453 :     *c_to++ = *c_from++;
454 :     :
455 :     bounds ?DO dup c@ I c! 1+ LOOP drop ;
456 :    
457 :     cmove> c_from c_to u -- string c_move_up
458 :     while (u-- > 0)
459 :     c_to[u] = c_from[u];
460 :     :
461 :     dup 0= IF drop 2drop exit THEN
462 :     rot over + -rot bounds swap 1-
463 :     DO 1- dup c@ I c! -1 +LOOP drop ;
464 :    
465 :     fill c_addr u c -- core
466 :     memset(c_addr,c,u);
467 :     :
468 :     -rot bounds
469 :     ?DO dup I c! LOOP drop ;
470 :    
471 :     compare c_addr1 u1 c_addr2 u2 -- n string
472 :     ""Compare the strings lexicographically. If they are equal, n is 0; if
473 :     the first string is smaller, n is -1; if the first string is larger, n
474 :     is 1. Currently this is based on the machine's character
475 :     comparison. In the future, this may change to considering the current
476 :     locale and its collation order.""
477 :     n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
478 :     if (n==0)
479 :     n = u1-u2;
480 :     if (n<0)
481 :     n = -1;
482 :     else if (n>0)
483 :     n = 1;
484 :     :
485 :     rot 2dup - >r min swap -text dup
486 :     IF rdrop
487 :     ELSE drop r@ 0>
488 :     IF rdrop -1
489 :     ELSE r> 1 and
490 :     THEN
491 :     THEN ;
492 :    
493 :     -text c_addr1 u c_addr2 -- n new dash_text
494 :     n = memcmp(c_addr1, c_addr2, u);
495 :     if (n<0)
496 :     n = -1;
497 :     else if (n>0)
498 :     n = 1;
499 :     :
500 :     swap bounds
501 :     ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
502 :     ELSE c@ I c@ - unloop THEN -text-flag ;
503 :     : -text-flag ( n -- -1/0/1 )
504 :     dup 0< IF drop -1 ELSE 0> 1 and THEN ;
505 :    
506 :     toupper c1 -- c2 gforth
507 :     c2 = toupper(c1);
508 :     :
509 :     dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
510 :    
511 :     capscomp c_addr1 u c_addr2 -- n new
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
522 :     ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
523 :    
524 :     -trailing c_addr u1 -- c_addr u2 string dash_trailing
525 :     u2 = u1;
526 : anton 1.4 while (u2>0 && c_addr[u2-1] == ' ')
527 : anton 1.1 u2--;
528 :     :
529 :     BEGIN 1- 2dup + c@ bl = WHILE
530 :     dup 0= UNTIL ELSE 1+ THEN ;
531 :    
532 :     /string c_addr1 u1 n -- c_addr2 u2 string slash_string
533 :     c_addr2 = c_addr1+n;
534 :     u2 = u1-n;
535 :     :
536 :     tuck - >r + r> dup 0< IF - 0 THEN ;
537 :    
538 :     + n1 n2 -- n core plus
539 :     n = n1+n2;
540 :    
541 :     \ PFE-0.9.14 has it differently, but the next release will have it as follows
542 :     under+ n1 n2 n3 -- n n2 gforth under_plus
543 :     ""add @var{n3} to @var{n1} (giving @var{n})""
544 :     n = n1+n3;
545 :     :
546 :     rot + swap ;
547 :    
548 :     - n1 n2 -- n core minus
549 :     n = n1-n2;
550 :     :
551 :     negate + ;
552 :    
553 :     negate n1 -- n2 core
554 :     /* use minus as alias */
555 :     n2 = -n1;
556 :     :
557 :     invert 1+ ;
558 :    
559 :     1+ n1 -- n2 core one_plus
560 :     n2 = n1+1;
561 :     :
562 :     1 + ;
563 :    
564 :     1- n1 -- n2 core one_minus
565 :     n2 = n1-1;
566 :     :
567 :     1 - ;
568 :    
569 :     max n1 n2 -- n core
570 :     if (n1<n2)
571 :     n = n2;
572 :     else
573 :     n = n1;
574 :     :
575 :     2dup < IF swap THEN drop ;
576 :    
577 :     min n1 n2 -- n core
578 :     if (n1<n2)
579 :     n = n1;
580 :     else
581 :     n = n2;
582 :     :
583 :     2dup > IF swap THEN drop ;
584 :    
585 :     abs n1 -- n2 core
586 :     if (n1<0)
587 :     n2 = -n1;
588 :     else
589 :     n2 = n1;
590 :     :
591 :     dup 0< IF negate THEN ;
592 :    
593 :     * n1 n2 -- n core star
594 :     n = n1*n2;
595 :     :
596 :     um* drop ;
597 :    
598 :     / n1 n2 -- n core slash
599 :     n = n1/n2;
600 :     :
601 :     /mod nip ;
602 :    
603 :     mod n1 n2 -- n core
604 :     n = n1%n2;
605 :     :
606 :     /mod drop ;
607 :    
608 :     /mod n1 n2 -- n3 n4 core slash_mod
609 :     n4 = n1/n2;
610 :     n3 = n1%n2; /* !! is this correct? look into C standard! */
611 :     :
612 :     >r s>d r> fm/mod ;
613 :    
614 :     2* n1 -- n2 core two_star
615 :     n2 = 2*n1;
616 :     :
617 :     dup + ;
618 :    
619 :     2/ n1 -- n2 core two_slash
620 :     /* !! is this still correct? */
621 :     n2 = n1>>1;
622 :     :
623 :     dup MINI and IF 1 ELSE 0 THEN
624 :     [ bits/byte cell * 1- ] literal
625 : jwilke 1.5 0 DO 2* swap dup 2* >r MINI and
626 : anton 1.1 IF 1 ELSE 0 THEN or r> swap
627 :     LOOP nip ;
628 :    
629 :     fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
630 :     ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
631 :     #ifdef BUGGY_LONG_LONG
632 :     DCell r = fmdiv(d1,n1);
633 :     n2=r.hi;
634 :     n3=r.lo;
635 :     #else
636 :     /* assumes that the processor uses either floored or symmetric division */
637 :     n3 = d1/n1;
638 :     n2 = d1%n1;
639 :     /* note that this 1%-3>0 is optimized by the compiler */
640 :     if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
641 :     n3--;
642 :     n2+=n1;
643 :     }
644 :     #endif
645 :     :
646 :     dup >r dup 0< IF negate >r dnegate r> THEN
647 :     over 0< IF tuck + swap THEN
648 :     um/mod
649 :     r> 0< IF swap negate swap THEN ;
650 :    
651 :     sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
652 :     ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
653 :     #ifdef BUGGY_LONG_LONG
654 :     DCell r = smdiv(d1,n1);
655 :     n2=r.hi;
656 :     n3=r.lo;
657 :     #else
658 :     /* assumes that the processor uses either floored or symmetric division */
659 :     n3 = d1/n1;
660 :     n2 = d1%n1;
661 :     /* note that this 1%-3<0 is optimized by the compiler */
662 :     if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
663 :     n3++;
664 :     n2-=n1;
665 :     }
666 :     #endif
667 :     :
668 :     over >r dup >r abs -rot
669 :     dabs rot um/mod
670 :     r> r@ xor 0< IF negate THEN
671 :     r> 0< IF swap negate swap THEN ;
672 :    
673 :     m* n1 n2 -- d core m_star
674 :     #ifdef BUGGY_LONG_LONG
675 :     d = mmul(n1,n2);
676 :     #else
677 :     d = (DCell)n1 * (DCell)n2;
678 :     #endif
679 :     :
680 :     2dup 0< and >r
681 :     2dup swap 0< and >r
682 :     um* r> - r> - ;
683 :    
684 :     um* u1 u2 -- ud core u_m_star
685 :     /* use u* as alias */
686 :     #ifdef BUGGY_LONG_LONG
687 :     ud = ummul(u1,u2);
688 :     #else
689 :     ud = (UDCell)u1 * (UDCell)u2;
690 :     #endif
691 :     :
692 :     >r >r 0 0 r> r> [ 8 cells ] literal 0
693 :     DO
694 :     over >r dup >r 0< and d2*+ drop
695 :     r> 2* r> swap
696 :     LOOP 2drop ;
697 :     : d2*+ ( ud n -- ud+n c )
698 :     over MINI
699 :     and >r >r 2dup d+ swap r> + swap r> ;
700 :    
701 :     um/mod ud u1 -- u2 u3 core u_m_slash_mod
702 :     #ifdef BUGGY_LONG_LONG
703 :     UDCell r = umdiv(ud,u1);
704 :     u2=r.hi;
705 :     u3=r.lo;
706 :     #else
707 :     u3 = ud/u1;
708 :     u2 = ud%u1;
709 :     #endif
710 :     :
711 :     0 swap [ 8 cells 1 + ] literal 0
712 : jwilke 1.5 ?DO /modstep
713 : anton 1.1 LOOP drop swap 1 rshift or swap ;
714 :     : /modstep ( ud c R: u -- ud-?u c R: u )
715 : jwilke 1.5 >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN d2*+ r> ;
716 : anton 1.1 : d2*+ ( ud n -- ud+n c )
717 :     over MINI
718 :     and >r >r 2dup d+ swap r> + swap r> ;
719 :    
720 :     m+ d1 n -- d2 double m_plus
721 :     #ifdef BUGGY_LONG_LONG
722 :     d2.lo = d1.lo+n;
723 :     d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
724 :     #else
725 :     d2 = d1+n;
726 :     #endif
727 :     :
728 :     s>d d+ ;
729 :    
730 :     d+ d1 d2 -- d double d_plus
731 :     #ifdef BUGGY_LONG_LONG
732 :     d.lo = d1.lo+d2.lo;
733 :     d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
734 :     #else
735 :     d = d1+d2;
736 :     #endif
737 :     :
738 :     rot + >r tuck + swap over u> r> swap - ;
739 :    
740 :     d- d1 d2 -- d double d_minus
741 :     #ifdef BUGGY_LONG_LONG
742 :     d.lo = d1.lo - d2.lo;
743 :     d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
744 :     #else
745 :     d = d1-d2;
746 :     #endif
747 :     :
748 :     dnegate d+ ;
749 :    
750 :     dnegate d1 -- d2 double
751 :     /* use dminus as alias */
752 :     #ifdef BUGGY_LONG_LONG
753 :     d2 = dnegate(d1);
754 :     #else
755 :     d2 = -d1;
756 :     #endif
757 :     :
758 :     invert swap negate tuck 0= - ;
759 :    
760 :     d2* d1 -- d2 double d_two_star
761 :     #ifdef BUGGY_LONG_LONG
762 :     d2.lo = d1.lo<<1;
763 :     d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
764 :     #else
765 :     d2 = 2*d1;
766 :     #endif
767 :     :
768 :     2dup d+ ;
769 :    
770 :     d2/ d1 -- d2 double d_two_slash
771 :     #ifdef BUGGY_LONG_LONG
772 :     d2.hi = d1.hi>>1;
773 :     d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
774 :     #else
775 :     d2 = d1>>1;
776 :     #endif
777 :     :
778 :     dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
779 :     r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
780 :    
781 :     and w1 w2 -- w core
782 :     w = w1&w2;
783 :    
784 :     or w1 w2 -- w core
785 :     w = w1|w2;
786 :     :
787 :     invert swap invert and invert ;
788 :    
789 :     xor w1 w2 -- w core
790 :     w = w1^w2;
791 :    
792 :     invert w1 -- w2 core
793 :     w2 = ~w1;
794 :     :
795 :     MAXU xor ;
796 :    
797 :     rshift u1 n -- u2 core
798 :     u2 = u1>>n;
799 :     :
800 :     0 ?DO 2/ MAXI and LOOP ;
801 :    
802 :     lshift u1 n -- u2 core
803 :     u2 = u1<<n;
804 :     :
805 :     0 ?DO 2* LOOP ;
806 :    
807 :     \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
808 :     define(comparisons,
809 :     $1= $2 -- f $6 $3equals
810 :     f = FLAG($4==$5);
811 :     :
812 :     [ char $1x char 0 = [IF]
813 :     ] IF false ELSE true THEN [
814 :     [ELSE]
815 :     ] xor 0= [
816 :     [THEN] ] ;
817 :    
818 :     $1<> $2 -- f $7 $3different
819 :     f = FLAG($4!=$5);
820 :     :
821 :     [ char $1x char 0 = [IF]
822 :     ] IF true ELSE false THEN [
823 :     [ELSE]
824 :     ] xor 0<> [
825 :     [THEN] ] ;
826 :    
827 :     $1< $2 -- f $8 $3less
828 :     f = FLAG($4<$5);
829 :     :
830 :     [ char $1x char 0 = [IF]
831 :     ] MINI and 0<> [
832 :     [ELSE] char $1x char u = [IF]
833 :     ] 2dup xor 0< IF nip ELSE - THEN 0< [
834 :     [ELSE]
835 :     ] MINI xor >r MINI xor r> u< [
836 :     [THEN]
837 :     [THEN] ] ;
838 :    
839 :     $1> $2 -- f $9 $3greater
840 :     f = FLAG($4>$5);
841 :     :
842 :     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
843 :     $1< ;
844 :    
845 :     $1<= $2 -- f gforth $3less_or_equal
846 :     f = FLAG($4<=$5);
847 :     :
848 :     $1> 0= ;
849 :    
850 :     $1>= $2 -- f gforth $3greater_or_equal
851 :     f = FLAG($4>=$5);
852 :     :
853 :     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
854 :     $1<= ;
855 :    
856 :     )
857 :    
858 :     comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
859 :     comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
860 :     comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
861 :    
862 :     \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
863 :     define(dcomparisons,
864 :     $1= $2 -- f $6 $3equals
865 :     #ifdef BUGGY_LONG_LONG
866 :     f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
867 :     #else
868 :     f = FLAG($4==$5);
869 :     #endif
870 :    
871 :     $1<> $2 -- f $7 $3different
872 :     #ifdef BUGGY_LONG_LONG
873 :     f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
874 :     #else
875 :     f = FLAG($4!=$5);
876 :     #endif
877 :    
878 :     $1< $2 -- f $8 $3less
879 :     #ifdef BUGGY_LONG_LONG
880 :     f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
881 :     #else
882 :     f = FLAG($4<$5);
883 :     #endif
884 :    
885 :     $1> $2 -- f $9 $3greater
886 :     #ifdef BUGGY_LONG_LONG
887 :     f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
888 :     #else
889 :     f = FLAG($4>$5);
890 :     #endif
891 :    
892 :     $1<= $2 -- f gforth $3less_or_equal
893 :     #ifdef BUGGY_LONG_LONG
894 :     f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
895 :     #else
896 :     f = FLAG($4<=$5);
897 :     #endif
898 :    
899 :     $1>= $2 -- f gforth $3greater_or_equal
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 :     )
907 :    
908 : pazsan 1.15 \+dcomps
909 : anton 1.1
910 :     dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
911 :     dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
912 :     dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
913 :    
914 : pazsan 1.15 \+
915 : anton 1.1
916 :     within u1 u2 u3 -- f core-ext
917 :     f = FLAG(u1-u2 < u3-u2);
918 :     :
919 :     over - >r - r> u< ;
920 :    
921 :     sp@ -- a_addr gforth spat
922 :     a_addr = sp+1;
923 :    
924 :     sp! a_addr -- gforth spstore
925 :     sp = a_addr;
926 :     /* works with and without TOS caching */
927 :    
928 :     rp@ -- a_addr gforth rpat
929 :     a_addr = rp;
930 :    
931 :     rp! a_addr -- gforth rpstore
932 :     rp = a_addr;
933 :    
934 : pazsan 1.15 \+floating
935 : anton 1.1
936 :     fp@ -- f_addr gforth fp_fetch
937 :     f_addr = fp;
938 :    
939 :     fp! f_addr -- gforth fp_store
940 :     fp = f_addr;
941 :    
942 : pazsan 1.15 \+
943 : anton 1.1
944 :     ;s -- gforth semis
945 :     ip = (Xt *)(*rp++);
946 :     NEXT_P0;
947 :    
948 :     >r w -- core to_r
949 :     *--rp = w;
950 :     :
951 :     (>r) ;
952 :     : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
953 :    
954 :     r> -- w core r_from
955 :     w = *rp++;
956 :     :
957 :     rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
958 :     Create (rdrop) ' ;s A,
959 :    
960 :     rdrop -- gforth
961 :     rp++;
962 :     :
963 :     r> r> drop >r ;
964 :    
965 :     2>r w1 w2 -- core-ext two_to_r
966 :     *--rp = w1;
967 :     *--rp = w2;
968 :     :
969 :     swap r> swap >r swap >r >r ;
970 :    
971 :     2r> -- w1 w2 core-ext two_r_from
972 :     w2 = *rp++;
973 :     w1 = *rp++;
974 :     :
975 :     r> r> swap r> swap >r swap ;
976 :    
977 :     2r@ -- w1 w2 core-ext two_r_fetch
978 :     w2 = rp[0];
979 :     w1 = rp[1];
980 :     :
981 :     i' j ;
982 :    
983 :     2rdrop -- gforth two_r_drop
984 :     rp+=2;
985 :     :
986 :     r> r> drop r> drop >r ;
987 :    
988 :     over w1 w2 -- w1 w2 w1 core
989 :     :
990 :     sp@ cell+ @ ;
991 :    
992 :     drop w -- core
993 :     :
994 :     IF THEN ;
995 :    
996 :     swap w1 w2 -- w2 w1 core
997 :     :
998 :     >r (swap) ! r> (swap) @ ;
999 :     Variable (swap)
1000 :    
1001 :     dup w -- w w core
1002 :     :
1003 :     sp@ @ ;
1004 :    
1005 :     rot w1 w2 w3 -- w2 w3 w1 core rote
1006 :     :
1007 :     [ defined? (swap) [IF] ]
1008 :     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
1009 :     Variable (rot)
1010 :     [ELSE] ]
1011 :     >r swap r> swap ;
1012 :     [THEN]
1013 :    
1014 :     -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
1015 :     :
1016 :     rot rot ;
1017 :    
1018 :     nip w1 w2 -- w2 core-ext
1019 :     :
1020 : jwilke 1.6 swap drop ;
1021 : anton 1.1
1022 :     tuck w1 w2 -- w2 w1 w2 core-ext
1023 :     :
1024 :     swap over ;
1025 :    
1026 :     ?dup w -- w core question_dupe
1027 :     if (w!=0) {
1028 :     IF_TOS(*sp-- = w;)
1029 :     #ifndef USE_TOS
1030 :     *--sp = w;
1031 :     #endif
1032 :     }
1033 :     :
1034 :     dup IF dup THEN ;
1035 :    
1036 :     pick u -- w core-ext
1037 :     w = sp[u+1];
1038 :     :
1039 :     1+ cells sp@ + @ ;
1040 :    
1041 :     2drop w1 w2 -- core two_drop
1042 :     :
1043 :     drop drop ;
1044 :    
1045 :     2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
1046 :     :
1047 :     over over ;
1048 :    
1049 :     2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
1050 :     :
1051 :     3 pick 3 pick ;
1052 :    
1053 :     2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
1054 :     :
1055 :     rot >r rot r> ;
1056 :    
1057 :     2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double-ext two_rote
1058 :     :
1059 :     >r >r 2swap r> r> 2swap ;
1060 :    
1061 :     2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
1062 :     :
1063 :     2swap 2drop ;
1064 :    
1065 :     2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
1066 :     :
1067 :     2swap 2over ;
1068 :    
1069 :     \ toggle is high-level: 0.11/0.42%
1070 :    
1071 :     @ a_addr -- w core fetch
1072 :     w = *a_addr;
1073 :    
1074 :     ! w a_addr -- core store
1075 :     *a_addr = w;
1076 :    
1077 :     +! n a_addr -- core plus_store
1078 :     *a_addr += n;
1079 :     :
1080 :     tuck @ + swap ! ;
1081 :    
1082 :     c@ c_addr -- c core cfetch
1083 :     c = *c_addr;
1084 :     :
1085 :     [ bigendian [IF] ]
1086 :     [ cell>bit 4 = [IF] ]
1087 :     dup [ 0 cell - ] Literal and @ swap 1 and
1088 :     IF $FF and ELSE 8>> THEN ;
1089 :     [ [ELSE] ]
1090 :     dup [ cell 1- ] literal and
1091 :     tuck - @ swap [ cell 1- ] literal xor
1092 :     0 ?DO 8>> LOOP $FF and
1093 :     [ [THEN] ]
1094 :     [ [ELSE] ]
1095 :     [ cell>bit 4 = [IF] ]
1096 :     dup [ 0 cell - ] Literal and @ swap 1 and
1097 :     IF 8>> ELSE $FF and THEN
1098 :     [ [ELSE] ]
1099 :     dup [ cell 1- ] literal and
1100 :     tuck - @ swap
1101 :     0 ?DO 8>> LOOP 255 and
1102 :     [ [THEN] ]
1103 :     [ [THEN] ]
1104 :     ;
1105 :     : 8>> 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ ;
1106 :    
1107 :     c! c c_addr -- core cstore
1108 :     *c_addr = c;
1109 :     :
1110 :     [ bigendian [IF] ]
1111 :     [ cell>bit 4 = [IF] ]
1112 :     tuck 1 and IF $FF and ELSE 8<< THEN >r
1113 :     dup -2 and @ over 1 and cells masks + @ and
1114 :     r> or swap -2 and ! ;
1115 :     Create masks $00FF , $FF00 ,
1116 :     [ELSE] ]
1117 :     dup [ cell 1- ] literal and dup
1118 :     [ cell 1- ] literal xor >r
1119 :     - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1120 :     rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1121 :     [THEN]
1122 :     [ELSE] ]
1123 :     [ cell>bit 4 = [IF] ]
1124 :     tuck 1 and IF 8<< ELSE $FF and THEN >r
1125 :     dup -2 and @ over 1 and cells masks + @ and
1126 :     r> or swap -2 and ! ;
1127 :     Create masks $FF00 , $00FF ,
1128 :     [ELSE] ]
1129 :     dup [ cell 1- ] literal and dup >r
1130 :     - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1131 :     rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1132 :     [THEN]
1133 :     [THEN]
1134 :     : 8<< 2* 2* 2* 2* 2* 2* 2* 2* ;
1135 :    
1136 :     2! w1 w2 a_addr -- core two_store
1137 :     a_addr[0] = w2;
1138 :     a_addr[1] = w1;
1139 :     :
1140 :     tuck ! cell+ ! ;
1141 :    
1142 :     2@ a_addr -- w1 w2 core two_fetch
1143 :     w2 = a_addr[0];
1144 :     w1 = a_addr[1];
1145 :     :
1146 :     dup cell+ @ swap @ ;
1147 :    
1148 :     cell+ a_addr1 -- a_addr2 core cell_plus
1149 :     a_addr2 = a_addr1+1;
1150 :     :
1151 :     cell + ;
1152 :    
1153 :     cells n1 -- n2 core
1154 :     n2 = n1 * sizeof(Cell);
1155 :     :
1156 :     [ cell
1157 :     2/ dup [IF] ] 2* [ [THEN]
1158 :     2/ dup [IF] ] 2* [ [THEN]
1159 :     2/ dup [IF] ] 2* [ [THEN]
1160 :     2/ dup [IF] ] 2* [ [THEN]
1161 :     drop ] ;
1162 :    
1163 :     char+ c_addr1 -- c_addr2 core care_plus
1164 :     c_addr2 = c_addr1 + 1;
1165 :     :
1166 :     1+ ;
1167 :    
1168 :     (chars) n1 -- n2 gforth paren_cares
1169 :     n2 = n1 * sizeof(Char);
1170 :     :
1171 :     ;
1172 :    
1173 :     count c_addr1 -- c_addr2 u core
1174 :     u = *c_addr1;
1175 :     c_addr2 = c_addr1+1;
1176 :     :
1177 :     dup 1+ swap c@ ;
1178 :    
1179 :     (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
1180 : pazsan 1.13 for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
1181 : anton 1.1 if ((UCell)F83NAME_COUNT(f83name1)==u &&
1182 :     memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1183 :     break;
1184 :     f83name2=f83name1;
1185 :     :
1186 :     BEGIN dup WHILE (find-samelen) dup WHILE
1187 :     >r 2dup r@ cell+ char+ capscomp 0=
1188 :     IF 2drop r> EXIT THEN
1189 :     r> @
1190 :     REPEAT THEN nip nip ;
1191 :     : (find-samelen) ( u f83name1 -- u f83name2/0 )
1192 :     BEGIN 2dup cell+ c@ $1F and <> WHILE @ dup 0= UNTIL THEN ;
1193 :    
1194 : pazsan 1.15 \+hash
1195 : anton 1.1
1196 :     (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
1197 : pazsan 1.13 struct F83Name *f83name1;
1198 : anton 1.1 f83name2=NULL;
1199 :     while(a_addr != NULL)
1200 :     {
1201 : pazsan 1.13 f83name1=(struct F83Name *)(a_addr[1]);
1202 : anton 1.1 a_addr=(Cell *)(a_addr[0]);
1203 :     if ((UCell)F83NAME_COUNT(f83name1)==u &&
1204 :     memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1205 :     {
1206 :     f83name2=f83name1;
1207 :     break;
1208 :     }
1209 :     }
1210 :     :
1211 :     BEGIN dup WHILE
1212 :     2@ >r >r dup r@ cell+ c@ $1F and =
1213 :     IF 2dup r@ cell+ char+ capscomp 0=
1214 :     IF 2drop r> rdrop EXIT THEN THEN
1215 :     rdrop r>
1216 :     REPEAT nip nip ;
1217 :    
1218 :     (tablefind) c_addr u a_addr -- f83name2 new paren_tablefind
1219 :     ""A case-sensitive variant of @code{(hashfind)}""
1220 : pazsan 1.13 struct F83Name *f83name1;
1221 : anton 1.1 f83name2=NULL;
1222 :     while(a_addr != NULL)
1223 :     {
1224 : pazsan 1.13 f83name1=(struct F83Name *)(a_addr[1]);
1225 : anton 1.1 a_addr=(Cell *)(a_addr[0]);
1226 :     if ((UCell)F83NAME_COUNT(f83name1)==u &&
1227 :     memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1228 :     {
1229 :     f83name2=f83name1;
1230 :     break;
1231 :     }
1232 :     }
1233 :     :
1234 :     BEGIN dup WHILE
1235 :     2@ >r >r dup r@ cell+ c@ $1F and =
1236 :     IF 2dup r@ cell+ char+ -text 0=
1237 :     IF 2drop r> rdrop EXIT THEN THEN
1238 :     rdrop r>
1239 :     REPEAT nip nip ;
1240 :    
1241 :     (hashkey) c_addr u1 -- u2 gforth paren_hashkey
1242 :     u2=0;
1243 :     while(u1--)
1244 :     u2+=(Cell)toupper(*c_addr++);
1245 :     :
1246 :     0 -rot bounds ?DO I c@ toupper + LOOP ;
1247 :    
1248 :     (hashkey1) c_addr u ubits -- ukey gforth paren_hashkey1
1249 :     ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1250 :     /* this hash function rotates the key at every step by rot bits within
1251 :     ubits bits and xors it with the character. This function does ok in
1252 :     the chi-sqare-test. Rot should be <=7 (preferably <=5) for
1253 :     ASCII strings (larger if ubits is large), and should share no
1254 :     divisors with ubits.
1255 :     */
1256 :     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];
1257 :     Char *cp = c_addr;
1258 :     for (ukey=0; cp<c_addr+u; cp++)
1259 :     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
1260 :     ^ toupper(*cp))
1261 :     & ((1<<ubits)-1));
1262 :     :
1263 :     dup rot-values + c@ over 1 swap lshift 1- >r
1264 :     tuck - 2swap r> 0 2swap bounds
1265 :     ?DO dup 4 pick lshift swap 3 pick rshift or
1266 :     I c@ toupper xor
1267 :     over and LOOP
1268 :     nip nip nip ;
1269 :     Create rot-values
1270 :     5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
1271 :     3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
1272 :     7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1273 :     7 c, 5 c, 5 c,
1274 :    
1275 : pazsan 1.15 \+
1276 : anton 1.1
1277 :     (parse-white) c_addr1 u1 -- c_addr2 u2 gforth paren_parse_white
1278 :     /* use !isgraph instead of isspace? */
1279 :     Char *endp = c_addr1+u1;
1280 :     while (c_addr1<endp && isspace(*c_addr1))
1281 :     c_addr1++;
1282 :     if (c_addr1<endp) {
1283 :     for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1284 :     ;
1285 :     u2 = c_addr1-c_addr2;
1286 :     }
1287 :     else {
1288 :     c_addr2 = c_addr1;
1289 :     u2 = 0;
1290 :     }
1291 :     :
1292 :     BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1293 :     REPEAT THEN 2dup
1294 :     BEGIN dup WHILE over c@ bl > WHILE 1 /string
1295 :     REPEAT THEN nip - ;
1296 :    
1297 :     aligned c_addr -- a_addr core
1298 :     a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1299 :     :
1300 :     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1301 :    
1302 :     faligned c_addr -- f_addr float f_aligned
1303 :     f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1304 :     :
1305 :     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1306 :    
1307 :     >body xt -- a_addr core to_body
1308 :     a_addr = PFA(xt);
1309 :     :
1310 :     2 cells + ;
1311 :    
1312 :     >code-address xt -- c_addr gforth to_code_address
1313 :     ""c_addr is the code address of the word xt""
1314 :     /* !! This behaves installation-dependently for DOES-words */
1315 :     c_addr = (Address)CODE_ADDRESS(xt);
1316 :     :
1317 :     @ ;
1318 :    
1319 :     >does-code xt -- a_addr gforth to_does_code
1320 :     ""If xt ist the execution token of a defining-word-defined word,
1321 :     a_addr is the start of the Forth code after the DOES>;
1322 :     Otherwise a_addr is 0.""
1323 :     a_addr = (Cell *)DOES_CODE(xt);
1324 :     :
1325 :     cell+ @ ;
1326 :    
1327 :     code-address! c_addr xt -- gforth code_address_store
1328 :     ""Creates a code field with code address c_addr at xt""
1329 :     MAKE_CF(xt, c_addr);
1330 : anton 1.10 CACHE_FLUSH(xt,(size_t)PFA(0));
1331 : anton 1.1 :
1332 :     ! ;
1333 :    
1334 :     does-code! a_addr xt -- gforth does_code_store
1335 :     ""creates a code field at xt for a defining-word-defined word; a_addr
1336 :     is the start of the Forth code after DOES>""
1337 :     MAKE_DOES_CF(xt, a_addr);
1338 : anton 1.10 CACHE_FLUSH(xt,(size_t)PFA(0));
1339 : anton 1.1 :
1340 :     dodoes: over ! cell+ ! ;
1341 :    
1342 :     does-handler! a_addr -- gforth does_handler_store
1343 :     ""creates a DOES>-handler at address a_addr. a_addr usually points
1344 :     just behind a DOES>.""
1345 :     MAKE_DOES_HANDLER(a_addr);
1346 : anton 1.10 CACHE_FLUSH((caddr_t)a_addr,DOES_HANDLER_SIZE);
1347 : anton 1.1 :
1348 :     drop ;
1349 :    
1350 :     /does-handler -- n gforth slash_does_handler
1351 :     ""the size of a does-handler (includes possible padding)""
1352 :     /* !! a constant or environmental query might be better */
1353 :     n = DOES_HANDLER_SIZE;
1354 :     :
1355 :     2 cells ;
1356 :    
1357 :     threading-method -- n gforth threading_method
1358 :     ""0 if the engine is direct threaded. Note that this may change during
1359 :     the lifetime of an image.""
1360 :     #if defined(DOUBLY_INDIRECT)
1361 :     n=2;
1362 :     #else
1363 :     # if defined(DIRECT_THREADED)
1364 :     n=0;
1365 :     # else
1366 :     n=1;
1367 :     # endif
1368 :     #endif
1369 :     :
1370 :     1 ;
1371 :    
1372 : pazsan 1.12 key-file wfileid -- n gforth paren_key_file
1373 : pazsan 1.17 #ifdef HAS_FILE
1374 : anton 1.1 fflush(stdout);
1375 : pazsan 1.12 n = key((FILE*)wfileid);
1376 : pazsan 1.17 #else
1377 :     n = key(stdin);
1378 :     #endif
1379 : anton 1.1
1380 : pazsan 1.12 key?-file wfileid -- n facility key_q_file
1381 : pazsan 1.17 #ifdef HAS_FILE
1382 : anton 1.1 fflush(stdout);
1383 : pazsan 1.12 n = key_query((FILE*)wfileid);
1384 : pazsan 1.17 #else
1385 :     n = key_query(stdin);
1386 :     #endif
1387 :    
1388 :     \+os
1389 : pazsan 1.12
1390 :     stdin -- wfileid gforth
1391 :     wfileid = (Cell)stdin;
1392 : anton 1.1
1393 :     stdout -- wfileid gforth
1394 :     wfileid = (Cell)stdout;
1395 :    
1396 :     stderr -- wfileid gforth
1397 :     wfileid = (Cell)stderr;
1398 :    
1399 :     form -- urows ucols gforth
1400 :     ""The number of lines and columns in the terminal. These numbers may change
1401 :     with the window size.""
1402 :     /* we could block SIGWINCH here to get a consistent size, but I don't
1403 :     think this is necessary or always beneficial */
1404 :     urows=rows;
1405 :     ucols=cols;
1406 :    
1407 :     flush-icache c_addr u -- gforth flush_icache
1408 :     ""Make sure that the instruction cache of the processor (if there is
1409 :     one) does not contain stale data at @var{c_addr} and @var{u} bytes
1410 :     afterwards. @code{END-CODE} performs a @code{flush-icache}
1411 :     automatically. Caveat: @code{flush-icache} might not work on your
1412 :     installation; this is usually the case if direct threading is not
1413 :     supported on your machine (take a look at your @file{machine.h}) and
1414 :     your machine has a separate instruction cache. In such cases,
1415 :     @code{flush-icache} does nothing instead of flushing the instruction
1416 :     cache.""
1417 :     FLUSH_ICACHE(c_addr,u);
1418 :    
1419 :     (bye) n -- gforth paren_bye
1420 :     return (Label *)n;
1421 :    
1422 :     (system) c_addr u -- wretval wior gforth peren_system
1423 :     int old_tp=terminal_prepped;
1424 :     deprep_terminal();
1425 :     wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1426 :     wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1427 :     if (old_tp)
1428 :     prep_terminal();
1429 :    
1430 :     getenv c_addr1 u1 -- c_addr2 u2 gforth
1431 :     c_addr2 = getenv(cstr(c_addr1,u1,1));
1432 :     u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1433 :    
1434 :     open-pipe c_addr u ntype -- wfileid wior gforth open_pipe
1435 :     wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
1436 :     wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1437 :    
1438 :     close-pipe wfileid -- wretval wior gforth close_pipe
1439 :     wretval = pclose((FILE *)wfileid);
1440 :     wior = IOR(wretval==-1);
1441 :    
1442 :     time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
1443 :     struct timeval time1;
1444 :     struct timezone zone1;
1445 :     struct tm *ltime;
1446 :     gettimeofday(&time1,&zone1);
1447 :     ltime=localtime((time_t *)&time1.tv_sec);
1448 :     nyear =ltime->tm_year+1900;
1449 :     nmonth=ltime->tm_mon+1;
1450 :     nday =ltime->tm_mday;
1451 :     nhour =ltime->tm_hour;
1452 :     nmin =ltime->tm_min;
1453 :     nsec =ltime->tm_sec;
1454 :    
1455 :     ms n -- facility-ext
1456 :     struct timeval timeout;
1457 :     timeout.tv_sec=n/1000;
1458 :     timeout.tv_usec=1000*(n%1000);
1459 :     (void)select(0,0,0,0,&timeout);
1460 :    
1461 :     allocate u -- a_addr wior memory
1462 :     a_addr = (Cell *)malloc(u?u:1);
1463 :     wior = IOR(a_addr==NULL);
1464 :    
1465 :     free a_addr -- wior memory
1466 :     free(a_addr);
1467 :     wior = 0;
1468 :    
1469 :     resize a_addr1 u -- a_addr2 wior memory
1470 :     ""Change the size of the allocated area at @i{a_addr1} to @i{u}
1471 :     address units, possibly moving the contents to a different
1472 :     area. @i{a_addr2} is the address of the resulting area. If
1473 :     @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
1474 :     @code{allocate}s @i{u} address units.""
1475 :     /* the following check is not necessary on most OSs, but it is needed
1476 :     on SunOS 4.1.2. */
1477 :     if (a_addr1==NULL)
1478 :     a_addr2 = (Cell *)malloc(u);
1479 :     else
1480 :     a_addr2 = (Cell *)realloc(a_addr1, u);
1481 :     wior = IOR(a_addr2==NULL); /* !! Define a return code */
1482 :    
1483 :     strerror n -- c_addr u gforth
1484 :     c_addr = strerror(n);
1485 :     u = strlen(c_addr);
1486 :    
1487 :     strsignal n -- c_addr u gforth
1488 :     c_addr = strsignal(n);
1489 :     u = strlen(c_addr);
1490 :    
1491 :     call-c w -- gforth call_c
1492 :     ""Call the C function pointed to by @i{w}. The C function has to
1493 :     access the stack itself. The stack pointers are exported in the global
1494 :     variables @code{SP} and @code{FP}.""
1495 :     /* This is a first attempt at support for calls to C. This may change in
1496 :     the future */
1497 :     IF_FTOS(fp[0]=FTOS);
1498 :     FP=fp;
1499 :     SP=sp;
1500 :     ((void (*)())w)();
1501 :     sp=SP;
1502 :     fp=FP;
1503 :     IF_TOS(TOS=sp[0]);
1504 :     IF_FTOS(FTOS=fp[0]);
1505 :    
1506 : pazsan 1.15 \+
1507 :     \+file
1508 : anton 1.1
1509 :     close-file wfileid -- wior file close_file
1510 :     wior = IOR(fclose((FILE *)wfileid)==EOF);
1511 :    
1512 :     open-file c_addr u ntype -- w2 wior file open_file
1513 :     w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1514 : pazsan 1.14 #if defined(GO32) && defined(MSDOS)
1515 :     if(w2 && !(ntype & 1))
1516 :     setbuf((FILE*)w2, NULL);
1517 :     #endif
1518 : anton 1.1 wior = IOR(w2 == 0);
1519 :    
1520 :     create-file c_addr u ntype -- w2 wior file create_file
1521 :     Cell fd;
1522 :     fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
1523 :     if (fd != -1) {
1524 :     w2 = (Cell)fdopen(fd, fileattr[ntype]);
1525 : pazsan 1.14 #if defined(GO32) && defined(MSDOS)
1526 :     if(w2 && !(ntype & 1))
1527 :     setbuf((FILE*)w2, NULL);
1528 :     #endif
1529 : anton 1.1 wior = IOR(w2 == 0);
1530 :     } else {
1531 :     w2 = 0;
1532 :     wior = IOR(1);
1533 :     }
1534 :    
1535 :     delete-file c_addr u -- wior file delete_file
1536 :     wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1537 :    
1538 :     rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1539 : pazsan 1.19 ""rename file c_addr1 u1 to new name c_addr2 u2""
1540 : anton 1.1 char *s1=tilde_cstr(c_addr2, u2, 1);
1541 :     wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1542 :    
1543 :     file-position wfileid -- ud wior file file_position
1544 :     /* !! use tell and lseek? */
1545 :     ud = LONG2UD(ftell((FILE *)wfileid));
1546 :     wior = IOR(UD2LONG(ud)==-1);
1547 :    
1548 :     reposition-file ud wfileid -- wior file reposition_file
1549 :     wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1550 :    
1551 :     file-size wfileid -- ud wior file file_size
1552 :     struct stat buf;
1553 :     wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1554 :     ud = LONG2UD(buf.st_size);
1555 :    
1556 :     resize-file ud wfileid -- wior file resize_file
1557 :     wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1558 :    
1559 :     read-file c_addr u1 wfileid -- u2 wior file read_file
1560 :     /* !! fread does not guarantee enough */
1561 :     u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1562 :     wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1563 :     /* !! is the value of ferror errno-compatible? */
1564 :     if (wior)
1565 :     clearerr((FILE *)wfileid);
1566 :    
1567 :     read-line c_addr u1 wfileid -- u2 flag wior file read_line
1568 :     /*
1569 :     Cell c;
1570 :     flag=-1;
1571 :     for(u2=0; u2<u1; u2++)
1572 :     {
1573 :     *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1574 :     if(c=='\n') break;
1575 :     if(c==EOF)
1576 :     {
1577 :     flag=FLAG(u2!=0);
1578 :     break;
1579 :     }
1580 :     }
1581 :     wior=FILEIO(ferror((FILE *)wfileid));
1582 :     */
1583 :     if ((flag=FLAG(!feof((FILE *)wfileid) &&
1584 :     fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1585 :     wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1586 :     if (wior)
1587 :     clearerr((FILE *)wfileid);
1588 :     u2 = strlen(c_addr);
1589 :     u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1590 :     }
1591 :     else {
1592 :     wior=0;
1593 :     u2=0;
1594 :     }
1595 :    
1596 : pazsan 1.15 \+
1597 :     \+file
1598 : anton 1.1
1599 :     write-file c_addr u1 wfileid -- wior file write_file
1600 :     /* !! fwrite does not guarantee enough */
1601 :     {
1602 :     UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1603 :     wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1604 :     if (wior)
1605 :     clearerr((FILE *)wfileid);
1606 :     }
1607 :    
1608 : pazsan 1.17 \+
1609 :    
1610 : anton 1.1 emit-file c wfileid -- wior gforth emit_file
1611 : pazsan 1.17 #ifdef HAS_FILE
1612 : anton 1.1 wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1613 :     if (wior)
1614 :     clearerr((FILE *)wfileid);
1615 : pazsan 1.17 #else
1616 :     putc(c, stdout);
1617 :     #endif
1618 : anton 1.1
1619 : pazsan 1.15 \+file
1620 : anton 1.1
1621 :     flush-file wfileid -- wior file-ext flush_file
1622 :     wior = IOR(fflush((FILE *) wfileid)==EOF);
1623 :    
1624 :     file-status c_addr u -- ntype wior file-ext file_status
1625 :     char *filename=tilde_cstr(c_addr, u, 1);
1626 :     if (access (filename, F_OK) != 0) {
1627 :     ntype=0;
1628 :     wior=IOR(1);
1629 :     }
1630 :     else if (access (filename, R_OK | W_OK) == 0) {
1631 :     ntype=2; /* r/w */
1632 :     wior=0;
1633 :     }
1634 :     else if (access (filename, R_OK) == 0) {
1635 :     ntype=0; /* r/o */
1636 :     wior=0;
1637 :     }
1638 :     else if (access (filename, W_OK) == 0) {
1639 :     ntype=4; /* w/o */
1640 :     wior=0;
1641 :     }
1642 :     else {
1643 :     ntype=1; /* well, we cannot access the file, but better deliver a legal
1644 :     access mode (r/o bin), so we get a decent error later upon open. */
1645 :     wior=0;
1646 :     }
1647 :    
1648 : pazsan 1.15 \+
1649 :     \+floating
1650 : anton 1.1
1651 :     comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1652 :     comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1653 :    
1654 :     d>f d -- r float d_to_f
1655 :     #ifdef BUGGY_LONG_LONG
1656 :     extern double ldexp(double x, int exp);
1657 :     r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
1658 :     #else
1659 :     r = d;
1660 :     #endif
1661 :    
1662 :     f>d r -- d float f_to_d
1663 :     #ifdef BUGGY_LONG_LONG
1664 :     d.hi = ldexp(r,-CELL_BITS) - (r<0);
1665 :     d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1666 :     #else
1667 :     d = r;
1668 :     #endif
1669 :    
1670 :     f! r f_addr -- float f_store
1671 :     *f_addr = r;
1672 :    
1673 :     f@ f_addr -- r float f_fetch
1674 :     r = *f_addr;
1675 :    
1676 :     df@ df_addr -- r float-ext d_f_fetch
1677 :     #ifdef IEEE_FP
1678 :     r = *df_addr;
1679 :     #else
1680 :     !! df@
1681 :     #endif
1682 :    
1683 :     df! r df_addr -- float-ext d_f_store
1684 :     #ifdef IEEE_FP
1685 :     *df_addr = r;
1686 :     #else
1687 :     !! df!
1688 :     #endif
1689 :    
1690 :     sf@ sf_addr -- r float-ext s_f_fetch
1691 :     #ifdef IEEE_FP
1692 :     r = *sf_addr;
1693 :     #else
1694 :     !! sf@
1695 :     #endif
1696 :    
1697 :     sf! r sf_addr -- float-ext s_f_store
1698 :     #ifdef IEEE_FP
1699 :     *sf_addr = r;
1700 :     #else
1701 :     !! sf!
1702 :     #endif
1703 :    
1704 :     f+ r1 r2 -- r3 float f_plus
1705 :     r3 = r1+r2;
1706 :    
1707 :     f- r1 r2 -- r3 float f_minus
1708 :     r3 = r1-r2;
1709 :    
1710 :     f* r1 r2 -- r3 float f_star
1711 :     r3 = r1*r2;
1712 :    
1713 :     f/ r1 r2 -- r3 float f_slash
1714 :     r3 = r1/r2;
1715 :    
1716 :     f** r1 r2 -- r3 float-ext f_star_star
1717 :     ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1718 :     r3 = pow(r1,r2);
1719 :    
1720 :     fnegate r1 -- r2 float
1721 :     r2 = - r1;
1722 :    
1723 :     fdrop r -- float
1724 :    
1725 :     fdup r -- r r float
1726 :    
1727 :     fswap r1 r2 -- r2 r1 float
1728 :    
1729 :     fover r1 r2 -- r1 r2 r1 float
1730 :    
1731 :     frot r1 r2 r3 -- r2 r3 r1 float
1732 :    
1733 :     fnip r1 r2 -- r2 gforth
1734 :    
1735 :     ftuck r1 r2 -- r2 r1 r2 gforth
1736 :    
1737 :     float+ f_addr1 -- f_addr2 float float_plus
1738 :     f_addr2 = f_addr1+1;
1739 :    
1740 :     floats n1 -- n2 float
1741 :     n2 = n1*sizeof(Float);
1742 :    
1743 :     floor r1 -- r2 float
1744 :     ""round towards the next smaller integral value, i.e., round toward negative infinity""
1745 :     /* !! unclear wording */
1746 :     r2 = floor(r1);
1747 :    
1748 :     fround r1 -- r2 float
1749 :     ""round to the nearest integral value""
1750 :     /* !! unclear wording */
1751 :     #ifdef HAVE_RINT
1752 :     r2 = rint(r1);
1753 :     #else
1754 :     r2 = floor(r1+0.5);
1755 :     /* !! This is not quite true to the rounding rules given in the standard */
1756 :     #endif
1757 :    
1758 :     fmax r1 r2 -- r3 float
1759 :     if (r1<r2)
1760 :     r3 = r2;
1761 :     else
1762 :     r3 = r1;
1763 :    
1764 :     fmin r1 r2 -- r3 float
1765 :     if (r1<r2)
1766 :     r3 = r1;
1767 :     else
1768 :     r3 = r2;
1769 :    
1770 :     represent r c_addr u -- n f1 f2 float
1771 :     char *sig;
1772 :     int flag;
1773 :     int decpt;
1774 :     sig=ecvt(r, u, &decpt, &flag);
1775 :     n=(r==0 ? 1 : decpt);
1776 :     f1=FLAG(flag!=0);
1777 :     f2=FLAG(isdigit(sig[0])!=0);
1778 :     memmove(c_addr,sig,u);
1779 :    
1780 :     >float c_addr u -- flag float to_float
1781 :     /* real signature: c_addr u -- r t / f */
1782 :     Float r;
1783 :     char *number=cstr(c_addr, u, 1);
1784 :     char *endconv;
1785 :     while(isspace(number[--u]) && u>0);
1786 :     switch(number[u])
1787 :     {
1788 :     case 'd':
1789 :     case 'D':
1790 :     case 'e':
1791 :     case 'E': break;
1792 :     default : u++; break;
1793 :     }
1794 :     number[u]='\0';
1795 :     r=strtod(number,&endconv);
1796 :     if((flag=FLAG(!(Cell)*endconv)))
1797 :     {
1798 :     IF_FTOS(fp[0] = FTOS);
1799 :     fp += -1;
1800 :     FTOS = r;
1801 :     }
1802 :     else if(*endconv=='d' || *endconv=='D')
1803 :     {
1804 :     *endconv='E';
1805 :     r=strtod(number,&endconv);
1806 :     if((flag=FLAG(!(Cell)*endconv)))
1807 :     {
1808 :     IF_FTOS(fp[0] = FTOS);
1809 :     fp += -1;
1810 :     FTOS = r;
1811 :     }
1812 :     }
1813 :    
1814 :     fabs r1 -- r2 float-ext
1815 :     r2 = fabs(r1);
1816 :    
1817 :     facos r1 -- r2 float-ext
1818 :     r2 = acos(r1);
1819 :    
1820 :     fasin r1 -- r2 float-ext
1821 :     r2 = asin(r1);
1822 :    
1823 :     fatan r1 -- r2 float-ext
1824 :     r2 = atan(r1);
1825 :    
1826 :     fatan2 r1 r2 -- r3 float-ext
1827 :     ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1828 :     intends this to be the inverse of @code{fsincos}. In gforth it is.""
1829 :     r3 = atan2(r1,r2);
1830 :    
1831 :     fcos r1 -- r2 float-ext
1832 :     r2 = cos(r1);
1833 :    
1834 :     fexp r1 -- r2 float-ext
1835 :     r2 = exp(r1);
1836 :    
1837 :     fexpm1 r1 -- r2 float-ext
1838 :     ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1839 :     #ifdef HAVE_EXPM1
1840 : pazsan 1.3 extern double
1841 :     #ifdef NeXT
1842 :     const
1843 :     #endif
1844 :     expm1(double);
1845 : anton 1.1 r2 = expm1(r1);
1846 :     #else
1847 :     r2 = exp(r1)-1.;
1848 :     #endif
1849 :    
1850 :     fln r1 -- r2 float-ext
1851 :     r2 = log(r1);
1852 :    
1853 :     flnp1 r1 -- r2 float-ext
1854 :     ""@i{r2}=ln(@i{r1}+1)""
1855 :     #ifdef HAVE_LOG1P
1856 : pazsan 1.3 extern double
1857 :     #ifdef NeXT
1858 :     const
1859 :     #endif
1860 :     log1p(double);
1861 : anton 1.1 r2 = log1p(r1);
1862 :     #else
1863 :     r2 = log(r1+1.);
1864 :     #endif
1865 :    
1866 :     flog r1 -- r2 float-ext
1867 :     ""the decimal logarithm""
1868 :     r2 = log10(r1);
1869 :    
1870 :     falog r1 -- r2 float-ext
1871 :     ""@i{r2}=10**@i{r1}""
1872 :     extern double pow10(double);
1873 :     r2 = pow10(r1);
1874 :    
1875 :     fsin r1 -- r2 float-ext
1876 :     r2 = sin(r1);
1877 :    
1878 :     fsincos r1 -- r2 r3 float-ext
1879 :     ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1880 :     r2 = sin(r1);
1881 :     r3 = cos(r1);
1882 :    
1883 :     fsqrt r1 -- r2 float-ext
1884 :     r2 = sqrt(r1);
1885 :    
1886 :     ftan r1 -- r2 float-ext
1887 :     r2 = tan(r1);
1888 :     :
1889 :     fsincos f/ ;
1890 :    
1891 :     fsinh r1 -- r2 float-ext
1892 :     r2 = sinh(r1);
1893 :     :
1894 :     fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1895 :    
1896 :     fcosh r1 -- r2 float-ext
1897 :     r2 = cosh(r1);
1898 :     :
1899 :     fexp fdup 1/f f+ f2/ ;
1900 :    
1901 :     ftanh r1 -- r2 float-ext
1902 :     r2 = tanh(r1);
1903 :     :
1904 :     f2* fexpm1 fdup 2. d>f f+ f/ ;
1905 :    
1906 :     fasinh r1 -- r2 float-ext
1907 :     r2 = asinh(r1);
1908 :     :
1909 :     fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1910 :    
1911 :     facosh r1 -- r2 float-ext
1912 :     r2 = acosh(r1);
1913 :     :
1914 :     fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1915 :    
1916 :     fatanh r1 -- r2 float-ext
1917 :     r2 = atanh(r1);
1918 :     :
1919 :     fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1920 :     r> IF fnegate THEN ;
1921 :    
1922 :     sfloats n1 -- n2 float-ext s_floats
1923 :     n2 = n1*sizeof(SFloat);
1924 :    
1925 :     dfloats n1 -- n2 float-ext d_floats
1926 :     n2 = n1*sizeof(DFloat);
1927 :    
1928 :     sfaligned c_addr -- sf_addr float-ext s_f_aligned
1929 :     sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
1930 :     :
1931 :     [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
1932 :    
1933 :     dfaligned c_addr -- df_addr float-ext d_f_aligned
1934 :     df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
1935 :     :
1936 :     [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
1937 :    
1938 :     \ The following words access machine/OS/installation-dependent
1939 :     \ Gforth internals
1940 :     \ !! how about environmental queries DIRECT-THREADED,
1941 :     \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1942 :    
1943 :     \ local variable implementation primitives
1944 : pazsan 1.15 \+
1945 :     \+glocals
1946 : anton 1.1
1947 :     @local# -- w gforth fetch_local_number
1948 :     w = *(Cell *)(lp+(Cell)NEXT_INST);
1949 :     INC_IP(1);
1950 :    
1951 :     @local0 -- w new fetch_local_zero
1952 :     w = *(Cell *)(lp+0*sizeof(Cell));
1953 :    
1954 :     @local1 -- w new fetch_local_four
1955 :     w = *(Cell *)(lp+1*sizeof(Cell));
1956 :    
1957 :     @local2 -- w new fetch_local_eight
1958 :     w = *(Cell *)(lp+2*sizeof(Cell));
1959 :    
1960 :     @local3 -- w new fetch_local_twelve
1961 :     w = *(Cell *)(lp+3*sizeof(Cell));
1962 :    
1963 : pazsan 1.15 \+floating
1964 : anton 1.1
1965 :     f@local# -- r gforth f_fetch_local_number
1966 :     r = *(Float *)(lp+(Cell)NEXT_INST);
1967 :     INC_IP(1);
1968 :    
1969 :     f@local0 -- r new f_fetch_local_zero
1970 :     r = *(Float *)(lp+0*sizeof(Float));
1971 :    
1972 :     f@local1 -- r new f_fetch_local_eight
1973 :     r = *(Float *)(lp+1*sizeof(Float));
1974 :    
1975 : pazsan 1.15 \+
1976 : anton 1.1
1977 :     laddr# -- c_addr gforth laddr_number
1978 :     /* this can also be used to implement lp@ */
1979 :     c_addr = (Char *)(lp+(Cell)NEXT_INST);
1980 :     INC_IP(1);
1981 :    
1982 :     lp+!# -- gforth lp_plus_store_number
1983 :     ""used with negative immediate values it allocates memory on the
1984 :     local stack, a positive immediate argument drops memory from the local
1985 :     stack""
1986 :     lp += (Cell)NEXT_INST;
1987 :     INC_IP(1);
1988 :    
1989 :     lp- -- new minus_four_lp_plus_store
1990 :     lp += -sizeof(Cell);
1991 :    
1992 :     lp+ -- new eight_lp_plus_store
1993 :     lp += sizeof(Float);
1994 :    
1995 :     lp+2 -- new sixteen_lp_plus_store
1996 :     lp += 2*sizeof(Float);
1997 :    
1998 :     lp! c_addr -- gforth lp_store
1999 :     lp = (Address)c_addr;
2000 :    
2001 :     >l w -- gforth to_l
2002 :     lp -= sizeof(Cell);
2003 :     *(Cell *)lp = w;
2004 :    
2005 : pazsan 1.15 \+floating
2006 : anton 1.1
2007 :     f>l r -- gforth f_to_l
2008 :     lp -= sizeof(Float);
2009 :     *(Float *)lp = r;
2010 :    
2011 : anton 1.11 fpick u -- r gforth
2012 :     r = fp[u+1]; /* +1, because update of fp happens before this fragment */
2013 :     :
2014 :     floats fp@ + f@ ;
2015 :    
2016 : pazsan 1.15 \+
2017 :     \+
2018 : anton 1.1
2019 : pazsan 1.15 \+OS
2020 : anton 1.1
2021 :     define(`uploop',
2022 :     `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
2023 :     define(`_uploop',
2024 :     `ifelse($1, `$3', `$5',
2025 :     `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
2026 :     \ argflist(argnum): Forth argument list
2027 :     define(argflist,
2028 :     `ifelse($1, 0, `',
2029 :     `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
2030 :     \ argdlist(argnum): declare C's arguments
2031 :     define(argdlist,
2032 :     `ifelse($1, 0, `',
2033 :     `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
2034 :     \ argclist(argnum): pass C's arguments
2035 :     define(argclist,
2036 :     `ifelse($1, 0, `',
2037 :     `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
2038 :     \ icall(argnum)
2039 :     define(icall,
2040 :     `icall$1 argflist($1)u -- uret gforth
2041 : pazsan 1.9 uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
2042 : anton 1.1
2043 :     ')
2044 :     define(fcall,
2045 :     `fcall$1 argflist($1)u -- rret gforth
2046 : pazsan 1.9 rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
2047 : anton 1.1
2048 :     ')
2049 :    
2050 :    
2051 :     open-lib c_addr1 u1 -- u2 gforth open_lib
2052 :     #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2053 : anton 1.8 #ifndef RTLD_GLOBAL
2054 :     #define RTLD_GLOBAL 0
2055 :     #endif
2056 : pazsan 1.7 u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_GLOBAL | RTLD_LAZY);
2057 : anton 1.1 #else
2058 : pazsan 1.18 # ifdef _WIN32
2059 : anton 1.1 u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
2060 :     # else
2061 :     #warning Define open-lib!
2062 :     u2 = 0;
2063 :     # endif
2064 :     #endif
2065 :    
2066 :     lib-sym c_addr1 u1 u2 -- u3 gforth lib_sym
2067 :     #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2068 :     u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
2069 :     #else
2070 : pazsan 1.18 # ifdef _WIN32
2071 : anton 1.1 u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
2072 :     # else
2073 :     #warning Define lib-sym!
2074 :     u3 = 0;
2075 :     # endif
2076 :     #endif
2077 :    
2078 :     uploop(i, 0, 7, `icall(i)')
2079 :     icall(20)
2080 :     uploop(i, 0, 7, `fcall(i)')
2081 :     fcall(20)
2082 :    
2083 : pazsan 1.15 \+
2084 : anton 1.1
2085 :     up! a_addr -- gforth up_store
2086 :     UP=up=(char *)a_addr;
2087 :     :
2088 :     up ! ;
2089 :     Variable UP

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help