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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help