File:  [gforth] / gforth / prim
Revision 1.17: download - view: text, annotated - select for diffs
Fri Dec 11 22:54:27 1998 UTC (25 years, 3 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added further options to shrink a kernel down
Cleaned up conditional primitives (works now for C-generated part, too)
Cleaned up mach files for embedded architectures
Cleaned up options in the kernel

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

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