File:  [gforth] / gforth / prim
Revision 1.22: download - view: text, annotated - select for diffs
Wed Feb 3 00:10:21 1999 UTC (20 years, 9 months ago) by crook
Branches: MAIN
CVS tags: HEAD
New "docclean" target for makefile (removes glossary dependencies when
rebuilding documentation). Changes to .fs files and prim are restricted
to glossary (\G) additions for the documentation; this has necessitated
the addition of new white-space in places to stop the \G stuff from
obscuring the code. Many additions to doc/gforth.ds - new sections
added, a few things moved and some sections re-written slightly. There
are a set of things to tidy up before this rev. is suitable for
release, and those will be my highest priority. I have also used
"@comment TODO" to highlight other sections I plan to work on, and
added a set of comments at the start to indicate other things I plan
to modify in the medium-term.

    1: \ Gforth primitives
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998 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., 675 Mass Ave, Cambridge, MA 02139, 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: \ prims2x is pedantic about tabs vs. blanks. The fields of the first
   36: \ line of a primitive are separated by tabs, the stack items in a
   37: \ stack effect by blanks.
   38: \
   39: \ Both pronounciation and stack items (in the stack effect) must
   40: \ conform to the C name syntax or the C compiler will complain.
   41: \ 
   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: \ 
   58: \ 
   59: \ The stack variables have the following types:
   60: \ 
   61: \ name matches	type
   62: \ f.*		Bool
   63: \ c.*		Char
   64: \ [nw].*		Cell
   65: \ u.*		UCell
   66: \ d.*		DCell
   67: \ ud.*		UDCell
   68: \ r.*		Float
   69: \ a_.*		Cell *
   70: \ c_.*		Char *
   71: \ f_.*		Float *
   72: \ df_.*		DFloat *
   73: \ sf_.*		SFloat *
   74: \ xt.*		XT
   75: \ wid.*		WID
   76: \ f83name.*	F83Name *
   77: \ 
   78: \ 
   79: \ 
   80: \ In addition the following names can be used:
   81: \ ip	the instruction pointer
   82: \ sp	the data stack pointer
   83: \ rp	the parameter stack pointer
   84: \ lp	the locals stack pointer
   85: \ NEXT	executes NEXT
   86: \ cfa	
   87: \ NEXT1	executes NEXT1
   88: \ FLAG(x)	makes a Forth flag from a C flag
   89: \ 
   90: \ 
   91: \ 
   92: \ Percentages in comments are from Koopmans book: average/maximum use
   93: \ (taken from four, not very representative benchmarks)
   94: \ 
   95: \ 
   96: \ 
   97: \ To do:
   98: \ 
   99: \ throw execute, cfa and NEXT1 out?
  100: \ macroize *ip, ip++, *ip++ (pipelining)?
  101: 
  102: \ these m4 macros would collide with identifiers
  103: undefine(`index')
  104: undefine(`shift')
  105: 
  106: noop	--		gforth
  107: ;
  108: :
  109:  ;
  110: 
  111: lit	-- w		gforth
  112: w = (Cell)NEXT_INST;
  113: INC_IP(1);
  114: :
  115:  r> dup @ swap cell+ >r ;
  116: 
  117: execute		xt --		core
  118: ""Perform the semantics represented by the execution token, xt.""
  119: ip=IP;
  120: IF_TOS(TOS = sp[0]);
  121: EXEC(xt);
  122: 
  123: perform		a_addr --	gforth
  124: ""Equivalent to @code{@ execute}.""
  125: /* and pfe */
  126: ip=IP;
  127: IF_TOS(TOS = sp[0]);
  128: EXEC(*(Xt *)a_addr);
  129: :
  130:  @ execute ;
  131: 
  132: \+glocals
  133: 
  134: branch-lp+!#	--	gforth	branch_lp_plus_store_number
  135: /* this will probably not be used */
  136: branch_adjust_lp:
  137: lp += (Cell)(IP[1]);
  138: goto branch;
  139: 
  140: \+
  141: 
  142: branch	--		gforth
  143: branch:
  144: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
  145: NEXT_P0;
  146: :
  147:  r> dup @ + >r ;
  148: 
  149: \ condbranch(forthname,restline,code,forthcode)
  150: \ this is non-syntactical: code must open a brace that is closed by the macro
  151: define(condbranch,
  152: $1	$2
  153: $3	ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
  154:         NEXT_P0;
  155: 	NEXT;
  156: }
  157: else
  158:     INC_IP(1);
  159: $4
  160: 
  161: \+glocals
  162: 
  163: $1-lp+!#	$2_lp_plus_store_number
  164: $3    goto branch_adjust_lp;
  165: }
  166: else
  167:     INC_IP(2);
  168: 
  169: \+
  170: )
  171: 
  172: condbranch(?branch,f --		f83	question_branch,
  173: if (f==0) {
  174:     IF_TOS(TOS = sp[0]);
  175: ,:
  176:  0= dup     \ !f !f
  177:  r> dup @   \ !f !f IP branchoffset
  178:  rot and +  \ !f IP|IP+branchoffset
  179:  swap 0= cell and + \ IP''
  180:  >r ;)
  181: 
  182: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
  183: \ is only used in if's (yet)
  184: 
  185: \+xconds
  186: 
  187: ?dup-?branch	f -- f	new	question_dupe_question_branch
  188: ""The run-time procedure compiled by @code{?DUP-IF}.""
  189: if (f==0) {
  190:   sp++;
  191:   IF_TOS(TOS = sp[0]);
  192:   ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
  193:   NEXT_P0;
  194:   NEXT;
  195: }
  196: else
  197:   INC_IP(1);
  198: 
  199: ?dup-0=-?branch	f --	new	question_dupe_zero_equals_question_branch
  200: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
  201: /* the approach taken here of declaring the word as having the stack
  202: effect ( f -- ) and correcting for it in the branch-taken case costs a
  203: few cycles in that case, but is easy to convert to a CONDBRANCH
  204: invocation */
  205: if (f!=0) {
  206:   sp--;
  207:   ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
  208:   NEXT_P0;
  209:   NEXT;
  210: }
  211: else
  212:   INC_IP(1);
  213: 
  214: \+
  215: 
  216: condbranch((next),--		cmFORTH	paren_next,
  217: if ((*rp)--) {
  218: ,:
  219:  r> r> dup 1- >r
  220:  IF dup @ + >r ELSE cell+ >r THEN ;)
  221: 
  222: condbranch((loop),--		gforth	paren_loop,
  223: Cell index = *rp+1;
  224: Cell limit = rp[1];
  225: if (index != limit) {
  226:     *rp = index;
  227: ,:
  228:  r> r> 1+ r> 2dup =
  229:  IF >r 1- >r cell+ >r
  230:  ELSE >r >r dup @ + >r THEN ;)
  231: 
  232: condbranch((+loop),n --		gforth	paren_plus_loop,
  233: /* !! check this thoroughly */
  234: Cell index = *rp;
  235: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
  236: /* dependent upon two's complement arithmetic */
  237: Cell olddiff = index-rp[1];
  238: if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
  239:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
  240: #ifdef i386
  241:     *rp += n;
  242: #else
  243:     *rp = index + n;
  244: #endif
  245:     IF_TOS(TOS = sp[0]);
  246: ,:
  247:  r> swap
  248:  r> r> 2dup - >r
  249:  2 pick r@ + r@ xor 0< 0=
  250:  3 pick r> xor 0< 0= or
  251:  IF    >r + >r dup @ + >r
  252:  ELSE  >r >r drop cell+ >r THEN ;)
  253: 
  254: \+xconds
  255: 
  256: condbranch((-loop),u --		gforth	paren_minus_loop,
  257: /* !! check this thoroughly */
  258: Cell index = *rp;
  259: UCell olddiff = index-rp[1];
  260: if (olddiff>u) {
  261: #ifdef i386
  262:     *rp -= u;
  263: #else
  264:     *rp = index - u;
  265: #endif
  266:     IF_TOS(TOS = sp[0]);
  267: ,)
  268: 
  269: condbranch((s+loop),n --		gforth	paren_symmetric_plus_loop,
  270: ""The run-time procedure compiled by S+LOOP. It loops until the index
  271: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
  272: version of (+LOOP).""
  273: /* !! check this thoroughly */
  274: Cell index = *rp;
  275: Cell diff = index-rp[1];
  276: Cell newdiff = diff+n;
  277: if (n<0) {
  278:     diff = -diff;
  279:     newdiff = -newdiff;
  280: }
  281: if (diff>=0 || newdiff<0) {
  282: #ifdef i386
  283:     *rp += n;
  284: #else
  285:     *rp = index + n;
  286: #endif
  287:     IF_TOS(TOS = sp[0]);
  288: ,)
  289: 
  290: \+
  291: 
  292: unloop		--	core
  293: rp += 2;
  294: :
  295:  r> rdrop rdrop >r ;
  296: 
  297: (for)	ncount --		cmFORTH		paren_for
  298: /* or (for) = >r -- collides with unloop! */
  299: *--rp = 0;
  300: *--rp = ncount;
  301: :
  302:  r> swap 0 >r >r >r ;
  303: 
  304: (do)	nlimit nstart --		gforth		paren_do
  305: /* or do it in high-level? 0.09/0.23% */
  306: *--rp = nlimit;
  307: *--rp = nstart;
  308: :
  309:  r> swap rot >r >r >r ;
  310: 
  311: (?do)	nlimit nstart --	gforth	paren_question_do
  312: *--rp = nlimit;
  313: *--rp = nstart;
  314: if (nstart == nlimit) {
  315:     IF_TOS(TOS = sp[0]);
  316:     goto branch;
  317:     }
  318: else {
  319:     INC_IP(1);
  320: }
  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)	nlimit nstart --	gforth	paren_plus_do
  332: *--rp = nlimit;
  333: *--rp = nstart;
  334: if (nstart >= nlimit) {
  335:     IF_TOS(TOS = sp[0]);
  336:     goto branch;
  337:     }
  338: else {
  339:     INC_IP(1);
  340: }
  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)	ulimit ustart --	gforth	paren_u_plus_do
  352: *--rp = ulimit;
  353: *--rp = ustart;
  354: if (ustart >= ulimit) {
  355:     IF_TOS(TOS = sp[0]);
  356:     goto branch;
  357:     }
  358: else {
  359:     INC_IP(1);
  360: }
  361: :
  362:  swap 2dup
  363:  r> swap >r swap >r
  364:  u>=
  365:  IF
  366:      dup @ +
  367:  ELSE
  368:      cell+
  369:  THEN  >r ;
  370: 
  371: (-do)	nlimit nstart --	gforth	paren_minus_do
  372: *--rp = nlimit;
  373: *--rp = nstart;
  374: if (nstart <= nlimit) {
  375:     IF_TOS(TOS = sp[0]);
  376:     goto branch;
  377:     }
  378: else {
  379:     INC_IP(1);
  380: }
  381: :
  382:  swap 2dup
  383:  r> swap >r swap >r
  384:  <=
  385:  IF
  386:      dup @ +
  387:  ELSE
  388:      cell+
  389:  THEN  >r ;
  390: 
  391: (u-do)	ulimit ustart --	gforth	paren_u_minus_do
  392: *--rp = ulimit;
  393: *--rp = ustart;
  394: if (ustart <= ulimit) {
  395:     IF_TOS(TOS = sp[0]);
  396:     goto branch;
  397:     }
  398: else {
  399:     INC_IP(1);
  400: }
  401: :
  402:  swap 2dup
  403:  r> swap >r swap >r
  404:  u<=
  405:  IF
  406:      dup @ +
  407:  ELSE
  408:      cell+
  409:  THEN  >r ;
  410: 
  411: \+
  412: 
  413: \ don't make any assumptions where the return stack is!!
  414: \ implement this in machine code if it should run quickly!
  415: 
  416: i	-- n		core
  417: n = *rp;
  418: :
  419: \ rp@ cell+ @ ;
  420:   r> r> tuck >r >r ;
  421: 
  422: i'	-- w		gforth		i_tick
  423: ""loop end value""
  424: w = rp[1];
  425: :
  426: \ rp@ cell+ cell+ @ ;
  427:   r> r> r> dup itmp ! >r >r >r itmp @ ;
  428: variable itmp
  429: 
  430: j	-- n		core
  431: n = rp[2];
  432: :
  433: \ rp@ cell+ cell+ cell+ @ ;
  434:   r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
  435: [IFUNDEF] itmp variable itmp [THEN]
  436: 
  437: k	-- n		gforth
  438: n = rp[4];
  439: :
  440: \ rp@ [ 5 cells ] Literal + @ ;
  441:   r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
  442: [IFUNDEF] itmp variable itmp [THEN]
  443: 
  444: \ digit is high-level: 0/0%
  445: 
  446: move	c_from c_to ucount --		core
  447: "" If ucount>0, copy the contents of ucount address units
  448: at c-from to c-to. @code{move} chooses its copy direction
  449: to avoid problems when c-from, c-to overlap.""
  450: memmove(c_to,c_from,ucount);
  451: /* make an Ifdef for bsd and others? */
  452: :
  453:  >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
  454: 
  455: cmove	c_from c_to u --	string
  456: "" If u>0, copy the contents of ucount characters from
  457: data space at c-from to c-to. The copy proceeds @code{char}-by-@code{char}
  458: from low address to high address.""
  459: while (u-- > 0)
  460:   *c_to++ = *c_from++;
  461: :
  462:  bounds ?DO  dup c@ I c! 1+  LOOP  drop ;
  463: 
  464: cmove>	c_from c_to u --	string	c_move_up
  465: "" If u>0, copy the contents of ucount characters from
  466: data space at c-from to c-to. The copy proceeds @code{char}-by-@code{char}
  467: from high address to low address.""
  468: while (u-- > 0)
  469:   c_to[u] = c_from[u];
  470: :
  471:  dup 0= IF  drop 2drop exit  THEN
  472:  rot over + -rot bounds swap 1-
  473:  DO  1- dup c@ I c!  -1 +LOOP  drop ;
  474: 
  475: fill	c_addr u c --	core
  476: "" If u>0, store character c in each of u consecutive
  477: @code{char} addresses in memory, starting at address c-addr.""
  478: memset(c_addr,c,u);
  479: :
  480:  -rot bounds
  481:  ?DO  dup I c!  LOOP  drop ;
  482: 
  483: compare		c_addr1 u1 c_addr2 u2 -- n	string
  484: ""Compare two strings lexicographically. If they are equal, n is 0; if
  485: the first string is smaller, n is -1; if the first string is larger, n
  486: is 1. Currently this is based on the machine's character
  487: comparison. In the future, this may change to considering the current
  488: locale and its collation order.""
  489: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
  490: if (n==0)
  491:   n = u1-u2;
  492: if (n<0)
  493:   n = -1;
  494: else if (n>0)
  495:   n = 1;
  496: :
  497:  rot 2dup - >r min swap -text dup
  498:  IF    rdrop
  499:  ELSE  drop r@ 0>
  500:        IF    rdrop -1
  501:        ELSE  r> 1 and
  502:        THEN
  503:  THEN ;
  504: 
  505: -text		c_addr1 u c_addr2 -- n	new	dash_text
  506: n = memcmp(c_addr1, c_addr2, u);
  507: if (n<0)
  508:   n = -1;
  509: else if (n>0)
  510:   n = 1;
  511: :
  512:  swap bounds
  513:  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
  514:  ELSE  c@ I c@ - unloop  THEN  -text-flag ;
  515: : -text-flag ( n -- -1/0/1 )
  516:  dup 0< IF  drop -1  ELSE  0>  1 and  THEN  ;
  517: 
  518: toupper	c1 -- c2	gforth
  519: c2 = toupper(c1);
  520: :
  521:  dup [char] a - [ char z char a - 1 + ] Literal u<  bl and - ;
  522: 
  523: capscomp	c_addr1 u c_addr2 -- n	new
  524: n = memcasecmp(c_addr1, c_addr2, u); /* !! use something that works in all locales */
  525: if (n<0)
  526:   n = -1;
  527: else if (n>0)
  528:   n = 1;
  529: :
  530:  swap bounds
  531:  ?DO  dup c@ I c@ <>
  532:      IF  dup c@ toupper I c@ toupper =
  533:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
  534:  ELSE  c@ toupper I c@ toupper - unloop  THEN  -text-flag ;
  535: 
  536: -trailing	c_addr u1 -- c_addr u2		string	dash_trailing
  537: u2 = u1;
  538: while (u2>0 && c_addr[u2-1] == ' ')
  539:   u2--;
  540: :
  541:  BEGIN  1- 2dup + c@ bl =  WHILE
  542:         dup  0= UNTIL  ELSE  1+  THEN ;
  543: 
  544: /string		c_addr1 u1 n -- c_addr2 u2	string	slash_string
  545: c_addr2 = c_addr1+n;
  546: u2 = u1-n;
  547: :
  548:  tuck - >r + r> dup 0< IF  - 0  THEN ;
  549: 
  550: +	n1 n2 -- n		core	plus
  551: n = n1+n2;
  552: 
  553: \ PFE-0.9.14 has it differently, but the next release will have it as follows
  554: under+	n1 n2 n3 -- n n2	gforth	under_plus
  555: ""add @var{n3} to @var{n1} (giving @var{n})""
  556: n = n1+n3;
  557: :
  558:  rot + swap ;
  559: 
  560: -	n1 n2 -- n		core	minus
  561: n = n1-n2;
  562: :
  563:  negate + ;
  564: 
  565: negate	n1 -- n2		core
  566: /* use minus as alias */
  567: n2 = -n1;
  568: :
  569:  invert 1+ ;
  570: 
  571: 1+	n1 -- n2		core		one_plus
  572: n2 = n1+1;
  573: :
  574:  1 + ;
  575: 
  576: 1-	n1 -- n2		core		one_minus
  577: n2 = n1-1;
  578: :
  579:  1 - ;
  580: 
  581: max	n1 n2 -- n	core
  582: if (n1<n2)
  583:   n = n2;
  584: else
  585:   n = n1;
  586: :
  587:  2dup < IF swap THEN drop ;
  588: 
  589: min	n1 n2 -- n	core
  590: if (n1<n2)
  591:   n = n1;
  592: else
  593:   n = n2;
  594: :
  595:  2dup > IF swap THEN drop ;
  596: 
  597: abs	n1 -- n2	core
  598: if (n1<0)
  599:   n2 = -n1;
  600: else
  601:   n2 = n1;
  602: :
  603:  dup 0< IF negate THEN ;
  604: 
  605: *	n1 n2 -- n		core	star
  606: n = n1*n2;
  607: :
  608:  um* drop ;
  609: 
  610: /	n1 n2 -- n		core	slash
  611: n = n1/n2;
  612: :
  613:  /mod nip ;
  614: 
  615: mod	n1 n2 -- n		core
  616: n = n1%n2;
  617: :
  618:  /mod drop ;
  619: 
  620: /mod	n1 n2 -- n3 n4		core		slash_mod
  621: n4 = n1/n2;
  622: n3 = n1%n2; /* !! is this correct? look into C standard! */
  623: :
  624:  >r s>d r> fm/mod ;
  625: 
  626: 2*	n1 -- n2		core		two_star
  627: n2 = 2*n1;
  628: :
  629:  dup + ;
  630: 
  631: 2/	n1 -- n2		core		two_slash
  632: /* !! is this still correct? */
  633: n2 = n1>>1;
  634: :
  635:  dup MINI and IF 1 ELSE 0 THEN
  636:  [ bits/byte cell * 1- ] literal 
  637:  0 DO 2* swap dup 2* >r MINI and 
  638:      IF 1 ELSE 0 THEN or r> swap
  639:  LOOP nip ;
  640: 
  641: fm/mod	d1 n1 -- n2 n3		core		f_m_slash_mod
  642: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
  643: #ifdef BUGGY_LONG_LONG
  644: DCell r = fmdiv(d1,n1);
  645: n2=r.hi;
  646: n3=r.lo;
  647: #else
  648: /* assumes that the processor uses either floored or symmetric division */
  649: n3 = d1/n1;
  650: n2 = d1%n1;
  651: /* note that this 1%-3>0 is optimized by the compiler */
  652: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
  653:   n3--;
  654:   n2+=n1;
  655: }
  656: #endif
  657: :
  658:  dup >r dup 0< IF  negate >r dnegate r>  THEN
  659:  over       0< IF  tuck + swap  THEN
  660:  um/mod
  661:  r> 0< IF  swap negate swap  THEN ;
  662: 
  663: sm/rem	d1 n1 -- n2 n3		core		s_m_slash_rem
  664: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
  665: #ifdef BUGGY_LONG_LONG
  666: DCell r = smdiv(d1,n1);
  667: n2=r.hi;
  668: n3=r.lo;
  669: #else
  670: /* assumes that the processor uses either floored or symmetric division */
  671: n3 = d1/n1;
  672: n2 = d1%n1;
  673: /* note that this 1%-3<0 is optimized by the compiler */
  674: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
  675:   n3++;
  676:   n2-=n1;
  677: }
  678: #endif
  679: :
  680:  over >r dup >r abs -rot
  681:  dabs rot um/mod
  682:  r> r@ xor 0< IF       negate       THEN
  683:  r>        0< IF  swap negate swap  THEN ;
  684: 
  685: m*	n1 n2 -- d		core	m_star
  686: #ifdef BUGGY_LONG_LONG
  687: d = mmul(n1,n2);
  688: #else
  689: d = (DCell)n1 * (DCell)n2;
  690: #endif
  691: :
  692:  2dup      0< and >r
  693:  2dup swap 0< and >r
  694:  um* r> - r> - ;
  695: 
  696: um*	u1 u2 -- ud		core	u_m_star
  697: /* use u* as alias */
  698: #ifdef BUGGY_LONG_LONG
  699: ud = ummul(u1,u2);
  700: #else
  701: ud = (UDCell)u1 * (UDCell)u2;
  702: #endif
  703: :
  704:    >r >r 0 0 r> r> [ 8 cells ] literal 0
  705:    DO
  706:        over >r dup >r 0< and d2*+ drop
  707:        r> 2* r> swap
  708:    LOOP 2drop ;
  709: : d2*+ ( ud n -- ud+n c )
  710:    over MINI
  711:    and >r >r 2dup d+ swap r> + swap r> ;
  712: 
  713: um/mod	ud u1 -- u2 u3		core	u_m_slash_mod
  714: #ifdef BUGGY_LONG_LONG
  715: UDCell r = umdiv(ud,u1);
  716: u2=r.hi;
  717: u3=r.lo;
  718: #else
  719: u3 = ud/u1;
  720: u2 = ud%u1;
  721: #endif
  722: :
  723:    0 swap [ 8 cells 1 + ] literal 0
  724:    ?DO /modstep
  725:    LOOP drop swap 1 rshift or swap ;
  726: : /modstep ( ud c R: u -- ud-?u c R: u )
  727:    >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN  d2*+ r> ;
  728: : d2*+ ( ud n -- ud+n c )
  729:    over MINI
  730:    and >r >r 2dup d+ swap r> + swap r> ;
  731: 
  732: m+	d1 n -- d2		double		m_plus
  733: #ifdef BUGGY_LONG_LONG
  734: d2.lo = d1.lo+n;
  735: d2.hi = d1.hi - (n<0) + (d2.lo<d1.lo);
  736: #else
  737: d2 = d1+n;
  738: #endif
  739: :
  740:  s>d d+ ;
  741: 
  742: d+	d1 d2 -- d		double	d_plus
  743: #ifdef BUGGY_LONG_LONG
  744: d.lo = d1.lo+d2.lo;
  745: d.hi = d1.hi + d2.hi + (d.lo<d1.lo);
  746: #else
  747: d = d1+d2;
  748: #endif
  749: :
  750:  rot + >r tuck + swap over u> r> swap - ;
  751: 
  752: d-	d1 d2 -- d		double		d_minus
  753: #ifdef BUGGY_LONG_LONG
  754: d.lo = d1.lo - d2.lo;
  755: d.hi = d1.hi-d2.hi-(d1.lo<d2.lo);
  756: #else
  757: d = d1-d2;
  758: #endif
  759: :
  760:  dnegate d+ ;
  761: 
  762: dnegate	d1 -- d2		double
  763: /* use dminus as alias */
  764: #ifdef BUGGY_LONG_LONG
  765: d2 = dnegate(d1);
  766: #else
  767: d2 = -d1;
  768: #endif
  769: :
  770:  invert swap negate tuck 0= - ;
  771: 
  772: d2*	d1 -- d2		double		d_two_star
  773: #ifdef BUGGY_LONG_LONG
  774: d2.lo = d1.lo<<1;
  775: d2.hi = (d1.hi<<1) | (d1.lo>>(CELL_BITS-1));
  776: #else
  777: d2 = 2*d1;
  778: #endif
  779: :
  780:  2dup d+ ;
  781: 
  782: d2/	d1 -- d2		double		d_two_slash
  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
  802: w = w1^w2;
  803: 
  804: invert	w1 -- w2		core
  805: w2 = ~w1;
  806: :
  807:  MAXU xor ;
  808: 
  809: rshift	u1 n -- u2		core
  810:   u2 = u1>>n;
  811: :
  812:     0 ?DO 2/ MAXI and LOOP ;
  813: 
  814: lshift	u1 n -- u2		core
  815:   u2 = u1<<n;
  816: :
  817:     0 ?DO 2* LOOP ;
  818: 
  819: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
  820: define(comparisons,
  821: $1=	$2 -- f		$6	$3equals
  822: f = FLAG($4==$5);
  823: :
  824:     [ char $1x char 0 = [IF]
  825: 	] IF false ELSE true THEN [
  826:     [ELSE]
  827: 	] xor 0= [
  828:     [THEN] ] ;
  829: 
  830: $1<>	$2 -- f		$7	$3different
  831: f = FLAG($4!=$5);
  832: :
  833:     [ char $1x char 0 = [IF]
  834: 	] IF true ELSE false THEN [
  835:     [ELSE]
  836: 	] xor 0<> [
  837:     [THEN] ] ;
  838: 
  839: $1<	$2 -- f		$8	$3less
  840: f = FLAG($4<$5);
  841: :
  842:     [ char $1x char 0 = [IF]
  843: 	] MINI and 0<> [
  844:     [ELSE] char $1x char u = [IF]
  845: 	]   2dup xor 0<  IF nip ELSE - THEN 0<  [
  846: 	[ELSE]
  847: 	    ] MINI xor >r MINI xor r> u< [
  848: 	[THEN]
  849:     [THEN] ] ;
  850: 
  851: $1>	$2 -- f		$9	$3greater
  852: f = FLAG($4>$5);
  853: :
  854:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
  855:     $1< ;
  856: 
  857: $1<=	$2 -- f		gforth	$3less_or_equal
  858: f = FLAG($4<=$5);
  859: :
  860:     $1> 0= ;
  861: 
  862: $1>=	$2 -- f		gforth	$3greater_or_equal
  863: f = FLAG($4>=$5);
  864: :
  865:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
  866:     $1<= ;
  867: 
  868: )
  869: 
  870: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
  871: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
  872: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
  873: 
  874: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
  875: define(dcomparisons,
  876: $1=	$2 -- f		$6	$3equals
  877: #ifdef BUGGY_LONG_LONG
  878: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
  879: #else
  880: f = FLAG($4==$5);
  881: #endif
  882: 
  883: $1<>	$2 -- f		$7	$3different
  884: #ifdef BUGGY_LONG_LONG
  885: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
  886: #else
  887: f = FLAG($4!=$5);
  888: #endif
  889: 
  890: $1<	$2 -- f		$8	$3less
  891: #ifdef BUGGY_LONG_LONG
  892: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
  893: #else
  894: f = FLAG($4<$5);
  895: #endif
  896: 
  897: $1>	$2 -- f		$9	$3greater
  898: #ifdef BUGGY_LONG_LONG
  899: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
  900: #else
  901: f = FLAG($4>$5);
  902: #endif
  903: 
  904: $1<=	$2 -- f		gforth	$3less_or_equal
  905: #ifdef BUGGY_LONG_LONG
  906: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
  907: #else
  908: f = FLAG($4<=$5);
  909: #endif
  910: 
  911: $1>=	$2 -- f		gforth	$3greater_or_equal
  912: #ifdef BUGGY_LONG_LONG
  913: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
  914: #else
  915: f = FLAG($4>=$5);
  916: #endif
  917: 
  918: )
  919: 
  920: \+dcomps
  921: 
  922: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
  923: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
  924: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
  925: 
  926: \+
  927: 
  928: within	u1 u2 u3 -- f		core-ext
  929: f = FLAG(u1-u2 < u3-u2);
  930: :
  931:  over - >r - r> u< ;
  932: 
  933: sp@	-- a_addr		gforth		spat
  934: a_addr = sp+1;
  935: 
  936: sp!	a_addr --		gforth		spstore
  937: sp = a_addr;
  938: /* works with and without TOS caching */
  939: 
  940: rp@	-- a_addr		gforth		rpat
  941: a_addr = rp;
  942: 
  943: rp!	a_addr --		gforth		rpstore
  944: rp = a_addr;
  945: 
  946: \+floating
  947: 
  948: fp@	-- f_addr	gforth	fp_fetch
  949: f_addr = fp;
  950: 
  951: fp!	f_addr --	gforth	fp_store
  952: fp = f_addr;
  953: 
  954: \+
  955: 
  956: ;s	--		gforth	semis
  957: ""The primitive compiled by @code{EXIT}.""
  958: ip = (Xt *)(*rp++);
  959: NEXT_P0;
  960: 
  961: >r	w --		core	to_r
  962: *--rp = w;
  963: :
  964:  (>r) ;
  965: : (>r)  rp@ cell+ @ rp@ ! rp@ cell+ ! ;
  966: 
  967: r>	-- w		core	r_from
  968: w = *rp++;
  969: :
  970:  rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
  971: Create (rdrop) ' ;s A,
  972: 
  973: rdrop	--		gforth
  974: rp++;
  975: :
  976:  r> r> drop >r ;
  977: 
  978: 2>r	w1 w2 --	core-ext	two_to_r
  979: *--rp = w1;
  980: *--rp = w2;
  981: :
  982:  swap r> swap >r swap >r >r ;
  983: 
  984: 2r>	-- w1 w2	core-ext	two_r_from
  985: w2 = *rp++;
  986: w1 = *rp++;
  987: :
  988:  r> r> swap r> swap >r swap ;
  989: 
  990: 2r@	-- w1 w2	core-ext	two_r_fetch
  991: w2 = rp[0];
  992: w1 = rp[1];
  993: :
  994:  i' j ;
  995: 
  996: 2rdrop	--		gforth	two_r_drop
  997: rp+=2;
  998: :
  999:  r> r> drop r> drop >r ;
 1000: 
 1001: over	w1 w2 -- w1 w2 w1		core
 1002: :
 1003:  sp@ cell+ @ ;
 1004: 
 1005: drop	w --		core
 1006: :
 1007:  IF THEN ;
 1008: 
 1009: swap	w1 w2 -- w2 w1		core
 1010: :
 1011:  >r (swap) ! r> (swap) @ ;
 1012: Variable (swap)
 1013: 
 1014: dup	w -- w w		core
 1015: :
 1016:  sp@ @ ;
 1017: 
 1018: rot	w1 w2 w3 -- w2 w3 w1	core	rote
 1019: :
 1020: [ defined? (swap) [IF] ]
 1021:     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
 1022: Variable (rot)
 1023: [ELSE] ]
 1024:     >r swap r> swap ;
 1025: [THEN]
 1026: 
 1027: -rot	w1 w2 w3 -- w3 w1 w2	gforth	not_rote
 1028: :
 1029:  rot rot ;
 1030: 
 1031: nip	w1 w2 -- w2		core-ext
 1032: :
 1033:  swap drop ;
 1034: 
 1035: tuck	w1 w2 -- w2 w1 w2	core-ext
 1036: :
 1037:  swap over ;
 1038: 
 1039: ?dup	w -- w			core	question_dupe
 1040: if (w!=0) {
 1041:   IF_TOS(*sp-- = w;)
 1042: #ifndef USE_TOS
 1043:   *--sp = w;
 1044: #endif
 1045: }
 1046: :
 1047:  dup IF dup THEN ;
 1048: 
 1049: pick	u -- w			core-ext
 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: w = *a_addr;
 1086: 
 1087: !	w a_addr --		core	store
 1088: *a_addr = w;
 1089: 
 1090: +!	n a_addr --		core	plus_store
 1091: *a_addr += n;
 1092: :
 1093:  tuck @ + swap ! ;
 1094: 
 1095: c@	c_addr -- c		core	cfetch
 1096: c = *c_addr;
 1097: :
 1098: [ bigendian [IF] ]
 1099:     [ cell>bit 4 = [IF] ]
 1100: 	dup [ 0 cell - ] Literal and @ swap 1 and
 1101: 	IF  $FF and  ELSE  8>>  THEN  ;
 1102:     [ [ELSE] ]
 1103: 	dup [ cell 1- ] literal and
 1104: 	tuck - @ swap [ cell 1- ] literal xor
 1105:  	0 ?DO 8>> LOOP $FF and
 1106:     [ [THEN] ]
 1107: [ [ELSE] ]
 1108:     [ cell>bit 4 = [IF] ]
 1109: 	dup [ 0 cell - ] Literal and @ swap 1 and
 1110: 	IF  8>>  ELSE  $FF and  THEN
 1111:     [ [ELSE] ]
 1112: 	dup [ cell  1- ] literal and 
 1113: 	tuck - @ swap
 1114: 	0 ?DO 8>> LOOP 255 and
 1115:     [ [THEN] ]
 1116: [ [THEN] ]
 1117: ;
 1118: : 8>> 2/ 2/ 2/ 2/  2/ 2/ 2/ 2/ ;
 1119: 
 1120: c!	c c_addr --		core	cstore
 1121: *c_addr = c;
 1122: :
 1123: [ bigendian [IF] ]
 1124:     [ cell>bit 4 = [IF] ]
 1125: 	tuck 1 and IF  $FF and  ELSE  8<<  THEN >r
 1126: 	dup -2 and @ over 1 and cells masks + @ and
 1127: 	r> or swap -2 and ! ;
 1128: 	Create masks $00FF , $FF00 ,
 1129:     [ELSE] ]
 1130: 	dup [ cell 1- ] literal and dup 
 1131: 	[ cell 1- ] literal xor >r
 1132: 	- dup @ $FF r@ 0 ?DO 8<< LOOP invert and
 1133: 	rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
 1134:     [THEN]
 1135: [ELSE] ]
 1136:     [ cell>bit 4 = [IF] ]
 1137: 	tuck 1 and IF  8<<  ELSE  $FF and  THEN >r
 1138: 	dup -2 and @ over 1 and cells masks + @ and
 1139: 	r> or swap -2 and ! ;
 1140: 	Create masks $FF00 , $00FF ,
 1141:     [ELSE] ]
 1142: 	dup [ cell 1- ] literal and dup >r
 1143: 	- dup @ $FF r@ 0 ?DO 8<< LOOP invert and
 1144: 	rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
 1145:     [THEN]
 1146: [THEN]
 1147: : 8<< 2* 2* 2* 2*  2* 2* 2* 2* ;
 1148: 
 1149: 2!	w1 w2 a_addr --		core	two_store
 1150: a_addr[0] = w2;
 1151: a_addr[1] = w1;
 1152: :
 1153:  tuck ! cell+ ! ;
 1154: 
 1155: 2@	a_addr -- w1 w2		core	two_fetch
 1156: w2 = a_addr[0];
 1157: w1 = a_addr[1];
 1158: :
 1159:  dup cell+ @ swap @ ;
 1160: 
 1161: cell+	a_addr1 -- a_addr2	core	cell_plus
 1162: a_addr2 = a_addr1+1;
 1163: :
 1164:  cell + ;
 1165: 
 1166: cells	n1 -- n2		core
 1167: n2 = n1 * sizeof(Cell);
 1168: :
 1169:  [ cell
 1170:  2/ dup [IF] ] 2* [ [THEN]
 1171:  2/ dup [IF] ] 2* [ [THEN]
 1172:  2/ dup [IF] ] 2* [ [THEN]
 1173:  2/ dup [IF] ] 2* [ [THEN]
 1174:  drop ] ;
 1175: 
 1176: char+	c_addr1 -- c_addr2	core	care_plus
 1177: c_addr2 = c_addr1 + 1;
 1178: :
 1179:  1+ ;
 1180: 
 1181: (chars)		n1 -- n2	gforth	paren_cares
 1182: n2 = n1 * sizeof(Char);
 1183: :
 1184:  ;
 1185: 
 1186: count	c_addr1 -- c_addr2 u	core
 1187: "" If c-add1 is the address of a counted string return the length of
 1188: the string, u, and the address of its first character, c-addr2.""
 1189: u = *c_addr1;
 1190: c_addr2 = c_addr1+1;
 1191: :
 1192:  dup 1+ swap c@ ;
 1193: 
 1194: (f83find)	c_addr u f83name1 -- f83name2	new	paren_f83find
 1195: for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
 1196:   if ((UCell)F83NAME_COUNT(f83name1)==u &&
 1197:       memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
 1198:     break;
 1199: f83name2=f83name1;
 1200: :
 1201:     BEGIN  dup WHILE  (find-samelen)  dup  WHILE
 1202: 	>r 2dup r@ cell+ char+ capscomp  0=
 1203: 	IF  2drop r>  EXIT  THEN
 1204: 	r> @
 1205:     REPEAT  THEN  nip nip ;
 1206: : (find-samelen) ( u f83name1 -- u f83name2/0 )
 1207:     BEGIN  2dup cell+ c@ $1F and <> WHILE  @  dup 0= UNTIL  THEN ;
 1208: 
 1209: \+hash
 1210: 
 1211: (hashfind)	c_addr u a_addr -- f83name2	new	paren_hashfind
 1212: struct F83Name *f83name1;
 1213: f83name2=NULL;
 1214: while(a_addr != NULL)
 1215: {
 1216:    f83name1=(struct F83Name *)(a_addr[1]);
 1217:    a_addr=(Cell *)(a_addr[0]);
 1218:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
 1219:        memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
 1220:      {
 1221: 	f83name2=f83name1;
 1222: 	break;
 1223:      }
 1224: }
 1225: :
 1226:  BEGIN  dup  WHILE
 1227:         2@ >r >r dup r@ cell+ c@ $1F and =
 1228:         IF  2dup r@ cell+ char+ capscomp 0=
 1229: 	    IF  2drop r> rdrop  EXIT  THEN  THEN
 1230: 	rdrop r>
 1231:  REPEAT nip nip ;
 1232: 
 1233: (tablefind)	c_addr u a_addr -- f83name2	new	paren_tablefind
 1234: ""A case-sensitive variant of @code{(hashfind)}""
 1235: struct F83Name *f83name1;
 1236: f83name2=NULL;
 1237: while(a_addr != NULL)
 1238: {
 1239:    f83name1=(struct F83Name *)(a_addr[1]);
 1240:    a_addr=(Cell *)(a_addr[0]);
 1241:    if ((UCell)F83NAME_COUNT(f83name1)==u &&
 1242:        memcmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
 1243:      {
 1244: 	f83name2=f83name1;
 1245: 	break;
 1246:      }
 1247: }
 1248: :
 1249:  BEGIN  dup  WHILE
 1250:         2@ >r >r dup r@ cell+ c@ $1F and =
 1251:         IF  2dup r@ cell+ char+ -text 0=
 1252: 	    IF  2drop r> rdrop  EXIT  THEN  THEN
 1253: 	rdrop r>
 1254:  REPEAT nip nip ;
 1255: 
 1256: (hashkey)	c_addr u1 -- u2		gforth	paren_hashkey
 1257: u2=0;
 1258: while(u1--)
 1259:    u2+=(Cell)toupper(*c_addr++);
 1260: :
 1261:  0 -rot bounds ?DO  I c@ toupper +  LOOP ;
 1262: 
 1263: (hashkey1)	c_addr u ubits -- ukey		gforth	paren_hashkey1
 1264: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
 1265: /* this hash function rotates the key at every step by rot bits within
 1266:    ubits bits and xors it with the character. This function does ok in
 1267:    the chi-sqare-test.  Rot should be <=7 (preferably <=5) for
 1268:    ASCII strings (larger if ubits is large), and should share no
 1269:    divisors with ubits.
 1270: */
 1271: 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];
 1272: Char *cp = c_addr;
 1273: for (ukey=0; cp<c_addr+u; cp++)
 1274:     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot))) 
 1275: 	     ^ toupper(*cp))
 1276: 	    & ((1<<ubits)-1));
 1277: :
 1278:  dup rot-values + c@ over 1 swap lshift 1- >r
 1279:  tuck - 2swap r> 0 2swap bounds
 1280:  ?DO  dup 4 pick lshift swap 3 pick rshift or
 1281:       I c@ toupper xor
 1282:       over and  LOOP
 1283:  nip nip nip ;
 1284: Create rot-values
 1285:   5 c, 0 c, 1 c, 2 c, 3 c,  4 c, 5 c, 5 c, 5 c, 5 c,
 1286:   3 c, 5 c, 5 c, 5 c, 5 c,  7 c, 5 c, 5 c, 5 c, 5 c,
 1287:   7 c, 5 c, 5 c, 5 c, 5 c,  6 c, 5 c, 5 c, 5 c, 5 c,
 1288:   7 c, 5 c, 5 c,
 1289: 
 1290: \+
 1291: 
 1292: (parse-white)	c_addr1 u1 -- c_addr2 u2	gforth	paren_parse_white
 1293: /* use !isgraph instead of isspace? */
 1294: Char *endp = c_addr1+u1;
 1295: while (c_addr1<endp && isspace(*c_addr1))
 1296:   c_addr1++;
 1297: if (c_addr1<endp) {
 1298:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
 1299:     ;
 1300:   u2 = c_addr1-c_addr2;
 1301: }
 1302: else {
 1303:   c_addr2 = c_addr1;
 1304:   u2 = 0;
 1305: }
 1306: :
 1307:  BEGIN  dup  WHILE  over c@ bl <=  WHILE  1 /string
 1308:  REPEAT  THEN  2dup
 1309:  BEGIN  dup  WHILE  over c@ bl >   WHILE  1 /string
 1310:  REPEAT  THEN  nip - ;
 1311: 
 1312: aligned		c_addr -- a_addr	core
 1313: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
 1314: :
 1315:  [ cell 1- ] Literal + [ -1 cells ] Literal and ;
 1316: 
 1317: faligned	c_addr -- f_addr	float	f_aligned
 1318: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
 1319: :
 1320:  [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
 1321: 
 1322: >body		xt -- a_addr	core	to_body
 1323: a_addr = PFA(xt);
 1324: :
 1325:     2 cells + ;
 1326: 
 1327: >code-address		xt -- c_addr		gforth	to_code_address
 1328: ""c_addr is the code address of the word xt""
 1329: /* !! This behaves installation-dependently for DOES-words */
 1330: c_addr = (Address)CODE_ADDRESS(xt);
 1331: :
 1332:     @ ;
 1333: 
 1334: >does-code	xt -- a_addr		gforth	to_does_code
 1335: ""If xt ist the execution token of a defining-word-defined word,
 1336: a_addr is the start of the Forth code after the @code{DOES>};
 1337: Otherwise a_addr is 0.""
 1338: a_addr = (Cell *)DOES_CODE(xt);
 1339: :
 1340:     cell+ @ ;
 1341: 
 1342: code-address!		c_addr xt --		gforth	code_address_store
 1343: ""Creates a code field with code address c_addr at xt""
 1344: MAKE_CF(xt, c_addr);
 1345: CACHE_FLUSH(xt,(size_t)PFA(0));
 1346: :
 1347:     ! ;
 1348: 
 1349: does-code!	a_addr xt --		gforth	does_code_store
 1350: ""creates a code field at xt for a defining-word-defined word; a_addr
 1351: is the start of the Forth code after DOES>""
 1352: MAKE_DOES_CF(xt, a_addr);
 1353: CACHE_FLUSH(xt,(size_t)PFA(0));
 1354: :
 1355:     dodoes: over ! cell+ ! ;
 1356: 
 1357: does-handler!	a_addr --	gforth	does_handler_store
 1358: ""creates a DOES>-handler at address a_addr. a_addr usually points
 1359: just behind a DOES>.""
 1360: MAKE_DOES_HANDLER(a_addr);
 1361: CACHE_FLUSH((caddr_t)a_addr,DOES_HANDLER_SIZE);
 1362: :
 1363:     drop ;
 1364: 
 1365: /does-handler	-- n	gforth	slash_does_handler
 1366: ""the size of a does-handler (includes possible padding)""
 1367: /* !! a constant or environmental query might be better */
 1368: n = DOES_HANDLER_SIZE;
 1369: :
 1370:     2 cells ;
 1371: 
 1372: threading-method	-- n	gforth	threading_method
 1373: ""0 if the engine is direct threaded. Note that this may change during
 1374: the lifetime of an image.""
 1375: #if defined(DOUBLY_INDIRECT)
 1376: n=2;
 1377: #else
 1378: # if defined(DIRECT_THREADED)
 1379: n=0;
 1380: # else
 1381: n=1;
 1382: # endif
 1383: #endif
 1384: :
 1385:  1 ;
 1386: 
 1387: key-file	wfileid -- n		gforth	paren_key_file
 1388: #ifdef HAS_FILE
 1389: fflush(stdout);
 1390: n = key((FILE*)wfileid);
 1391: #else
 1392: n = key(stdin);
 1393: #endif
 1394: 
 1395: key?-file	wfileid -- n		facility	key_q_file
 1396: #ifdef HAS_FILE
 1397: fflush(stdout);
 1398: n = key_query((FILE*)wfileid);
 1399: #else
 1400: n = key_query(stdin);
 1401: #endif
 1402: 
 1403: \+os
 1404: 
 1405: stdin	-- wfileid	gforth
 1406: wfileid = (Cell)stdin;
 1407: 
 1408: stdout	-- wfileid	gforth
 1409: wfileid = (Cell)stdout;
 1410: 
 1411: stderr	-- wfileid	gforth
 1412: wfileid = (Cell)stderr;
 1413: 
 1414: form	-- urows ucols	gforth
 1415: ""The number of lines and columns in the terminal. These numbers may change
 1416: with the window size.""
 1417: /* we could block SIGWINCH here to get a consistent size, but I don't
 1418:  think this is necessary or always beneficial */
 1419: urows=rows;
 1420: ucols=cols;
 1421: 
 1422: flush-icache	c_addr u --	gforth	flush_icache
 1423: ""Make sure that the instruction cache of the processor (if there is
 1424: one) does not contain stale data at @var{c_addr} and @var{u} bytes
 1425: afterwards. @code{END-CODE} performs a @code{flush-icache}
 1426: automatically. Caveat: @code{flush-icache} might not work on your
 1427: installation; this is usually the case if direct threading is not
 1428: supported on your machine (take a look at your @file{machine.h}) and
 1429: your machine has a separate instruction cache. In such cases,
 1430: @code{flush-icache} does nothing instead of flushing the instruction
 1431: cache.""
 1432: FLUSH_ICACHE(c_addr,u);
 1433: 
 1434: (bye)	n --	gforth	paren_bye
 1435: return (Label *)n;
 1436: 
 1437: (system)	c_addr u -- wretval wior	gforth	peren_system
 1438: #ifndef MSDOS
 1439: int old_tp=terminal_prepped;
 1440: deprep_terminal();
 1441: #endif
 1442: wretval=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
 1443: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
 1444: #ifndef MSDOS
 1445: if (old_tp)
 1446:   prep_terminal();
 1447: #endif
 1448: 
 1449: getenv	c_addr1 u1 -- c_addr2 u2	gforth
 1450: c_addr2 = getenv(cstr(c_addr1,u1,1));
 1451: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
 1452: 
 1453: open-pipe	c_addr u ntype -- wfileid wior	gforth	open_pipe
 1454: wfileid=(Cell)popen(cstr(c_addr,u,1),fileattr[ntype]); /* ~ expansion of 1st arg? */
 1455: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
 1456: 
 1457: close-pipe	wfileid -- wretval wior		gforth	close_pipe
 1458: wretval = pclose((FILE *)wfileid);
 1459: wior = IOR(wretval==-1);
 1460: 
 1461: time&date	-- nsec nmin nhour nday nmonth nyear	facility-ext	time_and_date
 1462: struct timeval time1;
 1463: struct timezone zone1;
 1464: struct tm *ltime;
 1465: gettimeofday(&time1,&zone1);
 1466: ltime=localtime((time_t *)&time1.tv_sec);
 1467: nyear =ltime->tm_year+1900;
 1468: nmonth=ltime->tm_mon+1;
 1469: nday  =ltime->tm_mday;
 1470: nhour =ltime->tm_hour;
 1471: nmin  =ltime->tm_min;
 1472: nsec  =ltime->tm_sec;
 1473: 
 1474: ms	n --	facility-ext
 1475: struct timeval timeout;
 1476: timeout.tv_sec=n/1000;
 1477: timeout.tv_usec=1000*(n%1000);
 1478: (void)select(0,0,0,0,&timeout);
 1479: 
 1480: allocate	u -- a_addr wior	memory
 1481: a_addr = (Cell *)malloc(u?u:1);
 1482: wior = IOR(a_addr==NULL);
 1483: 
 1484: free		a_addr -- wior		memory
 1485: free(a_addr);
 1486: wior = 0;
 1487: 
 1488: resize		a_addr1 u -- a_addr2 wior	memory
 1489: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
 1490: address units, possibly moving the contents to a different
 1491: area. @i{a_addr2} is the address of the resulting area. If
 1492: @code{a_addr1} is 0, Gforth's (but not the standard) @code{resize}
 1493: @code{allocate}s @i{u} address units.""
 1494: /* the following check is not necessary on most OSs, but it is needed
 1495:    on SunOS 4.1.2. */
 1496: if (a_addr1==NULL)
 1497:   a_addr2 = (Cell *)malloc(u);
 1498: else
 1499:   a_addr2 = (Cell *)realloc(a_addr1, u);
 1500: wior = IOR(a_addr2==NULL);	/* !! Define a return code */
 1501: 
 1502: strerror	n -- c_addr u	gforth
 1503: c_addr = strerror(n);
 1504: u = strlen(c_addr);
 1505: 
 1506: strsignal	n -- c_addr u	gforth
 1507: c_addr = strsignal(n);
 1508: u = strlen(c_addr);
 1509: 
 1510: call-c	w --	gforth	call_c
 1511: ""Call the C function pointed to by @i{w}. The C function has to
 1512: access the stack itself. The stack pointers are exported in the global
 1513: variables @code{SP} and @code{FP}.""
 1514: /* This is a first attempt at support for calls to C. This may change in
 1515:    the future */
 1516: IF_FTOS(fp[0]=FTOS);
 1517: FP=fp;
 1518: SP=sp;
 1519: ((void (*)())w)();
 1520: sp=SP;
 1521: fp=FP;
 1522: IF_TOS(TOS=sp[0]);
 1523: IF_FTOS(FTOS=fp[0]);
 1524: 
 1525: \+
 1526: \+file
 1527: 
 1528: close-file	wfileid -- wior		file	close_file
 1529: wior = IOR(fclose((FILE *)wfileid)==EOF);
 1530: 
 1531: open-file	c_addr u ntype -- wfileid wior	file	open_file
 1532: wfileid = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
 1533: #if defined(GO32) && defined(MSDOS)
 1534: if(wfileid && !(ntype & 1))
 1535:   setbuf((FILE*)wfileid, NULL);
 1536: #endif
 1537: wior =  IOR(wfileid == 0);
 1538: 
 1539: create-file	c_addr u ntype -- wfileid wior	file	create_file
 1540: Cell	fd;
 1541: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_TRUNC|ufileattr[ntype], 0666);
 1542: if (fd != -1) {
 1543:   wfileid = (Cell)fdopen(fd, fileattr[ntype]);
 1544: #if defined(GO32) && defined(MSDOS)
 1545:   if(wfileid && !(ntype & 1))
 1546:     setbuf((FILE*)wfileid, NULL);
 1547: #endif
 1548:   wior = IOR(wfileid == 0);
 1549: } else {
 1550:   wfileid = 0;
 1551:   wior = IOR(1);
 1552: }
 1553: 
 1554: delete-file	c_addr u -- wior		file	delete_file
 1555: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
 1556: 
 1557: rename-file	c_addr1 u1 c_addr2 u2 -- wior	file-ext	rename_file
 1558: ""rename file c_addr1 u1 to new name c_addr2 u2""
 1559: char *s1=tilde_cstr(c_addr2, u2, 1);
 1560: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
 1561: 
 1562: file-position	wfileid -- ud wior	file	file_position
 1563: /* !! use tell and lseek? */
 1564: ud = LONG2UD(ftell((FILE *)wfileid));
 1565: wior = IOR(UD2LONG(ud)==-1);
 1566: 
 1567: reposition-file	ud wfileid -- wior	file	reposition_file
 1568: wior = IOR(fseek((FILE *)wfileid, UD2LONG(ud), SEEK_SET)==-1);
 1569: 
 1570: file-size	wfileid -- ud wior	file	file_size
 1571: struct stat buf;
 1572: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
 1573: ud = LONG2UD(buf.st_size);
 1574: 
 1575: resize-file	ud wfileid -- wior	file	resize_file
 1576: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2LONG(ud))==-1);
 1577: 
 1578: read-file	c_addr u1 wfileid -- u2 wior	file	read_file
 1579: /* !! fread does not guarantee enough */
 1580: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
 1581: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
 1582: /* !! is the value of ferror errno-compatible? */
 1583: if (wior)
 1584:   clearerr((FILE *)wfileid);
 1585: 
 1586: read-line	c_addr u1 wfileid -- u2 flag wior	file	read_line
 1587: /*
 1588: Cell c;
 1589: flag=-1;
 1590: for(u2=0; u2<u1; u2++)
 1591: {
 1592:    *c_addr++ = (Char)(c = getc((FILE *)wfileid));
 1593:    if(c=='\n') break;
 1594:    if(c==EOF)
 1595:      {
 1596: 	flag=FLAG(u2!=0);
 1597: 	break;
 1598:      }
 1599: }
 1600: wior=FILEIO(ferror((FILE *)wfileid));
 1601: */
 1602: if ((flag=FLAG(!feof((FILE *)wfileid) &&
 1603: 	       fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
 1604:   wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
 1605:   if (wior)
 1606:     clearerr((FILE *)wfileid);
 1607:   u2 = strlen(c_addr);
 1608:   u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
 1609: }
 1610: else {
 1611:   wior=0;
 1612:   u2=0;
 1613: }
 1614: 
 1615: \+
 1616: \+file
 1617: 
 1618: write-file	c_addr u1 wfileid -- wior	file	write_file
 1619: /* !! fwrite does not guarantee enough */
 1620: {
 1621:   UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
 1622:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
 1623:   if (wior)
 1624:     clearerr((FILE *)wfileid);
 1625: }
 1626: 
 1627: \+
 1628: 
 1629: emit-file	c wfileid -- wior	gforth	emit_file
 1630: #ifdef HAS_FILE
 1631: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
 1632: if (wior)
 1633:   clearerr((FILE *)wfileid);
 1634: #else
 1635: putc(c, stdout);
 1636: #endif
 1637: 
 1638: \+file
 1639: 
 1640: flush-file	wfileid -- wior		file-ext	flush_file
 1641: wior = IOR(fflush((FILE *) wfileid)==EOF);
 1642: 
 1643: file-status	c_addr u -- ntype wior	file-ext	file_status
 1644: char *filename=tilde_cstr(c_addr, u, 1);
 1645: if (access (filename, F_OK) != 0) {
 1646:   ntype=0;
 1647:   wior=IOR(1);
 1648: }
 1649: else if (access (filename, R_OK | W_OK) == 0) {
 1650:   ntype=2; /* r/w */
 1651:   wior=0;
 1652: }
 1653: else if (access (filename, R_OK) == 0) {
 1654:   ntype=0; /* r/o */
 1655:   wior=0;
 1656: }
 1657: else if (access (filename, W_OK) == 0) {
 1658:   ntype=4; /* w/o */
 1659:   wior=0;
 1660: }
 1661: else {
 1662:   ntype=1; /* well, we cannot access the file, but better deliver a legal
 1663: 	    access mode (r/o bin), so we get a decent error later upon open. */
 1664:   wior=0;
 1665: }
 1666: 
 1667: \+
 1668: \+floating
 1669: 
 1670: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
 1671: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
 1672: 
 1673: d>f		d -- r		float	d_to_f
 1674: #ifdef BUGGY_LONG_LONG
 1675: extern double ldexp(double x, int exp);
 1676: r = ldexp((Float)d.hi,CELL_BITS) + (Float)d.lo;
 1677: #else
 1678: r = d;
 1679: #endif
 1680: 
 1681: f>d		r -- d		float	f_to_d
 1682: #ifdef BUGGY_LONG_LONG
 1683: d.hi = ldexp(r,-(int)(CELL_BITS)) - (r<0);
 1684: d.lo = r-ldexp((Float)d.hi,CELL_BITS);
 1685: #else
 1686: d = r;
 1687: #endif
 1688: 
 1689: f!		r f_addr --	float	f_store
 1690: *f_addr = r;
 1691: 
 1692: f@		f_addr -- r	float	f_fetch
 1693: r = *f_addr;
 1694: 
 1695: df@		df_addr -- r	float-ext	d_f_fetch
 1696: #ifdef IEEE_FP
 1697: r = *df_addr;
 1698: #else
 1699: !! df@
 1700: #endif
 1701: 
 1702: df!		r df_addr --	float-ext	d_f_store
 1703: #ifdef IEEE_FP
 1704: *df_addr = r;
 1705: #else
 1706: !! df!
 1707: #endif
 1708: 
 1709: sf@		sf_addr -- r	float-ext	s_f_fetch
 1710: #ifdef IEEE_FP
 1711: r = *sf_addr;
 1712: #else
 1713: !! sf@
 1714: #endif
 1715: 
 1716: sf!		r sf_addr --	float-ext	s_f_store
 1717: #ifdef IEEE_FP
 1718: *sf_addr = r;
 1719: #else
 1720: !! sf!
 1721: #endif
 1722: 
 1723: f+		r1 r2 -- r3	float	f_plus
 1724: r3 = r1+r2;
 1725: 
 1726: f-		r1 r2 -- r3	float	f_minus
 1727: r3 = r1-r2;
 1728: 
 1729: f*		r1 r2 -- r3	float	f_star
 1730: r3 = r1*r2;
 1731: 
 1732: f/		r1 r2 -- r3	float	f_slash
 1733: r3 = r1/r2;
 1734: 
 1735: f**		r1 r2 -- r3	float-ext	f_star_star
 1736: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
 1737: r3 = pow(r1,r2);
 1738: 
 1739: fnegate		r1 -- r2	float
 1740: r2 = - r1;
 1741: 
 1742: fdrop		r --		float
 1743: 
 1744: fdup		r -- r r	float
 1745: 
 1746: fswap		r1 r2 -- r2 r1	float
 1747: 
 1748: fover		r1 r2 -- r1 r2 r1	float
 1749: 
 1750: frot		r1 r2 r3 -- r2 r3 r1	float
 1751: 
 1752: fnip		r1 r2 -- r2	gforth
 1753: 
 1754: ftuck		r1 r2 -- r2 r1 r2	gforth
 1755: 
 1756: float+		f_addr1 -- f_addr2	float	float_plus
 1757: f_addr2 = f_addr1+1;
 1758: 
 1759: floats		n1 -- n2	float
 1760: n2 = n1*sizeof(Float);
 1761: 
 1762: floor		r1 -- r2	float
 1763: ""round towards the next smaller integral value, i.e., round toward negative infinity""
 1764: /* !! unclear wording */
 1765: r2 = floor(r1);
 1766: 
 1767: fround		r1 -- r2	float
 1768: ""round to the nearest integral value""
 1769: /* !! unclear wording */
 1770: #ifdef HAVE_RINT
 1771: r2 = rint(r1);
 1772: #else
 1773: r2 = floor(r1+0.5);
 1774: /* !! This is not quite true to the rounding rules given in the standard */
 1775: #endif
 1776: 
 1777: fmax		r1 r2 -- r3	float
 1778: if (r1<r2)
 1779:   r3 = r2;
 1780: else
 1781:   r3 = r1;
 1782: 
 1783: fmin		r1 r2 -- r3	float
 1784: if (r1<r2)
 1785:   r3 = r1;
 1786: else
 1787:   r3 = r2;
 1788: 
 1789: represent		r c_addr u -- n f1 f2	float
 1790: char *sig;
 1791: int flag;
 1792: int decpt;
 1793: sig=ecvt(r, u, &decpt, &flag);
 1794: n=(r==0 ? 1 : decpt);
 1795: f1=FLAG(flag!=0);
 1796: f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
 1797: memmove(c_addr,sig,u);
 1798: 
 1799: >float	c_addr u -- flag	float	to_float
 1800: /* real signature: c_addr u -- r t / f */
 1801: Float r;
 1802: char *number=cstr(c_addr, u, 1);
 1803: char *endconv;
 1804: while(isspace((unsigned)(number[--u])) && u>0);
 1805: switch(number[u])
 1806: {
 1807:    case 'd':
 1808:    case 'D':
 1809:    case 'e':
 1810:    case 'E':  break;
 1811:    default :  u++; break;
 1812: }
 1813: number[u]='\0';
 1814: r=strtod(number,&endconv);
 1815: if((flag=FLAG(!(Cell)*endconv)))
 1816: {
 1817:    IF_FTOS(fp[0] = FTOS);
 1818:    fp += -1;
 1819:    FTOS = r;
 1820: }
 1821: else if(*endconv=='d' || *endconv=='D')
 1822: {
 1823:    *endconv='E';
 1824:    r=strtod(number,&endconv);
 1825:    if((flag=FLAG(!(Cell)*endconv)))
 1826:      {
 1827: 	IF_FTOS(fp[0] = FTOS);
 1828: 	fp += -1;
 1829: 	FTOS = r;
 1830:      }
 1831: }
 1832: 
 1833: fabs		r1 -- r2	float-ext
 1834: r2 = fabs(r1);
 1835: 
 1836: facos		r1 -- r2	float-ext
 1837: r2 = acos(r1);
 1838: 
 1839: fasin		r1 -- r2	float-ext
 1840: r2 = asin(r1);
 1841: 
 1842: fatan		r1 -- r2	float-ext
 1843: r2 = atan(r1);
 1844: 
 1845: fatan2		r1 r2 -- r3	float-ext
 1846: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
 1847: intends this to be the inverse of @code{fsincos}. In gforth it is.""
 1848: r3 = atan2(r1,r2);
 1849: 
 1850: fcos		r1 -- r2	float-ext
 1851: r2 = cos(r1);
 1852: 
 1853: fexp		r1 -- r2	float-ext
 1854: r2 = exp(r1);
 1855: 
 1856: fexpm1		r1 -- r2	float-ext
 1857: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
 1858: #ifdef HAVE_EXPM1
 1859: extern double
 1860: #ifdef NeXT
 1861:               const
 1862: #endif
 1863:                     expm1(double);
 1864: r2 = expm1(r1);
 1865: #else
 1866: r2 = exp(r1)-1.;
 1867: #endif
 1868: 
 1869: fln		r1 -- r2	float-ext
 1870: r2 = log(r1);
 1871: 
 1872: flnp1		r1 -- r2	float-ext
 1873: ""@i{r2}=ln(@i{r1}+1)""
 1874: #ifdef HAVE_LOG1P
 1875: extern double
 1876: #ifdef NeXT
 1877:               const
 1878: #endif
 1879:                     log1p(double);
 1880: r2 = log1p(r1);
 1881: #else
 1882: r2 = log(r1+1.);
 1883: #endif
 1884: 
 1885: flog		r1 -- r2	float-ext
 1886: ""the decimal logarithm""
 1887: r2 = log10(r1);
 1888: 
 1889: falog		r1 -- r2	float-ext
 1890: ""@i{r2}=10**@i{r1}""
 1891: extern double pow10(double);
 1892: r2 = pow10(r1);
 1893: 
 1894: fsin		r1 -- r2	float-ext
 1895: r2 = sin(r1);
 1896: 
 1897: fsincos		r1 -- r2 r3	float-ext
 1898: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
 1899: r2 = sin(r1);
 1900: r3 = cos(r1);
 1901: 
 1902: fsqrt		r1 -- r2	float-ext
 1903: r2 = sqrt(r1);
 1904: 
 1905: ftan		r1 -- r2	float-ext
 1906: r2 = tan(r1);
 1907: :
 1908:  fsincos f/ ;
 1909: 
 1910: fsinh		r1 -- r2	float-ext
 1911: r2 = sinh(r1);
 1912: :
 1913:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
 1914: 
 1915: fcosh		r1 -- r2	float-ext
 1916: r2 = cosh(r1);
 1917: :
 1918:  fexp fdup 1/f f+ f2/ ;
 1919: 
 1920: ftanh		r1 -- r2	float-ext
 1921: r2 = tanh(r1);
 1922: :
 1923:  f2* fexpm1 fdup 2. d>f f+ f/ ;
 1924: 
 1925: fasinh		r1 -- r2	float-ext
 1926: r2 = asinh(r1);
 1927: :
 1928:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
 1929: 
 1930: facosh		r1 -- r2	float-ext
 1931: r2 = acosh(r1);
 1932: :
 1933:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
 1934: 
 1935: fatanh		r1 -- r2	float-ext
 1936: r2 = atanh(r1);
 1937: :
 1938:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
 1939:  r> IF  fnegate  THEN ;
 1940: 
 1941: sfloats		n1 -- n2	float-ext	s_floats
 1942: n2 = n1*sizeof(SFloat);
 1943: 
 1944: dfloats		n1 -- n2	float-ext	d_floats
 1945: n2 = n1*sizeof(DFloat);
 1946: 
 1947: sfaligned	c_addr -- sf_addr	float-ext	s_f_aligned
 1948: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
 1949: :
 1950:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
 1951: 
 1952: dfaligned	c_addr -- df_addr	float-ext	d_f_aligned
 1953: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
 1954: :
 1955:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
 1956: 
 1957: \ The following words access machine/OS/installation-dependent
 1958: \   Gforth internals
 1959: \ !! how about environmental queries DIRECT-THREADED,
 1960: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
 1961: 
 1962: \ local variable implementation primitives
 1963: \+
 1964: \+glocals
 1965: 
 1966: @local#		-- w	gforth	fetch_local_number
 1967: w = *(Cell *)(lp+(Cell)NEXT_INST);
 1968: INC_IP(1);
 1969: 
 1970: @local0	-- w	new	fetch_local_zero
 1971: w = *(Cell *)(lp+0*sizeof(Cell));
 1972: 
 1973: @local1	-- w	new	fetch_local_four
 1974: w = *(Cell *)(lp+1*sizeof(Cell));
 1975: 
 1976: @local2	-- w	new	fetch_local_eight
 1977: w = *(Cell *)(lp+2*sizeof(Cell));
 1978: 
 1979: @local3	-- w	new	fetch_local_twelve
 1980: w = *(Cell *)(lp+3*sizeof(Cell));
 1981: 
 1982: \+floating
 1983: 
 1984: f@local#	-- r	gforth	f_fetch_local_number
 1985: r = *(Float *)(lp+(Cell)NEXT_INST);
 1986: INC_IP(1);
 1987: 
 1988: f@local0	-- r	new	f_fetch_local_zero
 1989: r = *(Float *)(lp+0*sizeof(Float));
 1990: 
 1991: f@local1	-- r	new	f_fetch_local_eight
 1992: r = *(Float *)(lp+1*sizeof(Float));
 1993: 
 1994: \+
 1995: 
 1996: laddr#		-- c_addr	gforth	laddr_number
 1997: /* this can also be used to implement lp@ */
 1998: c_addr = (Char *)(lp+(Cell)NEXT_INST);
 1999: INC_IP(1);
 2000: 
 2001: lp+!#	--	gforth	lp_plus_store_number
 2002: ""used with negative immediate values it allocates memory on the
 2003: local stack, a positive immediate argument drops memory from the local
 2004: stack""
 2005: lp += (Cell)NEXT_INST;
 2006: INC_IP(1);
 2007: 
 2008: lp-	--	new	minus_four_lp_plus_store
 2009: lp += -sizeof(Cell);
 2010: 
 2011: lp+	--	new	eight_lp_plus_store
 2012: lp += sizeof(Float);
 2013: 
 2014: lp+2	--	new	sixteen_lp_plus_store
 2015: lp += 2*sizeof(Float);
 2016: 
 2017: lp!	c_addr --	gforth	lp_store
 2018: lp = (Address)c_addr;
 2019: 
 2020: >l	w --	gforth	to_l
 2021: lp -= sizeof(Cell);
 2022: *(Cell *)lp = w;
 2023: 
 2024: \+floating
 2025: 
 2026: f>l	r --	gforth	f_to_l
 2027: lp -= sizeof(Float);
 2028: *(Float *)lp = r;
 2029: 
 2030: fpick	u -- r		gforth
 2031: r = fp[u+1]; /* +1, because update of fp happens before this fragment */
 2032: :
 2033:  floats fp@ + f@ ;
 2034: 
 2035: \+
 2036: \+
 2037: 
 2038: \+OS
 2039: 
 2040: define(`uploop',
 2041:        `pushdef(`$1', `$2')_uploop(`$1', `$2', `$3', `$4', `$5')`'popdef(`$1')')
 2042: define(`_uploop',
 2043:        `ifelse($1, `$3', `$5',
 2044: 	       `$4`'define(`$1', incr($1))_uploop(`$1', `$2', `$3', `$4', `$5')')')
 2045: \ argflist(argnum): Forth argument list
 2046: define(argflist,
 2047:        `ifelse($1, 0, `',
 2048:                `uploop(`_i', 1, $1, `format(`u%d ', _i)', `format(`u%d ', _i)')')')
 2049: \ argdlist(argnum): declare C's arguments
 2050: define(argdlist,
 2051:        `ifelse($1, 0, `',
 2052:                `uploop(`_i', 1, $1, `Cell, ', `Cell')')')
 2053: \ argclist(argnum): pass C's arguments
 2054: define(argclist,
 2055:        `ifelse($1, 0, `',
 2056:                `uploop(`_i', 1, $1, `format(`u%d, ', _i)', `format(`u%d', _i)')')')
 2057: \ icall(argnum)
 2058: define(icall,
 2059: `icall$1	argflist($1)u -- uret	gforth
 2060: uret = (SYSCALL(Cell(*)(argdlist($1)))u)(argclist($1));
 2061: 
 2062: ')
 2063: define(fcall,
 2064: `fcall$1	argflist($1)u -- rret	gforth
 2065: rret = (SYSCALL(Float(*)(argdlist($1)))u)(argclist($1));
 2066: 
 2067: ')
 2068: 
 2069: 
 2070: open-lib	c_addr1 u1 -- u2	gforth	open_lib
 2071: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
 2072: #ifndef RTLD_GLOBAL
 2073: #define RTLD_GLOBAL 0
 2074: #endif
 2075: u2=(UCell) dlopen(cstr(c_addr1, u1, 1), RTLD_GLOBAL | RTLD_LAZY);
 2076: #else
 2077: #  ifdef _WIN32
 2078: u2 = (Cell) GetModuleHandle(cstr(c_addr1, u1, 1));
 2079: #  else
 2080: #warning Define open-lib!
 2081: u2 = 0;
 2082: #  endif
 2083: #endif
 2084: 
 2085: lib-sym	c_addr1 u1 u2 -- u3	gforth	lib_sym
 2086: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
 2087: u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
 2088: #else
 2089: #  ifdef _WIN32
 2090: u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
 2091: #  else
 2092: #warning Define lib-sym!
 2093: u3 = 0;
 2094: #  endif
 2095: #endif
 2096: 
 2097: uploop(i, 0, 7, `icall(i)')
 2098: icall(20)
 2099: uploop(i, 0, 7, `fcall(i)')
 2100: fcall(20)
 2101: 
 2102: \+
 2103: 
 2104: up!	a_addr --	gforth	up_store
 2105: UP=up=(char *)a_addr;
 2106: :
 2107:  up ! ;
 2108: Variable UP

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