Annotation of gforth/prim, revision 1.139
1.1 anton 1: \ Gforth primitives
2:
1.126 anton 3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 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
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
1.63 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 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: \
1.47 anton 29: \ forth name ( stack effect ) category [pronunciation]
1.1 anton 30: \ [""glossary entry""]
31: \ C code
32: \ [:
33: \ Forth code]
34: \
1.47 anton 35: \ Note: Fields in brackets are optional. Word specifications have to
36: \ be separated by at least one empty line
1.1 anton 37: \
38: \ Both pronounciation and stack items (in the stack effect) must
1.48 anton 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.
1.1 anton 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).
1.93 anton 56: \
57: \ For superinstructions the syntax is:
58: \
59: \ forth-name [/ c-name] = forth-name forth-name ...
60: \
1.1 anton 61: \
62: \ The stack variables have the following types:
63: \
64: \ name matches type
65: \ f.* Bool
66: \ c.* Char
1.93 anton 67: \ [nw].* Cell
1.1 anton 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 *
1.67 anton 79:
1.79 anton 80: \E stack data-stack sp Cell
81: \E stack fp-stack fp Float
82: \E stack return-stack rp Cell
83: \E
1.67 anton 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
1.71 anton 101: \E s" struct Longname *" single data-stack type-prefix longname
1.67 anton 102: \E
103: \E return-stack stack-prefix R:
104: \E inst-stream stack-prefix #
105: \E
106: \E set-current
1.97 anton 107: \E store-optimization on
1.109 anton 108: \E ' noop tail-nextp2 ! \ now INST_TAIL just stores, but does not jump
1.128 anton 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
1.67 anton 113:
1.1 anton 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')
1.78 pazsan 142: undefine(`symbols')
1.1 anton 143:
1.139 ! anton 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:
1.83 pazsan 183: \g control
184:
1.47 anton 185: noop ( -- ) gforth
1.1 anton 186: :
187: ;
188:
1.112 pazsan 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
1.1 anton 205:
1.47 anton 206: execute ( xt -- ) core
1.29 crook 207: ""Perform the semantics represented by the execution token, @i{xt}.""
1.102 anton 208: #ifndef NO_IP
1.1 anton 209: ip=IP;
1.102 anton 210: #endif
1.64 anton 211: IF_spTOS(spTOS = sp[0]);
1.76 anton 212: SUPER_END;
1.1 anton 213: EXEC(xt);
214:
1.47 anton 215: perform ( a_addr -- ) gforth
1.55 anton 216: ""@code{@@ execute}.""
1.1 anton 217: /* and pfe */
1.102 anton 218: #ifndef NO_IP
1.1 anton 219: ip=IP;
1.102 anton 220: #endif
1.64 anton 221: IF_spTOS(spTOS = sp[0]);
1.76 anton 222: SUPER_END;
1.1 anton 223: EXEC(*(Xt *)a_addr);
224: :
225: @ execute ;
226:
1.112 pazsan 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:
1.15 pazsan 266: \+glocals
1.1 anton 267:
1.112 pazsan 268: branch-lp+!# ( #a_target #nlocals -- ) gforth branch_lp_plus_store_number
1.1 anton 269: /* this will probably not be used */
1.68 anton 270: lp += nlocals;
1.112 pazsan 271: #ifdef NO_IP
272: INST_TAIL;
273: JUMP(a_target);
274: #else
275: SET_IP((Xt *)a_target);
276: #endif
1.1 anton 277:
1.15 pazsan 278: \+
1.1 anton 279:
1.112 pazsan 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
1.1 anton 287: :
1.112 pazsan 288: r> @ >r ;
1.1 anton 289:
1.112 pazsan 290: \ condbranch(forthname,stackeffect,restline,code1,code2,forthcode)
1.1 anton 291: \ this is non-syntactical: code must open a brace that is closed by the macro
292: define(condbranch,
1.112 pazsan 293: $1 ( `#'a_target $2 ) $3
294: $4 #ifdef NO_IP
1.96 anton 295: INST_TAIL;
1.112 pazsan 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
1.1 anton 303: }
1.87 anton 304: SUPER_CONTINUE;
1.112 pazsan 305: $6
1.1 anton 306:
1.15 pazsan 307: \+glocals
1.1 anton 308:
1.112 pazsan 309: $1-lp+!`#' ( `#'a_target `#'nlocals $2 ) $3_lp_plus_store_number
310: $4 #ifdef NO_IP
1.96 anton 311: INST_TAIL;
1.112 pazsan 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
1.1 anton 320: }
1.87 anton 321: SUPER_CONTINUE;
1.1 anton 322:
1.15 pazsan 323: \+
1.1 anton 324: )
325:
1.68 anton 326: condbranch(?branch,f --,f83 question_branch,
1.112 pazsan 327: ,if (f==0) {
1.5 jwilke 328: ,:
1.112 pazsan 329: 0= dup 0= \ !f f
330: r> tuck cell+ \ !f branchoffset f IP+
331: and -rot @ and or \ f&IP+|!f&branch
1.5 jwilke 332: >r ;)
1.1 anton 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:
1.15 pazsan 337: \+xconds
1.1 anton 338:
1.112 pazsan 339: ?dup-?branch ( #a_target f -- f ) new question_dupe_question_branch
1.1 anton 340: ""The run-time procedure compiled by @code{?DUP-IF}.""
341: if (f==0) {
342: sp++;
1.64 anton 343: IF_spTOS(spTOS = sp[0]);
1.112 pazsan 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
1.1 anton 351: }
1.87 anton 352: SUPER_CONTINUE;
1.1 anton 353:
1.112 pazsan 354: ?dup-0=-?branch ( #a_target f -- ) new question_dupe_zero_equals_question_branch
1.1 anton 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--;
1.112 pazsan 362: #ifdef NO_IP
363: JUMP(a_target);
364: #else
365: SET_IP((Xt *)a_target);
1.1 anton 366: NEXT;
1.112 pazsan 367: #endif
1.1 anton 368: }
1.87 anton 369: SUPER_CONTINUE;
1.1 anton 370:
1.15 pazsan 371: \+
1.31 jwilke 372: \fhas? skiploopprims 0= [IF]
1.1 anton 373:
1.68 anton 374: condbranch((next),R:n1 -- R:n2,cmFORTH paren_next,
1.65 anton 375: n2=n1-1;
1.112 pazsan 376: ,if (n1) {
1.1 anton 377: ,:
378: r> r> dup 1- >r
1.112 pazsan 379: IF @ >r ELSE cell+ >r THEN ;)
1.1 anton 380:
1.68 anton 381: condbranch((loop),R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_loop,
1.65 anton 382: n2=n1+1;
1.112 pazsan 383: ,if (n2 != nlimit) {
1.1 anton 384: ,:
385: r> r> 1+ r> 2dup =
386: IF >r 1- >r cell+ >r
1.112 pazsan 387: ELSE >r >r @ >r THEN ;)
1.1 anton 388:
1.68 anton 389: condbranch((+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_plus_loop,
1.1 anton 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 */
1.65 anton 393: Cell olddiff = n1-nlimit;
394: n2=n1+n;
1.112 pazsan 395: ,if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
1.1 anton 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
1.112 pazsan 402: IF >r + >r @ >r
1.1 anton 403: ELSE >r >r drop cell+ >r THEN ;)
404:
1.15 pazsan 405: \+xconds
1.1 anton 406:
1.68 anton 407: condbranch((-loop),u R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_minus_loop,
1.65 anton 408: UCell olddiff = n1-nlimit;
409: n2=n1-u;
1.112 pazsan 410: ,if (olddiff>u) {
1.1 anton 411: ,)
412:
1.68 anton 413: condbranch((s+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_symmetric_plus_loop,
1.1 anton 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 */
1.65 anton 418: Cell diff = n1-nlimit;
1.1 anton 419: Cell newdiff = diff+n;
420: if (n<0) {
421: diff = -diff;
422: newdiff = -newdiff;
423: }
1.65 anton 424: n2=n1+n;
1.112 pazsan 425: ,if (diff>=0 || newdiff<0) {
1.1 anton 426: ,)
427:
1.15 pazsan 428: \+
1.1 anton 429:
1.112 pazsan 430: (for) ( ncount -- R:nlimit R:ncount ) cmFORTH paren_for
1.1 anton 431: /* or (for) = >r -- collides with unloop! */
1.65 anton 432: nlimit=0;
1.1 anton 433: :
434: r> swap 0 >r >r >r ;
435:
1.112 pazsan 436: (do) ( nlimit nstart -- R:nlimit R:nstart ) gforth paren_do
1.1 anton 437: :
438: r> swap rot >r >r >r ;
439:
1.112 pazsan 440: (?do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth paren_question_do
441: #ifdef NO_IP
442: INST_TAIL;
443: #endif
1.1 anton 444: if (nstart == nlimit) {
1.112 pazsan 445: #ifdef NO_IP
446: JUMP(a_target);
447: #else
448: SET_IP((Xt *)a_target);
449: INST_TAIL; NEXT_P2;
450: #endif
1.1 anton 451: }
1.87 anton 452: SUPER_CONTINUE;
1.1 anton 453: :
454: 2dup =
455: IF r> swap rot >r >r
1.112 pazsan 456: @ >r
1.1 anton 457: ELSE r> swap rot >r >r
458: cell+ >r
459: THEN ; \ --> CORE-EXT
460:
1.15 pazsan 461: \+xconds
1.1 anton 462:
1.112 pazsan 463: (+do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth paren_plus_do
464: #ifdef NO_IP
465: INST_TAIL;
466: #endif
1.1 anton 467: if (nstart >= nlimit) {
1.112 pazsan 468: #ifdef NO_IP
469: JUMP(a_target);
470: #else
471: SET_IP((Xt *)a_target);
472: INST_TAIL; NEXT_P2;
473: #endif
1.1 anton 474: }
1.87 anton 475: SUPER_CONTINUE;
1.1 anton 476: :
477: swap 2dup
478: r> swap >r swap >r
479: >=
480: IF
1.112 pazsan 481: @
1.1 anton 482: ELSE
483: cell+
484: THEN >r ;
485:
1.112 pazsan 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
1.1 anton 490: if (ustart >= ulimit) {
1.112 pazsan 491: #ifdef NO_IP
492: JUMP(a_target);
493: #else
494: SET_IP((Xt *)a_target);
495: INST_TAIL; NEXT_P2;
496: #endif
1.1 anton 497: }
1.87 anton 498: SUPER_CONTINUE;
1.1 anton 499: :
500: swap 2dup
501: r> swap >r swap >r
502: u>=
503: IF
1.112 pazsan 504: @
1.1 anton 505: ELSE
506: cell+
507: THEN >r ;
508:
1.112 pazsan 509: (-do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth paren_minus_do
510: #ifdef NO_IP
511: INST_TAIL;
512: #endif
1.1 anton 513: if (nstart <= nlimit) {
1.112 pazsan 514: #ifdef NO_IP
515: JUMP(a_target);
516: #else
517: SET_IP((Xt *)a_target);
518: INST_TAIL; NEXT_P2;
519: #endif
1.1 anton 520: }
1.87 anton 521: SUPER_CONTINUE;
1.1 anton 522: :
523: swap 2dup
524: r> swap >r swap >r
525: <=
526: IF
1.112 pazsan 527: @
1.1 anton 528: ELSE
529: cell+
530: THEN >r ;
531:
1.112 pazsan 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
1.1 anton 536: if (ustart <= ulimit) {
1.112 pazsan 537: #ifdef NO_IP
538: JUMP(a_target);
539: #else
540: SET_IP((Xt *)a_target);
541: INST_TAIL; NEXT_P2;
542: #endif
1.1 anton 543: }
1.87 anton 544: SUPER_CONTINUE;
1.1 anton 545: :
546: swap 2dup
547: r> swap >r swap >r
548: u<=
549: IF
1.112 pazsan 550: @
1.1 anton 551: ELSE
552: cell+
553: THEN >r ;
554:
1.15 pazsan 555: \+
1.1 anton 556:
1.5 jwilke 557: \ don't make any assumptions where the return stack is!!
558: \ implement this in machine code if it should run quickly!
559:
1.65 anton 560: i ( R:n -- R:n n ) core
1.1 anton 561: :
1.5 jwilke 562: \ rp@ cell+ @ ;
563: r> r> tuck >r >r ;
1.1 anton 564:
1.65 anton 565: i' ( R:w R:w2 -- R:w R:w2 w ) gforth i_tick
1.1 anton 566: :
1.5 jwilke 567: \ rp@ cell+ cell+ @ ;
568: r> r> r> dup itmp ! >r >r >r itmp @ ;
569: variable itmp
1.1 anton 570:
1.65 anton 571: j ( R:n R:d1 -- n R:n R:d1 ) core
1.1 anton 572: :
1.5 jwilke 573: \ rp@ cell+ cell+ cell+ @ ;
574: r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
575: [IFUNDEF] itmp variable itmp [THEN]
1.1 anton 576:
1.65 anton 577: k ( R:n R:d1 R:d2 -- n R:n R:d1 R:d2 ) gforth
1.1 anton 578: :
1.5 jwilke 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]
1.31 jwilke 582:
583: \f[THEN]
1.1 anton 584:
585: \ digit is high-level: 0/0%
586:
1.83 pazsan 587: \g strings
588:
1.47 anton 589: move ( c_from c_to ucount -- ) core
1.52 anton 590: ""Copy the contents of @i{ucount} aus at @i{c-from} to
1.33 anton 591: @i{c-to}. @code{move} works correctly even if the two areas overlap.""
1.52 anton 592: /* !! note that the standard specifies addr, not c-addr */
1.1 anton 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:
1.47 anton 598: cmove ( c_from c_to u -- ) string c_move
1.33 anton 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}.""
1.125 anton 603: cmove(c_from,c_to,u);
1.1 anton 604: :
605: bounds ?DO dup c@ I c! 1+ LOOP drop ;
606:
1.47 anton 607: cmove> ( c_from c_to u -- ) string c_move_up
1.33 anton 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}.""
1.125 anton 612: cmove_up(c_from,c_to,u);
1.1 anton 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:
1.47 anton 618: fill ( c_addr u c -- ) core
1.52 anton 619: ""Store @i{c} in @i{u} chars starting at @i{c-addr}.""
1.1 anton 620: memset(c_addr,c,u);
621: :
622: -rot bounds
623: ?DO dup I c! LOOP drop ;
624:
1.47 anton 625: compare ( c_addr1 u1 c_addr2 u2 -- n ) string
1.29 crook 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}
1.1 anton 628: is 1. Currently this is based on the machine's character
1.26 crook 629: comparison. In the future, this may change to consider the current
1.1 anton 630: locale and its collation order.""
1.46 pazsan 631: /* close ' to keep fontify happy */
1.125 anton 632: n = compare(c_addr1, u1, c_addr2, u2);
1.1 anton 633: :
1.43 pazsan 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+ ;
1.1 anton 638:
1.125 anton 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+ ;
1.1 anton 652:
1.47 anton 653: toupper ( c1 -- c2 ) gforth
1.29 crook 654: ""If @i{c1} is a lower-case character (in the current locale), @i{c2}
1.25 anton 655: is the equivalent upper-case character. All other characters are unchanged.""
1.1 anton 656: c2 = toupper(c1);
657: :
658: dup [char] a - [ char z char a - 1 + ] Literal u< bl and - ;
659:
1.47 anton 660: /string ( c_addr1 u1 n -- c_addr2 u2 ) string slash_string
1.29 crook 661: ""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
1.27 crook 662: characters from the start of the string.""
1.1 anton 663: c_addr2 = c_addr1+n;
664: u2 = u1-n;
665: :
666: tuck - >r + r> dup 0< IF - 0 THEN ;
667:
1.83 pazsan 668: \g arith
669:
1.112 pazsan 670: lit ( #w -- w ) gforth
671: :
672: r> dup @ swap cell+ >r ;
673:
1.47 anton 674: + ( n1 n2 -- n ) core plus
1.1 anton 675: n = n1+n2;
676:
1.112 pazsan 677: \ lit+ / lit_plus = lit +
678:
679: lit+ ( n1 #n2 -- n ) new lit_plus
680: n=n1+n2;
681:
1.1 anton 682: \ PFE-0.9.14 has it differently, but the next release will have it as follows
1.47 anton 683: under+ ( n1 n2 n3 -- n n2 ) gforth under_plus
1.29 crook 684: ""add @i{n3} to @i{n1} (giving @i{n})""
1.1 anton 685: n = n1+n3;
686: :
687: rot + swap ;
688:
1.47 anton 689: - ( n1 n2 -- n ) core minus
1.1 anton 690: n = n1-n2;
691: :
692: negate + ;
693:
1.47 anton 694: negate ( n1 -- n2 ) core
1.1 anton 695: /* use minus as alias */
696: n2 = -n1;
697: :
698: invert 1+ ;
699:
1.47 anton 700: 1+ ( n1 -- n2 ) core one_plus
1.1 anton 701: n2 = n1+1;
702: :
703: 1 + ;
704:
1.47 anton 705: 1- ( n1 -- n2 ) core one_minus
1.1 anton 706: n2 = n1-1;
707: :
708: 1 - ;
709:
1.47 anton 710: max ( n1 n2 -- n ) core
1.1 anton 711: if (n1<n2)
712: n = n2;
713: else
714: n = n1;
715: :
716: 2dup < IF swap THEN drop ;
717:
1.47 anton 718: min ( n1 n2 -- n ) core
1.1 anton 719: if (n1<n2)
720: n = n1;
721: else
722: n = n2;
723: :
724: 2dup > IF swap THEN drop ;
725:
1.52 anton 726: abs ( n -- u ) core
727: if (n<0)
728: u = -n;
1.1 anton 729: else
1.52 anton 730: u = n;
1.1 anton 731: :
732: dup 0< IF negate THEN ;
733:
1.47 anton 734: * ( n1 n2 -- n ) core star
1.1 anton 735: n = n1*n2;
736: :
737: um* drop ;
738:
1.47 anton 739: / ( n1 n2 -- n ) core slash
1.1 anton 740: n = n1/n2;
741: :
742: /mod nip ;
743:
1.47 anton 744: mod ( n1 n2 -- n ) core
1.1 anton 745: n = n1%n2;
746: :
747: /mod drop ;
748:
1.47 anton 749: /mod ( n1 n2 -- n3 n4 ) core slash_mod
1.1 anton 750: n4 = n1/n2;
751: n3 = n1%n2; /* !! is this correct? look into C standard! */
752: :
753: >r s>d r> fm/mod ;
754:
1.47 anton 755: 2* ( n1 -- n2 ) core two_star
1.52 anton 756: ""Shift left by 1; also works on unsigned numbers""
1.1 anton 757: n2 = 2*n1;
758: :
759: dup + ;
760:
1.47 anton 761: 2/ ( n1 -- n2 ) core two_slash
1.52 anton 762: ""Arithmetic shift right by 1. For signed numbers this is a floored
763: division by 2 (note that @code{/} not necessarily floors).""
1.1 anton 764: n2 = n1>>1;
765: :
766: dup MINI and IF 1 ELSE 0 THEN
767: [ bits/byte cell * 1- ] literal
1.5 jwilke 768: 0 DO 2* swap dup 2* >r MINI and
1.1 anton 769: IF 1 ELSE 0 THEN or r> swap
770: LOOP nip ;
771:
1.47 anton 772: fm/mod ( d1 n1 -- n2 n3 ) core f_m_slash_mod
1.29 crook 773: ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}.""
1.1 anton 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:
1.47 anton 794: sm/rem ( d1 n1 -- n2 n3 ) core s_m_slash_rem
1.29 crook 795: ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0.""
1.1 anton 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:
1.47 anton 816: m* ( n1 n2 -- d ) core m_star
1.1 anton 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:
1.47 anton 827: um* ( u1 u2 -- ud ) core u_m_star
1.1 anton 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: :
1.137 pazsan 835: 0 -rot dup [ 8 cells ] literal -
1.1 anton 836: DO
1.137 pazsan 837: dup 0< I' and d2*+ drop
838: LOOP ;
1.1 anton 839: : d2*+ ( ud n -- ud+n c )
840: over MINI
841: and >r >r 2dup d+ swap r> + swap r> ;
842:
1.47 anton 843: um/mod ( ud u1 -- u2 u3 ) core u_m_slash_mod
1.32 anton 844: ""ud=u3*u1+u2, u1>u2>=0""
1.1 anton 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
1.5 jwilke 855: ?DO /modstep
1.1 anton 856: LOOP drop swap 1 rshift or swap ;
857: : /modstep ( ud c R: u -- ud-?u c R: u )
1.5 jwilke 858: >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN d2*+ r> ;
1.1 anton 859: : d2*+ ( ud n -- ud+n c )
860: over MINI
861: and >r >r 2dup d+ swap r> + swap r> ;
862:
1.47 anton 863: m+ ( d1 n -- d2 ) double m_plus
1.1 anton 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:
1.47 anton 873: d+ ( d1 d2 -- d ) double d_plus
1.1 anton 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:
1.47 anton 883: d- ( d1 d2 -- d ) double d_minus
1.1 anton 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:
1.47 anton 893: dnegate ( d1 -- d2 ) double d_negate
1.1 anton 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:
1.47 anton 903: d2* ( d1 -- d2 ) double d_two_star
1.52 anton 904: ""Shift left by 1; also works on unsigned numbers""
1.1 anton 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:
1.47 anton 914: d2/ ( d1 -- d2 ) double d_two_slash
1.52 anton 915: ""Arithmetic shift right by 1. For signed numbers this is a floored
916: division by 2.""
1.1 anton 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:
1.47 anton 927: and ( w1 w2 -- w ) core
1.1 anton 928: w = w1&w2;
929:
1.47 anton 930: or ( w1 w2 -- w ) core
1.1 anton 931: w = w1|w2;
932: :
933: invert swap invert and invert ;
934:
1.47 anton 935: xor ( w1 w2 -- w ) core x_or
1.1 anton 936: w = w1^w2;
937:
1.47 anton 938: invert ( w1 -- w2 ) core
1.1 anton 939: w2 = ~w1;
940: :
941: MAXU xor ;
942:
1.47 anton 943: rshift ( u1 n -- u2 ) core r_shift
1.53 anton 944: ""Logical shift right by @i{n} bits.""
1.1 anton 945: u2 = u1>>n;
946: :
947: 0 ?DO 2/ MAXI and LOOP ;
948:
1.47 anton 949: lshift ( u1 n -- u2 ) core l_shift
1.1 anton 950: u2 = u1<<n;
951: :
952: 0 ?DO 2* LOOP ;
953:
1.110 pazsan 954: \g compare
955:
1.1 anton 956: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
957: define(comparisons,
1.47 anton 958: $1= ( $2 -- f ) $6 $3equals
1.1 anton 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:
1.47 anton 967: $1<> ( $2 -- f ) $7 $3not_equals
1.1 anton 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:
1.47 anton 976: $1< ( $2 -- f ) $8 $3less_than
1.1 anton 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:
1.47 anton 988: $1> ( $2 -- f ) $9 $3greater_than
1.1 anton 989: f = FLAG($4>$5);
990: :
991: [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
992: $1< ;
993:
1.47 anton 994: $1<= ( $2 -- f ) gforth $3less_or_equal
1.1 anton 995: f = FLAG($4<=$5);
996: :
997: $1> 0= ;
998:
1.47 anton 999: $1>= ( $2 -- f ) gforth $3greater_or_equal
1.1 anton 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,
1.47 anton 1013: $1= ( $2 -- f ) $6 $3equals
1.1 anton 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:
1.47 anton 1020: $1<> ( $2 -- f ) $7 $3not_equals
1.1 anton 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:
1.47 anton 1027: $1< ( $2 -- f ) $8 $3less_than
1.1 anton 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:
1.47 anton 1034: $1> ( $2 -- f ) $9 $3greater_than
1.1 anton 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:
1.47 anton 1041: $1<= ( $2 -- f ) gforth $3less_or_equal
1.1 anton 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:
1.47 anton 1048: $1>= ( $2 -- f ) gforth $3greater_or_equal
1.1 anton 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:
1.15 pazsan 1057: \+dcomps
1.1 anton 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:
1.15 pazsan 1063: \+
1.1 anton 1064:
1.47 anton 1065: within ( u1 u2 u3 -- f ) core-ext
1.32 anton 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
1.52 anton 1072: u2=u3); if u1 is in this range, @code{within} returns true.""
1.1 anton 1073: f = FLAG(u1-u2 < u3-u2);
1074: :
1075: over - >r - r> u< ;
1076:
1.112 pazsan 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:
1.47 anton 1088: sp@ ( -- a_addr ) gforth sp_fetch
1.1 anton 1089: a_addr = sp+1;
1090:
1.47 anton 1091: sp! ( a_addr -- ) gforth sp_store
1.1 anton 1092: sp = a_addr;
1.64 anton 1093: /* works with and without spTOS caching */
1.1 anton 1094:
1.47 anton 1095: rp@ ( -- a_addr ) gforth rp_fetch
1.1 anton 1096: a_addr = rp;
1097:
1.47 anton 1098: rp! ( a_addr -- ) gforth rp_store
1.1 anton 1099: rp = a_addr;
1100:
1.15 pazsan 1101: \+floating
1.1 anton 1102:
1.47 anton 1103: fp@ ( -- f_addr ) gforth fp_fetch
1.1 anton 1104: f_addr = fp;
1105:
1.47 anton 1106: fp! ( f_addr -- ) gforth fp_store
1.1 anton 1107: fp = f_addr;
1108:
1.15 pazsan 1109: \+
1.1 anton 1110:
1.65 anton 1111: >r ( w -- R:w ) core to_r
1.1 anton 1112: :
1113: (>r) ;
1114: : (>r) rp@ cell+ @ rp@ ! rp@ cell+ ! ;
1115:
1.65 anton 1116: r> ( R:w -- w ) core r_from
1.1 anton 1117: :
1118: rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
1119: Create (rdrop) ' ;s A,
1120:
1.65 anton 1121: rdrop ( R:w -- ) gforth
1.1 anton 1122: :
1123: r> r> drop >r ;
1124:
1.136 pazsan 1125: 2>r ( d -- R:d ) core-ext two_to_r
1.1 anton 1126: :
1127: swap r> swap >r swap >r >r ;
1128:
1.136 pazsan 1129: 2r> ( R:d -- d ) core-ext two_r_from
1.1 anton 1130: :
1131: r> r> swap r> swap >r swap ;
1132:
1.136 pazsan 1133: 2r@ ( R:d -- R:d d ) core-ext two_r_fetch
1.1 anton 1134: :
1135: i' j ;
1136:
1.136 pazsan 1137: 2rdrop ( R:d -- ) gforth two_r_drop
1.1 anton 1138: :
1139: r> r> drop r> drop >r ;
1140:
1.47 anton 1141: over ( w1 w2 -- w1 w2 w1 ) core
1.1 anton 1142: :
1143: sp@ cell+ @ ;
1144:
1.47 anton 1145: drop ( w -- ) core
1.1 anton 1146: :
1147: IF THEN ;
1148:
1.47 anton 1149: swap ( w1 w2 -- w2 w1 ) core
1.1 anton 1150: :
1151: >r (swap) ! r> (swap) @ ;
1152: Variable (swap)
1153:
1.47 anton 1154: dup ( w -- w w ) core dupe
1.1 anton 1155: :
1156: sp@ @ ;
1157:
1.47 anton 1158: rot ( w1 w2 w3 -- w2 w3 w1 ) core rote
1.1 anton 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:
1.47 anton 1167: -rot ( w1 w2 w3 -- w3 w1 w2 ) gforth not_rote
1.1 anton 1168: :
1169: rot rot ;
1170:
1.47 anton 1171: nip ( w1 w2 -- w2 ) core-ext
1.1 anton 1172: :
1.6 jwilke 1173: swap drop ;
1.1 anton 1174:
1.47 anton 1175: tuck ( w1 w2 -- w2 w1 w2 ) core-ext
1.1 anton 1176: :
1177: swap over ;
1178:
1.47 anton 1179: ?dup ( w -- w ) core question_dupe
1.52 anton 1180: ""Actually the stack effect is: @code{( w -- 0 | w w )}. It performs a
1181: @code{dup} if w is nonzero.""
1.1 anton 1182: if (w!=0) {
1.64 anton 1183: IF_spTOS(*sp-- = w;)
1.1 anton 1184: #ifndef USE_TOS
1185: *--sp = w;
1186: #endif
1187: }
1188: :
1189: dup IF dup THEN ;
1190:
1.47 anton 1191: pick ( u -- w ) core-ext
1.52 anton 1192: ""Actually the stack effect is @code{ x0 ... xu u -- x0 ... xu x0 }.""
1.1 anton 1193: w = sp[u+1];
1194: :
1195: 1+ cells sp@ + @ ;
1196:
1.47 anton 1197: 2drop ( w1 w2 -- ) core two_drop
1.1 anton 1198: :
1199: drop drop ;
1200:
1.47 anton 1201: 2dup ( w1 w2 -- w1 w2 w1 w2 ) core two_dupe
1.1 anton 1202: :
1203: over over ;
1204:
1.47 anton 1205: 2over ( w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 ) core two_over
1.1 anton 1206: :
1207: 3 pick 3 pick ;
1208:
1.47 anton 1209: 2swap ( w1 w2 w3 w4 -- w3 w4 w1 w2 ) core two_swap
1.1 anton 1210: :
1211: rot >r rot r> ;
1212:
1.47 anton 1213: 2rot ( w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 ) double-ext two_rote
1.1 anton 1214: :
1215: >r >r 2swap r> r> 2swap ;
1216:
1.47 anton 1217: 2nip ( w1 w2 w3 w4 -- w3 w4 ) gforth two_nip
1.1 anton 1218: :
1219: 2swap 2drop ;
1220:
1.47 anton 1221: 2tuck ( w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 ) gforth two_tuck
1.1 anton 1222: :
1223: 2swap 2over ;
1224:
1225: \ toggle is high-level: 0.11/0.42%
1226:
1.110 pazsan 1227: \g memory
1228:
1.47 anton 1229: @ ( a_addr -- w ) core fetch
1.52 anton 1230: ""@i{w} is the cell stored at @i{a_addr}.""
1.1 anton 1231: w = *a_addr;
1232:
1.112 pazsan 1233: \ lit@ / lit_fetch = lit @
1234:
1235: lit@ ( #a_addr -- w ) new lit_fetch
1236: w = *a_addr;
1237:
1.47 anton 1238: ! ( w a_addr -- ) core store
1.52 anton 1239: ""Store @i{w} into the cell at @i{a-addr}.""
1.1 anton 1240: *a_addr = w;
1241:
1.47 anton 1242: +! ( n a_addr -- ) core plus_store
1.52 anton 1243: ""Add @i{n} to the cell at @i{a-addr}.""
1.1 anton 1244: *a_addr += n;
1245: :
1246: tuck @ + swap ! ;
1247:
1.47 anton 1248: c@ ( c_addr -- c ) core c_fetch
1.52 anton 1249: ""@i{c} is the char stored at @i{c_addr}.""
1.1 anton 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:
1.47 anton 1274: c! ( c c_addr -- ) core c_store
1.52 anton 1275: ""Store @i{c} into the char at @i{c-addr}.""
1.1 anton 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:
1.47 anton 1304: 2! ( w1 w2 a_addr -- ) core two_store
1.52 anton 1305: ""Store @i{w2} into the cell at @i{c-addr} and @i{w1} into the next cell.""
1.1 anton 1306: a_addr[0] = w2;
1307: a_addr[1] = w1;
1308: :
1309: tuck ! cell+ ! ;
1310:
1.47 anton 1311: 2@ ( a_addr -- w1 w2 ) core two_fetch
1.52 anton 1312: ""@i{w2} is the content of the cell stored at @i{a-addr}, @i{w1} is
1313: the content of the next cell.""
1.1 anton 1314: w2 = a_addr[0];
1315: w1 = a_addr[1];
1316: :
1317: dup cell+ @ swap @ ;
1318:
1.47 anton 1319: cell+ ( a_addr1 -- a_addr2 ) core cell_plus
1.52 anton 1320: ""@code{1 cells +}""
1.1 anton 1321: a_addr2 = a_addr1+1;
1322: :
1323: cell + ;
1324:
1.47 anton 1325: cells ( n1 -- n2 ) core
1.52 anton 1326: "" @i{n2} is the number of address units of @i{n1} cells.""
1.1 anton 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:
1.47 anton 1336: char+ ( c_addr1 -- c_addr2 ) core char_plus
1.52 anton 1337: ""@code{1 chars +}.""
1.1 anton 1338: c_addr2 = c_addr1 + 1;
1339: :
1340: 1+ ;
1341:
1.47 anton 1342: (chars) ( n1 -- n2 ) gforth paren_chars
1.1 anton 1343: n2 = n1 * sizeof(Char);
1344: :
1345: ;
1346:
1.47 anton 1347: count ( c_addr1 -- c_addr2 u ) core
1.56 anton 1348: ""@i{c-addr2} is the first character and @i{u} the length of the
1349: counted string at @i{c-addr1}.""
1.1 anton 1350: u = *c_addr1;
1351: c_addr2 = c_addr1+1;
1352: :
1353: dup 1+ swap c@ ;
1354:
1.110 pazsan 1355: \g compiler
1356:
1.138 pazsan 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:
1.112 pazsan 1384: (listlfind) ( c_addr u longname1 -- longname2 ) new paren_listlfind
1.125 anton 1385: longname2=listlfind(c_addr, u, longname1);
1.1 anton 1386: :
1.112 pazsan 1387: BEGIN dup WHILE (findl-samelen) dup WHILE
1388: >r 2dup r@ cell+ cell+ capscomp 0=
1.1 anton 1389: IF 2drop r> EXIT THEN
1390: r> @
1391: REPEAT THEN nip nip ;
1.112 pazsan 1392: : (findl-samelen) ( u longname1 -- u longname2/0 )
1393: BEGIN 2dup cell+ @ lcount-mask and <> WHILE @ dup 0= UNTIL THEN ;
1.1 anton 1394:
1.15 pazsan 1395: \+hash
1.1 anton 1396:
1.112 pazsan 1397: (hashlfind) ( c_addr u a_addr -- longname2 ) new paren_hashlfind
1.125 anton 1398: longname2 = hashlfind(c_addr, u, a_addr);
1.1 anton 1399: :
1400: BEGIN dup WHILE
1.112 pazsan 1401: 2@ >r >r dup r@ cell+ @ lcount-mask and =
1402: IF 2dup r@ cell+ cell+ capscomp 0=
1.1 anton 1403: IF 2drop r> rdrop EXIT THEN THEN
1404: rdrop r>
1405: REPEAT nip nip ;
1406:
1.112 pazsan 1407: (tablelfind) ( c_addr u a_addr -- longname2 ) new paren_tablelfind
1.1 anton 1408: ""A case-sensitive variant of @code{(hashfind)}""
1.125 anton 1409: longname2 = tablelfind(c_addr, u, a_addr);
1.1 anton 1410: :
1411: BEGIN dup WHILE
1.112 pazsan 1412: 2@ >r >r dup r@ cell+ @ lcount-mask and =
1413: IF 2dup r@ cell+ cell+ -text 0=
1.1 anton 1414: IF 2drop r> rdrop EXIT THEN THEN
1415: rdrop r>
1416: REPEAT nip nip ;
1.138 pazsan 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+ ;
1.1 anton 1423:
1.47 anton 1424: (hashkey1) ( c_addr u ubits -- ukey ) gforth paren_hashkey1
1.1 anton 1425: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
1.125 anton 1426: ukey = hashkey1(c_addr, u, ubits);
1.1 anton 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,
1.138 pazsan 1439:
1440: \+
1.1 anton 1441:
1.15 pazsan 1442: \+
1.1 anton 1443:
1.47 anton 1444: (parse-white) ( c_addr1 u1 -- c_addr2 u2 ) gforth paren_parse_white
1.125 anton 1445: struct Cellpair r=parse_white(c_addr1, u1);
1446: c_addr2 = (Char *)(r.n1);
1447: u2 = r.n2;
1.1 anton 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:
1.47 anton 1454: aligned ( c_addr -- a_addr ) core
1.29 crook 1455: "" @i{a-addr} is the first aligned address greater than or equal to @i{c-addr}.""
1.1 anton 1456: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
1457: :
1458: [ cell 1- ] Literal + [ -1 cells ] Literal and ;
1459:
1.47 anton 1460: faligned ( c_addr -- f_addr ) float f_aligned
1.29 crook 1461: "" @i{f-addr} is the first float-aligned address greater than or equal to @i{c-addr}.""
1.1 anton 1462: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
1463: :
1464: [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
1465:
1.35 jwilke 1466: \ threading stuff is currently only interesting if we have a compiler
1467: \fhas? standardthreading has? compiler and [IF]
1.47 anton 1468: threading-method ( -- n ) gforth threading_method
1.1 anton 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 ;
1.28 jwilke 1482:
1.35 jwilke 1483: \f[THEN]
1.1 anton 1484:
1.83 pazsan 1485: \g hostos
1486:
1.47 anton 1487: key-file ( wfileid -- n ) gforth paren_key_file
1.17 pazsan 1488: #ifdef HAS_FILE
1.1 anton 1489: fflush(stdout);
1.12 pazsan 1490: n = key((FILE*)wfileid);
1.17 pazsan 1491: #else
1492: n = key(stdin);
1493: #endif
1.1 anton 1494:
1.47 anton 1495: key?-file ( wfileid -- n ) facility key_q_file
1.17 pazsan 1496: #ifdef HAS_FILE
1.1 anton 1497: fflush(stdout);
1.12 pazsan 1498: n = key_query((FILE*)wfileid);
1.17 pazsan 1499: #else
1500: n = key_query(stdin);
1501: #endif
1502:
1503: \+os
1.12 pazsan 1504:
1.47 anton 1505: stdin ( -- wfileid ) gforth
1.12 pazsan 1506: wfileid = (Cell)stdin;
1.1 anton 1507:
1.47 anton 1508: stdout ( -- wfileid ) gforth
1.1 anton 1509: wfileid = (Cell)stdout;
1510:
1.47 anton 1511: stderr ( -- wfileid ) gforth
1.1 anton 1512: wfileid = (Cell)stderr;
1513:
1.47 anton 1514: form ( -- urows ucols ) gforth
1.1 anton 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:
1.47 anton 1522: flush-icache ( c_addr u -- ) gforth flush_icache
1.1 anton 1523: ""Make sure that the instruction cache of the processor (if there is
1.29 crook 1524: one) does not contain stale data at @i{c-addr} and @i{u} bytes
1.1 anton 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:
1.47 anton 1534: (bye) ( n -- ) gforth paren_bye
1.77 anton 1535: SUPER_END;
1.1 anton 1536: return (Label *)n;
1537:
1.125 anton 1538: (system) ( c_addr u -- wretval wior ) gforth paren_system
1.20 pazsan 1539: #ifndef MSDOS
1.1 anton 1540: int old_tp=terminal_prepped;
1541: deprep_terminal();
1.20 pazsan 1542: #endif
1.1 anton 1543: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
1544: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
1.20 pazsan 1545: #ifndef MSDOS
1.1 anton 1546: if (old_tp)
1547: prep_terminal();
1.20 pazsan 1548: #endif
1.1 anton 1549:
1.47 anton 1550: getenv ( c_addr1 u1 -- c_addr2 u2 ) gforth
1.29 crook 1551: ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2}
1.24 crook 1552: is the host operating system's expansion of that environment variable. If the
1.29 crook 1553: environment variable does not exist, @i{c-addr2 u2} specifies a string 0 characters
1.24 crook 1554: in length.""
1.46 pazsan 1555: /* close ' to keep fontify happy */
1.1 anton 1556: c_addr2 = getenv(cstr(c_addr1,u1,1));
1557: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
1558:
1.56 anton 1559: open-pipe ( c_addr u wfam -- wfileid wior ) gforth open_pipe
1.84 pazsan 1560: wfileid=(Cell)popen(cstr(c_addr,u,1),pfileattr[wfam]); /* ~ expansion of 1st arg? */
1.1 anton 1561: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
1562:
1.47 anton 1563: close-pipe ( wfileid -- wretval wior ) gforth close_pipe
1.1 anton 1564: wretval = pclose((FILE *)wfileid);
1565: wior = IOR(wretval==-1);
1566:
1.47 anton 1567: time&date ( -- nsec nmin nhour nday nmonth nyear ) facility-ext time_and_date
1.44 crook 1568: ""Report the current time of day. Seconds, minutes and hours are numbered from 0.
1569: Months are numbered from 1.""
1.127 anton 1570: #if 1
1571: time_t now;
1572: struct tm *ltime;
1573: time(&now);
1574: ltime=localtime(&now);
1575: #else
1.1 anton 1576: struct timeval time1;
1577: struct timezone zone1;
1578: struct tm *ltime;
1579: gettimeofday(&time1,&zone1);
1.51 anton 1580: /* !! Single Unix specification:
1581: If tzp is not a null pointer, the behaviour is unspecified. */
1.1 anton 1582: ltime=localtime((time_t *)&time1.tv_sec);
1.127 anton 1583: #endif
1.1 anton 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:
1.47 anton 1591: ms ( n -- ) facility-ext
1.44 crook 1592: ""Wait at least @i{n} milli-second.""
1.1 anton 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:
1.47 anton 1598: allocate ( u -- a_addr wior ) memory
1.29 crook 1599: ""Allocate @i{u} address units of contiguous data space. The initial
1.27 crook 1600: contents of the data space is undefined. If the allocation is successful,
1.29 crook 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}
1.52 anton 1603: is a non-zero I/O result code.""
1.1 anton 1604: a_addr = (Cell *)malloc(u?u:1);
1605: wior = IOR(a_addr==NULL);
1606:
1.47 anton 1607: free ( a_addr -- wior ) memory
1.29 crook 1608: ""Return the region of data space starting at @i{a-addr} to the system.
1.52 anton 1609: The region must originally have been obtained using @code{allocate} or
1.29 crook 1610: @code{resize}. If the operational is successful, @i{wior} is 0.
1.52 anton 1611: If the operation fails, @i{wior} is a non-zero I/O result code.""
1.1 anton 1612: free(a_addr);
1613: wior = 0;
1614:
1.47 anton 1615: resize ( a_addr1 u -- a_addr2 wior ) memory
1.26 crook 1616: ""Change the size of the allocated area at @i{a-addr1} to @i{u}
1.1 anton 1617: address units, possibly moving the contents to a different
1.27 crook 1618: area. @i{a-addr2} is the address of the resulting area.
1.52 anton 1619: If the operation is successful, @i{wior} is 0.
1620: If the operation fails, @i{wior} is a non-zero
1.29 crook 1621: I/O result code. If @i{a-addr1} is 0, Gforth's (but not the Standard)
1.27 crook 1622: @code{resize} @code{allocate}s @i{u} address units.""
1.1 anton 1623: /* the following check is not necessary on most OSs, but it is needed
1624: on SunOS 4.1.2. */
1.46 pazsan 1625: /* close ' to keep fontify happy */
1.1 anton 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:
1.47 anton 1632: strerror ( n -- c_addr u ) gforth
1.1 anton 1633: c_addr = strerror(n);
1634: u = strlen(c_addr);
1635:
1.47 anton 1636: strsignal ( n -- c_addr u ) gforth
1.133 anton 1637: c_addr = (Address)strsignal(n);
1.1 anton 1638: u = strlen(c_addr);
1639:
1.47 anton 1640: call-c ( w -- ) gforth call_c
1.1 anton 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 */
1.64 anton 1646: IF_fpTOS(fp[0]=fpTOS);
1.1 anton 1647: FP=fp;
1648: SP=sp;
1649: ((void (*)())w)();
1650: sp=SP;
1651: fp=FP;
1.64 anton 1652: IF_spTOS(spTOS=sp[0]);
1653: IF_fpTOS(fpTOS=fp[0]);
1.1 anton 1654:
1.15 pazsan 1655: \+
1656: \+file
1.1 anton 1657:
1.47 anton 1658: close-file ( wfileid -- wior ) file close_file
1.1 anton 1659: wior = IOR(fclose((FILE *)wfileid)==EOF);
1660:
1.56 anton 1661: open-file ( c_addr u wfam -- wfileid wior ) file open_file
1662: wfileid = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[wfam]);
1.22 crook 1663: wior = IOR(wfileid == 0);
1.1 anton 1664:
1.56 anton 1665: create-file ( c_addr u wfam -- wfileid wior ) file create_file
1.1 anton 1666: Cell fd;
1.56 anton 1667: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[wfam], 0666);
1.1 anton 1668: if (fd != -1) {
1.56 anton 1669: wfileid = (Cell)fdopen(fd, fileattr[wfam]);
1.22 crook 1670: wior = IOR(wfileid == 0);
1.1 anton 1671: } else {
1.22 crook 1672: wfileid = 0;
1.1 anton 1673: wior = IOR(1);
1674: }
1675:
1.47 anton 1676: delete-file ( c_addr u -- wior ) file delete_file
1.1 anton 1677: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
1678:
1.47 anton 1679: rename-file ( c_addr1 u1 c_addr2 u2 -- wior ) file-ext rename_file
1.29 crook 1680: ""Rename file @i{c_addr1 u1} to new name @i{c_addr2 u2}""
1.125 anton 1681: wior = rename_file(c_addr1, u1, c_addr2, u2);
1.1 anton 1682:
1.47 anton 1683: file-position ( wfileid -- ud wior ) file file_position
1.1 anton 1684: /* !! use tell and lseek? */
1.108 anton 1685: ud = OFF2UD(ftello((FILE *)wfileid));
1686: wior = IOR(UD2OFF(ud)==-1);
1.1 anton 1687:
1.47 anton 1688: reposition-file ( ud wfileid -- wior ) file reposition_file
1.108 anton 1689: wior = IOR(fseeko((FILE *)wfileid, UD2OFF(ud), SEEK_SET)==-1);
1.1 anton 1690:
1.47 anton 1691: file-size ( wfileid -- ud wior ) file file_size
1.1 anton 1692: struct stat buf;
1693: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
1.108 anton 1694: ud = OFF2UD(buf.st_size);
1.1 anton 1695:
1.47 anton 1696: resize-file ( ud wfileid -- wior ) file resize_file
1.108 anton 1697: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2OFF(ud))==-1);
1.1 anton 1698:
1.47 anton 1699: read-file ( c_addr u1 wfileid -- u2 wior ) file read_file
1.1 anton 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:
1.125 anton 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;
1.1 anton 1713:
1.15 pazsan 1714: \+
1.1 anton 1715:
1.47 anton 1716: write-file ( c_addr u1 wfileid -- wior ) file write_file
1.1 anton 1717: /* !! fwrite does not guarantee enough */
1.39 pazsan 1718: #ifdef HAS_FILE
1.1 anton 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: }
1.39 pazsan 1725: #else
1726: TYPE(c_addr, u1);
1727: #endif
1.17 pazsan 1728:
1.47 anton 1729: emit-file ( c wfileid -- wior ) gforth emit_file
1.17 pazsan 1730: #ifdef HAS_FILE
1.1 anton 1731: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
1732: if (wior)
1733: clearerr((FILE *)wfileid);
1.17 pazsan 1734: #else
1.36 pazsan 1735: PUTC(c);
1.17 pazsan 1736: #endif
1.1 anton 1737:
1.15 pazsan 1738: \+file
1.1 anton 1739:
1.47 anton 1740: flush-file ( wfileid -- wior ) file-ext flush_file
1.1 anton 1741: wior = IOR(fflush((FILE *) wfileid)==EOF);
1742:
1.56 anton 1743: file-status ( c_addr u -- wfam wior ) file-ext file_status
1.125 anton 1744: struct Cellpair r = file_status(c_addr, u);
1745: wfam = r.n1;
1746: wior = r.n2;
1.1 anton 1747:
1.112 pazsan 1748: file-eof? ( wfileid -- flag ) gforth file_eof_query
1749: flag = FLAG(feof((FILE *) wfileid));
1.1 anton 1750:
1.112 pazsan 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[] = {
1.115 anton 1799: #if DIRSEP=='/'
1800: /* Unix */
1.112 pazsan 1801: '\n'
1802: #else
1.115 anton 1803: /* DOS, Win, OS/2 */
1.112 pazsan 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
1.83 pazsan 1848:
1.1 anton 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:
1.47 anton 1852: d>f ( d -- r ) float d_to_f
1.1 anton 1853: #ifdef BUGGY_LONG_LONG
1854: extern double ldexp(double x, int exp);
1.113 anton 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;
1.1 anton 1860: #else
1861: r = d;
1862: #endif
1863:
1.47 anton 1864: f>d ( r -- d ) float f_to_d
1.100 pazsan 1865: extern DCell double2ll(Float r);
1866: d = double2ll(r);
1.1 anton 1867:
1.47 anton 1868: f! ( r f_addr -- ) float f_store
1.52 anton 1869: ""Store @i{r} into the float at address @i{f-addr}.""
1.1 anton 1870: *f_addr = r;
1871:
1.47 anton 1872: f@ ( f_addr -- r ) float f_fetch
1.52 anton 1873: ""@i{r} is the float at address @i{f-addr}.""
1.1 anton 1874: r = *f_addr;
1875:
1.47 anton 1876: df@ ( df_addr -- r ) float-ext d_f_fetch
1.52 anton 1877: ""Fetch the double-precision IEEE floating-point value @i{r} from the address @i{df-addr}.""
1.1 anton 1878: #ifdef IEEE_FP
1879: r = *df_addr;
1880: #else
1881: !! df@
1882: #endif
1883:
1.47 anton 1884: df! ( r df_addr -- ) float-ext d_f_store
1.52 anton 1885: ""Store @i{r} as double-precision IEEE floating-point value to the
1886: address @i{df-addr}.""
1.1 anton 1887: #ifdef IEEE_FP
1888: *df_addr = r;
1889: #else
1890: !! df!
1891: #endif
1892:
1.47 anton 1893: sf@ ( sf_addr -- r ) float-ext s_f_fetch
1.52 anton 1894: ""Fetch the single-precision IEEE floating-point value @i{r} from the address @i{sf-addr}.""
1.1 anton 1895: #ifdef IEEE_FP
1896: r = *sf_addr;
1897: #else
1898: !! sf@
1899: #endif
1900:
1.47 anton 1901: sf! ( r sf_addr -- ) float-ext s_f_store
1.52 anton 1902: ""Store @i{r} as single-precision IEEE floating-point value to the
1903: address @i{sf-addr}.""
1.1 anton 1904: #ifdef IEEE_FP
1905: *sf_addr = r;
1906: #else
1907: !! sf!
1908: #endif
1909:
1.47 anton 1910: f+ ( r1 r2 -- r3 ) float f_plus
1.1 anton 1911: r3 = r1+r2;
1912:
1.47 anton 1913: f- ( r1 r2 -- r3 ) float f_minus
1.1 anton 1914: r3 = r1-r2;
1915:
1.47 anton 1916: f* ( r1 r2 -- r3 ) float f_star
1.1 anton 1917: r3 = r1*r2;
1918:
1.47 anton 1919: f/ ( r1 r2 -- r3 ) float f_slash
1.1 anton 1920: r3 = r1/r2;
1921:
1.47 anton 1922: f** ( r1 r2 -- r3 ) float-ext f_star_star
1.26 crook 1923: ""@i{r3} is @i{r1} raised to the @i{r2}th power.""
1.1 anton 1924: r3 = pow(r1,r2);
1925:
1.47 anton 1926: fnegate ( r1 -- r2 ) float f_negate
1.1 anton 1927: r2 = - r1;
1928:
1.47 anton 1929: fdrop ( r -- ) float f_drop
1.1 anton 1930:
1.47 anton 1931: fdup ( r -- r r ) float f_dupe
1.1 anton 1932:
1.47 anton 1933: fswap ( r1 r2 -- r2 r1 ) float f_swap
1.1 anton 1934:
1.47 anton 1935: fover ( r1 r2 -- r1 r2 r1 ) float f_over
1.1 anton 1936:
1.47 anton 1937: frot ( r1 r2 r3 -- r2 r3 r1 ) float f_rote
1.1 anton 1938:
1.47 anton 1939: fnip ( r1 r2 -- r2 ) gforth f_nip
1.1 anton 1940:
1.47 anton 1941: ftuck ( r1 r2 -- r2 r1 r2 ) gforth f_tuck
1.1 anton 1942:
1.47 anton 1943: float+ ( f_addr1 -- f_addr2 ) float float_plus
1.52 anton 1944: ""@code{1 floats +}.""
1.1 anton 1945: f_addr2 = f_addr1+1;
1946:
1.47 anton 1947: floats ( n1 -- n2 ) float
1.52 anton 1948: ""@i{n2} is the number of address units of @i{n1} floats.""
1.1 anton 1949: n2 = n1*sizeof(Float);
1950:
1.47 anton 1951: floor ( r1 -- r2 ) float
1.26 crook 1952: ""Round towards the next smaller integral value, i.e., round toward negative infinity.""
1.1 anton 1953: /* !! unclear wording */
1954: r2 = floor(r1);
1955:
1.105 anton 1956: fround ( r1 -- r2 ) gforth f_round
1957: ""Round to the nearest integral value.""
1.1 anton 1958: r2 = rint(r1);
1959:
1.47 anton 1960: fmax ( r1 r2 -- r3 ) float f_max
1.1 anton 1961: if (r1<r2)
1962: r3 = r2;
1963: else
1964: r3 = r1;
1965:
1.47 anton 1966: fmin ( r1 r2 -- r3 ) float f_min
1.1 anton 1967: if (r1<r2)
1968: r3 = r1;
1969: else
1970: r3 = r2;
1971:
1.47 anton 1972: represent ( r c_addr u -- n f1 f2 ) float
1.1 anton 1973: char *sig;
1.122 anton 1974: size_t siglen;
1.1 anton 1975: int flag;
1976: int decpt;
1977: sig=ecvt(r, u, &decpt, &flag);
1.122 anton 1978: n=(r==0. ? 1 : decpt);
1.1 anton 1979: f1=FLAG(flag!=0);
1.21 anton 1980: f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
1.122 anton 1981: siglen=strlen(sig);
1.124 anton 1982: if (siglen>u) /* happens in glibc-2.1.3 if 999.. is rounded up */
1983: siglen=u;
1.122 anton 1984: memcpy(c_addr,sig,siglen);
1.123 anton 1985: memset(c_addr+siglen,f2?'0':' ',u-siglen);
1.1 anton 1986:
1.47 anton 1987: >float ( c_addr u -- flag ) float to_float
1.56 anton 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.""
1.1 anton 1994: Float r;
1.125 anton 1995: flag = to_float(c_addr, u, &r);
1996: if (flag) {
1997: IF_fpTOS(fp[0] = fpTOS);
1998: fp += -1;
1999: fpTOS = r;
1.1 anton 2000: }
2001:
1.47 anton 2002: fabs ( r1 -- r2 ) float-ext f_abs
1.1 anton 2003: r2 = fabs(r1);
2004:
1.47 anton 2005: facos ( r1 -- r2 ) float-ext f_a_cos
1.1 anton 2006: r2 = acos(r1);
2007:
1.47 anton 2008: fasin ( r1 -- r2 ) float-ext f_a_sine
1.1 anton 2009: r2 = asin(r1);
2010:
1.47 anton 2011: fatan ( r1 -- r2 ) float-ext f_a_tan
1.1 anton 2012: r2 = atan(r1);
2013:
1.47 anton 2014: fatan2 ( r1 r2 -- r3 ) float-ext f_a_tan_two
1.26 crook 2015: ""@i{r1/r2}=tan(@i{r3}). ANS Forth does not require, but probably
1.1 anton 2016: intends this to be the inverse of @code{fsincos}. In gforth it is.""
2017: r3 = atan2(r1,r2);
2018:
1.47 anton 2019: fcos ( r1 -- r2 ) float-ext f_cos
1.1 anton 2020: r2 = cos(r1);
2021:
1.47 anton 2022: fexp ( r1 -- r2 ) float-ext f_e_x_p
1.1 anton 2023: r2 = exp(r1);
2024:
1.47 anton 2025: fexpm1 ( r1 -- r2 ) float-ext f_e_x_p_m_one
1.1 anton 2026: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
2027: #ifdef HAVE_EXPM1
1.3 pazsan 2028: extern double
2029: #ifdef NeXT
2030: const
2031: #endif
2032: expm1(double);
1.1 anton 2033: r2 = expm1(r1);
2034: #else
2035: r2 = exp(r1)-1.;
2036: #endif
2037:
1.47 anton 2038: fln ( r1 -- r2 ) float-ext f_l_n
1.1 anton 2039: r2 = log(r1);
2040:
1.47 anton 2041: flnp1 ( r1 -- r2 ) float-ext f_l_n_p_one
1.1 anton 2042: ""@i{r2}=ln(@i{r1}+1)""
2043: #ifdef HAVE_LOG1P
1.3 pazsan 2044: extern double
2045: #ifdef NeXT
2046: const
2047: #endif
2048: log1p(double);
1.1 anton 2049: r2 = log1p(r1);
2050: #else
2051: r2 = log(r1+1.);
2052: #endif
2053:
1.47 anton 2054: flog ( r1 -- r2 ) float-ext f_log
1.26 crook 2055: ""The decimal logarithm.""
1.1 anton 2056: r2 = log10(r1);
2057:
1.47 anton 2058: falog ( r1 -- r2 ) float-ext f_a_log
1.1 anton 2059: ""@i{r2}=10**@i{r1}""
2060: extern double pow10(double);
2061: r2 = pow10(r1);
2062:
1.47 anton 2063: fsin ( r1 -- r2 ) float-ext f_sine
1.1 anton 2064: r2 = sin(r1);
2065:
1.47 anton 2066: fsincos ( r1 -- r2 r3 ) float-ext f_sine_cos
1.1 anton 2067: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
2068: r2 = sin(r1);
2069: r3 = cos(r1);
2070:
1.47 anton 2071: fsqrt ( r1 -- r2 ) float-ext f_square_root
1.1 anton 2072: r2 = sqrt(r1);
2073:
1.47 anton 2074: ftan ( r1 -- r2 ) float-ext f_tan
1.1 anton 2075: r2 = tan(r1);
2076: :
2077: fsincos f/ ;
2078:
1.47 anton 2079: fsinh ( r1 -- r2 ) float-ext f_cinch
1.1 anton 2080: r2 = sinh(r1);
2081: :
2082: fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
2083:
1.47 anton 2084: fcosh ( r1 -- r2 ) float-ext f_cosh
1.1 anton 2085: r2 = cosh(r1);
2086: :
2087: fexp fdup 1/f f+ f2/ ;
2088:
1.47 anton 2089: ftanh ( r1 -- r2 ) float-ext f_tan_h
1.1 anton 2090: r2 = tanh(r1);
2091: :
2092: f2* fexpm1 fdup 2. d>f f+ f/ ;
2093:
1.47 anton 2094: fasinh ( r1 -- r2 ) float-ext f_a_cinch
1.1 anton 2095: r2 = asinh(r1);
2096: :
2097: fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
2098:
1.47 anton 2099: facosh ( r1 -- r2 ) float-ext f_a_cosh
1.1 anton 2100: r2 = acosh(r1);
2101: :
2102: fdup fdup f* 1. d>f f- fsqrt f+ fln ;
2103:
1.47 anton 2104: fatanh ( r1 -- r2 ) float-ext f_a_tan_h
1.1 anton 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:
1.47 anton 2110: sfloats ( n1 -- n2 ) float-ext s_floats
1.52 anton 2111: ""@i{n2} is the number of address units of @i{n1}
1.29 crook 2112: single-precision IEEE floating-point numbers.""
1.1 anton 2113: n2 = n1*sizeof(SFloat);
2114:
1.47 anton 2115: dfloats ( n1 -- n2 ) float-ext d_floats
1.52 anton 2116: ""@i{n2} is the number of address units of @i{n1}
1.29 crook 2117: double-precision IEEE floating-point numbers.""
1.1 anton 2118: n2 = n1*sizeof(DFloat);
2119:
1.47 anton 2120: sfaligned ( c_addr -- sf_addr ) float-ext s_f_aligned
1.52 anton 2121: ""@i{sf-addr} is the first single-float-aligned address greater
1.29 crook 2122: than or equal to @i{c-addr}.""
1.1 anton 2123: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
2124: :
2125: [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
2126:
1.47 anton 2127: dfaligned ( c_addr -- df_addr ) float-ext d_f_aligned
1.52 anton 2128: ""@i{df-addr} is the first double-float-aligned address greater
1.29 crook 2129: than or equal to @i{c-addr}.""
1.1 anton 2130: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
2131: :
2132: [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
2133:
1.112 pazsan 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.""
1.125 anton 2138: r = v_star(f_addr1, nstride1, f_addr2, nstride2, ucount);
1.112 pazsan 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""
1.125 anton 2146: faxpy(ra, f_x, nstridex, f_y, nstridey, ucount);
1.112 pazsan 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:
1.1 anton 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
1.112 pazsan 2160:
1.15 pazsan 2161: \+glocals
1.1 anton 2162:
1.110 pazsan 2163: \g locals
2164:
1.68 anton 2165: @local# ( #noffset -- w ) gforth fetch_local_number
2166: w = *(Cell *)(lp+noffset);
1.1 anton 2167:
1.47 anton 2168: @local0 ( -- w ) new fetch_local_zero
1.112 pazsan 2169: w = ((Cell *)lp)[0];
1.1 anton 2170:
1.47 anton 2171: @local1 ( -- w ) new fetch_local_four
1.112 pazsan 2172: w = ((Cell *)lp)[1];
1.1 anton 2173:
1.47 anton 2174: @local2 ( -- w ) new fetch_local_eight
1.112 pazsan 2175: w = ((Cell *)lp)[2];
1.1 anton 2176:
1.47 anton 2177: @local3 ( -- w ) new fetch_local_twelve
1.112 pazsan 2178: w = ((Cell *)lp)[3];
1.1 anton 2179:
1.15 pazsan 2180: \+floating
1.1 anton 2181:
1.68 anton 2182: f@local# ( #noffset -- r ) gforth f_fetch_local_number
2183: r = *(Float *)(lp+noffset);
1.1 anton 2184:
1.47 anton 2185: f@local0 ( -- r ) new f_fetch_local_zero
1.112 pazsan 2186: r = ((Float *)lp)[0];
1.1 anton 2187:
1.47 anton 2188: f@local1 ( -- r ) new f_fetch_local_eight
1.112 pazsan 2189: r = ((Float *)lp)[1];
1.1 anton 2190:
1.15 pazsan 2191: \+
1.1 anton 2192:
1.68 anton 2193: laddr# ( #noffset -- c_addr ) gforth laddr_number
1.1 anton 2194: /* this can also be used to implement lp@ */
1.68 anton 2195: c_addr = (Char *)(lp+noffset);
1.1 anton 2196:
1.68 anton 2197: lp+!# ( #noffset -- ) gforth lp_plus_store_number
1.1 anton 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""
1.68 anton 2201: lp += noffset;
1.1 anton 2202:
1.47 anton 2203: lp- ( -- ) new minus_four_lp_plus_store
1.1 anton 2204: lp += -sizeof(Cell);
2205:
1.47 anton 2206: lp+ ( -- ) new eight_lp_plus_store
1.1 anton 2207: lp += sizeof(Float);
2208:
1.47 anton 2209: lp+2 ( -- ) new sixteen_lp_plus_store
1.1 anton 2210: lp += 2*sizeof(Float);
2211:
1.47 anton 2212: lp! ( c_addr -- ) gforth lp_store
1.1 anton 2213: lp = (Address)c_addr;
2214:
1.47 anton 2215: >l ( w -- ) gforth to_l
1.1 anton 2216: lp -= sizeof(Cell);
2217: *(Cell *)lp = w;
2218:
1.15 pazsan 2219: \+floating
1.1 anton 2220:
1.47 anton 2221: f>l ( r -- ) gforth f_to_l
1.1 anton 2222: lp -= sizeof(Float);
2223: *(Float *)lp = r;
2224:
1.47 anton 2225: fpick ( u -- r ) gforth
1.52 anton 2226: ""Actually the stack effect is @code{ r0 ... ru u -- r0 ... ru r0 }.""
1.11 anton 2227: r = fp[u+1]; /* +1, because update of fp happens before this fragment */
2228: :
2229: floats fp@ + f@ ;
2230:
1.15 pazsan 2231: \+
2232: \+
1.1 anton 2233:
1.15 pazsan 2234: \+OS
1.1 anton 2235:
1.110 pazsan 2236: \g syslib
2237:
1.131 pazsan 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:
1.136 pazsan 2267: av-start-void ( c_addr -- ) gforth av_start_void
1.131 pazsan 2268: av_start_void(alist, c_addr);
2269:
1.136 pazsan 2270: av-start-int ( c_addr -- ) gforth av_start_int
1.131 pazsan 2271: av_start_int(alist, c_addr, &irv);
2272:
1.136 pazsan 2273: av-start-float ( c_addr -- ) gforth av_start_float
1.131 pazsan 2274: av_start_float(alist, c_addr, &frv);
2275:
1.136 pazsan 2276: av-start-double ( c_addr -- ) gforth av_start_double
1.131 pazsan 2277: av_start_double(alist, c_addr, &drv);
2278:
1.136 pazsan 2279: av-start-longlong ( c_addr -- ) gforth av_start_longlong
1.131 pazsan 2280: av_start_longlong(alist, c_addr, &llrv);
2281:
1.136 pazsan 2282: av-start-ptr ( c_addr -- ) gforth av_start_ptr
1.131 pazsan 2283: av_start_ptr(alist, c_addr, void*, &prv);
2284:
2285: av-int ( w -- ) gforth av_int
2286: av_int(alist, w);
2287:
1.136 pazsan 2288: av-float ( r -- ) gforth av_float
1.131 pazsan 2289: av_float(alist, r);
2290:
1.136 pazsan 2291: av-double ( r -- ) gforth av_double
1.131 pazsan 2292: av_double(alist, r);
2293:
1.136 pazsan 2294: av-longlong ( d -- ) gforth av_longlong
1.131 pazsan 2295: av_longlong(alist, d);
2296:
1.136 pazsan 2297: av-ptr ( c_addr -- ) gforth av_ptr
1.131 pazsan 2298: av_ptr(alist, void*, c_addr);
2299:
1.136 pazsan 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
1.131 pazsan 2320: SAVE_REGS
2321: av_call(alist);
2322: REST_REGS
2323:
1.136 pazsan 2324: av-call-int ( -- w ) gforth av_call_int
1.131 pazsan 2325: SAVE_REGS
2326: av_call(alist);
1.134 pazsan 2327: REST_REGS
1.131 pazsan 2328: w = irv;
2329:
1.136 pazsan 2330: av-call-float ( -- r ) gforth av_call_float
1.131 pazsan 2331: SAVE_REGS
2332: av_call(alist);
2333: REST_REGS
2334: r = frv;
2335:
1.136 pazsan 2336: av-call-double ( -- r ) gforth av_call_double
1.131 pazsan 2337: SAVE_REGS
2338: av_call(alist);
2339: REST_REGS
2340: r = drv;
2341:
1.136 pazsan 2342: av-call-longlong ( -- d ) gforth av_call_longlong
1.131 pazsan 2343: SAVE_REGS
2344: av_call(alist);
2345: REST_REGS
2346: d = llrv;
2347:
1.136 pazsan 2348: av-call-ptr ( -- c_addr ) gforth av_call_ptr
1.131 pazsan 2349: SAVE_REGS
2350: av_call(alist);
2351: REST_REGS
2352: c_addr = prv;
2353:
1.135 pazsan 2354: alloc-callback ( a_ip -- c_addr ) gforth alloc_callback
2355: c_addr = (char *)alloc_callback(engine_callback, (Xt *)a_ip);
1.131 pazsan 2356:
1.135 pazsan 2357: va-start-void ( -- ) gforth va_start_void
2358: va_start_void(clist);
1.131 pazsan 2359:
1.135 pazsan 2360: va-start-int ( -- ) gforth va_start_int
2361: va_start_int(clist);
1.131 pazsan 2362:
1.135 pazsan 2363: va-start-longlong ( -- ) gforth va_start_longlong
2364: va_start_longlong(clist);
1.131 pazsan 2365:
1.135 pazsan 2366: va-start-ptr ( -- ) gforth va_start_ptr
2367: va_start_ptr(clist, (char *));
1.131 pazsan 2368:
1.135 pazsan 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);
1.131 pazsan 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:
1.1 anton 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,
1.47 anton 2435: `icall$1 ( argflist($1)u -- uret ) gforth
1.9 pazsan 2436: uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
1.1 anton 2437:
2438: ')
2439: define(fcall,
1.47 anton 2440: `fcall$1 ( argflist($1)u -- rret ) gforth
1.9 pazsan 2441: rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
1.1 anton 2442:
2443: ')
2444:
1.46 pazsan 2445: \ close ' to keep fontify happy
1.1 anton 2446:
2447: uploop(i, 0, 7, `icall(i)')
2448: icall(20)
2449: uploop(i, 0, 7, `fcall(i)')
2450: fcall(20)
2451:
1.15 pazsan 2452: \+
1.131 pazsan 2453: \+
1.1 anton 2454:
1.47 anton 2455: wcall ( u -- ) gforth
1.64 anton 2456: IF_fpTOS(fp[0]=fpTOS);
1.34 jwilke 2457: FP=fp;
1.95 pazsan 2458: sp=(Cell*)(SYSCALL(Cell*(*)(Cell *, void *))u)(sp, &FP);
1.34 jwilke 2459: fp=FP;
1.64 anton 2460: IF_spTOS(spTOS=sp[0];)
2461: IF_fpTOS(fpTOS=fp[0]);
1.46 pazsan 2462:
1.112 pazsan 2463: \+peephole
2464:
2465: \g peephole
2466:
1.119 anton 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""
1.121 anton 2482: a_prim = (Cell *)decompile_code((Label)a_code);
1.119 anton 2483:
1.112 pazsan 2484: \ set-next-code and call2 do not appear in images and can be
2485: \ renumbered arbitrarily
1.46 pazsan 2486:
1.112 pazsan 2487: set-next-code ( #w -- ) gforth set_next_code
2488: #ifdef NO_IP
2489: next_code = (Label)w;
2490: #endif
1.34 jwilke 2491:
1.112 pazsan 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);
1.45 anton 2497: #else
1.112 pazsan 2498: assert(0);
1.45 anton 2499: #endif
1.131 pazsan 2500:
2501: tag-offsets ( -- a_addr ) gforth tag_offsets
2502: extern Cell groups[32];
2503: a_addr = groups;
1.51 anton 2504:
1.54 pazsan 2505: \+
1.128 anton 2506:
2507: \g static_super
2508:
1.130 anton 2509: \C #if !defined(GFORTH_DEBUGGING) && !defined(INDIRECT_THREADED) && !defined(DOUBLY_INDIRECT) && !defined(VM_PROFILING)
2510:
1.112 pazsan 2511: include(peeprules.vmg)
1.130 anton 2512:
2513: \C #endif
1.54 pazsan 2514:
1.112 pazsan 2515: \g end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>