[gforth] / gforth / prim  

gforth: gforth/prim


1 : anton 1.1 \ Gforth primitives
2 :    
3 : anton 1.16 \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4 : anton 1.1
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 :     \ 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 :     \
26 :     \
27 :     \ This file contains primitive specifications in the following format:
28 :     \
29 : anton 1.47 \ forth name ( stack effect ) category [pronunciation]
30 : anton 1.1 \ [""glossary entry""]
31 :     \ C code
32 :     \ [:
33 :     \ Forth code]
34 :     \
35 : anton 1.47 \ Note: Fields in brackets are optional. Word specifications have to
36 :     \ be separated by at least one empty line
37 : anton 1.1 \
38 :     \ Both pronounciation and stack items (in the stack effect) must
39 :     \ conform to the C name syntax or the C compiler will complain.
40 :     \
41 :     \
42 :     \ These specifications are automatically translated into C-code for the
43 :     \ interpreter and into some other files. I hope that your C compiler has
44 :     \ decent optimization, otherwise the automatically generated code will
45 :     \ be somewhat slow. The Forth version of the code is included for manual
46 :     \ compilers, so they will need to compile only the important words.
47 :     \
48 :     \ Note that stack pointer adjustment is performed according to stack
49 :     \ effect by automatically generated code and NEXT is automatically
50 :     \ appended to the C code. Also, you can use the names in the stack
51 :     \ effect in the C code. Stack access is automatic. One exception: if
52 :     \ your code does not fall through, the results are not stored into the
53 :     \ stack. Use different names on both sides of the '--', if you change a
54 :     \ value (some stores to the stack are optimized away).
55 :     \
56 :     \
57 :     \
58 :     \ The stack variables have the following types:
59 :     \
60 :     \ name matches type
61 :     \ f.* Bool
62 :     \ c.* Char
63 :     \ [nw].* Cell
64 :     \ u.* UCell
65 :     \ d.* DCell
66 :     \ ud.* UDCell
67 :     \ r.* Float
68 :     \ a_.* Cell *
69 :     \ c_.* Char *
70 :     \ f_.* Float *
71 :     \ df_.* DFloat *
72 :     \ sf_.* SFloat *
73 :     \ xt.* XT
74 :     \ wid.* WID
75 :     \ f83name.* F83Name *
76 :     \
77 :     \
78 :     \
79 :     \ In addition the following names can be used:
80 :     \ ip the instruction pointer
81 :     \ sp the data stack pointer
82 :     \ rp the parameter stack pointer
83 :     \ lp the locals stack pointer
84 :     \ NEXT executes NEXT
85 :     \ cfa
86 :     \ NEXT1 executes NEXT1
87 :     \ FLAG(x) makes a Forth flag from a C flag
88 :     \
89 :     \
90 :     \
91 :     \ Percentages in comments are from Koopmans book: average/maximum use
92 :     \ (taken from four, not very representative benchmarks)
93 :     \
94 :     \
95 :     \
96 :     \ To do:
97 :     \
98 :     \ throw execute, cfa and NEXT1 out?
99 :     \ macroize *ip, ip++, *ip++ (pipelining)?
100 :    
101 :     \ these m4 macros would collide with identifiers
102 :     undefine(`index')
103 :     undefine(`shift')
104 :    
105 : anton 1.47 noop ( -- ) gforth
106 : anton 1.1 ;
107 :     :
108 :     ;
109 :    
110 : anton 1.47 lit ( -- w ) gforth
111 : anton 1.1 w = (Cell)NEXT_INST;
112 :     INC_IP(1);
113 :     :
114 :     r> dup @ swap cell+ >r ;
115 :    
116 : anton 1.47 execute ( xt -- ) core
117 : crook 1.29 ""Perform the semantics represented by the execution token, @i{xt}.""
118 : anton 1.1 ip=IP;
119 :     IF_TOS(TOS = sp[0]);
120 :     EXEC(xt);
121 :    
122 : anton 1.47 perform ( a_addr -- ) gforth
123 : crook 1.22 ""Equivalent to @code{@ execute}.""
124 : anton 1.1 /* and pfe */
125 :     ip=IP;
126 :     IF_TOS(TOS = sp[0]);
127 :     EXEC(*(Xt *)a_addr);
128 :     :
129 :     @ execute ;
130 :    
131 : jwilke 1.31 \fhas? skipbranchprims 0= [IF]
132 : pazsan 1.15 \+glocals
133 : anton 1.1
134 : anton 1.47 branch-lp+!# ( -- ) gforth branch_lp_plus_store_number
135 : anton 1.1 /* this will probably not be used */
136 :     branch_adjust_lp:
137 :     lp += (Cell)(IP[1]);
138 :     goto branch;
139 :    
140 : pazsan 1.15 \+
141 : anton 1.1
142 : anton 1.47 branch ( -- ) gforth
143 : anton 1.1 branch:
144 : anton 1.23 SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
145 : anton 1.1 :
146 :     r> dup @ + >r ;
147 :    
148 :     \ condbranch(forthname,restline,code,forthcode)
149 :     \ this is non-syntactical: code must open a brace that is closed by the macro
150 :     define(condbranch,
151 :     $1 $2
152 : anton 1.23 $3 SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
153 : anton 1.1 NEXT;
154 :     }
155 :     else
156 :     INC_IP(1);
157 :     $4
158 :    
159 : pazsan 1.15 \+glocals
160 : anton 1.1
161 :     $1-lp+!# $2_lp_plus_store_number
162 :     $3 goto branch_adjust_lp;
163 :     }
164 :     else
165 :     INC_IP(2);
166 :    
167 : pazsan 1.15 \+
168 : anton 1.1 )
169 :    
170 : anton 1.47 condbranch(?branch,( f -- ) f83 question_branch,
171 : anton 1.1 if (f==0) {
172 :     IF_TOS(TOS = sp[0]);
173 : jwilke 1.5 ,:
174 :     0= dup \ !f !f
175 :     r> dup @ \ !f !f IP branchoffset
176 :     rot and + \ !f IP|IP+branchoffset
177 :     swap 0= cell and + \ IP''
178 :     >r ;)
179 : anton 1.1
180 :     \ we don't need an lp_plus_store version of the ?dup-stuff, because it
181 :     \ is only used in if's (yet)
182 :    
183 : pazsan 1.15 \+xconds
184 : anton 1.1
185 : anton 1.47 ?dup-?branch ( f -- f ) new question_dupe_question_branch
186 : anton 1.1 ""The run-time procedure compiled by @code{?DUP-IF}.""
187 :     if (f==0) {
188 :     sp++;
189 :     IF_TOS(TOS = sp[0]);
190 : anton 1.23 SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
191 : anton 1.1 NEXT;
192 :     }
193 :     else
194 :     INC_IP(1);
195 :    
196 : anton 1.47 ?dup-0=-?branch ( f -- ) new question_dupe_zero_equals_question_branch
197 : anton 1.1 ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
198 :     /* the approach taken here of declaring the word as having the stack
199 :     effect ( f -- ) and correcting for it in the branch-taken case costs a
200 :     few cycles in that case, but is easy to convert to a CONDBRANCH
201 :     invocation */
202 :     if (f!=0) {
203 :     sp--;
204 : anton 1.23 SET_IP((Xt *)(((Cell)IP)+(Cell)NEXT_INST));
205 : anton 1.1 NEXT;
206 :     }
207 :     else
208 :     INC_IP(1);
209 :    
210 : pazsan 1.15 \+
211 : jwilke 1.31 \f[THEN]
212 :     \fhas? skiploopprims 0= [IF]
213 : anton 1.1
214 : anton 1.47 condbranch((next),( -- ) cmFORTH paren_next,
215 : anton 1.1 if ((*rp)--) {
216 :     ,:
217 :     r> r> dup 1- >r
218 :     IF dup @ + >r ELSE cell+ >r THEN ;)
219 :    
220 : anton 1.47 condbranch((loop),( -- ) gforth paren_loop,
221 : anton 1.1 Cell index = *rp+1;
222 :     Cell limit = rp[1];
223 :     if (index != limit) {
224 :     *rp = index;
225 :     ,:
226 :     r> r> 1+ r> 2dup =
227 :     IF >r 1- >r cell+ >r
228 :     ELSE >r >r dup @ + >r THEN ;)
229 :    
230 : anton 1.47 condbranch((+loop),( n -- ) gforth paren_plus_loop,
231 : anton 1.1 /* !! check this thoroughly */
232 :     Cell index = *rp;
233 :     /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
234 :     /* dependent upon two's complement arithmetic */
235 :     Cell olddiff = index-rp[1];
236 :     if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
237 :     || (olddiff^n)>=0 /* it is a wrap-around effect */) {
238 :     #ifdef i386
239 :     *rp += n;
240 :     #else
241 :     *rp = index + n;
242 :     #endif
243 :     IF_TOS(TOS = sp[0]);
244 :     ,:
245 :     r> swap
246 :     r> r> 2dup - >r
247 :     2 pick r@ + r@ xor 0< 0=
248 :     3 pick r> xor 0< 0= or
249 :     IF >r + >r dup @ + >r
250 :     ELSE >r >r drop cell+ >r THEN ;)
251 :    
252 : pazsan 1.15 \+xconds
253 : anton 1.1
254 : anton 1.47 condbranch((-loop),( u -- ) gforth paren_minus_loop,
255 : anton 1.1 /* !! check this thoroughly */
256 :     Cell index = *rp;
257 :     UCell olddiff = index-rp[1];
258 :     if (olddiff>u) {
259 :     #ifdef i386
260 :     *rp -= u;
261 :     #else
262 :     *rp = index - u;
263 :     #endif
264 :     IF_TOS(TOS = sp[0]);
265 :     ,)
266 :    
267 : anton 1.47 condbranch((s+loop),( n -- ) gforth paren_symmetric_plus_loop,
268 : anton 1.1 ""The run-time procedure compiled by S+LOOP. It loops until the index
269 :     crosses the boundary between limit and limit-sign(n). I.e. a symmetric
270 :     version of (+LOOP).""
271 :     /* !! check this thoroughly */
272 :     Cell index = *rp;
273 :     Cell diff = index-rp[1];
274 :     Cell newdiff = diff+n;
275 :     if (n<0) {
276 :     diff = -diff;
277 :     newdiff = -newdiff;
278 :     }
279 :     if (diff>=0 || newdiff<0) {
280 :     #ifdef i386
281 :     *rp += n;
282 :     #else
283 :     *rp = index + n;
284 :     #endif
285 :     IF_TOS(TOS = sp[0]);
286 :     ,)
287 :    
288 : pazsan 1.15 \+
289 : anton 1.1
290 : anton 1.47 unloop ( -- ) core
291 : anton 1.1 rp += 2;
292 :     :
293 :     r> rdrop rdrop >r ;
294 :    
295 : anton 1.47 (for) ( ncount -- ) cmFORTH paren_for
296 : anton 1.1 /* or (for) = >r -- collides with unloop! */
297 :     *--rp = 0;
298 :     *--rp = ncount;
299 :     :
300 :     r> swap 0 >r >r >r ;
301 :    
302 : anton 1.47 (do) ( nlimit nstart -- ) gforth paren_do
303 : anton 1.1 /* or do it in high-level? 0.09/0.23% */
304 :     *--rp = nlimit;
305 :     *--rp = nstart;
306 :     :
307 :     r> swap rot >r >r >r ;
308 :    
309 : anton 1.47 (?do) ( nlimit nstart -- ) gforth paren_question_do
310 : anton 1.1 *--rp = nlimit;
311 :     *--rp = nstart;
312 :     if (nstart == nlimit) {
313 :     IF_TOS(TOS = sp[0]);
314 :     goto branch;
315 :     }
316 :     else {
317 :     INC_IP(1);
318 :     }
319 :     :
320 :     2dup =
321 :     IF r> swap rot >r >r
322 :     dup @ + >r
323 :     ELSE r> swap rot >r >r
324 :     cell+ >r
325 :     THEN ; \ --> CORE-EXT
326 :    
327 : pazsan 1.15 \+xconds
328 : anton 1.1
329 : anton 1.47 (+do) ( nlimit nstart -- ) gforth paren_plus_do
330 : anton 1.1 *--rp = nlimit;
331 :     *--rp = nstart;
332 :     if (nstart >= nlimit) {
333 :     IF_TOS(TOS = sp[0]);
334 :     goto branch;
335 :     }
336 :     else {
337 :     INC_IP(1);
338 :     }
339 :     :
340 :     swap 2dup
341 :     r> swap >r swap >r
342 :     >=
343 :     IF
344 :     dup @ +
345 :     ELSE
346 :     cell+
347 :     THEN >r ;
348 :    
349 : anton 1.47 (u+do) ( ulimit ustart -- ) gforth paren_u_plus_do
350 : anton 1.1 *--rp = ulimit;
351 :     *--rp = ustart;
352 :     if (ustart >= ulimit) {
353 :     IF_TOS(TOS = sp[0]);
354 :     goto branch;
355 :     }
356 :     else {
357 :     INC_IP(1);
358 :     }
359 :     :
360 :     swap 2dup
361 :     r> swap >r swap >r
362 :     u>=
363 :     IF
364 :     dup @ +
365 :     ELSE
366 :     cell+
367 :     THEN >r ;
368 :    
369 : anton 1.47 (-do) ( nlimit nstart -- ) gforth paren_minus_do
370 : anton 1.1 *--rp = nlimit;
371 :     *--rp = nstart;
372 :     if (nstart <= nlimit) {
373 :     IF_TOS(TOS = sp[0]);
374 :     goto branch;
375 :     }
376 :     else {
377 :     INC_IP(1);
378 :     }
379 :     :
380 :     swap 2dup
381 :     r> swap >r swap >r
382 :     <=
383 :     IF
384 :     dup @ +
385 :     ELSE
386 :     cell+
387 :     THEN >r ;
388 :    
389 : anton 1.47 (u-do) ( ulimit ustart -- ) gforth paren_u_minus_do
390 : anton 1.1 *--rp = ulimit;
391 :     *--rp = ustart;
392 :     if (ustart <= ulimit) {
393 :     IF_TOS(TOS = sp[0]);
394 :     goto branch;
395 :     }
396 :     else {
397 :     INC_IP(1);
398 :     }
399 :     :
400 :     swap 2dup
401 :     r> swap >r swap >r
402 :     u<=
403 :     IF
404 :     dup @ +
405 :     ELSE
406 :     cell+
407 :     THEN >r ;
408 :    
409 : pazsan 1.15 \+
410 : anton 1.1
411 : jwilke 1.5 \ don't make any assumptions where the return stack is!!
412 :     \ implement this in machine code if it should run quickly!
413 :    
414 : anton 1.47 i ( -- n ) core
415 : anton 1.1 n = *rp;
416 :     :
417 : jwilke 1.5 \ rp@ cell+ @ ;
418 :     r> r> tuck >r >r ;
419 : anton 1.1
420 : anton 1.47 i' ( -- w ) gforth i_tick
421 : anton 1.1 ""loop end value""
422 :     w = rp[1];
423 :     :
424 : jwilke 1.5 \ rp@ cell+ cell+ @ ;
425 :     r> r> r> dup itmp ! >r >r >r itmp @ ;
426 :     variable itmp
427 : anton 1.1
428 : anton 1.47 j ( -- n ) core
429 : anton 1.1 n = rp[2];
430 :     :
431 : jwilke 1.5 \ rp@ cell+ cell+ cell+ @ ;
432 :     r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
433 :     [IFUNDEF] itmp variable itmp [THEN]
434 : anton 1.1
435 : anton 1.47 k ( -- n ) gforth
436 : anton 1.1 n = rp[4];
437 :     :
438 : jwilke 1.5 \ rp@ [ 5 cells ] Literal + @ ;
439 :     r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
440 :     [IFUNDEF] itmp variable itmp [THEN]
441 : jwilke 1.31
442 :     \f[THEN]
443 : anton 1.1
444 :     \ digit is high-level: 0/0%
445 :    
446 : anton 1.47 move ( c_from c_to ucount -- ) core
447 : anton 1.33 ""Copy the contents of @i{ucount} address units at @i{c-from} to
448 :     @i{c-to}. @code{move} works correctly even if the two areas overlap.""
449 : anton 1.1 memmove(c_to,c_from,ucount);
450 :     /* make an Ifdef for bsd and others? */
451 :     :
452 :     >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
453 :    
454 : anton 1.47 cmove ( c_from c_to u -- ) string c_move
455 : anton 1.33 ""Copy the contents of @i{ucount} characters from data space at
456 :     @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
457 :     from low address to high address; i.e., for overlapping areas it is
458 :     safe if @i{c-to}=<@i{c-from}.""
459 : anton 1.1 while (u-- > 0)
460 :     *c_to++ = *c_from++;
461 :     :
462 :     bounds ?DO dup c@ I c! 1+ LOOP drop ;
463 :    
464 : anton 1.47 cmove> ( c_from c_to u -- ) string c_move_up
465 : anton 1.33 ""Copy the contents of @i{ucount} characters from data space at
466 :     @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
467 :     from high address to low address; i.e., for overlapping areas it is
468 :     safe if @i{c-to}>=@i{c-from}.""
469 : anton 1.1 while (u-- > 0)
470 :     c_to[u] = c_from[u];
471 :     :
472 :     dup 0= IF drop 2drop exit THEN
473 :     rot over + -rot bounds swap 1-
474 :     DO 1- dup c@ I c! -1 +LOOP drop ;
475 :    
476 : anton 1.47 fill ( c_addr u c -- ) core
477 : crook 1.29 "" If @i{u}>0, store character @i{c} in each of @i{u} consecutive
478 :     @code{char} addresses in memory, starting at address @i{c-addr}.""
479 : anton 1.1 memset(c_addr,c,u);
480 :     :
481 :     -rot bounds
482 :     ?DO dup I c! LOOP drop ;
483 :    
484 : anton 1.47 compare ( c_addr1 u1 c_addr2 u2 -- n ) string
485 : crook 1.29 ""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
486 :     the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
487 : anton 1.1 is 1. Currently this is based on the machine's character
488 : crook 1.26 comparison. In the future, this may change to consider the current
489 : anton 1.1 locale and its collation order.""
490 : pazsan 1.46 /* close ' to keep fontify happy */
491 : anton 1.1 n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
492 :     if (n==0)
493 :     n = u1-u2;
494 :     if (n<0)
495 :     n = -1;
496 :     else if (n>0)
497 :     n = 1;
498 :     :
499 : pazsan 1.43 rot 2dup swap - >r min swap -text dup
500 :     IF rdrop ELSE drop r> sgn THEN ;
501 :     : sgn ( n -- -1/0/1 )
502 :     dup 0= IF EXIT THEN 0< 2* 1+ ;
503 : anton 1.1
504 : anton 1.47 -text ( c_addr1 u c_addr2 -- n ) new dash_text
505 : anton 1.1 n = memcmp(c_addr1, c_addr2, u);
506 :     if (n<0)
507 :     n = -1;
508 :     else if (n>0)
509 :     n = 1;
510 :     :
511 :     swap bounds
512 :     ?DO dup c@ I c@ = WHILE 1+ LOOP drop 0
513 :     ELSE c@ I c@ - unloop THEN -text-flag ;
514 : pazsan 1.43 : sgn ( n -- -1/0/1 )
515 :     dup 0= IF EXIT THEN 0< 2* 1+ ;
516 : anton 1.1
517 : anton 1.47 toupper ( c1 -- c2 ) gforth
518 : crook 1.29 ""If @i{c1} is a lower-case character (in the current locale), @i{c2}
519 : anton 1.25 is the equivalent upper-case character. All other characters are unchanged.""
520 : anton 1.1 c2 = toupper(c1);
521 :     :
522 :     dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
523 :    
524 : anton 1.47 capscomp ( c_addr1 u c_addr2 -- n ) new
525 : anton 1.1 n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
526 :     if (n<0)
527 :     n = -1;
528 :     else if (n>0)
529 :     n = 1;
530 :     :
531 :     swap bounds
532 :     ?DO dup c@ I c@ <>
533 :     IF dup c@ toupper I c@ toupper =
534 :     ELSE true THEN WHILE 1+ LOOP drop 0
535 :     ELSE c@ toupper I c@ toupper - unloop THEN -text-flag ;
536 :    
537 : anton 1.47 -trailing ( c_addr u1 -- c_addr u2 ) string dash_trailing
538 : crook 1.29 ""Adjust the string specified by @i{c-addr, u1} to remove all trailing
539 :     spaces. @i{u2} is the length of the modified string.""
540 : anton 1.1 u2 = u1;
541 : anton 1.4 while (u2>0 && c_addr[u2-1] == ' ')
542 : anton 1.1 u2--;
543 :     :
544 :     BEGIN 1- 2dup + c@ bl = WHILE
545 :     dup 0= UNTIL ELSE 1+ THEN ;
546 :    
547 : anton 1.47 /string ( c_addr1 u1 n -- c_addr2 u2 ) string slash_string
548 : crook 1.29 ""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
549 : crook 1.27 characters from the start of the string.""
550 : anton 1.1 c_addr2 = c_addr1+n;
551 :     u2 = u1-n;
552 :     :
553 :     tuck - >r + r> dup 0< IF - 0 THEN ;
554 :    
555 : anton 1.47 + ( n1 n2 -- n ) core plus
556 : anton 1.1 n = n1+n2;
557 :    
558 :     \ PFE-0.9.14 has it differently, but the next release will have it as follows
559 : anton 1.47 under+ ( n1 n2 n3 -- n n2 ) gforth under_plus
560 : crook 1.29 ""add @i{n3} to @i{n1} (giving @i{n})""
561 : anton 1.1 n = n1+n3;
562 :     :
563 :     rot + swap ;
564 :    
565 : anton 1.47 - ( n1 n2 -- n ) core minus
566 : anton 1.1 n = n1-n2;
567 :     :
568 :     negate + ;
569 :    
570 : anton 1.47 negate ( n1 -- n2 ) core
571 : anton 1.1 /* use minus as alias */
572 :     n2 = -n1;
573 :     :
574 :     invert 1+ ;
575 :    
576 : anton 1.47 1+ ( n1 -- n2 ) core one_plus
577 : anton 1.1 n2 = n1+1;
578 :     :
579 :     1 + ;
580 :    
581 : anton 1.47 1- ( n1 -- n2 ) core one_minus
582 : anton 1.1 n2 = n1-1;
583 :     :
584 :     1 - ;
585 :    
586 : anton 1.47 max ( n1 n2 -- n ) core
587 : anton 1.1 if (n1<n2)
588 :     n = n2;
589 :     else
590 :     n = n1;
591 :     :
592 :     2dup < IF swap THEN drop ;
593 :    
594 : anton 1.47 min ( n1 n2 -- n ) core
595 : anton 1.1 if (n1<n2)
596 :     n = n1;
597 :     else
598 :     n = n2;
599 :     :
600 :     2dup > IF swap THEN drop ;
601 :    
602 : anton 1.47 abs ( n1 -- n2 ) core
603 : anton 1.1 if (n1<0)
604 :     n2 = -n1;
605 :     else
606 :     n2 = n1;
607 :     :
608 :     dup 0< IF negate THEN ;
609 :    
610 : anton 1.47 * ( n1 n2 -- n ) core star
611 : anton 1.1 n = n1*n2;
612 :     :
613 :     um* drop ;
614 :    
615 : anton 1.47 / ( n1 n2 -- n ) core slash
616 : anton 1.1 n = n1/n2;
617 :     :
618 :     /mod nip ;
619 :    
620 : anton 1.47 mod ( n1 n2 -- n ) core
621 : anton 1.1 n = n1%n2;
622 :     :
623 :     /mod drop ;
624 :    
625 : anton 1.47 /mod ( n1 n2 -- n3 n4 ) core slash_mod
626 : anton 1.1 n4 = n1/n2;
627 :     n3 = n1%n2; /* !! is this correct? look into C standard! */
628 :     :
629 :     >r s>d r> fm/mod ;
630 :    
631 : anton 1.47 2* ( n1 -- n2 ) core two_star
632 : anton 1.1 n2 = 2*n1;
633 :     :
634 :     dup + ;
635 :    
636 : anton 1.47 2/ ( n1 -- n2 ) core two_slash
637 : anton 1.1 /* !! is this still correct? */
638 :     n2 = n1>>1;
639 :     :
640 :     dup MINI and IF 1 ELSE 0 THEN
641 :     [ bits/byte cell * 1- ] literal
642 : jwilke 1.5 0 DO 2* swap dup 2* >r MINI and
643 : anton 1.1 IF 1 ELSE 0 THEN or r> swap
644 :     LOOP nip ;
645 :    
646 : anton 1.47 fm/mod ( d1 n1 -- n2 n3 ) core f_m_slash_mod
647 : crook 1.29 ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}.""
648 : anton 1.1 #ifdef BUGGY_LONG_LONG
649 :     DCell r = fmdiv(d1,n1);
650 :     n2=r.hi;
651 :     n3=r.lo;
652 :     #else
653 :     /* assumes that the processor uses either floored or symmetric division */
654 :     n3 = d1/n1;
655 :     n2 = d1%n1;
656 :     /* note that this 1%-3>0 is optimized by the compiler */
657 :     if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
658 :     n3--;
659 :     n2+=n1;
660 :     }
661 :     #endif
662 :     :
663 :     dup >r dup 0< IF negate >r dnegate r> THEN
664 :     over 0< IF tuck + swap THEN
665 :     um/mod
666 :     r> 0< IF swap negate swap THEN ;
667 :    
668 : anton 1.47 sm/rem ( d1 n1 -- n2 n3 ) core s_m_slash_rem
669 : crook 1.29 ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0.""
670 : anton 1.1 #ifdef BUGGY_LONG_LONG
671 :     DCell r = smdiv(d1,n1);
672 :     n2=r.hi;
673 :     n3=r.lo;
674 :     #else
675 :     /* assumes that the processor uses either floored or symmetric division */
676 :     n3 = d1/n1;
677 :     n2 = d1%n1;
678 :     /* note that this 1%-3<0 is optimized by the compiler */
679 :     if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
680 :     n3++;
681 :     n2-=n1;
682 :     }
683 :     #endif
684 :     :
685 :     over >r dup >r abs -rot
686 :     dabs rot um/mod
687 :     r> r@ xor 0< IF negate THEN
688 :     r> 0< IF swap negate swap THEN ;
689 :    
690 : anton 1.47 m* ( n1 n2 -- d ) core m_star
691 : anton 1.1 #ifdef BUGGY_LONG_LONG
692 :     d = mmul(n1,n2);
693 :     #else
694 :     d = (DCell)n1 * (DCell)n2;
695 :     #endif
696 :     :
697 :     2dup 0< and >r
698 :     2dup swap 0< and >r
699 :     um* r> - r> - ;
700 :    
701 : anton 1.47 um* ( u1 u2 -- ud ) core u_m_star
702 : anton 1.1 /* use u* as alias */
703 :     #ifdef BUGGY_LONG_LONG
704 :     ud = ummul(u1,u2);
705 :     #else
706 :     ud = (UDCell)u1 * (UDCell)u2;
707 :     #endif
708 :     :
709 :     >r >r 0 0 r> r> [ 8 cells ] literal 0
710 :     DO
711 :     over >r dup >r 0< and d2*+ drop
712 :     r> 2* r> swap
713 :     LOOP 2drop ;
714 :     : d2*+ ( ud n -- ud+n c )
715 :     over MINI
716 :     and >r >r 2dup d+ swap r> + swap r> ;
717 :    
718 : anton 1.47 um/mod ( ud u1 -- u2 u3 ) core u_m_slash_mod
719 : anton 1.32 ""ud=u3*u1+u2, u1>u2>=0""
720 : anton 1.1 #ifdef BUGGY_LONG_LONG
721 :     UDCell r = umdiv(ud,u1);
722 :     u2=r.hi;
723 :     u3=r.lo;
724 :     #else
725 :     u3 = ud/u1;
726 :     u2 = ud%u1;
727 :     #endif
728 :     :
729 :     0 swap [ 8 cells 1 + ] literal 0
730 : jwilke 1.5 ?DO /modstep
731 : anton 1.1 LOOP drop swap 1 rshift or swap ;
732 :     : /modstep ( ud c R: u -- ud-?u c R: u )
733 : jwilke 1.5 >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN d2*+ r> ;
734 : anton 1.1 : d2*+ ( ud n -- ud+n c )
735 :     over MINI
736 :     and >r >r 2dup d+ swap r> + swap r> ;
737 :    
738 : anton 1.47 m+ ( d1 n -- d2 ) double m_plus
739 : anton 1.1 #ifdef BUGGY_LONG_LONG
740 :     d2.lo = d1.lo+n;
741 :     d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
742 :     #else
743 :     d2 = d1+n;
744 :     #endif
745 :     :
746 :     s>d d+ ;
747 :    
748 : anton 1.47 d+ ( d1 d2 -- d ) double d_plus
749 : anton 1.1 #ifdef BUGGY_LONG_LONG
750 :     d.lo = d1.lo+d2.lo;
751 :     d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
752 :     #else
753 :     d = d1+d2;
754 :     #endif
755 :     :
756 :     rot + >r tuck + swap over u> r> swap - ;
757 :    
758 : anton 1.47 d- ( d1 d2 -- d ) double d_minus
759 : anton 1.1 #ifdef BUGGY_LONG_LONG
760 :     d.lo = d1.lo - d2.lo;
761 :     d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
762 :     #else
763 :     d = d1-d2;
764 :     #endif
765 :     :
766 :     dnegate d+ ;
767 :    
768 : anton 1.47 dnegate ( d1 -- d2 ) double d_negate
769 : anton 1.1 /* use dminus as alias */
770 :     #ifdef BUGGY_LONG_LONG
771 :     d2 = dnegate(d1);
772 :     #else
773 :     d2 = -d1;
774 :     #endif
775 :     :
776 :     invert swap negate tuck 0= - ;
777 :    
778 : anton 1.47 d2* ( d1 -- d2 ) double d_two_star
779 : anton 1.1 #ifdef BUGGY_LONG_LONG
780 :     d2.lo = d1.lo<<1;
781 :     d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
782 :     #else
783 :     d2 = 2*d1;
784 :     #endif
785 :     :
786 :     2dup d+ ;
787 :    
788 : anton 1.47 d2/ ( d1 -- d2 ) double d_two_slash
789 : anton 1.1 #ifdef BUGGY_LONG_LONG
790 :     d2.hi = d1.hi>>1;
791 :     d2.lo= (d1.lo>>1) | (d1.hi<<(CELL_BITS-1));
792 :     #else
793 :     d2 = d1>>1;
794 :     #endif
795 :     :
796 :     dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
797 :     r> IF [ 1 8 cells 1- lshift ] Literal + THEN swap ;
798 :    
799 : anton 1.47 and ( w1 w2 -- w ) core
800 : anton 1.1 w = w1&w2;
801 :    
802 : anton 1.47 or ( w1 w2 -- w ) core
803 : anton 1.1 w = w1|w2;
804 :     :
805 :     invert swap invert and invert ;
806 :    
807 : anton 1.47 xor ( w1 w2 -- w ) core x_or
808 : anton 1.1 w = w1^w2;
809 :    
810 : anton 1.47 invert ( w1 -- w2 ) core
811 : anton 1.1 w2 = ~w1;
812 :     :
813 :     MAXU xor ;
814 :    
815 : anton 1.47 rshift ( u1 n -- u2 ) core r_shift
816 : anton 1.1 u2 = u1>>n;
817 :     :
818 :     0 ?DO 2/ MAXI and LOOP ;
819 :    
820 : anton 1.47 lshift ( u1 n -- u2 ) core l_shift
821 : anton 1.1 u2 = u1<<n;
822 :     :
823 :     0 ?DO 2* LOOP ;
824 :    
825 :     \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
826 :     define(comparisons,
827 : anton 1.47 $1= ( $2 -- f ) $6 $3equals
828 : anton 1.1 f = FLAG($4==$5);
829 :     :
830 :     [ char $1x char 0 = [IF]
831 :     ] IF false ELSE true THEN [
832 :     [ELSE]
833 :     ] xor 0= [
834 :     [THEN] ] ;
835 :    
836 : anton 1.47 $1<> ( $2 -- f ) $7 $3not_equals
837 : anton 1.1 f = FLAG($4!=$5);
838 :     :
839 :     [ char $1x char 0 = [IF]
840 :     ] IF true ELSE false THEN [
841 :     [ELSE]
842 :     ] xor 0<> [
843 :     [THEN] ] ;
844 :    
845 : anton 1.47 $1< ( $2 -- f ) $8 $3less_than
846 : anton 1.1 f = FLAG($4<$5);
847 :     :
848 :     [ char $1x char 0 = [IF]
849 :     ] MINI and 0<> [
850 :     [ELSE] char $1x char u = [IF]
851 :     ] 2dup xor 0< IF nip ELSE - THEN 0< [
852 :     [ELSE]
853 :     ] MINI xor >r MINI xor r> u< [
854 :     [THEN]
855 :     [THEN] ] ;
856 :    
857 : anton 1.47 $1> ( $2 -- f ) $9 $3greater_than
858 : anton 1.1 f = FLAG($4>$5);
859 :     :
860 :     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
861 :     $1< ;
862 :    
863 : anton 1.47 $1<= ( $2 -- f ) gforth $3less_or_equal
864 : anton 1.1 f = FLAG($4<=$5);
865 :     :
866 :     $1> 0= ;
867 :    
868 : anton 1.47 $1>= ( $2 -- f ) gforth $3greater_or_equal
869 : anton 1.1 f = FLAG($4>=$5);
870 :     :
871 :     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
872 :     $1<= ;
873 :    
874 :     )
875 :    
876 :     comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
877 :     comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
878 :     comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
879 :    
880 :     \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
881 :     define(dcomparisons,
882 : anton 1.47 $1= ( $2 -- f ) $6 $3equals
883 : anton 1.1 #ifdef BUGGY_LONG_LONG
884 :     f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
885 :     #else
886 :     f = FLAG($4==$5);
887 :     #endif
888 :    
889 : anton 1.47 $1<> ( $2 -- f ) $7 $3not_equals
890 : anton 1.1 #ifdef BUGGY_LONG_LONG
891 :     f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
892 :     #else
893 :     f = FLAG($4!=$5);
894 :     #endif
895 :    
896 : anton 1.47 $1< ( $2 -- f ) $8 $3less_than
897 : anton 1.1 #ifdef BUGGY_LONG_LONG
898 :     f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
899 :     #else
900 :     f = FLAG($4<$5);
901 :     #endif
902 :    
903 : anton 1.47 $1> ( $2 -- f ) $9 $3greater_than
904 : anton 1.1 #ifdef BUGGY_LONG_LONG
905 :     f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
906 :     #else
907 :     f = FLAG($4>$5);
908 :     #endif
909 :    
910 : anton 1.47 $1<= ( $2 -- f ) gforth $3less_or_equal
911 : anton 1.1 #ifdef BUGGY_LONG_LONG
912 :     f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
913 :     #else
914 :     f = FLAG($4<=$5);
915 :     #endif
916 :    
917 : anton 1.47 $1>= ( $2 -- f ) gforth $3greater_or_equal
918 : anton 1.1 #ifdef BUGGY_LONG_LONG
919 :     f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
920 :     #else
921 :     f = FLAG($4>=$5);
922 :     #endif
923 :    
924 :     )
925 :    
926 : pazsan 1.15 \+dcomps
927 : anton 1.1
928 :     dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
929 :     dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
930 :     dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
931 :    
932 : pazsan 1.15 \+
933 : anton 1.1
934 : anton 1.47 within ( u1 u2 u3 -- f ) core-ext
935 : anton 1.32 ""u2=<u1<u3 or: u3=<u2 and u1 is not in [u3,u2). This works for
936 :     unsigned and signed numbers (but not a mixture). Another way to think
937 :     about this word is to consider the numbers as a circle (wrapping
938 :     around from @code{max-u} to 0 for unsigned, and from @code{max-n} to
939 :     min-n for signed numbers); now consider the range from u2 towards
940 :     increasing numbers up to and excluding u3 (giving an empty range if
941 :     u2=u3; if u1 is in this range, @code{within} returns true.""
942 : anton 1.1 f = FLAG(u1-u2 < u3-u2);
943 :     :
944 :     over - >r - r> u< ;
945 :    
946 : anton 1.47 sp@ ( -- a_addr ) gforth sp_fetch
947 : anton 1.1 a_addr = sp+1;
948 :    
949 : anton 1.47 sp! ( a_addr -- ) gforth sp_store
950 : anton 1.1 sp = a_addr;
951 :     /* works with and without TOS caching */
952 :    
953 : anton 1.47 rp@ ( -- a_addr ) gforth rp_fetch
954 : anton 1.1 a_addr = rp;
955 :    
956 : anton 1.47 rp! ( a_addr -- ) gforth rp_store
957 : anton 1.1 rp = a_addr;
958 :    
959 : pazsan 1.15 \+floating
960 : anton 1.1
961 : anton 1.47 fp@ ( -- f_addr ) gforth fp_fetch
962 : anton 1.1 f_addr = fp;
963 :    
964 : anton 1.47 fp! ( f_addr -- ) gforth fp_store
965 : anton 1.1 fp = f_addr;
966 :    
967 : pazsan 1.15 \+
968 : anton 1.1
969 : anton 1.47 ;s ( -- ) gforth semis
970 : crook 1.22 ""The primitive compiled by @code{EXIT}.""
971 : anton 1.23 SET_IP((Xt *)(*rp++));
972 : anton 1.1
973 : anton 1.47 >r ( w -- ) core to_r
974 : anton 1.1 *--rp = w;
975 :     :
976 :     (>r) ;
977 :     : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
978 :    
979 : anton 1.47 r> ( -- w ) core r_from
980 : anton 1.1 w = *rp++;
981 :     :
982 :     rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
983 :     Create (rdrop) ' ;s A,
984 :    
985 : anton 1.47 rdrop ( -- ) gforth
986 : anton 1.1 rp++;
987 :     :
988 :     r> r> drop >r ;
989 :    
990 : anton 1.47 2>r ( w1 w2 -- ) core-ext two_to_r
991 : anton 1.1 *--rp = w1;
992 :     *--rp = w2;
993 :     :
994 :     swap r> swap >r swap >r >r ;
995 :    
996 : anton 1.47 2r> ( -- w1 w2 ) core-ext two_r_from
997 : anton 1.1 w2 = *rp++;
998 :     w1 = *rp++;
999 :     :
1000 :     r> r> swap r> swap >r swap ;
1001 :    
1002 : anton 1.47 2r@ ( -- w1 w2 ) core-ext two_r_fetch
1003 : anton 1.1 w2 = rp[0];
1004 :     w1 = rp[1];
1005 :     :
1006 :     i' j ;
1007 :    
1008 : anton 1.47 2rdrop ( -- ) gforth two_r_drop
1009 : anton 1.1 rp+=2;
1010 :     :
1011 :     r> r> drop r> drop >r ;
1012 :    
1013 : anton 1.47 over ( w1 w2 -- w1 w2 w1 ) core
1014 : anton 1.1 :
1015 :     sp@ cell+ @ ;
1016 :    
1017 : anton 1.47 drop ( w -- ) core
1018 : anton 1.1 :
1019 :     IF THEN ;
1020 :    
1021 : anton 1.47 swap ( w1 w2 -- w2 w1 ) core
1022 : anton 1.1 :
1023 :     >r (swap) ! r> (swap) @ ;
1024 :     Variable (swap)
1025 :    
1026 : anton 1.47 dup ( w -- w w ) core dupe
1027 : anton 1.1 :
1028 :     sp@ @ ;
1029 :    
1030 : anton 1.47 rot ( w1 w2 w3 -- w2 w3 w1 ) core rote
1031 : anton 1.1 :
1032 :     [ defined? (swap) [IF] ]
1033 :     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
1034 :     Variable (rot)
1035 :     [ELSE] ]
1036 :     >r swap r> swap ;
1037 :     [THEN]
1038 :    
1039 : anton 1.47 -rot ( w1 w2 w3 -- w3 w1 w2 ) gforth not_rote
1040 : anton 1.1 :
1041 :     rot rot ;
1042 :    
1043 : anton 1.47 nip ( w1 w2 -- w2 ) core-ext
1044 : anton 1.1 :
1045 : jwilke 1.6 swap drop ;
1046 : anton 1.1
1047 : anton 1.47 tuck ( w1 w2 -- w2 w1 w2 ) core-ext
1048 : anton 1.1 :
1049 :     swap over ;
1050 :    
1051 : anton 1.47 ?dup ( w -- w ) core question_dupe
1052 : anton 1.1 if (w!=0) {
1053 :     IF_TOS(*sp-- = w;)
1054 :     #ifndef USE_TOS
1055 :     *--sp = w;
1056 :     #endif
1057 :     }
1058 :     :
1059 :     dup IF dup THEN ;
1060 :    
1061 : anton 1.47 pick ( u -- w ) core-ext
1062 : anton 1.1 w = sp[u+1];
1063 :     :
1064 :     1+ cells sp@ + @ ;
1065 :    
1066 : anton 1.47 2drop ( w1 w2 -- ) core two_drop
1067 : anton 1.1 :
1068 :     drop drop ;
1069 :    
1070 : anton 1.47 2dup ( w1 w2 -- w1 w2 w1 w2 ) core two_dupe
1071 : anton 1.1 :
1072 :     over over ;
1073 :    
1074 : anton 1.47 2over ( w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 ) core two_over
1075 : anton 1.1 :
1076 :     3 pick 3 pick ;
1077 :    
1078 : anton 1.47 2swap ( w1 w2 w3 w4 -- w3 w4 w1 w2 ) core two_swap
1079 : anton 1.1 :
1080 :     rot >r rot r> ;
1081 :    
1082 : anton 1.47 2rot ( w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 ) double-ext two_rote
1083 : anton 1.1 :
1084 :     >r >r 2swap r> r> 2swap ;
1085 :    
1086 : anton 1.47 2nip ( w1 w2 w3 w4 -- w3 w4 ) gforth two_nip
1087 : anton 1.1 :
1088 :     2swap 2drop ;
1089 :    
1090 : anton 1.47 2tuck ( w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 ) gforth two_tuck
1091 : anton 1.1 :
1092 :     2swap 2over ;
1093 :    
1094 :     \ toggle is high-level: 0.11/0.42%
1095 :    
1096 : anton 1.47 @ ( a_addr -- w ) core fetch
1097 : crook 1.29 "" Read from the cell at address @i{a-addr}, and return its contents, @i{w}.""
1098 : anton 1.1 w = *a_addr;
1099 :    
1100 : anton 1.47 ! ( w a_addr -- ) core store
1101 : crook 1.29 "" Write the value @i{w} to the cell at address @i{a-addr}.""
1102 : anton 1.1 *a_addr = w;
1103 :    
1104 : anton 1.47 +! ( n a_addr -- ) core plus_store
1105 : crook 1.29 "" Add @i{n} to the value stored in the cell at address @i{a-addr}.""
1106 : anton 1.1 *a_addr += n;
1107 :     :
1108 :     tuck @ + swap ! ;
1109 :    
1110 : anton 1.47 c@ ( c_addr -- c ) core c_fetch
1111 : crook 1.29 "" Read from the char at address @i{c-addr}, and return its contents, @i{c}.""
1112 : anton 1.1 c = *c_addr;
1113 :     :
1114 :     [ bigendian [IF] ]
1115 :     [ cell>bit 4 = [IF] ]
1116 :     dup [ 0 cell - ] Literal and @ swap 1 and
1117 :     IF $FF and ELSE 8>> THEN ;
1118 :     [ [ELSE] ]
1119 :     dup [ cell 1- ] literal and
1120 :     tuck - @ swap [ cell 1- ] literal xor
1121 :     0 ?DO 8>> LOOP $FF and
1122 :     [ [THEN] ]
1123 :     [ [ELSE] ]
1124 :     [ cell>bit 4 = [IF] ]
1125 :     dup [ 0 cell - ] Literal and @ swap 1 and
1126 :     IF 8>> ELSE $FF and THEN
1127 :     [ [ELSE] ]
1128 :     dup [ cell 1- ] literal and
1129 :     tuck - @ swap
1130 :     0 ?DO 8>> LOOP 255 and
1131 :     [ [THEN] ]
1132 :     [ [THEN] ]
1133 :     ;
1134 :     : 8>> 2/ 2/ 2/ 2/ 2/ 2/ 2/ 2/ ;
1135 :    
1136 : anton 1.47 c! ( c c_addr -- ) core c_store
1137 : crook 1.29 "" Write the value @i{c} to the char at address @i{c-addr}.""
1138 : anton 1.1 *c_addr = c;
1139 :     :
1140 :     [ bigendian [IF] ]
1141 :     [ cell>bit 4 = [IF] ]
1142 :     tuck 1 and IF $FF and ELSE 8<< THEN >r
1143 :     dup -2 and @ over 1 and cells masks + @ and
1144 :     r> or swap -2 and ! ;
1145 :     Create masks $00FF , $FF00 ,
1146 :     [ELSE] ]
1147 :     dup [ cell 1- ] literal and dup
1148 :     [ cell 1- ] literal xor >r
1149 :     - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1150 :     rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1151 :     [THEN]
1152 :     [ELSE] ]
1153 :     [ cell>bit 4 = [IF] ]
1154 :     tuck 1 and IF 8<< ELSE $FF and THEN >r
1155 :     dup -2 and @ over 1 and cells masks + @ and
1156 :     r> or swap -2 and ! ;
1157 :     Create masks $FF00 , $00FF ,
1158 :     [ELSE] ]
1159 :     dup [ cell 1- ] literal and dup >r
1160 :     - dup @ $FF r@ 0 ?DO 8<< LOOP invert and
1161 :     rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
1162 :     [THEN]
1163 :     [THEN]
1164 :     : 8<< 2* 2* 2* 2* 2* 2* 2* 2* ;
1165 :    
1166 : anton 1.47 2! ( w1 w2 a_addr -- ) core two_store
1167 : crook 1.29 "" Write the value @i{w1, w2} to the double at address @i{a-addr}.""
1168 : anton 1.1 a_addr[0] = w2;
1169 :     a_addr[1] = w1;
1170 :     :
1171 :     tuck ! cell+ ! ;
1172 :    
1173 : anton 1.47 2@ ( a_addr -- w1 w2 ) core two_fetch
1174 : crook 1.29 "" Read from the double at address @i{a-addr}, and return its contents, @i{w1, w2}.""
1175 : anton 1.1 w2 = a_addr[0];
1176 :     w1 = a_addr[1];
1177 :     :
1178 :     dup cell+ @ swap @ ;
1179 :    
1180 : anton 1.47 cell+ ( a_addr1 -- a_addr2 ) core cell_plus
1181 : crook 1.29 "" Increment @i{a-addr1} by the number of address units corresponding to the size of
1182 :     one cell, to give @i{a-addr2}.""
1183 : anton 1.1 a_addr2 = a_addr1+1;
1184 :     :
1185 :     cell + ;
1186 :    
1187 : anton 1.47 cells ( n1 -- n2 ) core
1188 : crook 1.29 "" @i{n2} is the number of address units corresponding to @i{n1} cells.""
1189 : anton 1.1 n2 = n1 * sizeof(Cell);
1190 :     :
1191 :     [ cell
1192 :     2/ dup [IF] ] 2* [ [THEN]
1193 :     2/ dup [IF] ] 2* [ [THEN]
1194 :     2/ dup [IF] ] 2* [ [THEN]
1195 :     2/ dup [IF] ] 2* [ [THEN]
1196 :     drop ] ;
1197 :    
1198 : anton 1.47 char+ ( c_addr1 -- c_addr2 ) core char_plus
1199 : crook 1.29 "" Increment @i{c-addr1} by the number of address units corresponding to the size of
1200 :     one char, to give @i{c-addr2}.""
1201 : anton 1.1 c_addr2 = c_addr1 + 1;
1202 :     :
1203 :     1+ ;
1204 :    
1205 : anton 1.47 (chars) ( n1 -- n2 ) gforth paren_chars
1206 : anton 1.1 n2 = n1 * sizeof(Char);
1207 :     :
1208 :     ;
1209 :    
1210 : anton 1.47 count ( c_addr1 -- c_addr2 u ) core
1211 : crook 1.29 "" If @i{c-add1} is the address of a counted string return the length of
1212 :     the string, @i{u}, and the address of its first character, @i{c-addr2}.""
1213 : anton 1.1 u = *c_addr1;
1214 :     c_addr2 = c_addr1+1;
1215 :     :
1216 :     dup 1+ swap c@ ;
1217 :    
1218 : anton 1.47 (f83find) ( c_addr u f83name1 -- f83name2 ) new paren_f83find
1219 : pazsan 1.13 for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
1220 : anton 1.1 if ((UCell)F83NAME_COUNT(f83name1)==u &&
1221 :     memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1222 :     break;
1223 :     f83name2=f83name1;
1224 :     :
1225 :     BEGIN dup WHILE (find-samelen) dup WHILE
1226 :     >r 2dup r@ cell+ char+ capscomp 0=
1227 :     IF 2drop r> EXIT THEN
1228 :     r> @
1229 :     REPEAT THEN nip nip ;
1230 :     : (find-samelen) ( u f83name1 -- u f83name2/0 )
1231 :     BEGIN 2dup cell+ c@ $1F and <> WHILE @ dup 0= UNTIL THEN ;
1232 :    
1233 : pazsan 1.15 \+hash
1234 : anton 1.1
1235 : anton 1.47 (hashfind) ( c_addr u a_addr -- f83name2 ) new paren_hashfind
1236 : pazsan 1.13 struct F83Name *f83name1;
1237 : anton 1.1 f83name2=NULL;
1238 :     while(a_addr != NULL)
1239 :     {
1240 : pazsan 1.13 f83name1=(struct F83Name *)(a_addr[1]);
1241 : anton 1.1 a_addr=(Cell *)(a_addr[0]);
1242 :     if ((UCell)F83NAME_COUNT(f83name1)==u &&
1243 :     memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1244 :     {
1245 :     f83name2=f83name1;
1246 :     break;
1247 :     }
1248 :     }
1249 :     :
1250 :     BEGIN dup WHILE
1251 :     2@ >r >r dup r@ cell+ c@ $1F and =
1252 :     IF 2dup r@ cell+ char+ capscomp 0=
1253 :     IF 2drop r> rdrop EXIT THEN THEN
1254 :     rdrop r>
1255 :     REPEAT nip nip ;
1256 :    
1257 : anton 1.47 (tablefind) ( c_addr u a_addr -- f83name2 ) new paren_tablefind
1258 : anton 1.1 ""A case-sensitive variant of @code{(hashfind)}""
1259 : pazsan 1.13 struct F83Name *f83name1;
1260 : anton 1.1 f83name2=NULL;
1261 :     while(a_addr != NULL)
1262 :     {
1263 : pazsan 1.13 f83name1=(struct F83Name *)(a_addr[1]);
1264 : anton 1.1 a_addr=(Cell *)(a_addr[0]);
1265 :     if ((UCell)F83NAME_COUNT(f83name1)==u &&
1266 :     memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
1267 :     {
1268 :     f83name2=f83name1;
1269 :     break;
1270 :     }
1271 :     }
1272 :     :
1273 :     BEGIN dup WHILE
1274 :     2@ >r >r dup r@ cell+ c@ $1F and =
1275 :     IF 2dup r@ cell+ char+ -text 0=
1276 :     IF 2drop r> rdrop EXIT THEN THEN
1277 :     rdrop r>
1278 :     REPEAT nip nip ;
1279 :    
1280 : anton 1.47 (hashkey) ( c_addr u1 -- u2 ) gforth paren_hashkey
1281 : anton 1.1 u2=0;
1282 :     while(u1--)
1283 :     u2+=(Cell)toupper(*c_addr++);
1284 :     :
1285 :     0 -rot bounds ?DO I c@ toupper + LOOP ;
1286 :    
1287 : anton 1.47 (hashkey1) ( c_addr u ubits -- ukey ) gforth paren_hashkey1
1288 : anton 1.1 ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1289 :     /* this hash function rotates the key at every step by rot bits within
1290 :     ubits bits and xors it with the character. This function does ok in
1291 :     the chi-sqare-test. Rot should be <=7 (preferably <=5) for
1292 :     ASCII strings (larger if ubits is large), and should share no
1293 :     divisors with ubits.
1294 :     */
1295 :     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];
1296 :     Char *cp = c_addr;
1297 :     for (ukey=0; cp<c_addr+u; cp++)
1298 :     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot)))
1299 :     ^ toupper(*cp))
1300 :     & ((1<<ubits)-1));
1301 :     :
1302 :     dup rot-values + c@ over 1 swap lshift 1- >r
1303 :     tuck - 2swap r> 0 2swap bounds
1304 :     ?DO dup 4 pick lshift swap 3 pick rshift or
1305 :     I c@ toupper xor
1306 :     over and LOOP
1307 :     nip nip nip ;
1308 :     Create rot-values
1309 :     5 c, 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 5 c, 5 c, 5 c,
1310 :     3 c, 5 c, 5 c, 5 c, 5 c, 7 c, 5 c, 5 c, 5 c, 5 c,
1311 :     7 c, 5 c, 5 c, 5 c, 5 c, 6 c, 5 c, 5 c, 5 c, 5 c,
1312 :     7 c, 5 c, 5 c,
1313 :    
1314 : pazsan 1.15 \+
1315 : anton 1.1
1316 : anton 1.47 (parse-white) ( c_addr1 u1 -- c_addr2 u2 ) gforth paren_parse_white
1317 : anton 1.1 /* use !isgraph instead of isspace? */
1318 :     Char *endp = c_addr1+u1;
1319 :     while (c_addr1<endp && isspace(*c_addr1))
1320 :     c_addr1++;
1321 :     if (c_addr1<endp) {
1322 :     for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
1323 :     ;
1324 :     u2 = c_addr1-c_addr2;
1325 :     }
1326 :     else {
1327 :     c_addr2 = c_addr1;
1328 :     u2 = 0;
1329 :     }
1330 :     :
1331 :     BEGIN dup WHILE over c@ bl <= WHILE 1 /string
1332 :     REPEAT THEN 2dup
1333 :     BEGIN dup WHILE over c@ bl > WHILE 1 /string
1334 :     REPEAT THEN nip - ;
1335 :    
1336 : anton 1.47 aligned ( c_addr -- a_addr ) core
1337 : crook 1.29 "" @i{a-addr} is the first aligned address greater than or equal to @i{c-addr}.""
1338 : anton 1.1 a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1339 :     :
1340 :     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1341 :    
1342 : anton 1.47 faligned ( c_addr -- f_addr ) float f_aligned
1343 : crook 1.29 "" @i{f-addr} is the first float-aligned address greater than or equal to @i{c-addr}.""
1344 : anton 1.1 f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1345 :     :
1346 :     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1347 :    
1348 : anton 1.47 >body ( xt -- a_addr ) core to_body
1349 : crook 1.40 "" Get the address of the body of the word represented by @i{xt} (the address
1350 :     of the word's data field).""
1351 : anton 1.1 a_addr = PFA(xt);
1352 :     :
1353 :     2 cells + ;
1354 :    
1355 : jwilke 1.35 \ threading stuff is currently only interesting if we have a compiler
1356 :     \fhas? standardthreading has? compiler and [IF]
1357 : jwilke 1.28
1358 : anton 1.47 >code-address ( xt -- c_addr ) gforth to_code_address
1359 : crook 1.29 ""@i{c-addr} is the code address of the word @i{xt}.""
1360 : anton 1.1 /* !! This behaves installation-dependently for DOES-words */
1361 :     c_addr = (Address)CODE_ADDRESS(xt);
1362 :     :
1363 :     @ ;
1364 :    
1365 : anton 1.47 >does-code ( xt -- a_addr ) gforth to_does_code
1366 : crook 1.29 ""If @i{xt} is the execution token of a defining-word-defined word,
1367 :     @i{a-addr} is the start of the Forth code after the @code{DOES>};
1368 :     Otherwise @i{a-addr} is 0.""
1369 : anton 1.1 a_addr = (Cell *)DOES_CODE(xt);
1370 :     :
1371 :     cell+ @ ;
1372 :    
1373 : anton 1.47 code-address! ( c_addr xt -- ) gforth code_address_store
1374 : crook 1.29 ""Create a code field with code address @i{c-addr} at @i{xt}.""
1375 : anton 1.1 MAKE_CF(xt, c_addr);
1376 : anton 1.10 CACHE_FLUSH(xt,(size_t)PFA(0));
1377 : anton 1.1 :
1378 :     ! ;
1379 :    
1380 : anton 1.47 does-code! ( a_addr xt -- ) gforth does_code_store
1381 : crook 1.29 ""Create a code field at @i{xt} for a defining-word-defined word; @i{a-addr}
1382 : crook 1.26 is the start of the Forth code after @code{DOES>}.""
1383 : anton 1.1 MAKE_DOES_CF(xt, a_addr);
1384 : anton 1.10 CACHE_FLUSH(xt,(size_t)PFA(0));
1385 : anton 1.1 :
1386 :     dodoes: over ! cell+ ! ;
1387 :    
1388 : anton 1.47 does-handler! ( a_addr -- ) gforth does_handler_store
1389 : crook 1.29 ""Create a @code{DOES>}-handler at address @i{a-addr}. Usually, @i{a-addr} points
1390 : crook 1.26 just behind a @code{DOES>}.""
1391 : anton 1.1 MAKE_DOES_HANDLER(a_addr);
1392 : anton 1.10 CACHE_FLUSH((caddr_t)a_addr,DOES_HANDLER_SIZE);
1393 : anton 1.1 :
1394 :     drop ;
1395 :    
1396 : anton 1.47 /does-handler ( -- n ) gforth slash_does_handler
1397 : crook 1.26 ""The size of a @code{DOES>}-handler (includes possible padding).""
1398 : anton 1.1 /* !! a constant or environmental query might be better */
1399 :     n = DOES_HANDLER_SIZE;
1400 :     :
1401 :     2 cells ;
1402 :    
1403 : anton 1.47 threading-method ( -- n ) gforth threading_method
1404 : anton 1.1 ""0 if the engine is direct threaded. Note that this may change during
1405 :     the lifetime of an image.""
1406 :     #if defined(DOUBLY_INDIRECT)
1407 :     n=2;
1408 :     #else
1409 :     # if defined(DIRECT_THREADED)
1410 :     n=0;
1411 :     # else
1412 :     n=1;
1413 :     # endif
1414 :     #endif
1415 :     :
1416 :     1 ;
1417 : jwilke 1.28
1418 : jwilke 1.35 \f[THEN]
1419 : anton 1.1
1420 : anton 1.47 key-file ( wfileid -- n ) gforth paren_key_file
1421 : pazsan 1.17 #ifdef HAS_FILE
1422 : anton 1.1 fflush(stdout);
1423 : pazsan 1.12 n = key((FILE*)wfileid);
1424 : pazsan 1.17 #else
1425 :     n = key(stdin);
1426 :     #endif
1427 : anton 1.1
1428 : anton 1.47 key?-file ( wfileid -- n ) facility key_q_file
1429 : pazsan 1.17 #ifdef HAS_FILE
1430 : anton 1.1 fflush(stdout);
1431 : pazsan 1.12 n = key_query((FILE*)wfileid);
1432 : pazsan 1.17 #else
1433 :     n = key_query(stdin);
1434 :     #endif
1435 :    
1436 :     \+os
1437 : pazsan 1.12
1438 : anton 1.47 stdin ( -- wfileid ) gforth
1439 : pazsan 1.12 wfileid = (Cell)stdin;
1440 : anton 1.1
1441 : anton 1.47 stdout ( -- wfileid ) gforth
1442 : anton 1.1 wfileid = (Cell)stdout;
1443 :    
1444 : anton 1.47 stderr ( -- wfileid ) gforth
1445 : anton 1.1 wfileid = (Cell)stderr;
1446 :    
1447 : anton 1.47 form ( -- urows ucols ) gforth
1448 : anton 1.1 ""The number of lines and columns in the terminal. These numbers may change
1449 :     with the window size.""
1450 :     /* we could block SIGWINCH here to get a consistent size, but I don't
1451 :     think this is necessary or always beneficial */
1452 :     urows=rows;
1453 :     ucols=cols;
1454 :    
1455 : anton 1.47 flush-icache ( c_addr u -- ) gforth flush_icache
1456 : anton 1.1 ""Make sure that the instruction cache of the processor (if there is
1457 : crook 1.29 one) does not contain stale data at @i{c-addr} and @i{u} bytes
1458 : anton 1.1 afterwards. @code{END-CODE} performs a @code{flush-icache}
1459 :     automatically. Caveat: @code{flush-icache} might not work on your
1460 :     installation; this is usually the case if direct threading is not
1461 :     supported on your machine (take a look at your @file{machine.h}) and
1462 :     your machine has a separate instruction cache. In such cases,
1463 :     @code{flush-icache} does nothing instead of flushing the instruction
1464 :     cache.""
1465 :     FLUSH_ICACHE(c_addr,u);
1466 :    
1467 : anton 1.47 (bye) ( n -- ) gforth paren_bye
1468 : anton 1.1 return (Label *)n;
1469 :    
1470 : anton 1.47 (system) ( c_addr u -- wretval wior ) gforth peren_system
1471 : pazsan 1.20 #ifndef MSDOS
1472 : anton 1.1 int old_tp=terminal_prepped;
1473 :     deprep_terminal();
1474 : pazsan 1.20 #endif
1475 : anton 1.1 wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1476 :     wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1477 : pazsan 1.20 #ifndef MSDOS
1478 : anton 1.1 if (old_tp)
1479 :     prep_terminal();
1480 : pazsan 1.20 #endif
1481 : anton 1.1
1482 : anton 1.47 getenv ( c_addr1 u1 -- c_addr2 u2 ) gforth
1483 : crook 1.29 ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2}
1484 : crook 1.24 is the host operating system's expansion of that environment variable. If the
1485 : crook 1.29 environment variable does not exist, @i{c-addr2 u2} specifies a string 0 characters
1486 : crook 1.24 in length.""
1487 : pazsan 1.46 /* close ' to keep fontify happy */
1488 : anton 1.1 c_addr2 = getenv(cstr(c_addr1,u1,1));
1489 :     u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1490 :    
1491 : anton 1.47 open-pipe ( c_addr u ntype -- wfileid wior ) gforth open_pipe
1492 : anton 1.1 wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
1493 :     wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1494 :    
1495 : anton 1.47 close-pipe ( wfileid -- wretval wior ) gforth close_pipe
1496 : anton 1.1 wretval = pclose((FILE *)wfileid);
1497 :     wior = IOR(wretval==-1);
1498 :    
1499 : anton 1.47 time&date ( -- nsec nmin nhour nday nmonth nyear ) facility-ext time_and_date
1500 : crook 1.44 ""Report the current time of day. Seconds, minutes and hours are numbered from 0.
1501 :     Months are numbered from 1.""
1502 : anton 1.1 struct timeval time1;
1503 :     struct timezone zone1;
1504 :     struct tm *ltime;
1505 :     gettimeofday(&time1,&zone1);
1506 :     ltime=localtime((time_t *)&time1.tv_sec);
1507 :     nyear =ltime->tm_year+1900;
1508 :     nmonth=ltime->tm_mon+1;
1509 :     nday =ltime->tm_mday;
1510 :     nhour =ltime->tm_hour;
1511 :     nmin =ltime->tm_min;
1512 :     nsec =ltime->tm_sec;
1513 :    
1514 : anton 1.47 ms ( n -- ) facility-ext
1515 : crook 1.44 ""Wait at least @i{n} milli-second.""
1516 : anton 1.1 struct timeval timeout;
1517 :     timeout.tv_sec=n/1000;
1518 :     timeout.tv_usec=1000*(n%1000);
1519 :     (void)select(0,0,0,0,&timeout);
1520 :    
1521 : anton 1.47 allocate ( u -- a_addr wior ) memory
1522 : crook 1.29 ""Allocate @i{u} address units of contiguous data space. The initial
1523 : crook 1.27 contents of the data space is undefined. If the allocation is successful,
1524 : crook 1.29 @i{a-addr} is the start address of the allocated region and @i{wior}
1525 :     is 0. If the allocation fails, @i{a-addr} is undefined and @i{wior}
1526 : crook 1.27 is an implementation-defined I/O result code.""
1527 : anton 1.1 a_addr = (Cell *)malloc(u?u:1);
1528 :     wior = IOR(a_addr==NULL);
1529 :    
1530 : anton 1.47 free ( a_addr -- wior ) memory
1531 : crook 1.29 ""Return the region of data space starting at @i{a-addr} to the system.
1532 : crook 1.27 The regon must originally have been obtained using @code{allocate} or
1533 : crook 1.29 @code{resize}. If the operational is successful, @i{wior} is 0.
1534 :     If the operation fails, @i{wior} is an implementation-defined
1535 : crook 1.27 I/O result code.""
1536 : anton 1.1 free(a_addr);
1537 :     wior = 0;
1538 :    
1539 : anton 1.47 resize ( a_addr1 u -- a_addr2 wior ) memory
1540 : crook 1.26 ""Change the size of the allocated area at @i{a-addr1} to @i{u}
1541 : anton 1.1 address units, possibly moving the contents to a different
1542 : crook 1.27 area. @i{a-addr2} is the address of the resulting area.
1543 : crook 1.29 If the operational is successful, @i{wior} is 0.
1544 :     If the operation fails, @i{wior} is an implementation-defined
1545 :     I/O result code. If @i{a-addr1} is 0, Gforth's (but not the Standard)
1546 : crook 1.27 @code{resize} @code{allocate}s @i{u} address units.""
1547 : anton 1.1 /* the following check is not necessary on most OSs, but it is needed
1548 :     on SunOS 4.1.2. */
1549 : pazsan 1.46 /* close ' to keep fontify happy */
1550 : anton 1.1 if (a_addr1==NULL)
1551 :     a_addr2 = (Cell *)malloc(u);
1552 :     else
1553 :     a_addr2 = (Cell *)realloc(a_addr1, u);
1554 :     wior = IOR(a_addr2==NULL); /* !! Define a return code */
1555 :    
1556 : anton 1.47 strerror ( n -- c_addr u ) gforth
1557 : anton 1.1 c_addr = strerror(n);
1558 :     u = strlen(c_addr);
1559 :    
1560 : anton 1.47 strsignal ( n -- c_addr u ) gforth
1561 : anton 1.1 c_addr = strsignal(n);
1562 :     u = strlen(c_addr);
1563 :    
1564 : anton 1.47 call-c ( w -- ) gforth call_c
1565 : anton 1.1 ""Call the C function pointed to by @i{w}. The C function has to
1566 :     access the stack itself. The stack pointers are exported in the global
1567 :     variables @code{SP} and @code{FP}.""
1568 :     /* This is a first attempt at support for calls to C. This may change in
1569 :     the future */
1570 :     IF_FTOS(fp[0]=FTOS);
1571 :     FP=fp;
1572 :     SP=sp;
1573 :     ((void (*)())w)();
1574 :     sp=SP;
1575 :     fp=FP;
1576 :     IF_TOS(TOS=sp[0]);
1577 :     IF_FTOS(FTOS=fp[0]);
1578 :    
1579 : pazsan 1.15 \+
1580 :     \+file
1581 : anton 1.1
1582 : anton 1.47 close-file ( wfileid -- wior ) file close_file
1583 : anton 1.1 wior = IOR(fclose((FILE *)wfileid)==EOF);
1584 :    
1585 : anton 1.47 open-file ( c_addr u ntype -- wfileid wior ) file open_file
1586 : crook 1.22 wfileid = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
1587 : pazsan 1.14 #if defined(GO32) && defined(MSDOS)
1588 : crook 1.22 if(wfileid && !(ntype & 1))
1589 :     setbuf((FILE*)wfileid, NULL);
1590 : pazsan 1.14 #endif
1591 : crook 1.22 wior = IOR(wfileid == 0);
1592 : anton 1.1
1593 : anton 1.47 create-file ( c_addr u ntype -- wfileid wior ) file create_file
1594 : anton 1.1 Cell fd;
1595 :     fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
1596 :     if (fd != -1) {
1597 : crook 1.22 wfileid = (Cell)fdopen(fd, fileattr[ntype]);
1598 : pazsan 1.14 #if defined(GO32) && defined(MSDOS)
1599 : crook 1.22 if(wfileid && !(ntype & 1))
1600 :     setbuf((FILE*)wfileid, NULL);
1601 : pazsan 1.14 #endif
1602 : crook 1.22 wior = IOR(wfileid == 0);
1603 : anton 1.1 } else {
1604 : crook 1.22 wfileid = 0;
1605 : anton 1.1 wior = IOR(1);
1606 :     }
1607 :    
1608 : anton 1.47 delete-file ( c_addr u -- wior ) file delete_file
1609 : anton 1.1 wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1610 :    
1611 : anton 1.47 rename-file ( c_addr1 u1 c_addr2 u2 -- wior ) file-ext rename_file
1612 : crook 1.29 ""Rename file @i{c_addr1 u1} to new name @i{c_addr2 u2}""
1613 : anton 1.1 char *s1=tilde_cstr(c_addr2, u2, 1);
1614 :     wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
1615 :    
1616 : anton 1.47 file-position ( wfileid -- ud wior ) file file_position
1617 : anton 1.1 /* !! use tell and lseek? */
1618 :     ud = LONG2UD(ftell((FILE *)wfileid));
1619 :     wior = IOR(UD2LONG(ud)==-1);
1620 :    
1621 : anton 1.47 reposition-file ( ud wfileid -- wior ) file reposition_file
1622 : anton 1.1 wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
1623 :    
1624 : anton 1.47 file-size ( wfileid -- ud wior ) file file_size
1625 : anton 1.1 struct stat buf;
1626 :     wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1627 :     ud = LONG2UD(buf.st_size);
1628 :    
1629 : anton 1.47 resize-file ( ud wfileid -- wior ) file resize_file
1630 : anton 1.1 wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
1631 :    
1632 : anton 1.47 read-file ( c_addr u1 wfileid -- u2 wior ) file read_file
1633 : anton 1.1 /* !! fread does not guarantee enough */
1634 :     u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1635 :     wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1636 :     /* !! is the value of ferror errno-compatible? */
1637 :     if (wior)
1638 :     clearerr((FILE *)wfileid);
1639 :    
1640 : anton 1.47 read-line ( c_addr u1 wfileid -- u2 flag wior ) file read_line
1641 : anton 1.45 #if 1
1642 : anton 1.1 Cell c;
1643 :     flag=-1;
1644 :     for(u2=0; u2<u1; u2++)
1645 :     {
1646 : anton 1.45 c = getc((FILE *)wfileid);
1647 :     if (c=='\n') break;
1648 :     if (c=='\r') {
1649 :     if ((c = getc((FILE *)wfileid))!='\n')
1650 :     ungetc(c,(FILE *)wfileid);
1651 :     break;
1652 :     }
1653 :     if (c==EOF) {
1654 : anton 1.1 flag=FLAG(u2!=0);
1655 :     break;
1656 :     }
1657 : anton 1.45 c_addr[u2] = (Char)c;
1658 : anton 1.1 }
1659 :     wior=FILEIO(ferror((FILE *)wfileid));
1660 : anton 1.45 #else
1661 : anton 1.1 if ((flag=FLAG(!feof((FILE *)wfileid) &&
1662 :     fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
1663 : anton 1.30 wior=FILEIO(ferror((FILE *)wfileid)!=0); /* !! ior? */
1664 : anton 1.1 if (wior)
1665 :     clearerr((FILE *)wfileid);
1666 :     u2 = strlen(c_addr);
1667 :     u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
1668 :     }
1669 :     else {
1670 :     wior=0;
1671 :     u2=0;
1672 :     }
1673 : anton 1.45 #endif
1674 : anton 1.1
1675 : pazsan 1.15 \+
1676 : anton 1.1
1677 : anton 1.47 write-file ( c_addr u1 wfileid -- wior ) file write_file
1678 : anton 1.1 /* !! fwrite does not guarantee enough */
1679 : pazsan 1.39 #ifdef HAS_FILE
1680 : anton 1.1 {
1681 :     UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
1682 :     wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
1683 :     if (wior)
1684 :     clearerr((FILE *)wfileid);
1685 :     }
1686 : pazsan 1.39 #else
1687 :     TYPE(c_addr, u1);
1688 :     #endif
1689 : pazsan 1.17
1690 : anton 1.47 emit-file ( c wfileid -- wior ) gforth emit_file
1691 : pazsan 1.17 #ifdef HAS_FILE
1692 : anton 1.1 wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1693 :     if (wior)
1694 :     clearerr((FILE *)wfileid);
1695 : pazsan 1.17 #else
1696 : pazsan 1.36 PUTC(c);
1697 : pazsan 1.17 #endif
1698 : anton 1.1
1699 : pazsan 1.15 \+file
1700 : anton 1.1
1701 : anton 1.47 flush-file ( wfileid -- wior ) file-ext flush_file
1702 : anton 1.1 wior = IOR(fflush((FILE *) wfileid)==EOF);
1703 :    
1704 : anton 1.47 file-status ( c_addr u -- ntype wior ) file-ext file_status
1705 : anton 1.1 char *filename=tilde_cstr(c_addr, u, 1);
1706 :     if (access (filename, F_OK) != 0) {
1707 :     ntype=0;
1708 :     wior=IOR(1);
1709 :     }
1710 :     else if (access (filename, R_OK | W_OK) == 0) {
1711 :     ntype=2; /* r/w */
1712 :     wior=0;
1713 :     }
1714 :     else if (access (filename, R_OK) == 0) {
1715 :     ntype=0; /* r/o */
1716 :     wior=0;
1717 :     }
1718 :     else if (access (filename, W_OK) == 0) {
1719 :     ntype=4; /* w/o */
1720 :     wior=0;
1721 :     }
1722 :     else {
1723 :     ntype=1; /* well, we cannot access the file, but better deliver a legal
1724 :     access mode (r/o bin), so we get a decent error later upon open. */
1725 :     wior=0;
1726 :     }
1727 :    
1728 : pazsan 1.15 \+
1729 :     \+floating
1730 : anton 1.1
1731 :     comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
1732 :     comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
1733 :    
1734 : anton 1.47 d>f ( d -- r ) float d_to_f
1735 : anton 1.1 #ifdef BUGGY_LONG_LONG
1736 :     extern double ldexp(double x, int exp);
1737 :     r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
1738 :     #else
1739 :     r = d;
1740 :     #endif
1741 :    
1742 : anton 1.47 f>d ( r -- d ) float f_to_d
1743 : anton 1.1 #ifdef BUGGY_LONG_LONG
1744 : anton 1.21 d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
1745 : anton 1.1 d.lo = r-ldexp((Float)d.hi,CELL_BITS);
1746 :     #else
1747 :     d = r;
1748 :     #endif
1749 :    
1750 : anton 1.47 f! ( r f_addr -- ) float f_store
1751 : crook 1.29 "" Store the floating-point value @i{r} to address @i{f-addr}.""
1752 : anton 1.1 *f_addr = r;
1753 :    
1754 : anton 1.47 f@ ( f_addr -- r ) float f_fetch
1755 : crook 1.29 "" Fetch floating-point value @i{r} from address @i{f-addr}.""
1756 : anton 1.1 r = *f_addr;
1757 :    
1758 : anton 1.47 df@ ( df_addr -- r ) float-ext d_f_fetch
1759 : crook 1.29 "" Fetch the double-precision IEEE floating-point value @i{r} from the address @i{df-addr}.""
1760 : anton 1.1 #ifdef IEEE_FP
1761 :     r = *df_addr;
1762 :     #else
1763 :     !! df@
1764 :     #endif
1765 :    
1766 : anton 1.47 df! ( r df_addr -- ) float-ext d_f_store
1767 : crook 1.29 "" Store the double-precision IEEE floating-point value @i{r} to the address @i{df-addr}.""
1768 : anton 1.1 #ifdef IEEE_FP
1769 :     *df_addr = r;
1770 :     #else
1771 :     !! df!
1772 :     #endif
1773 :    
1774 : anton 1.47 sf@ ( sf_addr -- r ) float-ext s_f_fetch
1775 : crook 1.29 "" Fetch the single-precision IEEE floating-point value @i{r} from the address @i{sf-addr}.""
1776 : anton 1.1 #ifdef IEEE_FP
1777 :     r = *sf_addr;
1778 :     #else
1779 :     !! sf@
1780 :     #endif
1781 :    
1782 : anton 1.47 sf! ( r sf_addr -- ) float-ext s_f_store
1783 : crook 1.29 "" Store the single-precision IEEE floating-point value @i{r} to the address @i{sf-addr}.""
1784 : anton 1.1 #ifdef IEEE_FP
1785 :     *sf_addr = r;
1786 :     #else
1787 :     !! sf!
1788 :     #endif
1789 :    
1790 : anton 1.47 f+ ( r1 r2 -- r3 ) float f_plus
1791 : anton 1.1 r3 = r1+r2;
1792 :    
1793 : anton 1.47 f- ( r1 r2 -- r3 ) float f_minus
1794 : anton 1.1 r3 = r1-r2;
1795 :    
1796 : anton 1.47 f* ( r1 r2 -- r3 ) float f_star
1797 : anton 1.1 r3 = r1*r2;
1798 :    
1799 : anton 1.47 f/ ( r1 r2 -- r3 ) float f_slash
1800 : anton 1.1 r3 = r1/r2;
1801 :    
1802 : anton 1.47 f** ( r1 r2 -- r3 ) float-ext f_star_star
1803 : crook 1.26 ""@i{r3} is @i{r1} raised to the @i{r2}th power.""
1804 : anton 1.1 r3 = pow(r1,r2);
1805 :    
1806 : anton 1.47 fnegate ( r1 -- r2 ) float f_negate
1807 : anton 1.1 r2 = - r1;
1808 :    
1809 : anton 1.47 fdrop ( r -- ) float f_drop
1810 : anton 1.1
1811 : anton 1.47 fdup ( r -- r r ) float f_dupe
1812 : anton 1.1
1813 : anton 1.47 fswap ( r1 r2 -- r2 r1 ) float f_swap
1814 : anton 1.1
1815 : anton 1.47 fover ( r1 r2 -- r1 r2 r1 ) float f_over
1816 : anton 1.1
1817 : anton 1.47 frot ( r1 r2 r3 -- r2 r3 r1 ) float f_rote
1818 : anton 1.1
1819 : anton 1.47 fnip ( r1 r2 -- r2 ) gforth f_nip
1820 : anton 1.1
1821 : anton 1.47 ftuck ( r1 r2 -- r2 r1 r2 ) gforth f_tuck
1822 : anton 1.1
1823 : anton 1.47 float+ ( f_addr1 -- f_addr2 ) float float_plus
1824 : crook 1.29 "" Increment @i{f-addr1} by the number of address units corresponding to the size of
1825 :     one floating-point number, to give @i{f-addr2}.""
1826 : anton 1.1 f_addr2 = f_addr1+1;
1827 :    
1828 : anton 1.47 floats ( n1 -- n2 ) float
1829 : crook 1.29 ""@i{n2} is the number of address units corresponding to @i{n1} floating-point numbers.""
1830 : anton 1.1 n2 = n1*sizeof(Float);
1831 :    
1832 : anton 1.47 floor ( r1 -- r2 ) float
1833 : crook 1.26 ""Round towards the next smaller integral value, i.e., round toward negative infinity.""
1834 : anton 1.1 /* !! unclear wording */
1835 :     r2 = floor(r1);
1836 :    
1837 : anton 1.47 fround ( r1 -- r2 ) float f_round
1838 : crook 1.26 ""Round to the nearest integral value.""
1839 : anton 1.1 /* !! unclear wording */
1840 :     #ifdef HAVE_RINT
1841 :     r2 = rint(r1);
1842 :     #else
1843 :     r2 = floor(r1+0.5);
1844 :     /* !! This is not quite true to the rounding rules given in the standard */
1845 :     #endif
1846 :    
1847 : anton 1.47 fmax ( r1 r2 -- r3 ) float f_max
1848 : anton 1.1 if (r1<r2)
1849 :     r3 = r2;
1850 :     else
1851 :     r3 = r1;
1852 :    
1853 : anton 1.47 fmin ( r1 r2 -- r3 ) float f_min
1854 : anton 1.1 if (r1<r2)
1855 :     r3 = r1;
1856 :     else
1857 :     r3 = r2;
1858 :    
1859 : anton 1.47 represent ( r c_addr u -- n f1 f2 ) float
1860 : anton 1.1 char *sig;
1861 :     int flag;
1862 :     int decpt;
1863 :     sig=ecvt(r, u, &decpt, &flag);
1864 :     n=(r==0 ? 1 : decpt);
1865 :     f1=FLAG(flag!=0);
1866 : anton 1.21 f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
1867 : anton 1.1 memmove(c_addr,sig,u);
1868 :    
1869 : anton 1.47 >float ( c_addr u -- flag ) float to_float
1870 : crook 1.29 ""Attempt to convert the character string @i{c-addr u} to
1871 : crook 1.27 internal floating-point representation. If the string
1872 : crook 1.29 represents a valid floating-point number @i{r} is placed
1873 :     on the floating-point stack and @i{flag} is true. Otherwise,
1874 :     @i{flag} is false. A string of blanks is a special case
1875 : crook 1.40 and represents the floating-point number 0.""
1876 : anton 1.1 /* real signature: c_addr u -- r t / f */
1877 :     Float r;
1878 :     char *number=cstr(c_addr, u, 1);
1879 :     char *endconv;
1880 : pazsan 1.42 int sign = 0;
1881 :     if(number[0]=='-') {
1882 :     sign = 1;
1883 :     number++;
1884 :     u--;
1885 :     }
1886 : anton 1.21 while(isspace((unsigned)(number[--u])) && u>0);
1887 : anton 1.1 switch(number[u])
1888 :     {
1889 :     case 'd':
1890 :     case 'D':
1891 :     case 'e':
1892 :     case 'E': break;
1893 :     default : u++; break;
1894 :     }
1895 :     number[u]='\0';
1896 :     r=strtod(number,&endconv);
1897 :     if((flag=FLAG(!(Cell)*endconv)))
1898 :     {
1899 :     IF_FTOS(fp[0] = FTOS);
1900 :     fp += -1;
1901 : pazsan 1.42 FTOS = sign ? -r : r;
1902 : anton 1.1 }
1903 :     else if(*endconv=='d' || *endconv=='D')
1904 :     {
1905 :     *endconv='E';
1906 :     r=strtod(number,&endconv);
1907 :     if((flag=FLAG(!(Cell)*endconv)))
1908 :     {
1909 :     IF_FTOS(fp[0] = FTOS);
1910 :     fp += -1;
1911 : pazsan 1.42 FTOS = sign ? -r : r;
1912 : anton 1.1 }
1913 :     }
1914 :    
1915 : anton 1.47 fabs ( r1 -- r2 ) float-ext f_abs
1916 : anton 1.1 r2 = fabs(r1);
1917 :    
1918 : anton 1.47 facos ( r1 -- r2 ) float-ext f_a_cos
1919 : anton 1.1 r2 = acos(r1);
1920 :    
1921 : anton 1.47 fasin ( r1 -- r2 ) float-ext f_a_sine
1922 : anton 1.1 r2 = asin(r1);
1923 :    
1924 : anton 1.47 fatan ( r1 -- r2 ) float-ext f_a_tan
1925 : anton 1.1 r2 = atan(r1);
1926 :    
1927 : anton 1.47 fatan2 ( r1 r2 -- r3 ) float-ext f_a_tan_two
1928 : crook 1.26 ""@i{r1/r2}=tan(@i{r3}). ANS Forth does not require, but probably
1929 : anton 1.1 intends this to be the inverse of @code{fsincos}. In gforth it is.""
1930 :     r3 = atan2(r1,r2);
1931 :    
1932 : anton 1.47 fcos ( r1 -- r2 ) float-ext f_cos
1933 : anton 1.1 r2 = cos(r1);
1934 :    
1935 : anton 1.47 fexp ( r1 -- r2 ) float-ext f_e_x_p
1936 : anton 1.1 r2 = exp(r1);
1937 :    
1938 : anton 1.47 fexpm1 ( r1 -- r2 ) float-ext f_e_x_p_m_one
1939 : anton 1.1 ""@i{r2}=@i{e}**@i{r1}@minus{}1""
1940 :     #ifdef HAVE_EXPM1
1941 : pazsan 1.3 extern double
1942 :     #ifdef NeXT
1943 :     const
1944 :     #endif
1945 :     expm1(double);
1946 : anton 1.1 r2 = expm1(r1);
1947 :     #else
1948 :     r2 = exp(r1)-1.;
1949 :     #endif
1950 :    
1951 : anton 1.47 fln ( r1 -- r2 ) float-ext f_l_n
1952 : anton 1.1 r2 = log(r1);
1953 :    
1954 : anton 1.47 flnp1 ( r1 -- r2 ) float-ext f_l_n_p_one
1955 : anton 1.1 ""@i{r2}=ln(@i{r1}+1)""
1956 :     #ifdef HAVE_LOG1P
1957 : pazsan 1.3 extern double
1958 :     #ifdef NeXT
1959 :     const
1960 :     #endif
1961 :     log1p(double);
1962 : anton 1.1 r2 = log1p(r1);
1963 :     #else
1964 :     r2 = log(r1+1.);
1965 :     #endif
1966 :    
1967 : anton 1.47 flog ( r1 -- r2 ) float-ext f_log
1968 : crook 1.26 ""The decimal logarithm.""
1969 : anton 1.1 r2 = log10(r1);
1970 :    
1971 : anton 1.47 falog ( r1 -- r2 ) float-ext f_a_log
1972 : anton 1.1 ""@i{r2}=10**@i{r1}""
1973 :     extern double pow10(double);
1974 :     r2 = pow10(r1);
1975 :    
1976 : anton 1.47 fsin ( r1 -- r2 ) float-ext f_sine
1977 : anton 1.1 r2 = sin(r1);
1978 :    
1979 : anton 1.47 fsincos ( r1 -- r2 r3 ) float-ext f_sine_cos
1980 : anton 1.1 ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
1981 :     r2 = sin(r1);
1982 :     r3 = cos(r1);
1983 :    
1984 : anton 1.47 fsqrt ( r1 -- r2 ) float-ext f_square_root
1985 : anton 1.1 r2 = sqrt(r1);
1986 :    
1987 : anton 1.47 ftan ( r1 -- r2 ) float-ext f_tan
1988 : anton 1.1 r2 = tan(r1);
1989 :     :
1990 :     fsincos f/ ;
1991 :    
1992 : anton 1.47 fsinh ( r1 -- r2 ) float-ext f_cinch
1993 : anton 1.1 r2 = sinh(r1);
1994 :     :
1995 :     fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
1996 :    
1997 : anton 1.47 fcosh ( r1 -- r2 ) float-ext f_cosh
1998 : anton 1.1 r2 = cosh(r1);
1999 :     :
2000 :     fexp fdup 1/f f+ f2/ ;
2001 :    
2002 : anton 1.47 ftanh ( r1 -- r2 ) float-ext f_tan_h
2003 : anton 1.1 r2 = tanh(r1);
2004 :     :
2005 :     f2* fexpm1 fdup 2. d>f f+ f/ ;
2006 :    
2007 : anton 1.47 fasinh ( r1 -- r2 ) float-ext f_a_cinch
2008 : anton 1.1 r2 = asinh(r1);
2009 :     :
2010 :     fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
2011 :    
2012 : anton 1.47 facosh ( r1 -- r2 ) float-ext f_a_cosh
2013 : anton 1.1 r2 = acosh(r1);
2014 :     :
2015 :     fdup fdup f* 1. d>f f- fsqrt f+ fln ;
2016 :    
2017 : anton 1.47 fatanh ( r1 -- r2 ) float-ext f_a_tan_h
2018 : anton 1.1 r2 = atanh(r1);
2019 :     :
2020 :     fdup f0< >r fabs 1. d>f fover f- f/ f2* flnp1 f2/
2021 :     r> IF fnegate THEN ;
2022 :    
2023 : anton 1.47 sfloats ( n1 -- n2 ) float-ext s_floats
2024 : crook 1.29 ""@i{n2} is the number of address units corresponding to @i{n1}
2025 :     single-precision IEEE floating-point numbers.""
2026 : anton 1.1 n2 = n1*sizeof(SFloat);
2027 :    
2028 : anton 1.47 dfloats ( n1 -- n2 ) float-ext d_floats
2029 : crook 1.29 ""@i{n2} is the number of address units corresponding to @i{n1}
2030 :     double-precision IEEE floating-point numbers.""
2031 : anton 1.1 n2 = n1*sizeof(DFloat);
2032 :    
2033 : anton 1.47 sfaligned ( c_addr -- sf_addr ) float-ext s_f_aligned
2034 : crook 1.29 "" @i{sf-addr} is the first single-float-aligned address greater
2035 :     than or equal to @i{c-addr}.""
2036 : anton 1.1 sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
2037 :     :
2038 :     [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
2039 :    
2040 : anton 1.47 dfaligned ( c_addr -- df_addr ) float-ext d_f_aligned
2041 : crook 1.29 "" @i{df-addr} is the first double-float-aligned address greater
2042 :     than or equal to @i{c-addr}.""
2043 : anton 1.1 df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
2044 :     :
2045 :     [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
2046 :    
2047 :     \ The following words access machine/OS/installation-dependent
2048 :     \ Gforth internals
2049 :     \ !! how about environmental queries DIRECT-THREADED,
2050 :     \ INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
2051 :    
2052 :     \ local variable implementation primitives
2053 : pazsan 1.15 \+
2054 :     \+glocals
2055 : anton 1.1
2056 : anton 1.47 @local# ( -- w ) gforth fetch_local_number
2057 : anton 1.1 w = *(Cell *)(lp+(Cell)NEXT_INST);
2058 :     INC_IP(1);
2059 :    
2060 : anton 1.47 @local0 ( -- w ) new fetch_local_zero
2061 : anton 1.1 w = *(Cell *)(lp+0*sizeof(Cell));
2062 :    
2063 : anton 1.47 @local1 ( -- w ) new fetch_local_four
2064 : anton 1.1 w = *(Cell *)(lp+1*sizeof(Cell));
2065 :    
2066 : anton 1.47 @local2 ( -- w ) new fetch_local_eight
2067 : anton 1.1 w = *(Cell *)(lp+2*sizeof(Cell));
2068 :    
2069 : anton 1.47 @local3 ( -- w ) new fetch_local_twelve
2070 : anton 1.1 w = *(Cell *)(lp+3*sizeof(Cell));
2071 :    
2072 : pazsan 1.15 \+floating
2073 : anton 1.1
2074 : anton 1.47 f@local# ( -- r ) gforth f_fetch_local_number
2075 : anton 1.1 r = *(Float *)(lp+(Cell)NEXT_INST);
2076 :     INC_IP(1);
2077 :    
2078 : anton 1.47 f@local0 ( -- r ) new f_fetch_local_zero
2079 : anton 1.1 r = *(Float *)(lp+0*sizeof(Float));
2080 :    
2081 : anton 1.47 f@local1 ( -- r ) new f_fetch_local_eight
2082 : anton 1.1 r = *(Float *)(lp+1*sizeof(Float));
2083 :    
2084 : pazsan 1.15 \+
2085 : anton 1.1
2086 : anton 1.47 laddr# ( -- c_addr ) gforth laddr_number
2087 : anton 1.1 /* this can also be used to implement lp@ */
2088 :     c_addr = (Char *)(lp+(Cell)NEXT_INST);
2089 :     INC_IP(1);
2090 :    
2091 : anton 1.47 lp+!# ( -- ) gforth lp_plus_store_number
2092 : anton 1.1 ""used with negative immediate values it allocates memory on the
2093 :     local stack, a positive immediate argument drops memory from the local
2094 :     stack""
2095 :     lp += (Cell)NEXT_INST;
2096 :     INC_IP(1);
2097 :    
2098 : anton 1.47 lp- ( -- ) new minus_four_lp_plus_store
2099 : anton 1.1 lp += -sizeof(Cell);
2100 :    
2101 : anton 1.47 lp+ ( -- ) new eight_lp_plus_store
2102 : anton 1.1 lp += sizeof(Float);
2103 :    
2104 : anton 1.47 lp+2 ( -- ) new sixteen_lp_plus_store
2105 : anton 1.1 lp += 2*sizeof(Float);
2106 :    
2107 : anton 1.47 lp! ( c_addr -- ) gforth lp_store
2108 : anton 1.1 lp = (Address)c_addr;
2109 :    
2110 : anton 1.47 >l ( w -- ) gforth to_l
2111 : anton 1.1 lp -= sizeof(Cell);
2112 :     *(Cell *)lp = w;
2113 :    
2114 : pazsan 1.15 \+floating
2115 : anton 1.1
2116 : anton 1.47 f>l ( r -- ) gforth f_to_l
2117 : anton 1.1 lp -= sizeof(Float);
2118 :     *(Float *)lp = r;
2119 :    
2120 : anton 1.47 fpick ( u -- r ) gforth
2121 : anton 1.11 r = fp[u+1]; /* +1, because update of fp happens before this fragment */
2122 :     :
2123 :     floats fp@ + f@ ;
2124 :    
2125 : pazsan 1.15 \+
2126 :     \+
2127 : anton 1.1
2128 : pazsan 1.15 \+OS
2129 : anton 1.1
2130 :     define(`uploop',
2131 :     `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
2132 :     define(`_uploop',
2133 :     `ifelse($1, `$3', `$5',
2134 :     `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
2135 :     \ argflist(argnum): Forth argument list
2136 :     define(argflist,
2137 :     `ifelse($1, 0, `',
2138 :     `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
2139 :     \ argdlist(argnum): declare C's arguments
2140 :     define(argdlist,
2141 :     `ifelse($1, 0, `',
2142 :     `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
2143 :     \ argclist(argnum): pass C's arguments
2144 :     define(argclist,
2145 :     `ifelse($1, 0, `',
2146 :     `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
2147 :     \ icall(argnum)
2148 :     define(icall,
2149 : anton 1.47 `icall$1 ( argflist($1)u -- uret ) gforth
2150 : pazsan 1.9 uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
2151 : anton 1.1
2152 :     ')
2153 :     define(fcall,
2154 : anton 1.47 `fcall$1 ( argflist($1)u -- rret ) gforth
2155 : pazsan 1.9 rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
2156 : anton 1.1
2157 :     ')
2158 :    
2159 : pazsan 1.46 \ close ' to keep fontify happy
2160 : anton 1.1
2161 : anton 1.47 open-lib ( c_addr1 u1 -- u2 ) gforth open_lib
2162 : anton 1.1 #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2163 : anton 1.8 #ifndef RTLD_GLOBAL
2164 :     #define RTLD_GLOBAL 0
2165 :     #endif
2166 : pazsan 1.7 u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_GLOBAL | RTLD_LAZY);
2167 : anton 1.1 #else
2168 : pazsan 1.18 # ifdef _WIN32
2169 : anton 1.1 u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
2170 :     # else
2171 :     #warning Define open-lib!
2172 :     u2 = 0;
2173 :     # endif
2174 :     #endif
2175 :    
2176 : anton 1.47 lib-sym ( c_addr1 u1 u2 -- u3 ) gforth lib_sym
2177 : anton 1.1 #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
2178 :     u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
2179 :     #else
2180 : pazsan 1.18 # ifdef _WIN32
2181 : anton 1.1 u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
2182 :     # else
2183 :     #warning Define lib-sym!
2184 :     u3 = 0;
2185 :     # endif
2186 :     #endif
2187 :    
2188 :     uploop(i, 0, 7, `icall(i)')
2189 :     icall(20)
2190 :     uploop(i, 0, 7, `fcall(i)')
2191 :     fcall(20)
2192 :    
2193 : pazsan 1.15 \+
2194 : anton 1.1
2195 : anton 1.47 up! ( a_addr -- ) gforth up_store
2196 : anton 1.1 UP=up=(char *)a_addr;
2197 :     :
2198 :     up ! ;
2199 :     Variable UP
2200 : jwilke 1.34
2201 : anton 1.47 wcall ( u -- ) gforth
2202 : jwilke 1.34 IF_FTOS(fp[0]=FTOS);
2203 :     FP=fp;
2204 :     sp=(SYSCALL(Cell(*)(Cell *, void *))u)(sp, &FP);
2205 :     fp=FP;
2206 :     IF_TOS(TOS=sp[0];)
2207 :     IF_FTOS(FTOS=fp[0]);
2208 : pazsan 1.46
2209 :     \+file
2210 :    
2211 : anton 1.47 open-dir ( c_addr u -- wdirid wior ) gforth open_dir
2212 : pazsan 1.46 wdirid = (Cell)opendir(tilde_cstr(c_addr, u, 1));
2213 :     wior = IOR(wdirid == 0);
2214 :    
2215 : anton 1.47 read-dir ( c_addr u1 wdirid -- u2 flag wior ) gforth read_dir
2216 : pazsan 1.46 struct dirent * dent;
2217 :     dent = readdir((DIR *)wdirid);
2218 :     wior = 0;
2219 :     flag = -1;
2220 :     if(dent == NULL) {
2221 :     u2 = 0;
2222 :     flag = 0;
2223 :     } else {
2224 :     u2 = strlen(dent->d_name);
2225 :     if(u2 > u1)
2226 :     u2 = u1;
2227 :     memmove(c_addr, dent->d_name, u2);
2228 :     }
2229 :    
2230 : anton 1.47 close-dir ( wdirid -- wior ) gforth close_dir
2231 : pazsan 1.46 wior = IOR(closedir((DIR *)wdirid));
2232 :    
2233 : anton 1.47 filename-match ( c_addr1 u1 c_addr2 u2 -- flag ) gforth match_file
2234 : pazsan 1.46 char * string = cstr(c_addr1, u1, 1);
2235 :     char * pattern = cstr(c_addr2, u2, 0);
2236 :     flag = FLAG(!fnmatch(pattern, string, 0));
2237 :    
2238 :     \+
2239 : jwilke 1.34
2240 : anton 1.47 newline ( -- c_addr u ) gforth
2241 : anton 1.45 ""String containing the newline sequence of the host OS""
2242 :     char newline[] = {
2243 :     #ifdef unix
2244 :     '\n'
2245 :     #else
2246 :     '\r','\n'
2247 :     #endif
2248 :     };
2249 :     c_addr=newline;
2250 :     u=sizeof(newline);

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help