[gforth] / gforth / Attic / primitives  

gforth: gforth/Attic/primitives


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help