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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help