File:  [gforth] / gforth / Attic / primitives
Revision 1.59: download - view: text, annotated - select for diffs
Tue Sep 10 16:08:39 1996 UTC (27 years, 6 months ago) by anton
Branches: MAIN
CVS tags: v0-2-1, v0-2-0, HEAD
fixed bugs in code.fs
added primitive threading-method
fixed create-interpret/compile such that "' word >body" works as expected
documented some defining words

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

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