File:  [gforth] / gforth / Attic / primitives
Revision 1.47: download - view: text, annotated - select for diffs
Thu Nov 9 19:37:03 1995 UTC (28 years, 5 months ago) by pazsan
Branches: MAIN
CVS tags: gforth-0_1beta, HEAD
Made gforth run on Alpha (changed Bool to Cell and worked around
malloc(0)=0).

    1: \ Gforth primitives
    2: 
    3: \ Copyright (C) 1995 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 instructions in the following format:
   28: \ 
   29: \ forth name	stack effect	category	[pronunciation]
   30: \ [""glossary entry""]
   31: \ C code
   32: \ [:
   33: \ Forth code]
   34: \ 
   35: \ The pronunciation is also used for forming C names.
   36: \ 
   37: \ 
   38: \ 
   39: \ These informations are automatically translated into C-code for the
   40: \ interpreter and into some other files. I hope that your C compiler has
   41: \ decent optimization, otherwise the automatically generated code will
   42: \ be somewhat slow. The Forth version of the code is included for manual
   43: \ compilers, so they will need to compile only the important words.
   44: \ 
   45: \ Note that stack pointer adjustment is performed according to stack
   46: \ effect by automatically generated code and NEXT is automatically
   47: \ appended to the C code. Also, you can use the names in the stack
   48: \ effect in the C code. Stack access is automatic. One exception: if
   49: \ your code does not fall through, the results are not stored into the
   50: \ stack. Use different names on both sides of the '--', if you change a
   51: \ value (some stores to the stack are optimized away).
   52: \ 
   53: \ 
   54: \ 
   55: \ The stack variables have the following types:
   56: \ 
   57: \ name matches	type
   58: \ f.*		Bool
   59: \ c.*		Char
   60: \ [nw].*		Cell
   61: \ u.*		UCell
   62: \ d.*		DCell
   63: \ ud.*		UDCell
   64: \ r.*		Float
   65: \ a_.*		Cell *
   66: \ c_.*		Char *
   67: \ f_.*		Float *
   68: \ df_.*		DFloat *
   69: \ sf_.*		SFloat *
   70: \ xt.*		XT
   71: \ wid.*		WID
   72: \ f83name.*	F83Name *
   73: \ 
   74: \ 
   75: \ 
   76: \ In addition the following names can be used:
   77: \ ip	the instruction pointer
   78: \ sp	the data stack pointer
   79: \ rp	the parameter stack pointer
   80: \ lp	the locals stack pointer
   81: \ NEXT	executes NEXT
   82: \ cfa	
   83: \ NEXT1	executes NEXT1
   84: \ FLAG(x)	makes a Forth flag from a C flag
   85: \ 
   86: \ 
   87: \ 
   88: \ Percentages in comments are from Koopmans book: average/maximum use
   89: \ (taken from four, not very representative benchmarks)
   90: \ 
   91: \ 
   92: \ 
   93: \ To do:
   94: \ 
   95: \ throw execute, cfa and NEXT1 out?
   96: \ macroize *ip, ip++, *ip++ (pipelining)?
   97: 
   98: \ these m4 macros would collide with identifiers
   99: undefine(`index')
  100: undefine(`shift')
  101: 
  102: noop	--		gforth
  103: ;
  104: :
  105:  ;
  106: 
  107: lit	-- w		gforth
  108: w = (Cell)NEXT_INST;
  109: INC_IP(1);
  110: 
  111: execute		xt --		core
  112: ip=IP;
  113: IF_TOS(TOS = sp[0]);
  114: EXEC(xt);
  115: 
  116: branch-lp+!#	--	gforth	branch_lp_plus_store_number
  117: /* this will probably not be used */
  118: branch_adjust_lp:
  119: lp += (Cell)(IP[1]);
  120: goto branch;
  121: 
  122: branch	--		gforth
  123: branch:
  124: ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
  125: NEXT_P0;
  126: :
  127:  r> dup @ + >r ;
  128: 
  129: \ condbranch(forthname,restline,code)
  130: \ this is non-syntactical: code must open a brace that is closed by the macro
  131: define(condbranch,
  132: $1	$2
  133: $3	ip = (Xt *)(((Cell)IP)+(Cell)NEXT_INST);
  134:         NEXT_P0;
  135: 	NEXT;
  136: }
  137: else
  138:     INC_IP(1);
  139: 
  140: $1-lp+!#	$2_lp_plus_store_number
  141: $3    goto branch_adjust_lp;
  142: }
  143: else
  144:     INC_IP(2);
  145: 
  146: )
  147: 
  148: condbranch(?branch,f --		f83	question_branch,
  149: if (f==0) {
  150:     IF_TOS(TOS = sp[0]);
  151: )
  152: 
  153: condbranch((next),--		cmFORTH	paren_next,
  154: if ((*rp)--) {
  155: )
  156: 
  157: condbranch((loop),--		gforth	paren_loop,
  158: Cell index = *rp+1;
  159: Cell limit = rp[1];
  160: if (index != limit) {
  161:     *rp = index;
  162: )
  163: 
  164: condbranch((+loop),n --		gforth	paren_plus_loop,
  165: /* !! check this thoroughly */
  166: Cell index = *rp;
  167: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
  168: /* dependent upon two's complement arithmetic */
  169: Cell olddiff = index-rp[1];
  170: #ifndef undefined
  171: if ((olddiff^(olddiff+n))>=0   /* the limit is not crossed */
  172:     || (olddiff^n)>=0          /* it is a wrap-around effect */) {
  173: #else
  174: #ifndef MAXINT
  175: #define MAXINT ((((Cell)1)<<(8*sizeof(Cell)-1))-1)
  176: #endif
  177: if(((olddiff^MAXINT) >= n) ^ ((olddiff+n) < 0)) {
  178: #endif
  179: #ifdef i386
  180:     *rp += n;
  181: #else
  182:     *rp = index + n;
  183: #endif
  184:     IF_TOS(TOS = sp[0]);
  185: )
  186: 
  187: condbranch((-loop),u --		gforth	paren_minus_loop,
  188: /* !! check this thoroughly */
  189: Cell index = *rp;
  190: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
  191: /* dependent upon two's complement arithmetic */
  192: UCell olddiff = index-rp[1];
  193: if (olddiff>u) {
  194: #ifdef i386
  195:     *rp -= u;
  196: #else
  197:     *rp = index - u;
  198: #endif
  199:     IF_TOS(TOS = sp[0]);
  200: )
  201: 
  202: condbranch((s+loop),n --		gforth	paren_symmetric_plus_loop,
  203: ""The run-time procedure compiled by S+LOOP. It loops until the index
  204: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
  205: version of (+LOOP).""
  206: /* !! check this thoroughly */
  207: Cell index = *rp;
  208: Cell diff = index-rp[1];
  209: Cell newdiff = diff+n;
  210: if (n<0) {
  211:     diff = -diff;
  212:     newdiff = -newdiff;
  213: }
  214: if (diff>=0 || newdiff<0) {
  215: #ifdef i386
  216:     *rp += n;
  217: #else
  218:     *rp = index + n;
  219: #endif
  220:     IF_TOS(TOS = sp[0]);
  221: )
  222: 
  223: unloop		--	core
  224: rp += 2;
  225: :
  226:  r> rdrop rdrop >r ;
  227: 
  228: (for)	ncount --		cmFORTH		paren_for
  229: /* or (for) = >r -- collides with unloop! */
  230: *--rp = 0;
  231: *--rp = ncount;
  232: :
  233:  r> swap 0 >r >r >r ;
  234: 
  235: (do)	nlimit nstart --		gforth		paren_do
  236: /* or do it in high-level? 0.09/0.23% */
  237: *--rp = nlimit;
  238: *--rp = nstart;
  239: :
  240:  r> -rot swap >r >r >r ;
  241: 
  242: (?do)	nlimit nstart --	gforth	paren_question_do
  243: *--rp = nlimit;
  244: *--rp = nstart;
  245: if (nstart == nlimit) {
  246:     IF_TOS(TOS = sp[0]);
  247:     goto branch;
  248:     }
  249: else {
  250:     INC_IP(1);
  251: }
  252: 
  253: (+do)	nlimit nstart --	gforth	paren_plus_do
  254: *--rp = nlimit;
  255: *--rp = nstart;
  256: if (nstart >= nlimit) {
  257:     IF_TOS(TOS = sp[0]);
  258:     goto branch;
  259:     }
  260: else {
  261:     INC_IP(1);
  262: }
  263: 
  264: (u+do)	ulimit ustart --	gforth	paren_u_plus_do
  265: *--rp = ulimit;
  266: *--rp = ustart;
  267: if (ustart >= ulimit) {
  268:     IF_TOS(TOS = sp[0]);
  269:     goto branch;
  270:     }
  271: else {
  272:     INC_IP(1);
  273: }
  274: 
  275: (-do)	nlimit nstart --	gforth	paren_minus_do
  276: *--rp = nlimit;
  277: *--rp = nstart;
  278: if (nstart <= nlimit) {
  279:     IF_TOS(TOS = sp[0]);
  280:     goto branch;
  281:     }
  282: else {
  283:     INC_IP(1);
  284: }
  285: 
  286: (u-do)	ulimit ustart --	gforth	paren_u_minus_do
  287: *--rp = ulimit;
  288: *--rp = ustart;
  289: if (ustart <= ulimit) {
  290:     IF_TOS(TOS = sp[0]);
  291:     goto branch;
  292:     }
  293: else {
  294:     INC_IP(1);
  295: }
  296: 
  297: i	-- n		core
  298: n = *rp;
  299: 
  300: j	-- n		core
  301: n = rp[2];
  302: 
  303: \ digit is high-level: 0/0%
  304: 
  305: (emit)	c --		gforth	paren_emit
  306: putchar(c);
  307: emitcounter++;
  308: 
  309: (type)	c_addr n --	gforth	paren_type
  310: fwrite(c_addr,sizeof(Char),n,stdout);
  311: emitcounter += n;
  312: 
  313: (key)	-- n		gforth	paren_key
  314: fflush(stdout);
  315: /* !! noecho */
  316: n = key();
  317: 
  318: key?	-- n		facility	key_q
  319: fflush(stdout);
  320: n = key_query;
  321: 
  322: cr	--		core
  323: puts("");
  324: :
  325:  $0A emit ;
  326: 
  327: move	c_from c_to ucount --		core
  328: memmove(c_to,c_from,ucount);
  329: /* make an Ifdef for bsd and others? */
  330: :
  331:  >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
  332: 
  333: cmove	c_from c_to u --	string
  334: while (u-- > 0)
  335:   *c_to++ = *c_from++;
  336: :
  337:  bounds ?DO  dup c@ I c! 1+  LOOP  drop ;
  338: 
  339: cmove>	c_from c_to u --	string	c_move_up
  340: while (u-- > 0)
  341:   c_to[u] = c_from[u];
  342: :
  343:  dup 0= IF  drop 2drop exit  THEN
  344:  rot over + -rot bounds swap 1-
  345:  DO  1- dup c@ I c!  -1 +LOOP  drop ;
  346: 
  347: fill	c_addr u c --	core
  348: memset(c_addr,c,u);
  349: :
  350:  -rot bounds
  351:  ?DO  dup I c!  LOOP  drop ;
  352: 
  353: compare		c_addr1 u1 c_addr2 u2 -- n	string
  354: ""Compare the strings lexicographically. If they are equal, n is 0; if
  355: the first string is smaller, n is -1; if the first string is larger, n
  356: is 1. Currently this is based on the machine's character
  357: comparison. In the future, this may change to considering the current
  358: locale and its collation order.""
  359: n = memcmp(c_addr1, c_addr2, u1<u2 ? u1 : u2);
  360: if (n==0)
  361:   n = u1-u2;
  362: if (n<0)
  363:   n = -1;
  364: else if (n>0)
  365:   n = 1;
  366: :
  367:  rot 2dup - >r min swap -text dup
  368:  IF    rdrop
  369:  ELSE  drop r@ 0>
  370:        IF    rdrop -1
  371:        ELSE  r> 1 and
  372:        THEN
  373:  THEN ;
  374: 
  375: -text		c_addr1 u c_addr2 -- n	new	dash_text
  376: n = memcmp(c_addr1, c_addr2, u);
  377: if (n<0)
  378:   n = -1;
  379: else if (n>0)
  380:   n = 1;
  381: :
  382:  swap bounds
  383:  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
  384:  ELSE  c@ I c@ - unloop  THEN  -text-flag ;
  385: : -text-flag ( n -- -1/0/1 )
  386:  dup 0< IF  drop -1  ELSE  0>  IF  1  ELSE  0  THEN  THEN  ;
  387: 
  388: capscomp	c_addr1 u c_addr2 -- n	new
  389: Char c1, c2;
  390: for (;; u--, c_addr1++, c_addr2++) {
  391:   if (u == 0) {
  392:     n = 0;
  393:     break;
  394:   }
  395:   c1 = toupper(*c_addr1);
  396:   c2 = toupper(*c_addr2);
  397:   if (c1 != c2) {
  398:     if (c1 < c2)
  399:       n = -1;
  400:     else
  401:       n = 1;
  402:     break;
  403:   }
  404: }
  405: :
  406:  swap bounds
  407:  ?DO  dup c@ toupper I c@ toupper = WHILE  1+  LOOP  drop 0
  408:  ELSE  c@ toupper I c@ toupper - unloop  THEN  -text-flag ;
  409: 
  410: -trailing	c_addr u1 -- c_addr u2		string	dash_trailing
  411: u2 = u1;
  412: while (c_addr[u2-1] == ' ')
  413:   u2--;
  414: :
  415:  BEGIN  1- 2dup + c@ bl =  WHILE
  416:         dup  0= UNTIL  ELSE  1+  THEN ;
  417: 
  418: /string		c_addr1 u1 n -- c_addr2 u2	string	slash_string
  419: c_addr2 = c_addr1+n;
  420: u2 = u1-n;
  421: :
  422:  tuck - >r + r> dup 0< IF  - 0  THEN ;
  423: 
  424: +	n1 n2 -- n		core	plus
  425: n = n1+n2;
  426: 
  427: -	n1 n2 -- n		core	minus
  428: n = n1-n2;
  429: :
  430:  negate + ;
  431: 
  432: negate	n1 -- n2		core
  433: /* use minus as alias */
  434: n2 = -n1;
  435: :
  436:  invert 1+ ;
  437: 
  438: 1+	n1 -- n2		core		one_plus
  439: n2 = n1+1;
  440: :
  441:  1 + ;
  442: 
  443: 1-	n1 -- n2		core		one_minus
  444: n2 = n1-1;
  445: :
  446:  1 - ;
  447: 
  448: max	n1 n2 -- n	core
  449: if (n1<n2)
  450:   n = n2;
  451: else
  452:   n = n1;
  453: :
  454:  2dup < IF swap THEN drop ;
  455: 
  456: min	n1 n2 -- n	core
  457: if (n1<n2)
  458:   n = n1;
  459: else
  460:   n = n2;
  461: :
  462:  2dup > IF swap THEN drop ;
  463: 
  464: abs	n1 -- n2	core
  465: if (n1<0)
  466:   n2 = -n1;
  467: else
  468:   n2 = n1;
  469: :
  470:  dup 0< IF negate THEN ;
  471: 
  472: *	n1 n2 -- n		core	star
  473: n = n1*n2;
  474: :
  475:  um* drop ;
  476: 
  477: /	n1 n2 -- n		core	slash
  478: n = n1/n2;
  479: :
  480:  /mod nip ;
  481: 
  482: mod	n1 n2 -- n		core
  483: n = n1%n2;
  484: :
  485:  /mod drop ;
  486: 
  487: /mod	n1 n2 -- n3 n4		core		slash_mod
  488: n4 = n1/n2;
  489: n3 = n1%n2; /* !! is this correct? look into C standard! */
  490: :
  491:  >r s>d r> fm/mod ;
  492: 
  493: 2*	n1 -- n2		core		two_star
  494: n2 = 2*n1;
  495: :
  496:  dup + ;
  497: 
  498: 2/	n1 -- n2		core		two_slash
  499: /* !! is this still correct? */
  500: n2 = n1>>1;
  501: 
  502: fm/mod	d1 n1 -- n2 n3		core		f_m_slash_mod
  503: ""floored division: d1 = n3*n1+n2, n1>n2>=0 or 0>=n2>n1""
  504: /* assumes that the processor uses either floored or symmetric division */
  505: n3 = d1/n1;
  506: n2 = d1%n1;
  507: /* note that this 1%-3>0 is optimized by the compiler */
  508: if (1%-3>0 && (d1<0) != (n1<0) && n2!=0) {
  509:   n3--;
  510:   n2+=n1;
  511: }
  512: 
  513: sm/rem	d1 n1 -- n2 n3		core		s_m_slash_rem
  514: ""symmetric division: d1 = n3*n1+n2, sign(n2)=sign(d1) or 0""
  515: /* assumes that the processor uses either floored or symmetric division */
  516: n3 = d1/n1;
  517: n2 = d1%n1;
  518: /* note that this 1%-3<0 is optimized by the compiler */
  519: if (1%-3<0 && (d1<0) != (n1<0) && n2!=0) {
  520:   n3++;
  521:   n2-=n1;
  522: }
  523: :
  524:  over >r dup >r abs -rot
  525:  dabs rot um/mod
  526:  r> 0< IF       negate       THEN
  527:  r> 0< IF  swap negate swap  THEN ;
  528: 
  529: m*	n1 n2 -- d		core	m_star
  530: d = (DCell)n1 * (DCell)n2;
  531: :
  532:  2dup      0< and >r
  533:  2dup swap 0< and >r
  534:  um* r> - r> - ;
  535: 
  536: um*	u1 u2 -- ud		core	u_m_star
  537: /* use u* as alias */
  538: ud = (UDCell)u1 * (UDCell)u2;
  539: 
  540: um/mod	ud u1 -- u2 u3		core	u_m_slash_mod
  541: u3 = ud/u1;
  542: u2 = ud%u1;
  543: :
  544:   dup IF  0 (um/mod)  THEN  nip ; 
  545: : (um/mod)  ( ud ud--ud u)
  546:   2dup >r >r  dup 0< 
  547:   IF    2drop 0 
  548:   ELSE  2dup d+  (um/mod)  2*  THEN 
  549:   -rot  r> r> 2over 2over  du<
  550:   IF    2drop rot 
  551:   ELSE  dnegate  d+  rot 1+  THEN ; 
  552: 
  553: m+	d1 n -- d2		double		m_plus
  554: d2 = d1+n;
  555: :
  556:  s>d d+ ;
  557: 
  558: d+	d1 d2 -- d		double	d_plus
  559: d = d1+d2;
  560: :
  561:  >r swap >r over 2/ over 2/ + >r over 1 and over 1 and + 2/
  562:  r> + >r + r> 0< r> r> + swap - ;
  563: 
  564: d-	d1 d2 -- d		double		d_minus
  565: d = d1-d2;
  566: :
  567:  dnegate d+ ;
  568: 
  569: dnegate	d1 -- d2		double
  570: /* use dminus as alias */
  571: d2 = -d1;
  572: :
  573:  invert swap negate tuck 0= - ;
  574: 
  575: dmax	d1 d2 -- d	double
  576: if (d1<d2)
  577:   d = d2;
  578: else
  579:   d = d1;
  580: :
  581:  2over 2over d> IF  2swap  THEN 2drop ;
  582: 
  583: dmin	d1 d2 -- d	double
  584: if (d1<d2)
  585:   d = d1;
  586: else
  587:   d = d2;
  588: :
  589:  2over 2over d< IF  2swap  THEN 2drop ;
  590: 
  591: dabs	d1 -- d2	double
  592: if (d1<0)
  593:   d2 = -d1;
  594: else
  595:   d2 = d1;
  596: :
  597:  dup 0< IF dnegate THEN ;
  598: 
  599: d2*	d1 -- d2		double		d_two_star
  600: d2 = 2*d1;
  601: :
  602:  2dup d+ ;
  603: 
  604: d2/	d1 -- d2		double		d_two_slash
  605: /* !! is this still correct? */
  606: d2 = d1>>1;
  607: :
  608:  dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
  609:  r> IF  [ 1 8 cells 1- lshift ] Literal + THEN  swap ;
  610: 
  611: d>s	d -- n			double		d_to_s
  612: /* make this an alias for drop? */
  613: n = d;
  614: :
  615:  drop ;
  616: 
  617: and	w1 w2 -- w		core
  618: w = w1&w2;
  619: 
  620: or	w1 w2 -- w		core
  621: w = w1|w2;
  622: 
  623: xor	w1 w2 -- w		core
  624: w = w1^w2;
  625: 
  626: invert	w1 -- w2		core
  627: w2 = ~w1;
  628: :
  629:  -1 xor ;
  630: 
  631: rshift	u1 n -- u2		core
  632:   u2 = u1>>n;
  633: 
  634: lshift	u1 n -- u2		core
  635:   u2 = u1<<n;
  636: 
  637: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
  638: define(comparisons,
  639: $1=	$2 -- f		$6	$3equals
  640: f = FLAG($4==$5);
  641: 
  642: $1<>	$2 -- f		$7	$3different
  643: /* use != as alias ? */
  644: f = FLAG($4!=$5);
  645: 
  646: $1<	$2 -- f		$8	$3less
  647: f = FLAG($4<$5);
  648: 
  649: $1>	$2 -- f		$9	$3greater
  650: f = FLAG($4>$5);
  651: 
  652: $1<=	$2 -- f		gforth	$3less_or_equal
  653: f = FLAG($4<=$5);
  654: 
  655: $1>=	$2 -- f		gforth	$3greater_or_equal
  656: f = FLAG($4>=$5);
  657: 
  658: )
  659: 
  660: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
  661: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
  662: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
  663: comparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
  664: comparisons(d0, d, d_zero_, d, 0, double, gforth, double, gforth)
  665: comparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
  666: 
  667: within	u1 u2 u3 -- f		core-ext
  668: f = FLAG(u1-u2 < u3-u2);
  669: :
  670:  over - >r - r> u< ;
  671: 
  672: sp@	-- a_addr		gforth		spat
  673: a_addr = sp+1;
  674: 
  675: sp!	a_addr --		gforth		spstore
  676: sp = a_addr;
  677: /* works with and without TOS caching */
  678: 
  679: rp@	-- a_addr		gforth		rpat
  680: a_addr = rp;
  681: 
  682: rp!	a_addr --		gforth		rpstore
  683: rp = a_addr;
  684: 
  685: fp@	-- f_addr	gforth	fp_fetch
  686: f_addr = fp;
  687: 
  688: fp!	f_addr --	gforth	fp_store
  689: fp = f_addr;
  690: 
  691: ;s	--		gforth	semis
  692: ip = (Xt *)(*rp++);
  693: NEXT_P0;
  694: 
  695: >r	w --		core	to_r
  696: *--rp = w;
  697: 
  698: r>	-- w		core	r_from
  699: w = *rp++;
  700: 
  701: r@	-- w		core	r_fetch
  702: /* use r as alias */
  703: /* make r@ an alias for i */
  704: w = *rp;
  705: 
  706: rdrop	--		gforth
  707: rp++;
  708: 
  709: i'	-- w		gforth		i_tick
  710: w=rp[1];
  711: 
  712: 2>r	w1 w2 --	core-ext	two_to_r
  713: *--rp = w1;
  714: *--rp = w2;
  715: 
  716: 2r>	-- w1 w2	core-ext	two_r_from
  717: w2 = *rp++;
  718: w1 = *rp++;
  719: 
  720: 2r@	-- w1 w2	core-ext	two_r_fetch
  721: w2 = rp[0];
  722: w1 = rp[1];
  723: 
  724: 2rdrop	--		gforth	two_r_drop
  725: rp+=2;
  726: 
  727: over	w1 w2 -- w1 w2 w1		core
  728: 
  729: drop	w --		core
  730: 
  731: swap	w1 w2 -- w2 w1		core
  732: 
  733: dup	w -- w w		core
  734: 
  735: rot	w1 w2 w3 -- w2 w3 w1	core	rote
  736: 
  737: -rot	w1 w2 w3 -- w3 w1 w2	gforth	not_rote
  738: :
  739:  rot rot ;
  740: 
  741: nip	w1 w2 -- w2		core-ext
  742: :
  743:  swap drop ;
  744: 
  745: tuck	w1 w2 -- w2 w1 w2	core-ext
  746: :
  747:  swap over ;
  748: 
  749: ?dup	w -- w			core	question_dupe
  750: if (w!=0) {
  751:   IF_TOS(*sp-- = w;)
  752: #ifndef USE_TOS
  753:   *--sp = w;
  754: #endif
  755: }
  756: :
  757:  dup IF dup THEN ;
  758: 
  759: pick	u -- w			core-ext
  760: w = sp[u+1];
  761: :
  762:  1+ cells sp@ + @ ;
  763: 
  764: 2drop	w1 w2 --		core	two_drop
  765: :
  766:  drop drop ;
  767: 
  768: 2dup	w1 w2 -- w1 w2 w1 w2	core	two_dupe
  769: :
  770:  over over ;
  771: 
  772: 2over	w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2	core	two_over
  773: :
  774:  3 pick 3 pick ;
  775: 
  776: 2swap	w1 w2 w3 w4 -- w3 w4 w1 w2	core	two_swap
  777: :
  778:  >r -rot r> -rot ;
  779: 
  780: 2rot	w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2	double-ext	two_rote
  781: :
  782:  >r >r 2swap r> r> 2swap ;
  783: 
  784: 2nip	w1 w2 w3 w4 -- w3 w4	gforth	two_nip
  785: :
  786:  2swap 2drop ;
  787: 
  788: 2tuck	w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4	gforth	two_tuck
  789: :
  790:  2swap 2over ;
  791: 
  792: \ toggle is high-level: 0.11/0.42%
  793: 
  794: @	a_addr -- w		core	fetch
  795: w = *a_addr;
  796: 
  797: !	w a_addr --		core	store
  798: *a_addr = w;
  799: 
  800: +!	n a_addr --		core	plus_store
  801: *a_addr += n;
  802: 
  803: c@	c_addr -- c		core	cfetch
  804: c = *c_addr;
  805: 
  806: c!	c c_addr --		core	cstore
  807: *c_addr = c;
  808: 
  809: 2!	w1 w2 a_addr --		core	two_store
  810: a_addr[0] = w2;
  811: a_addr[1] = w1;
  812: :
  813:  tuck ! cell+ ! ;
  814: 
  815: 2@	a_addr -- w1 w2		core	two_fetch
  816: w2 = a_addr[0];
  817: w1 = a_addr[1];
  818: :
  819:  dup cell+ @ swap @ ;
  820: 
  821: d!	d a_addr --		double	d_store
  822: /* !! alignment problems on some machines */
  823: *(DCell *)a_addr = d;
  824: 
  825: d@	a_addr -- d		double	d_fetch
  826: d = *(DCell *)a_addr;
  827: 
  828: cell+	a_addr1 -- a_addr2	core	cell_plus
  829: a_addr2 = a_addr1+1;
  830: :
  831:  [ cell ] Literal + ;
  832: 
  833: cells	n1 -- n2		core
  834: n2 = n1 * sizeof(Cell);
  835: :
  836:  [ cell ]
  837:  [ 2/ dup ] [IF] 2* [THEN]
  838:  [ 2/ dup ] [IF] 2* [THEN]
  839:  [ 2/ dup ] [IF] 2* [THEN]
  840:  [ 2/ dup ] [IF] 2* [THEN]
  841:  [ drop ] ;
  842: 
  843: char+	c_addr1 -- c_addr2	core	care_plus
  844: c_addr2 = c_addr1 + 1;
  845: :
  846:  1+ ;
  847: 
  848: (chars)		n1 -- n2	gforth	paren_cares
  849: n2 = n1 * sizeof(Char);
  850: :
  851:  ;
  852: 
  853: count	c_addr1 -- c_addr2 u	core
  854: u = *c_addr1;
  855: c_addr2 = c_addr1+1;
  856: :
  857:  dup 1+ swap c@ ;
  858: 
  859: (bye)	n --	gforth	paren_bye
  860: return (Label *)n;
  861: 
  862: system	c_addr u -- n	gforth
  863: n=system(cstr(c_addr,u,1)); /* ~ expansion on first part of string? */
  864: 
  865: getenv	c_addr1 u1 -- c_addr2 u2	gforth
  866: c_addr2 = getenv(cstr(c_addr1,u1,1));
  867: u2 = (c_addr2 == NULL ? 0 : strlen(c_addr2));
  868: 
  869: popen	c_addr u n -- wfileid	own
  870: static char* mode[2]={"r","w"}; /* !! should we use FAM here? */
  871: wfileid=(Cell)popen(cstr(c_addr,u,1),mode[n]); /* ~ expansion of 1st arg? */
  872: 
  873: pclose	wfileid -- wior		own
  874: wior=pclose((FILE *)wfileid); /* !! what to do with the result */
  875: 
  876: time&date	-- nsec nmin nhour nday nmonth nyear	facility-ext	time_and_date
  877: struct timeval time1;
  878: struct timezone zone1;
  879: struct tm *ltime;
  880: gettimeofday(&time1,&zone1);
  881: ltime=localtime((time_t *)&time1.tv_sec);
  882: nyear =ltime->tm_year+1900;
  883: nmonth=ltime->tm_mon+1;
  884: nday  =ltime->tm_mday;
  885: nhour =ltime->tm_hour;
  886: nmin  =ltime->tm_min;
  887: nsec  =ltime->tm_sec;
  888: 
  889: ms	n --	facility-ext
  890: struct timeval timeout;
  891: timeout.tv_sec=n/1000;
  892: timeout.tv_usec=1000*(n%1000);
  893: (void)select(0,0,0,0,&timeout);
  894: 
  895: allocate	u -- a_addr wior	memory
  896: a_addr = (Cell *)malloc(u?u:1);
  897: wior = IOR(a_addr==NULL);
  898: 
  899: free		a_addr -- wior		memory
  900: free(a_addr);
  901: wior = 0;
  902: 
  903: resize		a_addr1 u -- a_addr2 wior	memory
  904: ""Change the size of the allocated area at @i{a_addr1} to @i{u}
  905: address units, possibly moving the contents to a different
  906: area. @i{a_addr2} is the address of the resulting area. If
  907: @code{a_addr2} is 0, gforth's (but not the standard) @code{resize}
  908: @code{allocate}s @i{u} address units.""
  909: /* the following check is not necessary on most OSs, but it is needed
  910:    on SunOS 4.1.2. */
  911: if (a_addr1==NULL)
  912:   a_addr2 = (Cell *)malloc(u);
  913: else
  914:   a_addr2 = (Cell *)realloc(a_addr1, u);
  915: wior = IOR(a_addr2==NULL);	/* !! Define a return code */
  916: 
  917: (f83find)	c_addr u f83name1 -- f83name2	new	paren_f83find
  918: for (; f83name1 != NULL; f83name1 = f83name1->next)
  919:   if (F83NAME_COUNT(f83name1)==u &&
  920:       strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
  921:     break;
  922: f83name2=f83name1;
  923: :
  924:  BEGIN  dup  WHILE
  925:         >r dup r@ cell+ c@ $1F and =
  926: 	IF  2dup r@ cell+ char+ capscomp  0=
  927: 	    IF  2drop r>  EXIT  THEN  THEN
  928: 	r> @
  929:  REPEAT  nip nip ;
  930: 
  931: (hashfind)	c_addr u a_addr -- f83name2	new	paren_hashfind
  932: F83Name *f83name1;
  933: f83name2=NULL;
  934: while(a_addr != NULL)
  935: {
  936:    f83name1=(F83Name *)(a_addr[1]);
  937:    a_addr=(Cell *)(a_addr[0]);
  938:    if (F83NAME_COUNT(f83name1)==u &&
  939:        strncasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
  940:      {
  941: 	f83name2=f83name1;
  942: 	break;
  943:      }
  944: }
  945: :
  946:  BEGIN  dup  WHILE
  947:         2@ >r >r dup r@ cell+ c@ $1F and =
  948:         IF  2dup r@ cell+ char+ capscomp 0=
  949: 	    IF  2drop r> rdrop  EXIT  THEN  THEN
  950: 	rdrop r>
  951:  REPEAT nip nip ;
  952: 
  953: (hashkey)	c_addr u1 -- u2		gforth	paren_hashkey
  954: u2=0;
  955: while(u1--)
  956:    u2+=(Cell)toupper(*c_addr++);
  957: :
  958:  0 -rot bounds ?DO  I c@ toupper +  LOOP ;
  959: 
  960: (hashkey1)	c_addr u ubits -- ukey		gforth	paren_hashkey1
  961: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
  962: /* this hash function rotates the key at every step by rot bits within
  963:    ubits bits and xors it with the character. This function does ok in
  964:    the chi-sqare-test.  Rot should be <=7 (preferably <=5) for
  965:    ASCII strings (larger if ubits is large), and should share no
  966:    divisors with ubits.
  967: */
  968: 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];
  969: Char *cp = c_addr;
  970: for (ukey=0; cp<c_addr+u; cp++)
  971:     ukey = ((((ukey<<rot) | (ukey>>(ubits-rot))) 
  972: 	     ^ toupper(*cp))
  973: 	    & ((1<<ubits)-1));
  974: :
  975:  dup rot-values + c@ over 1 swap lshift 1- >r
  976:  tuck - 2swap r> 0 2swap bounds
  977:  ?DO  dup 4 pick lshift swap 3 pick rshift or
  978:       I c@ toupper xor
  979:       over and  LOOP
  980:  nip nip nip ;
  981: Create rot-values
  982:   5 c, 0 c, 1 c, 2 c, 3 c,  4 c, 5 c, 5 c, 5 c, 5 c,
  983:   3 c, 5 c, 5 c, 5 c, 5 c,  7 c, 5 c, 5 c, 5 c, 5 c,
  984:   7 c, 5 c, 5 c, 5 c, 5 c,  6 c, 5 c, 5 c, 5 c, 5 c,
  985:   7 c, 5 c, 5 c,
  986: 
  987: (parse-white)	c_addr1 u1 -- c_addr2 u2	gforth	paren_parse_white
  988: /* use !isgraph instead of isspace? */
  989: Char *endp = c_addr1+u1;
  990: while (c_addr1<endp && isspace(*c_addr1))
  991:   c_addr1++;
  992: if (c_addr1<endp) {
  993:   for (c_addr2 = c_addr1; c_addr1<endp && !isspace(*c_addr1); c_addr1++)
  994:     ;
  995:   u2 = c_addr1-c_addr2;
  996: }
  997: else {
  998:   c_addr2 = c_addr1;
  999:   u2 = 0;
 1000: }
 1001: :
 1002:  BEGIN  dup  WHILE  over c@ bl <=  WHILE  1 /string
 1003:  REPEAT  THEN  2dup
 1004:  BEGIN  dup  WHILE  over c@ bl >   WHILE  1 /string
 1005:  REPEAT  THEN  nip - ;
 1006: 
 1007: close-file	wfileid -- wior		file	close_file
 1008: wior = IOR(fclose((FILE *)wfileid)==EOF);
 1009: 
 1010: open-file	c_addr u ntype -- w2 wior	file	open_file
 1011: w2 = (Cell)fopen(tilde_cstr(c_addr, u, 1), fileattr[ntype]);
 1012: wior =  IOR(w2 == 0);
 1013: 
 1014: create-file	c_addr u ntype -- w2 wior	file	create_file
 1015: Cell	fd;
 1016: fd = open(tilde_cstr(c_addr, u, 1), O_CREAT|O_RDWR|O_TRUNC, 0666);
 1017: if (fd != -1) {
 1018:   w2 = (Cell)fdopen(fd, fileattr[ntype]);
 1019:   wior = IOR(w2 == 0);
 1020: } else {
 1021:   w2 = 0;
 1022:   wior = IOR(1);
 1023: }
 1024: 
 1025: delete-file	c_addr u -- wior		file	delete_file
 1026: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
 1027: 
 1028: rename-file	c_addr1 u1 c_addr2 u2 -- wior	file-ext	rename_file
 1029: char *s1=tilde_cstr(c_addr2, u2, 1);
 1030: wior = IOR(rename(tilde_cstr(c_addr1, u1, 0), s1)==-1);
 1031: 
 1032: file-position	wfileid -- ud wior	file	file_position
 1033: /* !! use tell and lseek? */
 1034: ud = ftell((FILE *)wfileid);
 1035: wior = IOR(ud==-1);
 1036: 
 1037: reposition-file	ud wfileid -- wior	file	reposition_file
 1038: wior = IOR(fseek((FILE *)wfileid, (long)ud, SEEK_SET)==-1);
 1039: 
 1040: file-size	wfileid -- ud wior	file	file_size
 1041: struct stat buf;
 1042: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
 1043: ud = buf.st_size;
 1044: 
 1045: resize-file	ud wfileid -- wior	file	resize_file
 1046: wior = IOR(ftruncate(fileno((FILE *)wfileid), (Cell)ud)==-1);
 1047: 
 1048: read-file	c_addr u1 wfileid -- u2 wior	file	read_file
 1049: /* !! fread does not guarantee enough */
 1050: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
 1051: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
 1052: /* !! is the value of ferror errno-compatible? */
 1053: if (wior)
 1054:   clearerr((FILE *)wfileid);
 1055: 
 1056: read-line	c_addr u1 wfileid -- u2 flag wior	file	read_line
 1057: /*
 1058: Cell c;
 1059: flag=-1;
 1060: for(u2=0; u2<u1; u2++)
 1061: {
 1062:    *c_addr++ = (Char)(c = getc((FILE *)wfileid));
 1063:    if(c=='\n') break;
 1064:    if(c==EOF)
 1065:      {
 1066: 	flag=FLAG(u2!=0);
 1067: 	break;
 1068:      }
 1069: }
 1070: wior=FILEIO(ferror((FILE *)wfileid));
 1071: */
 1072: if ((flag=FLAG(!feof((FILE *)wfileid) &&
 1073: 	       fgets(c_addr,u1+1,(FILE *)wfileid) != NULL))) {
 1074:   wior=FILEIO(ferror((FILE *)wfileid)); /* !! ior? */
 1075:   if (wior)
 1076:     clearerr((FILE *)wfileid);
 1077:   u2 = strlen(c_addr);
 1078:   u2-=((u2>0) && (c_addr[u2-1]==NEWLINE));
 1079: }
 1080: else {
 1081:   wior=0;
 1082:   u2=0;
 1083: }
 1084: 
 1085: write-file	c_addr u1 wfileid -- wior	file	write_file
 1086: /* !! fwrite does not guarantee enough */
 1087: {
 1088:   Cell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
 1089:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
 1090:   if (wior)
 1091:     clearerr((FILE *)wfileid);
 1092: }
 1093: 
 1094: flush-file	wfileid -- wior		file-ext	flush_file
 1095: wior = IOR(fflush((FILE *) wfileid)==EOF);
 1096: 
 1097: file-status	c_addr u -- ntype wior	file-ext	file_status
 1098: char *filename=tilde_cstr(c_addr, u, 1);
 1099: if (access (filename, F_OK) != 0) {
 1100:   ntype=0;
 1101:   wior=IOR(1);
 1102: }
 1103: else if (access (filename, R_OK | W_OK) == 0) {
 1104:   ntype=2; /* r/w */
 1105:   wior=0;
 1106: }
 1107: else if (access (filename, R_OK) == 0) {
 1108:   ntype=0; /* r/o */
 1109:   wior=0;
 1110: }
 1111: else if (access (filename, W_OK) == 0) {
 1112:   ntype=4; /* w/o */
 1113:   wior=0;
 1114: }
 1115: else {
 1116:   ntype=1; /* well, we cannot access the file, but better deliver a legal
 1117: 	    access mode (r/o bin), so we get a decent error later upon open. */
 1118:   wior=0;
 1119: }
 1120: 
 1121: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
 1122: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
 1123: 
 1124: d>f		d -- r		float	d_to_f
 1125: r = d;
 1126: 
 1127: f>d		r -- d		float	f_to_d
 1128: /* !! basis 15 is not very specific */
 1129: d = r;
 1130: 
 1131: f!		r f_addr --	float	f_store
 1132: *f_addr = r;
 1133: 
 1134: f@		f_addr -- r	float	f_fetch
 1135: r = *f_addr;
 1136: 
 1137: df@		df_addr -- r	float-ext	d_f_fetch
 1138: #ifdef IEEE_FP
 1139: r = *df_addr;
 1140: #else
 1141: !! df@
 1142: #endif
 1143: 
 1144: df!		r df_addr --	float-ext	d_f_store
 1145: #ifdef IEEE_FP
 1146: *df_addr = r;
 1147: #else
 1148: !! df!
 1149: #endif
 1150: 
 1151: sf@		sf_addr -- r	float-ext	s_f_fetch
 1152: #ifdef IEEE_FP
 1153: r = *sf_addr;
 1154: #else
 1155: !! sf@
 1156: #endif
 1157: 
 1158: sf!		r sf_addr --	float-ext	s_f_store
 1159: #ifdef IEEE_FP
 1160: *sf_addr = r;
 1161: #else
 1162: !! sf!
 1163: #endif
 1164: 
 1165: f+		r1 r2 -- r3	float	f_plus
 1166: r3 = r1+r2;
 1167: 
 1168: f-		r1 r2 -- r3	float	f_minus
 1169: r3 = r1-r2;
 1170: 
 1171: f*		r1 r2 -- r3	float	f_star
 1172: r3 = r1*r2;
 1173: 
 1174: f/		r1 r2 -- r3	float	f_slash
 1175: r3 = r1/r2;
 1176: 
 1177: f**		r1 r2 -- r3	float-ext	f_star_star
 1178: ""@i{r3} is @i{r1} raised to the @i{r2}th power""
 1179: r3 = pow(r1,r2);
 1180: 
 1181: fnegate		r1 -- r2	float
 1182: r2 = - r1;
 1183: 
 1184: fdrop		r --		float
 1185: 
 1186: fdup		r -- r r	float
 1187: 
 1188: fswap		r1 r2 -- r2 r1	float
 1189: 
 1190: fover		r1 r2 -- r1 r2 r1	float
 1191: 
 1192: frot		r1 r2 r3 -- r2 r3 r1	float
 1193: 
 1194: fnip		r1 r2 -- r2	gforth
 1195: 
 1196: ftuck		r1 r2 -- r2 r1 r2	gforth
 1197: 
 1198: float+		f_addr1 -- f_addr2	float	float_plus
 1199: f_addr2 = f_addr1+1;
 1200: 
 1201: floats		n1 -- n2	float
 1202: n2 = n1*sizeof(Float);
 1203: 
 1204: floor		r1 -- r2	float
 1205: ""round towards the next smaller integral value, i.e., round toward negative infinity""
 1206: /* !! unclear wording */
 1207: r2 = floor(r1);
 1208: 
 1209: fround		r1 -- r2	float
 1210: ""round to the nearest integral value""
 1211: /* !! unclear wording */
 1212: #ifdef HAVE_RINT
 1213: r2 = rint(r1);
 1214: #else
 1215: r2 = floor(r1+0.5);
 1216: /* !! This is not quite true to the rounding rules given in the standard */
 1217: #endif
 1218: 
 1219: fmax		r1 r2 -- r3	float
 1220: if (r1<r2)
 1221:   r3 = r2;
 1222: else
 1223:   r3 = r1;
 1224: 
 1225: fmin		r1 r2 -- r3	float
 1226: if (r1<r2)
 1227:   r3 = r1;
 1228: else
 1229:   r3 = r2;
 1230: 
 1231: represent		r c_addr u -- n f1 f2	float
 1232: char *sig;
 1233: Cell flag;
 1234: Cell decpt;
 1235: sig=ecvt(r, u, (int *)&decpt, (int *)&flag);
 1236: n=(r==0 ? 1 : decpt);
 1237: f1=FLAG(flag!=0);
 1238: f2=FLAG(isdigit(sig[0])!=0);
 1239: memmove(c_addr,sig,u);
 1240: 
 1241: >float	c_addr u -- flag	float	to_float
 1242: /* real signature: c_addr u -- r t / f */
 1243: Float r;
 1244: char *number=cstr(c_addr, u, 1);
 1245: char *endconv;
 1246: while(isspace(number[--u]) && u>0);
 1247: switch(number[u])
 1248: {
 1249:    case 'd':
 1250:    case 'D':
 1251:    case 'e':
 1252:    case 'E':  break;
 1253:    default :  u++; break;
 1254: }
 1255: number[u]='\0';
 1256: r=strtod(number,&endconv);
 1257: if((flag=FLAG(!(Cell)*endconv)))
 1258: {
 1259:    IF_FTOS(fp[0] = FTOS);
 1260:    fp += -1;
 1261:    FTOS = r;
 1262: }
 1263: else if(*endconv=='d' || *endconv=='D')
 1264: {
 1265:    *endconv='E';
 1266:    r=strtod(number,&endconv);
 1267:    if((flag=FLAG(!(Cell)*endconv)))
 1268:      {
 1269: 	IF_FTOS(fp[0] = FTOS);
 1270: 	fp += -1;
 1271: 	FTOS = r;
 1272:      }
 1273: }
 1274: 
 1275: fabs		r1 -- r2	float-ext
 1276: r2 = fabs(r1);
 1277: 
 1278: facos		r1 -- r2	float-ext
 1279: r2 = acos(r1);
 1280: 
 1281: fasin		r1 -- r2	float-ext
 1282: r2 = asin(r1);
 1283: 
 1284: fatan		r1 -- r2	float-ext
 1285: r2 = atan(r1);
 1286: 
 1287: fatan2		r1 r2 -- r3	float-ext
 1288: ""@i{r1/r2}=tan@i{r3}. The standard does not require, but probably
 1289: intends this to be the inverse of @code{fsincos}. In gforth it is.""
 1290: r3 = atan2(r1,r2);
 1291: 
 1292: fcos		r1 -- r2	float-ext
 1293: r2 = cos(r1);
 1294: 
 1295: fexp		r1 -- r2	float-ext
 1296: r2 = exp(r1);
 1297: 
 1298: fexpm1		r1 -- r2	float-ext
 1299: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
 1300: #ifdef HAVE_EXPM1
 1301: extern double expm1(double);
 1302: r2 = expm1(r1);
 1303: #else
 1304: r2 = exp(r1)-1.;
 1305: #endif
 1306: 
 1307: fln		r1 -- r2	float-ext
 1308: r2 = log(r1);
 1309: 
 1310: flnp1		r1 -- r2	float-ext
 1311: ""@i{r2}=ln(@i{r1}+1)""
 1312: #ifdef HAVE_LOG1P
 1313: extern double log1p(double);
 1314: r2 = log1p(r1);
 1315: #else
 1316: r2 = log(r1+1.);
 1317: #endif
 1318: 
 1319: flog		r1 -- r2	float-ext
 1320: ""the decimal logarithm""
 1321: r2 = log10(r1);
 1322: 
 1323: falog		r1 -- r2	float-ext
 1324: ""@i{r2}=10**@i{r1}""
 1325: extern double pow10(double);
 1326: r2 = pow10(r1);
 1327: 
 1328: fsin		r1 -- r2	float-ext
 1329: r2 = sin(r1);
 1330: 
 1331: fsincos		r1 -- r2 r3	float-ext
 1332: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
 1333: r2 = sin(r1);
 1334: r3 = cos(r1);
 1335: 
 1336: fsqrt		r1 -- r2	float-ext
 1337: r2 = sqrt(r1);
 1338: 
 1339: ftan		r1 -- r2	float-ext
 1340: r2 = tan(r1);
 1341: :
 1342:  fsincos f/ ;
 1343: 
 1344: fsinh		r1 -- r2	float-ext
 1345: r2 = sinh(r1);
 1346: :
 1347:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
 1348: 
 1349: fcosh		r1 -- r2	float-ext
 1350: r2 = cosh(r1);
 1351: :
 1352:  fexp fdup 1/f f+ f2/ ;
 1353: 
 1354: ftanh		r1 -- r2	float-ext
 1355: r2 = tanh(r1);
 1356: :
 1357:  f2* fexpm1 fdup 2. d>f f+ f/ ;
 1358: 
 1359: fasinh		r1 -- r2	float-ext
 1360: r2 = asinh(r1);
 1361: :
 1362:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
 1363: 
 1364: facosh		r1 -- r2	float-ext
 1365: r2 = acosh(r1);
 1366: :
 1367:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
 1368: 
 1369: fatanh		r1 -- r2	float-ext
 1370: r2 = atanh(r1);
 1371: :
 1372:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
 1373:  r> IF  fnegate  THEN ;
 1374: 
 1375: sfloats		n1 -- n2	float-ext	s_floats
 1376: n2 = n1*sizeof(SFloat);
 1377: 
 1378: dfloats		n1 -- n2	float-ext	d_floats
 1379: n2 = n1*sizeof(DFloat);
 1380: 
 1381: aligned		c_addr -- a_addr	core
 1382: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
 1383: :
 1384:  [ cell 1- ] Literal + [ -1 cells ] Literal and ;
 1385: 
 1386: faligned	c_addr -- f_addr	float	f_aligned
 1387: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
 1388: :
 1389:  [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
 1390: 
 1391: sfaligned	c_addr -- sf_addr	float-ext	s_f_aligned
 1392: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
 1393: :
 1394:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
 1395: 
 1396: dfaligned	c_addr -- df_addr	float-ext	d_f_aligned
 1397: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
 1398: :
 1399:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
 1400: 
 1401: \ The following words access machine/OS/installation-dependent
 1402: \   Gforth internals
 1403: \ !! how about environmental queries DIRECT-THREADED,
 1404: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
 1405: 
 1406: >body		xt -- a_addr	core	to_body
 1407: a_addr = PFA(xt);
 1408: 
 1409: >code-address		xt -- c_addr		gforth	to_code_address
 1410: ""c_addr is the code address of the word xt""
 1411: /* !! This behaves installation-dependently for DOES-words */
 1412: c_addr = CODE_ADDRESS(xt);
 1413: 
 1414: >does-code	xt -- a_addr		gforth	to_does_code
 1415: ""If xt ist the execution token of a defining-word-defined word,
 1416: a_addr is the start of the Forth code after the DOES>; Otherwise the
 1417: behaviour is undefined""
 1418: /* !! there is currently no way to determine whether a word is
 1419: defining-word-defined */
 1420: a_addr = (Cell *)DOES_CODE(xt);
 1421: 
 1422: code-address!		c_addr xt --		gforth	code_address_store
 1423: ""Creates a code field with code address c_addr at xt""
 1424: MAKE_CF(xt, c_addr);
 1425: CACHE_FLUSH(xt,PFA(0));
 1426: 
 1427: does-code!	a_addr xt --		gforth	does_code_store
 1428: ""creates a code field at xt for a defining-word-defined word; a_addr
 1429: is the start of the Forth code after DOES>""
 1430: MAKE_DOES_CF(xt, a_addr);
 1431: CACHE_FLUSH(xt,PFA(0));
 1432: 
 1433: does-handler!	a_addr --	gforth	does_handler_store
 1434: ""creates a DOES>-handler at address a_addr. a_addr usually points
 1435: just behind a DOES>.""
 1436: MAKE_DOES_HANDLER(a_addr);
 1437: CACHE_FLUSH(a_addr,DOES_HANDLER_SIZE);
 1438: 
 1439: /does-handler	-- n	gforth	slash_does_handler
 1440: ""the size of a does-handler (includes possible padding)""
 1441: /* !! a constant or environmental query might be better */
 1442: n = DOES_HANDLER_SIZE;
 1443: 
 1444: flush-icache	c_addr u --	gforth	flush_icache
 1445: ""Make sure that the instruction cache of the processor (if there is
 1446: one) does not contain stale data at @var{c_addr} and @var{u} bytes
 1447: afterwards. @code{END-CODE} performs a @code{flush-icache}
 1448: automatically. Caveat: @code{flush-icache} might not work on your
 1449: installation; this is usually the case if direct threading is not
 1450: supported on your machine (take a look at your @file{machine.h}) and
 1451: your machine has a separate instruction cache. In such cases,
 1452: @code{flush-icache} does nothing instead of flushing the instruction
 1453: cache.""
 1454: FLUSH_ICACHE(c_addr,u);
 1455: 
 1456: toupper	c1 -- c2	gforth
 1457: c2 = toupper(c1);
 1458: 
 1459: \ local variable implementation primitives
 1460: @local#		-- w	gforth	fetch_local_number
 1461: w = *(Cell *)(lp+(Cell)NEXT_INST);
 1462: INC_IP(1);
 1463: 
 1464: @local0	-- w	new	fetch_local_zero
 1465: w = *(Cell *)(lp+0*sizeof(Cell));
 1466: 
 1467: @local1	-- w	new	fetch_local_four
 1468: w = *(Cell *)(lp+1*sizeof(Cell));
 1469: 
 1470: @local2	-- w	new	fetch_local_eight
 1471: w = *(Cell *)(lp+2*sizeof(Cell));
 1472: 
 1473: @local3	-- w	new	fetch_local_twelve
 1474: w = *(Cell *)(lp+3*sizeof(Cell));
 1475: 
 1476: f@local#	-- r	gforth	f_fetch_local_number
 1477: r = *(Float *)(lp+(Cell)NEXT_INST);
 1478: INC_IP(1);
 1479: 
 1480: f@local0	-- r	new	f_fetch_local_zero
 1481: r = *(Float *)(lp+0*sizeof(Float));
 1482: 
 1483: f@local1	-- r	new	f_fetch_local_eight
 1484: r = *(Float *)(lp+1*sizeof(Float));
 1485: 
 1486: laddr#		-- c_addr	gforth	laddr_number
 1487: /* this can also be used to implement lp@ */
 1488: c_addr = (Char *)(lp+(Cell)NEXT_INST);
 1489: INC_IP(1);
 1490: 
 1491: lp+!#	--	gforth	lp_plus_store_number
 1492: ""used with negative immediate values it allocates memory on the
 1493: local stack, a positive immediate argument drops memory from the local
 1494: stack""
 1495: lp += (Cell)NEXT_INST;
 1496: INC_IP(1);
 1497: 
 1498: lp-	--	new	minus_four_lp_plus_store
 1499: lp += -sizeof(Cell);
 1500: 
 1501: lp+	--	new	eight_lp_plus_store
 1502: lp += sizeof(Float);
 1503: 
 1504: lp+2	--	new	sixteen_lp_plus_store
 1505: lp += 2*sizeof(Float);
 1506: 
 1507: lp!	c_addr --	gforth	lp_store
 1508: lp = (Address)c_addr;
 1509: 
 1510: >l	w --	gforth	to_l
 1511: lp -= sizeof(Cell);
 1512: *(Cell *)lp = w;
 1513: 
 1514: f>l	r --	gforth	f_to_l
 1515: lp -= sizeof(Float);
 1516: *(Float *)lp = r;
 1517: 
 1518: up!	a_addr --	gforth	up_store
 1519: up0=up=(char *)a_addr;
 1520: 
 1521: call-c	w --	gforth	call_c
 1522: ""Call the C function pointed to by @i{w}. The C function has to
 1523: access the stack itself. The stack pointers are exported in the gloabl
 1524: variables @code{SP} and @code{FP}.""
 1525: /* This is a first attempt at support for calls to C. This may change in
 1526:    the future */
 1527: IF_FTOS(fp[0]=FTOS);
 1528: FP=fp;
 1529: SP=sp;
 1530: ((void (*)())w)();
 1531: sp=SP;
 1532: fp=FP;
 1533: IF_TOS(TOS=sp[0]);
 1534: IF_FTOS(FTOS=fp[0]);
 1535: 
 1536: strerror	n -- c_addr u	gforth
 1537: c_addr = strerror(n);
 1538: u = strlen(c_addr);
 1539: 
 1540: strsignal	n -- c_addr u	gforth
 1541: c_addr = strsignal(n);
 1542: u = strlen(c_addr);

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