[gforth] / gforth / prim  

gforth: gforth/prim


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help