[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 :     \ This file contains instructions in the following format:
7 :     \
8 :     \ forth name stack effect category [pronounciation]
9 :     \ [""glossary entry""]
10 :     \ C code
11 :     \ [:
12 :     \ Forth code]
13 :     \
14 :     \ The pronounciataion is also used for forming C names.
15 :     \
16 :     \ These informations are automagically translated into C-code for the
17 :     \ interpreter and into some other files. The forth name of a word is
18 :     \ automatically turned into upper case. I hope that your C compiler has
19 :     \ decent optimization, otherwise the automatically generated code will
20 :     \ be somewhat slow. The Forth version of the code is included for manual
21 :     \ compilers, so they will need to compile only the important words.
22 :     \
23 :     \ Note that stack pointer adjustment is performed according to stack
24 :     \ effect by automatically generated code and NEXT is automatically
25 :     \ appended to the C code. Also, you can use the names in the stack
26 :     \ effect in the C code. Stack access is automatic. One exception: if
27 :     \ your code does not fall through, the results are not stored into the
28 :     \ stack. Use different names on both sides of the '--', if you change a
29 :     \ value (some stores to the stack are optimized away).
30 :     \
31 :     \ The stack variables have the following types:
32 :     \ name matches type
33 :     \ f.* Bool
34 :     \ c.* Char
35 :     \ [nw].* Cell
36 :     \ u.* UCell
37 :     \ d.* DCell
38 :     \ ud.* UDCell
39 :     \ r.* Float
40 :     \ a_.* Cell *
41 :     \ c_.* Char *
42 :     \ f_.* Float *
43 :     \ df_.* DFloat *
44 :     \ sf_.* SFloat *
45 :     \ xt.* XT
46 :     \ wid.* WID
47 :     \ f83name.* F83Name *
48 :     \
49 :     \ In addition the following names can be used:
50 :     \ ip the instruction pointer
51 :     \ sp the data stack pointer
52 :     \ rp the parameter stack pointer
53 :     \ NEXT executes NEXT
54 :     \ cfa
55 :     \ NEXT1 executes NEXT1
56 :     \ FLAG(x) makes a Forth flag from a C flag
57 :     \
58 :     \ Percentages in comments are from Koopmans book: average/maximum use
59 :     \ (taken from four, not very representattive benchmarks)
60 :     \
61 :     \ To do:
62 :     \ make sensible error returns for file words
63 :     \
64 :     \ throw execute, cfa and NEXT1 out?
65 :     \ macroize *ip, ip++, *ip++ (pipelining)?
66 : anton 1.1
67 : anton 1.6 \ these m4 macros would collide with identifiers
68 : anton 1.1 undefine(`index')
69 :     undefine(`shift')
70 :    
71 :     noop -- fig
72 :     ;
73 :    
74 :     lit -- w fig
75 :     w = (Cell)*ip++;
76 :    
77 :     execute xt -- core,fig
78 :     cfa = xt;
79 :     IF_TOS(TOS = sp[0]);
80 :     NEXT1;
81 :    
82 : anton 1.9 branch-lp+!# -- new branch_lp_plus_store_number
83 :     /* this will probably not be used */
84 :     branch_adjust_lp:
85 :     lp += (int)(ip[1]);
86 :     goto branch;
87 :    
88 : anton 1.1 branch -- fig
89 :     branch:
90 :     ip = (Xt *)(((int)ip)+(int)*ip);
91 :    
92 : anton 1.9 \ condbranch(forthname,restline,code)
93 :     \ this is non-syntactical: code must open a brace that is close by the macro
94 :     define(condbranch,
95 :     $1 $2
96 :     $3 goto branch;
97 :     }
98 :     else
99 :     ip++;
100 :    
101 :     $1-lp+!# $2_lp_plus_store_number
102 :     $3 goto branch_adjust_lp;
103 :     }
104 :     else
105 :     ip+=2;
106 :    
107 :     )
108 :    
109 :     condbranch(?branch,f -- f83 question_branch,
110 : anton 1.1 if (f==0) {
111 :     IF_TOS(TOS = sp[0]);
112 : anton 1.9 )
113 : anton 1.1
114 : anton 1.9 condbranch((next),-- cmFORTH paren_next,
115 : anton 1.1 if ((*rp)--) {
116 : anton 1.9 )
117 : anton 1.1
118 : anton 1.9 condbranch((loop),-- fig paren_loop,
119 : anton 1.1 int index = *rp+1;
120 :     int limit = rp[1];
121 :     if (index != limit) {
122 :     *rp = index;
123 : anton 1.9 )
124 : anton 1.1
125 : anton 1.9 condbranch((+loop),n -- fig paren_plus_loop,
126 : anton 1.1 /* !! check this thoroughly */
127 :     int index = *rp;
128 :     /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
129 :     /* dependent upon two's complement arithmetic */
130 : pazsan 1.15 int olddiff = index-rp[1];
131 :     #ifndef undefined
132 : anton 1.9 if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
133 :     || (olddiff^n)>=0 /* it is a wrap-around effect */) {
134 : pazsan 1.15 #else
135 :     #ifndef MAXINT
136 :     #define MAXINT ((1<<(8*sizeof(Cell)-1))-1)
137 :     #endif
138 :     if(((olddiff^MAXINT) >= n) ? ((olddiff+n) >= 0) : ((olddiff+n) < 0)) {
139 :     #endif
140 :     #ifdef i386
141 :     *rp += n;
142 :     #else
143 :     *rp = index + n;
144 :     #endif
145 : anton 1.1 IF_TOS(TOS = sp[0]);
146 : anton 1.9 )
147 : anton 1.1
148 : anton 1.9 condbranch((s+loop),n -- new paren_symmetric_plus_loop,
149 : anton 1.1 ""The run-time procedure compiled by S+LOOP. It loops until the index
150 :     crosses the boundary between limit and limit-sign(n). I.e. a symmetric
151 :     version of (+LOOP).""
152 :     /* !! check this thoroughly */
153 : pazsan 1.15 int index = *rp;
154 :     int diff = index-rp[1];
155 : anton 1.1 int newdiff = diff+n;
156 :     if (n<0) {
157 :     diff = -diff;
158 : pazsan 1.15 newdiff = -newdiff;
159 : anton 1.1 }
160 :     if (diff>=0 || newdiff<0) {
161 : pazsan 1.15 #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 :     unloop -- core
170 :     rp += 2;
171 :    
172 :     (for) ncount -- cmFORTH paren_for
173 :     /* or (for) = >r -- collides with unloop! */
174 :     *--rp = 0;
175 :     *--rp = ncount;
176 :    
177 :     (do) nlimit nstart -- fig paren_do
178 :     /* or do it in high-level? 0.09/0.23% */
179 :     *--rp = nlimit;
180 :     *--rp = nstart;
181 :     :
182 : pazsan 1.13 r> -rot swap >r >r >r ;
183 : anton 1.1
184 :     (?do) nlimit nstart -- core-ext paren_question_do
185 :     *--rp = nlimit;
186 :     *--rp = nstart;
187 :     if (nstart == nlimit) {
188 :     IF_TOS(TOS = sp[0]);
189 :     goto branch;
190 :     }
191 :     else {
192 :     ip++;
193 :     }
194 :    
195 :     i -- n core,fig
196 :     n = *rp;
197 :    
198 :     j -- n core
199 :     n = rp[2];
200 :    
201 : anton 1.6 \ digit is high-level: 0/0%
202 : anton 1.1
203 : pazsan 1.10 (emit) c -- fig paren_emit
204 : anton 1.1 putchar(c);
205 :     emitcounter++;
206 : pazsan 1.10
207 :     (type) c_addr n -- fig paren_type
208 :     fwrite(c_addr,sizeof(Char),n,stdout);
209 :     emitcounter += n;
210 : anton 1.1
211 : pazsan 1.15 (key) -- n fig paren_key
212 : anton 1.1 fflush(stdout);
213 :     /* !! noecho */
214 :     n = key();
215 :    
216 : pazsan 1.2 key? -- n fig key_q
217 :     fflush(stdout);
218 :     n = key_query;
219 :    
220 : anton 1.1 cr -- fig
221 :     puts("");
222 :    
223 :     move c_from c_to ucount -- core
224 :     memmove(c_to,c_from,ucount);
225 : anton 1.6 /* make an Ifdef for bsd and others? */
226 : anton 1.1
227 :     cmove c_from c_to u -- string
228 :     while (u-- > 0)
229 :     *c_to++ = *c_from++;
230 :    
231 :     cmove> c_from c_to u -- string c_move_up
232 :     while (u-- > 0)
233 :     c_to[u] = c_from[u];
234 :    
235 :     fill c_addr u c -- core
236 :     memset(c_addr,c,u);
237 :    
238 :     compare c_addr1 u1 c_addr2 u2 -- n string
239 :     n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
240 :     if (n==0)
241 :     n = u1-u2;
242 :     if (n<0)
243 :     n = -1;
244 :     else if (n>0)
245 :     n = 1;
246 :    
247 :     -text c_addr1 u c_addr2 -- n new dash_text
248 :     n = memcmp(c_addr1, c_addr2, u);
249 :     if (n<0)
250 :     n = -1;
251 :     else if (n>0)
252 :     n = 1;
253 :    
254 :     capscomp c_addr1 u c_addr2 -- n new
255 :     Char c1, c2;
256 :     for (;; u--, c_addr1++, c_addr2++) {
257 :     if (u == 0) {
258 :     n = 0;
259 :     break;
260 :     }
261 :     c1 = toupper(*c_addr1);
262 :     c2 = toupper(*c_addr2);
263 :     if (c1 != c2) {
264 :     if (c1 < c2)
265 :     n = -1;
266 :     else
267 :     n = 1;
268 :     break;
269 :     }
270 :     }
271 :    
272 :     -trailing c_addr u1 -- c_addr u2 string dash_trailing
273 :     u2 = u1;
274 :     while (c_addr[u2-1] == ' ')
275 :     u2--;
276 :    
277 :     /string c_addr1 u1 n -- c_addr2 u2 string slash_string
278 :     c_addr2 = c_addr1+n;
279 :     u2 = u1-n;
280 :    
281 :     + n1 n2 -- n core,fig plus
282 :     n = n1+n2;
283 :    
284 :     - n1 n2 -- n core,fig minus
285 :     n = n1-n2;
286 :    
287 :     negate n1 -- n2 core,fig
288 :     /* use minus as alias */
289 :     n2 = -n1;
290 :    
291 :     1+ n1 -- n2 core one_plus
292 :     n2 = n1+1;
293 :    
294 :     1- n1 -- n2 core one_minus
295 :     n2 = n1-1;
296 :    
297 :     max n1 n2 -- n core
298 :     if (n1<n2)
299 :     n = n2;
300 :     else
301 :     n = n1;
302 :     :
303 :     2dup < if
304 :     swap drop
305 :     else
306 :     drop
307 :     endif ;
308 :    
309 :     min n1 n2 -- n core
310 :     if (n1<n2)
311 :     n = n1;
312 :     else
313 :     n = n2;
314 :    
315 :     abs n1 -- n2 core
316 :     if (n1<0)
317 :     n2 = -n1;
318 :     else
319 :     n2 = n1;
320 :    
321 :     * n1 n2 -- n core,fig star
322 :     n = n1*n2;
323 :    
324 :     / n1 n2 -- n core,fig slash
325 :     n = n1/n2;
326 :    
327 :     mod n1 n2 -- n core
328 :     n = n1%n2;
329 :    
330 :     /mod n1 n2 -- n3 n4 core slash_mod
331 :     n4 = n1/n2;
332 :     n3 = n1%n2; /* !! is this correct? look into C standard! */
333 :    
334 :     2* n1 -- n2 core two_star
335 :     n2 = 2*n1;
336 :    
337 :     2/ n1 -- n2 core two_slash
338 :     /* !! is this still correct? */
339 :     n2 = n1>>1;
340 :    
341 :     fm/mod d1 n1 -- n2 n3 core f_m_slash_mod
342 :     ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
343 :     /* assumes that the processor uses either floored or symmetric division */
344 :     n3 = d1/n1;
345 :     n2 = d1%n1;
346 :     /* note that this 1%-3>0 is optimized by the compiler */
347 :     if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
348 :     n3--;
349 :     n2+=n1;
350 :     }
351 :    
352 :     sm/rem d1 n1 -- n2 n3 core s_m_slash_rem
353 :     ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
354 :     /* assumes that the processor uses either floored or symmetric division */
355 :     n3 = d1/n1;
356 :     n2 = d1%n1;
357 :     /* note that this 1%-3<0 is optimized by the compiler */
358 :     if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
359 :     n3++;
360 :     n2-=n1;
361 :     }
362 :    
363 :     m* n1 n2 -- d core m_star
364 :     d = (DCell)n1 * (DCell)n2;
365 :    
366 :     um* u1 u2 -- ud core u_m_star
367 :     /* use u* as alias */
368 :     ud = (UDCell)u1 * (UDCell)u2;
369 :    
370 :     um/mod ud u1 -- u2 u3 core u_m_slash_mod
371 :     u3 = ud/u1;
372 :     u2 = ud%u1;
373 :    
374 :     m+ d1 n -- d2 double m_plus
375 :     d2 = d1+n;
376 :    
377 :     d+ d1 d2 -- d double,fig d_plus
378 :     d = d1+d2;
379 :    
380 :     d- d1 d2 -- d double d_minus
381 :     d = d1-d2;
382 :    
383 :     dnegate d1 -- d2 double
384 :     /* use dminus as alias */
385 :     d2 = -d1;
386 :    
387 :     dmax d1 d2 -- d double
388 :     if (d1<d2)
389 :     d = d2;
390 :     else
391 :     d = d1;
392 :    
393 :     dmin d1 d2 -- d double
394 :     if (d1<d2)
395 :     d = d1;
396 :     else
397 :     d = d2;
398 :    
399 :     dabs d1 -- d2 double
400 :     if (d1<0)
401 :     d2 = -d1;
402 :     else
403 :     d2 = d1;
404 :    
405 :     d2* d1 -- d2 double d_two_star
406 :     d2 = 2*d1;
407 :    
408 :     d2/ d1 -- d2 double d_two_slash
409 :     /* !! is this still correct? */
410 : pazsan 1.13 d2 = d1>>1;
411 : anton 1.1
412 :     d>s d -- n double d_to_s
413 :     /* make this an alias for drop? */
414 :     n = d;
415 :    
416 :     and w1 w2 -- w core,fig
417 :     w = w1&w2;
418 :    
419 :     or w1 w2 -- w core,fig
420 :     w = w1|w2;
421 :    
422 :     xor w1 w2 -- w core,fig
423 :     w = w1^w2;
424 :    
425 :     invert w1 -- w2 core
426 :     w2 = ~w1;
427 :    
428 :     rshift u1 n -- u2 core
429 :     u2 = u1>>n;
430 :    
431 :     lshift u1 n -- u2 core
432 :     u2 = u1<<n;
433 :    
434 : anton 1.6 \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
435 : anton 1.1 define(comparisons,
436 :     $1= $2 -- f $6 $3equals
437 :     f = FLAG($4==$5);
438 :    
439 :     $1<> $2 -- f $7 $3different
440 :     /* use != as alias ? */
441 :     f = FLAG($4!=$5);
442 :    
443 :     $1< $2 -- f $8 $3less
444 :     f = FLAG($4<$5);
445 :    
446 :     $1> $2 -- f $9 $3greater
447 :     f = FLAG($4>$5);
448 :    
449 :     $1<= $2 -- f new $3less_or_equal
450 :     f = FLAG($4<=$5);
451 :    
452 :     $1>= $2 -- f new $3greater_or_equal
453 :     f = FLAG($4>=$5);
454 :    
455 :     )
456 :    
457 :     comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
458 :     comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
459 :     comparisons(u, u1 u2, u_, u1, u2, new, new, core, core-ext)
460 :     comparisons(d, d1 d2, d_, d1, d2, double, new, double, new)
461 :     comparisons(d0, d, d_zero_, d, 0, double, new, double, new)
462 :     comparisons(du, ud1 ud2, d_u_, ud1, ud2, new, new, double-ext, new)
463 :    
464 :     within u1 u2 u3 -- f core-ext
465 :     f = FLAG(u1-u2 < u3-u2);
466 :    
467 :     sp@ -- a_addr fig spat
468 : pazsan 1.15 a_addr = sp+1;
469 : anton 1.1
470 :     sp! a_addr -- fig spstore
471 : pazsan 1.15 sp = a_addr;
472 : anton 1.1 /* works with and without TOS caching */
473 :    
474 :     rp@ -- a_addr fig rpat
475 :     a_addr = rp;
476 :    
477 :     rp! a_addr -- fig rpstore
478 :     rp = a_addr;
479 :    
480 :     fp@ -- f_addr new fp_fetch
481 :     f_addr = fp;
482 :    
483 :     fp! f_addr -- new fp_store
484 :     fp = f_addr;
485 :    
486 : pazsan 1.3 ;s -- core exit
487 : anton 1.1 ip = (Xt *)(*rp++);
488 :    
489 :     >r w -- core,fig to_r
490 :     *--rp = w;
491 :    
492 :     r> -- w core,fig r_from
493 :     w = *rp++;
494 :    
495 :     r@ -- w core,fig r_fetch
496 :     /* use r as alias */
497 :     /* make r@ an alias for i */
498 :     w = *rp;
499 :    
500 :     rdrop -- fig
501 :     rp++;
502 :    
503 :     i' -- w fig i_tick
504 :     w=rp[1];
505 :    
506 : anton 1.14 2>r w1 w2 -- core-ext two_to_r
507 :     *--rp = w1;
508 :     *--rp = w2;
509 :    
510 :     2r> -- w1 w2 core-ext two_r_from
511 :     w2 = *rp++;
512 :     w1 = *rp++;
513 :    
514 :     2r@ -- w1 w2 core-ext two_r_fetch
515 :     w2 = rp[0];
516 :     w1 = rp[1];
517 :    
518 :     2rdrop -- new two_r_drop
519 :     rp+=2;
520 :    
521 : anton 1.1 over w1 w2 -- w1 w2 w1 core,fig
522 :    
523 :     drop w -- core,fig
524 :    
525 :     swap w1 w2 -- w2 w1 core,fig
526 :    
527 :     dup w -- w w core,fig
528 :    
529 :     rot w1 w2 w3 -- w2 w3 w1 core rote
530 :    
531 :     -rot w1 w2 w3 -- w3 w1 w2 fig not_rote
532 :    
533 :     nip w1 w2 -- w2 core-ext
534 :    
535 :     tuck w1 w2 -- w2 w1 w2 core-ext
536 :    
537 :     ?dup w -- w core question_dupe
538 :     if (w!=0) {
539 : pazsan 1.7 IF_TOS(*sp-- = w;)
540 : anton 1.1 #ifndef USE_TOS
541 : pazsan 1.7 *--sp = w;
542 : anton 1.1 #endif
543 :     }
544 :    
545 :     pick u -- w core-ext
546 :     w = sp[u+1];
547 :    
548 :     2drop w1 w2 -- core two_drop
549 :    
550 :     2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
551 :    
552 :     2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
553 :    
554 :     2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
555 :    
556 :     2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double two_rote
557 :    
558 : anton 1.6 \ toggle is high-level: 0.11/0.42%
559 : anton 1.1
560 :     @ a_addr -- w fig fetch
561 :     w = *a_addr;
562 :    
563 :     ! w a_addr -- core,fig store
564 :     *a_addr = w;
565 :    
566 :     +! n a_addr -- core,fig plus_store
567 :     *a_addr += n;
568 :    
569 :     c@ c_addr -- c fig cfetch
570 :     c = *c_addr;
571 :    
572 :     c! c c_addr -- fig cstore
573 :     *c_addr = c;
574 :    
575 :     2! w1 w2 a_addr -- core two_store
576 :     a_addr[0] = w2;
577 :     a_addr[1] = w1;
578 :    
579 :     2@ a_addr -- w1 w2 core two_fetch
580 :     w2 = a_addr[0];
581 :     w1 = a_addr[1];
582 :    
583 :     d! d a_addr -- double d_store
584 :     /* !! alignment problems on some machines */
585 :     *(DCell *)a_addr = d;
586 :    
587 :     d@ a_addr -- d double d_fetch
588 :     d = *(DCell *)a_addr;
589 :    
590 :     cell+ a_addr1 -- a_addr2 core cell_plus
591 :     a_addr2 = a_addr1+1;
592 :    
593 :     cells n1 -- n2 core
594 :     n2 = n1 * sizeof(Cell);
595 :    
596 :     char+ c_addr1 -- c_addr2 core care_plus
597 :     c_addr2 = c_addr1+1;
598 :    
599 :     chars n1 -- n2 core cares
600 :     n2 = n1 * sizeof(Char);
601 :    
602 :     count c_addr1 -- c_addr2 u core
603 :     u = *c_addr1;
604 :     c_addr2 = c_addr1+1;
605 :    
606 :     (bye) n -- toolkit-ext paren_bye
607 :     deprep_terminal();
608 : pazsan 1.15 return (Label *)n;
609 : anton 1.1
610 :     system c_addr u -- n own
611 : anton 1.17 n=system(cstr(c_addr,u,1));
612 : anton 1.1
613 : anton 1.16 getenv c_addr1 u1 -- c_addr2 u2 new
614 : anton 1.17 c_addr2 = getenv(cstr(c_addr1,u1,1));
615 : anton 1.16 u2=strlen(c_addr2);
616 :    
617 : anton 1.1 popen c_addr u n -- wfileid own
618 :     static char* mode[2]={"r","w"};
619 : anton 1.17 wfileid=(Cell)popen(cstr(c_addr,u,1),mode[n]);
620 : anton 1.1
621 : anton 1.16 pclose wfileid -- wior own
622 : anton 1.1 wior=pclose((FILE *)wfileid);
623 : pazsan 1.2
624 : anton 1.16 time&date -- nyear nmonth nday nhour nmin nsec facility-ext time_and_date
625 : pazsan 1.2 struct timeval time1;
626 :     struct timezone zone1;
627 :     struct tm *ltime;
628 :     gettimeofday(&time1,&zone1);
629 :     ltime=localtime(&time1.tv_sec);
630 :     nyear =ltime->tm_year+1900;
631 :     nmonth=ltime->tm_mon;
632 :     nday =ltime->tm_mday;
633 :     nhour =ltime->tm_hour;
634 :     nmin =ltime->tm_min;
635 :     nsec =ltime->tm_sec;
636 :    
637 : anton 1.16 ms n -- facility-ext
638 : pazsan 1.2 struct timeval timeout;
639 :     timeout.tv_sec=n/1000;
640 :     timeout.tv_usec=1000*(n%1000);
641 :     (void)select(0,0,0,0,&timeout);
642 : anton 1.1
643 :     allocate u -- a_addr wior memory
644 :     a_addr = (Cell *)malloc(u);
645 : anton 1.6 wior = a_addr==NULL; /* !! Define a return code */
646 : anton 1.1
647 :     free a_addr -- wior memory
648 :     free(a_addr);
649 :     wior = 0;
650 :    
651 :     resize a_addr1 u -- a_addr2 wior memory
652 :     a_addr2 = realloc(a_addr1, u);
653 : anton 1.6 wior = a_addr2==NULL; /* !! Define a return code */
654 : anton 1.1
655 :     (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
656 :     for (; f83name1 != NULL; f83name1 = f83name1->next)
657 : pazsan 1.8 if (F83NAME_COUNT(f83name1)==u &&
658 : pazsan 1.13 strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
659 : pazsan 1.8 break;
660 :     f83name2=f83name1;
661 :    
662 : pazsan 1.13 (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
663 :     F83Name *f83name1;
664 :     f83name2=NULL;
665 :     while(a_addr != NULL)
666 :     {
667 :     f83name1=(F83Name *)(a_addr[1]);
668 :     a_addr=(Cell *)(a_addr[0]);
669 :     if (F83NAME_COUNT(f83name1)==u &&
670 :     strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
671 :     {
672 :     f83name2=f83name1;
673 :     break;
674 :     }
675 :     }
676 :    
677 : anton 1.14 (hashkey) c_addr u1 -- u2 new paren_hashkey
678 : pazsan 1.13 u2=0;
679 :     while(u1--)
680 :     u2+=(int)toupper(*c_addr++);
681 : anton 1.14
682 :     (hashkey1) c_addr u ubits -- ukey new paren_hashkey1
683 :     ""ukey is the hash key for the string c_addr u fitting in ubits bits""
684 :     /* this hash function rotates the key at every step by rot bits within
685 :     ubits bits and xors it with the character. This function does ok in
686 :     the chi-sqare-test. Rot should be <=7 (preferably <=5) for
687 :     ASCII strings (larger if ubits is large), and should share no
688 :     divisors with ubits.
689 :     */
690 :     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];
691 :     Char *cp = c_addr;
692 :     for (ukey=0; cp<c_addr+u; cp++)
693 :     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
694 :     ^ toupper(*cp))
695 :     & ((1<<ubits)-1));
696 : anton 1.1
697 :     (parse-white) c_addr1 u1 -- c_addr2 u2 new paren_parse_white
698 :     /* use !isgraph instead of isspace? */
699 :     Char *endp = c_addr1+u1;
700 :     while (c_addr1<endp && isspace(*c_addr1))
701 :     c_addr1++;
702 :     if (c_addr1<endp) {
703 :     for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
704 :     ;
705 :     u2 = c_addr1-c_addr2;
706 :     }
707 :     else {
708 :     c_addr2 = c_addr1;
709 :     u2 = 0;
710 :     }
711 :    
712 :     close-file wfileid -- wior file close_file
713 : pazsan 1.7 wior = FILEIO(fclose((FILE *)wfileid)==EOF);
714 : anton 1.1
715 :     open-file c_addr u ntype -- w2 wior file open_file
716 : anton 1.17 w2 = (Cell)fopen(cstr(c_addr, u,1), fileattr[ntype]);
717 : pazsan 1.7 wior = FILEEXIST(w2 == NULL);
718 : anton 1.1
719 :     create-file c_addr u ntype -- w2 wior file create_file
720 :     int fd;
721 : anton 1.17 fd = creat(cstr(c_addr, u,1), 0644);
722 : anton 1.1 if (fd > -1) {
723 :     w2 = (Cell)fdopen(fd, fileattr[ntype]);
724 :     assert(w2 != NULL);
725 :     wior = 0;
726 :     } else {
727 :     assert(fd == -1);
728 : pazsan 1.7 wior = FILEIO(fd);
729 : anton 1.1 w2 = 0;
730 :     }
731 :    
732 :     delete-file c_addr u -- wior file delete_file
733 : anton 1.17 wior = FILEEXIST(unlink(cstr(c_addr, u,1)));
734 : anton 1.1
735 :     rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
736 : anton 1.17 char *s1=cstr(c_addr2, u2,1);
737 :     wior = FILEEXIST(rename(cstr(c_addr1, u1, 0), s1));
738 : anton 1.1
739 :     file-position wfileid -- ud wior file file_position
740 :     /* !! use tell and lseek? */
741 :     ud = ftell((FILE *)wfileid);
742 :     wior = 0; /* !! or wior = FLAG(ud<0) */
743 :    
744 :     reposition-file ud wfileid -- wior file reposition_file
745 : pazsan 1.7 wior = FILEIO(fseek((FILE *)wfileid, (long)ud, SEEK_SET));
746 : anton 1.1
747 :     file-size wfileid -- ud wior file file_size
748 :     struct stat buf;
749 : pazsan 1.7 wior = FILEEXIST(fstat(fileno((FILE *)wfileid), &buf));
750 : anton 1.1 ud = buf.st_size;
751 :    
752 :     resize-file ud wfileid -- wior file resize_file
753 : pazsan 1.7 wior = FILEIO(ftruncate(fileno((FILE *)wfileid), (int)ud));
754 : anton 1.1
755 :     read-file c_addr u1 wfileid -- u2 wior file read_file
756 :     /* !! fread does not guarantee enough */
757 :     u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
758 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
759 : anton 1.1 /* !! who performs clearerr((FILE *)wfileid); ? */
760 :    
761 :     read-line c_addr u1 wfileid -- u2 flag wior file read_line
762 : pazsan 1.13 /*
763 :     Cell c;
764 :     flag=-1;
765 :     for(u2=0; u2<u1; u2++)
766 :     {
767 :     *c_addr++ = (Char)(c = getc((FILE *)wfileid));
768 :     if(c=='\n') break;
769 :     if(c==EOF)
770 :     {
771 :     flag=FLAG(u2!=0);
772 :     break;
773 :     }
774 :     }
775 :     wior=FILEIO(ferror((FILE *)wfileid));
776 :     */
777 :     if ((flag=FLAG(!feof((FILE *)wfileid) &&
778 :     fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
779 : anton 1.11 wior=FILEIO(ferror((FILE *)wfileid));
780 : pazsan 1.13 u2 = strlen(c_addr);
781 : anton 1.11 u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
782 :     }
783 :     else {
784 :     wior=0;
785 :     u2=0;
786 :     }
787 : anton 1.1
788 :     write-file c_addr u1 wfileid -- wior file write_file
789 :     /* !! fwrite does not guarantee enough */
790 :     {
791 :     int u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
792 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
793 : anton 1.1 }
794 :    
795 :     flush-file wfileid -- wior file-ext flush_file
796 : pazsan 1.7 wior = FILEIO(fflush((FILE *) wfileid));
797 : anton 1.1
798 :     comparisons(f, r1 r2, f_, r1, r2, new, new, float, new)
799 :     comparisons(f0, r, f_zero_, r, 0., float, new, float, new)
800 :    
801 :     d>f d -- r float d_to_f
802 :     r = d;
803 :    
804 :     f>d r -- d float f_to_d
805 :     /* !! basis 15 is not very specific */
806 :     d = r;
807 :    
808 :     f! r f_addr -- float f_store
809 :     *f_addr = r;
810 :    
811 :     f@ f_addr -- r float f_fetch
812 :     r = *f_addr;
813 :    
814 :     df@ df_addr -- r float-ext d_f_fetch
815 :     #ifdef IEEE_FP
816 :     r = *df_addr;
817 :     #else
818 :     !! df@
819 :     #endif
820 :    
821 :     df! r df_addr -- float-ext d_f_store
822 :     #ifdef IEEE_FP
823 :     *df_addr = r;
824 :     #else
825 :     !! df!
826 :     #endif
827 :    
828 :     sf@ sf_addr -- r float-ext s_f_fetch
829 :     #ifdef IEEE_FP
830 :     r = *sf_addr;
831 :     #else
832 :     !! sf@
833 :     #endif
834 :    
835 :     sf! r sf_addr -- float-ext s_f_store
836 :     #ifdef IEEE_FP
837 :     *sf_addr = r;
838 :     #else
839 :     !! sf!
840 :     #endif
841 :    
842 :     f+ r1 r2 -- r3 float f_plus
843 :     r3 = r1+r2;
844 :    
845 :     f- r1 r2 -- r3 float f_minus
846 :     r3 = r1-r2;
847 :    
848 :     f* r1 r2 -- r3 float f_star
849 :     r3 = r1*r2;
850 :    
851 :     f/ r1 r2 -- r3 float f_slash
852 :     r3 = r1/r2;
853 :    
854 :     f** r1 r2 -- r3 float-ext f_star_star
855 :     r3 = pow(r1,r2);
856 :    
857 :     fnegate r1 -- r2 float
858 :     r2 = - r1;
859 :    
860 :     fdrop r -- float
861 :    
862 :     fdup r -- r r float
863 :    
864 :     fswap r1 r2 -- r2 r1 float
865 :    
866 :     fover r1 r2 -- r1 r2 r1 float
867 :    
868 :     frot r1 r2 r3 -- r2 r3 r1 float
869 :    
870 :     float+ f_addr1 -- f_addr2 float float_plus
871 :     f_addr2 = f_addr1+1;
872 :    
873 :     floats n1 -- n2 float
874 :     n2 = n1*sizeof(Float);
875 :    
876 :     floor r1 -- r2 float
877 :     /* !! unclear wording */
878 :     r2 = floor(r1);
879 :    
880 :     fround r1 -- r2 float
881 :     /* !! unclear wording */
882 :     r2 = rint(r1);
883 :    
884 :     fmax r1 r2 -- r3 float
885 :     if (r1<r2)
886 :     r3 = r2;
887 :     else
888 :     r3 = r1;
889 :    
890 :     fmin r1 r2 -- r3 float
891 :     if (r1<r2)
892 :     r3 = r1;
893 :     else
894 :     r3 = r2;
895 :    
896 :     represent r c_addr u -- n f1 f2 float
897 :     char *sig;
898 :     int flag;
899 : anton 1.9 int decpt;
900 :     sig=ecvt(r, u, &decpt, &flag);
901 :     n=decpt;
902 : anton 1.1 f1=FLAG(flag!=0);
903 :     f2=FLAG(isdigit(sig[0])!=0);
904 :     memmove(c_addr,sig,u);
905 :    
906 :     >float c_addr u -- flag float to_float
907 :     /* real signature: c_addr u -- r t / f */
908 :     Float r;
909 : anton 1.17 char *number=cstr(c_addr, u, 1);
910 : anton 1.1 char *endconv;
911 :     r=strtod(number,&endconv);
912 : pazsan 1.8 if((flag=FLAG(!(int)*endconv)))
913 : anton 1.1 {
914 :     IF_FTOS(fp[0] = FTOS);
915 :     fp += -1;
916 :     FTOS = r;
917 :     }
918 :     else if(*endconv=='d' || *endconv=='D')
919 :     {
920 :     *endconv='E';
921 :     r=strtod(number,&endconv);
922 : pazsan 1.8 if((flag=FLAG(!(int)*endconv)))
923 : anton 1.1 {
924 :     IF_FTOS(fp[0] = FTOS);
925 :     fp += -1;
926 :     FTOS = r;
927 :     }
928 :     }
929 :    
930 :     fabs r1 -- r2 float-ext
931 :     r2 = fabs(r1);
932 :    
933 :     facos r1 -- r2 float-ext
934 :     r2 = acos(r1);
935 :    
936 :     fasin r1 -- r2 float-ext
937 :     r2 = asin(r1);
938 :    
939 :     fatan r1 -- r2 float-ext
940 :     r2 = atan(r1);
941 :    
942 :     fatan2 r1 r2 -- r3 float-ext
943 :     r3 = atan2(r1,r2);
944 :    
945 :     fcos r1 -- r2 float-ext
946 :     r2 = cos(r1);
947 :    
948 :     fexp r1 -- r2 float-ext
949 :     r2 = exp(r1);
950 :    
951 : pazsan 1.3 fexpm1 r1 -- r2 float-ext
952 :     r2 =
953 :     #ifdef expm1
954 :     expm1(r1);
955 :     #else
956 :     exp(r1)-1;
957 :     #endif
958 :    
959 : anton 1.1 fln r1 -- r2 float-ext
960 :     r2 = log(r1);
961 :    
962 : pazsan 1.3 flnp1 r1 -- r2 float-ext
963 :     r2 =
964 :     #ifdef log1p
965 :     log1p(r1);
966 :     #else
967 :     log(r1+1);
968 :     #endif
969 :    
970 : anton 1.1 flog r1 -- r2 float-ext
971 :     r2 = log10(r1);
972 :    
973 : pazsan 1.3 fsin r1 -- r2 float-ext
974 :     r2 = sin(r1);
975 :    
976 :     fsincos r1 -- r2 r3 float-ext
977 : anton 1.1 r2 = sin(r1);
978 :     r3 = cos(r1);
979 :    
980 :     fsqrt r1 -- r2 float-ext
981 :     r2 = sqrt(r1);
982 :    
983 :     ftan r1 -- r2 float-ext
984 :     r2 = tan(r1);
985 :    
986 : anton 1.6 \ The following words access machine/OS/installation-dependent ANSI
987 :     \ figForth internals
988 :     \ !! how about environmental queries DIRECT-THREADED,
989 :     \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
990 : anton 1.1
991 :     >body xt -- a_addr core to_body
992 :     a_addr = PFA(xt);
993 :    
994 :     >code-address xt -- c_addr new to_code_address
995 :     ""c_addr is the code address of the word xt""
996 :     /* !! This behaves installation-dependently for DOES-words */
997 :     c_addr = CODE_ADDRESS(xt);
998 :    
999 :     >does-code xt -- a_addr new to_does_code
1000 :     ""If xt ist the execution token of a defining-word-defined word,
1001 :     a_addr is the start of the Forth code after the DOES>; Otherwise the
1002 :     behaviour is uundefined""
1003 :     /* !! there is currently no way to determine whether a word is
1004 :     defining-word-defined */
1005 :     a_addr = DOES_CODE(xt);
1006 :    
1007 : pazsan 1.4 code-address! n xt -- new code_address_store
1008 : anton 1.1 ""Creates a code field with code address c_addr at xt""
1009 : pazsan 1.4 MAKE_CF(xt, symbols[CF(n)]);
1010 : pazsan 1.5 CACHE_FLUSH(xt,PFA(0));
1011 : anton 1.1
1012 :     does-code! a_addr xt -- new does_code_store
1013 :     ""creates a code field at xt for a defining-word-defined word; a_addr
1014 :     is the start of the Forth code after DOES>""
1015 :     MAKE_DOES_CF(xt, a_addr);
1016 : pazsan 1.5 CACHE_FLUSH(xt,PFA(0));
1017 : anton 1.1
1018 :     does-handler! a_addr -- new does_jump_store
1019 :     ""creates a DOES>-handler at address a_addr. a_addr usually points
1020 :     just behind a DOES>.""
1021 :     MAKE_DOES_HANDLER(a_addr);
1022 : pazsan 1.5 CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
1023 : anton 1.1
1024 :     /does-handler -- n new slash_does_handler
1025 :     ""the size of a does-handler (includes possible padding)""
1026 :     /* !! a constant or environmental query might be better */
1027 :     n = DOES_HANDLER_SIZE;
1028 :    
1029 :     toupper c1 -- c2 new
1030 :     c2 = toupper(c1);
1031 :    
1032 : anton 1.6 \ local variable implementation primitives
1033 : anton 1.1 @local# -- w new fetch_local_number
1034 :     w = *(Cell *)(lp+(int)(*ip++));
1035 :    
1036 : anton 1.9 @local0 -- w new fetch_local_zero
1037 :     w = *(Cell *)(lp+0);
1038 :    
1039 :     @local4 -- w new fetch_local_four
1040 :     w = *(Cell *)(lp+4);
1041 :    
1042 :     @local8 -- w new fetch_local_eight
1043 :     w = *(Cell *)(lp+8);
1044 :    
1045 :     @local12 -- w new fetch_local_twelve
1046 :     w = *(Cell *)(lp+12);
1047 :    
1048 : anton 1.1 f@local# -- r new f_fetch_local_number
1049 :     r = *(Float *)(lp+(int)(*ip++));
1050 :    
1051 : anton 1.9 f@local0 -- r new f_fetch_local_zero
1052 :     r = *(Float *)(lp+0);
1053 :    
1054 :     f@local8 -- r new f_fetch_local_eight
1055 :     r = *(Float *)(lp+8);
1056 :    
1057 : anton 1.1 laddr# -- c_addr new laddr_number
1058 :     /* this can also be used to implement lp@ */
1059 :     c_addr = (Char *)(lp+(int)(*ip++));
1060 :    
1061 :     lp+!# -- new lp_plus_store_number
1062 :     ""used with negative immediate values it allocates memory on the
1063 :     local stack, a positive immediate argument drops memory from the local
1064 :     stack""
1065 :     lp += (int)(*ip++);
1066 : anton 1.9
1067 :     -4lp+! -- new minus_four_lp_plus_store
1068 :     lp += -4;
1069 :    
1070 :     8lp+! -- new eight_lp_plus_store
1071 :     lp += 8;
1072 :    
1073 :     16lp+! -- new sixteen_lp_plus_store
1074 :     lp += 16;
1075 : anton 1.1
1076 :     lp! c_addr -- new lp_store
1077 :     lp = (Address)c_addr;
1078 :    
1079 :     >l w -- new to_l
1080 :     lp -= sizeof(Cell);
1081 :     *(Cell *)lp = w;
1082 :    
1083 :     f>l r -- new f_to_l
1084 :     lp -= sizeof(Float);
1085 :     *(Float *)lp = r;
1086 : pazsan 1.4
1087 :     up! a_addr -- new up_store
1088 : pazsan 1.8 up=(char *)a_addr;
1089 : pazsan 1.12 up0=(char *)a_addr;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help