File:  [gforth] / gforth / prim
Revision 1.103: download - view: text, annotated - select for diffs
Tue Dec 3 10:47:49 2002 UTC (16 years, 6 months ago) by anton
Branches: MAIN
CVS tags: HEAD
New word: "MEM,"
new implementations of CLITERAL (independent of threaded code size) and
   SLITERAL (also can deal with long strings now)

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>