[gforth] / gforth / Attic / primitives  

gforth: gforth/Attic/primitives


1 : anton 1.6 \ Copyright 1992 by the ANSI figForth Development Group
2 :     \
3 :     \ WARNING: This file is processed by m4. Make sure your identifiers
4 :     \ don't collide with m4's (e.g. by undefining them).
5 :     \
6 : pazsan 1.23 \
7 :     \
8 : anton 1.6 \ This file contains instructions in the following format:
9 :     \
10 : pazsan 1.23 \ forth name stack effect category [pronunciation]
11 : anton 1.6 \ [""glossary entry""]
12 :     \ C code
13 :     \ [:
14 :     \ Forth code]
15 :     \
16 : pazsan 1.23 \ The pronunciation is also used for forming C names.
17 :     \
18 :     \
19 : anton 1.6 \
20 : pazsan 1.23 \ These informations are automatically translated into C-code for the
21 :     \ interpreter and into some other files. I hope that your C compiler has
22 : anton 1.6 \ decent optimization, otherwise the automatically generated code will
23 :     \ be somewhat slow. The Forth version of the code is included for manual
24 :     \ compilers, so they will need to compile only the important words.
25 :     \
26 :     \ Note that stack pointer adjustment is performed according to stack
27 :     \ effect by automatically generated code and NEXT is automatically
28 :     \ appended to the C code. Also, you can use the names in the stack
29 :     \ effect in the C code. Stack access is automatic. One exception: if
30 :     \ your code does not fall through, the results are not stored into the
31 :     \ stack. Use different names on both sides of the '--', if you change a
32 :     \ value (some stores to the stack are optimized away).
33 :     \
34 : pazsan 1.23 \
35 :     \
36 : anton 1.6 \ The stack variables have the following types:
37 : pazsan 1.23 \
38 : anton 1.6 \ name matches type
39 :     \ f.* Bool
40 :     \ c.* Char
41 :     \ [nw].* Cell
42 :     \ u.* UCell
43 :     \ d.* DCell
44 :     \ ud.* UDCell
45 :     \ r.* Float
46 :     \ a_.* Cell *
47 :     \ c_.* Char *
48 :     \ f_.* Float *
49 :     \ df_.* DFloat *
50 :     \ sf_.* SFloat *
51 :     \ xt.* XT
52 :     \ wid.* WID
53 :     \ f83name.* F83Name *
54 :     \
55 : pazsan 1.23 \
56 :     \
57 : anton 1.6 \ In addition the following names can be used:
58 :     \ ip the instruction pointer
59 :     \ sp the data stack pointer
60 :     \ rp the parameter stack pointer
61 : pazsan 1.23 \ lp the locals stack pointer
62 : anton 1.6 \ NEXT executes NEXT
63 :     \ cfa
64 :     \ NEXT1 executes NEXT1
65 :     \ FLAG(x) makes a Forth flag from a C flag
66 :     \
67 : pazsan 1.23 \
68 :     \
69 : anton 1.6 \ Percentages in comments are from Koopmans book: average/maximum use
70 : pazsan 1.23 \ (taken from four, not very representative benchmarks)
71 :     \
72 : anton 1.6 \
73 : pazsan 1.23 \
74 : anton 1.6 \ To do:
75 :     \
76 :     \ throw execute, cfa and NEXT1 out?
77 :     \ macroize *ip, ip++, *ip++ (pipelining)?
78 : anton 1.1
79 : anton 1.6 \ these m4 macros would collide with identifiers
80 : anton 1.1 undefine(`index')
81 :     undefine(`shift')
82 :    
83 :     noop -- fig
84 :     ;
85 : pazsan 1.18 :
86 :     ;
87 : anton 1.1
88 :     lit -- w fig
89 : anton 1.35 w = (Cell)NEXT_INST;
90 :     INC_IP(1);
91 : anton 1.1
92 : anton 1.42 execute xt -- core
93 : anton 1.35 ip=IP;
94 : anton 1.1 cfa = xt;
95 :     IF_TOS(TOS = sp[0]);
96 :     NEXT1;
97 :    
98 : anton 1.9 branch-lp+!# -- new branch_lp_plus_store_number
99 :     /* this will probably not be used */
100 :     branch_adjust_lp:
101 : anton 1.35 lp += (Cell)(IP[1]);
102 : anton 1.9 goto branch;
103 :    
104 : anton 1.1 branch -- fig
105 :     branch:
106 : anton 1.35 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
107 :     NEXT_P0;
108 : pazsan 1.18 :
109 :     r> dup @ + >r ;
110 : anton 1.1
111 : anton 1.9 \ condbranch(forthname,restline,code)
112 : anton 1.35 \ this is non-syntactical: code must open a brace that is closed by the macro
113 : anton 1.9 define(condbranch,
114 :     $1 $2
115 : anton 1.35 $3 ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
116 :     NEXT_P0;
117 :     NEXT;
118 : anton 1.9 }
119 :     else
120 : anton 1.35 INC_IP(1);
121 : anton 1.9
122 :     $1-lp+!# $2_lp_plus_store_number
123 :     $3 goto branch_adjust_lp;
124 :     }
125 :     else
126 : anton 1.35 INC_IP(2);
127 : anton 1.9
128 :     )
129 :    
130 :     condbranch(?branch,f -- f83 question_branch,
131 : anton 1.1 if (f==0) {
132 :     IF_TOS(TOS = sp[0]);
133 : anton 1.9 )
134 : anton 1.1
135 : anton 1.9 condbranch((next),-- cmFORTH paren_next,
136 : anton 1.1 if ((*rp)--) {
137 : anton 1.9 )
138 : anton 1.1
139 : anton 1.9 condbranch((loop),-- fig paren_loop,
140 : pazsan 1.31 Cell index = *rp+1;
141 :     Cell limit = rp[1];
142 : anton 1.1 if (index != limit) {
143 :     *rp = index;
144 : anton 1.9 )
145 : anton 1.1
146 : anton 1.42 condbranch((+loop),n -- gforth paren_plus_loop,
147 : anton 1.1 /* !! check this thoroughly */
148 : pazsan 1.31 Cell index = *rp;
149 : anton 1.1 /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
150 :     /* dependent upon two's complement arithmetic */
151 : pazsan 1.31 Cell olddiff = index-rp[1];
152 : pazsan 1.33 #ifndef undefined
153 : anton 1.9 if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
154 :     || (olddiff^n)>=0 /* it is a wrap-around effect */) {
155 : pazsan 1.15 #else
156 :     #ifndef MAXINT
157 : pazsan 1.30 #define MAXINT ((((Cell)1)<<(8*sizeof(Cell)-1))-1)
158 : pazsan 1.15 #endif
159 : pazsan 1.18 if(((olddiff^MAXINT) >= n) ^ ((olddiff+n) < 0)) {
160 : pazsan 1.15 #endif
161 :     #ifdef i386
162 :     *rp += n;
163 :     #else
164 :     *rp = index + n;
165 :     #endif
166 : anton 1.1 IF_TOS(TOS = sp[0]);
167 : anton 1.9 )
168 : anton 1.1
169 : anton 1.42 condbranch((-loop),u -- gforth paren_minus_loop,
170 : anton 1.41 /* !! check this thoroughly */
171 :     Cell index = *rp;
172 :     /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
173 :     /* dependent upon two's complement arithmetic */
174 :     UCell olddiff = index-rp[1];
175 :     if (olddiff>u) {
176 :     *rp = index - u;
177 :     IF_TOS(TOS = sp[0]);
178 :     )
179 :    
180 : anton 1.42 condbranch((s+loop),n -- gforth paren_symmetric_plus_loop,
181 : anton 1.1 ""The run-time procedure compiled by S+LOOP. It loops until the index
182 :     crosses the boundary between limit and limit-sign(n). I.e. a symmetric
183 :     version of (+LOOP).""
184 :     /* !! check this thoroughly */
185 : pazsan 1.31 Cell index = *rp;
186 :     Cell diff = index-rp[1];
187 :     Cell newdiff = diff+n;
188 : anton 1.1 if (n<0) {
189 :     diff = -diff;
190 : pazsan 1.15 newdiff = -newdiff;
191 : anton 1.1 }
192 :     if (diff>=0 || newdiff<0) {
193 : pazsan 1.15 #ifdef i386
194 :     *rp += n;
195 :     #else
196 :     *rp = index + n;
197 :     #endif
198 : anton 1.1 IF_TOS(TOS = sp[0]);
199 : anton 1.9 )
200 : anton 1.1
201 :     unloop -- core
202 :     rp += 2;
203 : pazsan 1.18 :
204 :     r> rdrop rdrop >r ;
205 : anton 1.1
206 :     (for) ncount -- cmFORTH paren_for
207 :     /* or (for) = >r -- collides with unloop! */
208 :     *--rp = 0;
209 :     *--rp = ncount;
210 : pazsan 1.18 :
211 :     r> swap 0 >r >r >r ;
212 : anton 1.1
213 :     (do) nlimit nstart -- fig paren_do
214 :     /* or do it in high-level? 0.09/0.23% */
215 :     *--rp = nlimit;
216 :     *--rp = nstart;
217 :     :
218 : pazsan 1.13 r> -rot swap >r >r >r ;
219 : anton 1.1
220 : anton 1.41 (?do) nlimit nstart -- new paren_question_do
221 : anton 1.1 *--rp = nlimit;
222 :     *--rp = nstart;
223 :     if (nstart == nlimit) {
224 :     IF_TOS(TOS = sp[0]);
225 :     goto branch;
226 :     }
227 :     else {
228 : anton 1.35 INC_IP(1);
229 : anton 1.1 }
230 :    
231 : anton 1.41 (+do) nlimit nstart -- new paren_plus_do
232 :     *--rp = nlimit;
233 :     *--rp = nstart;
234 :     if (nstart >= nlimit) {
235 :     IF_TOS(TOS = sp[0]);
236 :     goto branch;
237 :     }
238 :     else {
239 :     INC_IP(1);
240 :     }
241 :    
242 :     (u+do) ulimit ustart -- new paren_u_plus_do
243 :     *--rp = ulimit;
244 :     *--rp = ustart;
245 :     if (ustart >= ulimit) {
246 :     IF_TOS(TOS = sp[0]);
247 :     goto branch;
248 :     }
249 :     else {
250 :     INC_IP(1);
251 :     }
252 :    
253 :     (-do) nlimit nstart -- new paren_minus_do
254 :     *--rp = nlimit;
255 :     *--rp = nstart;
256 :     if (nstart <= nlimit) {
257 :     IF_TOS(TOS = sp[0]);
258 :     goto branch;
259 :     }
260 :     else {
261 :     INC_IP(1);
262 :     }
263 :    
264 :     (u-do) ulimit ustart -- new paren_u_minus_do
265 :     *--rp = ulimit;
266 :     *--rp = ustart;
267 :     if (ustart <= ulimit) {
268 :     IF_TOS(TOS = sp[0]);
269 :     goto branch;
270 :     }
271 :     else {
272 :     INC_IP(1);
273 :     }
274 :    
275 : anton 1.42 i -- n core
276 : anton 1.1 n = *rp;
277 :    
278 :     j -- n core
279 :     n = rp[2];
280 :    
281 : anton 1.6 \ digit is high-level: 0/0%
282 : anton 1.1
283 : pazsan 1.10 (emit) c -- fig paren_emit
284 : anton 1.1 putchar(c);
285 :     emitcounter++;
286 : pazsan 1.10
287 : anton 1.42 (type) c_addr n -- gforth paren_type
288 : pazsan 1.10 fwrite(c_addr,sizeof(Char),n,stdout);
289 :     emitcounter += n;
290 : anton 1.1
291 : pazsan 1.15 (key) -- n fig paren_key
292 : anton 1.1 fflush(stdout);
293 :     /* !! noecho */
294 :     n = key();
295 :    
296 : anton 1.42 key? -- n facility key_q
297 : pazsan 1.2 fflush(stdout);
298 :     n = key_query;
299 :    
300 : anton 1.42 cr -- core
301 : anton 1.1 puts("");
302 : pazsan 1.18 :
303 :     $0A emit ;
304 : anton 1.1
305 :     move c_from c_to ucount -- core
306 :     memmove(c_to,c_from,ucount);
307 : anton 1.6 /* make an Ifdef for bsd and others? */
308 : pazsan 1.18 :
309 :     >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
310 : anton 1.1
311 :     cmove c_from c_to u -- string
312 :     while (u-- > 0)
313 :     *c_to++ = *c_from++;
314 : pazsan 1.18 :
315 :     bounds ?DO dup c@ I c! 1+ LOOP drop ;
316 : anton 1.1
317 :     cmove> c_from c_to u -- string c_move_up
318 :     while (u-- > 0)
319 :     c_to[u] = c_from[u];
320 : pazsan 1.18 :
321 :     dup 0= IF drop 2drop exit THEN
322 :     rot over + -rot bounds swap 1-
323 :     DO 1- dup c@ I c! -1 +LOOP drop ;
324 : anton 1.1
325 :     fill c_addr u c -- core
326 :     memset(c_addr,c,u);
327 : pazsan 1.18 :
328 :     -rot bounds
329 :     ?DO dup I c! LOOP drop ;
330 : anton 1.1
331 :     compare c_addr1 u1 c_addr2 u2 -- n string
332 : anton 1.26 ""Compare the strings lexicographically. If they are equal, n is 0; if
333 :     the first string is smaller, n is -1; if the first string is larger, n
334 :     is 1. Currently this is based on the machine's character
335 :     comparison. In the future, this may change to considering the current
336 :     locale and its collation order.""
337 : anton 1.1 n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
338 :     if (n==0)
339 :     n = u1-u2;
340 :     if (n<0)
341 :     n = -1;
342 :     else if (n>0)
343 :     n = 1;
344 : pazsan 1.18 :
345 :     rot 2dup - >r min swap -text dup
346 :     IF rdrop
347 :     ELSE drop r@ 0>
348 :     IF rdrop -1
349 :     ELSE r> 1 and
350 :     THEN
351 :     THEN ;
352 : anton 1.1
353 :     -text c_addr1 u c_addr2 -- n new dash_text
354 :     n = memcmp(c_addr1, c_addr2, u);
355 :     if (n<0)
356 :     n = -1;
357 :     else if (n>0)
358 :     n = 1;
359 : pazsan 1.18 :
360 :     swap bounds
361 :     ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
362 :     ELSE c@ I c@ - unloop THEN -text-flag ;
363 :     : -text-flag ( n -- -1/0/1 )
364 :     dup 0< IF drop -1 ELSE 0> IF 1 ELSE 0 THEN THEN ;
365 : anton 1.1
366 :     capscomp c_addr1 u c_addr2 -- n new
367 :     Char c1, c2;
368 :     for (;; u--, c_addr1++, c_addr2++) {
369 :     if (u == 0) {
370 :     n = 0;
371 :     break;
372 :     }
373 :     c1 = toupper(*c_addr1);
374 :     c2 = toupper(*c_addr2);
375 :     if (c1 != c2) {
376 :     if (c1 < c2)
377 :     n = -1;
378 :     else
379 :     n = 1;
380 :     break;
381 :     }
382 :     }
383 : pazsan 1.18 :
384 :     swap bounds
385 :     ?DO dup c@ toupper I c@ toupper = WHILE 1+ LOOP drop 0
386 :     ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
387 : anton 1.1
388 :     -trailing c_addr u1 -- c_addr u2 string dash_trailing
389 :     u2 = u1;
390 :     while (c_addr[u2-1] == ' ')
391 :     u2--;
392 : pazsan 1.18 :
393 :     BEGIN 1- 2dup + c@ bl = WHILE
394 :     dup 0= UNTIL ELSE 1+ THEN ;
395 : anton 1.1
396 :     /string c_addr1 u1 n -- c_addr2 u2 string slash_string
397 :     c_addr2 = c_addr1+n;
398 :     u2 = u1-n;
399 : pazsan 1.18 :
400 :     tuck - >r + r> dup 0< IF - 0 THEN ;
401 : anton 1.1
402 : anton 1.42 + n1 n2 -- n core plus
403 : anton 1.1 n = n1+n2;
404 :    
405 : anton 1.42 - n1 n2 -- n core minus
406 : anton 1.1 n = n1-n2;
407 : pazsan 1.18 :
408 :     negate + ;
409 : anton 1.1
410 : anton 1.42 negate n1 -- n2 core
411 : anton 1.1 /* use minus as alias */
412 :     n2 = -n1;
413 : pazsan 1.18 :
414 :     invert 1+ ;
415 : anton 1.1
416 :     1+ n1 -- n2 core one_plus
417 :     n2 = n1+1;
418 : pazsan 1.18 :
419 :     1 + ;
420 : anton 1.1
421 :     1- n1 -- n2 core one_minus
422 :     n2 = n1-1;
423 : pazsan 1.18 :
424 :     1 - ;
425 : anton 1.1
426 :     max n1 n2 -- n core
427 :     if (n1<n2)
428 :     n = n2;
429 :     else
430 :     n = n1;
431 :     :
432 : pazsan 1.18 2dup < IF swap THEN drop ;
433 : anton 1.1
434 :     min n1 n2 -- n core
435 :     if (n1<n2)
436 :     n = n1;
437 :     else
438 :     n = n2;
439 : pazsan 1.18 :
440 :     2dup > IF swap THEN drop ;
441 : anton 1.1
442 :     abs n1 -- n2 core
443 :     if (n1<0)
444 :     n2 = -n1;
445 :     else
446 :     n2 = n1;
447 : pazsan 1.18 :
448 :     dup 0< IF negate THEN ;
449 : anton 1.1
450 : anton 1.42 * n1 n2 -- n core star
451 : anton 1.1 n = n1*n2;
452 : pazsan 1.18 :
453 :     um* drop ;
454 : anton 1.1
455 : anton 1.42 / n1 n2 -- n core slash
456 : anton 1.1 n = n1/n2;
457 : pazsan 1.18 :
458 :     /mod nip ;
459 : anton 1.1
460 :     mod n1 n2 -- n core
461 :     n = n1%n2;
462 : pazsan 1.18 :
463 :     /mod drop ;
464 : anton 1.1
465 :     /mod n1 n2 -- n3 n4 core slash_mod
466 :     n4 = n1/n2;
467 :     n3 = n1%n2; /* !! is this correct? look into C standard! */
468 : pazsan 1.18 :
469 :     >r s>d r> fm/mod ;
470 : anton 1.1
471 :     2* n1 -- n2 core two_star
472 :     n2 = 2*n1;
473 : pazsan 1.18 :
474 :     dup + ;
475 : anton 1.1
476 :     2/ n1 -- n2 core two_slash
477 :     /* !! is this still correct? */
478 :     n2 = n1>>1;
479 :    
480 :     fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
481 :     ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
482 :     /* assumes that the processor uses either floored or symmetric division */
483 :     n3 = d1/n1;
484 :     n2 = d1%n1;
485 :     /* note that this 1%-3>0 is optimized by the compiler */
486 :     if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
487 :     n3--;
488 :     n2+=n1;
489 :     }
490 :    
491 :     sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
492 :     ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
493 :     /* assumes that the processor uses either floored or symmetric division */
494 :     n3 = d1/n1;
495 :     n2 = d1%n1;
496 :     /* note that this 1%-3<0 is optimized by the compiler */
497 :     if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
498 :     n3++;
499 :     n2-=n1;
500 :     }
501 : pazsan 1.18 :
502 :     over >r dup >r abs -rot
503 :     dabs rot um/mod
504 :     r> 0< IF negate THEN
505 :     r> 0< IF swap negate swap THEN ;
506 : anton 1.1
507 :     m* n1 n2 -- d core m_star
508 :     d = (DCell)n1 * (DCell)n2;
509 : pazsan 1.18 :
510 :     2dup 0< and >r
511 :     2dup swap 0< and >r
512 :     um* r> - r> - ;
513 : anton 1.1
514 :     um* u1 u2 -- ud core u_m_star
515 :     /* use u* as alias */
516 :     ud = (UDCell)u1 * (UDCell)u2;
517 :    
518 :     um/mod ud u1 -- u2 u3 core u_m_slash_mod
519 :     u3 = ud/u1;
520 :     u2 = ud%u1;
521 : pazsan 1.19 :
522 :     dup IF 0 (um/mod) THEN nip ;
523 :     : (um/mod) ( ud ud--ud u)
524 :     2dup >r >r dup 0<
525 :     IF 2drop 0
526 :     ELSE 2dup d+ (um/mod) 2* THEN
527 :     -rot r> r> 2over 2over du<
528 :     IF 2drop rot
529 :     ELSE dnegate d+ rot 1+ THEN ;
530 : anton 1.1
531 :     m+ d1 n -- d2 double m_plus
532 :     d2 = d1+n;
533 : pazsan 1.18 :
534 :     s>d d+ ;
535 : anton 1.1
536 : anton 1.42 d+ d1 d2 -- d double d_plus
537 : anton 1.1 d = d1+d2;
538 : pazsan 1.18 :
539 :     >r swap >r over 2/ over 2/ + >r over 1 and over 1 and + 2/
540 :     r> + >r + r> 0< r> r> + swap - ;
541 : anton 1.1
542 :     d- d1 d2 -- d double d_minus
543 :     d = d1-d2;
544 : pazsan 1.18 :
545 :     dnegate d+ ;
546 : anton 1.1
547 :     dnegate d1 -- d2 double
548 :     /* use dminus as alias */
549 :     d2 = -d1;
550 : pazsan 1.18 :
551 :     invert swap negate tuck 0= - ;
552 : anton 1.1
553 :     dmax d1 d2 -- d double
554 :     if (d1<d2)
555 :     d = d2;
556 :     else
557 :     d = d1;
558 : pazsan 1.18 :
559 :     2over 2over d> IF 2swap THEN 2drop ;
560 : anton 1.1
561 :     dmin d1 d2 -- d double
562 :     if (d1<d2)
563 :     d = d1;
564 :     else
565 :     d = d2;
566 : pazsan 1.18 :
567 :     2over 2over d< IF 2swap THEN 2drop ;
568 : anton 1.1
569 :     dabs d1 -- d2 double
570 :     if (d1<0)
571 :     d2 = -d1;
572 :     else
573 :     d2 = d1;
574 : pazsan 1.18 :
575 :     dup 0< IF dnegate THEN ;
576 : anton 1.1
577 :     d2* d1 -- d2 double d_two_star
578 :     d2 = 2*d1;
579 : pazsan 1.18 :
580 :     2dup d+ ;
581 : anton 1.1
582 :     d2/ d1 -- d2 double d_two_slash
583 :     /* !! is this still correct? */
584 : pazsan 1.13 d2 = d1>>1;
585 : pazsan 1.18 :
586 :     dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
587 :     r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
588 : anton 1.1
589 :     d>s d -- n double d_to_s
590 :     /* make this an alias for drop? */
591 :     n = d;
592 : pazsan 1.18 :
593 :     drop ;
594 : anton 1.1
595 : anton 1.42 and w1 w2 -- w core
596 : anton 1.1 w = w1&w2;
597 :    
598 : anton 1.42 or w1 w2 -- w core
599 : anton 1.1 w = w1|w2;
600 :    
601 : anton 1.42 xor w1 w2 -- w core
602 : anton 1.1 w = w1^w2;
603 :    
604 :     invert w1 -- w2 core
605 :     w2 = ~w1;
606 : pazsan 1.18 :
607 :     -1 xor ;
608 : anton 1.1
609 :     rshift u1 n -- u2 core
610 :     u2 = u1>>n;
611 :    
612 :     lshift u1 n -- u2 core
613 :     u2 = u1<<n;
614 :    
615 : anton 1.6 \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
616 : anton 1.1 define(comparisons,
617 :     $1= $2 -- f $6 $3equals
618 :     f = FLAG($4==$5);
619 :    
620 :     $1<> $2 -- f $7 $3different
621 :     /* use != as alias ? */
622 :     f = FLAG($4!=$5);
623 :    
624 :     $1< $2 -- f $8 $3less
625 :     f = FLAG($4<$5);
626 :    
627 :     $1> $2 -- f $9 $3greater
628 :     f = FLAG($4>$5);
629 :    
630 :     $1<= $2 -- f new $3less_or_equal
631 :     f = FLAG($4<=$5);
632 :    
633 :     $1>= $2 -- f new $3greater_or_equal
634 :     f = FLAG($4>=$5);
635 :    
636 :     )
637 :    
638 :     comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
639 :     comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
640 :     comparisons(u, u1 u2, u_, u1, u2, new, new, core, core-ext)
641 :     comparisons(d, d1 d2, d_, d1, d2, double, new, double, new)
642 :     comparisons(d0, d, d_zero_, d, 0, double, new, double, new)
643 :     comparisons(du, ud1 ud2, d_u_, ud1, ud2, new, new, double-ext, new)
644 :    
645 :     within u1 u2 u3 -- f core-ext
646 :     f = FLAG(u1-u2 < u3-u2);
647 : pazsan 1.18 :
648 :     over - >r - r> u< ;
649 : anton 1.1
650 :     sp@ -- a_addr fig spat
651 : pazsan 1.15 a_addr = sp+1;
652 : anton 1.1
653 :     sp! a_addr -- fig spstore
654 : pazsan 1.15 sp = a_addr;
655 : anton 1.1 /* works with and without TOS caching */
656 :    
657 :     rp@ -- a_addr fig rpat
658 :     a_addr = rp;
659 :    
660 :     rp! a_addr -- fig rpstore
661 :     rp = a_addr;
662 :    
663 :     fp@ -- f_addr new fp_fetch
664 :     f_addr = fp;
665 :    
666 :     fp! f_addr -- new fp_store
667 :     fp = f_addr;
668 :    
669 : anton 1.25 ;s -- fig semis
670 : anton 1.1 ip = (Xt *)(*rp++);
671 : anton 1.35 NEXT_P0;
672 : anton 1.1
673 : anton 1.42 >r w -- core to_r
674 : anton 1.1 *--rp = w;
675 :    
676 : anton 1.42 r> -- w core r_from
677 : anton 1.1 w = *rp++;
678 :    
679 : anton 1.42 r@ -- w core r_fetch
680 : anton 1.1 /* use r as alias */
681 :     /* make r@ an alias for i */
682 :     w = *rp;
683 :    
684 : anton 1.42 rdrop -- gforth
685 : anton 1.1 rp++;
686 :    
687 : anton 1.42 i' -- w gforth i_tick
688 : anton 1.1 w=rp[1];
689 :    
690 : anton 1.14 2>r w1 w2 -- core-ext two_to_r
691 :     *--rp = w1;
692 :     *--rp = w2;
693 :    
694 :     2r> -- w1 w2 core-ext two_r_from
695 :     w2 = *rp++;
696 :     w1 = *rp++;
697 :    
698 :     2r@ -- w1 w2 core-ext two_r_fetch
699 :     w2 = rp[0];
700 :     w1 = rp[1];
701 :    
702 : anton 1.42 2rdrop -- gforth two_r_drop
703 : anton 1.14 rp+=2;
704 :    
705 : anton 1.42 over w1 w2 -- w1 w2 w1 core
706 : anton 1.1
707 : anton 1.42 drop w -- core
708 : anton 1.1
709 : anton 1.42 swap w1 w2 -- w2 w1 core
710 : anton 1.1
711 : anton 1.42 dup w -- w w core
712 : anton 1.1
713 :     rot w1 w2 w3 -- w2 w3 w1 core rote
714 :    
715 : anton 1.42 -rot w1 w2 w3 -- w3 w1 w2 gforth not_rote
716 : pazsan 1.18 :
717 :     rot rot ;
718 : anton 1.1
719 :     nip w1 w2 -- w2 core-ext
720 : pazsan 1.18 :
721 :     swap drop ;
722 : anton 1.1
723 :     tuck w1 w2 -- w2 w1 w2 core-ext
724 : pazsan 1.18 :
725 :     swap over ;
726 : anton 1.1
727 :     ?dup w -- w core question_dupe
728 :     if (w!=0) {
729 : pazsan 1.7 IF_TOS(*sp-- = w;)
730 : anton 1.1 #ifndef USE_TOS
731 : pazsan 1.7 *--sp = w;
732 : anton 1.1 #endif
733 :     }
734 : pazsan 1.18 :
735 :     dup IF dup THEN ;
736 : anton 1.1
737 :     pick u -- w core-ext
738 :     w = sp[u+1];
739 : pazsan 1.18 :
740 :     1+ cells sp@ + @ ;
741 : anton 1.1
742 :     2drop w1 w2 -- core two_drop
743 : pazsan 1.18 :
744 :     drop drop ;
745 : anton 1.1
746 :     2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
747 : pazsan 1.18 :
748 :     over over ;
749 : anton 1.1
750 :     2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
751 : pazsan 1.18 :
752 :     3 pick 3 pick ;
753 : anton 1.1
754 :     2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
755 : pazsan 1.18 :
756 :     >r -rot r> -rot ;
757 : anton 1.1
758 :     2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double two_rote
759 : pazsan 1.18 :
760 :     >r >r 2swap r> r> 2swap ;
761 : anton 1.1
762 : anton 1.42 2nip w1 w2 w3 w4 -- w3 w4 gforth two_nip
763 :     :
764 :     2swap 2drop ;
765 :    
766 :     2tuck w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 gforth two_tuck
767 :     :
768 :     2swap 2over ;
769 :    
770 : anton 1.6 \ toggle is high-level: 0.11/0.42%
771 : anton 1.1
772 : anton 1.42 @ a_addr -- w core fetch
773 : anton 1.1 w = *a_addr;
774 :    
775 : anton 1.42 ! w a_addr -- core store
776 : anton 1.1 *a_addr = w;
777 :    
778 : anton 1.42 +! n a_addr -- core plus_store
779 : anton 1.1 *a_addr += n;
780 :    
781 : anton 1.42 c@ c_addr -- c core cfetch
782 : anton 1.1 c = *c_addr;
783 :    
784 : anton 1.42 c! c c_addr -- core cstore
785 : anton 1.1 *c_addr = c;
786 :    
787 :     2! w1 w2 a_addr -- core two_store
788 :     a_addr[0] = w2;
789 :     a_addr[1] = w1;
790 : pazsan 1.18 :
791 :     tuck ! cell+ ! ;
792 : anton 1.1
793 :     2@ a_addr -- w1 w2 core two_fetch
794 :     w2 = a_addr[0];
795 :     w1 = a_addr[1];
796 : pazsan 1.18 :
797 :     dup cell+ @ swap @ ;
798 : anton 1.1
799 :     d! d a_addr -- double d_store
800 :     /* !! alignment problems on some machines */
801 :     *(DCell *)a_addr = d;
802 :    
803 :     d@ a_addr -- d double d_fetch
804 :     d = *(DCell *)a_addr;
805 :    
806 :     cell+ a_addr1 -- a_addr2 core cell_plus
807 :     a_addr2 = a_addr1+1;
808 : pazsan 1.18 :
809 :     [ cell ] Literal + ;
810 : anton 1.1
811 :     cells n1 -- n2 core
812 :     n2 = n1 * sizeof(Cell);
813 : pazsan 1.18 :
814 :     [ cell ]
815 :     [ 2/ dup ] [IF] 2* [THEN]
816 :     [ 2/ dup ] [IF] 2* [THEN]
817 :     [ 2/ dup ] [IF] 2* [THEN]
818 :     [ 2/ dup ] [IF] 2* [THEN]
819 :     [ drop ] ;
820 : anton 1.1
821 :     char+ c_addr1 -- c_addr2 core care_plus
822 : pazsan 1.18 c_addr2 = c_addr1 + 1;
823 :     :
824 :     1+ ;
825 : anton 1.1
826 : anton 1.24 (chars) n1 -- n2 gforth paren_cares
827 : anton 1.1 n2 = n1 * sizeof(Char);
828 : pazsan 1.18 :
829 :     ;
830 : anton 1.1
831 :     count c_addr1 -- c_addr2 u core
832 :     u = *c_addr1;
833 :     c_addr2 = c_addr1+1;
834 : pazsan 1.18 :
835 :     dup 1+ swap c@ ;
836 : anton 1.1
837 : anton 1.42 (bye) n -- gforth paren_bye
838 : pazsan 1.15 return (Label *)n;
839 : anton 1.1
840 : anton 1.42 system c_addr u -- n gforth
841 : anton 1.39 n=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
842 : anton 1.1
843 : anton 1.42 getenv c_addr1 u1 -- c_addr2 u2 gforth
844 : anton 1.17 c_addr2 = getenv(cstr(c_addr1,u1,1));
845 : pazsan 1.40 u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
846 : anton 1.16
847 : anton 1.1 popen c_addr u n -- wfileid own
848 : anton 1.39 static char* mode[2]={"r","w"}; /* !! should we use FAM here? */
849 :     wfileid=(Cell)popen(cstr(c_addr,u,1),mode[n]); /* ~ expansion of 1st arg? */
850 : anton 1.1
851 : anton 1.39 pclose wfileid -- wior own
852 : anton 1.36 wior=pclose((FILE *)wfileid); /* !! what to do with the result */
853 : pazsan 1.2
854 : pazsan 1.21 time&date -- nsec nmin nhour nday nmonth nyear facility-ext time_and_date
855 : pazsan 1.2 struct timeval time1;
856 :     struct timezone zone1;
857 :     struct tm *ltime;
858 :     gettimeofday(&time1,&zone1);
859 : pazsan 1.40 ltime=localtime((time_t *)&time1.tv_sec);
860 : pazsan 1.2 nyear =ltime->tm_year+1900;
861 : pazsan 1.21 nmonth=ltime->tm_mon+1;
862 : pazsan 1.2 nday =ltime->tm_mday;
863 :     nhour =ltime->tm_hour;
864 :     nmin =ltime->tm_min;
865 :     nsec =ltime->tm_sec;
866 :    
867 : anton 1.16 ms n -- facility-ext
868 : pazsan 1.2 struct timeval timeout;
869 :     timeout.tv_sec=n/1000;
870 :     timeout.tv_usec=1000*(n%1000);
871 :     (void)select(0,0,0,0,&timeout);
872 : anton 1.1
873 :     allocate u -- a_addr wior memory
874 :     a_addr = (Cell *)malloc(u);
875 : anton 1.36 wior = IOR(a_addr==NULL);
876 : anton 1.1
877 :     free a_addr -- wior memory
878 :     free(a_addr);
879 :     wior = 0;
880 :    
881 :     resize a_addr1 u -- a_addr2 wior memory
882 : anton 1.36 ""Change the size of the allocated area at @i{a_addr1} to @i{u}
883 :     address units, possibly moving the contents to a different
884 :     area. @i{a_addr2} is the address of the resulting area. If
885 :     @code{a_addr2} is 0, gforth's (but not the standard) @code{resize}
886 :     @code{allocate}s @i{u} address units.""
887 :     /* the following check is not necessary on most OSs, but it is needed
888 :     on SunOS 4.1.2. */
889 :     if (a_addr1==NULL)
890 :     a_addr2 = (Cell *)malloc(u);
891 :     else
892 :     a_addr2 = (Cell *)realloc(a_addr1, u);
893 :     wior = IOR(a_addr2==NULL); /* !! Define a return code */
894 : anton 1.1
895 :     (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
896 :     for (; f83name1 != NULL; f83name1 = f83name1->next)
897 : pazsan 1.8 if (F83NAME_COUNT(f83name1)==u &&
898 : pazsan 1.13 strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
899 : pazsan 1.8 break;
900 :     f83name2=f83name1;
901 : pazsan 1.18 :
902 :     BEGIN dup WHILE
903 :     >r dup r@ cell+ c@ $1F and =
904 :     IF 2dup r@ cell+ char+ capscomp 0=
905 :     IF 2drop r> EXIT THEN THEN
906 :     r> @
907 :     REPEAT nip nip ;
908 : pazsan 1.8
909 : pazsan 1.13 (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
910 :     F83Name *f83name1;
911 :     f83name2=NULL;
912 :     while(a_addr != NULL)
913 :     {
914 :     f83name1=(F83Name *)(a_addr[1]);
915 :     a_addr=(Cell *)(a_addr[0]);
916 :     if (F83NAME_COUNT(f83name1)==u &&
917 :     strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
918 :     {
919 :     f83name2=f83name1;
920 :     break;
921 :     }
922 :     }
923 : pazsan 1.18 :
924 :     BEGIN dup WHILE
925 :     2@ >r >r dup r@ cell+ c@ $1F and =
926 :     IF 2dup r@ cell+ char+ capscomp 0=
927 :     IF 2drop r> rdrop EXIT THEN THEN
928 :     rdrop r>
929 :     REPEAT nip nip ;
930 : pazsan 1.13
931 : anton 1.14 (hashkey) c_addr u1 -- u2 new paren_hashkey
932 : pazsan 1.13 u2=0;
933 :     while(u1--)
934 : pazsan 1.30 u2+=(Cell)toupper(*c_addr++);
935 : pazsan 1.18 :
936 :     0 -rot bounds ?DO I c@ toupper + LOOP ;
937 : anton 1.14
938 :     (hashkey1) c_addr u ubits -- ukey new paren_hashkey1
939 :     ""ukey is the hash key for the string c_addr u fitting in ubits bits""
940 :     /* this hash function rotates the key at every step by rot bits within
941 :     ubits bits and xors it with the character. This function does ok in
942 :     the chi-sqare-test. Rot should be <=7 (preferably <=5) for
943 :     ASCII strings (larger if ubits is large), and should share no
944 :     divisors with ubits.
945 :     */
946 :     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];
947 :     Char *cp = c_addr;
948 :     for (ukey=0; cp<c_addr+u; cp++)
949 :     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
950 :     ^ toupper(*cp))
951 :     & ((1<<ubits)-1));
952 : pazsan 1.18 :
953 :     dup rot-values + c@ over 1 swap lshift 1- >r
954 :     tuck - 2swap r> 0 2swap bounds
955 :     ?DO dup 4 pick lshift swap 3 pick rshift or
956 :     I c@ toupper xor
957 :     over and LOOP
958 :     nip nip nip ;
959 :     Create rot-values
960 :     5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
961 :     3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
962 :     7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
963 :     7 c, 5 c, 5 c,
964 : anton 1.1
965 :     (parse-white) c_addr1 u1 -- c_addr2 u2 new paren_parse_white
966 :     /* use !isgraph instead of isspace? */
967 :     Char *endp = c_addr1+u1;
968 :     while (c_addr1<endp && isspace(*c_addr1))
969 :     c_addr1++;
970 :     if (c_addr1<endp) {
971 :     for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
972 :     ;
973 :     u2 = c_addr1-c_addr2;
974 :     }
975 :     else {
976 :     c_addr2 = c_addr1;
977 :     u2 = 0;
978 :     }
979 : pazsan 1.18 :
980 :     BEGIN dup WHILE over c@ bl <= WHILE 1 /string
981 :     REPEAT THEN 2dup
982 :     BEGIN dup WHILE over c@ bl > WHILE 1 /string
983 :     REPEAT THEN nip - ;
984 : anton 1.1
985 : anton 1.36 close-file wfileid -- wior file close_file
986 :     wior = IOR(fclose((FILE *)wfileid)==EOF);
987 : anton 1.1
988 :     open-file c_addr u ntype -- w2 wior file open_file
989 : anton 1.39 w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
990 : pazsan 1.40 wior = IOR(w2 == 0);
991 : anton 1.1
992 :     create-file c_addr u ntype -- w2 wior file create_file
993 : pazsan 1.33 Cell fd;
994 : anton 1.39 fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_RDWR|O_TRUNC, 0666);
995 : anton 1.36 if (fd != -1) {
996 : anton 1.1 w2 = (Cell)fdopen(fd, fileattr[ntype]);
997 : pazsan 1.40 wior = IOR(w2 == 0);
998 : anton 1.1 } else {
999 :     w2 = 0;
1000 : anton 1.36 wior = IOR(1);
1001 : anton 1.1 }
1002 :    
1003 :     delete-file c_addr u -- wior file delete_file
1004 : anton 1.39 wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1005 : anton 1.1
1006 :     rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
1007 : anton 1.39 char *s1=tilde_cstr(c_addr2, u2, 1);
1008 :     wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1009 : anton 1.1
1010 :     file-position wfileid -- ud wior file file_position
1011 :     /* !! use tell and lseek? */
1012 :     ud = ftell((FILE *)wfileid);
1013 : anton 1.36 wior = IOR(ud==-1);
1014 : anton 1.1
1015 :     reposition-file ud wfileid -- wior file reposition_file
1016 : anton 1.36 wior = IOR(fseek((FILE *)wfileid, (long)ud, SEEK_SET)==-1);
1017 : anton 1.1
1018 :     file-size wfileid -- ud wior file file_size
1019 :     struct stat buf;
1020 : anton 1.36 wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1021 : anton 1.1 ud = buf.st_size;
1022 :    
1023 :     resize-file ud wfileid -- wior file resize_file
1024 : anton 1.36 wior = IOR(ftruncate(fileno((FILE *)wfileid), (Cell)ud)==-1);
1025 : anton 1.1
1026 :     read-file c_addr u1 wfileid -- u2 wior file read_file
1027 :     /* !! fread does not guarantee enough */
1028 :     u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1029 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1030 : anton 1.36 /* !! is the value of ferror errno-compatible? */
1031 :     if (wior)
1032 :     clearerr((FILE *)wfileid);
1033 : anton 1.1
1034 :     read-line c_addr u1 wfileid -- u2 flag wior file read_line
1035 : pazsan 1.13 /*
1036 :     Cell c;
1037 :     flag=-1;
1038 :     for(u2=0; u2<u1; u2++)
1039 :     {
1040 :     *c_addr++ = (Char)(c = getc((FILE *)wfileid));
1041 :     if(c=='\n') break;
1042 :     if(c==EOF)
1043 :     {
1044 :     flag=FLAG(u2!=0);
1045 :     break;
1046 :     }
1047 :     }
1048 :     wior=FILEIO(ferror((FILE *)wfileid));
1049 :     */
1050 :     if ((flag=FLAG(!feof((FILE *)wfileid) &&
1051 :     fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1052 : anton 1.36 wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
1053 :     if (wior)
1054 :     clearerr((FILE *)wfileid);
1055 : pazsan 1.13 u2 = strlen(c_addr);
1056 : anton 1.11 u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1057 :     }
1058 :     else {
1059 :     wior=0;
1060 :     u2=0;
1061 :     }
1062 : anton 1.1
1063 :     write-file c_addr u1 wfileid -- wior file write_file
1064 :     /* !! fwrite does not guarantee enough */
1065 :     {
1066 : pazsan 1.31 Cell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1067 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1068 : anton 1.36 if (wior)
1069 :     clearerr((FILE *)wfileid);
1070 : anton 1.1 }
1071 :    
1072 :     flush-file wfileid -- wior file-ext flush_file
1073 : anton 1.36 wior = IOR(fflush((FILE *) wfileid)==EOF);
1074 : anton 1.1
1075 : anton 1.38 file-status c_addr u -- ntype wior file-ext file_status
1076 : anton 1.39 char *filename=tilde_cstr(c_addr, u, 1);
1077 : anton 1.38 if (access (filename, F_OK) != 0) {
1078 :     ntype=0;
1079 :     wior=IOR(1);
1080 :     }
1081 :     else if (access (filename, R_OK | W_OK) == 0) {
1082 :     ntype=2; /* r/w */
1083 :     wior=0;
1084 :     }
1085 :     else if (access (filename, R_OK) == 0) {
1086 :     ntype=0; /* r/o */
1087 :     wior=0;
1088 :     }
1089 :     else if (access (filename, W_OK) == 0) {
1090 :     ntype=4; /* w/o */
1091 :     wior=0;
1092 :     }
1093 :     else {
1094 :     ntype=1; /* well, we cannot access the file, but better deliver a legal
1095 :     access mode (r/o bin), so we get a decent error later upon open. */
1096 :     wior=0;
1097 :     }
1098 :    
1099 : anton 1.1 comparisons(f, r1 r2, f_, r1, r2, new, new, float, new)
1100 :     comparisons(f0, r, f_zero_, r, 0., float, new, float, new)
1101 :    
1102 :     d>f d -- r float d_to_f
1103 :     r = d;
1104 :    
1105 :     f>d r -- d float f_to_d
1106 :     /* !! basis 15 is not very specific */
1107 :     d = r;
1108 :    
1109 :     f! r f_addr -- float f_store
1110 :     *f_addr = r;
1111 :    
1112 :     f@ f_addr -- r float f_fetch
1113 :     r = *f_addr;
1114 :    
1115 :     df@ df_addr -- r float-ext d_f_fetch
1116 :     #ifdef IEEE_FP
1117 :     r = *df_addr;
1118 :     #else
1119 :     !! df@
1120 :     #endif
1121 :    
1122 :     df! r df_addr -- float-ext d_f_store
1123 :     #ifdef IEEE_FP
1124 :     *df_addr = r;
1125 :     #else
1126 :     !! df!
1127 :     #endif
1128 :    
1129 :     sf@ sf_addr -- r float-ext s_f_fetch
1130 :     #ifdef IEEE_FP
1131 :     r = *sf_addr;
1132 :     #else
1133 :     !! sf@
1134 :     #endif
1135 :    
1136 :     sf! r sf_addr -- float-ext s_f_store
1137 :     #ifdef IEEE_FP
1138 :     *sf_addr = r;
1139 :     #else
1140 :     !! sf!
1141 :     #endif
1142 :    
1143 :     f+ r1 r2 -- r3 float f_plus
1144 :     r3 = r1+r2;
1145 :    
1146 :     f- r1 r2 -- r3 float f_minus
1147 :     r3 = r1-r2;
1148 :    
1149 :     f* r1 r2 -- r3 float f_star
1150 :     r3 = r1*r2;
1151 :    
1152 :     f/ r1 r2 -- r3 float f_slash
1153 :     r3 = r1/r2;
1154 :    
1155 :     f** r1 r2 -- r3 float-ext f_star_star
1156 : anton 1.28 ""@i{r3} is @i{r1} raised to the @i{r2}th power""
1157 : anton 1.1 r3 = pow(r1,r2);
1158 :    
1159 :     fnegate r1 -- r2 float
1160 :     r2 = - r1;
1161 :    
1162 :     fdrop r -- float
1163 :    
1164 :     fdup r -- r r float
1165 :    
1166 :     fswap r1 r2 -- r2 r1 float
1167 :    
1168 :     fover r1 r2 -- r1 r2 r1 float
1169 :    
1170 :     frot r1 r2 r3 -- r2 r3 r1 float
1171 :    
1172 : anton 1.42 fnip r1 r2 -- r2 gforth
1173 :    
1174 :     ftuck r1 r2 -- r2 r1 r2 gforth
1175 :    
1176 : anton 1.1 float+ f_addr1 -- f_addr2 float float_plus
1177 :     f_addr2 = f_addr1+1;
1178 :    
1179 :     floats n1 -- n2 float
1180 :     n2 = n1*sizeof(Float);
1181 :    
1182 :     floor r1 -- r2 float
1183 : anton 1.28 ""round towards the next smaller integral value, i.e., round toward negative infinity""
1184 : anton 1.1 /* !! unclear wording */
1185 :     r2 = floor(r1);
1186 :    
1187 :     fround r1 -- r2 float
1188 : anton 1.28 ""round to the nearest integral value""
1189 : anton 1.1 /* !! unclear wording */
1190 : anton 1.26 #ifdef HAVE_RINT
1191 : anton 1.1 r2 = rint(r1);
1192 : anton 1.26 #else
1193 :     r2 = floor(r1+0.5);
1194 :     /* !! This is not quite true to the rounding rules given in the standard */
1195 :     #endif
1196 : anton 1.1
1197 :     fmax r1 r2 -- r3 float
1198 :     if (r1<r2)
1199 :     r3 = r2;
1200 :     else
1201 :     r3 = r1;
1202 :    
1203 :     fmin r1 r2 -- r3 float
1204 :     if (r1<r2)
1205 :     r3 = r1;
1206 :     else
1207 :     r3 = r2;
1208 :    
1209 :     represent r c_addr u -- n f1 f2 float
1210 :     char *sig;
1211 : pazsan 1.33 Cell flag;
1212 :     Cell decpt;
1213 : pazsan 1.40 sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
1214 : pazsan 1.33 n=(r==0 ? 1 : decpt);
1215 : anton 1.1 f1=FLAG(flag!=0);
1216 :     f2=FLAG(isdigit(sig[0])!=0);
1217 :     memmove(c_addr,sig,u);
1218 :    
1219 :     >float c_addr u -- flag float to_float
1220 :     /* real signature: c_addr u -- r t / f */
1221 :     Float r;
1222 : anton 1.17 char *number=cstr(c_addr, u, 1);
1223 : anton 1.1 char *endconv;
1224 : pazsan 1.32 while(isspace(number[--u]) && u>0);
1225 :     switch(number[u])
1226 : pazsan 1.23 {
1227 : pazsan 1.32 case 'd':
1228 :     case 'D':
1229 :     case 'e':
1230 :     case 'E': break;
1231 :     default : u++; break;
1232 : pazsan 1.23 }
1233 :     number[u]='\0';
1234 : anton 1.1 r=strtod(number,&endconv);
1235 : pazsan 1.30 if((flag=FLAG(!(Cell)*endconv)))
1236 : anton 1.1 {
1237 : pazsan 1.32 IF_FTOS(fp[0] = FTOS);
1238 :     fp += -1;
1239 :     FTOS = r;
1240 :     }
1241 :     else if(*endconv=='d' || *endconv=='D')
1242 :     {
1243 :     *endconv='E';
1244 :     r=strtod(number,&endconv);
1245 :     if((flag=FLAG(!(Cell)*endconv)))
1246 :     {
1247 : anton 1.1 IF_FTOS(fp[0] = FTOS);
1248 :     fp += -1;
1249 :     FTOS = r;
1250 : pazsan 1.32 }
1251 : anton 1.1 }
1252 :    
1253 :     fabs r1 -- r2 float-ext
1254 :     r2 = fabs(r1);
1255 :    
1256 :     facos r1 -- r2 float-ext
1257 :     r2 = acos(r1);
1258 :    
1259 :     fasin r1 -- r2 float-ext
1260 :     r2 = asin(r1);
1261 :    
1262 :     fatan r1 -- r2 float-ext
1263 :     r2 = atan(r1);
1264 :    
1265 :     fatan2 r1 r2 -- r3 float-ext
1266 : anton 1.28 ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
1267 :     intends this to be the inverse of @code{fsincos}. In gforth it is.""
1268 : anton 1.1 r3 = atan2(r1,r2);
1269 :    
1270 :     fcos r1 -- r2 float-ext
1271 :     r2 = cos(r1);
1272 :    
1273 :     fexp r1 -- r2 float-ext
1274 :     r2 = exp(r1);
1275 :    
1276 : pazsan 1.3 fexpm1 r1 -- r2 float-ext
1277 : anton 1.28 ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1278 : anton 1.27 #ifdef HAVE_EXPM1
1279 : anton 1.29 extern double expm1(double);
1280 :     r2 = expm1(r1);
1281 : pazsan 1.3 #else
1282 : anton 1.29 r2 = exp(r1)-1.;
1283 : pazsan 1.3 #endif
1284 :    
1285 : anton 1.1 fln r1 -- r2 float-ext
1286 :     r2 = log(r1);
1287 :    
1288 : pazsan 1.3 flnp1 r1 -- r2 float-ext
1289 : anton 1.28 ""@i{r2}=ln(@i{r1}+1)""
1290 : anton 1.27 #ifdef HAVE_LOG1P
1291 : anton 1.29 extern double log1p(double);
1292 :     r2 = log1p(r1);
1293 : pazsan 1.3 #else
1294 : anton 1.29 r2 = log(r1+1.);
1295 : pazsan 1.3 #endif
1296 :    
1297 : anton 1.1 flog r1 -- r2 float-ext
1298 : anton 1.28 ""the decimal logarithm""
1299 : anton 1.1 r2 = log10(r1);
1300 :    
1301 : anton 1.29 falog r1 -- r2 float-ext
1302 :     ""@i{r2}=10**@i{r1}""
1303 :     extern double pow10(double);
1304 :     r2 = pow10(r1);
1305 :    
1306 : pazsan 1.3 fsin r1 -- r2 float-ext
1307 :     r2 = sin(r1);
1308 :    
1309 :     fsincos r1 -- r2 r3 float-ext
1310 : anton 1.29 ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1311 : anton 1.1 r2 = sin(r1);
1312 :     r3 = cos(r1);
1313 :    
1314 :     fsqrt r1 -- r2 float-ext
1315 :     r2 = sqrt(r1);
1316 :    
1317 :     ftan r1 -- r2 float-ext
1318 :     r2 = tan(r1);
1319 : pazsan 1.32 :
1320 :     fsincos f/ ;
1321 : anton 1.29
1322 :     fsinh r1 -- r2 float-ext
1323 :     r2 = sinh(r1);
1324 : pazsan 1.32 :
1325 :     fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1326 : anton 1.29
1327 :     fcosh r1 -- r2 float-ext
1328 :     r2 = cosh(r1);
1329 : pazsan 1.32 :
1330 :     fexp fdup 1/f f+ f2/ ;
1331 : anton 1.29
1332 :     ftanh r1 -- r2 float-ext
1333 :     r2 = tanh(r1);
1334 : pazsan 1.32 :
1335 :     f2* fexpm1 fdup 2. d>f f+ f/ ;
1336 : anton 1.29
1337 :     fasinh r1 -- r2 float-ext
1338 :     r2 = asinh(r1);
1339 : pazsan 1.32 :
1340 :     fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
1341 : anton 1.29
1342 :     facosh r1 -- r2 float-ext
1343 :     r2 = acosh(r1);
1344 : pazsan 1.32 :
1345 :     fdup fdup f* 1. d>f f- fsqrt f+ fln ;
1346 : anton 1.29
1347 :     fatanh r1 -- r2 float-ext
1348 :     r2 = atanh(r1);
1349 : pazsan 1.32 :
1350 :     fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
1351 :     r> IF fnegate THEN ;
1352 : anton 1.1
1353 : anton 1.6 \ The following words access machine/OS/installation-dependent ANSI
1354 :     \ figForth internals
1355 :     \ !! how about environmental queries DIRECT-THREADED,
1356 :     \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
1357 : anton 1.1
1358 :     >body xt -- a_addr core to_body
1359 :     a_addr = PFA(xt);
1360 :    
1361 :     >code-address xt -- c_addr new to_code_address
1362 :     ""c_addr is the code address of the word xt""
1363 :     /* !! This behaves installation-dependently for DOES-words */
1364 :     c_addr = CODE_ADDRESS(xt);
1365 :    
1366 :     >does-code xt -- a_addr new to_does_code
1367 :     ""If xt ist the execution token of a defining-word-defined word,
1368 :     a_addr is the start of the Forth code after the DOES>; Otherwise the
1369 : anton 1.28 behaviour is undefined""
1370 : anton 1.1 /* !! there is currently no way to determine whether a word is
1371 :     defining-word-defined */
1372 : anton 1.20 a_addr = (Cell *)DOES_CODE(xt);
1373 : anton 1.1
1374 : anton 1.41 code-address! c_addr xt -- new code_address_store
1375 : anton 1.1 ""Creates a code field with code address c_addr at xt""
1376 : anton 1.41 MAKE_CF(xt, c_addr);
1377 : pazsan 1.5 CACHE_FLUSH(xt,PFA(0));
1378 : anton 1.1
1379 :     does-code! a_addr xt -- new does_code_store
1380 :     ""creates a code field at xt for a defining-word-defined word; a_addr
1381 :     is the start of the Forth code after DOES>""
1382 :     MAKE_DOES_CF(xt, a_addr);
1383 : pazsan 1.5 CACHE_FLUSH(xt,PFA(0));
1384 : anton 1.1
1385 : anton 1.41 does-handler! a_addr -- new does_handler_store
1386 : anton 1.1 ""creates a DOES>-handler at address a_addr. a_addr usually points
1387 :     just behind a DOES>.""
1388 :     MAKE_DOES_HANDLER(a_addr);
1389 : pazsan 1.5 CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1390 : anton 1.1
1391 :     /does-handler -- n new slash_does_handler
1392 :     ""the size of a does-handler (includes possible padding)""
1393 :     /* !! a constant or environmental query might be better */
1394 :     n = DOES_HANDLER_SIZE;
1395 : anton 1.41
1396 :     flush-icache c_addr u -- gforth flush_icache
1397 :     ""Make sure that the instruction cache of the processor (if there is
1398 :     one) does not contain stale data at @var{c_addr} and @var{u} bytes
1399 :     afterwards. @code{END-CODE} performs a @code{flush-icache}
1400 :     automatically. Caveat: @code{flush-icache} might not work on your
1401 :     installation; this is usually the case if direct threading is not
1402 :     supported on your machine (take a look at your @file{machine.h}) and
1403 :     your machine has a separate instruction cache. In such cases,
1404 :     @code{flush-icache} does nothing instead of flushing the instruction
1405 :     cache.""
1406 :     FLUSH_ICACHE(c_addr,u);
1407 : anton 1.1
1408 :     toupper c1 -- c2 new
1409 :     c2 = toupper(c1);
1410 :    
1411 : anton 1.6 \ local variable implementation primitives
1412 : anton 1.1 @local# -- w new fetch_local_number
1413 : anton 1.35 w = *(Cell *)(lp+(Cell)NEXT_INST);
1414 :     INC_IP(1);
1415 : anton 1.1
1416 : anton 1.9 @local0 -- w new fetch_local_zero
1417 : pazsan 1.18 w = *(Cell *)(lp+0*sizeof(Cell));
1418 : anton 1.9
1419 : pazsan 1.18 @local1 -- w new fetch_local_four
1420 :     w = *(Cell *)(lp+1*sizeof(Cell));
1421 : anton 1.9
1422 : pazsan 1.18 @local2 -- w new fetch_local_eight
1423 :     w = *(Cell *)(lp+2*sizeof(Cell));
1424 : anton 1.9
1425 : pazsan 1.18 @local3 -- w new fetch_local_twelve
1426 :     w = *(Cell *)(lp+3*sizeof(Cell));
1427 : anton 1.9
1428 : anton 1.1 f@local# -- r new f_fetch_local_number
1429 : anton 1.35 r = *(Float *)(lp+(Cell)NEXT_INST);
1430 :     INC_IP(1);
1431 : anton 1.1
1432 : anton 1.9 f@local0 -- r new f_fetch_local_zero
1433 : pazsan 1.18 r = *(Float *)(lp+0*sizeof(Float));
1434 : anton 1.9
1435 : pazsan 1.18 f@local1 -- r new f_fetch_local_eight
1436 :     r = *(Float *)(lp+1*sizeof(Float));
1437 : anton 1.9
1438 : anton 1.1 laddr# -- c_addr new laddr_number
1439 :     /* this can also be used to implement lp@ */
1440 : anton 1.35 c_addr = (Char *)(lp+(Cell)NEXT_INST);
1441 :     INC_IP(1);
1442 : anton 1.1
1443 :     lp+!# -- new lp_plus_store_number
1444 :     ""used with negative immediate values it allocates memory on the
1445 :     local stack, a positive immediate argument drops memory from the local
1446 :     stack""
1447 : anton 1.35 lp += (Cell)NEXT_INST;
1448 :     INC_IP(1);
1449 : anton 1.9
1450 : pazsan 1.18 lp- -- new minus_four_lp_plus_store
1451 :     lp += -sizeof(Cell);
1452 : anton 1.9
1453 : pazsan 1.18 lp+ -- new eight_lp_plus_store
1454 :     lp += sizeof(Float);
1455 : anton 1.9
1456 : pazsan 1.18 lp+2 -- new sixteen_lp_plus_store
1457 :     lp += 2*sizeof(Float);
1458 : anton 1.1
1459 :     lp! c_addr -- new lp_store
1460 :     lp = (Address)c_addr;
1461 :    
1462 :     >l w -- new to_l
1463 :     lp -= sizeof(Cell);
1464 :     *(Cell *)lp = w;
1465 :    
1466 :     f>l r -- new f_to_l
1467 :     lp -= sizeof(Float);
1468 :     *(Float *)lp = r;
1469 : pazsan 1.4
1470 :     up! a_addr -- new up_store
1471 : pazsan 1.18 up0=up=(char *)a_addr;
1472 : anton 1.36
1473 :     call-c w -- new call_c
1474 :     ""Call the C function pointed to by @i{w}. The C function has to
1475 :     access the stack itself. The stack pointers are exported in the gloabl
1476 :     variables @code{SP} and @code{FP}.""
1477 :     /* This is a first attempt at support for calls to C. This may change in
1478 :     the future */
1479 :     IF_FTOS(fp[0]=FTOS);
1480 :     FP=fp;
1481 :     SP=sp;
1482 :     ((void (*)())w)();
1483 :     sp=SP;
1484 :     fp=FP;
1485 :     IF_TOS(TOS=sp[0]);
1486 :     IF_FTOS(FTOS=fp[0]);
1487 :    
1488 :     strerror n -- c_addr u new
1489 :     c_addr = strerror(n);
1490 : anton 1.42 u = strlen(c_addr);
1491 :    
1492 :     strsignal n -- c_addr u new
1493 :     c_addr = strsignal(n);
1494 : anton 1.36 u = strlen(c_addr);

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help