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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help