File:  [gforth] / gforth / Attic / primitives
Revision 1.51: download - view: text, annotated - select for diffs
Fri Feb 9 17:34:11 1996 UTC (28 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
?DUP-IF and ?DUP-0=-IF are now supported by primitives
added primitives EMIT-FILE, STDOUT, STDERR
EMIT and TYPE now work through file words
added some code for the BUGGY_LONG_LONG case (not yet complete)
eliminated D! and D@
made DMIN, DMAX, DABS high-level
added compat/control.fs (?DUP-IF etc.)

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

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