[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 :     over w1 w2 -- w1 w2 w1 core,fig
492 :    
493 :     drop w -- core,fig
494 :    
495 :     swap w1 w2 -- w2 w1 core,fig
496 :    
497 :     dup w -- w w core,fig
498 :    
499 :     rot w1 w2 w3 -- w2 w3 w1 core rote
500 :    
501 :     -rot w1 w2 w3 -- w3 w1 w2 fig not_rote
502 :    
503 :     nip w1 w2 -- w2 core-ext
504 :    
505 :     tuck w1 w2 -- w2 w1 w2 core-ext
506 :    
507 :     ?dup w -- w core question_dupe
508 :     if (w!=0) {
509 : pazsan 1.7 IF_TOS(*sp-- = w;)
510 : anton 1.1 #ifndef USE_TOS
511 : pazsan 1.7 *--sp = w;
512 : anton 1.1 #endif
513 :     }
514 :    
515 :     pick u -- w core-ext
516 :     w = sp[u+1];
517 :    
518 :     2drop w1 w2 -- core two_drop
519 :    
520 :     2dup w1 w2 -- w1 w2 w1 w2 core two_dupe
521 :    
522 :     2over w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 core two_over
523 :    
524 :     2swap w1 w2 w3 w4 -- w3 w4 w1 w2 core two_swap
525 :    
526 :     2rot w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 double two_rote
527 :    
528 : anton 1.6 \ toggle is high-level: 0.11/0.42%
529 : anton 1.1
530 :     @ a_addr -- w fig fetch
531 :     w = *a_addr;
532 :    
533 :     ! w a_addr -- core,fig store
534 :     *a_addr = w;
535 :    
536 :     +! n a_addr -- core,fig plus_store
537 :     *a_addr += n;
538 :    
539 :     c@ c_addr -- c fig cfetch
540 :     c = *c_addr;
541 :    
542 :     c! c c_addr -- fig cstore
543 :     *c_addr = c;
544 :    
545 :     2! w1 w2 a_addr -- core two_store
546 :     a_addr[0] = w2;
547 :     a_addr[1] = w1;
548 :    
549 :     2@ a_addr -- w1 w2 core two_fetch
550 :     w2 = a_addr[0];
551 :     w1 = a_addr[1];
552 :    
553 :     d! d a_addr -- double d_store
554 :     /* !! alignment problems on some machines */
555 :     *(DCell *)a_addr = d;
556 :    
557 :     d@ a_addr -- d double d_fetch
558 :     d = *(DCell *)a_addr;
559 :    
560 :     cell+ a_addr1 -- a_addr2 core cell_plus
561 :     a_addr2 = a_addr1+1;
562 :    
563 :     cells n1 -- n2 core
564 :     n2 = n1 * sizeof(Cell);
565 :    
566 :     char+ c_addr1 -- c_addr2 core care_plus
567 :     c_addr2 = c_addr1+1;
568 :    
569 :     chars n1 -- n2 core cares
570 :     n2 = n1 * sizeof(Char);
571 :    
572 :     count c_addr1 -- c_addr2 u core
573 :     u = *c_addr1;
574 :     c_addr2 = c_addr1+1;
575 :    
576 :     (bye) n -- toolkit-ext paren_bye
577 :     deprep_terminal();
578 :     exit(n);
579 :    
580 :     system c_addr u -- n own
581 :     char pname[u+1];
582 :     cstr(pname,c_addr,u);
583 :     n=system(pname);
584 :    
585 :     popen c_addr u n -- wfileid own
586 :     char pname[u+1];
587 :     static char* mode[2]={"r","w"};
588 :     cstr(pname,c_addr,u);
589 :     wfileid=(Cell)popen(pname,mode[n]);
590 :    
591 :     pclose wfileid -- wior own
592 :     wior=pclose((FILE *)wfileid);
593 : pazsan 1.2
594 :     time&date -- nyear nmonth nday nhour nmin nsec ansi time_and_date
595 :     struct timeval time1;
596 :     struct timezone zone1;
597 :     struct tm *ltime;
598 :     gettimeofday(&time1,&zone1);
599 :     ltime=localtime(&time1.tv_sec);
600 :     nyear =ltime->tm_year+1900;
601 :     nmonth=ltime->tm_mon;
602 :     nday =ltime->tm_mday;
603 :     nhour =ltime->tm_hour;
604 :     nmin =ltime->tm_min;
605 :     nsec =ltime->tm_sec;
606 :    
607 :     ms n -- ansi
608 :     struct timeval timeout;
609 :     timeout.tv_sec=n/1000;
610 :     timeout.tv_usec=1000*(n%1000);
611 :     (void)select(0,0,0,0,&timeout);
612 : anton 1.1
613 :     allocate u -- a_addr wior memory
614 :     a_addr = (Cell *)malloc(u);
615 : anton 1.6 wior = a_addr==NULL; /* !! Define a return code */
616 : anton 1.1
617 :     free a_addr -- wior memory
618 :     free(a_addr);
619 :     wior = 0;
620 :    
621 :     resize a_addr1 u -- a_addr2 wior memory
622 :     a_addr2 = realloc(a_addr1, u);
623 : anton 1.6 wior = a_addr2==NULL; /* !! Define a return code */
624 : anton 1.1
625 :     (f83find) c_addr u f83name1 -- f83name2 new paren_f83find
626 :     for (; f83name1 != NULL; f83name1 = f83name1->next)
627 : pazsan 1.8 if (F83NAME_COUNT(f83name1)==u &&
628 : pazsan 1.13 strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
629 : pazsan 1.8 break;
630 :     f83name2=f83name1;
631 :    
632 : pazsan 1.13 (hashfind) c_addr u a_addr -- f83name2 new paren_hashfind
633 :     F83Name *f83name1;
634 :     f83name2=NULL;
635 :     while(a_addr != NULL)
636 :     {
637 :     f83name1=(F83Name *)(a_addr[1]);
638 :     a_addr=(Cell *)(a_addr[0]);
639 :     if (F83NAME_COUNT(f83name1)==u &&
640 :     strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
641 :     {
642 :     f83name2=f83name1;
643 :     break;
644 :     }
645 :     }
646 :    
647 :     (hashkey) c_addr u1 -- u2 new paren_hashkey
648 :     u2=0;
649 :     while(u1--)
650 :     u2+=(int)toupper(*c_addr++);
651 : anton 1.1
652 :     (parse-white) c_addr1 u1 -- c_addr2 u2 new paren_parse_white
653 :     /* use !isgraph instead of isspace? */
654 :     Char *endp = c_addr1+u1;
655 :     while (c_addr1<endp && isspace(*c_addr1))
656 :     c_addr1++;
657 :     if (c_addr1<endp) {
658 :     for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
659 :     ;
660 :     u2 = c_addr1-c_addr2;
661 :     }
662 :     else {
663 :     c_addr2 = c_addr1;
664 :     u2 = 0;
665 :     }
666 :    
667 :     close-file wfileid -- wior file close_file
668 : pazsan 1.7 wior = FILEIO(fclose((FILE *)wfileid)==EOF);
669 : anton 1.1
670 :     open-file c_addr u ntype -- w2 wior file open_file
671 :     char fname[u+1];
672 :     cstr(fname, c_addr, u);
673 :     w2 = (Cell)fopen(fname, fileattr[ntype]);
674 : pazsan 1.7 wior = FILEEXIST(w2 == NULL);
675 : anton 1.1
676 :     create-file c_addr u ntype -- w2 wior file create_file
677 :     int fd;
678 :     char fname[u+1];
679 :     cstr(fname, c_addr, u);
680 :     fd = creat(fname, 0666);
681 :     if (fd > -1) {
682 :     w2 = (Cell)fdopen(fd, fileattr[ntype]);
683 :     assert(w2 != NULL);
684 :     wior = 0;
685 :     } else {
686 :     assert(fd == -1);
687 : pazsan 1.7 wior = FILEIO(fd);
688 : anton 1.1 w2 = 0;
689 :     }
690 :    
691 :     delete-file c_addr u -- wior file delete_file
692 :     char fname[u+1];
693 :     cstr(fname, c_addr, u);
694 : pazsan 1.7 wior = FILEEXIST(unlink(fname));
695 : anton 1.1
696 :     rename-file c_addr1 u1 c_addr2 u2 -- wior file-ext rename_file
697 :     char fname1[u1+1];
698 :     char fname2[u2+1];
699 :     cstr(fname1, c_addr1, u1);
700 :     cstr(fname2, c_addr2, u2);
701 : pazsan 1.7 wior = FILEEXIST(rename(fname1, fname2));
702 : anton 1.1
703 :     file-position wfileid -- ud wior file file_position
704 :     /* !! use tell and lseek? */
705 :     ud = ftell((FILE *)wfileid);
706 :     wior = 0; /* !! or wior = FLAG(ud<0) */
707 :    
708 :     reposition-file ud wfileid -- wior file reposition_file
709 : pazsan 1.7 wior = FILEIO(fseek((FILE *)wfileid, (long)ud, SEEK_SET));
710 : anton 1.1
711 :     file-size wfileid -- ud wior file file_size
712 :     struct stat buf;
713 : pazsan 1.7 wior = FILEEXIST(fstat(fileno((FILE *)wfileid), &buf));
714 : anton 1.1 ud = buf.st_size;
715 :    
716 :     resize-file ud wfileid -- wior file resize_file
717 : pazsan 1.7 wior = FILEIO(ftruncate(fileno((FILE *)wfileid), (int)ud));
718 : anton 1.1
719 :     read-file c_addr u1 wfileid -- u2 wior file read_file
720 :     /* !! fread does not guarantee enough */
721 :     u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
722 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
723 : anton 1.1 /* !! who performs clearerr((FILE *)wfileid); ? */
724 :    
725 :     read-line c_addr u1 wfileid -- u2 flag wior file read_line
726 : pazsan 1.13 /*
727 :     Cell c;
728 :     flag=-1;
729 :     for(u2=0; u2<u1; u2++)
730 :     {
731 :     *c_addr++ = (Char)(c = getc((FILE *)wfileid));
732 :     if(c=='\n') break;
733 :     if(c==EOF)
734 :     {
735 :     flag=FLAG(u2!=0);
736 :     break;
737 :     }
738 :     }
739 :     wior=FILEIO(ferror((FILE *)wfileid));
740 :     */
741 :     if ((flag=FLAG(!feof((FILE *)wfileid) &&
742 :     fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
743 : anton 1.11 wior=FILEIO(ferror((FILE *)wfileid));
744 : pazsan 1.13 u2 = strlen(c_addr);
745 : anton 1.11 u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
746 :     }
747 :     else {
748 :     wior=0;
749 :     u2=0;
750 :     }
751 : anton 1.1
752 :     write-file c_addr u1 wfileid -- wior file write_file
753 :     /* !! fwrite does not guarantee enough */
754 :     {
755 :     int u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
756 : pazsan 1.7 wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
757 : anton 1.1 }
758 :    
759 :     flush-file wfileid -- wior file-ext flush_file
760 : pazsan 1.7 wior = FILEIO(fflush((FILE *) wfileid));
761 : anton 1.1
762 :     comparisons(f, r1 r2, f_, r1, r2, new, new, float, new)
763 :     comparisons(f0, r, f_zero_, r, 0., float, new, float, new)
764 :    
765 :     d>f d -- r float d_to_f
766 :     r = d;
767 :    
768 :     f>d r -- d float f_to_d
769 :     /* !! basis 15 is not very specific */
770 :     d = r;
771 :    
772 :     f! r f_addr -- float f_store
773 :     *f_addr = r;
774 :    
775 :     f@ f_addr -- r float f_fetch
776 :     r = *f_addr;
777 :    
778 :     df@ df_addr -- r float-ext d_f_fetch
779 :     #ifdef IEEE_FP
780 :     r = *df_addr;
781 :     #else
782 :     !! df@
783 :     #endif
784 :    
785 :     df! r df_addr -- float-ext d_f_store
786 :     #ifdef IEEE_FP
787 :     *df_addr = r;
788 :     #else
789 :     !! df!
790 :     #endif
791 :    
792 :     sf@ sf_addr -- r float-ext s_f_fetch
793 :     #ifdef IEEE_FP
794 :     r = *sf_addr;
795 :     #else
796 :     !! sf@
797 :     #endif
798 :    
799 :     sf! r sf_addr -- float-ext s_f_store
800 :     #ifdef IEEE_FP
801 :     *sf_addr = r;
802 :     #else
803 :     !! sf!
804 :     #endif
805 :    
806 :     f+ r1 r2 -- r3 float f_plus
807 :     r3 = r1+r2;
808 :    
809 :     f- r1 r2 -- r3 float f_minus
810 :     r3 = r1-r2;
811 :    
812 :     f* r1 r2 -- r3 float f_star
813 :     r3 = r1*r2;
814 :    
815 :     f/ r1 r2 -- r3 float f_slash
816 :     r3 = r1/r2;
817 :    
818 :     f** r1 r2 -- r3 float-ext f_star_star
819 :     r3 = pow(r1,r2);
820 :    
821 :     fnegate r1 -- r2 float
822 :     r2 = - r1;
823 :    
824 :     fdrop r -- float
825 :    
826 :     fdup r -- r r float
827 :    
828 :     fswap r1 r2 -- r2 r1 float
829 :    
830 :     fover r1 r2 -- r1 r2 r1 float
831 :    
832 :     frot r1 r2 r3 -- r2 r3 r1 float
833 :    
834 :     float+ f_addr1 -- f_addr2 float float_plus
835 :     f_addr2 = f_addr1+1;
836 :    
837 :     floats n1 -- n2 float
838 :     n2 = n1*sizeof(Float);
839 :    
840 :     floor r1 -- r2 float
841 :     /* !! unclear wording */
842 :     r2 = floor(r1);
843 :    
844 :     fround r1 -- r2 float
845 :     /* !! unclear wording */
846 :     r2 = rint(r1);
847 :    
848 :     fmax r1 r2 -- r3 float
849 :     if (r1<r2)
850 :     r3 = r2;
851 :     else
852 :     r3 = r1;
853 :    
854 :     fmin r1 r2 -- r3 float
855 :     if (r1<r2)
856 :     r3 = r1;
857 :     else
858 :     r3 = r2;
859 :    
860 :     represent r c_addr u -- n f1 f2 float
861 :     char *sig;
862 :     int flag;
863 : anton 1.9 int decpt;
864 :     sig=ecvt(r, u, &decpt, &flag);
865 :     n=decpt;
866 : anton 1.1 f1=FLAG(flag!=0);
867 :     f2=FLAG(isdigit(sig[0])!=0);
868 :     memmove(c_addr,sig,u);
869 :    
870 :     >float c_addr u -- flag float to_float
871 :     /* real signature: c_addr u -- r t / f */
872 :     Float r;
873 :     char number[u+1];
874 :     char *endconv;
875 :     cstr(number, c_addr, u);
876 :     r=strtod(number,&endconv);
877 : pazsan 1.8 if((flag=FLAG(!(int)*endconv)))
878 : anton 1.1 {
879 :     IF_FTOS(fp[0] = FTOS);
880 :     fp += -1;
881 :     FTOS = r;
882 :     }
883 :     else if(*endconv=='d' || *endconv=='D')
884 :     {
885 :     *endconv='E';
886 :     r=strtod(number,&endconv);
887 : pazsan 1.8 if((flag=FLAG(!(int)*endconv)))
888 : anton 1.1 {
889 :     IF_FTOS(fp[0] = FTOS);
890 :     fp += -1;
891 :     FTOS = r;
892 :     }
893 :     }
894 :    
895 :     fabs r1 -- r2 float-ext
896 :     r2 = fabs(r1);
897 :    
898 :     facos r1 -- r2 float-ext
899 :     r2 = acos(r1);
900 :    
901 :     fasin r1 -- r2 float-ext
902 :     r2 = asin(r1);
903 :    
904 :     fatan r1 -- r2 float-ext
905 :     r2 = atan(r1);
906 :    
907 :     fatan2 r1 r2 -- r3 float-ext
908 :     r3 = atan2(r1,r2);
909 :    
910 :     fcos r1 -- r2 float-ext
911 :     r2 = cos(r1);
912 :    
913 :     fexp r1 -- r2 float-ext
914 :     r2 = exp(r1);
915 :    
916 : pazsan 1.3 fexpm1 r1 -- r2 float-ext
917 :     r2 =
918 :     #ifdef expm1
919 :     expm1(r1);
920 :     #else
921 :     exp(r1)-1;
922 :     #endif
923 :    
924 : anton 1.1 fln r1 -- r2 float-ext
925 :     r2 = log(r1);
926 :    
927 : pazsan 1.3 flnp1 r1 -- r2 float-ext
928 :     r2 =
929 :     #ifdef log1p
930 :     log1p(r1);
931 :     #else
932 :     log(r1+1);
933 :     #endif
934 :    
935 : anton 1.1 flog r1 -- r2 float-ext
936 :     r2 = log10(r1);
937 :    
938 : pazsan 1.3 fsin r1 -- r2 float-ext
939 :     r2 = sin(r1);
940 :    
941 :     fsincos r1 -- r2 r3 float-ext
942 : anton 1.1 r2 = sin(r1);
943 :     r3 = cos(r1);
944 :    
945 :     fsqrt r1 -- r2 float-ext
946 :     r2 = sqrt(r1);
947 :    
948 :     ftan r1 -- r2 float-ext
949 :     r2 = tan(r1);
950 :    
951 : anton 1.6 \ The following words access machine/OS/installation-dependent ANSI
952 :     \ figForth internals
953 :     \ !! how about environmental queries DIRECT-THREADED,
954 :     \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
955 : anton 1.1
956 :     >body xt -- a_addr core to_body
957 :     a_addr = PFA(xt);
958 :    
959 :     >code-address xt -- c_addr new to_code_address
960 :     ""c_addr is the code address of the word xt""
961 :     /* !! This behaves installation-dependently for DOES-words */
962 :     c_addr = CODE_ADDRESS(xt);
963 :    
964 :     >does-code xt -- a_addr new to_does_code
965 :     ""If xt ist the execution token of a defining-word-defined word,
966 :     a_addr is the start of the Forth code after the DOES>; Otherwise the
967 :     behaviour is uundefined""
968 :     /* !! there is currently no way to determine whether a word is
969 :     defining-word-defined */
970 :     a_addr = DOES_CODE(xt);
971 :    
972 : pazsan 1.4 code-address! n xt -- new code_address_store
973 : anton 1.1 ""Creates a code field with code address c_addr at xt""
974 : pazsan 1.4 MAKE_CF(xt, symbols[CF(n)]);
975 : pazsan 1.5 CACHE_FLUSH(xt,PFA(0));
976 : anton 1.1
977 :     does-code! a_addr xt -- new does_code_store
978 :     ""creates a code field at xt for a defining-word-defined word; a_addr
979 :     is the start of the Forth code after DOES>""
980 :     MAKE_DOES_CF(xt, a_addr);
981 : pazsan 1.5 CACHE_FLUSH(xt,PFA(0));
982 : anton 1.1
983 :     does-handler! a_addr -- new does_jump_store
984 :     ""creates a DOES>-handler at address a_addr. a_addr usually points
985 :     just behind a DOES>.""
986 :     MAKE_DOES_HANDLER(a_addr);
987 : pazsan 1.5 CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
988 : anton 1.1
989 :     /does-handler -- n new slash_does_handler
990 :     ""the size of a does-handler (includes possible padding)""
991 :     /* !! a constant or environmental query might be better */
992 :     n = DOES_HANDLER_SIZE;
993 :    
994 :     toupper c1 -- c2 new
995 :     c2 = toupper(c1);
996 :    
997 : anton 1.6 \ local variable implementation primitives
998 : anton 1.1 @local# -- w new fetch_local_number
999 :     w = *(Cell *)(lp+(int)(*ip++));
1000 :    
1001 : anton 1.9 @local0 -- w new fetch_local_zero
1002 :     w = *(Cell *)(lp+0);
1003 :    
1004 :     @local4 -- w new fetch_local_four
1005 :     w = *(Cell *)(lp+4);
1006 :    
1007 :     @local8 -- w new fetch_local_eight
1008 :     w = *(Cell *)(lp+8);
1009 :    
1010 :     @local12 -- w new fetch_local_twelve
1011 :     w = *(Cell *)(lp+12);
1012 :    
1013 : anton 1.1 f@local# -- r new f_fetch_local_number
1014 :     r = *(Float *)(lp+(int)(*ip++));
1015 :    
1016 : anton 1.9 f@local0 -- r new f_fetch_local_zero
1017 :     r = *(Float *)(lp+0);
1018 :    
1019 :     f@local8 -- r new f_fetch_local_eight
1020 :     r = *(Float *)(lp+8);
1021 :    
1022 : anton 1.1 laddr# -- c_addr new laddr_number
1023 :     /* this can also be used to implement lp@ */
1024 :     c_addr = (Char *)(lp+(int)(*ip++));
1025 :    
1026 :     lp+!# -- new lp_plus_store_number
1027 :     ""used with negative immediate values it allocates memory on the
1028 :     local stack, a positive immediate argument drops memory from the local
1029 :     stack""
1030 :     lp += (int)(*ip++);
1031 : anton 1.9
1032 :     -4lp+! -- new minus_four_lp_plus_store
1033 :     lp += -4;
1034 :    
1035 :     8lp+! -- new eight_lp_plus_store
1036 :     lp += 8;
1037 :    
1038 :     16lp+! -- new sixteen_lp_plus_store
1039 :     lp += 16;
1040 : anton 1.1
1041 :     lp! c_addr -- new lp_store
1042 :     lp = (Address)c_addr;
1043 :    
1044 :     >l w -- new to_l
1045 :     lp -= sizeof(Cell);
1046 :     *(Cell *)lp = w;
1047 :    
1048 :     f>l r -- new f_to_l
1049 :     lp -= sizeof(Float);
1050 :     *(Float *)lp = r;
1051 : pazsan 1.4
1052 :     up! a_addr -- new up_store
1053 : pazsan 1.8 up=(char *)a_addr;
1054 : pazsan 1.12 up0=(char *)a_addr;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help