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