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