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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help