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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help