File:  [gforth] / gforth / prim
Revision 1.99: download - view: text, annotated - select for diffs
Tue Sep 24 16:50:28 2002 UTC (21 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added absolute versions of all relative branches
  I am not sure if I got conditional compilation right.
  I left the Forth code for acondbranch alone because I don't know
    what may be used there.

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

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