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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help