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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help