File:  [gforth] / gforth / prim
Revision 1.247: download - view: text, annotated - select for diffs
Sat Apr 17 21:32:49 2010 UTC (13 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
changed abi-code to use a signature that leads to nicer code

    1: \ Gforth primitives
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009 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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20: 
   21: \ WARNING: This file is processed by m4. Make sure your identifiers
   22: \ don't collide with m4's (e.g. by undefining them).
   23: \ 
   24: \ 
   25: \ 
   26: \ This file contains primitive specifications in the following format:
   27: \ 
   28: \ forth name	( stack effect )	category	[pronunciation]
   29: \ [""glossary entry""]
   30: \ C code
   31: \ [:
   32: \ Forth code]
   33: \ 
   34: \ Note: Fields in brackets are optional.  Word specifications have to
   35: \ be separated by at least one empty line
   36: \
   37: \ Both pronounciation and stack items (in the stack effect) must
   38: \ conform to the C identifier syntax or the C compiler will complain.
   39: \ If you don't have a pronounciation field, the Forth name is used,
   40: \ and has to conform to the C identifier syntax.
   41: \ 
   42: \ These specifications are automatically translated into C-code for the
   43: \ interpreter and into some other files. I hope that your C compiler has
   44: \ decent optimization, otherwise the automatically generated code will
   45: \ be somewhat slow. The Forth version of the code is included for manual
   46: \ compilers, so they will need to compile only the important words.
   47: \ 
   48: \ Note that stack pointer adjustment is performed according to stack
   49: \ effect by automatically generated code and NEXT is automatically
   50: \ appended to the C code. Also, you can use the names in the stack
   51: \ effect in the C code. Stack access is automatic. One exception: if
   52: \ your code does not fall through, the results are not stored into the
   53: \ stack. Use different names on both sides of the '--', if you change a
   54: \ value (some stores to the stack are optimized away).
   55: \
   56: \ For superinstructions the syntax is:
   57: \
   58: \ forth-name [/ c-name] = forth-name forth-name ...
   59: \
   60: \ 
   61: \ The stack variables have the following types:
   62: \ 
   63: \ name matches	type
   64: \ f.*		Bool
   65: \ c.*		Char
   66: \ [nw].*	Cell
   67: \ u.*		UCell
   68: \ d.*		DCell
   69: \ ud.*		UDCell
   70: \ r.*		Float
   71: \ a_.*		Cell *
   72: \ c_.*		Char *
   73: \ f_.*		Float *
   74: \ df_.*		DFloat *
   75: \ sf_.*		SFloat *
   76: \ xt.*		XT
   77: \ f83name.*	F83Name *
   78: 
   79: \E stack data-stack   sp Cell
   80: \E stack fp-stack     fp Float
   81: \E stack return-stack rp Cell
   82: \E
   83: \E get-current prefixes set-current
   84: \E 
   85: \E s" Bool"		single data-stack type-prefix f
   86: \E s" Char"		single data-stack type-prefix c
   87: \E s" Cell"		single data-stack type-prefix n
   88: \E s" Cell"		single data-stack type-prefix w
   89: \E s" UCell"		single data-stack type-prefix u
   90: \E s" DCell"		double data-stack type-prefix d
   91: \E s" UDCell"		double data-stack type-prefix ud
   92: \E s" Float"		single fp-stack   type-prefix r
   93: \E s" Cell *"		single data-stack type-prefix a_
   94: \E s" Char *"		single data-stack type-prefix c_
   95: \E s" Float *"		single data-stack type-prefix f_
   96: \E s" DFloat *"		single data-stack type-prefix df_
   97: \E s" SFloat *"		single data-stack type-prefix sf_
   98: \E s" Xt"		single data-stack type-prefix xt
   99: \E s" struct F83Name *"	single data-stack type-prefix f83name
  100: \E s" struct Longname *" single data-stack type-prefix longname
  101: \E 
  102: \E data-stack   stack-prefix S:
  103: \E fp-stack     stack-prefix F:
  104: \E return-stack stack-prefix R:
  105: \E inst-stream  stack-prefix #
  106: \E 
  107: \E set-current
  108: \E store-optimization on
  109: \E ' noop tail-nextp2 ! \ now INST_TAIL just stores, but does not jump
  110: \E
  111: \E `include-skipped-insts' on \ static superinsts include cells for components
  112: \E                            \ useful for dynamic programming and
  113: \E                            \ superinsts across entry points
  114: 
  115: \ 
  116: \ 
  117: \ 
  118: \ In addition the following names can be used:
  119: \ ip	the instruction pointer
  120: \ sp	the data stack pointer
  121: \ rp	the parameter stack pointer
  122: \ lp	the locals stack pointer
  123: \ NEXT	executes NEXT
  124: \ cfa	
  125: \ NEXT1	executes NEXT1
  126: \ FLAG(x)	makes a Forth flag from a C flag
  127: \ 
  128: \ 
  129: \ 
  130: \ Percentages in comments are from Koopmans book: average/maximum use
  131: \ (taken from four, not very representative benchmarks)
  132: \ 
  133: \ 
  134: \ 
  135: \ To do:
  136: \ 
  137: \ throw execute, cfa and NEXT1 out?
  138: \ macroize *ip, ip++, *ip++ (pipelining)?
  139: 
  140: \ Stack caching setup
  141: 
  142: ifdef(`STACK_CACHE_FILE', `include(STACK_CACHE_FILE)', `include(cache0.vmg)')
  143: 
  144: \ these m4 macros would collide with identifiers
  145: undefine(`index')
  146: undefine(`shift')
  147: undefine(`symbols')
  148: 
  149: \F 0 [if]
  150: 
  151: \ run-time routines for non-primitives.  They are defined as
  152: \ primitives, because that simplifies things.
  153: 
  154: (docol)	( -- R:a_retaddr )	gforth-internal	paren_docol
  155: ""run-time routine for colon definitions""
  156: #ifdef NO_IP
  157: a_retaddr = next_code;
  158: INST_TAIL;
  159: goto **(Label *)PFA(CFA);
  160: #else /* !defined(NO_IP) */
  161: a_retaddr = (Cell *)IP;
  162: SET_IP((Xt *)PFA(CFA));
  163: #endif /* !defined(NO_IP) */
  164: 
  165: (docon) ( -- w )	gforth-internal	paren_docon
  166: ""run-time routine for constants""
  167: w = *(Cell *)PFA(CFA);
  168: #ifdef NO_IP
  169: INST_TAIL;
  170: goto *next_code;
  171: #endif /* defined(NO_IP) */
  172: 
  173: (dovar) ( -- a_body )	gforth-internal	paren_dovar
  174: ""run-time routine for variables and CREATEd words""
  175: a_body = PFA(CFA);
  176: #ifdef NO_IP
  177: INST_TAIL;
  178: goto *next_code;
  179: #endif /* defined(NO_IP) */
  180: 
  181: (douser) ( -- a_user )	gforth-internal	paren_douser
  182: ""run-time routine for constants""
  183: a_user = (Cell *)(up+*(Cell *)PFA(CFA));
  184: #ifdef NO_IP
  185: INST_TAIL;
  186: goto *next_code;
  187: #endif /* defined(NO_IP) */
  188: 
  189: (dodefer) ( -- )	gforth-internal	paren_dodefer
  190: ""run-time routine for deferred words""
  191: #ifndef NO_IP
  192: ip=IP; /* undo any ip updating that may have been performed by NEXT_P0 */
  193: #endif /* !defined(NO_IP) */
  194: SUPER_END; /* !! probably unnecessary and may lead to measurement errors */
  195: VM_JUMP(EXEC1(*(Xt *)PFA(CFA)));
  196: 
  197: (dofield) ( n1 -- n2 )	gforth-internal	paren_field
  198: ""run-time routine for fields""
  199: n2 = n1 + *(Cell *)PFA(CFA);
  200: #ifdef NO_IP
  201: INST_TAIL;
  202: goto *next_code;
  203: #endif /* defined(NO_IP) */
  204: 
  205: (dovalue) ( -- w )	gforth-internal	paren_doval
  206: ""run-time routine for constants""
  207: w = *(Cell *)PFA(CFA);
  208: #ifdef NO_IP
  209: INST_TAIL;
  210: goto *next_code;
  211: #endif /* defined(NO_IP) */
  212: 
  213: (dodoes) ( -- a_body R:a_retaddr )	gforth-internal	paren_dodoes
  214: ""run-time routine for @code{does>}-defined words""
  215: #ifdef NO_IP
  216: a_retaddr = next_code;
  217: a_body = PFA(CFA);
  218: INST_TAIL;
  219: #ifdef DEBUG
  220: fprintf(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body);
  221: #endif
  222: goto **(Label *)DOES_CODE1(CFA);
  223: #else /* !defined(NO_IP) */
  224: a_retaddr = (Cell *)IP;
  225: a_body = PFA(CFA);
  226: #ifdef DEBUG
  227: fprintf(stderr, "dodoes to %x, push %x\n", a_retaddr, a_body);
  228: #endif
  229: SET_IP(DOES_CODE1(CFA));
  230: #endif /* !defined(NO_IP) */
  231: 
  232: (does-handler) ( -- )	gforth-internal	paren_does_handler
  233: ""just a slot to have an encoding for the DOESJUMP, 
  234: which is no longer used anyway (!! eliminate this)""
  235: 
  236: (doabicode) ( ... -- ...)	gforth-internal	paren_doabicode
  237: ""run-time routine for ABI-CODE definitions""
  238: abifunc *f = (abifunc *)PFA(CFA);
  239: Float *fp_mem = fp;
  240: sp = (*f)(sp, &fp_mem);
  241: fp = fp_mem;
  242: #ifdef NO_IP
  243: INST_TAIL;
  244: goto *next_code;
  245: #endif /* defined(NO_IP) */
  246: 
  247: \F [endif]
  248: 
  249: \g control
  250: 
  251: noop	( -- )		gforth
  252: :
  253:  ;
  254: 
  255: call	( #a_callee -- R:a_retaddr )	new
  256: ""Call callee (a variant of docol with inline argument).""
  257: #ifdef NO_IP
  258: assert(0);
  259: INST_TAIL;
  260: JUMP(a_callee);
  261: #else
  262: #ifdef DEBUG
  263:     {
  264:       CFA_TO_NAME((((Cell *)a_callee)-2));
  265:       fprintf(stderr,"%08lx: call %08lx %.*s\n",(Cell)ip,(Cell)a_callee,
  266: 	      len,name);
  267:     }
  268: #endif
  269: a_retaddr = (Cell *)IP;
  270: SET_IP((Xt *)a_callee);
  271: #endif
  272: 
  273: execute	( xt -- )		core
  274: ""Perform the semantics represented by the execution token, @i{xt}.""
  275: #ifdef DEBUG
  276: fprintf(stderr, "execute %08x\n", xt);
  277: #endif
  278: #ifndef NO_IP
  279: ip=IP;
  280: #endif
  281: SUPER_END;
  282: VM_JUMP(EXEC1(xt));
  283: 
  284: perform	( a_addr -- )	gforth
  285: ""@code{@@ execute}.""
  286: /* and pfe */
  287: #ifndef NO_IP
  288: ip=IP;
  289: #endif
  290: SUPER_END;
  291: VM_JUMP(EXEC1(*(Xt *)a_addr));
  292: :
  293:  @ execute ;
  294: 
  295: ;s	( R:w -- )		gforth	semis
  296: ""The primitive compiled by @code{EXIT}.""
  297: #ifdef NO_IP
  298: INST_TAIL;
  299: goto *(void *)w;
  300: #else
  301: SET_IP((Xt *)w);
  302: #endif
  303: 
  304: unloop	( R:w1 R:w2 -- )	core
  305: /* !! alias for 2rdrop */
  306: :
  307:  r> rdrop rdrop >r ;
  308: 
  309: lit-perform	( #a_addr -- )	new	lit_perform
  310: #ifndef NO_IP
  311: ip=IP;
  312: #endif
  313: SUPER_END;
  314: VM_JUMP(EXEC1(*(Xt *)a_addr));
  315: 
  316: does-exec ( #a_cfa -- R:nest a_pfa )	new	does_exec
  317: #ifdef NO_IP
  318: /* compiled to LIT CALL by compile_prim */
  319: assert(0);
  320: #else
  321: a_pfa = PFA(a_cfa);
  322: nest = (Cell)IP;
  323: #ifdef DEBUG
  324:     {
  325:       CFA_TO_NAME(a_cfa);
  326:       fprintf(stderr,"%08lx: does %08lx %.*s\n",
  327: 	      (Cell)ip,(Cell)a_cfa,len,name);
  328:     }
  329: #endif
  330: SET_IP(DOES_CODE1(a_cfa));
  331: #endif
  332: 
  333: \+glocals
  334: 
  335: branch-lp+!# ( #a_target #nlocals -- )	gforth	branch_lp_plus_store_number
  336: /* this will probably not be used */
  337: lp += nlocals;
  338: #ifdef NO_IP
  339: INST_TAIL;
  340: JUMP(a_target);
  341: #else
  342: SET_IP((Xt *)a_target);
  343: #endif
  344: 
  345: \+
  346: 
  347: branch	( #a_target -- )	gforth
  348: #ifdef NO_IP
  349: INST_TAIL;
  350: JUMP(a_target);
  351: #else
  352: SET_IP((Xt *)a_target);
  353: #endif
  354: :
  355:  r> @ >r ;
  356: 
  357: \ condbranch(forthname,stackeffect,restline,code1,code2,forthcode)
  358: \ this is non-syntactical: code must open a brace that is closed by the macro
  359: define(condbranch,
  360: $1 ( `#'a_target $2 ) $3
  361: $4	#ifdef NO_IP
  362: INST_TAIL;
  363: #endif
  364: $5	#ifdef NO_IP
  365: JUMP(a_target);
  366: #else
  367: SET_IP((Xt *)a_target);
  368: ifelse(condbranch_opt,`1',`INST_TAIL; NEXT_P2;',`/* condbranch_opt=0 */')
  369: #endif
  370: }
  371: ifelse(condbranch_opt,`1',`SUPER_CONTINUE;',`/* condbranch_opt=0 */')
  372: $6
  373: 
  374: \+glocals
  375: 
  376: $1-lp+!`#' ( `#'a_target `#'nlocals $2 ) $3_lp_plus_store_number
  377: $4	#ifdef NO_IP
  378: INST_TAIL;
  379: #endif
  380: $5	lp += nlocals;
  381: #ifdef NO_IP
  382: JUMP(a_target);
  383: #else
  384: SET_IP((Xt *)a_target);
  385: ifelse(condbranch_opt,`1',`INST_TAIL; NEXT_P2;',`/* condbranch_opt=0 */')
  386: #endif
  387: }
  388: ifelse(condbranch_opt,`1',`SUPER_CONTINUE;',`/* condbranch_opt=0 */')
  389: 
  390: \+
  391: )
  392: 
  393: condbranch(?branch,f --,f83	question_branch,
  394: ,if (f==0) {
  395: ,:
  396:  0= dup 0=          \ !f f
  397:  r> tuck cell+      \ !f branchoffset f IP+
  398:  and -rot @ and or  \ f&IP+|!f&branch
  399:  >r ;)
  400: 
  401: \ we don't need an lp_plus_store version of the ?dup-stuff, because it
  402: \ is only used in if's (yet)
  403: 
  404: \+xconds
  405: 
  406: ?dup-?branch	( #a_target f -- S:... )	new	question_dupe_question_branch
  407: ""The run-time procedure compiled by @code{?DUP-IF}.""
  408: if (f==0) {
  409: #ifdef NO_IP
  410: INST_TAIL;
  411: JUMP(a_target);
  412: #else
  413: SET_IP((Xt *)a_target);
  414: #endif
  415: } else {
  416: sp--;
  417: sp[0]=f;
  418: }
  419: 
  420: ?dup-0=-?branch ( #a_target f -- S:... ) new	question_dupe_zero_equals_question_branch
  421: ""The run-time procedure compiled by @code{?DUP-0=-IF}.""
  422: if (f!=0) {
  423:   sp--;
  424:   sp[0]=f;
  425: #ifdef NO_IP
  426:   JUMP(a_target);
  427: #else
  428:   SET_IP((Xt *)a_target);
  429: #endif
  430: }
  431: 
  432: \+
  433: \fhas? skiploopprims 0= [IF]
  434: 
  435: condbranch((next),R:n1 -- R:n2,cmFORTH	paren_next,
  436: n2=n1-1;
  437: ,if (n1) {
  438: ,:
  439:  r> r> dup 1- >r
  440:  IF @ >r ELSE cell+ >r THEN ;)
  441: 
  442: condbranch((loop),R:nlimit R:n1 -- R:nlimit R:n2,gforth	paren_loop,
  443: n2=n1+1;
  444: ,if (n2 != nlimit) {
  445: ,:
  446:  r> r> 1+ r> 2dup =
  447:  IF >r 1- >r cell+ >r
  448:  ELSE >r >r @ >r THEN ;)
  449: 
  450: condbranch((+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_plus_loop,
  451: /* !! check this thoroughly */
  452: /* sign bit manipulation and test: (x^y)<0 is equivalent to (x<0) != (y<0) */
  453: /* dependent upon two's complement arithmetic */
  454: Cell olddiff = n1-nlimit;
  455: n2=n1+n;	
  456: ,if (((olddiff^(olddiff+n))    /* the limit is not crossed */
  457:      &(olddiff^n))	       /* OR it is a wrap-around effect */
  458:     >=0) { /* & is used to avoid having two branches for gforth-native */
  459: ,:
  460:  r> swap
  461:  r> r> 2dup - >r
  462:  2 pick r@ + r@ xor 0< 0=
  463:  3 pick r> xor 0< 0= or
  464:  IF    >r + >r @ >r
  465:  ELSE  >r >r drop cell+ >r THEN ;)
  466: 
  467: \+xconds
  468: 
  469: condbranch((-loop),u R:nlimit R:n1 -- R:nlimit R:n2,gforth paren_minus_loop,
  470: UCell olddiff = n1-nlimit;
  471: n2=n1-u;
  472: ,if (olddiff>u) {
  473: ,)
  474: 
  475: condbranch((s+loop),n R:nlimit R:n1 -- R:nlimit R:n2,gforth	paren_symmetric_plus_loop,
  476: ""The run-time procedure compiled by S+LOOP. It loops until the index
  477: crosses the boundary between limit and limit-sign(n). I.e. a symmetric
  478: version of (+LOOP).""
  479: /* !! check this thoroughly */
  480: Cell diff = n1-nlimit;
  481: Cell newdiff = diff+n;
  482: if (n<0) {
  483:     diff = -diff;
  484:     newdiff = -newdiff;
  485: }
  486: n2=n1+n;
  487: ,if (((~diff)|newdiff)<0) { /* use | to avoid two branches for gforth-native */
  488: ,)
  489: 
  490: \+
  491: 
  492: (for)   ( ncount -- R:nlimit R:ncount )         cmFORTH         paren_for
  493: /* or (for) = >r -- collides with unloop! */
  494: nlimit=0;
  495: :
  496:  r> swap 0 >r >r >r ;
  497: 
  498: (do)    ( nlimit nstart -- R:nlimit R:nstart )  gforth          paren_do
  499: :
  500:  r> swap rot >r >r >r ;
  501: 
  502: (?do) ( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth	paren_question_do
  503: #ifdef NO_IP
  504:     INST_TAIL;
  505: #endif
  506: if (nstart == nlimit) {
  507: #ifdef NO_IP
  508:     JUMP(a_target);
  509: #else
  510:     SET_IP((Xt *)a_target);
  511: #endif
  512: }
  513: :
  514:   2dup =
  515:   IF   r> swap rot >r >r
  516:        @ >r
  517:   ELSE r> swap rot >r >r
  518:        cell+ >r
  519:   THEN ;				\ --> CORE-EXT
  520: 
  521: \+xconds
  522: 
  523: (+do)	( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth	paren_plus_do
  524: #ifdef NO_IP
  525:     INST_TAIL;
  526: #endif
  527: if (nstart >= nlimit) {
  528: #ifdef NO_IP
  529:     JUMP(a_target);
  530: #else
  531:     SET_IP((Xt *)a_target);
  532: #endif
  533: }
  534: :
  535:  swap 2dup
  536:  r> swap >r swap >r
  537:  >=
  538:  IF
  539:      @
  540:  ELSE
  541:      cell+
  542:  THEN  >r ;
  543: 
  544: (u+do)	( #a_target ulimit ustart -- R:ulimit R:ustart ) gforth	paren_u_plus_do
  545: #ifdef NO_IP
  546:     INST_TAIL;
  547: #endif
  548: if (ustart >= ulimit) {
  549: #ifdef NO_IP
  550: JUMP(a_target);
  551: #else
  552: SET_IP((Xt *)a_target);
  553: #endif
  554: }
  555: :
  556:  swap 2dup
  557:  r> swap >r swap >r
  558:  u>=
  559:  IF
  560:      @
  561:  ELSE
  562:      cell+
  563:  THEN  >r ;
  564: 
  565: (-do)	( #a_target nlimit nstart -- R:nlimit R:nstart ) gforth	paren_minus_do
  566: #ifdef NO_IP
  567:     INST_TAIL;
  568: #endif
  569: if (nstart <= nlimit) {
  570: #ifdef NO_IP
  571: JUMP(a_target);
  572: #else
  573: SET_IP((Xt *)a_target);
  574: #endif
  575: }
  576: :
  577:  swap 2dup
  578:  r> swap >r swap >r
  579:  <=
  580:  IF
  581:      @
  582:  ELSE
  583:      cell+
  584:  THEN  >r ;
  585: 
  586: (u-do)	( #a_target ulimit ustart -- R:ulimit R:ustart ) gforth	paren_u_minus_do
  587: #ifdef NO_IP
  588:     INST_TAIL;
  589: #endif
  590: if (ustart <= ulimit) {
  591: #ifdef NO_IP
  592: JUMP(a_target);
  593: #else
  594: SET_IP((Xt *)a_target);
  595: #endif
  596: }
  597: :
  598:  swap 2dup
  599:  r> swap >r swap >r
  600:  u<=
  601:  IF
  602:      @
  603:  ELSE
  604:      cell+
  605:  THEN  >r ;
  606: 
  607: \+
  608: 
  609: \ don't make any assumptions where the return stack is!!
  610: \ implement this in machine code if it should run quickly!
  611: 
  612: i	( R:n -- R:n n )		core
  613: :
  614: \ rp@ cell+ @ ;
  615:   r> r> tuck >r >r ;
  616: 
  617: i'	( R:w R:w2 -- R:w R:w2 w )		gforth		i_tick
  618: :
  619: \ rp@ cell+ cell+ @ ;
  620:   r> r> r> dup itmp ! >r >r >r itmp @ ;
  621: variable itmp
  622: 
  623: j	( R:w R:w1 R:w2 -- w R:w R:w1 R:w2 )	core
  624: :
  625: \ rp@ cell+ cell+ cell+ @ ;
  626:   r> r> r> r> dup itmp ! >r >r >r >r itmp @ ;
  627: [IFUNDEF] itmp variable itmp [THEN]
  628: 
  629: k	( R:w R:w1 R:w2 R:w3 R:w4 -- w R:w R:w1 R:w2 R:w3 R:w4 )	gforth
  630: :
  631: \ rp@ [ 5 cells ] Literal + @ ;
  632:   r> r> r> r> r> r> dup itmp ! >r >r >r >r >r >r itmp @ ;
  633: [IFUNDEF] itmp variable itmp [THEN]
  634: 
  635: \f[THEN]
  636: 
  637: \ digit is high-level: 0/0%
  638: 
  639: abi-call	( #a_callee ... -- ... )	gforth	abi_call
  640: abifunc *f = (abifunc *)a_callee;
  641: Float *fp_mem = fp;
  642: sp = (*f)(sp, &fp_mem);
  643: fp = fp_mem;
  644: 
  645: \g strings
  646: 
  647: move	( c_from c_to ucount -- )		core
  648: ""Copy the contents of @i{ucount} aus at @i{c-from} to
  649: @i{c-to}. @code{move} works correctly even if the two areas overlap.""
  650: /* !! note that the standard specifies addr, not c-addr */
  651: memmove(c_to,c_from,ucount);
  652: /* make an Ifdef for bsd and others? */
  653: :
  654:  >r 2dup u< IF r> cmove> ELSE r> cmove THEN ;
  655: 
  656: cmove	( c_from c_to u -- )	string	c_move
  657: ""Copy the contents of @i{ucount} characters from data space at
  658: @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
  659: from low address to high address; i.e., for overlapping areas it is
  660: safe if @i{c-to}=<@i{c-from}.""
  661: cmove(c_from,c_to,u);
  662: :
  663:  bounds ?DO  dup c@ I c! 1+  LOOP  drop ;
  664: 
  665: cmove>	( c_from c_to u -- )	string	c_move_up
  666: ""Copy the contents of @i{ucount} characters from data space at
  667: @i{c-from} to @i{c-to}. The copy proceeds @code{char}-by-@code{char}
  668: from high address to low address; i.e., for overlapping areas it is
  669: safe if @i{c-to}>=@i{c-from}.""
  670: cmove_up(c_from,c_to,u);
  671: :
  672:  dup 0= IF  drop 2drop exit  THEN
  673:  rot over + -rot bounds swap 1-
  674:  DO  1- dup c@ I c!  -1 +LOOP  drop ;
  675: 
  676: fill	( c_addr u c -- )	core
  677: ""Store @i{c} in @i{u} chars starting at @i{c-addr}.""
  678: memset(c_addr,c,u);
  679: :
  680:  -rot bounds
  681:  ?DO  dup I c!  LOOP  drop ;
  682: 
  683: compare	( c_addr1 u1 c_addr2 u2 -- n )	string
  684: ""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
  685: the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
  686: is 1. Currently this is based on the machine's character
  687: comparison. In the future, this may change to consider the current
  688: locale and its collation order.""
  689: /* close ' to keep fontify happy */ 
  690: n = compare(c_addr1, u1, c_addr2, u2);
  691: :
  692:  rot 2dup swap - >r min swap -text dup
  693:  IF  rdrop  ELSE  drop r> sgn  THEN ;
  694: : -text ( c_addr1 u c_addr2 -- n )
  695:  swap bounds
  696:  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
  697:  ELSE  c@ I c@ - unloop  THEN  sgn ;
  698: : sgn ( n -- -1/0/1 )
  699:  dup 0= IF EXIT THEN  0< 2* 1+ ;
  700: 
  701: \ -text is only used by replaced primitives now; move it elsewhere
  702: \ -text	( c_addr1 u c_addr2 -- n )	new	dash_text
  703: \ n = memcmp(c_addr1, c_addr2, u);
  704: \ if (n<0)
  705: \   n = -1;
  706: \ else if (n>0)
  707: \   n = 1;
  708: \ :
  709: \  swap bounds
  710: \  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
  711: \  ELSE  c@ I c@ - unloop  THEN  sgn ;
  712: \ : sgn ( n -- -1/0/1 )
  713: \  dup 0= IF EXIT THEN  0< 2* 1+ ;
  714: 
  715: toupper	( c1 -- c2 )	gforth
  716: ""If @i{c1} is a lower-case character (in the current locale), @i{c2}
  717: is the equivalent upper-case character. All other characters are unchanged.""
  718: c2 = toupper(c1);
  719: :
  720:  dup [char] a - [ char z char a - 1 + ] Literal u<  bl and - ;
  721: 
  722: capscompare	( c_addr1 u1 c_addr2 u2 -- n )	gforth
  723: ""Compare two strings lexicographically. If they are equal, @i{n} is 0; if
  724: the first string is smaller, @i{n} is -1; if the first string is larger, @i{n}
  725: is 1. Currently this is based on the machine's character
  726: comparison. In the future, this may change to consider the current
  727: locale and its collation order.""
  728: /* close ' to keep fontify happy */ 
  729: n = capscompare(c_addr1, u1, c_addr2, u2);
  730: 
  731: /string	( c_addr1 u1 n -- c_addr2 u2 )	string	slash_string
  732: ""Adjust the string specified by @i{c-addr1, u1} to remove @i{n}
  733: characters from the start of the string.""
  734: c_addr2 = c_addr1+n;
  735: u2 = u1-n;
  736: :
  737:  tuck - >r + r> dup 0< IF  - 0  THEN ;
  738: 
  739: \g arith
  740: 
  741: lit	( #w -- w )		gforth
  742: :
  743:  r> dup @ swap cell+ >r ;
  744: 
  745: +	( n1 n2 -- n )		core	plus
  746: n = n1+n2;
  747: 
  748: \ lit+ / lit_plus = lit +
  749: 
  750: lit+	( n1 #n2 -- n )		new	lit_plus
  751: #ifdef DEBUG
  752: fprintf(stderr, "lit+ %08x\n", n2);
  753: #endif
  754: n=n1+n2;
  755: 
  756: \ PFE-0.9.14 has it differently, but the next release will have it as follows
  757: under+	( n1 n2 n3 -- n n2 )	gforth	under_plus
  758: ""add @i{n3} to @i{n1} (giving @i{n})""
  759: n = n1+n3;
  760: :
  761:  rot + swap ;
  762: 
  763: -	( n1 n2 -- n )		core	minus
  764: n = n1-n2;
  765: :
  766:  negate + ;
  767: 
  768: negate	( n1 -- n2 )		core
  769: /* use minus as alias */
  770: n2 = -n1;
  771: :
  772:  invert 1+ ;
  773: 
  774: 1+	( n1 -- n2 )		core		one_plus
  775: n2 = n1+1;
  776: :
  777:  1 + ;
  778: 
  779: 1-	( n1 -- n2 )		core		one_minus
  780: n2 = n1-1;
  781: :
  782:  1 - ;
  783: 
  784: max	( n1 n2 -- n )	core
  785: if (n1<n2)
  786:   n = n2;
  787: else
  788:   n = n1;
  789: :
  790:  2dup < IF swap THEN drop ;
  791: 
  792: min	( n1 n2 -- n )	core
  793: if (n1<n2)
  794:   n = n1;
  795: else
  796:   n = n2;
  797: :
  798:  2dup > IF swap THEN drop ;
  799: 
  800: abs	( n -- u )	core
  801: if (n<0)
  802:   u = -n;
  803: else
  804:   u = n;
  805: :
  806:  dup 0< IF negate THEN ;
  807: 
  808: *	( n1 n2 -- n )		core	star
  809: n = n1*n2;
  810: :
  811:  um* drop ;
  812: 
  813: /	( n1 n2 -- n )		core	slash
  814: n = n1/n2;
  815: if (CHECK_DIVISION_SW && n2 == 0)
  816:   throw(BALL_DIVZERO);
  817: if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN)
  818:   throw(BALL_RESULTRANGE);
  819: if (FLOORED_DIV && ((n1^n2) < 0) && (n1%n2 != 0))
  820:   n--;
  821: :
  822:  /mod nip ;
  823: 
  824: mod	( n1 n2 -- n )		core
  825: n = n1%n2;
  826: if (CHECK_DIVISION_SW && n2 == 0)
  827:   throw(BALL_DIVZERO);
  828: if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN)
  829:   throw(BALL_RESULTRANGE);
  830: if(FLOORED_DIV && ((n1^n2) < 0) && n!=0) n += n2;
  831: :
  832:  /mod drop ;
  833: 
  834: /mod	( n1 n2 -- n3 n4 )		core		slash_mod
  835: n4 = n1/n2;
  836: n3 = n1%n2; /* !! is this correct? look into C standard! */
  837: if (CHECK_DIVISION_SW && n2 == 0)
  838:   throw(BALL_DIVZERO);
  839: if (CHECK_DIVISION_SW && n2 == -1 && n1 == CELL_MIN)
  840:   throw(BALL_RESULTRANGE);
  841: if (FLOORED_DIV && ((n1^n2) < 0) && n3!=0) {
  842:   n4--;
  843:   n3+=n2;
  844: }
  845: :
  846:  >r s>d r> fm/mod ;
  847: 
  848: */mod	( n1 n2 n3 -- n4 n5 )	core	star_slash_mod
  849: ""n1*n2=n3*n5+n4, with the intermediate result (n1*n2) being double.""
  850: #ifdef BUGGY_LL_MUL
  851: DCell d = mmul(n1,n2);
  852: #else
  853: DCell d = (DCell)n1 * (DCell)n2;
  854: #endif
  855: #ifdef ASM_SM_SLASH_REM
  856: ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, n4, n5);
  857: if (FLOORED_DIV && ((DHI(d)^n3)<0) && n4!=0) {
  858:   if (CHECK_DIVISION && n5 == CELL_MIN)
  859:     throw(BALL_RESULTRANGE);
  860:   n5--;
  861:   n4+=n3;
  862: }
  863: #else
  864: DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3);
  865: n4=DHI(r);
  866: n5=DLO(r);
  867: #endif
  868: :
  869:  >r m* r> fm/mod ;
  870: 
  871: */	( n1 n2 n3 -- n4 )	core	star_slash
  872: ""n4=(n1*n2)/n3, with the intermediate result being double.""
  873: #ifdef BUGGY_LL_MUL
  874: DCell d = mmul(n1,n2);
  875: #else
  876: DCell d = (DCell)n1 * (DCell)n2;
  877: #endif
  878: #ifdef ASM_SM_SLASH_REM
  879: Cell remainder;
  880: ASM_SM_SLASH_REM(DLO(d), DHI(d), n3, remainder, n4);
  881: if (FLOORED_DIV && ((DHI(d)^n3)<0) && remainder!=0) {
  882:   if (CHECK_DIVISION && n4 == CELL_MIN)
  883:     throw(BALL_RESULTRANGE);
  884:   n4--;
  885: }
  886: #else
  887: DCell r = FLOORED_DIV ? fmdiv(d,n3) : smdiv(d,n3);
  888: n4=DLO(r);
  889: #endif
  890: :
  891:  */mod nip ;
  892: 
  893: 2*	( n1 -- n2 )		core		two_star
  894: ""Shift left by 1; also works on unsigned numbers""
  895: n2 = 2*n1;
  896: :
  897:  dup + ;
  898: 
  899: 2/	( n1 -- n2 )		core		two_slash
  900: ""Arithmetic shift right by 1.  For signed numbers this is a floored
  901: division by 2 (note that @code{/} not necessarily floors).""
  902: n2 = n1>>1;
  903: :
  904:  dup MINI and IF 1 ELSE 0 THEN
  905:  [ bits/char cell * 1- ] literal 
  906:  0 DO 2* swap dup 2* >r MINI and 
  907:      IF 1 ELSE 0 THEN or r> swap
  908:  LOOP nip ;
  909: 
  910: fm/mod	( d1 n1 -- n2 n3 )		core		f_m_slash_mod
  911: ""Floored division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, @i{n1}>@i{n2}>=0 or 0>=@i{n2}>@i{n1}.""
  912: #ifdef ASM_SM_SLASH_REM
  913: ASM_SM_SLASH_REM(DLO(d1), DHI(d1), n1, n2, n3);
  914: if (((DHI(d1)^n1)<0) && n2!=0) {
  915:   if (CHECK_DIVISION && n3 == CELL_MIN)
  916:     throw(BALL_RESULTRANGE);
  917:   n3--;
  918:   n2+=n1;
  919: }
  920: #else /* !defined(ASM_SM_SLASH_REM) */
  921: DCell r = fmdiv(d1,n1);
  922: n2=DHI(r);
  923: n3=DLO(r);
  924: #endif /* !defined(ASM_SM_SLASH_REM) */
  925: :
  926:  dup >r dup 0< IF  negate >r dnegate r>  THEN
  927:  over       0< IF  tuck + swap  THEN
  928:  um/mod
  929:  r> 0< IF  swap negate swap  THEN ;
  930: 
  931: sm/rem	( d1 n1 -- n2 n3 )		core		s_m_slash_rem
  932: ""Symmetric division: @i{d1} = @i{n3}*@i{n1}+@i{n2}, sign(@i{n2})=sign(@i{d1}) or 0.""
  933: #ifdef ASM_SM_SLASH_REM
  934: ASM_SM_SLASH_REM(DLO(d1), DHI(d1), n1, n2, n3);
  935: #else /* !defined(ASM_SM_SLASH_REM) */
  936: DCell r = smdiv(d1,n1);
  937: n2=DHI(r);
  938: n3=DLO(r);
  939: #endif /* !defined(ASM_SM_SLASH_REM) */
  940: :
  941:  over >r dup >r abs -rot
  942:  dabs rot um/mod
  943:  r> r@ xor 0< IF       negate       THEN
  944:  r>        0< IF  swap negate swap  THEN ;
  945: 
  946: m*	( n1 n2 -- d )		core	m_star
  947: #ifdef BUGGY_LL_MUL
  948: d = mmul(n1,n2);
  949: #else
  950: d = (DCell)n1 * (DCell)n2;
  951: #endif
  952: :
  953:  2dup      0< and >r
  954:  2dup swap 0< and >r
  955:  um* r> - r> - ;
  956: 
  957: um*	( u1 u2 -- ud )		core	u_m_star
  958: /* use u* as alias */
  959: #ifdef BUGGY_LL_MUL
  960: ud = ummul(u1,u2);
  961: #else
  962: ud = (UDCell)u1 * (UDCell)u2;
  963: #endif
  964: :
  965:    0 -rot dup [ 8 cells ] literal -
  966:    DO
  967: 	dup 0< I' and d2*+ drop
  968:    LOOP ;
  969: : d2*+ ( ud n -- ud+n c )
  970:    over MINI
  971:    and >r >r 2dup d+ swap r> + swap r> ;
  972: 
  973: um/mod	( ud u1 -- u2 u3 )		core	u_m_slash_mod
  974: ""ud=u3*u1+u2, u1>u2>=0""
  975: #ifdef ASM_UM_SLASH_MOD
  976: ASM_UM_SLASH_MOD(DLO(ud), DHI(ud), u1, u2, u3);
  977: #else /* !defined(ASM_UM_SLASH_MOD) */
  978: UDCell r = umdiv(ud,u1);
  979: u2=DHI(r);
  980: u3=DLO(r);
  981: #endif /* !defined(ASM_UM_SLASH_MOD) */
  982: :
  983:    0 swap [ 8 cells 1 + ] literal 0
  984:    ?DO /modstep
  985:    LOOP drop swap 1 rshift or swap ;
  986: : /modstep ( ud c R: u -- ud-?u c R: u )
  987:    >r over r@ u< 0= or IF r@ - 1 ELSE 0 THEN  d2*+ r> ;
  988: : d2*+ ( ud n -- ud+n c )
  989:    over MINI
  990:    and >r >r 2dup d+ swap r> + swap r> ;
  991: 
  992: m+	( d1 n -- d2 )		double		m_plus
  993: #ifdef BUGGY_LL_ADD
  994: DLO_IS(d2, DLO(d1)+n);
  995: DHI_IS(d2, DHI(d1) - (n<0) + (DLO(d2)<DLO(d1)));
  996: #else
  997: d2 = d1+n;
  998: #endif
  999: :
 1000:  s>d d+ ;
 1001: 
 1002: d+	( d1 d2 -- d )		double	d_plus
 1003: #ifdef BUGGY_LL_ADD
 1004: DLO_IS(d, DLO(d1) + DLO(d2));
 1005: DHI_IS(d, DHI(d1) + DHI(d2) + (d.lo<DLO(d1)));
 1006: #else
 1007: d = d1+d2;
 1008: #endif
 1009: :
 1010:  rot + >r tuck + swap over u> r> swap - ;
 1011: 
 1012: d-	( d1 d2 -- d )		double		d_minus
 1013: #ifdef BUGGY_LL_ADD
 1014: DLO_IS(d, DLO(d1) - DLO(d2));
 1015: DHI_IS(d, DHI(d1)-DHI(d2)-(DLO(d1)<DLO(d2)));
 1016: #else
 1017: d = d1-d2;
 1018: #endif
 1019: :
 1020:  dnegate d+ ;
 1021: 
 1022: dnegate	( d1 -- d2 )		double	d_negate
 1023: /* use dminus as alias */
 1024: #ifdef BUGGY_LL_ADD
 1025: d2 = dnegate(d1);
 1026: #else
 1027: d2 = -d1;
 1028: #endif
 1029: :
 1030:  invert swap negate tuck 0= - ;
 1031: 
 1032: d2*	( d1 -- d2 )		double		d_two_star
 1033: ""Shift left by 1; also works on unsigned numbers""
 1034: d2 = DLSHIFT(d1,1);
 1035: :
 1036:  2dup d+ ;
 1037: 
 1038: d2/	( d1 -- d2 )		double		d_two_slash
 1039: ""Arithmetic shift right by 1.  For signed numbers this is a floored
 1040: division by 2.""
 1041: #ifdef BUGGY_LL_SHIFT
 1042: DHI_IS(d2, DHI(d1)>>1);
 1043: DLO_IS(d2, (DLO(d1)>>1) | (DHI(d1)<<(CELL_BITS-1)));
 1044: #else
 1045: d2 = d1>>1;
 1046: #endif
 1047: :
 1048:  dup 1 and >r 2/ swap 2/ [ 1 8 cells 1- lshift 1- ] Literal and
 1049:  r> IF  [ 1 8 cells 1- lshift ] Literal + THEN  swap ;
 1050: 
 1051: and	( w1 w2 -- w )		core
 1052: w = w1&w2;
 1053: 
 1054: or	( w1 w2 -- w )		core
 1055: w = w1|w2;
 1056: :
 1057:  invert swap invert and invert ;
 1058: 
 1059: xor	( w1 w2 -- w )		core	x_or
 1060: w = w1^w2;
 1061: 
 1062: invert	( w1 -- w2 )		core
 1063: w2 = ~w1;
 1064: :
 1065:  MAXU xor ;
 1066: 
 1067: rshift	( u1 n -- u2 )		core	r_shift
 1068: ""Logical shift right by @i{n} bits.""
 1069: #ifdef BROKEN_SHIFT
 1070:   u2 = rshift(u1, n);
 1071: #else
 1072:   u2 = u1 >> n;
 1073: #endif
 1074: :
 1075:     0 ?DO 2/ MAXI and LOOP ;
 1076: 
 1077: lshift	( u1 n -- u2 )		core	l_shift
 1078: #ifdef BROKEN_SHIFT
 1079:   u2 = lshift(u1, n);
 1080: #else
 1081:   u2 = u1 << n;
 1082: #endif
 1083: :
 1084:     0 ?DO 2* LOOP ;
 1085: 
 1086: \g compare
 1087: 
 1088: \ comparisons(prefix, args, prefix, arg1, arg2, wordsets...)
 1089: define(comparisons,
 1090: $1=	( $2 -- f )		$6	$3equals
 1091: f = FLAG($4==$5);
 1092: :
 1093:     [ char $1x char 0 = [IF]
 1094: 	] IF false ELSE true THEN [
 1095:     [ELSE]
 1096: 	] xor 0= [
 1097:     [THEN] ] ;
 1098: 
 1099: $1<>	( $2 -- f )		$7	$3not_equals
 1100: f = FLAG($4!=$5);
 1101: :
 1102:     [ char $1x char 0 = [IF]
 1103: 	] IF true ELSE false THEN [
 1104:     [ELSE]
 1105: 	] xor 0<> [
 1106:     [THEN] ] ;
 1107: 
 1108: $1<	( $2 -- f )		$8	$3less_than
 1109: f = FLAG($4<$5);
 1110: :
 1111:     [ char $1x char 0 = [IF]
 1112: 	] MINI and 0<> [
 1113:     [ELSE] char $1x char u = [IF]
 1114: 	]   2dup xor 0<  IF nip ELSE - THEN 0<  [
 1115: 	[ELSE]
 1116: 	    ] MINI xor >r MINI xor r> u< [
 1117: 	[THEN]
 1118:     [THEN] ] ;
 1119: 
 1120: $1>	( $2 -- f )		$9	$3greater_than
 1121: f = FLAG($4>$5);
 1122: :
 1123:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
 1124:     $1< ;
 1125: 
 1126: $1<=	( $2 -- f )		gforth	$3less_or_equal
 1127: f = FLAG($4<=$5);
 1128: :
 1129:     $1> 0= ;
 1130: 
 1131: $1>=	( $2 -- f )		gforth	$3greater_or_equal
 1132: f = FLAG($4>=$5);
 1133: :
 1134:     [ char $1x char 0 = [IF] ] negate [ [ELSE] ] swap [ [THEN] ]
 1135:     $1<= ;
 1136: 
 1137: )
 1138: 
 1139: comparisons(0, n, zero_, n, 0, core, core-ext, core, core-ext)
 1140: comparisons(, n1 n2, , n1, n2, core, core-ext, core, core)
 1141: comparisons(u, u1 u2, u_, u1, u2, gforth, gforth, core, core-ext)
 1142: 
 1143: \ dcomparisons(prefix, args, prefix, arg1, arg2, wordsets...)
 1144: define(dcomparisons,
 1145: $1=	( $2 -- f )		$6	$3equals
 1146: #ifdef BUGGY_LL_CMP
 1147: f = FLAG($4.lo==$5.lo && $4.hi==$5.hi);
 1148: #else
 1149: f = FLAG($4==$5);
 1150: #endif
 1151: 
 1152: $1<>	( $2 -- f )		$7	$3not_equals
 1153: #ifdef BUGGY_LL_CMP
 1154: f = FLAG($4.lo!=$5.lo || $4.hi!=$5.hi);
 1155: #else
 1156: f = FLAG($4!=$5);
 1157: #endif
 1158: 
 1159: $1<	( $2 -- f )		$8	$3less_than
 1160: #ifdef BUGGY_LL_CMP
 1161: f = FLAG($4.hi==$5.hi ? $4.lo<$5.lo : $4.hi<$5.hi);
 1162: #else
 1163: f = FLAG($4<$5);
 1164: #endif
 1165: 
 1166: $1>	( $2 -- f )		$9	$3greater_than
 1167: #ifdef BUGGY_LL_CMP
 1168: f = FLAG($4.hi==$5.hi ? $4.lo>$5.lo : $4.hi>$5.hi);
 1169: #else
 1170: f = FLAG($4>$5);
 1171: #endif
 1172: 
 1173: $1<=	( $2 -- f )		gforth	$3less_or_equal
 1174: #ifdef BUGGY_LL_CMP
 1175: f = FLAG($4.hi==$5.hi ? $4.lo<=$5.lo : $4.hi<=$5.hi);
 1176: #else
 1177: f = FLAG($4<=$5);
 1178: #endif
 1179: 
 1180: $1>=	( $2 -- f )		gforth	$3greater_or_equal
 1181: #ifdef BUGGY_LL_CMP
 1182: f = FLAG($4.hi==$5.hi ? $4.lo>=$5.lo : $4.hi>=$5.hi);
 1183: #else
 1184: f = FLAG($4>=$5);
 1185: #endif
 1186: 
 1187: )
 1188: 
 1189: \+dcomps
 1190: 
 1191: dcomparisons(d, d1 d2, d_, d1, d2, double, gforth, double, gforth)
 1192: dcomparisons(d0, d, d_zero_, d, DZERO, double, gforth, double, gforth)
 1193: dcomparisons(du, ud1 ud2, d_u_, ud1, ud2, gforth, gforth, double-ext, gforth)
 1194: 
 1195: \+
 1196: 
 1197: within	( u1 u2 u3 -- f )		core-ext
 1198: ""u2=<u1<u3 or: u3=<u2 and u1 is not in [u3,u2).  This works for
 1199: unsigned and signed numbers (but not a mixture).  Another way to think
 1200: about this word is to consider the numbers as a circle (wrapping
 1201: around from @code{max-u} to 0 for unsigned, and from @code{max-n} to
 1202: min-n for signed numbers); now consider the range from u2 towards
 1203: increasing numbers up to and excluding u3 (giving an empty range if
 1204: u2=u3); if u1 is in this range, @code{within} returns true.""
 1205: f = FLAG(u1-u2 < u3-u2);
 1206: :
 1207:  over - >r - r> u< ;
 1208: 
 1209: \g stack
 1210: 
 1211: useraddr	( #u -- a_addr )	new
 1212: a_addr = (Cell *)(up+u);
 1213: 
 1214: up!	( a_addr -- )	gforth	up_store
 1215: gforth_UP=up=(Address)a_addr;
 1216: :
 1217:  up ! ;
 1218: Variable UP
 1219: 
 1220: sp@	( S:... -- a_addr )		gforth		sp_fetch
 1221: a_addr = sp;
 1222: 
 1223: sp!	( a_addr -- S:... )		gforth		sp_store
 1224: sp = a_addr;
 1225: 
 1226: rp@	( -- a_addr )		gforth		rp_fetch
 1227: a_addr = rp;
 1228: 
 1229: rp!	( a_addr -- )		gforth		rp_store
 1230: rp = a_addr;
 1231: 
 1232: \+floating
 1233: 
 1234: fp@	( f:... -- f_addr )	gforth	fp_fetch
 1235: f_addr = fp;
 1236: 
 1237: fp!	( f_addr -- f:... )	gforth	fp_store
 1238: fp = f_addr;
 1239: 
 1240: \+
 1241: 
 1242: >r	( w -- R:w )		core	to_r
 1243: :
 1244:  (>r) ;
 1245: : (>r)  rp@ cell+ @ rp@ ! rp@ cell+ ! ;
 1246: 
 1247: r>	( R:w -- w )		core	r_from
 1248: :
 1249:  rp@ cell+ @ rp@ @ rp@ cell+ ! (rdrop) rp@ ! ;
 1250: Create (rdrop) ' ;s A,
 1251: 
 1252: rdrop	( R:w -- )		gforth
 1253: :
 1254:  r> r> drop >r ;
 1255: 
 1256: 2>r	( d -- R:d )	core-ext	two_to_r
 1257: :
 1258:  swap r> swap >r swap >r >r ;
 1259: 
 1260: 2r>	( R:d -- d )	core-ext	two_r_from
 1261: :
 1262:  r> r> swap r> swap >r swap ;
 1263: 
 1264: 2r@	( R:d -- R:d d )	core-ext	two_r_fetch
 1265: :
 1266:  i' j ;
 1267: 
 1268: 2rdrop	( R:d -- )		gforth	two_r_drop
 1269: :
 1270:  r> r> drop r> drop >r ;
 1271: 
 1272: over	( w1 w2 -- w1 w2 w1 )		core
 1273: :
 1274:  sp@ cell+ @ ;
 1275: 
 1276: drop	( w -- )		core
 1277: :
 1278:  IF THEN ;
 1279: 
 1280: swap	( w1 w2 -- w2 w1 )		core
 1281: :
 1282:  >r (swap) ! r> (swap) @ ;
 1283: Variable (swap)
 1284: 
 1285: dup	( w -- w w )		core	dupe
 1286: :
 1287:  sp@ @ ;
 1288: 
 1289: rot	( w1 w2 w3 -- w2 w3 w1 )	core	rote
 1290: :
 1291: [ defined? (swap) [IF] ]
 1292:     (swap) ! (rot) ! >r (rot) @ (swap) @ r> ;
 1293: Variable (rot)
 1294: [ELSE] ]
 1295:     >r swap r> swap ;
 1296: [THEN]
 1297: 
 1298: -rot	( w1 w2 w3 -- w3 w1 w2 )	gforth	not_rote
 1299: :
 1300:  rot rot ;
 1301: 
 1302: nip	( w1 w2 -- w2 )		core-ext
 1303: :
 1304:  swap drop ;
 1305: 
 1306: tuck	( w1 w2 -- w2 w1 w2 )	core-ext
 1307: :
 1308:  swap over ;
 1309: 
 1310: ?dup	( w -- S:... w )	core	question_dupe
 1311: ""Actually the stack effect is: @code{( w -- 0 | w w )}.  It performs a
 1312: @code{dup} if w is nonzero.""
 1313: if (w!=0) {
 1314:   *--sp = w;
 1315: }
 1316: :
 1317:  dup IF dup THEN ;
 1318: 
 1319: pick	( S:... u -- S:... w )		core-ext
 1320: ""Actually the stack effect is @code{ x0 ... xu u -- x0 ... xu x0 }.""
 1321: w = sp[u];
 1322: :
 1323:  1+ cells sp@ + @ ;
 1324: 
 1325: 2drop	( w1 w2 -- )		core	two_drop
 1326: :
 1327:  drop drop ;
 1328: 
 1329: 2dup	( w1 w2 -- w1 w2 w1 w2 )	core	two_dupe
 1330: :
 1331:  over over ;
 1332: 
 1333: 2over	( w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 )	core	two_over
 1334: :
 1335:  3 pick 3 pick ;
 1336: 
 1337: 2swap	( w1 w2 w3 w4 -- w3 w4 w1 w2 )	core	two_swap
 1338: :
 1339:  rot >r rot r> ;
 1340: 
 1341: 2rot	( w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 )	double-ext	two_rote
 1342: :
 1343:  >r >r 2swap r> r> 2swap ;
 1344: 
 1345: 2nip	( w1 w2 w3 w4 -- w3 w4 )	gforth	two_nip
 1346: :
 1347:  2swap 2drop ;
 1348: 
 1349: 2tuck	( w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 )	gforth	two_tuck
 1350: :
 1351:  2swap 2over ;
 1352: 
 1353: \ toggle is high-level: 0.11/0.42%
 1354: 
 1355: \g memory
 1356: 
 1357: @	( a_addr -- w )		core	fetch
 1358: ""@i{w} is the cell stored at @i{a_addr}.""
 1359: w = *a_addr;
 1360: 
 1361: \ lit@ / lit_fetch = lit @
 1362: 
 1363: lit@		( #a_addr -- w ) new	lit_fetch
 1364: w = *a_addr;
 1365: 
 1366: !	( w a_addr -- )		core	store
 1367: ""Store @i{w} into the cell at @i{a-addr}.""
 1368: *a_addr = w;
 1369: 
 1370: +!	( n a_addr -- )		core	plus_store
 1371: ""Add @i{n} to the cell at @i{a-addr}.""
 1372: *a_addr += n;
 1373: :
 1374:  tuck @ + swap ! ;
 1375: 
 1376: c@	( c_addr -- c )		core	c_fetch
 1377: ""@i{c} is the char stored at @i{c_addr}.""
 1378: c = *c_addr;
 1379: :
 1380: [ bigendian [IF] ]
 1381:     [ cell>bit 4 = [IF] ]
 1382: 	dup [ 0 cell - ] Literal and @ swap 1 and
 1383: 	IF  $FF and  ELSE  8>>  THEN  ;
 1384:     [ [ELSE] ]
 1385: 	dup [ cell 1- ] literal and
 1386: 	tuck - @ swap [ cell 1- ] literal xor
 1387:  	0 ?DO 8>> LOOP $FF and
 1388:     [ [THEN] ]
 1389: [ [ELSE] ]
 1390:     [ cell>bit 4 = [IF] ]
 1391: 	dup [ 0 cell - ] Literal and @ swap 1 and
 1392: 	IF  8>>  ELSE  $FF and  THEN
 1393:     [ [ELSE] ]
 1394: 	dup [ cell  1- ] literal and 
 1395: 	tuck - @ swap
 1396: 	0 ?DO 8>> LOOP 255 and
 1397:     [ [THEN] ]
 1398: [ [THEN] ]
 1399: ;
 1400: : 8>> 2/ 2/ 2/ 2/  2/ 2/ 2/ 2/ ;
 1401: 
 1402: c!	( c c_addr -- )		core	c_store
 1403: ""Store @i{c} into the char at @i{c-addr}.""
 1404: *c_addr = c;
 1405: :
 1406: [ bigendian [IF] ]
 1407:     [ cell>bit 4 = [IF] ]
 1408: 	tuck 1 and IF  $FF and  ELSE  8<<  THEN >r
 1409: 	dup -2 and @ over 1 and cells masks + @ and
 1410: 	r> or swap -2 and ! ;
 1411: 	Create masks $00FF , $FF00 ,
 1412:     [ELSE] ]
 1413: 	dup [ cell 1- ] literal and dup 
 1414: 	[ cell 1- ] literal xor >r
 1415: 	- dup @ $FF r@ 0 ?DO 8<< LOOP invert and
 1416: 	rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
 1417:     [THEN]
 1418: [ELSE] ]
 1419:     [ cell>bit 4 = [IF] ]
 1420: 	tuck 1 and IF  8<<  ELSE  $FF and  THEN >r
 1421: 	dup -2 and @ over 1 and cells masks + @ and
 1422: 	r> or swap -2 and ! ;
 1423: 	Create masks $FF00 , $00FF ,
 1424:     [ELSE] ]
 1425: 	dup [ cell 1- ] literal and dup >r
 1426: 	- dup @ $FF r@ 0 ?DO 8<< LOOP invert and
 1427: 	rot $FF and r> 0 ?DO 8<< LOOP or swap ! ;
 1428:     [THEN]
 1429: [THEN]
 1430: : 8<< 2* 2* 2* 2*  2* 2* 2* 2* ;
 1431: 
 1432: 2!	( w1 w2 a_addr -- )		core	two_store
 1433: ""Store @i{w2} into the cell at @i{c-addr} and @i{w1} into the next cell.""
 1434: a_addr[0] = w2;
 1435: a_addr[1] = w1;
 1436: :
 1437:  tuck ! cell+ ! ;
 1438: 
 1439: 2@	( a_addr -- w1 w2 )		core	two_fetch
 1440: ""@i{w2} is the content of the cell stored at @i{a-addr}, @i{w1} is
 1441: the content of the next cell.""
 1442: w2 = a_addr[0];
 1443: w1 = a_addr[1];
 1444: :
 1445:  dup cell+ @ swap @ ;
 1446: 
 1447: cell+	( a_addr1 -- a_addr2 )	core	cell_plus
 1448: ""@code{1 cells +}""
 1449: a_addr2 = a_addr1+1;
 1450: :
 1451:  cell + ;
 1452: 
 1453: cells	( n1 -- n2 )		core
 1454: "" @i{n2} is the number of address units of @i{n1} cells.""
 1455: n2 = n1 * sizeof(Cell);
 1456: :
 1457:  [ cell
 1458:  2/ dup [IF] ] 2* [ [THEN]
 1459:  2/ dup [IF] ] 2* [ [THEN]
 1460:  2/ dup [IF] ] 2* [ [THEN]
 1461:  2/ dup [IF] ] 2* [ [THEN]
 1462:  drop ] ;
 1463: 
 1464: char+	( c_addr1 -- c_addr2 )	core	char_plus
 1465: ""@code{1 chars +}.""
 1466: c_addr2 = c_addr1 + 1;
 1467: :
 1468:  1+ ;
 1469: 
 1470: (chars)	( n1 -- n2 )	gforth	paren_chars
 1471: n2 = n1 * sizeof(Char);
 1472: :
 1473:  ;
 1474: 
 1475: count	( c_addr1 -- c_addr2 u )	core
 1476: ""@i{c-addr2} is the first character and @i{u} the length of the
 1477: counted string at @i{c-addr1}.""
 1478: u = *c_addr1;
 1479: c_addr2 = c_addr1+1;
 1480: :
 1481:  dup 1+ swap c@ ;
 1482: 
 1483: \g compiler
 1484: 
 1485: \+f83headerstring
 1486: 
 1487: (f83find)	( c_addr u f83name1 -- f83name2 )	new	paren_f83find
 1488: for (; f83name1 != NULL; f83name1 = (struct F83Name *)(f83name1->next))
 1489:   if ((UCell)F83NAME_COUNT(f83name1)==u &&
 1490:       memcasecmp(c_addr, f83name1->name, u)== 0 /* or inline? */)
 1491:     break;
 1492: f83name2=f83name1;
 1493: #ifdef DEBUG
 1494: fprintf(stderr, "F83find ");
 1495: fwrite(c_addr, u, 1, stderr);
 1496: fprintf(stderr, " found %08x\n", f83name2); 
 1497: #endif
 1498: :
 1499:     BEGIN  dup WHILE  (find-samelen)  dup  WHILE
 1500: 	>r 2dup r@ cell+ char+ capscomp  0=
 1501: 	IF  2drop r>  EXIT  THEN
 1502: 	r> @
 1503:     REPEAT  THEN  nip nip ;
 1504: : (find-samelen) ( u f83name1 -- u f83name2/0 )
 1505:     BEGIN  2dup cell+ c@ $1F and <> WHILE  @  dup 0= UNTIL  THEN ;
 1506: : capscomp ( c_addr1 u c_addr2 -- n )
 1507:  swap bounds
 1508:  ?DO  dup c@ I c@ <>
 1509:      IF  dup c@ toupper I c@ toupper =
 1510:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
 1511:  ELSE  c@ toupper I c@ toupper - unloop  THEN  sgn ;
 1512: : sgn ( n -- -1/0/1 )
 1513:  dup 0= IF EXIT THEN  0< 2* 1+ ;
 1514: 
 1515: \-
 1516: 
 1517: (listlfind)	( c_addr u longname1 -- longname2 )	new	paren_listlfind
 1518: longname2=listlfind(c_addr, u, longname1);
 1519: :
 1520:     BEGIN  dup WHILE  (findl-samelen)  dup  WHILE
 1521: 	>r 2dup r@ cell+ cell+ capscomp  0=
 1522: 	IF  2drop r>  EXIT  THEN
 1523: 	r> @
 1524:     REPEAT  THEN  nip nip ;
 1525: : (findl-samelen) ( u longname1 -- u longname2/0 )
 1526:     BEGIN  2dup cell+ @ lcount-mask and <> WHILE  @  dup 0= UNTIL  THEN ;
 1527: : capscomp ( c_addr1 u c_addr2 -- n )
 1528:  swap bounds
 1529:  ?DO  dup c@ I c@ <>
 1530:      IF  dup c@ toupper I c@ toupper =
 1531:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
 1532:  ELSE  c@ toupper I c@ toupper - unloop  THEN  sgn ;
 1533: : sgn ( n -- -1/0/1 )
 1534:  dup 0= IF EXIT THEN  0< 2* 1+ ;
 1535: 
 1536: \+hash
 1537: 
 1538: (hashlfind)	( c_addr u a_addr -- longname2 )	new	paren_hashlfind
 1539: longname2 = hashlfind(c_addr, u, a_addr);
 1540: :
 1541:  BEGIN  dup  WHILE
 1542:         2@ >r >r dup r@ cell+ @ lcount-mask and =
 1543:         IF  2dup r@ cell+ cell+ capscomp 0=
 1544: 	    IF  2drop r> rdrop  EXIT  THEN  THEN
 1545: 	rdrop r>
 1546:  REPEAT nip nip ;
 1547: 
 1548: (tablelfind)	( c_addr u a_addr -- longname2 )	new	paren_tablelfind
 1549: ""A case-sensitive variant of @code{(hashfind)}""
 1550: longname2 = tablelfind(c_addr, u, a_addr);
 1551: :
 1552:  BEGIN  dup  WHILE
 1553:         2@ >r >r dup r@ cell+ @ lcount-mask and =
 1554:         IF  2dup r@ cell+ cell+ -text 0=
 1555: 	    IF  2drop r> rdrop  EXIT  THEN  THEN
 1556: 	rdrop r>
 1557:  REPEAT nip nip ;
 1558: : -text ( c_addr1 u c_addr2 -- n )
 1559:  swap bounds
 1560:  ?DO  dup c@ I c@ = WHILE  1+  LOOP  drop 0
 1561:  ELSE  c@ I c@ - unloop  THEN  sgn ;
 1562: : sgn ( n -- -1/0/1 )
 1563:  dup 0= IF EXIT THEN  0< 2* 1+ ;
 1564: 
 1565: (hashkey1)	( c_addr u ubits -- ukey )		gforth	paren_hashkey1
 1566: ""ukey is the hash key for the string c_addr u fitting in ubits bits""
 1567: ukey = hashkey1(c_addr, u, ubits);
 1568: :
 1569:  dup rot-values + c@ over 1 swap lshift 1- >r
 1570:  tuck - 2swap r> 0 2swap bounds
 1571:  ?DO  dup 4 pick lshift swap 3 pick rshift or
 1572:       I c@ toupper xor
 1573:       over and  LOOP
 1574:  nip nip nip ;
 1575: Create rot-values
 1576:   5 c, 0 c, 1 c, 2 c, 3 c,  4 c, 5 c, 5 c, 5 c, 5 c,
 1577:   3 c, 5 c, 5 c, 5 c, 5 c,  7 c, 5 c, 5 c, 5 c, 5 c,
 1578:   7 c, 5 c, 5 c, 5 c, 5 c,  6 c, 5 c, 5 c, 5 c, 5 c,
 1579:   7 c, 5 c, 5 c,
 1580: 
 1581: \+
 1582: 
 1583: \+
 1584: 
 1585: (parse-white)	( c_addr1 u1 -- c_addr2 u2 )	gforth	paren_parse_white
 1586: struct Cellpair r=parse_white(c_addr1, u1);
 1587: c_addr2 = (Char *)(r.n1);
 1588: u2 = r.n2;
 1589: :
 1590:  BEGIN  dup  WHILE  over c@ bl <=  WHILE  1 /string
 1591:  REPEAT  THEN  2dup
 1592:  BEGIN  dup  WHILE  over c@ bl >   WHILE  1 /string
 1593:  REPEAT  THEN  nip - ;
 1594: 
 1595: aligned	( c_addr -- a_addr )	core
 1596: "" @i{a-addr} is the first aligned address greater than or equal to @i{c-addr}.""
 1597: a_addr = (Cell *)((((Cell)c_addr)+(sizeof(Cell)-1))&(-sizeof(Cell)));
 1598: :
 1599:  [ cell 1- ] Literal + [ -1 cells ] Literal and ;
 1600: 
 1601: faligned	( c_addr -- f_addr )	float	f_aligned
 1602: "" @i{f-addr} is the first float-aligned address greater than or equal to @i{c-addr}.""
 1603: f_addr = (Float *)((((Cell)c_addr)+(sizeof(Float)-1))&(-sizeof(Float)));
 1604: :
 1605:  [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
 1606: 
 1607: \ threading stuff is currently only interesting if we have a compiler
 1608: \fhas? standardthreading has? compiler and [IF]
 1609: threading-method	( -- n )	gforth	threading_method
 1610: ""0 if the engine is direct threaded. Note that this may change during
 1611: the lifetime of an image.""
 1612: #if defined(DOUBLY_INDIRECT)
 1613: n=2;
 1614: #else
 1615: # if defined(DIRECT_THREADED)
 1616: n=0;
 1617: # else
 1618: n=1;
 1619: # endif
 1620: #endif
 1621: :
 1622:  1 ;
 1623: 
 1624: \f[THEN]
 1625: 
 1626: \g hostos
 1627: 
 1628: key-file	( wfileid -- c )		gforth	paren_key_file
 1629: ""Read one character @i{c} from @i{wfileid}.  This word disables
 1630: buffering for @i{wfileid}.  If you want to read characters from a
 1631: terminal in non-canonical (raw) mode, you have to put the terminal in
 1632: non-canonical mode yourself (using the C interface); the exception is
 1633: @code{stdin}: Gforth automatically puts it into non-canonical mode.""
 1634: #ifdef HAS_FILE
 1635: fflush(stdout);
 1636: c = key((FILE*)wfileid);
 1637: #else
 1638: c = key(stdin);
 1639: #endif
 1640: 
 1641: key?-file	( wfileid -- f )	        gforth	key_q_file
 1642: ""@i{f} is true if at least one character can be read from @i{wfileid}
 1643: without blocking.  If you also want to use @code{read-file} or
 1644: @code{read-line} on the file, you have to call @code{key?-file} or
 1645: @code{key-file} first (these two words disable buffering).""
 1646: #ifdef HAS_FILE
 1647: fflush(stdout);
 1648: f = key_query((FILE*)wfileid);
 1649: #else
 1650: f = key_query(stdin);
 1651: #endif
 1652: 
 1653: stdin	( -- wfileid )	gforth
 1654: ""The standard input file of the Gforth process.""
 1655: wfileid = (Cell)stdin;
 1656: 
 1657: stdout	( -- wfileid )	gforth
 1658: ""The standard output file of the Gforth process.""
 1659: wfileid = (Cell)stdout;
 1660: 
 1661: stderr	( -- wfileid )	gforth
 1662: ""The standard error output file of the Gforth process.""
 1663: wfileid = (Cell)stderr;
 1664: 
 1665: \+os
 1666: 
 1667: form	( -- urows ucols )	gforth
 1668: ""The number of lines and columns in the terminal. These numbers may
 1669: change with the window size.  Note that it depends on the OS whether
 1670: this reflects the actual size and changes with the window size
 1671: (currently only on Unix-like OSs).  On other OSs you just get a
 1672: default, and can tell Gforth the terminal size by setting the
 1673: environment variables @code{COLUMNS} and @code{LINES} before starting
 1674: Gforth.""
 1675: /* we could block SIGWINCH here to get a consistent size, but I don't
 1676:  think this is necessary or always beneficial */
 1677: urows=rows;
 1678: ucols=cols;
 1679: 
 1680: wcwidth	( u -- n )	gforth
 1681: ""The number of fixed-width characters per unicode character u""
 1682: #ifdef HAVE_WCWIDTH
 1683: n = wcwidth(u);
 1684: #else
 1685: n = 1;
 1686: #endif
 1687: 
 1688: flush-icache	( c_addr u -- )	gforth	flush_icache
 1689: ""Make sure that the instruction cache of the processor (if there is
 1690: one) does not contain stale data at @i{c-addr} and @i{u} bytes
 1691: afterwards. @code{END-CODE} performs a @code{flush-icache}
 1692: automatically. Caveat: @code{flush-icache} might not work on your
 1693: installation; this is usually the case if direct threading is not
 1694: supported on your machine (take a look at your @file{machine.h}) and
 1695: your machine has a separate instruction cache. In such cases,
 1696: @code{flush-icache} does nothing instead of flushing the instruction
 1697: cache.""
 1698: FLUSH_ICACHE((caddr_t)c_addr,u);
 1699: 
 1700: (bye)	( n -- )	gforth	paren_bye
 1701: SUPER_END;
 1702: return (Label *)n;
 1703: 
 1704: (system)	( c_addr u -- wretval wior )	gforth	paren_system
 1705: wretval = gforth_system(c_addr, u);  
 1706: wior = IOR(wretval==-1 || (wretval==127 && errno != 0));
 1707: 
 1708: getenv	( c_addr1 u1 -- c_addr2 u2 )	gforth
 1709: ""The string @i{c-addr1 u1} specifies an environment variable. The string @i{c-addr2 u2}
 1710: is the host operating system's expansion of that environment variable. If the
 1711: environment variable does not exist, @i{c-addr2 u2} specifies a string 0 characters
 1712: in length.""
 1713: /* close ' to keep fontify happy */
 1714: c_addr2 = (Char *)getenv(cstr(c_addr1,u1,1));
 1715: u2 = (c_addr2 == NULL ? 0 : strlen((char *)c_addr2));
 1716: 
 1717: open-pipe	( c_addr u wfam -- wfileid wior )	gforth	open_pipe
 1718: fflush(stdout);
 1719: wfileid=(Cell)popen(cstr(c_addr,u,1),pfileattr[wfam]); /* ~ expansion of 1st arg? */
 1720: wior = IOR(wfileid==0); /* !! the man page says that errno is not set reliably */
 1721: 
 1722: close-pipe	( wfileid -- wretval wior )		gforth	close_pipe
 1723: wretval = pclose((FILE *)wfileid);
 1724: wior = IOR(wretval==-1);
 1725: 
 1726: time&date	( -- nsec nmin nhour nday nmonth nyear )	facility-ext	time_and_date
 1727: ""Report the current time of day. Seconds, minutes and hours are numbered from 0.
 1728: Months are numbered from 1.""
 1729: #if 1
 1730: time_t now;
 1731: struct tm *ltime;
 1732: time(&now);
 1733: ltime=localtime(&now);
 1734: #else
 1735: struct timeval time1;
 1736: struct timezone zone1;
 1737: struct tm *ltime;
 1738: gettimeofday(&time1,&zone1);
 1739: /* !! Single Unix specification: 
 1740:    If tzp is not a null pointer, the behaviour is unspecified. */
 1741: ltime=localtime((time_t *)&time1.tv_sec);
 1742: #endif
 1743: nyear =ltime->tm_year+1900;
 1744: nmonth=ltime->tm_mon+1;
 1745: nday  =ltime->tm_mday;
 1746: nhour =ltime->tm_hour;
 1747: nmin  =ltime->tm_min;
 1748: nsec  =ltime->tm_sec;
 1749: 
 1750: ms	( u -- )	facility-ext
 1751: ""Wait at least @i{n} milli-second.""
 1752: gforth_ms(u);
 1753: 
 1754: allocate	( u -- a_addr wior )	memory
 1755: ""Allocate @i{u} address units of contiguous data space. The initial
 1756: contents of the data space is undefined. If the allocation is successful,
 1757: @i{a-addr} is the start address of the allocated region and @i{wior}
 1758: is 0. If the allocation fails, @i{a-addr} is undefined and @i{wior}
 1759: is a non-zero I/O result code.""
 1760: a_addr = (Cell *)malloc(u?u:1);
 1761: wior = IOR(a_addr==NULL);
 1762: 
 1763: free	( a_addr -- wior )		memory
 1764: ""Return the region of data space starting at @i{a-addr} to the system.
 1765: The region must originally have been obtained using @code{allocate} or
 1766: @code{resize}. If the operational is successful, @i{wior} is 0.
 1767: If the operation fails, @i{wior} is a non-zero I/O result code.""
 1768: free(a_addr);
 1769: wior = 0;
 1770: 
 1771: resize	( a_addr1 u -- a_addr2 wior )	memory
 1772: ""Change the size of the allocated area at @i{a-addr1} to @i{u}
 1773: address units, possibly moving the contents to a different
 1774: area. @i{a-addr2} is the address of the resulting area.
 1775: If the operation is successful, @i{wior} is 0.
 1776: If the operation fails, @i{wior} is a non-zero
 1777: I/O result code. If @i{a-addr1} is 0, Gforth's (but not the Standard)
 1778: @code{resize} @code{allocate}s @i{u} address units.""
 1779: /* the following check is not necessary on most OSs, but it is needed
 1780:    on SunOS 4.1.2. */
 1781: /* close ' to keep fontify happy */
 1782: if (a_addr1==NULL)
 1783:   a_addr2 = (Cell *)malloc(u);
 1784: else
 1785:   a_addr2 = (Cell *)realloc(a_addr1, u);
 1786: wior = IOR(a_addr2==NULL);	/* !! Define a return code */
 1787: 
 1788: strerror	( n -- c_addr u )	gforth
 1789: c_addr = (Char *)strerror(n);
 1790: u = strlen((char *)c_addr);
 1791: 
 1792: strsignal	( n -- c_addr u )	gforth
 1793: c_addr = (Char *)strsignal(n);
 1794: u = strlen((char *)c_addr);
 1795: 
 1796: call-c	( ... w -- ... )	gforth	call_c
 1797: ""Call the C function pointed to by @i{w}. The C function has to
 1798: access the stack itself. The stack pointers are exported in the global
 1799: variables @code{gforth_SP} and @code{gforth_FP}.""
 1800: /* This is a first attempt at support for calls to C. This may change in
 1801:    the future */
 1802: IF_fpTOS(fp[0]=fpTOS);
 1803: gforth_FP=fp;
 1804: gforth_SP=sp;
 1805: gforth_RP=rp;
 1806: gforth_LP=lp;
 1807: #ifdef HAS_LINKBACK
 1808: ((void (*)())w)();
 1809: #else
 1810: ((void (*)(void *))w)(gforth_pointers);
 1811: #endif
 1812: sp=gforth_SP;
 1813: fp=gforth_FP;
 1814: rp=gforth_RP;
 1815: lp=gforth_LP;
 1816: IF_fpTOS(fpTOS=fp[0]);
 1817: 
 1818: \+
 1819: \+file
 1820: 
 1821: close-file	( wfileid -- wior )		file	close_file
 1822: wior = IOR(fclose((FILE *)wfileid)==EOF);
 1823: 
 1824: open-file	( c_addr u wfam -- wfileid wior )	file	open_file
 1825: wfileid = opencreate_file(tilde_cstr(c_addr,u,1), wfam, 0, &wior);
 1826: 
 1827: create-file	( c_addr u wfam -- wfileid wior )	file	create_file
 1828: wfileid = opencreate_file(tilde_cstr(c_addr,u,1), wfam, O_CREAT|O_TRUNC, &wior);
 1829: 
 1830: delete-file	( c_addr u -- wior )		file	delete_file
 1831: wior = IOR(unlink(tilde_cstr(c_addr, u, 1))==-1);
 1832: 
 1833: rename-file	( c_addr1 u1 c_addr2 u2 -- wior )	file-ext	rename_file
 1834: ""Rename file @i{c_addr1 u1} to new name @i{c_addr2 u2}""
 1835: wior = rename_file(c_addr1, u1, c_addr2, u2);
 1836: 
 1837: file-position	( wfileid -- ud wior )	file	file_position
 1838: /* !! use tell and lseek? */
 1839: ud = OFF2UD(ftello((FILE *)wfileid));
 1840: wior = IOR(UD2OFF(ud)==-1);
 1841: 
 1842: reposition-file	( ud wfileid -- wior )	file	reposition_file
 1843: wior = IOR(fseeko((FILE *)wfileid, UD2OFF(ud), SEEK_SET)==-1);
 1844: 
 1845: file-size	( wfileid -- ud wior )	file	file_size
 1846: struct stat buf;
 1847: wior = IOR(fstat(fileno((FILE *)wfileid), &buf)==-1);
 1848: ud = OFF2UD(buf.st_size);
 1849: 
 1850: resize-file	( ud wfileid -- wior )	file	resize_file
 1851: wior = IOR(ftruncate(fileno((FILE *)wfileid), UD2OFF(ud))==-1);
 1852: 
 1853: read-file	( c_addr u1 wfileid -- u2 wior )	file	read_file
 1854: /* !! fread does not guarantee enough */
 1855: u2 = fread(c_addr, sizeof(Char), u1, (FILE *)wfileid);
 1856: if (u2>0)
 1857:    gf_regetc((FILE *)wfileid);
 1858: wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
 1859: /* !! is the value of ferror errno-compatible? */
 1860: if (wior)
 1861:   clearerr((FILE *)wfileid);
 1862: 
 1863: (read-line)	( c_addr u1 wfileid -- u2 flag u3 wior ) file	paren_read_line
 1864: struct Cellquad r = read_line(c_addr, u1, (FILE *)wfileid);
 1865: u2   = r.n1;
 1866: flag = r.n2;
 1867: u3   = r.n3;
 1868: wior = r.n4;
 1869: 
 1870: \+
 1871: 
 1872: write-file	( c_addr u1 wfileid -- wior )	file	write_file
 1873: /* !! fwrite does not guarantee enough */
 1874: #ifdef HAS_FILE
 1875: {
 1876:   UCell u2 = fwrite(c_addr, sizeof(Char), u1, (FILE *)wfileid);
 1877:   wior = FILEIO(u2<u1 && ferror((FILE *)wfileid));
 1878:   if (wior)
 1879:     clearerr((FILE *)wfileid);
 1880: }
 1881: #else
 1882: TYPE(c_addr, u1);
 1883: #endif
 1884: 
 1885: emit-file	( c wfileid -- wior )	gforth	emit_file
 1886: #ifdef HAS_FILE
 1887: wior = FILEIO(putc(c, (FILE *)wfileid)==EOF);
 1888: if (wior)
 1889:   clearerr((FILE *)wfileid);
 1890: #else
 1891: PUTC(c);
 1892: #endif
 1893: 
 1894: \+file
 1895: 
 1896: flush-file	( wfileid -- wior )		file-ext	flush_file
 1897: wior = IOR(fflush((FILE *) wfileid)==EOF);
 1898: 
 1899: file-status	( c_addr u -- wfam wior )	file-ext	file_status
 1900: struct Cellpair r = file_status(c_addr, u);
 1901: wfam = r.n1;
 1902: wior = r.n2;
 1903: 
 1904: file-eof?	( wfileid -- flag )	gforth	file_eof_query
 1905: flag = FLAG(feof((FILE *) wfileid));
 1906: 
 1907: open-dir	( c_addr u -- wdirid wior )	gforth	open_dir
 1908: ""Open the directory specified by @i{c-addr, u}
 1909: and return @i{dir-id} for futher access to it.""
 1910: wdirid = (Cell)opendir(tilde_cstr(c_addr, u, 1));
 1911: wior =  IOR(wdirid == 0);
 1912: 
 1913: read-dir	( c_addr u1 wdirid -- u2 flag wior )	gforth	read_dir
 1914: ""Attempt to read the next entry from the directory specified
 1915: by @i{dir-id} to the buffer of length @i{u1} at address @i{c-addr}. 
 1916: If the attempt fails because there is no more entries,
 1917: @i{ior}=0, @i{flag}=0, @i{u2}=0, and the buffer is unmodified.
 1918: If the attempt to read the next entry fails because of any other reason, 
 1919: return @i{ior}<>0.
 1920: If the attempt succeeds, store file name to the buffer at @i{c-addr}
 1921: and return @i{ior}=0, @i{flag}=true and @i{u2} equal to the size of the file name.
 1922: If the length of the file name is greater than @i{u1}, 
 1923: store first @i{u1} characters from file name into the buffer and
 1924: indicate "name too long" with @i{ior}, @i{flag}=true, and @i{u2}=@i{u1}.""
 1925: struct dirent * dent;
 1926: dent = readdir((DIR *)wdirid);
 1927: wior = 0;
 1928: flag = -1;
 1929: if(dent == NULL) {
 1930:   u2 = 0;
 1931:   flag = 0;
 1932: } else {
 1933:   u2 = strlen((char *)dent->d_name);
 1934:   if(u2 > u1) {
 1935:     u2 = u1;
 1936:     wior = -512-ENAMETOOLONG;
 1937:   }
 1938:   memmove(c_addr, dent->d_name, u2);
 1939: }
 1940: 
 1941: close-dir	( wdirid -- wior )	gforth	close_dir
 1942: ""Close the directory specified by @i{dir-id}.""
 1943: wior = IOR(closedir((DIR *)wdirid));
 1944: 
 1945: filename-match	( c_addr1 u1 c_addr2 u2 -- flag )	gforth	match_file
 1946: char * string = cstr(c_addr1, u1, 1);
 1947: char * pattern = cstr(c_addr2, u2, 0);
 1948: flag = FLAG(!fnmatch(pattern, string, 0));
 1949: 
 1950: set-dir	( c_addr u -- wior )	gforth set_dir
 1951: ""Change the current directory to @i{c-addr, u}.
 1952: Return an error if this is not possible""
 1953: wior = IOR(chdir(tilde_cstr(c_addr, u, 1)));
 1954: 
 1955: get-dir	( c_addr1 u1 -- c_addr2 u2 )	gforth get_dir
 1956: ""Store the current directory in the buffer specified by @i{c-addr1, u1}.
 1957: If the buffer size is not sufficient, return 0 0""
 1958: c_addr2 = (Char *)getcwd((char *)c_addr1, u1);
 1959: if(c_addr2 != NULL) {
 1960:   u2 = strlen((char *)c_addr2);
 1961: } else {
 1962:   u2 = 0;
 1963: }
 1964: 
 1965: =mkdir ( c_addr u wmode -- wior )        gforth equals_mkdir
 1966: ""Create directory @i{c-addr u} with mode @i{wmode}.""
 1967: wior = IOR(mkdir(tilde_cstr(c_addr,u,1),wmode));
 1968: 
 1969: \+
 1970: 
 1971: newline	( -- c_addr u )	gforth
 1972: ""String containing the newline sequence of the host OS""
 1973: char newline[] = {
 1974: #if DIRSEP=='/'
 1975: /* Unix */
 1976: '\n'
 1977: #else
 1978: /* DOS, Win, OS/2 */
 1979: '\r','\n'
 1980: #endif
 1981: };
 1982: c_addr=(Char *)newline;
 1983: u=sizeof(newline);
 1984: :
 1985:  "newline count ;
 1986: Create "newline e? crlf [IF] 2 c, $0D c, [ELSE] 1 c, [THEN] $0A c,
 1987: 
 1988: \+os
 1989: 
 1990: utime	( -- dtime )	gforth
 1991: ""Report the current time in microseconds since some epoch.""
 1992: struct timeval time1;
 1993: gettimeofday(&time1,NULL);
 1994: dtime = timeval2us(&time1);
 1995: 
 1996: cputime ( -- duser dsystem ) gforth
 1997: ""duser and dsystem are the respective user- and system-level CPU
 1998: times used since the start of the Forth system (excluding child
 1999: processes), in microseconds (the granularity may be much larger,
 2000: however).  On platforms without the getrusage call, it reports elapsed
 2001: time (since some epoch) for duser and 0 for dsystem.""
 2002: #ifdef HAVE_GETRUSAGE
 2003: struct rusage usage;
 2004: getrusage(RUSAGE_SELF, &usage);
 2005: duser = timeval2us(&usage.ru_utime);
 2006: dsystem = timeval2us(&usage.ru_stime);
 2007: #else
 2008: struct timeval time1;
 2009: gettimeofday(&time1,NULL);
 2010: duser = timeval2us(&time1);
 2011: dsystem = DZERO;
 2012: #endif
 2013: 
 2014: \+
 2015: 
 2016: \+floating
 2017: 
 2018: \g floating
 2019: 
 2020: comparisons(f, r1 r2, f_, r1, r2, gforth, gforth, float, gforth)
 2021: comparisons(f0, r, f_zero_, r, 0., float, gforth, float, gforth)
 2022: 
 2023: s>f	( n -- r )		float	s_to_f
 2024: r = n;
 2025: 
 2026: d>f	( d -- r )		float	d_to_f
 2027: #ifdef BUGGY_LL_D2F
 2028: extern double ldexp(double x, int exp);
 2029: if (DHI(d)<0) {
 2030: #ifdef BUGGY_LL_ADD
 2031:   DCell d2=dnegate(d);
 2032: #else
 2033:   DCell d2=-d;
 2034: #endif
 2035:   r = -(ldexp((Float)DHI(d2),CELL_BITS) + (Float)DLO(d2));
 2036: } else
 2037:   r = ldexp((Float)DHI(d),CELL_BITS) + (Float)DLO(d);
 2038: #else
 2039: r = d;
 2040: #endif
 2041: 
 2042: f>d	( r -- d )		float	f_to_d
 2043: extern DCell double2ll(Float r);
 2044: d = double2ll(r);
 2045: 
 2046: f>s	( r -- n )		float	f_to_s
 2047: n = (Cell)r;
 2048: 
 2049: f!	( r f_addr -- )	float	f_store
 2050: ""Store @i{r} into the float at address @i{f-addr}.""
 2051: *f_addr = r;
 2052: 
 2053: f@	( f_addr -- r )	float	f_fetch
 2054: ""@i{r} is the float at address @i{f-addr}.""
 2055: r = *f_addr;
 2056: 
 2057: df@	( df_addr -- r )	float-ext	d_f_fetch
 2058: ""Fetch the double-precision IEEE floating-point value @i{r} from the address @i{df-addr}.""
 2059: #ifdef IEEE_FP
 2060: r = *df_addr;
 2061: #else
 2062: !! df@
 2063: #endif
 2064: 
 2065: df!	( r df_addr -- )	float-ext	d_f_store
 2066: ""Store @i{r} as double-precision IEEE floating-point value to the
 2067: address @i{df-addr}.""
 2068: #ifdef IEEE_FP
 2069: *df_addr = r;
 2070: #else
 2071: !! df!
 2072: #endif
 2073: 
 2074: sf@	( sf_addr -- r )	float-ext	s_f_fetch
 2075: ""Fetch the single-precision IEEE floating-point value @i{r} from the address @i{sf-addr}.""
 2076: #ifdef IEEE_FP
 2077: r = *sf_addr;
 2078: #else
 2079: !! sf@
 2080: #endif
 2081: 
 2082: sf!	( r sf_addr -- )	float-ext	s_f_store
 2083: ""Store @i{r} as single-precision IEEE floating-point value to the
 2084: address @i{sf-addr}.""
 2085: #ifdef IEEE_FP
 2086: *sf_addr = r;
 2087: #else
 2088: !! sf!
 2089: #endif
 2090: 
 2091: f+	( r1 r2 -- r3 )	float	f_plus
 2092: r3 = r1+r2;
 2093: 
 2094: f-	( r1 r2 -- r3 )	float	f_minus
 2095: r3 = r1-r2;
 2096: 
 2097: f*	( r1 r2 -- r3 )	float	f_star
 2098: r3 = r1*r2;
 2099: 
 2100: f/	( r1 r2 -- r3 )	float	f_slash
 2101: r3 = r1/r2;
 2102: 
 2103: f**	( r1 r2 -- r3 )	float-ext	f_star_star
 2104: ""@i{r3} is @i{r1} raised to the @i{r2}th power.""
 2105: r3 = pow(r1,r2);
 2106: 
 2107: fm*	( r1 n -- r2 )	gforth	fm_star
 2108: r2 = r1*n;
 2109: 
 2110: fm/	( r1 n -- r2 )	gforth	fm_slash
 2111: r2 = r1/n;
 2112: 
 2113: fm*/	( r1 n1 n2 -- r2 )	gforth	fm_star_slash
 2114: r2 = (r1*n1)/n2;
 2115: 
 2116: f**2	( r1 -- r2 )	gforth	fm_square
 2117: r2 = r1*r1;
 2118: 
 2119: fnegate	( r1 -- r2 )	float	f_negate
 2120: r2 = - r1;
 2121: 
 2122: fdrop	( r -- )		float	f_drop
 2123: 
 2124: fdup	( r -- r r )	float	f_dupe
 2125: 
 2126: fswap	( r1 r2 -- r2 r1 )	float	f_swap
 2127: 
 2128: fover	( r1 r2 -- r1 r2 r1 )	float	f_over
 2129: 
 2130: frot	( r1 r2 r3 -- r2 r3 r1 )	float	f_rote
 2131: 
 2132: fnip	( r1 r2 -- r2 )	gforth	f_nip
 2133: 
 2134: ftuck	( r1 r2 -- r2 r1 r2 )	gforth	f_tuck
 2135: 
 2136: float+	( f_addr1 -- f_addr2 )	float	float_plus
 2137: ""@code{1 floats +}.""
 2138: f_addr2 = f_addr1+1;
 2139: 
 2140: floats	( n1 -- n2 )	float
 2141: ""@i{n2} is the number of address units of @i{n1} floats.""
 2142: n2 = n1*sizeof(Float);
 2143: 
 2144: floor	( r1 -- r2 )	float
 2145: ""Round towards the next smaller integral value, i.e., round toward negative infinity.""
 2146: /* !! unclear wording */
 2147: r2 = floor(r1);
 2148: 
 2149: fround	( r1 -- r2 )	float	f_round
 2150: ""Round to the nearest integral value.""
 2151: r2 = rint(r1);
 2152: 
 2153: fmax	( r1 r2 -- r3 )	float	f_max
 2154: if (r1<r2)
 2155:   r3 = r2;
 2156: else
 2157:   r3 = r1;
 2158: 
 2159: fmin	( r1 r2 -- r3 )	float	f_min
 2160: if (r1<r2)
 2161:   r3 = r1;
 2162: else
 2163:   r3 = r2;
 2164: 
 2165: represent	( r c_addr u -- n f1 f2 )	float
 2166: char *sig;
 2167: size_t siglen;
 2168: int flag;
 2169: int decpt;
 2170: sig=ecvt(r, u, &decpt, &flag);
 2171: n=(r==0. ? 1 : decpt);
 2172: flag=signbit(r); /* not all ecvt()s do this as desired */
 2173: f1=FLAG(flag!=0);
 2174: f2=FLAG(isdigit((unsigned)(sig[0]))!=0);
 2175: siglen=strlen((char *)sig);
 2176: if (siglen>u) /* happens in glibc-2.1.3 if 999.. is rounded up */
 2177:   siglen=u;
 2178: if (!f2) /* workaround Cygwin trailing 0s for Inf and Nan */
 2179:   for (; sig[siglen-1]=='0'; siglen--);
 2180:     ;
 2181: memcpy(c_addr,sig,siglen);
 2182: memset(c_addr+siglen,f2?'0':' ',u-siglen);
 2183: 
 2184: >float	( c_addr u -- f:... flag )	float	to_float
 2185: ""Actual stack effect: ( c_addr u -- r t | f ).  Attempt to convert the
 2186: character string @i{c-addr u} to internal floating-point
 2187: representation. If the string represents a valid floating-point number
 2188: @i{r} is placed on the floating-point stack and @i{flag} is
 2189: true. Otherwise, @i{flag} is false. A string of blanks is a special
 2190: case and represents the floating-point number 0.""
 2191: Float r;
 2192: flag = to_float(c_addr, u, &r);
 2193: if (flag) {
 2194:   fp--;
 2195:   fp[0]=r;
 2196: }
 2197: 
 2198: fabs	( r1 -- r2 )	float-ext	f_abs
 2199: r2 = fabs(r1);
 2200: 
 2201: facos	( r1 -- r2 )	float-ext	f_a_cos
 2202: r2 = acos(r1);
 2203: 
 2204: fasin	( r1 -- r2 )	float-ext	f_a_sine
 2205: r2 = asin(r1);
 2206: 
 2207: fatan	( r1 -- r2 )	float-ext	f_a_tan
 2208: r2 = atan(r1);
 2209: 
 2210: fatan2	( r1 r2 -- r3 )	float-ext	f_a_tan_two
 2211: ""@i{r1/r2}=tan(@i{r3}). ANS Forth does not require, but probably
 2212: intends this to be the inverse of @code{fsincos}. In gforth it is.""
 2213: r3 = atan2(r1,r2);
 2214: 
 2215: fcos	( r1 -- r2 )	float-ext	f_cos
 2216: r2 = cos(r1);
 2217: 
 2218: fexp	( r1 -- r2 )	float-ext	f_e_x_p
 2219: r2 = exp(r1);
 2220: 
 2221: fexpm1	( r1 -- r2 )	float-ext	f_e_x_p_m_one
 2222: ""@i{r2}=@i{e}**@i{r1}@minus{}1""
 2223: #ifdef HAVE_EXPM1
 2224: extern double
 2225: #ifdef NeXT
 2226:               const
 2227: #endif
 2228:                     expm1(double);
 2229: r2 = expm1(r1);
 2230: #else
 2231: r2 = exp(r1)-1.;
 2232: #endif
 2233: 
 2234: fln	( r1 -- r2 )	float-ext	f_l_n
 2235: r2 = log(r1);
 2236: 
 2237: flnp1	( r1 -- r2 )	float-ext	f_l_n_p_one
 2238: ""@i{r2}=ln(@i{r1}+1)""
 2239: #ifdef HAVE_LOG1P
 2240: extern double
 2241: #ifdef NeXT
 2242:               const
 2243: #endif
 2244:                     log1p(double);
 2245: r2 = log1p(r1);
 2246: #else
 2247: r2 = log(r1+1.);
 2248: #endif
 2249: 
 2250: flog	( r1 -- r2 )	float-ext	f_log
 2251: ""The decimal logarithm.""
 2252: r2 = log10(r1);
 2253: 
 2254: falog	( r1 -- r2 )	float-ext	f_a_log
 2255: ""@i{r2}=10**@i{r1}""
 2256: extern double pow10(double);
 2257: r2 = pow10(r1);
 2258: 
 2259: fsin	( r1 -- r2 )	float-ext	f_sine
 2260: r2 = sin(r1);
 2261: 
 2262: fsincos	( r1 -- r2 r3 )	float-ext	f_sine_cos
 2263: ""@i{r2}=sin(@i{r1}), @i{r3}=cos(@i{r1})""
 2264: r2 = sin(r1);
 2265: r3 = cos(r1);
 2266: 
 2267: fsqrt	( r1 -- r2 )	float-ext	f_square_root
 2268: r2 = sqrt(r1);
 2269: 
 2270: ftan	( r1 -- r2 )	float-ext	f_tan
 2271: r2 = tan(r1);
 2272: :
 2273:  fsincos f/ ;
 2274: 
 2275: fsinh	( r1 -- r2 )	float-ext	f_cinch
 2276: r2 = sinh(r1);
 2277: :
 2278:  fexpm1 fdup fdup 1. d>f f+ f/ f+ f2/ ;
 2279: 
 2280: fcosh	( r1 -- r2 )	float-ext	f_cosh
 2281: r2 = cosh(r1);
 2282: :
 2283:  fexp fdup 1/f f+ f2/ ;
 2284: 
 2285: ftanh	( r1 -- r2 )	float-ext	f_tan_h
 2286: r2 = tanh(r1);
 2287: :
 2288:  f2* fexpm1 fdup 2. d>f f+ f/ ;
 2289: 
 2290: fasinh	( r1 -- r2 )	float-ext	f_a_cinch
 2291: r2 = asinh(r1);
 2292: :
 2293:  fdup fdup f* 1. d>f f+ fsqrt f/ fatanh ;
 2294: 
 2295: facosh	( r1 -- r2 )	float-ext	f_a_cosh
 2296: r2 = acosh(r1);
 2297: :
 2298:  fdup fdup f* 1. d>f f- fsqrt f+ fln ;
 2299: 
 2300: fatanh	( r1 -- r2 )	float-ext	f_a_tan_h
 2301: r2 = atanh(r1);
 2302: :
 2303:  fdup f0< >r fabs 1. d>f fover f- f/  f2* flnp1 f2/
 2304:  r> IF  fnegate  THEN ;
 2305: 
 2306: sfloats	( n1 -- n2 )	float-ext	s_floats
 2307: ""@i{n2} is the number of address units of @i{n1}
 2308: single-precision IEEE floating-point numbers.""
 2309: n2 = n1*sizeof(SFloat);
 2310: 
 2311: dfloats	( n1 -- n2 )	float-ext	d_floats
 2312: ""@i{n2} is the number of address units of @i{n1}
 2313: double-precision IEEE floating-point numbers.""
 2314: n2 = n1*sizeof(DFloat);
 2315: 
 2316: sfaligned	( c_addr -- sf_addr )	float-ext	s_f_aligned
 2317: ""@i{sf-addr} is the first single-float-aligned address greater
 2318: than or equal to @i{c-addr}.""
 2319: sf_addr = (SFloat *)((((Cell)c_addr)+(sizeof(SFloat)-1))&(-sizeof(SFloat)));
 2320: :
 2321:  [ 1 sfloats 1- ] Literal + [ -1 sfloats ] Literal and ;
 2322: 
 2323: dfaligned	( c_addr -- df_addr )	float-ext	d_f_aligned
 2324: ""@i{df-addr} is the first double-float-aligned address greater
 2325: than or equal to @i{c-addr}.""
 2326: df_addr = (DFloat *)((((Cell)c_addr)+(sizeof(DFloat)-1))&(-sizeof(DFloat)));
 2327: :
 2328:  [ 1 dfloats 1- ] Literal + [ -1 dfloats ] Literal and ;
 2329: 
 2330: v*	( f_addr1 nstride1 f_addr2 nstride2 ucount -- r ) gforth v_star
 2331: ""dot-product: r=v1*v2.  The first element of v1 is at f_addr1, the
 2332: next at f_addr1+nstride1 and so on (similar for v2). Both vectors have
 2333: ucount elements.""
 2334: r = v_star(f_addr1, nstride1, f_addr2, nstride2, ucount);
 2335: :
 2336:  >r swap 2swap swap 0e r> 0 ?DO
 2337:      dup f@ over + 2swap dup f@ f* f+ over + 2swap
 2338:  LOOP 2drop 2drop ; 
 2339: 
 2340: faxpy	( ra f_x nstridex f_y nstridey ucount -- )	gforth
 2341: ""vy=ra*vx+vy""
 2342: faxpy(ra, f_x, nstridex, f_y, nstridey, ucount);
 2343: :
 2344:  >r swap 2swap swap r> 0 ?DO
 2345:      fdup dup f@ f* over + 2swap dup f@ f+ dup f! over + 2swap
 2346:  LOOP 2drop 2drop fdrop ;
 2347: 
 2348: \+
 2349: 
 2350: \ The following words access machine/OS/installation-dependent
 2351: \   Gforth internals
 2352: \ !! how about environmental queries DIRECT-THREADED,
 2353: \   INDIRECT-THREADED, TOS-CACHED, FTOS-CACHED, CODEFIELD-DOES */
 2354: 
 2355: \ local variable implementation primitives
 2356: 
 2357: \+glocals
 2358: 
 2359: \g locals
 2360: 
 2361: @local#	( #noffset -- w )	gforth	fetch_local_number
 2362: w = *(Cell *)(lp+noffset);
 2363: 
 2364: @local0	( -- w )	new	fetch_local_zero
 2365: w = ((Cell *)lp)[0];
 2366: 
 2367: @local1	( -- w )	new	fetch_local_four
 2368: w = ((Cell *)lp)[1];
 2369: 
 2370: @local2	( -- w )	new	fetch_local_eight
 2371: w = ((Cell *)lp)[2];
 2372: 
 2373: @local3	( -- w )	new	fetch_local_twelve
 2374: w = ((Cell *)lp)[3];
 2375: 
 2376: \+floating
 2377: 
 2378: f@local#	( #noffset -- r )	gforth	f_fetch_local_number
 2379: r = *(Float *)(lp+noffset);
 2380: 
 2381: f@local0	( -- r )	new	f_fetch_local_zero
 2382: r = ((Float *)lp)[0];
 2383: 
 2384: f@local1	( -- r )	new	f_fetch_local_eight
 2385: r = ((Float *)lp)[1];
 2386: 
 2387: \+
 2388: 
 2389: laddr#	( #noffset -- c_addr )	gforth	laddr_number
 2390: /* this can also be used to implement lp@ */
 2391: c_addr = (Char *)(lp+noffset);
 2392: 
 2393: lp+!#	( #noffset -- )	gforth	lp_plus_store_number
 2394: ""used with negative immediate values it allocates memory on the
 2395: local stack, a positive immediate argument drops memory from the local
 2396: stack""
 2397: lp += noffset;
 2398: 
 2399: lp-	( -- )	new	minus_four_lp_plus_store
 2400: lp += -sizeof(Cell);
 2401: 
 2402: lp+	( -- )	new	eight_lp_plus_store
 2403: lp += sizeof(Float);
 2404: 
 2405: lp+2	( -- )	new	sixteen_lp_plus_store
 2406: lp += 2*sizeof(Float);
 2407: 
 2408: lp!	( c_addr -- )	gforth	lp_store
 2409: lp = (Address)c_addr;
 2410: 
 2411: >l	( w -- )	gforth	to_l
 2412: lp -= sizeof(Cell);
 2413: *(Cell *)lp = w;
 2414: 
 2415: \+floating
 2416: 
 2417: f>l	( r -- )	gforth	f_to_l
 2418: lp -= sizeof(Float);
 2419: *(Float *)lp = r;
 2420: 
 2421: fpick	( f:... u -- f:... r )		gforth
 2422: ""Actually the stack effect is @code{ r0 ... ru u -- r0 ... ru r0 }.""
 2423: r = fp[u];
 2424: :
 2425:  floats fp@ + f@ ;
 2426: 
 2427: \+
 2428: \+
 2429: 
 2430: \+OS
 2431: 
 2432: \g syslib
 2433: 
 2434: open-lib	( c_addr1 u1 -- u2 )	gforth	open_lib
 2435: u2 = gforth_dlopen(c_addr1, u1);
 2436: 
 2437: lib-sym	( c_addr1 u1 u2 -- u3 )	gforth	lib_sym
 2438: #ifdef HAVE_LIBLTDL
 2439: u3 = (UCell) lt_dlsym((lt_dlhandle)u2, cstr(c_addr1, u1, 1));
 2440: #elif defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
 2441: u3 = (UCell) dlsym((void*)u2,cstr(c_addr1, u1, 1));
 2442: #else
 2443: #  ifdef _WIN32
 2444: u3 = (Cell) GetProcAddress((HMODULE)u2, cstr(c_addr1, u1, 1));
 2445: #  else
 2446: #warning Define lib-sym!
 2447: u3 = 0;
 2448: #  endif
 2449: #endif
 2450: 
 2451: wcall	( ... u -- ... )	gforth
 2452: gforth_FP=fp;
 2453: sp=(Cell*)(SYSCALL(Cell*(*)(Cell *, void *))u)(sp, &gforth_FP);
 2454: fp=gforth_FP;
 2455: 
 2456: uw@ ( c_addr -- u )	gforth u_w_fetch
 2457: ""@i{u} is the zero-extended 16-bit value stored at @i{c_addr}.""
 2458: u = *(UWyde*)(c_addr);
 2459: 
 2460: sw@ ( c_addr -- n )	gforth s_w_fetch
 2461: ""@i{n} is the sign-extended 16-bit value stored at @i{c_addr}.""
 2462: n = *(Wyde*)(c_addr);
 2463: 
 2464: w! ( w c_addr -- )	gforth w_store
 2465: ""Store the bottom 16 bits of @i{w} at @i{c_addr}.""
 2466: *(Wyde*)(c_addr) = w;
 2467: 
 2468: ul@ ( c_addr -- u )	gforth u_l_fetch
 2469: ""@i{u} is the zero-extended 32-bit value stored at @i{c_addr}.""
 2470: u = *(UTetrabyte*)(c_addr);
 2471: 
 2472: sl@ ( c_addr -- n )	gforth s_l_fetch
 2473: ""@i{n} is the sign-extended 32-bit value stored at @i{c_addr}.""
 2474: n = *(Tetrabyte*)(c_addr);
 2475: 
 2476: l! ( w c_addr -- )	gforth l_store
 2477: ""Store the bottom 32 bits of @i{w} at @i{c_addr}.""
 2478: *(Tetrabyte*)(c_addr) = w;
 2479: 
 2480: lib-error ( -- c_addr u )       gforth  lib_error
 2481: ""Error message for last failed @code{open-lib} or @code{lib-sym}.""
 2482: #ifdef HAVE_LIBLTDL
 2483: c_addr = (Char *)lt_dlerror();
 2484: u = (c_addr == NULL) ? 0 : strlen((char *)c_addr);
 2485: #elif defined(HAVE_LIBDL) || defined(HAVE_DLOPEN)
 2486: c_addr = dlerror();
 2487: u = strlen(c_addr);
 2488: #else
 2489: c_addr = "libltdl is not configured";
 2490: u = strlen(c_addr);
 2491: #endif
 2492: 
 2493: \+
 2494: \g peephole
 2495: 
 2496: \+peephole
 2497: 
 2498: compile-prim1 ( a_prim -- ) gforth compile_prim1
 2499: ""compile prim (incl. immargs) at @var{a_prim}""
 2500: compile_prim1(a_prim);
 2501: 
 2502: finish-code ( ... -- ... ) gforth finish_code
 2503: ""Perform delayed steps in code generation (branch resolution, I-cache
 2504: flushing).""
 2505: /* The ... above are a workaround for a bug in gcc-2.95, which fails
 2506:    to save spTOS (gforth-fast --enable-force-reg) */
 2507: finish_code();
 2508: 
 2509: forget-dyncode ( c_code -- f ) gforth-internal forget_dyncode
 2510: f = forget_dyncode(c_code);
 2511: 
 2512: decompile-prim ( a_code -- a_prim ) gforth-internal decompile_prim
 2513: ""a_prim is the code address of the primitive that has been
 2514: compile_prim1ed to a_code""
 2515: a_prim = (Cell *)decompile_code((Label)a_code);
 2516: 
 2517: \ set-next-code and call2 do not appear in images and can be
 2518: \ renumbered arbitrarily
 2519: 
 2520: set-next-code ( #w -- ) gforth set_next_code
 2521: #ifdef NO_IP
 2522: next_code = (Label)w;
 2523: #endif
 2524: 
 2525: call2 ( #a_callee #a_ret_addr -- R:a_ret_addr ) gforth
 2526: /* call with explicit return address */
 2527: #ifdef NO_IP
 2528: INST_TAIL;
 2529: JUMP(a_callee);
 2530: #else
 2531: assert(0);
 2532: #endif
 2533: 
 2534: tag-offsets ( -- a_addr ) gforth tag_offsets
 2535: extern Cell groups[32];
 2536: a_addr = groups;
 2537: 
 2538: \+
 2539: 
 2540: \g static_super
 2541: 
 2542: ifdef(`STACK_CACHE_FILE',
 2543: `include(peeprules.vmg)')
 2544: 
 2545: \g end

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